Update fines cronjob: remove redundant scripts, remove some superfluous code in remai...
[koha.git] / misc / cronjobs / fines.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
29 use strict;
30 BEGIN {
31     # find Koha's Perl modules
32     # test carefully before changing this
33     use FindBin;
34     eval { require "$FindBin::Bin/kohalib.pl" };
35 }
36 use C4::Context;
37 use C4::Circulation;
38 use C4::Overdues;
39 use C4::Calendar;
40 use Date::Calc qw/Date_to_Days/;
41 use C4::Biblio;
42
43
44 my $fldir = "/tmp" ;
45
46 my $libname=C4::Context->preference('LibraryName');
47 my $dbname= C4::Context->config('database');
48
49 my $today = C4::Dates->new();
50 my $datestr = $today->output('iso');
51 my $today_days= Date_to_Days(split(/-/,$today->output('iso')));
52 my $filename= $dbname;
53 $filename =~ s/\W//;
54 $filename = $fldir . '/'. $filename . $datestr . ".log";
55 open (FILE,">$filename") || die "Can't open LOG";
56 print FILE "cardnumber\tcategory\tsurname\tfirstname\temail\tphone\taddress\tcitystate\tbarcode\tdate_due\ttype\titemnumber\tdays_overdue\tfine\n";
57
58
59 my $DEBUG =1;
60
61 my $data=Getoverdues();
62 my $overdueItemsCounted=0 ;
63 my $borrowernumber;
64
65 for (my $i=0;$i<scalar(@$data);$i++){
66   my $datedue=C4::Dates->new($data->[$i]->{'date_due'},'iso');
67   my $datedue_days = Date_to_Days(split(/-/,$datedue->output('iso')));
68   my $due_str=$datedue->output();
69   my $borrower=BorType($data->[$i]->{'borrowernumber'});
70   my $branchcode;
71   if ( C4::Context->preference('CircControl') eq 'ItemHomeLibrary' ) {
72         $branchcode = $data->[$i]->{'homebranch'};
73   } elsif ( C4::Context->preference('CircControl') eq 'PatronLibrary' ) {
74         $branchcode = $borrower->{'branchcode'};
75 } else {
76         # CircControl must be PickupLibrary. (branchcode comes from issues table here).
77         $branchcode =  $data->[$i]->{'branchcode'};
78   }
79   my $calendar = C4::Calendar->new( branchcode => $branchcode );
80
81   my $isHoliday = $calendar->isHoliday( split( '/', C4::Dates->new()->output('metric') ) );
82       
83  if ($datedue_days <= $today_days){
84     $overdueItemsCounted++ if $DEBUG;
85     my $difference=$today_days - $datedue_days;
86     my ($amount,$type,$printout,$daycounttotal,$daycount)=
87                 CalcFine($data->[$i], $borrower->{'categorycode'}, $branchcode,undef,undef, $datedue ,$today);
88     my ($delays1,$delays2,$delays3)=GetOverdueDelays($borrower->{'categorycode'});
89
90         # Don't update the fine if today is a holiday.  
91         # This ensures that dropbox mode will remove the correct amount of fine.
92         if( (C4::Context->preference('finesMode') eq 'production') &&  ! $isHoliday ) {
93                 # FIXME - $type is always null, afaict.
94                 UpdateFine($data->[$i]->{'itemnumber'},$data->[$i]->{'borrowernumber'},$amount,$type,$due_str) if( $amount > 0 ) ;
95         }
96         print FILE "$printout\t$borrower->{'cardnumber'}\t$borrower->{'categorycode'}\t$borrower->{'surname'}\t$borrower->{'firstname'}\t$borrower->{'email'}\t$borrower->{'phone'}\t$borrower->{'address'}\t$borrower->{'city'}\t$data->[$i]->{'barcode'}\t$data->[$i]->{'date_due'}\t$type\t$data->[$i]->{'itemnumber'}\t$daycounttotal\t$amount\n";
97  }
98 }
99
100 my $numOverdueItems=scalar(@$data);
101 if ($DEBUG) {
102    print <<EOM
103
104 Number of Overdue Items counted $overdueItemsCounted
105 Number of Overdue Items reported $numOverdueItems
106
107 EOM
108 }
109
110 close FILE;