Related to Issue 27395. Compatibility issues with oracle. Reset on cur change
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Wed, 17 Dec 2014 19:06:50 +0100
changeset 25725 87b0a4144cc7
parent 25724 6f9cc94e865d
child 25726 401f1e9fc80d
Related to Issue 27395. Compatibility issues with oracle. Reset on cur change

- When the currency of a legal entity is changed the unit costs are deleted
and a message is prompt to the user to recalculate them again.
- Several performance issues in triggers and procedures.
- Modified organization selector to remove HQL query.
modules/org.openbravo.client.application/src-db/database/sourcedata/AD_MESSAGE.xml
modules/org.openbravo.client.application/src/org/openbravo/client/application/ApplicationComponentProvider.java
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-onchange-functions.js
src-db/database/model/functions/M_INITIALIZE_STOCK_VALUATION.xml
src-db/database/model/functions/M_UPDATE_STOCK_VALUATION.xml
src-db/database/model/triggers/AD_ORG_TRG.xml
src-db/database/model/triggers/M_PRODUCT_TRG.xml
src-db/database/model/triggers/M_TRANSACTION_COST_TRG.xml
src-db/database/model/triggers/M_TRANSACTION_TRG.xml
src-db/database/sourcedata/AD_FIELD.xml
src-db/database/sourcedata/OBUISEL_SELECTOR.xml
src-db/database/sourcedata/OBUISEL_SELECTOR_FIELD.xml
src/org/openbravo/costing/ResetStockValuation.java
--- a/modules/org.openbravo.client.application/src-db/database/sourcedata/AD_MESSAGE.xml	Tue Nov 11 17:32:58 2014 +0530
+++ b/modules/org.openbravo.client.application/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Dec 17 19:06:50 2014 +0100
@@ -348,6 +348,20 @@
 <!--20EBBDD0DE864992AD7B8090D1126A06-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--20EBBDD0DE864992AD7B8090D1126A06--></AD_MESSAGE>
 
+<!--2588E18549E74C79BD24F6717AF1DD8B--><AD_MESSAGE>
+<!--2588E18549E74C79BD24F6717AF1DD8B-->  <AD_MESSAGE_ID><![CDATA[2588E18549E74C79BD24F6717AF1DD8B]]></AD_MESSAGE_ID>
+<!--2588E18549E74C79BD24F6717AF1DD8B-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--2588E18549E74C79BD24F6717AF1DD8B-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--2588E18549E74C79BD24F6717AF1DD8B-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--2588E18549E74C79BD24F6717AF1DD8B-->  <VALUE><![CDATA[OBUIAPP_OrgCurrencyChange]]></VALUE>
+<!--2588E18549E74C79BD24F6717AF1DD8B-->  <MSGTEXT><![CDATA[Changing the Currency of the Organization requires to recalculate the Unit Costs of the product.
+
+When saving the current unit costs will be deleted if the currency is changed. Please execute the Reset Unit Costs process afterwards to recalculate them using the new Currency.]]></MSGTEXT>
+<!--2588E18549E74C79BD24F6717AF1DD8B-->  <MSGTYPE><![CDATA[W]]></MSGTYPE>
+<!--2588E18549E74C79BD24F6717AF1DD8B-->  <AD_MODULE_ID><![CDATA[9BA0836A3CD74EE4AB48753A47211BCC]]></AD_MODULE_ID>
+<!--2588E18549E74C79BD24F6717AF1DD8B-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--2588E18549E74C79BD24F6717AF1DD8B--></AD_MESSAGE>
+
 <!--26DBE659B9B746CF996AC9F4A6475BC3--><AD_MESSAGE>
 <!--26DBE659B9B746CF996AC9F4A6475BC3-->  <AD_MESSAGE_ID><![CDATA[26DBE659B9B746CF996AC9F4A6475BC3]]></AD_MESSAGE_ID>
 <!--26DBE659B9B746CF996AC9F4A6475BC3-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/ApplicationComponentProvider.java	Tue Nov 11 17:32:58 2014 +0530
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/ApplicationComponentProvider.java	Wed Dec 17 19:06:50 2014 +0100
@@ -246,6 +246,8 @@
         "web/org.openbravo.client.application/js/utilities/ob-section-stack.js", false));
     globalResources.add(createStaticResource(
         "web/org.openbravo.client.application/js/toolbar/ob-clone-order.js", false));
+    globalResources.add(createStaticResource(
+        "web/org.openbravo.client.application/js/utilities/ob-onchange-functions.js", false));
 
     // Alert Management
     globalResources.add(createStaticResource(
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-onchange-functions.js	Wed Dec 17 19:06:50 2014 +0100
@@ -0,0 +1,34 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (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) 2014 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+// = On Change Functions =
+//
+// Contains on change functions that are defined in the fields.
+
+OB = window.OB || {};
+OB.OnChange = window.OB.OnChange || {};
+
+
+//** {{{OB.OnChange.organizationCurrency}}} **
+// Used in the 'Organization' window, in the 'Currency' onchange field
+// It shows a warning dialog when currency is changed
+OB.OnChange.organizationCurrency = function (item, view, form, grid) {
+  if (view && view.messageBar) {
+    view.messageBar.setMessage('warning', null, OB.I18N.getLabel('OBUIAPP_OrgCurrencyChange'));
+  }
+};
\ No newline at end of file
--- a/src-db/database/model/functions/M_INITIALIZE_STOCK_VALUATION.xml	Tue Nov 11 17:32:58 2014 +0530
+++ b/src-db/database/model/functions/M_INITIALIZE_STOCK_VALUATION.xml	Wed Dec 17 19:06:50 2014 +0100
@@ -29,91 +29,67 @@
   
   -- Parameter
   TYPE RECORD IS REF CURSOR;
-  Cur_legal_entities RECORD;
-  Cur_products RECORD;
-  Cur_values RECORD;
+  Cur_legal_entities      RECORD;
+  Cur_products            RECORD;
+  Cur_values              RECORD;
   
   -- Parameter Variables
-  v_currency_ID    				VARCHAR2(32);
+  v_currency_ID           VARCHAR2(32);
+  v_costprecission        NUMBER;
 
 BEGIN
 
   FOR Cur_legal_entities IN (
- 
-    SELECT ad_org_id, warehouse_dimension
-    FROM m_costing_rule cr
-    WHERE isvalidated = 'Y'
-      AND COALESCE(datefrom, TO_DATE('01-01-1900', 'DD-MM-YYYY')) <= now()
-      AND COALESCE(dateto, TO_DATE('01-01-9999', 'DD-MM-YYYY')) >= now()
-      AND ad_client_id = p_client
-      AND (p_org IS NULL OR ad_org_id IN (SELECT ad_org_id
-                                          FROM ad_org
-                                          WHERE (ad_isorgincluded(ad_org_id, p_org, p_client) <> -1
-                                          OR ad_isorgincluded(p_org, ad_org_id, p_client) <> -1
-                                          OR ad_org_id = '0'))
-      AND ad_client_id = p_client)
+      SELECT ad_org_id, warehouse_dimension
+      FROM m_costing_rule cr
+      WHERE isvalidated = 'Y'
+        AND (datefrom IS NULL OR  datefrom <= now())
+        AND (dateto IS NULL OR dateto >= now())
+        AND ad_client_id = p_client
+        AND (p_org IS NULL OR AD_ORG_ISINNATURALTREE(ad_org_id, p_org, p_client) ='Y')
+    ) LOOP
 
-  ) LOOP
-    
-    SELECT c_currency_id
-    INTO v_currency_ID
-    FROM ad_org
-    WHERE ad_org_id = Cur_legal_entities.ad_org_id;
+    SELECT COALESCE(o.c_currency_id, c.c_currency_id), c_get_currency_precision(COALESCE(o.c_currency_id, c.c_currency_id), 'C')
+      INTO v_currency_ID, v_costprecission
+    FROM ad_org o
+      JOIN ad_client c ON o.ad_client_id = c.ad_client_id
+    WHERE o.ad_org_id = Cur_legal_entities.ad_org_id;
 
     FOR Cur_products IN (
-       SELECT m_product_id
-       FROM m_product
-       WHERE ad_org_id IN (SELECT ad_org_id
-                           FROM ad_org
-                           WHERE (ad_isorgincluded(ad_org_id, cur_legal_entities.ad_org_id, p_client) <> -1
-                           OR ad_isorgincluded(cur_legal_entities.ad_org_id, ad_org_id, p_client) <> -1
-                           OR ad_org_id = '0'))
-       AND ad_client_id = p_client
-       AND isstocked = 'Y'
-       AND producttype = 'I'
-       AND (p_product IS NULL OR m_product_id = p_product)
-       
-    ) LOOP
-    
-      DBMS_OUTPUT.PUT_LINE( 'product' || cur_products.m_product_id);
-    
+        SELECT m_product_id
+        FROM m_product
+        WHERE AD_ORG_ISINNATURALTREE(ad_org_id, p_org, p_client) ='Y'
+          AND ad_client_id = p_client
+          AND isstocked = 'Y'
+          AND producttype = 'I'
+          AND (p_product IS NULL OR m_product_id = p_product)
+      ) LOOP
       FOR Cur_values IN (
-      
-        SELECT COALESCE(SUM(aa.qty),0) as stock, COALESCE(SUM(aa.total_cost),0) as valuation, COUNT(*), aux_warehouse_id as m_warehouse_id
-        FROM (SELECT a.m_product_id, CASE a.iscostcalculated
-                                       WHEN 'Y' THEN c_currency_convert_precision(sumcost, a.c_currency_id, v_currency_id, a.movementdate, NULL, p_client, Cur_legal_entities.ad_org_id, 'C')
-                                       ELSE NULL
-                                     END AS total_cost, a.qty, aux_warehouse_id
-              FROM (SELECT trx.m_product_id,  SUM(trx.movementqty) AS qty, SUM (CASE WHEN trx.movementqty < 0 THEN -tc.cost ELSE tc.cost END) AS sumcost, trx.iscostcalculated, 
-                           tc.c_currency_id, tc.dateacct AS movementdate, (CASE WHEN Cur_legal_entities.warehouse_dimension = 'N' THEN NULL ELSE l.m_warehouse_id END) AS aux_warehouse_id
-                    FROM m_transaction trx
-                      JOIN m_locator l ON (trx.m_locator_id=l.m_locator_id)
-                      LEFT JOIN m_transaction_cost tc ON (trx.m_transaction_id = tc.m_transaction_id)
-                    WHERE trx.m_product_id = cur_products.m_product_id
-                      AND trx.iscostcalculated = 'Y'
-                      AND trx.ad_org_id IN (SELECT ad_org_id
-                                            FROM ad_org
-                                            WHERE (ad_isorgincluded(ad_org_id, Cur_legal_entities.ad_org_id, ad_client_id) <> -1
-                                            OR ad_isorgincluded(Cur_legal_entities.ad_org_id, ad_org_id, ad_client_id) <> -1
-                                            OR ad_org_id = '0'))
-                      AND trx.ad_client_id = p_client
-                      AND trx.ad_client_id = p_client
-                    GROUP BY trx.m_transaction_id, trx.m_product_id, trx.iscostcalculated, tc.c_currency_id, tc.dateacct, aux_warehouse_id) a
-              WHERE a.sumcost <> 0) aa
-         GROUP BY aux_warehouse_id
-
-      ) LOOP
-     
-        M_UPDATE_STOCK_VALUATION(p_client, Cur_legal_entities.ad_org_id, '0', Cur_values.m_warehouse_id, cur_products.m_product_id, Cur_values.stock, ROUND(Cur_values.valuation,c_get_currency_precision(v_currency_ID, 'C'))); 
-     
-      END LOOP;      
-    END LOOP;
-  END LOOP;    
-
-RETURN;
+          SELECT SUM(a.qty) as stock, ROUND(SUM(sumcost), v_costprecission) as valuation, aux_warehouse_id as m_warehouse_id
+          FROM (
+            SELECT trx.m_product_id, w.m_warehouse_id AS aux_warehouse_id,
+                SUM(trx.movementqty) AS qty, c_currency_convert_precision(SUM (CASE WHEN trx.movementqty < 0 THEN -tc.cost ELSE tc.cost END), tc.c_currency_id, v_currency_id, tc.dateacct, NULL, p_client, Cur_legal_entities.ad_org_id, 'C') AS sumcost
+            FROM m_transaction trx
+              JOIN m_locator l ON (trx.m_locator_id=l.m_locator_id)
+              LEFT JOIN m_warehouse w ON w.m_warehouse_id = CASE Cur_legal_entities.warehouse_dimension WHEN 'Y' THEN l.m_warehouse_id ELSE '-1' END
+              LEFT JOIN m_transaction_cost tc ON (trx.m_transaction_id = tc.m_transaction_id)
+            WHERE trx.m_product_id = cur_products.m_product_id
+              AND trx.iscostcalculated = 'Y'
+              AND AD_ORG_ISINNATURALTREE(trx.ad_org_id, Cur_legal_entities.ad_org_id, p_client) ='Y'
+              AND trx.ad_client_id = p_client
+            GROUP BY trx.m_transaction_id, trx.m_product_id, tc.c_currency_id, tc.dateacct, w.m_warehouse_id, trx.movementqty
+            ) a
+          GROUP BY aux_warehouse_id
+        ) LOOP
+        M_UPDATE_STOCK_VALUATION(p_client, Cur_legal_entities.ad_org_id, '0', Cur_values.m_warehouse_id, cur_products.m_product_id, Cur_values.stock,Cur_values.valuation); 
+      END LOOP; -- Loop values
+    END LOOP;  -- Loop products
+  END LOOP; -- Loop legal entities
 
 EXCEPTION
 WHEN OTHERS THEN
