diff --git a/org.adempiere.base/src/org/compiere/acct/Doc_Invoice.java b/org.adempiere.base/src/org/compiere/acct/Doc_Invoice.java index 6dafc5fc2f..1f0c6368c5 100644 --- a/org.adempiere.base/src/org/compiere/acct/Doc_Invoice.java +++ b/org.adempiere.base/src/org/compiere/acct/Doc_Invoice.java @@ -869,13 +869,17 @@ public class Doc_Invoice extends Doc BigDecimal qty = allocation.getQty(); 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); } } } + if (estimatedAmt.scale() > as.getCostingPrecision()) + { + estimatedAmt.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP); + } BigDecimal costAdjustmentAmt = allocationAmt; if (estimatedAmt.signum() > 0) { @@ -907,43 +911,46 @@ public class Doc_Invoice extends Doc if (!dr) costAdjustmentAmt = costAdjustmentAmt.negate(); - Trx trx = Trx.get(getTrxName(), false); - Savepoint savepoint = null; boolean zeroQty = false; - try { - savepoint = trx.setSavepoint(null); - BigDecimal costDetailAmt = costAdjustmentAmt; - //convert to accounting schema currency - 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; + if (costAdjustmentAmt.signum() != 0) + { + Trx trx = Trx.get(getTrxName(), false); + Savepoint savepoint = null; try { - trx.rollback(savepoint); - savepoint = null; - } catch (SQLException e1) { - throw new RuntimeException(e1.getLocalizedMessage(), e1); - } - } finally { - if (savepoint != null) { + savepoint = trx.setSavepoint(null); + BigDecimal costDetailAmt = costAdjustmentAmt; + //convert to accounting schema currency + 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 { - trx.releaseSavepoint(savepoint); - } catch (SQLException e) {} + trx.rollback(savepoint); + 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) { - drAmt = dr ? (reversal ? null : allocationAmt) : (reversal ? allocationAmt : null); - crAmt = dr ? (reversal ? allocationAmt : null) : (reversal ? null : allocationAmt); + drAmt = dr ? (reversal ? null : estimatedAmt) : (reversal ? estimatedAmt : null); + crAmt = dr ? (reversal ? estimatedAmt : null) : (reversal ? null : estimatedAmt); account = pc.getAccount(ProductCost.ACCTTYPE_P_LandedCostClearing, as); FactLine fl = fact.createLine (line, account, getC_Currency_ID(), drAmt, crAmt); fl.setDescription(desc); diff --git a/org.adempiere.base/src/org/compiere/acct/Doc_MatchInv.java b/org.adempiere.base/src/org/compiere/acct/Doc_MatchInv.java index 632b18bccf..18907d884a 100644 --- a/org.adempiere.base/src/org/compiere/acct/Doc_MatchInv.java +++ b/org.adempiere.base/src/org/compiere/acct/Doc_MatchInv.java @@ -508,7 +508,7 @@ public class Doc_MatchInv extends Doc { BigDecimal totalAmt = allocation.getAmt(); 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()) { I_C_Order order = orderLine.getC_Order(); diff --git a/org.adempiere.base/src/org/compiere/acct/Doc_MatchPO.java b/org.adempiere.base/src/org/compiere/acct/Doc_MatchPO.java index 419fe010b1..eafa8c67e2 100644 --- a/org.adempiere.base/src/org/compiere/acct/Doc_MatchPO.java +++ b/org.adempiere.base/src/org/compiere/acct/Doc_MatchPO.java @@ -198,7 +198,7 @@ public class Doc_MatchPO extends Doc { BigDecimal totalAmt = allocation.getAmt(); 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()) { MOrder order = m_oLine.getParent(); @@ -213,11 +213,11 @@ public class Doc_MatchPO extends Doc return null; } 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); + if (landedCost.scale() > as.getCostingPrecision()) + landedCost = landedCost.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP); int elementId = allocation.getC_OrderLandedCost().getM_CostElement_ID(); BigDecimal elementAmt = landedCostMap.get(elementId); if (elementAmt == null) @@ -442,6 +442,8 @@ public class Doc_MatchPO extends Doc 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 if (!MCostDetail.createOrder(as, m_oLine.getAD_Org_ID(), getM_Product_ID(), mMatchPO.getM_AttributeSetInstance_ID(), @@ -471,6 +473,8 @@ public class Doc_MatchPO extends Doc { BigDecimal amt = landedCostMap.get(elementId); 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(), getM_Product_ID(), mMatchPO.getM_AttributeSetInstance_ID(), m_oLine.getC_OrderLine_ID(), elementId, diff --git a/org.adempiere.base/src/org/compiere/model/MInvoiceLine.java b/org.adempiere.base/src/org/compiere/model/MInvoiceLine.java index d826c59fdd..c8b7739211 100644 --- a/org.adempiere.base/src/org/compiere/model/MInvoiceLine.java +++ b/org.adempiere.base/src/org/compiere/model/MInvoiceLine.java @@ -1098,7 +1098,7 @@ public class MInvoiceLine extends X_C_InvoiceLine { double result = getLineNetAmt().multiply(base).doubleValue(); result /= total.doubleValue(); - lca.setAmt(result, getPrecision()); + lca.setAmt(result, getParent().getC_Currency().getCostingPrecision()); } if (!lca.save()){ 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(); result /= total.doubleValue(); - lca.setAmt(result, getPrecision()); + lca.setAmt(result, getParent().getC_Currency().getCostingPrecision()); } if (!lca.save()){ msgreturn = new StringBuilder("Cannot save line Allocation = ").append(lca); diff --git a/org.adempiere.base/src/org/compiere/model/MOrderLandedCost.java b/org.adempiere.base/src/org/compiere/model/MOrderLandedCost.java index 15daceb800..adb1b17bdd 100644 --- a/org.adempiere.base/src/org/compiere/model/MOrderLandedCost.java +++ b/org.adempiere.base/src/org/compiere/model/MOrderLandedCost.java @@ -146,8 +146,8 @@ public class MOrderLandedCost extends X_C_OrderLandedCost { if (base.signum() != 0) { BigDecimal result = getAmt().multiply(base); - result = result.divide(total, orderLine.getParent().getC_Currency().getStdPrecision(), BigDecimal.ROUND_HALF_UP); - allocation.setAmt(result.doubleValue(), orderLine.getParent().getC_Currency().getStdPrecision()); + result = result.divide(total, orderLine.getParent().getC_Currency().getCostingPrecision(), BigDecimal.ROUND_HALF_UP); + allocation.setAmt(result.doubleValue(), orderLine.getParent().getC_Currency().getCostingPrecision()); } allocation.saveEx(); }