📄 shippingevents.java
字号:
/*
* $Id: ShippingEvents.java,v 1.10 2004/01/24 14:51:40 ajzeneski Exp $
*
* Copyright (c) 2001, 2002 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.order.shoppingcart.shipping;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.order.order.OrderReadHelper;
import org.ofbiz.order.shoppingcart.ShoppingCart;
import org.ofbiz.service.ModelService;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.common.geo.GeoWorker;
/**
* ShippingEvents - Events used for processing shipping fees
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.10 $
* @since 2.0
*/
public class ShippingEvents {
public static final String module = ShippingEvents.class.getName();
public static String getShipEstimate(HttpServletRequest request, HttpServletResponse response) {
ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
Map result = getShipEstimate(delegator, cart, null);
ServiceUtil.getMessages(request, result, null, "<li>", "</li>", "<ul>", "</ul>", null, null);
if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) {
return "error";
}
Double shippingTotal = (Double) result.get("shippingTotal");
// remove old shipping adjustments if there
cart.removeAdjustmentByType("SHIPPING_CHARGES");
// creat the new adjustment and add it to the cart
if (shippingTotal != null && shippingTotal.doubleValue() > 0) {
GenericValue orderAdjustment = delegator.makeValue("OrderAdjustment",
UtilMisc.toMap("orderAdjustmentTypeId", "SHIPPING_CHARGES", "amount", shippingTotal));
cart.addAdjustment(orderAdjustment);
}
// all done
return "success";
}
public static Map getShipEstimate(GenericDelegator delegator, ShoppingCart cart, String shippingMethod) {
String shipmentMethodTypeId = null;
String carrierPartyId = null;
if (UtilValidate.isNotEmpty(shippingMethod)) {
int delimiterPos = shippingMethod.indexOf('@');
if (delimiterPos > 0) {
shipmentMethodTypeId = shippingMethod.substring(0, delimiterPos);
carrierPartyId = shippingMethod.substring(delimiterPos + 1);
}
} else {
shipmentMethodTypeId = cart.getShipmentMethodTypeId();
carrierPartyId = cart.getCarrierPartyId();
}
return getShipEstimate(delegator, cart.getOrderType(), shipmentMethodTypeId, carrierPartyId, cart.getShippingContactMechId(), cart.getProductStoreId(), cart.getShippableSizes(), cart.getFeatureIdQtyMap(), cart.getShippableWeight(), cart.getShippableQuantity(), cart.getShippableTotal());
}
public static Map getShipEstimate(GenericDelegator delegator, OrderReadHelper orh) {
String shippingMethod = orh.getShippingMethodCode();
String shipmentMethodTypeId = null;
String carrierPartyId = null;
if (UtilValidate.isNotEmpty(shippingMethod)) {
int delimiterPos = shippingMethod.indexOf('@');
if (delimiterPos > 0) {
shipmentMethodTypeId = shippingMethod.substring(0, delimiterPos);
carrierPartyId = shippingMethod.substring(delimiterPos + 1);
}
}
GenericValue shipAddr = orh.getShippingAddress();
String contactMechId = shipAddr.getString("contactMechId");
return getShipEstimate(delegator, orh.getOrderTypeId(), shipmentMethodTypeId, carrierPartyId, contactMechId, orh.getProductStoreId(), orh.getShippableSizes(), orh.getFeatureIdQtyMap(), orh.getShippableWeight(), orh.getShippableQuantity(), orh.getShippableTotal());
}
public static Map getShipEstimate(GenericDelegator delegator, String orderTypeId, String shipmentMethodTypeId, String carrierPartyId, String shippingContactMechId, String productStoreId, List itemSizes, Map featureMap, double shippableWeight, double shippableQuantity, double shippableTotal) {
String standardMessage = "A problem occurred calculating shipping. Fees will be calculated offline.";
List errorMessageList = new ArrayList();
StringBuffer errorMessage = new StringBuffer();
if (shipmentMethodTypeId == null || carrierPartyId == null) {
if ("SALES_ORDER".equals(orderTypeId)) {
errorMessageList.add("Please Select Your Shipping Method.");
return ServiceUtil.returnError(errorMessageList);
} else {
return ServiceUtil.returnSuccess();
}
}
if (shippingContactMechId == null) {
errorMessageList.add("Please Select Your Shipping Address.");
return ServiceUtil.returnError(errorMessageList);
}
if (Debug.verboseOn()) {
Debug.logVerbose("Shippable Weight : " + shippableWeight, module);
Debug.logVerbose("Shippable Qty : " + shippableQuantity, module);
Debug.logVerbose("Shippable Total : " + shippableTotal, module);
}
// no shippable items; we won't change any shipping at all
if (shippableQuantity == 0) {
Map result = ServiceUtil.returnSuccess();
result.put("shippingTotal", new Double(0));
return result;
}
// Get the ShipmentCostEstimate(s)
Collection estimates = null;
try {
Map fields = UtilMisc.toMap("productStoreId", productStoreId, "shipmentMethodTypeId", shipmentMethodTypeId, "carrierPartyId", carrierPartyId, "carrierRoleTypeId", "CARRIER");
estimates = delegator.findByAnd("ShipmentCostEstimate", fields);
if (Debug.verboseOn()) Debug.logVerbose("Estimate fields: " + fields, module);
if (Debug.verboseOn()) Debug.logVerbose("Estimate(s): " + estimates, module);
} catch (GenericEntityException e) {
Debug.logError("[ShippingEvents.getShipEstimate] Cannot get shipping estimates.", module);
return ServiceUtil.returnSuccess(standardMessage);
}
if (estimates == null || estimates.size() < 1) {
Debug.logInfo("[ShippingEvents.getShipEstimate] No shipping estimate found.", module);
return ServiceUtil.returnSuccess(standardMessage);
}
if (Debug.verboseOn()) Debug.logVerbose("[ShippingEvents.getShipEstimate] Estimates begin size: " + estimates.size(), module);
// Get the PostalAddress
GenericValue shipAddress = null;
try {
shipAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", shippingContactMechId));
} catch (GenericEntityException e) {
Debug.logError("[ShippingEvents.getShipEstimate] Cannot get shipping address entity.", module);
return ServiceUtil.returnSuccess(standardMessage);
}
// 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 >= min && (max == 0 || shippableWeight <= max))
weightValid = true;
}
if (qv != null) {
useQty = true;
double min = 0.0001;
double max = 0.0001;
try {
min = qv.getDouble("fromQuantity").doubleValue();
max = qv.getDouble("thruQuantity").doubleValue();
} catch (Exception e) {}
if (shippableQuantity >= min && (max == 0 || shippableQuantity <= max))
qtyValid = true;
}
if (pv != null) {
usePrice = true;
double min = 0.0001;
double max = 0.0001;
try {
min = pv.getDouble("fromQuantity").doubleValue();
max = pv.getDouble("thruQuantity").doubleValue();
} catch (Exception e) {}
if (shippableTotal >= min && (max == 0 || shippableTotal <= max))
priceValid = true;
}
// Now check the tests.
if ((useWeight && weightValid) || (useQty && qtyValid) || (usePrice && priceValid))
estimateList.add(thisEstimate);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -