Search in sources :

Example 31 with ShoppingCart

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

the class ShoppingListEvents method createGuestShoppingListCookies.

/**
 * Create the guest cookies for a shopping list
 */
public static String createGuestShoppingListCookies(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    HttpSession session = request.getSession(true);
    ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart");
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    Properties systemProps = System.getProperties();
    String guestShoppingUserName = "GuestShoppingListId_" + systemProps.getProperty("user.name").replace(" ", "_");
    String productStoreId = ProductStoreWorker.getProductStoreId(request);
    int cookieAge = (60 * 60 * 24 * 30);
    String autoSaveListId = null;
    Cookie[] cookies = request.getCookies();
    // check userLogin
    if (userLogin != null) {
        String partyId = userLogin.getString("partyId");
        if (UtilValidate.isEmpty(partyId)) {
            return "success";
        }
    }
    // find shopping list ID
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(guestShoppingUserName)) {
                autoSaveListId = cookie.getValue();
                break;
            }
        }
    }
    // clear the auto-save info
    if (ProductStoreWorker.autoSaveCart(delegator, productStoreId)) {
        if (UtilValidate.isEmpty(autoSaveListId)) {
            try {
                Map<String, Object> listFields = UtilMisc.<String, Object>toMap("userLogin", userLogin, "productStoreId", productStoreId, "shoppingListTypeId", "SLT_SPEC_PURP", "listName", PERSISTANT_LIST_NAME);
                Map<String, Object> newListResult = dispatcher.runSync("createShoppingList", listFields);
                if (ServiceUtil.isError(newListResult)) {
                    String errorMessage = ServiceUtil.getErrorMessage(newListResult);
                    Debug.logError(errorMessage, module);
                    return null;
                }
                if (newListResult != null) {
                    autoSaveListId = (String) newListResult.get("shoppingListId");
                }
            } catch (GeneralException e) {
                Debug.logError(e, module);
            }
            Cookie guestShoppingListCookie = new Cookie(guestShoppingUserName, autoSaveListId);
            guestShoppingListCookie.setMaxAge(cookieAge);
            guestShoppingListCookie.setPath("/");
            guestShoppingListCookie.setSecure(true);
            guestShoppingListCookie.setHttpOnly(true);
            response.addCookie(guestShoppingListCookie);
        }
    }
    if (UtilValidate.isNotEmpty(autoSaveListId)) {
        if (UtilValidate.isNotEmpty(cart)) {
            cart.setAutoSaveListId(autoSaveListId);
        } else {
            cart = ShoppingCartEvents.getCartObject(request);
            cart.setAutoSaveListId(autoSaveListId);
        }
    }
    return "success";
}
Also used : Cookie(javax.servlet.http.Cookie) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) HttpSession(javax.servlet.http.HttpSession) UtilProperties(org.apache.ofbiz.base.util.UtilProperties) Properties(java.util.Properties) Delegator(org.apache.ofbiz.entity.Delegator) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart)

Example 32 with ShoppingCart

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

the class ShoppingListEvents method addBulkFromCart.

public static String addBulkFromCart(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
    GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
    String shoppingListId = request.getParameter("shoppingListId");
    String shoppingListTypeId = request.getParameter("shoppingListTypeId");
    String[] selectedCartItems = request.getParameterValues("selectedItem");
    if (UtilValidate.isEmpty(selectedCartItems)) {
        selectedCartItems = makeCartItemsArray(cart);
    }
    try {
        shoppingListId = addBulkFromCart(delegator, dispatcher, cart, userLogin, shoppingListId, shoppingListTypeId, selectedCartItems, true, true);
    } catch (IllegalArgumentException e) {
        request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
        return "error";
    }
    request.setAttribute("shoppingListId", shoppingListId);
    return "success";
}
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)

Example 33 with ShoppingCart

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

the class ShoppingListServices method makeShoppingListCart.

/**
 * Add a shoppinglist to an existing shoppingcart
 *
 * @param listCart the shopping cart list
 * @param dispatcher the local dispatcher
 * @param shoppingList a GenericValue object of the shopping list
 * @param locale the locale in use
 * @return the modified shopping cart adding the shopping list elements
 */
public static ShoppingCart makeShoppingListCart(ShoppingCart listCart, LocalDispatcher dispatcher, GenericValue shoppingList, Locale locale) {
    Delegator delegator = dispatcher.getDelegator();
    if (shoppingList != null && shoppingList.get("productStoreId") != null) {
        String productStoreId = shoppingList.getString("productStoreId");
        String currencyUom = shoppingList.getString("currencyUom");
        if (currencyUom == null) {
            GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator);
            if (productStore == null) {
                return null;
            }
            currencyUom = productStore.getString("defaultCurrencyUomId");
        }
        if (locale == null) {
            locale = Locale.getDefault();
        }
        List<GenericValue> items = null;
        try {
            items = shoppingList.getRelated("ShoppingListItem", null, UtilMisc.toList("shoppingListItemSeqId"), false);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
        if (UtilValidate.isNotEmpty(items)) {
            if (listCart == null) {
                listCart = new ShoppingCart(delegator, productStoreId, locale, currencyUom);
                listCart.setOrderPartyId(shoppingList.getString("partyId"));
                listCart.setAutoOrderShoppingListId(shoppingList.getString("shoppingListId"));
            } else {
                if (!listCart.getPartyId().equals(shoppingList.getString("partyId"))) {
                    Debug.logError("CANNOT add shoppingList: " + shoppingList.getString("shoppingListId") + " of partyId: " + shoppingList.getString("partyId") + " to a shoppingcart with a different orderPartyId: " + listCart.getPartyId(), module);
                    return listCart;
                }
            }
            ProductConfigWrapper configWrapper = null;
            for (GenericValue shoppingListItem : items) {
                String productId = shoppingListItem.getString("productId");
                BigDecimal quantity = shoppingListItem.getBigDecimal("quantity");
                Timestamp reservStart = shoppingListItem.getTimestamp("reservStart");
                BigDecimal reservLength = null;
                String configId = shoppingListItem.getString("configId");
                if (shoppingListItem.get("reservLength") != null) {
                    reservLength = shoppingListItem.getBigDecimal("reservLength");
                }
                BigDecimal reservPersons = null;
                if (shoppingListItem.get("reservPersons") != null) {
                    reservPersons = shoppingListItem.getBigDecimal("reservPersons");
                }
                if (UtilValidate.isNotEmpty(productId) && quantity != null) {
                    if (UtilValidate.isNotEmpty(configId)) {
                        configWrapper = ProductConfigWorker.loadProductConfigWrapper(delegator, dispatcher, configId, productId, listCart.getProductStoreId(), null, listCart.getWebSiteId(), listCart.getCurrency(), listCart.getLocale(), listCart.getAutoUserLogin());
                    }
                    // list items are noted in the shopping cart
                    String listId = shoppingListItem.getString("shoppingListId");
                    String itemId = shoppingListItem.getString("shoppingListItemSeqId");
                    Map<String, Object> attributes = UtilMisc.<String, Object>toMap("shoppingListId", listId, "shoppingListItemSeqId", itemId);
                    try {
                        listCart.addOrIncreaseItem(productId, null, quantity, reservStart, reservLength, reservPersons, null, null, null, null, null, attributes, null, configWrapper, null, null, null, dispatcher);
                    } catch (CartItemModifyException e) {
                        Debug.logError(e, "Unable to add product to List Cart - " + productId, module);
                    } catch (ItemNotFoundException e) {
                        Debug.logError(e, "Product not found - " + productId, module);
                    }
                }
            }
            if (listCart.size() > 0) {
                if (UtilValidate.isNotEmpty(shoppingList.get("paymentMethodId"))) {
                    listCart.addPayment(shoppingList.getString("paymentMethodId"));
                }
                if (UtilValidate.isNotEmpty(shoppingList.get("contactMechId"))) {
                    listCart.setAllShippingContactMechId(shoppingList.getString("contactMechId"));
                }
                if (UtilValidate.isNotEmpty(shoppingList.get("shipmentMethodTypeId"))) {
                    listCart.setAllShipmentMethodTypeId(shoppingList.getString("shipmentMethodTypeId"));
                }
                if (UtilValidate.isNotEmpty(shoppingList.get("carrierPartyId"))) {
                    listCart.setAllCarrierPartyId(shoppingList.getString("carrierPartyId"));
                }
                if (UtilValidate.isNotEmpty(shoppingList.getString("productPromoCodeId"))) {
                    listCart.addProductPromoCode(shoppingList.getString("productPromoCodeId"), dispatcher);
                }
            }
        }
    }
    return listCart;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ProductConfigWrapper(org.apache.ofbiz.product.config.ProductConfigWrapper) Timestamp(java.sql.Timestamp) 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) ItemNotFoundException(org.apache.ofbiz.order.shoppingcart.ItemNotFoundException)

Example 34 with ShoppingCart

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

the class ShoppingListServices method createListReorders.

