Merge remote-tracking branch 'origin/new/bug_7284'
[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
46 GetOptions(
47     'h|help'    => \$help,
48     'v|verbose' => \$verbose,
49     'o|out:s'   => \$output_dir,
50 );
51 my $usage = << 'ENDUSAGE';
52
53 This script calculates and charges overdue fines
54 to patron accounts.  If the Koha System Preference
55 'finesMode' is set to 'production', the fines are charged
56 to the patron accounts.  If set to 'test', the fines are
57 calculated but not applied.
58
59 This script has the following parameters :
60     -h --help: this message
61     -o --out:  ouput directory for logs (defaults to env or /tmp if !exist)
62     -v --verbose
63
64 ENDUSAGE
65
66 if ($help) {
67     print $usage;
68     exit;
69 }
70
71 my @borrower_fields =
72   qw(cardnumber categorycode surname firstname email phone address citystate);
73 my @item_fields  = qw(itemnumber barcode date_due);
74 my @other_fields = qw(type days_overdue fine);
75 my $libname      = C4::Context->preference('LibraryName');
76 my $control      = C4::Context->preference('CircControl');
77 my $mode         = C4::Context->preference('finesMode');
78 my $delim = "\t";    # ?  C4::Context->preference('delimiter') || "\t";
79
80 my %is_holiday;
81 my $today = DateTime->now( time_zone => C4::Context->tz() );
82 my $filename = get_filename($output_dir);
83
84 open my $fh, '>>', $filename or croak "Cannot write file $filename: $!";
85 print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
86 print {$fh} "\n";
87
88 my $counted  = 0;
89 my $overdues = Getoverdues();
90 for my $overdue ( @{$overdues} ) {
91     if ( !defined $overdue->{borrowernumber} ) {
92         carp
93 "ERROR in Getoverdues : issues.borrowernumber IS NULL.  Repair 'issues' table now!  Skipping record.\n";
94         next;
95     }
96     my $borrower = BorType( $overdue->{borrowernumber} );
97     my $branchcode =
98         ( $control eq 'ItemHomeLibrary' ) ? $overdue->{homebranch}
99       : ( $control eq 'PatronLibrary' )   ? $borrower->{branchcode}
100       :                                     $overdue->{branchcode};
101
102 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
103     if ( !exists $is_holiday{$branchcode} ) {
104         $is_holiday{$branchcode} = set_holiday( $branchcode, $today );
105     }
106
107     my $datedue = dt_from_string( $overdue->{date_due} );
108     if ( DateTime->compare( $datedue, $today ) == 1 ) {
109         next;    # not overdue
110     }
111     ++$counted;
112
113     my ( $amount, $type, $daycounttotal ) =
114       CalcFine( $overdue, $borrower->{categorycode},
115         $branchcode, $datedue, $today );
116
117     $type ||= q{};
118
119     # Don't update the fine if today is a holiday.
120     # This ensures that dropbox mode will remove the correct amount of fine.
121     if ( $mode eq 'production' && !$is_holiday{$branchcode} ) {
122         if ( $amount > 0 ) {
123             UpdateFine(
124                 $overdue->{itemnumber},
125                 $overdue->{borrowernumber},
126                 $amount, $type, output_pref($datedue)
127             );
128         }
129     }
130     my @cells;
131     push @cells,
132       map { defined $borrower->{$_} ? $borrower->{$_} : q{} } @borrower_fields;
133     push @cells, map { $overdue->{$_} } @item_fields;
134     push @cells, $type, $daycounttotal, $amount;
135     say {$fh} join $delim, @cells;
136 }
137 close $fh;
138
139 if ($verbose) {
140     my $overdue_items = @{$overdues};
141     print <<'EOM';
142 Fines assessment -- $today->ymd() -- Saved to $filename
143 Number of Overdue Items:
144      counted $overdue_items
145     reported $counted
146
147 EOM
148 }
149
150 sub set_holiday {
151     my ( $branch, $dt ) = @_;
152
153     my $calendar = Koha::Calendar->new( branchcode => $branch );
154     return $calendar->is_holiday($dt);
155 }
156
157 sub get_filename {
158     my $directory = shift;
159     if ( !$directory ) {
160         $directory = File::Spec->tmpdir();
161     }
162     if ( !-d $directory ) {
163         carp "Could not write to $directory ... does not exist!";
164     }
165     my $name = C4::Context->config('database');
166     $name =~ s/\W//;
167     $name .= join q{}, q{_}, $today->ymd(), '.log';
168     $name = File::Spec->catfile( $directory, $name );
169     if ($verbose) {
170         say "writing to $name";
171     }
172     return $name;
173 }