📄 shoppingcart.java
字号:
// =======================================================================
// Methods used for order creation
// =======================================================================
private void explodeItems(LocalDispatcher dispatcher) {
synchronized (cartLines) {
if (dispatcher != null) {
List cartLineItems = new LinkedList(cartLines);
Iterator itemIter = cartLineItems.iterator();
while (itemIter.hasNext()) {
ShoppingCartItem item = (ShoppingCartItem) itemIter.next();
Debug.logInfo("Item qty: " + item.getQuantity(), module);
try {
item.explodeItem(this, dispatcher);
} catch (CartItemModifyException e) {
Debug.logError(e, "Problem exploding item! Item not exploded.", module);
}
}
}
}
}
public List makeOrderItems() {
return makeOrderItems(false, null);
}
public List makeOrderItems(boolean explodeItems, LocalDispatcher dispatcher) {
// do the explosion
if (explodeItems && dispatcher != null)
explodeItems(dispatcher);
// now build the lines
synchronized (cartLines) {
List result = new LinkedList();
long cartLineSize = cartLines.size();
long seqId = 1;
Iterator itemIter = cartLines.iterator();
while (itemIter.hasNext()) {
ShoppingCartItem item = (ShoppingCartItem) itemIter.next();
// format the string with enough leading zeroes for the number of cartLines
NumberFormat nf = NumberFormat.getNumberInstance();
if (cartLineSize > 9) {
nf.setMinimumIntegerDigits(2);
} else if (cartLineSize > 99) {
nf.setMinimumIntegerDigits(3);
} else if (cartLineSize > 999) {
nf.setMinimumIntegerDigits(4);
} else if (cartLineSize > 9999) {
// if it's more than 9999, something's up... hit the sky
nf.setMinimumIntegerDigits(18);
}
String orderItemSeqId = nf.format(seqId);
seqId++;
item.setOrderItemSeqId(orderItemSeqId);
// the initial status for all item types
String initialStatus = "ITEM_CREATED";
GenericValue orderItem = getDelegator().makeValue("OrderItem", null);
orderItem.set("orderItemSeqId", orderItemSeqId);
orderItem.set("orderItemTypeId", item.getItemType());
orderItem.set("productId", item.getProductId());
orderItem.set("prodCatalogId", item.getProdCatalogId());
orderItem.set("productCategoryId", item.getProductCategoryId());
orderItem.set("quantity", new Double(item.getQuantity()));
orderItem.set("unitPrice", new Double(item.getBasePrice()));
orderItem.set("unitListPrice", new Double(item.getListPrice()));
orderItem.set("shoppingListId", item.getShoppingListId());
orderItem.set("shoppingListItemSeqId", item.getShoppingListItemSeqId());
orderItem.set("itemDescription", item.getName());
orderItem.set("comments", item.getItemComment());
orderItem.set("correspondingPoId", this.getPoNumber());
orderItem.set("statusId", initialStatus);
result.add(orderItem);
// don't do anything with adjustments here, those will be added below in makeAllAdjustments
}
return result;
}
}
/** make a list of all adjustments including order adjustments, order line adjustments, and special adjustments (shipping and tax if applicable) */
public List makeAllAdjustments() {
List allAdjs = new LinkedList();
// before returning adjustments, go through them to find all that need counter adjustments (for instance: free shipping)
Iterator allAdjsIter = this.getAdjustments().iterator();
while (allAdjsIter.hasNext()) {
GenericValue orderAdjustment = (GenericValue) allAdjsIter.next();
allAdjs.add(orderAdjustment);
if ("SHIPPING_CHARGES".equals(orderAdjustment.get("orderAdjustmentTypeId"))) {
Iterator fsppas = this.freeShippingProductPromoActions.iterator();
while (fsppas.hasNext()) {
GenericValue productPromoAction = (GenericValue) fsppas.next();
if ((productPromoAction.get("productId") == null || productPromoAction.getString("productId").equals(this.getShipmentMethodTypeId())) &&
(productPromoAction.get("partyId") == null || productPromoAction.getString("partyId").equals(this.getCarrierPartyId()))) {
Double shippingAmount = new Double(-OrderReadHelper.calcOrderAdjustment(orderAdjustment, getSubTotal()));
// always set orderAdjustmentTypeId to SHIPPING_CHARGES for free shipping adjustments
GenericValue fsOrderAdjustment = getDelegator().makeValue("OrderAdjustment",
UtilMisc.toMap("orderItemSeqId", orderAdjustment.get("orderItemSeqId"), "orderAdjustmentTypeId", "SHIPPING_CHARGES", "amount", shippingAmount,
"productPromoId", productPromoAction.get("productPromoId"), "productPromoRuleId", productPromoAction.get("productPromoRuleId"),
"productPromoActionSeqId", productPromoAction.get("productPromoActionSeqId")));
allAdjs.add(fsOrderAdjustment);
// if free shipping IS applied to this orderAdjustment, break
// out of the loop so that even if there are multiple free
// shipping adjustments that apply to this orderAdjustment it
// will only be compensated for once
break;
}
}
}
}
// add all of the item adjustments to this list too
Iterator itemIter = cartLines.iterator();
while (itemIter.hasNext()) {
ShoppingCartItem item = (ShoppingCartItem) itemIter.next();
Collection adjs = item.getAdjustments();
if (adjs != null) {
Iterator adjIter = adjs.iterator();
while (adjIter.hasNext()) {
GenericValue orderAdjustment = (GenericValue) adjIter.next();
orderAdjustment.set("orderItemSeqId", item.getOrderItemSeqId());
allAdjs.add(orderAdjustment);
if ("SHIPPING_CHARGES".equals(orderAdjustment.get("orderAdjustmentTypeId"))) {
Iterator fsppas = this.freeShippingProductPromoActions.iterator();
while (fsppas.hasNext()) {
GenericValue productPromoAction = (GenericValue) fsppas.next();
if ((productPromoAction.get("productId") == null || productPromoAction.getString("productId").equals(item.getShipmentMethodTypeId())) &&
(productPromoAction.get("partyId") == null || productPromoAction.getString("partyId").equals(item.getCarrierPartyId()))) {
Double shippingAmount = new Double(-OrderReadHelper.calcItemAdjustment(orderAdjustment, new Double(item.getQuantity()), new Double(item.getItemSubTotal())));
// always set orderAdjustmentTypeId to SHIPPING_CHARGES for free shipping adjustments
GenericValue fsOrderAdjustment = getDelegator().makeValue("OrderAdjustment",
UtilMisc.toMap("orderItemSeqId", orderAdjustment.get("orderItemSeqId"), "orderAdjustmentTypeId", "SHIPPING_CHARGES", "amount", shippingAmount,
"productPromoId", productPromoAction.get("productPromoId"), "productPromoRuleId", productPromoAction.get("productPromoRuleId"),
"productPromoActionSeqId", productPromoAction.get("productPromoActionSeqId")));
allAdjs.add(fsOrderAdjustment);
// if free shipping IS applied to this orderAdjustment, break
// out of the loop so that even if there are multiple free
// shipping adjustments that apply to this orderAdjustment it
// will only be compensated for once
break;
}
}
}
}
}
}
return allAdjs;
}
/** make a list of all OrderPaymentPreferences including all payment methods and types */
public List makeAllOrderPaymentPreferences() {
List allOpPrefs = new LinkedList();
// first create the payment methods (online payments?)
List paymentMethods = this.getPaymentMethods();
Iterator pmi = paymentMethods.iterator();
while (pmi.hasNext()) {
GenericValue paymentMethod = (GenericValue) pmi.next();
GenericValue p = delegator.makeValue("OrderPaymentPreference", new HashMap());
p.set("paymentMethodTypeId", paymentMethod.get("paymentMethodTypeId"));
p.set("paymentMethodId", paymentMethod.get("paymentMethodId"));
p.set("statusId", "PAYMENT_NOT_AUTH");
if (this.paymentMethodAmounts.get(paymentMethod.getString("paymentMethodId")) != null) {
p.set("maxAmount", this.paymentMethodAmounts.get(paymentMethod.getString("paymentMethodId")));
}
allOpPrefs.add(p);
}
// next create the payment types (offline payments?)
Iterator pti = this.getPaymentMethodTypeIds().iterator();
while (pti.hasNext()) {
String paymentMethodTypeId = (String) pti.next();
GenericValue p = delegator.makeValue("OrderPaymentPreference", new HashMap());
p.set("paymentMethodTypeId", paymentMethodTypeId);
p.set("statusId", "PAYMENT_NOT_RECEIVED");
if (this.paymentMethodTypeAmounts.get(paymentMethodTypeId) != null) {
p.set("maxAmount", this.paymentMethodTypeAmounts.get(paymentMethodTypeId));
}
allOpPrefs.add(p);
}
return allOpPrefs;
}
/** make a list of all OrderShipmentPreferences including ones for the order and order lines */
public List makeAllOrderShipmentPreferences() {
List allOshPrefs = new LinkedList();
// if nothing has been put into the value, don't set it; must at least have a carrierPartyId and a shipmentMethodTypeId
if (this.orderShipmentPreference.size() > 1) {
allOshPrefs.add(this.orderShipmentPreference);
}
// add all of the item adjustments to this list too
Iterator itemIter = cartLines.iterator();
while (itemIter.hasNext()) {
ShoppingCartItem item = (ShoppingCartItem) itemIter.next();
// if nothing has been put into the value, don't set it; must at least have a carrierPartyId and a shipmentMethodTypeId
GenericValue itemOrderShipmentPreference = item.getOrderShipmentPreference();
if (itemOrderShipmentPreference != null && itemOrderShipmentPreference.size() > 1) {
itemOrderShipmentPreference.set("orderItemSeqId", item.getOrderItemSeqId());
allOshPrefs.add(item.getOrderShipmentPreference());
}
}
return allOshPrefs;
}
/** make a list of OrderItemPriceInfos from the ShoppingCartItems */
public List makeAllOrderItemPriceInfos() {
List allInfos = new LinkedList();
// add all of the item adjustments to this list too
Iterator itemIter = cartLines.iterator();
while (itemIter.hasNext()) {
ShoppingCartItem item = (ShoppingCartItem) itemIter.next();
Collection infos = item.getOrderItemPriceInfos();
if (infos != null) {
Iterator infosIter = infos.iterator();
while (infosIter.hasNext()) {
GenericValue orderItemPriceInfo = (GenericValue) infosIter.next();
orderItemPriceInfo.set("orderItemSeqId", item.getOrderItemSeqId());
allInfos.add(orderItemPriceInfo);
}
}
}
return allInfos;
}
public List makeProductPromoUses() {
List productPromoUses = new ArrayList(this.productPromoUseInfoList.size());
String partyId = this.getPartyId();
int sequenceValue = 0;
Iterator productPromoUseInfoIter = this.productPromoUseInfoList.iterator();
while (productPromoUseInfoIter.hasNext()) {
ProductPromoUseInfo productPromoUseInfo = (ProductPromoUseInfo) productPromoUseInfoIter.next();
GenericValue productPromoUse = this.getDelegator().makeValue("ProductPromoUse", null);
productPromoUse.set("promoSequenceId", UtilFormatOut.formatPaddedNumber(sequenceValue, 5));
productPromoUse.set("productPromoId", productPromoUseInfo.getProductPromoId());
productPromoUse.set("productPromoCodeId", productPromoUseInfo.getProductPromoCodeId());
productPromoUse.set("totalDiscountAmount", new Double(productPromoUseInfo.getTotalDiscountAmount()));
productPromoUse.set("quantityLeftInActions", new Double(productPromoUseInfo.getQuantityLeftInActions()));
productPromoUse.set("partyId", partyId);
productPromoUses.add(productPromoUse);
sequenceValue++;
}
return productPromoUses;
}
/** make a list of SurveyResponse object to update with order information set */
public List makeAllOrderItemSurveyResponses() {
List allInfos = new LinkedList();
Iterator itemIter = this.iterator();
while (itemIter.hasNext()) {
ShoppingCartItem item = (ShoppingCartItem) itemIter.next();
List responses = (List) item.getAttribute("surveyResponses");
if (responses != null) {
Iterator ri = responses.iterator();
while (ri.hasNext()) {
String responseId = (String) ri.next();
GenericValue response = null;
try {
response = delegator.findByPrimaryKey("SurveyResponse", UtilMisc.toMap("surveyResponseId", responseId));
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to obtain SurveyResponse record for ID : " + responseId, module);
}
if (response != null) {
response.set("orderItemSeqId", item.getOrderItemSeqId());
allInfos.add(response);
}
}
}
}
r
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -