Bug 17600: Standardize our EXPORT_OK
[srvgit] / misc / cronjobs / holds / cancel_unfilled_holds.pl
index 29e7472..1095269 100755 (executable)
@@ -21,63 +21,61 @@ use Modern::Perl;
 BEGIN {
     # find Koha's Perl modules
     # test carefully before changing this
-    use FindBin;
+    use FindBin ();
     eval { require "$FindBin::Bin/../kohalib.pl" };
 }
 
-use Getopt::Long;
-use Pod::Usage;
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
 
+use Koha::Script -cron;
 use C4::Reserves;
-use C4::Log;
+use C4::Log qw( cronlogaction );
 use Koha::Holds;
 use Koha::Calendar;
-use Koha::DateUtils;
 use Koha::Libraries;
 
 cronlogaction();
 
 =head1 NAME
 
-cancel_unfilled_holds.pl
+cancel_unfilled_holds.pl - script to delete unfilled holds after a given
+number of days.
 
 =head1 SYNOPSIS
 
-cancel_unfilled_holds.pl
-    [-days][-library][-holidays]
+ cancel_unfilled_holds.pl [--days][--library][--holidays][--confirm][--verbose]
 
- Options:
-    -help                       brief help
-    -days                       cancel holds placed this many days ago which have not been filled
-    -library                    [repeatable] limit to specified branch(es)
-    -holidays                   skip holidays when calculating days waiting
-    -v                          verbose
-
-head1 OPTIONS
+=head1 OPTIONS
 
 =over 8
 
-=item B<-help>
+=item B<--help | -h>
 
 Print brief help and exit.
 
-=item B<-man>
-
-Print full documentation and exit.
-
-=item B<-days>
+=item B<--days>
 
 Specify the number of days waiting since a hold that remains unfilled was placed.
 E.g. a value of 730 would cancel holds placed 2 years ago or more that have never been filled
 
-=item B<-library>
+=item B<--library>
 
 Repeatable option to specify which branchcode(s) to cancel holds for.
 
-=item B<-holidays>
+=item B<--holidays>
 
 This switch specifies whether to count holidays as days waiting. Default is no.
 
+=item B<--confirm>
+
+Without this option, the script will run in test mode, and only report what it
+would have done if it were not running in test mode.
+
+=item B<--verbose | -v>
+
+More verbose output.
+
 =back
 
 =cut
@@ -90,12 +88,12 @@ my $verbose      = 0;
 my $confirm      = 0;
 
 GetOptions(
-    'help|?'    => \$help,
-    'days=s'    => \$days,
-    'library=s' => \@branchcodes,
-    'holidays'  => \$use_calendar,
-    'v'         => \$verbose,
-    'confirm'   => \$confirm,
+    'h|help|?'   => \$help,
+    'days=s'     => \$days,
+    'library=s'  => \@branchcodes,
+    'holidays'   => \$use_calendar,
+    'v|verbose'  => \$verbose,
+    'confirm'    => \$confirm,
 ) or pod2usage(1);
 pod2usage(1) if $help;
 
@@ -112,12 +110,7 @@ warn "Running in test mode, no actions will be taken" unless ($confirm);
 
 $verbose and warn "Looking for unfilled holds placed $days or more days ago\n";
 
-unless ( scalar @branchcodes > 0 ) {
-    my $branches = Koha::Libraries->search->get_column('branchcode');
-    while ( my $branch = $branches->next ) {
-        push @branchcodes, $branch->branchcode;
-    }
-}
+@branchcodes = Koha::Libraries->search->get_column('branchcode') if !@branchcodes;
 $verbose and warn "Running for branch(es): " . join( "|", @branchcodes ) . "\n";
 
 foreach my $branch (@branchcodes) {
@@ -144,7 +137,7 @@ foreach my $branch (@branchcodes) {
               . $hold->borrowernumber
               . " on biblio: "
               . $hold->biblionumber . "\n";
-            CancelReserve( { reserve_id => $hold->reserve_id } ) if $confirm;
+            $hold->cancel if $confirm;
         }
 
     }