Bug 14057: Inventory is painfully slow
[srvgit] / C4 / Overdues.pm
index 1797923..6eeb6cf 100644 (file)
@@ -6,18 +6,18 @@ package C4::Overdues;
 #
 # 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; FIXME - Bug 2505
@@ -47,7 +47,7 @@ BEGIN {
         &AmountNotify
         &UpdateFine
         &GetFine
-        
+        &get_chargeable_units
         &CheckItemNotify
         &GetOverduesForBranch
         &RemoveNotifyLine
@@ -248,7 +248,7 @@ sub CalcFine {
     my $fine_unit = $data->{lengthunit};
     $fine_unit ||= 'days';
 
-    my $chargeable_units = _get_chargeable_units($fine_unit, $start_date, $end_date, $branchcode);
+    my $chargeable_units = get_chargeable_units($fine_unit, $start_date, $end_date, $branchcode);
     my $units_minus_grace = $chargeable_units - $data->{firstremind};
     my $amount = 0;
     if ($data->{'chargeperiod'}  && ($units_minus_grace > 0)  ) {
@@ -267,9 +267,9 @@ sub CalcFine {
 }
 
 
-=head2 _get_chargeable_units
+=head2 get_chargeable_units
 
-    _get_chargeable_units($unit, $start_date_ $end_date, $branchcode);
+    get_chargeable_units($unit, $start_date_ $end_date, $branchcode);
 
 return integer value of units between C<$start_date> and C<$end_date>, factoring in holidays for C<$branchcode>.
 
@@ -281,7 +281,7 @@ C<$branchcode> is the branch whose calendar to use for finding holidays.
 
 =cut
 
-sub _get_chargeable_units {
+sub get_chargeable_units {
     my ($unit, $date_due, $date_returned, $branchcode) = @_;
 
     # If the due date is later than the return date
@@ -580,29 +580,24 @@ sub UpdateFine {
             #      print "no update needed $data->{'amount'}"
         }
     } else {
-        my $sth4 = $dbh->prepare(
-            "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
-        );
-        $sth4->execute($itemnum);
-        my $title = $sth4->fetchrow;
-
-#         #   print "not in account";
-#         my $sth3 = $dbh->prepare("Select max(accountno) from accountlines");
-#         $sth3->execute;
-# 
-#         # FIXME - Make $accountno a scalar.
-#         my @accountno = $sth3->fetchrow_array;
-#         $sth3->finish;
-#         $accountno[0]++;
-# begin transaction
-               my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
-               my $desc = ($type ? "$type " : '') . "$title $due";     # FIXEDME, avoid whitespace prefix on empty $type
-               my $query = "INSERT INTO accountlines
-                   (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
-                           VALUES (?,?,now(),?,?,'FU',?,?,?)";
-               my $sth2 = $dbh->prepare($query);
-               $debug and print STDERR "UpdateFine query: $query\nw/ args: $borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno\n";
-        $sth2->execute($borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno);
+        if ( $amount ) { # Don't add new fines with an amount of 0
+            my $sth4 = $dbh->prepare(
+                "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
+            );
+            $sth4->execute($itemnum);
+            my $title = $sth4->fetchrow;
+
+            my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
+
+            my $desc = ( $type ? "$type " : '' ) . "$title $due";    # FIXEDME, avoid whitespace prefix on empty $type
+
+            my $query = "INSERT INTO accountlines
+                         (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
+                         VALUES (?,?,now(),?,?,'FU',?,?,?)";
+            my $sth2 = $dbh->prepare($query);
+            $debug and print STDERR "UpdateFine query: $query\nw/ args: $borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno\n";
+            $sth2->execute( $borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno );
+        }
     }
     # logging action
     &logaction(