Bug 11401: QA followup
[koha.git] / misc / cronjobs / automatic_renewals.pl
1 # This file is part of Koha.
2 #
3 # Copyright (C) 2014 Hochschule für Gesundheit (hsg), Germany
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 =head1 NAME
19
20 automatic_renewals.pl - cron script to renew loans
21
22 =head1 SYNOPSIS
23
24 ./automatic_renewals.pl
25
26 or, in crontab:
27 0 3 * * * automatic_renewals.pl
28
29 =head1 DESCRIPTION
30
31 This script searches for issues scheduled for automatic renewal
32 (issues.auto_renew). If there are still renews left (Renewals allowed)
33 and the renewal isn't premature (No Renewal before) the issue is renewed.
34
35 =head1 OPTIONS
36
37 No options.
38
39 =cut
40
41 use Modern::Perl;
42
43 use C4::Circulation;
44 use C4::Context;
45
46 my $dbh = C4::Context->dbh;
47 my ( $borrowernumber, $itemnumber, $branch, $ok, $error );
48
49 my $query =
50 "SELECT borrowernumber, itemnumber, branchcode FROM issues WHERE auto_renew = 1";
51 my $sth = $dbh->prepare($query);
52 $sth->execute();
53
54 while ( ( $borrowernumber, $itemnumber, $branch ) = $sth->fetchrow_array ) {
55
56 # CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script
57     ( $ok, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
58     AddRenewal( $borrowernumber, $itemnumber, $branch )
59       if ( $error eq "auto_renew" );
60 }