Bug 22417: Adapt the batch_record_modification tool
[koha-ffzg.git] / tools / batch_extend_due_dates.pl
index e79e5bd..cae338c 100755 (executable)
@@ -24,23 +24,24 @@ use CGI;
 
 use C4::Auth qw( get_template_and_user );
 use C4::Output qw( output_html_with_http_headers );
-use C4::Items qw( ModItem );
 use Koha::Checkouts;
-use Koha::DateUtils qw( dt_from_string );
+use Koha::DateUtils qw( dt_from_string output_pref );
 
 my $input = new CGI;
 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', );
 }
@@ -53,6 +54,8 @@ elsif ( $op eq 'list' ) {
     my $new_hard_due_date = $input->param('new_hard_due_date');
     my $due_date_days     = $input->param('due_date_days');
 
+    $new_hard_due_date &&= dt_from_string($new_hard_due_date);
+
     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
     my $search_params;
     if (@categorycodes) {
@@ -100,42 +103,70 @@ elsif ( $op eq 'list' ) {
     );
 
     my @new_due_dates;
-    if ( not $new_hard_due_date && $due_date_days ) {
-        while ( my $checkout = $checkouts->next ) {
-            my $due_date = dt_from_string( $checkout->date_due );
-            push @new_due_dates, $due_date->add( days => $due_date_days );
+    while ( my $checkout = $checkouts->next ) {
+        if ($preview_results) {
+            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'
+                    }
+                )
+            );
+        } 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
+            ? dt_from_string($new_hard_due_date)
+            : undef,
+            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 } } );
     while ( my $checkout = $checkouts->next ) {
-        my $new_due_date = $new_hard_due_date
-          || dt_from_string( $checkout->date_due )->add( days => $due_date_days );
+        my $new_due_date = 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
+            }
+        );
 
         # Update checkout's due date
         $checkout->date_due($new_due_date)->store;
 
         # Update items.onloan
-        ModItem( { onloan => $new_due_date->strftime('%Y-%m-%d %H:%M') },
-            undef, $checkout->itemnumber );
+        $checkout->item->onloan($new_due_date)->store;
     }
 
     $template->param(
@@ -144,4 +175,23 @@ elsif ( $op eq 'modify' ) {
     );
 }
 
+sub calc_new_due_date {
+    my ($params)          = @_;
+    my $due_date          = $params->{due_date};
+    my $new_hard_due_date = $params->{new_hard_due_date};
+    my $add_days          = $params->{add_days};
+
+    my $new;
+    if ( $new_hard_due_date ) {
+      $new = $new_hard_due_date->clone->set(
+        hour   => $due_date->hour,
+        minute => $due_date->minute,
+        second => $due_date->second,
+      )
+  } else {
+      $new = $due_date->clone->add( days => $add_days );
+  }
+  return $new;
+}
+
 output_html_with_http_headers $input, $cookie, $template->output;