Patch from Joe Atzberger to remove $Id$ and $Log$ from scripts
[koha.git] / misc / fines2.pl
1 #!/usr/bin/perl
2
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
8 #
9 #  This script is meant to be run nightly out of cron.
10
11 # Copyright 2000-2002 Katipo Communications
12 #
13 # This file is part of Koha.
14 #
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
18 # version.
19 #
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.
23 #
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
27
28
29 use C4::Context;
30 use C4::Circulation;
31 use C4::Overdues;
32 use Date::Manip;
33 use C4::Biblio;
34 use strict;
35
36 open (FILE,'>/tmp/fines') || die;
37 # FIXME
38 # it looks like $count is just a counter, would it be
39 # better to rely on the length of the array @$data and turn the
40 # for loop below into a foreach loop?
41 #
42 my $DEBUG=0;
43 my ($data)=Getoverdues();
44 print scalar(@$data) if $DEBUG;
45 my $overdueItemsCounted=0 if $DEBUG;
46
47 # FIXME - There's got to be a better way to figure out what day
48 # today is.
49 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);
50 $mon++;
51 $year=$year+1900;
52
53 my $date=Date_DaysSince1BC($mon,$mday,$year);
54
55 print $date if $DEBUG;
56
57 my $borrowernumber;
58
59 # FIXME
60 # $total isn't used anywhere else in the file,
61 # can we delete it?
62 #
63 my $total=0;
64
65 # get the maxfine parameter
66 my $maxFine=C4::Context->preference("MaxFine") || 999999999;
67
68 # FIXME
69 # This should be rewritten to be a foreach loop
70 # Also, this loop is really long, and could be better grokked if broken
71 # into a number of smaller, separate functions
72 #
73 for (my $i=0;$i<scalar(@$data);$i++){
74     my @dates=split('-',$data->[$i]->{'date_due'});
75     my $date2=Date_DaysSince1BC($dates[1],$dates[2],$dates[0]);
76     my $due="$dates[2]/$dates[1]/$dates[0]";
77     my $borrower=BorType($data->[$i]->{'borrowernumber'});
78     if ($date2 <= $date){
79         $overdueItemsCounted++ if $DEBUG;
80         my $difference=$date-$date2;
81         my ($amount,$type,$printout)=
82         CalcFine($data->[$i],
83             $borrower->{'categorycode'},
84             $difference);
85         if ($amount > $maxFine){
86             $amount=$maxFine;
87         }
88         if ($amount > 0){
89             UpdateFine($data->[$i]->{'itemnumber'},$data->[$i]->{'borrowernumber'},$amount,$type,$due);
90             if ($borrower->{'categorycode'} eq 'C'){  # FIXME
91                 my $dbh = C4::Context->dbh;
92                 my $sth=$dbh->prepare("Select * from borrowers where borrowernumber=?");
93                 $sth->execute($borrower->{'guarantor'});
94                 my $tdata=$sth->fetchrow_hashref;
95                 $sth->finish;
96                 $borrower->{'phone'}=$tdata->{'phone'};
97             }
98             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" if $DEBUG;
99         }
100         if ($difference >= C4::Context->preference("NoReturnSetLost")){
101             my $borrower=BorType($data->[$i]->{'borrowernumber'});
102             if ($borrower->{'cardnumber'} ne ''){
103                 my $cost=ReplacementCost($data->[$i]->{'itemnumber'});
104                 my $dbh = C4::Context->dbh;
105                 my $accountno=C4::Accounts::getnextacctno($data->[$i]->{'borrowernumber'});
106                 my $item=GetBiblioFromItemNumber($data->[$i]->{'itemnumber'});
107                 if ($item->{'itemlost'} ne '1' && $item->{'itemlost'} ne '2' ){
108                     # FIXME this should be a separate function
109                     my $sth=$dbh->prepare("INSERT INTO accountlines
110                     (borrowernumber,itemnumber,accountno,date,amount,
111                     description,accounttype,amountoutstanding) VALUES
112                     (?,?,?,now(),?,?,'L',?)");
113                     $sth->execute($data->[$i]->{'borrowernumber'},$data->[$i]->{'itemnumber'},
114                     $accountno,$cost,"Lost item $item->{'title'} $item->{'barcode'} $due",$cost);
115                     $sth->finish;
116                     $sth=$dbh->prepare("UPDATE items SET itemlost=2 WHERE itemnumber=?");
117                     $sth->execute($data->[$i]->{'itemnumber'});
118                     $sth->finish;
119                 }
120             }
121         }
122     }
123 }
124
125 if ($DEBUG) {
126     my $numOverdueItems=scalar(@$data);
127     print <<EOM
128
129 Number of Overdue Items counted $overdueItemsCounted
130 Number of Overdue Items reported $numOverdueItems
131
132 EOM
133 }
134
135 close FILE;