svn commit: r477773 - in /incubator/ofbiz/trunk/applications/order: config/OrderErrorUiLabels.properties script/org/ofbiz/order/order/OrderServices.xml src/org/ofbiz/order/order/OrderServices.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r477773 - in /incubator/ofbiz/trunk/applications/order: config/OrderErrorUiLabels.properties script/org/ofbiz/order/order/OrderServices.xml src/org/ofbiz/order/order/OrderServices.java

sichen
Author: sichen
Date: Tue Nov 21 09:35:15 2006
New Revision: 477773

URL: http://svn.apache.org/viewvc?view=rev&rev=477773
Log:
Fix bugs with re-calculation of tax when removing order adjustments.  Now, instead of removing all the order adjustments, it recalculates tax and then adds it as an additional adjustment

Modified:
    incubator/ofbiz/trunk/applications/order/config/OrderErrorUiLabels.properties
    incubator/ofbiz/trunk/applications/order/script/org/ofbiz/order/order/OrderServices.xml
    incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java

Modified: incubator/ofbiz/trunk/applications/order/config/OrderErrorUiLabels.properties
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/order/config/OrderErrorUiLabels.properties?view=diff&rev=477773&r1=477772&r2=477773
==============================================================================
--- incubator/ofbiz/trunk/applications/order/config/OrderErrorUiLabels.properties (original)
+++ incubator/ofbiz/trunk/applications/order/config/OrderErrorUiLabels.properties Tue Nov 21 09:35:15 2006
@@ -71,6 +71,7 @@
 OrderEmailFulfillmentTypeNotYetImplemented=Email Fulfillment type not yet implemented
 OrderError=ERROR :
 OrderErrorCallingCountProductQuantityOrderedService=Error calling countProductQuantityOrdered service :
+OrderErrorCallingCreateOrderAdjustmentService=Error calling createOrderAdjustment service
 OrderErrorCannotCancelItemItemNotFound=ERROR : Cannot cancel item; item not found : ${itemMsgInfo}
 OrderErrorCannotChangeItemStatusItemNotFound=ERROR : Cannot change item status ; item not found.
 OrderErrorCannotCheckForFulfillmentItemNotFound=ERROR : Cannot check for fulfillment ; item not found.
@@ -265,6 +266,7 @@
 OrderUnableToLocateTaxFileAtLocation=Unable to locate tax file at location : ${taxFileLocation}
 OrderUnableToReadZipSalesDataFile=Unable to read ZipSales DataFile
 OrderUnableToRemoveSalesTaxAdjustments=Unable to remove SALES_TAX adjustments
+OrderUnableToRetrieveSalesTaxAdjustments=Unable to retrieve SALES_TAX adjustments
 OrderUnableToReserveInventory=Unable to reserve inventory
 OrderUnableToResetOrderGrandTotal=Unable to reset order grand total
 OrderUnableToSetCancelQuantity=Unable to set cancel quantity : ${itemMsgInfo}

Modified: incubator/ofbiz/trunk/applications/order/script/org/ofbiz/order/order/OrderServices.xml
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/order/script/org/ofbiz/order/order/OrderServices.xml?view=diff&rev=477773&r1=477772&r2=477773
==============================================================================
--- incubator/ofbiz/trunk/applications/order/script/org/ofbiz/order/order/OrderServices.xml (original)
+++ incubator/ofbiz/trunk/applications/order/script/org/ofbiz/order/order/OrderServices.xml Tue Nov 21 09:35:15 2006
@@ -473,14 +473,30 @@
                 <call-service service-name="cancelOrderItem" in-map-name="cancelOrderItemInMap"/>
             </if-compare>
         </iterate>
-        <!-- all existing auto order adjustments are removed -->
+
         <get-related value-name="order" relation-name="OrderAdjustment" list-name="orderAdjustments"/>
+
+        <!-- Accumulate the total existing promotional adjustment -->
+        <set field="existingOrderAdjustmentTotal" value="0" type="BigDecimal"/>        
         <iterate list-name="orderAdjustments" entry-name="orderAdjustment">
