X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=misc%2Fcronjobs%2Fstaticfines.pl;h=0966cb6134ace3958e9be1d866c5c10b494f6e44;hb=41a8005d1013815d312089c00e9e5464768e1332;hp=5d6e25cc8b486bf27691fb96fdf799c4f9ed0da3;hpb=15c8a453fc938410c66e3c0074f55da22395c560;p=srvgit diff --git a/misc/cronjobs/staticfines.pl b/misc/cronjobs/staticfines.pl index 5d6e25cc8b..0966cb6134 100755 --- a/misc/cronjobs/staticfines.pl +++ b/misc/cronjobs/staticfines.pl @@ -3,32 +3,29 @@ # This script loops through each overdue item, determines the fine, # and updates the total amount of fines due by each user. It relies on # the existence of /tmp/fines, which is created by ??? -# Doesnt really rely on it, it relys on being able to write to /tmp/ +# Doesn't really rely on it, it relies on being able to write to /tmp/ # It creates the fines file # # This script is meant to be run nightly out of cron. -# Copyright 2000-2002 Katipo Communications +# Copyright 2011-2012 BibLibre # # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with Koha; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . -# FIXME: use FinesMode as described or change syspref description -use strict; - -#use warnings; FIXME - Bug 2505 +use Modern::Perl; BEGIN { @@ -40,58 +37,64 @@ BEGIN { use Date::Calc qw/Date_to_Days/; +use Koha::Script -cron; use C4::Context; use C4::Circulation; use C4::Overdues; use C4::Calendar qw(); # don't need any exports from Calendar use C4::Biblio; -use C4::Debug; # supplying $debug and $cgi_debug +use C4::Log; use Getopt::Long; use List::MoreUtils qw/none/; +use Koha::DateUtils; +use Koha::Patrons; my $help = 0; my $verbose = 0; -my $output_dir; my @pcategories; my @categories; my %catamounts; my @libraries; my $delay; my $useborrowerlibrary; +my $borrowernumberlimit; my $borrowersalreadyapplied; # hashref of borrowers for whom we already applied the fine, so it's only applied once -my $debug = $ENV{'DEBUG'} || 0; +my $debug = 0; my $bigdebug = 0; GetOptions( 'h|help' => \$help, 'v|verbose' => \$verbose, - 'o|out:s' => \$output_dir, 'c|category:s'=> \@pcategories, 'l|library:s' => \@libraries, 'd|delay:i' => \$delay, - 'u|use-borrower-library' => \$useborrowerlibrary + 'u|use-borrower-library' => \$useborrowerlibrary, + 'b|borrower:i' => \$borrowernumberlimit ); my $usage = << 'ENDUSAGE'; This script calculates and charges overdue fines to patron accounts. If the Koha System Preference 'finesMode' is set to 'production', the fines are charged to the patron accounts. -If set to 'test', the fines are calculated but not applied. Please note that the fines won't be applied on a holiday. This script has the following parameters : -h --help: this message - -o --out: ouput directory for logs (defaults to env or /tmp if !exist) -v --verbose -c --category borrower_category,amount (repeatable) -l --library (repeatable) -d --delay -u --use-borrower-library: use borrower's library, regardless of the CircControl syspref + -b --borrower borrowernumber: only for one given borrower ENDUSAGE die $usage if $help; +cronlogaction(); + +my $dbh = C4::Context->dbh; + # Processing categories foreach (@pcategories) { my ($category, $amount) = split(',', $_); @@ -122,58 +125,48 @@ INIT { "Per overdue: ", join( ', ', @other_fields ), "\n", "Delimiter: '$delim'\n"; } - -my $data = Getoverdues(); -my $overdueItemsCounted = 0; -my %calendars = (); -$today = C4::Dates->new(); -$today_iso = $today->output('iso'); -$today_days = Date_to_Days( split( /-/, $today_iso ) ); - - - -if ($output_dir) { - $fldir = $output_dir if ( -d $output_dir ); +$debug and (defined $borrowernumberlimit) and print "--borrower limitation: borrower $borrowernumberlimit\n"; +my ($numOverdueItems, $data); +if (defined $borrowernumberlimit) { + ($numOverdueItems, $data) = checkoverdues($borrowernumberlimit); } else { - $fldir = $ENV{TMPDIR} || "/tmp"; -} -if ( !-d $fldir ) { - warn "Could not write to $fldir ... does not exist!"; + $data = Getoverdues(); + $numOverdueItems = scalar @$data; } -$filename = $dbname; -$filename =~ s/\W//; -$filename = $fldir . '/' . $filename . '_' . $today_iso . ".log"; -print "writing to $filename\n" if $verbose; -open my $FILE, ">", $filename or die "Cannot write file $filename: $!"; -print $FILE join $delim, ( @borrower_fields, @item_fields, @other_fields ); -print $FILE "\n"; +my $overdueItemsCounted = 0; +my %calendars = (); +$today = dt_from_string; +$today_iso = output_pref( { dt => $today, dateonly => 1, dateformat => 'iso' } ); +my ($tyear, $tmonth, $tday) = split( /-/, $today_iso ); +$today_days = Date_to_Days( $tyear, $tmonth, $tday ); for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) { - my $datedue; - my $datedue_days; + next if $data->[$i]->{'itemlost'}; + my ( $datedue, $datedue_days ); eval { - $datedue = C4::Dates->new( $data->[$i]->{'date_due'}, 'iso' ); - $datedue_days = Date_to_Days( split( /-/, $datedue->output('iso') ) ); + $datedue = dt_from_string( $data->[$i]->{'date_due'} ); + my $datedue_iso = output_pref( { dt => $datedue, dateonly => 1, dateformat => 'iso' } ); + $datedue_days = Date_to_Days( split( /-/, $datedue_iso ) ); }; if ($@) { - warn "Error on date for borrower " . $data->[$i]->{'borrowernumber'} . ": $@date_due: " . $data->[$i]->{'date_due'} . "\ndatedue_days: " . $datedue_days . "\nSkipping"; - next; + warn "Error on date for borrower " . $data->[$i]->{'borrowernumber'} . ": $@date_due: " . $data->[$i]->{'date_due'} . "\ndatedue_days: " . $datedue_days . "\nSkipping"; + next; } - my $due_str = $datedue->output(); + my $due_str = output_pref( { dt => $datedue, dateonly => 1 } ); unless ( defined $data->[$i]->{'borrowernumber'} ) { print STDERR "ERROR in Getoverdues line $i: issues.borrowernumber IS NULL. Repair 'issues' table now! Skipping record.\n"; next; # Note: this doesn't solve everything. After NULL borrowernumber, multiple issues w/ real borrowernumbers can pile up. } - my $borrower = BorType( $data->[$i]->{'borrowernumber'} ); + my $patron = Koha::Patrons->find( $data->[$i]->{'borrowernumber'} ); # Skipping borrowers that are not in @categories - $bigdebug and warn "Skipping borrower from category " . $borrower->{categorycode} if none { $borrower->{categorycode} eq $_ } @categories; - next if none { $borrower->{categorycode} eq $_ } @categories; + $bigdebug and warn "Skipping borrower from category " . $patron->categorycode if none { $patron->categorycode eq $_ } @categories; + next if none { $patron->categorycode eq $_ } @categories; my $branchcode = - ( $useborrowerlibrary ) ? $borrower->{branchcode} + ( $useborrowerlibrary ) ? $patron->branchcode : ( $control eq 'ItemHomeLibrary' ) ? $data->[$i]->{homebranch} - : ( $control eq 'PatronLibrary' ) ? $borrower->{branchcode} + : ( $control eq 'PatronLibrary' ) ? $patron->branchcode : $data->[$i]->{branchcode}; # In final case, CircControl must be PickupLibrary. (branchcode comes from issues table here). @@ -186,7 +179,7 @@ for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) { $calendars{$branchcode} = C4::Calendar->new( branchcode => $branchcode ); } $calendar = $calendars{$branchcode}; - my $isHoliday = $calendar->isHoliday( split '/', $today->output('metric') ); + my $isHoliday = $calendar->isHoliday( $tday, $tmonth, $tyear ); # Reassing datedue_days if -delay specified in commandline $bigdebug and warn "Using commandline supplied delay : $delay" if ($delay); @@ -195,14 +188,23 @@ for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) { ( $datedue_days <= $today_days ) or next; # or it's not overdue, right? $overdueItemsCounted++; - my ( $amount, $type, $daycounttotal, $daycount ) = CalcFine( $data->[$i], $borrower->{'categorycode'}, $branchcode, undef, undef, $datedue, $today ); + my ( $amount, $unitcounttotal, $unitcount ) = CalcFine( + $data->[$i], + $patron->categorycode, + $branchcode, + $datedue, + $today, + ); # Reassign fine's amount if specified in command-line - $amount = $catamounts{$borrower->{'categorycode'}} if (defined $catamounts{$borrower->{'categorycode'}}); - - # FIXME: $type NEVER gets populated by anything. - ( defined $type ) or $type = ''; + $amount = $catamounts{$patron->categorycode} if (defined $catamounts{$patron->categorycode}); + # We check if there is already a fine for the given borrower + my $fine = GetFine(undef, $data->[$i]->{'borrowernumber'}); + if ($fine > 0) { + $debug and warn "There is already a fine for borrower " . $data->[$i]->{'borrowernumber'} . ". Nothing to do here. Skipping this borrower"; + next; + } # Don't update the fine if today is a holiday. # This ensures that dropbox mode will remove the correct amount of fine. @@ -211,22 +213,34 @@ for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) { if($isHoliday) { $debug and warn "Today is a holiday. The fine for borrower " . $data->[$i]->{'borrowernumber'} . " will not be applied"; } else { - $debug and warn "Updating fine for borrower " . $data->[$i]->{'borrowernumber'} . " with amount : $amount"; - UpdateFine( $data->[$i]->{'itemnumber'}, $data->[$i]->{'borrowernumber'}, $amount, $type, $due_str ) if ( $amount > 0 ); + $debug and warn "Creating fine for borrower " . $data->[$i]->{'borrowernumber'} . " with amount : $amount"; + + # We mark this borrower as already processed $borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}} = 1; + + my $borrowernumber = $data->[$i]->{'borrowernumber'}; + my $itemnumber = $data->[$i]->{'itemnumber'}; + + # And we create the fine + my $sth4 = $dbh->prepare( "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?" ); + $sth4->execute($itemnumber); + my $title = $sth4->fetchrow; + + my $desc = "staticfine"; + my $query = "INSERT INTO accountlines + (borrowernumber,itemnumber,date,amount,description,debit_type_code,status,amountoutstanding) + VALUES (?,?,now(),?,?,'OVERDUE','RETURNED',?)"; + my $sth2 = $dbh->prepare($query); + $bigdebug and warn "query: $query\nw/ args: $borrowernumber, $itemnumber, $amount, $desc, $amount\n"; + $sth2->execute( $borrowernumber, $itemnumber, $amount, $desc, $amount ); + } } - my @cells = (); - push @cells, map { $borrower->{$_} } @borrower_fields; - push @cells, map { $data->[$i]->{$_} } @item_fields; - push @cells, $type, $daycounttotal, $amount; - print FILE join( $delim, @cells ), "\n"; } -my $numOverdueItems = scalar(@$data); if ($verbose) { print <