Bug 31203: Alter other cronjobs that currenlty use cronlogaction
[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 my $command_line_options = join(" ",@ARGV);
84
85 GetOptions(
86     'h|help|?'   => \$help,
87     'days=s'     => \$days,
88     'library=s'  => \@branchcodes,
89     'holidays'   => \$use_calendar,
90     'v|verbose'  => \$verbose,
91     'confirm'    => \$confirm,
92 ) or pod2usage(1);
93 pod2usage(1) if $help;
94
95 unless ( defined $days ) {
96     pod2usage(
97         {
98             -exitval => 1,
99             -msg =>
100 qq{\nError: You must specify a value for days waiting to cancel holds.\n},
101         }
102     );
103 }
104
105 cronlogaction({ info => $command_line_options });
106
107 warn "Running in test mode, no actions will be taken" unless ($confirm);
108
109 $verbose and warn "Looking for unfilled holds placed $days or more days ago\n";
110
111 @branchcodes = Koha::Libraries->search->get_column('branchcode') if !@branchcodes;
112 $verbose and warn "Running for branch(es): " . join( "|", @branchcodes ) . "\n";
113
114 foreach my $branch (@branchcodes) {
115
116     my $holds =
117       Koha::Holds->search( { branchcode => $branch } )->unfilled();
118
119     while ( my $hold = $holds->next ) {
120
121         my $age = $hold->age( $use_calendar );
122
123         $verbose
124           and warn "Hold #"
125           . $hold->reserve_id
126           . " has been unfilled for $age day(s)\n";
127
128         if ( $age >= $days ) {
129             my $action = $confirm ? "Cancelling " : "Would have cancelled ";
130             $verbose
131               and warn $action
132               . "reserve_id: "
133               . $hold->reserve_id
134               . " for borrower: "
135               . $hold->borrowernumber
136               . " on biblio: "
137               . $hold->biblionumber . "\n";
138             $hold->cancel if $confirm;
139         }
140
141     }
142
143 }
144
145 cronlogaction({ action => 'End', info => "COMPLETED" });