Line 0
Link Here
|
0 |
- |
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_waiting_holds.pl |
43 |
|
44 |
=head1 SYNOPSIS |
45 |
|
46 |
cancel_waiting_holds.pl |
47 |
[-days][-library][-holidays] |
48 |
|
49 |
Options: |
50 |
-help brief help |
51 |
-days cancel holds waiting this many days |
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 upon which to cancel holds. |
71 |
|
72 |
=item B<-library> |
73 |
|
74 |
Repeatable option to specify which branchcode(s) to cancel holds for. |
75 |
|
76 |
=item B<-holidays> |
77 |
|
78 |
This switch specifies whether to count holidays as days waiting. Default is no. |
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 |
|
90 |
GetOptions( |
91 |
'help|?' => \$help, |
92 |
'days=s' => \$days, |
93 |
'library=s' => \@branchcodes, |
94 |
'holidays' => \$use_calendar, |
95 |
'v' => \$verbose |
96 |
) or pod2usage(1); |
97 |
pod2usage(1) if $help; |
98 |
|
99 |
unless (defined $days) { |
100 |
pod2usage({ |
101 |
-exitval => 1, |
102 |
-msg => qq{\nError: You must specify a value for days waiting to cancel holds.\n}, |
103 |
}); |
104 |
} |
105 |
$verbose and warn "Will cancel unfilled holds placed $days or more days ago\n"; |
106 |
|
107 |
unless (scalar @branchcodes > 0 ) { |
108 |
my $branches = Koha::Libraries->search( {} , {columns => 'branchcode' } ); |
109 |
while ( my $branch = $branches->next ) { |
110 |
push @branchcodes, $branch->branchcode; |
111 |
} |
112 |
} |
113 |
$verbose and warn "Running for branch(es): ".join("|",@branchcodes)."\n"; |
114 |
|
115 |
foreach my $branch (@branchcodes){ |
116 |
|
117 |
my $holds = Koha::Holds->search( { found => undef , branchcode => $branch } ); |
118 |
|
119 |
while ( my $hold = $holds->next ){ |
120 |
|
121 |
my $days_waiting; |
122 |
my $today = DateTime->now(time_zone => C4::Context->tz ); |
123 |
|
124 |
if ( $use_calendar ) { |
125 |
my $calendar = Koha::Calendar->new( branchcode => $branch ); |
126 |
$days_waiting = $calendar->days_between( dt_from_string( $hold->reservedate ), $today ); |
127 |
} |
128 |
else { |
129 |
$days_waiting = $today->delta_days( dt_from_string( $hold->reservedate ) ); |
130 |
} |
131 |
$days_waiting = $days_waiting->in_units( 'days' ); |
132 |
|
133 |
$verbose and warn "Hold #".$hold->reserve_id." has been waiting $days_waiting day(s)\n"; |
134 |
|
135 |
if ( $days_waiting >= $days ) { |
136 |
$verbose and warn "Cancelling reserve_id: ".$hold->reserve_id." for borrower: ".$hold->borrowernumber." on biblio: ".$hold->biblionumber."\n"; |
137 |
CancelReserve( {reserve_id => $hold->reserve_id } ); |
138 |
} |
139 |
|
140 |
} |
141 |
|
142 |
} |
143 |
|