Author: jonesde
Date: Wed Nov 10 02:42:27 2010 New Revision: 1033331 URL: http://svn.apache.org/viewvc?rev=1033331&view=rev Log: Small change so that adjustment amounts are not rounded until added into the line item subtotal; rounding them too early causes loss of precision in general, and also makes the new VAT correction adjustments useless in some circumstances because they are often less than .01, but necessary to get the order to add up to exact amounts when compared to prices with tax included Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java?rev=1033331&r1=1033330&r2=1033331&view=diff ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderReadHelper.java Wed Nov 10 02:42:27 2010 @@ -2315,8 +2315,8 @@ public class OrderReadHelper { adjustments = EntityUtil.filterByAnd(adjustments, UtilMisc.toList(EntityCondition.makeCondition("orderAdjustmentTypeId", EntityOperator.NOT_EQUAL, "SALES_TAX"))); BigDecimal total = getOrderItemsTotal(orderItems, adjustments); BigDecimal adj = getOrderAdjustmentsTotal(orderItems, adjustments); - total = total.add(taxGrandTotal).setScale(scale,rounding); - return total.add(adj).setScale(scale,rounding); + total = ((total.add(taxGrandTotal)).add(adj)).setScale(scale,rounding); + return total; } public static List<GenericValue> getOrderHeaderAdjustments(List<GenericValue> adjustments, String shipGroupSeqId) { @@ -2499,6 +2499,7 @@ public class OrderReadHelper { // subtotal also includes non tax and shipping adjustments; tax and shipping will be calculated using this adjusted value result = result.add(getOrderItemAdjustmentsTotal(orderItem, adjustments, true, false, false, forTax, forShipping)); + // Debug.logInfo("In getOrderItemSubTotal result=" + result + ", rounded result=" + result.setScale(scale, rounding), module); return result.setScale(scale, rounding); } @@ -2657,10 +2658,11 @@ public class OrderReadHelper { public static BigDecimal calcItemAdjustment(GenericValue itemAdjustment, BigDecimal quantity, BigDecimal unitPrice) { BigDecimal adjustment = ZERO; if (itemAdjustment.get("amount") != null) { - adjustment = adjustment.add(setScaleByType("SALES_TAX".equals(itemAdjustment.get("orderAdjustmentTypeId")), itemAdjustment.getBigDecimal("amount"))); - } - else if (itemAdjustment.get("sourcePercentage") != null) { - adjustment = adjustment.add(setScaleByType("SALES_TAX".equals(itemAdjustment.get("orderAdjustmentTypeId")), itemAdjustment.getBigDecimal("sourcePercentage").multiply(quantity).multiply(unitPrice).multiply(percentage))); + //shouldn't round amounts here, wait until item total is added up otherwise incremental errors are introduced, and there is code that calls this method that does that already: adjustment = adjustment.add(setScaleByType("SALES_TAX".equals(itemAdjustment.get("orderAdjustmentTypeId")), itemAdjustment.getBigDecimal("amount"))); + adjustment = adjustment.add(itemAdjustment.getBigDecimal("amount")); + } else if (itemAdjustment.get("sourcePercentage") != null) { + // see comment above about rounding: adjustment = adjustment.add(setScaleByType("SALES_TAX".equals(itemAdjustment.get("orderAdjustmentTypeId")), itemAdjustment.getBigDecimal("sourcePercentage").multiply(quantity).multiply(unitPrice).multiply(percentage))); + adjustment = adjustment.add(itemAdjustment.getBigDecimal("sourcePercentage").multiply(quantity).multiply(unitPrice).multiply(percentage)); } if (Debug.verboseOn()) Debug.logVerbose("calcItemAdjustment: " + itemAdjustment + ", quantity=" + quantity + ", unitPrice=" + unitPrice + ", adjustment=" + adjustment, module); return adjustment; Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java?rev=1033331&r1=1033330&r2=1033331&view=diff ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java Wed Nov 10 02:42:27 2010 @@ -2615,8 +2615,9 @@ public class ShoppingCart implements Ite /** Returns the total from the cart, including tax/shipping. */ public BigDecimal getGrandTotal() { // sales tax and shipping are not stored as adjustments but rather as part of the ship group - // Debug.logInfo("Subtotal:" + this.getSubTotal() + " Shipping:" + this.getTotalShipping() + "SalesTax: "+ this.getTotalSalesTax() + " others: " + this.getOrderOtherAdjustmentTotal(),module); + // Debug.logInfo("Subtotal:" + this.getSubTotal() + " Shipping:" + this.getTotalShipping() + "SalesTax: "+ this.getTotalSalesTax() + " others: " + this.getOrderOtherAdjustmentTotal(), module); BigDecimal grandTotal = this.getSubTotal().add(this.getTotalShipping()).add(this.getTotalSalesTax()).add(this.getOrderOtherAdjustmentTotal()); + // Debug.logInfo("Grand Total before rounding:" + grandTotal, module); grandTotal = grandTotal.setScale(2, BigDecimal.ROUND_HALF_UP); return grandTotal; } |
Free forum by Nabble | Edit this page |