Line 0
Link Here
|
|
|
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 |
# This script converts reserves to recalls when a record has a set minimum number of reserves placed. |
31 |
# It DOES NOT CHECK if this is a legal recall. It assumes that the same rules that apply to reserves, also apply to recalls. |
32 |
|
33 |
use Koha::Script -cron; |
34 |
use Koha::DateUtils; |
35 |
use Koha::Recalls; |
36 |
use C4::Log; |
37 |
|
38 |
my $min = 1; |
39 |
my $verbose; |
40 |
|
41 |
GetOptions( |
42 |
'min|m=i' => \$min, |
43 |
'verbose|v' => \$verbose |
44 |
) or pod2usage(1); |
45 |
|
46 |
cronlogaction(); |
47 |
|
48 |
# get reserves where there is more than $min reserves on one biblio |
49 |
my @bib_reserves = Koha::Holds->search({}, { |
50 |
select => [ |
51 |
'biblionumber', |
52 |
{ count => 'biblionumber', -as => 'bibcount' } |
53 |
], |
54 |
group_by => 'biblionumber', |
55 |
having => { 'bibcount' => { '>=', $min } }, |
56 |
}); |
57 |
|
58 |
my $count = 0; |
59 |
foreach my $bib ( @bib_reserves ) { |
60 |
$bib = $bib->unblessed; |
61 |
if ( $bib->{bibcount} >= $min ) { |
62 |
# "Currently, if all items on a record are unavailable and there are $min or more holds on that same record, then we place a recall" |
63 |
|
64 |
my @reserves = Koha::Holds->search({ biblionumber => $bib->{biblionumber} }, { order_by -=> { -asc => 'reservedate' } }); |
65 |
my $reserve_to_convert = $reserves[0]; |
66 |
foreach my $res ( @reserves ) { |
67 |
# "Generally, we place the recall on the item that has been on loan for the longest." |
68 |
if ( dt_from_string($res->reservedate) < dt_from_string($reserve_to_convert->reservedate) ) { |
69 |
$reserve_to_convert = $res; |
70 |
} |
71 |
} |
72 |
|
73 |
my $patron = Koha::Patrons->find( $reserve_to_convert->borrowernumber ); |
74 |
my $item; |
75 |
my $can_convert; |
76 |
if ( $reserve_to_convert->item_level_hold ) { |
77 |
$item = Koha::Items->find( $reserve_to_convert->itemnumber ); |
78 |
if ( $item->checkout ) { |
79 |
# item-level reserve can be recalled because the relevant item is unavailable |
80 |
$can_convert = 1; |
81 |
} |
82 |
} else { |
83 |
my @items = Koha::Items->search({ biblionumber => $reserve_to_convert->biblionumber }); |
84 |
my $items_unavailable = 0; |
85 |
foreach ( @items ) { |
86 |
if ( $_->checkout or $_->itemlost or $_->withdrawn or $_->notforloan ) { |
87 |
# item is unavailable to be checked out right now |
88 |
$items_unavailable++; |
89 |
} |
90 |
} |
91 |
if ( $items_unavailable > 0 and $items_unavailable == scalar @items ) { |
92 |
# all items must be unavailable for this reserve to be converted to a recall |
93 |
$can_convert = 1; |
94 |
} |
95 |
} |
96 |
my $biblio = Koha::Biblios->find( $reserve_to_convert->biblionumber ); |
97 |
if ( $can_convert ) { |
98 |
my ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({ |
99 |
patron => $patron, |
100 |
biblio => $biblio, |
101 |
branchcode => $reserve_to_convert->branchcode, |
102 |
item => $item, |
103 |
expirationdate => $reserve_to_convert->expirationdate, |
104 |
interface => 'commandline', |
105 |
}); |
106 |
$reserve_to_convert->cancel({ cancellation_reason => 'RECALLED' }); |
107 |
$count++; |
108 |
if ( $verbose ) { |
109 |
my $reserve_id = $reserve_to_convert->reserve_id; |
110 |
my $biblionumber = $reserve_to_convert->biblionumber; |
111 |
print "$count. Hold $reserve_id converted to recall on biblio $biblionumber.\n"; |
112 |
} |
113 |
} |
114 |
} |
115 |
} |