details:   https://code.openbravo.com/erp/devel/pi/rev/40e6ff497d25
changeset: 17412:40e6ff497d25
user:      Naiara Martinez <naiara.martinez <at> openbravo.com>
date:      Mon Jul 02 13:13:59 2012 +0200
summary:   Fixed bug 20818 update PercentageOverDue,DaysOutStanding and 
FinalSettlementDate

details:   https://code.openbravo.com/erp/devel/pi/rev/2e48b8785e56
changeset: 17413:2e48b8785e56
user:      Víctor Martínez Romanos <victor.martinez <at> openbravo.com>
date:      Fri Jul 06 09:45:07 2012 +0200
summary:   Fixed bug 20818: implemented code review
- Optimized code that calculates overdue amount
- Implemented code that properly gets the current PSD
- Percentage Paid Late field is only displayed if != 0

diffstat:

 
modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml
 |   5 +-
 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_AddPayment.java
     |  88 +++++++++-
 src-db/database/sourcedata/AD_FIELD.xml                                        
                       |   4 +-
 3 files changed, 87 insertions(+), 10 deletions(-)

diffs (188 lines):

diff -r 4b990d5f82bc -r 2e48b8785e56 
modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml
--- 
a/modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml
     Thu Jul 05 18:06:26 2012 +0200
+++ 
b/modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml
     Fri Jul 06 09:45:07 2012 +0200
@@ -742,7 +742,10 @@
                 UPDATE C_INVOICE
                 SET TOTALPAID = TOTALPAID + 
COALESCE(cur_paymentschedule.OUTSTANDINGAMT, 0),
                     OUTSTANDINGAMT = OUTSTANDINGAMT - 
COALESCE(cur_paymentschedule.OUTSTANDINGAMT, 0),
-                    DUEAMT = V_DueAmount
+                    DUEAMT = V_DueAmount, 
+                    FINALSETTLEMENT = cur_paymentschedule.duedate,
+                    DAYSOUTSTANDING = 0,
+                    PERCENTAGEOVERDUE = 0
                 WHERE C_INVOICE_ID = P_RECORD_ID;
                 UPDATE C_INVOICE
                 SET ISPAID = 'Y'
diff -r 4b990d5f82bc -r 2e48b8785e56 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_AddPayment.java
--- 
a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_AddPayment.java
 Thu Jul 05 18:06:26 2012 +0200
+++ 
b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_AddPayment.java
 Fri Jul 06 09:45:07 2012 +0200
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2010-2011 Openbravo SLU
+ * All portions are Copyright (C) 2010-2012 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -34,6 +34,7 @@
 
 import org.hibernate.Query;
 import org.hibernate.Session;
+import org.hibernate.criterion.Projections;
 import org.hibernate.criterion.Restrictions;
 import org.openbravo.advpaymentmngt.dao.AdvPaymentMngtDao;
 import org.openbravo.advpaymentmngt.utility.FIN_Utility;
@@ -63,6 +64,7 @@
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentMethod;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentPropDetail;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentProposal;
+import org.openbravo.model.financialmgmt.payment.FIN_PaymentSchedInvV;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentSchedule;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentScheduleDetail;
 import org.openbravo.model.financialmgmt.payment.FinAccPaymentMethod;
@@ -962,23 +964,23 @@
     }
     OBDal.getInstance().save(paymentSchedule);
     if (paymentSchedule.getInvoice() != null) {
-      updateInvoicePaymentMonitor(paymentSchedule.getInvoice(), 
paymentSchedule.getDueDate(),
-          amount, writeOffAmount);
+      updateInvoicePaymentMonitor(paymentSchedule, amount, writeOffAmount);
     }
   }
 
   /**
    * Method used to update the payment monitor based on the payment made by 
the user.
    * 
-   * @param invoice
-   *          Invoice object going to be updated based on the payment. 
{Invoice}
+   * @param invoicePaymentSchedule
    * @param amount
    *          Amount of the transaction.
    * @param writeOffAmount
    *          Amount that has been wrote off.
    */
