Bug 23148: Fix bridge image paths in de-DE installer files
[koha.git] / misc / cronjobs / staticfines.pl
blob1e7d28770285fd3b077847872c0b2247478e66ea
1 #!/usr/bin/perl
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 # Doesn't really rely on it, it relies on being able to write to /tmp/
7 # It creates the fines file
9 # This script is meant to be run nightly out of cron.
11 # Copyright 2011-2012 BibLibre
13 # This file is part of Koha.
15 # Koha is free software; you can redistribute it and/or modify it
16 # under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 3 of the License, or
18 # (at your option) any later version.
20 # Koha is distributed in the hope that it will be useful, but
21 # WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 use Modern::Perl;
30 BEGIN {
32 # find Koha's Perl modules
33 # test carefully before changing this
34 use FindBin;
35 eval { require "$FindBin::Bin/kohalib.pl" };
38 use Date::Calc qw/Date_to_Days/;
40 use Koha::Script -cron;
41 use C4::Context;
42 use C4::Circulation;
43 use C4::Overdues;
44 use C4::Calendar qw(); # don't need any exports from Calendar
45 use C4::Biblio;
46 use C4::Debug; # supplying $debug and $cgi_debug
47 use C4::Log;
48 use Getopt::Long;
49 use List::MoreUtils qw/none/;
50 use Koha::DateUtils;
51 use Koha::Patrons;
53 my $help = 0;
54 my $verbose = 0;
55 my @pcategories;
56 my @categories;
57 my %catamounts;
58 my @libraries;
59 my $delay;
60 my $useborrowerlibrary;
61 my $borrowernumberlimit;
62 my $borrowersalreadyapplied; # hashref of borrowers for whom we already applied the fine, so it's only applied once
63 my $debug = $ENV{'DEBUG'} || 0;
64 my $bigdebug = 0;
66 GetOptions(
67 'h|help' => \$help,
68 'v|verbose' => \$verbose,
69 'c|category:s'=> \@pcategories,
70 'l|library:s' => \@libraries,
71 'd|delay:i' => \$delay,
72 'u|use-borrower-library' => \$useborrowerlibrary,
73 'b|borrower:i' => \$borrowernumberlimit
75 my $usage = << 'ENDUSAGE';
77 This script calculates and charges overdue fines to patron accounts.
79 If the Koha System Preference 'finesMode' is set to 'production', the fines are charged to the patron accounts.
81 Please note that the fines won't be applied on a holiday.
83 This script has the following parameters :
84 -h --help: this message
85 -v --verbose
86 -c --category borrower_category,amount (repeatable)
87 -l --library (repeatable)
88 -d --delay
89 -u --use-borrower-library: use borrower's library, regardless of the CircControl syspref
90 -b --borrower borrowernumber: only for one given borrower
92 ENDUSAGE
93 die $usage if $help;
95 cronlogaction();
97 my $dbh = C4::Context->dbh;
99 # Processing categories
100 foreach (@pcategories) {
101 my ($category, $amount) = split(',', $_);
102 push @categories, $category;
103 $catamounts{$category} = $amount;
106 use vars qw(@borrower_fields @item_fields @other_fields);
107 use vars qw($fldir $libname $control $mode $delim $dbname $today $today_iso $today_days);
108 use vars qw($filename);
110 CHECK {
111 @borrower_fields = qw(cardnumber categorycode surname firstname email phone address citystate);
112 @item_fields = qw(itemnumber barcode date_due);
113 @other_fields = qw(type days_overdue fine);
114 $libname = C4::Context->preference('LibraryName');
115 $control = C4::Context->preference('CircControl');
116 $mode = C4::Context->preference('finesMode');
117 $dbname = C4::Context->config('database');
118 $delim = "\t"; # ? C4::Context->preference('delimiter') || "\t";
122 INIT {
123 $debug and print "Each line will contain the following fields:\n",
124 "From borrowers : ", join( ', ', @borrower_fields ), "\n",
125 "From items : ", join( ', ', @item_fields ), "\n",
126 "Per overdue: ", join( ', ', @other_fields ), "\n",
127 "Delimiter: '$delim'\n";
129 $debug and (defined $borrowernumberlimit) and print "--borrower limitation: borrower $borrowernumberlimit\n";
130 my ($numOverdueItems, $data);
131 if (defined $borrowernumberlimit) {
132 ($numOverdueItems, $data) = checkoverdues($borrowernumberlimit);
133 } else {
134 $data = Getoverdues();
135 $numOverdueItems = scalar @$data;
137 my $overdueItemsCounted = 0;
138 my %calendars = ();
139 $today = dt_from_string;
140 $today_iso = output_pref( { dt => $today, dateonly => 1, dateformat => 'iso' } );
141 my ($tyear, $tmonth, $tday) = split( /-/, $today_iso );
142 $today_days = Date_to_Days( $tyear, $tmonth, $tday );
144 for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) {
145 next if $data->[$i]->{'itemlost'};
146 my ( $datedue, $datedue_days );
147 eval {
148 $datedue = dt_from_string( $data->[$i]->{'date_due'} );
149 my $datedue_iso = output_pref( { dt => $datedue, dateonly => 1, dateformat => 'iso' } );
150 $datedue_days = Date_to_Days( split( /-/, $datedue_iso ) );
152 if ($@) {
153 warn "Error on date for borrower " . $data->[$i]->{'borrowernumber'} . ": $@date_due: " . $data->[$i]->{'date_due'} . "\ndatedue_days: " . $datedue_days . "\nSkipping";
154 next;
156 my $due_str = output_pref( { dt => $datedue, dateonly => 1 } );
157 unless ( defined $data->[$i]->{'borrowernumber'} ) {
158 print STDERR "ERROR in Getoverdues line $i: issues.borrowernumber IS NULL. Repair 'issues' table now! Skipping record.\n";
159 next; # Note: this doesn't solve everything. After NULL borrowernumber, multiple issues w/ real borrowernumbers can pile up.
161 my $patron = Koha::Patrons->find( $data->[$i]->{'borrowernumber'} );
163 # Skipping borrowers that are not in @categories
164 $bigdebug and warn "Skipping borrower from category " . $patron->categorycode if none { $patron->categorycode eq $_ } @categories;
165 next if none { $patron->categorycode eq $_ } @categories;
167 my $branchcode =
168 ( $useborrowerlibrary ) ? $patron->branchcode
169 : ( $control eq 'ItemHomeLibrary' ) ? $data->[$i]->{homebranch}
170 : ( $control eq 'PatronLibrary' ) ? $patron->branchcode
171 : $data->[$i]->{branchcode};
172 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
174 # Skipping branchcodes that are not in @libraries
175 $bigdebug and warn "Skipping library $branchcode" if none { $branchcode eq $_ } @libraries;
176 next if none { $branchcode eq $_ } @libraries;
178 my $calendar;
179 unless ( defined( $calendars{$branchcode} ) ) {
180 $calendars{$branchcode} = C4::Calendar->new( branchcode => $branchcode );
182 $calendar = $calendars{$branchcode};
183 my $isHoliday = $calendar->isHoliday( $tday, $tmonth, $tyear );
185 # Reassing datedue_days if -delay specified in commandline
186 $bigdebug and warn "Using commandline supplied delay : $delay" if ($delay);
187 $datedue_days += $delay if ($delay);
189 ( $datedue_days <= $today_days ) or next; # or it's not overdue, right?
191 $overdueItemsCounted++;
192 my ( $amount, $unitcounttotal, $unitcount ) = CalcFine(
193 $data->[$i],
194 $patron->categorycode,
195 $branchcode,
196 $datedue,
197 $today,
200 # Reassign fine's amount if specified in command-line
201 $amount = $catamounts{$patron->categorycode} if (defined $catamounts{$patron->categorycode});
203 # We check if there is already a fine for the given borrower
204 my $fine = GetFine(undef, $data->[$i]->{'borrowernumber'});
205 if ($fine > 0) {
206 $debug and warn "There is already a fine for borrower " . $data->[$i]->{'borrowernumber'} . ". Nothing to do here. Skipping this borrower";
207 next;
210 # Don't update the fine if today is a holiday.
211 # This ensures that dropbox mode will remove the correct amount of fine.
212 if ( $mode eq 'production' and !$borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}}) {
213 # If we're on a holiday, warn the user (if debug) that no fine will be applied
214 if($isHoliday) {
215 $debug and warn "Today is a holiday. The fine for borrower " . $data->[$i]->{'borrowernumber'} . " will not be applied";
216 } else {
217 $debug and warn "Creating fine for borrower " . $data->[$i]->{'borrowernumber'} . " with amount : $amount";
219 # We mark this borrower as already processed
220 $borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}} = 1;
222 my $borrowernumber = $data->[$i]->{'borrowernumber'};
223 my $itemnumber = $data->[$i]->{'itemnumber'};
225 # And we create the fine
226 my $sth4 = $dbh->prepare( "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?" );
227 $sth4->execute($itemnumber);
228 my $title = $sth4->fetchrow;
230 my $desc = "staticfine";
231 my $query = "INSERT INTO accountlines
232 (borrowernumber,itemnumber,date,amount,description,debit_type_code,status,amountoutstanding)
233 VALUES (?,?,now(),?,?,'OVERDUE','RETURNED',?)";
234 my $sth2 = $dbh->prepare($query);
235 $bigdebug and warn "query: $query\nw/ args: $borrowernumber, $itemnumber, $amount, $desc, $amount\n";
236 $sth2->execute( $borrowernumber, $itemnumber, $amount, $desc, $amount );
242 if ($verbose) {
243 print <<EOM;
244 Fines assessment -- $today_iso
245 Number of Overdue Items:
246 counted $overdueItemsCounted
247 reported $numOverdueItems