Bug 20415: Add POD
[koha.git] / tools / batch_extend_due_dates.pl
blobcaec5bd2302c84485d8ced6b79d834f79fa824f4
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 Koha::Checkouts;
28 use Koha::DateUtils qw( dt_from_string );
30 my $input = new CGI;
31 my $op = $input->param('op') // q|form|;
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
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' },
43 if ( $op eq 'form' ) {
44 $template->param( view => 'form', );
46 elsif ( $op eq 'list' ) {
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');
55 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
56 my $search_params;
57 if (@categorycodes) {
58 $search_params->{'borrower.categorycode'} = { -in => \@categorycodes };
60 if (@branchcodes) {
61 $search_params->{'me.branchcode'} = { -in => \@branchcodes };
63 if ( $from_due_date and $to_due_date ) {
64 my $to_due_date_endday = dt_from_string($to_due_date);
65 $to_due_date_endday
66 ->set( # We set last second of day to see all checkouts from that day
67 hour => 23,
68 minute => 59,
69 second => 59
71 $search_params->{'me.date_due'} = {
72 -between => [
73 $dtf->format_datetime( dt_from_string($from_due_date) ),
74 $dtf->format_datetime($to_due_date_endday),
78 elsif ($from_due_date) {
79 $search_params->{'me.date_due'} =
80 { '>=' => $dtf->format_datetime( dt_from_string($from_due_date) ) };
82 elsif ($to_due_date) {
83 my $to_due_date_endday = dt_from_string($to_due_date);
84 $to_due_date_endday
85 ->set( # We set last second of day to see all checkouts from that day
86 hour => 23,
87 minute => 59,
88 second => 59
90 $search_params->{'me.date_due'} =
91 { '<=' => $dtf->format_datetime($to_due_date_endday) };
94 my $checkouts = Koha::Checkouts->search(
95 $search_params,
97 join => [ 'item', 'borrower' ]
101 my @new_due_dates;
102 if ( not $new_hard_due_date && $due_date_days ) {
103 while ( my $checkout = $checkouts->next ) {
104 my $due_date = dt_from_string( $checkout->date_due );
105 push @new_due_dates, $due_date->add( days => $due_date_days );
108 $template->param(
109 checkouts => $checkouts,
110 new_hard_due_date => $new_hard_due_date
111 ? dt_from_string($new_hard_due_date)
112 : undef,
113 due_date_days => $due_date_days,
114 new_due_dates => \@new_due_dates,
115 view => 'list',
118 elsif ( $op eq 'modify' ) {
120 # We want to modify selected checkouts!
121 my @issue_ids = $input->multi_param('issue_id');
122 my $new_hard_due_date = $input->param('new_hard_due_date');
123 my $due_date_days = $input->param('due_date_days');
125 $new_hard_due_date &&= dt_from_string($new_hard_due_date);
126 my $checkouts =
127 Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );
128 while ( my $checkout = $checkouts->next ) {
129 my $new_due_date = $new_hard_due_date
130 || dt_from_string( $checkout->date_due )->add( days => $due_date_days );
132 # Update checkout's due date
133 $checkout->date_due($new_due_date)->store;
135 # Update items.onloan
136 $checkout->item->onloan($new_due_date)->store;
139 $template->param(
140 view => 'report',
141 checkouts => $checkouts,
145 output_html_with_http_headers $input, $cookie, $template->output;