adding xisbn support
[koha.git] / misc / cronjobs / smsoverdues.pl
1 #!/usr/bin/perl
2
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
8 #
9 #  This script is meant to be run nightly out of cron.
10
11 # Copyright 2000-2002 Katipo Communications
12 #
13 # This file is part of Koha.
14 #
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
18 # version.
19 #
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.
23 #
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
27
28 # $Id: sendoverdues.pl,v 1.1.2.1 2007/03/26 22:38:09 tgarip1957 Exp $
29
30 use C4::Context;
31 use C4::Search;
32 use C4::Circulation;
33 use C4::Circulation::Fines;
34 use C4::Members;
35 use C4::Date;
36 use HTML::Template::Pro;
37 use Mail::Sendmail;
38 use Mail::RFC822::Address;
39 use C4::SMS;
40 use utf8;
41 my ($res,$ua);##variables for SMS
42
43 my $date=get_today();
44                 my $dbh = C4::Context->dbh;
45  
46
47
48 notifyOverdues();
49
50 sub     notifyOverdues {
51         # Look up the overdues for today. 
52         # Capture overdues which fall on our dates of interest.
53
54 ####################################################################################################
55 # Creating a big hash of available templates
56 my %email;
57 %email->{'template'}='email-2.txt';
58 my %sms; 
59 %sms->{'template'}='sms-2.txt';
60
61
62 my %firstReminder->{'email'} = \%email;
63 %firstReminder->{'sms'} = \%sms;
64
65 my %email2;
66 %email2->{'template'}='email-7.txt';
67 my %secondReminder->{'email'} = \%email2;
68 my %sms2;  
69 %sms2->{'template'}='sms-7.txt';
70 %secondReminder->{'sms'} = \%sms2;
71 my %letter2;
72 %letter2->{'template'}='letter-7.html';
73 %secondReminder->{'letter'} = \%letter2;    
74
75
76 my %email3;
77 %email3->{'template'}='email-29.txt';
78 my %sms3;
79 %sms3->{'template'}='sms-29.txt';
80 my %letter3;
81 %letter3->{'template'}='letter-29.html';
82
83 my %finalReminder->{'email'} = \%email3;
84 %finalReminder->{'letter'} = \%letter3;
85 %finalReminder->{'sms'} = \%sms3;
86
87 my %actions;
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
91
92 ##################################################################################################################
93 my @overdues2;#an array for each actiondate
94 my @overdues7;
95 my @overdues29;
96 my $filename;
97        
98    
99
100         # Retrieve an array of overdues.
101         my ($count, $overduesReference) = Getoverdues();
102         my @overdues=@$overduesReference;
103
104
105         # We're going to build a hash of arrays, containing the items requiring action.
106         # ->borrowernumber, date, @overdues
107         my %actionItems;
108         
109
110         foreach my $overdue (@overdues) {
111                  my $due_day=$overdue->{'date_due'};
112
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";            
121                         }
122                         $actionItems{$actiondate} = \@$$filename;
123                 }
124         }
125
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.
128         my %messages; 
129         my %borritem;
130
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};
139
140                                 my $borrower=responsibleBorrower($borrowernumber);
141                                 my ($method, $address) = preferedContactMethod($borrower);
142                   if ($method && $address) {    
143                                 
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);
149                                                 my @bookdetails;
150                                                                 my %row_data;
151                                          my $item = getiteminformation("", $overdue->{'itemnumber'});
152                                                         $row_data{'BARCODE'}=$item->{'barcode'};
153                                 
154                                                        my $title=substr($item->{'title'},0,25)."...";
155
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});
168                         }
169                         elsif ($method eq 'sms') {
170                         $result = sendSMS($address, %messages->{$borrowernumber});
171                         logContact($borrowernumber, $method, $address, $result, %messages->{$borrowernumber});
172                         }
173                         elsif ($method eq 'letter') {
174                         $result = printLetter($address, %messages->{$borrowernumber});
175                         }                                                       
176                        }##template exists
177                 }else{
178                 print "$borrowernumber has an overdue item, but no means of contact\n";
179                 }##$method              
180
181
182         } #end of 'foreach overdue'
183
184      } # end of foreach actiondate
185 }
186
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;
190
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;
197                         }
198         
199                 return $borrower;
200         }
201
202 }
203
204
205
206
207
208
209
210
211
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.
215         my $borrower=$_[0];
216         my $borrcat = getborrowercategoryinfo($borrower->{'categorycode'});
217 if(  !$borrcat->{'overduenoticerequired'}){
218 return (undef,undef);
219 }
220         my $method='';  
221         my $address=''; 
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});
226                                 $method="sms";
227                                 return ($method, $address);
228                 }
229         }
230         
231         if (($borrower->{'emailaddress'}) and (Mail::RFC822::Address::valid($borrower->{'emailaddress'}))) {
232                                 $address = $borrower->{'emailaddress'};
233                 $method="email";
234                                 return ($method, $address);
235         }
236                         
237         if ($borrower->{'streetaddress'}) {
238                                 $address =  mailingAddress($borrower);
239                         $method = 'letter';     
240         }
241 #print "$method, $address\n";
242         return ($method, $address);
243 }
244
245
246
247
248
249
250
251
252 sub     logContact {
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) = @_;
256
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);
262         $sth->finish();
263 }
264
265
266
267
268
269
270
271
272 sub     mailingAddress {
273         # Given a hash of borrower information, such as that returned by BorType, 
274         # return a mailing address. 
275         my $borrower=$_[0];
276
277         my $address =   $borrower->{'firstname'}."\n". 
278                         $borrower->{'streetaddress'}."\n".
279                         $borrower->{'streetcity'};
280
281         return $address;
282 }
283
284
285
286 sub     sendEmail {
287         # Given an email address, and a subject and message, attempt to send email.
288
289         my $to=$_[0];
290         my $from=$_[1];
291         my $subject=$_[2];
292         my $message=$_[3];
293 #    print "in email area";
294
295 #       print "\nSending Email To: $to\n$message\n";
296
297         my      %mail = (               To      => $to,
298                                         CC => 'library@library.neu.edu.tr', 
299                                         From    => $from,
300                                         Subject => $subject,
301                                         Message => $message);
302
303                 
304         if (not(sendmail %mail)) {       
305 warn  $Mail::Sendmail::error;
306                 warn "sendEmail to $to failed.";
307                 return 0;       
308         }
309         
310         return 1;
311 }
312
313
314 sub     sendSMS {
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;
321         return $error;
322 }
323
324
325 sub     printLetter {
326 print "letter\n";
327         # Print a letter
328         # FIXME - decide where to print
329         return 1;
330 }
331 sub changecharacter {
332 my ($string)=@_;
333 $_=$string;
334
335 s/ş/s/g;
336 s/Ş/S/g;
337 s/ü/u/g;
338 s/Ü/U/g;
339 s/ç/c/g;
340 s/Ç/C/g;
341 s/ö/o/g;
342 s/Ö/O/g;
343 s/ı/i/g;
344 s/İ/I/g;
345 s/ğ/g/g;
346 s/Ğ/G/g;
347 $string=$_;
348 return $string;
349 }
350 $dbh->disconnect();
351