Search in sources :

Example 21 with ShoppingCart

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);
        }
    }
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) CheckOutHelper(org.apache.ofbiz.order.shoppingcart.CheckOutHelper) Delegator(org.apache.ofbiz.entity.Delegator) Transaction(javax.transaction.Transaction) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException)

Example 22 with ShoppingCart

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;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) Delegator(org.apache.ofbiz.entity.Delegator) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart)

Example 23 with ShoppingCart

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;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Delegator(org.apache.ofbiz.entity.Delegator) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) CheckOutHelper(org.apache.ofbiz.order.shoppingcart.CheckOutHelper)

Example 24 with ShoppingCart

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;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) BigDecimal(java.math.BigDecimal) Delegator(org.apache.ofbiz.entity.Delegator) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) CartItemModifyException(org.apache.ofbiz.order.shoppingcart.CartItemModifyException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 25 with ShoppingCart

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;
}
Also used : Locale(java.util.Locale) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) Timestamp(java.sql.Timestamp) HashSet(java.util.HashSet) GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) BigDecimal(java.math.BigDecimal) Delegator(org.apache.ofbiz.entity.Delegator) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) CartItemModifyException(org.apache.ofbiz.order.shoppingcart.CartItemModifyException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ShoppingCartItem(org.apache.ofbiz.order.shoppingcart.ShoppingCartItem) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ShoppingCart (org.apache.ofbiz.order.shoppingcart.ShoppingCart)37 Delegator (org.apache.ofbiz.entity.Delegator)28 GenericValue (org.apache.ofbiz.entity.GenericValue)28 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)23 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)18 Locale (java.util.Locale)17 GeneralException (org.apache.ofbiz.base.util.GeneralException)14 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)13 BigDecimal (java.math.BigDecimal)9 CartItemModifyException (org.apache.ofbiz.order.shoppingcart.CartItemModifyException)9 HashMap (java.util.HashMap)8 CheckOutHelper (org.apache.ofbiz.order.shoppingcart.CheckOutHelper)8 ShoppingCartItem (org.apache.ofbiz.order.shoppingcart.ShoppingCartItem)7 Timestamp (java.sql.Timestamp)6 LinkedList (java.util.LinkedList)5 ItemNotFoundException (org.apache.ofbiz.order.shoppingcart.ItemNotFoundException)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)4 NVPEncoder (com.paypal.sdk.core.nvp.NVPEncoder)3 PayPalException (com.paypal.sdk.exceptions.PayPalException)3