Added POD.
[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
12 # Copyright 2000-2002 Katipo Communications
13 #
14 # This file is part of Koha.
15 #
16 # Koha is free software; you can redistribute it and/or modify it under the
17 # terms of the GNU General Public License as published by the Free Software
18 # Foundation; either version 2 of the License, or (at your option) any later
19 # version.
20 #
21 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
22 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
23 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License along with
26 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
27 # Suite 330, Boston, MA  02111-1307 USA
28
29 use C4::Database;
30 use C4::Search;
31 use C4::Circulation::Circ2;
32 use C4::Circulation::Fines;
33 use Date::Manip;
34
35 open (FILE,'>/tmp/fines') || die;
36 # FIXME
37 # it looks like $count is just a counter, would it be
38 # better to rely on the length of the array @$data and turn the
39 # for loop below into a foreach loop?
40 #
41 my ($numOverdueItems,$data)=Getoverdues();
42 print $numOverdueItems if $DEBUG;
43 my $overdueItemsCounted=0 if $DEBUG;
44
45 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);
46 $mon++;
47 $year=$year+1900;
48
49 my $date=Date_DaysSince1BC($mon,$mday,$year);
50
51 print $date if $DEBUG;
52
53 my $bornum;
54
55 # FIXME
56 # $total isn't used anywhere else in the file,
57 # can we delete it?
58 #
59 my $total=0;
60
61 # FIXME
62 # this probably ought to be a global variable or constant
63 # defined in a central place
64 #
65 # Yep
66 my $maxFine=5;
67
68 # FIXME
69 # delete both of these?
70 #my $bornum2=$data->[0]->{'borrowernumber'};
71 #my $i2=1;
72
73 # FIXME
74 # This should be rewritten to be a foreach loop
75 # Also, this loop is really long, and could be better grokked if broken
76 # into a number of smaller, separate functions
77 #
78 for (my $i=0;$i<$numOverdueItems;$i++){
79   my @dates=split('-',$data->[$i]->{'date_due'});
80   my $date2=Date_DaysSince1BC($dates[1],$dates[2],$dates[0]);    
81   my $due="$dates[2]/$dates[1]/$dates[0]";
82   my $borrower=BorType($data->[$i]->{'borrowernumber'});
83   if ($date2 <= $date){
84     $overdueItemsCounted++ if $DEBUG;
85     my $difference=$date-$date2;
86     my ($amount,$type,$printout)=
87         CalcFine($data->[$i]->{'itemnumber'},
88                  $borrower->{'categorycode'},
89                  $difference);      
90     if ($amount > $maxFine){
91       $amount=$maxFine;
92     }
93     if ($amount > 0){
94       UpdateFine($data->[$i]->{'itemnumber'},$data->[$i]->{'borrowernumber'},$amount,$type,$due);
95
96 #
97 # FIXME
98 # If this isn't needed it should be deleted
99 #
100 #      if ($amount ==5){
101 #             marklost();
102 #      }
103
104        if ($borrower->{'categorycode'} eq 'C'){  # FIXME
105                                                  # this should be a
106                                                  # separate function
107                                                  #
108          my $dbh=C4Connect;
109          my $query="Select * from borrowers where borrowernumber='$borrower->{'guarantor'}'";
110          my $sth=$dbh->prepare($query);
111          $sth->execute;
112          my $tdata=$sth->fetchrow_hashref;
113          $sth->finish;
114          $dbh->disconnect;
115          $borrower->{'phone'}=$tdata->{'phone'};
116        }
117        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";
118     } else { # FIXME
119              # if this is really useless, the whole else clause should be 
120              # deleted. 
121              #
122 #      print "$borrower->{'cardnumber'}\t$borrower->{'categorycode'}\t0 fine\n";
123     }
124     if ($difference >= 28){ # FIXME
125                             # this should be a separate function
126                             #
127       my $borrower=BorType($data->[$i]->{'borrowernumber'});
128       if ($borrower->{'cardnumber'} ne ''){
129         my $cost=ReplacementCost($data->[$i]->{'itemnumber'});  
130         my $dbh=C4Connect;
131         my $env;
132         my $accountno=C4::Circulation::Circ2::getnextacctno($env,$data->[$i]->{'borrowernumber'},$dbh);
133         my $item=itemnodata($env,$dbh,$data->[$i]->{'itemnumber'});
134         if ($item->{'itemlost'} ne '1' && $item->{'itemlost'} ne '2' ){
135               # FIXME
136               # this should be a separate function
137               #
138           $item->{'title'}=~ s/\'/\\'/g;
139           my $query="Insert into accountlines
140           (borrowernumber,itemnumber,accountno,date,amount,
141           description,accounttype,amountoutstanding) values
142           ($data->[$i]->{'borrowernumber'},$data->[$i]->{'itemnumber'},
143           '$accountno',now(),'$cost','Lost item $item->{'title'} $item->{'barcode'} $due','L','$cost')";
144           my $sth=$dbh->prepare($query);
145           $sth->execute;
146           $sth->finish;
147           $query="update items set itemlost=2 where itemnumber='$data->[$i]->{'itemnumber'}'";
148           $sth=$dbh->prepare($query);
149           $sth->execute;
150           $sth->finish;
151         } else { # FIXME
152                  # this should be deleted
153                  #
154         }
155         $dbh->disconnect;
156       }
157     }
158
159   }
160 }
161
162 if ($DEBUG) {
163    print <<EOM
164
165 Number of Overdue Items counted $overdueItemsCounted
166 Number of Overdue Items reported $numOverdueItems
167
168 EOM
169 }
170
171 close FILE;