Esempio n. 1
0
        /// <summary>
        /// 调用促销引擎计算促销折扣
        /// </summary>
        /// <param name="soInfo"></param>
        public virtual void CalculatePromotion(SOInfo soInfo)
        {
            //电子卡和实物卡Portal放在Portal计算
            if (soInfo.BaseInfo.SOType == SOType.ElectronicCard ||
                soInfo.BaseInfo.SOType == SOType.PhysicalCard)
            {
                soInfo.BaseInfo.CouponAmount = null;
                return;
            }

            //修改订单时,如果没有改变优惠券,则不重新验证和计算优惠券信息
            bool isModifyCoupons = true;

            if (IsUpdate &&
                OriginalSOInfo.SOPromotions != null &&
                OriginalSOInfo.SOPromotions.Count > 0 &&
                soInfo.SOPromotions != null &&
                soInfo.SOPromotions.Count > 0)
            {
                var currentCoupon = soInfo.Items.FirstOrDefault(item =>
                {
                    return(item.ProductType == SOProductType.Coupon);
                });
                int tmpCouponSysNo = currentCoupon != null ? currentCoupon.ProductSysNo.Value : 0;

                if (tmpCouponSysNo > 0)
                {
                    var orgCoupon = OriginalSOInfo.Items.FirstOrDefault(item =>
                    {
                        return(item.ProductType == SOProductType.Coupon);
                    });
                    int couponSysNo = orgCoupon != null ? orgCoupon.ProductSysNo.Value : 0;

                    if (couponSysNo == tmpCouponSysNo)
                    {
                        soInfo.SOPromotions = OriginalSOInfo.SOPromotions;
                        isModifyCoupons     = false;
                    }
                }
            }

            var newPromotionCalculates = ExternalDomainBroker.CalculateSOPromotion(soInfo, isModifyCoupons);

            soInfo.SOPromotions.RemoveAll(p => p.PromotionType != SOPromotionType.VendorGift);
            soInfo.SOPromotions.AddRange(newPromotionCalculates);

            //SOItemInfo的PromotionAmount 需要先初始化为0
            soInfo.BaseInfo.PromotionAmount             = 0;
            soInfo.Items.ForEach(p => p.PromotionAmount = 0);
            if (soInfo.SOPromotions != null &&
                soInfo.SOPromotions.Count > 0)
            {
                #region 如果有调价,将不使用的规则

                bool isManualPrice = soInfo.Items.Exists(p => p.AdjustPrice != 0 && !string.IsNullOrEmpty(p.AdjustPriceReason));
                //绑定
                if (isManualPrice)
                {
                    soInfo.SOPromotions = soInfo.SOPromotions.FindAll(p => p.PromotionType != SOPromotionType.Combo);
                }

                #endregion

                soInfo.Items.ForEach(item =>
                {
                    item.CouponAverageDiscount = (from p in soInfo.SOPromotions
                                                  from d in p.SOPromotionDetails
                                                  where d.MasterProductSysNo == item.ProductSysNo && p.PromotionType == SOPromotionType.Coupon
                                                  select d.DiscountAmount.Value).Sum() / item.Quantity.Value;

                    item.Price = (item.OriginalPrice - item.CouponAverageDiscount).Round(2);

                    item.PromotionAmount = (from p in soInfo.SOPromotions
                                            from d in p.SOPromotionDetails
                                            where d.MasterProductSysNo == item.ProductSysNo && p.PromotionType != SOPromotionType.Coupon
                                            select d.DiscountAmount.Value).Sum();
                    item.PromotionAmount = -item.PromotionAmount.Round(2);
                });

                foreach (var couponItem in soInfo.Items.Where(p => p.ProductType == SOProductType.Coupon))
                {
                    couponItem.Price     = 0;
                    couponItem.CostPrice = 0;
                }

                //电子卡和实物卡
                if (soInfo.BaseInfo.SOType == SOType.ElectronicCard ||
                    soInfo.BaseInfo.SOType == SOType.PhysicalCard)
                {
                    soInfo.BaseInfo.PromotionAmount = soInfo.SOPromotions
                                                      .Sum(item => item.DiscountAmount);

                    soInfo.BaseInfo.CouponAmount = null;
                    return;
                }

                //统计销售规则,销售立减总折扣
                soInfo.BaseInfo.PromotionAmount = soInfo.SOPromotions
                                                  .Where(item => item.PromotionType == SOPromotionType.Combo || item.PromotionType == SOPromotionType.SaleDiscountRule)
                                                  .Sum(item => item.DiscountAmount);

                if (soInfo.BaseInfo.PromotionAmount == 0)
                {
                    soInfo.BaseInfo.PromotionAmount = null;
                }

                //统计优惠券总折扣
                soInfo.BaseInfo.CouponAmount = soInfo.SOPromotions
                                               .Where(item => item.PromotionType == SOPromotionType.Coupon)
                                               .Sum(item => item.DiscountAmount);

                if (soInfo.BaseInfo.CouponAmount == 0)
                {
                    soInfo.BaseInfo.CouponAmount = null;
                }
            }

            //赠品信息计算
            CalculateVendorGift(soInfo);
        }