Procházet zdrojové kódy

Merge branch 'dev' of http://123.60.180.165:4647/ZZYDOP/DOPCore into dev

tangdi před 2 roky
rodič
revize
be1087a073

+ 382 - 217
MicroServices/Business/Business.Application/ReplenishmentManagement/ReplenishmentAppService.cs

@@ -1058,7 +1058,7 @@ namespace Business.Replenishment
             }
             planList = planList.OrderBy(p => p.PlanMonth).ThenBy(o => o.OrderNum).ToList();
             //获取BOM用于分解到原材料
-            var boms = _ic_bom.GetListAsync(a => planList.Select(p => p.SAPItemNumber).ToList().Contains(a.item_number) && a.factory_id == input.factory_id).Result;
+            var boms = _ic_bom.GetListAsync(a => planList.Select(p => p.SAPItemNumber).Distinct().ToList().Contains(a.item_number) && a.factory_id == input.factory_id).Result;
             var planItemList = planList.Select(a => a.SAPItemNumber).Distinct().ToList();
             var bomItemList=boms.Select(a=>a.item_number).Distinct().ToList();
             var expectedList = planItemList.Except(bomItemList).ToList();
@@ -1877,7 +1877,7 @@ namespace Business.Replenishment
             //中间件汇总,比如A成品10、11、12月都需要中间件C,B成品11、12月都需要中间件C,那么中间件C需要每个月汇总平均分到4周
             Dictionary<string, decimal?> subProductItem = new Dictionary<string, decimal?>();
             Dictionary<string, BomChildExamineDto> subProductItemModel = new Dictionary<string, BomChildExamineDto>();
-            var ropList = planList.Where(t => shipPlanList.Select(s => s.SAPItemNumber).ToList().Contains(t.number)).Select(t => t.number).Distinct().ToList();
+            var ropList = planList.Where(t => shipPlanList.Select(s => s.SAPItemNumber).Distinct().ToList().Contains(t.number)).Select(t => t.number).Distinct().ToList();
             var itemMasterList = _itemMaster.Select(t => ropList.Contains(t.ItemNum) && t.Domain == input.factory_id.ToString());
             Dictionary<string, decimal> minordsalesQty = new Dictionary<string, decimal>();
             List<mes_morder> moList = new List<mes_morder>();
@@ -1914,7 +1914,7 @@ namespace Business.Replenishment
                             a.moentry_sys_etime == GetNextMonday().AddDays(28 + 7).AddDays(-days) ||
                             a.moentry_sys_etime == GetNextMonday().AddDays(28 + 14).AddDays(-days) ||
                             a.moentry_sys_etime == GetNextMonday().AddDays(28 + 21).AddDays(-days)));
-                            if(MonthMo != null)
+                            if (MonthMo != null)
                             {
                                 MonthMo.morder_production_number += Math.Ceiling(itemQty.GetValueOrDefault() / 4);
                                 MonthMo.need_number += Math.Ceiling(itemQty.GetValueOrDefault() / 4);
@@ -2045,45 +2045,79 @@ namespace Business.Replenishment
                                     moentryList.Add(mes_Moentry);
                                 }
                             }
-
-                            if(k==3)
+                            if (k == 3)
                             {
-                                var itemMaster = itemMasterList.Find(i => i.ItemNum == itemSeq);
-                                if (itemMaster != null && itemMaster.MinOrd > 0)
+
+                                var MonthMoTotal = moList.Find(a => a.product_code == itemSeq && (
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28).AddDays(-days) ||
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 7).AddDays(-days) ||
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 14).AddDays(-days) ||
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 21).AddDays(-days)));
+                                if (MonthMoTotal != null)
                                 {
-                                    var MonthMoTotal = moList.Find(a => a.product_code == itemSeq && (
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28).AddDays(-days) ||
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 7).AddDays(-days) ||
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 14).AddDays(-days) ||
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 21).AddDays(-days)));
-                                    if (MonthMoTotal != null)
+                                    if (MonthMoTotal.need_number > 0)
                                     {
-                                        //考虑生产批量的计算逻辑
-                                        decimal productQty;
-                                        bool exist = minordsalesQty.TryGetValue(itemSeq, out productQty);
-                                        if (exist)
+                                        var itemMaster = itemMasterList.Find(i => i.ItemNum == itemSeq);
+                                        if (itemMaster != null && itemMaster.MinOrd > 0)
                                         {
-                                            //不需要生产
-                                            if (MonthMoTotal.need_number <= productQty)
+                                            //考虑生产批量的计算逻辑
+                                            decimal productQty;
+                                            bool exist = minordsalesQty.TryGetValue(itemSeq, out productQty);
+                                            if (exist)
                                             {
-                                                minordsalesQty[itemSeq] -= MonthMoTotal.need_number.GetValueOrDefault();
-                                                moList.Remove(MonthMoTotal);
-                                                moentryList.RemoveAll(a => a.moentry_mono == MonthMoTotal.morder_no);
-                                                weekPlan.RemoveAll(a => a.ProductionOrder == MonthMoTotal.morder_no);
+                                                //不需要生产
+                                                if (MonthMoTotal.need_number <= productQty)
+                                                {
+                                                    minordsalesQty[itemSeq] -= MonthMoTotal.need_number.GetValueOrDefault();
+                                                    moList.Remove(MonthMoTotal);
+                                                    moentryList.RemoveAll(a => a.moentry_mono == MonthMoTotal.morder_no);
+                                                    weekPlan.RemoveAll(a => a.ProductionOrder == MonthMoTotal.morder_no);
+                                                }
+                                                else
+                                                {
+                                                    //需要生产但是小于生产批量按生产批量生产,大于生产批量不需要改动
+                                                    if (MonthMoTotal.need_number - productQty < itemMaster.MinOrd)
+                                                    {
+                                                        decimal needProductQty = MonthMoTotal.need_number.GetValueOrDefault() - productQty;
+                                                        minordsalesQty[itemSeq] = Math.Ceiling(itemMaster.MinOrd) - needProductQty;
+                                                        MonthMoTotal.morder_production_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        MonthMoTotal.moentry_sys_stime = GetNextMonday().AddDays(28).AddDays(14).AddDays(-ProductTime(routingOps, MonthMoTotal.product_code, input.factory_id.ToString(), MonthMoTotal.need_number.GetValueOrDefault()) - days);
+                                                        MonthMoTotal.moentry_sys_etime = GetNextMonday().AddDays(28).AddDays(14).AddDays(-days);
+                                                        moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).morder_production_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).need_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanStartDate = MonthMoTotal.moentry_sys_stime;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Week = $"WK{GetWeekOfYear(MonthMoTotal.moentry_sys_stime.Value).ToString("00")}";
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Qty = MonthMoTotal.morder_production_number.Value;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Year = GetNextMonday().AddDays(28).AddDays(14).Year;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Month = GetNextMonday().AddDays(28).AddDays(14).Month;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).WeekSeq = WeekOfMonth(GetNextMonday().AddDays(28).AddDays(14), 1);
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanMonth = $"{GetNextMonday().AddDays(28).AddDays(14).Year}-{GetNextMonday().AddDays(28).AddDays(14).Month.ToString("00")}";
+                                                        if (item.area == "国内" || item.area == "中国")
+                                                        {
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
+                                                        }
+                                                        else
+                                                        {
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
+                                                        }
+                                                    }
+                                                }
                                             }
                                             else
                                             {
-                                                //需要生产但是小于生产批量按生产批量生产,大于生产批量不需要改动
-                                                if (MonthMoTotal.need_number - productQty < itemMaster.MinOrd)
+                                                if (MonthMoTotal.need_number < Math.Ceiling(itemMaster.MinOrd))
                                                 {
-                                                    decimal needProductQty = MonthMoTotal.need_number.GetValueOrDefault() - productQty;
-                                                    minordsalesQty[itemSeq] = Math.Ceiling(itemMaster.MinOrd) - needProductQty;
+                                                    minordsalesQty[itemSeq] = Math.Ceiling(itemMaster.MinOrd) - MonthMoTotal.need_number.Value;
+                                                    MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
                                                     MonthMoTotal.morder_production_number = Math.Ceiling(itemMaster.MinOrd);
                                                     MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
                                                     MonthMoTotal.moentry_sys_stime = GetNextMonday().AddDays(28).AddDays(14).AddDays(-ProductTime(routingOps, MonthMoTotal.product_code, input.factory_id.ToString(), MonthMoTotal.need_number.GetValueOrDefault()) - days);
                                                     MonthMoTotal.moentry_sys_etime = GetNextMonday().AddDays(28).AddDays(14).AddDays(-days);
-                                                    moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).morder_production_number= Math.Ceiling(itemMaster.MinOrd);
-                                                    moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).need_number= Math.Ceiling(itemMaster.MinOrd);
+                                                    moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).morder_production_number = Math.Ceiling(itemMaster.MinOrd);
+                                                    moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).need_number = Math.Ceiling(itemMaster.MinOrd);
                                                     weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanStartDate = MonthMoTotal.moentry_sys_stime;
                                                     weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Week = $"WK{GetWeekOfYear(MonthMoTotal.moentry_sys_stime.Value).ToString("00")}";
                                                     weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Qty = MonthMoTotal.morder_production_number.Value;
