svn commit: r480796 - /incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r480796 - /incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java

sichen
Author: sichen
Date: Wed Nov 29 16:52:29 2006
New Revision: 480796

URL: http://svn.apache.org/viewvc?view=rev&rev=480796
Log:
Fixed bugs with billing account where the updatePaymentApplicationDef method was implemented incorrectly to the design of the PaymentApplication/BillingAccount data model.  Now you can use payment and applications to add value to and apply value from a billing account, though this really needs some serious refactoring or rewriting

Modified:
    incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java

Modified: incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java?view=diff&rev=480796&r1=480795&r2=480796
==============================================================================
--- incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java (original)
+++ incubator/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/invoice/InvoiceServices.java Wed Nov 29 16:52:29 2006
@@ -2057,13 +2057,15 @@
         }
 
         // on a new paymentApplication check if only billing or invoice or tax
-        // id is provided not 2,3...
+        // id is provided not 2,3... BUT a combination of billingAccountId and invoiceId is permitted - that's how you use a
+        // Billing Account to pay for an Invoice
         if (paymentApplicationId == null) {
             int count = 0;
             if (invoiceId != null) count++;
             if (toPaymentId != null) count++;
             if (billingAccountId != null) count++;
             if (taxAuthGeoId != null) count++;
+            if ((billingAccountId != null) && (invoiceId != null)) count--;
             if (count != 1) {
                 errorMessageList.add(UtilProperties.getMessage(resource, "AccountingSpecifyInvoiceToPaymentBillingAccountTaxGeoId", locale));
             }
@@ -2189,28 +2191,35 @@
                 ServiceUtil.returnError(e.getMessage());
             }
             if (billingAccount == null) {
-                errorMessageList.add(UtilProperties.getMessage(resource, "AccountingBillingAccountNotFound",UtilMisc.toMap("bilingAccountId",billingAccountId), locale));
+                errorMessageList.add(UtilProperties.getMessage(resource, "AccountingBillingAccountNotFound",UtilMisc.toMap("billingAccountId",billingAccountId), locale));
             }
+            
+            // Get the available balance, which is how much can be used, rather than the regular balance, which is how much has already been charged
             try {
                 billingAccountApplyAvailable = billingAccount.getBigDecimal("accountLimit").add(
-                        BillingAccountWorker.getBillingAccountBalance(billingAccount)).setScale(decimals,rounding);
+                        BillingAccountWorker.getBillingAccountAvailableBalance(billingAccount)).setScale(decimals,rounding);
             } catch (GenericEntityException e) {
-                errorMessageList.add(UtilProperties.getMessage(resource, "AccountingBillingAccountBalanceNotFound",UtilMisc.toMap("bilingAccountId",billingAccountId), locale));
+                errorMessageList.add(UtilProperties.getMessage(resource, "AccountingBillingAccountBalanceNotFound",UtilMisc.toMap("billingAccountId",billingAccountId), locale));
                 ServiceUtil.returnError(e.getMessage());
             }
 
             if (paymentApplicationId == null) {
-                // only check when a new record is created, existing record check is done in the paymentAllication section
-                if (billingAccountApplyAvailable.signum() <= 0) {
-                    errorMessageList.add(UtilProperties.getMessage(resource, "AccountingBillingAccountBalanceProblem",UtilMisc.toMap("bilingAccountId",billingAccountId,"isoCode",billingAccount.getString("accountCurrencyUomId")), locale));
-                } else {
-                    // check here for too much application if a new record is
-                    // added (paymentApplicationId == null)
-                    if (amountApplied.compareTo(billingAccountApplyAvailable) == 1) {
-                        errorMessageList.add(UtilProperties.getMessage(resource, "AccountingBillingAccountLessRequested",
-                                UtilMisc.toMap("billingAccountId",billingAccountId,
+                // when creating a new PaymentApplication, check if there is sufficient balance in the billing account, but only if the invoiceId is not null
+                // If you create a PaymentApplication with both billingAccountId and invoiceId, then you're applying a billing account towards an invoice
+                // If you create a PaymentApplication just with billingAccountId and no invoiceId, then you're adding value to billing account, so it should not matter
+                // what the previous available balance is
+                if (invoiceId != null) {
+                    if (billingAccountApplyAvailable.signum() <= 0)  {
+                        errorMessageList.add(UtilProperties.getMessage(resource, "AccountingBillingAccountBalanceProblem",UtilMisc.toMap("billingAccountId",billingAccountId,"isoCode",billingAccount.getString("accountCurrencyUomId")), locale));
+                    } else {
+                        // check here for too much application if a new record is
+                        // added (paymentApplicationId == null)
+                        if (amountApplied.compareTo(billingAccountApplyAvailable) == 1) {
+                            errorMessageList.add(UtilProperties.getMessage(resource, "AccountingBillingAccountLessRequested",
+                                        UtilMisc.toMap("billingAccountId",billingAccountId,
                                             "billingAccountApplyAvailable",billingAccountApplyAvailable,
                                             "amountApplied",amountApplied,"isoCode",billingAccount.getString("accountCurrencyUomId")),locale));
+                        }
                     }
                 }
             }