Search in sources :

Example 11 with ShoppingCart

use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.

the class ShippingEvents method getShipEstimate.

public static String getShipEstimate(HttpServletRequest request, HttpServletResponse response) {
    ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    int shipGroups = cart.getShipGroupSize();
    for (int i = 0; i < shipGroups; i++) {
        String shipmentMethodTypeId = cart.getShipmentMethodTypeId(i);
        if (UtilValidate.isEmpty(shipmentMethodTypeId)) {
            continue;
        }
        Map<String, Object> result = getShipGroupEstimate(dispatcher, delegator, cart, i);
        ServiceUtil.getMessages(request, result, null, "", "", "", "", null, null);
        if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) {
            return "error";
        }
        BigDecimal shippingTotal = (BigDecimal) result.get("shippingTotal");
        if (shippingTotal == null) {
            shippingTotal = BigDecimal.ZERO;
        }
        cart.setItemShipGroupEstimate(shippingTotal, i);
    }
    ProductPromoWorker.doPromotions(cart, dispatcher);
    // all done
    return "success";
}
Also used : LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) Delegator(org.apache.ofbiz.entity.Delegator) BigDecimal(java.math.BigDecimal)

Example 12 with ShoppingCart

use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.

the class ShoppingListEvents method saveCartToAutoSaveList.

/**
 * Saves the shopping cart to the specialized (auto-save) shopping list
 */
public static String saveCartToAutoSaveList(HttpServletRequest request, HttpServletResponse response) {
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
    try {
        fillAutoSaveList(cart, dispatcher);
    } catch (GeneralException e) {
        Debug.logError(e, "Error saving the cart to the auto-save list: " + e.toString(), module);
    }
    return "success";
}
Also used : LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart)

Example 13 with ShoppingCart

use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.

the class ShoppingListEvents method restoreAutoSaveList.

/**
 * Restores the specialized (auto-save) shopping list back into the shopping cart
 */
public static String restoreAutoSaveList(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    GenericValue productStore = ProductStoreWorker.getProductStore(request);
    if (!ProductStoreWorker.autoSaveCart(productStore)) {
        // if auto-save is disabled just return here
        return "success";
    }
    HttpSession session = request.getSession();
    ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
    // safety check for missing required parameter.
    if (cart.getWebSiteId() == null) {
        cart.setWebSiteId(WebSiteWorker.getWebSiteId(request));
    }
    // locate the user's identity
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    if (userLogin == null) {
        userLogin = (GenericValue) session.getAttribute("autoUserLogin");
    }
    // find the list ID
    String autoSaveListId = cart.getAutoSaveListId();
    if (autoSaveListId == null) {
        try {
            autoSaveListId = getAutoSaveListId(delegator, dispatcher, null, userLogin, cart.getProductStoreId());
        } catch (GeneralException e) {
            Debug.logError(e, module);
        }
        cart.setAutoSaveListId(autoSaveListId);
    } else if (userLogin != null) {
        String existingAutoSaveListId = null;
        try {
            existingAutoSaveListId = getAutoSaveListId(delegator, dispatcher, null, userLogin, cart.getProductStoreId());
        } catch (GeneralException e) {
            Debug.logError(e, module);
        }
        if (existingAutoSaveListId != null) {
            if (!existingAutoSaveListId.equals(autoSaveListId)) {
                // Replace with existing shopping list
                cart.setAutoSaveListId(existingAutoSaveListId);
                autoSaveListId = existingAutoSaveListId;
                cart.setLastListRestore(null);
            } else {
                // because at this point items in the cart and the items in the shopping list are same so just return.
                return "success";
            }
        }
    }
    // check to see if we are okay to load this list
    java.sql.Timestamp lastLoad = cart.getLastListRestore();
    boolean okayToLoad = autoSaveListId == null ? false : (lastLoad == null ? true : false);
    if (!okayToLoad && lastLoad != null) {
        GenericValue shoppingList = null;
        try {
            shoppingList = EntityQuery.use(delegator).from("ShoppingList").where("shoppingListId", autoSaveListId).queryOne();
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
        if (shoppingList != null) {
            java.sql.Timestamp lastModified = shoppingList.getTimestamp("lastAdminModified");
            if (lastModified != null) {
                if (lastModified.after(lastLoad)) {
                    okayToLoad = true;
                }
                if (cart.size() == 0 && lastModified.after(cart.getCartCreatedTime())) {
                    okayToLoad = true;
                }
            }
        }
    }
    // load (restore) the list of we have determined it is okay to load
    if (okayToLoad) {
        String prodCatalogId = CatalogWorker.getCurrentCatalogId(request);
        try {
            addListToCart(delegator, dispatcher, cart, prodCatalogId, autoSaveListId, false, false, userLogin != null ? true : false);
            cart.setLastListRestore(UtilDateTime.nowTimestamp());
        } catch (IllegalArgumentException e) {
            Debug.logError(e, module);
        }
    }
    return "success";
}
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) HttpSession(javax.servlet.http.HttpSession) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Timestamp(java.sql.Timestamp)

Example 14 with ShoppingCart

use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.

the class OrderTestServices method createTestSalesOrderSingle.

