/// <summary> /// 插入 /// </summary> /// <param name="info"></param> public void Insert(PurchasingDetailInfo info) { if ((int)PurchasingGoodsType.Gift == info.PurchasingGoodsType) { info.Price = 0; #region -- 赠品 IList <PurchasingDetailInfo> ilist = _purchasingDetailDao.Select(info.PurchasingID).Where(p => p.GoodsID == info.GoodsID).Where(p => p.PurchasingGoodsType == (int)PurchasingGoodsType.Gift).ToList(); if (ilist.Count == 0) { //为赠品并且数据库采购明细表里面不存在赠品的商品 _purchasingDetailDao.Insert(info); } else { //存在数据库里面采购明细表里面存在赠品 PurchasingDetailInfo pdInfo = ilist[0]; pdInfo.PlanQuantity = pdInfo.PlanQuantity + info.PlanQuantity; _purchasingDetailDao.Update(pdInfo, pdInfo.PurchasingGoodsID); } #endregion } else { #region -- 非赠品 IList <PurchasingDetailInfo> ilist = _purchasingDetailDao.Select(info.PurchasingID).Where(p => p.GoodsID == info.GoodsID).Where(p => p.PurchasingGoodsType != (int)PurchasingGoodsType.Gift).ToList(); if (ilist.Count == 0) { //采购明细里面不存在非赠品,直接插入 _purchasingDetailDao.Insert(info); } else { //采购明细里面存在非赠品,直接修改采购计划数量 PurchasingDetailInfo pdInfo = ilist[0]; pdInfo.PlanQuantity = pdInfo.PlanQuantity + info.PlanQuantity; _purchasingDetailDao.Update(pdInfo, pdInfo.PurchasingGoodsID);//修改数量 } #endregion } }
/// <summary>拆分采购单并插入新的采购单 /// </summary> /// <param name="purchasingId">原始采购单Id</param> /// <param name="list">新采购单信息</param> private void SplitPurchasing(Guid purchasingId, IList <PurchasingGoodsSplitInfo> list) { var purchasingInfo = _purchasing.GetPurchasingById(purchasingId); IList <PurchasingDetailInfo> purchasingDetailInfoList = _purchasingDetail.Select(purchasingInfo.PurchasingID); var addedPurchasingList = new List <PurchasingInfo>(); var addedPurchasingDetailList = new List <PurchasingDetailInfo>(); var newList = list.Where(ent => ent.PurchasingGoodsItemList.Count > 0).ToList(); for (var i = 0; i < newList.Count; i++) { var info = newList[i]; var newPurchasingId = Guid.NewGuid(); addedPurchasingList.Add(new PurchasingInfo { PurchasingID = newPurchasingId, ArrivalTime = purchasingInfo.ArrivalTime, CompanyID = purchasingInfo.CompanyID, CompanyName = purchasingInfo.CompanyName, Description = purchasingInfo.Description, Director = purchasingInfo.Director, EndTime = DateTime.MaxValue, FilialeID = purchasingInfo.FilialeID, IsException = purchasingInfo.IsException, NextPurchasingDate = purchasingInfo.NextPurchasingDate, PersonResponsible = purchasingInfo.PersonResponsible, PersonResponsibleName = purchasingInfo.PersonResponsibleName, PmId = purchasingInfo.PmId, PmName = purchasingInfo.PmName, PurchasingFilialeId = info.FilialeId, PurchasingNo = i > 0 ? purchasingInfo.PurchasingNo + "V" + i : purchasingInfo.PurchasingNo, PurchasingState = (int)PurchasingState.Purchasing, PurchasingToDate = purchasingInfo.PurchasingToDate, PurchasingType = purchasingInfo.PurchasingType, StartTime = purchasingInfo.StartTime, WarehouseID = purchasingInfo.WarehouseID, //IsOut = info.PurchasingGoodsItemList[0].IsOut }); foreach (var item in info.PurchasingGoodsItemList) { var item1 = item; var pgInfos = purchasingDetailInfoList.Where(ent => ent.GoodsCode == item1.GoodsCode); foreach (var pgInfo in pgInfos) { addedPurchasingDetailList.Add(new PurchasingDetailInfo { PurchasingID = newPurchasingId, GoodsID = pgInfo.GoodsID, GoodsName = pgInfo.GoodsName, GoodsCode = pgInfo.GoodsCode, Specification = pgInfo.Specification, CompanyID = purchasingInfo.CompanyID, Price = pgInfo.Price, PlanQuantity = pgInfo.PlanQuantity, RealityQuantity = 0, State = 0, Units = pgInfo.Units, PurchasingGoodsID = pgInfo.PurchasingGoodsID, SixtyDaySales = pgInfo.SixtyDaySales, ThirtyDaySales = pgInfo.ThirtyDaySales, ElevenDaySales = pgInfo.ElevenDaySales, CPrice = pgInfo.Price, PurchasingGoodsType = pgInfo.PurchasingGoodsType }); } } } using (var ts = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromMinutes(2))) { try { foreach (var item in addedPurchasingList) { _purchasing.PurchasingInsert(item); _purchasing.PurchasingUpdate(item.PurchasingID, item.PurchasingFilialeId, item.IsOut); } foreach (var purchasingDetailInfo in addedPurchasingDetailList) { _purchasingDetail.Insert(purchasingDetailInfo); } var returnStr = Delete(purchasingInfo.PurchasingID); if (string.IsNullOrWhiteSpace(returnStr)) { ts.Complete(); } } catch (Exception ex) { SAL.LogCenter.LogService.LogError(string.Format("采购单拆单异常,PurchasingId={0}", purchasingId), "采购管理", ex); } } }
/// <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); }