From 4d669d7bb29d93e9dd1b30193c955810a1101884 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Tue, 15 Jul 2014 10:52:30 -0400
Subject: [PATCH] Bug 6427 - Add & update cron scripts

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Sean McGarvey <seanm@pascolibraries.org>
---
 misc/cronjobs/create_koc_db.pl                |   41 +--------------
 misc/cronjobs/fines.pl                        |   10 +++-
 misc/cronjobs/recalculate_account_balances.pl |   72 +++++++++++++++++++++++++
 3 files changed, 80 insertions(+), 43 deletions(-)
 create mode 100755 misc/cronjobs/recalculate_account_balances.pl

diff --git a/misc/cronjobs/create_koc_db.pl b/misc/cronjobs/create_koc_db.pl
index ccc4a5b..f49519e 100755
--- a/misc/cronjobs/create_koc_db.pl
+++ b/misc/cronjobs/create_koc_db.pl
@@ -256,10 +256,8 @@ SELECT borrowernumber,
        city,
        phone,
        dateofbirth,
-       sum( accountlines.amountoutstanding ) as total_fines
+       account_balance as total_fines
 FROM borrowers
-LEFT JOIN accountlines USING (borrowernumber)
-GROUP BY borrowernumber;
 END_SQL
 
     my $fields_count = $sth_mysql->execute();
@@ -278,43 +276,6 @@ END_SQL
     }
     $dbh_sqlite->commit();
     print "inserted $count borrowers\n" if $verbose;
-    # add_fines_to_borrowers_table();
-}
-
-=head2 add_fines_to_borrowers_table
-
-Import the fines from koha.accountlines into the sqlite db
-
-=cut
-
-sub add_fines_to_borrowers_table {
-
-    print "preparing to update borrowers\n" if $verbose;
-    my $sth_mysql = $dbh_mysql->prepare(
-        "SELECT DISTINCT borrowernumber, SUM( amountoutstanding ) AS total_fines
-                                    FROM accountlines
-                                    GROUP BY borrowernumber"
-    );
-    $sth_mysql->execute();
-    my $count;
-    while ( my $result = $sth_mysql->fetchrow_hashref() ) {
-        $count++;
-        if ( $verbose ) {
-            print '.' unless ( $count % 10 );
-            print "$count\n" unless ( $count % 1000 );
-        }
-
-        my $borrowernumber = $result->{'borrowernumber'};
-        my $total_fines    = $result->{'total_fines'};
-
-        # warn "Fines for Borrower # $borrowernumber are \$ $total_fines \n" if $verbose;
-        my $sql = "UPDATE borrowers SET total_fines = ? WHERE borrowernumber = ?";
-
-        my $sth_sqlite = $dbh_sqlite->prepare($sql);
-        $sth_sqlite->execute( $total_fines, $borrowernumber );
-        $sth_sqlite->finish();
-    }
-    print "updated $count borrowers\n" if ( $verbose && $count );
 }
 
 =head2 create_issue_table
diff --git a/misc/cronjobs/fines.pl b/misc/cronjobs/fines.pl
index 38c344a..6ee6a91 100755
--- a/misc/cronjobs/fines.pl
+++ b/misc/cronjobs/fines.pl
@@ -129,9 +129,13 @@ for my $overdue ( @{$overdues} ) {
     if ( $mode eq 'production' && !$is_holiday{$branchcode} ) {
         if ( $amount > 0 ) {
             UpdateFine(
-                $overdue->{itemnumber},
-                $overdue->{borrowernumber},
-                $amount, $type, output_pref($datedue)
+                {
+                    itemnumber     => $overdue->{itemnumber},
+                    borrowernumber => $overdue->{borrowernumber},
+                    amount         => $amount,
+                    due            => output_pref($datedue),
+                    issue_id       => $overdue->{issue_id}
+                }
             );
         }
     }
diff --git a/misc/cronjobs/recalculate_account_balances.pl b/misc/cronjobs/recalculate_account_balances.pl
new file mode 100755
index 0000000..63086c2
--- /dev/null
+++ b/misc/cronjobs/recalculate_account_balances.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl -w
+
+# Copyright 2010 Biblibre SARL
+#
+# 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 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.
+#
+# 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.
+
+use strict;
+use warnings;
+use utf8;
+use v5.10;
+
+BEGIN {
+
+    # find Koha's Perl modules
+    # test carefully before changing this
+    use FindBin;
+    eval { require "$FindBin::Bin/../kohalib.pl" };
+}
+
+use Getopt::Long;
+
+use Pod::Usage;
+use Koha::Database;
+use Koha::Accounts;
+
+my ($help, $verbose, $borrower);
+
+GetOptions(
+    'h|help'         => \$help,
+    'v|verbose'      => \$verbose,
+    'b|borrower:s'   => \$borrower,
+);
+
+if($help){
+    print <<EOF
+    This script recalculates all patron account balances
+    Parameters :
+    -h --help This message
+    -v --verbose
+    -b --borrower - Recalculate only this borrower's account balance
+EOF
+;
+    exit;
+}
+
+say "Finding borrowers..." if ( $verbose );
+
+my $params;
+$params->{borrowernumber} = $borrower if ( $borrower );
+
+my $borrowers_rs = Koha::Database->new()->schema->resultset('Borrower')->search( $params );
+
+while ( my $borrower = $borrowers_rs->next() ) {
+    print "Setting balance for " . $borrower->firstname() . " " . $borrower->surname() . " ( " . $borrower->cardnumber() . " ) to " if ( $verbose );
+    my $account_balance = RecalculateAccountBalance({
+        borrower => $borrower
+    });
+    say $account_balance if ( $verbose );
+}
-- 
1.7.2.5