View | Details | Raw Unified | Return to bug 6858
Collapse All | Expand All

(-)a/misc/cronjobs/staticfines.pl (-44 / +43 lines)
Lines 8-14 Link Here
8
#
8
#
9
#  This script is meant to be run nightly out of cron.
9
#  This script is meant to be run nightly out of cron.
10
10
11
# Copyright 2000-2002 Katipo Communications
11
# Copyright 2011-2012 BibLibre
12
#
12
#
13
# This file is part of Koha.
13
# This file is part of Koha.
14
#
14
#
Lines 25-34 Link Here
25
# with Koha; if not, write to the Free Software Foundation, Inc.,
25
# with Koha; if not, write to the Free Software Foundation, Inc.,
26
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
27
28
# FIXME: use FinesMode as described or change syspref description
28
use Modern::Perl;
29
use strict;
30
31
#use warnings; FIXME - Bug 2505
32
29
33
BEGIN {
30
BEGIN {
34
31
Lines 51-63 use List::MoreUtils qw/none/; Link Here
51
48
52
my $help    = 0;
49
my $help    = 0;
53
my $verbose = 0;
50
my $verbose = 0;
54
my $output_dir;
55
my @pcategories;
51
my @pcategories;
56
my @categories;
52
my @categories;
57
my %catamounts;
53
my %catamounts;
58
my @libraries;
54
my @libraries;
59
my $delay;
55
my $delay;
60
my $useborrowerlibrary;
56
my $useborrowerlibrary;
57
my $borrowernumberlimit;
61
my $borrowersalreadyapplied; # hashref of borrowers for whom we already applied the fine, so it's only applied once
58
my $borrowersalreadyapplied; # hashref of borrowers for whom we already applied the fine, so it's only applied once
62
my $debug = $ENV{'DEBUG'} || 0;
59
my $debug = $ENV{'DEBUG'} || 0;
63
my $bigdebug = 0;
60
my $bigdebug = 0;
Lines 65-75 my $bigdebug = 0; Link Here
65
GetOptions(
62
GetOptions(
66
    'h|help'      => \$help,
63
    'h|help'      => \$help,
67
    'v|verbose'   => \$verbose,
64
    'v|verbose'   => \$verbose,
68
    'o|out:s'     => \$output_dir,
69
    'c|category:s'=> \@pcategories,
65
    'c|category:s'=> \@pcategories,
70
    'l|library:s' => \@libraries,
66
    'l|library:s' => \@libraries,
71
    'd|delay:i'   => \$delay,
67
    'd|delay:i'   => \$delay,
72
    'u|use-borrower-library' => \$useborrowerlibrary
68
    'u|use-borrower-library' => \$useborrowerlibrary,
69
    'b|borrower:i' => \$borrowernumberlimit
73
);
70
);
74
my $usage = << 'ENDUSAGE';
71
my $usage = << 'ENDUSAGE';
75
72
Lines 82-97 Please note that the fines won't be applied on a holiday. Link Here
82
79
83
This script has the following parameters :
80
This script has the following parameters :
84
    -h --help: this message
81
    -h --help: this message
85
    -o --out:  ouput directory for logs (defaults to env or /tmp if !exist)
86
    -v --verbose
82
    -v --verbose
87
    -c --category borrower_category,amount (repeatable)
83
    -c --category borrower_category,amount (repeatable)
88
    -l --library (repeatable)
84
    -l --library (repeatable)
89
    -d --delay
85
    -d --delay
90
    -u --use-borrower-library: use borrower's library, regardless of the CircControl syspref
86
    -u --use-borrower-library: use borrower's library, regardless of the CircControl syspref
87
    -b --borrower borrowernumber: only for one given borrower
91
88
92
ENDUSAGE
89
ENDUSAGE
93
die $usage if $help;
90
die $usage if $help;
94
91
92
my $dbh = C4::Context->dbh;
93
95
# Processing categories
94
# Processing categories
96
foreach (@pcategories) {
95
foreach (@pcategories) {
97
    my ($category, $amount) = split(',', $_);
96
    my ($category, $amount) = split(',', $_);
Lines 122-163 INIT { Link Here
122
      "Per overdue: ",     join( ', ', @other_fields ),    "\n",
121
      "Per overdue: ",     join( ', ', @other_fields ),    "\n",
123
      "Delimiter: '$delim'\n";
122
      "Delimiter: '$delim'\n";
124
}
123
}
125
124
$debug and (defined $borrowernumberlimit) and print "--borrower limitation: borrower $borrowernumberlimit\n";
126
my $data                = Getoverdues();
125
my $data = (defined $borrowernumberlimit) ? checkoverdues($borrowernumberlimit) : Getoverdues();
127
my $overdueItemsCounted = 0;
126
my $overdueItemsCounted = 0;
128
my %calendars           = ();
127
my %calendars           = ();
129
$today      = C4::Dates->new();
128
$today      = C4::Dates->new();
130
$today_iso  = $today->output('iso');
129
$today_iso  = $today->output('iso');
131
$today_days = Date_to_Days( split( /-/, $today_iso ) );
130
$today_days = Date_to_Days( split( /-/, $today_iso ) );
132
131
133
134
135
if ($output_dir) {
136
    $fldir = $output_dir if ( -d $output_dir );
137
} else {
138
    $fldir = $ENV{TMPDIR} || "/tmp";
139
}
140
if ( !-d $fldir ) {
141
    warn "Could not write to $fldir ... does not exist!";
142
}
143
$filename = $dbname;
144
$filename =~ s/\W//;
145
$filename = $fldir . '/' . $filename . '_' . $today_iso . ".log";
146
print "writing to $filename\n" if $verbose;
147
open my $FILE, ">", $filename or die "Cannot write file $filename: $!";
148
print $FILE join $delim, ( @borrower_fields, @item_fields, @other_fields );
149
print $FILE "\n";
150
151
for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) {
132
for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) {
152
    my $datedue;
133
    my $datedue;
153
    my $datedue_days;
134
    my $datedue_days;
154
    eval {
135
    eval {
155
        $datedue = C4::Dates->new( $data->[$i]->{'date_due'}, 'iso' );
136
	$datedue = C4::Dates->new( $data->[$i]->{'date_due'}, 'iso' );
156
        $datedue_days = Date_to_Days( split( /-/, $datedue->output('iso') ) );
137
	$datedue_days = Date_to_Days( split( /-/, $datedue->output('iso') ) );
157
    };
138
    };
158
    if ($@) {
139
    if ($@) {
159
        warn "Error on date for borrower " . $data->[$i]->{'borrowernumber'} .  ": $@date_due: " . $data->[$i]->{'date_due'} . "\ndatedue_days: " . $datedue_days . "\nSkipping";
140
	warn "Error on date for borrower " . $data->[$i]->{'borrowernumber'} .  ": $@date_due: " . $data->[$i]->{'date_due'} . "\ndatedue_days: " . $datedue_days . "\nSkipping";
160
        next;
141
	next;
161
    }
142
    }
162
    my $due_str = $datedue->output();
143
    my $due_str = $datedue->output();
163
    unless ( defined $data->[$i]->{'borrowernumber'} ) {
144
    unless ( defined $data->[$i]->{'borrowernumber'} ) {
Lines 200-209 for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) { Link Here
200
    # Reassign fine's amount if specified in command-line
181
    # Reassign fine's amount if specified in command-line
201
    $amount = $catamounts{$borrower->{'categorycode'}} if (defined $catamounts{$borrower->{'categorycode'}});
182
    $amount = $catamounts{$borrower->{'categorycode'}} if (defined $catamounts{$borrower->{'categorycode'}});
202
183
184
    # We check if there is already a fine for the given borrower
185
    my $fine = GetFine($data->[$i]->{'borrowernumber'});
186
    if ($fine > 0) {
187
        $debug and warn "There is already a fine for borrower " . $data->[$i]->{'borrowernumber'} . ". Nothing to do here. Skipping this borrower";
188
        next;
189
    }
190
203
    # FIXME: $type NEVER gets populated by anything.
191
    # FIXME: $type NEVER gets populated by anything.
204
    ( defined $type ) or $type = '';
192
    ( defined $type ) or $type = '';
205
193
206
207
    # Don't update the fine if today is a holiday.
194
    # Don't update the fine if today is a holiday.
208
    # This ensures that dropbox mode will remove the correct amount of fine.
195
    # This ensures that dropbox mode will remove the correct amount of fine.
209
    if ( $mode eq 'production' and !$borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}}) {
196
    if ( $mode eq 'production' and !$borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}}) {
Lines 211-232 for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) { Link Here
211
        if($isHoliday) {
198
        if($isHoliday) {
212
            $debug and warn "Today is a holiday. The fine for borrower " . $data->[$i]->{'borrowernumber'} . " will not be applied";
199
            $debug and warn "Today is a holiday. The fine for borrower " . $data->[$i]->{'borrowernumber'} . " will not be applied";
213
        } else {
200
        } else {
214
            $debug and warn "Updating fine for borrower " . $data->[$i]->{'borrowernumber'} . " with amount : $amount";
201
            $debug and warn "Creating fine for borrower " . $data->[$i]->{'borrowernumber'} . " with amount : $amount";
215
            UpdateFine( $data->[$i]->{'itemnumber'}, $data->[$i]->{'borrowernumber'}, $amount, $type, $due_str ) if ( $amount > 0 );
202
203
            # We mark this borrower as already processed
216
            $borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}} = 1;
204
            $borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}} = 1;
205
206
            my $borrowernumber = $data->[$i]->{'borrowernumber'};
207
            my $itemnumber     = $data->[$i]->{'itemnumber'};
208
209
            # And we create the fine
210
            my $sth4 = $dbh->prepare( "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?" );
211
            $sth4->execute($itemnumber);
212
            my $title = $sth4->fetchrow;
213
214
            my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
215
            my $desc        = "staticfine";
216
            my $query       = "INSERT INTO accountlines
217
                        (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
218
                                VALUES (?,?,now(),?,?,'F',?,?,?)";
219
            my $sth2 = $dbh->prepare($query);
220
            $bigdebug and warn "query: $query\nw/ args: $borrowernumber, $itemnumber, $amount, $desc, $amount, $amount, $nextaccntno\n";
221
            $sth2->execute( $borrowernumber, $itemnumber, $amount, $desc, $amount, $amount, $nextaccntno );
222
217
        }
223
        }
218
    }
224
    }
219
    my @cells = ();
220
    push @cells, map { $borrower->{$_} } @borrower_fields;
221
    push @cells, map { $data->[$i]->{$_} } @item_fields;
222
    push @cells, $type, $daycounttotal, $amount;
223
    print FILE join( $delim, @cells ), "\n";
224
}
225
}
225
226
226
my $numOverdueItems = scalar(@$data);
227
my $numOverdueItems = scalar(@$data);
227
if ($verbose) {
228
if ($verbose) {
228
    print <<EOM;
229
    print <<EOM;
229
Fines assessment -- $today_iso -- Saved to $filename
230
Fines assessment -- $today_iso
230
Number of Overdue Items:
231
Number of Overdue Items:
231
     counted $overdueItemsCounted
232
     counted $overdueItemsCounted
232
    reported $numOverdueItems
233
    reported $numOverdueItems
Lines 234-237 Number of Overdue Items: Link Here
234
EOM
235
EOM
235
}
236
}
236
237
237
close FILE;
238
- 

Return to bug 6858