Exemple #1
0
        /// <summary>
        /// 根据订单生成待结算项
        /// </summary>
        public static SettleAccountsDetails CreateSettleAccountDetails(ContextSession contextSession, CommodityOrder commodityOrder, MallApply mall, int?orderServiceState = null)
        {
            //LogHelper.Info("开始生成结算项,订单ID:" + commodityOrder.Id);

            var currentDate = DateTime.Now;

            // 1:商家,2:金和众销(给金和分的钱),3:商贸众销(给分享者分的钱),4:商贸众筹,5:推广主分成,6:应用主分成,7金和分润,8买家,9一级分销,10二级分销,11三级分销,12渠道推广,20:一级代理,21:二级代理
            //var payeeType = new List<int> { 3, 5, 9, 10, 11, 20, 21 };
            //var orderitemlist = OrderItem.ObjectSet().Where(n => n.CommodityOrderId == commodityOrder.Id).
            //    GroupJoin(OrderItemShare.ObjectSet().Where(s => payeeType.Contains(s.PayeeType)), o => o.Id, s => s.OrderItemId, (o, s) =>
            //        new { o.Id, o.Name, o.Number, o.RealPrice, o.CommodityId, o.ComCategoryId, Commission = s.Sum(_ => (decimal?)_.Commission) }).ToList();

            // 暂不计算推广佣金
            var orderitemlist = OrderItem.ObjectSet().Where(n => n.CommodityOrderId == commodityOrder.Id).Select(o =>
                                                                                                                 new { o.Id, o.Name, o.Number, o.RealPrice, o.CommodityId, o.ComCategoryId, o.CommodityAttributes }).ToList();
            bool isSetSettleAmount = true;
            List <SettleAccountsOrderItem> items = new List <SettleAccountsOrderItem>();
            var payTime = commodityOrder.PaymentTime.Value;

            foreach (var orderItem in orderitemlist)
            {
                // 结算单订单项
                SettleAccountsOrderItem saItem = new SettleAccountsOrderItem();
                saItem.Id                    = saItem.OrderItemId = orderItem.Id;
                saItem.ModifiedOn            = saItem.SubTime = currentDate;
                saItem.OrderId               = commodityOrder.Id;
                saItem.OrderItemRefundAmount = 0;               // 暂无单品退货
                saItem.OrderItemPromotionCommissionAmount = 0M; // orderItem.Commission ?? 0M;
                saItem.OrderItemName   = orderItem.Name;
                saItem.OrderItemNumber = orderItem.Number;
                saItem.OrderItemPrice  = orderItem.RealPrice.Value;
                // 自营他配
                if (mall.Type == 0)
                {
                    var commoditySettleAmount = CommoditySettleAmount.ObjectSet()
                                                .Where(c => c.CommodityId == orderItem.CommodityId && c.EffectiveTime < payTime)
                                                .OrderByDescending(c => c.SubTime)
                                                .ThenByDescending(c => c.EffectiveTime)
                                                .FirstOrDefault();
                    if (commoditySettleAmount == null)
                    {
                        // 未设置结算价,暂不结算
                        //LogHelper.Info("开始生成结算项,订单ID:" + commodityOrder.Id + ",自营商家,未设置结算价,商品ID:" + orderItem.CommodityId);
                        return(null);
                    }
                    //LogHelper.Info("开始生成结算项,订单ID:" + commodityOrder.Id + ",自营商家,商品ID:" + orderItem.CommodityId + ",商品属性:" + orderItem.CommodityAttributes + ",结算价:" + commoditySettleAmount.CommodityAttrJson);

                    var saAttrs = JsonHelper.JsonDeserialize <List <Jinher.AMP.BTP.Deploy.CustomDTO.CommodityAttributePrice> >(commoditySettleAmount.CommodityAttrJson);
                    var attrs   = (orderItem.CommodityAttributes ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if (attrs.Length == 0)
                    {
                        // 无属性
                        saItem.SettleAmount = saAttrs[0].SettlePrice.Value;
                    }
                    else if (attrs.Length == 1)
                    {
                        // 单属性
                        var settlePrice = saAttrs.Where(a => a.AttributeValue == attrs[0]).FirstOrDefault();
                        if (settlePrice == null)
                        {
                            LogHelper.Error("SettleAccountHelper.CreateSettleAccountDetails 生成结算单失败,结算价设置异常,商品ID:" + orderItem.CommodityId + ",商品属性:" + orderItem.CommodityAttributes + ",结算价ID:" + commoditySettleAmount.Id + ",结算价属性:" + commoditySettleAmount.CommodityAttrJson);
                            return(null);
                        }
                        saItem.SettleAmount = settlePrice.SettlePrice.Value;
                    }
                    else if (attrs.Length == 2)
                    {
                        // 双属性
                        var settlePrice = saAttrs.Where(a =>
                                                        (a.AttributeValue == attrs[0] && a.SecAttributeValue == attrs[1]) ||
                                                        (a.AttributeValue == attrs[1] && a.SecAttributeValue == attrs[0]))
                                          .FirstOrDefault();
                        if (settlePrice == null)
                        {
                            LogHelper.Error("SettleAccountHelper.CreateSettleAccountDetails 生成结算单失败,结算价设置异常,商品ID:" + orderItem.CommodityId + ",商品属性:" + orderItem.CommodityAttributes + ",结算价ID:" + commoditySettleAmount.Id + ",结算价属性:" + commoditySettleAmount.CommodityAttrJson);
                            return(null);
                        }
                        saItem.SettleAmount = settlePrice.SettlePrice.Value;
                    }
                    else
                    {
                        LogHelper.Error("SettleAccountHelper.CreateSettleAccountDetails 生成结算单失败,商品属性异常,商品ID:" + orderItem.CommodityId + ",商品属性:" + orderItem.CommodityAttributes);
                        return(null);
                    }
                    //orderItem.com
                }
                // 第三方
                else if (mall.Type == 1)
                {
                    // 查询商品易捷币抵用数量
                    var yjbInfo = YJBSV.GetOrderItemYJBInfo(commodityOrder.EsAppId.Value, commodityOrder.Id);
                    if (!yjbInfo.IsSuccess)
                    {
                        saItem.OrderItemYJBAmount = 0;
                    }
                    else
                    {
                        var currentCommodityYJBInfo = yjbInfo.Data.Items.Where(c => c.CommodityId == orderItem.CommodityId).FirstOrDefault();
                        if (currentCommodityYJBInfo != null && currentCommodityYJBInfo.IsMallYJB)
                        {
                            saItem.OrderItemYJBAmount = currentCommodityYJBInfo.InsteadCashAmount;
                        }
                    }

                    // 计算实际成交价



                    #region 计算商品佣金比例
                    // 获取佣金比例:按结算日期获取有效的(启用日期小于等于结算日期的最近一次设置的佣金比例)佣金比例【基础、类目、商品】;
                    saItem.BaseCommission = BaseCommission.ObjectSet().Join(MallApply.ObjectSet(),
                                                                            b => b.MallApplyId, m => m.Id, (b, m) => new { b.Commission, b.EffectiveTime, b.SubTime, m.AppId, m.EsAppId }).
                                            Where(t => t.EsAppId == commodityOrder.EsAppId && t.AppId == commodityOrder.AppId && t.EffectiveTime < payTime).
                                            OrderByDescending(t => t.SubTime).Select(t => (decimal?)t.Commission).FirstOrDefault();

                    // 查询当前商品的类目
                    List <Guid> categoryIds    = new List <Guid>();
                    var         tempCategories = CommodityCategory.ObjectSet().Where(c => c.AppId == commodityOrder.EsAppId && c.CommodityId == orderItem.CommodityId).Join(Category.ObjectSet(), cc => cc.CategoryId, c => c.Id, (cc, c) => new { c.Id, c.CurrentLevel, c.ParentId, cc.IsDel }).ToList();
                    foreach (var item in tempCategories)
                    {
                        if (item.IsDel.HasValue && item.IsDel.Value)
                        {
                            continue;
                        }
                        if (item.CurrentLevel == 3 || item.CurrentLevel == 4 || item.CurrentLevel == 5)
                        {
                            var lv2 = Category.FindByID(item.ParentId.Value);
                            if (item.CurrentLevel == 3)
                            {
                                categoryIds.Add(lv2.ParentId.Value);
                                continue;
                            }
                            var lv3 = Category.FindByID(lv2.ParentId.Value);
                            if (item.CurrentLevel == 4)
                            {
                                categoryIds.Add(lv3.ParentId.Value);
                                continue;
                            }
                            var lv4 = Category.FindByID(lv3.ParentId.Value);
                            categoryIds.Add(lv4.ParentId.Value);
                        }
                        else if (item.CurrentLevel == 2)
                        {
                            categoryIds.Add(item.ParentId.Value);
                        }
                        else if (item.CurrentLevel == 1)
                        {
                            categoryIds.Add(item.Id);
                        }
                    }

                    var commissions = CategoryCommission.ObjectSet().
                                      Join(MallApply.ObjectSet(), b => b.MallApplyId, m => m.Id,
                                           (b, m) => new { b.CategoryId, b.Commission, b.EffectiveTime, b.SubTime, m.AppId, m.EsAppId }).
                                      Where(t =>
                                            t.EsAppId == commodityOrder.EsAppId &&
                                            t.AppId == commodityOrder.AppId &&
                                            categoryIds.Contains(t.CategoryId) &&
                                            t.EffectiveTime < payTime).
                                      GroupBy(t => t.CategoryId).
                                      Select(group => group.OrderByDescending(g => g.SubTime).Select(g => g.Commission).FirstOrDefault()).
                                      ToList();
                    if (commissions.Count > 0)
                    {
                        saItem.CategoryCommission = commissions.Min();
                    }
                    saItem.CommodityCommission = CommodityCommission.ObjectSet().Join(MallApply.ObjectSet(),
                                                                                      b => b.MallApplyId, m => m.Id, (b, m) => new { b.CommodityId, b.Commission, b.EffectiveTime, b.SubTime, m.AppId, m.EsAppId }).
                                                 Where(t => t.EsAppId == commodityOrder.EsAppId && t.AppId == commodityOrder.AppId &&
                                                       t.CommodityId == orderItem.CommodityId && t.EffectiveTime < payTime).
                                                 OrderByDescending(t => t.SubTime).Select(t => (decimal?)t.Commission).FirstOrDefault();
                    #endregion

                    // 商城佣金计算公式:商品销售价*佣金比例【商品佣金优先,类目佣金其次、基础佣金最后,三选一计算】* 购买数量;
                    saItem.PromotionAmount = Math.Truncate(
                        (saItem.CommodityCommission.HasValue ? saItem.CommodityCommission.Value :
                         (saItem.CategoryCommission.HasValue ? saItem.CategoryCommission.Value :
                          (saItem.BaseCommission.HasValue ? saItem.BaseCommission.Value : 0)
                         )) * saItem.OrderItemPrice * saItem.OrderItemNumber) / 100;
                }

                saItem.EntityState = EntityState.Added;
                items.Add(saItem);
            }

            // 结算单订单详情
            SettleAccountsDetails sad = new SettleAccountsDetails();
            sad.Id                = sad.OrderId = commodityOrder.Id;
            sad.EntityState       = EntityState.Added;
            sad.ModifiedOn        = sad.SubTime = currentDate;
            sad.IsSettled         = false;
            sad.AppId             = commodityOrder.AppId;
            sad.EsAppId           = commodityOrder.EsAppId.Value;
            sad.OrderCode         = commodityOrder.Code;
            sad.OrderSubTime      = payTime;
            sad.OrderAmount       = commodityOrder.Price;
            sad.OrderRealAmount   = commodityOrder.RealPrice.Value;
            sad.OrderFreight      = commodityOrder.Freight;
            sad.IsSetSettleAmount = isSetSettleAmount;

            // 导入订单时,记录退款金额
            if (commodityOrder.State == 7 || orderServiceState == 7)
            {
                var refund = OrderRefundAfterSales.ObjectSet().Where(o => o.OrderId == commodityOrder.Id).FirstOrDefault();
                // 全额退款 不计入结算单
                if (refund.IsFullRefund == 1)
                {
                    sad.IsSettled = true;
                    if (contextSession != null)
                    {
                        foreach (var item in items)
                        {
                            contextSession.SaveObject(item);
                        }
                        contextSession.SaveObject(sad);
                    }
                    return(sad);
                }
                else
                {
                    sad.OrderRefundAmount = refund.RefundMoney;
                }
            }
            else
            {
                sad.OrderRefundAmount = 0; // 暂无单品退货
            }

            //// 计算商城优惠券金额
            //var orderP ayDetailId = OrderPayDetail.ObjectSet().Where(c => c.OrderId == commodityOrder.Id && c.ObjectType == 1).Select(c => c.ObjectId).FirstOrDefault();
            //if (orderPayDetailId != Guid.Empty)
            //{
            //    var request = new Coupon.Deploy.CustomDTO.ListCouponNewRequestDTO
            //    {
            //        CouponIds = new List<Guid> { orderPayDetailId },
            //        UserId = commodityOrder.UserId
            //    };
            //    var coupons = CouponSV.Instance.GetUserCouponsByIds(request);
            //    if (coupons.IsSuccess)
            //    {
            //        var firstCoupon = coupons.Data[0];

            //        sad.OrderCouponAmount = firstCoupon.Cash;
            //        // 判断是否为电商馆
            //        sad.IsMallCoupon = ZPHSV.Instance.IsAppPavilion(firstCoupon.ShopId);
            //    }
            //    else
            //    {
            //        LogHelper.Error(string.Format("CouponSV.GetUserCouponsByIds返回结果不成功,入参 CouponIds: {0} UserId: {1},出参 Code: {2} Info: {3}。", orderPayDetailId, commodityOrder.UserId, coupons.Code, coupons.Info));
            //    }
            //}

            // 计算商家结算金额

            decimal spreadMoney = 0;
            if (commodityOrder.SpreadGold > 0)
            {
                spreadMoney = commodityOrder.SpreadGold.ToMoney();
            }
            sad.OrderSpreadAmount = spreadMoney;

            // 1.第三方
            if (mall.Type == 1)
            {
                sad.OrderYJBAmount = items.Sum(i => i.OrderItemYJBAmount);

                // 商城佣金
                sad.PromotionAmount = items.Sum(i => i.PromotionAmount);

                // 优先级:1推广主、2三级分销、 3众销
                //sad.OrderPromotionCommissionAmount = commodityOrder.SpreadGold > 0 ? commodityOrder.SpreadGold.ToMoney() :
                //    commodityOrder.DistributeMoney > 0 ? commodityOrder.DistributeMoney : commodityOrder.Commission;
                // 老版本,已弃 商家结算金额 = 订单总额 + 商城优惠券总金额 - 退款总金额 - 推广佣金总额 - 商城佣金总额
                // 版本1:商家结算金额 =  实收款 +  商城易捷币抵用金额 - 退款金额  - 商城佣金
                // 版本2:商家结算金额 =  实收款 +  商城易捷币抵用金额 - 退款金额  - 商城佣金 - 推广佣金总额
                // 商家结算金额
                sad.SellerAmount = Math.Truncate((sad.OrderRealAmount + sad.OrderYJBAmount - sad.OrderRefundAmount - sad.PromotionAmount - sad.OrderSpreadAmount.Value) * 100) / 100;
            }
            // 0.自营他配
            else if (mall.Type == 0)
            {
                // 商品结算价
                decimal totalsSettlePrice = 0;
                foreach (var item in items)
                {
                    totalsSettlePrice += item.SettleAmount * item.OrderItemNumber;
                }
                sad.SettleAmount = totalsSettlePrice;
                // 商家结算金额 = 商品结算价*数量 + 运费 - 退款金额
                sad.SellerAmount = Math.Truncate((sad.SettleAmount + sad.OrderFreight - sad.OrderRefundAmount) * 100) / 100;
                // 商城佣金
                sad.PromotionAmount = Math.Truncate((sad.OrderRealAmount - sad.SellerAmount - sad.OrderSpreadAmount.Value) * 100) / 100;
            }
            // 2.自营自配自采
            // 3.自营自配统采
            if (contextSession != null)
            {
                foreach (var item in items)
                {
                    contextSession.SaveObject(item);
                }
                contextSession.SaveObject(sad);
            }
            return(sad);
        }
Exemple #2
0
        /// <summary>
        /// 查询商城信息
        /// </summary>
        /// <param name="search">查询类</param>
        /// <returns>结果</returns>
        public List <Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.MallApplyDTO> GetMallApplyInfoListExt(Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.MallApplyDTO search)
        {
            var searchlist = new List <Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.MallApplyDTO>();

            if (search.EsAppId == Guid.Empty && search.AppId == Guid.Empty)
            {
                return(searchlist);
            }
            var mallapplylistQuery = MallApply.ObjectSet().Where(m => m.State.Value < 5);

            if (!string.IsNullOrWhiteSpace(search.AppName))
            {
                mallapplylistQuery = mallapplylistQuery.Where(p => p.AppName.Contains(search.AppName));
            }
            if (search.UserId != Guid.Empty)
            {
                mallapplylistQuery = mallapplylistQuery.Where(p => p.UserId == search.UserId);
            }
            if (!string.IsNullOrWhiteSpace(search.AppId.ToString()) && (!search.AppId.ToString().Contains("00000000-0000-0000-0000-000000000000")))
            {
                mallapplylistQuery = mallapplylistQuery.Where(p => p.AppId == search.AppId);
            }
            if (!string.IsNullOrWhiteSpace(search.EsAppId.ToString()) && (!search.EsAppId.ToString().Contains("00000000-0000-0000-0000-000000000000")))
            {
                mallapplylistQuery = mallapplylistQuery.Where(p => p.EsAppId == search.EsAppId);
            }
            if (search.Type.HasValue)
            {
                mallapplylistQuery = mallapplylistQuery.Where(p => p.Type == search.Type);
            }
            var baseCommissionlist         = BaseCommission.ObjectSet().ToList();
            var commoditySettleAmountQuery = CommoditySettleAmount.ObjectSet().AsQueryable();

            foreach (var item in mallapplylistQuery.ToList())
            {
                var model = new Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.MallApplyDTO();
                model.Id           = item.Id;
                model.SubTime      = item.SubTime;
                model.ModifiedOn   = item.ModifiedOn;
                model.AppId        = item.AppId;
                model.EsAppId      = item.EsAppId;
                model.AppName      = item.AppName;
                model.EsAppName    = item.EsAppName;
                model.ApplyContent = item.ApplyContent;
                int State = Convert.ToInt32(item.State.Value);
                #region 获取基础佣金比例
                var baseCommission = baseCommissionlist
                                     .Where(p => p.MallApplyId == item.Id)
                                     .OrderByDescending(p => p.SubTime).FirstOrDefault();
                if (baseCommission != null)
                {
                    model.Commission = baseCommission.Commission;
                }
                #endregion
                model.State     = State;
                model.StateShow = new EnumHelper().GetDescription((MallApplyEnum)State);

                //model.StateShow = (State == 0
                //     ? new EnumHelper().GetDescription(MallApplyEnum.RZSQ)
                //        : State == 1
                //            ? new EnumHelper().GetDescription(MallApplyEnum.QXRZ)
                //              : State == 2
                //                 ? new EnumHelper().GetDescription(MallApplyEnum.TG)
                //                   : State == 3
                //                     ? new EnumHelper().GetDescription(MallApplyEnum.BTG)
                //                       : State == 4
                //                         ? new EnumHelper().GetDescription(MallApplyEnum.GQ)
                //                           : State == 5
                //                              ? new EnumHelper().GetDescription(MallApplyEnum.QXRZQR) : "");

                model.Type                = item.Type;
                model.TypeString          = item.GetTypeString();
                model.IsAllSetSettlePrice = true;
                if (model.Type == 0)
                {
                    // 查询是否有商品未设置结算价
                    if (Commodity.ObjectSet().Where(a => a.AppId == item.AppId && !a.IsDel).Count()
                        > commoditySettleAmountQuery.Where(b => b.AppId == item.AppId).GroupBy(c => c.CommodityId).Select(g => g.Key).Count())
                    {
                        model.IsAllSetSettlePrice = false;
                    }
                }
                searchlist.Add(model);
            }
            return(searchlist);
        }