Just added some comments for Pat
[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 use C4::Database;
12 use C4::Search;
13 use C4::Circulation::Circ2;
14 use C4::Circulation::Fines;
15 use Date::Manip;
16
17 open (FILE,'>/tmp/fines') || die;
18 # FIXME
19 # it looks like $count is just a counter, would it be
20 # better to rely on the length of the array @$data and turn the
21 # for loop below into a foreach loop?
22 #
23 my ($numOverdueItems,$data)=Getoverdues();
24 print $numOverdueItems if $DEBUG;
25 my $overdueItemsCounted=0 if $DEBUG;
26
27 # FIXME
28 # delete this?
29 # yep just a debuging thing
30 #$numOverdueItems=1000;
31
32 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);
33 $mon++;
34 $year=$year+1900;
35
36 my $date=Date_DaysSince1BC($mon,$mday,$year);
37
38 # FIXME
39 # delete this?
40 # another debugging thing, and yep it can go (you can make the script fake a day, say u want to rerun the overdues for
41 # a day when for some reason the script didnt run)
42 #my $date=Date_DaysSince1BC(1,24,2002);
43 print $date if $DEBUG;
44
45 my $bornum;
46
47 # FIXME
48 # $total isn't used anywhere else in the file,
49 # can we delete it?
50 #
51 my $total=0;
52
53 # FIXME
54 # this probably ought to be a global variable or constant
55 # defined in a central place
56 #
57 # Yep
58 my $maxFine=5;
59
60 # FIXME
61 # delete both of these?
62 #my $bornum2=$data->[0]->{'borrowernumber'};
63 #my $i2=1;
64
65 # FIXME
66 # This should be rewritten to be a foreach loop
67 # Also, this loop is really long, and could be better grokked if broken
68 # into a number of smaller, separate functions
69 #
70 for (my $i=0;$i<$numOverdueItems;$i++){
71   my @dates=split('-',$data->[$i]->{'date_due'});
72   my $date2=Date_DaysSince1BC($dates[1],$dates[2],$dates[0]);    
73   my $due="$dates[2]/$dates[1]/$dates[0]";
74   my $borrower=BorType($data->[$i]->{'borrowernumber'});
75   if ($date2 <= $date){
76     $overdueItemsCounted++ if $DEBUG;
77     my $difference=$date-$date2;
78     my ($amount,$type,$printout)=
79         CalcFine($data->[$i]->{'itemnumber'},
80                  $borrower->{'categorycode'},
81                  $difference);      
82     if ($amount > $maxFine){
83       $amount=$maxFine;
84     }
85     if ($amount > 0){
86       UpdateFine($data->[$i]->{'itemnumber'},$data->[$i]->{'borrowernumber'},$amount,$type,$due);
87
88 #
89 # FIXME
90 # If this isn't needed it should be deleted
91 #
92
93 #      if ($amount ==5){
94 #             marklost();
95 #      }
96        if ($borrower->{'categorycode'} eq 'C'){  # FIXME
97                                                  # this should be a
98                                                  # separate function
99                                                  #
100          my $dbh=C4Connect;
101          my $query="Select * from borrowers where borrowernumber='$borrower->{'guarantor'}'";
102          my $sth=$dbh->prepare($query);
103          $sth->execute;
104          my $tdata=$sth->fetchrow_hashref;
105          $sth->finish;
106          $dbh->disconnect;
107          $borrower->{'phone'}=$tdata->{'phone'};
108        }
109        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";
110     } else { # FIXME
111              # if this is really useless, the whole else clause should be 
112              # deleted. 
113              #
114 #      print "$borrower->{'cardnumber'}\t$borrower->{'categorycode'}\t0 fine\n";
115     }
116     if ($difference >= 28){ # FIXME
117                             # this should be a separate function
118                             #
119       my $borrower=BorType($data->[$i]->{'borrowernumber'});
120       if ($borrower->{'cardnumber'} ne ''){
121         my $cost=ReplacementCost($data->[$i]->{'itemnumber'});  
122         my $dbh=C4Connect;
123         my $env;
124         my $accountno=C4::Circulation::Circ2::getnextacctno($env,$data->[$i]->{'borrowernumber'},$dbh);
125         my $item=itemnodata($env,$dbh,$data->[$i]->{'itemnumber'});
126         if ($item->{'itemlost'} ne '1' && $item->{'itemlost'} ne '2' ){
127               # FIXME
128               # this should be a separate function
129               #
130           $item->{'title'}=~ s/\'/\\'/g;
131           my $query="Insert into accountlines
132           (borrowernumber,itemnumber,accountno,date,amount,
133           description,accounttype,amountoutstanding) values
134           ($data->[$i]->{'borrowernumber'},$data->[$i]->{'itemnumber'},
135           '$accountno',now(),'$cost','Lost item $item->{'title'} $item->{'barcode'} $due','L','$cost')";
136           my $sth=$dbh->prepare($query);
137           $sth->execute;
138           $sth->finish;
139           $query="update items set itemlost=2 where itemnumber='$data->[$i]->{'itemnumber'}'";
140           $sth=$dbh->prepare($query);
141           $sth->execute;
142           $sth->finish;
143         } else { # FIXME
144                  # this should be deleted
145                  #
146         }
147         $dbh->disconnect;
148       }
149     }
150
151   }
152 }
153
154 if ($DEBUG) {
155    print <<EOM
156
157 Number of Overdue Items counted $overdueItemsCounted
158 Number of Overdue Items reported $numOverdueItems
159
160 EOM
161 }
162
163 close FILE;