/// <summary> /// 购物车转购物车明细简单对象(用于Json存储) /// </summary> /// <param name="shoppingCart">购物车</param> /// <returns>Json格式的购物车明细集合</returns> /// <remarks>2013-09-26 吴文强 创建</remarks> public static List <JsonCartItem> ConvertJson(this CrShoppingCart shoppingCart) { var jsonCartItems = new List <JsonCartItem>(); var items = shoppingCart.GetShoppingCartItem(); var promotionKeys = new List <string>(); foreach (var item in items) { if (item.IsLock == (int)CustomerStatus.购物车是否锁定.是 && !string.IsNullOrEmpty(item.GroupCode)) { var keys = string.Format("{0},{1}", item.Promotions, item.GroupCode); if (promotionKeys.Contains(keys)) { continue; } promotionKeys.Add(keys); } jsonCartItems.Add(new JsonCartItem() { IsChecked = item.IsChecked, GroupCode = item.GroupCode, IsLock = item.IsLock, ProductSysNo = item.ProductSysNo, Promotions = item.Promotions, Quantity = item.Quantity, ProductSalesType = item.ProductSalesType }); } return(jsonCartItems); }
/// <summary> /// 获取当前购物车有效可使用的优惠券 /// </summary> /// <param name="customerSysNo">客户系统编号</param> /// <param name="shoppingCart">购物车对象</param> /// <param name="platformType">优惠券使用平台类型</param> /// <returns>优惠券信息集合</returns> /// <remarks>2013-08-30 吴文强 创建</remarks> public IList <SpCoupon> GetCurrentCartValidCoupons(int customerSysNo, CrShoppingCart shoppingCart, PromotionStatus.促销使用平台[] platformType) { //获取未使用的优惠券 var coupons = GetCustomerCoupons(customerSysNo, PromotionStatus.优惠券状态.已审核, platformType); return(SpCouponEngineBo.Instance.CheckCoupon(customerSysNo, coupons, shoppingCart)); }
/// <summary> /// 购物车转购物车明细 /// </summary> /// <param name="shoppingCart">购物车</param> /// <param name="customerSysNo">客户系统编号</param> /// <returns>购物车明细</returns> /// <remarks>2013-09-26 吴文强 创建</remarks> public static List <CBCrShoppingCartItem> GetShoppingCartItem(this CrShoppingCart shoppingCart, int customerSysNo = -1) { var items = new List <CBCrShoppingCartItem>(); //读取赠品 if (shoppingCart.GroupPromotions != null) { foreach (var groupPromotion in shoppingCart.GroupPromotions) { items.AddRange(groupPromotion.UsedGiftProducts.ConvertShoppingCartItems(customerSysNo)); } } //购物车组 if (shoppingCart.ShoppingCartGroups != null) { foreach (var cartGroup in shoppingCart.ShoppingCartGroups) { //读取赠品 if (cartGroup.GroupPromotions != null) { foreach (var groupPromotion in cartGroup.GroupPromotions) { if (groupPromotion != null && groupPromotion.UsedGiftProducts != null) { items.AddRange(groupPromotion.UsedGiftProducts.ConvertShoppingCartItems(customerSysNo)); } } } items.AddRange(cartGroup.ShoppingCartItems); } } return(items); }
/// <summary> /// 计算购物车结算金额 /// </summary> /// <param name="shoppingCart">购物车对象</param> /// <param name="expensesAmount">太平洋保险</param> /// <returns>购物车对象</returns> /// <remarks>2013-09-03 吴文强 创建</remarks> private CrShoppingCart CalculateSettlementAmount(CrShoppingCart shoppingCart, decimal expensesAmount = 0M) { var productAmount = shoppingCart.ProductAmount - shoppingCart.ProductDiscountAmount - shoppingCart.SettlementDiscountAmount; var freightAmount = shoppingCart.FreightAmount - shoppingCart.FreightDiscountAmount; var couponAmount = shoppingCart.CouponAmount; //2016-4-22 王耀发 修改 var taxfee = shoppingCart.TaxFee; //if (taxfee <= 50) //{ // taxfee = 0; //} //商品金额>优惠券金额 if (productAmount > couponAmount) { //商品金额=商品金额-优惠券金额 productAmount = productAmount - couponAmount; } else { //优惠券金额=商品金额,商品金额=0 couponAmount = productAmount; productAmount = 0; } shoppingCart.CouponAmount = couponAmount; //结算金额=商品金额-优惠券金额+运费(优惠券不能抵消运费)+ 税费+太平洋保险 shoppingCart.SettlementAmount = productAmount + freightAmount + taxfee + expensesAmount; return(shoppingCart); }
/// <summary> /// 获取购物车中所有赠品 /// </summary> /// <param name="shoppingCart">购物车对象</param> /// <returns>赠品集合</returns> /// <remarks>2013-10-24 吴文强 创建</remarks> public static IList <CBSpPromotionGift> Gifts(this CrShoppingCart shoppingCart) { var promotionGifts = new List <CBSpPromotionGift>(); promotionGifts.AddRange(shoppingCart.ProductGroupGifts()); promotionGifts.AddRange(shoppingCart.OrderGroupGifts()); return(promotionGifts); }
/// <summary> /// 根据顾客ID和产品Id查询购物车 /// </summary> /// <param name="customerSysNo">客户SysNo.</param> /// <param name="productSysNo">产品SysNo.</param> /// <returns>购物车</returns> /// <exception cref="System.NotImplementedException"></exception> /// <remarks>2013-08-05 唐永勤 创建</remarks> public override CrShoppingCart GetShoppingCart(int customerSysNo, int productSysNo) { CrShoppingCart entity = Context.Select <CrShoppingCart>("*") .From("CrShoppingCart") .Where("CustomerSysno = @CustomerSysno and ProductSysNo = @ProductSysNo") .Parameter("CustomerSysno", customerSysNo) .Parameter("ProductSysNo", productSysNo) .QuerySingle(); return(entity); }
/// <summary> /// 计算运费 /// </summary> /// <param name="shoppingCart"></param> /// <param name="deliveryTypeSysNo"></param> /// <param name="areaSysNo"></param> /// <param name="warehouseSysNo"></param> /// <returns></returns> /// <remarks>2016-01-25 王耀发 创建</remarks> private CrShoppingCart CalculateFreigh(CrShoppingCart shoppingCart, int?deliveryTypeSysNo, int?areaSysNo, int?warehouseSysNo) { if (deliveryTypeSysNo != null && areaSysNo != null && warehouseSysNo != null) { //获得对应仓库明细 WhWarehouse wdata = Hyt.BLL.Warehouse.WhWarehouseBo.GetEntity((int)warehouseSysNo); //获取仓库物流关联详情 var warehouseDeliveryType = Hyt.BLL.Warehouse.WhWarehouseBo.Instance.GetWarehouseDeliveryType((int)warehouseSysNo, (int)deliveryTypeSysNo); //获得对应商品和数量 王耀发 2016-1-13 创建 string productSysNoAndNumber = ""; if (shoppingCart.ShoppingCartGroups != null) { foreach (var item in shoppingCart.ShoppingCartGroups) { foreach (var productItem in item.ShoppingCartItems) { if (productSysNoAndNumber == "") { productSysNoAndNumber = productItem.ProductSysNo.ToString() + '_' + productItem.Quantity.ToString(); } else { productSysNoAndNumber += "," + productItem.ProductSysNo.ToString() + '_' + productItem.Quantity.ToString(); } } } if (warehouseDeliveryType != null && productSysNoAndNumber != "") { var freight = Hyt.BLL.FreightModule.FreightModuleDaoBo.Instance.GetFareTotal((int)areaSysNo, (int)warehouseSysNo, productSysNoAndNumber, (int)deliveryTypeSysNo); if (freight == null) { shoppingCart.FreightAmount = 0; } else { shoppingCart.FreightAmount = (freight.Freigh == -1 ? 0 : freight.Freigh); } } else { shoppingCart.FreightAmount = 0; } } else { shoppingCart.FreightAmount = 0; } } else { shoppingCart.FreightAmount = 0; } return(shoppingCart); }
/// <summary> /// 移除购物车中下架的赠品 /// </summary> /// <param name="shoppingCart">购物车</param> /// <param name="isFrontProduct">是否只允许前台下单的商品(默认:true)</param> /// <returns>购物车</returns> /// <remarks>2013-12-24 吴文强 创建</remarks> public CrShoppingCart RemoveSoldoutGift(CrShoppingCart shoppingCart, bool isFrontProduct = true) { var allPromotionGifts = new List <CBSpPromotionGift>(); foreach (var shoppingCartGroup in shoppingCart.ShoppingCartGroups) { if (shoppingCartGroup.GroupPromotions == null) { continue; } foreach (var groupPromotion in shoppingCartGroup.GroupPromotions.Where(groupPromotion => groupPromotion.GiftProducts != null)) { allPromotionGifts.AddRange(groupPromotion.GiftProducts); } } if (shoppingCart.GroupPromotions != null) { foreach (var groupPromotion in shoppingCart.GroupPromotions.Where(groupPromotion => groupPromotion.GiftProducts != null)) { allPromotionGifts.AddRange(groupPromotion.GiftProducts); } } var giftProducts = PdProductBo.Instance.GetOnlineProduct(allPromotionGifts.Select(ci => ci.ProductSysNo).ToArray(), isFrontProduct); foreach (var shoppingCartGroup in shoppingCart.ShoppingCartGroups) { if (shoppingCartGroup.GroupPromotions == null) { continue; } foreach (var groupPromotion in shoppingCartGroup.GroupPromotions.Where(groupPromotion => groupPromotion.GiftProducts != null)) { groupPromotion.GiftProducts = groupPromotion.GiftProducts.Where(t => giftProducts.Contains(t.ProductSysNo)).ToList(); } } if (shoppingCart.GroupPromotions != null) { foreach (var groupPromotion in shoppingCart.GroupPromotions.Where(groupPromotion => groupPromotion.GiftProducts != null)) { groupPromotion.GiftProducts = groupPromotion.GiftProducts.Where(t => giftProducts.Contains(t.ProductSysNo)).ToList(); } } return(shoppingCart); }
/// <summary> /// 计算购物车 /// </summary> /// <param name="customer">客户对象</param> /// <param name="shoppingCart">购物车对象</param> /// <param name="cartGiftItems">赠品明细集合</param> /// <param name="promotionToPython">促销计算元数据</param> /// <returns>购物车对象</returns> /// <remarks>2013-09-03 吴文强 创建</remarks> private CrShoppingCart CalculateCart(CrCustomer customer, CrShoppingCart shoppingCart, IList <CBCrShoppingCartItem> cartGiftItems, SpPromotionToPython promotionToPython = null) { if (shoppingCart.AllPromotions == null) { return(shoppingCart); } //检查促销支持的商品 ScriptEngine engine = Python.CreateEngine(); ScriptScope scope = engine.CreateScope(); var currentPromotions = shoppingCart.AllPromotions.Where(p => p.PromotionType == (int)PromotionStatus.促销应用类型.应用到分类 || p.PromotionType == (int)PromotionStatus.促销应用类型.应用到商品 || p.PromotionType == (int)PromotionStatus.促销应用类型.团购 || p.PromotionType == (int)PromotionStatus.促销应用类型.组合套餐).OrderBy(p => p.PromotionType).ThenByDescending(p => p.Priority); foreach (var rule in currentPromotions) { ScriptSource sourceCode = engine.CreateScriptSourceFromString(rule.PromotionRule.RuleScript); scope.SetVariable("CurrSpPromotion", rule); scope.SetVariable("CurrCrCustomer", customer); scope.SetVariable("CurrAllPromotion", shoppingCart.AllPromotions); scope.SetVariable("CurrCartGiftItems", cartGiftItems); scope.SetVariable("CurrSpPromotionToPython", promotionToPython); //if (rule.PromotionType == (int)PromotionStatus.促销应用类型.团购) //{ // GsGroupShopping groupShopping= GroupShoppingBo.Instance.Get() // scope.SetVariable("CurrGroupShopping", ); //} //if (rule.PromotionType == (int)PromotionStatus.促销应用类型.组合套餐) //{ // scope.SetVariable("CurrCombo", cartGiftItems); //} sourceCode.Execute(scope); try { var pyCalculateCart = scope.GetVariable <Func <CrShoppingCart, CrShoppingCart> >("CalculateCart"); shoppingCart = pyCalculateCart(shoppingCart); } catch (Exception ex) { SysLog.Instance.Error(LogStatus.系统日志来源.后台, ex.Message, LogStatus.系统日志目标类型.促销, rule.SysNo, ex); } } return(shoppingCart); }
/// <summary> /// 订单的赠品(不包含商品组的赠品) /// </summary> /// <param name="shoppingCart">购物车对象</param> /// <returns>赠品集合</returns> /// <remarks>2013-11-29 吴文强 创建</remarks> public static IList <CBSpPromotionGift> OrderGroupGifts(this CrShoppingCart shoppingCart) { var promotionGifts = new List <CBSpPromotionGift>(); if (shoppingCart.GroupPromotions != null) { foreach ( var groupPromotion in shoppingCart.GroupPromotions.Where( groupPromotion => groupPromotion != null && groupPromotion.UsedGiftProducts != null)) { promotionGifts.AddRange(groupPromotion.UsedGiftProducts); } } return(promotionGifts); }
/// <summary> /// 计算税费 /// </summary> /// <param name="shoppingCart"></param> /// <param name="warehouseSysNo"></param> /// <returns></returns> /// <remarks>2016-01-25 王耀发 创建</remarks> private CrShoppingCart CalculateTaxFee(CrShoppingCart shoppingCart, int?warehouseSysNo) { decimal TaxFee = 0; if (warehouseSysNo != null && warehouseSysNo != 0) { //获得对应仓库明细 WhWarehouse wdata = Hyt.BLL.Warehouse.WhWarehouseBo.GetEntity((int)warehouseSysNo); //订单税费 王耀发 2016-1-13 创建 if (shoppingCart.ShoppingCartGroups != null) { foreach (var item in shoppingCart.ShoppingCartGroups) { foreach (var productItem in item.ShoppingCartItems) { //如果对应仓库类型为保税和直邮,计算税费 王耀发 2016-1-13 创建 if (wdata.WarehouseType == (int)Hyt.Model.WorkflowStatus.WarehouseStatus.仓库类型.保税 || wdata.WarehouseType == (int)Hyt.Model.WorkflowStatus.WarehouseStatus.仓库类型.直邮) { PdProduct Product = PdProductBo.Instance.GetProductBySysNo(productItem.ProductSysNo); if (Product != null) { if (Product.Tax != 0) { var SalesAmount = productItem.SalesUnitPrice * productItem.Quantity - productItem.DiscountAmount; //2016-4-22 王耀发 修改 TaxFee += (SalesAmount * (Product.Tax / 100)); } } } } //2016-4-22 王耀发 修改 if (TaxFee != 0) { TaxFee += (shoppingCart.FreightAmount * Convert.ToDecimal(0.119)); } } } } shoppingCart.TaxFee = TaxFee; return(shoppingCart); }
/// <summary> /// 商品组的赠品 /// </summary> /// <param name="shoppingCart">购物车对象</param> /// <returns>赠品集合</returns> /// <remarks>2013-11-29 吴文强 创建</remarks> public static IList <CBSpPromotionGift> ProductGroupGifts(this CrShoppingCart shoppingCart) { var promotionGifts = new List <CBSpPromotionGift>(); foreach (var shoppingCartGroup in shoppingCart.ShoppingCartGroups) { if (shoppingCartGroup.GroupPromotions == null) { continue; } foreach ( var groupPromotion in shoppingCartGroup.GroupPromotions.Where( groupPromotion => groupPromotion != null && groupPromotion.UsedGiftProducts != null)) { promotionGifts.AddRange(groupPromotion.UsedGiftProducts); } } return(promotionGifts); }
/// <summary> /// 计算组价格 /// </summary> /// <param name="shoppingCart">购物车对象</param> /// <returns>购物车对象</returns> /// <remarks>2013-09-03 吴文强 创建</remarks> private CrShoppingCart CalculateGroupPrice(CrShoppingCart shoppingCart) { foreach (var scg in shoppingCart.ShoppingCartGroups) { scg.DiscountAmount = scg.ShoppingCartItems.Where(item => item.IsChecked == (int)CustomerStatus.是否选中.是).Sum(a => a.DiscountAmount); scg.TotalAmount = scg.ShoppingCartItems.Where(item => item.IsChecked == (int)CustomerStatus.是否选中.是).Sum(a => a.SaleTotalAmount); //计算赠品加购价 var sumGpPurchasePrice = scg.GroupPromotions == null ? 0 : scg.GroupPromotions.Where(gp => gp != null && gp.UsedGiftProducts != null) .Sum(gp => gp.UsedGiftProducts.Sum(ugp => ugp.PurchasePrice)); scg.TotalAmount += sumGpPurchasePrice; } shoppingCart.ProductDiscountAmount = shoppingCart.ShoppingCartGroups.Sum(a => a.DiscountAmount); shoppingCart.ProductAmount = shoppingCart.ShoppingCartGroups.Sum(a => a.TotalAmount); //未使用促销的商品排在前面显示 shoppingCart.ShoppingCartGroups = shoppingCart.ShoppingCartGroups.OrderBy(scg => (scg.GroupPromotions ?? new List <CrShoppingCartGroupPromotion>()).Count()).ToList(); return(shoppingCart); }
/// <summary> /// 购物车商品数(不包含赠品) /// </summary> /// <param name="shoppingCart">购物车</param> /// <returns>购物车商品数</returns> /// <remarks>2013-10-16 吴文强 创建</remarks> public static int CartItemNumber(this CrShoppingCart shoppingCart) { return(shoppingCart.GetShoppingCartItem() .Where(t => t.ProductSalesType != (int)CustomerStatus.商品销售类型.赠品).Sum(t => t.Quantity)); }
/// <summary> /// 根据促销创建购物车商品组并构建购物车对象 /// </summary> /// <param name="promotions">有效促销集合</param> /// <param name="shoppingCartItems">购物车明细集合</param> /// <returns>购物车对象</returns> /// <remarks>2013-09-03 吴文强 创建</remarks> private CrShoppingCart CreateShoppingCart(List <CBSpPromotion> promotions, IList <CBCrShoppingCartItem> shoppingCartItems) { //构建购物车对象 var newShoppingCart = new CrShoppingCart(); newShoppingCart.ShoppingCartGroups = new List <CrShoppingCartGroup>(); //按照使用促销最多的商品倒序排列 var tempShoppingCarts = shoppingCartItems.OrderByDescending(sc => (sc.Promotions ?? string.Empty).Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Count()); var noPromotionsGroup = new CrShoppingCartGroup(); noPromotionsGroup.ShoppingCartItems = new List <CBCrShoppingCartItem>(); //遍历购物车商品 foreach (var shoppingCartItem in tempShoppingCarts) { if (string.IsNullOrEmpty(shoppingCartItem.Promotions)) { noPromotionsGroup.ShoppingCartItems.Add(shoppingCartItem); continue; } //当前商品可使用的促销 var scPromotions = Array.ConvertAll( (shoppingCartItem.Promotions ?? string.Empty).Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries), int.Parse); CrShoppingCartGroup scg = null; if (shoppingCartItem.IsLock == 1) { //查询已锁定的促销组 scg = newShoppingCart.ShoppingCartGroups.FirstOrDefault( g => g.IsLock && g.Promotions == shoppingCartItem.Promotions && g.GroupCode == shoppingCartItem.GroupCode); } else { //查询不为锁定的促销组 scg = newShoppingCart.ShoppingCartGroups.FirstOrDefault( g => !g.IsLock && g.GroupPromotions.Any( scgp => scPromotions.Any(s => s == scgp.PromotionSysNo))); } if (scg != null) { //存在促销组则添加当前商品到促销组 foreach (var s in scPromotions) { if (scg.GroupPromotions.FirstOrDefault(scgp => scgp.PromotionSysNo == s) == null) { var promotion = promotions.FirstOrDefault(p => p.SysNo == s); if (promotion != null) { scg.GroupPromotions.Add(new CrShoppingCartGroupPromotion() { PromotionSysNo = s, RuleType = promotion.PromotionRule.RuleType }); } } } scg.ShoppingCartItems.Add(shoppingCartItem); } else { //不存在促销组则新建促销组 newShoppingCart.ShoppingCartGroups.Add(new CrShoppingCartGroup() { IsLock = (shoppingCartItem.IsLock == (int)CustomerStatus.购物车是否锁定.是), Promotions = shoppingCartItem.Promotions, GroupCode = shoppingCartItem.GroupCode, ShoppingCartItems = new List <CBCrShoppingCartItem>() { shoppingCartItem }, GroupPromotions = scPromotions.Select(s => { var promotion = promotions.FirstOrDefault(p => p.SysNo == s); return(promotion != null ? new CrShoppingCartGroupPromotion() { PromotionSysNo = s, RuleType = promotion.PromotionRule.RuleType } : null); }).ToList() }); } } //如果无促销商品中存在数据,则将该组添加到购物车 if (noPromotionsGroup.ShoppingCartItems.Count > 0) { newShoppingCart.ShoppingCartGroups.Add(noPromotionsGroup); } return(newShoppingCart); }
/// <summary> /// 检查适合当前购物车的优惠券 /// </summary> /// <param name="customerSysNo">客户编号</param> /// <param name="coupons">优惠券集合</param> /// <param name="shoppingCart">购物车对象</param> /// <param name="isExpired">是否过期(是否忽略已使用和过期促销)</param> /// <returns>当前购物车可使用的优惠券集合</returns> /// <remarks>2013-08-31 吴文强 创建</remarks> public IList <SpCoupon> CheckCoupon(int customerSysNo, IList <SpCoupon> coupons, CrShoppingCart shoppingCart, bool isExpired = true) { //为空返回空对象 if (coupons == null || coupons.Count == 0 || shoppingCart == null) { return(new List <SpCoupon>()); } ScriptEngine engine = Python.CreateEngine(); ScriptScope scope = engine.CreateScope(); //客户信息 var customer = CrCustomerBo.Instance.GetCrCustomerItem(customerSysNo); //优惠券的促销信息 var promotions = SpPromotionBo.Instance.GetPromotions(coupons.Select(c => c.PromotionSysNo).ToArray()); IList <SpCoupon> validCoupon = new List <SpCoupon>(); foreach (var coupon in coupons) { var currPromotion = promotions.FirstOrDefault(p => p.SysNo == coupon.PromotionSysNo); if (currPromotion == null) { continue; //无效优惠券 } //是否过期=true:检查使用次数和过期时间;是否过期=false:忽略当前过期和已使用 if (isExpired == true) //忽略已使用和过期促销 { if (coupon.UsedQuantity >= coupon.UseQuantity) //优惠券 已使用数量>=使用数量 { if (coupon.CouponCode != shoppingCart.CouponCode) { continue; //(无效优惠券) 优惠券代码 != 购物车优惠券代码 } } if (coupon.StartTime > DateTime.Now) { continue; //(无效优惠券) 优惠券开始时间>当前时间 } if (DateTime.Now > coupon.EndTime) { continue; //(无效优惠券) 当前时间>优惠券结束时间 } } else //不忽略已使用和过期促销 { //不执行任何操作 } //修改下面代码的实现方式并加入新条件解决修改订单时优惠券丢失问题 by ywb 2014-01-03 //if ((currPromotion == null) // || (isExpired && (coupon.UseQuantity <= coupon.UsedQuantity || coupon.StartTime >= DateTime.Now || // coupon.EndTime < DateTime.Now))) //{ // continue; //} ScriptSource sourceCode = engine.CreateScriptSourceFromString(currPromotion.PromotionRule.RuleScript); scope.SetVariable("CurrSpPromotion", currPromotion); scope.SetVariable("CurrCrCustomer", customer); scope.SetVariable("CurrSpCoupon", coupon); sourceCode.Execute(scope); try { var pyCheckCoupon = scope.GetVariable <Func <CrShoppingCart, bool> >("CheckCoupon"); var result = pyCheckCoupon(shoppingCart); if (result) { validCoupon.Add(coupon); } } catch (Exception ex) { SysLog.Instance.Error(LogStatus.系统日志来源.后台, ex.Message, LogStatus.系统日志目标类型.促销, coupon.SysNo, ex); } } return(validCoupon); }
/// <summary> /// 购物车转销售单明细 /// </summary> /// <param name="cacheKey">缓存Key</param> /// <param name="orderSysNo">订单号</param> /// <param name="transactionSysNo">事物编号</param> /// <param name="shoppingCart">购物车</param> /// <returns>销售单明细集合</returns> /// <remarks>2013-09-10 吴文强 创建</remarks> public List <SoOrderItem> ShoppingCartToOrderItem(string cacheKey, int orderSysNo, string transactionSysNo, CrShoppingCart shoppingCart) { var orderItems = new List <SoOrderItem>(); var removeCartSysNo = new List <int>(); //获取购物车中所有赠品 var shoppingCartGiftItems = shoppingCart.GetShoppingCartItem().Where(sci => sci.ProductSalesType == (int)CustomerStatus.商品销售类型.赠品); var order = SoOrderBo.Instance.GetEntity(orderSysNo); var customer = CrCustomerBo.Instance.GetModel(order.CustomerSysNo); foreach (var cartGroup in shoppingCart.ShoppingCartGroups) { #region 添加商品 //添加商品 foreach (var cartItem in cartGroup.ShoppingCartItems) { var groupName = string.Empty; var usedPromotions = string.Empty; if (cartItem.IsLock == (int)CustomerStatus.购物车是否锁定.是) { groupName = cartGroup.GroupPromotions != null && cartGroup.GroupPromotions.Count > 0 ? cartGroup.GroupPromotions[0].Description : string.Empty; usedPromotions = cartGroup.Promotions; } else { if (cartItem.Promotions != null) { var proms = cartItem.Promotions.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries) .Select(t => Convert.ToInt32(t)); if (cartGroup.GroupPromotions != null) { usedPromotions = string.Join(";", cartGroup.GroupPromotions.Where( t => proms.Contains(t.PromotionSysNo) && t.IsUsed) .Select(s => s.PromotionSysNo.ToString())); } } } orderItems.Add(new SoOrderItem() { OrderSysNo = orderSysNo, TransactionSysNo = transactionSysNo, ProductSysNo = cartItem.ProductSysNo, ProductName = cartItem.ProductName, Quantity = cartItem.Quantity, OriginalPrice = cartItem.OriginPrice, SalesUnitPrice = cartItem.SalesUnitPrice, SalesAmount = cartItem.SaleTotalAmount, DiscountAmount = cartItem.DiscountAmount, ChangeAmount = 0, RealStockOutQuantity = 0, ProductSalesType = cartItem.ProductSalesType, ProductSalesTypeSysNo = cartItem.ProductSalesTypeSysNo, GroupCode = cartItem.GroupCode, GroupName = groupName, UsedPromotions = usedPromotions//UsedPromotions = cartItem.UsedPromotions }); //需要从购物车中移除的系统编号 removeCartSysNo.Add(cartItem.SysNo); } #endregion #region 添加商品组赠品 //添加赠品 if (cartGroup.GroupPromotions != null) { foreach (var groupPromotion in cartGroup.GroupPromotions) { if (groupPromotion.UsedGiftProducts == null) { continue; } foreach (var giftProduct in groupPromotion.UsedGiftProducts) { var product = PdProductBo.Instance.GetProduct(giftProduct.ProductSysNo); if (product == null) { continue; } //从购物车获取数据 orderItems.Add(new SoOrderItem() { OrderSysNo = orderSysNo, TransactionSysNo = transactionSysNo, ProductSysNo = giftProduct.ProductSysNo, ProductName = giftProduct.ProductName, Quantity = 1, OriginalPrice = PdPriceBo.Instance.GetUserRankPrice(giftProduct.ProductSysNo, customer.LevelSysNo), SalesUnitPrice = giftProduct.PurchasePrice, SalesAmount = giftProduct.PurchasePrice, DiscountAmount = 0, ChangeAmount = 0, RealStockOutQuantity = 0, ProductSalesType = (int)CustomerStatus.商品销售类型.赠品, ProductSalesTypeSysNo = giftProduct.SysNo, GroupCode = string.Empty, GroupName = string.Empty, UsedPromotions = giftProduct.PromotionSysNo.ToString() }); //需要从购物车中移除的系统编号 removeCartSysNo.Add(giftProduct.ProductSysNo); } } } #endregion } #region 添加订单赠品 //添加赠品 if (shoppingCart.GroupPromotions != null) { foreach (var groupPromotion in shoppingCart.GroupPromotions) { if (groupPromotion.UsedGiftProducts == null) { continue; } foreach (var giftProduct in groupPromotion.UsedGiftProducts) { var product = PdProductBo.Instance.GetProduct(giftProduct.ProductSysNo); if (product == null) { continue; } //从购物车获取数据 orderItems.Add(new SoOrderItem() { OrderSysNo = orderSysNo, TransactionSysNo = transactionSysNo, ProductSysNo = giftProduct.ProductSysNo, ProductName = giftProduct.ProductName, Quantity = 1, OriginalPrice = PdPriceBo.Instance.GetUserRankPrice(giftProduct.ProductSysNo, customer.LevelSysNo), SalesUnitPrice = giftProduct.PurchasePrice, SalesAmount = giftProduct.PurchasePrice, DiscountAmount = 0, ChangeAmount = 0, RealStockOutQuantity = 0, ProductSalesType = (int)CustomerStatus.商品销售类型.赠品, ProductSalesTypeSysNo = giftProduct.SysNo, GroupCode = string.Empty, GroupName = string.Empty, UsedPromotions = giftProduct.PromotionSysNo.ToString() }); //需要从购物车中移除的系统编号 removeCartSysNo.Add(giftProduct.ProductSysNo); } } } #endregion return(orderItems); }
/// <summary> /// 计算订单 /// </summary> /// <param name="customer">客户对象</param> /// <param name="shoppingCart">购物车对象</param> /// <param name="cartGiftItems">赠品明细集合</param> /// <param name="promotionToPython">促销计算元数据</param> /// <returns>购物车对象</returns> /// <remarks>2013-09-03 吴文强 创建</remarks> private CrShoppingCart CalculateOrder(CrCustomer customer, CrShoppingCart shoppingCart, IList <CBCrShoppingCartItem> cartGiftItems, SpPromotionToPython promotionToPython = null) { if (shoppingCart.AllPromotions == null) { return(shoppingCart); } //检查促销支持的商品 ScriptEngine engine = Python.CreateEngine(); ScriptScope scope = engine.CreateScope(); var currentPromotions = shoppingCart.AllPromotions.Where(p => p.PromotionType == (int)PromotionStatus.促销应用类型.应用到商品合计 || p.PromotionType == (int)PromotionStatus.促销应用类型.应用到订单合计 || p.PromotionType == (int)PromotionStatus.促销应用类型.应用到运费).OrderBy(p => p.PromotionType).ThenByDescending(p => p.Priority); //检查支持的促销 foreach (var rule in currentPromotions) { ScriptSource sourceCode = engine.CreateScriptSourceFromString(rule.PromotionRule.RuleScript); scope.SetVariable("CurrSpPromotion", rule); scope.SetVariable("CurrCrCustomer", customer); scope.SetVariable("CurrAllPromotion", shoppingCart.AllPromotions); scope.SetVariable("CurrCartGiftItems", cartGiftItems); scope.SetVariable("CurrSpPromotionToPython", promotionToPython); sourceCode.Execute(scope); try { var pyCalculateCart = scope.GetVariable <Func <CrShoppingCart, CrShoppingCart> >("CheckPromotionProduct"); shoppingCart = pyCalculateCart(shoppingCart); } catch (Exception ex) { SysLog.Instance.Error(LogStatus.系统日志来源.后台, ex.Message, LogStatus.系统日志目标类型.促销, rule.SysNo, ex); } } //计算促销 foreach (var rule in currentPromotions) { ScriptSource sourceCode = engine.CreateScriptSourceFromString(rule.PromotionRule.RuleScript); scope.SetVariable("CurrSpPromotion", rule); scope.SetVariable("CurrCrCustomer", customer); scope.SetVariable("CurrAllPromotion", shoppingCart.AllPromotions); scope.SetVariable("CurrSpPromotionToPython", promotionToPython); sourceCode.Execute(scope); try { var pyCalculateCart = scope.GetVariable <Func <CrShoppingCart, CrShoppingCart> >("CalculateCart"); shoppingCart = pyCalculateCart(shoppingCart); } catch (Exception ex) { SysLog.Instance.Error(LogStatus.系统日志来源.后台, ex.Message, LogStatus.系统日志目标类型.促销, rule.SysNo, ex); } } #region 2013-12-31 吴文强 添加赠品加购计算 //计算赠品加购价 var sumGpPurchasePrice = shoppingCart.GroupPromotions == null ? 0 : shoppingCart.GroupPromotions.Where(gp => gp.UsedGiftProducts != null) .Sum(gp => gp.UsedGiftProducts.Sum(ugp => ugp.PurchasePrice)); //2013-12-31 吴文强 计算订单赠品加购价添加至商品合计金额中(促销商品合计需排除加购价的商品金额) //计算订单赠品加购价 shoppingCart.ProductAmount += sumGpPurchasePrice; //计算购物车结算金额 shoppingCart = CalculateSettlementAmount(shoppingCart); #endregion return(shoppingCart); }
/// <summary> /// 补单(订单-出库单-配送单-结算单) /// </summary> /// <param name="deliverUserSysNo">配送员系统编号</param> /// <param name="customerSysNo">顾客系统编号</param> /// <param name="receiveAddress">收货地址</param> /// <param name="defaultWarehouseSysNo">仓库系统编号</param> /// <param name="shoppingCart">购物车</param> /// <param name="user">登录用户信息</param> /// <returns>补单完成情况</returns> /// <remarks> /// 2013-09-25 郑荣华 创建 /// 2014-04-21 何方 不再使用补单功能 /// </remarks> public Result RemedyOrder(int deliverUserSysNo, int customerSysNo, SoReceiveAddress receiveAddress, int defaultWarehouseSysNo, CrShoppingCart shoppingCart, SyUser user) { var result = new Result() { Message = "开始补单", Status = false, StatusCode = 0 }; var deliveryTypeSysNo = DeliveryType.百城当日达; try { //创建订单 var order = SoOrderBo.Instance.CreateOrder(user.SysNo, customerSysNo, receiveAddress, defaultWarehouseSysNo, deliveryTypeSysNo, PaymentType.现金, shoppingCart, 0, null, OrderStatus.销售单来源.业务员补单, null, OrderStatus.销售方式.售后订单, null, OrderStatus.销售单对用户隐藏.否, "", "补单", "补单", "补单任意时间段", OrderStatus.配送前是否联系.否, "补单"); result.Message = "订单创建完成"; //修改订单状态 order.Status = (int)OrderStatus.销售单状态.待创建出库单; SoOrderBo.Instance.UpdateOrder(order); //更新订单 余勇修改为调用业务层方法 ISoOrderDao.Instance.Update(order); result.Message = "订单状态修改完成"; //获取订单明细 order.OrderItemList = SoOrderBo.Instance.GetOrderItemsByOrderId(order.SysNo); if (order.OrderItemList != null) { foreach (var oo in order.OrderItemList) { oo.RealStockOutQuantity = oo.Quantity; } } //创建出库单 var stockout = SoOrderBo.Instance.CreateOutStock(order.OrderItemList, defaultWarehouseSysNo, user); result.Message = "出库单创建完成"; //更新出库单状态处于待配送状态 WhWarehouseBo.Instance.UpdateOrderStockOutStatus(order.SysNo, WarehouseStatus.出库单状态.待配送, user.SysNo); //创建配送单+结算单 var item = new LgDeliveryItem() { NoteType = (int)LogisticsStatus.配送单据类型.出库单, NoteSysNo = stockout.SysNo, ExpressNo = "" }; var list = new List <LgDeliveryItem> { item }; //配送方式 var delivertType = DeliveryTypeBo.Instance.GetDeliveryType(deliveryTypeSysNo); LgDeliveryBo.Instance.CreateLgDelivery(defaultWarehouseSysNo, deliverUserSysNo, delivertType, user.SysNo, list, true); //新增会员明细 2014-1-17 黄志勇 添加 LgSettlementBo.Instance.WriteShopNewCustomerDetail(order.CustomerSysNo, stockout.StockOutAmount); result.Status = true; result.StatusCode = 1; result.Message = "补单操作完成"; return(result); } catch (Exception ex) { Log.SysLog.Instance.Error(LogStatus.系统日志来源.后台, result.Message, LogStatus.系统日志目标类型.补单, 0, ex); return(result); } }