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