Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2021 Aleisha Amohia <aleisha@catalyst.net.nz> |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
use Getopt::Long; |
22 |
|
23 |
BEGIN { |
24 |
# find Koha's Perl modules |
25 |
# test carefully before changing this |
26 |
use FindBin; |
27 |
eval { require "$FindBin::Bin/../kohalib.pl" }; |
28 |
} |
29 |
|
30 |
use Koha::Script -cron; |
31 |
use Koha::DateUtils qw( dt_from_string ); |
32 |
use Koha::Recalls; |
33 |
use C4::Log; |
34 |
|
35 |
cronlogaction(); |
36 |
|
37 |
=head1 NAME |
38 |
|
39 |
convert_holds_to_recalls.pl |
40 |
|
41 |
=head1 SYNOPSIS |
42 |
|
43 |
convert_holds_to_recalls.pl [-v] [--min X] |
44 |
|
45 |
This script converts holds to recalls when a record has a set minimum number of holds placed. |
46 |
|
47 |
If a record is found to have the specified minimum number of holds, the script will find the oldest hold and convert it to a recall, as long as it would be a legal recall. |
48 |
|
49 |
It will only convert the single oldest hold. Once converted, the script will move on to the next record, it will not continue to convert holds on this record. |
50 |
|
51 |
Options: |
52 |
-v verbose |
53 |
--min X minimum number of holds |
54 |
|
55 |
=head1 OPTIONS |
56 |
|
57 |
=over 8 |
58 |
|
59 |
=item B<-v> | B<--verbose> |
60 |
|
61 |
Verbose. Without this flag set, only fatal errors are reported. |
62 |
|
63 |
=item B<-m> | B<--min> |
64 |
|
65 |
The minimum number of holds that may exist on a record for it to be selected for conversion |
66 |
|
67 |
=back |
68 |
|
69 |
=cut |
70 |
|
71 |
my $min = 5; |
72 |
my $verbose; |
73 |
|
74 |
GetOptions( |
75 |
'min|m=i' => \$min, |
76 |
'verbose|v' => \$verbose |
77 |
) or pod2usage(1); |
78 |
|
79 |
# get holds where there is more than $min holds on one biblio |
80 |
my @bib_holds = Koha::Holds->search({}, { |
81 |
select => [ |
82 |
'biblionumber', |
83 |
{ count => 'biblionumber', -as => 'bibcount' } |
84 |
], |
85 |
group_by => 'biblionumber', |
86 |
having => { 'bibcount' => { '>=', $min } }, |
87 |
})->as_list; |
88 |
|
89 |
my $count = 0; |
90 |
foreach my $bib ( @bib_holds ) { |
91 |
$bib = $bib->unblessed; |
92 |
if ( $bib->{bibcount} >= $min ) { |
93 |
# If there are $min or more holds on the same record, convert the oldest hold to a recall |
94 |
|
95 |
my @holds = Koha::Holds->search({ biblionumber => $bib->{biblionumber} }, { order_by => { -asc => 'reservedate' } })->as_list; |
96 |
my $hold_to_convert = $holds[0]; |
97 |
foreach my $res ( @holds ) { |
98 |
if ( dt_from_string($res->reservedate) < dt_from_string($hold_to_convert->reservedate) ) { |
99 |
$hold_to_convert = $res; |
100 |
} |
101 |
} |
102 |
|
103 |
my $patron = Koha::Patrons->find( $hold_to_convert->borrowernumber ); |
104 |
my $biblio = Koha::Biblios->find( $hold_to_convert->biblionumber ); |
105 |
my $item; |
106 |
my $can_convert; |
107 |
if ( $hold_to_convert->item_level_hold ) { |
108 |
$item = Koha::Items->find( $hold_to_convert->itemnumber ); |
109 |
if ( $item->can_be_recalled({ patron => $patron, hold_convert => 1 }) ) { |
110 |
$can_convert = 1; |
111 |
} |
112 |
} else { |
113 |
if ( $biblio->can_be_recalled({ patron => $patron, hold_convert => 1 }) ) { |
114 |
$can_convert = 1; |
115 |
} |
116 |
} |
117 |
if ( $can_convert ) { |
118 |
my ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({ |
119 |
patron => $patron, |
120 |
biblio => $biblio, |
121 |
branchcode => $hold_to_convert->branchcode, |
122 |
item => $item, |
123 |
expirationdate => $hold_to_convert->expirationdate, |
124 |
interface => 'commandline', |
125 |
}); |
126 |
$hold_to_convert->cancel({ cancellation_reason => 'RECALLED' }); |
127 |
$count++; |
128 |
if ( $verbose ) { |
129 |
my $hold_id = $hold_to_convert->hold_id; |
130 |
my $biblionumber = $hold_to_convert->biblionumber; |
131 |
print "$count. Hold converted to recall (hold_id: $hold_id, biblionumber: $biblionumber).\n"; |
132 |
} |
133 |
} |
134 |
} |
135 |
} |