public static Map<String, Object> createListReorders(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");
    boolean beganTransaction = false;
    EntityQuery eq = EntityQuery.use(delegator).from("ShoppingList").where("shoppingListTypeId", "SLT_AUTO_REODR", "isActive", "Y").orderBy("-lastOrderedDate");
    try {
        beganTransaction = TransactionUtil.begin();
    } catch (GenericTransactionException e1) {
        Debug.logError(e1, "[Delegator] Could not begin transaction: " + e1.toString(), module);
    }
    try (EntityListIterator eli = eq.queryIterator()) {
        if (eli != null) {
            GenericValue shoppingList;
            while (((shoppingList = eli.next()) != null)) {
                Timestamp lastOrder = shoppingList.getTimestamp("lastOrderedDate");
                RecurrenceInfo recurrence = null;
                GenericValue recurrenceInfo = shoppingList.getRelatedOne("RecurrenceInfo", false);
                Timestamp startDateTime = recurrenceInfo.getTimestamp("startDateTime");
                try {
                    recurrence = new RecurrenceInfo(recurrenceInfo);
                } catch (RecurrenceInfoException e) {
                    Debug.logError(e, module);
                }
                // check the next recurrence
                if (recurrence != null) {
                    long next = lastOrder == null ? recurrence.next(startDateTime.getTime()) : recurrence.next(lastOrder.getTime());
                    Timestamp now = UtilDateTime.nowTimestamp();
                    Timestamp nextOrder = UtilDateTime.getDayStart(UtilDateTime.getTimestamp(next));
                    if (nextOrder.after(now)) {
                        continue;
                    }
                } else {
                    continue;
                }
                ShoppingCart listCart = makeShoppingListCart(dispatcher, shoppingList, locale);
                CheckOutHelper helper = new CheckOutHelper(dispatcher, delegator, listCart);
                // store the order
                Map<String, Object> createResp = helper.createOrder(userLogin);
                if (createResp == null || (createResp != null && ServiceUtil.isError(createResp))) {
                    Debug.logError("Cannot create order for shopping list - " + shoppingList, module);
                } else {
                    String orderId = (String) createResp.get("orderId");
                    // authorize the payments
                    Map<String, Object> payRes = null;
                    try {
                        payRes = helper.processPayment(ProductStoreWorker.getProductStore(listCart.getProductStoreId(), delegator), userLogin);
                    } catch (GeneralException e) {
                        Debug.logError(e, module);
                    }
                    if (payRes != null && ServiceUtil.isError(payRes)) {
                        Debug.logError("Payment processing problems with shopping list - " + shoppingList, module);
                    }
                    shoppingList.set("lastOrderedDate", UtilDateTime.nowTimestamp());
                    shoppingList.store();
                    // send notification
                    try {
                        dispatcher.runAsync("sendOrderPayRetryNotification", UtilMisc.toMap("orderId", orderId));
                    } catch (GenericServiceException e) {
                        Debug.logError(e, module);
                    }
                    // increment the recurrence
                    recurrence.incrementCurrentCount();
                }
            }
        }
        return ServiceUtil.returnSuccess();
    } catch (GenericEntityException e) {
        try {
            // only rollback the transaction if we started one...
            TransactionUtil.rollback(beganTransaction, "Error creating shopping list auto-reorders", e);
        } catch (GenericEntityException e2) {
            Debug.logError(e2, "[Delegator] Could not rollback transaction: " + e2.toString(), module);
        }
        String errMsg = UtilProperties.getMessage(resource_error, "OrderErrorWhileCreatingNewShoppingListBasedAutomaticReorder", UtilMisc.toMap("errorString", e.toString()), locale);
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(errMsg);
    } finally {
        try {
            // only commit the transaction if we started one... this will throw an exception if it fails
            TransactionUtil.commit(beganTransaction);
        } catch (GenericEntityException e) {
            Debug.logError(e, "Could not commit transaction for creating new shopping list based automatic reorder", module);
        }
    }
}
Also used : Locale(java.util.Locale) RecurrenceInfoException(org.apache.ofbiz.service.calendar.RecurrenceInfoException) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) Timestamp(java.sql.Timestamp) CheckOutHelper(org.apache.ofbiz.order.shoppingcart.CheckOutHelper) Delegator(org.apache.ofbiz.entity.Delegator) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) RecurrenceInfo(org.apache.ofbiz.service.calendar.RecurrenceInfo) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Example 35 with ShoppingCart

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

the class ExpressCheckoutEvents method setExpressCheckout.

public static String setExpressCheckout(HttpServletRequest request, HttpServletResponse response) {
    Locale locale = UtilHttp.getLocale(request);
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
    CheckoutType checkoutType = determineCheckoutType(request);
    if (!checkoutType.equals(CheckoutType.NONE)) {
        String serviceName = null;
        if (checkoutType.equals(CheckoutType.PAYFLOW)) {
            serviceName = "payflowSetExpressCheckout";
        } else if (checkoutType.equals(CheckoutType.STANDARD)) {
            serviceName = "payPalSetExpressCheckout";
        }
        Map<String, ? extends Object> inMap = UtilMisc.toMap("userLogin", cart.getUserLogin(), "cart", cart);
        Map<String, Object> result = null;
        try {
            result = dispatcher.runSync(serviceName, inMap);
        } catch (GenericServiceException e) {
            Debug.logInfo(e, module);
            request.setAttribute("_EVENT_MESSAGE_", UtilProperties.getMessage(resourceErr, "AccountingPayPalCommunicationError", locale));
            return "error";
        }
        if (ServiceUtil.isError(result)) {
            String errorMessage = ServiceUtil.getErrorMessage(result);
            request.setAttribute("_ERROR_MESSAGE_", errorMessage);
            Debug.logError(errorMessage, module);
            return "error";
        }
    }
    return "success";
}
Also used : Locale(java.util.Locale) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

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