From b3f0791140ff18b24c6408ad095dd02b7fe856d1 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 14 Jul 2016 06:46:15 -0400 Subject: [PATCH] Bug 16187 - Add a script to cancel unfilled holds after a specified number of days This script takes parameters: days - how many days waiting to concal an unfilled hold on or after library - (repeatable) branches to consider holidays - whether or not to count holidays (default is no) To test: 1 - Place some holds with varying reservedates 2 - Run script with different parameters to verify options are respected (-v for verbosity will assist here) 3 - verify that script does nothing without days parameter Sponsored by: Siskiyou County Library (http://www.siskiyoulibrary.info/) --- misc/cronjobs/holds/cancel_waiting_holds.pl | 143 ++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100755 misc/cronjobs/holds/cancel_waiting_holds.pl diff --git a/misc/cronjobs/holds/cancel_waiting_holds.pl b/misc/cronjobs/holds/cancel_waiting_holds.pl new file mode 100755 index 0000000..baf1fa0 --- /dev/null +++ b/misc/cronjobs/holds/cancel_waiting_holds.pl @@ -0,0 +1,143 @@ +#!/usr/bin/perl + +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +BEGIN { + # find Koha's Perl modules + # test carefully before changing this + use FindBin; + eval { require "$FindBin::Bin/../kohalib.pl" }; +} + +use Getopt::Long; +use Pod::Usage; + +use C4::Reserves; +use C4::Log; +use Koha::Holds; +use Koha::Calendar; +use Koha::DateUtils; +use Koha::Libraries; + +cronlogaction(); + +=head1 NAME + +cancel_waiting_holds.pl + +=head1 SYNOPSIS + +cancel_waiting_holds.pl + [-days][-library][-holidays] + + Options: + -help brief help + -days cancel holds waiting this many days + -library [repeatable] limit to specified branch(es) + -holidays skip holidays when calculating days waiting + -v verbose + +head1 OPTIONS + +=over 8 + +=item B<-help> + +Print brief help and exit. + +=item B<-man> + +Print full documentation and exit. + +=item B<-days> + +Specify the number of days waiting upon which to cancel holds. + +=item B<-library> + +Repeatable option to specify which branchcode(s) to cancel holds for. + +=item B<-holidays> + +This switch specifies whether to count holidays as days waiting. Default is no. + +=back + +=cut + +my $help = 0; +my $days; +my @branchcodes; +my $use_calendar = 0; +my $verbose = 0; + +GetOptions( + 'help|?' => \$help, + 'days=s' => \$days, + 'library=s' => \@branchcodes, + 'holidays' => \$use_calendar, + 'v' => \$verbose +) or pod2usage(1); +pod2usage(1) if $help; + +unless (defined $days) { + pod2usage({ + -exitval => 1, + -msg => qq{\nError: You must specify a value for days waiting to cancel holds.\n}, + }); +} +$verbose and warn "Will cancel unfilled holds placed $days or more days ago\n"; + +unless (scalar @branchcodes > 0 ) { + my $branches = Koha::Libraries->search( {} , {columns => 'branchcode' } ); + while ( my $branch = $branches->next ) { + push @branchcodes, $branch->branchcode; + } +} +$verbose and warn "Running for branch(es): ".join("|",@branchcodes)."\n"; + +foreach my $branch (@branchcodes){ + + my $holds = Koha::Holds->search( { found => undef , branchcode => $branch } ); + + while ( my $hold = $holds->next ){ + + my $days_waiting; + my $today = DateTime->now(time_zone => C4::Context->tz ); + + if ( $use_calendar ) { + my $calendar = Koha::Calendar->new( branchcode => $branch ); + $days_waiting = $calendar->days_between( dt_from_string( $hold->reservedate ), $today ); + } + else { + $days_waiting = $today->delta_days( dt_from_string( $hold->reservedate ) ); + } + $days_waiting = $days_waiting->in_units( 'days' ); + + $verbose and warn "Hold #".$hold->reserve_id." has been waiting $days_waiting day(s)\n"; + + if ( $days_waiting >= $days ) { + $verbose and warn "Cancelling reserve_id: ".$hold->reserve_id." for borrower: ".$hold->borrowernumber." on biblio: ".$hold->biblionumber."\n"; + CancelReserve( {reserve_id => $hold->reserve_id } ); + } + + } + +} + -- 2.1.4