-  private static void updateInvoicePaymentMonitor(Invoice invoice, Date 
dueDate, BigDecimal amount,
-      BigDecimal writeOffAmount) {
+  private static void updateInvoicePaymentMonitor(FIN_PaymentSchedule 
invoicePaymentSchedule,
+      BigDecimal amount, BigDecimal writeOffAmount) {
+    Invoice invoice = invoicePaymentSchedule.getInvoice();
+    Date dueDate = invoicePaymentSchedule.getDueDate();
     boolean isDueDateFlag = dueDate.compareTo(new Date()) <= 0;
     invoice.setTotalPaid(invoice.getTotalPaid().add(amount));
     invoice.setLastCalculatedOnDate(new Date());
@@ -991,7 +993,15 @@
       if (isDueDateFlag)
         invoice.setDueAmount(invoice.getDueAmount().subtract(writeOffAmount));
     }
+
     if (0 == invoice.getOutstandingAmount().compareTo(BigDecimal.ZERO)) {
+      Date finalSettlementDate = getFinalSettlementDate(invoice);
+      // If date is null invoice amount = 0 then nothing to set
+      if (finalSettlementDate != null) {
+        invoice.setFinalSettlementDate(finalSettlementDate);
+        
invoice.setDaysSalesOutstanding(FIN_Utility.getDaysBetween(invoice.getInvoiceDate(),
+            finalSettlementDate));
+      }
       invoice.setPaymentComplete(true);
     } else
       invoice.setPaymentComplete(false);
@@ -1002,6 +1012,11 @@
           && (firstDueDate == null || 
firstDueDate.after(paymentSchedule.getDueDate())))
         firstDueDate = paymentSchedule.getDueDate();
     }
+
+    BigDecimal overdueAmount = calculateOverdueAmount(invoicePaymentSchedule);
+    invoice.setPercentageOverdue(overdueAmount.multiply(new BigDecimal("100"))
+        .divide(invoice.getGrandTotalAmount(), 2, 
BigDecimal.ROUND_HALF_UP).longValue());
+
     if (firstDueDate != null)
       invoice.setDaysTillDue(FIN_Utility.getDaysToDue(firstDueDate));
     else
@@ -1009,6 +1024,65 @@
     OBDal.getInstance().save(invoice);
   }
 
