public static void RunTask() { var pg = DataAccessor.GetPurchasingGoodsList(TaskType.All); var goodsIdList = pg.Select(w => w.GoodsId).Distinct().ToList(); IList <GoodsInfo> goodsList = _goodsCenterSao.GetGoodsListByGoodsIds(goodsIdList).ToList(); var warehouses = WMSSao.GetAllCanUseWarehouseDics(); foreach (var p in pg) { bool isRun; TaskType taskType; bool hasSaveLastPurchasingDate; int avgStockDays; DateTime nextStockDate; DateTime nextPurchasingDate; int step; GoodsInfo goodsInfo = goodsList.FirstOrDefault(w => w.GoodsId == p.GoodsId); if (goodsInfo == null) { continue; } p.GoodsName = goodsInfo.GoodsName; p.GoodsCode = goodsInfo.GoodsCode; p.Units = goodsInfo.Units; if (goodsInfo.ExpandInfo != null) { p.PackQuantity = goodsInfo.ExpandInfo.PackCount; } //计算报备日期是否匹配 CalculateStockDay(p, p.LastPurchasingDate, p.FilingDay, p.StockingDays, out isRun, out taskType, out avgStockDays, out nextStockDate, out step, out hasSaveLastPurchasingDate, out nextPurchasingDate); if (isRun) { Routine.DoPurchasing(p, goodsInfo, avgStockDays, nextStockDate, taskType, step, nextPurchasingDate, warehouses); } if (hasSaveLastPurchasingDate) { //记录最后的采购运行时间 DataAccessor.UpdateLastPurchasingDate(p.WarehouseId, p.HostingFilialeId, p.GoodsId, DateTime.Now); } } }
/// <summary> /// 获取商品的总销售统计信息 /// </summary> /// <param name="goodsIdList"></param> /// <param name="warehouseId"></param> /// <param name="stockUpDays"></param> /// <returns></returns> public static IList <ChildGoodsSalePurchasing> GetChildGoodsSaleTotalByDays(List <Guid> goodsIdList, Guid warehouseId, int stockUpDays) { var dateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); var total = DataAccessor.GetChildGoodsSaleList(goodsIdList, warehouseId, dateTime.AddDays(-110), dateTime); var childGoodsSaleAll = new List <ChildGoodsSalePurchasing>(); var totalSales = DataAccessor.GetSaleDays(goodsIdList, warehouseId, dateTime); foreach (var guid in goodsIdList) { var realList = total.Where(act => act.GoodsId == guid); //计算前第一个备货周期的销售额 var childSaleList1 = realList.Where(act => act.DayTime >= dateTime.AddDays(-30) && act.DayTime < dateTime); //计算前第二个备货周期的销售额 var childSaleList2 = realList.Where(act => act.DayTime >= dateTime.AddDays(-60) && act.DayTime < dateTime.AddDays(30)); //计算前第三个备货周期的销售额 var childSaleList3 = realList.Where(act => act.DayTime >= dateTime.AddDays(-90) && act.DayTime < dateTime.AddDays(-60)); foreach (var hostingFilialeGroup in realList.GroupBy(act => act.HostingFilialeId)) { var filialeSales = totalSales.Where(act => act.RealGoodsId == guid && act.HostingFilialeId == hostingFilialeGroup.Key); childGoodsSaleAll.Add(new ChildGoodsSalePurchasing { GoodsId = guid, FirstNumberOneStockUpSale = childSaleList1.Where(act => act.HostingFilialeId == hostingFilialeGroup.Key).Sum(act => act.SaleQuantity), FirstNumberTwoStockUpSale = childSaleList2.Where(act => act.HostingFilialeId == hostingFilialeGroup.Key).Sum(act => act.SaleQuantity), FirstNumberThreeStockUpSale = childSaleList3.Where(act => act.HostingFilialeId == hostingFilialeGroup.Key).Sum(act => act.SaleQuantity), Specification = hostingFilialeGroup.First().Specification, HostingFilialeId = hostingFilialeGroup.Key, PerStepSales = GetPreStepSales(realList, dateTime, filialeSales.Max(act => act.Days)) }); } } return(childGoodsSaleAll); }
public static void RunTask() { var now = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); int week = GetWeek(now); if (week == 1 || week == 3) { #region --> 今天周一或周三 var pg = DataAccessor.GetPurchasingGoodsList(TaskType.All); var goodsIdList = pg.Select(w => w.GoodsId).Distinct().ToList(); IList <GoodsInfo> goodsList = _goodsCenterSao.GetGoodsListByGoodsIds(goodsIdList).ToList(); if (goodsList.Count == 0) { return; } var dictPurchase = new Dictionary <PurchasingInfo, List <PurchasingDetailInfo> >(); var purchasingSets = new List <PurchasingGoods>(); pg = pg.Where(w => w.StockUpDay == week && w.HostingFilialeId != Guid.Empty).ToList(); var goodsDics = goodsList.ToDictionary(k => k.GoodsId, v => v); var realGoodsDics = new Dictionary <Guid, GoodsInfo>(); Dictionary <Guid, List <Guid> > goodsWithRealGoods = new Dictionary <Guid, List <Guid> >(); foreach (var warehouseIdGroup in pg.GroupBy(act => new { act.WarehouseId, act.HostingFilialeId })) { var warehouseId = warehouseIdGroup.Key.WarehouseId; var hostingFilialeId = warehouseIdGroup.Key.HostingFilialeId; var stockStatistics = WMSSao.GetStockStatisticsDtosForAuto(warehouseId, hostingFilialeId); var planPurchasingGoods1 = DataAccessor.GetAllSumPurchasingQuantity(warehouseId, hostingFilialeId).ToDictionary(k => k.GoodsID, v => v.PurchasingQuantity); foreach (var companyIdGroup in warehouseIdGroup.GroupBy(act => act.CompanyId)) { var purchaseGroupIds = companyIdGroup.Select(w => w.PurchaseGroupId).Distinct().ToList(); foreach (var purchaseGroupId in purchaseGroupIds) { //采购分组 var pgList = companyIdGroup.Where(w => w.PurchaseGroupId == purchaseGroupId).ToList(); var personResponsibles = pgList.Select(w => w.PersonResponsible).Distinct().ToList(); foreach (var personResponsible in personResponsibles) { var prList = pgList.Where(w => w.PersonResponsible == personResponsible).ToList(); if (prList.Count > 0) { foreach (var prInfo in prList.Where(ent => goodsDics.ContainsKey(ent.GoodsId))) { GoodsInfo goodsInfo = goodsDics[prInfo.GoodsId]; if (goodsInfo == null) { continue; } prInfo.GoodsName = goodsInfo.GoodsName; prInfo.GoodsCode = goodsInfo.GoodsCode; prInfo.Units = goodsInfo.Units; if (goodsInfo.ExpandInfo != null) { prInfo.PackQuantity = goodsInfo.ExpandInfo.PackCount; } List <Guid> realGoodsIdList; if (goodsWithRealGoods.ContainsKey(prInfo.GoodsId)) { realGoodsIdList = goodsWithRealGoods[prInfo.GoodsId]; } else { realGoodsIdList = _goodsCenterSao.GetRealGoodsIdsByGoodsId(prInfo.GoodsId).ToList(); if (realGoodsIdList.Count == 0) { realGoodsIdList.Add(prInfo.GoodsId); } goodsWithRealGoods.Add(prInfo.GoodsId, realGoodsIdList); } int stockUpDays = prInfo.FilingForm == 1 ? GetStockUpDays(prInfo) : prInfo.FilingTrigger; if (stockUpDays == 0) { continue; } var childGoodsSaleAll = GetChildGoodsSaleTotalByDays(realGoodsIdList, new List <Guid> { prInfo.WarehouseId }, stockUpDays, now); if (childGoodsSaleAll.Count > 0) { foreach (var sale in childGoodsSaleAll) { sale.PlanPurchasingquantity = sale.WeightedAverageSaleQuantity * stockUpDays; if (sale.PlanPurchasingquantity > 0) { //个位数0<x<=5向上取为5,个位数为6<x<=9向上取整为10 char[] temp = sale.PlanPurchasingquantity.ToString(CultureInfo.InvariantCulture).ToArray(); int unitsDigit = Convert.ToInt32(temp[temp.Length - 1]); if (unitsDigit > 0 && unitsDigit < 5) { sale.PlanPurchasingquantity += 5 - unitsDigit; } else if (unitsDigit > 5) { sale.PlanPurchasingquantity += 10 - unitsDigit; } } //计算当前的采购商品数量,包含扣除的已经采购完成和部分采购完成和赠品类型 sale.SubtractPurchasingQuantity = planPurchasingGoods1.ContainsKey(sale.GoodsId)? planPurchasingGoods1[sale.GoodsId] : 0; //计算当前仓库存货数量 sale.NonceWarehouseStockQuantity = stockStatistics.Where(ent => ent.RealGoodsId == sale.GoodsId).Sum(info => info.CurrentStock + info.UppingQuantity - info.RequireQuantity - info.SubtotalQuantity); if (prInfo.FilingForm == 2) { //2触发报备 //计算是否满足不足数量 double planQuantity = sale.WeightedAverageSaleQuantity * prInfo.Insufficient; double realityQuantity = Math.Ceiling(planQuantity - sale.SubtractPurchasingQuantity - sale.NonceWarehouseStockQuantity); if (realityQuantity <= 0) { //无需报备 continue; } } if (sale.RealityNeedPurchasingQuantity > 0) { var salesinfo = DataAccessor.GetChildGoodsSale(sale.GoodsId, warehouseId, sale.HostingFilialeId, DateTime.Now); PurchasingInfo existingPurchasingInfo = null; bool isExist = false; if (dictPurchase.Keys.Count > 0) { existingPurchasingInfo = dictPurchase.Keys.FirstOrDefault(act => act.PersonResponsible == personResponsible && act.PurchasingFilialeId == hostingFilialeId && act.WarehouseID == warehouseId && companyIdGroup.Key == act.CompanyID); } var detailList = new List <PurchasingDetailInfo>(); if (existingPurchasingInfo == null) { existingPurchasingInfo = new PurchasingInfo { PurchasingID = Guid.NewGuid(), CompanyID = companyIdGroup.Key, CompanyName = prList[0].CompanyName, WarehouseID = warehouseId, PurchasingState = (int)PurchasingState.NoSubmit, PurchasingType = (int)PurchasingType.AutoStock, PersonResponsible = personResponsible, PurchaseGroupId = purchaseGroupId, StartTime = DateTime.Now, EndTime = DateTime.MaxValue, Description = string.Format("[采购类别:{0};系统自动报备]", EnumAttribute.GetKeyName(PurchasingType.Custom)), FilialeID = hostingFilialeId, PurchasingFilialeId = hostingFilialeId }; } else { isExist = true; detailList = dictPurchase[existingPurchasingInfo]; } var purchasingDetailInfo = new PurchasingDetailInfo { PurchasingID = existingPurchasingInfo.PurchasingID, GoodsID = sale.GoodsId, GoodsName = prInfo.GoodsName, GoodsCode = prInfo.GoodsCode, Specification = sale.Specification, CompanyID = companyIdGroup.Key, Price = prInfo.Price, PlanQuantity = sale.RealityNeedPurchasingQuantity, RealityQuantity = 0, State = 0, Units = prInfo.Units, PurchasingGoodsID = Guid.NewGuid(), SixtyDaySales = salesinfo.SixtyDaySales, ThirtyDaySales = salesinfo.ThirtyDaySales, ElevenDaySales = salesinfo.ElevenDaySales, CPrice = prInfo.Price }; detailList.Add(purchasingDetailInfo); if (!realGoodsDics.ContainsKey(sale.GoodsId)) { realGoodsDics.Add(sale.GoodsId, goodsInfo); } if (detailList.Count > 0 && !isExist) { dictPurchase.Add(existingPurchasingInfo, detailList); } } } if (!purchasingSets.Any(act => act.WarehouseId == warehouseId && act.HostingFilialeId == hostingFilialeId && act.GoodsId == prInfo.GoodsId)) { purchasingSets.Add(prInfo); } } } } } } } } PurchasingPromotion(dictPurchase, realGoodsDics, purchasingSets); #endregion } }
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> /// <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; //} }