Fixed issue 0032044: Ensure stable ids in docs send from WebPOS to the server (Payment API)
authorMiguel de Juana <miguel.dejuana@openbravo.com>
Thu, 18 Feb 2016 13:23:15 +0100
changeset 28686 6f297f8de5d6
parent 28685 975b020c2587
child 28687 8c53c9e9c26f
Fixed issue 0032044: Ensure stable ids in docs send from WebPOS to the server (Payment API)

- Set some entities ids related to order loading in Web POS to ensure stable ids
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_AddPayment.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java	Tue Jan 26 17:24:27 2016 +0100
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java	Thu Feb 18 13:23:15 2016 +0100
@@ -486,7 +486,21 @@
       FIN_PaymentMethod paymentMethod, FIN_FinancialAccount finAccount, String strPaymentAmount,
       Date paymentDate, String referenceNo, Currency paymentCurrency, BigDecimal finTxnConvertRate,
       BigDecimal finTxnAmount) {
+    return getNewPayment(isReceipt, organization, docType, strPaymentDocumentNo, businessPartner,
+        paymentMethod, finAccount, strPaymentAmount, paymentDate, referenceNo, paymentCurrency,
+        finTxnConvertRate, finTxnAmount, null);
+  }
+
+  public FIN_Payment getNewPayment(boolean isReceipt, Organization organization,
+      DocumentType docType, String strPaymentDocumentNo, BusinessPartner businessPartner,
+      FIN_PaymentMethod paymentMethod, FIN_FinancialAccount finAccount, String strPaymentAmount,
+      Date paymentDate, String referenceNo, Currency paymentCurrency, BigDecimal finTxnConvertRate,
+      BigDecimal finTxnAmount, String paymentId) {
     final FIN_Payment newPayment = OBProvider.getInstance().get(FIN_Payment.class);
+    if (paymentId != null) {
+      newPayment.setId(paymentId);
+      newPayment.setNewOBObject(true);
+    }
     newPayment.setReceipt(isReceipt);
     newPayment.setDocumentType(docType);
     newPayment.setDocumentNo(strPaymentDocumentNo);
@@ -529,16 +543,20 @@
       FIN_PaymentScheduleDetail paymentScheduleDetail, BigDecimal paymentDetailAmount,
       BigDecimal writeoffAmount, boolean isRefund, GLItem glitem) {
     return getNewPaymentDetail(payment, paymentScheduleDetail, paymentDetailAmount, writeoffAmount,
-        isRefund, glitem, true);
+        isRefund, glitem, true, null);
   }
 
   public FIN_PaymentDetail getNewPaymentDetail(FIN_Payment payment,
       FIN_PaymentScheduleDetail paymentScheduleDetail, BigDecimal paymentDetailAmount,
-      BigDecimal writeoffAmount, boolean isRefund, GLItem glitem, boolean doFlush) {
+      BigDecimal writeoffAmount, boolean isRefund, GLItem glitem, boolean doFlush, String paymentId) {
     try {
       OBContext.setAdminMode(true);
       final FIN_PaymentDetail newPaymentDetail = OBProvider.getInstance().get(
           FIN_PaymentDetail.class);
+      if (paymentId != null) {
+        newPaymentDetail.setId(paymentId);
+        newPaymentDetail.setNewOBObject(true);
+      }
       List<FIN_PaymentDetail> paymentDetails = payment.getFINPaymentDetailList();
       newPaymentDetail.setFinPayment(payment);
       newPaymentDetail.setOrganization(payment.getOrganization());
@@ -584,9 +602,13 @@
   }
 
   public FIN_PaymentScheduleDetail getNewPaymentScheduleDetail(Organization organization,
-      BigDecimal amount) {
+      BigDecimal amount, String paymentId) {
     final FIN_PaymentScheduleDetail newPaymentScheduleDetail = OBProvider.getInstance().get(
         FIN_PaymentScheduleDetail.class);
+    if (paymentId != null) {
+      newPaymentScheduleDetail.setId(paymentId);
+      newPaymentScheduleDetail.setNewOBObject(true);
+    }
     newPaymentScheduleDetail.setOrganization(organization);
     // As '0' is not a valid organization for transactions we can assume that organization client is
     // transaction client
@@ -599,6 +621,12 @@
     return newPaymentScheduleDetail;
   }
 
+  public FIN_PaymentScheduleDetail getNewPaymentScheduleDetail(Organization organization,
+      BigDecimal amount) {
+
+    return getNewPaymentScheduleDetail(organization, amount, null);
+  }
+
   /**
    * Returns a new FIN_PaymentScheduleDetail for the given accounting dimensions
    * 
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java	Tue Jan 26 17:24:27 2016 +0100
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java	Thu Feb 18 13:23:15 2016 +0100
@@ -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-2013 Openbravo SLU
+ * All portions are Copyright (C) 2010-2016 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -98,6 +98,8 @@
         FIN_FinaccTransaction.class);
     OBContext.setAdminMode();
     try {
+      newTransaction.setId(payment.getId());
+      newTransaction.setNewOBObject(true);
       newTransaction.setFinPayment(payment);
       newTransaction.setOrganization(payment.getOrganization());
       newTransaction.setAccount(payment.getAccount());
@@ -110,10 +112,12 @@
       newTransaction.setNdDimension(payment.getNdDimension());
       newTransaction.setCurrency(payment.getAccount().getCurrency());
       String desc = "";
-      if(payment.getDescription()!=null && !payment.getDescription().isEmpty()){
-    	  desc = payment.getDescription()
-    	          .replace("\n", ". ")
-    	          .substring(0, payment.getDescription().length() > 254 ? 254 : payment.getDescription().length());
+      if (payment.getDescription() != null && !payment.getDescription().isEmpty()) {
+        desc = payment
+            .getDescription()
+            .replace("\n", ". ")
+            .substring(0,
+                payment.getDescription().length() > 254 ? 254 : payment.getDescription().length());
       }
       newTransaction.setDescription(desc);
       newTransaction.setClient(payment.getClient());
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_AddPayment.java	Tue Jan 26 17:24:27 2016 +0100
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_AddPayment.java	Thu Feb 18 13:23:15 2016 +0100
@@ -135,6 +135,8 @@
    *          Amount of payment in currency of financial account
    * @param doFlush
    *          Force to flush inside the method after creating the payment
+   * @param paymentId
+   *          id to set in new entities
    * @return The FIN_Payment OBObject containing all the Payment Details.
    */
   public static FIN_Payment savePayment(FIN_Payment _payment, boolean isReceipt,
@@ -144,7 +146,7 @@
       List<FIN_PaymentScheduleDetail> selectedPaymentScheduleDetails,
       HashMap<String, BigDecimal> selectedPaymentScheduleDetailsAmounts, boolean isWriteoff,
       boolean isRefund, Currency paymentCurrency, BigDecimal finTxnConvertRate,
-      BigDecimal finTxnAmount, boolean doFlush) {
+      BigDecimal finTxnAmount, boolean doFlush, String paymentId) {
     dao = new AdvPaymentMngtDao();
 
     BigDecimal assignedAmount = BigDecimal.ZERO;
@@ -154,7 +156,7 @@
     } else {
       payment = dao.getNewPayment(isReceipt, organization, docType, strPaymentDocumentNo,
           businessPartner, paymentMethod, finAccount, strPaymentAmount, paymentDate, referenceNo,
-          paymentCurrency, finTxnConvertRate, finTxnAmount);
+          paymentCurrency, finTxnConvertRate, finTxnAmount, paymentId);
       if (doFlush) {
         try {
           OBDal.getInstance().flush();
@@ -184,9 +186,10 @@
       // TODO: Review this condition !=0??
       if (assignedAmount.compareTo(payment.getAmount()) == -1) {
         FIN_PaymentScheduleDetail refundScheduleDetail = dao.getNewPaymentScheduleDetail(
-            payment.getOrganization(), payment.getAmount().subtract(assignedAmount));
+            payment.getOrganization(), payment.getAmount().subtract(assignedAmount), paymentId);
         dao.getNewPaymentDetail(payment, refundScheduleDetail,
-            payment.getAmount().subtract(assignedAmount), BigDecimal.ZERO, false, null);
+            payment.getAmount().subtract(assignedAmount), BigDecimal.ZERO, false, null, true,
+            paymentId);
       }
     } catch (final Exception e) {
       e.printStackTrace(System.err);
@@ -260,7 +263,7 @@
     return savePayment(_payment, isReceipt, docType, strPaymentDocumentNo, businessPartner,
         paymentMethod, finAccount, strPaymentAmount, paymentDate, organization, referenceNo,
         selectedPaymentScheduleDetails, selectedPaymentScheduleDetailsAmounts, isWriteoff,
-        isRefund, paymentCurrency, finTxnConvertRate, finTxnAmount, true);
+        isRefund, paymentCurrency, finTxnConvertRate, finTxnAmount, true, null);
   }
 
   /*
@@ -276,7 +279,7 @@
     return savePayment(_payment, isReceipt, docType, strPaymentDocumentNo, businessPartner,
         paymentMethod, finAccount, strPaymentAmount, paymentDate, organization, referenceNo,
         selectedPaymentScheduleDetails, selectedPaymentScheduleDetailsAmounts, isWriteoff,
-        isRefund, null, null, null, true);
+        isRefund, null, null, null, true, null);
   }
 
   /**
@@ -334,7 +337,7 @@
     return savePayment(_payment, isReceipt, docType, strPaymentDocumentNo, businessPartner,
         paymentMethod, finAccount, strPaymentAmount, paymentDate, organization, referenceNo,
         selectedPaymentScheduleDetails, selectedPaymentScheduleDetailsAmounts, isWriteoff,
-        isRefund, null, null, null, doFlush);
+        isRefund, null, null, null, doFlush, null);
   }
 
   /**
@@ -520,7 +523,7 @@
         OBDal.getInstance().save(paymentScheduleDetail);
       }
       dao.getNewPaymentDetail(payment, paymentScheduleDetail, paymentDetailAmount,
-          amountDifference, false, null, doFlush);
+          amountDifference, false, null, doFlush, null);
     }
     return assignedAmount;
   }
@@ -657,17 +660,20 @@
    *          Amount of the new Payment Detail.
    * @param glitem
    *          GLItem to be set in the new Payment Detail.
+   * @param paymentId
+   *          id to set in new entities
    */