+  DBMS_OUTPUT.PUT_LINE('Error initializing Stock Valuation') ;
+  DBMS_OUTPUT.PUT_LINE(SQLERRM) ;
   RAISE;
 END M_INITIALIZE_STOCK_VALUATION
 ]]></body>
--- a/src-db/database/model/functions/M_UPDATE_STOCK_VALUATION.xml	Tue Nov 11 17:32:58 2014 +0530
+++ b/src-db/database/model/functions/M_UPDATE_STOCK_VALUATION.xml	Wed Dec 17 19:06:50 2014 +0100
@@ -46,32 +46,38 @@
   v_count               NUMBER;
   v_wh_dimension        VARCHAR(1);
   v_warehouse           VARCHAR(32);
+  v_costprecission      NUMBER;
   
 BEGIN
 
-  v_warehouse := p_warehouse;
-
   SELECT warehouse_dimension
   INTO v_wh_dimension
   FROM m_costing_rule cr
   WHERE isvalidated = 'Y'
-    AND COALESCE(datefrom, TO_DATE('01-01-1900', 'DD-MM-YYYY')) <= now()
-    AND COALESCE(dateto, TO_DATE('01-01-9999', 'DD-MM-YYYY')) >= now()
+    AND (datefrom IS NULL OR datefrom <= now())
+    AND (dateto IS NULL OR dateto >= now())
     AND ad_client_id = p_client
-    AND p_org = ad_org_id; 
-    
+    AND ad_org_id = p_org;
+
   IF (v_wh_dimension = 'N') THEN
     v_warehouse := NULL;
+  ELSE
+    v_warehouse := p_warehouse;
   END IF;
+  
+  SELECT c_get_currency_precision(COALESCE(o.c_currency_id, c.c_currency_id), 'C')
+    INTO v_costprecission
+  FROM ad_org o
+    JOIN ad_client c ON o.ad_client_id = c.ad_client_id
+  WHERE o.ad_org_id = p_org;
 
   SELECT COUNT(*), COALESCE(SUM(stock), 0), COALESCE(SUM(stock_valuation),0)
-  INTO v_count, v_stock, v_valuation
+    INTO v_count, v_stock, v_valuation
   FROM m_stock_valuation
   WHERE ad_client_id = p_client
     AND ad_org_id = p_org
     AND m_product_id = p_product
-    AND (v_warehouse IS NULL
-      OR m_warehouse_id = v_warehouse);
+    AND (v_warehouse IS NULL OR m_warehouse_id = v_warehouse);
 
   v_stock := v_stock + p_stock;
   v_valuation := v_valuation + p_valuation;
@@ -80,9 +86,9 @@
     v_unit_price := 0;
     v_valuation := 0;
   ELSE
-    v_unit_price := v_valuation/v_stock;
+    v_unit_price := ROUND(v_valuation/v_stock, v_costprecission);
   END IF;
-      
+
   IF (v_count > 0) THEN
     UPDATE m_stock_valuation
     SET stock = v_stock,
@@ -90,25 +96,20 @@
         unit_price = v_unit_price
      WHERE ad_client_id = p_client
        AND ad_org_id = p_org
-       AND (v_warehouse IS NULL
-         OR m_warehouse_id = v_warehouse);
+       AND m_product_id = p_product
+       AND (v_warehouse IS NULL OR m_warehouse_id = v_warehouse);
   ELSE
-    INSERT INTO m_stock_valuation
-  (
-    m_stock_valuation_id, ad_client_id, ad_org_id,
-    created, createdby, updated, updatedby,
-    isactive, stock, stock_valuation, m_product_id, m_warehouse_id, unit_price
-  )
-  VALUES
-  (
-    GET_UUID(), p_client, p_org,
-    now(), COALESCE(p_user,'0'), now(), COALESCE(p_user,'0'),
-    'Y', v_stock, v_valuation, p_product, v_warehouse, v_unit_price
-  );
+    INSERT INTO m_stock_valuation (
+        m_stock_valuation_id, ad_client_id, ad_org_id,
+        created, createdby, updated, updatedby,
+        isactive, stock, stock_valuation, m_product_id, m_warehouse_id, unit_price
+    ) VALUES (
+        GET_UUID(), p_client, p_org,
+        now(), COALESCE(p_user,'0'), now(), COALESCE(p_user,'0'),
+        'Y', v_stock, v_valuation, p_product, v_warehouse, v_unit_price
+    );
   END IF;
 
-RETURN;
-
 EXCEPTION
 WHEN OTHERS THEN
   RAISE;
--- a/src-db/database/model/triggers/AD_ORG_TRG.xml	Tue Nov 11 17:32:58 2014 +0530
+++ b/src-db/database/model/triggers/AD_ORG_TRG.xml	Wed Dec 17 19:06:50 2014 +0100
@@ -27,8 +27,7 @@
   TYPE RECORD IS REF CURSOR;
     CUR_PeriodControl RECORD;
     v_count NUMBER;
-  
-    
+
 BEGIN
     
     IF AD_isTriggerEnabled()='N' THEN RETURN;
@@ -136,6 +135,25 @@
       AND AD_Tree_ID=v_xTree_ID
       AND Node_ID=:old.AD_Org_ID;     
   END IF;
