in applications/product/src/main/java/org/apache/ofbiz/product/store/ProductStoreWorker.java [242:432]
public static List<GenericValue> getAvailableStoreShippingMethods(Delegator delegator, String productStoreId, GenericValue shippingAddress,
List<BigDecimal> itemSizes, Map<String, BigDecimal> featureIdMap, BigDecimal weight, BigDecimal orderTotal) {
if (featureIdMap == null) {
featureIdMap = new HashMap<>();
}
List<GenericValue> shippingMethods = null;
try {
shippingMethods = EntityQuery.use(delegator).from("ProductStoreShipmentMethView").where("productStoreId",
productStoreId).orderBy("sequenceNumber").cache(true).queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to get ProductStore shipping methods", MODULE);
return null;
}
// clone the list for concurrent modification
List<GenericValue> returnShippingMethods = UtilMisc.makeListWritable(shippingMethods);
if (shippingMethods != null) {
for (GenericValue method: shippingMethods) {
// test min/max weight first
BigDecimal minWeight = method.getBigDecimal("minWeight");
BigDecimal maxWeight = method.getBigDecimal("maxWeight");
if (minWeight != null && minWeight.compareTo(BigDecimal.ZERO) > 0 && minWeight.compareTo(weight) > 0) {
returnShippingMethods.remove(method);
continue;
}
if (maxWeight != null && maxWeight.compareTo(BigDecimal.ZERO) > 0 && maxWeight.compareTo(weight) < 0) {
returnShippingMethods.remove(method);
continue;
}
// test order total
BigDecimal minTotal = method.getBigDecimal("minTotal");
BigDecimal maxTotal = method.getBigDecimal("maxTotal");
if (minTotal != null && minTotal.compareTo(BigDecimal.ZERO) > 0 && minTotal.compareTo(orderTotal) > 0) {
returnShippingMethods.remove(method);
continue;
}
if (maxTotal != null && maxTotal.compareTo(BigDecimal.ZERO) > 0 && maxTotal.compareTo(orderTotal) < 0) {
returnShippingMethods.remove(method);
continue;
}
// test product sizes
BigDecimal minSize = method.getBigDecimal("minSize");
BigDecimal maxSize = method.getBigDecimal("maxSize");
if (minSize != null && minSize.compareTo(BigDecimal.ZERO) > 0) {
boolean allMatch = false;
if (itemSizes != null) {
allMatch = true;
for (BigDecimal size: itemSizes) {
if (size.compareTo(minSize) < 0) {
allMatch = false;
}
}
}
if (!allMatch) {
returnShippingMethods.remove(method);
continue;
}
}
if (maxSize != null && maxSize.compareTo(BigDecimal.ZERO) > 0) {
boolean allMatch = false;
if (itemSizes != null) {
allMatch = true;
for (BigDecimal size: itemSizes) {
if (size.compareTo(maxSize) > 0) {
allMatch = false;
}
}
}
if (!allMatch) {
returnShippingMethods.remove(method);
continue;
}
}
// check USPS address
String allowUspsAddr = method.getString("allowUspsAddr");
String requireUspsAddr = method.getString("requireUspsAddr");
boolean isUspsAddress = ContactMechWorker.isUspsAddress(shippingAddress);
if ("N".equals(allowUspsAddr) && isUspsAddress) {
returnShippingMethods.remove(method);
continue;
}
if ("Y".equals(requireUspsAddr) && !isUspsAddress) {
returnShippingMethods.remove(method);
continue;
}
// check company address
String companyPartyId = method.getString("companyPartyId");
String allowCompanyAddr = method.getString("allowCompanyAddr");
String requireCompanyAddr = method.getString("requireCompanyAddr");
boolean isCompanyAddress = ContactMechWorker.isCompanyAddress(shippingAddress, companyPartyId);
if ("N".equals(allowCompanyAddr) && isCompanyAddress) {
returnShippingMethods.remove(method);
continue;
}
if ("Y".equals(requireCompanyAddr) && !isCompanyAddress) {
returnShippingMethods.remove(method);
continue;
}
// check the items excluded from shipping
String includeFreeShipping = method.getString("includeNoChargeItems");
if (includeFreeShipping != null && "N".equalsIgnoreCase(includeFreeShipping)) {
if (UtilValidate.isEmpty(itemSizes) && orderTotal.compareTo(BigDecimal.ZERO) == 0) {
returnShippingMethods.remove(method);
continue;
}
}
// check the geos
String includeGeoId = method.getString("includeGeoId");
String excludeGeoId = method.getString("excludeGeoId");
if (UtilValidate.isNotEmpty(includeGeoId) || UtilValidate.isNotEmpty(excludeGeoId)) {
if (shippingAddress == null) {
returnShippingMethods.remove(method);
continue;
}
}
if (UtilValidate.isNotEmpty(includeGeoId)) {
List<GenericValue> includeGeoGroup = GeoWorker.expandGeoGroup(includeGeoId, delegator);
if (!GeoWorker.containsGeo(includeGeoGroup, shippingAddress.getString("countryGeoId"), delegator)
&& !GeoWorker.containsGeo(includeGeoGroup, shippingAddress.getString("stateProvinceGeoId"), delegator)
&& !GeoWorker.containsGeo(includeGeoGroup, shippingAddress.getString("postalCodeGeoId"), delegator)) {
// not in required included geos
returnShippingMethods.remove(method);
continue;
}
}
if (UtilValidate.isNotEmpty(excludeGeoId)) {
List<GenericValue> excludeGeoGroup = GeoWorker.expandGeoGroup(excludeGeoId, delegator);
if (GeoWorker.containsGeo(excludeGeoGroup, shippingAddress.getString("countryGeoId"), delegator)
|| GeoWorker.containsGeo(excludeGeoGroup, shippingAddress.getString("stateProvinceGeoId"), delegator)
|| GeoWorker.containsGeo(excludeGeoGroup, shippingAddress.getString("postalCodeGeoId"), delegator)) {
// in excluded geos
returnShippingMethods.remove(method);
continue;
}
}
// check the features
String includeFeatures = method.getString("includeFeatureGroup");
String excludeFeatures = method.getString("excludeFeatureGroup");
if (UtilValidate.isNotEmpty(includeFeatures)) {
List<GenericValue> includedFeatures = null;
try {
includedFeatures = EntityQuery.use(delegator).from("ProductFeatureGroupAppl").where("productFeatureGroupId",
includeFeatures).cache(true).queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to lookup ProductFeatureGroupAppl records for group : " + includeFeatures, MODULE);
}
if (includedFeatures != null) {
boolean foundOne = false;
for (GenericValue appl: includedFeatures) {
if (featureIdMap.containsKey(appl.getString("productFeatureId"))) {
foundOne = true;
break;
}
}
if (!foundOne) {
returnShippingMethods.remove(method);
continue;
}
}
}
if (UtilValidate.isNotEmpty(excludeFeatures)) {
List<GenericValue> excludedFeatures = null;
try {
excludedFeatures = EntityQuery.use(delegator).from("ProductFeatureGroupAppl").where("productFeatureGroupId",
excludeFeatures).cache(true).queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to lookup ProductFeatureGroupAppl records for group : " + excludeFeatures, MODULE);
}
if (excludedFeatures != null) {
for (GenericValue appl: excludedFeatures) {
if (featureIdMap.containsKey(appl.getString("productFeatureId"))) {
returnShippingMethods.remove(method);
continue;
}
}
}
}
}
}
return returnShippingMethods;
}