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;
54 'v|verbose' => \$verbose,
56 'o|out:s' => \$output_dir,
57 'm|maxdays:i' => \$maxdays,
59 my $usage = << 'ENDUSAGE';
61 This script calculates and charges overdue fines
62 to patron accounts. The Koha system preference 'finesMode' controls
63 whether the fines are calculated and charged to the patron accounts ("Calculate and charge");
64 or not calculated ("Don't calculate").
66 This script has the following parameters :
67 -h --help: this message
68 -l --log: log the output to a file (optional if the -o parameter is given)
69 -o --out: ouput directory for logs (defaults to env or /tmp if !exist)
71 -m --maxdays: how many days back of overdues to process
80 my $script_handler = Koha::Script->new({ script => $0 });
83 $script_handler->lock_exec;
86 my $message = "Skipping execution of $0 ($_)";
87 print STDERR "$message\n"
89 cronlogaction( $message );
96 qw(cardnumber categorycode surname firstname email phone address citystate);
97 my @item_fields = qw(itemnumber barcode date_due);
98 my @other_fields = qw(days_overdue fine);
99 my $libname = C4::Context->preference('LibraryName');
100 my $control = C4::Context->preference('CircControl');
101 my $mode = C4::Context->preference('finesMode');
102 my $delim = "\t"; # ? C4::Context->preference('delimiter') || "\t";
105 my $today = dt_from_string();
107 if ($log or $output_dir) {
108 $filename = get_filename($output_dir);
113 open $fh, '>>', $filename or croak "Cannot write file $filename: $!";
114 print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
119 $params->{maximumdays} = $maxdays if $maxdays;
120 my $overdues = Getoverdues($params);
121 for my $overdue ( @{$overdues} ) {
122 next if $overdue->{itemlost};
124 if ( !defined $overdue->{borrowernumber} ) {
126 "ERROR in Getoverdues : issues.borrowernumber IS NULL. Repair 'issues' table now! Skipping record.\n";
129 my $patron = Koha::Patrons->find( $overdue->{borrowernumber} );
131 ( $control eq 'ItemHomeLibrary' ) ? $overdue->{homebranch}
132 : ( $control eq 'PatronLibrary' ) ? $patron->branchcode
133 : $overdue->{branchcode};
134 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
136 my $datedue = dt_from_string( $overdue->{date_due} );
137 if ( DateTime->compare( $datedue, $today ) == 1 ) {
142 my ( $amount, $unitcounttotal, $unitcount ) =
143 CalcFine( $overdue, $patron->categorycode,
144 $branchcode, $datedue, $today );
146 if ( $mode eq 'production' ) {
147 if ( $amount && $amount > 0 ) {
150 issue_id => $overdue->{issue_id},
151 itemnumber => $overdue->{itemnumber},
152 borrowernumber => $overdue->{borrowernumber},
154 due => output_pref($datedue),
159 my $borrower = $patron->unblessed;
163 map { defined $borrower->{$_} ? $borrower->{$_} : q{} }
165 push @cells, map { $overdue->{$_} } @item_fields;
166 push @cells, $unitcounttotal, $amount;
167 say {$fh} join $delim, @cells;
175 my $overdue_items = @{$overdues};
177 Fines assessment -- $today
180 say "Saved to $filename";
183 Number of Overdue Items:
184 counted $overdue_items
191 my $directory = shift;
193 $directory = C4::Context::temporary_directory;
195 if ( !-d $directory ) {
196 carp "Could not write to $directory ... does not exist!";
198 my $name = C4::Context->config('database');
200 $name .= join q{}, q{_}, $today->ymd(), '.log';
201 $name = File::Spec->catfile( $directory, $name );
202 if ($verbose && $log) {
203 say "writing to $name";