+  IF (UPDATING) THEN
+    IF (COALESCE(:old.c_currency_id, '-1') <> COALESCE(:new.c_currency_id, '-1')) THEN
+      SELECT COUNT(*) INTO v_count
+      FROM DUAL
+      WHERE EXISTS (SELECT 1 FROM ad_preference
+                    WHERE property = 'UnitaryCost'
+                      AND ad_client_id IN (:NEW.ad_client_id, '0')
+                      AND TO_CHAR(value) = 'Y')
+        AND EXISTS (SELECT 1 FROM m_costing_rule
+                    WHERE isvalidated = 'Y'
+                      AND (datefrom IS NULL OR  datefrom <= now())
+                      AND (dateto IS NULL OR dateto >= now())
+                      AND ad_org_id = :new.ad_org_id);
+      IF (v_count > 0) THEN
+        DELETE FROM m_stock_valuation
+        WHERE ad_org_id = :new.ad_org_id;
+      END IF;
+    END IF;
+  END IF;
   -- Deleting
 EXCEPTION
 WHEN NO_DATA_FOUND THEN
--- a/src-db/database/model/triggers/M_PRODUCT_TRG.xml	Tue Nov 11 17:32:58 2014 +0530
+++ b/src-db/database/model/triggers/M_PRODUCT_TRG.xml	Wed Dec 17 19:06:50 2014 +0100
@@ -134,39 +134,29 @@
 
  ELSIF (INSERTING) THEN
  
-   -- Insert into M_Stock_Valuation
- 
-  SELECT COUNT(*) 
-    INTO v_count
-  FROM ad_preference
-  WHERE property = 'UnitaryCost'
-    AND ad_client_id IN (:NEW.ad_client_id, '0')
-    AND TO_CHAR(value) = 'Y';
-    
+  -- Insert into M_Stock_Valuation
+  SELECT COUNT(*) INTO v_count
+  FROM DUAL
+  WHERE EXISTS (SELECT 1 FROM ad_preference
+                WHERE property = 'UnitaryCost'
+                  AND ad_client_id IN (:NEW.ad_client_id, '0')
+                  AND TO_CHAR(value) = 'Y');
+
   IF (v_count > 0) THEN
-  
     FOR Cur_Legal IN (
       SELECT ad_org_id, warehouse_dimension
       FROM m_costing_rule cr
       WHERE isvalidated = 'Y'
-        AND COALESCE(datefrom, TO_DATE('01-01-1900', 'DD-MM-YYYY')) <= now()
-        AND COALESCE(dateto, TO_DATE('01-01-9999', 'DD-MM-YYYY')) >= now()
-        AND (:NEW.ad_org_id IS NULL OR ad_org_id IN (SELECT ad_org_id
-                                                    FROM ad_org
-                                                    WHERE (ad_isorgincluded(ad_org_id, :NEW.ad_org_id, :NEW.ad_client_id) <> -1
-                                                      OR ad_isorgincluded(:NEW.ad_org_id, ad_org_id, :NEW.ad_client_id) <> -1
-                                                      OR ad_org_id = '0')
-        AND ad_client_id = :NEW.ad_client_id))
+        AND (datefrom IS NULL OR  datefrom <= now())
+        AND (dateto IS NULL OR dateto >= now())
+        AND AD_ORG_ISINNATURALTREE(ad_org_id, :NEW.ad_org_id, :NEW.ad_client_id) ='Y'
+        AND ad_client_id = :NEW.ad_client_id
     ) LOOP 
-      IF cur_legal.warehouse_dimension = 'Y' THEN
+      IF (cur_legal.warehouse_dimension = 'Y') THEN
         FOR Cur_Warehouses IN(
           SELECT m_warehouse_id
           FROM m_warehouse
-          WHERE ad_org_id IN (SELECT ad_org_id
-                              FROM ad_org
-                              WHERE (ad_isorgincluded(ad_org_id, cur_legal.ad_org_id, :NEW.ad_client_id) <> -1
-                                OR ad_isorgincluded(cur_legal.ad_org_id, ad_org_id, :NEW.ad_client_id) <> -1
-                                OR ad_org_id = '0'))
+          WHERE AD_ORG_ISINNATURALTREE(ad_org_id, cur_legal.ad_org_id, :NEW.ad_client_id) ='Y'
             AND ad_client_id = :NEW.ad_client_id
         ) LOOP
           M_UPDATE_STOCK_VALUATION(:NEW.ad_client_id, cur_legal.ad_org_id, '0', cur_warehouses.m_warehouse_id, :NEW.m_product_id, 0, 0); 
