From 425c29e1e3c80b8d1fc637bf557dc3801e3245e6 Mon Sep 17 00:00:00 2001
From: Jason Stephenson <jstephenson@mvlc.org>
Date: Thu, 22 Sep 2011 12:52:08 -0400
Subject: [PATCH] Fixes for SIP2 Fee Payment.

Skip negative or zero bill balance when paying multiple bills.

Log amount paid, bill id, balance, and amount remaining when paying
multiple bills.

Use sprintf("%.2f") to avoid floating point errors.

Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
Signed-off-by: Dan Scott <dscott@laurentian.ca>
---
 .../src/perlmods/lib/OpenILS/SIP/Transaction/FeePayment.pm     | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/FeePayment.pm b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/FeePayment.pm
index 28de0dce68..d0b3fa4709 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/FeePayment.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/FeePayment.pm
@@ -126,6 +126,9 @@ sub do_fee_payment {
             foreach my $bill (@{$results}) {
                 my $payment;
                 syslog('LOG_INFO', 'OILS: bill '. $bill->id . ' amount ' . $bill->balance_owed);
+                # Skip negative or zero-balance bills. (Not that I've
+                # ever seen any.)
+                next if ($bill->balance_owed <= 0);
                 if ($bill->balance_owed >= $amount_paid) {
                     # We owe as much as or more than we have money
                     # left, so pay what we have left.
@@ -139,10 +142,13 @@ sub do_fee_payment {
                 }
                 # Add the payment to our array.
                 push(@payments, [$bill->id, $payment]);
+                # Attempt to round $amount_paid to avoid floating point error.
+                $amount_paid = sprintf("%.2f", $amount_paid);
+                syslog('LOG_INFO', "OILS: paid $payment on " . $bill->id . " with balance " . $bill->balance_owed . " and $amount_paid remaining");
                 # Leave if we ran out of money.
-                last if ($amount_paid == 0);
+                last if ($amount_paid == 0.00);
             }
-            if (@payments && $amount_paid == 0) {
+            if (@payments && $amount_paid == 0.00) {
                 # pay the bills with a reference to our payments
                 # array.
                 my $resp = $self->pay_bills(\@payments);
-- 
2.11.0