Modified fines.pl to not throw warning if --out not specified
[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 #
13 # This file is part of Koha.
14 #
15 # Koha is free software; you can redistribute it and/or modify it under the
16 # terms of the GNU General Public License as published by the Free Software
17 # Foundation; either version 2 of the License, or (at your option) any later
18 # version.
19 #
20 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
21 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License along with
25 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26 # Suite 330, Boston, MA  02111-1307 USA
27
28
29 # FIXME: use FinesMode as described or change syspref description
30 use strict;
31
32 BEGIN {
33     # find Koha's Perl modules
34     # test carefully before changing this
35     use FindBin;
36     eval { require "$FindBin::Bin/kohalib.pl" };
37 }
38
39 use Date::Calc qw/Date_to_Days/;
40
41 use C4::Context;
42 use C4::Circulation;
43 use C4::Overdues;
44 use C4::Calendar qw();  # don't need any exports from Calendar
45 use C4::Biblio;
46 use C4::Debug;  # supplying $debug and $cgi_debug
47 use Getopt::Long;
48
49 my $help = 0;
50 my $verbose = 0;
51 my $output_dir;
52
53 GetOptions( 'h|help'    => \$help,
54             'v|verbose' => \$verbose,
55             'o|out:s'   => \$output_dir,
56        );
57 my $usage = << 'ENDUSAGE';
58
59 This script calculates and charges overdue fines
60 to patron accounts.  If the Koha System Preference
61 'finesMode' is set to 'production', the fines are charged
62 to the patron accounts.  If set to 'test', the fines are
63 calculated but not applied.
64
65 This script has the following parameters :
66     -h --help: this message
67     -o --out:  ouput directory for logs (defaults to env or /tmp if !exist)
68     -v --verbose
69
70 ENDUSAGE
71
72 die $usage if $help;
73
74 use vars qw(@borrower_fields @item_fields @other_fields);
75 use vars qw($fldir $libname $control $mode $delim $dbname $today $today_iso $today_days);
76 use vars qw($filename);
77
78 CHECK {
79     @borrower_fields = qw(cardnumber categorycode surname firstname email phone address citystate);
80         @item_fields = qw(itemnumber barcode date_due);
81        @other_fields = qw(type days_overdue fine);
82     $libname = C4::Context->preference('LibraryName');
83     $control = C4::Context->preference('CircControl');
84     $mode    = C4::Context->preference('finesMode');
85     $dbname  = C4::Context->config('database');
86     $delim   = "\t"; # ?  C4::Context->preference('delimiter') || "\t";
87
88 }
89
90 INIT {
91     $debug and print "Each line will contain the following fields:\n",
92         "From borrowers : ", join(', ', @borrower_fields), "\n",
93         "From items : ", join(', ', @item_fields), "\n",
94         "Per overdue: ", join(', ', @other_fields), "\n",
95         "Delimiter: '$delim'\n";
96 }
97
98 my $data = Getoverdues();
99 my $overdueItemsCounted = 0;
100 my %calendars = ();
101 $today = C4::Dates->new();
102 $today_iso = $today->output('iso');
103 $today_days = Date_to_Days(split(/-/,$today_iso));
104 if($output_dir){
105     $fldir = $output_dir if( -d $output_dir );
106 } else {
107     $fldir = $ENV{TMPDIR} || "/tmp";
108 }
109 if (!-d $fldir) {
110     warn "Could not write to $fldir ... does not exist!";
111 }
112 $filename = $dbname;
113 $filename =~ s/\W//;
114 $filename = $fldir . '/'. $filename . '_' .  $today_iso . ".log";
115 print "writing to $filename\n";
116 open (FILE, ">$filename") or die "Cannot write file $filename: $!";
117 print FILE join $delim, (@borrower_fields, @item_fields, @other_fields);
118 print FILE "\n";
119
120 for (my $i=0; $i<scalar(@$data); $i++) {
121     my $datedue = C4::Dates->new($data->[$i]->{'date_due'},'iso');
122     my $datedue_days = Date_to_Days(split(/-/,$datedue->output('iso')));
123     my $due_str = $datedue->output();
124     unless (defined $data->[$i]->{'borrowernumber'}) {
125         print STDERR "ERROR in Getoverdues line $i: issues.borrowernumber IS NULL.  Repair 'issues' table now!  Skipping record.\n";
126         next;   # Note: this doesn't solve everything.  After NULL borrowernumber, multiple issues w/ real borrowernumbers can pile up.
127     }
128     my $borrower = BorType($data->[$i]->{'borrowernumber'});
129     my $branchcode = ($control eq 'ItemHomeLibrary') ? $data->[$i]->{homebranch} :
130                      ($control eq 'PatronLibrary'  ) ?   $borrower->{branchcode} :
131                                                        $data->[$i]->{branchcode} ;
132     # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here).
133     my $calendar;
134     unless (defined ($calendars{$branchcode})) {
135         $calendars{$branchcode} = C4::Calendar->new(branchcode => $branchcode);
136     }
137     $calendar = $calendars{$branchcode};
138     my $isHoliday = $calendar->isHoliday(split '/', $today->output('metric'));
139       
140     ($datedue_days <= $today_days) or next; # or it's not overdue, right?
141
142     $overdueItemsCounted++;
143     my ($amount,$type,$daycounttotal,$daycount)=
144                 CalcFine($data->[$i], $borrower->{'categorycode'}, $branchcode,undef,undef, $datedue, $today);
145         # FIXME: $type NEVER gets populated by anything.
146     (defined $type) or $type = '';
147         # Don't update the fine if today is a holiday.  
148         # This ensures that dropbox mode will remove the correct amount of fine.
149         if ($mode eq 'production' and  ! $isHoliday) {
150                 UpdateFine($data->[$i]->{'itemnumber'},$data->[$i]->{'borrowernumber'},$amount,$type,$due_str) if( $amount > 0 ) ;
151         }
152     my @cells = ();
153     push @cells, map {$borrower->{$_}} @borrower_fields;
154     push @cells, map {$data->[$i]->{$_}} @item_fields;
155     push @cells, $type, $daycounttotal, $amount;
156     print FILE join($delim, @cells), "\n";
157 }
158
159 my $numOverdueItems = scalar(@$data);
160 if ($verbose) {
161    print <<EOM;
162 Fines assessment -- $today_iso -- Saved to $filename
163 Number of Overdue Items:
164      counted $overdueItemsCounted
165     reported $numOverdueItems
166
167 EOM
168 }
169
170 close FILE;