fix malformed call of XSLTParse4Display
[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
25 # with Koha; if not, write to the Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
28
29 # FIXME: use FinesMode as described or change syspref description
30 use strict;
31 #use warnings; FIXME - Bug 2505
32
33 BEGIN {
34     # find Koha's Perl modules
35     # test carefully before changing this
36     use FindBin;
37     eval { require "$FindBin::Bin/kohalib.pl" };
38 }
39
40 use Date::Calc qw/Date_to_Days/;
41
42 use C4::Context;
43 use C4::Circulation;
44 use C4::Overdues;
45 use C4::Calendar qw();  # don't need any exports from Calendar
46 use C4::Biblio;
47 use C4::Debug;  # supplying $debug and $cgi_debug
48 use Getopt::Long;
49
50 my $help = 0;
51 my $verbose = 0;
52 my $output_dir;
53
54 GetOptions( 'h|help'    => \$help,
55             'v|verbose' => \$verbose,
56             'o|out:s'   => \$output_dir,
57        );
58 my $usage = << 'ENDUSAGE';
59
60 This script calculates and charges overdue fines
61 to patron accounts.  If the Koha System Preference
62 'finesMode' is set to 'production', the fines are charged
63 to the patron accounts.  If set to 'test', the fines are
64 calculated but not applied.
65
66 This script has the following parameters :
67     -h --help: this message
68     -o --out:  ouput directory for logs (defaults to env or /tmp if !exist)
69     -v --verbose
70
71 ENDUSAGE
72
73 die $usage if $help;
74
75 use vars qw(@borrower_fields @item_fields @other_fields);
76 use vars qw($fldir $libname $control $mode $delim $dbname $today $today_iso $today_days);
77 use vars qw($filename);
78
79 CHECK {
80     @borrower_fields = qw(cardnumber categorycode surname firstname email phone address citystate);
81         @item_fields = qw(itemnumber barcode date_due);
82        @other_fields = qw(type days_overdue fine);
83     $libname = C4::Context->preference('LibraryName');
84     $control = C4::Context->preference('CircControl');
85     $mode    = C4::Context->preference('finesMode');
86     $dbname  = C4::Context->config('database');
87     $delim   = "\t"; # ?  C4::Context->preference('delimiter') || "\t";
88
89 }
90
91 INIT {
92     $debug and print "Each line will contain the following fields:\n",
93         "From borrowers : ", join(', ', @borrower_fields), "\n",
94         "From items : ", join(', ', @item_fields), "\n",
95         "Per overdue: ", join(', ', @other_fields), "\n",
96         "Delimiter: '$delim'\n";
97 }
98
99 my $data = Getoverdues();
100 my $overdueItemsCounted = 0;
101 my %calendars = ();
102 $today = C4::Dates->new();
103 $today_iso = $today->output('iso');
104 $today_days = Date_to_Days(split(/-/,$today_iso));
105 if($output_dir){
106     $fldir = $output_dir if( -d $output_dir );
107 } else {
108     $fldir = $ENV{TMPDIR} || "/tmp";
109 }
110 if (!-d $fldir) {
111     warn "Could not write to $fldir ... does not exist!";
112 }
113 $filename = $dbname;
114 $filename =~ s/\W//;
115 $filename = $fldir . '/'. $filename . '_' .  $today_iso . ".log";
116 print "writing to $filename\n" if $verbose;
117 open (FILE, ">$filename") or die "Cannot write file $filename: $!";
118 print FILE join $delim, (@borrower_fields, @item_fields, @other_fields);
119 print FILE "\n";
120
121 for (my $i=0; $i<scalar(@$data); $i++) {
122     my $datedue = C4::Dates->new($data->[$i]->{'date_due'},'iso');
123     my $datedue_days = Date_to_Days(split(/-/,$datedue->output('iso')));
124     my $due_str = $datedue->output();
125     unless (defined $data->[$i]->{'borrowernumber'}) {
126         print STDERR "ERROR in Getoverdues line $i: issues.borrowernumber IS NULL.  Repair 'issues' table now!  Skipping record.\n";
127         next;   # Note: this doesn't solve everything.  After NULL borrowernumber, multiple issues w/ real borrowernumbers can pile up.
128     }
129     my $borrower = BorType($data->[$i]->{'borrowernumber'});
130     my $branchcode = ($control eq 'ItemHomeLibrary') ? $data->[$i]->{homebranch} :
131                      ($control eq 'PatronLibrary'  ) ?   $borrower->{branchcode} :
132                                                        $data->[$i]->{branchcode} ;
133     # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
134     my $calendar;
135     unless (defined ($calendars{$branchcode})) {
136         $calendars{$branchcode} = C4::Calendar->new(branchcode => $branchcode);
137     }
138     $calendar = $calendars{$branchcode};
139     my $isHoliday = $calendar->isHoliday(split '/', $today->output('metric'));
140       
141     ($datedue_days <= $today_days) or next; # or it's not overdue, right?
142
143     $overdueItemsCounted++;
144     my ($amount,$type,$daycounttotal,$daycount)=
145                 CalcFine($data->[$i], $borrower->{'categorycode'}, $branchcode,undef,undef, $datedue, $today);
146         # FIXME: $type NEVER gets populated by anything.
147     (defined $type) or $type = '';
148         # Don't update the fine if today is a holiday.  
149         # This ensures that dropbox mode will remove the correct amount of fine.
150         if ($mode eq 'production' and  ! $isHoliday) {
151                 UpdateFine($data->[$i]->{'itemnumber'},$data->[$i]->{'borrowernumber'},$amount,$type,$due_str) if( $amount > 0 ) ;
152         }
153     my @cells = ();
154     push @cells, map {$borrower->{$_}} @borrower_fields;
155     push @cells, map {$data->[$i]->{$_}} @item_fields;
156     push @cells, $type, $daycounttotal, $amount;
157     print FILE join($delim, @cells), "\n";
158 }
159
160 my $numOverdueItems = scalar(@$data);
161 if ($verbose) {
162    print <<EOM;
163 Fines assessment -- $today_iso -- Saved to $filename
164 Number of Overdue Items:
165      counted $overdueItemsCounted
166     reported $numOverdueItems
167
168 EOM
169 }
170
171 close FILE;