示例#1
0
 private static Parameter[] GetDebitNoteDetailParameters(DebitNoteDetailInfo info)
 {
     return(new[]
     {
         new Parameter(PARM_PURCHASINGID, info.PurchasingId),
         new Parameter(PARM_GOODSID, info.GoodsId),
         new Parameter(PARM_GOODSNAME, info.GoodsName),
         new Parameter(PARM_SPECIFICATION, info.Specification),
         new Parameter(PARM_GIVINGCOUNT, info.GivingCount),
         new Parameter(PARM_ARRIVALCOUNT, info.ArrivalCount),
         new Parameter(PARM_PRICE, info.Price),
         new Parameter(PARM_STATE, info.State),
         new Parameter(PARM_AMOUNT, info.Amount),
         new Parameter(PARM_MEMO, info.Memo),
         new Parameter(PARM_ID, info.Id)
     });
 }
示例#2
0
        private DebitNoteDetailInfo ReaderDebitNoteDetail(IDataReader dr)
        {
            var info = new DebitNoteDetailInfo
            {
                PurchasingId  = dr["PurchasingId"] == DBNull.Value ? Guid.Empty : new Guid(dr["PurchasingId"].ToString()),
                GoodsId       = dr["GoodsId"] == DBNull.Value ? Guid.Empty : new Guid(dr["GoodsId"].ToString()),
                GoodsName     = dr["GoodsName"] == DBNull.Value ? string.Empty : dr["GoodsName"].ToString(),
                Specification = dr["Specification"] == DBNull.Value ? string.Empty : dr["Specification"].ToString(),
                GivingCount   = dr["GivingCount"] == DBNull.Value ? 0 : int.Parse(dr["GivingCount"].ToString()),
                ArrivalCount  = dr["ArrivalCount"] == DBNull.Value ? 0 : int.Parse(dr["ArrivalCount"].ToString()),
                Price         = dr["Price"] == DBNull.Value ? 0 : decimal.Parse(dr["Price"].ToString()),
                State         = dr["State"] == DBNull.Value ? 0 : int.Parse(dr["State"].ToString()),
                Amount        = dr["Amount"] == DBNull.Value ? 0 : decimal.Parse(dr["Amount"].ToString()),
                Memo          = dr["Memo"] == DBNull.Value ? string.Empty : dr["Memo"].ToString(),
                Id            = dr["ID"] == DBNull.Value ? Guid.Empty : new Guid(dr["ID"].ToString())
            };

            return(info);
        }
示例#3
0
        /// <summary>
        /// 添加借记单明细
        /// </summary>
        /// <param name="detailInfo"></param>
        public static void AddDebitNoteDetail(DebitNoteDetailInfo detailInfo)
        {
            const string SQL_INSERT_DEBITNOTEDETAIL = @"
IF EXISTS(SELECT 0 FROM [lmshop_DebitNoteDetail] WHERE [PurchasingId]=@PurchasingId AND [GoodsId]=@GoodsId)
BEGIN
	UPDATE [lmshop_DebitNoteDetail] SET [GivingCount]+=@GivingCount WHERE [PurchasingId]=@PurchasingId AND [GoodsId]=@GoodsId
END
ELSE
BEGIN
	INSERT INTO [lmshop_DebitNoteDetail]([PurchasingId],[GoodsId],[GoodsName],[Specification],[GivingCount],[ArrivalCount],[Price],[State],[Amount],[Memo],[ID]) VALUES(@PurchasingId,@GoodsId,@GoodsName,@Specification,@GivingCount,@ArrivalCount,@Price,@State,@Amount,@Memo,@ID)
END
";
            var          detailParms = GetDebitNoteDetailParameters(detailInfo);

            using (var db = DatabaseFactory.Create())
            {
                db.Execute(false, SQL_INSERT_DEBITNOTEDETAIL, detailParms);
            }
        }
示例#4
0
 /// <summary>
 /// 添加借记单明细
 /// </summary>
 /// <param name="detailInfo"></param>
 public void AddDebitNoteDetail(DebitNoteDetailInfo detailInfo)
 {
     SqlParameter[] detailParms = GetDebitNoteDetailParameters();
     try
     {
         detailParms[0].Value  = detailInfo.PurchasingId;
         detailParms[1].Value  = detailInfo.GoodsId;
         detailParms[2].Value  = detailInfo.GoodsName;
         detailParms[3].Value  = detailInfo.Specification;
         detailParms[4].Value  = detailInfo.GivingCount;
         detailParms[5].Value  = detailInfo.ArrivalCount;
         detailParms[6].Value  = detailInfo.Price;
         detailParms[7].Value  = detailInfo.State;
         detailParms[8].Value  = detailInfo.Amount;
         detailParms[9].Value  = detailInfo.Memo;
         detailParms[10].Value = detailInfo.Id;
         SqlHelper.ExecuteNonQuery(GlobalConfig.ERP_DB_NAME, false, SQL_INSERT_DEBITNOTEDETAIL, detailParms);
     }
     catch (SqlException ex)
     {
         throw new ApplicationException(ex.Message);
     }
 }
