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
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>.
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. The Koha system preference 'finesMode' controls
57 whether the fines are calculated and charged to the patron accounts ("Calculate and charge");
58 calculated and emailed to the admin but not applied ("Calculate (but only for mailing to the admin)"); or not calculated ("Don't calculate").
60 This script has the following parameters :
61 -h --help: this message
62 -l --log: log the output to a file (optional if the -o parameter is given)
63 -o --out: ouput directory for logs (defaults to env or /tmp if !exist)
74 qw(cardnumber categorycode surname firstname email phone address citystate);
75 my @item_fields = qw(itemnumber barcode date_due);
76 my @other_fields = qw(type days_overdue fine);
77 my $libname = C4::Context->preference('LibraryName');
78 my $control = C4::Context->preference('CircControl');
79 my $mode = C4::Context->preference('finesMode');
80 my $delim = "\t"; # ? C4::Context->preference('delimiter') || "\t";
83 my $today = DateTime->now( time_zone => C4::Context->tz() );
85 if ($log or $output_dir) {
86 $filename = get_filename($output_dir);
91 open $fh, '>>', $filename or croak "Cannot write file $filename: $!";
92 print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
96 my $overdues = Getoverdues();
97 for my $overdue ( @{$overdues} ) {
98 next if $overdue->{itemlost};
100 if ( !defined $overdue->{borrowernumber} ) {
102 "ERROR in Getoverdues : issues.borrowernumber IS NULL. Repair 'issues' table now! Skipping record.\n";
105 my $borrower = BorType( $overdue->{borrowernumber} );
107 ( $control eq 'ItemHomeLibrary' ) ? $overdue->{homebranch}
108 : ( $control eq 'PatronLibrary' ) ? $borrower->{branchcode}
109 : $overdue->{branchcode};
111 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
112 if ( !exists $is_holiday{$branchcode} ) {
113 $is_holiday{$branchcode} = set_holiday( $branchcode, $today );
116 my $datedue = dt_from_string( $overdue->{date_due} );
117 if ( DateTime->compare( $datedue, $today ) == 1 ) {
122 my ( $amount, $type, $unitcounttotal ) =
123 CalcFine( $overdue, $borrower->{categorycode},
124 $branchcode, $datedue, $today );
127 # Don't update the fine if today is a holiday.
128 # This ensures that dropbox mode will remove the correct amount of fine.
129 if ( $mode eq 'production' && !$is_holiday{$branchcode} ) {
132 $overdue->{itemnumber},
133 $overdue->{borrowernumber},
134 $amount, $type, output_pref($datedue)
141 map { defined $borrower->{$_} ? $borrower->{$_} : q{} }
143 push @cells, map { $overdue->{$_} } @item_fields;
144 push @cells, $type, $unitcounttotal, $amount;
145 say {$fh} join $delim, @cells;
153 my $overdue_items = @{$overdues};
155 Fines assessment -- $today
158 say "Saved to $filename";
161 Number of Overdue Items:
162 counted $overdue_items
169 my ( $branch, $dt ) = @_;
171 my $calendar = Koha::Calendar->new( branchcode => $branch );
172 return $calendar->is_holiday($dt);
176 my $directory = shift;
178 $directory = File::Spec->tmpdir();
180 if ( !-d $directory ) {
181 carp "Could not write to $directory ... does not exist!";
183 my $name = C4::Context->config('database');
185 $name .= join q{}, q{_}, $today->ymd(), '.log';
186 $name = File::Spec->catfile( $directory, $name );
187 if ($verbose && $log) {
188 say "writing to $name";