-            <!-- make sure this is not a manual adjustments -->
-            <if-not-empty field-name="orderAdjustment.productPromoId">
-                <remove-value value-name="orderAdjustment"/>
-            </if-not-empty>
+            <if>
+                <condition>
+                    <and>
+                        <not><if-empty field-name="orderAdjustment.productPromoId"/></not>
+                    </and>
+                </condition>
+                <then>
+                    <calculate field-name="existingOrderAdjustmentTotal" decimal-scale="3">
+                        <calcop operator="add">
+                            <calcop operator="get" field-name="orderAdjustment.amount"/>
+                            <calcop operator="get" field-name="existingOrderAdjustmentTotal"/>
+                        </calcop>
+                    </calculate>
+                </then>
+            </if>
         </iterate>
+
+        <!-- Recalculate the promotions for the order -->
         <set-service-fields service-name="loadCartFromOrder" to-map-name="loadCartFromOrderInMap" map-name="parameters"/>
         <!--<set value="false" field="loadCartFromOrderInMap.applyOrderAdjustments"/>-->
         <call-service service-name="loadCartFromOrder" in-map-name="loadCartFromOrderInMap">
@@ -518,11 +534,38 @@
             </if-empty>
         </iterate>
         <call-object-method obj-field-name="cart" method-name="makeAllAdjustments" ret-field-name="adjustments"/>
+
+        <!-- Accumulate the new promotion total from the recalculated promotion adjustments -->
+        <set field="newOrderAdjustmentTotal" value="0" type="BigDecimal"/>        
         <iterate list-name="adjustments" entry-name="adjustment">
-            <set from-field="parameters.orderId" field="adjustment.orderId"/>
-            <sequenced-id-to-env sequence-name="OrderAdjustment" env-name="adjustment.orderAdjustmentId"/>
-            <create-value value-name="adjustment"/>
+            <calculate field-name="newOrderAdjustmentTotal" decimal-scale="3">
+                <calcop operator="add">
+                    <calcop operator="get" field-name="adjustment.amount"/>
+                    <calcop operator="get" field-name="newOrderAdjustmentTotal"/>
+                </calcop>
+            </calculate>
         </iterate>
+        
+        <!-- Determine the difference between existing and new promotion adjustment totals, if any -->
+        <calculate field-name="orderAdjustmentTotalDifference" decimal-scale="3" type="BigDecimal">
+            <calcop operator="subtract" field-name="newOrderAdjustmentTotal">
+                <calcop operator="get" field-name="existingOrderAdjustmentTotal"/>
+            </calcop>
+        </calculate>
+        
+        <!-- If the total has changed, create an OrderAdjustment to reflect the fact -->
+        <if-compare field-name="orderAdjustmentTotalDifference" value="0" operator="not-equals" type="BigDecimal">
+            <set field="createOrderAdjContext.orderAdjustmentTypeId" value="PROMOTION_ADJUSTMENT"/>
+            <set field="createOrderAdjContext.orderId" from-field="parameters.orderId"/>
+            <set field="createOrderAdjContext.orderItemSeqId" value="_NA_"/>
+            <set field="createOrderAdjContext.shipGroupSeqId" value="_NA_"/>
+            <set field="createOrderAdjContext.orderId" from-field="parameters.orderId"/>
+            <set field="createOrderAdjContext.description" value="Adjustment due to order change"/>
+            <set field="createOrderAdjContext.amount" from-field="orderAdjustmentTotalDifference" type="Double"/>
+            <call-service service-name="createOrderAdjustment" in-map-name="createOrderAdjContext" include-user-login="true"/>
+            <check-errors/>        
+        </if-compare>
+
     </simple-method>
     
     <!--UpdateOrderContactMech-->

Modified: incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java
URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java?view=diff&rev=477773&r1=477772&r2=477773
==============================================================================
--- incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java (original)
+++ incubator/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java Tue Nov 21 09:35:15 2006
@@ -25,14 +25,7 @@
 import javolution.util.FastList;
 import javolution.util.FastMap;
 
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.GeneralException;
-import org.ofbiz.base.util.GeneralRuntimeException;
-import org.ofbiz.base.util.UtilDateTime;
-import org.ofbiz.base.util.UtilFormatOut;
-import org.ofbiz.base.util.UtilMisc;
-import org.ofbiz.base.util.UtilProperties;
-import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.*;
 import org.ofbiz.base.util.collections.ResourceBundleMapWrapper;
 import org.ofbiz.common.DataModelConstants;
 import org.ofbiz.entity.GenericDelegator;
@@ -86,6 +79,9 @@
         purchaseAttributeRoleMap.put("shipFromVendorPartyId", "SHIP_FROM_VENDOR");
         purchaseAttributeRoleMap.put("supplierAgentPartyId", "SUPPLIER_AGENT");
     }
+    public static final int taxDecimals = UtilNumber.getBigDecimalScale("salestax.calc.decimals");
+    public static final int taxRounding = UtilNumber.getBigDecimalRoundingMode("salestax.rounding");
+    public static final BigDecimal ZERO = (new BigDecimal("0")).setScale(taxDecimals, taxRounding);    
 
     /** Service for creating a new order */
     public static Map createOrder(DispatchContext ctx, Map context) {
@@ -1189,7 +1185,7 @@
         return ServiceUtil.returnSuccess();
     }
 
-    /** Service for checking and re-clac the tax amount */
+    /** Service for checking and re-calc the tax amount */
     public static Map recalcOrderTax(DispatchContext ctx, Map context) {
         LocalDispatcher dispatcher = ctx.getDispatcher();
         GenericDelegator delegator = ctx.getDelegator();
@@ -1228,19 +1224,29 @@
             return ServiceUtil.returnSuccess();
         }
 
-        // remove the tax adjustments
-        int removed = 0;
+        // Retrieve the order tax adjustments
+        List orderTaxAdjustments = null;
         try {
-            removed = delegator.removeByAnd("OrderAdjustment", UtilMisc.toMap("orderId", orderId, "orderAdjustmentTypeId", "SALES_TAX"));
-        } catch (GenericEntityException e) {
-            Debug.logError(e, "Unable to remove SALES_TAX adjustments for order : " + orderId, module);
-            return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToRemoveSalesTaxAdjustments",locale));
+            orderTaxAdjustments = delegator.findByAnd("OrderAdjustment", UtilMisc.toMap("orderId", orderId, "orderAdjustmentTypeId", "SALES_TAX"));
+        } catch( GenericEntityException e ) {
+            Debug.logError(e, "Unable to retrieve SALES_TAX adjustments for order : " + orderId, module);
+            return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToRetrieveSalesTaxAdjustments",locale));
+        }
+
+        // Accumulate the total existing tax adjustment
+        BigDecimal totalExistingOrderTax = ZERO;
+        Iterator otait = UtilMisc.toIterator(orderTaxAdjustments);
+        while (otait != null && otait.hasNext()) {
+            GenericValue orderTaxAdjustment = (GenericValue) otait.next();  
+            if( orderTaxAdjustment.get("amount") != null) {
+                totalExistingOrderTax = totalExistingOrderTax.add(orderTaxAdjustment.getBigDecimal("amount").setScale(taxDecimals, taxRounding));
+            }
         }
-        Debug.logInfo("Removed : " + removed + " SALES_TAX adjustments for order [" + orderId + "]", module);
 
+        // Recalculate the taxes for the order
+        BigDecimal totalNewOrderTax = ZERO;
         OrderReadHelper orh = new OrderReadHelper(orderHeader);
         List shipGroups = orh.getOrderItemShipGroups();