--- a/src-db/database/model/triggers/M_TRANSACTION_COST_TRG.xml	Tue Nov 11 17:32:58 2014 +0530
+++ b/src-db/database/model/triggers/M_TRANSACTION_COST_TRG.xml	Wed Dec 17 19:06:50 2014 +0100
@@ -3,9 +3,6 @@
     <trigger name="M_TRANSACTION_COST_TRG" table="M_TRANSACTION_COST" fires="after" insert="true" update="true" delete="true" foreach="row">
       <body><![CDATA[
 
-
-
-
 /*************************************************************************
 * The contents of this file are subject to the Openbravo  Public  License
 * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
@@ -23,94 +20,81 @@
 * Contributor(s):  ______________________________________.
 ************************************************************************/
 
-  v_warehouse            VARCHAR(32);
-  v_org                  VARCHAR(32);
-  v_transaction          VARCHAR(32);
-  v_old_transaction			 VARCHAR(32);
+  v_org_id               VARCHAR2(32);
+  v_legal_org_id         VARCHAR2(32);
+  v_client_id            VARCHAR2(32);
+  v_product_id           VARCHAR2(32);
+  v_transaction_id       VARCHAR2(32);
+  v_old_transaction_id   VARCHAR2(32);
+  v_warehouse_id         VARCHAR2(32);
+  v_currency_id          VARCHAR2(32);
+  v_currency_to_id       VARCHAR2(32);
   v_sign                 NUMBER;
-  v_old_sign             NUMBER;
-  v_product              VARCHAR(32);
-  v_trans_date           DATE;
-  v_legal                VARCHAR(32);
-  v_count                NUMBER;
+  v_updatestockvaluation NUMBER;
   v_valuation            NUMBER;
-  v_old_valuation			   NUMBER;
-  v_currency_to          VARCHAR(32);
 
-    
 BEGIN
 
   IF AD_isTriggerEnabled()='N' THEN RETURN;
   END IF;
 
+  IF (INSERTING) THEN
+    v_transaction_id := :NEW.m_transaction_id;
+    v_org_id := :NEW.ad_org_id;
+    v_client_id := :NEW.ad_client_id;
+    v_currency_id := :NEW.c_currency_id;
+  ELSIF (DELETING) THEN
+    v_old_transaction_id := :OLD.m_transaction_id;
+    v_org_id := :OLD.ad_org_id;
+    v_client_id := :OLD.ad_client_id;
+    v_currency_id := :OLD.c_currency_id;
+  ELSIF (UPDATING AND :OLD.cost <> :NEW.cost) THEN
+    v_transaction_id := :NEW.m_transaction_id;
+    v_old_transaction_id := :OLD.m_transaction_id;
+    v_org_id := :OLD.ad_org_id;
+    v_client_id := :OLD.ad_client_id;
+    v_currency_id := :OLD.c_currency_id;
+  END IF;
+
   --Update m_stock_valuation
-  
-  SELECT COUNT(*) 
-    INTO v_count
-  FROM ad_preference
-  WHERE property = 'UnitaryCost'
-    AND ad_client_id IN (:NEW.ad_client_id, '0')
-    AND TO_CHAR(value) = 'Y';
-    
-  IF (v_count > 0) THEN
-  
-    IF INSERTING THEN
-      v_transaction := :NEW.m_transaction_id;
-      v_org := :NEW.ad_org_id;
-    ELSIF DELETING THEN
-      v_transaction := :OLD.m_transaction_id;
-      v_org := :OLD.ad_org_id;
-    ELSIF UPDATING  AND :OLD.cost <> :NEW.cost THEN
-      v_transaction := :NEW.m_transaction_id;
-      v_old_transaction := :OLD.m_transaction_id;
-      v_org := :OLD.ad_org_id;
+  SELECT COUNT(*) INTO v_updatestockvaluation
+  FROM DUAL
+  WHERE EXISTS (SELECT 1 FROM ad_preference
+                WHERE property = 'UnitaryCost'
+                  AND ad_client_id IN (:NEW.ad_client_id, '0')
+                  AND TO_CHAR(value) = 'Y');
+
+  IF (v_updatestockvaluation > 0) THEN
+    SELECT o.ad_org_id, COALESCE(o.c_currency_id, c.c_currency_id)
+      INTO v_legal_org_id, v_currency_to_id
+    FROM ad_org o JOIN ad_client c ON o.ad_client_id = c.ad_client_id
+    WHERE o.ad_org_id = ad_get_org_le_bu(v_org_id,'LE');
+
+    IF (v_transaction_id IS NOT NULL) THEN
+      SELECT t.m_product_id, l.m_warehouse_id, CASE WHEN t.movementqty < 0 THEN -1 ELSE 1 END
+        INTO v_product_id, v_warehouse_id, v_sign
+      FROM m_transaction t
+        JOIN m_locator l ON t.m_locator_id = l.m_locator_id
+      WHERE m_transaction_id = v_transaction_id;
+
+      v_valuation := c_currency_convert_precision(:NEW.cost, v_currency_id, v_currency_to_id, :NEW.costdate, NULL, v_client_id, v_legal_org_id, 'C');
+      v_valuation := v_valuation * v_sign;
+      M_UPDATE_STOCK_VALUATION(v_client_id, v_legal_org_id, :NEW.createdby, v_warehouse_id, v_product_id, 0, v_valuation);
     END IF;
-      
-    SELECT t.m_product_id, l.m_warehouse_id, t.movementdate, CASE WHEN t.movementqty < 0 THEN 0 ELSE 1 END
-    INTO v_product, v_warehouse, v_trans_date, v_sign
-    FROM m_transaction t
-    JOIN m_locator l ON (t.m_locator_id = l.m_locator_id)
-    WHERE m_transaction_id = v_transaction;
-    
-     IF (v_old_transaction IS NOT NULL) THEN
-       SELECT CASE WHEN t.movementqty < 0 THEN 0 ELSE 1 END
-       INTO  v_old_sign
-       FROM m_transaction t
-       JOIN m_locator l ON (t.m_locator_id = l.m_locator_id)
-       WHERE m_transaction_id = v_old_transaction;
-     END IF;
-    
-    SELECT ad_org_id, c_currency_id
-    INTO v_legal, v_currency_to
-    FROM ad_org
-    WHERE ad_org_id = ad_get_org_le_bu(v_org,'LE');
-    
-    IF INSERTING THEN
-        v_valuation := c_currency_convert_precision(:NEW.cost, :NEW.c_currency_id, v_currency_to, :NEW.costdate, NULL, :NEW.ad_client_id, v_legal, 'C');
-      IF v_sign <> 1 THEN
-        v_valuation := - v_valuation;
-      END IF;
-      M_UPDATE_STOCK_VALUATION(:NEW.ad_client_id, v_legal, :NEW.createdby, v_warehouse, v_product, 0, v_valuation);
-    ELSIF DELETING THEN
-      v_valuation := c_currency_convert_precision(:OLD.cost, :OLD.c_currency_id, v_currency_to, :OLD.costdate, NULL, :OLD.ad_client_id, v_legal, 'C');
-      IF v_sign <> 1 THEN
-        v_valuation := - v_valuation;
-      END IF;
-      M_UPDATE_STOCK_VALUATION(:OLD.ad_client_id, v_legal, :OLD.createdby, v_warehouse, v_product, 0, - v_valuation);
-    ELSIF UPDATING AND :OLD.cost <> :NEW.cost THEN
-      v_valuation := c_currency_convert_precision(:NEW.cost, :NEW.c_currency_id, v_currency_to, :NEW.costdate, NULL, :NEW.ad_client_id, v_legal, 'C');
-      IF v_sign <> 1 THEN
-        v_valuation := - v_valuation;
-      END IF;
-      v_old_valuation := c_currency_convert_precision(:OLD.cost, :OLD.c_currency_id, v_currency_to, :OLD.costdate, NULL, :OLD.ad_client_id, v_legal, 'C');
-      IF v_old_sign <> 1 THEN
-        v_old_valuation := - v_old_valuation;
-      END IF;
-      M_UPDATE_STOCK_VALUATION(:OLD.ad_client_id, v_legal, :OLD.createdby, v_warehouse, v_product, 0, - v_old_valuation);
-      M_UPDATE_STOCK_VALUATION(:NEW.ad_client_id, v_legal, :NEW.createdby, v_warehouse, v_product, 0, v_valuation);
+
+    IF (v_old_transaction_id IS NOT NULL) THEN
+      SELECT t.m_product_id, l.m_warehouse_id, CASE WHEN t.movementqty < 0 THEN -1 ELSE 1 END
+        INTO v_product_id, v_warehouse_id, v_sign
+      FROM m_transaction t
+        JOIN m_locator l ON (t.m_locator_id = l.m_locator_id)
+      WHERE m_transaction_id = v_old_transaction_id;
+
+      v_valuation := c_currency_convert_precision(:OLD.cost, v_currency_id, v_currency_to_id, :OLD.costdate, NULL, v_client_id, v_legal_org_id, 'C');
+      v_valuation := v_valuation * v_sign;
+      M_UPDATE_STOCK_VALUATION(v_client_id, v_legal_org_id, :OLD.createdby, v_warehouse_id, v_product_id, 0, - v_valuation);
     END IF;
   END IF;
-  
+
   END M_TRANSACTION_COST_TRG
 ]]></body>
     </trigger>
