Make changes to Storage/Publisher/action.pm required by new void_payment.
authorJason Stephenson <jason@sigio.com>
Tue, 7 Jan 2014 22:06:59 +0000 (17:06 -0500)
committerKathy Lussier <klussier@masslnc.org>
Thu, 13 Feb 2014 05:40:56 +0000 (00:40 -0500)
Signed-off-by: Jason Stephenson <jason@sigio.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CircCommon.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/action.pm

index 261e391..77a4748 100644 (file)
@@ -364,7 +364,7 @@ sub bill_payment_map_for_xact {
     # Check for CStoreEditor and make a new one if we have to. This
     # allows one-off calls to this subroutine to pass undef as the
     # CStoreEditor and not have to create one of their own.
-    $e = OpenILS::Utils::CStoreEditor->new; unless ($e);
+    $e = OpenILS::Utils::CStoreEditor->new unless ($e);
 
     # find all bills in order
     my $bill_search = [
index 08bd32c..21fe357 100644 (file)
@@ -14,6 +14,7 @@ use DateTime::Format::ISO8601;
 use OpenILS::Utils::Penalty;
 use POSIX qw(ceil);
 use OpenILS::Application::Circ::CircCommon;
+my $CC = "OpenILS::Application::Circ::CircCommon";
 use OpenILS::Application::AppUtils;
 my $U = "OpenILS::Application::AppUtils";
 
@@ -637,19 +638,22 @@ sub patron_circ_summary {
     return undef unless ($id);
     my $c_table = action::circulation->table;
     my $b_table = money::billing->table;
+    my $p_table = money::void_payment->table;
 
     $log->debug("Retrieving patron summary for id $id", DEBUG);
 
     my $select = <<"    SQL";
         SELECT  COUNT(DISTINCT c.id), SUM( COALESCE(b.amount,0) )
           FROM  $c_table c
-            LEFT OUTER JOIN $b_table b ON (c.id = b.xact AND b.voided = FALSE)
+            LEFT OUTER JOIN $b_table b ON (c.id = b.xact)
+            LEFT OUTER JOIN $p_table p ON (b.id = p.billing)
           WHERE c.usr = ?
             AND c.xact_finish IS NULL
             AND (
                 c.stop_fines NOT IN ('CLAIMSRETURNED','LOST')
                 OR c.stop_fines IS NULL
             )
+            AND p IS NULL
     SQL
 
     return action::survey->db_Main->selectrow_arrayref($select, {}, $id);
@@ -1112,21 +1116,29 @@ sub generate_fines {
                 " (user ".$c->usr.").\n".
                 "\tItem was due on or before: ".localtime($due)."\n");
 
-            my @fines = money::billing->search_where(
-                { xact => $c->id,
-                  btype => 1,
-                  billing_ts => { '>' => $c->$due_date_method } },
-                { order_by => 'billing_ts DESC'}
-            );
+            # Retrieve the bill_payment_map for the circulation transaction.
+            my $bill_payment_map = $CC->bill_payment_map_for_xact(undef, $c);
 
-            my $f_idx = 0;
-            my $fine = $fines[$f_idx] if (@fines);
-            if ($overbill) {
-                $fine = $fines[++$f_idx] while ($fine and $fine->voided);
-            }
+            # Pull out those entries for overdue bills in reverse date order.
+            my @fine_map = sort { $b->bill->billing_ts() <=> $a->bill->billing_ts() }
+                grep { $_->bill->btype() == 1 && $_->bill->billing_ts() > $c->$due_date_method }
+                    @{$bill_payment_map};
 
+            my $f_idx = 0;
             my $current_fine_total = 0;
-            $current_fine_total += int($_->amount * 100) for (grep { $_ and !$_->voided } @fines);
+            my $fine;
+            if (@fine_map) {
+                if ($overbill) {
+                    while ($fine_map[$f_idx] and $fine_map[$f_idx]->void_amount() >= $fine_map[$f_idx]->bill_amount()) {
+                        $f_idx++;
+                    }
+                }
+                # Set the $fine to be used for $last_fine below.
+                $fine = $fine_map[$f_idx]->bill() if ($fine_map[$f_idx]);
+                # Calculate the current fine total using the modified bill amounts from the bill payment map.
+                map { $current_fine_total += int($_->bill->amount() * 100) }
+                    grep { $_->bill_amount() < $_->void_amount() } @fine_map;
+            }
 
             my $last_fine;
             if ($fine) {
@@ -1136,7 +1148,7 @@ sub generate_fines {
                 $log->info( "Potential first billing for circ ".$c->id );
                 $last_fine = $due;
 
-                $grace_period = OpenILS::Application::Circ::CircCommon->extend_grace_period($c->$circ_lib_method->to_fieldmapper->id,$c->$due_date_method,$grace_period,undef,$hoo{$c->$circ_lib_method});
+                $grace_period = $CC->extend_grace_period($c->$circ_lib_method->to_fieldmapper->id,$c->$due_date_method,$grace_period,undef,$hoo{$c->$circ_lib_method});
             }
 
             return if ($last_fine > $now);