/**
* Calculates the purchase price of a product
*/
public static Map<String, Object> calculatePurchasePrice(DispatchContext dctx, Map<String, ? extends Object> context) {
GenericDelegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Map<String, Object> result = FastMap.newInstance();
List<GenericValue> orderItemPriceInfos = FastList.newInstance();
boolean validPriceFound = false;
BigDecimal price = BigDecimal.ZERO;
GenericValue product = (GenericValue)context.get("product");
String productId = product.getString("productId");
String currencyUomId = (String)context.get("currencyUomId");
String partyId = (String)context.get("partyId");
BigDecimal quantity = (BigDecimal)context.get("quantity");
// a) Get the Price from the Agreement* data model
// TODO: Implement this
// b) If no price can be found, get the lastPrice from the SupplierProduct entity
if (!validPriceFound) {
Map<String, Object> priceContext = UtilMisc.toMap("currencyUomId", currencyUomId, "partyId", partyId, "productId", productId, "quantity", quantity);
List<GenericValue> productSuppliers = null;
try {
Map<String, Object> priceResult = dispatcher.runSync("getSuppliersForProduct", priceContext);
if (ServiceUtil.isError(priceResult)) {
String errMsg = ServiceUtil.getErrorMessage(priceResult);
Debug.logError(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
productSuppliers = UtilGenerics.checkList(priceResult.get("supplierProducts"));
} catch (GenericServiceException gse) {
Debug.logError(gse, module);
return ServiceUtil.returnError(gse.getMessage());
}
if (productSuppliers != null) {
for (GenericValue productSupplier: productSuppliers) {
if (!validPriceFound) {
price = ((BigDecimal)productSupplier.get("lastPrice"));
validPriceFound = true;
}
// add a orderItemPriceInfo element too, without orderId or orderItemId
StringBuilder priceInfoDescription = new StringBuilder();
priceInfoDescription.append("SupplierProduct ");
priceInfoDescription.append("[minimumOrderQuantity:");
priceInfoDescription.append(productSupplier.getBigDecimal("minimumOrderQuantity"));
priceInfoDescription.append(", lastPrice: ");
priceInfoDescription.append(productSupplier.getBigDecimal("lastPrice"));
priceInfoDescription.append("]");
GenericValue orderItemPriceInfo = delegator.makeValue("OrderItemPriceInfo");
//orderItemPriceInfo.set("productPriceRuleId", productPriceAction.get("productPriceRuleId"));
//orderItemPriceInfo.set("productPriceActionSeqId", productPriceAction.get("productPriceActionSeqId"));
//orderItemPriceInfo.set("modifyAmount", modifyAmount);
// make sure description is <= than 250 chars
String priceInfoDescriptionString = priceInfoDescription.toString();
if (priceInfoDescriptionString.length() > 250) {
priceInfoDescriptionString = priceInfoDescriptionString.substring(0, 250);
}
orderItemPriceInfo.set("description", priceInfoDescriptionString);
orderItemPriceInfos.add(orderItemPriceInfo);
}
}
}
// c) If no price can be found, get the averageCost from the ProductPrice entity
if (!validPriceFound) {
List<GenericValue> prices = null;
try {
prices = delegator.findByAnd("ProductPrice", UtilMisc.toMap("productId", productId,
"productPricePurposeId", "PURCHASE"), UtilMisc.toList("-fromDate"));
// if no prices are found; find the prices of the parent product
if (prices == null || prices.size() == 0) {
GenericValue parentProduct = ProductWorker.getParentProduct(productId, delegator);
if (parentProduct != null) {
String parentProductId = parentProduct.getString("productId");
prices = delegator.findByAnd("ProductPrice", UtilMisc.toMap("productId", parentProductId,
"productPricePurposeId", "PURCHASE"), UtilMisc.toList("-fromDate"));
}
}
} catch (GenericEntityException e) {
Debug.logError(e, module);