--- a/src-db/database/model/triggers/M_TRANSACTION_TRG.xml	Tue Nov 11 17:32:58 2014 +0530
+++ b/src-db/database/model/triggers/M_TRANSACTION_TRG.xml	Wed Dec 17 19:06:50 2014 +0100
@@ -32,11 +32,12 @@
   v_LINE NUMBER;
   v_DateNull DATE := TO_DATE('01-01-1900', 'DD-MM-YYYY');
   v_DocumentLineNo M_InventoryLine.line%TYPE;
-  v_Change_valuation BOOLEAN;
   v_sv_count NUMBER;
   v_warehouse VARCHAR(32);
   v_count NUMBER;
-    
+  v_doIncrease BOOLEAN:= FALSE;
+  v_doDecrease BOOLEAN:= FALSE;
+  v_stockdiff NUMBER:= 0;
 BEGIN
     
     IF AD_isTriggerEnabled()='N' THEN RETURN;
@@ -161,27 +162,51 @@
   END IF;
 
   --Update m_stock_valuation
+  SELECT COUNT(*) INTO v_count
+  FROM DUAL
+  WHERE EXISTS (SELECT 1 FROM ad_preference
+                WHERE property = 'UnitaryCost'
+                  AND ad_client_id IN (:NEW.ad_client_id, '0')
+                  AND TO_CHAR(value) = 'Y');
+
+  IF (v_count > 0) THEN
+    IF (INSERTING) THEN
+      IF (:NEW.iscostcalculated = 'Y') THEN
+        v_doIncrease := TRUE;
+      END IF;
+    ELSIF (DELETING) THEN
+      IF (:OLD.iscostcalculated = 'Y') THEN
+        v_doDecrease := TRUE;
+      END IF;
+    ELSIF (UPDATING) THEN
+      IF (:NEW.iscostcalculated = 'Y' AND :OLD.iscostcalculated = 'N') THEN
+        v_doIncrease := TRUE;
+      END IF;
+      IF (:NEW.iscostcalculated = 'N' AND :OLD.iscostcalculated = 'Y') THEN
+        v_doDecrease := TRUE;
+      END IF;
+    END IF;
+    IF (v_doIncrease) THEN
+      v_stockdiff := :NEW.movementqty;
+      IF (v_stockdiff <> 0) THEN
+        SELECT m_warehouse_id
+          INTO v_warehouse
+        FROM m_locator
+        WHERE m_locator_id = :NEW.m_locator_id;
   
-  SELECT COUNT(*) 
-    INTO v_count
-  FROM ad_preference
-  WHERE property = 'UnitaryCost'
-    AND ad_client_id IN (:NEW.ad_client_id, '0')
-    AND TO_CHAR(value) = 'Y';
-    
-  IF (v_count > 0) THEN
-    SELECT m_warehouse_id
-    INTO v_warehouse
-    FROM m_locator
-    WHERE m_locator_id = :NEW.m_locator_id;
-    
-    IF INSERTING THEN
-      M_UPDATE_STOCK_VALUATION(:NEW.ad_client_id, ad_get_org_le_bu(:NEW.ad_org_id,'LE'), :NEW.createdby, v_warehouse, :NEW.m_product_id, COALESCE(:NEW.movementqty, 0), 0);
-    ELSIF DELETING THEN
-      M_UPDATE_STOCK_VALUATION(:OLD.ad_client_id, ad_get_org_le_bu(:OLD.ad_org_id,'LE'), :OLD.createdby, v_warehouse, :OLD.m_product_id, - COALESCE(:OLD.movementqty, 0), 0);
-    ELSIF UPDATING AND :OLD.iscostcalculated = 'N' AND :NEW.iscostcalculated = 'Y' AND :OLD.movementqty <> :NEW.movementqty THEN
-      M_UPDATE_STOCK_VALUATION(:OLD.ad_client_id, ad_get_org_le_bu(:OLD.ad_org_id,'LE'), :OLD.createdby, v_warehouse, :OLD.m_product_id, - COALESCE(:OLD.movementqty, 0), 0);
-      M_UPDATE_STOCK_VALUATION(:NEW.ad_client_id, ad_get_org_le_bu(:NEW.ad_org_id,'LE'), :NEW.createdby, v_warehouse, :NEW.m_product_id, COALESCE(:NEW.movementqty, 0), 0);
+        M_UPDATE_STOCK_VALUATION(:NEW.ad_client_id, ad_get_org_le_bu(:NEW.ad_org_id,'LE'), :NEW.createdby, v_warehouse, :NEW.m_product_id, v_stockdiff, 0);
+      END IF;
+    END IF;
+    IF (v_doDecrease) THEN
+      v_stockdiff := - :OLD.movementqty;
+      IF (v_stockdiff <> 0) THEN
+        SELECT m_warehouse_id
+          INTO v_warehouse
+        FROM m_locator
+        WHERE m_locator_id = :OLD.m_locator_id;
+  
+        M_UPDATE_STOCK_VALUATION(:OLD.ad_client_id, ad_get_org_le_bu(:OLD.ad_org_id,'LE'), :OLD.createdby, v_warehouse, :OLD.m_product_id, v_stockdiff, 0);
+      END IF;
     END IF;
   END IF;
 
