Bug 21068: Remove NorwegianPatronDB related code
[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 # Doesn't 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 use C4::Log;
42
43 my $help;
44 my $verbose;
45 my $output_dir;
46 my $log;
47
48 GetOptions(
49     'h|help'    => \$help,
50     'v|verbose' => \$verbose,
51     'l|log'     => \$log,
52     'o|out:s'   => \$output_dir,
53 );
54 my $usage = << 'ENDUSAGE';
55
56 This script calculates and charges overdue fines
57 to patron accounts.  The Koha system preference 'finesMode' controls
58 whether the fines are calculated and charged to the patron accounts ("Calculate and charge");
59 calculated and emailed to the admin but not applied ("Calculate (but only for mailing to the admin)"); or not calculated ("Don't calculate").
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 cronlogaction();
75
76 my @borrower_fields =
77   qw(cardnumber categorycode surname firstname email phone address citystate);
78 my @item_fields  = qw(itemnumber barcode date_due);
79 my @other_fields = qw(type days_overdue fine);
80 my $libname      = C4::Context->preference('LibraryName');
81 my $control      = C4::Context->preference('CircControl');
82 my $mode         = C4::Context->preference('finesMode');
83 my $delim = "\t";    # ?  C4::Context->preference('delimiter') || "\t";
84
85 my %is_holiday;
86 my $today = DateTime->now( time_zone => C4::Context->tz() );
87 my $filename;
88 if ($log or $output_dir) {
89     $filename = get_filename($output_dir);
90 }
91
92 my $fh;
93 if ($filename) {
94     open $fh, '>>', $filename or croak "Cannot write file $filename: $!";
95     print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
96     print {$fh} "\n";
97 }
98 my $counted = 0;
99 my $overdues = Getoverdues();
100 for my $overdue ( @{$overdues} ) {
101     next if $overdue->{itemlost};
102
103     if ( !defined $overdue->{borrowernumber} ) {
104         carp
105 "ERROR in Getoverdues : issues.borrowernumber IS NULL.  Repair 'issues' table now!  Skipping record.\n";
106         next;
107     }
108     my $borrower = BorType( $overdue->{borrowernumber} );
109     my $branchcode =
110         ( $control eq 'ItemHomeLibrary' ) ? $overdue->{homebranch}
111       : ( $control eq 'PatronLibrary' )   ? $borrower->{branchcode}
112       :                                     $overdue->{branchcode};
113
114 # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
115     if ( !exists $is_holiday{$branchcode} ) {
116         $is_holiday{$branchcode} = set_holiday( $branchcode, $today );
117     }
118
119     my $datedue = dt_from_string( $overdue->{date_due} );
120     if ( DateTime->compare( $datedue, $today ) == 1 ) {
121         next;    # not overdue
122     }
123     ++$counted;
124
125     my ( $amount, $type, $unitcounttotal ) =
126       CalcFine( $overdue, $borrower->{categorycode},
127         $branchcode, $datedue, $today );
128     $type ||= q{};
129
130     # Don't update the fine if today is a holiday.
131     # This ensures that dropbox mode will remove the correct amount of fine.
132     if ( $mode eq 'production' && !$is_holiday{$branchcode} ) {
133         if ( $amount && $amount > 0 ) {
134             UpdateFine(
135                 {
136                     issue_id       => $overdue->{issue_id},
137                     itemnumber     => $overdue->{itemnumber},
138                     borrowernumber => $overdue->{borrowernumber},
139                     amount         => $amount,
140                     type           => $type,
141                     due            => output_pref($datedue),
142                 }
143             );
144         }
145     }
146     if ($filename) {
147         my @cells;
148         push @cells,
149           map { defined $borrower->{$_} ? $borrower->{$_} : q{} }
150           @borrower_fields;
151         push @cells, map { $overdue->{$_} } @item_fields;
152         push @cells, $type, $unitcounttotal, $amount;
153         say {$fh} join $delim, @cells;
154     }
155 }
156 if ($filename){
157     close $fh;
158 }
159
160 if ($verbose) {
161     my $overdue_items = @{$overdues};
162     print <<"EOM";
163 Fines assessment -- $today
164 EOM
165     if ($filename) {
166         say "Saved to $filename";
167     }
168     print <<"EOM";
169 Number of Overdue Items:
170      counted $overdue_items
171     reported $counted
172
173 EOM
174 }
175
176 sub set_holiday {
177     my ( $branch, $dt ) = @_;
178
179     my $calendar = Koha::Calendar->new( branchcode => $branch );
180     return $calendar->is_holiday($dt);
181 }
182
183 sub get_filename {
184     my $directory = shift;
185     if ( !$directory ) {
186         $directory = C4::Context::temporary_directory;
187     }
188     if ( !-d $directory ) {
189         carp "Could not write to $directory ... does not exist!";
190     }
191     my $name = C4::Context->config('database');
192     $name =~ s/\W//;
193     $name .= join q{}, q{_}, $today->ymd(), '.log';
194     $name = File::Spec->catfile( $directory, $name );
195     if ($verbose && $log) {
196         say "writing to $name";
197     }
198     return $name;
199 }