📄 checkouthelper.java
字号:
/*
* $Id: CheckOutHelper.java,v 1.20 2004/02/28 19:49:47 ajzeneski Exp $
*
* Copyright (c) 2001, 2002 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.order.shoppingcart;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilFormatOut;
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.condition.EntityExpr;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.order.order.OrderChangeHelper;
import org.ofbiz.order.order.OrderReadHelper;
import org.ofbiz.party.contact.ContactHelper;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ModelService;
import org.ofbiz.service.ServiceUtil;
/**
* A facade over the ShoppingCart to simplify the relatively complex
* processing required to create an order in the system.
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @author <a href="mailto:cnelson@einnovation.com">Chris Nelson</a>
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @author <a href="mailto:tristana@twibble.org">Tristan Austin</a>
* @version $Revision: 1.20 $
* @since 2.0
*/
public class CheckOutHelper {
public static final String module = CheckOutHelper.class.getName();
public static final String resource = "OrderUiLabels";
private ShoppingCart cart;
private GenericDelegator delegator;
private LocalDispatcher dispatcher;
public CheckOutHelper(LocalDispatcher dispatcher, GenericDelegator delegator, ShoppingCart cart) {
this.delegator = delegator;
this.dispatcher = dispatcher;
this.cart = cart;
}
public Map setCheckOutShippingAddress(String shippingContactMechId) {
List errorMessages = new ArrayList();
Map result;
String errMsg = null;
if (this.cart != null && this.cart.size() > 0) {
errorMessages.addAll(setCheckOutShippingAddressInternal(shippingContactMechId));
} else {
errMsg = UtilProperties.getMessage(resource,"checkhelper.no_items_in_cart", (cart != null ? cart.getLocale() : Locale.getDefault()));
errorMessages.add(errMsg);
}
if (errorMessages.size() == 1) {
result = ServiceUtil.returnError(errorMessages.get(0).toString());
} else if (errorMessages.size() > 0) {
result = ServiceUtil.returnError(errorMessages);
} else {
result = ServiceUtil.returnSuccess();
}
return result;
}
private List setCheckOutShippingAddressInternal(String shippingContactMechId) {
List errorMessages = new ArrayList();
String errMsg = null;
// set the shipping address
if (UtilValidate.isNotEmpty(shippingContactMechId)) {
this.cart.setShippingContactMechId(shippingContactMechId);
} else {
errMsg = UtilProperties.getMessage(resource,"checkhelper.select_shipping_destination", (cart != null ? cart.getLocale() : Locale.getDefault()));
errorMessages.add(errMsg);
}
return errorMessages;
}
public Map setCheckOutShippingOptions(String shippingMethod, String correspondingPoId, String shippingInstructions,
String orderAdditionalEmails, String maySplit, String giftMessage, String isGift) {
List errorMessages = new ArrayList();
Map result;
String errMsg = null;
if (this.cart != null && this.cart.size() > 0) {
errorMessages.addAll(setCheckOutShippingOptionsInternal(shippingMethod, correspondingPoId,
shippingInstructions, orderAdditionalEmails, maySplit, giftMessage, isGift));
} else {
errMsg = UtilProperties.getMessage(resource,"checkhelper.no_items_in_cart", (cart != null ? cart.getLocale() : Locale.getDefault()));
errorMessages.add(errMsg);
}
if (errorMessages.size() == 1) {
result = ServiceUtil.returnError(errorMessages.get(0).toString());
} else if (errorMessages.size() > 0) {
result = ServiceUtil.returnError(errorMessages);
} else {
result = ServiceUtil.returnSuccess();
}
return result;
}
private List setCheckOutShippingOptionsInternal(String shippingMethod, String correspondingPoId, String shippingInstructions,
String orderAdditionalEmails, String maySplit, String giftMessage, String isGift) {
List errorMessages = new ArrayList();
String errMsg = null;
// set the general shipping options
if (UtilValidate.isNotEmpty(shippingMethod)) {
int delimiterPos = shippingMethod.indexOf('@');
String shipmentMethodTypeId = null;
String carrierPartyId = null;
if (delimiterPos > 0) {
shipmentMethodTypeId = shippingMethod.substring(0, delimiterPos);
carrierPartyId = shippingMethod.substring(delimiterPos + 1);
}
this.cart.setShipmentMethodTypeId(shipmentMethodTypeId);
this.cart.setCarrierPartyId(carrierPartyId);
} else {
errMsg = UtilProperties.getMessage(resource,"checkhelper.select_shipping_method", (cart != null ? cart.getLocale() : Locale.getDefault()));
errorMessages.add(errMsg);
}
this.cart.setShippingInstructions(shippingInstructions);
if (UtilValidate.isNotEmpty(maySplit)) {
cart.setMaySplit(Boolean.valueOf(maySplit));
} else {
errMsg = UtilProperties.getMessage(resource,"checkhelper.select_splitting_preference", (cart != null ? cart.getLocale() : Locale.getDefault()));
errorMessages.add(errMsg);
}
this.cart.setGiftMessage(giftMessage);
if (UtilValidate.isNotEmpty(isGift)) {
cart.setIsGift(Boolean.valueOf(isGift));
} else {
errMsg = UtilProperties.getMessage(resource,"checkhelper.specify_if_order_is_gift", (cart != null ? cart.getLocale() : Locale.getDefault()));
errorMessages.add(errMsg);
}
this.cart.setOrderAdditionalEmails(orderAdditionalEmails);
// set the PO number
if (UtilValidate.isNotEmpty(correspondingPoId)) {
this.cart.setPoNumber(correspondingPoId);
} else {
this.cart.setPoNumber("(none)");
}
return errorMessages;
}
public Map setCheckOutPayment(Map selectedPaymentMethods, List singleUsePayments, String billingAccountId, Double billingAccountAmt) {
List errorMessages = new ArrayList();
Map result;
String errMsg = null;
if (this.cart != null && this.cart.size() > 0) {
errorMessages.addAll(setCheckOutPaymentInternal(selectedPaymentMethods, singleUsePayments, billingAccountId, billingAccountAmt));
} else {
errMsg = UtilProperties.getMessage(resource,"checkhelper.no_items_in_cart", (cart != null ? cart.getLocale() : Locale.getDefault()));
errorMessages.add(errMsg);
}
if (errorMessages.size() == 1) {
result = ServiceUtil.returnError(errorMessages.get(0).toString());
} else if (errorMessages.size() > 0) {
result = ServiceUtil.returnError(errorMessages);
} else {
result = ServiceUtil.returnSuccess();
}
return result;
}
private List setCheckOutPaymentInternal(Map selectedPaymentMethods, List singleUsePayments, String billingAccountId, Double billingAccountAmt) {
List errorMessages = new ArrayList();
String errMsg = null;
if (singleUsePayments == null) {
singleUsePayments = new ArrayList();
}
// set the billing account amount
if (billingAccountId != null && billingAccountAmt != null && !billingAccountId.equals("_NA_")) {
cart.setBillingAccount(billingAccountId, billingAccountAmt.doubleValue());
} else {
cart.setBillingAccount(null, 0.00);
}
// set the payment method option
if (selectedPaymentMethods != null && selectedPaymentMethods.size() > 0) {
// clear out the old payments
this.cart.clearPaymentMethodTypeIds();
this.cart.clearPaymentMethodIds();
// if we are EXT_BILLACT (billing account only) then we need to make sure we have enough credit
if (selectedPaymentMethods.containsKey("EXT_BILLACT")) {
double accountCredit = this.availableAccountBalance(cart.getBillingAccountId());
// make sure we have enough to cover; if this is selected we don't have other payment methods
if (cart.getGrandTotal() > accountCredit) {
errMsg = UtilProperties.getMessage(resource,"checkhelper.insufficient_credit_available_on_account", (cart != null ? cart.getLocale() : Locale.getDefault()));
errorMessages.add(errMsg);
}
}
Set paymentMethods = selectedPaymentMethods.keySet();
Iterator i = paymentMethods.iterator();
while (i.hasNext()) {
String checkOutPaymentId = (String) i.next();
// get the selected amount to use
Double paymentAmount = null;
if (selectedPaymentMethods.get(checkOutPaymentId) != null) {
paymentAmount = (Double) selectedPaymentMethods.get(checkOutPaymentId);
}
// all payment method ids will be numeric, type ids will start with letter
if (Character.isLetter(checkOutPaymentId.charAt(0))) {
this.cart.addPaymentMethodTypeId(checkOutPaymentId);
} else {
boolean singleUse = singleUsePayments.contains(checkOutPaymentId);
this.cart.setPaymentMethodAmount(checkOutPaymentId, paymentAmount, singleUse);
//Debug.logInfo("Set Payment Method : " + checkOutPaymentId + " @ " + paymentAmount, module);
}
}
} else {
errMsg = UtilProperties.getMessage(resource,"checkhelper.select_method_of_payment", (cart != null ? cart.getLocale() : Locale.getDefault()));
errorMessages.add(errMsg);
}
return errorMessages;
}
public Map setCheckOutOptions(String shippingMethod, String shippingContactMechId, Map selectedPaymentMethods,
List singleUsePayments, String billingAccountId, Double billingAccountAmt, String correspondingPoId,
String shippingInstructions, String orderAdditionalEmails, String maySplit, String giftMessage, String isGift) {
List errorMessages = new ArrayList();
Map result = null;
String errMsg = null;
if (this.cart != null && this.cart.size() > 0) {
// set the general shipping options and method
errorMessages.addAll(setCheckOutShippingOptionsInternal(shippingMethod, correspondingPoId,
shippingInstructions, orderAdditionalEmails, maySplit, giftMessage, isGift));
// set the shipping address
errorMessages.addAll(setCheckOutShippingAddressInternal(shippingContactMechId));
// set the payment method(s) option
errorMessages.addAll(setCheckOutPaymentInternal(selectedPaymentMethods, singleUsePayments, billingAccountId, billingAccountAmt));
} else {
errMsg = UtilProperties.getMessage(resource,"checkhelper.no_items_in_cart", (cart != null ? cart.getLocale() : Locale.getDefault()));
errorMessages.add(errMsg);
}
if (errorMessages.size() == 1) {
result = ServiceUtil.returnError(errorMessages.get(0).toString());
} else if (errorMessages.size() > 0) {
result = ServiceUtil.returnError(errorMessages);
} else {
result = ServiceUtil.returnSuccess();
}
return result;
}
public Map checkGiftCard(Map params, Map selectedPaymentMethods) {
List errorMessages = new ArrayList();
Map errorMaps = new HashMap();
Map result = new HashMap();
String errMsg = null;
// handle gift card payment
if (params.get("addGiftCard") != null) {
String gcNum = (String) params.get("giftCardNumber");
String gcPin = (String) params.get("giftCardPin");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -