Merge branch 'bug_9656' into 3.12-master
[koha.git] / misc / cronjobs / fines.pl
1 #!/usr/bin/perl
2
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
8 #
9 #  This script is meant to be run nightly out of cron.
10
11 # Copyright 2000-2002 Katipo Communications
12 # Copyright 2011 PTFS-Europe Ltd
13 #
14 # This file is part of Koha.
15 #
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
19 # version.
20 #
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.
24 #
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.
28
29 use strict;
30 use warnings;
31 use 5.010;
32
33 use C4::Context;
34 use C4::Overdues;
35 use Getopt::Long;
36 use Carp;
37 use File::Spec;
38
39 use Koha::Calendar;
40 use Koha::DateUtils;
41
42 my $help;
43 my $verbose;
44 my $output_dir;
45 my $log;
46
47 GetOptions(
48     'h|help'    => \$help,
49     'v|verbose' => \$verbose,
50     'l|log'     => \$log,
51     'o|out:s'   => \$output_dir,
52 );
53 my $usage = << 'ENDUSAGE';
54
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.
60
61 This script has the following parameters :
62     -h --help: this message
63     -l --log: log the output to a file (optional if the -o parameter is given)
64     -o --out:  ouput directory for logs (defaults to env or /tmp if !exist)
65     -v --verbose
66
67 ENDUSAGE
68
69 if ($help) {
70     print $usage;
71     exit;
72 }
73
74 my @borrower_fields =
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";
82
83 my %is_holiday;
84 my $today = DateTime->now( time_zone => C4::Context->tz() );
85 my $filename;
86 if ($log or $output_dir) {
87     $filename = get_filename($output_dir);
88 }
89
90 my $fh;
91 if ($filename) {
92     open $fh, '>>', $filename or croak "Cannot write file $filename: $!";
93     print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
94     print {$fh} "\n";
95 }
96 my $counted = 0;
97 my $overdues = Getoverdues();
98 for my $overdue ( @{$overdues} ) {
99     if ( !defined $overdue->{borrowernumber} ) {
100         carp
101 "ERROR in Getoverdues : issues.borrowernumber IS NULL.  Repair 'issues' table now!  Skipping record.\n";
102         next;
103     }
104     my $borrower = BorType( $overdue->{borrowernumber} );
105     my $branchcode =
106         ( $control eq 'ItemHomeLibrary' ) ? $overdue->{homebranch}
107       : ( $control eq 'PatronLibrary' )   ? $borrower->{branchcode}
108       :                                     $overdue->{branchcode};
109
110 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
111     if ( !exists $is_holiday{$branchcode} ) {
112         $is_holiday{$branchcode} = set_holiday( $branchcode, $today );
113     }
114
115     my $datedue = dt_from_string( $overdue->{date_due} );
116     if ( DateTime->compare( $datedue, $today ) == 1 ) {
117         next;    # not overdue
118     }
119     ++$counted;
120
121     my ( $amount, $type, $unitcounttotal ) =
122       CalcFine( $overdue, $borrower->{categorycode},
123         $branchcode, $datedue, $today );
124     $type ||= q{};
125
126     # Don't update the fine if today is a holiday.
127     # This ensures that dropbox mode will remove the correct amount of fine.
128     if ( $mode eq 'production' && !$is_holiday{$branchcode} ) {
129         if ( $amount > 0 ) {
130             UpdateFine(
131                 $overdue->{itemnumber},
132                 $overdue->{borrowernumber},
133                 $amount, $type, output_pref($datedue)
134             );
135         }
136     }
137     if ($filename) {
138         my @cells;
139         push @cells,
140           map { defined $borrower->{$_} ? $borrower->{$_} : q{} }
141           @borrower_fields;
142         push @cells, map { $overdue->{$_} } @item_fields;
143         push @cells, $type, $unitcounttotal, $amount;
144         say {$fh} join $delim, @cells;
145     }
146 }
147 if ($filename){
148     close $fh;
149 }
150
151 if ($verbose) {
152     my $overdue_items = @{$overdues};
153     print <<"EOM";
154 Fines assessment -- $today
155 EOM
156     if ($filename) {
157         say "Saved to $filename";
158     }
159     print <<"EOM";
160 Number of Overdue Items:
161      counted $overdue_items
162     reported $counted
163
164 EOM
165 }
166
167 sub set_holiday {
168     my ( $branch, $dt ) = @_;
169
170     my $calendar = Koha::Calendar->new( branchcode => $branch );
171     return $calendar->is_holiday($dt);
172 }
173
174 sub get_filename {
175     my $directory = shift;
176     if ( !$directory ) {
177         $directory = File::Spec->tmpdir();
178     }
179     if ( !-d $directory ) {
180         carp "Could not write to $directory ... does not exist!";
181     }
182     my $name = C4::Context->config('database');
183     $name =~ s/\W//;
184     $name .= join q{}, q{_}, $today->ymd(), '.log';
185     $name = File::Spec->catfile( $directory, $name );
186     if ($verbose && $log) {
187         say "writing to $name";
188     }
189     return $name;
190 }