Beispiel #1
0
        public void Run(JobContext context)
        {
            var soList = FreightDA.GetSOList();

            foreach (var so in soList)
            {
                var tracking = FreightDA.GetTrackingNumber(so.SOSysNo, int.Parse(so.LocalWHSysNo));
                if (tracking != null && !string.IsNullOrEmpty(tracking.TrackingNumber))
                {
                    //订单运单号和实际重量
                    so.TrackingNumber = tracking.TrackingNumber;
                    so.RealWeight     = tracking.RealWeight;

                    //订单的实际支出运费
                    #region 【 开始计算运费 】
                    List <ShippingFeeQueryInfo> qryList = new List <ShippingFeeQueryInfo>();
                    var qry = new ShippingFeeQueryInfo();
                    qry.TransID           = so.SOSysNo.ToString();
                    qry.SoAmount          = so.SoAmount;
                    qry.SoTotalWeight     = Convert.ToInt32(so.RealWeight.ToString("F0")) * 1000;
                    qry.SOSingleMaxWeight = so.SOSingleMaxWeight;
                    qry.AreaId            = so.ReceiveAreaSysNo;
                    qry.CustomerSysNo     = so.CustomerSysNo;
                    qry.IsUseDiscount     = 0;
                    qry.SubShipTypeList   = so.ShipTypeSysNo.ToString();

                    VendorInfo vendorInfo = FreightDA.GetVendorInfo(so.MerchantSysNo);
                    qry.SellType      = vendorInfo == null ? 0 : Convert.ToInt32(vendorInfo.SellerType);
                    qry.MerchantSysNo = so.MerchantSysNo;

                    qry.ShipTypeId = 0;
                    qryList.Add(qry);

                    List <ShippingInfo> shipFeeList     = FreightDA.GetAllShippingFee(qryList);
                    ShippingInfo        curShippingInfo = null;

                    curShippingInfo = shipFeeList.Find(x => x.TransID == so.SOSysNo.ToString() && x.ShippingTypeID.ToString() == so.ShipTypeSysNo.ToString());
                    if (curShippingInfo != null)
                    {
                        so.RealPayFreight = curShippingInfo.ShippingPrice;
                    }
                    #endregion

                    //创建统计订单运费
                    if (FreightDA.GetSOFreightStat(so.SOSysNo) == null)
                    {
                        FreightDA.CreateSOFreightStat(so);
                    }
                }
            }
        }
        private void InnerCalculate(KeyValuePair <string, OrderInfo> preSubOrderKVS, List <FreeShippingItemConfig> rules)
        {
            bool isMatchFreeShippingCharge = false;

            ECommerce.Entity.Area contactAddressInfo = null;

            OrderInfo preSubOrderInfo       = preSubOrderKVS.Value;
            OrderInfo clonedPreSubOrderInfo = (OrderInfo)preSubOrderInfo.Clone();
            //原运费
            decimal ShippingPrice = preSubOrderInfo.ShippingAmount;
            //有效的免运费规则
            string matchedRule = null;

            foreach (var rule in rules)
            {
                // 1. 检查是否满足金额条件限制
                isMatchFreeShippingCharge = OrderAmtMatchedContext.GetContext(rule).IsMatched(preSubOrderInfo, rule);

                #region 2. 检查是否满足支付类型条件限制(旧逻辑现在不用)

                /*
                 * if (isMatchFreeShippingCharge && !string.IsNullOrWhiteSpace(rule.PayTypeSettingValue))
                 * {
                 *  isMatchFreeShippingCharge = false;
                 *
                 *  string[] setValues = rule.PayTypeSettingValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                 *  for (int i = 0; i < setValues.Length; i++)
                 *  {
                 *      if (setValues[i].Trim() == preSubOrderInfo.PayTypeID)
                 *      {
                 *          isMatchFreeShippingCharge = true;
                 *          break;
                 *      }
                 *  }
                 * }*/
                #endregion

                // 3. 检查是否满足配送区域条件限制
                if (isMatchFreeShippingCharge && !string.IsNullOrWhiteSpace(rule.ShipAreaSettingValue))
                {
                    isMatchFreeShippingCharge = false;
                    contactAddressInfo        = ECommerce.DataAccess.Common.CommonDA.GetAreaBySysNo(preSubOrderInfo.Contact.AddressAreaID);
                    if (contactAddressInfo != null && contactAddressInfo.ProvinceSysNo != null)
                    {
                        string[] setValues = rule.ShipAreaSettingValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < setValues.Length; i++)
                        {
                            if (setValues[i].Trim() == contactAddressInfo.ProvinceSysNo.ToString())
                            {
                                isMatchFreeShippingCharge = true;
                                break;
                            }
                        }
                    }
                }

                //商品免运费数量
                int ProductFreeNumber = 0;
                #region 排除免运费的商品
                if (isMatchFreeShippingCharge && rule.ProductSettingValue != null && rule.ProductSettingValue.Count > 0)
                {
                    isMatchFreeShippingCharge = false;
                    if (clonedPreSubOrderInfo.OrderItemGroupList != null)
                    {
                        foreach (var itemGroup in clonedPreSubOrderInfo.OrderItemGroupList)
                        {
                            if (itemGroup.ProductItemList != null)
                            {
                                for (int i = itemGroup.ProductItemList.Count - 1; i >= 0; i--)
                                {
                                    if (rule.ProductSettingValue.Exists(productSysNo => itemGroup.ProductItemList[i].ProductSysNo == productSysNo))
                                    {
                                        isMatchFreeShippingCharge = true;
                                        itemGroup.ProductItemList.RemoveAt(i);
                                        ProductFreeNumber++;
                                    }
                                }
                            }
                        }
                    }
                    if (clonedPreSubOrderInfo.GiftItemList != null)
                    {
                        for (int i = clonedPreSubOrderInfo.GiftItemList.Count - 1; i >= 0; i--)
                        {
                            if (rule.ProductSettingValue.Exists(productSysNo => clonedPreSubOrderInfo.GiftItemList[i].ProductSysNo == productSysNo))
                            {
                                clonedPreSubOrderInfo.GiftItemList.RemoveAt(i);
                                ProductFreeNumber++;
                            }
                        }
                    }
                    if (clonedPreSubOrderInfo.AttachmentItemList != null)
                    {
                        for (int i = clonedPreSubOrderInfo.AttachmentItemList.Count - 1; i >= 0; i--)
                        {
                            if (rule.ProductSettingValue.Exists(productSysNo => clonedPreSubOrderInfo.AttachmentItemList[i].ProductSysNo == productSysNo))
                            {
                                clonedPreSubOrderInfo.AttachmentItemList.RemoveAt(i);
                                ProductFreeNumber++;
                            }
                        }
                    }
                }
                #endregion
                if (ProductFreeNumber > 0)
                {
                    matchedRule += rule.ToXmlString() + ";";
                }

                /*
                 * //对同时满足门槛金额限制、支付类型限制、配送区域限制的子单重新计算运费(旧逻辑现在不用)
                 * if (isMatchFreeShippingCharge)
                 * {
                 *  CalcShippingAmount(preSubOrderKVS, rule);
                 *  //break;   //旧逻辑
                 * }
                 */
            }


            //排除免运费商品后,子单中还剩下部分商品,计算这部分商品的运费作为本单运费
            //fixbug: 在没有收货地址的情况下,如果订单满足免运费条件,此时ShippingFeeQueryInfo对象的AreaId=0,计算运费的sp进入某个特定分支后会报错
            if (clonedPreSubOrderInfo.TotalItemCount > 0 && preSubOrderInfo.Contact.AddressAreaID > 0)
            //if (ProductFreeNumber <= 0 && preSubOrderInfo.Contact.AddressAreaID > 0 && clonedPreSubOrderInfo.TotalItemCount > 0)
            {
                List <ShippingFeeQueryInfo> qryList = new List <ShippingFeeQueryInfo>();
                ShippingFeeQueryInfo        qry     = new ShippingFeeQueryInfo();
                qry.TransID           = preSubOrderKVS.Key;
                qry.SoAmount          = clonedPreSubOrderInfo.TotalProductAmount;
                qry.SoTotalWeight     = clonedPreSubOrderInfo.TotalWeight;
                qry.SOSingleMaxWeight = clonedPreSubOrderInfo.MaxWeight;
                qry.AreaId            = preSubOrderInfo.Contact.AddressAreaID;
                qry.CustomerSysNo     = preSubOrderInfo.Customer.SysNo;
                qry.IsUseDiscount     = 0;
                qry.SubShipTypeList   = preSubOrderInfo.ShipTypeID.ToString();
                qry.SellType          = Convert.ToInt32(preSubOrderInfo["SellerType"]);
                qry.MerchantSysNo     = Convert.ToInt32(preSubOrderKVS.Key.Split('|')[0]);
                qry.ShipTypeId        = 0;
                qryList.Add(qry);

                List <ShippingInfo> shipFeeList = PipelineDA.GetAllShippingFee(qryList);

                ShippingInfo curShippingInfo = shipFeeList.Find(x => x.TransID == preSubOrderKVS.Key && x.ShippingTypeID.ToString() == preSubOrderInfo.ShipTypeID);
                if (curShippingInfo != null)
                {
                    preSubOrderInfo.ShippingAmount = curShippingInfo.ShippingPrice; //+curShippingInfo.ShippingPackageFee;
                }
            }
            else
            {
                //排除免运费商品后,子单中商品数量为0,免本单运费
                preSubOrderInfo.ShippingAmount = 0m;
            }
            if (!string.IsNullOrWhiteSpace(matchedRule))
            {
                preSubOrderInfo["FreeShippingChargeLog"] = string.Format("符合免运费规则:{0}\r\n,原运费为:{1:F2}元,减免后运费为:{2:F2}元",
                                                                         matchedRule.ToXmlString(), ShippingPrice, preSubOrderInfo.ShippingAmount);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 计算运费
        /// </summary>
        /// <param name="order"></param>
        private void CalculateShippingAmount(OrderInfo order)
        {
            #region 【 step1 设置好各个子单的配送方式 】

            List <ShipTypeInfo> allShipTypeList = PipelineDA.GetSupportedShipTypeList(order.Contact.AddressAreaID, (PaymentCategory)order.PaymentCategory);

            List <int> stockSysNoList = order.SubOrderList.Values.Where(x => x["WarehouseNumber"] != null && (int)x["WarehouseNumber"] > 0)
                                        .Select(x => Convert.ToInt32(x["WarehouseNumber"]))
                                        .Distinct()
                                        .ToList();
            List <SimpleStockInfo> stockList = PipelineDA.QueryStockInfoList(stockSysNoList);

            List <ShippingFeeQueryInfo> qryList = new List <ShippingFeeQueryInfo>();
            ShippingFeeQueryInfo        qry     = null;
            ShipTypeInfo curShipTypeInfo        = null;
            //SimpleStockInfo curStockInfo = null;

            foreach (var kvs in order.SubOrderList)
            {
                OrderInfo subOrderInfo = kvs.Value;

                if (subOrderInfo["WarehouseNumber"] != null && (int)subOrderInfo["WarehouseNumber"] > 0)
                {
                    //curStockInfo = stockList.Find(x => x.SysNo == Convert.ToInt32(subOrderInfo["WarehouseNumber"]));
                    //判断是否是外贸型仓库,如果是外贸型仓库,配送方式由系统决定,否则由用户选择决定
                    //if (curStockInfo.StockType == 0 || curStockInfo.StockType == 1)
                    //{
                    //    curShipTypeInfo = PipelineDA.Pipeline_GetMerchantStockShippingType(Convert.ToInt32(kvs.Key.Split('|')[0]), Convert.ToInt32(subOrderInfo["WarehouseNumber"]));
                    //}
                    //else
                    //{
                    curShipTypeInfo = allShipTypeList.Find(x => x.ShipTypeSysNo.ToString() == subOrderInfo.ShipTypeID);
                    //}
                }
                if (curShipTypeInfo != null)
                {
                    subOrderInfo.ShipTypeID      = curShipTypeInfo.ShipTypeID;
                    subOrderInfo["ShipTypeName"] = curShipTypeInfo.ShipTypeName;
                    subOrderInfo["ShipTypeDesc"] = string.Format("{1}出库({0})", curShipTypeInfo.ShipTypeName
                                                                 , subOrderInfo["WarehouseName"]);

                    qry                   = new ShippingFeeQueryInfo();
                    qry.TransID           = kvs.Key;
                    qry.SoAmount          = subOrderInfo.TotalProductAmount;
                    qry.SoTotalWeight     = subOrderInfo.TotalWeight;
                    qry.SOSingleMaxWeight = subOrderInfo.MaxWeight;
                    qry.AreaId            = subOrderInfo.Contact.AddressAreaID;
                    qry.CustomerSysNo     = subOrderInfo.Customer.SysNo;
                    qry.IsUseDiscount     = 0;
                    qry.SubShipTypeList   = curShipTypeInfo.ShipTypeSysNo.ToString();
                    qry.SellType          = Convert.ToInt32(subOrderInfo["SellerType"]);
                    qry.MerchantSysNo     = Convert.ToInt32(kvs.Key.Split('|')[0]);
                    qry.ShipTypeId        = 0;
                    qryList.Add(qry);
                }
            }

            #endregion

            #region 【 开始计算运费 】

            List <ShippingInfo> shipFeeList     = PipelineDA.GetAllShippingFee(qryList);
            ShippingInfo        curShippingInfo = null;

            foreach (var kvs in order.SubOrderList)
            {
                curShippingInfo = shipFeeList.Find(x => x.TransID == kvs.Key && x.ShippingTypeID.ToString() == kvs.Value.ShipTypeID);
                if (curShippingInfo != null)
                {
                    kvs.Value.ShippingAmount = curShippingInfo.ShippingPrice; //+curShippingInfo.ShippingPackageFee;
                }
            }

            order.ShippingAmount = order.SubOrderList.Sum(x => x.Value.ShippingAmount);

            #endregion
        }
        private void CalcShippingAmount(KeyValuePair <string, OrderInfo> preSubOrderKVS, FreeShippingItemConfig matchedRule)
        {
            OrderInfo preSubOrderInfo       = preSubOrderKVS.Value;
            OrderInfo clonedPreSubOrderInfo = (OrderInfo)preSubOrderInfo.Clone();
            //主商品免运费数量
            int ProductFreeNumber = 0;

            //全网模式,免本单运费
            if (matchedRule.IsGlobal)
            {
                preSubOrderInfo.ShippingAmount = 0m;
            }
            else
            {
                //非全网模式,排除免运费的商品,计算剩下商品的运费
                if (matchedRule.ProductSettingValue != null && matchedRule.ProductSettingValue.Count > 0)
                {
                    #region 排除免运费的商品

                    if (clonedPreSubOrderInfo.OrderItemGroupList != null)
                    {
                        foreach (var itemGroup in clonedPreSubOrderInfo.OrderItemGroupList)
                        {
                            if (itemGroup.ProductItemList != null)
                            {
                                for (int i = itemGroup.ProductItemList.Count - 1; i >= 0; i--)
                                {
                                    if (matchedRule.ProductSettingValue.Exists(productSysNo => itemGroup.ProductItemList[i].ProductSysNo == productSysNo))
                                    {
                                        itemGroup.ProductItemList.RemoveAt(i);
                                        ProductFreeNumber++;
                                    }
                                }
                            }
                        }
                    }
                    if (clonedPreSubOrderInfo.GiftItemList != null)
                    {
                        for (int i = clonedPreSubOrderInfo.GiftItemList.Count - 1; i >= 0; i--)
                        {
                            if (matchedRule.ProductSettingValue.Exists(productSysNo => clonedPreSubOrderInfo.GiftItemList[i].ProductSysNo == productSysNo))
                            {
                                clonedPreSubOrderInfo.GiftItemList.RemoveAt(i);
                            }
                        }
                    }
                    if (clonedPreSubOrderInfo.AttachmentItemList != null)
                    {
                        for (int i = clonedPreSubOrderInfo.AttachmentItemList.Count - 1; i >= 0; i--)
                        {
                            if (matchedRule.ProductSettingValue.Exists(productSysNo => clonedPreSubOrderInfo.AttachmentItemList[i].ProductSysNo == productSysNo))
                            {
                                clonedPreSubOrderInfo.AttachmentItemList.RemoveAt(i);
                            }
                        }
                    }
                    #endregion

                    //排除免运费商品后,子单中还剩下部分商品,计算这部分商品的运费作为本单运费
                    //fixbug: 在没有收货地址的情况下,如果订单满足免运费条件,此时ShippingFeeQueryInfo对象的AreaId=0,计算运费的sp进入某个特定分支后会报错
                    if (clonedPreSubOrderInfo.TotalItemCount > 0 &&
                        preSubOrderInfo.Contact.AddressAreaID > 0)
                    //if (ProductFreeNumber <= 0 && preSubOrderInfo.Contact.AddressAreaID > 0 && clonedPreSubOrderInfo.TotalItemCount > 0)
                    {
                        List <ShippingFeeQueryInfo> qryList = new List <ShippingFeeQueryInfo>();
                        ShippingFeeQueryInfo        qry     = new ShippingFeeQueryInfo();
                        qry.TransID           = preSubOrderKVS.Key;
                        qry.SoAmount          = clonedPreSubOrderInfo.TotalProductAmount;
                        qry.SoTotalWeight     = clonedPreSubOrderInfo.TotalWeight;
                        qry.SOSingleMaxWeight = clonedPreSubOrderInfo.MaxWeight;
                        qry.AreaId            = preSubOrderInfo.Contact.AddressAreaID;
                        qry.CustomerSysNo     = preSubOrderInfo.Customer.SysNo;
                        qry.IsUseDiscount     = 0;
                        qry.SubShipTypeList   = preSubOrderInfo.ShipTypeID.ToString();
                        qry.SellType          = Convert.ToInt32(preSubOrderInfo["SellerType"]);
                        qry.MerchantSysNo     = Convert.ToInt32(preSubOrderKVS.Key.Split('|')[0]);
                        qry.ShipTypeId        = 0;
                        qryList.Add(qry);

                        List <ShippingInfo> shipFeeList = PipelineDA.GetAllShippingFee(qryList);

                        ShippingInfo curShippingInfo = shipFeeList.Find(x => x.TransID == preSubOrderKVS.Key && x.ShippingTypeID.ToString() == preSubOrderInfo.ShipTypeID);
                        if (curShippingInfo != null)
                        {
                            preSubOrderInfo.ShippingAmount = curShippingInfo.ShippingPrice; //+curShippingInfo.ShippingPackageFee;
                        }
                    }
                    else
                    {
                        //排除免运费商品后,子单中商品数量为0,免本单运费
                        preSubOrderInfo.ShippingAmount = 0m;
                    }
                }
            }
            preSubOrderInfo["FreeShippingChargeLog"] = string.Format("符合免运费规则:{0}\r\n,原运费为:{1:F2}元,减免后运费为:{2:F2}元",
                                                                     matchedRule.ToXmlString(), clonedPreSubOrderInfo.ShippingAmount, preSubOrderInfo.ShippingAmount);
        }
        /// <summary>
        /// 计算运费
        /// </summary>
        /// <param name="order"></param>
        private void CalculateShippingAmount(OrderInfo order)
        {
            #region 【 step1 设置好各个子单的配送方式 】

            List <ShipTypeInfo> allShipTypeList = PipelineDA.GetSupportedShipTypeList(order.Contact.AddressAreaID, (PaymentCategory)order.PaymentCategory);

            ShipTypeInfo        curShipTypeInfo      = null;
            List <ShipTypeInfo> tempShipTypeList     = null;
            List <ShipTypeInfo> subOrderShipTypeList = null;

            List <ShippingFeeQueryInfo> qryList = new List <ShippingFeeQueryInfo>();
            ShippingFeeQueryInfo        qry     = null;

            foreach (var kvs in order.SubOrderList)
            {
                OrderInfo subOrderInfo = kvs.Value;

                if (subOrderInfo["WarehouseNumber"] != null)
                {
                    int subOrderWarehouseNumber = Convert.ToInt32(subOrderInfo["WarehouseNumber"]);
                    tempShipTypeList = allShipTypeList.FindAll(shipType => shipType.OnlyForStockSysNo == subOrderWarehouseNumber &&
                                                               shipType.StoreType == (int)subOrderInfo["StoreType"]);
                }
                else
                {
                    tempShipTypeList = allShipTypeList;
                }

                subOrderShipTypeList = null;
                curShipTypeInfo      = null;

                if (tempShipTypeList != null)
                {
                    subOrderShipTypeList = new List <ShipTypeInfo>();
                    subOrderShipTypeList.AddRange(tempShipTypeList);
                }

                if (subOrderShipTypeList != null && subOrderShipTypeList.Count > 0)
                {
                    subOrderShipTypeList.Sort((x, y) => x.Priority.CompareTo(y.Priority));
                    curShipTypeInfo = subOrderShipTypeList[0];
                }

                if (curShipTypeInfo != null)
                {
                    subOrderInfo.ShipTypeID      = curShipTypeInfo.ShipTypeID;
                    subOrderInfo["ShipTypeName"] = curShipTypeInfo.ShipTypeName;
                    subOrderInfo["ShipTypeDesc"] = string.Format("{0}:{1}出库直邮", curShipTypeInfo.ShipTypeName
                                                                 , subOrderInfo["WarehouseName"]);

                    qry                   = new ShippingFeeQueryInfo();
                    qry.TransID           = kvs.Key;
                    qry.SoAmount          = subOrderInfo.TotalProductAmount;
                    qry.SoTotalWeight     = subOrderInfo.TotalWeight;
                    qry.SOSingleMaxWeight = subOrderInfo.MaxWeight;
                    qry.AreaId            = subOrderInfo.Contact.AddressAreaID;
                    qry.CustomerSysNo     = subOrderInfo.Customer.SysNo;
                    qry.IsUseDiscount     = 0;
                    qry.SubShipTypeList   = curShipTypeInfo.ShipTypeID.ToString();
                    qry.SellType          = Convert.ToInt32(subOrderInfo["SellerType"]);
                    qry.MerchantSysNo     = Convert.ToInt32(kvs.Key.Split('|')[0]);
                    qry.ShipTypeId        = 0;
                    qryList.Add(qry);
                }
            }

            #endregion

            #region 【 开始计算运费 】

            List <ShippingInfo> shipFeeList     = PipelineDA.GetAllShippingFee(qryList);
            ShippingInfo        curShippingInfo = null;

            foreach (var kvs in order.SubOrderList)
            {
                curShippingInfo = shipFeeList.Find(x => x.TransID == kvs.Key && x.ShippingTypeID.ToString() == kvs.Value.ShipTypeID);
                if (curShippingInfo != null)
                {
                    kvs.Value.ShippingAmount = 0m;
                }
            }

            order.ShippingAmount = order.SubOrderList.Sum(x => x.Value.ShippingAmount);

            #endregion
        }