@@ -2103,62 +2137,63 @@ namespace Business.Replenishment
                                                     }
                                                 }
                                             }
-                                        }
-                                        else
-                                        {
-                                            if(MonthMoTotal.need_number< Math.Ceiling(itemMaster.MinOrd))
+                                            var planBOM = boms.Find(b => b.item_number == itemSeq);
+                                            var pretreament = pretreatments.Where(c => c.sourceid == planBOM.mysql_id).ToList();
+                                            var returnlist = ObjectMapper.Map<List<b_bom_pretreatment>, List<BomChildExamineDto>>(pretreament);
+                                            returnlist = returnlist.OrderBy(s => s.num_order).ToList();
+                                            var level1Dto = returnlist[0];
+                                            level1Dto.needCount = MonthMoTotal.need_number.GetValueOrDefault();
+                                            CaclMaterialShortage(returnlist);
+                                            foreach (var r in returnlist)
                                             {
-                                                minordsalesQty[itemSeq] += Math.Ceiling(itemMaster.MinOrd) - MonthMoTotal.need_number.Value;
-                                                MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
-                                                MonthMoTotal.morder_production_number = Math.Ceiling(itemMaster.MinOrd);
-                                                MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
-                                                MonthMoTotal.moentry_sys_stime = GetNextMonday().AddDays(28).AddDays(14).AddDays(-ProductTime(routingOps, MonthMoTotal.product_code, input.factory_id.ToString(), MonthMoTotal.need_number.GetValueOrDefault()) - days);
-                                                MonthMoTotal.moentry_sys_etime = GetNextMonday().AddDays(28).AddDays(14).AddDays(-days);
-                                                moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).morder_production_number = Math.Ceiling(itemMaster.MinOrd);
-                                                moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).need_number = Math.Ceiling(itemMaster.MinOrd);
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanStartDate = MonthMoTotal.moentry_sys_stime;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Week = $"WK{GetWeekOfYear(MonthMoTotal.moentry_sys_stime.Value).ToString("00")}";
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Qty = MonthMoTotal.morder_production_number.Value;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Year = GetNextMonday().AddDays(28).AddDays(14).Year;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Month = GetNextMonday().AddDays(28).AddDays(14).Month;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).WeekSeq = WeekOfMonth(GetNextMonday().AddDays(28).AddDays(14), 1);
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanMonth = $"{GetNextMonday().AddDays(28).AddDays(14).Year}-{GetNextMonday().AddDays(28).AddDays(14).Month.ToString("00")}";
-                                                if (item.area == "国内" || item.area == "中国")
+                                                if (r.item_number != itemSeq && r.erp_cls == 1 && !string.IsNullOrEmpty(r.bom_number))
                                                 {
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
-                                                }
-                                                else
-                                                {
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
+                                                    if (subProductItem.ContainsKey(r.item_number))
+                                                    {
+                                                        subProductItem[r.item_number] += r.needCount;
+                                                    }
+                                                    else
+                                                    {
+                                                        subProductItem.Add(r.item_number, r.needCount);
+                                                        subProductItemModel.Add(r.item_number, r);
+                                                    }
                                                 }
                                             }
                                         }
-                                        var planBOM = boms.Find(b => b.item_number ==itemSeq);
-                                        var pretreament = pretreatments.Where(c => c.sourceid == planBOM.mysql_id).ToList();
-                                        var returnlist = ObjectMapper.Map<List<b_bom_pretreatment>, List<BomChildExamineDto>>(pretreament);
-                                        returnlist = returnlist.OrderBy(s => s.num_order).ToList();
-                                        var level1Dto = returnlist[0];
-                                        level1Dto.needCount = MonthMoTotal.need_number.GetValueOrDefault();
-                                        CaclMaterialShortage(returnlist);
-                                        foreach (var r in returnlist)
+                                        else
                                         {
-                                            if (r.item_number != itemSeq && r.erp_cls == 1 && !string.IsNullOrEmpty(r.bom_number))
+                                            var planBOM = boms.Find(b => b.item_number == itemSeq);
+                                            var pretreament = pretreatments.Where(c => c.sourceid == planBOM.mysql_id).ToList();
+                                            var returnlist = ObjectMapper.Map<List<b_bom_pretreatment>, List<BomChildExamineDto>>(pretreament);
+                                            returnlist = returnlist.OrderBy(s => s.num_order).ToList();
+                                            var level1Dto = returnlist[0];
+                                            level1Dto.needCount = MonthMoTotal.need_number.GetValueOrDefault();
+                                            CaclMaterialShortage(returnlist);
+                                            foreach (var r in returnlist)
                                             {
-                                                if (subProductItem.ContainsKey(r.item_number))
-                                                {
-                                                    subProductItem[r.item_number] += r.needCount;
-                                                }
-                                                else
+                                                if (r.item_number != itemSeq && r.erp_cls == 1 && !string.IsNullOrEmpty(r.bom_number))
                                                 {
-                                                    subProductItem.Add(r.item_number, r.needCount);
-                                                    subProductItemModel.Add(r.item_number, r);
+                                                    if (subProductItem.ContainsKey(r.item_number))
+                                                    {
+                                                        subProductItem[r.item_number] += r.needCount;
+                                                    }
+                                                    else
+                                                    {
+                                                        subProductItem.Add(r.item_number, r.needCount);
+                                                        subProductItemModel.Add(r.item_number, r);
+                                                    }
                                                 }
                                             }
                                         }
                                     }
