Bug 28606: Remove $DEBUG and $ENV{DEBUG}
[srvgit] / misc / cronjobs / staticfines.pl
index 1d45806..0966cb6 100755 (executable)
@@ -3,7 +3,7 @@
 #  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.
@@ -37,15 +37,17 @@ 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;
@@ -57,7 +59,7 @@ 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(
@@ -74,7 +76,6 @@ 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.
 
@@ -90,6 +91,8 @@ This script has the following parameters :
 ENDUSAGE
 die $usage if $help;
 
+cronlogaction();
+
 my $dbh = C4::Context->dbh;
 
 # Processing categories
@@ -132,38 +135,38 @@ if (defined $borrowernumberlimit) {
 }
 my $overdueItemsCounted = 0;
 my %calendars           = ();
-$today      = C4::Dates->new();
-$today_iso  = $today->output('iso');
+$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++ ) {
     next if $data->[$i]->{'itemlost'};
-    my $datedue;
-    my $datedue_days;
+    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;
     }
-    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).
 
@@ -185,16 +188,16 @@ for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) {
     ( $datedue_days <= $today_days ) or next;    # or it's not overdue, right?
 
     $overdueItemsCounted++;
-    my ( $amount, $type, $unitcounttotal, $unitcount ) = CalcFine(
+    my ( $amount, $unitcounttotal, $unitcount ) = CalcFine(
         $data->[$i],
-        $borrower->{'categorycode'},
+        $patron->categorycode,
         $branchcode,
-        dt_from_string($datedue->output('iso')),
-        dt_from_string($today->output('iso')),
+        $datedue,
+        $today,
     );
 
     # Reassign fine's amount if specified in command-line
-    $amount = $catamounts{$borrower->{'categorycode'}} if (defined $catamounts{$borrower->{'categorycode'}});
+    $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'});
@@ -203,9 +206,6 @@ for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) {
         next;
     }
 
-    # FIXME: $type NEVER gets populated by anything.
-    ( defined $type ) or $type = '';
-
     # Don't update the fine if today is a holiday.
     # This ensures that dropbox mode will remove the correct amount of fine.
     if ( $mode eq 'production' and !$borrowersalreadyapplied->{$data->[$i]->{'borrowernumber'}}) {
@@ -226,14 +226,13 @@ for ( my $i = 0 ; $i < scalar(@$data) ; $i++ ) {
             $sth4->execute($itemnumber);
             my $title = $sth4->fetchrow;
 
-            my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
             my $desc        = "staticfine";
             my $query       = "INSERT INTO accountlines
-                        (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
-                                VALUES (?,?,now(),?,?,'F',?,?,?)";
+                        (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, $amount, $nextaccntno\n";
-            $sth2->execute( $borrowernumber, $itemnumber, $amount, $desc, $amount, $amount, $nextaccntno );
+            $bigdebug and warn "query: $query\nw/ args: $borrowernumber, $itemnumber, $amount, $desc, $amount\n";
+            $sth2->execute( $borrowernumber, $itemnumber, $amount, $desc, $amount );
 
         }
     }