[CostAdj] Fixes DateTo implementation for Costing Rule
authorEduardo Argal Guibert <eduardo.argal@openbravo.com>
Thu, 02 Oct 2014 13:04:43 +0200
changeset 24987 b3c81022b811
parent 24986 aff1a556a5e6
child 24988 78440a0c23d0
[CostAdj] Fixes DateTo implementation for Costing Rule

Sets column DateaTo as active, generates the field and modifies process to attend this new column
src-db/database/sourcedata/AD_COLUMN.xml
src-db/database/sourcedata/AD_FIELD.xml
src/org/openbravo/costing/CostingRuleProcess.java
--- a/src-db/database/sourcedata/AD_COLUMN.xml	Wed Oct 01 23:27:27 2014 +0200
+++ b/src-db/database/sourcedata/AD_COLUMN.xml	Thu Oct 02 13:04:43 2014 +0200
@@ -312861,7 +312861,7 @@
 <!--B928FFF6908B0124E040007F01004E03-->  <AD_COLUMN_ID><![CDATA[B928FFF6908B0124E040007F01004E03]]></AD_COLUMN_ID>
 <!--B928FFF6908B0124E040007F01004E03-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
 <!--B928FFF6908B0124E040007F01004E03-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
-<!--B928FFF6908B0124E040007F01004E03-->  <ISACTIVE><![CDATA[N]]></ISACTIVE>
+<!--B928FFF6908B0124E040007F01004E03-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
 <!--B928FFF6908B0124E040007F01004E03-->  <NAME><![CDATA[Ending Date]]></NAME>
 <!--B928FFF6908B0124E040007F01004E03-->  <DESCRIPTION><![CDATA[A parameter stating the ending time range related for a specified request, query, etc.]]></DESCRIPTION>
 <!--B928FFF6908B0124E040007F01004E03-->  <HELP><![CDATA[The Date To indicates the end date of a range (inclusive)]]></HELP>
@@ -312882,7 +312882,7 @@
 <!--B928FFF6908B0124E040007F01004E03-->  <ISSESSIONATTR><![CDATA[N]]></ISSESSIONATTR>
 <!--B928FFF6908B0124E040007F01004E03-->  <ISSECONDARYKEY><![CDATA[N]]></ISSECONDARYKEY>
 <!--B928FFF6908B0124E040007F01004E03-->  <ISDESENCRYPTABLE><![CDATA[N]]></ISDESENCRYPTABLE>
-<!--B928FFF6908B0124E040007F01004E03-->  <DEVELOPMENTSTATUS><![CDATA[NI]]></DEVELOPMENTSTATUS>
+<!--B928FFF6908B0124E040007F01004E03-->  <DEVELOPMENTSTATUS><![CDATA[RE]]></DEVELOPMENTSTATUS>
 <!--B928FFF6908B0124E040007F01004E03-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--B928FFF6908B0124E040007F01004E03-->  <POSITION><![CDATA[13]]></POSITION>
 <!--B928FFF6908B0124E040007F01004E03-->  <ISTRANSIENT><![CDATA[N]]></ISTRANSIENT>
--- a/src-db/database/sourcedata/AD_FIELD.xml	Wed Oct 01 23:27:27 2014 +0200
+++ b/src-db/database/sourcedata/AD_FIELD.xml	Thu Oct 02 13:04:43 2014 +0200
@@ -195754,6 +195754,35 @@
 <!--349D75F6F62E4743BEC635906B0DEF55-->  <EM_OBUIAPP_SHOWSUMMARY><![CDATA[N]]></EM_OBUIAPP_SHOWSUMMARY>
 <!--349D75F6F62E4743BEC635906B0DEF55--></AD_FIELD>
 
