Search in sources :

Example 31 with OrderReadHelper

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

the class CheckOutEvents method createReplacementOrder.

/**
 * Create a replacement order from an existing order against a lost shipment etc. *
 */
public static String createReplacementOrder(HttpServletRequest request, HttpServletResponse response) {
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    HttpSession session = request.getSession();
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");
    Map<String, Object> context = cart.makeCartMap(dispatcher, false);
    String originalOrderId = request.getParameter("orderId");
    // create the replacement order adjustment
    List<GenericValue> orderAdjustments = UtilGenerics.checkList(context.get("orderAdjustments"));
    List<GenericValue> orderItems = UtilGenerics.checkList(context.get("orderItems"));
    OrderReadHelper orderReadHelper = new OrderReadHelper(orderAdjustments, orderItems);
    BigDecimal grandTotal = orderReadHelper.getOrderGrandTotal();
    if (grandTotal.compareTo(new BigDecimal(0)) != 0) {
        GenericValue adjustment = delegator.makeValue("OrderAdjustment");
        adjustment.set("orderAdjustmentTypeId", "REPLACE_ADJUSTMENT");
        adjustment.set("amount", grandTotal.negate());
        adjustment.set("comments", "ReShip Order for Order #" + originalOrderId);
        adjustment.set("createdDate", UtilDateTime.nowTimestamp());
        adjustment.set("createdByUserLogin", userLogin.getString("userLoginId"));
        cart.addAdjustment(adjustment);
    }
    // create the order association
    List<ShoppingCartItem> cartLines = cart.items();
    for (ShoppingCartItem sci : cartLines) {
        int index = cart.getItemIndex(sci);
        try {
            GenericValue orderItem = EntityQuery.use(delegator).from("OrderItem").where("orderId", originalOrderId, "isPromo", sci.getIsPromo() ? "Y" : "N", "productId", sci.getProductId(), "orderItemTypeId", sci.getItemType()).queryFirst();
            if (orderItem != null) {
                sci.setAssociatedOrderId(orderItem.getString("orderId"));
                sci.setAssociatedOrderItemSeqId(orderItem.getString("orderItemSeqId"));
                sci.setOrderItemAssocTypeId("REPLACEMENT");
                cart.addItem(index, sci);
            }
        } catch (CartItemModifyException | GenericEntityException e) {
            Debug.logError(e.getMessage(), module);
        }
    }
    String result = createOrder(request, response);
    if ("error".equals(result)) {
        return "error";
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HttpSession(javax.servlet.http.HttpSession) OrderReadHelper(org.apache.ofbiz.order.order.OrderReadHelper) BigDecimal(java.math.BigDecimal) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 32 with OrderReadHelper

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

the class ShoppingCartHelper method addToCartFromOrder.

public Map<String, Object> addToCartFromOrder(String catalogId, String orderId, String[] itemIds, boolean addAll, String itemGroupNumber) {
    List<String> errorMsgs = new ArrayList<>();
    Map<String, Object> result;
    String errMsg = null;
    if (UtilValidate.isEmpty(orderId)) {
        errMsg = UtilProperties.getMessage(resource_error, "cart.order_not_specified_to_add_from", this.cart.getLocale());
        result = ServiceUtil.returnError(errMsg);
        return result;
    }
    boolean noItems = true;
    List<? extends Object> itemIdList = null;
    Iterator<? extends Object> itemIter = null;
    OrderReadHelper orderHelper = new OrderReadHelper(delegator, orderId);
    if (addAll) {
        itemIdList = orderHelper.getOrderItems();
    } else {
        if (itemIds != null) {
            itemIdList = Arrays.asList(itemIds);
        }
    }
    if (UtilValidate.isNotEmpty(itemIdList)) {
        itemIter = itemIdList.iterator();
    }
    String orderItemTypeId = null;
    String productId = null;
    if (itemIter != null && itemIter.hasNext()) {
        while (itemIter.hasNext()) {
            GenericValue orderItem = null;
            Object value = itemIter.next();
            if (value instanceof GenericValue) {
                orderItem = (GenericValue) value;
            } else {
                String orderItemSeqId = (String) value;
                orderItem = orderHelper.getOrderItem(orderItemSeqId);
            }
            // do not include PROMO items
            if (orderItem.get("isPromo") != null && "Y".equals(orderItem.getString("isPromo"))) {
                continue;
            }
            orderItemTypeId = orderItem.getString("orderItemTypeId");
            productId = orderItem.getString("productId");
            // do not store rental items
            if ("RENTAL_ORDER_ITEM".equals(orderItemTypeId)) {
                continue;
            }
            if (UtilValidate.isNotEmpty(productId) && orderItem.get("quantity") != null) {
                BigDecimal amount = orderItem.getBigDecimal("selectedAmount");
                ProductConfigWrapper configWrapper = null;
                String aggregatedProdId = null;
                if (EntityTypeUtil.hasParentType(delegator, "ProductType", "productTypeId", ProductWorker.getProductTypeId(delegator, productId), "parentTypeId", "AGGREGATED")) {
                    try {
                        GenericValue instanceProduct = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
                        String configId = instanceProduct.getString("configId");
                        aggregatedProdId = ProductWorker.getInstanceAggregatedId(delegator, productId);
                        configWrapper = ProductConfigWorker.loadProductConfigWrapper(delegator, dispatcher, configId, aggregatedProdId, cart.getProductStoreId(), catalogId, cart.getWebSiteId(), cart.getCurrency(), cart.getLocale(), cart.getAutoUserLogin());
                    } catch (GenericEntityException e) {
                        errorMsgs.add(e.getMessage());
                    }
                }
                try {
                    this.cart.addOrIncreaseItem(UtilValidate.isNotEmpty(aggregatedProdId) ? aggregatedProdId : productId, amount, orderItem.getBigDecimal("quantity"), null, null, null, null, null, null, null, catalogId, configWrapper, orderItemTypeId, itemGroupNumber, null, dispatcher);
                    noItems = false;
                } catch (CartItemModifyException | ItemNotFoundException e) {
                    errorMsgs.add(e.getMessage());
                }
            }
        }
        if (errorMsgs.size() > 0) {
            result = ServiceUtil.returnError(errorMsgs);
            result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
            // don't return error because this is a non-critical error and should go back to the same page
            return result;
        }
    } else {
        noItems = true;
    }
    if (noItems) {
        result = ServiceUtil.returnSuccess();
        result.put("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error, "OrderNoItemsFoundToAdd", this.cart.getLocale()));
        // don't return error because this is a non-critical error and should go back to the same page
        return result;
    }
    result = ServiceUtil.returnSuccess();
    return result;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ArrayList(java.util.ArrayList) ProductConfigWrapper(org.apache.ofbiz.product.config.ProductConfigWrapper) OrderReadHelper(org.apache.ofbiz.order.order.OrderReadHelper) BigDecimal(java.math.BigDecimal) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Aggregations

GenericValue (org.apache.ofbiz.entity.GenericValue)32 OrderReadHelper (org.apache.ofbiz.order.order.OrderReadHelper)32 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)30 Delegator (org.apache.ofbiz.entity.Delegator)28 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)28 BigDecimal (java.math.BigDecimal)24 Locale (java.util.Locale)24 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)24 HashMap (java.util.HashMap)22 Timestamp (java.sql.Timestamp)7 Map (java.util.Map)5 GeneralException (org.apache.ofbiz.base.util.GeneralException)5 LinkedList (java.util.LinkedList)4 ModelService (org.apache.ofbiz.service.ModelService)3 NVPDecoder (com.paypal.sdk.core.nvp.NVPDecoder)2 NVPEncoder (com.paypal.sdk.core.nvp.NVPEncoder)2 PayPalException (com.paypal.sdk.exceptions.PayPalException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2