You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ja...@apache.org on 2007/04/28 11:44:43 UTC

svn commit: r533322 - in /ofbiz/trunk/applications: order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java product/src/org/ofbiz/product/product/ProductWorker.java

Author: jacopoc
Date: Sat Apr 28 02:44:43 2007
New Revision: 533322

URL: http://svn.apache.org/viewvc?view=rev&rev=533322
Log:
Implemented product geo rules for shipping address geo.

Modified:
    ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java
    ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java

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?view=diff&rev=533322&r1=533321&r2=533322
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java (original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCart.java Sat Apr 28 02:44:43 2007
@@ -504,7 +504,7 @@
             // If the billing address is already set, verify if the new product
             // is available in the address' geo
             GenericValue product = item.getProduct();
-            if (product != null) {
+            if (product != null && isSalesOrder()) {
                 GenericValue billingAddress = this.getBillingAddress();
                 if (billingAddress != null) {
                     if (!ProductWorker.isBillableToAddress(product, billingAddress)) {
@@ -1440,16 +1440,18 @@
     /** adds a payment method/payment method type */
     public CartPaymentInfo addPaymentAmount(String id, Double amount, String refNum, String authCode, boolean isSingleUse, boolean isPresent, boolean replace) {
         CartPaymentInfo inf = this.getPaymentInfo(id, refNum, authCode, amount, true);
-        GenericValue billingAddress = inf.getBillingAddress(this.getDelegator());
-        if (billingAddress != null) {
-            // this payment method will set the billing address for the order;
-            // before it is set we have to verify if the billing address is
-            // compatible with the ProductGeos
-            Iterator products = (this.getItemsProducts(this.cartLines)).iterator();
-            while (products.hasNext()) {
-                GenericValue product = (GenericValue)products.next();
-                if (!ProductWorker.isBillableToAddress(product, billingAddress)) {
-                    throw new IllegalArgumentException("The billing address is not compatible with ProductGeos rules.");            
+        if (isSalesOrder()) {
+            GenericValue billingAddress = inf.getBillingAddress(this.getDelegator());
+            if (billingAddress != null) {
+                // this payment method will set the billing address for the order;
+                // before it is set we have to verify if the billing address is
+                // compatible with the ProductGeos
+                Iterator products = (this.getItemsProducts(this.cartLines)).iterator();
+                while (products.hasNext()) {
+                    GenericValue product = (GenericValue)products.next();
+                    if (!ProductWorker.isBillableToAddress(product, billingAddress)) {
+                        throw new IllegalArgumentException("The billing address is not compatible with ProductGeos rules.");
+                    }
                 }
             }
         }
@@ -1832,7 +1834,9 @@
     // ----------------------------------------
 
     public int addShipInfo() {
-        shipInfo.add(new CartShipInfo());
+        CartShipInfo csi = new CartShipInfo();
+        csi.orderTypeId = getOrderType();
+        shipInfo.add(csi);
         return (shipInfo.size() - 1);
     }
 
@@ -1866,7 +1870,9 @@
         }
 
         if (shipInfo.size() == 0) {
-            shipInfo.add(new CartShipInfo());
+            CartShipInfo csi = new CartShipInfo();
+            csi.orderTypeId = getOrderType();
+            shipInfo.add(csi);
         }
 
         return (CartShipInfo) shipInfo.get(idx);
@@ -1988,6 +1994,7 @@
         CartShipInfo toGroup = null;
         if (toIndex == -1) {
             toGroup = new CartShipInfo();
+            toGroup.orderTypeId = getOrderType();
             this.shipInfo.add(toGroup);
             toIndex = this.shipInfo.size() - 1;
         } else {
@@ -2043,6 +2050,31 @@
     /** Sets the shipping contact mech id. */
     public void setShippingContactMechId(int idx, String shippingContactMechId) {
         CartShipInfo csi = this.getShipInfo(idx);
+        if (isSalesOrder()) {
+            // Verify if the new address is compatible with the ProductGeos rules of
+            // the products already in the cart
+            GenericValue shippingAddress = null;
+            try {
+                shippingAddress = this.getDelegator().findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", shippingContactMechId));
+            } catch(GenericEntityException gee) {
+                Debug.logError(gee, "Error retrieving the shipping address for contactMechId [" + shippingContactMechId + "].", module);
+            }
+            if (shippingAddress != null) {
+                Set shipItems = csi.getShipItems();
+                if (UtilValidate.isNotEmpty(shipItems)) {
+                    Iterator siit = shipItems.iterator();
+                    while (siit.hasNext()) {
+                        ShoppingCartItem cartItem = (ShoppingCartItem) siit.next();
+                        GenericValue product = cartItem.getProduct();
+                        if (UtilValidate.isNotEmpty(product)) {
+                            if (!ProductWorker.isShippableToAddress(product, shippingAddress)) {
+                                throw new IllegalArgumentException("The shipping address is not compatible with ProductGeos rules.");
+                            }
+                        }
+                    }
+                }
+            }
+        }
         csi.contactMechId = shippingContactMechId;
     }
 
@@ -3913,6 +3945,7 @@
     public static class CartShipInfo implements Serializable {
         public LinkedMap shipItemInfo = new LinkedMap();
         public List shipTaxAdj = new LinkedList();
+        public String orderTypeId = null;
         public String contactMechId = null;
         public String shipmentMethodTypeId = null;
         public String supplierPartyId = null;
@@ -3926,6 +3959,7 @@
         public Timestamp shipBeforeDate = null;
         public Timestamp shipAfterDate = null;
 
+        public String getOrderTypeId() { return orderTypeId; }
         public String getContactMechId() { return contactMechId; }
         public String getCarrierPartyId() { return carrierPartyId; }
         public String getSupplierPartyId() { return supplierPartyId; }
@@ -4015,6 +4049,9 @@
         public CartShipItemInfo setItemInfo(ShoppingCartItem item, double quantity, List taxAdj) {
             CartShipItemInfo itemInfo = (CartShipItemInfo) shipItemInfo.get(item);
             if (itemInfo == null) {
+                if (!isShippableToAddress(item)) {
+                    throw new IllegalArgumentException("The shipping address is not compatible with ProductGeos rules.");
+                }
                 itemInfo = new CartShipItemInfo();
                 itemInfo.item = item;
                 shipItemInfo.put(item, itemInfo);
@@ -4031,6 +4068,9 @@
         public CartShipItemInfo setItemInfo(ShoppingCartItem item, List taxAdj) {
             CartShipItemInfo itemInfo = (CartShipItemInfo) shipItemInfo.get(item);
             if (itemInfo == null) {
+                if (!isShippableToAddress(item)) {
+                    throw new IllegalArgumentException("The shipping address is not compatible with ProductGeos rules.");
+                }
                 itemInfo = new CartShipItemInfo();
                 itemInfo.item = item;
                 shipItemInfo.put(item, itemInfo);
@@ -4046,6 +4086,9 @@
         public CartShipItemInfo setItemInfo(ShoppingCartItem item, double quantity) {
             CartShipItemInfo itemInfo = (CartShipItemInfo) shipItemInfo.get(item);
             if (itemInfo == null) {
+                if (!isShippableToAddress(item)) {
+                    throw new IllegalArgumentException("The shipping address is not compatible with ProductGeos rules.");
+                }
                 itemInfo = new CartShipItemInfo();
                 itemInfo.item = item;
                 shipItemInfo.put(item, itemInfo);
@@ -4060,6 +4103,26 @@
 
         public Set getShipItems() {
             return shipItemInfo.keySet();
+        }
+
+        private boolean isShippableToAddress(ShoppingCartItem item) {
+            if ("SALES_ORDER".equals(getOrderTypeId())) {
+                // Verify if the new address is compatible with the ProductGeos rules of
+                // the products already in the cart
+                GenericValue shippingAddress = null;
+                try {
+                    shippingAddress = item.getDelegator().findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", contactMechId));
+                } catch(GenericEntityException gee) {
+                    Debug.logError(gee, "Error retrieving the shipping address for contactMechId [" + contactMechId + "].", module);
+                }
+                if (shippingAddress != null) {
+                    GenericValue product = item.getProduct();
+                    if (UtilValidate.isNotEmpty(product)) {
+                        return ProductWorker.isShippableToAddress(product, shippingAddress);
+                    }
+                }
+            }
+            return true;
         }
 
         /**

Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java?view=diff&rev=533322&r1=533321&r2=533322
==============================================================================
--- ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java (original)
+++ ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java Sat Apr 28 02:44:43 2007
@@ -93,23 +93,27 @@
             } catch (GenericEntityException e) {
                 Debug.logError(e, module);
             }
-            if (UtilValidate.isEmpty(productGeos)) {
+            List excludeGeos = EntityUtil.filterByAnd(productGeos, UtilMisc.toMap("productGeoEnumId", productGeoPrefix + "EXCLUDE"));
+            List includeGeos = EntityUtil.filterByAnd(productGeos, UtilMisc.toMap("productGeoEnumId", productGeoPrefix + "INCLUDE"));
+            if (UtilValidate.isEmpty(excludeGeos) && UtilValidate.isEmpty(includeGeos)) {
                 // If no GEOs are configured the default is TRUE
                 return true;
             }
-            List excludeGeos = EntityUtil.filterByAnd(productGeos, UtilMisc.toMap("productGeoEnumId", productGeoPrefix + "EXCLUDE"));
-            List includeGeos = EntityUtil.filterByAnd(productGeos, UtilMisc.toMap("productGeoEnumId", productGeoPrefix + "INCLUDE"));
             Iterator productGeosIt = null;
             // exclusion
             productGeosIt = excludeGeos.iterator();
             while (productGeosIt.hasNext()) {
                 GenericValue productGeo = (GenericValue)productGeosIt.next();
-                List includeGeoGroup = GeoWorker.expandGeoGroup(productGeo.getString("geoId"), delegator);
-                if (GeoWorker.containsGeo(includeGeoGroup, postalAddress.getString("countryGeoId"), delegator) ||
-                      GeoWorker.containsGeo(includeGeoGroup, postalAddress.getString("stateProvinceGeoId"), delegator) ||
-                      GeoWorker.containsGeo(includeGeoGroup, postalAddress.getString("postalCodeGeoId"), delegator)) {
+                List excludeGeoGroup = GeoWorker.expandGeoGroup(productGeo.getString("geoId"), delegator);
+                if (GeoWorker.containsGeo(excludeGeoGroup, postalAddress.getString("countryGeoId"), delegator) ||
+                      GeoWorker.containsGeo(excludeGeoGroup, postalAddress.getString("stateProvinceGeoId"), delegator) ||
+                      GeoWorker.containsGeo(excludeGeoGroup, postalAddress.getString("postalCodeGeoId"), delegator)) {
                     return false;
                 }
+            }
+            if (UtilValidate.isEmpty(includeGeos)) {
+                // If no GEOs are configured the default is TRUE
+                return true;
             }
             // inclusion
             productGeosIt = includeGeos.iterator();