private void GenMiRccp(DateTime planVersion, CodeMaster.TimeUnit dateType, string dateIndex, IList<MrpFlowDetail> mrpFlowDetailList, List<RccpTransGroup> rccpTransGroupList, BusinessException businessException) { var itemDiscontinueList = this.genericMgr.FindAll<ItemDiscontinue>(); var rccpMiPlanList = new List<RccpMiPlan>(); var workCalendars = this.genericMgr.FindAll<WorkCalendar> (@" from WorkCalendar as w where w.DateType =? and w.ResourceGroup=? and w.DateIndex between ? and ? ", new object[] { dateType, CodeMaster.ResourceGroup.MI, dateIndex, rccpTransGroupList.Max(p => p.DateIndex) }); double cleanTime = double.Parse(systemMgr.GetEntityPreferenceValue(Entity.SYS.EntityPreference.CodeEnum.MiCleanTime)); var miFlowDetailList = mrpFlowDetailList.Where(p => p.ResourceGroup == CodeMaster.ResourceGroup.MI); foreach(var groupRccpTrans in rccpTransGroupList) { DateTime dateFrom = DateTime.Now; if(dateType == CodeMaster.TimeUnit.Week) { dateFrom = Utility.DateTimeHelper.GetWeekIndexDateFrom(groupRccpTrans.DateIndex); } else { dateFrom = DateTime.Parse(groupRccpTrans.DateIndex + "-01"); } var mrpFlowDetail = miFlowDetailList.FirstOrDefault(p => p.ResourceGroup == CodeMaster.ResourceGroup.MI && p.Item == groupRccpTrans.Item && p.StartDate <= dateFrom && p.EndDate > dateFrom); if(mrpFlowDetail != null) { var workCalendar = workCalendars.FirstOrDefault(p => p.DateIndex == groupRccpTrans.DateIndex && p.Flow == mrpFlowDetail.Flow); var miPlan = new RccpMiPlan(); DateTime planDate = DateTime.Now; if(workCalendar != null) { miPlan.HaltTime = workCalendar.HaltTime * 24 * 60; miPlan.TrialProduceTime = workCalendar.TrialTime * 24 * 60; miPlan.Holiday = workCalendar.Holiday * 24 * 60; miPlan.UpTime = workCalendar.UpTime * 24 * 60; if(dateType == CodeMaster.TimeUnit.Month) { planDate = DateTime.Parse(groupRccpTrans.DateIndex + "-01"); //miPlan.UpTime = DateTime.DaysInMonth(planDate.Year, planDate.Month) * 24 * 60 * ((8 * 60 - cleanTime) / (8 * 60)); //miPlan.UpTime = workCalendar.UpTime * 24 * 60; } else if(dateType == CodeMaster.TimeUnit.Week) { planDate = Utility.DateTimeHelper.GetWeekIndexDateFrom(groupRccpTrans.DateIndex); //miPlan.HaltTime = (7 - workCalendar.Holiday) * 24 * 60 * (cleanTime / (8 * 60)); //miPlan.UpTime = (7 - workCalendar.Holiday) * 24 * 60 * ((8 * 60 - cleanTime) / (8 * 60)); } } else { //出错 businessException.AddMessage("没有找到炼胶的工作日历"); } miPlan.ProductLine = mrpFlowDetail.Flow; miPlan.Item = groupRccpTrans.Item; miPlan.DateIndex = groupRccpTrans.DateIndex; miPlan.DateType = dateType; miPlan.PlanVersion = planVersion; miPlan.Qty = groupRccpTrans.Qty; var miItem = this.itemMgr.GetCacheItem(groupRccpTrans.Item); if(miItem == null) { businessException.AddMessage(new Message(CodeMaster.MessageType.Error, string.Format("没有找到此物料{0}的对应的工时", groupRccpTrans.Item))); } else { miPlan.WorkHour = miItem.WorkHour; } miPlan.CheRateQty = mrpFlowDetail.UnitCount; //替代物料 var itemDiscontinues = itemDiscontinueList.Where(p => p.Item == miPlan.Item && p.StartDate <= planDate && (!p.EndDate.HasValue || (p.EndDate.HasValue && p.EndDate.Value > planDate))).OrderBy(p => p.Priority).ToList(); var items = new List<string>(); items.Add(miPlan.Item); items.AddRange(itemDiscontinues.Select(p => p.DiscontinueItem)); //可委外的物料 var flowDetail = mrpFlowDetailList.FirstOrDefault(f => f.Type == CodeMaster.OrderType.SubContract && items.Contains(f.Item)); if(flowDetail != null) { miPlan.SubFlowDetail = flowDetail; } rccpMiPlanList.Add(miPlan); //this.genericMgr.Create(miPlan); } } var groupMiPlans = (from p in rccpMiPlanList group p by new { p.ProductLine, p.DateIndex, p.UpTime } into g select new { ProductLine = g.Key.ProductLine, DateIndex = g.Key.DateIndex, UpTime = g.Key.UpTime, List = g }).OrderBy(p => p.DateIndex).ThenBy(p => p.ProductLine); var purchasePlanList = new List<PurchasePlan>(); foreach(var groupMiPlan in groupMiPlans) { double requireTime = groupMiPlan.List.Sum(p => p.RequireTime); double currentTime = requireTime - groupMiPlan.UpTime; if(currentTime > 0) { DateTime dateFrom = DateTime.Now; if(dateType == CodeMaster.TimeUnit.Week) { dateFrom = Utility.DateTimeHelper.GetWeekIndexDateFrom(groupMiPlan.DateIndex); } else { dateFrom = DateTime.Parse(groupMiPlan.DateIndex + "-01"); } foreach(var plan in groupMiPlan.List) { if(plan.SubFlowDetail != null) { double currentQty = (currentTime / plan.WorkHour) * plan.CheRateQty; double subQty = currentQty > plan.Qty ? plan.Qty : currentQty; currentTime = currentTime - (subQty / plan.CheRateQty) * plan.WorkHour; var purchasePlan = new PurchasePlan(); purchasePlan.Item = plan.Item; //purchasePlan.Sequence = plan.SubFlowDetail.Sequence; purchasePlan.Flow = plan.SubFlowDetail.Flow; purchasePlan.LocationTo = plan.SubFlowDetail.LocationTo; purchasePlan.OrderType = plan.SubFlowDetail.Type; purchasePlan.WindowTime = dateFrom; purchasePlan.StartTime = dateFrom.AddHours(-plan.SubFlowDetail.LeadTime); purchasePlan.Qty = subQty; purchasePlan.PlanQty = subQty; purchasePlan.DateType = dateType; purchasePlan.PlanVersion = planVersion; purchasePlanList.Add(purchasePlan); plan.SubQty = subQty; plan.Qty -= subQty; //auto update } } } } #region Create this.genericMgr.BulkInsert<RccpMiPlan>(rccpMiPlanList); this.genericMgr.BulkInsert<PurchasePlan>(purchasePlanList); #endregion Create }
private void GenPurchaseRccp(DateTime planVersion, CodeMaster.TimeUnit dateType, BusinessException businessException, IList<MrpFlowDetail> mrpFlowDetailList, IEnumerable<RccpTransGroup> rccpTransGroupList, DateTime snapTime, User user) { if(businessException.GetMessages().Where(p => p.MessageType == CodeMaster.MessageType.Error).Count() > 0) { //如果有错误,退出,不产生采购物料需求 return; } var flowDetails = mrpFlowDetailList .Where(p => p.Type == CodeMaster.OrderType.Procurement || p.Type == CodeMaster.OrderType.CustomerGoods || p.Type == CodeMaster.OrderType.SubContract || p.Type == CodeMaster.OrderType.ScheduleLine); var purchasePlanList = new List<PurchasePlan>(); var rccpTransGroupByIndexList = (from p in rccpTransGroupList group p by p.DateIndex into g select new { DateIndex = g.Key, List = g }).OrderBy(p => p.DateIndex).ToList(); foreach(var rccpTransGroupByIndex in rccpTransGroupByIndexList) { DateTime windowTime = DateTime.Now; if(dateType == CodeMaster.TimeUnit.Week) { windowTime = DateTimeHelper.GetWeekIndexDateFrom(rccpTransGroupByIndex.DateIndex); } else if(dateType == CodeMaster.TimeUnit.Month) { windowTime = DateTime.Parse(rccpTransGroupByIndex.DateIndex + "-01"); } var mrpFlowDetailDic = flowDetails.Where(p => p.StartDate <= windowTime && p.EndDate > windowTime) .GroupBy(p => p.Item, (k, g) => new { k, g }) .ToDictionary(d => d.k, d => d.g); foreach(var groupRccpTrans in rccpTransGroupByIndex.List) { var mrpFlowDetails = mrpFlowDetailDic.ValueOrDefault(groupRccpTrans.Item); if(mrpFlowDetails != null) { foreach(var mrpFlowDetail in mrpFlowDetails) { var purchasePlan = new PurchasePlan(); purchasePlan.Item = groupRccpTrans.Item; //purchasePlan.Sequence = mrpFlowDetail.Sequence; purchasePlan.Flow = mrpFlowDetail.Flow; purchasePlan.LocationTo = mrpFlowDetail.LocationTo; purchasePlan.OrderType = mrpFlowDetail.Type; purchasePlan.WindowTime = windowTime; var leadDay = Utility.DateTimeHelper.TimeTranfer((decimal)mrpFlowDetail.LeadTime, CodeMaster.TimeUnit.Hour, CodeMaster.TimeUnit.Day); if(dateType == CodeMaster.TimeUnit.Week) { purchasePlan.StartTime = purchasePlan.WindowTime.AddDays(3).AddDays(-leadDay); purchasePlan.StartTime = Utility.DateTimeHelper.GetWeekStart(purchasePlan.StartTime); } else { purchasePlan.StartTime = purchasePlan.WindowTime.AddDays(15).AddDays(-leadDay); purchasePlan.StartTime = Utility.DateTimeHelper.GetStartTime(CodeMaster.TimeUnit.Month, purchasePlan.StartTime); } purchasePlan.Qty = (mrpFlowDetail.MrpWeight / mrpFlowDetails.Sum(p => p.MrpWeight)) * groupRccpTrans.Qty; purchasePlan.PlanQty = purchasePlan.Qty; purchasePlan.DateType = dateType; purchasePlan.PlanVersion = planVersion; purchasePlanList.Add(purchasePlan); } } else { if(groupRccpTrans.IsLastLevel) { businessException.AddMessage(new Message(CodeMaster.MessageType.Warning, "没有找到物料{0}的采购路线", groupRccpTrans.Item)); } } } } string hql = string.Empty; if(dateType == CodeMaster.TimeUnit.Week) { hql = "from FlowStrategy where IsCheckMrpWeeklyPlan =? and Flow in(?"; } else if(dateType == CodeMaster.TimeUnit.Month) { hql = "from FlowStrategy where IsCheckMrpMonthlyPlan =? and Flow in(?"; } var flowStategys = this.genericMgr.FindAllIn<FlowStrategy> (hql, purchasePlanList.Select(p => p.Flow).Where(p => !string.IsNullOrWhiteSpace(p)).Distinct(), new object[] { true }); var flowMasterDic = this.genericMgr.FindAllIn<FlowMaster> ("from FlowMaster where Code in(?", flowStategys.Select(p => p.Flow).Distinct()) .GroupBy(p => p.Code, (k, g) => new { k, g.First().Description }) .ToDictionary(d => d.k, d => d.Description); foreach(var flowStategy in flowStategys) { PurchasePlanMaster purchasePlanMaster = new PurchasePlanMaster(); purchasePlanMaster.DateType = dateType; purchasePlanMaster.Flow = flowStategy.Flow; purchasePlanMaster.FlowDescription = flowMasterDic[flowStategy.Flow]; purchasePlanMaster.PlanVersion = planVersion; purchasePlanMaster.SnapTime = snapTime; purchasePlanMaster.SourcePlanVersion = snapTime; purchasePlanMaster.CreateUserId = user.Id; purchasePlanMaster.CreateUserName = user.FullName; purchasePlanMaster.CreateDate = DateTime.Now; purchasePlanMaster.LastModifyUserId = user.Id; purchasePlanMaster.LastModifyUserName = user.FullName; purchasePlanMaster.LastModifyDate = DateTime.Now; this.genericMgr.Create(purchasePlanMaster); } purchasePlanList = purchasePlanList.Where(p => flowStategys.Select(q => q.Flow).Contains(p.Flow)).ToList(); this.genericMgr.BulkInsert<PurchasePlan>(purchasePlanList); }
public ActionResult _Update(GridCommand command, PurchasePlan purchasePlan, DateTime? NplanVersion, string NplanDate, string Nflow, string Nitem, bool? NisStartTime, int NDateType) { MaterailPlanSearchModel searchModel = new MaterailPlanSearchModel(); searchModel.Flow = Nflow; searchModel.IsStartTime = NisStartTime.HasValue ? NisStartTime.Value : false; searchModel.Item = Nitem; searchModel.PlanDate = NplanDate; searchModel.PlanVersion = NplanVersion; searchModel.DateType = NDateType; PurchasePlan newPurchasePlan = genericMgr.FindById<PurchasePlan>(purchasePlan.Id); newPurchasePlan.Qty = purchasePlan.CurrentQty; genericMgr.Update(newPurchasePlan); SearchStatementModel searchStatementModel = PrepareSearchStatement(command, searchModel); GridModel<PurchasePlan> purchasePlanList = GetAjaxPageData<PurchasePlan>(searchStatementModel, command); foreach (var purchasePlanEntity in purchasePlanList.Data) { purchasePlanEntity.CurrentQty = purchasePlanEntity.Qty; purchasePlanEntity.ItemDescription = this.itemMgr.GetCacheItem(purchasePlanEntity.Item).Description; } return PartialView(purchasePlanList); }