a3bded3aea57c698519d8565fc0865ef70c489ef
[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 [-c|--confirm] [--send-notices]
27
28 or, in crontab:
29 0 3 * * * automatic_renewals.pl -c
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 =over
40
41 =item B<--send-notices>
42
43 Send AUTO_RENEWALS notices to patrons if the auto renewal has been done.
44
45 Note that this option does not support digest yet.
46
47 =item B<-v|--verbose>
48
49 Print report to standard out.
50
51 =item B<-c|--confirm>
52
53 Without this parameter no changes will be made
54
55 =back
56
57 =cut
58
59 use Modern::Perl;
60 use Pod::Usage;
61 use Getopt::Long;
62
63 use Koha::Script -cron;
64 use C4::Circulation;
65 use C4::Context;
66 use C4::Log;
67 use C4::Letters;
68 use Koha::Checkouts;
69 use Koha::Libraries;
70 use Koha::Patrons;
71
72 my ( $help, $send_notices, $verbose, $confirm );
73 GetOptions(
74     'h|help' => \$help,
75     'send-notices' => \$send_notices,
76     'v|verbose'    => \$verbose,
77     'c|confirm'     => \$confirm,
78 ) || pod2usage(1);
79
80 pod2usage(0) if $help;
81 cronlogaction();
82
83 my $auto_renews = Koha::Checkouts->search({ auto_renew => 1, 'borrower.autorenewal' => 1 },{ join => 'borrower'});
84
85 my %report;
86 $verbose = 1 unless $verbose or $confirm;
87 print "Test run only\n" unless $confirm;
88 while ( my $auto_renew = $auto_renews->next ) {
89
90     # CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script
91     my ( $ok, $error ) = CanBookBeRenewed( $auto_renew->borrowernumber, $auto_renew->itemnumber );
92     if ( $error eq 'auto_renew' ) {
93         if ($verbose) {
94             say sprintf "Issue id: %s for borrower: %s and item: %s ". ( $confirm ? 'will' : 'would') . " be renewed.",
95               $auto_renew->issue_id, $auto_renew->borrowernumber, $auto_renew->itemnumber;
96         }
97         if ($confirm){
98             my $date_due = AddRenewal( $auto_renew->borrowernumber, $auto_renew->itemnumber, $auto_renew->branchcode );
99             $auto_renew->auto_renew_error(undef)->store;
100         }
101         push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew;
102     } elsif ( $error eq 'too_many'
103         or $error eq 'on_reserve'
104         or $error eq 'restriction'
105         or $error eq 'overdue'
106         or $error eq 'auto_account_expired'
107         or $error eq 'auto_too_late'
108         or $error eq 'auto_too_much_oweing'
109         or $error eq 'auto_too_soon'
110         or $error eq 'item_denied_renewal' ) {
111         if ( $verbose ) {
112             say sprintf "Issue id: %s for borrower: %s and item: %s ". ( $confirm ? 'will' : 'would') . " not be renewed. (%s)",
113               $auto_renew->issue_id, $auto_renew->borrowernumber, $auto_renew->itemnumber, $error;
114         }
115         if ( not $auto_renew->auto_renew_error or $error ne $auto_renew->auto_renew_error ) {
116             $auto_renew->auto_renew_error($error)->store if $confirm;
117             push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
118               if $error ne 'auto_too_soon';    # Do not notify if it's too soon
119         }
120     }
121 }
122
123 if ( $send_notices ) {
124     for my $borrowernumber ( keys %report ) {
125         my $patron = Koha::Patrons->find($borrowernumber);
126         for my $issue ( @{ $report{$borrowernumber} } ) {
127             my $item   = Koha::Items->find( $issue->itemnumber );
128             my $letter = C4::Letters::GetPreparedLetter(
129                 module      => 'circulation',
130                 letter_code => 'AUTO_RENEWALS',
131                 tables      => {
132                     borrowers => $patron->borrowernumber,
133                     issues    => $issue->itemnumber,
134                     items     => $issue->itemnumber,
135                     biblio    => $item->biblionumber,
136                 },
137                 lang => $patron->lang,
138             );
139
140             my $library = Koha::Libraries->find( $patron->branchcode );
141             my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
142
143             C4::Letters::EnqueueLetter(
144                 {   letter                 => $letter,
145                     borrowernumber         => $borrowernumber,
146                     message_transport_type => 'email',
147                     from_address           => $admin_email_address,
148                 }
149             ) if $confirm;
150         }
151     }
152 }