Bug 32822: Fix cataloguing/value_builder/unimarc_field_010.pl
[koha-ffzg.git] / tools / batch_extend_due_dates.pl
index 572e57b..ea96169 100755 (executable)
@@ -25,21 +25,23 @@ use CGI;
 use C4::Auth qw( get_template_and_user );
 use C4::Output qw( output_html_with_http_headers );
 use Koha::Checkouts;
-use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::DateUtils qw( dt_from_string );
 
-my $input = new CGI;
+my $input = CGI->new;
 my $op = $input->param('op') // q|form|;
+my $preview_results = $input->param('preview_results');
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {
         template_name   => 'tools/batch_extend_due_dates.tt',
         query           => $input,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { tools => 'batch_extend_due_dates' },
     }
 );
 
+my @issue_ids;
+
 if ( $op eq 'form' ) {
     $template->param( view => 'form', );
 }
@@ -57,7 +59,7 @@ elsif ( $op eq 'list' ) {
     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
     my $search_params;
     if (@categorycodes) {
-        $search_params->{'borrower.categorycode'} = { -in => \@categorycodes };
+        $search_params->{'patron.categorycode'} = { -in => \@categorycodes };
     }
     if (@branchcodes) {
         $search_params->{'me.branchcode'} = { -in => \@branchcodes };
@@ -96,39 +98,50 @@ elsif ( $op eq 'list' ) {
     my $checkouts = Koha::Checkouts->search(
         $search_params,
         {
-            join => [ 'item', 'borrower' ]
+            join => [ 'item', 'patron' ]
         }
     );
 
     my @new_due_dates;
     while ( my $checkout = $checkouts->next ) {
-        push @new_due_dates,
-          output_pref({ dt => calc_new_due_date(
-            {
-                due_date          => dt_from_string($checkout->date_due),
-                new_hard_due_date => $new_hard_due_date,
-                add_days          => $due_date_days
-            }
-          ), dateformat => 'iso' });
+        if ($preview_results) {
+            push(
+                @new_due_dates,
+                calc_new_due_date(
+                    {
+                        due_date => dt_from_string( $checkout->date_due ),
+                        new_hard_due_date => $new_hard_due_date,
+                        add_days          => $due_date_days
+                    }
+                ),
+            );
+        } else {
+            push( @issue_ids, $checkout->id );
+        }
     }
 
-    $template->param(
-        checkouts         => $checkouts,
-        new_hard_due_date => $new_hard_due_date
-        ? dt_from_string($new_hard_due_date)
-        : undef,
-        due_date_days => $due_date_days,
-        new_due_dates => \@new_due_dates,
-        view          => 'list',
-    );
+    if ( $preview_results ) {
+        $template->param(
+            checkouts         => $checkouts,
+            new_hard_due_date => $new_hard_due_date,
+            due_date_days => $due_date_days,
+            new_due_dates => \@new_due_dates,
+            view          => 'list',
+        );
+    } else {
+        $op = 'modify';
+    }
 }
-elsif ( $op eq 'modify' ) {
+
+if ( $op eq 'modify' ) {
 
     # We want to modify selected checkouts!
-    my @issue_ids         = $input->multi_param('issue_id');
     my $new_hard_due_date = $input->param('new_hard_due_date');
     my $due_date_days     = $input->param('due_date_days');
 
+    # @issue_ids will already be populated if we are skipping the results display
+    @issue_ids = $input->multi_param('issue_id') unless @issue_ids;
+
     $new_hard_due_date &&= dt_from_string($new_hard_due_date);
     my $checkouts =
       Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );