installer: command-line scripts improve finding C4 modules
[koha.git] / misc / cronjobs / overduenotices.pl
1 #!/usr/bin/perl -w
2 #-----------------------------------
3 # Script Name: overduenotices.pl
4 # Script Version: 1.0
5 # Date:  2003/9/7
6 # Author:  Stephen Hedges (shedges@skemotah.com)
7 # modified by Paul Poulain (paul@koha-fr.org)
8 # Description: 
9 #       This script runs a Koha report of items that
10 #       are between 7 and 30 days overdue and generates
11 #       a file that may be dumped to a printer.  The time period
12 #       may be changed by editing the SQL statement handle
13 #       prepared in line 52.  The actual wording of the overdue
14 #       notices may be changed by editing the $notice variable
15 #       in line 101.  The current notice text is formatted to
16 #       fit the standard 34-line 'Speedimailer' form.
17 # Revision History:
18 #    1.0  2003/9/7: original version
19 #-----------------------------------
20 # Copyright 2003 Skemotah Solutions
21 #
22 # This file is part of Koha.
23 #
24 # Koha is free software; you can redistribute it and/or modify it under the
25 # terms of the GNU General Public License as published by the Free Software
26 # Foundation; either version 2 of the License, or (at your option) any later
27 # version.
28 #
29 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
30 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
31 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
32 #
33 # You should have received a copy of the GNU General Public License along with
34 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
35 # Suite 330, Boston, MA  02111-1307 USA
36
37 use strict;
38 BEGIN {
39     # find Koha's Perl modules
40     # test carefully before changing this
41     use FindBin;
42     eval { require "$FindBin::Bin/../kohalib.pl" };
43 }
44 use C4::Context;
45 use C4::Dates;
46 use Mail::Sendmail;  # comment out if not doing e-mail notices
47 use Getopt::Long;
48
49 my ($confirm, $nomail);
50 GetOptions(
51     'c'    => \$confirm,
52         'n'     => \$nomail,
53 );
54 unless ($confirm) {
55         print qq|
56 This script will send overdue notices by e-mail and prepare a file of\nnotices for printing if the borrower does not have e-mail.
57 You MUST edit this script for your library BEFORE you run it for the first time!
58 See the comments in the script for directions on changing the script.
59 This script has 2 parameters :
60         -c to confirm and remove this help & warning
61         -n to avoid sending any mail. Instead, all mail messages are printed on screen. Usefull for testing purposes.
62
63 Do you wish to continue? (y/n)
64 |;
65         chomp($_ = <STDIN>);
66         exit unless (/^y/i);  # comment these lines out once you've made the changes
67         
68 }
69 #
70 # BEGINNING OF PARAMETERS
71 #
72 my $mindays = 7; # the notice will be sent after mindays days (grace period)
73 my $maxdays = 30; # issues being more than maxdays late are managed somewhere else. (borrower probably suspended)
74 my $smtpserver = 'smtp.server.com'; # your smtp server (the server who sent mails)
75 my $from = 'librarianname@library.com'; # all the mails sent to the borrowers will appear coming from here.
76 my $mailtitle = 'Overdues'; # the title of the mails
77 my $librarymail = 'librarystaff@library.com'; # all notices without mail are sent (in 1 mail) to this mail address. They must then be managed manually.
78 # this parameter (the last) is the text of the mail that is sent.
79 # this text contains fields that are replaced by their value. Those fields must be written between brackets
80 # The following fields are available :
81 # <date> <itemcount> <firstname> <lastname> <address1> <address2> <address3> <city> <postcode>
82 my $mailtext = "\n\n\nDear library borrower\n\n\n       <date>\n\n       According to our records, you have <itemcount> items, the description of which follows, that are at\n       least a week overdue for return to the library or renewal:\n               title           author          barcode\n<titles>\n
83        If you have registered a password with the library, you may use it\n       and your library card to login at http://XXX.org\n       to check the status of your account, or you may call any of our branch\n       Please be advised that all library services will be blocked\n       if items are allowed to go more than 30 days overdue.\n\n       Thank you for using your public libraries.\n\n\n                                             <firstname> <lastname>\n                                             <address1>\n                                             <address2>\n                                             <city>  <postcode>\n\n\n\n\n\n";
84 #
85 # END OF PARAMETERS
86 #
87 open OUTFILE, ">overdues" or die "Cannot open file overdues: $!";
88
89 # set the e-mail server -- comment out if not doing e-mail notices
90 unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , $smtpserver;
91 #                                         set your own mail server name here
92
93 my $dbh = C4::Context->dbh;
94 my $sth = $dbh->prepare ("SELECT COUNT(*), issues.borrowernumber,firstname,surname,streetaddress,physstreet,city,zipcode,emailaddress FROM issues,borrowers,categories WHERE returndate IS NULL AND TO_DAYS(NOW())-TO_DAYS(date_due) BETWEEN 0 and 500 AND issues.borrowernumber=borrowers.borrowernumber and borrowers.categorycode=categories.categorycode and categories.overduenoticerequired=1 group by issues.borrowernumber");
95 my $sth2 = $dbh->prepare("SELECT biblio.title,biblio.author,items.barcode FROM issues,items,biblio WHERE items.itemnumber=issues.itemnumber and biblio.biblionumber=items.biblionumber AND issues.borrowernumber=? AND returndate IS NULL AND TO_DAYS(NOW())-TO_DAYS(date_due) BETWEEN 0 and 500");
96
97 $sth->execute;
98
99 # my $itemcount = 0;
100 # my $row;
101 my $count = 0;   # to keep track of how many notices are printed
102 my $e_count = 0;   # and e-mailed
103 my $date=localtime;
104 my ($itemcount,$borrowernumber,$firstname,$lastname,$address1,$address2,$city,$postcode,$email);
105
106 while (($itemcount,$borrowernumber,$firstname,$lastname,$address1,$address2,$city,$postcode,$email) = $sth->fetchrow) {
107                 my $notice = $mailtext;
108                 $notice =~ s/\<itemcount\>/$itemcount/g;
109                 $notice =~ s/\<firstname\>/$firstname/g;
110                 $notice =~ s/\<lastname\>/$lastname/g;
111                 $notice =~ s/\<address1\>/$address1/g;
112                 $notice =~ s/\<address2\>/$address2/g;
113                 $notice =~ s/\<city\>/$city/g;
114                 $notice =~ s/\<postcode\>/$postcode/g;
115                 $notice =~ s/\<date\>/$date/g;
116
117                 $sth2->execute($borrowernumber);
118                 my $titles="";
119                 my ($title, $author, $barcode);
120                 while (($title, $author, $barcode) = $sth2->fetchrow){
121                         $titles .= "            ".($title?$title:"")."  ".($author?$author:"")."        ".($barcode?$barcode:"")."\n";
122                 }
123                 $notice =~ s/\<titles\>/$titles/g;
124                 $sth2->finish;
125         # if not using e-mail notices, comment out the following lines
126                 if ($email) {   # or you might check for borrowers.preferredcont 
127                         if ($nomail) {
128                                 print "TO => $email\n";
129                                 print "FROM => $from\n";
130                                 print "SUBJECT => $mailtitle\n";
131                                 print "MESSAGE => $notice\n";
132                         } else {
133                                 my %mail = ( To      => $email,
134                                                                 From    => $from,
135                                                                 Subject => $mailtitle,
136                                                                 Message => $notice,
137                                         );
138                                 sendmail(%mail);
139                         }
140                         $e_count++
141                 } else {
142                         print OUTFILE $notice;
143                         $count++;
144                 }    # and comment this one out, too, if not using e-mail
145
146 }
147 $sth->finish;
148 close OUTFILE;
149 # if some notices have to be printed & managed by the library, send them to library mail address.
150 if ($count) {
151                 open ODUES, "overdues" or die "Cannot open file overdues: $!";
152                 my $notice = "$e_count overdue notices e-mailed\n";
153                 $notice .= "$count overdue notices in file for printing\n\n";
154
155                 $notice .= <ODUES>;
156                 if ($nomail) {
157                         print "TO => $email\n" if $email;
158                         print "FROM => $from\n";
159                         print "SUBJECT => Koha overdue\n";
160                         print "MESSAGE => $notice\n";
161                 } else {
162                         my %mail = ( To      => $email,
163                                                         From    => $from,
164                                                         Subject => 'Koha overdues',
165                                                         Message => $notice,
166                                 );
167                         sendmail(%mail);
168                 }
169 }