+                                    else
+                                    {
+                                        moList.Remove(MonthMoTotal);
+                                        moentryList.RemoveAll(a => a.moentry_mono == MonthMoTotal.morder_no);
+                                        weekPlan.RemoveAll(a => a.ProductionOrder == MonthMoTotal.morder_no);
+                                    }
                                 }
+
                             }
                         }
                         else if (k <= 7)
@@ -2323,36 +2358,70 @@ namespace Business.Replenishment
                             }
                             if (k == 7)
                             {
-                                var itemMaster = itemMasterList.Find(i => i.ItemNum == itemSeq);
-                                if (itemMaster != null && itemMaster.MinOrd > 0)
+                                var MonthMoTotal = moList.Find(a => a.product_code == itemSeq && (
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 28).AddDays(-days) ||
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 35).AddDays(-days) ||
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 42).AddDays(-days) ||
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 49).AddDays(-days)));
+                                if (MonthMoTotal != null)
                                 {
-                                    var MonthMoTotal = moList.Find(a => a.product_code == itemSeq && (
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 28).AddDays(-days) ||
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 35).AddDays(-days) ||
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 42).AddDays(-days) ||
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 49).AddDays(-days)));
-                                    if (MonthMoTotal != null)
+                                    if (MonthMoTotal.need_number > 0)
                                     {
-                                        //考虑生产批量的计算逻辑
-                                        decimal productQty;
-                                        bool exist = minordsalesQty.TryGetValue(itemSeq, out productQty);
-                                        if (exist)
+                                        var itemMaster = itemMasterList.Find(i => i.ItemNum == itemSeq);
+                                        if (itemMaster != null && itemMaster.MinOrd > 0)
                                         {
-                                            //不需要生产
-                                            if (MonthMoTotal.need_number <= productQty)
+                                            //考虑生产批量的计算逻辑
+                                            decimal productQty;
+                                            bool exist = minordsalesQty.TryGetValue(itemSeq, out productQty);
+                                            if (exist)
                                             {
-                                                minordsalesQty[itemSeq] -= MonthMoTotal.need_number.GetValueOrDefault();
-                                                moList.Remove(MonthMoTotal);
-                                                moentryList.RemoveAll(a => a.moentry_mono == MonthMoTotal.morder_no);
-                                                weekPlan.RemoveAll(a => a.ProductionOrder == MonthMoTotal.morder_no);
+                                                //不需要生产
+                                                if (MonthMoTotal.need_number <= productQty)
+                                                {
+                                                    minordsalesQty[itemSeq] -= MonthMoTotal.need_number.GetValueOrDefault();
+                                                    moList.Remove(MonthMoTotal);
+                                                    moentryList.RemoveAll(a => a.moentry_mono == MonthMoTotal.morder_no);
+                                                    weekPlan.RemoveAll(a => a.ProductionOrder == MonthMoTotal.morder_no);
+                                                }
+                                                else
+                                                {
+                                                    //需要生产但是小于生产批量按生产批量生产,大于生产批量不需要改动
+                                                    if (MonthMoTotal.need_number - productQty < itemMaster.MinOrd)
+                                                    {
+                                                        decimal needProductQty = MonthMoTotal.need_number.GetValueOrDefault() - productQty;
+                                                        minordsalesQty[itemSeq] = Math.Ceiling(itemMaster.MinOrd) - needProductQty;
+                                                        MonthMoTotal.morder_production_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        MonthMoTotal.moentry_sys_stime = GetNextMonday().AddDays(28).AddDays(42).AddDays(-ProductTime(routingOps, MonthMoTotal.product_code, input.factory_id.ToString(), MonthMoTotal.need_number.GetValueOrDefault()) - days);
+                                                        MonthMoTotal.moentry_sys_etime = GetNextMonday().AddDays(28).AddDays(42).AddDays(-days);
+                                                        moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).morder_production_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).need_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanStartDate = MonthMoTotal.moentry_sys_stime;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Week = $"WK{GetWeekOfYear(MonthMoTotal.moentry_sys_stime.Value).ToString("00")}";
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Qty = MonthMoTotal.morder_production_number.Value;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Year = GetNextMonday().AddDays(28).AddDays(42).Year;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Month = GetNextMonday().AddDays(28).AddDays(42).Month;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).WeekSeq = WeekOfMonth(GetNextMonday().AddDays(28).AddDays(42), 1);
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanMonth = $"{GetNextMonday().AddDays(28).AddDays(42).Year}-{GetNextMonday().AddDays(28).AddDays(42).Month.ToString("00")}";
+                                                        if (item.area == "国内" || item.area == "中国")
+                                                        {
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
+                                                        }
+                                                        else
+                                                        {
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
+                                                        }
+                                                    }
+                                                }
                                             }
                                             else
                                             {
-                                                //需要生产但是小于生产批量按生产批量生产,大于生产批量不需要改动
-                                                if (MonthMoTotal.need_number - productQty < itemMaster.MinOrd)
+                                                if (MonthMoTotal.need_number < Math.Ceiling(itemMaster.MinOrd))
                                                 {
-                                                    decimal needProductQty = MonthMoTotal.need_number.GetValueOrDefault() - productQty;
-                                                    minordsalesQty[itemSeq] = Math.Ceiling(itemMaster.MinOrd) - needProductQty;
+                                                    minordsalesQty[itemSeq] = Math.Ceiling(itemMaster.MinOrd) - MonthMoTotal.need_number.Value;
+                                                    MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
                                                     MonthMoTotal.morder_production_number = Math.Ceiling(itemMaster.MinOrd);
                                                     MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
                                                     MonthMoTotal.moentry_sys_stime = GetNextMonday().AddDays(28).AddDays(42).AddDays(-ProductTime(routingOps, MonthMoTotal.product_code, input.factory_id.ToString(), MonthMoTotal.need_number.GetValueOrDefault()) - days);
@@ -2378,61 +2447,61 @@ namespace Business.Replenishment
                                                     }
                                                 }
                                             }
