details:   https://code.openbravo.com/erp/devel/pi/rev/3f4dc99dd5be
changeset: 30354:3f4dc99dd5be
user:      Alvaro Ferraz <alvaro.ferraz <at> openbravo.com>
date:      Fri Sep 30 14:35:09 2016 +0200
summary:   Related to issue 33954: Fix trigger in Oracle

Event handler has been created in order to update subsequent reconciliations 
balances when a reconciliation is deleted, instead of doing it in 
FIN_Reconciliation trigger.

diffstat:

 
modules/org.openbravo.advpaymentmngt/src-db/database/model/triggers/APRM_FIN_RECON_CHECK_TRG.xml
                 |  16 +-
 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/event/FIN_ReconciliationEventListener.java
 |  77 ++++++++++
 2 files changed, 78 insertions(+), 15 deletions(-)

diffs (119 lines):

diff -r e0f24c050e4a -r 3f4dc99dd5be 
modules/org.openbravo.advpaymentmngt/src-db/database/model/triggers/APRM_FIN_RECON_CHECK_TRG.xml
--- 
a/modules/org.openbravo.advpaymentmngt/src-db/database/model/triggers/APRM_FIN_RECON_CHECK_TRG.xml
  Fri Sep 30 14:12:43 2016 +0200
+++ 
b/modules/org.openbravo.advpaymentmngt/src-db/database/model/triggers/APRM_FIN_RECON_CHECK_TRG.xml
  Fri Sep 30 14:35:09 2016 +0200
@@ -15,13 +15,12 @@
 * 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-2016 Openbravo SLU
+* All portions are Copyright (C) 2010 Openbravo SLU
 * All Rights Reserved.
 * Contributor(s):  ______________________________________.
 *************************************************************************/
 
 v_DateNull DATE := TO_DATE('01-01-1900','DD-MM-YYYY');
-v_balance NUMBER;
   
 BEGIN
    
@@ -48,19 +47,6 @@
    IF(:OLD.PROCESSED='Y') THEN
      RAISE_APPLICATION_ERROR(-20000, '@20501@');
    END IF;
-
-   --Is necessary update starting and ending balances of subsequents 
reconciliations
-   SELECT endingbalance - startingbalance
-   INTO v_balance
-   FROM fin_reconciliation
-   WHERE fin_reconciliation_id = :old.fin_reconciliation_id;
-
-   UPDATE fin_reconciliation
-   SET startingbalance = startingbalance - v_balance,
-   endingbalance = endingbalance - v_balance
-   WHERE fin_financial_account_id = :old.fin_financial_account_id
-   AND statementdate > :old.statementdate;
-
   END IF;
 END APRM_FIN_RECON_CHECK_TRG
 ]]></body>
diff -r e0f24c050e4a -r 3f4dc99dd5be 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/event/FIN_ReconciliationEventListener.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ 
b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/event/FIN_ReconciliationEventListener.java
  Fri Sep 30 14:35:09 2016 +0200
@@ -0,0 +1,77 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2016 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+package org.openbravo.advpaymentmngt.event;
+
+import java.math.BigDecimal;
+
+import javax.enterprise.event.Observes;
+
+import org.hibernate.Query;
+import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
+import org.openbravo.client.kernel.event.EntityDeleteEvent;
+import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.financialmgmt.payment.FIN_Reconciliation;
+
+public class FIN_ReconciliationEventListener extends 
EntityPersistenceEventObserver {
+  private static Entity[] entities = { ModelProvider.getInstance().getEntity(
+      FIN_Reconciliation.ENTITY_NAME) };
+
+  @Override
+  protected Entity[] getObservedEntities() {
+    return entities;
+  }
+
+  public void onDelete(@Observes
+  EntityDeleteEvent event) {
+    if (!isValidEvent(event)) {
+      return;
+    }
+    FIN_Reconciliation rec = OBDal.getInstance().get(FIN_Reconciliation.class,
+        event.getTargetInstance().getId());
+    if (!rec.isProcessed()) {
+      updateNextReconciliationsBalance(rec);
+    }
+  }
+
+  /**
+   * Update starting balance and ending balance of subsequent reconciliations 
when one
+   * reconciliation is deleted
+   * 
+   * @param rec
+   *          Reconciliation being deleted
+   */
+  private void updateNextReconciliationsBalance(final FIN_Reconciliation rec) {
+    BigDecimal balance = 
rec.getEndingBalance().subtract(rec.getStartingbalance());
+    StringBuffer update = new StringBuffer();
+    update.append(" update " + FIN_Reconciliation.ENTITY_NAME);
+    update.append(" set " + FIN_Reconciliation.PROPERTY_STARTINGBALANCE + " = "
+        + FIN_Reconciliation.PROPERTY_STARTINGBALANCE + " - :balance");
+    update.append(" , " + FIN_Reconciliation.PROPERTY_ENDINGBALANCE + " = "
+        + FIN_Reconciliation.PROPERTY_ENDINGBALANCE + " - :balance");
+    update.append(" where " + FIN_Reconciliation.PROPERTY_ACCOUNT + ".id = 
:accountId");
+    update.append(" and " + FIN_Reconciliation.PROPERTY_TRANSACTIONDATE + " > 
:date");
+    Query updateQry = 
OBDal.getInstance().getSession().createQuery(update.toString());
+    updateQry.setBigDecimal("balance", balance);
+    updateQry.setString("accountId", rec.getAccount().getId());
+    updateQry.setTimestamp("date", rec.getTransactionDate());
+    updateQry.executeUpdate();
+  }
+}

------------------------------------------------------------------------------
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to