📄 shipmentservices.java
字号:
/* * $Id: ShipmentServices.java 7017 2006-03-19 07:49:12Z jacopo $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.shipment.shipment;import java.util.*;import javolution.util.FastMap;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.StringUtil;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.common.geo.GeoWorker;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.EntityListIterator;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ModelService;import org.ofbiz.service.ServiceUtil;/** * ShipmentServices * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 7017 $ * @since 2.0 */public class ShipmentServices { public static final String module = ShipmentServices.class.getName(); public static Map createShipmentEstimate(DispatchContext dctx, Map context) { Map result = new HashMap(); GenericDelegator delegator = dctx.getDelegator(); List storeAll = new ArrayList(); String shipMethodAndParty = (String) context.get("shipMethod"); List shipMethodSplit = StringUtil.split(shipMethodAndParty, "|"); // Create the basic entity. GenericValue estimate = delegator.makeValue("ShipmentCostEstimate", null); estimate.set("shipmentCostEstimateId", delegator.getNextSeqId("ShipmentCostEstimate")); estimate.set("shipmentMethodTypeId", shipMethodSplit.get(1)); estimate.set("carrierPartyId", shipMethodSplit.get(0)); estimate.set("carrierRoleTypeId", "CARRIER"); estimate.set("productStoreId", context.get("productStoreId")); estimate.set("geoIdTo", context.get("toGeo")); estimate.set("geoIdFrom", context.get("fromGeo")); estimate.set("partyId", context.get("partyId")); estimate.set("roleTypeId", context.get("roleTypeId")); estimate.set("orderPricePercent", context.get("flatPercent")); estimate.set("orderFlatPrice", context.get("flatPrice")); estimate.set("orderItemFlatPrice", context.get("flatItemPrice")); estimate.set("productFeatureGroupId", context.get("productFeatureGroupId")); estimate.set("oversizeUnit", context.get("oversizeUnit")); estimate.set("oversizePrice", context.get("oversizePrice")); estimate.set("featurePercent", context.get("featurePercent")); estimate.set("featurePrice", context.get("featurePrice")); storeAll.add(estimate); if (!applyQuantityBreak(context, result, storeAll, delegator, estimate, "w", "weight", "Weight")) { return result; } if (!applyQuantityBreak(context, result, storeAll, delegator, estimate, "q", "quantity", "Quantity")) { return result; } if (!applyQuantityBreak(context, result, storeAll, delegator, estimate, "p", "price", "Price")) { return result; } try { delegator.storeAll(storeAll); } catch (GenericEntityException e) { result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); result.put(ModelService.ERROR_MESSAGE, "Problem reading product features: " + e.toString()); return result; } result.put("shipmentCostEstimateId", estimate.get("shipmentCostEstimateId")); result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); return result; } public static Map removeShipmentEstimate(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); String shipmentCostEstimateId = (String) context.get("shipmentCostEstimateId"); GenericValue estimate = null; try { estimate = delegator.findByPrimaryKey("ShipmentCostEstimate", UtilMisc.toMap("shipmentCostEstimateId", shipmentCostEstimateId)); estimate.remove(); if (estimate.get("weightBreakId") != null) delegator.removeRelated("WeightQuantityBreak", estimate); if (estimate.get("quantityBreakId") != null) delegator.removeRelated("QuantityQuantityBreak", estimate); if (estimate.get("priceBreakId") != null) delegator.removeRelated("PriceQuantityBreak", estimate); } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError("Problem removing entity or related entities (" + e.toString() + ")"); } return ServiceUtil.returnSuccess(); } private static boolean applyQuantityBreak(Map context, Map result, List storeAll, GenericDelegator delegator, GenericValue estimate, String prefix, String breakType, String breakTypeString) { Double min = (Double) context.get(prefix + "min"); Double max = (Double) context.get(prefix + "max"); if (min != null || max != null) { if (min != null && max != null) { if (min.doubleValue() <= max.doubleValue() || max.doubleValue() == 0) { try { String newSeqId = delegator.getNextSeqId("QuantityBreak"); GenericValue weightBreak = delegator.makeValue("QuantityBreak", null); weightBreak.set("quantityBreakId", newSeqId); weightBreak.set("quantityBreakTypeId", "SHIP_" + breakType.toUpperCase()); weightBreak.set("fromQuantity", min); weightBreak.set("thruQuantity", max); estimate.set(breakType + "BreakId", newSeqId); estimate.set(breakType + "UnitPrice", (Double) context.get(prefix + "price")); if (context.containsKey(prefix + "uom")) { estimate.set(breakType + "UomId", (String) context.get(prefix + "uom")); } storeAll.add(0, weightBreak); } catch ( Exception e ) { Debug.logError(e, module); } } else { result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); result.put(ModelService.ERROR_MESSAGE, "Max " + breakTypeString + " must not be less than Min " + breakTypeString + "."); return false; } } else { result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); result.put(ModelService.ERROR_MESSAGE, breakTypeString+" Span Requires BOTH Fields."); return false; } } return true; } // ShippingEstimate Calc Service public static Map calcShipmentCostEstimate(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); // prepare the data String productStoreId = (String) context.get("productStoreId"); String carrierRoleTypeId = (String) context.get("carrierRoleTypeId"); String carrierPartyId = (String) context.get("carrierPartyId"); String shipmentMethodTypeId = (String) context.get("shipmentMethodTypeId"); String shippingContactMechId = (String) context.get("shippingContactMechId"); List shippableItemInfo = (List) context.get("shippableItemInfo"); //Map shippableFeatureMap = (Map) context.get("shippableFeatureMap"); //List shippableItemSizes = (List) context.get("shippableItemSizes"); Double shippableTotal = (Double) context.get("shippableTotal"); Double shippableQuantity = (Double) context.get("shippableQuantity"); Double shippableWeight = (Double) context.get("shippableWeight"); Double initialEstimateAmt = (Double) context.get("initialEstimateAmt"); if (shippableTotal == null) { shippableTotal = new Double(0.00); } if (shippableQuantity == null) { shippableQuantity = new Double(0.00); } if (shippableWeight == null) { shippableWeight = new Double(0.00); } // get the ShipmentCostEstimate(s) Map estFields = UtilMisc.toMap("productStoreId", productStoreId, "shipmentMethodTypeId", shipmentMethodTypeId, "carrierPartyId", carrierPartyId, "carrierRoleTypeId", carrierRoleTypeId); Collection estimates = null; try { estimates = delegator.findByAnd("ShipmentCostEstimate", estFields); } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError("Unable to locate estimates from database"); } if (estimates == null || estimates.size() < 1) { if (initialEstimateAmt == null || initialEstimateAmt.doubleValue() == 0.00) { Debug.logWarning("Using the passed context : " + context, module); Debug.logWarning("No shipping estimates found; the shipping amount returned is 0!", module); } Map respNow = ServiceUtil.returnSuccess(); respNow.put("shippingEstimateAmount", new Double(0.00)); return respNow; } // Get the PostalAddress GenericValue shipAddress = null; try { shipAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", shippingContactMechId)); } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError("Cannot get shipping address entity"); } // Get the possible estimates. ArrayList estimateList = new ArrayList(); Iterator i = estimates.iterator(); while (i.hasNext()) { GenericValue thisEstimate = (GenericValue) i.next(); String toGeo = thisEstimate.getString("geoIdTo"); List toGeoList = GeoWorker.expandGeoGroup(toGeo, delegator); // Make sure we have a valid GEOID. if (toGeoList == null || toGeoList.size() == 0 || GeoWorker.containsGeo(toGeoList, shipAddress.getString("countryGeoId"), delegator) || GeoWorker.containsGeo(toGeoList, shipAddress.getString("stateProvinceGeoId"), delegator) || GeoWorker.containsGeo(toGeoList, shipAddress.getString("postalCodeGeoId"), delegator)) { /* if (toGeo == null || toGeo.equals("") || toGeo.equals(shipAddress.getString("countryGeoId")) || toGeo.equals(shipAddress.getString("stateProvinceGeoId")) || toGeo.equals(shipAddress.getString("postalCodeGeoId"))) { */ GenericValue wv = null; GenericValue qv = null; GenericValue pv = null; try { wv = thisEstimate.getRelatedOne("WeightQuantityBreak"); } catch (GenericEntityException e) { } try { qv = thisEstimate.getRelatedOne("QuantityQuantityBreak"); } catch (GenericEntityException e) { } try { pv = thisEstimate.getRelatedOne("PriceQuantityBreak"); } catch (GenericEntityException e) { } if (wv == null && qv == null && pv == null) { estimateList.add(thisEstimate); } else { // Do some testing. boolean useWeight = false; boolean weightValid = false; boolean useQty = false; boolean qtyValid = false; boolean usePrice = false; boolean priceValid = false; if (wv != null) { useWeight = true; double min = 0.0001; double max = 0.0001; try { min = wv.getDouble("fromQuantity").doubleValue(); max = wv.getDouble("thruQuantity").doubleValue(); } catch (Exception e) { } if (shippableWeight.doubleValue() >= min && (max == 0 || shippableWeight.doubleValue() <= max)) weightValid = true; } if (qv != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -