Bug 12633: Remove SQLHelper in C4::Members
[srvgit] / C4 / Overdues.pm
index b3ec066..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
@@ -109,14 +109,14 @@ sub Getoverdues {
     my $statement;
     if ( C4::Context->preference('item-level_itypes') ) {
         $statement = "
-   SELECT issues.*, items.itype as itemtype, items.homebranch, items.barcode
+   SELECT issues.*, items.itype as itemtype, items.homebranch, items.barcode, items.itemlost
      FROM issues 
 LEFT JOIN items       USING (itemnumber)
     WHERE date_due < NOW()
 ";
     } else {
         $statement = "
-   SELECT issues.*, biblioitems.itemtype, items.itype, items.homebranch, items.barcode
+   SELECT issues.*, biblioitems.itemtype, items.itype, items.homebranch, items.barcode, items.itemlost
      FROM issues 
 LEFT JOIN items       USING (itemnumber)
 LEFT JOIN biblioitems USING (biblioitemnumber)
@@ -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(
@@ -655,9 +650,16 @@ sub GetFine {
     my $dbh   = C4::Context->dbh();
     my $query = q|SELECT sum(amountoutstanding) as fineamount FROM accountlines
     where accounttype like 'F%'
-  AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?|;
+  AND amountoutstanding > 0 AND borrowernumber=?|;
+    my @query_param;
+    push @query_param, $borrowernumber;
+    if (defined $itemnum )
+    {
+        $query .= " AND itemnumber=?";
+        push @query_param, $itemnum;
+    }
     my $sth = $dbh->prepare($query);
-    $sth->execute( $itemnum, $borrowernumber );
+    $sth->execute( @query_param );
     my $fine = $sth->fetchrow_hashref();
     if ($fine->{fineamount}) {
         return $fine->{fineamount};
@@ -756,14 +758,18 @@ returns a list of branch codes for branches with overdue rules defined.
 
 sub GetBranchcodesWithOverdueRules {
     my $dbh               = C4::Context->dbh;
-    my $rqoverduebranches = $dbh->prepare("SELECT DISTINCT branchcode FROM overduerules WHERE delay1 IS NOT NULL AND branchcode <> '' ORDER BY branchcode");
-    $rqoverduebranches->execute;
-    my @branches = map { shift @$_ } @{ $rqoverduebranches->fetchall_arrayref };
-    if (!$branches[0]) {
-       my $availbranches = C4::Branch::GetBranches();
-       @branches = keys %$availbranches;
+    my $branchcodes = $dbh->selectcol_arrayref(q|
+        SELECT DISTINCT(branchcode)
+        FROM overduerules
+        WHERE delay1 IS NOT NULL
+        ORDER BY branchcode
+    |);
+    if ( $branchcodes->[0] eq '' ) {
+        # If a default rule exists, all branches should be returned
+        my $availbranches = C4::Branch::GetBranches();
+        return keys %$availbranches;
     }
-    return @branches;
+    return @$branchcodes;
 }
 
 =head2 CheckItemNotify