+  private static BigDecimal calculateOverdueAmount(FIN_PaymentSchedule 
invoicePaymentSchedule) {
+    Invoice invoice = invoicePaymentSchedule.getInvoice();
+    BigDecimal overdueOriginal = BigDecimal.ZERO;
+    FIN_PaymentScheduleDetail currentPSD = 
getLastCreatedPaymentScheduleDetail(invoicePaymentSchedule);
+    for (FIN_PaymentSchedule paymentSchedule : 
invoice.getFINPaymentScheduleList()) {
+      Date paymentDueDate = paymentSchedule.getDueDate();
+      for (FIN_PaymentScheduleDetail psd : paymentSchedule
+          .getFINPaymentScheduleDetailInvoicePaymentScheduleList()) {
+        if (!psd.isCanceled()
+            && psd.getPaymentDetails() != null
+            && 
(FIN_Utility.isPaymentConfirmed(psd.getPaymentDetails().getFinPayment().getStatus(),
+                psd) || currentPSD.getId().equals(psd.getId()))) {
+          Date paymentDate = 
psd.getPaymentDetails().getFinPayment().getPaymentDate();
+          if (paymentDate.after(paymentDueDate)) {
+            overdueOriginal = overdueOriginal.add(psd.getAmount());
+          }
+        }
+      }
+
+    }
+    return overdueOriginal;
+  }
+
+  private static FIN_PaymentScheduleDetail getLastCreatedPaymentScheduleDetail(
+      FIN_PaymentSchedule invoicePaymentSchedule) {
+    final OBCriteria<FIN_PaymentScheduleDetail> obc = 
OBDal.getInstance().createCriteria(
+        FIN_PaymentScheduleDetail.class);
+    OBContext.setAdminMode();
+    try {
+      
obc.add(Restrictions.eq(FIN_PaymentScheduleDetail.PROPERTY_INVOICEPAYMENTSCHEDULE,
+          invoicePaymentSchedule));
+      obc.addOrderBy(FIN_PaymentScheduleDetail.PROPERTY_CREATIONDATE, false);
+      obc.setMaxResults(1);
+      return (FIN_PaymentScheduleDetail) obc.uniqueResult();
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+
+  }
+
+  /**
+   * Returns the date in which last payment for this invoice took place
+   * 
+   * @param invoice
+   * @return
+   */
+  private static Date getFinalSettlementDate(Invoice invoice) {
+    final OBCriteria<FIN_PaymentSchedInvV> obc = 
OBDal.getInstance().createCriteria(
+        FIN_PaymentSchedInvV.class);
+    OBContext.setAdminMode();
+    try {
+      obc.add(Restrictions.eq(FIN_PaymentSchedInvV.PROPERTY_INVOICE, invoice));
+      
obc.setProjection(Projections.max(FIN_PaymentSchedInvV.PROPERTY_LASTPAYMENT));
+      return (Date) obc.uniqueResult();
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+  }
+
   /**
    * Returns true if a financial account transactions has to be automatically 
triggered after
    * payment is processed.
diff -r 4b990d5f82bc -r 2e48b8785e56 src-db/database/sourcedata/AD_FIELD.xml
--- a/src-db/database/sourcedata/AD_FIELD.xml   Thu Jul 05 18:06:26 2012 +0200
+++ b/src-db/database/sourcedata/AD_FIELD.xml   Fri Jul 06 09:45:07 2012 +0200
@@ -225769,7 +225769,7 @@
 <!--B1EBC90EB5ECC896E040A8C0280118CA-->  
<IGNOREINWAD><![CDATA[N]]></IGNOREINWAD>
 <!--B1EBC90EB5ECC896E040A8C0280118CA-->  
<AD_FIELDGROUP_ID><![CDATA[5779017ADB22479BB143E49A8B3A9AE0]]></AD_FIELDGROUP_ID>
 <!--B1EBC90EB5ECC896E040A8C0280118CA-->  
<ISDISPLAYED><![CDATA[Y]]></ISDISPLAYED>
-<!--B1EBC90EB5ECC896E040A8C0280118CA-->  
<DISPLAYLOGIC><![CDATA[@Processed@='Y' & @Totalpaid@!0]]></DISPLAYLOGIC>
+<!--B1EBC90EB5ECC896E040A8C0280118CA-->  
<DISPLAYLOGIC><![CDATA[@Processed@='Y' & @Totalpaid@!0 & 
@Percentageoverdue@!0]]></DISPLAYLOGIC>
 <!--B1EBC90EB5ECC896E040A8C0280118CA-->  
<DISPLAYLENGTH><![CDATA[4]]></DISPLAYLENGTH>
 <!--B1EBC90EB5ECC896E040A8C0280118CA-->  <ISREADONLY><![CDATA[Y]]></ISREADONLY>
 <!--B1EBC90EB5ECC896E040A8C0280118CA-->  <SEQNO><![CDATA[400]]></SEQNO>
@@ -225856,7 +225856,7 @@
 <!--B1EBCACD0EB4E3F4E040A8C028011B08-->  
<IGNOREINWAD><![CDATA[N]]></IGNOREINWAD>
 <!--B1EBCACD0EB4E3F4E040A8C028011B08-->  
<AD_FIELDGROUP_ID><![CDATA[5779017ADB22479BB143E49A8B3A9AE0]]></AD_FIELDGROUP_ID>
 <!--B1EBCACD0EB4E3F4E040A8C028011B08-->  
<ISDISPLAYED><![CDATA[Y]]></ISDISPLAYED>
-<!--B1EBCACD0EB4E3F4E040A8C028011B08-->  
<DISPLAYLOGIC><![CDATA[@Processed@='Y' & @Totalpaid@!0]]></DISPLAYLOGIC>
+<!--B1EBCACD0EB4E3F4E040A8C028011B08-->  
<DISPLAYLOGIC><![CDATA[@Processed@='Y' & @Totalpaid@!0 & 
@Percentageoverdue@!0]]></DISPLAYLOGIC>
 <!--B1EBCACD0EB4E3F4E040A8C028011B08-->  
<DISPLAYLENGTH><![CDATA[4]]></DISPLAYLENGTH>
 <!--B1EBCACD0EB4E3F4E040A8C028011B08-->  <ISREADONLY><![CDATA[Y]]></ISREADONLY>
 <!--B1EBCACD0EB4E3F4E040A8C028011B08-->  <SEQNO><![CDATA[340]]></SEQNO>

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to