3 # This script loops through each overdue item, determines the fine,
4 # and updates the total amount of fines due by each user. It relies on
5 # the existence of /tmp/fines, which is created by ???
6 # Doesnt really rely on it, it relys on being able to write to /tmp/
7 # It creates the fines file
9 # This script is meant to be run nightly out of cron.
11 # Copyright 2000-2002 Katipo Communications
13 # This file is part of Koha.
15 # Koha is free software; you can redistribute it and/or modify it under the
16 # terms of the GNU General Public License as published by the Free Software
17 # Foundation; either version 2 of the License, or (at your option) any later
20 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
21 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License along with
25 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26 # Suite 330, Boston, MA 02111-1307 USA
28 # $Id: sendoverdues.pl,v 1.1.2.1 2007/03/26 22:38:09 tgarip1957 Exp $
31 # find Koha's Perl modules
32 # test carefully before changing this
34 eval { require "$FindBin::Bin/../kohalib.pl" };
40 use C4::Circulation::Fines;
42 use C4::Dates qw/format_date/;
43 use HTML::Template::Pro;
45 use Mail::RFC822::Address;
48 my ($res,$ua);##variables for SMS
51 my $dbh = C4::Context->dbh;
58 # Look up the overdues for today.
59 # Capture overdues which fall on our dates of interest.
61 ####################################################################################################
62 # Creating a big hash of available templates
64 %email->{'template'}='email-2.txt';
66 %sms->{'template'}='sms-2.txt';
69 my %firstReminder->{'email'} = \%email;
70 %firstReminder->{'sms'} = \%sms;
73 %email2->{'template'}='email-7.txt';
74 my %secondReminder->{'email'} = \%email2;
76 %sms2->{'template'}='sms-7.txt';
77 %secondReminder->{'sms'} = \%sms2;
79 %letter2->{'template'}='letter-7.html';
80 %secondReminder->{'letter'} = \%letter2;
84 %email3->{'template'}='email-29.txt';
86 %sms3->{'template'}='sms-29.txt';
88 %letter3->{'template'}='letter-29.html';
90 my %finalReminder->{'email'} = \%email3;
91 %finalReminder->{'letter'} = \%letter3;
92 %finalReminder->{'sms'} = \%sms3;
95 %actions->{'1'}=\%firstReminder;
96 %actions->{'3'}=\%secondReminder;###This was 7 days changed to 3 days
97 %actions->{'20'}=\%finalReminder;###This was 29 days changed to 20 days
99 ##################################################################################################################
100 my @overdues2;#an array for each actiondate
107 # Retrieve an array of overdues.
108 my ($count, $overduesReference) = Getoverdues();
109 my @overdues=@$overduesReference;
112 # We're going to build a hash of arrays, containing the items requiring action.
113 # ->borrowernumber, date, @overdues
117 foreach my $overdue (@overdues) {
118 my $due_day=$overdue->{'date_due'};
120 my $difference=DATE_subtract($date,$due_day);
121 # If does this item fall on a day of interest?
122 $overdue->{'difference'}=$difference;
123 foreach my $actiondate (keys(%actions)) {
124 if ($actiondate == $difference) {
125 $filename='overdues'.$actiondate;
126 push @$$filename,$overdue;
127 #print "$actiondate,-,$overdue->{borrowernumber}\n";
129 $actionItems{$actiondate} = \@$$filename;
133 # We now have a hash containing overdues which need actioning, we can step through each set.
134 # Work from earilest to latest. We only wish to send the most urgent message.
138 foreach my $actiondate (keys %actions) {
139 # print "\n\nThe following items are $actiondate days overdue.\n";
140 my $items = $actionItems{$actiondate};
141 $filename='overdues'.$actiondate;
142 foreach my $overdue (@$$filename) {
143 # Detemine which borrower is responsible for this overdue;
144 # if the offender is a child, then the guarantor is the person to notify
145 my $borrowernumber=$overdue->{borrowernumber};
147 my $borrower=responsibleBorrower($borrowernumber);
148 my ($method, $address) = preferedContactMethod($borrower);
149 if ($method && $address) {
151 # Do we have to send something, using this method on this day?
152 if (%actions->{$actiondate}->{$method}->{'template'}) {
153 my $intranetdir=C4::Context->config('intranetdir');
154 # Template the message
155 my $template = HTML::Template::Pro->new(filename => $intranetdir.'/scripts/misc/notifys/templates/'.%actions->{$actiondate}->{$method}->{'template'}, die_on_bad_params => 0);
158 my $item = getiteminformation("", $overdue->{'itemnumber'});
159 $row_data{'BARCODE'}=$item->{'barcode'};
161 my $title=substr($item->{'title'},0,25)."...";
163 $title=changecharacter($title);
164 $row_data{'TITLE'}=$title;
165 $row_data{'DATE_DUE'}=format_date($overdue->{'date_due'});
166 $row_data{'cardnumber'}=$borrower->{'cardnumber'};
167 push(@bookdetails, \%row_data);
168 $template->param(BOOKDETAILS => \@bookdetails);
169 my $name= "$borrower->{'firstname'} $borrower->{'surname'}";
170 $template->param(NAME=> $name);
171 %messages->{$borrower->{'borrowernumber'}} = $template->output();
172 if ($method eq 'email') {
173 $result = sendEmail($address, 'library@library.neu.edu.tr', 'Overdue Library Items', %messages->{$borrowernumber});
174 logContact($borrowernumber, $method, $address, $result, %messages->{$borrowernumber});
176 elsif ($method eq 'sms') {
177 $result = sendSMS($address, %messages->{$borrowernumber});
178 logContact($borrowernumber, $method, $address, $result, %messages->{$borrowernumber});
180 elsif ($method eq 'letter') {
181 $result = printLetter($address, %messages->{$borrowernumber});
185 print "$borrowernumber has an overdue item, but no means of contact\n";
189 } #end of 'foreach overdue'
191 } # end of foreach actiondate
194 sub responsibleBorrower {
195 # Given an overdue item, return the details of the borrower responible as a hash of database columns.
196 my $borrowernumber=shift;
198 if ($borrowernumber) {
199 my $borrower=BorType($borrowernumber);
200 # Overdue books assigned to children have notices sent to the guarantor.
201 if ($borrower->{'categorycode'} eq 'C') {
202 my $guarantor=BorType($borrower->{'guarantor'});
203 $borrower = $guarantor;
219 sub preferedContactMethod {
220 # Given a reference to borrower details, in the format
221 # returned by BorType(), determine the prefered contact method, and address to use.
223 my $borrcat = getborrowercategoryinfo($borrower->{'categorycode'});
224 if( !$borrcat->{'overduenoticerequired'}){
225 return (undef,undef);
229 ## if borrower has a phone set that as our preferrred contact
230 if ($borrower->{'phoneday'}) {
231 if (parse_phone($borrower->{phoneday})){
232 $address = parse_phone($borrower->{phoneday});
234 return ($method, $address);
238 if (($borrower->{'emailaddress'}) and (Mail::RFC822::Address::valid($borrower->{'emailaddress'}))) {
239 $address = $borrower->{'emailaddress'};
241 return ($method, $address);
244 if ($borrower->{'streetaddress'}) {
245 $address = mailingAddress($borrower);
248 #print "$method, $address\n";
249 return ($method, $address);
260 # Given the details of an attempt to contact a borrower,
261 # log them in the attempted_contacts table of the koha database.
262 my ($borrowernumber, $method, $address, $result, $message) = @_;
264 my $querystring = " insert into attempted_contacts
265 (borrowernumber, method, address, result, message, date)
266 values (?, ?, ?, ?, ?, now())";
267 my $sth= $dbh->prepare($querystring);
268 $sth->execute($borrowernumber, $method, $address, $result, $message);
280 # Given a hash of borrower information, such as that returned by BorType,
281 # return a mailing address.
284 my $address = $borrower->{'firstname'}."\n".
285 $borrower->{'streetaddress'}."\n".
286 $borrower->{'streetcity'};
294 # Given an email address, and a subject and message, attempt to send email.
300 # print "in email area";
302 # print "\nSending Email To: $to\n$message\n";
304 my %mail = ( To => $to,
305 CC => 'library@library.neu.edu.tr',
308 Message => $message);
311 if (not(sendmail %mail)) {
312 warn $Mail::Sendmail::error;
313 warn "sendEmail to $to failed.";
322 my ($phone, $message)=@_;
323 ($res,$ua)=get_sms_auth() unless $res;
324 # Given a cell number and a message, attempt to send an SMS message.
325 my $sendresult=send_sms($ua,$phone,$message,$res->{pSessionId});
326 my $error=error_codes($sendresult->{pErrCode});
327 return 1 unless $error;
335 # FIXME - decide where to print
338 sub changecharacter {