1003754 Landed cost issue -- related to IDEMPIERE-1285. Fix rounding issue. Fix wrong posting when estimated landed cost is more than actual landed cost.

This commit is contained in:
Heng Sin Low 2014-05-12 16:15:15 +08:00
parent 8cbea90a1b
commit cfa8050b0b
5 changed files with 57 additions and 46 deletions

View File

@ -869,13 +869,17 @@ public class Doc_Invoice extends Doc
BigDecimal qty = allocation.getQty(); BigDecimal qty = allocation.getQty();
if (qty.compareTo(iol.getMovementQty()) != 0) if (qty.compareTo(iol.getMovementQty()) != 0)
{ {
amt = amt.multiply(iol.getMovementQty()).divide(qty, BigDecimal.ROUND_HALF_UP); amt = amt.multiply(iol.getMovementQty()).divide(qty, 12, BigDecimal.ROUND_HALF_UP);
} }
estimatedAmt = estimatedAmt.add(amt); estimatedAmt = estimatedAmt.add(amt);
} }
} }
} }
if (estimatedAmt.scale() > as.getCostingPrecision())
{
estimatedAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
}
BigDecimal costAdjustmentAmt = allocationAmt; BigDecimal costAdjustmentAmt = allocationAmt;
if (estimatedAmt.signum() > 0) if (estimatedAmt.signum() > 0)
{ {
@ -907,43 +911,46 @@ public class Doc_Invoice extends Doc
if (!dr) if (!dr)
costAdjustmentAmt = costAdjustmentAmt.negate(); costAdjustmentAmt = costAdjustmentAmt.negate();
Trx trx = Trx.get(getTrxName(), false);
Savepoint savepoint = null;
boolean zeroQty = false; boolean zeroQty = false;
try { if (costAdjustmentAmt.signum() != 0)
savepoint = trx.setSavepoint(null); {
BigDecimal costDetailAmt = costAdjustmentAmt; Trx trx = Trx.get(getTrxName(), false);
//convert to accounting schema currency Savepoint savepoint = null;
if (getC_Currency_ID() != as.getC_Currency_ID())
costDetailAmt = MConversionRate.convert(getCtx(), costDetailAmt,
getC_Currency_ID(), as.getC_Currency_ID(),
getDateAcct(), getC_ConversionType_ID(),
getAD_Client_ID(), getAD_Org_ID());
if (costDetailAmt.scale() > as.getCostingPrecision())
costDetailAmt = costDetailAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
if (!MCostDetail.createInvoice(as, lca.getAD_Org_ID(),
lca.getM_Product_ID(), lca.getM_AttributeSetInstance_ID(),
C_InvoiceLine_ID, lca.getM_CostElement_ID(),
costDetailAmt, lca.getQty(),
desc, getTrxName())) {
throw new RuntimeException("Failed to create cost detail record.");
}
} catch (SQLException e) {
throw new RuntimeException(e.getLocalizedMessage(), e);
} catch (AverageCostingZeroQtyException e) {
zeroQty = true;
try { try {
trx.rollback(savepoint); savepoint = trx.setSavepoint(null);
savepoint = null; BigDecimal costDetailAmt = costAdjustmentAmt;
} catch (SQLException e1) { //convert to accounting schema currency
throw new RuntimeException(e1.getLocalizedMessage(), e1); if (getC_Currency_ID() != as.getC_Currency_ID())
} costDetailAmt = MConversionRate.convert(getCtx(), costDetailAmt,
} finally { getC_Currency_ID(), as.getC_Currency_ID(),
if (savepoint != null) { getDateAcct(), getC_ConversionType_ID(),
getAD_Client_ID(), getAD_Org_ID());
if (costDetailAmt.scale() > as.getCostingPrecision())
costDetailAmt = costDetailAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
if (!MCostDetail.createInvoice(as, lca.getAD_Org_ID(),
lca.getM_Product_ID(), lca.getM_AttributeSetInstance_ID(),
C_InvoiceLine_ID, lca.getM_CostElement_ID(),
costDetailAmt, lca.getQty(),
desc, getTrxName())) {
throw new RuntimeException("Failed to create cost detail record.");
}
} catch (SQLException e) {
throw new RuntimeException(e.getLocalizedMessage(), e);
} catch (AverageCostingZeroQtyException e) {
zeroQty = true;
try { try {
trx.releaseSavepoint(savepoint); trx.rollback(savepoint);
} catch (SQLException e) {} savepoint = null;
} catch (SQLException e1) {
throw new RuntimeException(e1.getLocalizedMessage(), e1);
}
} finally {
if (savepoint != null) {
try {
trx.releaseSavepoint(savepoint);
} catch (SQLException e) {}
}
} }
} }
@ -978,8 +985,8 @@ public class Doc_Invoice extends Doc
} }
else if (compare < 0) else if (compare < 0)
{ {
drAmt = dr ? (reversal ? null : allocationAmt) : (reversal ? allocationAmt : null); drAmt = dr ? (reversal ? null : estimatedAmt) : (reversal ? estimatedAmt : null);
crAmt = dr ? (reversal ? allocationAmt : null) : (reversal ? null : allocationAmt); crAmt = dr ? (reversal ? estimatedAmt : null) : (reversal ? null : estimatedAmt);
account = pc.getAccount(ProductCost.ACCTTYPE_P_LandedCostClearing, as); account = pc.getAccount(ProductCost.ACCTTYPE_P_LandedCostClearing, as);
FactLine fl = fact.createLine (line, account, getC_Currency_ID(), drAmt, crAmt); FactLine fl = fact.createLine (line, account, getC_Currency_ID(), drAmt, crAmt);
fl.setDescription(desc); fl.setDescription(desc);

View File

@ -508,7 +508,7 @@ public class Doc_MatchInv extends Doc
{ {
BigDecimal totalAmt = allocation.getAmt(); BigDecimal totalAmt = allocation.getAmt();
BigDecimal totalQty = allocation.getQty(); BigDecimal totalQty = allocation.getQty();
BigDecimal amt = totalAmt.multiply(tQty).divide(totalQty, BigDecimal.ROUND_HALF_UP); BigDecimal amt = totalAmt.multiply(tQty).divide(totalQty, 12, BigDecimal.ROUND_HALF_UP);
if (orderLine.getC_Currency_ID() != as.getC_Currency_ID()) if (orderLine.getC_Currency_ID() != as.getC_Currency_ID())
{ {
I_C_Order order = orderLine.getC_Order(); I_C_Order order = orderLine.getC_Order();

View File

@ -198,7 +198,7 @@ public class Doc_MatchPO extends Doc
{ {
BigDecimal totalAmt = allocation.getAmt(); BigDecimal totalAmt = allocation.getAmt();
BigDecimal totalQty = allocation.getQty(); BigDecimal totalQty = allocation.getQty();
BigDecimal amt = totalAmt.multiply(m_ioLine.getMovementQty()).divide(totalQty, as.getCostingPrecision(), RoundingMode.HALF_UP); BigDecimal amt = totalAmt.multiply(m_ioLine.getMovementQty()).divide(totalQty, 12, RoundingMode.HALF_UP);
if (m_oLine.getC_Currency_ID() != as.getC_Currency_ID()) if (m_oLine.getC_Currency_ID() != as.getC_Currency_ID())
{ {
MOrder order = m_oLine.getParent(); MOrder order = m_oLine.getParent();
@ -213,11 +213,11 @@ public class Doc_MatchPO extends Doc
return null; return null;
} }
amt = amt.multiply(rate); amt = amt.multiply(rate);
if (amt.scale() > as.getCostingPrecision())
amt = amt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
} }
amt = amt.divide(getQty(), as.getCostingPrecision(), RoundingMode.HALF_UP); amt = amt.divide(getQty(), 12, RoundingMode.HALF_UP);
landedCost = landedCost.add(amt); landedCost = landedCost.add(amt);
if (landedCost.scale() > as.getCostingPrecision())
landedCost = landedCost.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
int elementId = allocation.getC_OrderLandedCost().getM_CostElement_ID(); int elementId = allocation.getC_OrderLandedCost().getM_CostElement_ID();
BigDecimal elementAmt = landedCostMap.get(elementId); BigDecimal elementAmt = landedCostMap.get(elementId);
if (elementAmt == null) if (elementAmt == null)
@ -442,6 +442,8 @@ public class Doc_MatchPO extends Doc
return error; return error;
} }
if (tAmt.scale() > as.getCostingPrecision())
tAmt = tAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
// Set Total Amount and Total Quantity from Matched PO // Set Total Amount and Total Quantity from Matched PO
if (!MCostDetail.createOrder(as, m_oLine.getAD_Org_ID(), if (!MCostDetail.createOrder(as, m_oLine.getAD_Org_ID(),
getM_Product_ID(), mMatchPO.getM_AttributeSetInstance_ID(), getM_Product_ID(), mMatchPO.getM_AttributeSetInstance_ID(),
@ -471,6 +473,8 @@ public class Doc_MatchPO extends Doc
{ {
BigDecimal amt = landedCostMap.get(elementId); BigDecimal amt = landedCostMap.get(elementId);
amt = amt.multiply(tQty); amt = amt.multiply(tQty);
if (amt.scale() > as.getCostingPrecision())
amt = amt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
if (!MCostDetail.createOrder(as, m_oLine.getAD_Org_ID(), if (!MCostDetail.createOrder(as, m_oLine.getAD_Org_ID(),
getM_Product_ID(), mMatchPO.getM_AttributeSetInstance_ID(), getM_Product_ID(), mMatchPO.getM_AttributeSetInstance_ID(),
m_oLine.getC_OrderLine_ID(), elementId, m_oLine.getC_OrderLine_ID(), elementId,

View File

@ -1098,7 +1098,7 @@ public class MInvoiceLine extends X_C_InvoiceLine
{ {
double result = getLineNetAmt().multiply(base).doubleValue(); double result = getLineNetAmt().multiply(base).doubleValue();
result /= total.doubleValue(); result /= total.doubleValue();
lca.setAmt(result, getPrecision()); lca.setAmt(result, getParent().getC_Currency().getCostingPrecision());
} }
if (!lca.save()){ if (!lca.save()){
msgreturn = new StringBuilder("Cannot save line Allocation = ").append(lca); msgreturn = new StringBuilder("Cannot save line Allocation = ").append(lca);
@ -1223,7 +1223,7 @@ public class MInvoiceLine extends X_C_InvoiceLine
{ {
double result = getLineNetAmt().multiply(base).doubleValue(); double result = getLineNetAmt().multiply(base).doubleValue();
result /= total.doubleValue(); result /= total.doubleValue();
lca.setAmt(result, getPrecision()); lca.setAmt(result, getParent().getC_Currency().getCostingPrecision());
} }
if (!lca.save()){ if (!lca.save()){
msgreturn = new StringBuilder("Cannot save line Allocation = ").append(lca); msgreturn = new StringBuilder("Cannot save line Allocation = ").append(lca);

View File

@ -146,8 +146,8 @@ public class MOrderLandedCost extends X_C_OrderLandedCost {
if (base.signum() != 0) if (base.signum() != 0)
{ {
BigDecimal result = getAmt().multiply(base); BigDecimal result = getAmt().multiply(base);
result = result.divide(total, orderLine.getParent().getC_Currency().getStdPrecision(), BigDecimal.ROUND_HALF_UP); result = result.divide(total, orderLine.getParent().getC_Currency().getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
allocation.setAmt(result.doubleValue(), orderLine.getParent().getC_Currency().getStdPrecision()); allocation.setAmt(result.doubleValue(), orderLine.getParent().getC_Currency().getCostingPrecision());
} }
allocation.saveEx(); allocation.saveEx();
} }