Bug 9978: Replace license header with the correct license (GPLv3+)
[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
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.
20 #
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.
25 #
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>.
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.  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").
59
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)
64     -v --verbose
65
66 ENDUSAGE
67
68 if ($help) {
69     print $usage;
70     exit;
71 }
72
73 my @borrower_fields =
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";
81
82 my %is_holiday;
83 my $today = DateTime->now( time_zone => C4::Context->tz() );
84 my $filename;
85 if ($log or $output_dir) {
86     $filename = get_filename($output_dir);
87 }
88
89 my $fh;
90 if ($filename) {
91     open $fh, '>>', $filename or croak "Cannot write file $filename: $!";
92     print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
93     print {$fh} "\n";
94 }
95 my $counted = 0;
96 my $overdues = Getoverdues();
97 for my $overdue ( @{$overdues} ) {
98     next if $overdue->{itemlost};
99
100     if ( !defined $overdue->{borrowernumber} ) {
101         carp
102 "ERROR in Getoverdues : issues.borrowernumber IS NULL.  Repair 'issues' table now!  Skipping record.\n";
103         next;
104     }
105     my $borrower = BorType( $overdue->{borrowernumber} );
106     my $branchcode =
107         ( $control eq 'ItemHomeLibrary' ) ? $overdue->{homebranch}
108       : ( $control eq 'PatronLibrary' )   ? $borrower->{branchcode}
109       :                                     $overdue->{branchcode};
110
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 );
114     }
115
116     my $datedue = dt_from_string( $overdue->{date_due} );
117     if ( DateTime->compare( $datedue, $today ) == 1 ) {
118         next;    # not overdue
119     }
120     ++$counted;
121
122     my ( $amount, $type, $unitcounttotal ) =
123       CalcFine( $overdue, $borrower->{categorycode},
124         $branchcode, $datedue, $today );
125     $type ||= q{};
126
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} ) {
130         if ( $amount > 0 ) {
131             UpdateFine(
132                 $overdue->{itemnumber},
133                 $overdue->{borrowernumber},
134                 $amount, $type, output_pref($datedue)
135             );
136         }
137     }
138     if ($filename) {
139         my @cells;
140         push @cells,
141           map { defined $borrower->{$_} ? $borrower->{$_} : q{} }
142           @borrower_fields;
143         push @cells, map { $overdue->{$_} } @item_fields;
144         push @cells, $type, $unitcounttotal, $amount;
145         say {$fh} join $delim, @cells;
146     }
147 }
148 if ($filename){
149     close $fh;
150 }
151
152 if ($verbose) {
153     my $overdue_items = @{$overdues};
154     print <<"EOM";
155 Fines assessment -- $today
156 EOM
157     if ($filename) {
158         say "Saved to $filename";
159     }
160     print <<"EOM";
161 Number of Overdue Items:
162      counted $overdue_items
163     reported $counted
164
165 EOM
166 }
167
168 sub set_holiday {
169     my ( $branch, $dt ) = @_;
170
171     my $calendar = Koha::Calendar->new( branchcode => $branch );
172     return $calendar->is_holiday($dt);
173 }
174
175 sub get_filename {
176     my $directory = shift;
177     if ( !$directory ) {
178         $directory = File::Spec->tmpdir();
179     }
180     if ( !-d $directory ) {
181         carp "Could not write to $directory ... does not exist!";
182     }
183     my $name = C4::Context->config('database');
184     $name =~ s/\W//;
185     $name .= join q{}, q{_}, $today->ymd(), '.log';
186     $name = File::Spec->catfile( $directory, $name );
187     if ($verbose && $log) {
188         say "writing to $name";
189     }
190     return $name;
191 }