Bug 25898: Prohibit indirect object notation
[koha.git] / tools / batch_extend_due_dates.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2020 Koha Development Team
6 #
7 # Koha is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 3
10 # of the License, or (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General
18 # Public License along with Koha; if not, see
19 # <http://www.gnu.org/licenses>
20
21 use Modern::Perl;
22
23 use CGI;
24
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use Koha::Checkouts;
28 use Koha::DateUtils qw( dt_from_string output_pref );
29
30 my $input = CGI->new;
31 my $op = $input->param('op') // q|form|;
32 my $preview_results = $input->param('preview_results');
33
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => 'tools/batch_extend_due_dates.tt',
37         query           => $input,
38         type            => "intranet",
39         flagsrequired   => { tools => 'batch_extend_due_dates' },
40     }
41 );
42
43 my @issue_ids;
44
45 if ( $op eq 'form' ) {
46     $template->param( view => 'form', );
47 }
48 elsif ( $op eq 'list' ) {
49
50     my @categorycodes     = $input->multi_param('categorycodes');
51     my @branchcodes       = $input->multi_param('branchcodes');
52     my $from_due_date     = $input->param('from_due_date');
53     my $to_due_date       = $input->param('to_due_date');
54     my $new_hard_due_date = $input->param('new_hard_due_date');
55     my $due_date_days     = $input->param('due_date_days');
56
57     $new_hard_due_date &&= dt_from_string($new_hard_due_date);
58
59     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
60     my $search_params;
61     if (@categorycodes) {
62         $search_params->{'borrower.categorycode'} = { -in => \@categorycodes };
63     }
64     if (@branchcodes) {
65         $search_params->{'me.branchcode'} = { -in => \@branchcodes };
66     }
67     if ( $from_due_date and $to_due_date ) {
68         my $to_due_date_endday = dt_from_string($to_due_date);
69         $to_due_date_endday
70           ->set(  # We set last second of day to see all checkouts from that day
71             hour   => 23,
72             minute => 59,
73             second => 59
74           );
75         $search_params->{'me.date_due'} = {
76             -between => [
77                 $dtf->format_datetime( dt_from_string($from_due_date) ),
78                 $dtf->format_datetime($to_due_date_endday),
79             ]
80         };
81     }
82     elsif ($from_due_date) {
83         $search_params->{'me.date_due'} =
84           { '>=' => $dtf->format_datetime( dt_from_string($from_due_date) ) };
85     }
86     elsif ($to_due_date) {
87         my $to_due_date_endday = dt_from_string($to_due_date);
88         $to_due_date_endday
89           ->set(  # We set last second of day to see all checkouts from that day
90             hour   => 23,
91             minute => 59,
92             second => 59
93           );
94         $search_params->{'me.date_due'} =
95           { '<=' => $dtf->format_datetime($to_due_date_endday) };
96     }
97
98     my $checkouts = Koha::Checkouts->search(
99         $search_params,
100         {
101             join => [ 'item', 'borrower' ]
102         }
103     );
104
105     my @new_due_dates;
106     while ( my $checkout = $checkouts->next ) {
107         if ($preview_results) {
108             push(
109                 @new_due_dates,
110                 output_pref(
111                     {
112                         dt => calc_new_due_date(
113                             {
114                                 due_date =>
115                                   dt_from_string( $checkout->date_due ),
116                                 new_hard_due_date => $new_hard_due_date,
117                                 add_days          => $due_date_days
118                             }
119                         ),
120                         dateformat => 'iso'
121                     }
122                 )
123             );
124         } else {
125             push( @issue_ids, $checkout->id );
126         }
127     }
128
129     if ( $preview_results ) {
130         $template->param(
131             checkouts         => $checkouts,
132             new_hard_due_date => $new_hard_due_date
133             ? dt_from_string($new_hard_due_date)
134             : undef,
135             due_date_days => $due_date_days,
136             new_due_dates => \@new_due_dates,
137             view          => 'list',
138         );
139     } else {
140         $op = 'modify';
141     }
142 }
143
144 if ( $op eq 'modify' ) {
145
146     # We want to modify selected checkouts!
147     my $new_hard_due_date = $input->param('new_hard_due_date');
148     my $due_date_days     = $input->param('due_date_days');
149
150     # @issue_ids will already be populated if we are skipping the results display
151     @issue_ids = $input->multi_param('issue_id') unless @issue_ids;
152
153     $new_hard_due_date &&= dt_from_string($new_hard_due_date);
154     my $checkouts =
155       Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );
156     while ( my $checkout = $checkouts->next ) {
157         my $new_due_date = calc_new_due_date(
158             {
159                 due_date          => dt_from_string($checkout->date_due),
160                 new_hard_due_date => $new_hard_due_date,
161                 add_days          => $due_date_days
162             }
163         );
164
165         # Update checkout's due date
166         $checkout->date_due($new_due_date)->store;
167
168         # Update items.onloan
169         $checkout->item->onloan($new_due_date)->store;
170     }
171
172     $template->param(
173         view      => 'report',
174         checkouts => $checkouts,
175     );
176 }
177
178 sub calc_new_due_date {
179     my ($params)          = @_;
180     my $due_date          = $params->{due_date};
181     my $new_hard_due_date = $params->{new_hard_due_date};
182     my $add_days          = $params->{add_days};
183
184     my $new;
185     if ( $new_hard_due_date ) {
186       $new = $new_hard_due_date->clone->set(
187         hour   => $due_date->hour,
188         minute => $due_date->minute,
189         second => $due_date->second,
190       )
191   } else {
192       $new = $due_date->clone->add( days => $add_days );
193   }
194   return $new;
195 }
196
197 output_html_with_http_headers $input, $cookie, $template->output;