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 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 under the
17 # terms of the GNU General Public License as published by the Free Software
18 # Foundation; either version 2 of the License, or (at your option) any later
21 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
22 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
23 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License along
26 # with Koha; if not, write to the Free Software Foundation, Inc.,
27 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
49 'v|verbose' => \$verbose,
51 'o|out:s' => \$output_dir,
53 my $usage = << 'ENDUSAGE';
55 This script calculates and charges overdue fines
56 to patron accounts. If the Koha System Preference
57 'finesMode' is set to 'production', the fines are charged
58 to the patron accounts. If set to 'test', the fines are
59 calculated but not applied.
61 This script has the following parameters :
62 -h --help: this message
63 -l --log: log the output to a file
64 -o --out: ouput directory for logs (defaults to env or /tmp if !exist)
75 qw(cardnumber categorycode surname firstname email phone address citystate);
76 my @item_fields = qw(itemnumber barcode date_due);
77 my @other_fields = qw(type days_overdue fine);
78 my $libname = C4::Context->preference('LibraryName');
79 my $control = C4::Context->preference('CircControl');
80 my $mode = C4::Context->preference('finesMode');
81 my $delim = "\t"; # ? C4::Context->preference('delimiter') || "\t";
84 my $today = DateTime->now( time_zone => C4::Context->tz() );
85 my $filename = get_filename($output_dir);
89 open $fh, '>>', $filename or croak "Cannot write file $filename: $!";
90 print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
94 my $overdues = Getoverdues();
95 for my $overdue ( @{$overdues} ) {
96 if ( !defined $overdue->{borrowernumber} ) {
98 "ERROR in Getoverdues : issues.borrowernumber IS NULL. Repair 'issues' table now! Skipping record.\n";
101 my $borrower = BorType( $overdue->{borrowernumber} );
103 ( $control eq 'ItemHomeLibrary' ) ? $overdue->{homebranch}
104 : ( $control eq 'PatronLibrary' ) ? $borrower->{branchcode}
105 : $overdue->{branchcode};
107 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
108 if ( !exists $is_holiday{$branchcode} ) {
109 $is_holiday{$branchcode} = set_holiday( $branchcode, $today );
112 my $datedue = dt_from_string( $overdue->{date_due} );
113 if ( DateTime->compare( $datedue, $today ) == 1 ) {
118 my ( $amount, $type, $unitcounttotal ) =
119 CalcFine( $overdue, $borrower->{categorycode},
120 $branchcode, $datedue, $today );
123 # Don't update the fine if today is a holiday.
124 # This ensures that dropbox mode will remove the correct amount of fine.
125 if ( $mode eq 'production' && !$is_holiday{$branchcode} ) {
128 $overdue->{itemnumber},
129 $overdue->{borrowernumber},
130 $amount, $type, output_pref($datedue)
137 map { defined $borrower->{$_} ? $borrower->{$_} : q{} }
139 push @cells, map { $overdue->{$_} } @item_fields;
140 push @cells, $type, $unitcounttotal, $amount;
141 say {$fh} join $delim, @cells;
149 my $overdue_items = @{$overdues};
151 Fines assessment -- $today
154 say "Saved to $filename";
157 Number of Overdue Items:
158 counted $overdue_items
165 my ( $branch, $dt ) = @_;
167 my $calendar = Koha::Calendar->new( branchcode => $branch );
168 return $calendar->is_holiday($dt);
172 my $directory = shift;
174 $directory = File::Spec->tmpdir();
176 if ( !-d $directory ) {
177 carp "Could not write to $directory ... does not exist!";
179 my $name = C4::Context->config('database');
181 $name .= join q{}, q{_}, $today->ymd(), '.log';
182 $name = File::Spec->catfile( $directory, $name );
183 if ($verbose && $log) {
184 say "writing to $name";