From 0c0d0ef4557a10c1ec19ebbbd38dbe6ec79f5ba1 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 17 Sep 2024 12:11:08 +0000 Subject: [PATCH] Bug 37941: WIP different actions in forms --- .../modules/tools/batch_extend_due_dates.tt | 33 ++-- tools/batch_extend_due_dates.pl | 145 ++++++++---------- 2 files changed, 86 insertions(+), 92 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_extend_due_dates.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_extend_due_dates.tt index aa7161ada74..c1f1fb7889b 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_extend_due_dates.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_extend_due_dates.tt @@ -82,7 +82,7 @@ [% END %] [% IF view == 'form' %] -
+ [% INCLUDE 'csrf-token.inc' %]
Checkout criteria: @@ -139,19 +139,9 @@
-
- Options: -
    -
  1. - - - This is not recommended when changing very large numbers of due dates. -
  2. -
-
- - + + Cancel
@@ -188,7 +178,9 @@ [% checkout.issuedate | $KohaDates %] [% Branches.GetName( checkout.branchcode ) | html %] - [% new_due_dates.shift | $KohaDates as_due_date => 1 %] + [%- SET new_due_date = new_due_dates.shift -%] + [%- new_due_date | $KohaDates as_due_date => 1 -%] + [% END %] @@ -197,7 +189,7 @@
Reminder: this action will modify all selected checkouts!
- + @@ -297,6 +289,17 @@ "paginate": false })); + $('.submit_extend').on('click', function(){ + let form_action = $(this).val(); + if( form_action == 'Update due dates' ){ + let form_input = ''; + $("#extend_due_dates_form").append(form_input).attr('method','post').submit(); + } else { + let form_input = ''; + $("#extend_due_dates_form").append(form_input).attr('method','get').submit(); + } + }); + $("#extend_due_dates_form").on('submit', function(e) { var new_hard_due_date = $("#new_hard_due_date").val(); var due_date_days = $("#due_date_days").val(); diff --git a/tools/batch_extend_due_dates.pl b/tools/batch_extend_due_dates.pl index d6b0e84fc85..6cefe674980 100755 --- a/tools/batch_extend_due_dates.pl +++ b/tools/batch_extend_due_dates.pl @@ -22,22 +22,22 @@ use Modern::Perl; use CGI; -use C4::Auth qw( get_template_and_user ); +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 ); use Koha::Items; -my $input = CGI->new; -my $op = $input->param('op') // q|form|; +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", - flagsrequired => { tools => 'batch_extend_due_dates' }, + template_name => 'tools/batch_extend_due_dates.tt', + query => $input, + type => "intranet", + flagsrequired => { tools => 'batch_extend_due_dates' }, } ); @@ -45,8 +45,27 @@ my @issue_ids; if ( $op eq 'form' ) { $template->param( view => 'form', ); -} -elsif ( $op eq 'list' ) { +} elsif ( $op eq 'cud-commit_preview' ) { + @issue_ids = $input->multi_param('issue_id') unless @issue_ids; + + my $checkouts = + Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } ); + while ( my $checkout = $checkouts->next ) { + my $new_due_date = $input->param( 'new_date_due_issue_' . $checkout->issue_id ); + + # Update checkout's due date + $checkout->date_due($new_due_date)->store; + + # Update items.onloan + $checkout->item->onloan($new_due_date)->store; + } + + $template->param( + view => 'report', + checkouts => $checkouts, + ); + +} else { my @categorycodes = $input->multi_param('categorycodes'); my @itemtypecodes = $input->multi_param('itemtypecodes'); @@ -75,93 +94,65 @@ elsif ( $op eq 'list' ) { if ( $from_due_date and $to_due_date ) { my $to_due_date_endday = dt_from_string($to_due_date); - $to_due_date_endday - ->set( # We set last second of day to see all checkouts from that day + $to_due_date_endday->set( # We set last second of day to see all checkouts from that day hour => 23, minute => 59, second => 59 - ); + ); $search_params->{'me.date_due'} = { -between => [ $dtf->format_datetime( dt_from_string($from_due_date) ), $dtf->format_datetime($to_due_date_endday), ] }; - } - elsif ($from_due_date) { + } elsif ($from_due_date) { $search_params->{'me.date_due'} = - { '>=' => $dtf->format_datetime( dt_from_string($from_due_date) ) }; - } - elsif ($to_due_date) { + { '>=' => $dtf->format_datetime( dt_from_string($from_due_date) ) }; + } elsif ($to_due_date) { my $to_due_date_endday = dt_from_string($to_due_date); - $to_due_date_endday - ->set( # We set last second of day to see all checkouts from that day + $to_due_date_endday->set( # We set last second of day to see all checkouts from that day hour => 23, minute => 59, second => 59 - ); + ); $search_params->{'me.date_due'} = - { '<=' => $dtf->format_datetime($to_due_date_endday) }; + { '<=' => $dtf->format_datetime($to_due_date_endday) }; } my $checkouts = Koha::Checkouts->search( $search_params, - { - join => [ 'item', 'patron' ] - } + { join => [ 'item', 'patron' ] } ); my @new_due_dates; while ( my $checkout = $checkouts->next ) { - 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 ); - } - } - - 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', + 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 { - $op = 'cud-modify'; } -} - -if ( $op eq 'cud-modify' ) { - # We want to modify selected checkouts! - 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; +if ( $op = 'list' ) { + $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', + ); +} elsif ( $op eq 'cud-modify' ) { - $new_hard_due_date &&= dt_from_string($new_hard_due_date); - my $checkouts = - Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } ); + $checkouts->reset; while ( my $checkout = $checkouts->next ) { - 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 - } - ); + my $new_due_date = shift(@new_due_dates); # Update checkout's due date $checkout->date_due($new_due_date)->store; @@ -183,16 +174,16 @@ sub calc_new_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; + 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; -- 2.39.5