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" };
37 use Date::Manip; # qw( Date_DaysSince1BC ) ;
43 use C4::Overdues; # qw( Getoverdues CalcFine );
50 GetOptions('verbose+', \$verbose);
51 $debug and $verbose++;
53 # my $filename = "/tmp/fines";
54 # open (FILE, ">$filename") or die "Cannot write to $filename";
56 my ($data)=Getoverdues();
57 my $overdueItemsCounted=0;
59 # FIXME - There's got to be a better way to figure out what day today is.
60 my ($mday,$mon,$year) = (localtime)[3..5]; # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
61 my $date = Date_DaysSince1BC($mon+1,$mday,$year+1900);
62 my $today_obj = C4::Dates->new();
63 my $today_iso = $today_obj->output('iso');
66 printf "Number of overdues: %7d\nDate_DaysSince1BC : %7d\n", scalar(@$data), $date;
67 print "today: $today_iso\n";
70 # get the maxfine parameter
71 my $maxFine=C4::Context->preference("MaxFine") || 999999999;
73 our $phone_sth = C4::Context->dbh->prepare("Select * from borrowers where borrowernumber=?");
74 sub get_guarantor_phone ($) {
75 $phone_sth->execute(shift);
76 my $x = $phone_sth->fetchrow_hashref;
80 our $fine_sth = C4::Context->dbh->prepare("
81 INSERT INTO accountlines
82 (borrowernumber, itemnumber, accountno, date,
83 amount, description, accounttype, amountoutstanding)
84 VALUES (?,?,?,now(),?,?,'L',?)
86 sub insert_fine ($$$$$$) {
87 $verbose and print "inserting fine: " . join(", ",@_), "\n";
88 return $fine_sth->execute(@_);
92 my $date_due = $_->{date_due};
93 $verbose and print "date_due: $date_due ", ($date_due le $today_iso ? 'fine!' : 'ok'), "\n";
94 my @dates=split('-',$date_due);
95 my $date2=Date_DaysSince1BC($dates[1],$dates[2],$dates[0]);
96 my $due="$dates[2]/$dates[1]/$dates[0]";
97 my $borrowernumber = $_->{borrowernumber};
98 my $itemnumber = $_->{itemnumber};
99 my $borrower = BorType($borrowernumber);
100 ($date_due le $today_iso) or next; # it is valid to string compare ISO dates.
101 $overdueItemsCounted++ if $verbose;
102 my $difference=$date-$date2;
103 my (@calc_returns) = CalcFine(
104 $_, $borrower->{categorycode}, $_->{homebranch},
105 undef, undef, C4::Dates->new($date_due,'iso'), $today_obj
108 my $dump = Dumper($_);
110 $verbose and print "CalcFine($dump" .
111 "\t$borrower->{categorycode}, $_->{homebranch},undef,undef,[$date_due],[today]) returns:\n" . Dumper(\@calc_returns), "\n";
113 my ($amount,$type,$printout) = @calc_returns[0..2];
114 # ($amount,$chargename,$daycount,$daycounttotal)=&CalcFine($itemnumber,$categorycode,$branch,$days_overdue,$description, $start_date, $end_date );
116 ($amount > $maxFine) and $amount = $maxFine;
118 UpdateFine($itemnumber,$borrowernumber,$amount,$type,$due);
119 if ($borrower->{'guarantor'}) {
120 $borrower->{'phone'} = get_guarantor_phone($borrower->{'guarantor'}) || $borrower->{'phone'};
122 print "$printout\t$borrower->{'cardnumber'}\t$borrower->{'categorycode'}\t$borrower->{'firstname'}\t$borrower->{'surname'}\t",
123 "$_->{'date_due'}\t$type\t$difference\t",
124 "$borrower->{'emailaddress'}\t$borrower->{'phone'}\t$borrower->{'streetaddress'}\t$borrower->{'city'}\t$amount\n" if $verbose;
126 if ($difference >= C4::Context->preference("NoReturnSetLost")){
127 my $borrower=BorType($borrowernumber);
128 if ($borrower->{'cardnumber'} ne ''){
129 my $cost = ReplacementCost($itemnumber);
130 my $item = GetBiblioFromItemNumber($itemnumber);
131 if ($item->{'itemlost'} ne '1' && $item->{'itemlost'} ne '2' ){
135 C4::Accounts::getnextacctno($borrowernumber),
137 "Lost item $item->{'title'} $item->{'barcode'} $due",
140 ModItem({ itemlost => 2 }, undef, $itemnumber);
147 my $numOverdueItems=scalar(@$data);
150 Number of Overdue Items counted $overdueItemsCounted
151 Number of Overdue Items reported $numOverdueItems