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