Fixed bug 27924 be able to enter backdatedfrom while creating a costing rule
authorSandra Huguet <sandra.huguet@openbravo.com>
Wed, 22 Oct 2014 09:04:25 +0200
changeset 25050 80785b0c736e
parent 25049 b3f260eaae0b
child 25051 aea6ec46de6a
Fixed bug 27924 be able to enter backdatedfrom while creating a costing rule
src-db/database/sourcedata/AD_COLUMN.xml
src-db/database/sourcedata/AD_FIELD.xml
src-db/database/sourcedata/AD_MESSAGE.xml
src/org/openbravo/costing/CostingRuleProcess.java
src/org/openbravo/event/CostingRuleEventHandler.java
--- a/src-db/database/sourcedata/AD_COLUMN.xml	Tue Oct 21 18:58:30 2014 +0200
+++ b/src-db/database/sourcedata/AD_COLUMN.xml	Wed Oct 22 09:04:25 2014 +0200
@@ -287056,6 +287056,7 @@
 <!--842AB447B9D1488EA42F3DACA71934F3-->  <ISPARENT><![CDATA[N]]></ISPARENT>
 <!--842AB447B9D1488EA42F3DACA71934F3-->  <ISMANDATORY><![CDATA[N]]></ISMANDATORY>
 <!--842AB447B9D1488EA42F3DACA71934F3-->  <ISUPDATEABLE><![CDATA[Y]]></ISUPDATEABLE>
+<!--842AB447B9D1488EA42F3DACA71934F3-->  <READONLYLOGIC><![CDATA[@isvalidated@='Y']]></READONLYLOGIC>
 <!--842AB447B9D1488EA42F3DACA71934F3-->  <ISIDENTIFIER><![CDATA[N]]></ISIDENTIFIER>
 <!--842AB447B9D1488EA42F3DACA71934F3-->  <SEQNO><![CDATA[200]]></SEQNO>
 <!--842AB447B9D1488EA42F3DACA71934F3-->  <ISTRANSLATED><![CDATA[N]]></ISTRANSLATED>
--- a/src-db/database/sourcedata/AD_FIELD.xml	Tue Oct 21 18:58:30 2014 +0200
+++ b/src-db/database/sourcedata/AD_FIELD.xml	Wed Oct 22 09:04:25 2014 +0200
@@ -197333,7 +197333,7 @@
 <!--39932E6D9F234DE7BE28C602E43BD91D-->  <IGNOREINWAD><![CDATA[N]]></IGNOREINWAD>
 <!--39932E6D9F234DE7BE28C602E43BD91D-->  <ISDISPLAYED><![CDATA[Y]]></ISDISPLAYED>
 <!--39932E6D9F234DE7BE28C602E43BD91D-->  <DISPLAYLENGTH><![CDATA[19]]></DISPLAYLENGTH>
-<!--39932E6D9F234DE7BE28C602E43BD91D-->  <ISREADONLY><![CDATA[Y]]></ISREADONLY>
+<!--39932E6D9F234DE7BE28C602E43BD91D-->  <ISREADONLY><![CDATA[N]]></ISREADONLY>
 <!--39932E6D9F234DE7BE28C602E43BD91D-->  <SEQNO><![CDATA[70]]></SEQNO>
 <!--39932E6D9F234DE7BE28C602E43BD91D-->  <ISSAMELINE><![CDATA[N]]></ISSAMELINE>
 <!--39932E6D9F234DE7BE28C602E43BD91D-->  <ISFIELDONLY><![CDATA[N]]></ISFIELDONLY>
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Tue Oct 21 18:58:30 2014 +0200
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Oct 22 09:04:25 2014 +0200
@@ -18660,6 +18660,18 @@
 <!--452669FBC6B44F90BD0FC29AFF79084B-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--452669FBC6B44F90BD0FC29AFF79084B--></AD_MESSAGE>
 
+<!--45395544AF19476D82D2F38FF529F2BD--><AD_MESSAGE>
+<!--45395544AF19476D82D2F38FF529F2BD-->  <AD_MESSAGE_ID><![CDATA[45395544AF19476D82D2F38FF529F2BD]]></AD_MESSAGE_ID>
+<!--45395544AF19476D82D2F38FF529F2BD-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--45395544AF19476D82D2F38FF529F2BD-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--45395544AF19476D82D2F38FF529F2BD-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--45395544AF19476D82D2F38FF529F2BD-->  <VALUE><![CDATA[WrongFixBackdatedFrom]]></VALUE>
+<!--45395544AF19476D82D2F38FF529F2BD-->  <MSGTEXT><![CDATA[Fix backdated from date you have entered is invalid  because it is part of a period closed, please ensure that date entered is not part of a period closed. ]]></MSGTEXT>
+<!--45395544AF19476D82D2F38FF529F2BD-->  <MSGTYPE><![CDATA[W]]></MSGTYPE>
+<!--45395544AF19476D82D2F38FF529F2BD-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--45395544AF19476D82D2F38FF529F2BD-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--45395544AF19476D82D2F38FF529F2BD--></AD_MESSAGE>
+
 <!--4572743978F94C65969984E770B89172--><AD_MESSAGE>
 <!--4572743978F94C65969984E770B89172-->  <AD_MESSAGE_ID><![CDATA[4572743978F94C65969984E770B89172]]></AD_MESSAGE_ID>
 <!--4572743978F94C65969984E770B89172-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src/org/openbravo/costing/CostingRuleProcess.java	Tue Oct 21 18:58:30 2014 +0200
