Bug 24986: DBRev 19.05.12.001
[koha.git] / tools / batch_extend_due_dates.pl
blob46f97a25bb045678b57bf2d86fc5e988cfb5c9d6
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright 2020 Koha Development Team
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.
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.
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>
21 use Modern::Perl;
23 use CGI;
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Items qw( ModItem );
28 use Koha::Checkouts;
29 use Koha::DateUtils qw( dt_from_string output_pref );
31 my $input = new CGI;
32 my $op = $input->param('op') // q|form|;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36 template_name => 'tools/batch_extend_due_dates.tt',
37 query => $input,
38 type => "intranet",
39 authnotrequired => 0,
40 flagsrequired => { tools => 'batch_extend_due_dates' },
44 if ( $op eq 'form' ) {
45 $template->param( view => 'form', );
47 elsif ( $op eq 'list' ) {
49 my @categorycodes = $input->multi_param('categorycodes');
50 my @branchcodes = $input->multi_param('branchcodes');
51 my $from_due_date = $input->param('from_due_date');
52 my $to_due_date = $input->param('to_due_date');
53 my $new_hard_due_date = $input->param('new_hard_due_date');
54 my $due_date_days = $input->param('due_date_days');
56 $new_hard_due_date &&= dt_from_string($new_hard_due_date);
58 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
59 my $search_params;
60 if (@categorycodes) {
61 $search_params->{'borrower.categorycode'} = { -in => \@categorycodes };
63 if (@branchcodes) {
64 $search_params->{'me.branchcode'} = { -in => \@branchcodes };
66 if ( $from_due_date and $to_due_date ) {
67 my $to_due_date_endday = dt_from_string($to_due_date);
68 $to_due_date_endday
69 ->set( # We set last second of day to see all checkouts from that day
70 hour => 23,
71 minute => 59,
72 second => 59
74 $search_params->{'me.date_due'} = {
75 -between => [
76 $dtf->format_datetime( dt_from_string($from_due_date) ),
77 $dtf->format_datetime($to_due_date_endday),
81 elsif ($from_due_date) {
82 $search_params->{'me.date_due'} =
83 { '>=' => $dtf->format_datetime( dt_from_string($from_due_date) ) };
85 elsif ($to_due_date) {
86 my $to_due_date_endday = dt_from_string($to_due_date);
87 $to_due_date_endday
88 ->set( # We set last second of day to see all checkouts from that day
89 hour => 23,
90 minute => 59,
91 second => 59
93 $search_params->{'me.date_due'} =
94 { '<=' => $dtf->format_datetime($to_due_date_endday) };
97 my $checkouts = Koha::Checkouts->search(
98 $search_params,
100 join => [ 'item', 'borrower' ]
104 my @new_due_dates;
105 while ( my $checkout = $checkouts->next ) {
106 push @new_due_dates,
107 output_pref({ dt => calc_new_due_date(
109 due_date => dt_from_string($checkout->date_due),
110 new_hard_due_date => $new_hard_due_date,
111 add_days => $due_date_days
113 ), dateformat => 'iso' });
116 $template->param(
117 checkouts => $checkouts,
118 new_hard_due_date => $new_hard_due_date
119 ? dt_from_string($new_hard_due_date)
120 : undef,
121 due_date_days => $due_date_days,
122 new_due_dates => \@new_due_dates,
123 view => 'list',
126 elsif ( $op eq 'modify' ) {
128 # We want to modify selected checkouts!
129 my @issue_ids = $input->multi_param('issue_id');
130 my $new_hard_due_date = $input->param('new_hard_due_date');
131 my $due_date_days = $input->param('due_date_days');
133 $new_hard_due_date &&= dt_from_string($new_hard_due_date);
134 my $checkouts =
135 Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );
136 while ( my $checkout = $checkouts->next ) {
137 my $new_due_date = calc_new_due_date(
139 due_date => dt_from_string($checkout->date_due),
140 new_hard_due_date => $new_hard_due_date,
141 add_days => $due_date_days
145 # Update checkout's due date
146 $checkout->date_due($new_due_date)->store;
148 # Update items.onloan
149 ModItem( { onloan => $new_due_date->strftime('%Y-%m-%d %H:%M') },
150 undef, $checkout->itemnumber );
153 $template->param(
154 view => 'report',
155 checkouts => $checkouts,
159 sub calc_new_due_date {
160 my ($params) = @_;
161 my $due_date = $params->{due_date};
162 my $new_hard_due_date = $params->{new_hard_due_date};
163 my $add_days = $params->{add_days};
165 my $new;
166 if ( $new_hard_due_date ) {
167 $new = $new_hard_due_date->clone->set(
168 hour => $due_date->hour,
169 minute => $due_date->minute,
170 second => $due_date->second,
172 } else {
173 $new = $due_date->clone->add( days => $add_days );
175 return $new;
178 output_html_with_http_headers $input, $cookie, $template->output;