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