1003754 Landed cost issue -- related to IDEMPIERE-1285. Fix rounding issue with current product cost for average costing.

This commit is contained in:
Heng Sin Low 2014-05-19 14:40:07 +08:00
parent 86207e9729
commit b8c2ed4b2b
2 changed files with 30 additions and 8 deletions

View File

@ -22,6 +22,8 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Savepoint; import java.sql.Savepoint;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import org.adempiere.exceptions.AverageCostingZeroQtyException; import org.adempiere.exceptions.AverageCostingZeroQtyException;
@ -826,6 +828,8 @@ public class Doc_Invoice extends Doc
for (int i = 0; i < lcas.length; i++) for (int i = 0; i < lcas.length; i++)
totalBase += lcas[i].getBase().doubleValue(); totalBase += lcas[i].getBase().doubleValue();
Map<String, BigDecimal> costDetailAmtMap = new HashMap<String, BigDecimal>();
// Create New // Create New
MInvoiceLine il = new MInvoiceLine (getCtx(), C_InvoiceLine_ID, getTrxName()); MInvoiceLine il = new MInvoiceLine (getCtx(), C_InvoiceLine_ID, getTrxName());
for (int i = 0; i < lcas.length; i++) for (int i = 0; i < lcas.length; i++)
@ -878,7 +882,7 @@ public class Doc_Invoice extends Doc
if (estimatedAmt.scale() > as.getCostingPrecision()) if (estimatedAmt.scale() > as.getCostingPrecision())
{ {
estimatedAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP); estimatedAmt = estimatedAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
} }
BigDecimal costAdjustmentAmt = allocationAmt; BigDecimal costAdjustmentAmt = allocationAmt;
if (estimatedAmt.signum() > 0) if (estimatedAmt.signum() > 0)
@ -928,6 +932,12 @@ public class Doc_Invoice extends Doc
if (costDetailAmt.scale() > as.getCostingPrecision()) if (costDetailAmt.scale() > as.getCostingPrecision())
costDetailAmt = costDetailAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP); costDetailAmt = costDetailAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
String key = lca.getM_Product_ID()+"_"+lca.getM_AttributeSetInstance_ID();
BigDecimal prevAmt = costDetailAmtMap.remove(key);
if (prevAmt != null) {
costDetailAmt = costDetailAmt.add(prevAmt);
}
costDetailAmtMap.put(key, costDetailAmt);
if (!MCostDetail.createInvoice(as, lca.getAD_Org_ID(), if (!MCostDetail.createInvoice(as, lca.getAD_Org_ID(),
lca.getM_Product_ID(), lca.getM_AttributeSetInstance_ID(), lca.getM_Product_ID(), lca.getM_AttributeSetInstance_ID(),
C_InvoiceLine_ID, lca.getM_CostElement_ID(), C_InvoiceLine_ID, lca.getM_CostElement_ID(),
@ -963,6 +973,14 @@ public class Doc_Invoice extends Doc
if (allocationAmt.signum() > 0) if (allocationAmt.signum() > 0)
{ {
if (allocationAmt.scale() > as.getStdPrecision())
{
allocationAmt = allocationAmt.setScale(as.getStdPrecision(), BigDecimal.ROUND_HALF_UP);
}
if (estimatedAmt.scale() > as.getStdPrecision())
{
estimatedAmt = estimatedAmt.setScale(as.getStdPrecision(), BigDecimal.ROUND_HALF_UP);
}
int compare = allocationAmt.compareTo(estimatedAmt); int compare = allocationAmt.compareTo(estimatedAmt);
if (compare > 0) if (compare > 0)
{ {

View File

@ -1501,13 +1501,17 @@ public class MCost extends X_M_Cost
+", CostElement="+getM_CostElement().getName()+", Schema="+getC_AcctSchema().getName()); +", CostElement="+getM_CostElement().getName()+", Schema="+getC_AcctSchema().getName());
} }
BigDecimal oldSum = getCurrentCostPrice().multiply(getCurrentQty());
BigDecimal newSum = amt; // is total already
BigDecimal sumAmt = oldSum.add(newSum);
BigDecimal sumQty = getCurrentQty().add(qty); BigDecimal sumQty = getCurrentQty().add(qty);
if (sumQty.signum() != 0) if (sumQty.signum() != 0)
{ {
BigDecimal cost = sumAmt.divide(sumQty, getPrecision(), BigDecimal.ROUND_HALF_UP); BigDecimal oldSum = getCurrentCostPrice().multiply(getCurrentQty());
BigDecimal oldCost = oldSum.divide(sumQty, 12, BigDecimal.ROUND_HALF_UP);
BigDecimal newCost = amt.divide(sumQty, 12, BigDecimal.ROUND_HALF_UP); //amt is total already
BigDecimal cost = oldCost.add(newCost);
if (cost.scale() > (getPrecision()*2))
{
cost = cost.setScale((getPrecision()*2), BigDecimal.ROUND_HALF_UP);
}
setCurrentCostPrice(cost); setCurrentCostPrice(cost);
} }
// //