Bug 35782: Replace TT plugin's method Biblio::HoldsCount
[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 );
29 use Koha::Items;
30
31 my $input = CGI->new;
32 my $op = $input->param('op') // q|form|;
33 my $preview_results = $input->param('preview_results');
34
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name   => 'tools/batch_extend_due_dates.tt',
38         query           => $input,
39         type            => "intranet",
40         flagsrequired   => { tools => 'batch_extend_due_dates' },
41     }
42 );
43
44 my @issue_ids;
45
46 if ( $op eq 'form' ) {
47     $template->param( view => 'form', );
48 }
49 elsif ( $op eq 'list' ) {
50
51     my @categorycodes     = $input->multi_param('categorycodes');
52     my @itemtypecodes     = $input->multi_param('itemtypecodes');
53     my @branchcodes       = $input->multi_param('branchcodes');
54     my $from_due_date     = $input->param('from_due_date');
55     my $to_due_date       = $input->param('to_due_date');
56     my $new_hard_due_date = $input->param('new_hard_due_date');
57     my $due_date_days     = $input->param('due_date_days');
58
59     $new_hard_due_date &&= dt_from_string($new_hard_due_date);
60
61     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
62     my $search_params;
63     if (@categorycodes) {
64         $search_params->{'patron.categorycode'} = { -in => \@categorycodes };
65     }
66     if (@itemtypecodes) {
67         my $search_items->{'itype'} = { -in => \@itemtypecodes };
68         my @itemnumbers = Koha::Items->search($search_items)->get_column('itemnumber');
69
70         $search_params->{'itemnumber'} = { -in => \@itemnumbers };
71     }
72     if (@branchcodes) {
73         $search_params->{'me.branchcode'} = { -in => \@branchcodes };
74     }
75
76     if ( $from_due_date and $to_due_date ) {
77         my $to_due_date_endday = dt_from_string($to_due_date);
78         $to_due_date_endday
79           ->set(  # We set last second of day to see all checkouts from that day
80             hour   => 23,
81             minute => 59,
82             second => 59
83           );
84         $search_params->{'me.date_due'} = {
85             -between => [
86                 $dtf->format_datetime( dt_from_string($from_due_date) ),
87                 $dtf->format_datetime($to_due_date_endday),
88             ]
89         };
90     }
91     elsif ($from_due_date) {
92         $search_params->{'me.date_due'} =
93           { '>=' => $dtf->format_datetime( dt_from_string($from_due_date) ) };
94     }
95     elsif ($to_due_date) {
96         my $to_due_date_endday = dt_from_string($to_due_date);
97         $to_due_date_endday
98           ->set(  # We set last second of day to see all checkouts from that day
99             hour   => 23,
100             minute => 59,
101             second => 59
102           );
103         $search_params->{'me.date_due'} =
104           { '<=' => $dtf->format_datetime($to_due_date_endday) };
105     }
106
107     my $checkouts = Koha::Checkouts->search(
108         $search_params,
109         {
110             join => [ 'item', 'patron' ]
111         }
112     );
113
114     my @new_due_dates;
115     while ( my $checkout = $checkouts->next ) {
116         if ($preview_results) {
117             push(
118                 @new_due_dates,
119                 calc_new_due_date(
120                     {
121                         due_date => dt_from_string( $checkout->date_due ),
122                         new_hard_due_date => $new_hard_due_date,
123                         add_days          => $due_date_days
124                     }
125                 ),
126             );
127         } else {
128             push( @issue_ids, $checkout->id );
129         }
130     }
131
132     if ( $preview_results ) {
133         $template->param(
134             checkouts         => $checkouts,
135             new_hard_due_date => $new_hard_due_date,
136             due_date_days => $due_date_days,
137             new_due_dates => \@new_due_dates,
138             view          => 'list',
139         );
140     } else {
141         $op = 'cud-modify';
142     }
143 }
144
145 if ( $op eq 'cud-modify' ) {
146
147     # We want to modify selected checkouts!
148     my $new_hard_due_date = $input->param('new_hard_due_date');
149     my $due_date_days     = $input->param('due_date_days');
150
151     # @issue_ids will already be populated if we are skipping the results display
152     @issue_ids = $input->multi_param('issue_id') unless @issue_ids;
153
154     $new_hard_due_date &&= dt_from_string($new_hard_due_date);
155     my $checkouts =
156       Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );
157     while ( my $checkout = $checkouts->next ) {
158         my $new_due_date = calc_new_due_date(
159             {
160                 due_date          => dt_from_string($checkout->date_due),
161                 new_hard_due_date => $new_hard_due_date,
162                 add_days          => $due_date_days
163             }
164         );
165
166         # Update checkout's due date
167         $checkout->date_due($new_due_date)->store;
168
169         # Update items.onloan
170         $checkout->item->onloan($new_due_date)->store;
171     }
172
173     $template->param(
174         view      => 'report',
175         checkouts => $checkouts,
176     );
177 }
178
179 sub calc_new_due_date {
180     my ($params)          = @_;
181     my $due_date          = $params->{due_date};
182     my $new_hard_due_date = $params->{new_hard_due_date};
183     my $add_days          = $params->{add_days};
184
185     my $new;
186     if ( $new_hard_due_date ) {
187       $new = $new_hard_due_date->clone->set(
188         hour   => $due_date->hour,
189         minute => $due_date->minute,
190         second => $due_date->second,
191       )
192   } else {
193       $new = $due_date->clone->add( days => $add_days );
194   }
195   return $new;
196 }
197
198 output_html_with_http_headers $input, $cookie, $template->output;