private PurchaseSetInfo ReaderPurchaseSet(IDataReader dr) { var info = new PurchaseSetInfo { GoodsId = SqlRead.GetGuid(dr, "GoodsId"), GoodsName = SqlRead.GetString(dr, "GoodsName"), WarehouseId = SqlRead.GetGuid(dr, "WarehouseId"), CompanyId = SqlRead.GetGuid(dr, "CompanyId"), PurchasePrice = SqlRead.GetDecimal(dr, "PurchasePrice"), PersonResponsible = SqlRead.GetGuid(dr, "PersonResponsible"), Memo = SqlRead.GetString(dr, "Memo"), PromotionId = SqlRead.GetGuid(dr, "PromotionId"), IsStockUp = SqlRead.GetBoolean(dr, "IsStockUp"), PurchaseGroupId = SqlRead.GetGuid(dr, "PurchaseGroupId"), FilingForm = SqlRead.GetInt(dr, "FilingForm"), StockUpDay = SqlRead.GetInt(dr, "StockUpDay"), FirstWeek = SqlRead.GetInt(dr, "FirstWeek"), SecondWeek = SqlRead.GetInt(dr, "SecondWeek"), ThirdWeek = SqlRead.GetInt(dr, "ThirdWeek"), FourthWeek = SqlRead.GetInt(dr, "FourthWeek"), FilingTrigger = SqlRead.GetInt(dr, "FilingTrigger"), Insufficient = SqlRead.GetInt(dr, "Insufficient"), IsDelete = Convert.ToInt32(dr["IsDelete"]), CompanyName = SqlRead.GetString(dr, "CompanyName"), HostingFilialeId = SqlRead.GetGuid(dr, "HostingFilialeId") }; return(info); }
/// <summary> /// 获取商品采购设置 /// </summary> /// <param name="goodsId"></param> /// <param name="hostingFilialeId"></param> /// <param name="warehouseId"></param> /// <returns></returns> public PurchaseSetInfo GetPurchaseSetInfo(Guid goodsId, Guid hostingFilialeId, Guid warehouseId) { const string SQL = SQL_SELECT_PURCHASESET + " WHERE GoodsId=@GoodsId AND WarehouseId=@WarehouseId AND HostingFilialeId=@HostingFilialeId"; var parms = new[] { new SqlParameter(PARM_GOODSID, SqlDbType.UniqueIdentifier) { Value = goodsId }, new SqlParameter(PARM_WAREHOUSEID, SqlDbType.UniqueIdentifier) { Value = warehouseId }, new SqlParameter(PARM_HOSTINGFILIALEID, SqlDbType.UniqueIdentifier) { Value = hostingFilialeId } }; PurchaseSetInfo info = null; using (var dr = SqlHelper.ExecuteReader(GlobalConfig.ERP_DB_NAME, true, SQL, parms)) { if (dr.Read()) { info = ReaderPurchaseSet(dr); } } return(info); }
/// <summary> /// 修改商品采购设置 /// </summary> /// <param name="info"></param> /// <param name="errorMessage"></param> public int UpdatePurchaseSet(PurchaseSetInfo info, out string errorMessage) { errorMessage = string.Empty; const string SQL_UPDATE_PURCHASESET = @" UPDATE [lmshop_PurchaseSet] SET [GoodsName]=@GoodsName ,[CompanyId]=@CompanyId ,[PurchasePrice]=@PurchasePrice ,[PersonResponsible]=@PersonResponsible ,[Memo]=@Memo ,[PromotionId]=@PromotionId ,[IsStockUp]=@IsStockUp ,[PurchaseGroupId]=@PurchaseGroupId ,[FilingForm]=@FilingForm ,[StockUpDay]=@StockUpDay ,[FirstWeek]=@FirstWeek ,[SecondWeek]=@SecondWeek ,[ThirdWeek]=@ThirdWeek ,[FourthWeek]=@FourthWeek ,[FilingTrigger]=@FilingTrigger ,[Insufficient]=@Insufficient WHERE [GoodsId]=@GoodsId AND [WarehouseId]=@WarehouseId AND [HostingFilialeId] = @HostingFilialeId"; SqlParameter[] parms = GetPurchaseSetParameters(); parms[0].Value = info.GoodsId; parms[1].Value = info.GoodsName; parms[2].Value = info.WarehouseId; parms[3].Value = info.CompanyId; parms[4].Value = Convert.ToDouble(info.PurchasePrice); parms[5].Value = info.PersonResponsible; parms[6].Value = info.Memo; parms[7].Value = info.PromotionId; parms[8].Value = info.IsStockUp; parms[9].Value = info.PurchaseGroupId; parms[10].Value = info.FilingForm; parms[11].Value = info.StockUpDay; parms[12].Value = info.FirstWeek; parms[13].Value = info.SecondWeek; parms[14].Value = info.ThirdWeek; parms[15].Value = info.FourthWeek; parms[16].Value = info.FilingTrigger; parms[17].Value = info.Insufficient; parms[18].Value = info.HostingFilialeId; try { return(SqlHelper.ExecuteNonQuery(GlobalConfig.ERP_DB_NAME, false, SQL_UPDATE_PURCHASESET, parms)); } catch (Exception ex) { errorMessage = ex.Message; throw new ApplicationException(ex.Message, ex); } }
/// <summary> /// 商品采购设置中获取责任人ID和责任人名字 /// </summary> /// <returns></returns> public IList <PurchaseSetInfo> GetPersonList() { const string SQL = @"SELECT DISTINCT(PersonResponsible) AS PersonResponsible FROM [lmshop_PurchaseSet] WHERE IsDelete = 1"; IList <PurchaseSetInfo> list = new List <PurchaseSetInfo>(); using (var dr = SqlHelper.ExecuteReader(GlobalConfig.ERP_DB_NAME, true, SQL, null)) { while (dr.Read()) { var info = new PurchaseSetInfo { PersonResponsible = dr["PersonResponsible"] == DBNull.Value ? Guid.Empty : new Guid(dr["PersonResponsible"].ToString()) }; list.Add(info); } } return(list); }
/// <summary> /// 添加商品采购设置 /// </summary> /// <param name="info"></param> /// <param name="errorMessage"></param> public bool AddPurchaseSet(PurchaseSetInfo info, out string errorMessage) { errorMessage = string.Empty; const string SQL = @" INSERT INTO [lmshop_PurchaseSet]([GoodsId],[GoodsName],[WarehouseId],[CompanyId],[PurchasePrice],[PersonResponsible],[Memo],[PromotionId],[IsStockUp],[PurchaseGroupId],[FilingForm],[StockUpDay],[FirstWeek],[SecondWeek],[ThirdWeek],[FourthWeek],[FilingTrigger],[Insufficient],[HostingFilialeId]) VALUES(@GoodsId,@GoodsName,@WarehouseId,@CompanyId,@PurchasePrice,@PersonResponsible,@Memo,@PromotionId,@IsStockUp,@PurchaseGroupId,@FilingForm,@StockUpDay,@FirstWeek,@SecondWeek,@ThirdWeek,@FourthWeek,@FilingTrigger,@Insufficient,@HostingFilialeId)"; SqlParameter[] parms = GetPurchaseSetParameters(); parms[0].Value = info.GoodsId; parms[1].Value = info.GoodsName; parms[2].Value = info.WarehouseId; parms[3].Value = info.CompanyId; parms[4].Value = Convert.ToDouble(info.PurchasePrice); parms[5].Value = info.PersonResponsible; parms[6].Value = info.Memo; parms[7].Value = info.PromotionId; parms[8].Value = info.IsStockUp; parms[9].Value = info.PurchaseGroupId; parms[10].Value = info.FilingForm; parms[11].Value = info.StockUpDay; parms[12].Value = info.FirstWeek; parms[13].Value = info.SecondWeek; parms[14].Value = info.ThirdWeek; parms[15].Value = info.FourthWeek; parms[16].Value = info.FilingTrigger; parms[17].Value = info.Insufficient; parms[18].Value = info.HostingFilialeId; try { var result = SqlHelper.ExecuteNonQuery(GlobalConfig.ERP_DB_NAME, false, SQL, parms); return(result > 0); } catch (Exception ex) { errorMessage = ex.Message; throw new ApplicationException(ex.Message, 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); }