svn commit: r990751 - in /ofbiz/trunk: applications/order/src/org/ofbiz/order/shoppingcart/ applications/product/src/org/ofbiz/shipment/shipment/ applications/product/src/org/ofbiz/shipment/thirdparty/ups/ framework/common/data/

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

svn commit: r990751 - in /ofbiz/trunk: applications/order/src/org/ofbiz/order/shoppingcart/ applications/product/src/org/ofbiz/shipment/shipment/ applications/product/src/org/ofbiz/shipment/thirdparty/ups/ framework/common/data/

ashish-18
Author: ashish
Date: Mon Aug 30 09:55:02 2010
New Revision: 990751

URL: http://svn.apache.org/viewvc?rev=990751&view=rev
Log:
Applied patch from jira issue OFBIZ-3909 - Bug in showing online estimate rate on UPS services.
UPS Online shipping rates are showing wrong values when product weight having different weightUomId. It doesn't support UOM Conversion as it is working in USPS services.
So fixing this bug, following work is done.

1) Calling generic methods "getPackageSplit", "getProductItemInfo" & "calcPackageWeight" methods from ShipmentWorker.java
2) Removed duplicate methods from UpsServices.java

Thanks Amit for the contribution.

Modified:
    ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java
    ofbiz/trunk/applications/product/src/org/ofbiz/shipment/shipment/ShipmentWorker.java
    ofbiz/trunk/applications/product/src/org/ofbiz/shipment/thirdparty/ups/UpsServices.java
    ofbiz/trunk/framework/common/data/UnitData.xml

Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java?rev=990751&r1=990750&r2=990751&view=diff
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java (original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java Mon Aug 30 09:55:02 2010
@@ -1878,6 +1878,7 @@ public class ShoppingCartItem implements
         Map itemInfo = FastMap.newInstance();
         itemInfo.put("productId", this.getProductId());
         itemInfo.put("weight", this.getWeight());
+        itemInfo.put("weightUomId", this.getProduct().getString("weightUomId"));
         itemInfo.put("size", this.getSize());
         itemInfo.put("piecesIncluded", Long.valueOf(this.getPiecesIncluded()));
         itemInfo.put("featureSet", this.getFeatureSet());

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/shipment/shipment/ShipmentWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/shipment/shipment/ShipmentWorker.java?rev=990751&r1=990750&r2=990751&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/shipment/shipment/ShipmentWorker.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/shipment/shipment/ShipmentWorker.java Mon Aug 30 09:55:02 2010
@@ -102,12 +102,12 @@ public class ShipmentWorker {
         value = totalValue.divide(totalIssued, 10, BigDecimal.ROUND_HALF_EVEN).multiply(quantity);
         return value;
     }
-    
+
     public static List<Map<String, BigDecimal>> getPackageSplit(DispatchContext dctx, List<Map<String, Object>> shippableItemInfo, BigDecimal maxWeight) {
         // create the package list w/ the first package
         List<Map<String, BigDecimal>> packages = FastList.newInstance();
 
-        if (shippableItemInfo != null) {
+        if (UtilValidate.isNotEmpty(shippableItemInfo)) {
             for (Map<String, Object> itemInfo: shippableItemInfo) {
                 long pieces = ((Long) itemInfo.get("piecesIncluded")).longValue();
                 BigDecimal totalQuantity = (BigDecimal) itemInfo.get("quantity");
@@ -119,7 +119,6 @@ public class ShipmentWorker {
                     pieces = 1; // can NEVER be less than one
                 }
                 BigDecimal weight = totalWeight.divide(BigDecimal.valueOf(pieces), generalRounding);
-
                 for (int z = 1; z <= totalQuantity.intValue(); z++) {
                     BigDecimal partialQty = pieces > 1 ? BigDecimal.ONE.divide(BigDecimal.valueOf(pieces), generalRounding) : BigDecimal.ONE;
                     for (long x = 0; x < pieces; x++) {
@@ -140,7 +139,7 @@ public class ShipmentWorker {
                                     BigDecimal packageWeight = calcPackageWeight(dctx, packageMap, shippableItemInfo, weight);
                                     if (packageWeight.compareTo(maxWeight) <= 0) {
                                         BigDecimal qty = (BigDecimal) packageMap.get(productId);
-                                        qty = qty == null ? BigDecimal.ZERO : qty;
+                                        qty = UtilValidate.isEmpty(qty) ? BigDecimal.ZERO : qty;
                                         packageMap.put(productId, qty.add(partialQty));
                                         addedToPackage = true;
                                     }
@@ -158,16 +157,12 @@ public class ShipmentWorker {
         }
         return packages;
     }
-    
+
     public static BigDecimal calcPackageWeight(DispatchContext dctx, Map<String, BigDecimal> packageMap, List<Map<String, Object>> shippableItemInfo, BigDecimal additionalWeight) {
 
         LocalDispatcher dispatcher = dctx.getDispatcher();
         BigDecimal totalWeight = BigDecimal.ZERO;
         String defaultWeightUomId = UtilProperties.getPropertyValue("shipment.properties", "shipment.default.weight.uom");
-        if (UtilValidate.isEmpty(defaultWeightUomId)) {
-            Debug.logWarning("No shipment.default.weight.uom set in shipment.properties, setting it to WT_oz for USPS", module);
-            defaultWeightUomId = "WT_oz";
-        }
 
         for (Map.Entry<String, BigDecimal> entry: packageMap.entrySet()) {
             String productId = entry.getKey();
@@ -175,14 +170,12 @@ public class ShipmentWorker {
             BigDecimal productWeight = (BigDecimal) productInfo.get("weight");
             BigDecimal quantity = (BigDecimal) packageMap.get(productId);
 
-            // DLK - I'm not sure if this line is working. shipment_package seems to leave this value null so???
-            String weightUomId = (String) productInfo.get("weight_uom_id");
+            String weightUomId = (String) productInfo.get("weightUomId");
 
             Debug.logInfo("Product Id : " + productId.toString() + " Product Weight : " + String.valueOf(productWeight) + " Product UomId : " + weightUomId + " assuming " + defaultWeightUomId + " if null. Quantity : " + String.valueOf(quantity), module);
 
             if (UtilValidate.isEmpty(weightUomId)) {
                 weightUomId = defaultWeightUomId;
-                //  Most shipping modules assume pounds while ProductEvents.java assumes WT_oz. - Line 720 for example.
             }
             if (!"WT_lb".equals(weightUomId)) {
                 // attempt a conversion to pounds
@@ -192,13 +185,11 @@ public class ShipmentWorker {
                 } catch (GenericServiceException ex) {
                     Debug.logError(ex, module);
                 }
-
-                if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS) && result.get("convertedValue") != null) {
+                if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS) && UtilValidate.isNotEmpty(result.get("convertedValue"))) {
                     productWeight = (BigDecimal) result.get("convertedValue");
                 } else {
                     Debug.logError("Unsupported weightUom [" + weightUomId + "] for calcPackageWeight running productId " + productId + ", could not find a conversion factor to WT_lb",module);
                 }
-
             }
 
             totalWeight = totalWeight.add(productWeight.multiply(quantity));
@@ -206,17 +197,16 @@ public class ShipmentWorker {
         Debug.logInfo("Package Weight : " + String.valueOf(totalWeight) + " lbs.", module);
         return totalWeight.add(additionalWeight);
     }
-    
+
     public static Map<String, Object> getProductItemInfo(List<Map<String, Object>> shippableItemInfo, String productId) {
-        if (shippableItemInfo != null) {
-            for (Map<String, Object> testMap: shippableItemInfo) {
-                String id = (String) testMap.get("productId");
-                if (productId.equals(id)) {
-                    return testMap;
+        if (UtilValidate.isNotEmpty(shippableItemInfo)) {
+            for (Map<String, Object> itemInfoMap: shippableItemInfo) {
+                String compareProductId = (String) itemInfoMap.get("productId");
+                if (productId.equals(compareProductId)) {
+                    return itemInfoMap;
                 }
             }
         }
         return null;
     }
 }
-

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/shipment/thirdparty/ups/UpsServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/shipment/thirdparty/ups/UpsServices.java?rev=990751&r1=990750&r2=990751&view=diff
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/shipment/thirdparty/ups/UpsServices.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/shipment/thirdparty/ups/UpsServices.java Mon Aug 30 09:55:02 2010
@@ -62,6 +62,7 @@ import org.ofbiz.service.GenericServiceE
 import org.ofbiz.service.LocalDispatcher;
 import org.ofbiz.service.ModelService;
 import org.ofbiz.service.ServiceUtil;
+import org.ofbiz.shipment.shipment.ShipmentWorker;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
@@ -1658,11 +1659,11 @@ public class UpsServices {
         }
     }
 
-    private static void splitEstimatePackages(Document requestDoc, Element shipmentElement, List<Map<String, Object>> shippableItemInfo, BigDecimal maxWeight, BigDecimal minWeight) {
-        List<Map<String, BigDecimal>> packages = getPackageSplit(shippableItemInfo, maxWeight);
+    private static void splitEstimatePackages(DispatchContext dctx, Document requestDoc, Element shipmentElement, List<Map<String, Object>> shippableItemInfo, BigDecimal maxWeight, BigDecimal minWeight) {
+        List<Map<String, BigDecimal>> packages = ShipmentWorker.getPackageSplit(dctx, shippableItemInfo, maxWeight);
         if (UtilValidate.isNotEmpty(packages)) {
             for (Map<String, BigDecimal> packageMap: packages) {
-                addPackageElement(requestDoc, shipmentElement, shippableItemInfo, packageMap, minWeight);
+                addPackageElement(dctx, requestDoc, shipmentElement, shippableItemInfo, packageMap, minWeight);
             }
         } else {
 
@@ -1682,8 +1683,8 @@ public class UpsServices {
         }
     }
 
-    private static void addPackageElement(Document requestDoc, Element shipmentElement, List<Map<String, Object>> shippableItemInfo, Map<String, BigDecimal> packageMap, BigDecimal minWeight) {
-        BigDecimal packageWeight = checkForDefaultPackageWeight(calcPackageWeight(packageMap, shippableItemInfo, BigDecimal.ZERO), minWeight);
+    private static void addPackageElement(DispatchContext dctx, Document requestDoc, Element shipmentElement, List<Map<String, Object>> shippableItemInfo, Map<String, BigDecimal> packageMap, BigDecimal minWeight) {
+        BigDecimal packageWeight = checkForDefaultPackageWeight(ShipmentWorker.calcPackageWeight(dctx,packageMap, shippableItemInfo, BigDecimal.ZERO), minWeight);
         Element packageElement = UtilXml.addChildElement(shipmentElement, "Package", requestDoc);
         Element packagingTypeElement = UtilXml.addChildElement(packageElement, "PackagingType", requestDoc);
         UtilXml.addChildElementValue(packagingTypeElement, "Code", "00", requestDoc);
@@ -1695,7 +1696,7 @@ public class UpsServices {
         if (packageMap.size() ==1) {
             Iterator<String> i = packageMap.keySet().iterator();
             String productId = i.next();
-            Map<String, Object> productInfo = getProductItemInfo(shippableItemInfo, productId);
+            Map<String, Object> productInfo = ShipmentWorker.getProductItemInfo(shippableItemInfo, productId);
             if (productInfo.get("inShippingBox") != null &&  ((String) productInfo.get("inShippingBox")).equalsIgnoreCase("Y")
                     && productInfo.get("shippingDepth") !=null && productInfo.get("shippingWidth") !=null && productInfo.get("shippingHeight") !=null) {
                 Element dimensionsElement = UtilXml.addChildElement(packageElement, "Dimensions", requestDoc);
@@ -1722,91 +1723,6 @@ public class UpsServices {
         return (weight.compareTo(BigDecimal.ZERO) > 0 && weight.compareTo(minWeight) > 0 ? weight : minWeight);
     }
 
-    private static List<Map<String, BigDecimal>> getPackageSplit(List<Map<String, Object>> shippableItemInfo, BigDecimal maxWeight) {
-        // create the package list w/ the first package
-        List<Map<String, BigDecimal>> packages = FastList.newInstance();
-
-        if (shippableItemInfo != null) {
-            for (Map<String, Object> itemInfo: shippableItemInfo) {
-                long pieces = ((Long) itemInfo.get("piecesIncluded")).longValue();
-                BigDecimal totalQuantity = (BigDecimal) itemInfo.get("quantity");
-                BigDecimal totalWeight = (BigDecimal) itemInfo.get("weight");
-                String productId = (String) itemInfo.get("productId");
-
-                // sanity check
-                if (pieces < 1) {
-                    pieces = 1; // can NEVER be less than one
-                }
-                BigDecimal weight = totalWeight.divide(BigDecimal.valueOf(pieces), generalRounding);
-
-                for (int z = 1; z <= totalQuantity.intValue(); z++) {
-                    BigDecimal partialQty = pieces > 1 ? BigDecimal.ONE.divide(BigDecimal.valueOf(pieces), generalRounding) : BigDecimal.ONE;
-                    for (long x = 0; x < pieces; x++) {
-                        if (itemInfo.get("inShippingBox") != null &&  ((String) itemInfo.get("inShippingBox")).equalsIgnoreCase("Y")) {
-                            Map<String, BigDecimal> newPackage = FastMap.newInstance();
-                            newPackage.put(productId, partialQty);
-                            packages.add(newPackage);
-                        } else if (weight.compareTo(maxWeight) >= 0) {
-                            Map<String, BigDecimal> newPackage = FastMap.newInstance();
-                            newPackage.put(productId, partialQty);
-                            packages.add(newPackage);
-                        } else if (totalWeight.compareTo(BigDecimal.ZERO) > 0) {
-                            // create the first package
-                            if (packages.size() == 0) {
-                                packages.add(FastMap.<String, BigDecimal>newInstance());
-                            }
-
-                            // package loop
-                            //int packageSize = packages.size();
-                            boolean addedToPackage = false;
-                            for (Map<String, BigDecimal> packageMap: packages) {
-                                if (!addedToPackage) {
-                                    BigDecimal packageWeight = calcPackageWeight(packageMap, shippableItemInfo, weight);
-                                    if (packageWeight.compareTo(maxWeight) <= 0) {
-                                        BigDecimal qty = packageMap.get(productId);
-                                        qty = qty == null ? BigDecimal.ZERO : qty;
-                                        packageMap.put(productId, qty.add(partialQty));
-                                        addedToPackage = true;
-                                    }
-                                }
-                            }
-                            if (!addedToPackage) {
-                                Map<String, BigDecimal> packageMap = FastMap.newInstance();
-                                packageMap.put(productId, partialQty);
-                                packages.add(packageMap);
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        return packages;
-    }
-
-    private static BigDecimal calcPackageWeight(Map<String, BigDecimal> packageMap, List<Map<String, Object>> shippableItemInfo, BigDecimal additionalWeight) {
-        BigDecimal totalWeight = BigDecimal.ZERO;
-        for (Map.Entry<String, BigDecimal> entry: packageMap.entrySet()) {
-            String productId = entry.getKey();
-            Map<String, Object> productInfo = getProductItemInfo(shippableItemInfo, productId);
-            BigDecimal productWeight = (BigDecimal) productInfo.get("weight");
-            BigDecimal quantity = (BigDecimal) packageMap.get(productId);
-            totalWeight = totalWeight.add(productWeight.multiply(quantity));
-        }
-        return totalWeight.add(additionalWeight);
-    }
-
-    private static Map<String, Object> getProductItemInfo(List<Map<String, Object>> shippableItemInfo, String productId) {
-        if (shippableItemInfo != null) {
-            for (Map<String, Object> testMap: shippableItemInfo) {
-                String id = (String) testMap.get("productId");
-                if (productId.equals(id)) {
-                    return testMap;
-                }
-            }
-        }
-        return null;
-    }
-
     public static Map<String, Object> handleUpsRateInquireResponse(Document rateResponseDocument) {
         // process TrackResponse, update data as needed
         Element rateResponseElement = rateResponseDocument.getDocumentElement();
@@ -2138,7 +2054,7 @@ public class UpsServices {
 
         // Passing in a list of package weights overrides the calculation of same via shippableItemInfo
         if (UtilValidate.isEmpty(packageWeights)) {
-            splitEstimatePackages(rateRequestDoc, shipmentElement, shippableItemInfo, maxWeight, minWeight);
+            splitEstimatePackages(dctx, rateRequestDoc, shipmentElement, shippableItemInfo, maxWeight, minWeight);
         } else {
             for (BigDecimal packageWeight: packageWeights) {
                 addPackageElement(rateRequestDoc,  shipmentElement, packageWeight);

Modified: ofbiz/trunk/framework/common/data/UnitData.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/data/UnitData.xml?rev=990751&r1=990750&r2=990751&view=diff
==============================================================================
--- ofbiz/trunk/framework/common/data/UnitData.xml (original)
+++ ofbiz/trunk/framework/common/data/UnitData.xml Mon Aug 30 09:55:02 2010
@@ -265,6 +265,7 @@ under the License.
     <UomConversion uomId="WT_gr" uomIdTo="WT_g" conversionFactor="0.0648"/>
     <UomConversion uomId="WT_kg" uomIdTo="WT_g" conversionFactor="1000"/>
     <UomConversion uomId="WT_kg" uomIdTo="WT_lb" conversionFactor="2.2"/>
+    <UomConversion uomId="WT_g" uomIdTo="WT_kg" conversionFactor="0.001"/>
     <UomConversion uomId="WT_g" uomIdTo="WT_mg" conversionFactor="1000"/>
     <UomConversion uomId="WT_g" uomIdTo="WT_oz" conversionFactor="0.03527"/>
     <UomConversion uomId="WT_g" uomIdTo="WT_lb" conversionFactor="0.00220462247604"/>