merging 2.2 branch with head. Sorry for not making it before, many many commits done...
[koha.git] / misc / 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 use C4::Context;
39 use C4::Date;
40 use Mail::Sendmail;  # comment out if not doing e-mail notices
41
42 print "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.\nYou MUST edit this script for your library BEFORE you run it for the first time!\nSee the comments in the script for directions on changing the script.\n\nDo you wish to continue? (y/n) ";
43 chomp($_ = <STDIN>);
44 die unless (/^y/i);  # comment these lines out once you've made the changes
45
46 #
47 # BEGINNING OF PARAMETERS
48 #
49 my $mindays = 7 # the notice will be sent after mindays days (grace period)
50 my $maxdays = 30 # issues being more than maxdays late are managed somewhere else. (borrower probably suspended)
51 my $smtpserver = 'smtp.yourserver.com'; # your smtp server (the server who sent mails)
52 my $from = 'library@yourname.org';
53 my $mailtitle = 'Overdue library items';
54 my $librarymail = 'library@yourname.org'; # all notices without mail are sent (in 1 mail) to this mail address. They must then be managed manually.
55 # this parameter (the last) is the text of the mail that is sent.
56 # this text contains fields that are replaced by their value. Those fields must be written between brackets
57 # The following fields are available :
58 # <date> <itemcount> <firstname> <lastname> <address1> <address2> <address3> <city> <postcode>
59 my $mailtext = "\n\n\nDear library borrower\n\n\n       <date>\n\n       According to our records, you have <itemcount> items that are at\n       least a week overdue for return to the library or renewal.\n       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";
60 #
61 # END OF PARAMETERS
62 #
63 open OUTFILE, ">overdues" or die "Cannot open file overdues: $!";
64
65 # set the e-mail server -- comment out if not doing e-mail notices
66 unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , $smtpserver;
67 #                                         set your own mail server name here
68
69 my $dbh = C4::Context->dbh;
70 my $sth = $dbh->prepare ("SELECT issues.borrowernumber,firstname,surname,streetaddress,physstreet,city,zipcode,emailaddress FROM issues,borrowers,categorie WHERE returndate IS NULL AND TO_DAYS(NOW())-TO_DAYS(date_due) BETWEEN $mindays and $maxdays AND issues.borrowernumber=borrowers.borrowernumber and borrowers.categorycode=categories.categorycode and categories.overduenoticerequired=1 ORDER BY issues.borrowernumber");
71 my $first_borrno = $dbh->prepare ("SELECT borrowernumber FROM issues WHERE returndate IS NULL AND TO_DAYS(NOW())-TO_DAYS(date_due) BETWEEN $mindays and $maxdays ORDER BY borrowernumber");
72 my $get_date = $dbh->prepare ("SELECT CURDATE()");
73
74 $get_date->execute;
75 my $daydate = $get_date->fetchrow_arrayref;
76 my $rawdate = $daydate->[0];
77 my $date = format_date($rawdate);
78 $first_borrno->execute;               # get first borrowernumber
79 my $firstborr = $first_borrno->fetchrow_arrayref;
80 my $borrowernumber = $firstborr->[0];
81 $first_borrno->finish;  
82
83 $sth->execute;
84
85 my $itemcount = 0;
86 my $row;
87 my $count = 0;   # to keep track of how many notices are printed
88 my $e_count = 0;   # and e-mailed
89 my ($firstname,$lastname,$address1,$address2,$city,$postcode,$email);
90
91 while ($row = $sth->fetchrow_arrayref) {
92     my $borrno = $row->[0];
93     if ($itemcount==0) {    # store values for first borrower
94                 $firstname = $row->[1];
95                 $lastname = $row->[2];
96                 $address1 = $row->[3];
97                 $address2 = $row->[4];
98                 unless ($address2) {
99                         $address2 = '';
100                 }
101                 $city = $row->[5];
102                 unless ($city) {
103                         $city = '';
104                 }
105                 $postcode = $row->[6];
106                 unless ($postcode) {
107                         $postcode = '';
108                 }
109                 $email = $row->[7];
110     }
111     if ($borrno == $borrowernumber) {     # next borrower yet?
112                 $itemcount++;
113                 next;
114     } else {
115                 $borrowernumber = $borrno;
116                 my $notice = $mailtext;
117                 $notice =~ s/\<date\>/$date/g;
118                 $notice =~ s/\<itemcount\>/$itemcount/g;
119                 $notice =~ s/\<firstname\>/$firstname/g;
120                 $notice =~ s/\<lastname\>/$lastname/g;
121                 $notice =~ s/\<address1\>/$address1/g;
122                 $notice =~ s/\<address2\>/$address2/g;
123                 $notice =~ s/\<address3\>/$address3/g;
124                 $notice =~ s/\<city\>/$city/g;
125                 $notice =~ s/\<postcode\>/$postcode/g;
126         
127         # if not using e-mail notices, comment out the following lines
128                 if ($email) {   # or you might check for borrowers.preferredcont 
129                         my %mail = ( To      => $email,
130                                                         From    => $from,
131                                                         Subject => $mailtitle,
132                                                         Message => $notice,
133                                 );
134                         sendmail(%mail);
135                         $e_count++
136                 } else {
137                 # if not using e-mail notices, comment out the above lines
138                         print $notice;
139                         print OUTFILE $notice;
140                         $count++;
141                 }    # and comment this one out, too, if not using e-mail
142
143                 $itemcount = 1;   #start the count for next notice
144                 $firstname = $row->[1]; # and store the new values
145                 $lastname = $row->[2];
146                 $address1 = $row->[3];
147                 $address2 = $row->[4];
148                 unless ($address2) {
149                         $address2 = '';
150                 }
151                 $city = $row->[5];
152                 unless ($city) {
153                         $city = '';
154                 }
155                 $postcode = $row->[6];
156                 unless ($postcode) {
157                         $postcode = '';
158                 }
159                 $email = $row->[7];
160         }
161 }
162 $sth->finish;
163 close OUTFILE;
164 # if some notices have to be printed & managed by the library, send them to library mail address.
165 if ($count) {
166                 open ODUES, "overdues" or die "Cannot open file overdues: $!";
167                 my $notice = "$e_count overdue notices e-mailed\n";
168                 my $notice .= "$count overdue notices in file for printing\n\n";
169
170                 my $notice .= <ODUES>;
171             my %mail = ( To      => $email,
172                          From    => $from,
173                          Subject => 'Koha overdues',
174                          Message => $notice,
175                          );
176             sendmail(%mail);
177