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 $
33 use C4::Circulation::Fines;
36 use HTML::Template::Pro;
38 use Mail::RFC822::Address;
41 my ($res,$ua);##variables for SMS
44 my $dbh = C4::Context->dbh;
51 # Look up the overdues for today.
52 # Capture overdues which fall on our dates of interest.
54 ####################################################################################################
55 # Creating a big hash of available templates
57 %email->{'template'}='email-2.txt';
59 %sms->{'template'}='sms-2.txt';
62 my %firstReminder->{'email'} = \%email;
63 %firstReminder->{'sms'} = \%sms;
66 %email2->{'template'}='email-7.txt';
67 my %secondReminder->{'email'} = \%email2;
69 %sms2->{'template'}='sms-7.txt';
70 %secondReminder->{'sms'} = \%sms2;
72 %letter2->{'template'}='letter-7.html';
73 %secondReminder->{'letter'} = \%letter2;
77 %email3->{'template'}='email-29.txt';
79 %sms3->{'template'}='sms-29.txt';
81 %letter3->{'template'}='letter-29.html';
83 my %finalReminder->{'email'} = \%email3;
84 %finalReminder->{'letter'} = \%letter3;
85 %finalReminder->{'sms'} = \%sms3;
88 %actions->{'1'}=\%firstReminder;
89 %actions->{'3'}=\%secondReminder;###This was 7 days changed to 3 days
90 %actions->{'20'}=\%finalReminder;###This was 29 days changed to 20 days
92 ##################################################################################################################
93 my @overdues2;#an array for each actiondate
100 # Retrieve an array of overdues.
101 my ($count, $overduesReference) = Getoverdues();
102 my @overdues=@$overduesReference;
105 # We're going to build a hash of arrays, containing the items requiring action.
106 # ->borrowernumber, date, @overdues
110 foreach my $overdue (@overdues) {
111 my $due_day=$overdue->{'date_due'};
113 my $difference=DATE_subtract($date,$due_day);
114 # If does this item fall on a day of interest?
115 $overdue->{'difference'}=$difference;
116 foreach my $actiondate (keys(%actions)) {
117 if ($actiondate == $difference) {
118 $filename='overdues'.$actiondate;
119 push @$$filename,$overdue;
120 #print "$actiondate,-,$overdue->{borrowernumber}\n";
122 $actionItems{$actiondate} = \@$$filename;
126 # We now have a hash containing overdues which need actioning, we can step through each set.
127 # Work from earilest to latest. We only wish to send the most urgent message.
131 foreach my $actiondate (keys %actions) {
132 # print "\n\nThe following items are $actiondate days overdue.\n";
133 my $items = $actionItems{$actiondate};
134 $filename='overdues'.$actiondate;
135 foreach my $overdue (@$$filename) {
136 # Detemine which borrower is responsible for this overdue;
137 # if the offender is a child, then the guarantor is the person to notify
138 my $borrowernumber=$overdue->{borrowernumber};
140 my $borrower=responsibleBorrower($borrowernumber);
141 my ($method, $address) = preferedContactMethod($borrower);
142 if ($method && $address) {
144 # Do we have to send something, using this method on this day?
145 if (%actions->{$actiondate}->{$method}->{'template'}) {
146 my $intranetdir=C4::Context->config('intranetdir');
147 # Template the message
148 my $template = HTML::Template::Pro->new(filename => $intranetdir.'/scripts/misc/notifys/templates/'.%actions->{$actiondate}->{$method}->{'template'}, die_on_bad_params => 0);
151 my $item = getiteminformation("", $overdue->{'itemnumber'});
152 $row_data{'BARCODE'}=$item->{'barcode'};
154 my $title=substr($item->{'title'},0,25)."...";
156 $title=changecharacter($title);
157 $row_data{'TITLE'}=$title;
158 $row_data{'DATE_DUE'}=format_date($overdue->{'date_due'});
159 $row_data{'cardnumber'}=$borrower->{'cardnumber'};
160 push(@bookdetails, \%row_data);
161 $template->param(BOOKDETAILS => \@bookdetails);
162 my $name= "$borrower->{'firstname'} $borrower->{'surname'}";
163 $template->param(NAME=> $name);
164 %messages->{$borrower->{'borrowernumber'}} = $template->output();
165 if ($method eq 'email') {
166 $result = sendEmail($address, 'library@library.neu.edu.tr', 'Overdue Library Items', %messages->{$borrowernumber});
167 logContact($borrowernumber, $method, $address, $result, %messages->{$borrowernumber});
169 elsif ($method eq 'sms') {
170 $result = sendSMS($address, %messages->{$borrowernumber});
171 logContact($borrowernumber, $method, $address, $result, %messages->{$borrowernumber});
173 elsif ($method eq 'letter') {
174 $result = printLetter($address, %messages->{$borrowernumber});
178 print "$borrowernumber has an overdue item, but no means of contact\n";
182 } #end of 'foreach overdue'
184 } # end of foreach actiondate
187 sub responsibleBorrower {
188 # Given an overdue item, return the details of the borrower responible as a hash of database columns.
189 my $borrowernumber=shift;
191 if ($borrowernumber) {
192 my $borrower=BorType($borrowernumber);
193 # Overdue books assigned to children have notices sent to the guarantor.
194 if ($borrower->{'categorycode'} eq 'C') {
195 my $guarantor=BorType($borrower->{'guarantor'});
196 $borrower = $guarantor;
212 sub preferedContactMethod {
213 # Given a reference to borrower details, in the format
214 # returned by BorType(), determine the prefered contact method, and address to use.
216 my $borrcat = getborrowercategoryinfo($borrower->{'categorycode'});
217 if( !$borrcat->{'overduenoticerequired'}){
218 return (undef,undef);
222 ## if borrower has a phone set that as our preferrred contact
223 if ($borrower->{'phoneday'}) {
224 if (parse_phone($borrower->{phoneday})){
225 $address = parse_phone($borrower->{phoneday});
227 return ($method, $address);
231 if (($borrower->{'emailaddress'}) and (Mail::RFC822::Address::valid($borrower->{'emailaddress'}))) {
232 $address = $borrower->{'emailaddress'};
234 return ($method, $address);
237 if ($borrower->{'streetaddress'}) {
238 $address = mailingAddress($borrower);
241 #print "$method, $address\n";
242 return ($method, $address);
253 # Given the details of an attempt to contact a borrower,
254 # log them in the attempted_contacts table of the koha database.
255 my ($borrowernumber, $method, $address, $result, $message) = @_;
257 my $querystring = " insert into attempted_contacts
258 (borrowernumber, method, address, result, message, date)
259 values (?, ?, ?, ?, ?, now())";
260 my $sth= $dbh->prepare($querystring);
261 $sth->execute($borrowernumber, $method, $address, $result, $message);
273 # Given a hash of borrower information, such as that returned by BorType,
274 # return a mailing address.
277 my $address = $borrower->{'firstname'}."\n".
278 $borrower->{'streetaddress'}."\n".
279 $borrower->{'streetcity'};
287 # Given an email address, and a subject and message, attempt to send email.
293 # print "in email area";
295 # print "\nSending Email To: $to\n$message\n";
297 my %mail = ( To => $to,
298 CC => 'library@library.neu.edu.tr',
301 Message => $message);
304 if (not(sendmail %mail)) {
305 warn $Mail::Sendmail::error;
306 warn "sendEmail to $to failed.";
315 my ($phone, $message)=@_;
316 ($res,$ua)=get_sms_auth() unless $res;
317 # Given a cell number and a message, attempt to send an SMS message.
318 my $sendresult=send_sms($ua,$phone,$message,$res->{pSessionId});
319 my $error=error_codes($sendresult->{pErrCode});
320 return 1 unless $error;
328 # FIXME - decide where to print
331 sub changecharacter {