⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 orderservices.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                                Debug.logError(e, "Error when looking up product in createOrder service, product failed inventory reservation", module);
                            }
                            String invErrMsg = "The product ";

                            if (product != null) {
                                invErrMsg += getProductName(product, orderItem);
                            }
                            invErrMsg += " with ID " + orderItem.getString("productId") + " is no longer in stock. Please try reducing the quantity or removing the product from this order.";
                            resErrorMessages.add(invErrMsg);
                        }
                    }
                }

                if (resErrorMessages.size() > 0) {
                    result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
                    result.put(ModelService.ERROR_MESSAGE_LIST, resErrorMessages);
                    return result;
                }
                // END inventory reservation
            }

            result.put("orderId", orderId);
            result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
        } catch (GenericEntityException e) {
            Debug.logError(e, "Problem with reservations", module);
            result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
            result.put(ModelService.ERROR_MESSAGE, "ERROR: Could not create order (write error: " + e.getMessage() + ").");
        }

        return result;
    }

    public static String getProductName(GenericValue product, GenericValue orderItem) {
        if (UtilValidate.isNotEmpty(product.getString("productName"))) {
            return product.getString("productName");
        } else {
            return orderItem.getString("itemDescription");
        }
    }

    public static String getProductName(GenericValue product, String orderItemName) {
    	if (UtilValidate.isNotEmpty(product.getString("productName"))) {
    		return product.getString("productName");
    	} else {
    		return orderItemName;
    	}
    }

    /** Service for resetting the OrderHeader grandTotal */
    public static Map resetGrandTotal(DispatchContext ctx, Map context) {
        GenericDelegator delegator = ctx.getDelegator();
        //appears to not be used: GenericValue userLogin = (GenericValue) context.get("userLogin");
        String orderId = (String) context.get("orderId");

        GenericValue orderHeader = null;
        try {
            orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId));
        } catch (GenericEntityException e) {
            String errMsg = "ERROR: Could not set grantTotal on OrderHeader entity: " + e.toString();
            Debug.logError(e, errMsg, module);
            return ServiceUtil.returnError(errMsg);
        }

        if (orderHeader != null) {
            OrderReadHelper orh = new OrderReadHelper(orderHeader);
            Double currentTotal = orderHeader.getDouble("grandTotal");
            if (orh.getOrderGrandTotal() != currentTotal.doubleValue()) {
                orderHeader.set("grandTotal", new Double(orh.getOrderGrandTotal()));
                try {
                    orderHeader.store();
                } catch (GenericEntityException e) {
                    String errMsg = "ERROR: Could not set grantTotal on OrderHeader entity: " + e.toString();
                    Debug.logError(e, errMsg, module);
                    return ServiceUtil.returnError(errMsg);
                }
            }
        }

        return ServiceUtil.returnSuccess();
    }

    /** Service for setting the OrderHeader grandTotal for all OrderHeaders with no grandTotal */
    public static Map setEmptyGrandTotals(DispatchContext ctx, Map context) {
        GenericDelegator delegator = ctx.getDelegator();
        LocalDispatcher dispatcher = ctx.getDispatcher();
        GenericValue userLogin = (GenericValue) context.get("userLogin");

        try {
            EntityListIterator eli = delegator.findListIteratorByCondition("OrderHeader", new EntityExpr("grandTotal", EntityOperator.EQUALS, null), UtilMisc.toList("orderId"), null);
            GenericValue orderHeader = null;
            List orderIdList = new LinkedList();
            while ((orderHeader = (GenericValue) eli.next()) != null) {
                orderIdList.add(orderHeader.get("orderId"));
            }
            eli.close();
            
            Iterator orderIdIter = orderIdList.iterator();
            while (orderIdIter.hasNext()) {
                String orderId = (String) orderIdIter.next();
                Map results = dispatcher.runSync("resetGrandTotal", UtilMisc.toMap("orderId", orderId, "userLogin", userLogin));
                if (ServiceUtil.isError(results)) {
                    return ServiceUtil.returnError(null, null, null, results);
                }
            }
        } catch (GenericServiceException e) {
            String errMsg = "ERROR: Could not set grantTotal on OrderHeader entity: " + e.toString();
            Debug.logError(e, errMsg, module);
            return ServiceUtil.returnError(errMsg);
        } catch (GenericEntityException e) {
            String errMsg = "ERROR: Could not set grantTotal on OrderHeader entity: " + e.toString();
            Debug.logError(e, errMsg, module);
            return ServiceUtil.returnError(errMsg);
        }

        return ServiceUtil.returnSuccess();
    }

    /** Service for checking and re-clac the tax amount */
    public static Map recalcOrderTax(DispatchContext ctx, Map context) {
        LocalDispatcher dispatcher = ctx.getDispatcher();
        GenericDelegator delegator = ctx.getDelegator();
        String orderId = (String) context.get("orderId");
        GenericValue userLogin = (GenericValue) context.get("userLogin");

        // check and make sure we have permission to change the order
        Security security = ctx.getSecurity();
        if (!security.hasEntityPermission("ORDERMGR", "_UPDATE", userLogin)) {
            GenericValue placingCustomer = null;
            try {
                Map placingCustomerFields = UtilMisc.toMap("orderId", orderId, "partyId", userLogin.getString("partyId"), "roleTypeId", "PLACING_CUSTOMER");
                placingCustomer = delegator.findByPrimaryKey("OrderRole", placingCustomerFields);
            } catch (GenericEntityException e) {
                return ServiceUtil.returnError("ERROR: Cannot get OrderRole entity: " + e.getMessage());
            }
            if (placingCustomer == null)
                return ServiceUtil.returnError("You do not have permission to change this order's status.");
        }

        // get the order header
        GenericValue orderHeader = null;
        try {
            orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId));
        } catch (GenericEntityException e) {
            return ServiceUtil.returnError("ERROR: Cannot get OrderHeader entity: " + e.getMessage());
        }

        if (orderHeader == null) {
            return ServiceUtil.returnError("ERROR: No valid order header found for orderId : " + orderId);
        }

        // remove the tax adjustments
        int removed = 0;
        try {
            removed = delegator.removeByAnd("OrderAdjustment", UtilMisc.toMap("orderId", orderId, "orderAdjustmentTypeId", "SALES_TAX"));
        } catch (GenericEntityException e) {
            Debug.logError(e, "Unable to remove SALES_TAX adjustments for order : " + orderId, module);
            return ServiceUtil.returnError("Unable to remove SALES_TAX adjustments");
        }
        Debug.logInfo("Removed : " + removed + " SALES_TAX adjustments for order [" + orderId + "]", module);

        OrderReadHelper orh = new OrderReadHelper(orderHeader);
        List validOrderItems = orh.getValidOrderItems();
        if (validOrderItems != null) {
            // prepare the inital lists
            List products = new ArrayList(validOrderItems.size());
            List amounts = new ArrayList(validOrderItems.size());
            List shipAmts = new ArrayList(validOrderItems.size());

            // adjustments and total
            List allAdjustments = orh.getAdjustments();
            List orderHeaderAdjustments = OrderReadHelper.getOrderHeaderAdjustments(allAdjustments);
            double orderSubTotal = OrderReadHelper.getOrderItemsSubTotal(validOrderItems, allAdjustments);

            // shipping amount
            Double orderShipping = new Double(OrderReadHelper.calcOrderAdjustments(orderHeaderAdjustments, orderSubTotal, false, false, true));

            // build up the list of tax calc service parameters
            for (int i = 0; i < validOrderItems.size(); i++) {
                GenericValue orderItem = (GenericValue) validOrderItems.get(i);

                try {
                    products.add(i, orderItem.getRelatedOne("Product"));  // get the product entity
                    amounts.add(i, new Double(OrderReadHelper.getOrderItemSubTotal(orderItem, allAdjustments, true, false))); // get the item amount
                    shipAmts.add(i, new Double(OrderReadHelper.getOrderItemAdjustmentsTotal(orderItem, allAdjustments, false, false, true))); // get the shipping amount
                } catch (GenericEntityException e) {
                    Debug.logError(e, "Cannot read order item entity : " + orderItem, module);
                    return ServiceUtil.returnError("Cannot read the order item entity");
                }
            }

            // prepare the service context
            Map serviceContext = UtilMisc.toMap("productStoreId", orh.getProductStoreId(), "itemProductList", products, "itemAmountList", amounts,
                "itemShippingList", shipAmts, "orderShippingAmount", orderShipping, "shippingAddress", orh.getShippingAddress());

            // invoke the calcTax service
            Map serviceResult = null;
            try {
                serviceResult = dispatcher.runSync("calcTax", serviceContext);
            } catch (GenericServiceException e) {
                Debug.logError(e, module);
                return ServiceUtil.returnError("Problem occurred in tax service");
            }

            if (ServiceUtil.isError(serviceResult)) {
                return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
            }

            // the adjustments (returned in order) from the tax service
            List orderAdj = (List) serviceResult.get("orderAdjustments");
            List itemAdj = (List) serviceResult.get("itemAdjustments");

            // the toStore List
            List toStore = new ArrayList();

            // set the order adjustments
            if (orderAdj != null && orderAdj.size() > 0) {
                Iterator oai = orderAdj.iterator();
                while (oai.hasNext()) {
                    GenericValue oa = (GenericValue) oai.next();

                    Long adjSeqId = delegator.getNextSeqId("OrderAdjustment");
                    oa.set("orderAdjustmentId", adjSeqId.toString());
                    oa.set("orderId", orderId);
                    toStore.add(oa);
                }
            }

            // set the item adjustments
            if (itemAdj != null && itemAdj.size() > 0) {
                for (int i = 0; i < validOrderItems.size(); i++) {
                    GenericValue orderItem = (GenericValue) validOrderItems.get(i);
                    List itemAdjustments = (List) itemAdj.get(i);
                    Iterator ida = itemAdjustments.iterator();
                    while (ida.hasNext()) {
                        GenericValue ia = (GenericValue) ida.next();

                        Long adjSeqId = delegator.getNextSeqId("OrderAdjustment");
                        ia.set("orderAdjustmentId", adjSeqId.toString());
                        ia.set("orderId", orderId);
                        ia.set("orderItemSeqId", orderItem.getString("orderItemSeqId"));
                        toStore.add(ia);
                    }
                }
            }

            // store the new adjustments
            try {
                delegator.storeAll(toStore);
            } catch (GenericEntityException e) {
                Debug.logError(e, module);
                return ServiceUtil.returnError("Unable to update order tax information : " + orderId);
            }
        }

        return ServiceUtil.returnSuccess();
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -