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>.
32 # find Koha's Perl modules
33 # test carefully before changing this
35 eval { require "$FindBin::Bin/kohalib.pl" };
38 use Date::Calc qw( Date_to_Days );
40 use Koha::Script -cron;
42 use C4::Overdues qw( CalcFine checkoverdues GetFine Getoverdues );
43 use C4::Calendar qw(); # don't need any exports from Calendar
44 use C4::Log qw( cronlogaction );
45 use Getopt::Long qw( GetOptions );
46 use List::MoreUtils qw( none );
47 use Koha::DateUtils qw( dt_from_string output_pref );
57 my $useborrowerlibrary;
58 my $borrowernumberlimit;
59 my $borrowersalreadyapplied; # hashref of borrowers for whom we already applied the fine, so it's only applied once
65 'v|verbose' => \$verbose,
66 'c|category:s'=> \@pcategories,
67 'l|library:s' => \@libraries,
68 'd|delay:i' => \$delay,
69 'u|use-borrower-library' => \$useborrowerlibrary,
70 'b|borrower:i' => \$borrowernumberlimit
72 my $usage = << 'ENDUSAGE';
74 This script calculates and charges overdue fines to patron accounts.
76 If the Koha System Preference 'finesMode' is set to 'production', the fines are charged to the patron accounts.
78 Please note that the fines won't be applied on a holiday.
80 This script has the following parameters :
81 -h --help: this message
83 -c --category borrower_category,amount (repeatable)
84 -l --library (repeatable)
86 -u --use-borrower-library: use borrower's library, regardless of the CircControl syspref
87 -b --borrower borrowernumber: only for one given borrower
94 my $dbh = C4::Context->dbh;
96 # Processing categories
97 foreach (@pcategories) {
98 my ($category, $amount) = split(',', $_);
99 push @categories, $category;
100 $catamounts{$category} = $amount;
103 use vars qw(@borrower_fields @item_fields @other_fields);
104 use vars qw($fldir $libname $control $mode $delim $dbname $today $today_iso $today_days);
105 use vars qw($filename);
108 @borrower_fields = qw(cardnumber categorycode surname firstname email phone address citystate);
109 @item_fields = qw(itemnumber barcode date_due);
110 @other_fields = qw(type days_overdue fine);
111 $libname = C4::Context->preference('LibraryName');
112 $control = C4::Context->preference('CircControl');
113 $mode = C4::Context->preference('finesMode');
114 $dbname = C4::Context->config('database');
115 $delim = "\t"; # ? C4::Context->preference('delimiter') || "\t";
120 $debug and print "Each line will contain the following fields:\n",
121 "From borrowers : ", join( ', ', @borrower_fields ), "\n",
122 "From items : ", join( ', ', @item_fields ), "\n",
123 "Per overdue: ", join( ', ', @other_fields ), "\n",
124 "Delimiter: '$delim'\n";
126 $debug and (defined $borrowernumberlimit) and print "--borrower limitation: borrower $borrowernumberlimit\n";
127 my ($numOverdueItems, $data);
128 if (defined $borrowernumberlimit) {
129 ($numOverdueItems, $data) = checkoverdues($borrowernumberlimit);
131 $data = Getoverdues();
132 $numOverdueItems = scalar @$data;
134 my $overdueItemsCounted = 0;
136 $today = dt_from_string;
137 $today_iso = output_pref( { dt => $today, dateonly => 1, dateformat => 'iso' } );
138 my ($tyear, $tmonth, $tday) = split( /-/, $today_iso );
139 $today_days = Date_to_Days( $tyear, $tmonth, $tday );
141 for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) {
142 next if $data->[$i]->{'itemlost'};
143 my ( $datedue, $datedue_days );
145 $datedue = dt_from_string( $data->[$i]->{'date_due'} );
146 my $datedue_iso = output_pref( { dt => $datedue, dateonly => 1, dateformat => 'iso' } );
147 $datedue_days = Date_to_Days( split( /-/, $datedue_iso ) );
150 warn "Error on date for borrower " . $data->[$i]->{'borrowernumber'} . ": $@date_due: " . $data->[$i]->{'date_due'} . "\ndatedue_days: " . $datedue_days . "\nSkipping";
153 my $due_str = output_pref( { dt => $datedue, dateonly => 1 } );
154 unless ( defined $data->[$i]->{'borrowernumber'} ) {
155 print STDERR "ERROR in Getoverdues line $i: issues.borrowernumber IS NULL. Repair 'issues' table now! Skipping record.\n";
156 next; # Note: this doesn't solve everything. After NULL borrowernumber, multiple issues w/ real borrowernumbers can pile up.
158 my $patron = Koha::Patrons->find( $data->[$i]->{'borrowernumber'} );
160 # Skipping borrowers that are not in @categories
161 $bigdebug and warn "Skipping borrower from category " . $patron->categorycode if none { $patron->categorycode eq $_ } @categories;
162 next if none { $patron->categorycode eq $_ } @categories;
165 ( $useborrowerlibrary ) ? $patron->branchcode
166 : ( $control eq 'ItemHomeLibrary' ) ? $data->[$i]->{homebranch}
167 : ( $control eq 'PatronLibrary' ) ? $patron->branchcode
168 : $data->[$i]->{branchcode};
169 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
171 # Skipping branchcodes that are not in @libraries
172 $bigdebug and warn "Skipping library $branchcode" if none { $branchcode eq $_ } @libraries;
173 next if none { $branchcode eq $_ } @libraries;
176 unless ( defined( $calendars{$branchcode} ) ) {
177 $calendars{$branchcode} = C4::Calendar->new( branchcode => $branchcode );
179 $calendar = $calendars{$branchcode};
180 my $isHoliday = $calendar->isHoliday( $tday, $tmonth, $tyear );
182 # Reassing datedue_days if -delay specified in commandline
183 $bigdebug and warn "Using commandline supplied delay : $delay" if ($delay);
184 $datedue_days += $delay if ($delay);
186 ( $datedue_days <= $today_days ) or next; # or it's not overdue, right?
188 $overdueItemsCounted++;
189 my ( $amount, $unitcounttotal, $unitcount ) = CalcFine(
191 $patron->categorycode,
197 # Reassign fine's amount if specified in command-line
198 $amount = $catamounts{$patron->categorycode} if (defined $catamounts{$patron->categorycode});
200 # We check if there is already a fine for the given borrower
201 my $fine = GetFine(undef, $data->[$i]->{'borrowernumber'});
203 $debug and warn "There is already a fine for borrower " . $data->[$i]->{'borrowernumber'} . ". Nothing to do here. Skipping this borrower";
207 # Don't update the fine if today is a holiday.
208 # This ensures that dropbox mode will remove the correct amount of fine.
209 if ( $mode eq 'production' and !$borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}}) {
210 # If we're on a holiday, warn the user (if debug) that no fine will be applied
212 $debug and warn "Today is a holiday. The fine for borrower " . $data->[$i]->{'borrowernumber'} . " will not be applied";
214 $debug and warn "Creating fine for borrower " . $data->[$i]->{'borrowernumber'} . " with amount : $amount";
216 # We mark this borrower as already processed
217 $borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}} = 1;
219 my $borrowernumber = $data->[$i]->{'borrowernumber'};
220 my $itemnumber = $data->[$i]->{'itemnumber'};
222 # And we create the fine
223 my $sth4 = $dbh->prepare( "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?" );
224 $sth4->execute($itemnumber);
225 my $title = $sth4->fetchrow;
227 my $desc = "staticfine";
228 my $query = "INSERT INTO accountlines
229 (borrowernumber,itemnumber,date,amount,description,debit_type_code,status,amountoutstanding)
230 VALUES (?,?,now(),?,?,'OVERDUE','RETURNED',?)";
231 my $sth2 = $dbh->prepare($query);
232 $bigdebug and warn "query: $query\nw/ args: $borrowernumber, $itemnumber, $amount, $desc, $amount\n";
233 $sth2->execute( $borrowernumber, $itemnumber, $amount, $desc, $amount );
241 Fines assessment -- $today_iso
242 Number of Overdue Items:
243 counted $overdueItemsCounted
244 reported $numOverdueItems