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.
13 use C4::Circulation::Circ2;
14 use C4::Circulation::Fines;
17 open (FILE,'>/tmp/fines') || die;
19 # it looks like $count is just a counter, would it be
20 # better to rely on the length of the array @$data and turn the
21 # for loop below into a foreach loop?
23 my ($numOverdueItems,$data)=Getoverdues();
24 print $numOverdueItems if $DEBUG;
25 my $overdueItemsCounted=0 if $DEBUG;
27 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);
31 my $date=Date_DaysSince1BC($mon,$mday,$year);
33 print $date if $DEBUG;
38 # $total isn't used anywhere else in the file,
44 # this probably ought to be a global variable or constant
45 # defined in a central place
51 # delete both of these?
52 #my $bornum2=$data->[0]->{'borrowernumber'};
56 # This should be rewritten to be a foreach loop
57 # Also, this loop is really long, and could be better grokked if broken
58 # into a number of smaller, separate functions
60 for (my $i=0;$i<$numOverdueItems;$i++){
61 my @dates=split('-',$data->[$i]->{'date_due'});
62 my $date2=Date_DaysSince1BC($dates[1],$dates[2],$dates[0]);
63 my $due="$dates[2]/$dates[1]/$dates[0]";
64 my $borrower=BorType($data->[$i]->{'borrowernumber'});
66 $overdueItemsCounted++ if $DEBUG;
67 my $difference=$date-$date2;
68 my ($amount,$type,$printout)=
69 CalcFine($data->[$i]->{'itemnumber'},
70 $borrower->{'categorycode'},
72 if ($amount > $maxFine){
76 UpdateFine($data->[$i]->{'itemnumber'},$data->[$i]->{'borrowernumber'},$amount,$type,$due);
80 # If this isn't needed it should be deleted
86 if ($borrower->{'categorycode'} eq 'C'){ # FIXME
91 my $query="Select * from borrowers where borrowernumber='$borrower->{'guarantor'}'";
92 my $sth=$dbh->prepare($query);
94 my $tdata=$sth->fetchrow_hashref;
97 $borrower->{'phone'}=$tdata->{'phone'};
99 print "$printout\t$borrower->{'cardnumber'}\t$borrower->{'categorycode'}\t$borrower->{'firstname'}\t$borrower->{'surname'}\t$data->[$i]->{'date_due'}\t$type\t$difference\t$borrower->{'emailaddress'}\t$borrower->{'phone'}\t$borrower->{'streetaddress'}\t$borrower->{'city'}\t$amount\n";
101 # if this is really useless, the whole else clause should be
104 # print "$borrower->{'cardnumber'}\t$borrower->{'categorycode'}\t0 fine\n";
106 if ($difference >= 28){ # FIXME
107 # this should be a separate function
109 my $borrower=BorType($data->[$i]->{'borrowernumber'});
110 if ($borrower->{'cardnumber'} ne ''){
111 my $cost=ReplacementCost($data->[$i]->{'itemnumber'});
114 my $accountno=C4::Circulation::Circ2::getnextacctno($env,$data->[$i]->{'borrowernumber'},$dbh);
115 my $item=itemnodata($env,$dbh,$data->[$i]->{'itemnumber'});
116 if ($item->{'itemlost'} ne '1' && $item->{'itemlost'} ne '2' ){
118 # this should be a separate function
120 $item->{'title'}=~ s/\'/\\'/g;
121 my $query="Insert into accountlines
122 (borrowernumber,itemnumber,accountno,date,amount,
123 description,accounttype,amountoutstanding) values
124 ($data->[$i]->{'borrowernumber'},$data->[$i]->{'itemnumber'},
125 '$accountno',now(),'$cost','Lost item $item->{'title'} $item->{'barcode'} $due','L','$cost')";
126 my $sth=$dbh->prepare($query);
129 $query="update items set itemlost=2 where itemnumber='$data->[$i]->{'itemnumber'}'";
130 $sth=$dbh->prepare($query);
134 # this should be deleted
147 Number of Overdue Items counted $overdueItemsCounted
148 Number of Overdue Items reported $numOverdueItems