From ab37dd125809adb569d2e93ca146cd3bde984538 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Tue, 11 Nov 2014 14:19:02 -0500 Subject: [PATCH] IDEMPIERE-2057 Modify "Update Costing" process to create a cost adjustment document / IDEMPIERE-2306 Can't create cost detail when costing level is Batch/Lot from import inventory --- .../src/org/compiere/process/CostUpdate.java | 4 +-- .../org/compiere/process/ImportInventory.java | 12 ++++++--- .../src/org/compiere/model/MCost.java | 25 +++++++++++++------ .../src/org/compiere/model/MCostDetail.java | 16 +++++++++--- .../org/compiere/model/MInventoryLine.java | 2 +- .../src/org/compiere/model/MProduct.java | 2 +- .../src/org/compiere/model/ProductCost.java | 2 +- 7 files changed, 43 insertions(+), 20 deletions(-) diff --git a/org.adempiere.base.process/src/org/compiere/process/CostUpdate.java b/org.adempiere.base.process/src/org/compiere/process/CostUpdate.java index dce469dc4a..fff15e77eb 100644 --- a/org.adempiere.base.process/src/org/compiere/process/CostUpdate.java +++ b/org.adempiere.base.process/src/org/compiere/process/CostUpdate.java @@ -513,7 +513,7 @@ public class CostUpdate extends SvrProcess } if (retValue == null) { - MProduct product = MProduct.get(getCtx(), cost.getM_Product_ID()); + MProduct product = new MProduct(getCtx(), cost.getM_Product_ID(), get_TrxName()); MAcctSchema as = MAcctSchema.get(getCtx(), cost.getC_AcctSchema_ID()); retValue = MCost.getLastInvoicePrice(product, cost.getM_AttributeSetInstance_ID(), cost.getAD_Org_ID(), as.getC_Currency_ID()); @@ -532,7 +532,7 @@ public class CostUpdate extends SvrProcess } if (retValue == null) { - MProduct product = MProduct.get(getCtx(), cost.getM_Product_ID()); + MProduct product = new MProduct(getCtx(), cost.getM_Product_ID(), get_TrxName()); MAcctSchema as = MAcctSchema.get(getCtx(), cost.getC_AcctSchema_ID()); retValue = MCost.getLastPOPrice(product, cost.getM_AttributeSetInstance_ID(), cost.getAD_Org_ID(), as.getC_Currency_ID()); diff --git a/org.adempiere.base.process/src/org/compiere/process/ImportInventory.java b/org.adempiere.base.process/src/org/compiere/process/ImportInventory.java index 4401be2e9d..bb937793bb 100644 --- a/org.adempiere.base.process/src/org/compiere/process/ImportInventory.java +++ b/org.adempiere.base.process/src/org/compiere/process/ImportInventory.java @@ -400,7 +400,7 @@ public class ImportInventory extends SvrProcess x_isInternalUse = isInternalUse; noInsert++; } - MProduct product = MProduct.get(getCtx(), imp.getM_Product_ID()); + MProduct product = new MProduct(getCtx(), imp.getM_Product_ID(), get_TrxName()); // Line int M_AttributeSetInstance_ID = 0; if ((imp.getLot() != null && imp.getLot().length() > 0) || (imp.getSerNo() != null && imp.getSerNo().length() > 0)) @@ -519,10 +519,13 @@ public class ImportInventory extends SvrProcess costASI = 0; } else if (MAcctSchema.COSTINGLEVEL_Organization.equals(costingLevel)) { costASI = 0; + } else if (MAcctSchema.COSTINGLEVEL_BatchLot.equals(costingLevel)) { + costOrgID = 0; } - MCost cost = MCost.get (MProduct.get(getCtx(), imp.getM_Product_ID()), costASI + MCost cost = MCost.get (product, costASI , acctSchema, costOrgID, p_M_CostElement_ID, get_TrxName()); - + if (cost.is_new()) + cost.saveEx(); if (costingDoc == null) { costingDoc = new MInventory(getCtx(), 0, get_TrxName()); costingDoc.setC_DocType_ID(p_C_DocType_ID); @@ -539,7 +542,8 @@ public class ImportInventory extends SvrProcess costingLine.setNewCostPrice(imp.getCurrentCostPrice()); costingLine.setM_Locator_ID(0); costingLine.setAD_Org_ID(imp.getAD_Org_ID()); - costingLine.saveEx(); + costingLine.setM_AttributeSetInstance_ID(costASI); + costingLine.saveEx(); imp.setM_CostingLine_ID(costingLine.getM_InventoryLine_ID()); imp.saveEx(); diff --git a/org.adempiere.base/src/org/compiere/model/MCost.java b/org.adempiere.base/src/org/compiere/model/MCost.java index b9dd42fe44..9b4b65577e 100644 --- a/org.adempiere.base/src/org/compiere/model/MCost.java +++ b/org.adempiere.base/src/org/compiere/model/MCost.java @@ -55,14 +55,10 @@ import org.compiere.util.Trx; */ public class MCost extends X_M_Cost { - - - - /** - * - */ - private static final long serialVersionUID = -127982599769472918L; - + /** + * + */ + private static final long serialVersionUID = -8904980122276406878L; /** * Retrieve/Calculate Current Cost Price @@ -1520,6 +1516,19 @@ public class MCost extends X_M_Cost setCurrentQty(getCurrentQty().add(qty)); } // setWeightedAverage + /** + * @param amt unit amt + */ + public void setWeightedAverageInitial (BigDecimal amtUnit) + { + BigDecimal cost = amtUnit; + if (cost.scale() > (getPrecision()*2)) + { + cost = cost.setScale((getPrecision()*2), BigDecimal.ROUND_HALF_UP); + } + setCurrentCostPrice(cost); + } // setWeightedAverageInitial + /** * Get Costing Precision * @return precision (6) diff --git a/org.adempiere.base/src/org/compiere/model/MCostDetail.java b/org.adempiere.base/src/org/compiere/model/MCostDetail.java index 3591772bd1..24586d8816 100644 --- a/org.adempiere.base/src/org/compiere/model/MCostDetail.java +++ b/org.adempiere.base/src/org/compiere/model/MCostDetail.java @@ -833,7 +833,7 @@ public class MCostDetail extends X_M_CostDetail // get costing level for product MAcctSchema as = MAcctSchema.get(getCtx(), getC_AcctSchema_ID()); - MProduct product = MProduct.get(getCtx(), getM_Product_ID()); + MProduct product = new MProduct(getCtx(), getM_Product_ID(), get_TrxName()); String CostingLevel = product.getCostingLevel(as); // Org Element int Org_ID = getAD_Org_ID(); @@ -1152,7 +1152,12 @@ public class MCostDetail extends X_M_CostDetail costingMethod = getM_InventoryLine().getM_Inventory().getCostingMethod(); if (MCostElement.COSTINGMETHOD_AverageInvoice.equals(costingMethod)) { - cost.setWeightedAverage(amt.multiply(cost.getCurrentQty()), qty); + if (cost.getCurrentQty().signum() == 0 && qty.signum() == 0) { + // IDEMPIERE-2057 - this is a cost adjustment when there is no qty - setting the initial cost + cost.setWeightedAverageInitial(amt); + } else { + cost.setWeightedAverage(amt.multiply(cost.getCurrentQty()), qty); + } } } else if (addition) @@ -1177,7 +1182,12 @@ public class MCostDetail extends X_M_CostDetail costingMethod = getM_InventoryLine().getM_Inventory().getCostingMethod(); if (MCostElement.COSTINGMETHOD_AveragePO.equals(costingMethod)) { - cost.setWeightedAverage(amt.multiply(cost.getCurrentQty()), qty); + if (cost.getCurrentQty().signum() == 0 && qty.signum() == 0) { + // IDEMPIERE-2057 - this is a cost adjustment when there is no qty - setting the initial cost + cost.setWeightedAverageInitial(amt); + } else { + cost.setWeightedAverage(amt.multiply(cost.getCurrentQty()), qty); + } } } else if (addition) diff --git a/org.adempiere.base/src/org/compiere/model/MInventoryLine.java b/org.adempiere.base/src/org/compiere/model/MInventoryLine.java index 39cc9e6689..0f70e8131c 100644 --- a/org.adempiere.base/src/org/compiere/model/MInventoryLine.java +++ b/org.adempiere.base/src/org/compiere/model/MInventoryLine.java @@ -353,7 +353,7 @@ public class MInventoryLine extends X_M_InventoryLine } int M_ASI_ID = getM_AttributeSetInstance_ID(); - MProduct product = getProduct(); + MProduct product = new MProduct(getCtx(), getM_Product_ID(), get_TrxName()); MClient client = MClient.get(getCtx()); MAcctSchema as = client.getAcctSchema(); String costingLevel = product.getCostingLevel(as); diff --git a/org.adempiere.base/src/org/compiere/model/MProduct.java b/org.adempiere.base/src/org/compiere/model/MProduct.java index ce10511096..8abfdcc2b2 100644 --- a/org.adempiere.base/src/org/compiere/model/MProduct.java +++ b/org.adempiere.base/src/org/compiere/model/MProduct.java @@ -910,7 +910,7 @@ public class MProduct extends X_M_Product if (ce == null) { return null; } - MCost cost = MCost.get(this, M_ASI_ID, as, AD_Org_ID, ce.getM_CostElement_ID(), (String)null); + MCost cost = MCost.get(this, M_ASI_ID, as, AD_Org_ID, ce.getM_CostElement_ID(), get_TrxName()); return cost.is_new() ? null : cost; } } // MProduct diff --git a/org.adempiere.base/src/org/compiere/model/ProductCost.java b/org.adempiere.base/src/org/compiere/model/ProductCost.java index 8195cbbb34..da243aab49 100644 --- a/org.adempiere.base/src/org/compiere/model/ProductCost.java +++ b/org.adempiere.base/src/org/compiere/model/ProductCost.java @@ -48,7 +48,7 @@ public class ProductCost { m_M_Product_ID = M_Product_ID; if (m_M_Product_ID != 0) - m_product = MProduct.get (ctx, M_Product_ID); + m_product = new MProduct(ctx, M_Product_ID, trxName); m_M_AttributeSetInstance_ID = M_AttributeSetInstance_ID; m_trxName = trxName; } // ProductCost