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;
35 use C4::Overdues qw( Getoverdues CalcFine UpdateFine );
36 use Getopt::Long qw( GetOptions );
37 use Carp qw( carp croak );
39 use Try::Tiny qw( catch try );
42 use Koha::DateUtils qw( dt_from_string output_pref );
44 use C4::Log qw( cronlogaction );
52 my $command_line_options = join(" ",@ARGV);
56 'v|verbose' => \$verbose,
58 'o|out:s' => \$output_dir,
59 'm|maxdays:i' => \$maxdays,
61 my $usage = << 'ENDUSAGE';
63 This script calculates and charges overdue fines
64 to patron accounts. The Koha system preference 'finesMode' controls
65 whether the fines are calculated and charged to the patron accounts ("Calculate and charge");
66 or not calculated ("Don't calculate").
68 This script has the following parameters :
69 -h --help: this message
70 -l --log: log the output to a file (optional if the -o parameter is given)
71 -o --out: ouput directory for logs (defaults to env or /tmp if !exist)
73 -m --maxdays: how many days back of overdues to process
82 my $script_handler = Koha::Script->new({ script => $0 });
85 $script_handler->lock_exec;
88 my $message = "Skipping execution of $0 ($_)";
89 print STDERR "$message\n"
91 cronlogaction( $message );
95 cronlogaction({ info => $command_line_options });
98 qw(cardnumber categorycode surname firstname email phone address citystate);
99 my @item_fields = qw(itemnumber barcode date_due);
100 my @other_fields = qw(days_overdue fine);
101 my $libname = C4::Context->preference('LibraryName');
102 my $control = C4::Context->preference('CircControl');
103 my $mode = C4::Context->preference('finesMode');
104 my $delim = "\t"; # ? C4::Context->preference('CSVDelimiter') || "\t";
107 my $today = dt_from_string();
109 if ($log or $output_dir) {
110 $filename = get_filename($output_dir);
115 open $fh, '>>', $filename or croak "Cannot write file $filename: $!";
116 print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
122 $params->{maximumdays} = $maxdays if $maxdays;
123 my $overdues = Getoverdues($params);
124 for my $overdue ( @{$overdues} ) {
125 next if $overdue->{itemlost};
127 if ( !defined $overdue->{borrowernumber} ) {
129 "ERROR in Getoverdues : issues.borrowernumber IS NULL. Repair 'issues' table now! Skipping record.\n";
132 my $patron = Koha::Patrons->find( $overdue->{borrowernumber} );
134 ( $control eq 'ItemHomeLibrary' ) ? $overdue->{homebranch}
135 : ( $control eq 'PatronLibrary' ) ? $patron->branchcode
136 : $overdue->{branchcode};
138 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
139 if ( !exists $is_holiday{$branchcode} ) {
140 $is_holiday{$branchcode} = set_holiday( $branchcode, $today );
143 my $datedue = dt_from_string( $overdue->{date_due} );
144 if ( DateTime->compare( $datedue, $today ) == 1 ) {
149 my ( $amount, $unitcounttotal, $unitcount ) =
150 CalcFine( $overdue, $patron->categorycode,
151 $branchcode, $datedue, $today );
153 # Don't update the fine if today is a holiday.
154 # This ensures that dropbox mode will remove the correct amount of fine.
156 $mode eq 'production'
157 && ( !$is_holiday{$branchcode}
158 || C4::Context->preference('ChargeFinesOnClosedDays') )
159 && ( $amount && $amount > 0 )
164 issue_id => $overdue->{issue_id},
165 itemnumber => $overdue->{itemnumber},
166 borrowernumber => $overdue->{borrowernumber},
173 my $borrower = $patron->unblessed;
177 map { defined $borrower->{$_} ? $borrower->{$_} : q{} }
179 push @cells, map { $overdue->{$_} } @item_fields;
180 push @cells, $unitcounttotal, $amount;
181 say {$fh} join $delim, @cells;
189 my $overdue_items = @{$overdues};
191 Fines assessment -- $today
194 say "Saved to $filename";
197 Number of Overdue Items:
198 counted $overdue_items
205 cronlogaction({ action => 'End', info => "COMPLETED" });
208 my ( $branch, $dt ) = @_;
210 my $calendar = Koha::Calendar->new( branchcode => $branch );
211 return $calendar->is_holiday($dt);
215 my $directory = shift;
217 $directory = C4::Context::temporary_directory;
219 if ( !-d $directory ) {
220 carp "Could not write to $directory ... does not exist!";
222 my $name = C4::Context->config('database');
224 $name .= join q{}, q{_}, $today->ymd(), '.log';
225 $name = File::Spec->catfile( $directory, $name );
226 if ($verbose && $log) {
227 say "writing to $name";