use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.
the class OrderServices method callProcessOrderPayments.
// generic method for processing an order's payment(s)
public static Map<String, Object> callProcessOrderPayments(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
Transaction trans = null;
try {
// disable transaction processing
trans = TransactionUtil.suspend();
// get the cart
ShoppingCart cart = (ShoppingCart) context.get("shoppingCart");
GenericValue userLogin = cart.getUserLogin();
Boolean manualHold = (Boolean) context.get("manualHold");
if (manualHold == null) {
manualHold = Boolean.FALSE;
}
if (!"PURCHASE_ORDER".equals(cart.getOrderType())) {
String productStoreId = cart.getProductStoreId();
GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator);
CheckOutHelper coh = new CheckOutHelper(dispatcher, delegator, cart);
// process payment
Map<String, Object> payResp;
try {
payResp = coh.processPayment(productStore, userLogin, false, manualHold.booleanValue());
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(payResp)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderProcessOrderPayments", locale), null, null, payResp);
}
}
return ServiceUtil.returnSuccess();
} catch (GenericTransactionException e) {
return ServiceUtil.returnError(e.getMessage());
} finally {
// resume transaction
try {
TransactionUtil.resume(trans);
} catch (GenericTransactionException e) {
Debug.logWarning(e, e.getMessage(), module);
}
}
}
use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.
the class OrderServices method loadCartForUpdate.
public static Map<String, Object> loadCartForUpdate(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
String orderId = (String) context.get("orderId");
GenericValue userLogin = (GenericValue) context.get("userLogin");
ShoppingCart cart = null;
Map<String, Object> result = null;
try {
cart = loadCartForUpdate(dispatcher, delegator, userLogin, orderId);
result = ServiceUtil.returnSuccess();
result.put("shoppingCart", cart);
} catch (GeneralException e) {
Debug.logError(e, module);
result = ServiceUtil.returnError(e.getMessage());
}
result.put("orderId", orderId);
return result;
}
use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.
the class OrderServices method createOrderFromShoppingCart.
// generic method for creating an order from a shopping cart
public static Map<String, Object> createOrderFromShoppingCart(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
ShoppingCart cart = (ShoppingCart) context.get("shoppingCart");
GenericValue userLogin = cart.getUserLogin();
CheckOutHelper coh = new CheckOutHelper(dispatcher, delegator, cart);
Map<String, Object> createOrder = coh.createOrder(userLogin);
if (ServiceUtil.isError(createOrder)) {
return createOrder;
}
String orderId = (String) createOrder.get("orderId");
Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("shoppingCart", cart);
result.put("orderId", orderId);
return result;
}
use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.
the class OrderServices method createSimpleNonProductSalesOrder.
// create simple non-product order
public static Map<String, Object> createSimpleNonProductSalesOrder(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
String paymentMethodId = (String) context.get("paymentMethodId");
String productStoreId = (String) context.get("productStoreId");
String currency = (String) context.get("currency");
String partyId = (String) context.get("partyId");
Map<String, BigDecimal> itemMap = UtilGenerics.checkMap(context.get("itemMap"));
ShoppingCart cart = new ShoppingCart(delegator, productStoreId, null, locale, currency);
try {
cart.setUserLogin(userLogin, dispatcher);
} catch (CartItemModifyException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
cart.setOrderType("SALES_ORDER");
cart.setOrderPartyId(partyId);
for (String item : itemMap.keySet()) {
BigDecimal price = itemMap.get(item);
try {
cart.addNonProductItem("BULK_ORDER_ITEM", item, null, price, BigDecimal.ONE, null, null, null, dispatcher);
} catch (CartItemModifyException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
// set the payment method
try {
cart.addPayment(paymentMethodId);
} catch (IllegalArgumentException e) {
return ServiceUtil.returnError(e.getMessage());
}
// save the order (new tx)
Map<String, Object> createResp;
try {
createResp = dispatcher.runSync("createOrderFromShoppingCart", UtilMisc.toMap("shoppingCart", cart), 90, true);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(createResp)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(createResp));
}
// auth the order (new tx)
Map<String, Object> authResp;
try {
authResp = dispatcher.runSync("callProcessOrderPayments", UtilMisc.toMap("shoppingCart", cart), 180, true);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(authResp)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(authResp));
}
Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("orderId", createResp.get("orderId"));
return result;
}
use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.
the class OrderServices method updateApprovedOrderItems.
public static Map<String, Object> updateApprovedOrderItems(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
String orderId = (String) context.get("orderId");
Map<String, String> overridePriceMap = UtilGenerics.checkMap(context.get("overridePriceMap"));
Map<String, String> itemDescriptionMap = UtilGenerics.checkMap(context.get("itemDescriptionMap"));
Map<String, String> itemPriceMap = UtilGenerics.checkMap(context.get("itemPriceMap"));
Map<String, String> itemQtyMap = UtilGenerics.checkMap(context.get("itemQtyMap"));
Map<String, String> itemReasonMap = UtilGenerics.checkMap(context.get("itemReasonMap"));
Map<String, String> itemCommentMap = UtilGenerics.checkMap(context.get("itemCommentMap"));
Map<String, String> itemAttributesMap = UtilGenerics.checkMap(context.get("itemAttributesMap"));
Map<String, String> itemEstimatedShipDateMap = UtilGenerics.checkMap(context.get("itemShipDateMap"));
Map<String, String> itemEstimatedDeliveryDateMap = UtilGenerics.checkMap(context.get("itemDeliveryDateMap"));
Boolean calcTax = (Boolean) context.get("calcTax");
if (calcTax == null) {
calcTax = Boolean.TRUE;
}
// obtain a shopping cart object for updating
ShoppingCart cart = null;
try {
cart = loadCartForUpdate(dispatcher, delegator, userLogin, orderId);
} catch (GeneralException e) {
return ServiceUtil.returnError(e.getMessage());
}
// go through the item map and obtain the totals per item
Map<String, BigDecimal> itemTotals = new HashMap<>();
for (String key : itemQtyMap.keySet()) {
String quantityStr = itemQtyMap.get(key);
BigDecimal groupQty = BigDecimal.ZERO;
try {
groupQty = (BigDecimal) ObjectType.simpleTypeConvert(quantityStr, "BigDecimal", null, locale);
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (groupQty.compareTo(BigDecimal.ZERO) < 0) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderItemQtyMustBePositive", locale));
}
String[] itemInfo = key.split(":");
BigDecimal tally = itemTotals.get(itemInfo[0]);
if (tally == null) {
tally = groupQty;
} else {
tally = tally.add(groupQty);
}
itemTotals.put(itemInfo[0], tally);
}
// set the items amount/price
for (String itemSeqId : itemTotals.keySet()) {
ShoppingCartItem cartItem = cart.findCartItem(itemSeqId);
if (cartItem != null) {
BigDecimal qty = itemTotals.get(itemSeqId);
BigDecimal priceSave = cartItem.getBasePrice();
try {
// if not and if quantity is in decimal format then return error.
if (!ProductWorker.isDecimalQuantityOrderAllowed(delegator, cartItem.getProductId(), cart.getProductStoreId())) {
BigDecimal remainder = qty.remainder(BigDecimal.ONE);
if (remainder.compareTo(BigDecimal.ZERO) != 0) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "cart.addToCart.quantityInDecimalNotAllowed", locale));
}
qty = qty.setScale(0, UtilNumber.getRoundingMode("order.rounding"));
} else {
qty = qty.setScale(UtilNumber.getBigDecimalScale("order.decimals"), UtilNumber.getRoundingMode("order.rounding"));
}
} catch (GenericEntityException e) {
Debug.logError(e.getMessage(), module);
qty = BigDecimal.ONE;
}
// set quantity
try {
// trigger external ops, don't reset ship groups (and update prices for both PO and SO items)
cartItem.setQuantity(qty, dispatcher, cart, false, false);
} catch (CartItemModifyException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
Debug.logInfo("Set item quantity: [" + itemSeqId + "] " + qty, module);
if (cartItem.getIsModifiedPrice()) {
cartItem.setBasePrice(priceSave);
}
if (overridePriceMap.containsKey(itemSeqId)) {
String priceStr = itemPriceMap.get(itemSeqId);
if (UtilValidate.isNotEmpty(priceStr)) {
BigDecimal price = null;
try {
price = (BigDecimal) ObjectType.simpleTypeConvert(priceStr, "BigDecimal", null, locale);
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
cartItem.setBasePrice(price);
cartItem.setIsModifiedPrice(true);
Debug.logInfo("Set item price: [" + itemSeqId + "] " + price, module);
}
}
// Update the item description
if (itemDescriptionMap != null && itemDescriptionMap.containsKey(itemSeqId)) {
String description = itemDescriptionMap.get(itemSeqId);
if (UtilValidate.isNotEmpty(description)) {
cartItem.setName(description);
Debug.logInfo("Set item description: [" + itemSeqId + "] " + description, module);
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderItemDescriptionCannotBeEmpty", locale));
}
}
// Update the item comment
if (itemCommentMap != null && itemCommentMap.containsKey(itemSeqId)) {
String comments = itemCommentMap.get(itemSeqId);
if (UtilValidate.isNotEmpty(comments)) {
cartItem.setItemComment(comments);
Debug.logInfo("Set item comment: [" + itemSeqId + "] " + comments, module);
}
}
// update the order item attributes
if (itemAttributesMap != null) {
// go through the item attributes map once to get a list of key names
Set<String> attributeNames = new HashSet<>();
Set<String> keys = itemAttributesMap.keySet();
for (String key : keys) {
String[] attributeInfo = key.split(":");
attributeNames.add(attributeInfo[0]);
}
String attrValue = null;
for (String attrName : attributeNames) {
attrValue = itemAttributesMap.get(attrName + ":" + itemSeqId);
if (UtilValidate.isNotEmpty(attrName)) {
cartItem.setOrderItemAttribute(attrName, attrValue);
Debug.logInfo("Set item attribute Name: [" + itemSeqId + "] " + attrName + " , Value:" + attrValue, module);
}
}
}
} else {
Debug.logInfo("Unable to locate shopping cart item for seqId #" + itemSeqId, module);
}
}
// Create Estimated Delivery dates
if (null != itemEstimatedDeliveryDateMap) {
for (Map.Entry<String, String> entry : itemEstimatedDeliveryDateMap.entrySet()) {
String itemSeqId = entry.getKey();
// ignore internationalised variant of dates
if (!itemSeqId.endsWith("_i18n")) {
String estimatedDeliveryDate = entry.getValue();
if (UtilValidate.isNotEmpty(estimatedDeliveryDate)) {
Timestamp deliveryDate = Timestamp.valueOf(estimatedDeliveryDate);
ShoppingCartItem cartItem = cart.findCartItem(itemSeqId);
cartItem.setDesiredDeliveryDate(deliveryDate);
}
}
}
}
// Create Estimated ship dates
if (null != itemEstimatedShipDateMap) {
for (Map.Entry<String, String> entry : itemEstimatedShipDateMap.entrySet()) {
String itemSeqId = entry.getKey();
// ignore internationalised variant of dates
if (!itemSeqId.endsWith("_i18n")) {
String estimatedShipDate = entry.getValue();
if (UtilValidate.isNotEmpty(estimatedShipDate)) {
Timestamp shipDate = Timestamp.valueOf(estimatedShipDate);
ShoppingCartItem cartItem = cart.findCartItem(itemSeqId);
cartItem.setEstimatedShipDate(shipDate);
}
}
}
}
// update the group amounts
for (String key : itemQtyMap.keySet()) {
String quantityStr = itemQtyMap.get(key);
BigDecimal groupQty = BigDecimal.ZERO;
try {
groupQty = (BigDecimal) ObjectType.simpleTypeConvert(quantityStr, "BigDecimal", null, locale);
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String[] itemInfo = key.split(":");
int groupIdx = -1;
try {
groupIdx = Integer.parseInt(itemInfo[1]);
} catch (NumberFormatException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// set the group qty
ShoppingCartItem cartItem = cart.findCartItem(itemInfo[0]);
if (cartItem != null) {
try {
// if not and if quantity is in decimal format then return error.
if (!ProductWorker.isDecimalQuantityOrderAllowed(delegator, cartItem.getProductId(), cart.getProductStoreId())) {
BigDecimal remainder = groupQty.remainder(BigDecimal.ONE);
if (remainder.compareTo(BigDecimal.ZERO) != 0) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "cart.addToCart.quantityInDecimalNotAllowed", locale));
}
groupQty = groupQty.setScale(0, UtilNumber.getRoundingMode("order.rounding"));
} else {
groupQty = groupQty.setScale(UtilNumber.getBigDecimalScale("order.decimals"), UtilNumber.getRoundingMode("order.rounding"));
}
} catch (GenericEntityException e) {
Debug.logError(e.getMessage(), module);
groupQty = BigDecimal.ONE;
}
int shipGroupIndex = cart.getShipInfoIndex(itemInfo[1]);
if (Debug.infoOn()) {
Debug.logInfo("Shipping info (before) for group #" + (shipGroupIndex) + " [" + cart.getShipmentMethodTypeId(shipGroupIndex) + " / " + cart.getCarrierPartyId(shipGroupIndex) + "]", module);
}
cart.setItemShipGroupQty(cartItem, groupQty, shipGroupIndex);
if (Debug.infoOn()) {
Debug.logInfo("Set ship group qty: [" + itemInfo[0] + " / " + itemInfo[1] + " (" + (shipGroupIndex) + ")] " + groupQty, module);
Debug.logInfo("Shipping info (after) for group #" + (shipGroupIndex) + " [" + cart.getShipmentMethodTypeId(shipGroupIndex) + " / " + cart.getCarrierPartyId(shipGroupIndex) + "]", module);
}
}
}
// save all the updated information
try {
saveUpdatedCartToOrder(dispatcher, delegator, cart, locale, userLogin, orderId, UtilMisc.<String, Object>toMap("itemReasonMap", itemReasonMap, "itemCommentMap", itemCommentMap), calcTax, false);
} catch (GeneralException e) {
return ServiceUtil.returnError(e.getMessage());
}
// run promotions to handle all changes in the cart
ProductPromoWorker.doPromotions(cart, dispatcher);
// log an order note
try {
Map<String, Object> result = dispatcher.runSync("createOrderNote", UtilMisc.<String, Object>toMap("orderId", orderId, "note", "Updated order.", "internalNote", "Y", "userLogin", userLogin));
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("shoppingCart", cart);
result.put("orderId", orderId);
return result;
}
Aggregations