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
40 use Mail::Sendmail; # comment out if not doing e-mail notices
43 my ($confirm, $nomail);
50 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.
51 You MUST edit this script for your library BEFORE you run it for the first time!
52 See the comments in the script for directions on changing the script.
53 This script has 2 parameters :
54 -c to confirm and remove this help & warning
55 -n to avoid sending any mail. Instead, all mail messages are printed on screen. Usefull for testing purposes.
57 Do you wish to continue? (y/n)
60 exit unless (/^y/i); # comment these lines out once you've made the changes
64 # BEGINNING OF PARAMETERS
66 my $mindays = 7; # the notice will be sent after mindays days (grace period)
67 my $maxdays = 30; # issues being more than maxdays late are managed somewhere else. (borrower probably suspended)
68 my $smtpserver = 'smtp.server.com'; # your smtp server (the server who sent mails)
69 my $from = 'librarianname@library.com'; # all the mails sent to the borrowers will appear coming from here.
70 my $mailtitle = 'Overdues'; # the title of the mails
71 my $librarymail = 'librarystaff@library.com'; # all notices without mail are sent (in 1 mail) to this mail address. They must then be managed manually.
72 # this parameter (the last) is the text of the mail that is sent.
73 # this text contains fields that are replaced by their value. Those fields must be written between brackets
74 # The following fields are available :
75 # <date> <itemcount> <firstname> <lastname> <address1> <address2> <address3> <city> <postcode>
76 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
77 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";
81 open OUTFILE, ">overdues" or die "Cannot open file overdues: $!";
83 # set the e-mail server -- comment out if not doing e-mail notices
84 unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , $smtpserver;
85 # set your own mail server name here
87 my $dbh = C4::Context->dbh;
88 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");
89 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");
95 my $count = 0; # to keep track of how many notices are printed
96 my $e_count = 0; # and e-mailed
98 my ($itemcount,$borrnum,$firstname,$lastname,$address1,$address2,$city,$postcode,$email);
100 while (($itemcount,$borrnum,$firstname,$lastname,$address1,$address2,$city,$postcode,$email) = $sth->fetchrow) {
101 my $notice = $mailtext;
102 $notice =~ s/\<itemcount\>/$itemcount/g;
103 $notice =~ s/\<firstname\>/$firstname/g;
104 $notice =~ s/\<lastname\>/$lastname/g;
105 $notice =~ s/\<address1\>/$address1/g;
106 $notice =~ s/\<address2\>/$address2/g;
107 $notice =~ s/\<city\>/$city/g;
108 $notice =~ s/\<postcode\>/$postcode/g;
109 $notice =~ s/\<date\>/$date/g;
111 $sth2->execute($borrnum);
113 my ($title, $author, $barcode);
114 while (($title, $author, $barcode) = $sth2->fetchrow){
115 $titles .= " ".($title?$title:"")." ".($author?$author:"")." ".($barcode?$barcode:"")."\n";
117 $notice =~ s/\<titles\>/$titles/g;
119 # if not using e-mail notices, comment out the following lines
120 if ($email) { # or you might check for borrowers.preferredcont
122 print "TO => $email\n";
123 print "FROM => $from\n";
124 print "SUBJECT => $mailtitle\n";
125 print "MESSAGE => $notice\n";
127 my %mail = ( To => $email,
129 Subject => $mailtitle,
136 print OUTFILE $notice;
138 } # and comment this one out, too, if not using e-mail
143 # if some notices have to be printed & managed by the library, send them to library mail address.
145 open ODUES, "overdues" or die "Cannot open file overdues: $!";
146 my $notice = "$e_count overdue notices e-mailed\n";
147 $notice .= "$count overdue notices in file for printing\n\n";
151 print "TO => $email\n" if $email;
152 print "FROM => $from\n";
153 print "SUBJECT => Koha overdue\n";
154 print "MESSAGE => $notice\n";
156 my %mail = ( To => $email,
158 Subject => 'Koha overdues',