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