2 #-----------------------------------
3 # Script Name: overduenotices.pl
6 # Author: Stephen Hedges (shedges@skemotah.com)
7 # modified by Paul Poulain (paul@koha-fr.org)
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.
18 # 1.0 2003/9/7: original version
19 #-----------------------------------
20 # Copyright 2003 Skemotah Solutions
22 # This file is part of Koha.
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
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.
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
39 # find Koha's Perl modules
40 # test carefully before changing this
42 eval { require "$FindBin::Bin/../kohalib.pl" };
46 use Mail::Sendmail; # comment out if not doing e-mail notices
49 my ($confirm, $nomail);
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.
63 Do you wish to continue? (y/n)
66 exit unless (/^y/i); # comment these lines out once you've made the changes
70 # BEGINNING OF PARAMETERS
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";
87 open OUTFILE, ">overdues" or die "Cannot open file overdues: $!";
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
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 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 TO_DAYS(NOW())-TO_DAYS(date_due) BETWEEN 0 and 500");
101 my $count = 0; # to keep track of how many notices are printed
102 my $e_count = 0; # and e-mailed
104 my ($itemcount,$borrowernumber,$firstname,$lastname,$address1,$address2,$city,$postcode,$email);
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;
117 $sth2->execute($borrowernumber);
119 my ($title, $author, $barcode);
120 while (($title, $author, $barcode) = $sth2->fetchrow){
121 $titles .= " ".($title?$title:"")." ".($author?$author:"")." ".($barcode?$barcode:"")."\n";
123 $notice =~ s/\<titles\>/$titles/g;
125 # if not using e-mail notices, comment out the following lines
126 if ($email) { # or you might check for borrowers.preferredcont
128 print "TO => $email\n";
129 print "FROM => $from\n";
130 print "SUBJECT => $mailtitle\n";
131 print "MESSAGE => $notice\n";
133 my %mail = ( To => $email,
135 Subject => $mailtitle,
137 'Content-Type' => 'text/plain; charset="utf8"',
143 print OUTFILE $notice;
145 } # and comment this one out, too, if not using e-mail
150 # if some notices have to be printed & managed by the library, send them to library mail address.
152 open ODUES, "overdues" or die "Cannot open file overdues: $!";
153 my $notice = "$e_count overdue notices e-mailed\n";
154 $notice .= "$count overdue notices in file for printing\n\n";
158 print "TO => $email\n" if $email;
159 print "FROM => $from\n";
160 print "SUBJECT => Koha overdue\n";
161 print "MESSAGE => $notice\n";
163 my %mail = ( To => $email,
165 Subject => 'Koha overdues',
167 'Content-Type' => 'text/plain; charset="utf8"',