-                                        }
-                                        else
-                                        {
-                                            if (MonthMoTotal.need_number < Math.Ceiling(itemMaster.MinOrd))
+                                            var planBOM = boms.Find(b => b.item_number == itemSeq);
+                                            var pretreament = pretreatments.Where(c => c.sourceid == planBOM.mysql_id).ToList();
+                                            var returnlist = ObjectMapper.Map<List<b_bom_pretreatment>, List<BomChildExamineDto>>(pretreament);
+                                            returnlist = returnlist.OrderBy(s => s.num_order).ToList();
+                                            var level1Dto = returnlist[0];
+                                            level1Dto.needCount = MonthMoTotal.need_number.GetValueOrDefault();
+                                            CaclMaterialShortage(returnlist);
+                                            foreach (var r in returnlist)
                                             {
-                                                minordsalesQty[itemSeq] += Math.Ceiling(itemMaster.MinOrd) - MonthMoTotal.need_number.Value;
-                                                MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
-                                                MonthMoTotal.morder_production_number = Math.Ceiling(itemMaster.MinOrd);
-                                                MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
-                                                MonthMoTotal.moentry_sys_stime = GetNextMonday().AddDays(28).AddDays(42).AddDays(-ProductTime(routingOps, MonthMoTotal.product_code, input.factory_id.ToString(), MonthMoTotal.need_number.GetValueOrDefault()) - days);
-                                                MonthMoTotal.moentry_sys_etime = GetNextMonday().AddDays(28).AddDays(42).AddDays(-days);
-                                                moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).morder_production_number = Math.Ceiling(itemMaster.MinOrd);
-                                                moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).need_number = Math.Ceiling(itemMaster.MinOrd);
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanStartDate = MonthMoTotal.moentry_sys_stime;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Week = $"WK{GetWeekOfYear(MonthMoTotal.moentry_sys_stime.Value).ToString("00")}";
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Qty = MonthMoTotal.morder_production_number.Value;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Year = GetNextMonday().AddDays(28).AddDays(42).Year;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Month = GetNextMonday().AddDays(28).AddDays(42).Month;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).WeekSeq = WeekOfMonth(GetNextMonday().AddDays(28).AddDays(42), 1);
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanMonth = $"{GetNextMonday().AddDays(28).AddDays(42).Year}-{GetNextMonday().AddDays(28).AddDays(42).Month.ToString("00")}";
-                                                if (item.area == "国内" || item.area == "中国")
-                                                {
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
-                                                }
-                                                else
+                                                if (r.item_number != itemSeq && r.erp_cls == 1 && !string.IsNullOrEmpty(r.bom_number))
                                                 {
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
+                                                    if (subProductItem.ContainsKey(r.item_number))
+                                                    {
+                                                        subProductItem[r.item_number] += r.needCount;
+                                                    }
+                                                    else
+                                                    {
+                                                        subProductItem.Add(r.item_number, r.needCount);
+                                                        subProductItemModel.Add(r.item_number, r);
+                                                    }
                                                 }
                                             }
                                         }
-                                        var planBOM = boms.Find(b => b.item_number == itemSeq);
-                                        var pretreament = pretreatments.Where(c => c.sourceid == planBOM.mysql_id).ToList();
-                                        var returnlist = ObjectMapper.Map<List<b_bom_pretreatment>, List<BomChildExamineDto>>(pretreament);
-                                        returnlist = returnlist.OrderBy(s => s.num_order).ToList();
-                                        var level1Dto = returnlist[0];
-                                        level1Dto.needCount = MonthMoTotal.need_number.GetValueOrDefault();
-                                        CaclMaterialShortage(returnlist);
-                                        foreach (var r in returnlist)
+                                        else
                                         {
-                                            if (r.item_number != itemSeq && r.erp_cls == 1 && !string.IsNullOrEmpty(r.bom_number))
+                                            var planBOM = boms.Find(b => b.item_number == itemSeq);
+                                            var pretreament = pretreatments.Where(c => c.sourceid == planBOM.mysql_id).ToList();
+                                            var returnlist = ObjectMapper.Map<List<b_bom_pretreatment>, List<BomChildExamineDto>>(pretreament);
+                                            returnlist = returnlist.OrderBy(s => s.num_order).ToList();
+                                            var level1Dto = returnlist[0];
+                                            level1Dto.needCount = MonthMoTotal.need_number.GetValueOrDefault();
+                                            CaclMaterialShortage(returnlist);
+                                            foreach (var r in returnlist)
                                             {
-                                                if (subProductItem.ContainsKey(r.item_number))
+                                                if (r.item_number != itemSeq && r.erp_cls == 1 && !string.IsNullOrEmpty(r.bom_number))
                                                 {
-                                                    subProductItem[r.item_number] += r.needCount;
-                                                }
-                                                else
-                                                {
-                                                    subProductItem.Add(r.item_number, r.needCount);
-                                                    subProductItemModel.Add(r.item_number, r);
+                                                    if (subProductItem.ContainsKey(r.item_number))
+                                                    {
+                                                        subProductItem[r.item_number] += r.needCount;
+                                                    }
+                                                    else
+                                                    {
+                                                        subProductItem.Add(r.item_number, r.needCount);
+                                                        subProductItemModel.Add(r.item_number, r);
+                                                    }
                                                 }
                                             }
                                         }
                                     }
+                                    else
+                                    {
+                                        moList.Remove(MonthMoTotal);
+                                        moentryList.RemoveAll(a => a.moentry_mono == MonthMoTotal.morder_no);
+                                        weekPlan.RemoveAll(a => a.ProductionOrder == MonthMoTotal.morder_no);
+                                    }
                                 }
                             }
                         }
@@ -2576,36 +2645,70 @@ namespace Business.Replenishment
                             }
                             if (k == 11)
                             {
-                                var itemMaster = itemMasterList.Find(i => i.ItemNum == itemSeq);
-                                if (itemMaster != null && itemMaster.MinOrd > 0)
+                                var MonthMoTotal = moList.Find(a => a.product_code == itemSeq && (
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 56).AddDays(-days) ||
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 63).AddDays(-days) ||
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 70).AddDays(-days) ||
+                                a.moentry_sys_etime == GetNextMonday().AddDays(28 + 77).AddDays(-days)));
+                                if (MonthMoTotal != null)
                                 {
-                                    var MonthMoTotal = moList.Find(a => a.product_code == itemSeq && (
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 56).AddDays(-days) ||
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 63).AddDays(-days) ||
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 70).AddDays(-days) ||
-                                    a.moentry_sys_etime == GetNextMonday().AddDays(28 + 77).AddDays(-days)));
-                                    if (MonthMoTotal != null)
+                                    if (MonthMoTotal.need_number > 0)
                                     {
-                                        //考虑生产批量的计算逻辑
-                                        decimal productQty;
-                                        bool exist = minordsalesQty.TryGetValue(itemSeq, out productQty);
-                                        if (exist)
+                                        var itemMaster = itemMasterList.Find(i => i.ItemNum == itemSeq);
+                                        if (itemMaster != null && itemMaster.MinOrd > 0)
                                         {
-                                            //不需要生产
-                                            if (MonthMoTotal.need_number <= productQty)
+                                            //考虑生产批量的计算逻辑
+                                            decimal productQty;
+                                            bool exist = minordsalesQty.TryGetValue(itemSeq, out productQty);
+                                            if (exist)
                                             {
-                                                minordsalesQty[itemSeq] -= MonthMoTotal.need_number.GetValueOrDefault();
-                                                moList.Remove(MonthMoTotal);
-                                                moentryList.RemoveAll(a => a.moentry_mono == MonthMoTotal.morder_no);
-                                                weekPlan.RemoveAll(a => a.ProductionOrder == MonthMoTotal.morder_no);
+                                                //不需要生产
+                                                if (MonthMoTotal.need_number <= productQty)
+                                                {
+                                                    minordsalesQty[itemSeq] -= MonthMoTotal.need_number.GetValueOrDefault();
+                                                    moList.Remove(MonthMoTotal);
+                                                    moentryList.RemoveAll(a => a.moentry_mono == MonthMoTotal.morder_no);
+                                                    weekPlan.RemoveAll(a => a.ProductionOrder == MonthMoTotal.morder_no);
+                                                }
+                                                else
+                                                {
+                                                    //需要生产但是小于生产批量按生产批量生产,大于生产批量不需要改动
+                                                    if (MonthMoTotal.need_number - productQty < itemMaster.MinOrd)
+                                                    {
+                                                        decimal needProductQty = MonthMoTotal.need_number.GetValueOrDefault() - productQty;
+                                                        minordsalesQty[itemSeq] = Math.Ceiling(itemMaster.MinOrd) - needProductQty;
+                                                        MonthMoTotal.morder_production_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        MonthMoTotal.moentry_sys_stime = GetNextMonday().AddDays(28).AddDays(70).AddDays(-ProductTime(routingOps, MonthMoTotal.product_code, input.factory_id.ToString(), MonthMoTotal.need_number.GetValueOrDefault()) - days);
+                                                        MonthMoTotal.moentry_sys_etime = GetNextMonday().AddDays(28).AddDays(70).AddDays(-days);
+                                                        moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).morder_production_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).need_number = Math.Ceiling(itemMaster.MinOrd);
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanStartDate = MonthMoTotal.moentry_sys_stime;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Week = $"WK{GetWeekOfYear(MonthMoTotal.moentry_sys_stime.Value).ToString("00")}";
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Qty = MonthMoTotal.morder_production_number.Value;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Year = GetNextMonday().AddDays(28).AddDays(70).Year;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Month = GetNextMonday().AddDays(28).AddDays(70).Month;
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).WeekSeq = WeekOfMonth(GetNextMonday().AddDays(28).AddDays(70), 1);
+                                                        weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanMonth = $"{GetNextMonday().AddDays(28).AddDays(70).Year}-{GetNextMonday().AddDays(28).AddDays(70).Month.ToString("00")}";
+                                                        if (item.area == "国内" || item.area == "中国")
+                                                        {
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
+                                                        }
+                                                        else
+                                                        {
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
+                                                            weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
+                                                        }
+                                                    }
+                                                }
                                             }
                                             else
                                             {
-                                                //需要生产但是小于生产批量按生产批量生产,大于生产批量不需要改动
-                                                if (MonthMoTotal.need_number - productQty < itemMaster.MinOrd)
+                                                if (MonthMoTotal.need_number < Math.Ceiling(itemMaster.MinOrd))
                                                 {
-                                                    decimal needProductQty = MonthMoTotal.need_number.GetValueOrDefault() - productQty;
-                                                    minordsalesQty[itemSeq] = Math.Ceiling(itemMaster.MinOrd) - needProductQty;
+                                                    minordsalesQty[itemSeq] = Math.Ceiling(itemMaster.MinOrd) - MonthMoTotal.need_number.Value;
+                                                    MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
                                                     MonthMoTotal.morder_production_number = Math.Ceiling(itemMaster.MinOrd);
                                                     MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
                                                     MonthMoTotal.moentry_sys_stime = GetNextMonday().AddDays(28).AddDays(70).AddDays(-ProductTime(routingOps, MonthMoTotal.product_code, input.factory_id.ToString(), MonthMoTotal.need_number.GetValueOrDefault()) - days);
@@ -2631,61 +2734,61 @@ namespace Business.Replenishment
                                                     }
                                                 }
                                             }
-                                        }
-                                        else
-                                        {
-                                            if (MonthMoTotal.need_number < Math.Ceiling(itemMaster.MinOrd))
+                                            var planBOM = boms.Find(b => b.item_number == itemSeq);
+                                            var pretreament = pretreatments.Where(c => c.sourceid == planBOM.mysql_id).ToList();
+                                            var returnlist = ObjectMapper.Map<List<b_bom_pretreatment>, List<BomChildExamineDto>>(pretreament);
+                                            returnlist = returnlist.OrderBy(s => s.num_order).ToList();
+                                            var level1Dto = returnlist[0];
+                                            level1Dto.needCount = MonthMoTotal.need_number.GetValueOrDefault();
+                                            CaclMaterialShortage(returnlist);
+                                            foreach (var r in returnlist)
                                             {
-                                                minordsalesQty[itemSeq] += Math.Ceiling(itemMaster.MinOrd) - MonthMoTotal.need_number.Value;
-                                                MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
-                                                MonthMoTotal.morder_production_number = Math.Ceiling(itemMaster.MinOrd);
-                                                MonthMoTotal.need_number = Math.Ceiling(itemMaster.MinOrd);
-                                                MonthMoTotal.moentry_sys_stime = GetNextMonday().AddDays(28).AddDays(70).AddDays(-ProductTime(routingOps, MonthMoTotal.product_code, input.factory_id.ToString(), MonthMoTotal.need_number.GetValueOrDefault()) - days);
-                                                MonthMoTotal.moentry_sys_etime = GetNextMonday().AddDays(28).AddDays(70).AddDays(-days);
-                                                moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).morder_production_number = Math.Ceiling(itemMaster.MinOrd);
-                                                moentryList.Find(a => a.moentry_mono == MonthMoTotal.morder_no).need_number = Math.Ceiling(itemMaster.MinOrd);
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanStartDate = MonthMoTotal.moentry_sys_stime;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Week = $"WK{GetWeekOfYear(MonthMoTotal.moentry_sys_stime.Value).ToString("00")}";
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Qty = MonthMoTotal.morder_production_number.Value;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Year = GetNextMonday().AddDays(28).AddDays(70).Year;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).Month = GetNextMonday().AddDays(28).AddDays(70).Month;
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).WeekSeq = WeekOfMonth(GetNextMonday().AddDays(28).AddDays(70), 1);
-                                                weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).PlanMonth = $"{GetNextMonday().AddDays(28).AddDays(70).Year}-{GetNextMonday().AddDays(28).AddDays(70).Month.ToString("00")}";
-                                                if (item.area == "国内" || item.area == "中国")
-                                                {
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
-                                                }
-                                                else
+                                                if (r.item_number != itemSeq && r.erp_cls == 1 && !string.IsNullOrEmpty(r.bom_number))
                                                 {
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).SterilizationDate = MonthMoTotal.moentry_sys_etime.Value;
-                                                    weekPlan.Find(a => a.ProductionOrder == MonthMoTotal.morder_no).InStockDate = MonthMoTotal.moentry_sys_etime.Value.AddDays(days);
+                                                    if (subProductItem.ContainsKey(r.item_number))
+                                                    {
+                                                        subProductItem[r.item_number] += r.needCount;
+                                                    }
+                                                    else
+                                                    {
+                                                        subProductItem.Add(r.item_number, r.needCount);
+                                                        subProductItemModel.Add(r.item_number, r);
+                                                    }
                                                 }
                                             }
                                         }
