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
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/;
43 use C4::Calendar qw(); # don't need any exports from Calendar
45 use C4::Debug; # supplying $debug and $cgi_debug
47 use List::MoreUtils qw/none/;
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
60 my $debug = $ENV{'DEBUG'} || 0;
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.
77 If set to 'test', the fines are calculated but not applied.
79 Please note that the fines won't be applied on a holiday.
81 This script has the following parameters :
82 -h --help: this message
84 -c --category borrower_category,amount (repeatable)
85 -l --library (repeatable)
87 -u --use-borrower-library: use borrower's library, regardless of the CircControl syspref
88 -b --borrower borrowernumber: only for one given borrower
93 my $dbh = C4::Context->dbh;
95 # Processing categories
96 foreach (@pcategories) {
97 my ($category, $amount) = split(',', $_);
98 push @categories, $category;
99 $catamounts{$category} = $amount;
102 use vars qw(@borrower_fields @item_fields @other_fields);
103 use vars qw($fldir $libname $control $mode $delim $dbname $today $today_iso $today_days);
104 use vars qw($filename);
107 @borrower_fields = qw(cardnumber categorycode surname firstname email phone address citystate);
108 @item_fields = qw(itemnumber barcode date_due);
109 @other_fields = qw(type days_overdue fine);
110 $libname = C4::Context->preference('LibraryName');
111 $control = C4::Context->preference('CircControl');
112 $mode = C4::Context->preference('finesMode');
113 $dbname = C4::Context->config('database');
114 $delim = "\t"; # ? C4::Context->preference('delimiter') || "\t";
119 $debug and print "Each line will contain the following fields:\n",
120 "From borrowers : ", join( ', ', @borrower_fields ), "\n",
121 "From items : ", join( ', ', @item_fields ), "\n",
122 "Per overdue: ", join( ', ', @other_fields ), "\n",
123 "Delimiter: '$delim'\n";
125 $debug and (defined $borrowernumberlimit) and print "--borrower limitation: borrower $borrowernumberlimit\n";
126 my ($numOverdueItems, $data);
127 if (defined $borrowernumberlimit) {
128 ($numOverdueItems, $data) = checkoverdues($borrowernumberlimit);
130 $data = Getoverdues();
131 $numOverdueItems = scalar @$data;
133 my $overdueItemsCounted = 0;
135 $today = C4::Dates->new();
136 $today_iso = $today->output('iso');
137 my ($tyear, $tmonth, $tday) = split( /-/, $today_iso );
138 $today_days = Date_to_Days( $tyear, $tmonth, $tday );
140 for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) {
141 next if $data->[$i]->{'itemlost'};
145 $datedue = C4::Dates->new( $data->[$i]->{'date_due'}, 'iso' );
146 $datedue_days = Date_to_Days( split( /-/, $datedue->output('iso') ) );
149 warn "Error on date for borrower " . $data->[$i]->{'borrowernumber'} . ": $@date_due: " . $data->[$i]->{'date_due'} . "\ndatedue_days: " . $datedue_days . "\nSkipping";
152 my $due_str = $datedue->output();
153 unless ( defined $data->[$i]->{'borrowernumber'} ) {
154 print STDERR "ERROR in Getoverdues line $i: issues.borrowernumber IS NULL. Repair 'issues' table now! Skipping record.\n";
155 next; # Note: this doesn't solve everything. After NULL borrowernumber, multiple issues w/ real borrowernumbers can pile up.
157 my $borrower = BorType( $data->[$i]->{'borrowernumber'} );
159 # Skipping borrowers that are not in @categories
160 $bigdebug and warn "Skipping borrower from category " . $borrower->{categorycode} if none { $borrower->{categorycode} eq $_ } @categories;
161 next if none { $borrower->{categorycode} eq $_ } @categories;
164 ( $useborrowerlibrary ) ? $borrower->{branchcode}
165 : ( $control eq 'ItemHomeLibrary' ) ? $data->[$i]->{homebranch}
166 : ( $control eq 'PatronLibrary' ) ? $borrower->{branchcode}
167 : $data->[$i]->{branchcode};
168 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
170 # Skipping branchcodes that are not in @libraries
171 $bigdebug and warn "Skipping library $branchcode" if none { $branchcode eq $_ } @libraries;
172 next if none { $branchcode eq $_ } @libraries;
175 unless ( defined( $calendars{$branchcode} ) ) {
176 $calendars{$branchcode} = C4::Calendar->new( branchcode => $branchcode );
178 $calendar = $calendars{$branchcode};
179 my $isHoliday = $calendar->isHoliday( $tday, $tmonth, $tyear );
181 # Reassing datedue_days if -delay specified in commandline
182 $bigdebug and warn "Using commandline supplied delay : $delay" if ($delay);
183 $datedue_days += $delay if ($delay);
185 ( $datedue_days <= $today_days ) or next; # or it's not overdue, right?
187 $overdueItemsCounted++;
188 my ( $amount, $type, $unitcounttotal, $unitcount ) = CalcFine(
190 $borrower->{'categorycode'},
192 dt_from_string($datedue->output('iso')),
193 dt_from_string($today->output('iso')),
196 # Reassign fine's amount if specified in command-line
197 $amount = $catamounts{$borrower->{'categorycode'}} if (defined $catamounts{$borrower->{'categorycode'}});
199 # We check if there is already a fine for the given borrower
200 my $fine = GetFine(undef, $data->[$i]->{'borrowernumber'});
202 $debug and warn "There is already a fine for borrower " . $data->[$i]->{'borrowernumber'} . ". Nothing to do here. Skipping this borrower";
206 # FIXME: $type NEVER gets populated by anything.
207 ( defined $type ) or $type = '';
209 # Don't update the fine if today is a holiday.
210 # This ensures that dropbox mode will remove the correct amount of fine.
211 if ( $mode eq 'production' and !$borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}}) {
212 # If we're on a holiday, warn the user (if debug) that no fine will be applied
214 $debug and warn "Today is a holiday. The fine for borrower " . $data->[$i]->{'borrowernumber'} . " will not be applied";
216 $debug and warn "Creating fine for borrower " . $data->[$i]->{'borrowernumber'} . " with amount : $amount";
218 # We mark this borrower as already processed
219 $borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}} = 1;
221 my $borrowernumber = $data->[$i]->{'borrowernumber'};
222 my $itemnumber = $data->[$i]->{'itemnumber'};
224 # And we create the fine
225 my $sth4 = $dbh->prepare( "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?" );
226 $sth4->execute($itemnumber);
227 my $title = $sth4->fetchrow;
229 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
230 my $desc = "staticfine";
231 my $query = "INSERT INTO accountlines
232 (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
233 VALUES (?,?,now(),?,?,'F',?,?,?)";
234 my $sth2 = $dbh->prepare($query);
235 $bigdebug and warn "query: $query\nw/ args: $borrowernumber, $itemnumber, $amount, $desc, $amount, $amount, $nextaccntno\n";
236 $sth2->execute( $borrowernumber, $itemnumber, $amount, $desc, $amount, $amount, $nextaccntno );
244 Fines assessment -- $today_iso
245 Number of Overdue Items:
246 counted $overdueItemsCounted
247 reported $numOverdueItems