@@ -, +, @@ cancel_expired_holds (t/db_dependent/Reserves/CancelExpiredReserves.t) --- C4/Reserves.pm | 4 +- misc/cronjobs/holds/cancel_expired_holds.pl | 52 +++++++++++++++++-- .../Reserves/CancelExpiredReserves.t | 26 +++++++++- 3 files changed, 77 insertions(+), 5 deletions(-) --- a/C4/Reserves.pm +++ a/C4/Reserves.pm @@ -901,6 +901,7 @@ Cancels all reserves with an expiration date from before today. =cut sub CancelExpiredReserves { + my $cancellation_reason = shift; my $today = dt_from_string(); my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays'); my $expireWaiting = C4::Context->preference('ExpireReservesMaxPickUpDelay'); @@ -918,7 +919,8 @@ sub CancelExpiredReserves { next if !$cancel_on_holidays && $calendar->is_holiday( $today ); my $cancel_params = {}; - if ( $hold->found eq 'W' ) { + $cancel_params->{cancellation_reason} = $cancellation_reason if defined($cancellation_reason); + if ( defined($hold->found) && $hold->found eq 'W' ) { $cancel_params->{charge_cancel_fee} = 1; } $hold->cancel( $cancel_params ); --- a/misc/cronjobs/holds/cancel_expired_holds.pl +++ a/misc/cronjobs/holds/cancel_expired_holds.pl @@ -1,6 +1,7 @@ #!/usr/bin/perl # Copyright 2009-2010 Kyle Hall +# Copyright 2020 PTFS Europe # # This file is part of Koha. # @@ -17,7 +18,29 @@ # You should have received a copy of the GNU General Public License # along with Koha; if not, see . +=head1 NAME + +cancel_expired_holds.pl - cron script to cancel holds as they expire + +=head1 SYNOPSIS + + ./cancel_expired_holds.pl + ./cancel_expired_holds.pl --reason="EXPIRED" + +or, in crontab: + + 0 1 * * * cancel_expired_holds.pl + 0 1 * * * cancel_expired_holds.pl --reason="EXPIRED" + +=head1 DESCRIPTION + +This script calls C4::Reserves::CancelExpiredReserves which will find and cancel all expired reseves in the system. + +=cut + use Modern::Perl; +use Getopt::Long; +use Pod::Usage; BEGIN { # find Koha's Perl modules @@ -26,12 +49,35 @@ BEGIN { eval { require "$FindBin::Bin/../kohalib.pl" }; } -# cancel all expired hold requests - use Koha::Script -cron; use C4::Reserves; use C4::Log; +=head1 OPTIONS + +=over 8 + +=item B<--help> + +Print a brief help message and exits. + +=item B<--reason> + +Optionally adds a reason for cancellation (which will trigger a notice to be sent to the patron) + +=back + +=cut + +my $help = 0; +my $reason; + +GetOptions( + 'help|?' => \$help, + 'reason=s' => \$reason +) or pod2usage(1); +pod2usage(1) if $help; + cronlogaction(); -CancelExpiredReserves(); +CancelExpiredReserves($reason); --- a/t/db_dependent/Reserves/CancelExpiredReserves.t +++ a/t/db_dependent/Reserves/CancelExpiredReserves.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use t::lib::Mocks; use t::lib::TestBuilder; @@ -199,4 +199,28 @@ subtest 'Test handling of in transit reserves by CancelExpiredReserves' => sub { }; +subtest 'Test handling of cancellation reason if passed' => sub { + plan tests => 2; + + my $builder = t::lib::TestBuilder->new(); + + my $expdate = dt_from_string->add( days => -2 ); + my $reserve = $builder->build({ + source => 'Reserve', + value => { + expirationdate => '2018-01-01', + found => 'T', + cancellationdate => undef, + suspend => 0, + suspend_until => undef + } + }); + my $reserve_id = $reserve->{reserve_id}; + my $count = Koha::Holds->search->count; + CancelExpiredReserves("EXPIRED"); + is(Koha::Holds->search->count, $count-1, "Hold is cancelled when reason is passed"); + my $old_reserve = Koha::Old::Holds->find($reserve_id); + is($old_reserve->cancellation_reason, 'EXPIRED', "Hold cancellation_reason was set correctly"); +}; + $schema->storage->txn_rollback; --