Bug 15705: Reset the last error when an auto renew successes
[koha.git] / misc / cronjobs / automatic_renewals.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2014 Hochschule für Gesundheit (hsg), Germany
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 =head1 NAME
21
22 automatic_renewals.pl - cron script to renew loans
23
24 =head1 SYNOPSIS
25
26 ./automatic_renewals.pl
27
28 or, in crontab:
29 0 3 * * * automatic_renewals.pl
30
31 =head1 DESCRIPTION
32
33 This script searches for issues scheduled for automatic renewal
34 (issues.auto_renew). If there are still renews left (Renewals allowed)
35 and the renewal isn't premature (No Renewal before) the issue is renewed.
36
37 =head1 OPTIONS
38
39 No options.
40
41 =cut
42
43 use Modern::Perl;
44
45 use C4::Circulation;
46 use C4::Context;
47 use C4::Log;
48 use C4::Letters;
49 use Koha::Checkouts;
50 use Koha::Libraries;
51 use Koha::Patrons;
52
53 cronlogaction();
54
55 my $auto_renews = Koha::Checkouts->search({ auto_renew => 1 });
56
57 my %report;
58 while ( my $auto_renew = $auto_renews->next ) {
59
60     # CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script
61     my ( $ok, $error ) = CanBookBeRenewed( $auto_renew->borrowernumber, $auto_renew->itemnumber );
62     if ( $error eq 'auto_renew' ) {
63         my $date_due = AddRenewal( $auto_renew->borrowernumber, $auto_renew->itemnumber, $auto_renew->branchcode );
64         $auto_renew->auto_renew_error(undef)->store;
65         push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew;
66     } elsif ( $error eq 'too_many'
67         or $error eq 'on_reserve'
68         or $error eq 'restriction'
69         or $error eq 'overdue'
70         or $error eq 'auto_too_late'
71         or $error eq 'auto_too_much_oweing'
72         or $error eq 'auto_too_soon' ) {
73         if ( not $auto_renew->auto_renew_error or $error ne $auto_renew->auto_renew_error ) {
74             $auto_renew->auto_renew_error($error)->store;
75             push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
76               if $error ne 'auto_too_soon';    # Do not notify if it's too soon
77         }
78     }
79 }
80
81 for my $borrowernumber ( keys %report ) {
82     my $patron = Koha::Patrons->find($borrowernumber);
83     my @issues;
84     for my $issue ( @{ $report{$borrowernumber} } ) {
85         my $item   = Koha::Items->find( $issue->itemnumber );
86         my $letter = C4::Letters::GetPreparedLetter(
87             module      => 'circulation',
88             letter_code => 'AUTO_RENEWALS',
89             tables      => {
90                 borrowers => $patron->borrowernumber,
91                 issues    => $issue->itemnumber,
92                 items     => $issue->itemnumber,
93                 biblio    => $item->biblionumber,
94             },
95         );
96
97         my $library = Koha::Libraries->find( $patron->branchcode );
98         my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
99
100         C4::Letters::EnqueueLetter(
101             {   letter                 => $letter,
102                 borrowernumber         => $borrowernumber,
103                 message_transport_type => 'email',
104                 from_address           => $admin_email_address,
105             }
106         );
107     }
108 }