Author: sichen
Date: Fri Mar 7 14:30:28 2008 New Revision: 634851 URL: http://svn.apache.org/viewvc?rev=634851&view=rev Log: Added support for calculating shipment estimates from any origin postal address via calcShipmentEstimateInterface.shippingOriginContactMechId. Use this to implement estimation of shipping between a supplier and a customer for drop shipping instead of between the product store facility and customer. UPS implementation included. Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/shipping/ShippingEstimateWrapper.java ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/shipping/ShippingEvents.java ofbiz/trunk/applications/product/servicedef/services_shipment.xml ofbiz/trunk/applications/product/servicedef/services_shipment_ups.xml ofbiz/trunk/applications/product/src/org/ofbiz/shipment/thirdparty/ups/UpsServices.java Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/shipping/ShippingEstimateWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/shipping/ShippingEstimateWrapper.java?rev=634851&r1=634850&r2=634851&view=diff ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/shipping/ShippingEstimateWrapper.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/shipping/ShippingEstimateWrapper.java Fri Mar 7 14:30:28 2008 @@ -50,6 +50,7 @@ protected double shippableWeight = 0; protected double shippableTotal = 0; protected String partyId = null; + protected String supplierPartyId = null; public static ShippingEstimateWrapper getWrapper(LocalDispatcher dispatcher, ShoppingCart cart, int shipGroup) { return new ShippingEstimateWrapper(dispatcher, cart, shipGroup); @@ -68,6 +69,7 @@ this.shippingAddress = cart.getShippingAddress(shipGroup); this.productStoreId = cart.getProductStoreId(); this.partyId = cart.getPartyId(); + this.supplierPartyId = cart.getSupplierPartyId(shipGroup); this.loadShippingMethods(); this.loadEstimates(); @@ -95,7 +97,7 @@ Map estimateMap = ShippingEvents.getShipGroupEstimate(dispatcher, delegator, "SALES_ORDER", shippingMethodTypeId, carrierPartyId, carrierRoleTypeId, shippingCmId, productStoreId, - shippableItemInfo, shippableWeight, shippableQuantity, shippableTotal, partyId); + supplierPartyId, shippableItemInfo, shippableWeight, shippableQuantity, shippableTotal, partyId); if (!ServiceUtil.isError(estimateMap)) { Double shippingTotal = (Double) estimateMap.get("shippingTotal"); Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/shipping/ShippingEvents.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/shipping/ShippingEvents.java?rev=634851&r1=634850&r2=634851&view=diff ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/shipping/ShippingEvents.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/shipping/ShippingEvents.java Fri Mar 7 14:30:28 2008 @@ -27,10 +27,14 @@ import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; +import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.UtilProperties; import org.ofbiz.entity.GenericDelegator; import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.condition.EntityExpr; +import org.ofbiz.entity.condition.EntityOperator; +import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.order.order.OrderReadHelper; import org.ofbiz.order.shoppingcart.ShoppingCart; import org.ofbiz.product.store.ProductStoreWorker; @@ -38,7 +42,13 @@ import org.ofbiz.service.LocalDispatcher; import org.ofbiz.service.ModelService; import org.ofbiz.service.ServiceUtil; -import org.ofbiz.base.util.UtilMisc; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * ShippingEvents - Events used for processing shipping fees @@ -87,7 +97,7 @@ String carrierPartyId = cart.getCarrierPartyId(groupNo); return getShipGroupEstimate(dispatcher, delegator, cart.getOrderType(), shipmentMethodTypeId, carrierPartyId, null, - cart.getShippingContactMechId(groupNo), cart.getProductStoreId(), cart.getShippableItemInfo(groupNo), + cart.getShippingContactMechId(groupNo), cart.getProductStoreId(), cart.getSupplierPartyId(groupNo), cart.getShippableItemInfo(groupNo), cart.getShippableWeight(groupNo), cart.getShippableQuantity(groupNo), cart.getShippableTotal(groupNo), cart.getPartyId()); } @@ -103,6 +113,7 @@ String shipmentMethodTypeId = shipGroup.getString("shipmentMethodTypeId"); String carrierRoleTypeId = shipGroup.getString("carrierRoleTypeId"); String carrierPartyId = shipGroup.getString("carrierPartyId"); + String supplierPartyId = shipGroup.getString("supplierPartyId"); GenericValue shipAddr = orh.getShippingAddress(shipGroupSeqId); if (shipAddr == null) { @@ -116,14 +127,24 @@ partyId = partyObject.getString("partyId"); } return getShipGroupEstimate(dispatcher, delegator, orh.getOrderTypeId(), shipmentMethodTypeId, carrierPartyId, carrierRoleTypeId, - contactMechId, orh.getProductStoreId(), orh.getShippableItemInfo(shipGroupSeqId), orh.getShippableWeight(shipGroupSeqId).doubleValue(), + contactMechId, orh.getProductStoreId(), supplierPartyId, orh.getShippableItemInfo(shipGroupSeqId), orh.getShippableWeight(shipGroupSeqId).doubleValue(), orh.getShippableQuantity(shipGroupSeqId).doubleValue(), orh.getShippableTotal(shipGroupSeqId).doubleValue(), partyId); } + // version with no support for using the supplier's address as the origin public static Map getShipGroupEstimate(LocalDispatcher dispatcher, GenericDelegator delegator, String orderTypeId, String shipmentMethodTypeId, String carrierPartyId, String carrierRoleTypeId, String shippingContactMechId, String productStoreId, List itemInfo, double shippableWeight, double shippableQuantity, double shippableTotal, String partyId) { + return getShipGroupEstimate(dispatcher, delegator, orderTypeId, shipmentMethodTypeId, carrierPartyId, + carrierRoleTypeId, shippingContactMechId, productStoreId, null, itemInfo, + shippableWeight, shippableQuantity, shippableTotal, partyId); + } + + public static Map getShipGroupEstimate(LocalDispatcher dispatcher, GenericDelegator delegator, String orderTypeId, + String shipmentMethodTypeId, String carrierPartyId, String carrierRoleTypeId, String shippingContactMechId, + String productStoreId, String supplierPartyId, List itemInfo, double shippableWeight, double shippableQuantity, + double shippableTotal, String partyId) { String standardMessage = "A problem occurred calculating shipping. Fees will be calculated offline."; List errorMessageList = new ArrayList(); @@ -147,6 +168,20 @@ // return ServiceUtil.returnError(errorMessageList); // } + // if as supplier is associated, then we have a drop shipment and should use the origin shipment address of it + String shippingOriginContactMechId = null; + if (supplierPartyId != null) { + try { + GenericValue originAddress = getShippingOriginContactMech(delegator, supplierPartyId); + if (originAddress == null) { + return ServiceUtil.returnError("Cannot find the origin shipping address (SHIP_ORIG_LOCATION) for the supplier with ID ["+supplierPartyId+"]. Will not be able to calculate drop shipment estimate."); + } + shippingOriginContactMechId = originAddress.getString("contactMechId"); + } catch (GeneralException e) { + return ServiceUtil.returnError(standardMessage); + } + } + // no shippable items; we won't change any shipping at all if (shippableQuantity == 0) { Map result = ServiceUtil.returnSuccess(); @@ -178,6 +213,7 @@ serviceFields.put("carrierPartyId", carrierPartyId); serviceFields.put("shipmentMethodTypeId", shipmentMethodTypeId); serviceFields.put("shippingContactMechId", shippingContactMechId); + serviceFields.put("shippingOriginContactMechId", shippingOriginContactMechId); serviceFields.put("partyId", partyId); // call the external shipping service @@ -276,6 +312,30 @@ } } return externalShipAmt; + } + + /** + * Attempts to get the supplier's shipping origin address and failing that, the general location. + */ + public static GenericValue getShippingOriginContactMech(GenericDelegator delegator, String supplierPartyId) throws GeneralException { + List conditions = UtilMisc.toList( + new EntityExpr("partyId", EntityOperator.EQUALS, supplierPartyId), + new EntityExpr("contactMechTypeId", EntityOperator.EQUALS, "POSTAL_ADDRESS"), + new EntityExpr("contactMechPurposeTypeId", EntityOperator.IN, UtilMisc.toList("SHIP_ORIG_LOCATION", "GENERAL_LOCATION")), + EntityUtil.getFilterByDateExpr("contactFromDate", "contactThruDate"), + EntityUtil.getFilterByDateExpr("purposeFromDate", "purposeThruDate") + ); + List<GenericValue> addresses = delegator.findByAnd("PartyContactWithPurpose", conditions, UtilMisc.toList("contactMechPurposeTypeId DESC")); + + GenericValue generalAddress = null; + GenericValue originAddress = null; + for (GenericValue address : addresses) { + if ("GENERAL_LOCATION".equals(address.get("contactMechPurposeTypeId"))) + generalAddress = address; + else if ("SHIP_ORIG_LOCATION".equals(address.get("contactMechPurposeTypeId"))) + originAddress = address; + } + return originAddress != null ? originAddress : generalAddress; } } Modified: ofbiz/trunk/applications/product/servicedef/services_shipment.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/servicedef/services_shipment.xml?rev=634851&r1=634850&r2=634851&view=diff ============================================================================== --- ofbiz/trunk/applications/product/servicedef/services_shipment.xml (original) +++ ofbiz/trunk/applications/product/servicedef/services_shipment.xml Fri Mar 7 14:30:28 2008 @@ -632,6 +632,7 @@ <attribute name="serviceConfigProps" type="String" mode="IN" optional="true"/> <attribute name="initialEstimateAmt" type="Double" mode="IN" optional="true"/> <attribute name="shippingContactMechId" type="String" mode="IN" optional="true"/> + <attribute name="shippingOriginContactMechId" type="String" mode="IN" optional="true"/> <attribute name="shippingPostalCode" type="String" mode="IN" optional="true"/> <attribute name="shippingCountryCode" type="String" mode="IN" optional="true"/> <attribute name="shipmentMethodTypeId" type="String" mode="IN" optional="false"/> Modified: ofbiz/trunk/applications/product/servicedef/services_shipment_ups.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/servicedef/services_shipment_ups.xml?rev=634851&r1=634850&r2=634851&view=diff ============================================================================== --- ofbiz/trunk/applications/product/servicedef/services_shipment_ups.xml (original) +++ ofbiz/trunk/applications/product/servicedef/services_shipment_ups.xml Fri Mar 7 14:30:28 2008 @@ -83,6 +83,8 @@ <attribute name="upsRateCodeMap" type="Map" mode="OUT" optional="false"/> <attribute name="isResidentialAddress" type="String" mode="IN" optional="true"/> <attribute name="shippingCountryCode" type="String" mode="IN" optional="true"/> + <!-- Allow specifying a from address if different from product store's facility address. --> + <attribute name="shipFromAddress" type="GenericValue" mode="IN" optional="true"/> </service> <!-- UPS Address Validation --> 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=634851&r1=634850&r2=634851&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 Fri Mar 7 14:30:28 2008 @@ -1483,6 +1483,7 @@ GenericDelegator delegator = dctx.getDelegator(); // prepare the data String shippingContactMechId = (String) context.get("shippingContactMechId"); + String shippingOriginContactMechId = (String) context.get("shippingOriginContactMechId"); // obtain the ship-to address GenericValue shipToAddress = null; if (shippingContactMechId != null) { @@ -1496,6 +1497,17 @@ return ServiceUtil.returnError("Unable to determine ship-to address"); } + // obtain the ship from address if provided + GenericValue shipFromAddress = null; + if (shippingOriginContactMechId != null) { + try { + shipFromAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", shippingOriginContactMechId)); + } catch (GenericEntityException e) { + Debug.logError(e, module); + return ServiceUtil.returnError("Unable to determine ship-from address for drop shipping"); + } + } + GenericValue destCountryGeo = null; try { destCountryGeo = shipToAddress.getRelatedOne("CountryGeo"); @@ -1518,6 +1530,7 @@ cxt.put("shippableQuantity", context.get("shippableQuantity")); cxt.put("shippableWeight", context.get("shippableWeight")); cxt.put("isResidentialAddress", context.get("isResidentialAddress")); + cxt.put("shipFromAddress", shipFromAddress); try{ return dctx.getDispatcher().runSync("upsRateEstimateByPostalCode", cxt); @@ -1914,28 +1927,31 @@ // grab the pickup type; if none is defined we will assume daily pickup String pickupType = UtilProperties.getPropertyValue(serviceConfigProps, "shipment.ups.shipper.pickup.type", "01"); - - // locate the ship-from address based on the product store's default facility - GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator); - - GenericValue shipFromAddress = null; - if (productStore != null && productStore.get("inventoryFacilityId") != null) { - List shipLocs = null; - try { - shipLocs = delegator.findByAnd("FacilityContactMechPurpose", UtilMisc.toMap("facilityId", - productStore.getString("inventoryFacilityId"), "contactMechPurposeTypeId", - "SHIP_ORIG_LOCATION"), UtilMisc.toList("-fromDate")); - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - if (shipLocs != null) { - shipLocs = EntityUtil.filterByDate(shipLocs); - GenericValue purp = EntityUtil.getFirst(shipLocs); - if (purp != null) { - try { - shipFromAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", purp.getString("contactMechId"))); - } catch (GenericEntityException e) { - Debug.logError(e, module); + + // if we're drop shipping from a supplier, then the address is given to us + GenericValue shipFromAddress = (GenericValue) context.get("shipFromAddress"); + if (shipFromAddress == null) { + + // locate the ship-from address based on the product store's default facility + GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator); + if (productStore != null && productStore.get("inventoryFacilityId") != null) { + List shipLocs = null; + try { + shipLocs = delegator.findByAnd("FacilityContactMechPurpose", UtilMisc.toMap("facilityId", + productStore.getString("inventoryFacilityId"), "contactMechPurposeTypeId", + "SHIP_ORIG_LOCATION"), UtilMisc.toList("-fromDate")); + } catch (GenericEntityException e) { + Debug.logError(e, module); + } + if (shipLocs != null) { + shipLocs = EntityUtil.filterByDate(shipLocs); + GenericValue purp = EntityUtil.getFirst(shipLocs); + if (purp != null) { + try { + shipFromAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", purp.getString("contactMechId"))); + } catch (GenericEntityException e) { + Debug.logError(e, module); + } } } } @@ -1943,19 +1959,6 @@ if (shipFromAddress == null) { return ServiceUtil.returnError("Unable to determine ship-from address"); } - - // obtain the ship-to address - /*GenericValue shipToAddress = null; - if (shippingContactMechId != null) { - try { - shipToAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", shippingContactMechId)); - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - } - if (shipToAddress == null) { - return ServiceUtil.returnError("Unable to determine ship-to address"); - }*/ // locate the service code String serviceCode = null; |
Free forum by Nabble | Edit this page |