--- a/src-db/database/sourcedata/AD_FIELD.xml	Tue Nov 11 17:32:58 2014 +0530
+++ b/src-db/database/sourcedata/AD_FIELD.xml	Wed Dec 17 19:06:50 2014 +0100
@@ -184270,6 +184270,7 @@
 <!--1418FA20C83B4B6A99BB2729D52997E3-->  <STARTINODDCOLUMN><![CDATA[N]]></STARTINODDCOLUMN>
 <!--1418FA20C83B4B6A99BB2729D52997E3-->  <STARTNEWLINE><![CDATA[N]]></STARTNEWLINE>
 <!--1418FA20C83B4B6A99BB2729D52997E3-->  <ISSHOWNINSTATUSBAR><![CDATA[N]]></ISSHOWNINSTATUSBAR>
+<!--1418FA20C83B4B6A99BB2729D52997E3-->  <ONCHANGEFUNCTION><![CDATA[OB.OnChange.organizationCurrency]]></ONCHANGEFUNCTION>
 <!--1418FA20C83B4B6A99BB2729D52997E3-->  <EM_OBUIAPP_SHOWSUMMARY><![CDATA[N]]></EM_OBUIAPP_SHOWSUMMARY>
 <!--1418FA20C83B4B6A99BB2729D52997E3--></AD_FIELD>
 
--- a/src-db/database/sourcedata/OBUISEL_SELECTOR.xml	Tue Nov 11 17:32:58 2014 +0530
+++ b/src-db/database/sourcedata/OBUISEL_SELECTOR.xml	Wed Dec 17 19:06:50 2014 +0100
@@ -665,18 +665,13 @@
 <!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <NAME><![CDATA[Legal Organizations Selector]]></NAME>
 <!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <AD_REFERENCE_ID><![CDATA[EE3A1FFA879945C88957F4401BF15C0B]]></AD_REFERENCE_ID>
 <!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <AD_TABLE_ID><![CDATA[155]]></AD_TABLE_ID>
+<!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <WHERECLAUSE><![CDATA[exists (select 1 from OrganizationType ot where ot = e.organizationType and ot.legalEntity = true) AND e.ready=true]]></WHERECLAUSE>
 <!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <OBCLKER_TEMPLATE_ID><![CDATA[9314DE8599AD44E7BFC4CC50699042AB]]></OBCLKER_TEMPLATE_ID>
 <!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
-<!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <VALUEFIELD_ID><![CDATA[5EB79D1F0D424CA4BBA838C3AB4AEE88]]></VALUEFIELD_ID>
-<!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <DISPLAYFIELD_ID><![CDATA[56CF04D01F364FD1AC5BA64E0D5CC4D6]]></DISPLAYFIELD_ID>
 <!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <SUGGESTIONTEXTMATCHSTYLE><![CDATA[substring]]></SUGGESTIONTEXTMATCHSTYLE>
 <!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <POPUPTEXTMATCHSTYLE><![CDATA[substring]]></POPUPTEXTMATCHSTYLE>
-<!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <HQL><![CDATA[select org.id as orgid, org.name as name, org.searchKey as value
-from Organization org
-where org.organizationType in ('1','3')
-and @additional_filters@]]></HQL>
 <!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <ENTITY_ALIAS><![CDATA[org]]></ENTITY_ALIAS>
-<!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <CUSTOM_QUERY><![CDATA[Y]]></CUSTOM_QUERY>
+<!--BA3E7AE2C78E44AF890EDEFCDB76CD9A-->  <CUSTOM_QUERY><![CDATA[N]]></CUSTOM_QUERY>
 <!--BA3E7AE2C78E44AF890EDEFCDB76CD9A--></OBUISEL_SELECTOR>
 
 <!--BD1DA40E134A42B9889B529302A96871--><OBUISEL_SELECTOR>
--- a/src-db/database/sourcedata/OBUISEL_SELECTOR_FIELD.xml	Tue Nov 11 17:32:58 2014 +0530
+++ b/src-db/database/sourcedata/OBUISEL_SELECTOR_FIELD.xml	Wed Dec 17 19:06:50 2014 +0100
@@ -1260,7 +1260,9 @@
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <AD_COLUMN_ID><![CDATA[522]]></AD_COLUMN_ID>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <NAME><![CDATA[name]]></NAME>
+<!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <PROPERTY><![CDATA[name]]></PROPERTY>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <ISCENTRALLYMAINTAINED><![CDATA[N]]></ISCENTRALLYMAINTAINED>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <SORT><![CDATA[Y]]></SORT>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <FILTER><![CDATA[Y]]></FILTER>
@@ -1269,8 +1271,6 @@
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <SORTNO><![CDATA[0]]></SORTNO>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <ISOUTFIELD><![CDATA[N]]></ISOUTFIELD>
-<!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <DISPLAY_EXPRESSION><![CDATA[name]]></DISPLAY_EXPRESSION>
-<!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <CLAUSE_LEFT_PART><![CDATA[org.name]]></CLAUSE_LEFT_PART>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <AD_REFERENCE_ID><![CDATA[10]]></AD_REFERENCE_ID>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6-->  <SHOWINPICKLIST><![CDATA[N]]></SHOWINPICKLIST>
 <!--56CF04D01F364FD1AC5BA64E0D5CC4D6--></OBUISEL_SELECTOR_FIELD>
@@ -1359,26 +1359,6 @@
 <!--5C49AEF18F02402180E472E45E58C776-->  <SHOWINPICKLIST><![CDATA[N]]></SHOWINPICKLIST>
 <!--5C49AEF18F02402180E472E45E58C776--></OBUISEL_SELECTOR_FIELD>
 
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88--><OBUISEL_SELECTOR_FIELD>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <OBUISEL_SELECTOR_FIELD_ID><![CDATA[5EB79D1F0D424CA4BBA838C3AB4AEE88]]></OBUISEL_SELECTOR_FIELD_ID>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <OBUISEL_SELECTOR_ID><![CDATA[BA3E7AE2C78E44AF890EDEFCDB76CD9A]]></OBUISEL_SELECTOR_ID>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <NAME><![CDATA[orgid]]></NAME>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <ISCENTRALLYMAINTAINED><![CDATA[N]]></ISCENTRALLYMAINTAINED>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <SORT><![CDATA[Y]]></SORT>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <FILTER><![CDATA[Y]]></FILTER>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <SEARCHINSUGGESTIONBOX><![CDATA[N]]></SEARCHINSUGGESTIONBOX>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <SHOWINGRID><![CDATA[N]]></SHOWINGRID>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <SORTNO><![CDATA[0]]></SORTNO>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <ISOUTFIELD><![CDATA[N]]></ISOUTFIELD>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <DISPLAY_EXPRESSION><![CDATA[orgid]]></DISPLAY_EXPRESSION>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <AD_REFERENCE_ID><![CDATA[276]]></AD_REFERENCE_ID>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88-->  <SHOWINPICKLIST><![CDATA[N]]></SHOWINPICKLIST>
-<!--5EB79D1F0D424CA4BBA838C3AB4AEE88--></OBUISEL_SELECTOR_FIELD>
-
 <!--61468BCC79F14B0289A046060D119EC2--><OBUISEL_SELECTOR_FIELD>
 <!--61468BCC79F14B0289A046060D119EC2-->  <OBUISEL_SELECTOR_FIELD_ID><![CDATA[61468BCC79F14B0289A046060D119EC2]]></OBUISEL_SELECTOR_FIELD_ID>
 <!--61468BCC79F14B0289A046060D119EC2-->  <OBUISEL_SELECTOR_ID><![CDATA[A48B1462EE7F4C109F06564E0B4677A8]]></OBUISEL_SELECTOR_ID>
