3 # use lib ('/usr/local/koha/intranet/modules');
8 use C4::Circulation::Circ2;
9 use C4::Circulation::Fines;
14 use Mail::RFC822::Address;
18 #levyFines(); # Do not levy real fines in testing situation.
24 # - Need to calculate the fine on each book; no idea how to get this information from Koha
25 # - Need to diffentricate between the total_fines including replacement costs,
26 # and the total fines if the books are returned in the day 29 notices (see above).
27 # - clean up the %actions hash creation code.
30 # - preferedcont field in borrowers hash; does this do anything?
32 # - which 'address' to send sms to?
33 # - senders returning success or fail
38 # Look at the current overdues, and levy fines on the offenders.
43 # Work out what today is as an integer value.
44 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);
45 $mon++; $year=$year+1900;
46 my $date=Date_DaysSince1BC($mon,$mday,$year);
50 # Retrieve an array of overdues.
51 my ($count, $overduesReference) = Getoverdues();
52 print "$count overdue items where found.\n\n";
53 my @overdues=@$overduesReference;
55 foreach my $overdue (@overdues) {
56 my @dates=split('-',$overdue->{'date_due'});
57 my $due_day=Date_DaysSince1BC($dates[1],$dates[2],$dates[0]);
60 # Check that the item is really overdue. The output of Getoverdues() will normally
61 # always be overdue items. However, if you are running this script with a value of $date other than the current time, this check is needed.
62 if ($due_day <= $date) {
63 my $difference=$date-$due_day;
64 print "Itemnumber ".$overdue->{'itemnumber'}." is issued to ".$overdue->{'borrowernumber'}." is overdue by $difference days.\n";
66 # Calculate the cost of this overdue.
67 # Fines vary according to borrower type, but cannot exceed the maximum fine.
68 print $overdue->{'borrowernumber'};
69 my $borrower=BorType($overdue->{'borrowernumber'});
71 my ($amount,$type,$printout)=CalcFine($overdue->{'itemnumber'}, $borrower->{'categorycode'}, $difference);
72 if ($amount > $maxfine){
77 my $due="$dates[2]/$dates[1]/$dates[0]";
78 UpdateFine($overdue->{'itemnumber'}, $overdue->{'borrowernumber'}, $amount, $type, $due);
79 print $overdue->{'borrowernumber'}." has been fined $amount for itemnumber ".$overdue->{'itemnumber'}." overdue for $difference days.\n";
84 # After 28 days, the item is marked lost and the replacement charge is added as a fine
85 if ($difference >= 28) {
86 my $borrower=BorType($overdue->{'borrowernumber'});
87 if ($borrower->{'cardnumber'} ne ''){
88 my $cost=ReplacementCost($overdue->{'itemnumber'});
92 my $accountno=C4::Circulation::Circ2::getnextacctno($env,$overdue->{'borrowernumber'},$dbh);
93 my $item=itemnodata($env,$dbh,$overdue->{'itemnumber'});
94 if ($item->{'itemlost'} ne '1' && $item->{'itemlost'} ne '2' ){
95 $item->{'title'}=~ s/\'/\\'/g;
96 my $query="Insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding)
97 values ($overdue->{'borrowernumber'}, $overdue->{'itemnumber'},
98 '$accountno',now(),'$cost','Lost item $item->{'title'} $item->{'barcode'}','L','$cost')";
100 my $sth=$dbh->prepare($query);
104 $query="update items set itemlost=2 where itemnumber='$overdue->{'itemnumber'}'";
105 $sth=$dbh->prepare($query);
123 # Look up the overdues for today.
124 # Capture overdues which fall on our dates of interest.
129 ####################################################################################################
130 # Creating a big hash of available templates
132 %email->{'template'}='email-8.txt';
134 %sms->{'template'}='sms-8.txt';
137 %fax1->{'template'}='fax-8.html';
139 my %firstReminder->{'email'} = \%email;
140 %firstReminder->{'sms'} = \%sms;
141 %firstReminder->{'fax'} = \%fax1;
144 %email2->{'template'}='email-15.txt';
147 %fax2->{'template'}='fax-15.html';
150 %letter2->{'template'}='fax-15.html';
152 my %sms2->{'template'}='sms-15.txt';
153 my %secondReminder->{'email'} = \%email2;
154 %secondReminder->{'sms'} = \%sms2;
155 %secondReminder->{'fax'} = \%fax2;
156 %secondReminder->{'letter'} = \%letter2;
160 %email3->{'template'}='email-29.txt';
162 %fax3->{'template'}='fax-29.html';
164 %letter3->{'template'}='letter-29.html';
166 my %finalReminder->{'email'} = \%email3;
167 %finalReminder->{'fax'} = \%fax3;
168 %finalReminder->{'letter'} = \%letter3;
172 %actions->{'8'}=\%firstReminder;
173 %actions->{'15'}=\%secondReminder;
174 %actions->{'29'}=\%finalReminder;
176 ##################################################################################################################
179 # Work out what today is as an integer value.
180 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);
181 $mon++; $year=$year+1900;
182 my $date=Date_DaysSince1BC($mon,$mday,$year);
185 # Retrieve an array of overdues.
186 my ($count, $overduesReference) = Getoverdues();
187 print "$count overdue items where found.\n\n";
188 my @overdues=@$overduesReference;
191 # We're going to build a hash of arrays, containing the items requiring action.
192 # ->borrowernumber, date, @overdues
194 foreach my $actionday (keys(%actions)) {
196 %actionItems->{$actionday} = \@items;
201 foreach my $overdue (@overdues) {
202 my @dates=split('-',$overdue->{'date_due'});
203 my $due_day=Date_DaysSince1BC($dates[1],$dates[2],$dates[0]);
205 my $difference=$date-$due_day;
206 # $overdue->{'fine'}=GetFine($overdue->{'itemnumber'});
207 # If does this item fall on a day of interest?
208 $overdue->{'difference'}=$difference;
209 foreach my $actiondate (keys(%actions)) {
210 if ($actiondate == $difference) {
211 my @items = @{%actionItems->{$actiondate}};
215 %actionItems->{$actiondate} = \@items;
223 # We now have a hash containing overdues which need actioning, we can step through each set.
224 # Work from earilest to latest. We only wish to send the most urgent message.
228 foreach my $actiondate (sort {$a <=> $b} (keys(%actions))) {
229 print "\n\nThe following items are $actiondate days overdue.\n";
230 my @items = @{%actionItems->{$actiondate}};
233 foreach my $overdue (@items) {
234 if ($overdue->{'difference'} eq $actiondate) {
235 # Detemine which borrower is responsible for this overdue;
236 # if the offender is a child, then the garentor is the person to notify
237 my $borrower=responsibleBorrower($overdue);
240 my ($method, $address) = preferedContactMethod($borrower);
243 # Do we have to send something, using this method on this day?
244 if (%actions->{$actiondate}->{$method}->{'template'}) {
245 # If this user has one overdue, then they may have offers as well.
246 # No point in sending a notice without mentioning all of the items.
248 foreach my $over (@overdues) {
249 my $responisble= responsibleBorrower($over);
250 if ($responisble->{'borrowernumber'} eq $borrower->{'borrowernumber'}) {
251 $over->{'borrowernumber'}=$responisble->{'borrowernumber'};
253 push (@alloverdues, \%o);
257 my $dbh=C4Connect(); # FIXME disconnect this
259 # Template the message
260 my $template = HTML::Template->new(filename => 'templates/'.%actions->{$actiondate}->{$method}->{'template'}, die_on_bad_params => 0);
264 foreach my $over (@alloverdues) {
266 my $env; #FIXME what is this varible for?
268 if ( my $item = itemnodata($env, $dbh, $over->{'itemnumber'})){
269 print "getting fine ($over->{'itemnumber'} $overdue->{'borrowernumber'} $over->{'borrowernumber'}\n";
270 my $fine = GetFine($over->{'itemnumber'},$overdue->{'borrowernumber'});
275 my $rep = ReplacementCost2($over->{'itemnumber'},$overdue->{'borrowernumber'});
282 $borritem{"$over->{'itemnumber'} $over->{'borrowernumber'}"}=$fine;
284 $borritem{"$over->{'itemnumber'} $over->{'borrowernumber'}"}+=$fine;
286 print $borritem{"$over->{'itemnumber'} $over->{'borrowernumber'}"},"\n";
287 $total_fines += $borritem{"$over->{'itemnumber'} $over->{'borrowernumber'}"};
288 $item->{'title'}=substr($item->{'title'},0,25);
289 my $len=length($item->{'title'});
292 $item->{'title'}.=" " x $diff;
295 $row_data{'BARCODE'}=$item->{'barcode'};
296 $row_data{'TITLE'}=$item->{'title'};
297 $row_data{'DATE_DUE'}=$over->{'date_due'};
298 $row_data{'FINE'}=$borritem{"$over->{'itemnumber'} $over->{'borrowernumber'}"};
299 $row_data{'REP'}=$rep;
301 push(@bookdetails, \%row_data);
303 print "Missing item $over->{'itemnumber'}\n";
307 $template->param(BOOKDETAILS => \@bookdetails);
311 %params->{'borrowernumber'} = $overdue->{'borrowernumber'};
312 my ($count, $acctlines, $total) = &getboracctrecord($env, \%params);
313 $template->param(FINES_TOTAL => $total_fines);
314 $template->param(OWING => $total);
315 my $name= "$borrower->{'firstname'} $borrower->{'surname'}";
316 $template->param(NAME=> $name);
318 %messages->{$borrower->{'borrowernumber'}} = $template->output();
321 print "No $method needs to be sent at $overdue->{'difference'} days; not sending\n";
326 print "This borrower has an overdue item, but no means of contact\n";
329 } #end of 'if this overdue falls on an action date'
331 } #end of 'foreach overdue'
333 } # end of foreach actiondate
336 # How that all of the messsages to be sent have been composed, send them.
337 foreach my $borrowernumber (keys(%messages)) {
338 print "$borrowernumber\n";
340 my $borrower=BorType($borrowernumber);
341 my ($method, $address) = preferedContactMethod($borrower);
344 if ($method eq 'email') {
345 $result = sendEmail($address, 'lep@library.org.nz', 'Overdue Library Items', %messages->{$borrowernumber});
347 elsif ($method eq 'sms') {
348 $result = sendSMS($address, %messages->{$borrowernumber});
350 elsif ($method eq 'fax') {
351 $result = sendFax($address, %messages->{$borrowernumber});
353 elsif ($method eq 'letter') {
354 $result = printLetter($address, %messages->{$borrowernumber});
358 #print %messages->{$borrowernumber}; # debug
361 # Log the outcome of this attempt
362 logContact($borrowernumber, $method, $address, $result, %messages->{$borrowernumber});
379 sub responsibleBorrower {
380 # Given an overdue item, return the details of the borrower responible as a hash of database columns.
383 if ($overdue->{'borrowernumber'}) {
384 my $borrower=BorType($overdue->{'borrowernumber'});
387 # Overdue books assigned to children have notices sent to the guarantor.
388 if ($borrower->{'categorycode'} eq 'C') {
390 my $query="Select borrowernumber from borrowers
391 where borrowernumber=?";
393 my $sth=$dbh->prepare($query);
394 $sth->execute($borrower->{'guarantor'});
396 my $tdata=$sth->fetchrow_hashref();
400 my $guarantor=BorType($tdata->{'borrowernumber'});
401 $borrower = $guarantor;
417 sub preferedContactMethod {
418 # Given a reference to borrower details, in the format
419 # returned by BorType(), determine the prefered contact method, and address to use.
421 # print "finding borrower method $borrower->{'preferredcont'} $borrower->{'emailaddress'} $borrower->{'streetaddress'}\n";
423 # Possible contact methods, in order of preference are:
424 my @methods = ('email', 'sms', 'fax', 'letter');
430 # Does this borrower have a borrower.preferredcont set?
431 # If so, push it to the head of our array of methods to try.
432 # If it's a method unheard of by this system, then we'll drop though to the prefined methods above.
433 # Note use of unshift to push onto the front of the array.
434 if ($borrower->{'preferredcont'}) {
435 unshift(@methods, $borrower->{'preferredcont'});
439 # Cycle through the possible methods until one is accepted
440 while ((@methods) and (!$address)) {
441 $method=shift(@methods);
444 if ($method eq 'email') {
445 if (($borrower->{'emailaddress'}) and (Mail::RFC822::Address::valid($borrower->{'emailaddress'}))) {
446 $address = $borrower->{'emailaddress'};
449 elsif ($method eq 'fax') {
450 if ($borrower->{'faxnumber'}) {
451 $address = $borrower->{'faxnumber'};
454 elsif ($method eq 'sms') {
455 if ($borrower->{'textmessaging'}) {
456 $address = $borrower->{'textmessaging'};
459 elsif ($method eq 'letter') {
460 if ($borrower->{'streetaddress'}) {
461 $address = mailingAddress($borrower);
465 print "$method, $address\n";
466 return ($method, $address);
477 # Given the details of an attempt to contact a borrower,
478 # log them in the attempted_contacts table of the koha database.
479 my ($borrowernumber, $method, $address, $result, $message) = @_;
481 my $dbh=C4Connect(); # FIXME - disconnect me
482 my $querystring = " insert into attempted_contacts
483 (borrowernumber, method, address, result, message, date)
484 values (?, ?, ?, ?, ?, now())";
485 my $sth= $dbh->prepare($querystring);
486 $sth->execute($borrowernumber, $method, $address, $result, $message);
498 # Given a hash of borrower information, such as that returned by BorType,
499 # return a mailing address.
502 my $address = $borrower->{'firstname'}."\n".
503 $borrower->{'streetaddress'}."\n".
504 $borrower->{'streetcity'};
516 # Given an overdue item, return the current fines on it
531 # Given an email address, and a subject and message, attempt to send email.
537 # print "in email area";
539 # print "\nSending Email To: $to\n$message\n";
541 my %mail = ( To => $to,
542 # CC => 'rosalie@library.org.nz',
545 Message => $message);
548 if (not(sendmail %mail)) {
549 warn "sendEmail to $to failed.";
559 # Given a cell number and a message, attempt to send an SMS message.
560 # FIXME - needs information about how to do this at HLT
567 # Given a fax number, and a message, attempt to send a fax.
568 # FIXME - needs information about how to do this at HLT
569 # This is fairly easy.
570 # We will be past the body of the fax as HTML.
571 # We can pass this through html2ps to generate Postscript suitable
572 # for passing to the fax server.
579 # FIXME - needs information about how to do this at HLT