From c7996f59061e4e94bf7b729c842b32ce6ac426bd Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Thu, 22 Oct 2020 16:26:03 +0100
Subject: [PATCH] Bug 12656: Allow --reason to be passed to
 cancel_expired_holds

This patch adds the --reason option to cancel_expired_holds which allows
the library to optionally set a reason for cancellation when running the
cronjob. This will prompt the HOLD_CANCELLED notice to be sent to the
patron.
---
 C4/Reserves.pm                              |  2 +
 misc/cronjobs/holds/cancel_expired_holds.pl | 52 +++++++++++++++++++--
 2 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index d971324f1b..651942f421 100644
--- a/C4/Reserves.pm
+++ b/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,6 +919,7 @@ sub CancelExpiredReserves {
         next if !$cancel_on_holidays && $calendar->is_holiday( $today );
 
         my $cancel_params = {};
+        $cancel_params->{cancellation_reason} = $cancellation_reason if defined($cancellation_reason);
         if ( $hold->found eq 'W' ) {
             $cancel_params->{charge_cancel_fee} = 1;
         }
diff --git a/misc/cronjobs/holds/cancel_expired_holds.pl b/misc/cronjobs/holds/cancel_expired_holds.pl
index 463c6b2ba5..232bde708e 100755
--- a/misc/cronjobs/holds/cancel_expired_holds.pl
+++ b/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 <http://www.gnu.org/licenses>.
 
+=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);
-- 
2.20.1