svn commit: r475329 - in /incubator/ofbiz/trunk/applications/accounting: entitydef/entitymodel.xml 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: r475329 - in /incubator/ofbiz/trunk/applications/accounting: entitydef/entitymodel.xml src/org/ofbiz/accounting/invoice/InvoiceServices.java

sichen
Author: sichen
Date: Wed Nov 15 09:53:05 2006
New Revision: 475329

URL: http://svn.apache.org/viewvc?view=rev&rev=475329
Log:
OFBIZ-446: Change allocation of payment applications to invoices so that only those which are SENT or RECEIVED are applied to DISBURSEMENTS or RECEIPTS respectively.  Previously, the allocation was blind and would apply any PaymentApplication to its invoice without checking status or paymentTypeId. The use of PaymentAndApplication simplified some related code, this whole section of InvoiceServices could use a good refactoring.

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

Modified: incubator/ofbiz/trunk/applications/accounting/entitydef/entitymodel.xml
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/accounting/entitydef/entitymodel.xml?view=diff&rev=475329&r1=475328&r2=475329
==============================================================================
--- incubator/ofbiz/trunk/applications/accounting/entitydef/entitymodel.xml (original)
+++ incubator/ofbiz/trunk/applications/accounting/entitydef/entitymodel.xml Wed Nov 15 09:53:05 2006
@@ -2326,6 +2326,9 @@
       <relation type="one-nofk" rel-entity-name="StatusItem">
         <key-map field-name="statusId"/>
       </relation>
+      <relation type="one-nofk" rel-entity-name="PaymentType">
+        <key-map field-name="paymentTypeId"/>
+      </relation>
       <relation type="one-nofk" rel-entity-name="PaymentMethodType">
         <key-map field-name="paymentMethodTypeId"/>
       </relation>

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=475329&r1=475328&r2=475329
==============================================================================
--- 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 15 09:53:05 2006
@@ -32,6 +32,7 @@
 import org.ofbiz.accounting.payment.BillingAccountWorker;
 import org.ofbiz.accounting.payment.PaymentWorker;
 import org.ofbiz.accounting.payment.PaymentGatewayServices;
+import org.ofbiz.accounting.util.UtilAccounting;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilDateTime;
 import org.ofbiz.base.util.UtilFormatOut;
@@ -1710,9 +1711,25 @@
             return ServiceUtil.returnSuccess();
         }
         
+        // Get the payment applications that can be used to pay the invoice
         List paymentAppl = null;
         try {
-            paymentAppl = delegator.findByAnd("PaymentApplication", UtilMisc.toMap("invoiceId", invoiceId));
+            paymentAppl = delegator.findByAnd("PaymentAndApplication", UtilMisc.toMap("invoiceId", invoiceId));
+            if (paymentAppl != null) {
+
+                // For each payment application, select only those that are RECEIVED or SENT based on whether the payment is a RECEIPT or DISBURSEMENT respectively
+                for (Iterator iter = paymentAppl.iterator(); iter.hasNext(); ) {
+                    GenericValue payment = (GenericValue) iter.next();
+                    if ("PMNT_RECEIVED".equals(payment.get("statusId")) && UtilAccounting.isReceipt(payment)) {
+                        continue; // keep
+                    }
+                    if ("PMNT_SENT".equals(payment.get("statusId")) && UtilAccounting.isDisbursement(payment)) {
+                        continue; // keep
+                    }
+                    // all other cases, remove the payment applicaition
+                    iter.remove();
+                }
+            }
         } catch (GenericEntityException e) {
             String errMsg = UtilProperties.getMessage(resource, "AccountingProblemGettingPaymentApplication",UtilMisc.toMap("invoiceId",invoiceId), locale);
             Debug.logError(e, errMsg, module);
@@ -1726,22 +1743,14 @@
             while (pai.hasNext()) {
                 GenericValue payAppl = (GenericValue) pai.next();
                 payments.put(payAppl.getString("paymentId"), payAppl.getBigDecimal("amountApplied"));
-                
+
                 // paidDate will be the last date (chronologically) of all the Payments applied to this invoice
-                try {
-                    GenericValue Payment = payAppl.getRelatedOne("Payment");
-                    Timestamp paymentDate = Payment.getTimestamp("effectiveDate");
-                    if (paymentDate != null) {
-                        if ((paidDate == null) || (paidDate.before(paymentDate))) {
-                            paidDate = paymentDate;
-                        }
-                    }    
-                } catch (GenericEntityException ex) {
-                   String errMsg = UtilProperties.getMessage(resource, "AccountingCannotGetPaymentForApplication",UtilMisc.toMap("payAppl",payAppl,"msg",ex.getMessage()), locale);
-                   Debug.logError(ex, errMsg, module);
-                   return ServiceUtil.returnError(errMsg);
-                }
-                
+                Timestamp paymentDate = payAppl.getTimestamp("effectiveDate");
+                if (paymentDate != null) {
+                    if ((paidDate == null) || (paidDate.before(paymentDate))) {
+                        paidDate = paymentDate;
+                    }
+                }    
             }
         }
 
@@ -2838,6 +2847,7 @@
             List paymentApplications = payment.getRelated("PaymentApplication");
             if (UtilValidate.isEmpty(paymentApplications)) return ServiceUtil.returnSuccess();
 
+            // TODO: this is inefficient -- instead use HashSet to construct a distinct Set of invoiceIds, then iterate over it and call checkInvoicePaymentAppls
             Iterator iter = paymentApplications.iterator();
             while (iter.hasNext()) {
                 GenericValue paymentApplication = (GenericValue) iter.next();