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
31 # find Koha's Perl modules
32 # test carefully before changing this
34 eval { require "$FindBin::Bin/kohalib.pl" };
40 use Date::Manip qw/Date_DaysSince1BC/;
46 my $libname=C4::Context->preference('LibraryName');
47 my $dbname= C4::Context->config('database');
49 my $SET_LOST = 0; # automatically charge item price at delay=3 if set.
51 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);
54 my $date=Date_DaysSince1BC($mon,$mday,$year);
55 my $datestr = $year . sprintf("-%02d-%02d",$mon,$mday);
56 my $filename= $dbname;
58 $filename = $fldir . '/'. $filename . $datestr . ".log";
59 open (FILE,">$filename") || die "Can't open LOG";
60 print FILE "cardnumber\tcategory\tsurname\tfirstname\temail\tphone\taddress\tcitystate\tbarcode\tdate_due\ttype\titemnumber\tdays_overdue\tfine\n";
63 # it looks like $count is just a counter, would it be
64 # better to rely on the length of the array @$data and turn the
65 # for loop below into a foreach loop?
68 my $data=Getoverdues();
69 # warn "Overdues : = ".scalar(@$data)." => ".Data::Dumper::Dumper($data);
70 my $overdueItemsCounted=0 if $DEBUG;
71 my $reference = $year."".$mon;
74 for (my $i=0;$i<scalar(@$data);$i++){
75 my @dates=split('-',$data->[$i]->{'date_due'});
76 my $date2=Date_DaysSince1BC($dates[1],$dates[2],$dates[0]);
77 my $datedue=$data->[$i]->{'date_due'};
78 my $due="$dates[1]/$dates[2]/$dates[0]";
79 my $borrower=BorType($data->[$i]->{'borrowernumber'});
81 if ( C4::Context->preference('CircControl') eq 'ItemHomeLibrary' ) {
82 $branchcode = $data->[$i]->{'homebranch'};
83 } elsif ( C4::Context->preference('CircControl') eq 'PatronLibrary' ) {
84 $branchcode = $borrower->{'branchcode'};
86 # CircControl must be PickupLibrary. (branchcode comes from issues table here).
87 $branchcode = $data->[$i]->{'branchcode'};
89 my $calendar = C4::Calendar->new( branchcode => $branchcode );
91 my @dmy = split( '-', C4::Dates->new()->output('metric') ) ;
92 my $isHoliday = $calendar->isHoliday( split( '/', C4::Dates->new()->output('metric') ) );
96 $overdueItemsCounted++ if $DEBUG;
97 my $difference=$date-$date2;
98 my $start_date = C4::Dates->new($data->[$i]->{'date_due'},'iso');
99 my $end_date = C4::Dates->new($datestr,'iso');
100 my ($amount,$type,$printout,$daycounttotal,$daycount)=
101 CalcFine($data->[$i], $borrower->{'categorycode'}, $branchcode,undef,undef, $start_date,$end_date);
102 my ($delays1,$delays2,$delays3)=GetOverdueDelays($borrower->{'categorycode'});
104 # Don't update the fine if today is a holiday.
105 # This ensures that dropbox mode will remove the correct amount of fine.
107 # FIXME - $type is always null, afaict.
108 UpdateFine($data->[$i]->{'itemnumber'},$data->[$i]->{'borrowernumber'},$amount,$type,$due) if( $amount > 0 ) ;
110 if($delays1 and $delays2 and $delays3) {
112 my $debarredstatus=CheckBorrowerDebarred($borrower->{'borrowernumber'});
114 #DELAYS 1##########################################
115 if ($amount > 0 && $daycount >= $delays1 && $daycount < $delays2){
116 # FIXME : already in GetIssuingRules ?
117 my $debarred1=GetOverduerules($borrower->{'categorycode'},1);
118 (UpdateBorrowerDebarred($borrower->{'borrowernumber'}))if(($debarred1 eq '1' ) and ($debarredstatus eq '0'));
119 # is there an open "dossier" for this date & borrower
120 my $getnotifyid=CheckExistantNotifyid($borrower->{'borrowernumber'},$datedue);
121 my $update=CheckAccountLineLevelInfo($borrower->{'borrowernumber'},$data->[$i]->{'itemnumber'},1,$datedue);
123 if ($getnotifyid eq '0'){
124 $starter=GetNextIdNotify($reference,$borrower->{'borrowernumber'});
126 $starter=$getnotifyid;
129 UpdateAccountLines($starter,1,$borrower->{'borrowernumber'},$data->[$i]->{'itemnumber'});
132 #DELAYS 2#################################
134 if ($daycount >= $delays2 && $daycount < $delays3){
135 my $debarred2=GetOverduerules($borrower->{'categorycode'},2);
136 (UpdateBorrowerDebarred($borrower->{'borrowernumber'}))if(($debarred2 eq '1' ) and ($debarredstatus eq '0'));
137 my $update=CheckAccountLineLevelInfo($borrower->{'borrowernumber'},$data->[$i]->{'itemnumber'},2,$datedue);
139 UpdateAccountLines(undef,2,$borrower->{'borrowernumber'},$data->[$i]->{'itemnumber'});
142 ###############################################
144 #DELAYS 3###################################
145 if ($daycount >= $delays3 ){
146 my $debarred3=GetOverduerules($borrower->{'categorycode'},3);
147 (UpdateBorrowerDebarred($borrower->{'borrowernumber'}))if(($debarred3 eq '1' ) and ($debarredstatus eq '0'));
148 my $update=CheckAccountLineLevelInfo($borrower->{'borrowernumber'},$data->[$i]->{'itemnumber'},3,$datedue);
150 UpdateAccountLines(undef,3,$borrower->{'borrowernumber'},$data->[$i]->{'itemnumber'});
152 my $items=GetItems($data->[$i]->{'itemnumber'});
153 my $todaydate=$year."-".$mon."-".$mday;
154 # add item price, the item is considered as lost.
155 my $description="Item Price";
156 my $typeaccount="IP";
158 my $notifyid=GetNotifyId($borrower->{'borrowernumber'},$data->[$i]->{'itemnumber'});
159 my $timestamp=$todaydate." ".$hour."\:".$min."\:".$sec;
160 my $create=CheckAccountLineItemInfo($borrower->{'borrowernumber'},$data->[$i]->{'itemnumber'},$typeaccount,$notifyid);
161 if ($SET_LOST && ($create eq '0') ){
162 CreateItemAccountLine($borrower->{'borrowernumber'},$data->[$i]->{'itemnumber'},$todaydate,$items->{'price'},$description,$typeaccount,
163 $items->{'price'},$timestamp,$notifyid,$level);
166 ###############################################
170 if ($borrower->{'category_type'} eq 'C'){
171 my $query=qq| SELECT *
173 WHERE borrowernumber=?|;
174 my $dbh = C4::Context->dbh;
175 my $sth=$dbh->prepare($query);
176 $sth->execute($borrower->{'guarantorid'});
177 my $tdata=$sth->fetchrow_hashref;
179 $borrower->{'phone'}=$tdata->{'phone'};
181 print FILE "$printout\t$borrower->{'cardnumber'}\t$borrower->{'categorycode'}\t$borrower->{'surname'}\t$borrower->{'firstname'}\t$borrower->{'email'}\t$borrower->{'phone'}\t$borrower->{'address'}\t$borrower->{'city'}\t$data->[$i]->{'barcode'}\t$data->[$i]->{'date_due'}\t$type\t$data->[$i]->{'itemnumber'}\t$daycounttotal\t$amount\n";
185 my $numOverdueItems=scalar(@$data);
189 Number of Overdue Items counted $overdueItemsCounted
190 Number of Overdue Items reported $numOverdueItems