Bug 25247: Only convert data ot objects when displaying on screen
[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 = new CGI;
31 my $op = $input->param('op') // q|form|;
32
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {
35         template_name   => 'tools/batch_extend_due_dates.tt',
36         query           => $input,
37         type            => "intranet",
38         authnotrequired => 0,
39         flagsrequired   => { tools => 'batch_extend_due_dates' },
40     }
41 );
42
43 if ( $op eq 'form' ) {
44     $template->param( view => 'form', );
45 }
46 elsif ( $op eq 'list' ) {
47
48     my @categorycodes     = $input->multi_param('categorycodes');
49     my @branchcodes       = $input->multi_param('branchcodes');
50     my $from_due_date     = $input->param('from_due_date');
51     my $to_due_date       = $input->param('to_due_date');
52     my $new_hard_due_date = $input->param('new_hard_due_date');
53     my $due_date_days     = $input->param('due_date_days');
54
55     $new_hard_due_date &&= dt_from_string($new_hard_due_date);
56
57     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
58     my $search_params;
59     if (@categorycodes) {
60         $search_params->{'borrower.categorycode'} = { -in => \@categorycodes };
61     }
62     if (@branchcodes) {
63         $search_params->{'me.branchcode'} = { -in => \@branchcodes };
64     }
65     if ( $from_due_date and $to_due_date ) {
66         my $to_due_date_endday = dt_from_string($to_due_date);
67         $to_due_date_endday
68           ->set(  # We set last second of day to see all checkouts from that day
69             hour   => 23,
70             minute => 59,
71             second => 59
72           );
73         $search_params->{'me.date_due'} = {
74             -between => [
75                 $dtf->format_datetime( dt_from_string($from_due_date) ),
76                 $dtf->format_datetime($to_due_date_endday),
77             ]
78         };
79     }
80     elsif ($from_due_date) {
81         $search_params->{'me.date_due'} =
82           { '>=' => $dtf->format_datetime( dt_from_string($from_due_date) ) };
83     }
84     elsif ($to_due_date) {
85         my $to_due_date_endday = dt_from_string($to_due_date);
86         $to_due_date_endday
87           ->set(  # We set last second of day to see all checkouts from that day
88             hour   => 23,
89             minute => 59,
90             second => 59
91           );
92         $search_params->{'me.date_due'} =
93           { '<=' => $dtf->format_datetime($to_due_date_endday) };
94     }
95
96     my $checkouts = Koha::Checkouts->search(
97         $search_params,
98         {
99             join => [ 'item', 'borrower' ]
100         }
101     );
102
103     my @new_due_dates;
104     while ( my $checkout = $checkouts->next ) {
105         push @new_due_dates,
106           output_pref({ dt => calc_new_due_date(
107             {
108                 due_date          => dt_from_string($checkout->date_due),
109                 new_hard_due_date => $new_hard_due_date,
110                 add_days          => $due_date_days
111             }
112           ), dateformat => 'iso' });
113     }
114
115     $template->param(
116         checkouts         => $checkouts,
117         new_hard_due_date => $new_hard_due_date
118         ? dt_from_string($new_hard_due_date)
119         : undef,
120         due_date_days => $due_date_days,
121         new_due_dates => \@new_due_dates,
122         view          => 'list',
123     );
124 }
125 elsif ( $op eq 'modify' ) {
126
127     # We want to modify selected checkouts!
128     my @issue_ids         = $input->multi_param('issue_id');
129     my $new_hard_due_date = $input->param('new_hard_due_date');
130     my $due_date_days     = $input->param('due_date_days');
131
132     $new_hard_due_date &&= dt_from_string($new_hard_due_date);
133     my $checkouts =
134       Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );
135     while ( my $checkout = $checkouts->next ) {
136         my $new_due_date = calc_new_due_date(
137             {
138                 due_date          => dt_from_string($checkout->date_due),
139                 new_hard_due_date => $new_hard_due_date,
140                 add_days          => $due_date_days
141             }
142         );
143
144         # Update checkout's due date
145         $checkout->date_due($new_due_date)->store;
146
147         # Update items.onloan
148         $checkout->item->onloan($new_due_date)->store;
149     }
150
151     $template->param(
152         view      => 'report',
153         checkouts => $checkouts,
154     );
155 }
156
157 sub calc_new_due_date {
158     my ($params)          = @_;
159     my $due_date          = $params->{due_date};
160     my $new_hard_due_date = $params->{new_hard_due_date};
161     my $add_days          = $params->{add_days};
162
163     my $new;
164     if ( $new_hard_due_date ) {
165       $new = $new_hard_due_date->clone->set(
166         hour   => $due_date->hour,
167         minute => $due_date->minute,
168         second => $due_date->second,
169       )
170   } else {
171       $new = $due_date->clone->add( days => $add_days );
172   }
173   return $new;
174 }
175
176 output_html_with_http_headers $input, $cookie, $template->output;