Пример #1
0
 /// <summary>
 /// 增加dbo.RMA及dbo.RMAItems
 /// </summary>
 /// <param name="config"></param>
 private void CreateRma(RmaConfig config)
 {
     using (var db = new YintaiHZhouContext())
     {
         db.RMAs.Add(new RMA
         {
             CreateDate    = DateTime.Now,
             RMAReason     = 10, //默认是其他原因
             Reason        = config.Reason,
             Status        = 2,  //退货审核通过,客服发起的默认是审核通过的
             OrderNo       = config.OpcSale.OrderNo,
             CreateUser    = config.UserId,
             RMAAmount     = config.Details.Sum(x => x.ReturnCount * x.OrderItem.ItemPrice),
             UpdateDate    = DateTime.Now,
             UpdateUser    = config.UserId,
             RMAType       = 2,//2为线下退货
             ContactPerson = string.Empty,
             ContactPhone  = string.Empty,
             RMANo         = config.RmaNo
         });
         foreach (var detail in config.Details)
         {
             db.RMAItems.Add(new RMAItem()
             {
                 RMANo          = config.RmaNo,
                 ItemPrice      = detail.OrderItem.ItemPrice,
                 ExtendPrice    = detail.OrderItem.ItemPrice * detail.ReturnCount,
                 ProductDesc    = detail.OrderItem.ProductDesc,
                 ProductId      = detail.OrderItem.ProductId,
                 ColorId        = detail.OrderItem.ColorId,
                 SizeId         = detail.OrderItem.SizeId,
                 Quantity       = detail.ReturnCount,
                 ColorValueId   = detail.OrderItem.ColorValueId,
                 SizeValueId    = detail.OrderItem.SizeValueId,
                 ColorValueName = detail.OrderItem.ColorValueName,
                 SizeValueName  = detail.OrderItem.SizeValueName,
                 UpdateDate     = DateTime.Now,
                 StoreItem      = detail.OrderItem.StoreItemNo,
                 StoreDesc      = detail.OrderItem.StoreItemDesc,
                 BrandId        = detail.OrderItem.BrandId,
                 CreateDate     = DateTime.Now,
                 UnitPrice      = detail.OrderItem.UnitPrice
             });
         }
         db.SaveChanges();
     }
 }
Пример #2
0
        private void CreateSaleRmaSub(int userId, RMARequest rma, string saleSource)
        {
            if (string.Empty == rma.Remark)
            {
                throw new RmaException(string.Format("没有退货备注,订单明细号:{0}", rma.OrderNo));
            }

            List <OPC_SaleDetail> saleDetails =
                _saleDetailRepository.GetByOrderNo(rma.OrderNo, 1, 1000).Result.Where(t => !t.BackNumber.HasValue || t.BackNumber.Value < t.SaleCount).OrderByDescending(t => t.SaleCount).ToList();
            List <OPC_Sale> sales =
                _saleRepository.GetByOrderNo(rma.OrderNo, -1).OrderByDescending(t => t.SalesCount).ToList();
            IList <OrderItem> orderItems =
                _orderItemRepository.GetByIDs(rma.ReturnProducts.Select(t => t.Key));

            using (var db = new YintaiHZhouContext())
                using (var ts = new TransactionScope())
                {
                    IList <RmaConfig> rmaInfos = new List <RmaConfig>();
                    foreach (var kv in rma.ReturnProducts)
                    {
                        var orderItemId = kv.Key;
                        var rmaCount    = kv.Value;
                        var item        = db.Set <OrderItem>().FirstOrDefault(x => x.Id == orderItemId);
                        if (item == null)
                        {
                            throw new RmaException(string.Format("无效的商品明细记录({0})或商品ID({1})", kv.Key, kv.Value));
                        }

                        var items = from i in db.Set <RMAItem>()
                                    from r in db.Set <RMA>()
                                    from o in db.Set <Order>()
                                    where
                                    i.RMANo == r.RMANo &&
                                    r.OrderNo == o.OrderNo &&
                                    i.ProductId == item.ProductId &&
                                    o.OrderNo == rma.OrderNo &&
                                    !InvalidRmaStatus.Contains(r.Status)

                                    select i;

                        int countOfItems = items.Any() ? items.Sum(i => i.Quantity) : 0;

                        if (item.Quantity - countOfItems < rmaCount)
                        {
                            throw new RmaException("退货数量大于可退货数量");
                        }

                        OrderItem oItem = orderItems.FirstOrDefault(t => t.Id == kv.Key);

                        List <OPC_SaleDetail> details =
                            saleDetails.Where(t => t.OrderItemId == kv.Key).OrderByDescending(t => t.SaleCount).ToList();
                        int            returnCount = kv.Value;
                        OPC_SaleDetail detail      = details.FirstOrDefault();
                        if (detail == null)
                        {
                            //没有销售明细
                            throw new RmaException(string.Format("没有销售明细不存在,订单明细号:{0}", kv.Key));
                        }

                        while (detail != null && returnCount > CalculateRmaCount(detail))
                        {
                            RmaConfig opcRma = rmaInfos.FirstOrDefault(t => t.SaleOrderNo == detail.SaleOrderNo);
                            if (opcRma == null)
                            {
                                opcRma = new RmaConfig(userId)
                                {
                                    SaleRmaSource = saleSource,
                                    RmaNo         = CreateRmaNo(),
                                    SaleOrderNo   = detail.SaleOrderNo,
                                    RefundAmount  = rma.RealRMASumMoney,
                                    StoreFee      = rma.StoreFee,
                                    CustomFee     = rma.CustomFee
                                };
                                rma.RealRMASumMoney = 0;
                                rma.StoreFee        = 0;
                                rma.CustomFee       = 0;

                                opcRma.OpcSale = sales.FirstOrDefault(t => t.SaleOrderNo == opcRma.SaleOrderNo);
                                opcRma.StoreID = _sectionRepository.GetByID(opcRma.OpcSale.SectionId.Value).StoreId.Value;
                                rmaInfos.Add(opcRma);
                            }

                            int countCanRMA = CalculateRmaCount(detail);

                            var opcRmaDetail = new SubRmaConfig
                            {
                                OpcSaleDetail = detail,
                                OrderItem     = oItem,
                                OrderDetailId = kv.Key,
                                ReturnCount   = countCanRMA
                            };
                            opcRma.Details.Add(opcRmaDetail);
                            rmaInfos.Add(opcRma);

                            //更新退货数量
                            UpdateSaleDetailOfBackNum(detail, countCanRMA, db);

                            returnCount = returnCount - countCanRMA;
                            details.Remove(detail);
                            detail = details.FirstOrDefault();
                        }

                        if (returnCount > 0)
                        {
                            RmaConfig config = rmaInfos.FirstOrDefault(t => t.SaleOrderNo == detail.SaleOrderNo);
                            if (config == null)
                            {
                                config = new RmaConfig(userId)
                                {
                                    SaleRmaSource = saleSource,
                                    Reason        = rma.Remark,
                                    SaleOrderNo   = detail.SaleOrderNo,
                                    RefundAmount  = rma.RealRMASumMoney,
                                    StoreFee      = rma.StoreFee,
                                    CustomFee     = rma.CustomFee
                                };

                                config.OpcSale = sales.FirstOrDefault(t => t.SaleOrderNo == config.SaleOrderNo);
                                config.StoreID = _sectionRepository.GetByID(config.OpcSale.SectionId.Value).StoreId.Value;
                                config.RmaNo   = CreateRmaNo();
                                rmaInfos.Add(config);
                            }
                            var subConfig = new SubRmaConfig
                            {
                                OpcSaleDetail = detail,
                                OrderItem     = oItem,
                                OrderDetailId = kv.Key,
                                ReturnCount   = returnCount
                            };
                            config.Details.Add(subConfig);
                            //更新退货数量
                            UpdateSaleDetailOfBackNum(detail, returnCount, db);
                        }
                    }
                    Save(rmaInfos);
                    ts.Complete();
                }
        }