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
25 # with Koha; if not, write to the Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 # $Id: sendoverdues.pl,v 1.1.2.1 2007/03/26 22:38:09 tgarip1957 Exp $
31 #use warnings; FIXME - Bug 2505
34 # find Koha's Perl modules
35 # test carefully before changing this
37 eval { require "$FindBin::Bin/../kohalib.pl" };
43 use C4
::Circulation
::Fines
;
45 use C4
::Dates qw
/format_date/;
46 use HTML
::Template
::Pro
;
48 use Mail
::RFC822
::Address
;
51 my ($res,$ua);##variables for SMS
54 my $dbh = C4
::Context
->dbh;
61 # Look up the overdues for today.
62 # Capture overdues which fall on our dates of interest.
64 ####################################################################################################
65 # Creating a big hash of available templates
67 %email->{'template'}='email-2.txt';
69 %sms->{'template'}='sms-2.txt';
72 my %firstReminder->{'email'} = \
%email;
73 %firstReminder->{'sms'} = \
%sms;
76 %email2->{'template'}='email-7.txt';
77 my %secondReminder->{'email'} = \
%email2;
79 %sms2->{'template'}='sms-7.txt';
80 %secondReminder->{'sms'} = \
%sms2;
82 %letter2->{'template'}='letter-7.html';
83 %secondReminder->{'letter'} = \
%letter2;
87 %email3->{'template'}='email-29.txt';
89 %sms3->{'template'}='sms-29.txt';
91 %letter3->{'template'}='letter-29.html';
93 my %finalReminder->{'email'} = \
%email3;
94 %finalReminder->{'letter'} = \
%letter3;
95 %finalReminder->{'sms'} = \
%sms3;
98 %actions->{'1'}=\
%firstReminder;
99 %actions->{'3'}=\
%secondReminder;###This was 7 days changed to 3 days
100 %actions->{'20'}=\
%finalReminder;###This was 29 days changed to 20 days
102 ##################################################################################################################
103 my @overdues2;#an array for each actiondate
110 # Retrieve an array of overdues.
111 my ($count, $overduesReference) = Getoverdues
();
112 my @overdues=@
$overduesReference;
115 # We're going to build a hash of arrays, containing the items requiring action.
116 # ->borrowernumber, date, @overdues
120 foreach my $overdue (@overdues) {
121 my $due_day=$overdue->{'date_due'};
123 my $difference=DATE_subtract
($date,$due_day);
124 # If does this item fall on a day of interest?
125 $overdue->{'difference'}=$difference;
126 foreach my $actiondate (keys(%actions)) {
127 if ($actiondate == $difference) {
128 $filename='overdues'.$actiondate;
129 push @
$$filename,$overdue;
130 #print "$actiondate,-,$overdue->{borrowernumber}\n";
132 $actionItems{$actiondate} = \@
$$filename;
136 # We now have a hash containing overdues which need actioning, we can step through each set.
137 # Work from earilest to latest. We only wish to send the most urgent message.
141 foreach my $actiondate (keys %actions) {
142 # print "\n\nThe following items are $actiondate days overdue.\n";
143 my $items = $actionItems{$actiondate};
144 $filename='overdues'.$actiondate;
145 foreach my $overdue (@
$$filename) {
146 # Detemine which borrower is responsible for this overdue;
147 # if the offender is a child, then the guarantor is the person to notify
148 my $borrowernumber=$overdue->{borrowernumber
};
150 my $borrower=responsibleBorrower
($borrowernumber);
151 my ($method, $address) = preferedContactMethod
($borrower);
152 if ($method && $address) {
154 # Do we have to send something, using this method on this day?
155 if (%actions->{$actiondate}->{$method}->{'template'}) {
156 my $intranetdir=C4
::Context
->config('intranetdir');
157 # Template the message
158 my $template = HTML
::Template
::Pro
->new(filename
=> $intranetdir.'/scripts/misc/notifys/templates/'.%actions->{$actiondate}->{$method}->{'template'}, die_on_bad_params
=> 0);
161 my $item = getiteminformation
("", $overdue->{'itemnumber'});
162 $row_data{'BARCODE'}=$item->{'barcode'};
164 my $title=substr($item->{'title'},0,25)."...";
166 $title=changecharacter
($title);
167 $row_data{'TITLE'}=$title;
168 $row_data{'DATE_DUE'}=format_date
($overdue->{'date_due'});
169 $row_data{'cardnumber'}=$borrower->{'cardnumber'};
170 push(@bookdetails, \
%row_data);
171 $template->param(BOOKDETAILS
=> \
@bookdetails);
172 my $name= "$borrower->{'firstname'} $borrower->{'surname'}";
173 $template->param(NAME
=> $name);
174 %messages->{$borrower->{'borrowernumber'}} = $template->output();
175 if ($method eq 'email') {
176 $result = sendEmail
($address, 'library@library.neu.edu.tr', 'Overdue Library Items', %messages->{$borrowernumber});
177 logContact
($borrowernumber, $method, $address, $result, %messages->{$borrowernumber});
179 elsif ($method eq 'sms') {
180 $result = sendSMS
($address, %messages->{$borrowernumber});
181 logContact
($borrowernumber, $method, $address, $result, %messages->{$borrowernumber});
183 elsif ($method eq 'letter') {
184 $result = printLetter
($address, %messages->{$borrowernumber});
188 print "$borrowernumber has an overdue item, but no means of contact\n";
192 } #end of 'foreach overdue'
194 } # end of foreach actiondate
197 sub responsibleBorrower
{
198 # Given an overdue item, return the details of the borrower responible as a hash of database columns.
199 my $borrowernumber=shift;
201 if ($borrowernumber) {
202 my $borrower=BorType
($borrowernumber);
203 # Overdue books assigned to children have notices sent to the guarantor.
204 if ($borrower->{'categorycode'} eq 'C') {
205 my $guarantor=BorType
($borrower->{'guarantor'});
206 $borrower = $guarantor;
222 sub preferedContactMethod
{
223 # Given a reference to borrower details, in the format
224 # returned by BorType(), determine the prefered contact method, and address to use.
226 my $borrcat = getborrowercategoryinfo
($borrower->{'categorycode'});
227 if( !$borrcat->{'overduenoticerequired'}){
228 return (undef,undef);
232 ## if borrower has a phone set that as our preferrred contact
233 if ($borrower->{'phoneday'}) {
234 if (parse_phone
($borrower->{phoneday
})){
235 $address = parse_phone
($borrower->{phoneday
});
237 return ($method, $address);
241 if (($borrower->{'emailaddress'}) and (Mail
::RFC822
::Address
::valid
($borrower->{'emailaddress'}))) {
242 $address = $borrower->{'emailaddress'};
244 return ($method, $address);
247 if ($borrower->{'streetaddress'}) {
248 $address = mailingAddress
($borrower);
251 #print "$method, $address\n";
252 return ($method, $address);
263 # Given the details of an attempt to contact a borrower,
264 # log them in the attempted_contacts table of the koha database.
265 my ($borrowernumber, $method, $address, $result, $message) = @_;
267 my $querystring = " insert into attempted_contacts
268 (borrowernumber, method, address, result, message, date)
269 values (?, ?, ?, ?, ?, now())";
270 my $sth= $dbh->prepare($querystring);
271 $sth->execute($borrowernumber, $method, $address, $result, $message);
283 # Given a hash of borrower information, such as that returned by BorType,
284 # return a mailing address.
287 my $address = $borrower->{'firstname'}."\n".
288 $borrower->{'streetaddress'}."\n".
289 $borrower->{'streetcity'};
297 # Given an email address, and a subject and message, attempt to send email.
303 # print "in email area";
305 # print "\nSending Email To: $to\n$message\n";
307 my %mail = ( To
=> $to,
308 CC
=> 'library@library.neu.edu.tr',
311 Message
=> $message);
314 if (not(sendmail
%mail)) {
315 warn $Mail::Sendmail
::error
;
316 warn "sendEmail to $to failed.";
325 my ($phone, $message)=@_;
326 ($res,$ua)=get_sms_auth
() unless $res;
327 # Given a cell number and a message, attempt to send an SMS message.
328 my $sendresult=send_sms
($ua,$phone,$message,$res->{pSessionId
});
329 my $error=error_codes
($sendresult->{pErrCode
});
330 return 1 unless $error;
338 # FIXME - decide where to print
341 sub changecharacter
{