Bug 23463: Remove DelItemCheck and ItemSafeToDelete
[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 C4::Items qw( ModItem );
28 use Koha::Checkouts;
29 use Koha::DateUtils qw( dt_from_string );
30
31 my $input = new CGI;
32 my $op = $input->param('op') // q|form|;
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         authnotrequired => 0,
40         flagsrequired   => { tools => 'batch_extend_due_dates' },
41     }
42 );
43
44 if ( $op eq 'form' ) {
45     $template->param( view => 'form', );
46 }
47 elsif ( $op eq 'list' ) {
48
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');
55
56     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
57     my $search_params;
58     if (@categorycodes) {
59         $search_params->{'borrower.categorycode'} = { -in => \@categorycodes };
60     }
61     if (@branchcodes) {
62         $search_params->{'me.branchcode'} = { -in => \@branchcodes };
63     }
64     if ( $from_due_date and $to_due_date ) {
65         my $to_due_date_endday = dt_from_string($to_due_date);
66         $to_due_date_endday
67           ->set(  # We set last second of day to see all checkouts from that day
68             hour   => 23,
69             minute => 59,
70             second => 59
71           );
72         $search_params->{'me.date_due'} = {
73             -between => [
74                 $dtf->format_datetime( dt_from_string($from_due_date) ),
75                 $dtf->format_datetime($to_due_date_endday),
76             ]
77         };
78     }
79     elsif ($from_due_date) {
80         $search_params->{'me.date_due'} =
81           { '>=' => $dtf->format_datetime( dt_from_string($from_due_date) ) };
82     }
83     elsif ($to_due_date) {
84         my $to_due_date_endday = dt_from_string($to_due_date);
85         $to_due_date_endday
86           ->set(  # We set last second of day to see all checkouts from that day
87             hour   => 23,
88             minute => 59,
89             second => 59
90           );
91         $search_params->{'me.date_due'} =
92           { '<=' => $dtf->format_datetime($to_due_date_endday) };
93     }
94
95     my $checkouts = Koha::Checkouts->search(
96         $search_params,
97         {
98             join => [ 'item', 'borrower' ]
99         }
100     );
101
102     my @new_due_dates;
103     if ( not $new_hard_due_date && $due_date_days ) {
104         while ( my $checkout = $checkouts->next ) {
105             my $due_date = dt_from_string( $checkout->date_due );
106             push @new_due_dates, $due_date->add( days => $due_date_days );
107         }
108     }
109     $template->param(
110         checkouts         => $checkouts,
111         new_hard_due_date => $new_hard_due_date
112         ? dt_from_string($new_hard_due_date)
113         : undef,
114         due_date_days => $due_date_days,
115         new_due_dates => \@new_due_dates,
116         view          => 'list',
117     );
118 }
119 elsif ( $op eq 'modify' ) {
120
121     # We want to modify selected checkouts!
122     my @issue_ids         = $input->multi_param('issue_id');
123     my $new_hard_due_date = $input->param('new_hard_due_date');
124     my $due_date_days     = $input->param('due_date_days');
125
126     $new_hard_due_date &&= dt_from_string($new_hard_due_date);
127     my $checkouts =
128       Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );
129     while ( my $checkout = $checkouts->next ) {
130         my $new_due_date = $new_hard_due_date
131           || dt_from_string( $checkout->date_due )->add( days => $due_date_days );
132
133         # Update checkout's due date
134         $checkout->date_due($new_due_date)->store;
135
136         # Update items.onloan
137         ModItem( { onloan => $new_due_date->strftime('%Y-%m-%d %H:%M') },
138             undef, $checkout->itemnumber );
139     }
140
141     $template->param(
142         view      => 'report',
143         checkouts => $checkouts,
144     );
145 }
146
147 output_html_with_http_headers $input, $cookie, $template->output;