Fixes issue 30232: Costing record not referencing transaction
authorAlvaro Ferraz <alvaro.ferraz@openbravo.com>
Fri, 03 Jul 2015 11:08:17 +0200
changeset 27086 fb92cb9bd2eb
parent 27085 adc2491ee60b
child 27087 b45cf0559789
Fixes issue 30232: Costing record not referencing transaction

getRelatedTransactions method in AverageCostAdjustment.java was creating an HQL query with a wrong parenthesis. Because of that Order By clause did not appear in the query, what was causing wrong Costing entries.
Also, when this error happens, current costing will be updated with the new cost, instead of creating a new costing with a null transaction.
src/org/openbravo/costing/AverageCostAdjustment.java
--- a/src/org/openbravo/costing/AverageCostAdjustment.java	Mon Jul 06 12:32:17 2015 +0200
+++ b/src/org/openbravo/costing/AverageCostAdjustment.java	Fri Jul 03 11:08:17 2015 +0200
@@ -431,35 +431,20 @@
             + OBDateUtils.formatDate(new Date()));
       }
       if (currentCosting.getCost().compareTo(cost) != 0) {
-        basetrx = getTransaction();
-        Date newDate = new Date();
-        Date dateTo = currentCosting.getEndingDate();
-        currentCosting.setEndingDate(newDate);
+        // Update existing costing
+        currentCosting.setPermanent(Boolean.FALSE);
         OBDal.getInstance().save(currentCosting);
-        Costing newCosting = OBProvider.getInstance().get(Costing.class);
-        newCosting.setCost(cost);
-        newCosting.setCurrency((Currency) OBDal.getInstance().getProxy(Currency.ENTITY_NAME,
-            strCurrentCurId));
-        newCosting.setStartingDate(newDate);
-        newCosting.setEndingDate(dateTo);
-        newCosting.setInventoryTransaction(null);
-        newCosting.setProduct(basetrx.getProduct());
-        if (isManufacturingProduct) {
-          newCosting.setOrganization((Organization) OBDal.getInstance().getProxy(
-              Organization.ENTITY_NAME, "0"));
-        } else {
-          newCosting.setOrganization((Organization) OBDal.getInstance().getProxy(
-              Organization.ENTITY_NAME, strCostOrgId));
+        OBDal.getInstance().flush();
+
+        if (currentCosting.getOriginalCost() == null) {
+          currentCosting.setOriginalCost(currentCosting.getCost());
         }
-        newCosting.setQuantity(null);
-        newCosting.setTotalMovementQuantity(currentStock);
-        newCosting.setPrice(cost);
-        newCosting.setCostType("AVA");
-        newCosting.setManual(Boolean.FALSE);
-        newCosting.setPermanent(Boolean.TRUE);
-        newCosting.setProduction(trxType == TrxType.ManufacturingProduced);
-        newCosting.setWarehouse((Warehouse) getCostDimensions().get(CostDimension.Warehouse));
-        OBDal.getInstance().save(newCosting);
+        currentCosting.setPrice(cost);
+        currentCosting.setCost(cost);
+        currentCosting.setTotalMovementQuantity(currentStock);
+        currentCosting.setManual(Boolean.FALSE);
+        currentCosting.setPermanent(Boolean.TRUE);
+        OBDal.getInstance().save(currentCosting);
       }
     }
   }
@@ -617,9 +602,8 @@
       wh.append("          trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + " = :trxqty");
       wh.append("          and trx." + MaterialTransaction.PROPERTY_ID + " > :trxid");
       wh.append("    )))))");
-      wh.append(" )))");
+      wh.append(" ))))");
     }
-    wh.append(")");
     wh.append("  and org.id in (:orgs)");
     if (warehouse != null) {
       wh.append("  and loc." + Locator.PROPERTY_WAREHOUSE + " = :warehouse");