NoZebra SQL index management :
[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 use Getopt::Long;
42
43 my ($confirm, $nomail);
44 GetOptions(
45     'c'    => \$confirm,
46         'n'     => \$nomail,
47 );
48 unless ($confirm) {
49         print qq|
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.
56
57 Do you wish to continue? (y/n)
58 |;
59         chomp($_ = <STDIN>);
60         exit unless (/^y/i);  # comment these lines out once you've made the changes
61         
62 }
63 #
64 # BEGINNING OF PARAMETERS
65 #
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";
78 #
79 # END OF PARAMETERS
80 #
81 open OUTFILE, ">overdues" or die "Cannot open file overdues: $!";
82
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
86
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");
90
91 $sth->execute;
92
93 # my $itemcount = 0;
94 # my $row;
95 my $count = 0;   # to keep track of how many notices are printed
96 my $e_count = 0;   # and e-mailed
97 my $date=localtime;
98 my ($itemcount,$borrowernumber,$firstname,$lastname,$address1,$address2,$city,$postcode,$email);
99
100 while (($itemcount,$borrowernumber,$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;
110
111                 $sth2->execute($borrowernumber);
112                 my $titles="";
113                 my ($title, $author, $barcode);
114                 while (($title, $author, $barcode) = $sth2->fetchrow){
115                         $titles .= "            ".($title?$title:"")."  ".($author?$author:"")."        ".($barcode?$barcode:"")."\n";
116                 }
117                 $notice =~ s/\<titles\>/$titles/g;
118                 $sth2->finish;
119         # if not using e-mail notices, comment out the following lines
120                 if ($email) {   # or you might check for borrowers.preferredcont 
121                         if ($nomail) {
122                                 print "TO => $email\n";
123                                 print "FROM => $from\n";
124                                 print "SUBJECT => $mailtitle\n";
125                                 print "MESSAGE => $notice\n";
126                         } else {
127                                 my %mail = ( To      => $email,
128                                                                 From    => $from,
129                                                                 Subject => $mailtitle,
130                                                                 Message => $notice,
131                                         );
132                                 sendmail(%mail);
133                         }
134                         $e_count++
135                 } else {
136                         print OUTFILE $notice;
137                         $count++;
138                 }    # and comment this one out, too, if not using e-mail
139
140 }
141 $sth->finish;
142 close OUTFILE;
143 # if some notices have to be printed & managed by the library, send them to library mail address.
144 if ($count) {
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";
148
149                 $notice .= <ODUES>;
150                 if ($nomail) {
151                         print "TO => $email\n" if $email;
152                         print "FROM => $from\n";
153                         print "SUBJECT => Koha overdue\n";
154                         print "MESSAGE => $notice\n";
155                 } else {
156                         my %mail = ( To      => $email,
157                                                         From    => $from,
158                                                         Subject => 'Koha overdues',
159                                                         Message => $notice,
160                                 );
161                         sendmail(%mail);
162                 }
163 }