/// <summary> /// 获得常规报备天数 /// </summary> /// <returns></returns> public static int GetStockUpDays(PurchasingGoods pg) { int stockUpDay = 0; var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); //该月第一天 int startDateWeek = GetWeek(startDate); //该月第一天星期几 int nowDateWeek = GetWeek(DateTime.Now); //今天星期几 int weekOfMonth = GetWeekOfMonth(DateTime.Now); //当前第几周 if (nowDateWeek == 1 || nowDateWeek == 3) { if (nowDateWeek == 1) { //今天周一 if (startDateWeek != 1) { //该月第一天不是周一,当前第几周 - 1 weekOfMonth = weekOfMonth - 1; } } else if (nowDateWeek == 3) { //今天周三 if (startDateWeek > 3) { //该月第一天不是周三以及以上,当前第几周 - 1 weekOfMonth = weekOfMonth - 1; } } switch (weekOfMonth) { case 1: stockUpDay = pg.FirstWeek; break; case 2: stockUpDay = pg.SecondWeek; break; case 3: stockUpDay = pg.ThirdWeek; break; case 4: stockUpDay = pg.FourthWeek; break; } } return(stockUpDay); }
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); } } } }
/// <summary> /// 开始常规报备 /// </summary> public static void DoPurchasing(PurchasingGoods p, GoodsInfo goodsInfo, int avgStockDays, DateTime nextStockDate, TaskType taskType, int step, DateTime nextPurchasingDate, Dictionary <Guid, string> warehouseDics) { #if debug Console.WriteLine(""); Console.WriteLine("商品ID:{0}", p.GoodsId); Console.WriteLine("商品名称:{0}", p.GoodsName); if (p.WarehouseId != Guid.Empty) { Console.WriteLine("对应仓库:{0}", p.WarehouseName); } else { Console.WriteLine("对应仓库:全部仓库"); } #endif List <Guid> goodsIdList = _goodsCenterSao.GetRealGoodsIdsByGoodsId(p.GoodsId).ToList(); if (goodsIdList.Count == 0) { goodsIdList.Add(p.GoodsId); } //指定仓库报备 if (p.WarehouseId != Guid.Empty) { //获取销售总统计 var childGoodsSaleAll = GetChildGoodsSaleTotalByDays(goodsIdList, p.WarehouseId, p.StockingDays); //逐个算出计划采购 if (childGoodsSaleAll.Count > 0) { var stocks = WMSSao.GetStockStatisticsDtos(goodsIdList.ToDictionary(k => k, v => goodsInfo.GoodsType), p.WarehouseId, Guid.Empty); foreach (var childGoodsSalePurchasing in childGoodsSaleAll.GroupBy(act => act.HostingFilialeId)) { var key = childGoodsSalePurchasing.Key; //循环遍历子商品的销售和采购信息 OperationSaleAndPurchasing(p, childGoodsSalePurchasing, p.WarehouseId, key, avgStockDays, nextStockDate, taskType, step, nextPurchasingDate, stocks.ContainsKey(key) ? stocks[key] : new List <ERP.SAL.WMS.StockStatisticsDTO>()); } } else { Console.WriteLine("近期都没有销售记录!"); } #if debug #endif } else { //全仓库报备 foreach (var info in warehouseDics) { var stocks = WMSSao.GetStockStatisticsDtos(goodsIdList.ToDictionary(k => k, v => goodsInfo.GoodsType), p.WarehouseId, Guid.Empty); //获取销售总统计 var childGoodsSaleAll = GetChildGoodsSaleTotalByDays(goodsIdList, info.Key, p.StockingDays); foreach (var childGoodsSalePurchasing in childGoodsSaleAll.GroupBy(act => act.HostingFilialeId)) { var key = childGoodsSalePurchasing.Key; //循环遍历子商品的销售和采购信息 OperationSaleAndPurchasing(p, childGoodsSalePurchasing, info.Key, key, avgStockDays, nextStockDate, taskType, step, nextPurchasingDate, stocks.ContainsKey(key) ? stocks[key] : new List <ERP.SAL.WMS.StockStatisticsDTO>()); } } } #if debug { Console.WriteLine(""); Console.WriteLine("========================================================================"); Console.WriteLine(""); } #endif }
/// <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); } }
private static void CalculateStockDay(PurchasingGoods p, DateTime lastPurchasingDate, int filingDay, int stockDays, out bool isRun, out TaskType taskType, out int avgStockDays, out DateTime nextStockDate, out int step, out bool saveLastPurchasingDate, out DateTime nextPurchasingDate) { nextPurchasingDate = DateTime.MinValue; step = 0; isRun = false; taskType = TaskType.Routine; saveLastPurchasingDate = false; var addMonths = 3; if (stockDays == 30) { addMonths = 1; } else if (stockDays == 60) { addMonths = 2; } int nowDay = DateTime.Now.Day; avgStockDays = Convert.ToInt32(Math.Ceiling((double)stockDays / 4)); //计算出上一次虚拟的报备时间 if (lastPurchasingDate == DateTime.MinValue) { if (nowDay > filingDay) { lastPurchasingDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-") + filingDay); } else if (nowDay <= filingDay) { lastPurchasingDate = DateTime.Parse(DateTime.Now.AddMonths(-addMonths).ToString("yyyy-MM-") + filingDay); } //记录最后的采购运行时间 DataAccessor.UpdateLastPurchasingDate(p.WarehouseId, p.HostingFilialeId, p.GoodsId, lastPurchasingDate.Date); } //计算报备周期的天数间隔,并平均四等分 nextStockDate = lastPurchasingDate.AddMonths(addMonths); //开始匹配四次备货的时间 if (DateTime.Now.ToShortDateString() == lastPurchasingDate.AddDays(avgStockDays).ToShortDateString()) { nextPurchasingDate = DateTime.Now.AddDays(avgStockDays); taskType = TaskType.Warning; isRun = true; step = 2; } else if (DateTime.Now.ToShortDateString() == lastPurchasingDate.AddDays(avgStockDays * 2).ToShortDateString()) { nextPurchasingDate = DateTime.Now.AddDays(avgStockDays * 2); taskType = TaskType.Routine; isRun = true; step = 3; } else if (DateTime.Now.ToShortDateString() == lastPurchasingDate.AddDays(avgStockDays * 3).ToShortDateString()) { nextPurchasingDate = nextStockDate; taskType = TaskType.Warning; isRun = true; step = 4; } else if ((DateTime.Now - nextStockDate).Days == 0) { nextPurchasingDate = DateTime.Now.AddDays(avgStockDays); taskType = TaskType.Routine; isRun = true; saveLastPurchasingDate = true; nextStockDate = DateTime.Now.AddMonths(addMonths); step = 1; } //else if ((DateTime.Now - nextStockDate).Days > 0) //{ // var tempDay = p.FilingDay; // while (DateTime.Now.Day > tempDay) // { // tempDay += avgStockDays; // } // var tempDate = DateTime.Parse(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + tempDay); // nextPurchasingDate = tempDate; // taskType = TaskType.Routine; // isRun = true; // saveLastPurchasingDate = true; // nextStockDate = DateTime.Now.AddMonths(addMonths); // step = 1; //} }