|
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_reserves_to_recalls.pl |
| 40 |
|
| 41 |
=head1 SYNOPSIS |
| 42 |
|
| 43 |
convert_reserves_to_recalls.pl [-v] [--min X] |
| 44 |
|
| 45 |
This script converts reserves to recalls when a record has a set minimum number of reserves placed. |
| 46 |
|
| 47 |
Options: |
| 48 |
-v verbose |
| 49 |
--min X minimum number of reserves |
| 50 |
|
| 51 |
=cut |
| 52 |
|
| 53 |
my $min = 1; |
| 54 |
my $verbose; |
| 55 |
|
| 56 |
GetOptions( |
| 57 |
'min|m=i' => \$min, |
| 58 |
'verbose|v' => \$verbose |
| 59 |
) or pod2usage(1); |
| 60 |
|
| 61 |
# get reserves where there is more than $min reserves on one biblio |
| 62 |
my @bib_reserves = Koha::Holds->search({}, { |
| 63 |
select => [ |
| 64 |
'biblionumber', |
| 65 |
{ count => 'biblionumber', -as => 'bibcount' } |
| 66 |
], |
| 67 |
group_by => 'biblionumber', |
| 68 |
having => { 'bibcount' => { '>=', $min } }, |
| 69 |
})->as_list; |
| 70 |
|
| 71 |
my $count = 0; |
| 72 |
foreach my $bib ( @bib_reserves ) { |
| 73 |
$bib = $bib->unblessed; |
| 74 |
if ( $bib->{bibcount} >= $min ) { |
| 75 |
# If there are $min or more holds on the same record, convert the oldest hold to a recall |
| 76 |
|
| 77 |
my @reserves = Koha::Holds->search({ biblionumber => $bib->{biblionumber} }, { order_by => { -asc => 'reservedate' } })->as_list; |
| 78 |
my $reserve_to_convert = $reserves[0]; |
| 79 |
foreach my $res ( @reserves ) { |
| 80 |
if ( dt_from_string($res->reservedate) < dt_from_string($reserve_to_convert->reservedate) ) { |
| 81 |
$reserve_to_convert = $res; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
my $patron = Koha::Patrons->find( $reserve_to_convert->borrowernumber ); |
| 86 |
my $biblio = Koha::Biblios->find( $reserve_to_convert->biblionumber ); |
| 87 |
my $item; |
| 88 |
my $can_convert; |
| 89 |
if ( $reserve_to_convert->item_level_hold ) { |
| 90 |
$item = Koha::Items->find( $reserve_to_convert->itemnumber ); |
| 91 |
if ( $item->can_be_recalled({ patron => $patron, hold_convert => 1 }) ) { |
| 92 |
$can_convert = 1; |
| 93 |
} |
| 94 |
} else { |
| 95 |
if ( $biblio->can_be_recalled({ patron => $patron, hold_convert => 1 }) ) { |
| 96 |
$can_convert = 1; |
| 97 |
} |
| 98 |
} |
| 99 |
if ( $can_convert ) { |
| 100 |
my ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({ |
| 101 |
patron => $patron, |
| 102 |
biblio => $biblio, |
| 103 |
branchcode => $reserve_to_convert->branchcode, |
| 104 |
item => $item, |
| 105 |
expirationdate => $reserve_to_convert->expirationdate, |
| 106 |
interface => 'commandline', |
| 107 |
}); |
| 108 |
$reserve_to_convert->cancel({ cancellation_reason => 'RECALLED' }); |
| 109 |
$count++; |
| 110 |
if ( $verbose ) { |
| 111 |
my $reserve_id = $reserve_to_convert->reserve_id; |
| 112 |
my $biblionumber = $reserve_to_convert->biblionumber; |
| 113 |
print "$count. Hold $reserve_id converted to recall on biblio $biblionumber.\n"; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |