use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class OrderServices method cancelOrderItem.
/**
* Service to cancel an order item quantity
*/
public static Map<String, Object> cancelOrderItem(DispatchContext ctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = ctx.getDispatcher();
Delegator delegator = ctx.getDelegator();
Locale locale = (Locale) context.get("locale");
Map<String, Object> resp = new HashMap<String, Object>();
GenericValue userLogin = (GenericValue) context.get("userLogin");
BigDecimal cancelQuantity = (BigDecimal) context.get("cancelQuantity");
String orderId = (String) context.get("orderId");
String orderItemSeqId = (String) context.get("orderItemSeqId");
String shipGroupSeqId = (String) context.get("shipGroupSeqId");
Map<String, String> itemReasonMap = UtilGenerics.checkMap(context.get("itemReasonMap"));
Map<String, String> itemCommentMap = UtilGenerics.checkMap(context.get("itemCommentMap"));
Map<String, String> itemQuantityMap = UtilGenerics.checkMap(context.get("itemQtyMap"));
if ((cancelQuantity == null) && UtilValidate.isNotEmpty(itemQuantityMap)) {
String key = orderItemSeqId + ":" + shipGroupSeqId;
if (UtilValidate.isNotEmpty(itemQuantityMap.get(key))) {
cancelQuantity = new BigDecimal(itemQuantityMap.get(key));
}
}
// debugging message info
String itemMsgInfo = orderId + " / " + orderItemSeqId + " / " + shipGroupSeqId;
// check and make sure we have permission to change the order
Security security = ctx.getSecurity();
boolean hasPermission = OrderServices.hasPermission(orderId, userLogin, "UPDATE", security, delegator);
if (!hasPermission) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderYouDoNotHavePermissionToChangeThisOrdersStatus", locale));
}
Map<String, String> fields = UtilMisc.<String, String>toMap("orderId", orderId);
if (orderItemSeqId != null) {
fields.put("orderItemSeqId", orderItemSeqId);
}
if (shipGroupSeqId != null) {
fields.put("shipGroupSeqId", shipGroupSeqId);
}
OrderReadHelper orh = new OrderReadHelper(delegator, orderId);
List<GenericValue> orderItemShipGroupAssocs = null;
try {
orderItemShipGroupAssocs = EntityQuery.use(delegator).from("OrderItemShipGroupAssoc").where(fields).queryList();
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCannotGetOrderItemAssocEntity", UtilMisc.toMap("itemMsgInfo", itemMsgInfo), locale));
}
if (orderItemShipGroupAssocs != null) {
for (GenericValue orderItemShipGroupAssoc : orderItemShipGroupAssocs) {
GenericValue orderItem = null;
String itemStatus = "ITEM_CANCELLED";
try {
orderItem = orderItemShipGroupAssoc.getRelatedOne("OrderItem", false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
if (orderItem == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCannotCancelItemItemNotFound", UtilMisc.toMap("itemMsgInfo", itemMsgInfo), locale));
}
BigDecimal aisgaCancelQuantity = orderItemShipGroupAssoc.getBigDecimal("cancelQuantity");
if (aisgaCancelQuantity == null) {
aisgaCancelQuantity = BigDecimal.ZERO;
}
BigDecimal availableQuantity = orderItemShipGroupAssoc.getBigDecimal("quantity").subtract(aisgaCancelQuantity);
BigDecimal itemCancelQuantity = orderItem.getBigDecimal("cancelQuantity");
if (itemCancelQuantity == null) {
itemCancelQuantity = BigDecimal.ZERO;
}
BigDecimal itemQuantity = orderItem.getBigDecimal("quantity").subtract(itemCancelQuantity);
if (availableQuantity == null) {
availableQuantity = BigDecimal.ZERO;
}
if (itemQuantity == null) {
itemQuantity = BigDecimal.ZERO;
}
if ("PURCHASE_ORDER".equals(orh.getOrderTypeId())) {
BigDecimal receivedQty = orh.getItemReceivedQuantity(orderItem);
if (receivedQty.compareTo(BigDecimal.ZERO) > 0) {
itemStatus = "ITEM_COMPLETED";
}
itemQuantity = itemQuantity.subtract(receivedQty);
} else {
BigDecimal shippedQty = orh.getItemShippedQuantity(orderItem);
if (shippedQty.compareTo(BigDecimal.ZERO) > 0) {
itemStatus = "ITEM_COMPLETED";
}
itemQuantity = itemQuantity.subtract(shippedQty);
}
BigDecimal thisCancelQty = null;
if (cancelQuantity != null) {
thisCancelQty = cancelQuantity;
} else {
thisCancelQty = itemQuantity;
}
if (availableQuantity.compareTo(thisCancelQty) >= 0) {
if (availableQuantity.compareTo(BigDecimal.ZERO) == 0) {
// OrderItemShipGroupAssoc already cancelled
continue;
}
orderItem.set("cancelQuantity", itemCancelQuantity.add(thisCancelQty));
orderItemShipGroupAssoc.set("cancelQuantity", aisgaCancelQuantity.add(thisCancelQty));
try {
List<GenericValue> toStore = UtilMisc.toList(orderItem, orderItemShipGroupAssoc);
delegator.storeAll(toStore);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToSetCancelQuantity", UtilMisc.toMap("itemMsgInfo", itemMsgInfo), locale));
}
Map<String, Object> localCtx = UtilMisc.toMap("userLogin", userLogin, "orderId", orderItem.getString("orderId"), "orderItemSeqId", orderItem.getString("orderItemSeqId"), "shipGroupSeqId", orderItemShipGroupAssoc.getString("shipGroupSeqId"));
if (availableQuantity.compareTo(thisCancelQty) == 0) {
try {
resp = dispatcher.runSync("deleteOrderItemShipGroupAssoc", localCtx);
if (ServiceUtil.isError(resp)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(resp));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
// create order item change record
if (!"Y".equals(orderItem.getString("isPromo"))) {
String reasonEnumId = null;
String changeComments = null;
if (UtilValidate.isNotEmpty(itemReasonMap)) {
reasonEnumId = itemReasonMap.get(orderItem.getString("orderItemSeqId"));
}
if (UtilValidate.isNotEmpty(itemCommentMap)) {
changeComments = itemCommentMap.get(orderItem.getString("orderItemSeqId"));
}
Map<String, Object> serviceCtx = new HashMap<>();
serviceCtx.put("orderId", orderItem.getString("orderId"));
serviceCtx.put("orderItemSeqId", orderItem.getString("orderItemSeqId"));
serviceCtx.put("cancelQuantity", thisCancelQty);
serviceCtx.put("changeTypeEnumId", "ODR_ITM_CANCEL");
serviceCtx.put("reasonEnumId", reasonEnumId);
serviceCtx.put("changeComments", changeComments);
serviceCtx.put("userLogin", userLogin);
try {
resp = dispatcher.runSync("createOrderItemChange", serviceCtx);
if (ServiceUtil.isError(resp)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(resp));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
// log an order note
try {
BigDecimal quantity = thisCancelQty.setScale(1, orderRounding);
String cancelledItemToOrder = UtilProperties.getMessage(resource, "OrderCancelledItemToOrder", locale);
resp = dispatcher.runSync("createOrderNote", UtilMisc.<String, Object>toMap("orderId", orderId, "note", cancelledItemToOrder + orderItem.getString("productId") + " (" + quantity + ")", "internalNote", "Y", "userLogin", userLogin));
if (ServiceUtil.isError(resp)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(resp));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
if (thisCancelQty.compareTo(itemQuantity) >= 0) {
if ("ITEM_COMPLETED".equals(itemStatus) && "SALES_ORDER".equals(orh.getOrderTypeId())) {
// If partial item shipped then release remaining inventory of SO item and marked SO item as completed.
Map<String, Object> cancelOrderItemInvResCtx = UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItem.getString("orderItemSeqId"), "shipGroupSeqId", shipGroupSeqId, "cancelQuantity", thisCancelQty, "userLogin", userLogin);
try {
dispatcher.runSyncIgnore("cancelOrderItemInvResQty", cancelOrderItemInvResCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToUpdateInventoryReservations", UtilMisc.toMap("itemMsgInfo", itemMsgInfo), locale));
}
}
// all items are cancelled -- mark the item as cancelled
Map<String, Object> statusCtx = UtilMisc.<String, Object>toMap("orderId", orderId, "orderItemSeqId", orderItem.getString("orderItemSeqId"), "statusId", itemStatus, "userLogin", userLogin);
try {
dispatcher.runSyncIgnore("changeOrderItemStatus", statusCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToCancelOrderLine", UtilMisc.toMap("itemMsgInfo", itemMsgInfo), locale));
}
} else {
// reverse the inventory reservation
Map<String, Object> invCtx = UtilMisc.<String, Object>toMap("orderId", orderId, "orderItemSeqId", orderItem.getString("orderItemSeqId"), "shipGroupSeqId", shipGroupSeqId, "cancelQuantity", thisCancelQty, "userLogin", userLogin);
try {
dispatcher.runSyncIgnore("cancelOrderItemInvResQty", invCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToUpdateInventoryReservations", UtilMisc.toMap("itemMsgInfo", itemMsgInfo), locale));
}
}
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderInvalidCancelQuantityCannotCancel", UtilMisc.toMap("thisCancelQty", thisCancelQty), locale));
}
}
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCannotCancelItemItemNotFound", UtilMisc.toMap("itemMsgInfo", itemMsgInfo), locale));
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class OrderServices method allowOrderSplit.
public static Map<String, Object> allowOrderSplit(DispatchContext ctx, Map<String, ? extends Object> context) {
Delegator delegator = ctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String orderId = (String) context.get("orderId");
String shipGroupSeqId = (String) context.get("shipGroupSeqId");
Locale locale = (Locale) context.get("locale");
// check and make sure we have permission to change the order
Security security = ctx.getSecurity();
if (!security.hasEntityPermission("ORDERMGR", "_UPDATE", userLogin)) {
GenericValue placingCustomer = null;
try {
placingCustomer = EntityQuery.use(delegator).from("OrderRole").where("orderId", orderId, "partyId", userLogin.getString("partyId"), "roleTypeId", "PLACING_CUSTOMER").queryOne();
} catch (GenericEntityException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCannotGetOrderRoleEntity", locale) + e.getMessage());
}
if (placingCustomer == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderYouDoNotHavePermissionToChangeThisOrdersStatus", locale));
}
}
GenericValue shipGroup = null;
try {
shipGroup = EntityQuery.use(delegator).from("OrderItemShipGroup").where("orderId", orderId, "shipGroupSeqId", shipGroupSeqId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Problems getting OrderItemShipGroup for : " + orderId + " / " + shipGroupSeqId, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderCannotUpdateProblemGettingOrderShipmentPreference", locale));
}
if (shipGroup != null) {
shipGroup.set("maySplit", "Y");
try {
shipGroup.store();
} catch (GenericEntityException e) {
Debug.logError("Problem saving OrderItemShipGroup for : " + orderId + " / " + shipGroupSeqId, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderCannotUpdateProblemSettingOrderShipmentPreference", locale));
}
} else {
Debug.logError("ERROR: Got a NULL OrderItemShipGroup", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderCannotUpdateNoAvailableGroupsToChange", locale));
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class OrderServices method createOrder.
/**
* Service for creating a new order
*/
public static Map<String, Object> createOrder(DispatchContext ctx, Map<String, ? extends Object> context) {
Delegator delegator = ctx.getDelegator();
LocalDispatcher dispatcher = ctx.getDispatcher();
Security security = ctx.getSecurity();
List<GenericValue> toBeStored = new LinkedList<>();
Locale locale = (Locale) context.get("locale");
Map<String, Object> successResult = ServiceUtil.returnSuccess();
GenericValue userLogin = (GenericValue) context.get("userLogin");
// get the order type
String orderTypeId = (String) context.get("orderTypeId");
String partyId = (String) context.get("partyId");
String billFromVendorPartyId = (String) context.get("billFromVendorPartyId");
// check security permissions for order:
// SALES ORDERS - if userLogin has ORDERMGR_SALES_CREATE or ORDERMGR_CREATE permission, or if it is same party as the partyId, or
// if it is an AGENT (sales rep) creating an order for his customer
// PURCHASE ORDERS - if there is a PURCHASE_ORDER permission
Map<String, Object> resultSecurity = new HashMap<>();
boolean hasPermission = OrderServices.hasPermission(orderTypeId, partyId, userLogin, "CREATE", security);
// jacopoc: what is the meaning of this code block? FIXME
if (!hasPermission) {
partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, resultSecurity, "ORDERMGR", "_CREATE");
if (resultSecurity.size() > 0) {
return resultSecurity;
}
}
// get the product store for the order, but it is required only for sales orders
String productStoreId = (String) context.get("productStoreId");
GenericValue productStore = null;
if (("SALES_ORDER".equals(orderTypeId)) && (UtilValidate.isNotEmpty(productStoreId))) {
try {
productStore = EntityQuery.use(delegator).from("ProductStore").where("productStoreId", productStoreId).cache().queryOne();
} catch (GenericEntityException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCouldNotFindProductStoreWithID", UtilMisc.toMap("productStoreId", productStoreId), locale) + e.toString());
}
}
// figure out if the order is immediately fulfilled based on product store settings
boolean isImmediatelyFulfilled = false;
if (productStore != null) {
isImmediatelyFulfilled = "Y".equals(productStore.getString("isImmediatelyFulfilled"));
}
successResult.put("orderTypeId", orderTypeId);
// lookup the order type entity
GenericValue orderType = null;
try {
orderType = EntityQuery.use(delegator).from("OrderType").where("orderTypeId", orderTypeId).cache().queryOne();
} catch (GenericEntityException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorOrderTypeLookupFailed", locale) + e.toString());
}
// make sure we have a valid order type
if (orderType == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorInvalidOrderTypeWithID", UtilMisc.toMap("orderTypeId", orderTypeId), locale));
}
// check to make sure we have something to order
List<GenericValue> orderItems = UtilGenerics.checkList(context.get("orderItems"));
if (orderItems.size() < 1) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "items.none", locale));
}
// all this marketing pkg auto stuff is deprecated in favor of MARKETING_PKG_AUTO productTypeId and a BOM of MANUF_COMPONENT assocs
// these need to be retrieved now because they might be needed for exploding MARKETING_PKG_AUTO
List<GenericValue> orderAdjustments = UtilGenerics.checkList(context.get("orderAdjustments"));
List<GenericValue> orderItemShipGroupInfo = UtilGenerics.checkList(context.get("orderItemShipGroupInfo"));
List<GenericValue> orderItemPriceInfo = UtilGenerics.checkList(context.get("orderItemPriceInfos"));
// check inventory and other things for each item
List<String> errorMessages = new LinkedList<>();
Map<String, BigDecimal> normalizedItemQuantities = new HashMap<>();
Map<String, String> normalizedItemNames = new HashMap<>();
Map<String, GenericValue> itemValuesBySeqId = new HashMap<>();
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
// also count quantities ordered while going through the loop
for (GenericValue orderItem : orderItems) {
// start by putting it in the itemValuesById Map
itemValuesBySeqId.put(orderItem.getString("orderItemSeqId"), orderItem);
String currentProductId = orderItem.getString("productId");
if (currentProductId != null) {
// only normalize items with a product associated (ignore non-product items)
if (normalizedItemQuantities.get(currentProductId) == null) {
normalizedItemQuantities.put(currentProductId, orderItem.getBigDecimal("quantity"));
normalizedItemNames.put(currentProductId, orderItem.getString("itemDescription"));
} else {
BigDecimal currentQuantity = normalizedItemQuantities.get(currentProductId);
normalizedItemQuantities.put(currentProductId, currentQuantity.add(orderItem.getBigDecimal("quantity")));
}
try {
// count product ordered quantities
// run this synchronously so it will run in the same transaction
Map<String, Object> result = dispatcher.runSync("countProductQuantityOrdered", UtilMisc.<String, Object>toMap("productId", currentProductId, "quantity", orderItem.getBigDecimal("quantity"), "userLogin", userLogin));
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
} catch (GenericServiceException e1) {
Debug.logError(e1, "Error calling countProductQuantityOrdered service", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCallingCountProductQuantityOrderedService", locale) + e1.toString());
}
}
}
if (!"PURCHASE_ORDER".equals(orderTypeId) && productStoreId == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorTheProductStoreIdCanOnlyBeNullForPurchaseOrders", locale));
}
Timestamp orderDate = (Timestamp) context.get("orderDate");
for (String currentProductId : normalizedItemQuantities.keySet()) {
// lookup the product entity for each normalized item; error on products not found
BigDecimal currentQuantity = normalizedItemQuantities.get(currentProductId);
String itemName = normalizedItemNames.get(currentProductId);
GenericValue product = null;
try {
product = EntityQuery.use(delegator).from("Product").where("productId", currentProductId).cache().queryOne();
} catch (GenericEntityException e) {
String errMsg = UtilProperties.getMessage(resource_error, "product.not_found", new Object[] { currentProductId }, locale);
Debug.logError(e, errMsg, module);
errorMessages.add(errMsg);
continue;
}
if (product == null) {
String errMsg = UtilProperties.getMessage(resource_error, "product.not_found", new Object[] { currentProductId }, locale);
Debug.logError(errMsg, module);
errorMessages.add(errMsg);
continue;
}
if ("SALES_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_error, "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)) {
boolean salesDiscontinuationFlag = false;
// When past orders are imported, they should be imported even if sales discontinuation date is in the past but if the order date was before it
if (orderDate != null && product.get("salesDiscontinuationDate") != null) {
salesDiscontinuationFlag = orderDate.after(product.getTimestamp("salesDiscontinuationDate")) && nowTimestamp.after(product.getTimestamp("salesDiscontinuationDate"));
} else if (product.get("salesDiscontinuationDate") != null) {
salesDiscontinuationFlag = nowTimestamp.after(product.getTimestamp("salesDiscontinuationDate"));
}
// check to see if salesDiscontinuationDate has passed
if (salesDiscontinuationFlag) {
String excMsg = UtilProperties.getMessage(resource_error, "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)) {
// check to see if we have inventory available
try {
Map<String, Object> invReqResult = dispatcher.runSync("isStoreInventoryAvailableOrNotRequired", UtilMisc.toMap("productStoreId", productStoreId, "productId", product.get("productId"), "product", product, "quantity", currentQuantity));
if (ServiceUtil.isError(invReqResult)) {
errorMessages.add(ServiceUtil.getErrorMessage(invReqResult));
List<String> errMsgList = UtilGenerics.checkList(invReqResult.get(ModelService.ERROR_MESSAGE_LIST));
errorMessages.addAll(errMsgList);
} else if (!"Y".equals(invReqResult.get("availableOrNotRequired"))) {
String invErrMsg = UtilProperties.getMessage(resource_error, "product.out_of_stock", new Object[] { getProductName(product, itemName), currentProductId }, locale);
Debug.logWarning(invErrMsg, module);
errorMessages.add(invErrMsg);
continue;
}
} catch (GenericServiceException e) {
String errMsg = "Fatal error calling inventory checking services: " + e.toString();
Debug.logError(e, errMsg, module);
errorMessages.add(errMsg);
}
}
}
// add the fixedAsset id to the workefforts map by obtaining the fixed Asset number from the FixedAssetProduct table
// is an optional parameter from this service but mandatory for rental items
List<GenericValue> workEfforts = UtilGenerics.checkList(context.get("workEfforts"));
for (GenericValue orderItem : orderItems) {
if ("RENTAL_ORDER_ITEM".equals(orderItem.getString("orderItemTypeId"))) {
// check to see if workefforts are available for this order type.
if (UtilValidate.isEmpty(workEfforts)) {
String errMsg = "Work Efforts missing for ordertype RENTAL_ORDER_ITEM " + "Product: " + orderItem.getString("productId");
Debug.logError(errMsg, module);
errorMessages.add(errMsg);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderRentalOrderItems", locale));
}
for (GenericValue workEffort : workEfforts) {
// create the entity maps required.
if (workEffort.getString("workEffortId").equals(orderItem.getString("orderItemSeqId"))) {
List<GenericValue> selFixedAssetProduct = null;
try {
selFixedAssetProduct = EntityQuery.use(delegator).from("FixedAssetProduct").where("productId", orderItem.getString("productId"), "fixedAssetProductTypeId", "FAPT_USE").filterByDate(nowTimestamp, "fromDate", "thruDate").queryList();
} catch (GenericEntityException e) {
String excMsg = "Could not find related Fixed Asset for the product: " + orderItem.getString("productId");
Debug.logError(excMsg, module);
errorMessages.add(excMsg);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderCouldNotFindRelatedFixedAssetForTheProduct", UtilMisc.toMap("productId", orderItem.getString("productId")), locale));
}
if (UtilValidate.isNotEmpty(selFixedAssetProduct)) {
Iterator<GenericValue> firstOne = selFixedAssetProduct.iterator();
if (firstOne.hasNext()) {
GenericValue fixedAssetProduct = delegator.makeValue("FixedAssetProduct");
fixedAssetProduct = firstOne.next();
workEffort.set("fixedAssetId", fixedAssetProduct.get("fixedAssetId"));
// have quantity easy available later...
workEffort.set("quantityToProduce", orderItem.get("quantity"));
workEffort.set("createdByUserLogin", userLogin.get("userLoginId"));
}
}
// item found, so go to next orderitem.
break;
}
}
}
}
if (errorMessages.size() > 0) {
return ServiceUtil.returnError(errorMessages);
}
// the inital status for ALL order types
String initialStatus = "ORDER_CREATED";
successResult.put("statusId", initialStatus);
// create the order object
String orderId = (String) context.get("orderId");
String orgPartyId = null;
if (productStore != null) {
orgPartyId = productStore.getString("payToPartyId");
} else if (billFromVendorPartyId != null) {
orgPartyId = billFromVendorPartyId;
}
if (UtilValidate.isNotEmpty(orgPartyId)) {
Map<String, Object> getNextOrderIdContext = new HashMap<>();
getNextOrderIdContext.putAll(context);
getNextOrderIdContext.put("partyId", orgPartyId);
getNextOrderIdContext.put("userLogin", userLogin);
if (("SALES_ORDER".equals(orderTypeId)) || (productStoreId != null)) {
getNextOrderIdContext.put("productStoreId", productStoreId);
}
if (UtilValidate.isEmpty(orderId)) {
try {
getNextOrderIdContext = ctx.makeValidContext("getNextOrderId", ModelService.IN_PARAM, getNextOrderIdContext);
Map<String, Object> getNextOrderIdResult = dispatcher.runSync("getNextOrderId", getNextOrderIdContext);
if (ServiceUtil.isError(getNextOrderIdResult)) {
String errMsg = UtilProperties.getMessage(resource_error, "OrderErrorGettingNextOrderIdWhileCreatingOrder", locale);
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(getNextOrderIdResult));
}
orderId = (String) getNextOrderIdResult.get("orderId");
} catch (GenericServiceException e) {
String errMsg = UtilProperties.getMessage(resource_error, "OrderCaughtGenericServiceExceptionWhileGettingOrderId", locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
}
}
if (UtilValidate.isEmpty(orderId)) {
// for purchase orders or when other orderId generation fails, a product store id should not be required to make an order
orderId = delegator.getNextSeqId("OrderHeader");
}
String billingAccountId = (String) context.get("billingAccountId");
if (orderDate == null) {
orderDate = nowTimestamp;
}
Map<String, Object> orderHeaderMap = UtilMisc.<String, Object>toMap("orderId", orderId, "orderTypeId", orderTypeId, "orderDate", orderDate, "entryDate", nowTimestamp, "statusId", initialStatus, "billingAccountId", billingAccountId);
orderHeaderMap.put("orderName", context.get("orderName"));
orderHeaderMap.put("agreementId", context.get("agreementId"));
if (isImmediatelyFulfilled) {
// also flag this order as needing inventory issuance so that when it is set to complete it will be issued immediately (needsInventoryIssuance = Y)
orderHeaderMap.put("needsInventoryIssuance", "Y");
}
GenericValue orderHeader = delegator.makeValue("OrderHeader", orderHeaderMap);
// determine the sales channel
String salesChannelEnumId = (String) context.get("salesChannelEnumId");
if ((salesChannelEnumId == null) || "UNKNWN_SALES_CHANNEL".equals(salesChannelEnumId)) {
// try the default store sales channel
if ("SALES_ORDER".equals(orderTypeId) && (productStore != null)) {
salesChannelEnumId = productStore.getString("defaultSalesChannelEnumId");
}
// if there's still no channel, set to unknown channel
if (salesChannelEnumId == null) {
salesChannelEnumId = "UNKNWN_SALES_CHANNEL";
}
}
orderHeader.set("salesChannelEnumId", salesChannelEnumId);
if (context.get("currencyUom") != null) {
orderHeader.set("currencyUom", context.get("currencyUom"));
}
if (context.get("firstAttemptOrderId") != null) {
orderHeader.set("firstAttemptOrderId", context.get("firstAttemptOrderId"));
}
if (context.get("grandTotal") != null) {
orderHeader.set("grandTotal", context.get("grandTotal"));
}
if (UtilValidate.isNotEmpty(context.get("visitId"))) {
orderHeader.set("visitId", context.get("visitId"));
}
if (UtilValidate.isNotEmpty(context.get("internalCode"))) {
orderHeader.set("internalCode", context.get("internalCode"));
}
if (UtilValidate.isNotEmpty(context.get("externalId"))) {
orderHeader.set("externalId", context.get("externalId"));
}
if (UtilValidate.isNotEmpty(context.get("originFacilityId"))) {
orderHeader.set("originFacilityId", context.get("originFacilityId"));
}
if (UtilValidate.isNotEmpty(context.get("productStoreId"))) {
orderHeader.set("productStoreId", context.get("productStoreId"));
}
if (UtilValidate.isNotEmpty(context.get("transactionId"))) {
orderHeader.set("transactionId", context.get("transactionId"));
}
if (UtilValidate.isNotEmpty(context.get("terminalId"))) {
orderHeader.set("terminalId", context.get("terminalId"));
}
if (UtilValidate.isNotEmpty(context.get("autoOrderShoppingListId"))) {
orderHeader.set("autoOrderShoppingListId", context.get("autoOrderShoppingListId"));
}
if (UtilValidate.isNotEmpty(context.get("webSiteId"))) {
orderHeader.set("webSiteId", context.get("webSiteId"));
}
if (userLogin != null && userLogin.get("userLoginId") != null) {
orderHeader.set("createdBy", userLogin.getString("userLoginId"));
}
String invoicePerShipment = EntityUtilProperties.getPropertyValue("accounting", "create.invoice.per.shipment", delegator);
if (UtilValidate.isNotEmpty(invoicePerShipment)) {
orderHeader.set("invoicePerShipment", invoicePerShipment);
}
// first try to create the OrderHeader; if this does not fail, continue.
try {
delegator.create(orderHeader);
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot create OrderHeader entity; problems with insert", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderOrderCreationFailedPleaseNotifyCustomerService", locale));
}
// create the order status record
String orderStatusSeqId = delegator.getNextSeqId("OrderStatus");
GenericValue orderStatus = delegator.makeValue("OrderStatus", UtilMisc.toMap("orderStatusId", orderStatusSeqId));
orderStatus.set("orderId", orderId);
orderStatus.set("statusId", orderHeader.getString("statusId"));
orderStatus.set("statusDatetime", nowTimestamp);
orderStatus.set("statusUserLogin", userLogin.getString("userLoginId"));
toBeStored.add(orderStatus);
// before processing orderItems process orderItemGroups so that they'll be in place for the foreign keys and what not
List<GenericValue> orderItemGroups = UtilGenerics.checkList(context.get("orderItemGroups"));
if (UtilValidate.isNotEmpty(orderItemGroups)) {
for (GenericValue orderItemGroup : orderItemGroups) {
orderItemGroup.set("orderId", orderId);
toBeStored.add(orderItemGroup);
}
}
// set the order items
for (GenericValue orderItem : orderItems) {
orderItem.set("orderId", orderId);
toBeStored.add(orderItem);
// create the item status record
String itemStatusId = delegator.getNextSeqId("OrderStatus");
GenericValue itemStatus = delegator.makeValue("OrderStatus", UtilMisc.toMap("orderStatusId", itemStatusId));
itemStatus.put("statusId", orderItem.get("statusId"));
itemStatus.put("orderId", orderId);
itemStatus.put("orderItemSeqId", orderItem.get("orderItemSeqId"));
itemStatus.put("statusDatetime", nowTimestamp);
itemStatus.set("statusUserLogin", userLogin.getString("userLoginId"));
toBeStored.add(itemStatus);
}
// set the order attributes
List<GenericValue> orderAttributes = UtilGenerics.checkList(context.get("orderAttributes"));
if (UtilValidate.isNotEmpty(orderAttributes)) {
for (GenericValue oatt : orderAttributes) {
oatt.set("orderId", orderId);
toBeStored.add(oatt);
}
}
// set the order item attributes
List<GenericValue> orderItemAttributes = UtilGenerics.checkList(context.get("orderItemAttributes"));
if (UtilValidate.isNotEmpty(orderItemAttributes)) {
for (GenericValue oiatt : orderItemAttributes) {
oiatt.set("orderId", orderId);
toBeStored.add(oiatt);
}
}
// create the order internal notes
List<String> orderInternalNotes = UtilGenerics.checkList(context.get("orderInternalNotes"));
if (UtilValidate.isNotEmpty(orderInternalNotes)) {
for (String orderInternalNote : orderInternalNotes) {
try {
Map<String, Object> noteOutputMap = dispatcher.runSync("createOrderNote", UtilMisc.<String, Object>toMap("orderId", orderId, "internalNote", "Y", "note", orderInternalNote, "userLogin", userLogin));
if (ServiceUtil.isError(noteOutputMap)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderOrderNoteCannotBeCreated", UtilMisc.toMap("errorString", ""), locale), null, null, noteOutputMap);
}
} catch (GenericServiceException e) {
Debug.logError(e, "Error creating internal notes while creating order: " + e.toString(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderOrderNoteCannotBeCreated", UtilMisc.toMap("errorString", e.toString()), locale));
}
}
}
// create the order public notes
List<String> orderNotes = UtilGenerics.checkList(context.get("orderNotes"));
if (UtilValidate.isNotEmpty(orderNotes)) {
for (String orderNote : orderNotes) {
try {
Map<String, Object> noteOutputMap = dispatcher.runSync("createOrderNote", UtilMisc.<String, Object>toMap("orderId", orderId, "internalNote", "N", "note", orderNote, "userLogin", userLogin));
if (ServiceUtil.isError(noteOutputMap)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderOrderNoteCannotBeCreated", UtilMisc.toMap("errorString", ""), locale), null, null, noteOutputMap);
}
} catch (GenericServiceException e) {
Debug.logError(e, "Error creating notes while creating order: " + e.toString(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderOrderNoteCannotBeCreated", UtilMisc.toMap("errorString", e.toString()), locale));
}
}
}
// create also the techData calendars to keep track of availability of the fixed asset.
if (UtilValidate.isNotEmpty(workEfforts)) {
List<GenericValue> tempList = new LinkedList<>();
for (GenericValue workEffort : workEfforts) {
// create the entity maps required.
GenericValue workOrderItemFulfillment = delegator.makeValue("WorkOrderItemFulfillment");
// find fixed asset supplied on the workeffort map
GenericValue fixedAsset = null;
Debug.logInfo("find the fixedAsset", module);
try {
fixedAsset = EntityQuery.use(delegator).from("FixedAsset").where("fixedAssetId", workEffort.get("fixedAssetId")).queryOne();
} catch (GenericEntityException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderFixedAssetNotFoundFixedAssetId", UtilMisc.toMap("fixedAssetId", workEffort.get("fixedAssetId")), locale));
}
if (fixedAsset == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderFixedAssetNotFoundFixedAssetId", UtilMisc.toMap("fixedAssetId", workEffort.get("fixedAssetId")), locale));
}
// see if this fixed asset has a calendar, when no create one and attach to fixed asset
Debug.logInfo("find the techdatacalendar", module);
GenericValue techDataCalendar = null;
try {
techDataCalendar = fixedAsset.getRelatedOne("TechDataCalendar", false);
} catch (GenericEntityException e) {
Debug.logInfo("TechData calendar does not exist yet so create for fixedAsset: " + fixedAsset.get("fixedAssetId"), module);
}
if (techDataCalendar == null) {
for (GenericValue currentValue : tempList) {
if ("FixedAsset".equals(currentValue.getEntityName()) && currentValue.getString("fixedAssetId").equals(workEffort.getString("fixedAssetId"))) {
fixedAsset = currentValue;
break;
}
}
for (GenericValue currentValue : tempList) {
if ("TechDataCalendar".equals(currentValue.getEntityName()) && currentValue.getString("calendarId").equals(fixedAsset.getString("calendarId"))) {
techDataCalendar = currentValue;
break;
}
}
}
if (techDataCalendar == null) {
techDataCalendar = delegator.makeValue("TechDataCalendar");
Debug.logInfo("create techdata calendar because it does not exist", module);
String calendarId = delegator.getNextSeqId("TechDataCalendar");
techDataCalendar.set("calendarId", calendarId);
tempList.add(techDataCalendar);
Debug.logInfo("update fixed Asset", module);
fixedAsset.set("calendarId", calendarId);
tempList.add(fixedAsset);
}
// then create the workEffort and the workOrderItemFulfillment to connect to the order and orderItem
// orderItemSeqNo is stored here so save first
workOrderItemFulfillment.set("orderItemSeqId", workEffort.get("workEffortId").toString());
// workeffort
// find next available workEffortId
String workEffortId = delegator.getNextSeqId("WorkEffort");
workEffort.set("workEffortId", workEffortId);
workEffort.set("workEffortTypeId", "ASSET_USAGE");
// a lot of workefforts selection services expect a value here....
workEffort.set("currentStatusId", "_NA_");
// store workeffort before workOrderItemFulfillment because of workEffortId key constraint
toBeStored.add(workEffort);
// workOrderItemFulfillment
workOrderItemFulfillment.set("workEffortId", workEffortId);
workOrderItemFulfillment.set("orderId", orderId);
toBeStored.add(workOrderItemFulfillment);
// now create the TechDataExcDay, when they do not exist, create otherwise update the capacity values
// please note that calendarId is the same for (TechData)Calendar, CalendarExcDay and CalendarExWeek
Timestamp estimatedStartDate = workEffort.getTimestamp("estimatedStartDate");
Timestamp estimatedCompletionDate = workEffort.getTimestamp("estimatedCompletionDate");
long dayCount = (estimatedCompletionDate.getTime() - estimatedStartDate.getTime()) / 86400000;
while (--dayCount >= 0) {
GenericValue techDataCalendarExcDay = null;
// find an existing Day exception record
Timestamp exceptionDateStartTime = UtilDateTime.getDayStart(new Timestamp(estimatedStartDate.getTime()), (int) dayCount);
try {
techDataCalendarExcDay = EntityQuery.use(delegator).from("TechDataCalendarExcDay").where("calendarId", fixedAsset.get("calendarId"), "exceptionDateStartTime", exceptionDateStartTime).queryOne();
} catch (GenericEntityException e) {
Debug.logInfo(" techData excday record not found so creating........", module);
}
if (techDataCalendarExcDay == null) {
for (GenericValue currentValue : tempList) {
if ("TechDataCalendarExcDay".equals(currentValue.getEntityName()) && currentValue.getString("calendarId").equals(fixedAsset.getString("calendarId")) && currentValue.getTimestamp("exceptionDateStartTime").equals(exceptionDateStartTime)) {
techDataCalendarExcDay = currentValue;
break;
}
}
}
if (techDataCalendarExcDay == null) {
techDataCalendarExcDay = delegator.makeValue("TechDataCalendarExcDay");
techDataCalendarExcDay.set("calendarId", fixedAsset.get("calendarId"));
techDataCalendarExcDay.set("exceptionDateStartTime", exceptionDateStartTime);
// initialise to zero
techDataCalendarExcDay.set("usedCapacity", BigDecimal.ZERO);
techDataCalendarExcDay.set("exceptionCapacity", fixedAsset.getBigDecimal("productionCapacity"));
}
// add the quantity to the quantity on the date record
BigDecimal newUsedCapacity = techDataCalendarExcDay.getBigDecimal("usedCapacity").add(workEffort.getBigDecimal("quantityToProduce"));
// check to see if the requested quantity is available on the requested day but only when the maximum capacity is set on the fixed asset
if (fixedAsset.get("productionCapacity") != null) {
if (newUsedCapacity.compareTo(techDataCalendarExcDay.getBigDecimal("exceptionCapacity")) > 0) {
String errMsg = UtilProperties.getMessage(resource_error, "OrderFixedAssetSoldOut", UtilMisc.toMap("fixedAssetId", workEffort.get("fixedAssetId"), "exceptionDateStartTime", techDataCalendarExcDay.getString("exceptionDateStartTime")), locale);
Debug.logError(errMsg, module);
errorMessages.add(errMsg);
continue;
}
}
techDataCalendarExcDay.set("usedCapacity", newUsedCapacity);
tempList.add(techDataCalendarExcDay);
}
}
if (tempList.size() > 0) {
toBeStored.addAll(tempList);
}
}
if (errorMessages.size() > 0) {
return ServiceUtil.returnError(errorMessages);
}
// item adjustments...
if (UtilValidate.isNotEmpty(orderAdjustments)) {
for (GenericValue orderAdjustment : orderAdjustments) {
try {
orderAdjustment.set("orderAdjustmentId", delegator.getNextSeqId("OrderAdjustment"));
} catch (IllegalArgumentException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCouldNotGetNextSequenceIdForOrderAdjustmentCannotCreateOrder", locale));
}
orderAdjustment.set("orderId", orderId);
orderAdjustment.set("createdDate", UtilDateTime.nowTimestamp());
orderAdjustment.set("createdByUserLogin", userLogin.getString("userLoginId"));
if (UtilValidate.isEmpty(orderAdjustment.get("orderItemSeqId"))) {
orderAdjustment.set("orderItemSeqId", DataModelConstants.SEQ_ID_NA);
}
if (UtilValidate.isEmpty(orderAdjustment.get("shipGroupSeqId"))) {
orderAdjustment.set("shipGroupSeqId", DataModelConstants.SEQ_ID_NA);
}
toBeStored.add(orderAdjustment);
}
}
// set the order contact mechs
List<GenericValue> orderContactMechs = UtilGenerics.checkList(context.get("orderContactMechs"));
if (UtilValidate.isNotEmpty(orderContactMechs)) {
for (GenericValue ocm : orderContactMechs) {
ocm.set("orderId", orderId);
toBeStored.add(ocm);
}
}
// set the order item contact mechs
List<GenericValue> orderItemContactMechs = UtilGenerics.checkList(context.get("orderItemContactMechs"));
if (UtilValidate.isNotEmpty(orderItemContactMechs)) {
for (GenericValue oicm : orderItemContactMechs) {
oicm.set("orderId", orderId);
toBeStored.add(oicm);
}
}
// set the order item ship groups
// this list will contain the ids of all the ship groups for drop shipments (no reservations)
List<String> dropShipGroupIds = new LinkedList<>();
if (UtilValidate.isNotEmpty(orderItemShipGroupInfo)) {
for (GenericValue valueObj : orderItemShipGroupInfo) {
valueObj.set("orderId", orderId);
if ("OrderItemShipGroup".equals(valueObj.getEntityName())) {
// ship group
if (valueObj.get("carrierRoleTypeId") == null) {
valueObj.set("carrierRoleTypeId", "CARRIER");
}
if (UtilValidate.isNotEmpty(valueObj.getString("supplierPartyId"))) {
dropShipGroupIds.add(valueObj.getString("shipGroupSeqId"));
}
} else if ("OrderAdjustment".equals(valueObj.getEntityName())) {
// shipping / tax adjustment(s)
if (UtilValidate.isEmpty(valueObj.get("orderItemSeqId"))) {
valueObj.set("orderItemSeqId", DataModelConstants.SEQ_ID_NA);
}
valueObj.set("orderAdjustmentId", delegator.getNextSeqId("OrderAdjustment"));
valueObj.set("createdDate", UtilDateTime.nowTimestamp());
valueObj.set("createdByUserLogin", userLogin.getString("userLoginId"));
}
toBeStored.add(valueObj);
}
}
// set the additional party roles
Map<String, List<String>> additionalPartyRole = UtilGenerics.checkMap(context.get("orderAdditionalPartyRoleMap"));
if (additionalPartyRole != null) {
for (Map.Entry<String, List<String>> entry : additionalPartyRole.entrySet()) {
String additionalRoleTypeId = entry.getKey();
List<String> parties = entry.getValue();
if (parties != null) {
for (String additionalPartyId : parties) {
toBeStored.add(delegator.makeValue("PartyRole", UtilMisc.toMap("partyId", additionalPartyId, "roleTypeId", additionalRoleTypeId)));
toBeStored.add(delegator.makeValue("OrderRole", UtilMisc.toMap("orderId", orderId, "partyId", additionalPartyId, "roleTypeId", additionalRoleTypeId)));
}
}
}
}
// set the item survey responses
List<GenericValue> surveyResponses = UtilGenerics.checkList(context.get("orderItemSurveyResponses"));
if (UtilValidate.isNotEmpty(surveyResponses)) {
for (GenericValue surveyResponse : surveyResponses) {
surveyResponse.set("orderId", orderId);
toBeStored.add(surveyResponse);
}
}
// set the item price info; NOTE: this must be after the orderItems are stored for referential integrity
if (UtilValidate.isNotEmpty(orderItemPriceInfo)) {
for (GenericValue oipi : orderItemPriceInfo) {
try {
oipi.set("orderItemPriceInfoId", delegator.getNextSeqId("OrderItemPriceInfo"));
} catch (IllegalArgumentException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCouldNotGetNextSequenceIdForOrderItemPriceInfoCannotCreateOrder", locale));
}
oipi.set("orderId", orderId);
toBeStored.add(oipi);
}
}
// set the item associations
List<GenericValue> orderItemAssociations = UtilGenerics.checkList(context.get("orderItemAssociations"));
if (UtilValidate.isNotEmpty(orderItemAssociations)) {
for (GenericValue orderItemAssociation : orderItemAssociations) {
if (orderItemAssociation.get("toOrderId") == null) {
orderItemAssociation.set("toOrderId", orderId);
} else if (orderItemAssociation.get("orderId") == null) {
orderItemAssociation.set("orderId", orderId);
}
toBeStored.add(orderItemAssociation);
}
}
// store the orderProductPromoUseInfos
List<GenericValue> orderProductPromoUses = UtilGenerics.checkList(context.get("orderProductPromoUses"));
if (UtilValidate.isNotEmpty(orderProductPromoUses)) {
for (GenericValue productPromoUse : orderProductPromoUses) {
productPromoUse.set("orderId", orderId);
toBeStored.add(productPromoUse);
}
}
// store the orderProductPromoCodes
Set<String> orderProductPromoCodes = UtilGenerics.checkSet(context.get("orderProductPromoCodes"));
if (UtilValidate.isNotEmpty(orderProductPromoCodes)) {
for (String productPromoCodeId : orderProductPromoCodes) {
GenericValue orderProductPromoCode = delegator.makeValue("OrderProductPromoCode");
orderProductPromoCode.set("orderId", orderId);
orderProductPromoCode.set("productPromoCodeId", productPromoCodeId);
toBeStored.add(orderProductPromoCode);
}
}
// see the attributeRoleMap definition near the top of this file for attribute-role mappings
Map<String, String> attributeRoleMap = salesAttributeRoleMap;
if ("PURCHASE_ORDER".equals(orderTypeId)) {
attributeRoleMap = purchaseAttributeRoleMap;
}
for (Map.Entry<String, String> attributeRoleEntry : attributeRoleMap.entrySet()) {
if (UtilValidate.isNotEmpty(context.get(attributeRoleEntry.getKey()))) {
// make sure the party is in the role before adding
toBeStored.add(delegator.makeValue("PartyRole", UtilMisc.toMap("partyId", context.get(attributeRoleEntry.getKey()), "roleTypeId", attributeRoleEntry.getValue())));
toBeStored.add(delegator.makeValue("OrderRole", UtilMisc.toMap("orderId", orderId, "partyId", context.get(attributeRoleEntry.getKey()), "roleTypeId", attributeRoleEntry.getValue())));
}
}
// set the affiliate -- This is going to be removed...
String affiliateId = (String) context.get("affiliateId");
if (UtilValidate.isNotEmpty(affiliateId)) {
toBeStored.add(delegator.makeValue("OrderRole", UtilMisc.toMap("orderId", orderId, "partyId", affiliateId, "roleTypeId", "AFFILIATE")));
}
// set the distributor
String distributorId = (String) context.get("distributorId");
if (UtilValidate.isNotEmpty(distributorId)) {
toBeStored.add(delegator.makeValue("OrderRole", UtilMisc.toMap("orderId", orderId, "partyId", distributorId, "roleTypeId", "DISTRIBUTOR")));
}
// find all parties in role VENDOR associated with WebSite OR ProductStore (where WebSite overrides, if specified), associated first valid with the Order
if (UtilValidate.isNotEmpty(context.get("productStoreId"))) {
try {
GenericValue productStoreRole = EntityQuery.use(delegator).from("ProductStoreRole").where("roleTypeId", "VENDOR", "productStoreId", context.get("productStoreId")).orderBy("-fromDate").filterByDate().queryFirst();
if (productStoreRole != null) {
toBeStored.add(delegator.makeValue("OrderRole", UtilMisc.toMap("orderId", orderId, "partyId", productStoreRole.get("partyId"), "roleTypeId", "VENDOR")));
}
} catch (GenericEntityException e) {
Debug.logError(e, "Error looking up Vendor for the current Product Store", module);
}
}
if (UtilValidate.isNotEmpty(context.get("webSiteId"))) {
try {
GenericValue webSiteRole = EntityQuery.use(delegator).from("WebSiteRole").where("roleTypeId", "VENDOR", "webSiteId", context.get("webSiteId")).orderBy("-fromDate").filterByDate().queryFirst();
if (webSiteRole != null) {
toBeStored.add(delegator.makeValue("OrderRole", UtilMisc.toMap("orderId", orderId, "partyId", webSiteRole.get("partyId"), "roleTypeId", "VENDOR")));
}
} catch (GenericEntityException e) {
Debug.logError(e, "Error looking up Vendor for the current Web Site", module);
}
}
// set the order payment info
List<GenericValue> orderPaymentInfos = UtilGenerics.checkList(context.get("orderPaymentInfo"));
if (UtilValidate.isNotEmpty(orderPaymentInfos)) {
for (GenericValue valueObj : orderPaymentInfos) {
valueObj.set("orderId", orderId);
if ("OrderPaymentPreference".equals(valueObj.getEntityName())) {
if (valueObj.get("orderPaymentPreferenceId") == null) {
valueObj.set("orderPaymentPreferenceId", delegator.getNextSeqId("OrderPaymentPreference"));
valueObj.set("createdDate", UtilDateTime.nowTimestamp());
valueObj.set("createdByUserLogin", userLogin.getString("userLoginId"));
}
if (valueObj.get("statusId") == null) {
valueObj.set("statusId", "PAYMENT_NOT_RECEIVED");
}
}
toBeStored.add(valueObj);
}
}
// store the trackingCodeOrder entities
List<GenericValue> trackingCodeOrders = UtilGenerics.checkList(context.get("trackingCodeOrders"));
if (UtilValidate.isNotEmpty(trackingCodeOrders)) {
for (GenericValue trackingCodeOrder : trackingCodeOrders) {
trackingCodeOrder.set("orderId", orderId);
toBeStored.add(trackingCodeOrder);
}
}
// store the OrderTerm entities
List<GenericValue> orderTerms = UtilGenerics.checkList(context.get("orderTerms"));
if (UtilValidate.isNotEmpty(orderTerms)) {
for (GenericValue orderTerm : orderTerms) {
orderTerm.set("orderId", orderId);
if (orderTerm.get("orderItemSeqId") == null) {
orderTerm.set("orderItemSeqId", "_NA_");
}
toBeStored.add(orderTerm);
}
}
// if a workEffortId is passed, then prepare a OrderHeaderWorkEffort value
String workEffortId = (String) context.get("workEffortId");
if (UtilValidate.isNotEmpty(workEffortId)) {
GenericValue orderHeaderWorkEffort = delegator.makeValue("OrderHeaderWorkEffort");
orderHeaderWorkEffort.set("orderId", orderId);
orderHeaderWorkEffort.set("workEffortId", workEffortId);
toBeStored.add(orderHeaderWorkEffort);
}
try {
// store line items, etc so that they will be there for the foreign key checks
delegator.storeAll(toBeStored);
List<String> resErrorMessages = new LinkedList<>();
// add a product service to inventory
if (UtilValidate.isNotEmpty(orderItems)) {
for (GenericValue orderItem : orderItems) {
String productId = (String) orderItem.get("productId");
GenericValue product = delegator.getRelatedOne("Product", orderItem, false);
if (product != null && ("SERVICE_PRODUCT".equals(product.get("productTypeId")) || "AGGREGATEDSERV_CONF".equals(product.get("productTypeId")))) {
String inventoryFacilityId = null;
if ("Y".equals(productStore.getString("oneInventoryFacility"))) {
inventoryFacilityId = productStore.getString("inventoryFacilityId");
if (UtilValidate.isEmpty(inventoryFacilityId)) {
Debug.logWarning("ProductStore with id " + productStoreId + " has Y for oneInventoryFacility but inventoryFacilityId is empty, returning false for inventory check", module);
}
} else {
List<GenericValue> productFacilities = null;
GenericValue productFacility = null;
try {
productFacilities = product.getRelated("ProductFacility", null, null, true);
} catch (GenericEntityException e) {
Debug.logWarning(e, "Error invoking getRelated in isCatalogInventoryAvailable", module);
}
if (UtilValidate.isNotEmpty(productFacilities)) {
productFacility = EntityUtil.getFirst(productFacilities);
inventoryFacilityId = (String) productFacility.get("facilityId");
}
}
Map<String, Object> ripCtx = new HashMap<>();
if (UtilValidate.isNotEmpty(inventoryFacilityId) && UtilValidate.isNotEmpty(productId) && orderItem.getBigDecimal("quantity").compareTo(BigDecimal.ZERO) > 0) {
// do something tricky here: run as the "system" user
GenericValue permUserLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", "system").cache().queryOne();
ripCtx.put("productId", productId);
ripCtx.put("facilityId", inventoryFacilityId);
ripCtx.put("inventoryItemTypeId", "SERIALIZED_INV_ITEM");
ripCtx.put("statusId", "INV_AVAILABLE");
ripCtx.put("quantityAccepted", orderItem.getBigDecimal("quantity"));
ripCtx.put("quantityRejected", 0.0);
ripCtx.put("userLogin", permUserLogin);
try {
Map<String, Object> ripResult = dispatcher.runSync("receiveInventoryProduct", ripCtx);
if (ServiceUtil.isError(ripResult)) {
String errMsg = ServiceUtil.getErrorMessage(ripResult);
@SuppressWarnings("unchecked") Collection<? extends String> map = (Collection<? extends String>) UtilMisc.<String, String>toMap("reasonCode", "ReceiveInventoryServiceError", "description", errMsg);
resErrorMessages.addAll(map);
}
} catch (GenericServiceException e) {
Debug.logWarning(e, "Error invoking receiveInventoryProduct service in createOrder", module);
}
}
}
}
}
// START inventory reservation
try {
reserveInventory(delegator, dispatcher, userLogin, locale, orderItemShipGroupInfo, dropShipGroupIds, itemValuesBySeqId, orderTypeId, productStoreId, resErrorMessages);
} catch (GeneralException e) {
return ServiceUtil.returnError(e.getMessage());
}
if (resErrorMessages.size() > 0) {
return ServiceUtil.returnError(resErrorMessages);
}
// END inventory reservation
successResult.put("orderId", orderId);
} catch (GenericEntityException e) {
Debug.logError(e, "Problem with order storage or reservations", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorCouldNotCreateOrderWriteError", locale) + e.getMessage() + ").");
}
return successResult;
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class OrderServices method checkItemStatus.
/**
* Service for checking to see if an order is fully completed or canceled
*/
public static Map<String, Object> checkItemStatus(DispatchContext ctx, Map<String, ? extends Object> context) {
Delegator delegator = ctx.getDelegator();
LocalDispatcher dispatcher = ctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String orderId = (String) context.get("orderId");
// check and make sure we have permission to change the order
Security security = ctx.getSecurity();
boolean hasPermission = OrderServices.hasPermission(orderId, userLogin, "UPDATE", security, delegator);
if (!hasPermission) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderYouDoNotHavePermissionToChangeThisOrdersStatus", locale));
}
// get the order header
GenericValue orderHeader = null;
try {
orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get OrderHeader record", module);
}
if (orderHeader == null) {
Debug.logError("OrderHeader came back as null", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderCannotUpdateNullOrderHeader", UtilMisc.toMap("orderId", orderId), locale));
}
// get the order items
List<GenericValue> orderItems = null;
try {
orderItems = EntityQuery.use(delegator).from("OrderItem").where("orderId", orderId).queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get OrderItem records", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderProblemGettingOrderItemRecords", locale));
}
String orderHeaderStatusId = orderHeader.getString("statusId");
String orderTypeId = orderHeader.getString("orderTypeId");
boolean allCanceled = true;
boolean allComplete = true;
boolean allApproved = true;
if (orderItems != null) {
for (GenericValue item : orderItems) {
String statusId = item.getString("statusId");
if (!"ITEM_CANCELLED".equals(statusId)) {
allCanceled = false;
if (!"ITEM_COMPLETED".equals(statusId)) {
allComplete = false;
if (!"ITEM_APPROVED".equals(statusId)) {
allApproved = false;
break;
}
}
}
}
// find the next status to set to (if any)
String newStatus = null;
if (allCanceled) {
newStatus = "ORDER_CANCELLED";
} else if (allComplete) {
newStatus = "ORDER_COMPLETED";
} else if (allApproved) {
boolean changeToApprove = true;
// this is a bit of a pain: if the current statusId = ProductStore.headerApprovedStatus and we don't have that status in the history then we don't want to change it on approving the items
if (UtilValidate.isNotEmpty(orderHeader.getString("productStoreId"))) {
try {
GenericValue productStore = EntityQuery.use(delegator).from("ProductStore").where("productStoreId", orderHeader.getString("productStoreId")).queryOne();
if (productStore != null) {
String headerApprovedStatus = productStore.getString("headerApprovedStatus");
if (UtilValidate.isNotEmpty(headerApprovedStatus)) {
if (headerApprovedStatus.equals(orderHeaderStatusId)) {
List<GenericValue> orderStatusList = EntityQuery.use(delegator).from("OrderStatus").where("orderId", orderId, "statusId", headerApprovedStatus, "orderItemSeqId", null).queryList();
// should be 1 in the history, but just in case accept 0 too
if (orderStatusList.size() <= 1) {
changeToApprove = false;
}
}
}
}
} catch (GenericEntityException e) {
String errMsg = UtilProperties.getMessage(resource_error, "OrderDatabaseErrorCheckingIfWeShouldChangeOrderHeaderStatusToApproved", UtilMisc.toMap("errorString", e.toString()), locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
}
if ("ORDER_SENT".equals(orderHeaderStatusId)) {
changeToApprove = false;
}
if ("ORDER_COMPLETED".equals(orderHeaderStatusId)) {
if ("SALES_ORDER".equals(orderTypeId)) {
changeToApprove = false;
}
}
if ("ORDER_CANCELLED".equals(orderHeaderStatusId)) {
changeToApprove = false;
}
if (changeToApprove) {
newStatus = "ORDER_APPROVED";
if ("ORDER_HOLD".equals(orderHeaderStatusId)) {
// Don't let the system to auto approve order if the order was put on hold.
return ServiceUtil.returnSuccess();
}
}
}
// now set the new order status
if (newStatus != null && !newStatus.equals(orderHeaderStatusId)) {
Map<String, Object> serviceContext = UtilMisc.<String, Object>toMap("orderId", orderId, "statusId", newStatus, "userLogin", userLogin);
Map<String, Object> newSttsResult = null;
try {
newSttsResult = dispatcher.runSync("changeOrderStatus", serviceContext);
} catch (GenericServiceException e) {
Debug.logError(e, "Problem calling the changeOrderStatus service", module);
}
if (ServiceUtil.isError(newSttsResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(newSttsResult));
}
}
} else {
Debug.logWarning(UtilProperties.getMessage(resource_error, "OrderReceivedNullForOrderItemRecordsOrderId", UtilMisc.toMap("orderId", orderId), locale), module);
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class ZipSalesServices method importFlatTable.
// import table service
public static Map<String, Object> importFlatTable(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
Security security = dctx.getSecurity();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String taxFileLocation = (String) context.get("taxFileLocation");
String ruleFileLocation = (String) context.get("ruleFileLocation");
Locale locale = (Locale) context.get("locale");
// do security check
if (!security.hasPermission("SERVICE_INVOKE_ANY", userLogin)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderYouDoNotHavePermissionToLoadTaxTables", locale));
}
// get a now stamp (we'll use 2000-01-01)
Timestamp now = parseDate("20000101", null);
// load the data file
DataFile tdf = null;
try {
tdf = DataFile.makeDataFile(UtilURL.fromResource(dataFile), flatTable);
} catch (DataFileException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToReadZipSalesDataFile", locale));
}
// locate the file to be imported
URL tUrl = UtilURL.fromResource(taxFileLocation);
if (tUrl == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToLocateTaxFileAtLocation", UtilMisc.toMap("taxFileLocation", taxFileLocation), locale));
}
RecordIterator tri = null;
try {
tri = tdf.makeRecordIterator(tUrl);
} catch (DataFileException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderProblemGettingTheRecordIterator", locale));
}
if (tri != null) {
while (tri.hasNext()) {
Record entry = null;
try {
entry = tri.next();
} catch (DataFileException e) {
Debug.logError(e, module);
}
GenericValue newValue = delegator.makeValue("ZipSalesTaxLookup");
// PK fields
newValue.set("zipCode", entry.getString("zipCode").trim());
newValue.set("stateCode", entry.get("stateCode") != null ? entry.getString("stateCode").trim() : "_NA_");
newValue.set("city", entry.get("city") != null ? entry.getString("city").trim() : "_NA_");
newValue.set("county", entry.get("county") != null ? entry.getString("county").trim() : "_NA_");
newValue.set("fromDate", parseDate(entry.getString("effectiveDate"), now));
// non-PK fields
newValue.set("countyFips", entry.get("countyFips"));
newValue.set("countyDefault", entry.get("countyDefault"));
newValue.set("generalDefault", entry.get("generalDefault"));
newValue.set("insideCity", entry.get("insideCity"));
newValue.set("geoCode", entry.get("geoCode"));
newValue.set("stateSalesTax", entry.get("stateSalesTax"));
newValue.set("citySalesTax", entry.get("citySalesTax"));
newValue.set("cityLocalSalesTax", entry.get("cityLocalSalesTax"));
newValue.set("countySalesTax", entry.get("countySalesTax"));
newValue.set("countyLocalSalesTax", entry.get("countyLocalSalesTax"));
newValue.set("comboSalesTax", entry.get("comboSalesTax"));
newValue.set("stateUseTax", entry.get("stateUseTax"));
newValue.set("cityUseTax", entry.get("cityUseTax"));
newValue.set("cityLocalUseTax", entry.get("cityLocalUseTax"));
newValue.set("countyUseTax", entry.get("countyUseTax"));
newValue.set("countyLocalUseTax", entry.get("countyLocalUseTax"));
newValue.set("comboUseTax", entry.get("comboUseTax"));
try {
delegator.createOrStore(newValue);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorWritingRecordsToTheDatabase", locale));
}
// console log
Debug.logInfo(newValue.get("zipCode") + "/" + newValue.get("stateCode") + "/" + newValue.get("city") + "/" + newValue.get("county") + "/" + newValue.get("fromDate"), module);
}
}
// load the data file
DataFile rdf = null;
try {
rdf = DataFile.makeDataFile(UtilURL.fromResource(dataFile), ruleTable);
} catch (DataFileException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToReadZipSalesDataFile", locale));
}
// locate the file to be imported
URL rUrl = UtilURL.fromResource(ruleFileLocation);
if (rUrl == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToLocateRuleFileFromLocation", UtilMisc.toMap("ruleFileLocation", ruleFileLocation), locale));
}
RecordIterator rri = null;
try {
rri = rdf.makeRecordIterator(rUrl);
} catch (DataFileException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderProblemGettingTheRecordIterator", locale));
}
if (rri != null) {
while (rri.hasNext()) {
Record entry = null;
try {
entry = rri.next();
} catch (DataFileException e) {
Debug.logError(e, module);
}
if (UtilValidate.isNotEmpty(entry.getString("stateCode"))) {
GenericValue newValue = delegator.makeValue("ZipSalesRuleLookup");
// PK fields
newValue.set("stateCode", entry.get("stateCode") != null ? entry.getString("stateCode").trim() : "_NA_");
newValue.set("city", entry.get("city") != null ? entry.getString("city").trim() : "_NA_");
newValue.set("county", entry.get("county") != null ? entry.getString("county").trim() : "_NA_");
newValue.set("fromDate", parseDate(entry.getString("effectiveDate"), now));
// non-PK fields
newValue.set("idCode", entry.get("idCode") != null ? entry.getString("idCode").trim() : null);
newValue.set("taxable", entry.get("taxable") != null ? entry.getString("taxable").trim() : null);
newValue.set("shipCond", entry.get("shipCond") != null ? entry.getString("shipCond").trim() : null);
try {
// using storeAll as an easy way to create/update
delegator.storeAll(UtilMisc.toList(newValue));
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorWritingRecordsToTheDatabase", locale));
}
// console log
Debug.logInfo(newValue.get("stateCode") + "/" + newValue.get("city") + "/" + newValue.get("county") + "/" + newValue.get("fromDate"), module);
}
}
}
return ServiceUtil.returnSuccess();
}
Aggregations