+<!--34A0792C6B2D45BFAAB0EABED52680E9--><AD_FIELD>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <AD_FIELD_ID><![CDATA[34A0792C6B2D45BFAAB0EABED52680E9]]></AD_FIELD_ID>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <NAME><![CDATA[Ending Date]]></NAME>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <DESCRIPTION><![CDATA[A parameter stating the ending time range related for a specified request, query, etc.]]></DESCRIPTION>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <HELP><![CDATA[The Date To indicates the end date of a range (inclusive)]]></HELP>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <ISCENTRALLYMAINTAINED><![CDATA[Y]]></ISCENTRALLYMAINTAINED>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <AD_TAB_ID><![CDATA[6868B706DA8340158DE353A6C252A564]]></AD_TAB_ID>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <AD_COLUMN_ID><![CDATA[B928FFF6908B0124E040007F01004E03]]></AD_COLUMN_ID>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <IGNOREINWAD><![CDATA[N]]></IGNOREINWAD>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <ISDISPLAYED><![CDATA[Y]]></ISDISPLAYED>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <DISPLAYLENGTH><![CDATA[19]]></DISPLAYLENGTH>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <ISREADONLY><![CDATA[Y]]></ISREADONLY>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <SEQNO><![CDATA[35]]></SEQNO>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <ISSAMELINE><![CDATA[N]]></ISSAMELINE>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <ISFIELDONLY><![CDATA[N]]></ISFIELDONLY>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <ISENCRYPTED><![CDATA[N]]></ISENCRYPTED>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <SHOWINRELATION><![CDATA[Y]]></SHOWINRELATION>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <ISFIRSTFOCUSEDFIELD><![CDATA[N]]></ISFIRSTFOCUSEDFIELD>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <GRID_SEQNO><![CDATA[30]]></GRID_SEQNO>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <STARTINODDCOLUMN><![CDATA[N]]></STARTINODDCOLUMN>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <STARTNEWLINE><![CDATA[N]]></STARTNEWLINE>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <ISSHOWNINSTATUSBAR><![CDATA[N]]></ISSHOWNINSTATUSBAR>
+<!--34A0792C6B2D45BFAAB0EABED52680E9-->  <EM_OBUIAPP_SHOWSUMMARY><![CDATA[N]]></EM_OBUIAPP_SHOWSUMMARY>
+<!--34A0792C6B2D45BFAAB0EABED52680E9--></AD_FIELD>
+
 <!--34A18501CC664E9CA1B5F1BC050FB18A--><AD_FIELD>
 <!--34A18501CC664E9CA1B5F1BC050FB18A-->  <AD_FIELD_ID><![CDATA[34A18501CC664E9CA1B5F1BC050FB18A]]></AD_FIELD_ID>
 <!--34A18501CC664E9CA1B5F1BC050FB18A-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
@@ -267045,7 +267074,7 @@
 <!--BA3EF92B971D2126E040007F01005946-->  <SHOWINRELATION><![CDATA[Y]]></SHOWINRELATION>
 <!--BA3EF92B971D2126E040007F01005946-->  <ISFIRSTFOCUSEDFIELD><![CDATA[N]]></ISFIRSTFOCUSEDFIELD>
 <!--BA3EF92B971D2126E040007F01005946-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
-<!--BA3EF92B971D2126E040007F01005946-->  <GRID_SEQNO><![CDATA[30]]></GRID_SEQNO>
+<!--BA3EF92B971D2126E040007F01005946-->  <GRID_SEQNO><![CDATA[35]]></GRID_SEQNO>
 <!--BA3EF92B971D2126E040007F01005946-->  <STARTINODDCOLUMN><![CDATA[N]]></STARTINODDCOLUMN>
 <!--BA3EF92B971D2126E040007F01005946-->  <STARTNEWLINE><![CDATA[N]]></STARTNEWLINE>
 <!--BA3EF92B971D2126E040007F01005946-->  <ISSHOWNINSTATUSBAR><![CDATA[Y]]></ISSHOWNINSTATUSBAR>
--- a/src/org/openbravo/costing/CostingRuleProcess.java	Wed Oct 01 23:27:27 2014 +0200
+++ b/src/org/openbravo/costing/CostingRuleProcess.java	Thu Oct 02 13:04:43 2014 +0200
@@ -85,7 +85,8 @@
 
       // Checks
       migrationCheck();
-      boolean existsPreviousRule = existsPreviousRule(rule);
+      CostingRule prevCostingRule = getPreviousRule(rule);
+      boolean existsPreviousRule = prevCostingRule != null;
       boolean existsTransactions = existsTransactions(naturalOrgs, childOrgs);
       if (existsPreviousRule) {
         // Product with costing rule. All trx must be calculated.
@@ -112,6 +113,8 @@
           startingDate = DateUtils.truncate(new Date(), Calendar.SECOND);
           rule.setStartingDate(startingDate);
           log4j.debug("setting starting date " + startingDate);
+          prevCostingRule.setEndingDate(startingDate);
+          OBDal.getInstance().save(prevCostingRule);
           OBDal.getInstance().flush();
         }
         createCostingRuleInits(ruleId, childOrgs, startingDate);
@@ -156,17 +159,19 @@
     }
   }
 
-  private boolean existsPreviousRule(CostingRule rule) {
+  private CostingRule getPreviousRule(CostingRule rule) {
     StringBuffer where = new StringBuffer();
     where.append(" as cr");
     where.append(" where cr." + CostingRule.PROPERTY_ORGANIZATION + " = :ruleOrg");
     where.append("   and cr." + CostingRule.PROPERTY_VALIDATED + " = true");
+    where.append("   order by cr." + CostingRule.PROPERTY_STARTINGDATE + " desc");
 
     OBQuery<CostingRule> crQry = OBDal.getInstance().createQuery(CostingRule.class,
         where.toString());
     crQry.setFilterOnReadableOrganization(false);
     crQry.setNamedParameter("ruleOrg", rule.getOrganization());
-    return crQry.count() > 0;
+    crQry.setMaxResult(1);
+    return (CostingRule) crQry.uniqueResult();
   }
 
   private boolean existsTransactions(Set<String> naturalOrgs, Set<String> childOrgs) {