Bug 20727: Move temporary_directory() to C4::Context
[koha.git] / misc / cronjobs / fines.pl
index 27bd061..9fa7b94 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 relys on being able to write to /tmp/
 # It creates the fines file
 #
 #  This script is meant to be run nightly out of cron.
 #
 # 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 <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
@@ -38,26 +38,30 @@ use File::Spec;
 
 use Koha::Calendar;
 use Koha::DateUtils;
+use Koha::UploadedFile;
+use C4::Log;
 
 my $help;
 my $verbose;
 my $output_dir;
+my $log;
 
 GetOptions(
     'h|help'    => \$help,
     'v|verbose' => \$verbose,
+    'l|log'     => \$log,
     'o|out:s'   => \$output_dir,
 );
 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.
+to patron accounts.  The Koha system preference 'finesMode' controls
+whether the fines are calculated and charged to the patron accounts ("Calculate and charge");
+calculated and emailed to the admin but not applied ("Calculate (but only for mailing to the admin)"); or not calculated ("Don't calculate").
 
 This script has the following parameters :
     -h --help: this message
+    -l --log: log the output to a file (optional if the -o parameter is given)
     -o --out:  ouput directory for logs (defaults to env or /tmp if !exist)
     -v --verbose
 
@@ -68,6 +72,8 @@ if ($help) {
     exit;
 }
 
+cronlogaction();
+
 my @borrower_fields =
   qw(cardnumber categorycode surname firstname email phone address citystate);
 my @item_fields  = qw(itemnumber barcode date_due);
@@ -79,15 +85,22 @@ my $delim = "\t";    # ?  C4::Context->preference('delimiter') || "\t";
 
 my %is_holiday;
 my $today = DateTime->now( time_zone => C4::Context->tz() );
-my $filename = get_filename($output_dir);
-
-open my $fh, '>>', $filename or croak "Cannot write file $filename: $!";
-print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
-print {$fh} "\n";
+my $filename;
+if ($log or $output_dir) {
+    $filename = get_filename($output_dir);
+}
 
-my $counted  = 0;
+my $fh;
+if ($filename) {
+    open $fh, '>>', $filename or croak "Cannot write file $filename: $!";
+    print {$fh} join $delim, ( @borrower_fields, @item_fields, @other_fields );
+    print {$fh} "\n";
+}
+my $counted = 0;
 my $overdues = Getoverdues();
 for my $overdue ( @{$overdues} ) {
+    next if $overdue->{itemlost};
+
     if ( !defined $overdue->{borrowernumber} ) {
         carp
 "ERROR in Getoverdues : issues.borrowernumber IS NULL.  Repair 'issues' table now!  Skipping record.\n";
@@ -118,27 +131,42 @@ for my $overdue ( @{$overdues} ) {
     # 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' && !$is_holiday{$branchcode} ) {
-        if ( $amount > 0 ) {
+        if ( $amount && $amount > 0 ) {
             UpdateFine(
-                $overdue->{itemnumber},
-                $overdue->{borrowernumber},
-                $amount, $type, output_pref($datedue)
+                {
+                    issue_id       => $overdue->{issue_id},
+                    itemnumber     => $overdue->{itemnumber},
+                    borrowernumber => $overdue->{borrowernumber},
+                    amount         => $amount,
+                    type           => $type,
+                    due            => output_pref($datedue),
+                }
             );
         }
     }
-    my @cells;
-    push @cells,
-      map { defined $borrower->{$_} ? $borrower->{$_} : q{} } @borrower_fields;
-    push @cells, map { $overdue->{$_} } @item_fields;
-    push @cells, $type, $unitcounttotal, $amount;
-    say {$fh} join $delim, @cells;
+    if ($filename) {
+        my @cells;
+        push @cells,
+          map { defined $borrower->{$_} ? $borrower->{$_} : q{} }
+          @borrower_fields;
+        push @cells, map { $overdue->{$_} } @item_fields;
+        push @cells, $type, $unitcounttotal, $amount;
+        say {$fh} join $delim, @cells;
+    }
+}
+if ($filename){
+    close $fh;
 }
-close $fh;
 
 if ($verbose) {
     my $overdue_items = @{$overdues};
     print <<"EOM";
-Fines assessment -- $today -- Saved to $filename
+Fines assessment -- $today
+EOM
+    if ($filename) {
+        say "Saved to $filename";
+    }
+    print <<"EOM";
 Number of Overdue Items:
      counted $overdue_items
     reported $counted
@@ -156,7 +184,7 @@ sub set_holiday {
 sub get_filename {
     my $directory = shift;
     if ( !$directory ) {
-        $directory = File::Spec->tmpdir();
+        $directory = C4::Context::temporary_directory;
     }
     if ( !-d $directory ) {
         carp "Could not write to $directory ... does not exist!";
@@ -165,7 +193,7 @@ sub get_filename {
     $name =~ s/\W//;
     $name .= join q{}, q{_}, $today->ymd(), '.log';
     $name = File::Spec->catfile( $directory, $name );
-    if ($verbose) {
+    if ($verbose && $log) {
         say "writing to $name";
     }
     return $name;