bug fixing:
[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 # $Id$
29
30 use C4::Context;
31 use C4::Search;
32 use C4::Circulation::Circ2;
33 use C4::Circulation::Fines;
34 use Date::Manip;
35 use C4::Biblio;
36
37 open (FILE,'>/tmp/fines') || die;
38 # FIXME
39 # it looks like $count is just a counter, would it be
40 # better to rely on the length of the array @$data and turn the
41 # for loop below into a foreach loop?
42 #
43 my ($numOverdueItems,$data)=Getoverdues();
44 print $numOverdueItems 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 $bornum;
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 # FIXME
66 # this probably ought to be a global variable or constant
67 # defined in a central place
68 #
69 # Yep
70 my $maxFine=5;
71
72 # FIXME
73 # delete both of these?
74 #my $bornum2=$data->[0]->{'borrowernumber'};
75 #my $i2=1;
76
77 # FIXME
78 # This should be rewritten to be a foreach loop
79 # Also, this loop is really long, and could be better grokked if broken
80 # into a number of smaller, separate functions
81 #
82 for (my $i=0;$i<$numOverdueItems;$i++){
83   my @dates=split('-',$data->[$i]->{'date_due'});
84   my $date2=Date_DaysSince1BC($dates[1],$dates[2],$dates[0]);
85   my $due="$dates[2]/$dates[1]/$dates[0]";
86   my $borrower=BorType($data->[$i]->{'borrowernumber'});
87   if ($date2 <= $date){
88     $overdueItemsCounted++ if $DEBUG;
89     my $difference=$date-$date2;
90     my ($amount,$type,$printout)=
91         CalcFine($data->[$i]->{'itemnumber'},
92                  $borrower->{'categorycode'},
93                  $difference);
94     if ($amount > $maxFine){
95       $amount=$maxFine;
96     }
97     if ($amount > 0){
98       UpdateFine($data->[$i]->{'itemnumber'},$data->[$i]->{'borrowernumber'},$amount,$type,$due);
99
100 #
101 # FIXME
102 # If this isn't needed it should be deleted
103 #
104 #      if ($amount ==5){
105 #             marklost();
106 #      }
107
108        if ($borrower->{'categorycode'} eq 'C'){  # FIXME
109                                                  # this should be a
110                                                  # separate function
111                                                  #
112          my $dbh = C4::Context->dbh;
113          my $sth=$dbh->prepare("Select * from borrowers where borrowernumber=?");
114          $sth->execute($borrower->{'guarantor'});
115          my $tdata=$sth->fetchrow_hashref;
116          $sth->finish;
117          $borrower->{'phone'}=$tdata->{'phone'};
118        }
119        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";
120     } else { # FIXME
121              # if this is really useless, the whole else clause should be
122              # deleted.
123              #
124 #      print "$borrower->{'cardnumber'}\t$borrower->{'categorycode'}\t0 fine\n";
125     }
126     if ($difference >= 28){ # FIXME
127                             # this should be a separate function
128                             #
129       my $borrower=BorType($data->[$i]->{'borrowernumber'});
130       if ($borrower->{'cardnumber'} ne ''){
131         my $cost=ReplacementCost($data->[$i]->{'itemnumber'});
132         my $dbh = C4::Context->dbh;
133         my $env;
134         my $accountno=C4::Circulation::Circ2::getnextacctno($env,$data->[$i]->{'borrowernumber'},$dbh);
135         my $item=getbibliofromitemnumber($env,$dbh,$data->[$i]->{'itemnumber'});
136         if ($item->{'itemlost'} ne '1' && $item->{'itemlost'} ne '2' ){
137               # FIXME
138               # this should be a separate function
139               #
140           my $sth=$dbh->prepare("Insert into accountlines
141           (borrowernumber,itemnumber,accountno,date,amount,
142           description,accounttype,amountoutstanding) values
143           (?,?,?,now(),?,?,'L',?)");
144           $sth->execute($data->[$i]->{'borrowernumber'},$data->[$i]->{'itemnumber'},
145           $accountno,$cost,"Lost item $item->{'title'} $item->{'barcode'} $due",$cost);
146           $sth->finish;
147           $sth=$dbh->prepare("update items set itemlost=2 where itemnumber=?");
148           $sth->execute($data->[$i]->{'itemnumber'});
149           $sth->finish;
150         }
151       }
152     }
153
154   }
155 }
156
157 if ($DEBUG) {
158    print <<EOM
159
160 Number of Overdue Items counted $overdueItemsCounted
161 Number of Overdue Items reported $numOverdueItems
162
163 EOM
164 }
165
166 close FILE;