示例#5
0
        private static void PurchasingPromotion(Dictionary <PurchasingInfo, List <PurchasingDetailInfo> > dictPurchase, Dictionary <Guid, GoodsInfo> dicGoods, IList <PurchasingGoods> pg)
        {
            if (dictPurchase.Count > 0)
            {
                //IList<PurchaseSetInfo> purchaseSetList = _purchaseSetBll.GetPurchaseSetList();
                foreach (KeyValuePair <PurchasingInfo, List <PurchasingDetailInfo> > keyValuePair in dictPurchase)
                {
                    keyValuePair.Key.PurchasingNo = DataAccessor.GetCode(BaseInfo.CodeType.PH);
                    var pInfo = keyValuePair.Key;
                    IList <PurchasingDetailInfo> plist = keyValuePair.Value;
                    //pInfo.IsOut = true;
                    _purchasing.PurchasingInsert(pInfo);

                    //非赠品采购商品
                    IList <PurchasingDetailInfo> plist2 = plist.Where(w => w.PurchasingGoodsType != (int)PurchasingGoodsType.Gift).ToList();

                    #region [现返]
                    //如果赠送方式为总数量赠送时使用  key 主商品ID  value 额外赠送
                    var dics           = new Dictionary <string, int>();    //需赠送
                    var debitExtraDics = new Dictionary <string, int>();    //借计单已送
                    var addDetailsList = new List <PurchasingDetailInfo>(); //添加赠品
                                                                            //处理原理:
                                                                            //赠品数量=原采购数量/(买几个+送几个)*送几个
                                                                            //购买数量=原采购数量-赠品数量
                    foreach (var pdInfo in plist2)
                    {
                        var goodsBaseInfo = new GoodsInfo();
                        if (dicGoods != null)
                        {
                            bool hasKey = dicGoods.ContainsKey(pdInfo.GoodsID);
                            if (hasKey)
                            {
                                goodsBaseInfo = dicGoods.FirstOrDefault(w => w.Key == pdInfo.GoodsID).Value;
                            }
                        }
                        PurchasingGoods purchaseSetInfo = pg.FirstOrDefault(w => w.GoodsId == goodsBaseInfo.GoodsId && w.WarehouseId == pInfo.WarehouseID);
                        if (purchaseSetInfo == null)
                        {
                            continue;
                        }
                        IList <PurchasePromotionInfo> ppList = _purchasePromotionBll.GetPurchasePromotionList(purchaseSetInfo.PromotionId, purchaseSetInfo.GoodsId, purchaseSetInfo.WarehouseId, pInfo.PurchasingFilialeId, (int)PurchasePromotionType.Back);
                        PurchasePromotionInfo         ppInfo = ppList.FirstOrDefault(w => w.GivingCount > 0 && w.StartDate <= DateTime.Now && w.EndDate >= DateTime.Now);
                        if (ppInfo != null)
                        {
                            #region  新增
                            if (!ppInfo.IsSingle)  //按商品总数量进行赠送
                            {
                                if (!dics.ContainsKey(goodsBaseInfo.GoodsName))
                                {
                                    var dataList = plist2.Where(act => act.GoodsName == goodsBaseInfo.GoodsName).OrderByDescending(act => act.PlanQuantity).ToList();
                                    //余数
                                    var extra = dataList.Sum(act => Convert.ToInt32(act.PlanQuantity)) % (ppInfo.BuyCount + ppInfo.GivingCount);
                                    if (extra == ppInfo.BuyCount)
                                    {
                                        dataList[0].PlanQuantity += ppInfo.GivingCount;
                                    }
                                    else
                                    {
                                        //应赠送次数
                                        int actquantity = extra % (ppInfo.BuyCount + ppInfo.GivingCount);
                                        if (actquantity >= (ppInfo.BuyCount / float.Parse("2.0")))
                                        {
                                            dataList[0].PlanQuantity = dataList[0].PlanQuantity + (ppInfo.BuyCount + ppInfo.GivingCount) - actquantity;
                                        }
                                    }
                                    //总商品赠送商品总数
                                    var sumTotal = dataList.Sum(act => Convert.ToInt32(act.PlanQuantity)) / (ppInfo.BuyCount + ppInfo.GivingCount);
                                    dics.Add(goodsBaseInfo.GoodsName, sumTotal);
                                    if (dics[goodsBaseInfo.GoodsName] > 0)
                                    {
                                        foreach (var item in dataList.OrderByDescending(act => act.PlanQuantity))
                                        {
                                            var count = dics[item.GoodsName] - Convert.ToInt32(item.PlanQuantity);
                                            if (count > 0)  //赠品数>购买数
                                            {
                                                item.Price = 0;
                                                item.PurchasingGoodsType = (int)PurchasingGoodsType.Gift;
                                                item.RealityQuantity     = 0;
                                                dics[item.GoodsName]     = count;
                                            }
                                            else if (count < 0)
                                            {
                                                addDetailsList.Add(new PurchasingDetailInfo
                                                {
                                                    PurchasingGoodsID   = Guid.NewGuid(),
                                                    PurchasingID        = item.PurchasingID,
                                                    GoodsID             = item.GoodsID,
                                                    GoodsName           = item.GoodsName,
                                                    GoodsCode           = item.GoodsCode,
                                                    Specification       = item.Specification,
                                                    CompanyID           = item.CompanyID,
                                                    Price               = 0,
                                                    PlanQuantity        = dics[item.GoodsName],
                                                    PurchasingGoodsType = (int)PurchasingGoodsType.Gift,
                                                    RealityQuantity     = 0,
                                                    State               = item.State,
                                                    Description         = "",
                                                    Units               = item.Units,
                                                    SixtyDaySales       = item.SixtyDaySales,
                                                    ThirtyDaySales      = item.ThirtyDaySales,
                                                    ElevenDaySales      = item.ElevenDaySales,
                                                    CPrice              = 0
                                                });
                                                item.RealityQuantity = 0;
                                                item.PlanQuantity    = Math.Abs(count);
                                                dics[item.GoodsName] = 0;
                                            }
                                            else
                                            {
                                                item.Price = 0;
                                                item.PurchasingGoodsType = (int)PurchasingGoodsType.Gift;
                                                item.RealityQuantity     = 0;
                                                item.CPrice          = 0;
                                                dics[item.GoodsName] = 0;
                                            }
                                            if (dics[item.GoodsName] == 0)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            else  //按单光度
                            {
                                //应赠余数
                                var actquantity = pdInfo.PlanQuantity % (ppInfo.BuyCount + ppInfo.GivingCount);
                                if (actquantity > 0)
                                {
                                    if (actquantity >= ((ppInfo.BuyCount + ppInfo.GivingCount) / float.Parse("2.0")))
                                    {
                                        pdInfo.PlanQuantity = pdInfo.PlanQuantity + (ppInfo.BuyCount + ppInfo.GivingCount) - actquantity;
                                    }
                                }
                                int pQuantity = int.Parse(pdInfo.PlanQuantity.ToString(CultureInfo.InvariantCulture));
                                //赠品数量=原采购数量/(买几个+送几个)*送几个
                                int quantity = pQuantity / (ppInfo.BuyCount + ppInfo.GivingCount) * ppInfo.GivingCount;
                                if (quantity > 0)
                                {
                                    var oldPurchasingDetailInfo = plist.FirstOrDefault(w => w.PurchasingGoodsID == pdInfo.PurchasingGoodsID);
                                    if (oldPurchasingDetailInfo != null)
                                    {
                                        //购买数量=原采购数量-赠品数量
                                        oldPurchasingDetailInfo.PlanQuantity -= (quantity);
                                    }

                                    var purchasingDetailInfo = plist.FirstOrDefault(w => w.GoodsID == pdInfo.GoodsID && w.PurchasingGoodsType == (int)PurchasingGoodsType.Gift);
                                    if (purchasingDetailInfo != null)
                                    {
                                        //在原赠品数量累加
                                        purchasingDetailInfo.PlanQuantity += (quantity);
                                    }
                                    else
                                    {
                                        purchasingDetailInfo = new PurchasingDetailInfo
                                        {
                                            PurchasingGoodsID   = Guid.NewGuid(),
                                            PurchasingID        = pInfo.PurchasingID,
                                            GoodsID             = pdInfo.GoodsID,
                                            GoodsName           = pdInfo.GoodsName,
                                            GoodsCode           = pdInfo.GoodsCode,
                                            Specification       = pdInfo.Specification,
                                            CompanyID           = pdInfo.CompanyID,
                                            Price               = 0,
                                            PlanQuantity        = (quantity),
                                            PurchasingGoodsType = (int)PurchasingGoodsType.Gift,
                                            RealityQuantity     = 0,
                                            State               = 0,
                                            Description         = "",
                                            Units               = pdInfo.Units,
                                            SixtyDaySales       = pdInfo.SixtyDaySales,
                                            ThirtyDaySales      = pdInfo.ThirtyDaySales,
                                            ElevenDaySales      = pdInfo.ElevenDaySales
                                        };
                                        addDetailsList.Add(purchasingDetailInfo);
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion

                    #region [非现返生成借记单]

                    var debitNoteDetailList = new List <DebitNoteDetailInfo>();
                    foreach (var pdInfo in plist2)
                    {
                        var goodsBaseInfo = new GoodsInfo();
                        if (dicGoods != null)
                        {
                            bool hasKey = dicGoods.ContainsKey(pdInfo.GoodsID);
                            if (hasKey)
                            {
                                goodsBaseInfo = dicGoods.FirstOrDefault(w => w.Key == pdInfo.GoodsID).Value;
                            }
                        }
                        PurchasingGoods purchaseSetInfo = pg.FirstOrDefault(w => w.GoodsId == goodsBaseInfo.GoodsId && w.WarehouseId == pInfo.WarehouseID);
                        if (purchaseSetInfo != null)
                        {
                            IList <PurchasePromotionInfo> ppList = _purchasePromotionBll.GetPurchasePromotionList(purchaseSetInfo.PromotionId, purchaseSetInfo.GoodsId, purchaseSetInfo.WarehouseId, pInfo.PurchasingFilialeId, (int)PurchasePromotionType.NoBack);
                            PurchasePromotionInfo         ppInfo = ppList.FirstOrDefault(w => w.GivingCount > 0 && w.StartDate <= DateTime.Now && w.EndDate >= DateTime.Now);
                            if (ppInfo != null)
                            {
                                int pQuantity = Convert.ToInt32(pdInfo.PlanQuantity);
                                //赠品数量=原采购数量/买几个*送几个
                                #region  新增
                                //按商品总数量进行赠送
                                if (!ppInfo.IsSingle && dicGoods != null)
                                {
                                    if (!dics.ContainsKey(goodsBaseInfo.GoodsName))
                                    {
                                        var dataList = plist2.Where(act => act.GoodsName == goodsBaseInfo.GoodsName).ToList();
                                        //单光度赠送商品总数
                                        var total = dataList.Sum(act => (Convert.ToInt32(act.PlanQuantity) / (ppInfo.BuyCount + ppInfo.GivingCount)));
                                        //总商品赠送商品总数
                                        var sumTotal = dataList.Sum(act => Convert.ToInt32(act.PlanQuantity)) / (ppInfo.BuyCount + ppInfo.GivingCount);
                                        if (sumTotal > total)
                                        {
                                            dics.Add(goodsBaseInfo.GoodsName, sumTotal);
                                        }
                                    }
                                }
                                #endregion
                                int quantity = pQuantity / ppInfo.BuyCount * ppInfo.GivingCount;
                                if (quantity > 0)
                                {
                                    var debitNoteDetailInfo = new DebitNoteDetailInfo
                                    {
                                        PurchasingId  = pInfo.PurchasingID,
                                        GoodsId       = pdInfo.GoodsID,
                                        GoodsName     = pdInfo.GoodsName,
                                        Specification = pdInfo.Specification,
                                        GivingCount   = quantity,
                                        ArrivalCount  = 0,
                                        Price         = pdInfo.Price,
                                        State         = 0,
                                        Amount        = quantity * pdInfo.Price,
                                        Memo          = "",
                                        Id            = Guid.NewGuid()
                                    };
                                    debitNoteDetailList.Add(debitNoteDetailInfo);
                                }
                            }
                        }
                    }

                    if (addDetailsList.Count > 0)
                    {
                        foreach (var purchasingDetailInfo in addDetailsList)
                        {
                            purchasingDetailInfo.Price = 0;
                            plist.Add(purchasingDetailInfo);
                        }
                    }
                    #endregion

                    #region 处理额外赠送商品
                    foreach (var dic in dics)
                    {
                        KeyValuePair <string, int> dic1 = dic;
                        if (debitExtraDics.ContainsKey(dic.Key))
                        {
                            var total = dic1.Value - debitExtraDics[dic.Key];
                            if (total > 0)
                            {
                                var data = debitNoteDetailList.Where(act => act.GoodsName == dic1.Key).OrderByDescending(act => act.GivingCount).ToList();
                                for (int i = 0; i < total; i++)
                                {
                                    data[i].GivingCount += 1;
                                    data[i].Amount       = data[i].Price * data[i].GivingCount;
                                }
                            }
                        }
                    }
                    #endregion

                    foreach (var item in plist.Where(act => act.Price == 0 && act.PurchasingGoodsType != (int)PurchasingGoodsType.Gift))
                    {
                        var info = plist.FirstOrDefault(act => act.GoodsName == item.GoodsName && act.Price > 0);
                        item.Price = info != null ? info.Price : 0;
                    }
                    //保存采购单明细
                    _purchasingDetailBll.Save(plist);

                    //添加借记单
                    if (debitNoteDetailList.Count > 0)
                    {
                        var debitNoteInfo = new DebitNoteInfo
                        {
                            PurchasingId      = pInfo.PurchasingID,
                            PurchaseGroupId   = pInfo.PurchaseGroupId,
                            PurchasingNo      = pInfo.PurchasingNo,
                            CompanyId         = pInfo.CompanyID,
                            PresentAmount     = debitNoteDetailList.Sum(w => w.Amount),
                            CreateDate        = DateTime.Now,
                            FinishDate        = DateTime.MinValue,
                            State             = (int)DebitNoteState.ToPurchase,
                            WarehouseId       = pInfo.WarehouseID,
                            Memo              = "",
                            PersonResponsible = pInfo.PersonResponsible,
                            NewPurchasingId   = Guid.Empty
                        };
                        _debitNoteBll.AddPurchaseSetAndDetail(debitNoteInfo, debitNoteDetailList);
                    }
                }

                if (dicGoods != null && dicGoods.Count > 0)
                {
                    var completeGoodsIds = dicGoods.Select(keyValuePair => keyValuePair.Value.GoodsId).ToList();
                    foreach (var item in pg.Where(ent => completeGoodsIds.Contains(ent.GoodsId)))
                    {
                        DataAccessor.UpdateLastPurchasingDate(item.WarehouseId, item.HostingFilialeId, item.GoodsId, DateTime.Now);
                    }
                }
            }
        }
示例#6
0
            /// <summary>
            /// 循环遍历子商品的销售和采购信息
            /// </summary>
            /// <param name="p"></param>
            /// <param name="childGoodsSaleAll"></param>
            /// <param name="warehouseId"></param>
            /// <param name="hostingFilialeId"></param>
            /// <param name="avgStockDays"></param>
            /// <param name="nextStockDate"></param>
            /// <param name="taskType"></param>
            /// <param name="step"></param>
            /// <param name="nextPurchasingDate"></param>
            private static void OperationSaleAndPurchasing(PurchasingGoods p, IEnumerable <ChildGoodsSalePurchasing> childGoodsSaleAll, Guid warehouseId, Guid hostingFilialeId,
                                                           int avgStockDays, DateTime nextStockDate, TaskType taskType, int step, DateTime nextPurchasingDate, List <ERP.SAL.WMS.StockStatisticsDTO> stocks)
            {
                //如果传过来的仓库ID是空,说明是指定仓库
                if (warehouseId == Guid.Empty)
                {
                    warehouseId = p.WarehouseId;
                }
                //产生一个采购单ID
                var purchasingId     = Guid.NewGuid();
                var tempPurchasingId = DataAccessor.GetSamePurchasingId(p.CompanyId, p.PersonResponsible, warehouseId, hostingFilialeId);

                if (tempPurchasingId != Guid.Empty)
                {
                    purchasingId = tempPurchasingId;
                }

                //是否有子商品采购信息
                var    hasChildGoodsPurchasingInfo = false;
                var    isException  = false;
                string purchasingNo = DataAccessor.GetCode(BaseInfo.CodeType.PH);
                List <ChildGoodsSalePurchasing> childGoodsSalePurchasingList = childGoodsSaleAll.ToList();
                List <Guid> goodsIdOrRealGoodsIdList  = childGoodsSalePurchasingList.Select(w => w.GoodsId).Distinct().ToList();
                Dictionary <Guid, GoodsInfo> dicGoods = _goodsCenterSao.GetGoodsBaseListByGoodsIdOrRealGoodsIdList(goodsIdOrRealGoodsIdList);
                //如果赠送方式为总数量赠送时使用  key 主商品ID  value 额外赠送
                var dics = new Dictionary <Guid, int>();

                //循环遍历子商品的销售和采购信息
                foreach (var sale in childGoodsSaleAll)
                {
#if debug
                    Console.WriteLine("规格:{0},销售一:{1},销售二:{2},销售三:{3},平均销售:{4},增长率:{5}", sale.Specification, sale.FirstNumberOneStockUpSale, sale.FirstNumberTwoStockUpSale, sale.FirstNumberThreeStockUpSale, sale.WeightedAverageSaleQuantity, sale.SaleInCrease);
#endif

                    //计算计划采购数量
                    if (taskType == TaskType.Warning)
                    {
                        if (step == 4)
                        {
                            sale.PlanPurchasingquantity = sale.WeightedAverageSaleQuantity * ((nextStockDate - DateTime.Now).Days + 1 + p.ArrivalDays);
                        }
                        else
                        {
                            sale.PlanPurchasingquantity = sale.WeightedAverageSaleQuantity * (avgStockDays + p.ArrivalDays);
                        }
                    }
                    else if (taskType == TaskType.Routine)
                    {
                        if (step == 3)
                        {
                            sale.PlanPurchasingquantity = sale.WeightedAverageSaleQuantity * ((nextPurchasingDate - DateTime.Now).Days + (avgStockDays * 2) + 1 + p.ArrivalDays);
                        }
                        else
                        {
                            sale.PlanPurchasingquantity = sale.WeightedAverageSaleQuantity * ((nextStockDate - DateTime.Now).Days + 1 + p.ArrivalDays);
                        }
                    }

                    //计算当前的采购商品数量,包含扣除的已经采购完成和部分采购完成和赠品类型
                    sale.SubtractPurchasingQuantity = DataAccessor.GetSumPurchasingQuantity(sale.GoodsId, warehouseId, hostingFilialeId);

                    //计算当前仓库存货数量
                    var info = stocks.FirstOrDefault(act => act.RealGoodsId == sale.GoodsId);
                    sale.NonceWarehouseStockQuantity = info != null ? info.CurrentStock + info.UppingQuantity - info.RequireQuantity : 0;


#if debug
                    Console.WriteLine("预计采购:{0},采购中的数量:{1},当前库存数量:{2}", sale.PlanPurchasingquantity, sale.SubtractPurchasingQuantity, sale.NonceWarehouseStockQuantity);
                    if (sale.GoodsId == "D41FBCB0-DB51-447D-8F7C-9767D612EC2B".ToGuid())
                    {
                        sale.IsNull();
                    }
#endif

                    //判断实际的采购数量大于零时,新增一条采购记录
                    if (sale.RealityNeedPurchasingQuantity > 0)
                    {
                        //如果是预警报备,计划采购还是到下一次周期的报备
                        if (taskType == TaskType.Warning)
                        {
                            sale.PlanPurchasingquantity = sale.WeightedAverageSaleQuantity * ((nextStockDate - DateTime.Now).Days + 1 + p.ArrivalDays);
                        }

#if debug
                        Console.WriteLine("实际采购数量:{0}", sale.RealityNeedPurchasingQuantity);
#endif

                        //判断是否有最低库存,如果有要判断采购是否满足最低库存
                        var minStockQuantity = DataAccessor.GetGoodsStockMinQuantity(sale.GoodsId, p.WarehouseId);
                        if (minStockQuantity > 0)
                        {
                            if (sale.RealityNeedPurchasingQuantity < minStockQuantity)
                            {
                                var newPlanPurchasingquantity = (minStockQuantity - sale.RealityNeedPurchasingQuantity) + sale.PlanPurchasingquantity;
                                sale.PlanPurchasingquantity = newPlanPurchasingquantity;
                            }
                        }

                        //采购数
                        int purchasingQuantity = 0;

                        #region [现返]
                        //处理原理:
                        //赠品数量=原采购数量/(买几个+送几个)*送几个
                        //购买数量=原采购数量-赠品数量
                        int zpquantity = 0;
                        var goodsInfo  = new GoodsInfo();
                        if (dicGoods != null)
                        {
                            bool hasKey = dicGoods.ContainsKey(sale.GoodsId);
                            if (hasKey)
                            {
                                goodsInfo = dicGoods.FirstOrDefault(w => w.Key == sale.GoodsId).Value;
                            }
                        }
                        IList <PurchasePromotionInfo> ppList = DataAccessor.GetPurchasePromotionList(goodsInfo.GoodsId, (int)PurchasePromotionType.Back, hostingFilialeId);
                        PurchasePromotionInfo         ppInfo = ppList.FirstOrDefault(w => w.GivingCount > 0 && w.StartDate <= DateTime.Now && w.EndDate >= DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 17:29:59"));
                        if (ppInfo != null)
                        {
                            //赠品数量=原采购数量/(买几个+送几个)*送几个
                            zpquantity = (int)sale.RealityNeedPurchasingQuantity / (ppInfo.BuyCount + ppInfo.GivingCount) * ppInfo.GivingCount;
                            if (zpquantity > 0)
                            {
                                int xfQuantity = (int)sale.RealityNeedPurchasingQuantity - zpquantity; //现返类型实际要采购多少数量
                                purchasingQuantity = (int)GetBoxNumber(p.PackQuantity, xfQuantity);    //加入箱数采购算法计算采购数
                            }
                        }
                        else
                        {
                            purchasingQuantity = (int)GetBoxNumber(p.PackQuantity, (int)sale.RealityNeedPurchasingQuantity);//加入箱数采购算法计算采购数
                        }
                        #endregion

                        #region [非现返生成借记单]

                        var addedExtra = new List <Guid>();
                        //借记单明细
                        var debitNoteDetailList = new List <DebitNoteDetailInfo>();
                        IList <PurchasePromotionInfo> ppList2 = DataAccessor.GetPurchasePromotionList(goodsInfo.GoodsId, (int)PurchasePromotionType.NoBack, hostingFilialeId);
                        PurchasePromotionInfo         ppInfo2 = ppList2.FirstOrDefault(w => w.GivingCount > 0 && w.StartDate <= DateTime.Now && w.EndDate >= DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 17:29:59"));
                        if (ppInfo2 != null)
                        {
                            int pQuantity = (int)sale.RealityNeedPurchasingQuantity;
                            //赠品数量=原采购数量/买几个*送几个
                            int fxfzpquantity = pQuantity / ppInfo2.BuyCount * ppInfo2.GivingCount;
                            int extra         = addedExtra.Contains(goodsInfo.GoodsId)
                                        ? 0
                                        : dics[goodsInfo.GoodsId];
                            if (!addedExtra.Contains(goodsInfo.GoodsId))
                            {
                                addedExtra.Add(goodsInfo.GoodsId);
                            }
                            if (fxfzpquantity > 0 || extra > 0)
                            {
                                var debitNoteDetailInfo = new DebitNoteDetailInfo
                                {
                                    PurchasingId  = purchasingId,
                                    GoodsId       = sale.GoodsId,
                                    GoodsName     = p.GoodsName,
                                    Specification = sale.Specification,
                                    GivingCount   = fxfzpquantity + extra,
                                    ArrivalCount  = 0,
                                    Price         = p.Price,
                                    State         = 0,
                                    Amount        = fxfzpquantity * p.Price,
                                    Memo          = "",
                                    Id            = Guid.NewGuid()
                                };
                                debitNoteDetailList.Add(debitNoteDetailInfo);
                            }
                            if (debitNoteDetailList.Count > 0)
                            {
                                var debitNoteInfo = new DebitNoteInfo
                                {
                                    PurchasingId      = purchasingId,
                                    PurchasingNo      = purchasingNo,
                                    CompanyId         = p.CompanyId,
                                    PresentAmount     = debitNoteDetailList.Sum(w => w.Amount),
                                    CreateDate        = DateTime.Now,
                                    FinishDate        = DateTime.MinValue,
                                    State             = (int)DebitNoteState.ToPurchase,
                                    WarehouseId       = p.WarehouseId,
                                    Memo              = "",
                                    PersonResponsible = p.PersonResponsible,
                                    NewPurchasingId   = Guid.Empty
                                };
                                var dnInfo = DataAccessor.GetDebitNoteInfo(purchasingId); //是否已经有借记单
                                if (dnInfo != null && dnInfo.PurchasingId != Guid.Empty)  //已经有借记单则修改借记单
                                {
                                    var dnlist = DataAccessor.GetDebitNoteDetailList(purchasingId);
                                    if (dnlist != null && dnlist.Any(act => act.GoodsId == sale.GoodsId))
                                    {
                                        DataAccessor.UpdateDebitNoteDetail(purchasingId, sale.GoodsId, fxfzpquantity);
                                    }
                                    else
                                    {
                                        foreach (var dinfo in debitNoteDetailList)
                                        {
                                            DataAccessor.AddDebitNoteDetail(dinfo);
                                        }
                                    }
                                }
                                else
                                {
                                    DataAccessor.AddPurchaseSetAndDetail(debitNoteInfo, debitNoteDetailList);
                                }
                            }
                        }
                        #endregion

                        var purchasingGoodsId = Guid.Empty;
                        if (tempPurchasingId != Guid.Empty)
                        {
                            purchasingGoodsId = DataAccessor.GetSamePurchasingGoodsId(tempPurchasingId, sale.GoodsId, sale.Specification, sale.HostingFilialeId);
                        }
                        if (purchasingGoodsId != Guid.Empty)
                        {
                            DataAccessor.UpdatePurchasingGoodsPlanQuantity(purchasingGoodsId, purchasingQuantity);

                            if (zpquantity > 0)//如果有现反赠品则添加一条采购明细记录
                            {
                                var salesinfo = DataAccessor.GetChildGoodsSale(sale.GoodsId, warehouseId, hostingFilialeId, DateTime.Now);
                                DataAccessor.InsertPurchasingDetail(Guid.NewGuid(), (tempPurchasingId == Guid.Empty ? purchasingId : tempPurchasingId), sale.GoodsId,
                                                                    p.GoodsName, p.Units, p.GoodsCode, sale.Specification,
                                                                    p.CompanyId, 0, zpquantity, 0, 0, string.Empty,
                                                                    sale.WeightedAverageSaleQuantity / 30,
                                                                    zpquantity, p.StockingDays + p.ArrivalDays, isException,
                                                                    salesinfo.SixtyDaySales, salesinfo.ThirtyDaySales,
                                                                    (salesinfo.ElevenDaySales / 11), (int)PurchasingGoodsType.Gift);
                            }
                        }
                        else
                        {
                            if (sale.RealityNeedPurchasingQuantity > 0)
                            {
                                var salesinfo = DataAccessor.GetChildGoodsSale(sale.GoodsId, warehouseId, hostingFilialeId, DateTime.Now);

                                if (salesinfo.GoodsId != Guid.Empty)
                                {
                                    //当前活动报备异常
                                    if ((salesinfo.ElevenDaySales / 11) >= ((salesinfo.ThirtyDaySales / 30) * 1.8))
                                    {
                                        isException = true;
                                    }
                                    //历史活动报备异常
                                    if ((salesinfo.ElevenDaySales / 11) * 1.6 <= (salesinfo.ThirtyDaySales / 30) || (salesinfo.ElevenDaySales / 11) * 1.6 <= (salesinfo.SixtyDaySales / 30))
                                    {
                                        isException = true;
                                    }
                                }

                                //插入具体的商品采购信息记录

                                if (zpquantity > 0)//如果有现反赠品则添加一条采购明细记录
                                {
                                    DataAccessor.InsertPurchasingDetail(Guid.NewGuid(), (tempPurchasingId == Guid.Empty ? purchasingId : tempPurchasingId), sale.GoodsId, p.GoodsName, p.Units, p.GoodsCode, sale.Specification,
                                                                        p.CompanyId, 0, zpquantity, 0, 0, string.Empty, sale.WeightedAverageSaleQuantity / 30,
                                                                        zpquantity, p.StockingDays + p.ArrivalDays, isException, salesinfo.SixtyDaySales,
                                                                        salesinfo.ThirtyDaySales, (salesinfo.ElevenDaySales / 11), (int)PurchasingGoodsType.Gift);//赠品
                                }
                                DataAccessor.InsertPurchasingDetail(Guid.NewGuid(), purchasingId, sale.GoodsId, p.GoodsName, p.Units, p.GoodsCode,
                                                                    sale.Specification, p.CompanyId, p.Price, purchasingQuantity, 0, 0,
                                                                    string.Empty, sale.WeightedAverageSaleQuantity / 30,
                                                                    purchasingQuantity, p.StockingDays + p.ArrivalDays, isException,
                                                                    salesinfo.SixtyDaySales, salesinfo.ThirtyDaySales,
                                                                    (salesinfo.ElevenDaySales / 11), (int)PurchasingGoodsType.NoGift);//非赠品

                                hasChildGoodsPurchasingInfo = true;

#if debug
                                Console.WriteLine("新增采购单成功,单据ID:{0}", purchasingId);
#endif
                            }
                        }
                    }
                }

                //插入采购单记录
                if (hasChildGoodsPurchasingInfo && tempPurchasingId == Guid.Empty)
                {
                    if (step == 3)
                    {
                        nextPurchasingDate.AddDays(avgStockDays * 2);
                    }

                    DataAccessor.InsertPurchasing(purchasingId, purchasingNo, p.CompanyId, p.CompanyName, hostingFilialeId, warehouseId, hostingFilialeId, 0, 1, DateTime.Now, DateTime.Now,
                                                  "[自动报备]", Guid.Empty, nextStockDate.AddDays(p.ArrivalDays), nextPurchasingDate, isException, p.PersonResponsible);
                }
            }
示例#7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="purDics"></param>
        /// <param name="dicGoods"></param>
        /// <param name="purchaseSetList"></param>
        /// <param name="msg"></param>
        public bool CreatePurchasing(Dictionary <PurchasingInfo, IList <PurchasingDetailInfo> > purDics, Dictionary <string, Guid> dicGoods,
                                     IList <PurchaseSetInfo> purchaseSetList, out string msg)
        {
            if (dicGoods == null || dicGoods.Count == 0)
            {
                msg = string.Empty;
                return(true);
            }

            using (var ts = new TransactionScope(TransactionScopeOption.Required))
            {
                foreach (KeyValuePair <PurchasingInfo, IList <PurchasingDetailInfo> > keyValue in purDics)
                {
                    var pInfo = keyValue.Key;
                    IList <PurchasingDetailInfo> plist = keyValue.Value;

                    IList <PurchasingDetailInfo> plist2 = plist.Where(w => w.PurchasingGoodsType != (int)PurchasingGoodsType.Gift).ToList();
                    //如果赠送方式为总数量赠送时使用  key 主商品ID  value 额外赠送
                    var dics           = new Dictionary <string, int>();    //需赠送
                    var sendDics       = new Dictionary <string, int>();    //现返已赠送
                    var debitExtraDics = new Dictionary <string, int>();    //借计单已送
                    var addDetailsList = new List <PurchasingDetailInfo>(); //添加赠品
                    #region [现返]
                    //处理原理:
                    //赠品数量=原采购数量/(买几个+送几个)*送几个
                    //购买数量=原采购数量-赠品数量
                    foreach (var pdInfo in plist2)
                    {
                        if (!dicGoods.ContainsKey(pdInfo.GoodsName))
                        {
                            continue;
                        }
                        var             goodsBaseInfo   = dicGoods[pdInfo.GoodsName];
                        PurchaseSetInfo purchaseSetInfo = purchaseSetList.FirstOrDefault(w => w.GoodsId == goodsBaseInfo && pInfo.WarehouseID == w.WarehouseId && pInfo.PurchasingFilialeId == w.HostingFilialeId);
                        if (purchaseSetInfo == null)
                        {
                            continue;
                        }
                        IList <PurchasePromotionInfo> ppList = _purchasePromotionManager.GetPurchasePromotionList(purchaseSetInfo.PromotionId, purchaseSetInfo.GoodsId, purchaseSetInfo.WarehouseId, purchaseSetInfo.HostingFilialeId,
                                                                                                                  (int)PurchasePromotionType.Back);
                        PurchasePromotionInfo ppInfo = ppList.FirstOrDefault(w => w.GivingCount > 0 && w.StartDate <= DateTime.Now && w.EndDate >= DateTime.Now);
                        if (ppInfo != null)
                        {
                            #region  新增
                            if (!ppInfo.IsSingle)  //按商品总数量进行赠送
                            {
                                if (!dics.ContainsKey(pdInfo.GoodsName))
                                {
                                    var dataList = plist2.Where(act => act.GoodsName == pdInfo.GoodsName).OrderByDescending(act => act.PlanQuantity).ToList();
                                    //余数
                                    var extra = dataList.Sum(act => Convert.ToInt32(act.PlanQuantity)) % (ppInfo.BuyCount + ppInfo.GivingCount);
                                    if (extra == ppInfo.BuyCount)
                                    {
                                        dataList[0].PlanQuantity += ppInfo.GivingCount;
                                    }
                                    else
                                    {
                                        //应赠送次数
                                        int actquantity = extra % (ppInfo.BuyCount + ppInfo.GivingCount);
                                        if (actquantity >= ((ppInfo.BuyCount + ppInfo.GivingCount) / float.Parse("2.0")))
                                        {
                                            dataList[0].PlanQuantity = dataList[0].PlanQuantity + (ppInfo.BuyCount + ppInfo.GivingCount) - actquantity;
                                        }
                                    }
                                    //总商品赠送商品总数
                                    var sumTotal = dataList.Sum(act => Convert.ToInt32(act.PlanQuantity)) * ppInfo.GivingCount / (ppInfo.BuyCount + ppInfo.GivingCount);
                                    dics.Add(pdInfo.GoodsName, sumTotal);
                                    if (dics[pdInfo.GoodsName] > 0)
                                    {
                                        foreach (var item in dataList.OrderByDescending(act => act.PlanQuantity))
                                        {
                                            if (dics[item.GoodsName] == 0)
                                            {
                                                continue;
                                            }
                                            var count = dics[item.GoodsName] - Convert.ToInt32(item.PlanQuantity);
                                            if (count > 0)  //赠品数>购买数
                                            {
                                                item.Price = 0;
                                                item.PurchasingGoodsType = (int)PurchasingGoodsType.Gift;
                                                item.RealityQuantity     = 0;
                                                dics[item.GoodsName]     = count;
                                            }
                                            else if (count < 0)  //赠送数小于采购数
                                            {
                                                addDetailsList.Add(new PurchasingDetailInfo
                                                {
                                                    PurchasingGoodsID   = Guid.NewGuid(),
                                                    PurchasingID        = item.PurchasingID,
                                                    GoodsID             = item.GoodsID,
                                                    GoodsName           = item.GoodsName,
                                                    GoodsCode           = item.GoodsCode,
                                                    Specification       = item.Specification,
                                                    CompanyID           = item.CompanyID,
                                                    Price               = 0,
                                                    PlanQuantity        = dics[item.GoodsName],
                                                    PurchasingGoodsType = (int)PurchasingGoodsType.Gift,
                                                    RealityQuantity     = 0,
                                                    State               = item.State,
                                                    Description         = "",
                                                    Units               = item.Units,
                                                    SixtyDaySales       = item.SixtyDaySales,
                                                    ThirtyDaySales      = item.ThirtyDaySales,
                                                    ElevenDaySales      = item.ElevenDaySales,
                                                    CPrice              = 0
                                                });
                                                item.RealityQuantity = 0;
                                                item.PlanQuantity    = Math.Abs(count);
                                                dics[item.GoodsName] = 0;
                                            }
                                            else   //赠送数=采购数
                                            {
                                                item.Price = 0;
                                                item.PurchasingGoodsType = (int)PurchasingGoodsType.Gift;
                                                item.RealityQuantity     = 0;
                                                item.CPrice          = 0;
                                                dics[item.GoodsName] = 0;
                                            }
                                        }
                                    }
                                }
                            }
                            else  //按单光度
                            {
                                //应赠余数
                                var actquantity = pdInfo.PlanQuantity % (ppInfo.BuyCount + ppInfo.GivingCount);
                                if (actquantity > 0)
                                {
                                    if (actquantity >= ((ppInfo.BuyCount + ppInfo.GivingCount) / float.Parse("2.0")))
                                    {
                                        pdInfo.PlanQuantity = pdInfo.PlanQuantity + (ppInfo.BuyCount + ppInfo.GivingCount) - actquantity;
                                    }
                                }
                                int pQuantity = int.Parse(pdInfo.PlanQuantity.ToString(CultureInfo.InvariantCulture));
                                //赠品数量=原采购数量/(买几个+送几个)*送几个
                                int quantity = pQuantity / (ppInfo.BuyCount + ppInfo.GivingCount) * ppInfo.GivingCount;
                                if (quantity > 0)
                                {
                                    var oldPurchasingDetailInfo = plist.FirstOrDefault(w => w.PurchasingGoodsID == pdInfo.PurchasingGoodsID);
                                    if (oldPurchasingDetailInfo != null)
                                    {
                                        //购买数量=原采购数量-赠品数量
                                        oldPurchasingDetailInfo.PlanQuantity -= (quantity);
                                    }

                                    var purchasingDetailInfo = plist.FirstOrDefault(w => w.GoodsID == pdInfo.GoodsID && w.PurchasingGoodsType == (int)PurchasingGoodsType.Gift);
                                    if (purchasingDetailInfo != null)
                                    {
                                        //在原赠品数量累加
                                        purchasingDetailInfo.PlanQuantity += (quantity);
                                    }
                                    else
                                    {
                                        purchasingDetailInfo = new PurchasingDetailInfo
                                        {
                                            PurchasingGoodsID   = Guid.NewGuid(),
                                            PurchasingID        = pInfo.PurchasingID,
                                            GoodsID             = pdInfo.GoodsID,
                                            GoodsName           = pdInfo.GoodsName,
                                            GoodsCode           = pdInfo.GoodsCode,
                                            Specification       = pdInfo.Specification,
                                            CompanyID           = pdInfo.CompanyID,
                                            Price               = 0,
                                            PlanQuantity        = (quantity),
                                            PurchasingGoodsType = (int)PurchasingGoodsType.Gift,
                                            RealityQuantity     = 0,
                                            State               = 0,
                                            Description         = "",
                                            Units               = pdInfo.Units,
                                            SixtyDaySales       = pdInfo.SixtyDaySales,
                                            ThirtyDaySales      = pdInfo.ThirtyDaySales,
                                            ElevenDaySales      = pdInfo.ElevenDaySales
                                        };
                                        addDetailsList.Add(purchasingDetailInfo);
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    if (addDetailsList.Count > 0)
                    {
                        foreach (var purchasingDetailInfo in addDetailsList)
                        {
                            purchasingDetailInfo.Price = 0;
                            plist.Add(purchasingDetailInfo);
                        }
                    }

                    #endregion

                    #region [非现返生成借记单]
                    var debitNoteDetailList = new List <DebitNoteDetailInfo>();
                    foreach (var pdInfo in plist2)
                    {
                        if (!dicGoods.ContainsKey(pdInfo.GoodsName))
                        {
                            continue;
                        }
                        var             goodsBaseInfo   = dicGoods[pdInfo.GoodsName];
                        PurchaseSetInfo purchaseSetInfo = purchaseSetList.FirstOrDefault(w => w.GoodsId == goodsBaseInfo && w.HostingFilialeId == pInfo.PurchasingFilialeId);
                        if (purchaseSetInfo != null)
                        {
                            IList <PurchasePromotionInfo> ppList = _purchasePromotionManager.GetPurchasePromotionList(purchaseSetInfo.PromotionId, purchaseSetInfo.GoodsId, purchaseSetInfo.WarehouseId, purchaseSetInfo.HostingFilialeId,
                                                                                                                      (int)PurchasePromotionType.NoBack);
                            PurchasePromotionInfo ppInfo = ppList.FirstOrDefault(w => w.GivingCount > 0 && w.StartDate <= DateTime.Now && w.EndDate >= DateTime.Now);
                            if (ppInfo != null)
                            {
                                int pQuantity = Convert.ToInt32(pdInfo.PlanQuantity);
                                //赠品数量=原采购数量/买几个*送几个
                                int quantity = pQuantity / ppInfo.BuyCount * ppInfo.GivingCount;
                                #region  新增
                                //按商品总数量进行赠送
                                if (!ppInfo.IsSingle)
                                {
                                    if (!dics.ContainsKey(pdInfo.GoodsName))
                                    {
                                        var dataList = plist2.Where(act => act.GoodsName == pdInfo.GoodsName).ToList();
                                        //单光度赠送商品总数
                                        var total = dataList.Sum(act => (Convert.ToInt32(act.PlanQuantity) / (ppInfo.BuyCount + ppInfo.GivingCount)));
                                        //总商品赠送商品总数
                                        var sumTotal = dataList.Sum(act => Convert.ToInt32(act.PlanQuantity)) / (ppInfo.BuyCount + ppInfo.GivingCount);
                                        if (sumTotal > total)
                                        {
                                            dics.Add(pdInfo.GoodsName, sumTotal);
                                        }
                                    }
                                }
                                #endregion
                                if (quantity > 0)
                                {
                                    var debitNoteDetailInfo = new DebitNoteDetailInfo
                                    {
                                        PurchasingId  = pInfo.PurchasingID,
                                        GoodsId       = pdInfo.GoodsID,
                                        GoodsName     = pdInfo.GoodsName,
                                        Specification = pdInfo.Specification,
                                        GivingCount   = quantity,
                                        ArrivalCount  = 0,
                                        Price         = pdInfo.Price,
                                        State         = 0,
                                        Amount        = quantity * pdInfo.Price,
                                        Memo          = "",
                                        Id            = Guid.NewGuid()
                                    };
                                    if (debitExtraDics.ContainsKey(pdInfo.GoodsName))
                                    {
                                        debitExtraDics[pdInfo.GoodsName] = debitExtraDics[pdInfo.GoodsName] + quantity;
                                    }
                                    else
                                    {
                                        debitExtraDics.Add(pdInfo.GoodsName, quantity);
                                    }
                                    debitNoteDetailList.Add(debitNoteDetailInfo);
                                }
                            }
                        }
                    }

                    #endregion

                    #region 处理额外赠送商品
                    foreach (var dic in dics)
                    {
                        KeyValuePair <string, int> dic1 = dic;
                        if (sendDics.ContainsKey(dic.Key))
                        {
                            var total = dic1.Value - sendDics[dic.Key];
                            if (total > 0)
                            {
                                var data = plist.Where(act => act.GoodsName == dic1.Key && act.PurchasingGoodsType != (int)PurchasingGoodsType.Gift)
                                           .OrderByDescending(act => act.PlanQuantity).ToList();
                                var data2 = plist.Where(act => act.GoodsName == dic1.Key && act.PurchasingGoodsType == (int)PurchasingGoodsType.Gift)
                                            .OrderByDescending(act => act.PlanQuantity);
                                for (int i = 0; i < total; i++)
                                {
                                    var purchasingDetailInfo = data2.FirstOrDefault(w => w.GoodsID == plist[i].GoodsID);
                                    if (purchasingDetailInfo != null)
                                    {
                                        data[i].PlanQuantity -= 1;
                                        purchasingDetailInfo.PlanQuantity += 1;
                                    }
                                }
                            }
                        }
                        if (debitExtraDics.ContainsKey(dic.Key))
                        {
                            var total = dic1.Value - debitExtraDics[dic.Key];
                            if (total > 0)
                            {
                                var data = debitNoteDetailList.Where(act => act.GoodsName == dic1.Key).OrderByDescending(act => act.GivingCount).ToList();
                                for (int i = 0; i < total; i++)
                                {
                                    data[i].GivingCount += 1;
                                    data[i].Amount       = data[i].Price * data[i].GivingCount;
                                }
                            }
                        }
                    }
                    #endregion

                    var originalDetails = _purchasingDetailManager.Select(pInfo.PurchasingID);

                    _purchasingDetailManager.Delete(pInfo.PurchasingID);
                    _purchasing.DeleteById(pInfo.PurchasingID);

                    //pInfo.IsOut = true;
                    //插入采购单
                    _purchasing.PurchasingInsert(pInfo);
                    //保存采购单明细
                    foreach (var item in plist.Where(act => act.Price == 0 && act.PurchasingGoodsType != (int)PurchasingGoodsType.Gift))
                    {
                        var info = plist.FirstOrDefault(act => act.GoodsName == item.GoodsName && act.Price > 0);
                        item.Price = info != null?info.Price:0;
                    }

                    var ids = new List <Guid>();
                    if (originalDetails.Count > 0)
                    {
                        foreach (var purchasingDetailInfo in originalDetails)
                        {
                            var info = plist.FirstOrDefault(act =>
                                                            act.GoodsID == purchasingDetailInfo.GoodsID &&
                                                            act.PurchasingGoodsType == purchasingDetailInfo.PurchasingGoodsType);
                            if (info != null)
                            {
                                purchasingDetailInfo.PlanQuantity += info.PlanQuantity;
                                ids.Add(info.PurchasingGoodsID);
                            }
                            _purchasingDetailManager.Insert(purchasingDetailInfo);
                        }
                    }
                    foreach (var item in plist.Where(act => !ids.Contains(act.PurchasingGoodsID)))
                    {
                        _purchasingDetailManager.Insert(item);
                    }

                    //添加借记单
                    if (debitNoteDetailList.Count > 0)
                    {
                        IDebitNote debitNoteManager = new DebitNote(Environment.GlobalConfig.DB.FromType.Write);
                        var        debitNoteInfo    = new DebitNoteInfo
                        {
                            PurchasingId      = pInfo.PurchasingID,
                            PurchasingNo      = pInfo.PurchasingNo,
                            CompanyId         = pInfo.CompanyID,
                            PresentAmount     = debitNoteDetailList.Sum(w => w.Amount),
                            CreateDate        = DateTime.Now,
                            FinishDate        = DateTime.MinValue,
                            State             = (int)DebitNoteState.ToPurchase,
                            WarehouseId       = pInfo.WarehouseID,
                            Memo              = "",
                            PersonResponsible = pInfo.PersonResponsible,
                            NewPurchasingId   = Guid.Empty
                        };
                        debitNoteManager.AddPurchaseSetAndDetail(debitNoteInfo, debitNoteDetailList);
                    }
                }
                ts.Complete();
            }
            msg = string.Empty;
            return(true);
        }