@@ -2653,16 +2633,17 @@
 <!--B0D7816370094619A58B6E0272638374-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
 <!--B0D7816370094619A58B6E0272638374-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
 <!--B0D7816370094619A58B6E0272638374-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--B0D7816370094619A58B6E0272638374-->  <AD_COLUMN_ID><![CDATA[2045]]></AD_COLUMN_ID>
 <!--B0D7816370094619A58B6E0272638374-->  <NAME><![CDATA[value]]></NAME>
+<!--B0D7816370094619A58B6E0272638374-->  <PROPERTY><![CDATA[searchKey]]></PROPERTY>
 <!--B0D7816370094619A58B6E0272638374-->  <ISCENTRALLYMAINTAINED><![CDATA[N]]></ISCENTRALLYMAINTAINED>
 <!--B0D7816370094619A58B6E0272638374-->  <SORT><![CDATA[Y]]></SORT>
 <!--B0D7816370094619A58B6E0272638374-->  <FILTER><![CDATA[Y]]></FILTER>
-<!--B0D7816370094619A58B6E0272638374-->  <SEARCHINSUGGESTIONBOX><![CDATA[N]]></SEARCHINSUGGESTIONBOX>
+<!--B0D7816370094619A58B6E0272638374-->  <SEARCHINSUGGESTIONBOX><![CDATA[Y]]></SEARCHINSUGGESTIONBOX>
 <!--B0D7816370094619A58B6E0272638374-->  <SHOWINGRID><![CDATA[N]]></SHOWINGRID>
 <!--B0D7816370094619A58B6E0272638374-->  <SORTNO><![CDATA[0]]></SORTNO>
 <!--B0D7816370094619A58B6E0272638374-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
 <!--B0D7816370094619A58B6E0272638374-->  <ISOUTFIELD><![CDATA[N]]></ISOUTFIELD>
-<!--B0D7816370094619A58B6E0272638374-->  <DISPLAY_EXPRESSION><![CDATA[value]]></DISPLAY_EXPRESSION>
 <!--B0D7816370094619A58B6E0272638374-->  <AD_REFERENCE_ID><![CDATA[10]]></AD_REFERENCE_ID>
 <!--B0D7816370094619A58B6E0272638374-->  <SHOWINPICKLIST><![CDATA[N]]></SHOWINPICKLIST>
 <!--B0D7816370094619A58B6E0272638374--></OBUISEL_SELECTOR_FIELD>
--- a/src/org/openbravo/costing/ResetStockValuation.java	Tue Nov 11 17:32:58 2014 +0530
+++ b/src/org/openbravo/costing/ResetStockValuation.java	Wed Dec 17 19:06:50 2014 +0100
@@ -5,7 +5,6 @@
 import java.util.Map;
 
 import org.apache.log4j.Logger;
-import org.codehaus.jettison.json.JSONArray;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.hibernate.Query;
@@ -24,8 +23,6 @@
   protected JSONObject doExecute(Map<String, Object> parameters, String content) {
     try {
       JSONObject result = new JSONObject();
-      JSONObject msg = new JSONObject();
-      boolean errorMessage = false;
 
       JSONObject request = new JSONObject(content);
       JSONObject params = request.getJSONObject("_params");
@@ -36,6 +33,22 @@
         strOrgID = (String) params.get("AD_Org_ID");
       }
 
+      JSONObject msg = doResetStockValuation(strOrgID);
+
+      result.put("message", msg);
+      result.put("retryExecution", true);
+      return result;
+
+    } catch (JSONException e) {
+      log.error("Error in process", e);
+      return new JSONObject();
+    }
+  }
+
+  public static JSONObject doResetStockValuation(String strOrgID) {
+    try {
+      JSONObject msg = new JSONObject();
+      boolean errorMessage = false;
       // delete existing records
       StringBuffer sql = new StringBuffer();
       sql.append("delete from");
@@ -58,30 +71,26 @@
       storedProcedureParams.add(null);
       try {
         CallStoredProcedure.getInstance().call("M_INITIALIZE_STOCK_VALUATION",
-            storedProcedureParams, null, true, false);
+            storedProcedureParams, null, false, false);
       } catch (Exception e) {
         errorMessage = true;
-        msg.put("msgType", "error");
-        msg.put("msgTitle", OBMessageUtils.messageBD("Error"));
-        msg.put("msgText", OBMessageUtils.messageBD("ConversionRateCommandError"));
+        msg.put("severity", "error");
+        msg.put("title", OBMessageUtils.messageBD("Error"));
+        msg.put("message", OBMessageUtils.translateError(e.getMessage()));
       }
 
       if (!errorMessage) {
-        msg.put("msgType", "success");
-        msg.put("msgTitle", OBMessageUtils.messageBD("Success"));
+        msg.put("severity", "success");
+        msg.put("message", OBMessageUtils.messageBD("Success"));
       }
 
-      JSONArray actions = new JSONArray();
-      JSONObject msgAction = new JSONObject();
-      msgAction.put("showMsgInProcessView", msg);
-      actions.put(msgAction);
-      result.put("responseActions", actions);
-      result.put("retryExecution", true);
-      return result;
+      return msg;
 
     } catch (JSONException e) {
       log.error("Error in process", e);
       return new JSONObject();
     }
+
   }
+
 }