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

📄 checkouthelper.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                carrierPartyId = shippingMethod.substring(delimiterPos + 1);
            }

            this.cart.setShipmentMethodTypeId(shipmentMethodTypeId);
            this.cart.setCarrierPartyId(carrierPartyId);
        } else {
            errMsg = UtilProperties.getMessage(resource,"checkhelper.select_shipping_method", (cart != null ? cart.getLocale() : Locale.getDefault()));
            result = ServiceUtil.returnError(errMsg);
        }

        //Set the remaining order options
        this.cart.setShippingInstructions(shippingInstructions);
        this.cart.setGiftMessage(giftMessage);
        this.cart.setMaySplit(Boolean.valueOf(maySplit));
        this.cart.setIsGift(Boolean.valueOf(isGift));

        result = ServiceUtil.returnSuccess();
        return result;
    }

    /**
     * Indicates whether the payment should be made offline or
     * whether further settings will be given for the electronic
     * payment method.
     *
     * @param paymentMthodType "offline" to indicate that is to be paid
     * offline, <code>null</code> or anything else for online.
     * @return A Map conforming to the OFBiz Service conventions containing
     * any error messages
     */
    public Map finalizeOrderEntryMethodType(String paymentMthodType) {
        Map result;

        this.cart.clearPaymentMethodTypeIds();
        this.cart.clearPaymentMethodIds();
        if (paymentMthodType != null && paymentMthodType.equals("offline")) {
            this.cart.addPaymentMethodTypeId("EXT_OFFLINE");
        }

        result = ServiceUtil.returnSuccess();
        return result;
    }

    /**
     * Sets the payment ID to use during the checkout process
     *
     * @param checkOutPaymentId The type of payment to use, should
     * be "OFFLINE_PAYMENT" to indicate offlinem otherwise the payment ID
     * will be associated with the cart
     * @return A Map conforming to the OFBiz Service conventions containing
     * any error messages. Includes the field "OFFLINE_PAYMENT"
     * containing a <code>Boolean</code> indicating whether it's an offline
     * payment or not.
     */
    public Map finalizeOrderEntryPayment(String checkOutPaymentId, Double amount, boolean singleUse, boolean append) {
        Map result = ServiceUtil.returnSuccess();

        if (UtilValidate.isNotEmpty(checkOutPaymentId)) {
            if (!checkOutPaymentId.equals("OFFLINE_PAYMENT")) {
                // clear out the old payments
                if (!append) {
                    this.cart.clearPaymentMethodTypeIds();
                    this.cart.clearPaymentMethodIds();
                }
                // all payment method ids will be numeric, type ids will start with letter
                if (Character.isLetter(checkOutPaymentId.charAt(0))) {
                    this.cart.addPaymentMethodTypeId(checkOutPaymentId);
                } else {
                    this.cart.setPaymentMethodAmount(checkOutPaymentId, amount, singleUse);
                }
            } else {
                this.cart.clearPaymentMethodIds();
                this.cart.clearPaymentMethodTypeIds();
                result.put("OFFLINE_PAYMENT", new Boolean(true));
            }
        }

        return result;
    }

    /**
     * Defines the payment options for an order.
     *
     * @param params Contains the amount associated with
     * each <code>paymentMethodTypeId</code>.
     * @return A Map conforming to the OFBiz Service conventions containing
     * any error messages. Includes the field "OFFLINE_PAYMENTS"
     * containing a <code>Boolean</code> indicating whether it's an offline
     * payment or not.
     */
    public Map finalizeOrderEntryOfflinePayments(Map params) {
        Map result = ServiceUtil.returnSuccess();
        List errorMessages = new ArrayList();
        String errMsg=null;

        // get a list of payment types
        List paymentTypes = null;
        try {
            paymentTypes = delegator.findAll("PaymentMethodType");
        } catch (GenericEntityException e) {
            Debug.logError(e, "Cannot get payment method types from datasource", module);
        }
        if (paymentTypes != null) {
            Map paymentPrefs = new HashMap();
            double paymentTally = 0.00;
            Iterator pi = paymentTypes.iterator();
            while (pi.hasNext()) {
                GenericValue paymentMethodType = (GenericValue) pi.next();
                String paymentType = null;
                if (paymentMethodType != null && paymentMethodType.get("paymentMethodTypeId") != null) {
                    paymentType = paymentMethodType.getString("paymentMethodTypeId");
                }

                // get the amount by type
                double paymentAmount = 0.00;
                if (paymentType != null && !paymentType.equals("OFFLINE")) {
                    String amount = (String) params.get(paymentMethodType.getString("paymentMethodTypeId"));
                    if (amount != null && amount.length() > 0) {
                        try {
                            paymentAmount = NumberFormat.getNumberInstance().parse(amount).doubleValue();
                        } catch (java.text.ParseException pe) {
                            errMsg = UtilProperties.getMessage(resource,"checkhelper.problems_parsing_amount", (cart != null ? cart.getLocale() : Locale.getDefault()));
                            result = ServiceUtil.returnError(errMsg);
                            return result;
                        }
                    }
                }

                // only worry about types w/ an amount
                if (paymentAmount > 0.00) {
                    paymentPrefs.put(paymentType, new Double(paymentAmount));
                    paymentTally += paymentAmount;
                }
            }

            double cartTotal = cart.getGrandTotal();
            if (cartTotal != paymentTally) {
                errMsg = UtilProperties.getMessage(resource,"checkhelper.totals_do_not_match_order_total", (cart != null ? cart.getLocale() : Locale.getDefault()));
                result = ServiceUtil.returnError(errMsg);
                return result;
            } else {
                Set keySet = paymentPrefs.keySet();
                Iterator i = keySet.iterator();
                while (i.hasNext()) {
                    String type = (String) i.next();
                    Double amt = (Double) paymentPrefs.get(type);
                    cart.addPaymentMethodTypeId(type, amt);
                }
                result.put("OFFLINE_PAYMENTS", new Boolean(true));
            }
        }

        return result;
    }

    /**
     * Performs all the finalization settings and combines all the results.
     * This is a convenience method, primarily to match the original
     * code structure of the adapter class <code>CheckOutEvents</code>.
     * <p>
     * I would prefer to remove this altogether and move the method
     * {@link #addErrors(List, Map, Map) addErrors} to the utility
     * class {@link ServiceUtil ServiceUtil}
     *
     * @see CheckOutHelper#finalizeOrderEntryMethodType(String)
     * @see CheckOutHelper#finalizeOrderEntryOfflinePayments(Map)
     * @see CheckOutHelper#finalizeOrderEntryOptions(String, String, String, String, String)
     * @see CheckOutHelper#finalizeOrderEntryPayment(String, Double, boolean, boolean)
     * @see CheckOutHelper#finalizeOrderEntryShip(String)
     */
    public Map finalizeOrderEntry(String finalizeMode, String shippingContactMechId, String shippingMethod,
                                  String shippingInstructions, String maySplit, String giftMessage, String isGift, String methodType,
                                  String checkOutPaymentId, boolean isSingleUsePayment, boolean appendPayment, Map params) {
        Map result = ServiceUtil.returnSuccess();
        Map errorMaps = new HashMap();
        Map callResult;
        List errorMessages = new ArrayList();

        // set the shipping method
        if (finalizeMode != null && finalizeMode.equals("ship")) {
            callResult = this.finalizeOrderEntryShip(shippingContactMechId);
            this.addErrors(errorMessages, errorMaps, callResult);
        }

        // set the options
        if (finalizeMode != null && finalizeMode.equals("options")) {
            callResult = this.finalizeOrderEntryOptions(shippingMethod, shippingInstructions, maySplit, giftMessage, isGift);
            this.addErrors(errorMessages, errorMaps, callResult);
        }

        // payment option; if offline we skip the payment screen
        if (finalizeMode != null && finalizeMode.equals("payoption")) {
            callResult = this.finalizeOrderEntryMethodType(methodType);
            this.addErrors(errorMessages, errorMaps, callResult);
        }

        // set the payment
        if (finalizeMode != null && finalizeMode.equals("payment")) {
            Map selectedPaymentMethods = null;
            if (checkOutPaymentId != null) {
                callResult = this.finalizeOrderEntryPayment(checkOutPaymentId, null, isSingleUsePayment, appendPayment);
                this.addErrors(errorMessages, errorMaps, callResult);
                selectedPaymentMethods = UtilMisc.toMap(checkOutPaymentId, null);
            }
            callResult = checkGiftCard(params, selectedPaymentMethods);
            this.addErrors(errorMessages, errorMaps, callResult);
            if (errorMessages.size() == 0 && errorMaps.size() == 0) {
                String gcPaymentMethodId = (String) callResult.get("paymentMethodId");
                Double giftCardAmount = (Double) callResult.get("amount");
                Map gcCallRes = this.finalizeOrderEntryPayment(gcPaymentMethodId, giftCardAmount, true, true);
                this.addErrors(errorMessages, errorMaps, gcCallRes);
            }
        }

        // create offline payments
        if (finalizeMode != null && finalizeMode.equals("offline_payments")) {
            callResult = this.finalizeOrderEntryOfflinePayments(params);
            this.addErrors(errorMessages, errorMaps, callResult);
        }

        //See whether we need to return an error or not
        if (errorMessages.size() > 0) {
            result.put(ModelService.ERROR_MESSAGE_LIST, errorMessages);
            result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        }
        if (errorMaps.size() > 0) {
            result.put(ModelService.ERROR_MESSAGE_MAP, errorMaps);
            result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
        }

        return result;
    }

    /**
     * Takes the result of an invocation and extracts any error messages
     * and adds them to the targetList. This will handle both List and String
     * error messags.
     *
     * @param targetList    The List to add the error messages to
     * @param targetMap The Map to add any Map error messages to
     * @param callResult The result from an invocation
     */
    private void addErrors(List targetList, Map targetMap, Map callResult) {
        List newList;
        Map.Entry entry;
        Iterator mapIter;
        Map errorMsgMap;
        StringBuffer outMsg;

        //See if there is a single message
        if (callResult.containsKey(ModelService.ERROR_MESSAGE)) {
            targetList.add(callResult.get(ModelService.ERROR_MESSAGE));
        }

        //See if there is a message list
        if (callResult.containsKey(ModelService.ERROR_MESSAGE_LIST)) {
            newList = (List) callResult.get(ModelService.ERROR_MESSAGE_LIST);
            targetList.addAll(newList);
        }

        //See if there are an error message map
        if (callResult.containsKey(ModelService.ERROR_MESSAGE_MAP)) {
            errorMsgMap = (Map) callResult.get(ModelService.ERROR_MESSAGE_MAP);
            targetMap.putAll(errorMsgMap);
        }
    }

    public double availableAccountBalance(String billingAccountId) {
        GenericValue billingAccount = null;
        Double accountBalance = new Double(0.00);
        Double accountLimit = new Double(0.00);

        if (billingAccountId != null) {
            try {
                Map res = dispatcher.runSync("calcBillingAccountBalance", UtilMisc.toMap("billingAccountId", billingAccountId));
                billingAccount = (GenericValue) res.get("billingAccount");
                accountBalance = (Double) res.get("accountBalance");
            } catch (GenericServiceException e) {
                Debug.logError(e, module);
            }
            if (billingAccount != null) {
                accountLimit = billingAccount.getDouble("accountLimit");
            }

            if (accountLimit == null) {
                accountLimit = new Double(0.00);
            }
            if (accountBalance == null) {
                accountBalance = new Double(0.00);
            }
        }

        double availableBalance = (accountLimit.doubleValue() - accountBalance.doubleValue());
        String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00");
        DecimalFormat formatter = new DecimalFormat(currencyFormat);
        String availableString = formatter.format(availableBalance);
        Double available = null;
        try {
            available = new Double(formatter.parse(availableString).doubleValue());
        } catch (ParseException e) {
            Debug.logError(e, "Problem getting parsed available amount", module);
        }
        //Debug.logInfo("Billing Account : " + billingAccountId + " - " + available, module);
        return available.doubleValue();
    }

    public Map makeBillingAccountMap(List paymentPrefs) {
        Map accountMap = new HashMap();
        if (paymentPrefs != null) {
            Iterator i = accountMap.keySet().iterator();
            while (i.hasNext()) {
                GenericValue pp = (GenericValue) i.next();
                if (pp.get("billingAccountId") != null) {
                    accountMap.put(pp.getString("billingAccountId"), pp.getDouble("maxAmount"));
                }
            }
        }
        return accountMap;
    }
}

⌨️ 快捷键说明

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