public static Map<String, Object> createTestSalesOrderSingle(DispatchContext dctx, Map<String, ? extends Object> context) {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Delegator delegator = dctx.getDelegator();
    Locale locale = (Locale) context.get("locale");
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    String productCategoryId = (String) context.get("productCategoryId");
    String productStoreId = (String) context.get("productStoreId");
    String currencyUomId = (String) context.get("currencyUomId");
    String partyId = (String) context.get("partyId");
    String productId = (String) context.get("productId");
    Integer numberOfProductsPerOrder = (Integer) context.get("numberOfProductsPerOrder");
    String salesChannel = (String) context.get("salesChannel");
    if (UtilValidate.isEmpty(salesChannel)) {
        salesChannel = "WEB_SALES_CHANNEL";
    }
    List<String> productsList = new LinkedList<String>();
    try {
        if (UtilValidate.isNotEmpty(productId)) {
            productsList.add(productId);
            numberOfProductsPerOrder = 1;
        } else {
            Map<String, Object> result = dispatcher.runSync("getProductCategoryMembers", UtilMisc.toMap("categoryId", productCategoryId));
            if (ServiceUtil.isError(result)) {
                return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
            }
            if (result.get("categoryMembers") != null) {
                List<GenericValue> productCategoryMembers = UtilGenerics.checkList(result.get("categoryMembers"));
                if (productCategoryMembers != null) {
                    for (GenericValue prodCatMemb : productCategoryMembers) {
                        if (prodCatMemb != null) {
                            productsList.add(prodCatMemb.getString("productId"));
                        }
                    }
                }
            }
        }
    } catch (GenericServiceException gse) {
        return ServiceUtil.returnError(gse.getMessage());
    } catch (Exception e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    if (productsList.size() == 0) {
        return ServiceUtil.returnError(UtilProperties.getMessage("OrderUiLabels", "OrderCreateTestSalesOrderSingleError", UtilMisc.toMap("productCategoryId", productCategoryId), locale));
    }
    Random r = new Random();
    ShoppingCart cart = new ShoppingCart(delegator, productStoreId, locale, currencyUomId);
    cart.setOrderType("SALES_ORDER");
    cart.setChannelType(salesChannel);
    cart.setProductStoreId(productStoreId);
    cart.setBillToCustomerPartyId(partyId);
    cart.setPlacingCustomerPartyId(partyId);
    cart.setShipToCustomerPartyId(partyId);
    cart.setEndUserCustomerPartyId(partyId);
    try {
        cart.setUserLogin(userLogin, dispatcher);
    } catch (Exception exc) {
        Debug.logWarning("Error setting userLogin in the cart: " + exc.getMessage(), module);
    }
    int numberOfProductsPerOrderInt = numberOfProductsPerOrder.intValue();
    for (int j = 1; j <= numberOfProductsPerOrderInt; j++) {
        // get a product
        int k = r.nextInt(productsList.size());
        try {
            cart.addOrIncreaseItem(productsList.get(k), null, BigDecimal.ONE, null, null, null, null, null, null, null, null, /*catalogId*/
            null, null, /*itemType*/
            null, /*itemGroupNumber*/
            null, dispatcher);
        } catch (CartItemModifyException | ItemNotFoundException exc) {
            Debug.logWarning("Error adding product with id " + productsList.get(k) + " to the cart: " + exc.getMessage(), module);
        }
    }
    cart.setDefaultCheckoutOptions(dispatcher);
    CheckOutHelper checkout = new CheckOutHelper(dispatcher, delegator, cart);
    Map<String, Object> orderCreateResult = checkout.createOrder(userLogin);
    String orderId = (String) orderCreateResult.get("orderId");
    Map<String, Object> resultMap = ServiceUtil.returnSuccess();
    // approve the order
    if (UtilValidate.isNotEmpty(orderId)) {
        Debug.logInfo("Created test order with id: " + orderId, module);
        boolean approved = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId);
        Debug.logInfo("Test order with id: " + orderId + " has been approved: " + approved, module);
        resultMap.put("orderId", orderId);
    }
    Boolean shipOrder = (Boolean) context.get("shipOrder");
    if (shipOrder.booleanValue() && UtilValidate.isNotEmpty(orderId)) {
        try {
            Map<String, Object> result = dispatcher.runSync("quickShipEntireOrder", UtilMisc.toMap("orderId", orderId, "userLogin", userLogin));
            if (ServiceUtil.isError(result)) {
                return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
            }
            Debug.logInfo("Test sales order with id [" + orderId + "] has been shipped", module);
        } catch (GenericServiceException gse) {
            Debug.logWarning("Unable to quick ship test sales order with id [" + orderId + "] with error: " + gse.getMessage(), module);
        } catch (Exception exc) {
            Debug.logWarning("Unable to quick ship test sales order with id [" + orderId + "] with error: " + exc.getMessage(), module);
        }
    }
    return resultMap;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) CheckOutHelper(org.apache.ofbiz.order.shoppingcart.CheckOutHelper) LinkedList(java.util.LinkedList) CartItemModifyException(org.apache.ofbiz.order.shoppingcart.CartItemModifyException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ItemNotFoundException(org.apache.ofbiz.order.shoppingcart.ItemNotFoundException) Delegator(org.apache.ofbiz.entity.Delegator) Random(java.util.Random) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) CartItemModifyException(org.apache.ofbiz.order.shoppingcart.CartItemModifyException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ItemNotFoundException(org.apache.ofbiz.order.shoppingcart.ItemNotFoundException)

Example 15 with ShoppingCart

use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.

the class ExpressCheckoutEvents method expressCheckoutCancel.

public static String expressCheckoutCancel(HttpServletRequest request, HttpServletResponse response) {
    ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
    cart.removeAttribute("payPalCheckoutToken");
    return "success";
}
Also used : ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart)

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