-        List toStore = new ArrayList();
         if (shipGroups != null) {
             Iterator itr = shipGroups.iterator();
             while (itr.hasNext()) {
@@ -1327,42 +1333,61 @@
                     List orderAdj = (List) serviceResult.get("orderAdjustments");
                     List itemAdj = (List) serviceResult.get("itemAdjustments");
 
-                    // set the order adjustments
+                    // Accumulate the new tax total from the recalculated header adjustments
                     if (orderAdj != null && orderAdj.size() > 0) {
                         Iterator oai = orderAdj.iterator();
                         while (oai.hasNext()) {
                             GenericValue oa = (GenericValue) oai.next();
-                            oa.set("orderAdjustmentId", delegator.getNextSeqId("OrderAdjustment"));
-                            oa.set("orderId", orderId);
-                            toStore.add(oa);
+                            if( oa.get("amount") != null) {
+                                totalNewOrderTax = totalNewOrderTax.add(oa.getBigDecimal("amount").setScale(taxDecimals, taxRounding));
+                            }
+
+
                         }
                     }
 
-                    // set the item adjustments
+                    // Accumulate the new tax total from the recalculated item adjustments
                     if (itemAdj != null && itemAdj.size() > 0) {
-                        for (int i = 0; i < validOrderItems.size(); i++) {
-                            GenericValue orderItem = (GenericValue) validOrderItems.get(i);
+                        for (int i = 0; i < itemAdj.size(); i++) {
                             List itemAdjustments = (List) itemAdj.get(i);
                             Iterator ida = itemAdjustments.iterator();
                             while (ida.hasNext()) {
                                 GenericValue ia = (GenericValue) ida.next();
-                                ia.set("orderAdjustmentId", delegator.getNextSeqId("OrderAdjustment"));
-                                ia.set("orderId", orderId);
-                                ia.set("shipGroupSeqId", shipGroupSeqId);
-                                ia.set("orderItemSeqId", orderItem.getString("orderItemSeqId"));
-                                toStore.add(ia);
+                                if( ia.get("amount") != null) {
+                                    totalNewOrderTax = totalNewOrderTax.add(ia.getBigDecimal("amount").setScale(taxDecimals, taxRounding));
+                                }
                             }
                         }
                     }
                 }
             }
 
-            // store the new adjustments
-            try {
-                delegator.storeAll(toStore);
-            } catch (GenericEntityException e) {
-                Debug.logError(e, module);
-                return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToUpdateOrderTaxInformation" ,UtilMisc.toMap("orderId",orderId), locale));
+            // Determine the difference between existing and new tax adjustment totals, if any
+            BigDecimal orderTaxDifference = totalNewOrderTax.subtract(totalExistingOrderTax).setScale(taxDecimals, taxRounding);
+
+            // If the total has changed, create an OrderAdjustment to reflect the fact
+            if (orderTaxDifference.signum() != 0) {
+                Map createOrderAdjContext = new HashMap();
+                createOrderAdjContext.put("orderAdjustmentTypeId", "SALES_TAX");
+                createOrderAdjContext.put("orderId", orderId);
+                createOrderAdjContext.put("orderItemSeqId", "_NA_");
+                createOrderAdjContext.put("shipGroupSeqId", "_NA_");
+                createOrderAdjContext.put("description", "Tax adjustment due to order change");
+                createOrderAdjContext.put("amount", new Double(orderTaxDifference.doubleValue()));
+                createOrderAdjContext.put("amount", new Double(orderTaxDifference.doubleValue()));
+                createOrderAdjContext.put("userLogin", userLogin);
+                Map createOrderAdjResponse = null;
+                try {
+                    createOrderAdjResponse = dispatcher.runSync("createOrderAdjustment", createOrderAdjContext);
+                } catch( GenericServiceException e ) {
+                    String createOrderAdjErrMsg = UtilProperties.getMessage(resource_error, "OrderErrorCallingCreateOrderAdjustmentService", locale);
+                    Debug.logError(createOrderAdjErrMsg, module);
+                    return ServiceUtil.returnError(createOrderAdjErrMsg);
+                }
+                if (ServiceUtil.isError(createOrderAdjResponse)) {
+                    Debug.logError(ServiceUtil.getErrorMessage(createOrderAdjResponse), module);
+                    return ServiceUtil.returnError(ServiceUtil.getErrorMessage(createOrderAdjResponse));
+                }
             }
         }