-                                        var planBOM = boms.Find(b => b.item_number == itemSeq);
-                                        var pretreament = pretreatments.Where(c => c.sourceid == planBOM.mysql_id).ToList();
-                                        var returnlist = ObjectMapper.Map<List<b_bom_pretreatment>, List<BomChildExamineDto>>(pretreament);
-                                        returnlist = returnlist.OrderBy(s => s.num_order).ToList();
-                                        var level1Dto = returnlist[0];
-                                        level1Dto.needCount = MonthMoTotal.need_number.GetValueOrDefault();
-                                        CaclMaterialShortage(returnlist);
-                                        foreach (var r in returnlist)
+                                        else
                                         {
-                                            if (r.item_number != itemSeq && r.erp_cls == 1 && !string.IsNullOrEmpty(r.bom_number))
+                                            var planBOM = boms.Find(b => b.item_number == itemSeq);
+                                            var pretreament = pretreatments.Where(c => c.sourceid == planBOM.mysql_id).ToList();
+                                            var returnlist = ObjectMapper.Map<List<b_bom_pretreatment>, List<BomChildExamineDto>>(pretreament);
+                                            returnlist = returnlist.OrderBy(s => s.num_order).ToList();
+                                            var level1Dto = returnlist[0];
+                                            level1Dto.needCount = MonthMoTotal.need_number.GetValueOrDefault();
+                                            CaclMaterialShortage(returnlist);
+                                            foreach (var r in returnlist)
                                             {
-                                                if (subProductItem.ContainsKey(r.item_number))
+                                                if (r.item_number != itemSeq && r.erp_cls == 1 && !string.IsNullOrEmpty(r.bom_number))
                                                 {
-                                                    subProductItem[r.item_number] += r.needCount;
-                                                }
-                                                else
-                                                {
-                                                    subProductItem.Add(r.item_number, r.needCount);
-                                                    subProductItemModel.Add(r.item_number, r);
+                                                    if (subProductItem.ContainsKey(r.item_number))
+                                                    {
+                                                        subProductItem[r.item_number] += r.needCount;
+                                                    }
+                                                    else
+                                                    {
+                                                        subProductItem.Add(r.item_number, r.needCount);
+                                                        subProductItemModel.Add(r.item_number, r);
+                                                    }
                                                 }
                                             }
                                         }
                                     }
+                                    else
+                                    {
+                                        moList.Remove(MonthMoTotal);
+                                        moentryList.RemoveAll(a => a.moentry_mono == MonthMoTotal.morder_no);
+                                        weekPlan.RemoveAll(a => a.ProductionOrder == MonthMoTotal.morder_no);
+                                    }
                                 }
                             }
                         }
@@ -2895,7 +2998,7 @@ namespace Business.Replenishment
                     {
                         itemQty = Math.Ceiling(itemQty.GetValueOrDefault());
                     }
-                    if(!weeksubPlanList.Any(a=>a.ItemNumber== subProductItemModel[key].item_number && a.PlanMonth== kMonth))
+                    if(!weeksubPlanList.Any(a=>a.ItemNumber== subProductItemModel[key].item_number && a.PlanMonth== kMonth) && itemQty>0)
                     {
                         mes_morder mes_Morder = new mes_morder();
                         mes_Morder.GenerateNewId(help.NextId());
@@ -3004,7 +3107,7 @@ namespace Business.Replenishment
                 weekPlan[i].ProductionOrder = morder_noList[i].NbrResult;
                 weekPlan[i].OrderNO = OrderNOList[i].NbrResult;
             }
-            await PlanOrderResourceCheck(moList.OrderBy(a=>a.moentry_sys_stime).ToList(), moentryList, bang_id, input);
+            await PlanOrderResourceCheck(moList.OrderBy(a=>a.moentry_sys_stime).ToList(), moentryList, bang_id,false, input);
 
             var examine_resultList = _mysql_examine_result.GetListAsync(a => a.bangid == bang_id).Result;
             await _businessDbContext.BulkInsertAsync(moList);
@@ -3019,7 +3122,7 @@ namespace Business.Replenishment
                 moList.ForEach(s => { s.create_time = DateTime.Now; });
                 List<b_bom_child_examine> childExamineList = _mysql_bom_child_examine.GetListAsync(c => examine_resultList.Select(x => x.Id).Contains(c.examine_id.GetValueOrDefault())).Result;
                 //同步工单
-                CreateWorkOrdDates(moList, routingOps, workOrdSave, workOrdRoutingSave, workOrdDetails, examine_resultList, childExamineList);
+                CreateWorkOrdDates(moList, routingOps, workOrdSave, workOrdRoutingSave, workOrdDetails, examine_resultList, childExamineList,false);
             }
 
             if (workOrdSave.Any())
@@ -3606,7 +3709,7 @@ namespace Business.Replenishment
         /// </summary>
         /// <param name="morders"></param>
         /// <param name="allRoutings">工艺路线数据</param>
-        public void CreateWorkOrdDates(List<mes_morder> morders, List<RoutingOpDetail> allRoutings, List<WorkOrdMaster> workOrds, List<WorkOrdRouting> workOrdRoutings, List<WorkOrdDetail> workOrdDetails, List<b_examine_result> exmResult, List<b_bom_child_examine> childExamineList)
+        public void CreateWorkOrdDates(List<mes_morder> morders, List<RoutingOpDetail> allRoutings, List<WorkOrdMaster> workOrds, List<WorkOrdRouting> workOrdRoutings, List<WorkOrdDetail> workOrdDetails, List<b_examine_result> exmResult, List<b_bom_child_examine> childExamineList,bool isyearcheck)
         {
             //工单主表
             WorkOrdMaster workOrd;
@@ -3629,7 +3732,7 @@ namespace Business.Replenishment
                 workOrd.QtyOrded = item.need_number.GetValueOrDefault();
                 workOrd.Period = 1;
                 workOrd.Priority = item.urgent;
-                workOrd.Status = "";
+                workOrd.Status =isyearcheck? "p":"";
                 workOrd.IsActive = true;
                 workOrd.IsConfirm = true;
                 workOrd.CreateTime = DateTime.Now;
@@ -4016,7 +4119,7 @@ namespace Business.Replenishment
         /// </summary>
         /// <param name="input"></param>
         /// <returns></returns>
-        public async Task<string> PlanOrderResourceCheck(List<mes_morder> Mes_Morders, List<mes_moentry> moentryList,long bangid, InputDto input)
+        public async Task<string> PlanOrderResourceCheck(List<mes_morder> Mes_Morders, List<mes_moentry> moentryList,long bangid,bool isyearcheck, InputDto input)
         {
             if (Mes_Morders.Any())
             {
@@ -4076,7 +4179,7 @@ namespace Business.Replenishment
                 //    _scheduleResultOpMaster.Delete(s => monolist.Contains(s.WorkOrd));
                 //}
                 //只走计划工单
-                var rtn = await OrderKittingCheck(Mes_Morders, moentryList, bangid, true);
+                var rtn = await OrderKittingCheck(Mes_Morders, moentryList, bangid,isyearcheck, true);
             }
             else
             {
@@ -4126,7 +4229,7 @@ namespace Business.Replenishment
         /// 工单检查物料齐套
         /// </summary>
         /// <param name="input"></param>
-        public async Task<PschedDto> OrderKittingCheck(List<mes_morder> mo_Mes_Morders, List<mes_moentry> moentryList,long bangid, bool planCheck = false)
+        public async Task<PschedDto> OrderKittingCheck(List<mes_morder> mo_Mes_Morders, List<mes_moentry> moentryList,long bangid,bool isyearcheck,bool planCheck = false)
         {
             //资源检查结果
             PschedDto rtn = new PschedDto();
@@ -4327,6 +4430,50 @@ namespace Business.Replenishment
                     try
                     {
                         _businessDbContext.BulkUpdate(mo_Mes_Morders);
+                        if (_CalcBomViewAppService.mordersInsertList.Any())
+                        {
+                            var molist = ObjectMapper.Map<List<mo_mes_morder>, List<mes_morder>>(_CalcBomViewAppService.mordersInsertList);
+                            _businessDbContext.BulkInsert(molist);
+
+                            List<WorkOrdMaster> workOrdSave = new List<WorkOrdMaster>();
+                            List<WorkOrdRouting> workOrdRoutingSave = new List<WorkOrdRouting>();
+                            List<WorkOrdDetail> workOrdDetails = new List<WorkOrdDetail>();
+                            if (molist.Any())
+                            {
+                                molist.ForEach(s => { s.create_time = DateTime.Now;s.factory_id = input.factoryId;s.company_id = input.company_id; });
+                                //同步工单
+                                var routings=_routingOpDetail.Select(r => molist.Select(a => a.product_code).Distinct().ToList().Contains(r.RoutingCode) && r.Domain == input.factoryId.ToString());
+                                routings.AddRange(routingOps);
+                                CreateWorkOrdDates(molist, routings, workOrdSave, workOrdRoutingSave, workOrdDetails, new List<b_examine_result>(), new List<b_bom_child_examine>(),true);
+                                if (workOrdSave.Any())
+                                {
+                                    workOrdSave = CalcPriority(workOrdSave, input.factoryId.ToString());
+                                    _businessDbContext.BulkInsert(workOrdSave);
+                                }
+                                var DBworkOrdList = _workOrdMaster.Select(a => workOrdSave.Count > 0 && a.Domain == workOrdSave[0].Domain && workOrdSave.Select(c => c.WorkOrd).Contains(a.WorkOrd));
+                                if (workOrdRoutingSave.Any())
+                                {
+                                    workOrdRoutingSave.ForEach(c =>
+                                    {
+                                        c.WorkOrdMasterRecID = DBworkOrdList.Where(a => a.WorkOrd == c.WorkOrd).First().RecID;
+                                    });
+                                    _businessDbContext.BulkInsert(workOrdRoutingSave);
+                                }
+                                if (workOrdDetails.Any())
+                                {
+                                    workOrdDetails.ForEach(c =>
+                                    {
+                                        c.WorkOrdMasterRecID = DBworkOrdList.Where(a => a.WorkOrd == c.WorkOrd).First().RecID;
+                                    });
+                                    _businessDbContext.BulkInsert(workOrdDetails);
+                                }
+                            }
+                        }
+                        if (_CalcBomViewAppService.moentriesInsertList.Any())
+                        {
+                            var moentrylist = ObjectMapper.Map<List<mo_mes_moentry>, List<mes_moentry>>(_CalcBomViewAppService.moentriesInsertList);
+                            _businessDbContext.BulkInsert(moentrylist);
+                        }
                         if (prmainlist.Any())
                         {
                             var pr_mainlist = ObjectMapper.Map<List<mo_srm_pr_main>, List<srm_pr_main>>(prmainlist);
@@ -5084,7 +5231,7 @@ namespace Business.Replenishment
                 mes_Morder.product_code = ic_item.ItemNum;
                 mes_Morder.product_name = ic_item.Descr;
                 mes_Morder.morder_date = DateTime.Now.Date.AddDays(1);
-                mes_Morder.moentry_sys_stime= Convert.ToDateTime(productList[0].PlanMonth + "-01");
+                mes_Morder.moentry_sys_stime= Convert.ToDateTime(productList[i].PlanMonth + "-01");
                 mes_Morder.moentry_prd = null;
                 mes_Morder.moentry_prdname = null;
                 mes_Morder.moentry_wrkc = null;
@@ -5113,11 +5260,12 @@ namespace Business.Replenishment
                 moList.Add(mes_Morder);
                 moentryList.Add(mes_Moentry);
             }
-            var safeItemQty = _itemMaster.Select(a => a.PurMfg == "L" && a.SafetyStk > 0 &&a.IsActive);
-            if(safeItemQty.Count>0)
+            var safeItemQty = _itemMaster.Select(a => a.PurMfg == "L" && a.SafetyStk > 0 && a.IsActive);
+            if (safeItemQty.Count > 0)
             {
+                var itemStock = CalcStock(safeItemQty.Select(m => m.ItemNum).Distinct().ToList(), input);
                 var safelist = _serialNumberAppService.GetBillNo(input.factory_id.ToString(), "M5", productList.Count(), "", 1);
-                for(int i=0;i<safeItemQty.Count;i++)
+                for (int i = 0; i < safeItemQty.Count; i++)
                 {
                     mes_morder mes_Morder = new mes_morder();
                     mes_Morder.GenerateNewId(help.NextId());
@@ -5144,8 +5292,25 @@ namespace Business.Replenishment
                     mes_Morder.moentry_wrkcname = null;
                     mes_Morder.picking_qty = 0;
                     mes_Morder.unit = safeItemQty[i].UM;
-                    mes_Morder.morder_production_number = safeItemQty[i].SafetyStk;
-                    mes_Morder.need_number = safeItemQty[i].SafetyStk;
+                    var itemStockQty = itemStock.Find(a => a.ItemNumber == safeItemQty[i].ItemNum);
+                    if (itemStockQty != null)
+                    {
+                        if (itemStockQty.Qty < safeItemQty[i].SafetyStk)
+                        {
+                            mes_Morder.morder_production_number = safeItemQty[i].SafetyStk - itemStockQty.Qty;
+                            mes_Morder.need_number = safeItemQty[i].SafetyStk - itemStockQty.Qty;
+                        }
+                        else
+                        {
+                            //超过安全库存不用考虑
+                            continue;
+                        }
+                    }
+                    else
+                    {
+                        mes_Morder.morder_production_number = safeItemQty[i].SafetyStk;
+                        mes_Morder.need_number = safeItemQty[i].SafetyStk;
+                    }
                     mes_Morder.remaining_number = 0;
                     mes_Morder.create_time = DateTime.Now;
                     //生成工单子表数据
@@ -5171,7 +5336,7 @@ namespace Business.Replenishment
             //await _mysql_mes_morder.InsertManyAsync(moList);
             //await _mysql_mes_moentry.InsertManyAsync(moentryList);
             moList = moList.OrderBy(a => a.moentry_sys_stime).ToList();
-            await PlanOrderResourceCheck(moList,moentryList, bangid,input);
+            await PlanOrderResourceCheck(moList,moentryList, bangid,true,input);
             return "OK";
         }
 

+ 3 - 3
MicroServices/Business/Business.Application/ResourceExamineManagement/ProductionScheduleAppService.cs

@@ -237,13 +237,13 @@ namespace Business.ResourceExamineManagement
             IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
             domain = configuration.GetConnectionString("Factory_id");
 
-            //获取需要排产的工单(获取四周的工单:正常工单+已审批通过的特殊工单)
-            DateTime endDate = DateTime.Now.Date.AddDays(28).AddDays(1);
+            ////获取需要排产的工单(获取四周的工单:正常工单+已审批通过的特殊工单)
+            //DateTime endDate = DateTime.Now.Date.AddDays(28).AddDays(1);
             //取数开始时间为当前时间
             DateTime startTime = DateTime.Now;
             scheTime = startTime;
             //尚未开始生产的工单+正在生产的工单
-            var workOrds = _workOrdMaster.Select(p => p.IsActive && p.Domain == domain && ((p.OrdDate < endDate && p.OrdDate >= startTime && p.Status.ToLower() =="p" && (string.IsNullOrEmpty(p.Typed) || (!string.IsNullOrEmpty(p.Typed) && p.Typed.ToUpper() != "PW" && p.BusinessID >0 ))) || p.Status.ToLower() == "w")).ToList();
+            var workOrds = _workOrdMaster.Select(p => p.IsActive && p.Domain == domain && ((p.Status.ToUpper() =="P" && (string.IsNullOrEmpty(p.Typed) || (!string.IsNullOrEmpty(p.Typed) && p.Typed.ToUpper() != "PW" && p.BusinessID >0 ))) || p.Status.ToUpper() == "W")).ToList();
             if (workOrds.Any())
             {
                 await DoProductSchedule(startTime, workOrds, domain, 1);

+ 8 - 7
MicroServices/Business/Business.Application/ResourceExamineManagement/ResourceExamineAppService.cs

@@ -4888,12 +4888,13 @@ namespace Business.ResourceExamineManagement
         /// <exception cref="NotImplementedException"></exception>
         public async Task<string> ProductionSchedule(string domain)
         {
-            //获取需要排产的工单(获取四周的工单:正常工单+已审批通过的特殊工单)
-            DateTime endDate = DateTime.Now.Date.AddDays(28).AddDays(1);
             //取数开始时间为当前日期
             DateTime startTime = DateTime.Now;
-            //尚未开始生产的工单+正在生产的工单
-            var workOrds = _workOrdMaster.Select(p => p.IsActive && p.Domain == domain && ((p.OrdDate < endDate && p.OrdDate >= startTime && p.Status.ToUpper() == "P" && (string.IsNullOrEmpty(p.Typed) || (!string.IsNullOrEmpty(p.Typed) && p.Typed.ToUpper() != "PW" && p.BusinessID > 0))) || p.Status.ToUpper() == "W")).ToList();
+            ////获取需要排产的工单(获取四周的工单:正常工单+已审批通过的特殊工单)
+            //DateTime endDate = DateTime.Now.Date.AddDays(28).AddDays(1);
+            ////尚未开始生产的工单+正在生产的工单
+            //var workOrds = _workOrdMaster.Select(p => p.IsActive && p.Domain == domain && ((p.OrdDate < endDate && p.OrdDate >= startTime && p.Status.ToUpper() == "P" && (string.IsNullOrEmpty(p.Typed) || (!string.IsNullOrEmpty(p.Typed) && p.Typed.ToUpper() != "PW" && p.BusinessID > 0))) || p.Status.ToUpper() == "W")).ToList();
+            var workOrds = _workOrdMaster.Select(p=>p.IsActive && p.Domain == domain && ((p.Status.ToUpper() == "P" && (string.IsNullOrEmpty(p.Typed) || (!string.IsNullOrEmpty(p.Typed) && p.Typed.ToUpper() != "PW" && p.BusinessID > 0))) || p.Status.ToUpper() == "W")).ToList();
             if (workOrds.Any())
             {
                 await _productionScheduleAppService.DoProductSchedule(startTime, workOrds, domain, 2);
@@ -4909,12 +4910,12 @@ namespace Business.ResourceExamineManagement
         /// <exception cref="NotImplementedException"></exception>
         public async Task<string> UpdatePlanDate(UpdateDto input)
         {
-            //获取需要排产的工单(获取四周的工单:正常工单+已审批通过的特殊工单)
-            DateTime endDate = DateTime.Now.Date.AddDays(28).AddDays(1);
+            ////获取需要排产的工单(获取四周的工单:正常工单+已审批通过的特殊工单)
+            //DateTime endDate = DateTime.Now.Date.AddDays(28).AddDays(1);
             //取数开始时间为当前天的下一天
             DateTime startTime = DateTime.Now;
             //尚未开始生产的工单+正在生产的工单
-            var workOrds = _workOrdMaster.Select(p => p.IsActive && p.Domain == input.domain && (p.WorkOrd == input.workord || (p.OrdDate < endDate && p.OrdDate >= startTime && p.Status.ToUpper() == "P" && (string.IsNullOrEmpty(p.Typed) || (!string.IsNullOrEmpty(p.Typed) && p.Typed.ToUpper() != "PW" && p.BusinessID > 0))) || p.Status.ToLower() == "w")).ToList();
+            var workOrds = _workOrdMaster.Select(p => p.IsActive && p.Domain == input.domain && (p.WorkOrd == input.workord || (p.Status.ToUpper() == "P" && (string.IsNullOrEmpty(p.Typed) || (!string.IsNullOrEmpty(p.Typed) && p.Typed.ToUpper() != "PW" && p.BusinessID > 0))) || p.Status.ToUpper() == "W")).ToList();
             if (!workOrds.Exists(p=>p.WorkOrd == input.workord))
             {
                 return "NO|工单【" + input.workord + "】不存在,请联系系统管理员!";

+ 6 - 0
MicroServices/Business/Business.Domain/StructuredDB/Production/WorkOrdMaster.cs

@@ -63,6 +63,12 @@ namespace Business.Domain
         [Comment("订单数量")]
         public decimal QtyOrded { get; set; }
 
+        /// <summary>
+        /// 已完成数量
+        /// </summary>
+        [Comment("已完成数量")]
+        public decimal QtyCompleted { get; set; }
+
         /// <summary>
         /// 工艺路线编码
         /// </summary>

+ 1 - 0
MicroServices/Business/Business.Host/Business.Host.csproj

@@ -43,6 +43,7 @@
 
   <ItemGroup>
     <Folder Include="DataSeeder\" />
+    <Folder Include="Logs\" />
     <Folder Include="wwwroot\" />
   </ItemGroup>