+++ b/src/org/openbravo/costing/CostingRuleProcess.java	Wed Oct 22 09:04:25 2014 +0200
@@ -118,7 +118,9 @@
           OBDal.getInstance().flush();
         }
         createCostingRuleInits(ruleId, childOrgs, startingDate);
-        rule.setFixbackdatedfrom(startingDate);
+        if (rule.getFixbackdatedfrom() == null) {
+          rule.setFixbackdatedfrom(startingDate);
+        }
 
         // Update cost of inventories and process starting physical inventories.
         updateInventoriesCostAndProcessInitInventories(ruleId, startingDate, existsPreviousRule);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/event/CostingRuleEventHandler.java	Wed Oct 22 09:04:25 2014 +0200
@@ -0,0 +1,112 @@
+/*
+ *************************************************************************
+ * 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) 2013-2014 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+package org.openbravo.event;
+
+import java.text.ParseException;
+import java.util.Date;
+
+import javax.enterprise.event.Observes;
+
+import org.apache.log4j.Logger;
+import org.hibernate.HibernateException;
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
+import org.openbravo.base.model.Property;
+import org.openbravo.client.kernel.event.EntityNewEvent;
+import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
+import org.openbravo.client.kernel.event.EntityUpdateEvent;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.erpCommon.utility.OBDateUtils;
+import org.openbravo.erpCommon.utility.OBMessageUtils;
+import org.openbravo.model.materialmgmt.cost.CostingRule;
+
+public class CostingRuleEventHandler extends EntityPersistenceEventObserver {
+  private static Entity[] entities = { ModelProvider.getInstance().getEntity(
+      CostingRule.ENTITY_NAME) };
+  protected Logger logger = Logger.getLogger(this.getClass());
+
+  @Override
+  protected Entity[] getObservedEntities() {
+    return entities;
+  }
+
+  public void onSave(@Observes EntityUpdateEvent event) {
+    if (!isValidEvent(event)) {
+      return;
+    }
+    final Entity costingRule = ModelProvider.getInstance().getEntity(CostingRule.ENTITY_NAME);
+    final Property genericProperty = costingRule
+        .getProperty(CostingRule.PROPERTY_BACKDATEDTRANSACTIONSFIXED);
+    Boolean isbackdatedtransaction = (Boolean) event.getCurrentState(genericProperty);
+    CostingRule rule = (CostingRule) event.getTargetInstance();
+    checkFixBackdatedFrom(isbackdatedtransaction, rule);
+  }
+
+  public void onUpdate(@Observes EntityUpdateEvent event) {
+    if (!isValidEvent(event)) {
+      return;
+    }
+    final Entity costingRule = ModelProvider.getInstance().getEntity(CostingRule.ENTITY_NAME);
+    final Property genericProperty = costingRule
+        .getProperty(CostingRule.PROPERTY_BACKDATEDTRANSACTIONSFIXED);
+    Boolean isbackdatedtransaction = (Boolean) event.getCurrentState(genericProperty);
+    CostingRule rule = (CostingRule) event.getTargetInstance();
+    checkFixBackdatedFrom(isbackdatedtransaction, rule);
+  }
+
+  public void onNew(@Observes EntityNewEvent event) {
+    if (!isValidEvent(event)) {
+      return;
+    }
+    final Entity costingRule = ModelProvider.getInstance().getEntity(CostingRule.ENTITY_NAME);
+    final Property genericProperty = costingRule
+        .getProperty(CostingRule.PROPERTY_BACKDATEDTRANSACTIONSFIXED);
+    Boolean isbackdatedtransaction = (Boolean) event.getCurrentState(genericProperty);
+    CostingRule rule = (CostingRule) event.getTargetInstance();
+    checkFixBackdatedFrom(isbackdatedtransaction, rule);
+  }
+
+  private void checkFixBackdatedFrom(Boolean isbackdatedtransaction, CostingRule rule) {
+    StringBuilder hql = new StringBuilder();
+    final Session session = OBDal.getInstance().getSession();
+    hql.append("select min(p.startingDate)  from FinancialMgmtPeriodControl pc"
+        + " inner join  pc.period p" + " where  periodstatus='O' " + " and p.client= :client"
+        + " and p.organization= :org ");
+
+    final Query query = session.createQuery(hql.toString());
+    query.setParameter("client", rule.getClient());
+    query.setParameter("org", rule.getOrganization());
+    query.uniqueResult();
+
+    try {
+      if (isbackdatedtransaction
+          && (rule.getFixbackdatedfrom()).before(OBDateUtils.getDate(OBDateUtils
+              .formatDate((Date) query.uniqueResult())))) {
+        throw new OBException(OBMessageUtils.messageBD("WrongFixBackdatedFrom"));
+      }
+    } catch (HibernateException e) {
+      logger.error("Error executing process", e);
+    } catch (ParseException e) {
+      logger.error("Error executing process", e);
+    }
+  }
+}
\ No newline at end of file