📄 shoppingcartitem.java
字号:
this.delegatorName = _product.getDelegator().getDelegatorName();
this.orderShipmentPreference = delegator.makeValue("OrderShipmentPreference", null);
this.addAllProductFeatureAndAppls(additionalProductFeatureAndAppls);
this.locale = locale;
}
/** Creates new ShopingCartItem object. */
protected ShoppingCartItem(GenericDelegator delegator, String itemTypeId, String description, String categoryId, double basePrice, Map attributes, String prodCatalogId, Locale locale) {
this.delegator = delegator;
this.itemType = itemTypeId;
this.itemDescription = description;
this.productCategoryId = categoryId;
this.basePrice = basePrice;
this.attributes = attributes;
this.prodCatalogId = prodCatalogId;
this.delegatorName = delegator.getDelegatorName();
this.locale = locale;
}
public String getProdCatalogId() {
return this.prodCatalogId;
}
/** Sets the user selected amount */
public void setSelectedAmount(double selectedAmount) {
this.selectedAmount = selectedAmount;
}
/** Returns the user selected amount */
public double getSelectedAmount() {
return this.selectedAmount;
}
/** Sets the base price for the item; use with caution */
public void setBasePrice(double basePrice) {
this.basePrice = basePrice;
}
/** Sets the quantity for the item and validates the change in quantity, etc */
public void setQuantity(double quantity, LocalDispatcher dispatcher, ShoppingCart cart) throws CartItemModifyException {
setQuantity(quantity, dispatcher, cart, true);
}
/** Sets the quantity for the item and validates the change in quantity, etc */
public void setQuantity(double quantity, LocalDispatcher dispatcher, ShoppingCart cart, boolean doPromotions) throws CartItemModifyException {
if (this.quantity == quantity) {
return;
}
if (this.isPromo) {
throw new CartItemModifyException("Sorry, you can't change the quantity on the promotion item " + this.getName() + " (product ID: " + productId + "), not setting quantity.");
}
// check inventory if new quantity is greater than old quantity; don't worry about inventory getting pulled out from under, that will be handled at checkout time
if (_product != null && quantity > this.quantity) {
String productStoreId = cart.getProductStoreId();
if (org.ofbiz.product.store.ProductStoreWorker.isStoreInventoryRequired(productStoreId, this.getProduct(), this.getDelegator())) {
if (!org.ofbiz.product.store.ProductStoreWorker.isStoreInventoryAvailable(productStoreId, productId, quantity, this.getDelegator(), dispatcher)) {
String excMsg = "Sorry, we do not have enough (you tried " + UtilFormatOut.formatQuantity(quantity) + ") of the product " + this.getName() + " (product ID: " + productId + ") in stock, not adding to cart. Please try a lower quantity, try again later, or call customer service for more information.";
Debug.logWarning(excMsg, module);
throw new CartItemModifyException(excMsg);
}
}
}
// set quantity before promos so order total, etc will be updated
this.quantity = quantity;
this.updatePrice(dispatcher, cart);
// apply/unapply promotions - only for sales orders
if (doPromotions && cart.getOrderType().equals("SALES_ORDER")) {
org.ofbiz.order.shoppingcart.product.ProductPromoWorker.doPromotions(cart, dispatcher);
}
}
public void updatePrice(LocalDispatcher dispatcher, ShoppingCart cart) throws CartItemModifyException {
// set basePrice using the calculateProductPrice service
if (_product != null) {
try {
Map priceContext = new HashMap();
priceContext.put("currencyUomId", cart.getCurrency());
priceContext.put("product", this.getProduct());
priceContext.put("prodCatalogId", this.getProdCatalogId());
priceContext.put("webSiteId", cart.getWebSiteId());
String partyId = cart.getPartyId();
if (partyId != null) {
priceContext.put("partyId", partyId);
}
priceContext.put("quantity", new Double(this.getQuantity()));
Map priceResult = dispatcher.runSync("calculateProductPrice", priceContext);
if (ModelService.RESPOND_ERROR.equals(priceResult.get(ModelService.RESPONSE_MESSAGE))) {
throw new CartItemModifyException("There was an error while calculating the price: " + priceResult.get(ModelService.ERROR_MESSAGE));
}
if (priceResult.get("listPrice") != null) this.listPrice = ((Double) priceResult.get("listPrice")).doubleValue();
if (cart.getOrderType().equals("PURCHASE_ORDER")) {
if (priceResult.get("averageCost") != null)
this.basePrice = ((Double) priceResult.get("averageCost")).doubleValue();
} else {
if (priceResult.get("price") != null)
this.basePrice = ((Double) priceResult.get("price")).doubleValue();
}
this.orderItemPriceInfos = (List) priceResult.get("orderItemPriceInfos");
} catch (GenericServiceException e) {
throw new CartItemModifyException("There was an error while calculating the price", e);
}
}
}
/** Returns the quantity. */
public double getQuantity() {
return this.quantity;
}
public double getPromoQuantityUsed() {
if (this.getIsPromo()) {
return this.quantity;
} else {
return this.promoQuantityUsed;
}
}
public double getPromoQuantityAvailable() {
if (this.getIsPromo()) {
return 0;
} else {
return this.quantity - this.promoQuantityUsed;
}
}
public Iterator getQuantityUsedPerPromoActualIter() {
return this.quantityUsedPerPromoActual.entrySet().iterator();
}
public Iterator getQuantityUsedPerPromoCandidateIter() {
return this.quantityUsedPerPromoCandidate.entrySet().iterator();
}
public Iterator getQuantityUsedPerPromoFailedIter() {
return this.quantityUsedPerPromoFailed.entrySet().iterator();
}
public synchronized double addPromoQuantityCandidateUse(double quantityDesired, GenericValue productPromoCondAction, boolean checkAvailableOnly) {
if (quantityDesired == 0) return 0;
double promoQuantityAvailable = this.getPromoQuantityAvailable();
double promoQuantityToUse = quantityDesired;
if (promoQuantityAvailable > 0) {
if (promoQuantityToUse > promoQuantityAvailable) {
promoQuantityToUse = promoQuantityAvailable;
}
if (!checkAvailableOnly) {
// keep track of candidate promo uses on cartItem
GenericPK productPromoCondActionPK = productPromoCondAction.getPrimaryKey();
Double existingValue = (Double) this.quantityUsedPerPromoCandidate.get(productPromoCondActionPK);
if (existingValue == null) {
this.quantityUsedPerPromoCandidate.put(productPromoCondActionPK, new Double(promoQuantityToUse));
} else {
this.quantityUsedPerPromoCandidate.put(productPromoCondActionPK, new Double(promoQuantityToUse + existingValue.doubleValue()));
}
this.promoQuantityUsed += promoQuantityToUse;
//Debug.logInfo("promoQuantityToUse=" + promoQuantityToUse + ", quantityDesired=" + quantityDesired + ", for promoCondAction: " + productPromoCondAction, module);
//Debug.logInfo("promoQuantityUsed now=" + promoQuantityUsed, module);
}
return promoQuantityToUse;
} else {
return 0;
}
}
public double getPromoQuantityCandidateUse(GenericValue productPromoCondAction) {
GenericPK productPromoCondActionPK = productPromoCondAction.getPrimaryKey();
Double existingValue = (Double) this.quantityUsedPerPromoCandidate.get(productPromoCondActionPK);
if (existingValue == null) {
return 0;
} else {
return existingValue.doubleValue();
}
}
public double getPromoQuantityCandidateUseActionAndAllConds(GenericValue productPromoAction) {
double totalUse = 0;
String productPromoId = productPromoAction.getString("productPromoId");
String productPromoRuleId = productPromoAction.getString("productPromoRuleId");
GenericPK productPromoActionPK = productPromoAction.getPrimaryKey();
Double existingValue = (Double) this.quantityUsedPerPromoCandidate.get(productPromoActionPK);
if (existingValue != null) {
totalUse = existingValue.doubleValue();
}
Iterator entryIter = this.quantityUsedPerPromoCandidate.entrySet().iterator();
while (entryIter.hasNext()) {
Map.Entry entry = (Map.Entry) entryIter.next();
GenericPK productPromoCondActionPK = (GenericPK) entry.getKey();
Double quantityUsed = (Double) entry.getValue();
if (quantityUsed != null) {
// must be in the same rule and be a condition
if (productPromoId.equals(productPromoCondActionPK.getString("productPromoId")) &&
productPromoRuleId.equals(productPromoCondActionPK.getString("productPromoRuleId")) &&
productPromoCondActionPK.containsKey("productPromoCondSeqId")) {
totalUse += quantityUsed.doubleValue();
}
}
}
return totalUse;
}
public synchronized void resetPromoRuleUse(String productPromoId, String productPromoRuleId) {
Iterator entryIter = this.quantityUsedPerPromoCandidate.entrySet().iterator();
while (entryIter.hasNext()) {
Map.Entry entry = (Map.Entry) entryIter.next();
GenericPK productPromoCondActionPK = (GenericPK) entry.getKey();
Double quantityUsed = (Double) entry.getValue();
if (productPromoId.equals(productPromoCondActionPK.getString("productPromoId")) && productPromoRuleId.equals(productPromoCondActionPK.getString("productPromoRuleId"))) {
entryIter.remove();
Double existingValue = (Double) this.quantityUsedPerPromoFailed.get(productPromoCondActionPK);
if (existingValue == null) {
this.quantityUsedPerPromoFailed.put(productPromoCondActionPK, quantityUsed);
} else {
this.quantityUsedPerPromoFailed.put(productPromoCondActionPK, new Double(quantityUsed.doubleValue() + existingValue.doubleValue()));
}
this.promoQuantityUsed -= quantityUsed.doubleValue();
}
}
}
public synchronized void confirmPromoRuleUse(String productPromoId, String productPromoRuleId) {
Iterator entryIter = this.quantityUsedPerPromoCandidate.entrySet().iterator();
while (entryIter.hasNext()) {
Map.Entry entry = (Map.Entry) entryIter.next();
GenericPK productPromoCondActionPK = (GenericPK) entry.getKey();
Double quantityUsed = (Double) entry.getValue();
if (productPromoId.equals(productPromoCondActionPK.getString("productPromoId")) && productPromoRuleId.equals(productPromoCondActionPK.getString("productPromoRuleId"))) {
entryIter.remove();
Double existingValue = (Double) this.quantityUsedPerPromoActual.get(productPromoCondActionPK);
if (existingValue == null) {
this.quantityUsedPerPromoActual.put(productPromoCondActionPK, quantityUsed);
} else {
this.quantityUsedPerPromoActual.put(productPromoCondActionPK, new Double(quantityUsed.doubleValue() + existingValue.doubleValue()));
}
}
}
}
public synchronized void clearPromoRuleUseInfo() {
this.quantityUsedPerPromoActual.clear();
this.quantityUsedPerPromoCandidate.clear();
this.quantityUsedPerPromoFailed.clear();
this.promoQuantityUsed = this.getIsPromo() ? this.quantity : 0;
}
/** Sets the item comment. */
public void setItemComment(String itemComment) {
this.itemComment = itemComment;
}
/** Returns the item's comment. */
public String getItemComment() {
return itemComment;
}
/** Sets the item type. */
public void setItemType(String itemType) {
this.itemType = itemType;
}
/** Returns the item type. */
public String getItemType() {
return itemType;
}
/** Returns the item type description. */
public String getItemTypeDescription() {
GenericValue orderItemType = null;
try {
orderItemType = this.getDelegator().findByPrimaryKeyCache("OrderItemType", UtilMisc.toMap("orderItemTypeId", this.getItemType()));
} catch (GenericEntityException e) {
Debug.logWarning(e, "Problems getting OrderItemType for: " + this.getItemType(), module);
}
if (itemType != null) {
return orderItemType.getString("description");
}
return null;
}
/** Returns the productCategoryId for the item or null if none. */
public String getProductCategoryId() {
return this.productCategoryId;
}
public void setOrderItemSeqId(String orderItemSeqId) {
this.orderItemSeqId = orderItemSeqId;
}
public String getOrderItemSeqId() {
return orderItemSeqId;
}
public void setShoppingList(String shoppingListId, String itemSeqId) {
attributes.put("shoppingListId", shoppingListId);
attributes.put("shoppingListItemSeqId", itemSeqId);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -