3 # This file is part of Koha.
5 # Copyright (C) 2014 Hochschule für Gesundheit (hsg), Germany
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.
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.
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>.
22 automatic_renewals.pl - cron script to renew loans
26 ./automatic_renewals.pl [--send-notices]
29 0 3 * * * automatic_renewals.pl
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.
41 =item B<--send-notices>
43 Send AUTO_RENEWALS notices to patrons if the auto renewal has been done.
45 Note that this option does not support digest yet.
49 Print report to standard out.
53 Without this parameter no changes will be made
63 use Koha::Script -cron;
72 my ( $help, $send_notices, $verbose, $commit );
75 'send-notices' => \$send_notices,
76 'v|verbose' => \$verbose,
77 'c|commit' => \$commit,
80 pod2usage(0) if $help;
83 my $auto_renews = Koha::Checkouts->search({ auto_renew => 1 });
86 print "Test run only\n" unless $commit;
87 while ( my $auto_renew = $auto_renews->next ) {
89 # CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script
90 my ( $ok, $error ) = CanBookBeRenewed( $auto_renew->borrowernumber, $auto_renew->itemnumber );
91 if ( $error eq 'auto_renew' ) {
93 my $date_due = AddRenewal( $auto_renew->borrowernumber, $auto_renew->itemnumber, $auto_renew->branchcode );
94 $auto_renew->auto_renew_error(undef)->store;
96 $verbose && print "Issue id: " . $auto_renew->issue_id . " for borrower: " . $auto_renew->borrowernumber . " and item: " . $auto_renew->itemnumber;
97 $verbose && $commit ? print " will be renewed.\n" : print " would be renewed.\n";
98 push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew;
99 } elsif ( $error eq 'too_many'
100 or $error eq 'on_reserve'
101 or $error eq 'restriction'
102 or $error eq 'overdue'
103 or $error eq 'auto_account_expired'
104 or $error eq 'auto_too_late'
105 or $error eq 'auto_too_much_oweing'
106 or $error eq 'auto_too_soon'
107 or $error eq 'item_denied_renewal' ) {
109 print "Issue id: " . $auto_renew->issue_id . " for borrower: " . $auto_renew->borrowernumber . " and item: " . $auto_renew->itemnumber;
110 $commit ? print " will not be renewed " : print " would not be renewed ";
113 if ( not $auto_renew->auto_renew_error or $error ne $auto_renew->auto_renew_error ) {
114 $auto_renew->auto_renew_error($error)->store if $commit;
115 push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
116 if $error ne 'auto_too_soon'; # Do not notify if it's too soon
121 if ( $send_notices ) {
122 for my $borrowernumber ( keys %report ) {
123 my $patron = Koha::Patrons->find($borrowernumber);
124 for my $issue ( @{ $report{$borrowernumber} } ) {
125 my $item = Koha::Items->find( $issue->itemnumber );
126 my $letter = C4::Letters::GetPreparedLetter(
127 module => 'circulation',
128 letter_code => 'AUTO_RENEWALS',
130 borrowers => $patron->borrowernumber,
131 issues => $issue->itemnumber,
132 items => $issue->itemnumber,
133 biblio => $item->biblionumber,
135 lang => $patron->lang,
138 my $library = Koha::Libraries->find( $patron->branchcode );
139 my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
141 C4::Letters::EnqueueLetter(
143 borrowernumber => $borrowernumber,
144 message_transport_type => 'email',
145 from_address => $admin_email_address,