fix to avoid an error when removing an authority
[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::Circulation;
32 use C4::Overdues;
33 use Date::Manip;
34 use C4::Biblio;
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 ($numOverdueItems,$data)=Getoverdues();
43 print $numOverdueItems if $DEBUG;
44 my $overdueItemsCounted=0 if $DEBUG;
45
46 # FIXME - There's got to be a better way to figure out what day
47 # today is.
48 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);
49 $mon++;
50 $year=$year+1900;
51
52 my $date=Date_DaysSince1BC($mon,$mday,$year);
53
54 print $date if $DEBUG;
55
56 my $borrowernumber;
57
58 # FIXME
59 # $total isn't used anywhere else in the file,
60 # can we delete it?
61 #
62 my $total=0;
63
64 # FIXME
65 # this probably ought to be a global variable or constant
66 # defined in a central place
67 #
68 # Yep
69 my $maxFine=5;
70
71 # FIXME
72 # delete both of these?
73 #my $borrowernumber2=$data->[0]->{'borrowernumber'};
74 #my $i2=1;
75
76 # FIXME
77 # This should be rewritten to be a foreach loop
78 # Also, this loop is really long, and could be better grokked if broken
79 # into a number of smaller, separate functions
80 #
81 for (my $i=0;$i<$numOverdueItems;$i++){
82   my @dates=split('-',$data->[$i]->{'date_due'});
83   my $date2=Date_DaysSince1BC($dates[1],$dates[2],$dates[0]);
84   my $due="$dates[2]/$dates[1]/$dates[0]";
85   my $borrower=BorType($data->[$i]->{'borrowernumber'});
86   if ($date2 <= $date){
87     $overdueItemsCounted++ if $DEBUG;
88     my $difference=$date-$date2;
89     my ($amount,$type,$printout)=
90     CalcFine($data->[$i]->{'itemnumber'},
91          $borrower->{'categorycode'},
92          $difference);
93     if ($amount > $maxFine){
94       $amount=$maxFine;
95     }
96     if ($amount > 0){
97       UpdateFine($data->[$i]->{'itemnumber'},$data->[$i]->{'borrowernumber'},$amount,$type,$due);
98
99 #
100 # FIXME
101 # If this isn't needed it should be deleted
102 #
103 #      if ($amount ==5){
104 #          marklost();
105 #      }
106
107        if ($borrower->{'categorycode'} eq 'C'){  # FIXME
108                                              # this should be a
109                                                  # separate function
110                                                  #
111      my $dbh = C4::Context->dbh;
112      my $sth=$dbh->prepare("Select * from borrowers where borrowernumber=?");
113      $sth->execute($borrower->{'guarantor'});
114      my $tdata=$sth->fetchrow_hashref;
115      $sth->finish;
116      $borrower->{'phone'}=$tdata->{'phone'};
117        }
118        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";
119     } else { # FIXME
120          # if this is really useless, the whole else clause should be
121          # deleted.
122              #
123 #      print "$borrower->{'cardnumber'}\t$borrower->{'categorycode'}\t0 fine\n";
124     }
125     if ($difference >= 28){ # FIXME
126                         # this should be a separate function
127                             #
128       my $borrower=BorType($data->[$i]->{'borrowernumber'});
129       if ($borrower->{'cardnumber'} ne ''){
130         my $cost=ReplacementCost($data->[$i]->{'itemnumber'});
131     my $dbh = C4::Context->dbh;
132     my $accountno=C4::Circulation::Circ2::getnextacctno($data->[$i]->{'borrowernumber'});
133     my $item=GetBiblioFromItemNumber($data->[$i]->{'itemnumber'});
134     if ($item->{'itemlost'} ne '1' && $item->{'itemlost'} ne '2' ){
135               # FIXME
136               # this should be a separate function
137               #
138       my $sth=$dbh->prepare("INSERT INTO accountlines
139       (borrowernumber,itemnumber,accountno,date,amount,
140       description,accounttype,amountoutstanding) VALUES
141       (?,?,?,now(),?,?,'L',?)");
142       $sth->execute($data->[$i]->{'borrowernumber'},$data->[$i]->{'itemnumber'},
143       $accountno,$cost,"Lost item $item->{'title'} $item->{'barcode'} $due",$cost);
144       $sth->finish;
145       $sth=$dbh->prepare("update items set itemlost=2 where itemnumber=?");
146       $sth->execute($data->[$i]->{'itemnumber'});
147       $sth->finish;
148     }
149       }
150     }
151
152   }
153 }
154
155 if ($DEBUG) {
156    print <<EOM
157
158 Number of Overdue Items counted $overdueItemsCounted
159 Number of Overdue Items reported $numOverdueItems
160
161 EOM
162 }
163
164 close FILE;