Bug 20920: Add missing dependency on CGI::Compile
[koha.git] / misc / cronjobs / automatic_renewals.pl
blob6a170ca20f5f0aabc81f340b2c70b25dd8d4206a
1 #!/usr/bin/perl
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>.
20 =head1 NAME
22 automatic_renewals.pl - cron script to renew loans
24 =head1 SYNOPSIS
26 ./automatic_renewals.pl [--send-notices]
28 or, in crontab:
29 0 3 * * * automatic_renewals.pl
31 =head1 DESCRIPTION
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.
37 =head1 OPTIONS
39 =over
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.
47 =back
49 =cut
51 use Modern::Perl;
52 use Pod::Usage;
53 use Getopt::Long;
55 use C4::Circulation;
56 use C4::Context;
57 use C4::Log;
58 use C4::Letters;
59 use Koha::Checkouts;
60 use Koha::Libraries;
61 use Koha::Patrons;
63 my ( $help, $send_notices );
64 GetOptions(
65 'h|help' => \$help,
66 'send-notices' => \$send_notices,
67 ) || pod2usage(1);
69 pod2usage(0) if $help;
71 cronlogaction();
73 my $auto_renews = Koha::Checkouts->search({ auto_renew => 1 });
75 my %report;
76 while ( my $auto_renew = $auto_renews->next ) {
78 # CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script
79 my ( $ok, $error ) = CanBookBeRenewed( $auto_renew->borrowernumber, $auto_renew->itemnumber );
80 if ( $error eq 'auto_renew' ) {
81 my $date_due = AddRenewal( $auto_renew->borrowernumber, $auto_renew->itemnumber, $auto_renew->branchcode );
82 $auto_renew->auto_renew_error(undef)->store;
83 push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew;
84 } elsif ( $error eq 'too_many'
85 or $error eq 'on_reserve'
86 or $error eq 'restriction'
87 or $error eq 'overdue'
88 or $error eq 'auto_account_expired'
89 or $error eq 'auto_too_late'
90 or $error eq 'auto_too_much_oweing'
91 or $error eq 'auto_too_soon' ) {
92 if ( not $auto_renew->auto_renew_error or $error ne $auto_renew->auto_renew_error ) {
93 $auto_renew->auto_renew_error($error)->store;
94 push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
95 if $error ne 'auto_too_soon'; # Do not notify if it's too soon
100 if ( $send_notices ) {
101 for my $borrowernumber ( keys %report ) {
102 my $patron = Koha::Patrons->find($borrowernumber);
103 for my $issue ( @{ $report{$borrowernumber} } ) {
104 my $item = Koha::Items->find( $issue->itemnumber );
105 my $letter = C4::Letters::GetPreparedLetter(
106 module => 'circulation',
107 letter_code => 'AUTO_RENEWALS',
108 tables => {
109 borrowers => $patron->borrowernumber,
110 issues => $issue->itemnumber,
111 items => $issue->itemnumber,
112 biblio => $item->biblionumber,
116 my $library = Koha::Libraries->find( $patron->branchcode );
117 my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
119 C4::Letters::EnqueueLetter(
120 { letter => $letter,
121 borrowernumber => $borrowernumber,
122 message_transport_type => 'email',
123 from_address => $admin_email_address,