-  public static void saveGLItem(FIN_Payment payment, BigDecimal glitemAmount, GLItem glitem) {
+  public static void saveGLItem(FIN_Payment payment, BigDecimal glitemAmount, GLItem glitem,
+      String paymentId) {
     // FIXME: added to access the FIN_PaymentSchedule and FIN_PaymentScheduleDetail tables to be
     // removed when new security implementation is done
     dao = new AdvPaymentMngtDao();
     OBContext.setAdminMode();
     try {
       FIN_PaymentScheduleDetail psd = dao.getNewPaymentScheduleDetail(payment.getOrganization(),
-          glitemAmount);
+          glitemAmount, paymentId);
       FIN_PaymentDetail pd = dao.getNewPaymentDetail(payment, psd, glitemAmount, BigDecimal.ZERO,
-          false, glitem);
+          false, glitem, true, paymentId);
       pd.setFinPayment(payment);
       OBDal.getInstance().save(pd);
       OBDal.getInstance().save(payment);
@@ -676,6 +682,10 @@
     }
   }
 
+  public static void saveGLItem(FIN_Payment payment, BigDecimal glitemAmount, GLItem glitem) {
+    saveGLItem(payment, glitemAmount, glitem, null);
+  }
+
   /**
    * It adds to the Payment a new Payment Detail with the given GL Item, amount and accounting
    * dimensions