📄 orderservices.java
字号:
/*
* $Id: OrderServices.java,v 1.34 2004/03/05 20:30:26 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.order;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.*;
import org.ofbiz.base.util.*;
import org.ofbiz.common.DataModelConstants;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityConditionList;
import org.ofbiz.entity.condition.EntityExpr;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.condition.EntityFieldMap;
import org.ofbiz.entity.util.EntityListIterator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.entity.util.EntityFindOptions;
import org.ofbiz.order.shoppingcart.ShoppingCart;
import org.ofbiz.order.shoppingcart.shipping.ShippingEvents;
import org.ofbiz.party.contact.ContactHelper;
import org.ofbiz.product.store.ProductStoreWorker;
import org.ofbiz.security.Security;
import org.ofbiz.service.*;
import org.ofbiz.workflow.WfUtil;
/**
* Order Processing Services
*
* @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>
* @version $Revision: 1.34 $
* @since 2.0
*/
public class OrderServices {
public static final String module = OrderServices.class.getName();
public static final String resource = "org.ofbiz.order.order.PackageMessages";
/** Service for creating a new order */
public static Map createOrder(DispatchContext ctx, Map context) {
Map result = new HashMap();
GenericDelegator delegator = ctx.getDelegator();
LocalDispatcher dispatcher = ctx.getDispatcher();
Security security = ctx.getSecurity();
List toBeStored = new LinkedList();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
// check security
String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "ORDERMGR", "_CREATE");
if (result.size() > 0) {
return result;
}
// get the order type
String orderTypeId = (String) context.get("orderTypeId");
result.put("orderTypeId", orderTypeId);
// lookup the order type entity
GenericValue orderType = null;
try {
orderType = delegator.findByPrimaryKeyCache("OrderType", UtilMisc.toMap("orderTypeId", orderTypeId));
} catch (GenericEntityException e) {
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, "ERROR: OrderType lookup failed: " + e.toString());
return result;
}
// make sure we have a valid order type
if (orderType == null) {
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, "ERROR: Invalid OrderType");
return result;
}
// check to make sure we have something to order
List orderItems = (List) context.get("orderItems");
if (orderItems.size() < 1) {
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, UtilProperties.getMessage(resource, "items.none", locale));
return result;
}
// check inventory and other things for each item
String productStoreId = (String) context.get("productStoreId");
List errorMessages = new LinkedList();
Map normalizedItemQuantities = new HashMap();
Map normalizedItemNames = new HashMap();
Iterator itemIter = orderItems.iterator();
java.sql.Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
// need to run through the items combining any cases where multiple lines refer to the
// same product so the inventory check will work correctly
while (itemIter.hasNext()) {
GenericValue orderItem = (GenericValue) itemIter.next();
String currentProductId = (String) orderItem.get("productId");
if (currentProductId != null) {
// only normalize items with a product associated (ignore non-product items)
if (normalizedItemQuantities.get(currentProductId) == null) {
normalizedItemQuantities.put(currentProductId, new Double(orderItem.getDouble("quantity").doubleValue()));
normalizedItemNames.put(currentProductId, new String(orderItem.getString("itemDescription")));
} else {
Double currentQuantity = (Double) normalizedItemQuantities.get(currentProductId);
normalizedItemQuantities.put(currentProductId, new Double(currentQuantity.doubleValue() + orderItem.getDouble("quantity").doubleValue()));
}
}
}
if (!"PURCHASE_ORDER".equals(orderTypeId) && productStoreId == null) {
return ServiceUtil.returnError("ERROR: The productStoreId can only be null for purchase orders");
}
Iterator normalizedIter = normalizedItemQuantities.keySet().iterator();
while (normalizedIter.hasNext()) {
// lookup the product entity for each normalized item; error on products not found
String currentProductId = (String) normalizedIter.next();
Double currentQuantity = (Double) normalizedItemQuantities.get(currentProductId);
String itemName = (String) normalizedItemNames.get(currentProductId);
GenericValue product = null;
try {
product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", currentProductId));
} catch (GenericEntityException e) {
String errMsg = UtilProperties.getMessage(resource, "product.not_found", new Object[] { currentProductId }, locale);
Debug.logError(e, errMsg, module);
errorMessages.add(errMsg);
continue;
}
if (product == null) {
String errMsg = UtilProperties.getMessage(resource, "product.not_found", new Object[] { currentProductId }, locale);
Debug.logError(errMsg, module);
errorMessages.add(errMsg);
continue;
}
if ("SALES_ORDER".equals(orderTypeId) || "WORK_ORDER".equals(orderTypeId)) {
// check to see if introductionDate hasn't passed yet
if (product.get("introductionDate") != null && nowTimestamp.before(product.getTimestamp("introductionDate"))) {
String excMsg = UtilProperties.getMessage(resource, "product.not_yet_for_sale",
new Object[] { getProductName(product, itemName), product.getString("productId") }, locale);
Debug.logWarning(excMsg, module);
errorMessages.add(excMsg);
continue;
}
}
if ("SALES_ORDER".equals(orderTypeId) || "WORK_ORDER".equals(orderTypeId)) {
// check to see if salesDiscontinuationDate has passed
if (product.get("salesDiscontinuationDate") != null && nowTimestamp.after(product.getTimestamp("salesDiscontinuationDate"))) {
String excMsg = UtilProperties.getMessage(resource, "product.no_longer_for_sale",
new Object[] { getProductName(product, itemName), product.getString("productId") }, locale);
Debug.logWarning(excMsg, module);
errorMessages.add(excMsg);
continue;
}
}
if ("SALES_ORDER".equals(orderTypeId) || "WORK_ORDER".equals(orderTypeId)) {
// check to see if we have inventory available
if (ProductStoreWorker.isStoreInventoryRequired(productStoreId, product, delegator)) {
if (!ProductStoreWorker.isStoreInventoryAvailable(productStoreId, currentProductId,
currentQuantity.doubleValue(), delegator, dispatcher)) {
String invErrMsg = UtilProperties.getMessage(resource, "product.out_of_stock",
new Object[] { getProductName(product, itemName), currentProductId }, locale);
Debug.logWarning(invErrMsg, module);
errorMessages.add(invErrMsg);
continue;
}
}
}
}
if (errorMessages.size() > 0) {
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE_LIST, errorMessages);
return result;
}
// the inital status for ALL order types
String initialStatus = "ORDER_CREATED";
result.put("statusId", initialStatus);
// create the order object
String orderId = delegator.getNextSeqId("OrderHeader").toString();
String billingAccountId = (String) context.get("billingAccountId");
GenericValue order = delegator.makeValue("OrderHeader",
UtilMisc.toMap("orderId", orderId, "orderTypeId", orderTypeId,
"orderDate", nowTimestamp, "entryDate", nowTimestamp,
"statusId", initialStatus, "billingAccountId", billingAccountId));
if (context.get("currencyUom") != null) {
order.set("currencyUom", context.get("currencyUom"));
}
if (context.get("firstAttemptOrderId") != null) {
order.set("firstAttemptOrderId", context.get("firstAttemptOrderId"));
}
if (context.get("grandTotal") != null) {
order.set("grandTotal", context.get("grandTotal"));
}
if (UtilValidate.isNotEmpty((String) context.get("visitId"))) {
order.set("visitId", context.get("visitId"));
}
if (UtilValidate.isNotEmpty((String) context.get("productStoreId"))) {
order.set("productStoreId", context.get("productStoreId"));
}
if (UtilValidate.isNotEmpty((String) context.get("webSiteId"))) {
order.set("webSiteId", context.get("webSiteId"));
}
if (userLogin != null && userLogin.get("userLoginId") != null) {
order.set("createdBy", userLogin.getString("userLoginId"));
}
// first try to create the OrderHeader; if this does not fail, continue.
try {
delegator.create(order);
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot create OrderHeader entity; problems with insert", module);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, "Order creation failed; please notify customer service.");
return result;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -