Fixes Issue 29110:It is not possible to change currency in header tab in G/L
authorAtul Gaware <atul.gaware@openbravo.com>
Wed, 11 Mar 2015 11:48:22 +0530
changeset 26164 bb7b3752a14d
parent 26163 5a9f94c18c9b
child 26165 ac9670b7bfdd
Fixes Issue 29110:It is not possible to change currency in header tab in G/L
Journal

As there is trigger mutating error, updation of gl journal line currency and
currency rate is moved to a event handler from gl_journal_trg trigger.
src-db/database/model/triggers/GL_JOURNAL_TRG.xml
src/org/openbravo/event/GLJournalEventHandler.java
--- a/src-db/database/model/triggers/GL_JOURNAL_TRG.xml	Wed Mar 11 15:19:41 2015 +0000
+++ b/src-db/database/model/triggers/GL_JOURNAL_TRG.xml	Wed Mar 11 11:48:22 2015 +0530
@@ -17,7 +17,7 @@
     * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
     * All Rights Reserved.
     * Contributor(s): Openbravo SLU
-    * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.U.
+    * Contributions are Copyright (C) 2001-2015 Openbravo, S.L.U.
     *
     * Specifically, this derivative work is based upon the following Compiere
     * file and version.
@@ -34,12 +34,6 @@
 
     -- Subtract Old Amount
     IF(UPDATING) THEN
-      IF (:old.c_currency_id <> :new.c_currency_id) THEN
-        UPDATE gl_journalline SET c_currency_id = :new.c_currency_id where gl_journal_id = :old.gl_journal_id;
-      END IF;
-      IF (:old.currencyrate <> :new.currencyrate) THEN
-        UPDATE gl_journalline SET currencyrate = :new.currencyrate where gl_journal_id = :old.gl_journal_id;
-      END IF;
       IF(:old.TotalDr <> :NEW.TotalDr OR :OLD.TotalCr <> :NEW.TotalCr) THEN  
       IF(:old.GL_JournalBatch_ID IS NOT NULL AND :old.IsActive='Y') THEN
 	  UPDATE GL_JournalBatch
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/event/GLJournalEventHandler.java	Wed Mar 11 11:48:22 2015 +0530
@@ -0,0 +1,80 @@
+/*
+ *************************************************************************
+ * 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) 2015 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+package org.openbravo.event;
+
+import javax.enterprise.event.Observes;
+
+import org.apache.log4j.Logger;
+import org.hibernate.ScrollMode;
+import org.hibernate.ScrollableResults;
+import org.hibernate.criterion.Restrictions;
+import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
+import org.openbravo.base.model.Property;
+import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
+import org.openbravo.client.kernel.event.EntityUpdateEvent;
+import org.openbravo.dal.service.OBCriteria;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.financialmgmt.gl.GLJournal;
+import org.openbravo.model.financialmgmt.gl.GLJournalLine;
+
+public class GLJournalEventHandler extends EntityPersistenceEventObserver {
+  protected Logger logger = Logger.getLogger(this.getClass());
+  private static Entity[] entities = { ModelProvider.getInstance().getEntity(GLJournal.ENTITY_NAME) };
+
+  @Override
+  protected Entity[] getObservedEntities() {
+    return entities;
+  }
+
+  public void onUpdate(@Observes EntityUpdateEvent event) {
+    if (!isValidEvent(event)) {
+      return;
+    }
+    final GLJournal glj = (GLJournal) event.getTargetInstance();
+    // Update GLJournalLine with updated Currency and Currency Rate.
+    final Entity gljournal = ModelProvider.getInstance().getEntity(GLJournal.ENTITY_NAME);
+    final Property currencyProperty = gljournal.getProperty(GLJournal.PROPERTY_CURRENCY);
+    final Property currencyRate = gljournal.getProperty(GLJournal.PROPERTY_RATE);
+    if (!event.getCurrentState(currencyProperty).equals(event.getPreviousState(currencyProperty))
+        || !event.getCurrentState(currencyRate).equals(event.getPreviousState(currencyRate))) {
+      OBCriteria<GLJournalLine> gljournallineCriteria = OBDal.getInstance().createCriteria(
+          GLJournalLine.class);
+      gljournallineCriteria.add(Restrictions.eq(GLJournalLine.PROPERTY_JOURNALENTRY, glj));
+      ScrollableResults scrollLines = gljournallineCriteria.scroll(ScrollMode.FORWARD_ONLY);
+      if (gljournallineCriteria.count() > 0) {
+        try {
+          while (scrollLines.next()) {
+            final GLJournalLine journalLine = (GLJournalLine) scrollLines.get()[0];
+            if (!glj.getCurrency().getId().equals(journalLine.getCurrency().getId())) {
+              journalLine.setCurrency(glj.getCurrency());
+              OBDal.getInstance().save(journalLine);
+            }
+            if (!glj.getRate().equals(journalLine.getRate())) {
+              journalLine.setRate(glj.getRate());
+              OBDal.getInstance().save(journalLine);
+            }
+          }
+        } finally {
+          scrollLines.close();
+        }
+      }
+    }
+  }
+}
\ No newline at end of file