1 24 package org.ofbiz.order.shoppingcart.shipping; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 33 import org.ofbiz.base.util.Debug; 34 import org.ofbiz.base.util.GeneralException; 35 import org.ofbiz.base.util.UtilValidate; 36 import org.ofbiz.entity.GenericDelegator; 37 import org.ofbiz.entity.GenericValue; 38 import org.ofbiz.order.order.OrderReadHelper; 39 import org.ofbiz.order.shoppingcart.ShoppingCart; 40 import org.ofbiz.product.store.ProductStoreWorker; 41 import org.ofbiz.service.GenericServiceException; 42 import org.ofbiz.service.LocalDispatcher; 43 import org.ofbiz.service.ModelService; 44 import org.ofbiz.service.ServiceUtil; 45 import org.ofbiz.base.util.UtilMisc; 46 47 54 public class ShippingEvents { 55 56 public static final String module = ShippingEvents.class.getName(); 57 58 public static String getShipEstimate(HttpServletRequest request, HttpServletResponse response) { 59 ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart"); 60 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 61 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 62 63 int shipGroups = cart.getShipGroupSize(); 64 for (int i = 0; i < shipGroups; i++) { 65 Map result = getShipGroupEstimate(dispatcher, delegator, cart, i); 66 ServiceUtil.getMessages(request, result, null, "", "", "", "", null, null); 67 if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) { 68 return "error"; 69 } 70 71 Double shippingTotal = (Double ) result.get("shippingTotal"); 72 if (shippingTotal == null) { 73 shippingTotal = new Double (0.00); 74 } 75 cart.setItemShipGroupEstimate(shippingTotal.doubleValue(), i); 76 } 77 78 return "success"; 80 } 81 82 public static Map getShipGroupEstimate(LocalDispatcher dispatcher, GenericDelegator delegator, ShoppingCart cart, int groupNo) { 83 if (!cart.shippingApplies()) { 85 Map responseResult = ServiceUtil.returnSuccess(); 86 responseResult.put("shippingTotal", new Double (0.00)); 87 return responseResult; 88 } 89 90 String shipmentMethodTypeId = cart.getShipmentMethodTypeId(groupNo); 91 String carrierPartyId = cart.getCarrierPartyId(groupNo); 92 93 return getShipGroupEstimate(dispatcher, delegator, cart.getOrderType(), shipmentMethodTypeId, carrierPartyId, null, 94 cart.getShippingContactMechId(groupNo), cart.getProductStoreId(), cart.getShippableItemInfo(groupNo), 95 cart.getShippableWeight(groupNo), cart.getShippableQuantity(groupNo), cart.getShippableTotal(groupNo)); 96 } 97 98 public static Map getShipEstimate(LocalDispatcher dispatcher, GenericDelegator delegator, OrderReadHelper orh, String shipGroupSeqId) { 99 if (!orh.shippingApplies()) { 101 Map responseResult = ServiceUtil.returnSuccess(); 102 responseResult.put("shippingTotal", new Double (0.00)); 103 return responseResult; 104 } 105 106 GenericValue shipGroup = orh.getOrderItemShipGroup(shipGroupSeqId); 107 String shipmentMethodTypeId = shipGroup.getString("shipmentMethodTypeId"); 108 String carrierRoleTypeId = shipGroup.getString("carrierRoleTypeId"); 109 String carrierPartyId = shipGroup.getString("carrierPartyId"); 110 111 GenericValue shipAddr = orh.getShippingAddress(shipGroupSeqId); 112 if (shipAddr == null) { 113 return UtilMisc.toMap("shippingTotal", new Double (0)); 114 } 115 116 String contactMechId = shipAddr.getString("contactMechId"); 117 return getShipGroupEstimate(dispatcher, delegator, orh.getOrderTypeId(), shipmentMethodTypeId, carrierPartyId, carrierRoleTypeId, 118 contactMechId, orh.getProductStoreId(), orh.getShippableItemInfo(shipGroupSeqId), orh.getShippableWeight(shipGroupSeqId), 119 orh.getShippableQuantity(shipGroupSeqId), orh.getShippableTotal(shipGroupSeqId)); 120 } 121 122 public static Map getShipGroupEstimate(LocalDispatcher dispatcher, GenericDelegator delegator, String orderTypeId, 123 String shipmentMethodTypeId, String carrierPartyId, String carrierRoleTypeId, String shippingContactMechId, 124 String productStoreId, List itemInfo, double shippableWeight, double shippableQuantity, 125 double shippableTotal) { 126 String standardMessage = "A problem occurred calculating shipping. Fees will be calculated offline."; 127 List errorMessageList = new ArrayList (); 128 129 if (shipmentMethodTypeId == null || carrierPartyId == null) { 130 if ("SALES_ORDER".equals(orderTypeId)) { 131 errorMessageList.add("Please Select Your Shipping Method."); 132 return ServiceUtil.returnError(errorMessageList); 133 } else { 134 return ServiceUtil.returnSuccess(); 135 } 136 } 137 138 if (carrierRoleTypeId == null) { 139 carrierRoleTypeId = "CARRIER"; 140 } 141 142 if (shippingContactMechId == null) { 143 errorMessageList.add("Please Select Your Shipping Address."); 144 return ServiceUtil.returnError(errorMessageList); 145 } 146 147 if (shippableQuantity == 0) { 149 Map result = ServiceUtil.returnSuccess(); 150 result.put("shippingTotal", new Double (0)); 151 return result; 152 } 153 154 GenericValue storeShipMethod = ProductStoreWorker.getProductStoreShipmentMethod(delegator, productStoreId, 156 shipmentMethodTypeId, carrierPartyId, carrierRoleTypeId); 157 158 if (storeShipMethod == null) { 159 errorMessageList.add("No applicable shipment method found."); 160 return ServiceUtil.returnError(errorMessageList); 161 } 162 163 double shippingTotal = 0.00; 165 166 Map serviceFields = new HashMap (); 168 serviceFields.put("initialEstimateAmt", new Double (shippingTotal)); 169 serviceFields.put("shippableTotal", new Double (shippableTotal)); 170 serviceFields.put("shippableQuantity", new Double (shippableQuantity)); 171 serviceFields.put("shippableWeight", new Double (shippableWeight)); 172 serviceFields.put("shippableItemInfo", itemInfo); 173 serviceFields.put("productStoreId", productStoreId); 174 serviceFields.put("carrierRoleTypeId", "CARRIER"); 175 serviceFields.put("carrierPartyId", carrierPartyId); 176 serviceFields.put("shipmentMethodTypeId", shipmentMethodTypeId); 177 serviceFields.put("shippingContactMechId", shippingContactMechId); 178 179 try { 181 Double externalAmt = getExternalShipEstimate(dispatcher, storeShipMethod, serviceFields); 182 if (externalAmt != null) { 183 shippingTotal += externalAmt.doubleValue(); 184 } 185 } catch (GeneralException e) { 186 return ServiceUtil.returnSuccess(standardMessage); 187 } 188 189 serviceFields.put("initialEstimateAmt", new Double (shippingTotal)); 191 192 try { 194 Double genericAmt = getGenericShipEstimate(dispatcher, storeShipMethod, serviceFields); 195 if (genericAmt != null) { 196 shippingTotal += genericAmt.doubleValue(); 197 } 198 } catch (GeneralException e) { 199 return ServiceUtil.returnSuccess(standardMessage); 200 } 201 202 Map responseResult = ServiceUtil.returnSuccess(); 204 responseResult.put("shippingTotal", new Double (shippingTotal)); 205 return responseResult; 206 } 207 208 public static Double getGenericShipEstimate(LocalDispatcher dispatcher, GenericValue storeShipMeth, Map context) throws GeneralException { 209 Map genericEstimate = null; 211 Double genericShipAmt = null; 212 try { 213 genericEstimate = dispatcher.runSync("calcShipmentCostEstimate", context); 214 } catch (GenericServiceException e) { 215 Debug.logError(e, "Shipment Service Error", module); 216 throw new GeneralException(); 217 } 218 if (ServiceUtil.isError(genericEstimate)) { 219 Debug.logError(ServiceUtil.getErrorMessage(genericEstimate), module); 220 throw new GeneralException(); 221 } else { 222 genericShipAmt = (Double ) genericEstimate.get("shippingEstimateAmount"); 223 } 224 return genericShipAmt; 225 } 226 227 public static Double getExternalShipEstimate(LocalDispatcher dispatcher, GenericValue storeShipMeth, Map context) throws GeneralException { 228 Double externalShipAmt = null; 230 if (storeShipMeth.get("serviceName") != null) { 231 String serviceName = storeShipMeth.getString("serviceName"); 232 String configProps = storeShipMeth.getString("configProps"); 233 if (UtilValidate.isNotEmpty(serviceName)) { 234 context.put("serviceConfigProps", configProps); 236 237 Map serviceResp = null; 239 try { 240 Debug.log("Service : " + serviceName + " / " + configProps + " -- " + context, module); 241 serviceResp = dispatcher.runSync(serviceName, context, 0, true); 243 } catch (GenericServiceException e) { 244 Debug.logError(e, "Shipment Service Error", module); 245 throw new GeneralException(); 246 } 247 if (!ServiceUtil.isError(serviceResp)) { 248 externalShipAmt = (Double ) serviceResp.get("shippingEstimateAmount"); 249 } else { 250 String errMsg = "Error getting external shipment cost estimate: " + ServiceUtil.getErrorMessage(serviceResp); 251 Debug.logError(errMsg, module); 252 throw new GeneralException(errMsg); 253 } 254 } 255 } 256 return externalShipAmt; 257 } 258 } 259 260 | Popular Tags |