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 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 2000-2002 Katipo Communications
12 # Copyright 2011 PTFS-Europe Ltd
14 # This file is part of Koha.
16 # Koha is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 3 of the License, or
19 # (at your option) any later version.
21 # Koha is distributed in the hope that it will be useful, but
22 # WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with Koha; if not, see <http://www.gnu.org/licenses>.
33 use Koha::Script -cron;
51 'v|verbose' => \$verbose,
53 'o|out:s' => \$output_dir,
55 my $usage = << 'ENDUSAGE';
57 This script calculates and charges overdue fines
58 to patron accounts. The Koha system preference 'finesMode' controls
59 whether the fines are calculated and charged to the patron accounts ("Calculate and charge");
60 calculated and emailed to the admin but not applied ("Calculate (but only for mailing to the admin)"); or not calculated ("Don't calculate").
62 This script has the following parameters :
63 -h --help: this message
64 -l --log: log the output to a file (optional if the -o parameter is given)
65 -o --out: ouput directory for logs (defaults to env or /tmp if !exist)
78 qw(cardnumber categorycode surname firstname email phone address citystate);
79 my @item_fields = qw(itemnumber barcode date_due);
80 my @other_fields = qw(days_overdue fine);
81 my $libname = C4::Context->preference('LibraryName');
82 my $control = C4::Context->preference('CircControl');
83 my $mode = C4::Context->preference('finesMode');
84 my $delim = "\t"; # ? C4::Context->preference('delimiter') || "\t";
87 my $today = DateTime->now( time_zone => C4::Context->tz() );
89 if ($log or $output_dir) {
90 $filename = get_filename($output_dir);
95 open $fh, '>>', $filename or croak "Cannot write file $filename: $!";
96 print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
100 my $overdues = Getoverdues();
101 for my $overdue ( @{$overdues} ) {
102 next if $overdue->{itemlost};
104 if ( !defined $overdue->{borrowernumber} ) {
106 "ERROR in Getoverdues : issues.borrowernumber IS NULL. Repair 'issues' table now! Skipping record.\n";
109 my $borrower = BorType( $overdue->{borrowernumber} );
111 ( $control eq 'ItemHomeLibrary' ) ? $overdue->{homebranch}
112 : ( $control eq 'PatronLibrary' ) ? $borrower->{branchcode}
113 : $overdue->{branchcode};
115 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
116 if ( !exists $is_holiday{$branchcode} ) {
117 $is_holiday{$branchcode} = set_holiday( $branchcode, $today );
120 my $datedue = dt_from_string( $overdue->{date_due} );
121 if ( DateTime->compare( $datedue, $today ) == 1 ) {
126 my ( $amount, $unitcounttotal, $unitcount ) =
127 CalcFine( $overdue, $borrower->{categorycode},
128 $branchcode, $datedue, $today );
130 # Don't update the fine if today is a holiday.
131 # This ensures that dropbox mode will remove the correct amount of fine.
132 if ( $mode eq 'production' && !$is_holiday{$branchcode} ) {
133 if ( $amount && $amount > 0 ) {
136 issue_id => $overdue->{issue_id},
137 itemnumber => $overdue->{itemnumber},
138 borrowernumber => $overdue->{borrowernumber},
140 due => output_pref($datedue),
148 map { defined $borrower->{$_} ? $borrower->{$_} : q{} }
150 push @cells, map { $overdue->{$_} } @item_fields;
151 push @cells, $unitcounttotal, $amount;
152 say {$fh} join $delim, @cells;
160 my $overdue_items = @{$overdues};
162 Fines assessment -- $today
165 say "Saved to $filename";
168 Number of Overdue Items:
169 counted $overdue_items
176 my ( $branch, $dt ) = @_;
178 my $calendar = Koha::Calendar->new( branchcode => $branch );
179 return $calendar->is_holiday($dt);
183 my $directory = shift;
185 $directory = C4::Context::temporary_directory;
187 if ( !-d $directory ) {
188 carp "Could not write to $directory ... does not exist!";
190 my $name = C4::Context->config('database');
192 $name .= join q{}, q{_}, $today->ymd(), '.log';
193 $name = File::Spec->catfile( $directory, $name );
194 if ($verbose && $log) {
195 say "writing to $name";