407bc9cc8ed8a659fe463e204ab252af2968b030
[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 C4::Reserves;
32 use C4::Log;
33 use Koha::Holds;
34 use Koha::Calendar;
35 use Koha::DateUtils;
36 use Koha::Libraries;
37
38 cronlogaction();
39
40 =head1 NAME
41
42 cancel_unfilled_holds.pl
43
44 =head1 SYNOPSIS
45
46 cancel_unfilled_holds.pl
47     [-days][-library][-holidays]
48
49  Options:
50     -help                       brief help
51     -days                       cancel holds placed this many days ago which have not been filled
52     -library                    [repeatable] limit to specified branch(es)
53     -holidays                   skip holidays when calculating days waiting
54     -v                          verbose
55
56 head1 OPTIONS
57
58 =over 8
59
60 =item B<-help>
61
62 Print brief help and exit.
63
64 =item B<-man>
65
66 Print full documentation and exit.
67
68 =item B<-days>
69
70 Specify the number of days waiting since a hold that remains unfilled was placed.
71 E.g. a value of 730 would cancel holds placed 2 years ago or more that have never been filled
72
73 =item B<-library>
74
75 Repeatable option to specify which branchcode(s) to cancel holds for.
76
77 =item B<-holidays>
78
79 This switch specifies whether to count holidays as days waiting. Default is no.
80
81 =back
82
83 =cut
84
85 my $help = 0;
86 my $days;
87 my @branchcodes;
88 my $use_calendar = 0;
89 my $verbose      = 0;
90 my $confirm      = 0;
91
92 GetOptions(
93     'h|help|?'   => \$help,
94     'days=s'     => \$days,
95     'library=s'  => \@branchcodes,
96     'holidays'   => \$use_calendar,
97     'v|verbosev' => \$verbose,
98     'confirm'    => \$confirm,
99 ) or pod2usage(1);
100 pod2usage(1) if $help;
101
102 unless ( defined $days ) {
103     pod2usage(
104         {
105             -exitval => 1,
106             -msg =>
107 qq{\nError: You must specify a value for days waiting to cancel holds.\n},
108         }
109     );
110 }
111 warn "Running in test mode, no actions will be taken" unless ($confirm);
112
113 $verbose and warn "Looking for unfilled holds placed $days or more days ago\n";
114
115 @branchcodes = Koha::Libraries->search->get_column('branchcode') if !@branchcodes;
116 $verbose and warn "Running for branch(es): " . join( "|", @branchcodes ) . "\n";
117
118 foreach my $branch (@branchcodes) {
119
120     my $holds =
121       Koha::Holds->search( { branchcode => $branch } )->unfilled();
122
123     while ( my $hold = $holds->next ) {
124
125         my $age = $hold->age( $use_calendar );
126
127         $verbose
128           and warn "Hold #"
129           . $hold->reserve_id
130           . " has been unfilled for $age day(s)\n";
131
132         if ( $age >= $days ) {
133             my $action = $confirm ? "Cancelling " : "Would have cancelled ";
134             $verbose
135               and warn $action
136               . "reserve_id: "
137               . $hold->reserve_id
138               . " for borrower: "
139               . $hold->borrowernumber
140               . " on biblio: "
141               . $hold->biblionumber . "\n";
142             $hold->cancel if $confirm;
143         }
144
145     }
146
147 }