public void Calculate(ref OrderInfo order) { order.CouponErrorDesc = string.Empty; if (string.IsNullOrWhiteSpace(order.CouponCode)) { return; } string couponCode = order.CouponCode.ToUpper().Trim(); #region 1、初次筛选该优惠券号码对于前用户是否能用 List <CustomerCouponInfo> customerCouponList = PromotionDA.GetCanUsingCouponCodeList(order.Customer.SysNo, order.Customer.CustomerRank); CustomerCouponInfo customerCoupon = customerCouponList.Find(f => f.CouponCode.ToUpper().Trim() == couponCode); if (customerCoupon == null) { SetCouponError(ref order, LanguageHelper.GetText("您没有此优惠券号码{0}", order.LanguageCode), couponCode); return; } if (customerCoupon.IsExpired) { SetCouponError(ref order, LanguageHelper.GetText("此优惠券号码{0}已过期", order.LanguageCode), couponCode); return; } if (customerCoupon.CustomerMaxFrequency.HasValue && customerCoupon.UsedCount >= customerCoupon.CustomerMaxFrequency.Value) { SetCouponError(ref order, LanguageHelper.GetText("您已达到使用优惠券号码{0}的次数上限", order.LanguageCode), couponCode); return; } int totalUsedCount = 0; int CodeTotalUsedCount = 0; //用户参与活动的次数 int CustomerUsedCount = 0; //if (customerCoupon.CodeType.Trim().ToUpper() == "C") //{ CodeTotalUsedCount = PromotionDA.GetCouponCodeTotalUsedCount(couponCode); //} //else //{ //这个是活动的已使用次数 totalUsedCount = PromotionDA.GetCouponTotalUsedCount(customerCoupon.CouponSysNo); CustomerUsedCount = PromotionDA.GetCustomerCouponNumber(customerCoupon.CouponSysNo, order.Customer.SysNo); //} if (customerCoupon.WebsiteMaxFrequency.HasValue && CodeTotalUsedCount >= customerCoupon.WebsiteMaxFrequency) { SetCouponError(ref order, LanguageHelper.GetText("此优惠券号码{0}在全网使用次数已用尽,请使用其他优惠券号码。", order.LanguageCode), couponCode); return; } //if (customerCoupon.WebsiteMaxFrequency.HasValue && totalUsedCount >= customerCoupon.WebsiteMaxFrequency) //{ // SetCouponError(ref order, LanguageHelper.GetText("此优惠券号码{0}的已达到全网使用次数上限", order.LanguageCode), couponCode); // return; //} #endregion #region 2、获取该优惠券号码对应的优惠券活动所有信息 CouponInfo coupon = PromotionDA.GetComboInfoByCouponCode(order.CouponCode); var orderItem = order.OrderItemGroupList.FirstOrDefault(); int merchantSysNo = orderItem == null ? 1 : orderItem.MerchantSysNo; string merchantName = orderItem == null ? "泰隆" : orderItem.MerchantName; if (coupon.MerchantSysNo != merchantSysNo && coupon.MerchantSysNo != 1) { SetCouponError(ref order, LanguageHelper.GetText(string.Format("该优惠券不是{0}优惠券", merchantName), order.LanguageCode)); return; } if (coupon.SaleRulesEx.CustomerMaxFrequency.HasValue && CustomerUsedCount >= coupon.SaleRulesEx.CustomerMaxFrequency.Value) //if (coupon.SaleRulesEx.CustomerMaxFrequency.HasValue && customerCoupon.UsedCount >= coupon.SaleRulesEx.CustomerMaxFrequency.Value) { SetCouponError(ref order, LanguageHelper.GetText("您已达到使用此优惠券活动的次数上限", order.LanguageCode)); return; } if (coupon.SaleRulesEx.MaxFrequency.HasValue && totalUsedCount >= coupon.SaleRulesEx.MaxFrequency.Value) { SetCouponError(ref order, LanguageHelper.GetText("此优惠券活动已达到全网使用次数上限", order.LanguageCode)); return; } if (coupon == null) { SetCouponError(ref order, LanguageHelper.GetText("此优惠券{0}不存在", order.LanguageCode), couponCode); return; } #endregion #region 3、详细检查该优惠券号码是否可用 if (!string.IsNullOrWhiteSpace(coupon.SaleRulesEx.NeedEmailVerification) && coupon.SaleRulesEx.NeedEmailVerification.ToUpper().Trim() == "Y") { if (order.Customer.IsEmailConfirmed != 1) { SetCouponError(ref order, LanguageHelper.GetText("此优惠券要求客户验证了电子邮箱才可使用!", order.LanguageCode)); return; } } if (!string.IsNullOrWhiteSpace(coupon.SaleRulesEx.NeedMobileVerification) && coupon.SaleRulesEx.NeedMobileVerification.ToUpper().Trim() == "Y") { if (order.Customer.IsPhoneValided != 1) { SetCouponError(ref order, LanguageHelper.GetText("此优惠券要求客户验证了手机才可使用!", order.LanguageCode)); return; } } if (coupon.SaleRulesList.Count > 0 && coupon.SaleRulesList.Exists(f => f.SaleRuleType == CouponSaleRuleType.RelCustomerRank)) { //-1表示不限制 if (!coupon.SaleRulesList.Exists(f => f.CustomerRank == -1)) { int customerRank = order.Customer.CustomerRank; if (!coupon.SaleRulesList.Exists(f => f.CustomerRank == customerRank)) { SetCouponError(ref order, LanguageHelper.GetText("当前客户不满足此优惠券要求的客户等级!", order.LanguageCode)); return; } } } if (coupon.SaleRulesList.Count > 0 && coupon.SaleRulesList.Exists(f => f.SaleRuleType == CouponSaleRuleType.RelArea)) { int areaID = order.Contact.AddressAreaID; Area area = PromotionDA.GetAreaBySysNo(areaID); int provinceId = area.ProvinceSysNo.Value; if (!coupon.SaleRulesList.Exists(f => f.SaleRuleType == CouponSaleRuleType.RelArea && f.AreaSysNo == provinceId)) { SetCouponError(ref order, LanguageHelper.GetText("当前客户不满足此优惠券要求的客户地区!", order.LanguageCode)); return; } } #endregion #region 4、计算该优惠券可以抵扣多少,填充到OrderInfo中 if (coupon.DiscountRuleList != null && coupon.DiscountRuleList.Count > 0) { //取得满足优惠券条件的商品总金额 decimal canCalculateAmount = GetCanCalculateAmount(ref order, coupon); if (canCalculateAmount == 0) { SetCouponError(ref order, LanguageHelper.GetText("没有满足该优惠券的商品,无法抵扣", order.LanguageCode)); return; } canCalculateAmount = canCalculateAmount - Math.Abs(order.TotalDiscountAmount); if (canCalculateAmount < coupon.SaleRulesEx.OrderAmountLimit) { SetCouponError(ref order, LanguageHelper.GetText("没有满足该优惠券要求的商品总金额下限{0},无法抵扣", order.LanguageCode), coupon.SaleRulesEx.OrderAmountLimit); return; } #region 处理订单 折扣金额模式 和 百分比模式 if (coupon.DiscountRuleList[0].DiscountType == CouponDiscountType.OrderAmountDiscount || coupon.DiscountRuleList[0].DiscountType == CouponDiscountType.OrderAmountPercentage) { //获取适合的折扣规则 Coupon_DiscountRules curDiscountRule = GetMatchDiscountRule(coupon, canCalculateAmount); if (curDiscountRule == null) { SetCouponError(ref order, LanguageHelper.GetText("订单总金额未达到优惠券要求的最小购买金额,无法抵扣", order.LanguageCode)); return; } decimal discount = 0m; if (curDiscountRule.DiscountType == CouponDiscountType.OrderAmountDiscount) { discount = Math.Abs(Math.Round(curDiscountRule.Value, 2)); } else { discount = Math.Abs(Math.Round(canCalculateAmount * curDiscountRule.Value, 2)); } if (coupon.SaleRulesEx.OrderMaxDiscount.HasValue && coupon.SaleRulesEx.OrderMaxDiscount.Value > 0m) { if (discount > coupon.SaleRulesEx.OrderMaxDiscount.Value) { discount = coupon.SaleRulesEx.OrderMaxDiscount.Value; } } order.CouponCodeSysNo = customerCoupon.CouponCodeSysNo; order.CouponSysNo = customerCoupon.CouponSysNo; order.CouponName = customerCoupon.CouponName; order.CouponAmount = discount; order.CouponErrorDesc = string.Empty; } #endregion #region 处理 一个商品直减模式 if (coupon.DiscountRuleList[0].DiscountType == CouponDiscountType.ProductPriceDiscount) { if (coupon.SaleRulesList == null || coupon.SaleRulesList.Count == 0) { SetCouponError(ref order, LanguageHelper.GetText("没有满足该优惠券的指定商品,无法抵扣", order.LanguageCode)); return; } Coupon_SaleRules productSaleRule = coupon.SaleRulesList.Find(f => f.SaleRuleType == CouponSaleRuleType.RelProduct); int discountProductSysNo = productSaleRule.ProductSysNo; int discountProductCount = 0; decimal productReduce = 0m; decimal productPrice = 0.00m; foreach (OrderItemGroup itemGroup in order.OrderItemGroupList) { OrderProductItem item = itemGroup.ProductItemList.Find(f => f.ProductSysNo == discountProductSysNo); if (item != null) { discountProductCount += item.UnitQuantity * itemGroup.Quantity; productPrice = item.UnitSalePrice; } } if (discountProductCount == 0) { SetCouponError(ref order, LanguageHelper.GetText("没有满足该优惠券的指定商品,无法抵扣", order.LanguageCode)); return; } if (discountProductCount > coupon.DiscountRuleList[0].Quantity) { discountProductCount = coupon.DiscountRuleList[0].Quantity; } productReduce = coupon.DiscountRuleList[0].Value; if (coupon.DiscountRuleList[0].Value > productPrice) { productReduce = productPrice; } decimal discount = Math.Abs(Math.Round(productReduce * discountProductCount, 2)); if (coupon.SaleRulesEx.OrderMaxDiscount.HasValue && coupon.SaleRulesEx.OrderMaxDiscount.Value > 0m) { if (discount > coupon.SaleRulesEx.OrderMaxDiscount.Value) { discount = coupon.SaleRulesEx.OrderMaxDiscount.Value; } } order.CouponCodeSysNo = customerCoupon.CouponCodeSysNo; order.CouponSysNo = customerCoupon.CouponSysNo; order.CouponName = customerCoupon.CouponName; order.CouponAmount = discount; order.CouponErrorDesc = string.Empty; order["Coupon_DiscountProductSysNo"] = discountProductSysNo; order["Coupon_DiscountProductCount"] = discountProductCount; } #endregion #region 处理 一个商品最终售价模式 if (coupon.DiscountRuleList[0].DiscountType == CouponDiscountType.ProductPriceFinal) { if (coupon.SaleRulesList == null || coupon.SaleRulesList.Count == 0) { SetCouponError(ref order, LanguageHelper.GetText("没有满足该优惠券的指定商品,无法抵扣", order.LanguageCode)); return; } Coupon_SaleRules productSaleRule = coupon.SaleRulesList.Find(f => f.SaleRuleType == CouponSaleRuleType.RelProduct); int discountProductSysNo = productSaleRule.ProductSysNo; decimal discountProductPrice = 0.00m; int discountProductCount = 0; foreach (OrderItemGroup itemGroup in order.OrderItemGroupList) { OrderProductItem item = itemGroup.ProductItemList.Find(f => f.ProductSysNo == discountProductSysNo); if (item != null) { discountProductCount += item.UnitQuantity * itemGroup.Quantity; discountProductPrice = item.UnitSalePrice; } } if (discountProductCount == 0) { SetCouponError(ref order, LanguageHelper.GetText("没有满足该优惠券的指定商品,无法抵扣", order.LanguageCode)); return; } if (discountProductCount > coupon.DiscountRuleList[0].Quantity) { discountProductCount = coupon.DiscountRuleList[0].Quantity; } decimal discount = 0m; if (discountProductPrice > coupon.DiscountRuleList[0].Value) { discount = Math.Abs(Math.Round((discountProductPrice - coupon.DiscountRuleList[0].Value) * discountProductCount, 2)); } else { SetCouponError(ref order, LanguageHelper.GetText("该优惠券的指定商品已经是优惠券设定的最低价格,无法抵扣", order.LanguageCode)); return; } if (coupon.SaleRulesEx.OrderMaxDiscount.HasValue && coupon.SaleRulesEx.OrderMaxDiscount.Value > 0m) { if (discount > coupon.SaleRulesEx.OrderMaxDiscount.Value) { discount = coupon.SaleRulesEx.OrderMaxDiscount.Value; } } order.CouponCodeSysNo = customerCoupon.CouponCodeSysNo; order.CouponSysNo = customerCoupon.CouponSysNo; order.CouponName = customerCoupon.CouponName; order.CouponAmount = discount; order.CouponErrorDesc = string.Empty; order["Coupon_DiscountProductSysNo"] = discountProductSysNo; order["Coupon_DiscountProductCount"] = discountProductCount; } #endregion order["Coupon_DiscountType"] = coupon.DiscountRuleList[0].DiscountType; } #endregion }