Bug 22600: Set 'commandline' interface appropriately
[koha.git] / misc / cronjobs / holds / cancel_unfilled_holds.pl
1 #!/usr/bin/perl
2
3 #
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 BEGIN {
22     # find Koha's Perl modules
23     # test carefully before changing this
24     use FindBin;
25     eval { require "$FindBin::Bin/../kohalib.pl" };
26 }
27
28 use Getopt::Long;
29 use Pod::Usage;
30
31 use Koha::Script -cron;
32 use C4::Reserves;
33 use C4::Log;
34 use Koha::Holds;
35 use Koha::Calendar;
36 use Koha::DateUtils;
37 use Koha::Libraries;
38
39 cronlogaction();
40
41 =head1 NAME
42
43 cancel_unfilled_holds.pl - script to delete unfilled holds after a given
44 number of days.
45
46 =head1 SYNOPSIS
47
48  cancel_unfilled_holds.pl [--days][--library][--holidays][--confirm][--verbose]
49
50 =head1 OPTIONS
51
52 =over 8
53
54 =item B<--help | -h>
55
56 Print brief help and exit.
57
58 =item B<--days>
59
60 Specify the number of days waiting since a hold that remains unfilled was placed.
61 E.g. a value of 730 would cancel holds placed 2 years ago or more that have never been filled
62
63 =item B<--library>
64
65 Repeatable option to specify which branchcode(s) to cancel holds for.
66
67 =item B<--holidays>
68
69 This switch specifies whether to count holidays as days waiting. Default is no.
70
71 =item B<--confirm>
72
73 Without this option, the script will run in test mode, and only report what it
74 would have done if it were not running in test mode.
75
76 =item B<--verbose | -v>
77
78 More verbose output.
79
80 =back
81
82 =cut
83
84 my $help = 0;
85 my $days;
86 my @branchcodes;
87 my $use_calendar = 0;
88 my $verbose      = 0;
89 my $confirm      = 0;
90
91 GetOptions(
92     'h|help|?'   => \$help,
93     'days=s'     => \$days,
94     'library=s'  => \@branchcodes,
95     'holidays'   => \$use_calendar,
96     'v|verbose'  => \$verbose,
97     'confirm'    => \$confirm,
98 ) or pod2usage(1);
99 pod2usage(1) if $help;
100
101 unless ( defined $days ) {
102     pod2usage(
103         {
104             -exitval => 1,
105             -msg =>
106 qq{\nError: You must specify a value for days waiting to cancel holds.\n},
107         }
108     );
109 }
110 warn "Running in test mode, no actions will be taken" unless ($confirm);
111
112 $verbose and warn "Looking for unfilled holds placed $days or more days ago\n";
113
114 @branchcodes = Koha::Libraries->search->get_column('branchcode') if !@branchcodes;
115 $verbose and warn "Running for branch(es): " . join( "|", @branchcodes ) . "\n";
116
117 foreach my $branch (@branchcodes) {
118
119     my $holds =
120       Koha::Holds->search( { branchcode => $branch } )->unfilled();
121
122     while ( my $hold = $holds->next ) {
123
124         my $age = $hold->age( $use_calendar );
125
126         $verbose
127           and warn "Hold #"
128           . $hold->reserve_id
129           . " has been unfilled for $age day(s)\n";
130
131         if ( $age >= $days ) {
132             my $action = $confirm ? "Cancelling " : "Would have cancelled ";
133             $verbose
134               and warn $action
135               . "reserve_id: "
136               . $hold->reserve_id
137               . " for borrower: "
138               . $hold->borrowernumber
139               . " on biblio: "
140               . $hold->biblionumber . "\n";
141             $hold->cancel if $confirm;
142         }
143
144     }
145
146 }