📄 priceservices.java
字号:
/*
* $Id: PriceServices.java,v 1.8 2004/02/26 08:34:35 jonesde Exp $
*
* Copyright (c) 2001-2004 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.product.price;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilProperties;
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.product.product.ProductWorker;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
/**
* PriceServices - Workers and Services class for product price related functionality
*
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.8 $
* @since 2.0
*/
public class PriceServices {
public static final String module = PriceServices.class.getName();
/**
* <p>Calculates the price of a product from pricing rules given the following input, and of course access to the database:</p>
* <ul>
* <li>productId
* <li>partyId
* <li>prodCatalogId
* <li>webSiteId
* <li>productStoreGroupId
* <li>quantity
* </ul>
*/
public static Map calculateProductPrice(DispatchContext dctx, Map context) {
boolean optimizeForLargeRuleSet = false;
// UtilTimer utilTimer = new UtilTimer();
// utilTimer.timerString("Starting price calc", module);
// utilTimer.setLog(false);
GenericDelegator delegator = dctx.getDelegator();
Map result = new HashMap();
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
boolean isSale = false;
List orderItemPriceInfos = new LinkedList();
GenericValue product = (GenericValue) context.get("product");
String productId = product.getString("productId");
String prodCatalogId = (String) context.get("prodCatalogId");
String webSiteId = (String) context.get("webSiteId");
String productStoreGroupId = (String) context.get("productStoreGroupId");
if (UtilValidate.isEmpty(productStoreGroupId)) productStoreGroupId = "_NA_";
// if currency uom is null get from properties file, if still null assume USD (USD: American Dollar) for now
String currencyUomId = (String) context.get("currencyUomId");
if (currencyUomId == null || currencyUomId.length() == 0) {
currencyUomId = UtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD");
}
// if this product is variant, find the virtual product and apply checks to it as well
String virtualProductId = null;
if ("Y".equals(product.getString("isVariant"))) {
try {
virtualProductId = ProductWorker.getVariantVirtualId(product);
} catch (GenericEntityException e) {
Debug.logError(e, "Error getting virtual product id from the database while calculating price", module);
return ServiceUtil.returnError("Error getting virtual product id from the database while calculating price: " + e.toString());
}
}
// get prices for virtual product if one is found; get all ProductPrice entities for this productId and currencyUomId
List virtualProductPrices = null;
if (virtualProductId != null) {
try {
virtualProductPrices = delegator.findByAndCache("ProductPrice", UtilMisc.toMap("productId", virtualProductId, "currencyUomId", currencyUomId, "productStoreGroupId", productStoreGroupId), UtilMisc.toList("-fromDate"));
} catch (GenericEntityException e) {
Debug.logError(e, "An error occurred while getting the product prices", module);
}
virtualProductPrices = EntityUtil.filterByDate(virtualProductPrices, true);
}
// NOTE: partyId CAN be null
String partyId = (String) context.get("partyId");
if (UtilValidate.isEmpty(partyId) && context.get("userLogin") != null) {
GenericValue userLogin = (GenericValue) context.get("userLogin");
partyId = userLogin.getString("partyId");
}
// check for auto-userlogin for price rules
if (UtilValidate.isEmpty(partyId) && context.get("autoUserLogin") != null) {
GenericValue userLogin = (GenericValue) context.get("autoUserLogin");
partyId = userLogin.getString("partyId");
}
Double quantityDbl = (Double) context.get("quantity");
if (quantityDbl == null) quantityDbl = new Double(1.0);
double quantity = quantityDbl.doubleValue();
// for prices, get all ProductPrice entities for this productId and currencyUomId
List productPrices = null;
try {
productPrices = delegator.findByAndCache("ProductPrice", UtilMisc.toMap("productId", productId, "currencyUomId", currencyUomId, "productStoreGroupId", productStoreGroupId), UtilMisc.toList("-fromDate"));
} catch (GenericEntityException e) {
Debug.logError(e, "An error occurred while getting the product prices", module);
}
productPrices = EntityUtil.filterByDate(productPrices, true);
// ===== get the prices we need: list, default, average cost, promo, min, max =====
List listPrices = EntityUtil.filterByAnd(productPrices, UtilMisc.toMap("productPriceTypeId", "LIST_PRICE"));
GenericValue listPriceValue = EntityUtil.getFirst(listPrices);
if (listPrices != null && listPrices.size() > 1) {
if (Debug.infoOn()) Debug.logInfo("There is more than one LIST_PRICE with the currencyUomId " + currencyUomId + " and productId " + productId + ", using the latest found with price: " + listPriceValue.getDouble("price"), module);
}
List defaultPrices = EntityUtil.filterByAnd(productPrices, UtilMisc.toMap("productPriceTypeId", "DEFAULT_PRICE"));
GenericValue defaultPriceValue = EntityUtil.getFirst(defaultPrices);
if (defaultPrices != null && defaultPrices.size() > 1) {
if (Debug.infoOn()) Debug.logInfo("There is more than one DEFAULT_PRICE with the currencyUomId " + currencyUomId + " and productId " + productId + ", using the latest found with price: " + defaultPriceValue.getDouble("price"), module);
}
List averageCosts = EntityUtil.filterByAnd(productPrices, UtilMisc.toMap("productPriceTypeId", "AVERAGE_COST"));
GenericValue averageCostValue = EntityUtil.getFirst(averageCosts);
if (averageCosts != null && averageCosts.size() > 1) {
if (Debug.infoOn()) Debug.logInfo("There is more than one AVERAGE_COST with the currencyUomId " + currencyUomId + " and productId " + productId + ", using the latest found with price: " + averageCostValue.getDouble("price"), module);
}
List promoPrices = EntityUtil.filterByAnd(productPrices, UtilMisc.toMap("productPriceTypeId", "PROMO_PRICE"));
GenericValue promoPriceValue = EntityUtil.getFirst(promoPrices);
if (promoPrices != null && promoPrices.size() > 1) {
if (Debug.infoOn()) Debug.logInfo("There is more than one PROMO_PRICE with the currencyUomId " + currencyUomId + " and productId " + productId + ", using the latest found with price: " + promoPriceValue.getDouble("price"), module);
}
List minimumPrices = EntityUtil.filterByAnd(productPrices, UtilMisc.toMap("productPriceTypeId", "MINIMUM_PRICE"));
GenericValue minimumPriceValue = EntityUtil.getFirst(minimumPrices);
if (minimumPrices != null && minimumPrices.size() > 1) {
if (Debug.infoOn()) Debug.logInfo("There is more than one MINIMUM_PRICE with the currencyUomId " + currencyUomId + " and productId " + productId + ", using the latest found with price: " + minimumPriceValue.getDouble("price"), module);
}
List maximumPrices = EntityUtil.filterByAnd(productPrices, UtilMisc.toMap("productPriceTypeId", "MAXIMUM_PRICE"));
GenericValue maximumPriceValue = EntityUtil.getFirst(maximumPrices);
if (maximumPrices != null && maximumPrices.size() > 1) {
if (Debug.infoOn()) Debug.logInfo("There is more than one MAXIMUM_PRICE with the currencyUomId " + currencyUomId + " and productId " + productId + ", using the latest found with price: " + maximumPriceValue.getDouble("price"), module);
}
// if any of these prices is missing and this product is a variant, default to the corresponding price on the virtual product
if (virtualProductPrices != null && virtualProductPrices.size() > 0) {
if (listPriceValue == null) {
List virtualTempPrices = EntityUtil.filterByAnd(virtualProductPrices, UtilMisc.toMap("productPriceTypeId", "LIST_PRICE"));
listPriceValue = EntityUtil.getFirst(virtualTempPrices);
if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
if (Debug.infoOn()) Debug.logInfo("There is more than one LIST_PRICE with the currencyUomId " + currencyUomId + " and productId " + virtualProductId + ", using the latest found with price: " + listPriceValue.getDouble("price"), module);
}
}
if (defaultPriceValue == null) {
List virtualTempPrices = EntityUtil.filterByAnd(virtualProductPrices, UtilMisc.toMap("productPriceTypeId", "DEFAULT_PRICE"));
defaultPriceValue = EntityUtil.getFirst(virtualTempPrices);
if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
if (Debug.infoOn()) Debug.logInfo("There is more than one DEFAULT_PRICE with the currencyUomId " + currencyUomId + " and productId " + virtualProductId + ", using the latest found with price: " + defaultPriceValue.getDouble("price"), module);
}
}
if (averageCostValue == null) {
List virtualTempPrices = EntityUtil.filterByAnd(virtualProductPrices, UtilMisc.toMap("productPriceTypeId", "AVERAGE_COST"));
averageCostValue = EntityUtil.getFirst(virtualTempPrices);
if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
if (Debug.infoOn()) Debug.logInfo("There is more than one AVERAGE_COST with the currencyUomId " + currencyUomId + " and productId " + virtualProductId + ", using the latest found with price: " + averageCostValue.getDouble("price"), module);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -