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