ResourceExamineAppService.cs 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Dynamic.Core;
  6. using System.Threading.Tasks;
  7. using Volo.Abp.Application.Dtos;
  8. using Volo.Abp.Domain.Repositories;
  9. using Business.Models;
  10. using Microsoft.AspNetCore.Authorization;
  11. using Business.Permissions;
  12. using XCZ;
  13. using Business.ResourceExamineManagement.Dto;
  14. using Bussiness.Model.MES.IC;
  15. using AutoMapper.Internal.Mappers;
  16. using Bussiness.Model.Tech;
  17. using Bussiness.Model.Production;
  18. using Business.Core.MongoDBHelper;
  19. using Business.Core.Utilities;
  20. using Hangfire.Storage.Monitoring;
  21. using Business.BookManagement.Dto;
  22. using Volo.Abp.ObjectMapping;
  23. using Volo.Abp.Application.Services;
  24. using ZstdSharp.Unsafe;
  25. using System.Transactions;
  26. using NUglify.JavaScript.Syntax;
  27. using System.Linq.Expressions;
  28. using XCZ.Extensions;
  29. using System.ComponentModel;
  30. using System.Reflection.Emit;
  31. using NUglify.Helpers;
  32. using Microsoft.AspNetCore.SignalR.Protocol;
  33. using XCZ.Extensions;
  34. using System.ComponentModel.Design;
  35. using Volo.Abp.Validation.StringValues;
  36. using System.Runtime.CompilerServices;
  37. using MongoDB.Driver;
  38. using Volo.Abp.Validation.Localization;
  39. using Hangfire.Annotations;
  40. using System.IO.Compression;
  41. using System.Collections;
  42. using System.Data.SqlTypes;
  43. namespace Business.ResourceExamineManagement
  44. {
  45. /// <summary>
  46. /// 资源检查
  47. /// </summary>
  48. //[Authorize(BusinessPermissions.ResourceExamine.Default)]
  49. public class ResourceExamineAppService : ApplicationService, IResourceExamineAppService
  50. {
  51. #region 服务
  52. /// <summary>
  53. /// 工艺路径
  54. /// </summary>
  55. private readonly IMongoDB<mes_technique> _mes_technique;
  56. private IRepository<mes_technique, long> _mysql_mes_technique;
  57. /// <summary>
  58. /// 工序
  59. /// </summary>
  60. private readonly IMongoDB<mes_process> _mes_process;
  61. /// <summary>
  62. /// 工艺关联工序
  63. /// </summary>
  64. private readonly IMongoDB<mes_tech_process> _mes_tech_process;
  65. /// <summary>
  66. /// 工作日历
  67. /// </summary>
  68. private readonly IMongoDB<mes_work_calendar> _mes_work_calendar;
  69. /// <summary>
  70. /// 工作日历明细
  71. /// </summary>
  72. private readonly IMongoDB<mes_work_calendar_list> _mes_work_calendar_list;
  73. /// <summary>
  74. /// 工艺工序关联工位
  75. /// </summary>
  76. private readonly IMongoDB<mes_tech_proc_workshop> _mes_tech_proc_workshop;
  77. /// <summary>
  78. /// 排程占用记录
  79. /// </summary>
  80. private readonly IMongoDB<mes_schedule_occupy> _mes_schedule_occupy;
  81. /// <summary>
  82. /// 物料占用记录
  83. /// </summary>
  84. private readonly IMongoDB<ic_item_stockoccupy> _ic_item_stockoccupy;
  85. /// <summary>
  86. /// 物料详情
  87. /// </summary>
  88. private readonly IMongoDB<ic_item> _ic_item;
  89. private IRepository<ic_item, long> _mysql_ic_item;
  90. /// <summary>
  91. /// 物料BOM
  92. /// </summary>
  93. private readonly IMongoDB<ic_bom> _ic_bom;
  94. private IRepository<ic_bom, long> _mysql_ic_bom;
  95. /// <summary>
  96. /// 物料BOM明细
  97. /// </summary>
  98. private readonly IMongoDB<ic_bom_child> _ic_bom_child;
  99. private IRepository<ic_bom_child, long> _mysql_ic_bom_child;
  100. /// <summary>
  101. /// 物料库存表
  102. /// </summary>
  103. private readonly IMongoDB<ic_item_stock> _ic_item_stock;
  104. /// <summary>
  105. /// 物料质检表
  106. /// </summary>
  107. private readonly IMongoDB<ic_check> _ic_check;
  108. /// <summary>
  109. /// 替代群组
  110. /// </summary>
  111. private readonly IMongoDB<ic_substitute> _ic_substitute;
  112. /// <summary>
  113. /// 替代群组
  114. /// </summary>
  115. private readonly IMongoDB<ic_substitute_all> _ic_substitute_all;
  116. /// <summary>
  117. /// 替代群组
  118. /// </summary>
  119. private readonly IMongoDB<ic_substitute_all_dtl> _ic_substitute_all_dtl;
  120. /// <summary>
  121. /// 生产工单主表
  122. /// </summary>
  123. private readonly IMongoDB<mes_morder> _mes_morder;
  124. /// <summary>
  125. /// 在制工单占用记录表
  126. /// </summary>
  127. private readonly IMongoDB<mes_mooccupy> _mes_mooccupy;
  128. /// <summary>
  129. /// 销售工单
  130. /// </summary>
  131. private readonly IRepository<crm_seorder> _crm_seorder;
  132. /// <summary>
  133. /// 销售工单
  134. /// </summary>
  135. private readonly IRepository<crm_seorderentry> _crm_seorderentry;
  136. #endregion
  137. /// <summary>
  138. /// 构造函数
  139. /// </summary>
  140. /// <param name="icitem"></param>
  141. /// <param name="icbom"></param>
  142. public ResourceExamineAppService(
  143. IMongoDB<mes_technique> mes_technique,
  144. IMongoDB<mes_process> mes_process,
  145. IMongoDB<mes_tech_process> mes_tech_process,
  146. IMongoDB<mes_tech_proc_workshop> mes_tech_proc_workshop,
  147. IMongoDB<ic_item> ic_item,
  148. IMongoDB<ic_bom> ic_bom,
  149. IMongoDB<ic_bom_child> ic_bom_child,
  150. IMongoDB<ic_item_stock> ic_item_stock,
  151. IMongoDB<ic_check> ic_check,
  152. //IMongoDB<ic_substitute> ic_substitute,
  153. //IMongoDB<ic_substitute_all> ic_substitute_all,
  154. //IMongoDB<ic_substitute_all_dtl> ic_substitute_all_dtl,
  155. IMongoDB<mes_morder> mes_morder,
  156. IMongoDB<mes_mooccupy> mes_mooccupy,
  157. IMongoDB<ic_item_stockoccupy> ic_item_stockoccupy,
  158. IRepository<ic_item, long> mysql_ic_item,
  159. IRepository<ic_bom, long> mysql_ic_bom,
  160. IRepository<ic_bom_child, long> mysql_ic_bom_child,
  161. IRepository<mes_technique, long> mysql_mes_technique,
  162. IRepository<crm_seorder, long> mysql_crm_seorder,
  163. IRepository<crm_seorderentry, long> mysql_crm_seorderentry
  164. )
  165. {
  166. _mes_technique = mes_technique;
  167. _mes_process = mes_process;
  168. _mes_tech_process = mes_tech_process;
  169. _mes_tech_proc_workshop = mes_tech_proc_workshop;
  170. _ic_item = ic_item;
  171. _ic_bom = ic_bom;
  172. _ic_bom_child = ic_bom_child;
  173. _ic_item_stock = ic_item_stock;
  174. _ic_check = ic_check;
  175. //_ic_substitute = ic_substitute;
  176. //_ic_substitute_all = ic_substitute_all;
  177. //_ic_substitute_all_dtl = ic_substitute_all_dtl;
  178. _mes_morder = mes_morder;
  179. _mes_mooccupy = mes_mooccupy;
  180. _ic_item_stockoccupy = ic_item_stockoccupy;
  181. _mysql_ic_item = mysql_ic_item;
  182. _mysql_ic_bom = mysql_ic_bom;
  183. _mysql_ic_bom_child = mysql_ic_bom_child;
  184. _mysql_mes_technique = mysql_mes_technique;
  185. }
  186. /// <summary>
  187. /// mongoDB示例方法,后期删除
  188. /// </summary>
  189. /// <returns></returns>
  190. public async Task test()
  191. {
  192. //多条插入
  193. List<mes_technique> infos = new List<mes_technique>();
  194. mes_technique info;
  195. for (int i = 0; i < 3; i++)
  196. {
  197. info = new mes_technique();
  198. info.GenerateNewId();
  199. info.tech_name = "多条" + i;
  200. info.level = i;
  201. infos.Add(info);
  202. }
  203. await _mes_technique.InsertMany(infos);
  204. var info2 = _mes_technique.GetAll().Result;
  205. var a = new PschedDto();
  206. a.count = info2.Count;
  207. //获取数据
  208. var info1 = await _mes_technique.GetOneByID((long)1732029975067480064);
  209. //更新数据
  210. info1.tech_name = "更新***";
  211. var rlt = await _mes_technique.UpdateOne(info1, info1.Id);
  212. //根据条件查询数据
  213. Expression<Func<mes_technique, bool>> filter = x => x.Id == (long)1732376973889097728 && x.tech_name == "多条0";
  214. var info3 = await _mes_technique.GetManyByCondition(filter);
  215. }
  216. /// <summary>
  217. /// 资源检查
  218. /// </summary>
  219. /// <param name="input"></param>
  220. /// <returns></returns>
  221. /// <exception cref="NotImplementedException"></exception>
  222. public async Task<PschedDto> ReceiveResult(SeorderentryDto input)
  223. {
  224. //数据库快照-同步mysql库数据到mongoDB中
  225. await SyncData();
  226. //生成当前计算bangid
  227. //SnowFlake snow = new SnowFlake();
  228. //long bangid = snow.NextId();
  229. ////产能检查
  230. //await ProductiveExamine(1733221167209762816, 100);
  231. return null;
  232. throw new NotImplementedException();
  233. }
  234. /// <summary>
  235. /// 数据库快照
  236. /// </summary>
  237. /// <returns></returns>
  238. public async Task SyncData()
  239. {
  240. var query = _mysql_mes_technique.GetQueryableAsync().Result.ToListAsync().Result;
  241. await _mes_technique.InsertMany(query);
  242. //var dt1 = DateTime.Now;
  243. //var a = await _mysql_ic_bom.GetQueryableAsync();
  244. //var query1 =await a.ToListAsync();
  245. //await _ic_bom.InsertMany(query1);
  246. //var dt2 = DateTime.Now;
  247. //var c = 3;
  248. }
  249. /// <summary>
  250. /// 产能计算
  251. /// </summary>
  252. /// <param name="tech_id">工艺路径主键</param>
  253. /// <param name="packages">需要生产产品件数</param>
  254. public async Task<DateTime> ProductiveExamine(long tech_id, int packages)
  255. {
  256. if (packages <= 0)
  257. {
  258. throw new NotImplementedException("产能计算参数有误!");
  259. }
  260. #region 1、数据准备
  261. //1.1、获取工艺路径数据
  262. mes_technique tech = await _mes_technique.GetOneByID(tech_id);
  263. //1.2、获取工艺路径关联工序数据
  264. List<mes_tech_process> tech_Processes = await _mes_tech_process.GetManyByCondition(x => x.tech_id == tech_id);
  265. //1.3、获取当前工艺路径下的工序数据
  266. FilterDefinition<mes_process> filter = Builders<mes_process>.Filter.In(s => s.Id, tech_Processes.Select(m => m.proc_id).ToList());
  267. List<mes_process> process = await _mes_process.GetManyByIds(filter);
  268. //1.3、获取工艺工序关联工位信息
  269. FilterDefinition<mes_tech_proc_workshop> filter1 = Builders<mes_tech_proc_workshop>.Filter.In(s => s.tech_proc_id, tech_Processes.Select(m => m.Id).ToList());
  270. List<mes_tech_proc_workshop> tech_Proc_Workshops = await _mes_tech_proc_workshop.GetManyByIds(filter1);
  271. #endregion
  272. #region 计算产能,得到耗时
  273. decimal sumTimes = 0.00m;//总耗时(分钟)
  274. //工序需要等待时间记录
  275. List<StartTimeDto> starts = new List<StartTimeDto>();
  276. //1、获取工艺路径下的第一层级工序
  277. List<mes_tech_process> fistChilds = tech_Processes.Where(p => p.parentprocid == tech_id).OrderBy(m => m.Id).ToList();
  278. if (fistChilds.Count == 0)
  279. {
  280. throw new NotImplementedException("当前工艺路径没有配置工序,请调整!");
  281. }
  282. decimal curTakeTime = 0.00m;//当前工序耗时(分钟)
  283. //添加第一个工序需要等待时间记录
  284. StartTimeDto dto;
  285. foreach (var chd in fistChilds)
  286. {
  287. dto = new StartTimeDto();
  288. if (chd.nextprocid == null)//最后一个工序
  289. {
  290. //计算最后一个工序耗时
  291. curTakeTime = CalcTakeTime(chd, packages);
  292. }
  293. else
  294. {
  295. curTakeTime = CalcTakeTime(chd, chd.lq.Value);
  296. }
  297. sumTimes += curTakeTime;
  298. //添加耗时记录
  299. dto.tech_id = tech_id;
  300. dto.proc_id = chd.proc_id;
  301. dto.nextproc_id = chd.nextprocid;
  302. dto.wait_time = curTakeTime;
  303. starts.Add(dto);
  304. }
  305. #endregion
  306. return DateTime.Now.AddDays(1).AddMinutes((double)sumTimes);
  307. }
  308. /// <summary>
  309. /// 计算当前工序前置准备时间
  310. /// </summary>
  311. /// <param name="proc"></param>
  312. /// <param name="quantity">LeadQuantity to Start Next</param>
  313. /// <returns></returns>
  314. private decimal CalcTakeTime(mes_tech_process proc, decimal quantity)
  315. {
  316. decimal takeTime = 0.00m;//当前工序前置准备时间(分钟)
  317. if (proc.wctype == 1)//人工型:数量/uph(一小时生产数量)*60(小时转换为分钟)/wsinuse(工位数)
  318. {
  319. takeTime = quantity / proc.uph.Value * 60 / proc.wsinuse.Value;
  320. return takeTime;
  321. }
  322. else if (proc.wctype == 2)//流水线型:数量*ct(生产一件所需时间)
  323. {
  324. takeTime = quantity * proc.ct.Value;
  325. return takeTime;
  326. }
  327. else if (proc.wctype == 3)//设备型:向上取整(数量/一次可加工数量)*ct(老化一次所需时间)
  328. {
  329. takeTime = Math.Ceiling(quantity / proc.upe.Value) * proc.ct.Value;
  330. return takeTime;
  331. }
  332. return takeTime;
  333. }
  334. /// <summary>
  335. /// 生成主工单
  336. /// </summary>
  337. /// <param name="BomNumber">Bom编码</param>
  338. /// <param name="Quantity">需要数量</param>
  339. /// <param name="DeliverDate">交付日期</param>
  340. /// <param name="OrderId">销售订单ID</param>
  341. /// <returns></returns>
  342. public async Task GenerateMorder(string BomNumber, decimal? Quantity, DateTime DeliverDate, long OrderId)
  343. {
  344. //需要生成的物料,需要生产的数量,对应销售订单。
  345. //1.库存、在制工单检查完成后 当前BOM需要自制时 产生工单。
  346. //2.每一个销售订单行对应一个工单。
  347. if (string.IsNullOrEmpty(BomNumber) || Quantity == null)
  348. {
  349. //TODO: 参数异常 直接返回
  350. }
  351. //获取销售订单信息
  352. //var seorder = await _crm_seorder.FindAsync(x => x.Id == OrderId);
  353. var seorderentry = await _crm_seorderentry.FindAsync(x => x.seorder_id == OrderId);
  354. }
  355. /// <summary>
  356. /// 检查成品库存
  357. /// </summary>
  358. /// <param name="OrderId">销售订单ID</param>
  359. /// <returns></returns>
  360. public async Task<bool> CheckFinishedProductInventory(long OrderId)
  361. {
  362. //获取销售订单信息
  363. var seorder = await _crm_seorder.FindAsync(x => x.Id == OrderId);
  364. //获取销售订单子表
  365. var seorderentry = await _crm_seorderentry.FindAsync(x => x.seorder_id == OrderId);
  366. if (string.IsNullOrEmpty(seorderentry.bom_number))
  367. {
  368. return false;
  369. }
  370. Expression<Func<ic_item_stock, bool>> filter = x => x.icitem_id == long.Parse(seorderentry.bom_number);
  371. var ic_Item_Stocks = await _ic_item_stock.GetManyByCondition(filter);
  372. if (seorderentry.qty <= ic_Item_Stocks.Sum(x => x.sqty))
  373. {
  374. return true;
  375. }
  376. else
  377. {
  378. return false;
  379. }
  380. }
  381. /// <summary>
  382. /// 检查在制工单
  383. /// </summary>
  384. /// <param name="bomNumber">Bom编码</param>
  385. /// <param name="Quantity">需要数量</param>
  386. /// <param name="DeliverDate">交付日期</param>
  387. /// <param name="OrderId">销售订单ID</param>
  388. /// <returns></returns>
  389. public async Task<List<mes_morder>> CheckMorder(string bomNumber, decimal? Quantity, DateTime DeliverDate, long OrderId)
  390. {
  391. if (string.IsNullOrEmpty(bomNumber) || Quantity != null)
  392. {
  393. //TODO:入参异常;
  394. throw new NotImplementedException("BOM编码或需求数量不能为空!");
  395. }
  396. //根据Bom编码查询出对应工单并且状态不为完成、关闭,非委外工单。
  397. //TODO:工单类型;
  398. Expression<Func<mes_morder, bool>> filter = x => x.bom_number == bomNumber && (x.morder_state != "完成" || x.morder_state != "关闭"
  399. && x.morder_icitem_type != "相关委外工单") && x.IsDeleted == false;
  400. var morderList = await _mes_morder.GetManyByCondition(filter);
  401. //获取销售订单信息
  402. //var seorder = await _crm_seorder.FindAsync(x => x.Id == OrderId);
  403. var seorderentry = await _crm_seorderentry.FindAsync(x => x.seorder_id == OrderId);
  404. //获取物料详情
  405. var mysql_ic_item = await _mysql_ic_item.FindAsync(x => x.number == bomNumber);
  406. //工单已被占用后要与占用表关联查询...减去占用量后 剩下生产数量可供下个销售工单使用。
  407. Expression<Func<mes_mooccupy, bool>> mooccupyfilter = x => x.moo_state == 1 && x.IsDeleted == false;
  408. var mes_mooccupyList = await _mes_mooccupy.GetManyByCondition(mooccupyfilter);
  409. //首先满足需求数量工单其次判断是否满足交付日期、当数量不满足时继续查找最早交付日期订单 工单数量累加。
  410. //当前工单计划日期-1天 小于交付日期 && 计算生产数量-入库数据并且大于需求产品数量。
  411. var morderDataList = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate &&
  412. (x.morder_production_number - x.inventory_number) > Quantity).ToList();
  413. if (morderDataList.Count > 0)
  414. {
  415. //存在此数据满足当前BOM交付找到最早日期工单,则返回无需后续继续检查。
  416. var morder = morderDataList.OrderByDescending(x => x.planner_end_date).FirstOrDefault();
  417. var mooccupies = mes_mooccupyList.Where(x => x.moo_moid == morder.Id).ToList();
  418. var mes_Mooccupy = GetMooccupies(seorderentry, mysql_ic_item, morder, mooccupies);
  419. await _mes_mooccupy.InsertOne(mes_Mooccupy);
  420. }
  421. else
  422. {
  423. // 寻找最早日期工单 && 计算生产数量-入库数据并且大于需求产品数量后累加直到满足需求产品数量
  424. var morderListData = morderList.Where(x => x.planner_end_date.Value.AddDays(-1) < DeliverDate).OrderByDescending(x => x.planner_end_date).ToList();
  425. if (morderListData.Count == 0)
  426. {
  427. //TODO:后期处理无在制工单返回内容
  428. throw new NotImplementedException("无可用在制工单!");
  429. }
  430. List<mes_mooccupy> mes_Mooccupies = new List<mes_mooccupy>();
  431. decimal? number = Quantity;
  432. foreach (var item in morderListData)
  433. {
  434. //查询出工单已占用总数量
  435. var mooccupies = mes_mooccupyList.Where(x => x.moo_moid == item.Id).ToList();
  436. var mes_Mooccupy = GetMooccupies(seorderentry, mysql_ic_item, item, mooccupies);
  437. mes_Mooccupies.Add(mes_Mooccupy);
  438. //需求数量-占用量后小于或等于0 停止循环占用工单
  439. if (number - mes_Mooccupy.moo_qty <= 0)
  440. {
  441. break;
  442. }
  443. }
  444. }
  445. return morderList;
  446. }
  447. /// <summary>
  448. /// 拼接工单占用表
  449. /// </summary>
  450. /// <param name="seorderentry">销售订单子表</param>
  451. /// <param name="mysql_ic_item">物料详情表</param>
  452. /// <param name="item">工单表</param>
  453. /// <param name="mes_mooccupy">占用工单表</param>
  454. /// <returns></returns>
  455. public mes_mooccupy GetMooccupies(crm_seorderentry seorderentry, ic_item mysql_ic_item, mes_morder item, List<mes_mooccupy> mes_mooccupy)
  456. {
  457. decimal? Sumqty = 0;
  458. if (mes_mooccupy.Count > 0)
  459. {
  460. Sumqty = mes_mooccupy.Sum(x => x.moo_qty);
  461. }
  462. //生成mes_mooccupy工单占用表数据,代表多个工单被某个销售订单已占用。
  463. mes_mooccupy mes_Mooccupy = new mes_mooccupy();
  464. mes_Mooccupy.GenerateNewId();
  465. mes_Mooccupy.moo_id_type = "分配";
  466. mes_Mooccupy.moo_id_billid = seorderentry.seorder_id;//销售订单ID
  467. mes_Mooccupy.fbill_no = seorderentry.bill_no;//销售订单号
  468. mes_Mooccupy.fentry_id = seorderentry.entry_seq.Value;//销售订单行
  469. mes_Mooccupy.fitem_name = mysql_ic_item.name;//物料名称
  470. mes_Mooccupy.fitem_number = mysql_ic_item.number;
  471. mes_Mooccupy.fmodel = mysql_ic_item.model;//规格型号
  472. mes_Mooccupy.moo_moid = item.Id;
  473. mes_Mooccupy.moo_mo = item.morder_no;
  474. //占用量=生产计划数量-入库数量-已被占用数量
  475. mes_Mooccupy.moo_qty = item.morder_production_number - item.inventory_number - Sumqty;
  476. mes_Mooccupy.moo_stime = DateTime.Now;
  477. mes_Mooccupy.moo_etime = DateTime.Now;//日期来源需确定
  478. mes_Mooccupy.moo_state = 1;
  479. mes_Mooccupy.moo_cbr = string.Empty;
  480. //mes_Mooccupy.moo_ctime = ;
  481. mes_Mooccupy.moo_creason = string.Empty;
  482. mes_Mooccupy.tenant_id = 0;//TODO:企业ID =集团 或公司ID
  483. return mes_Mooccupy;
  484. }
  485. /// <summary>
  486. /// BOM预处理
  487. /// </summary>
  488. /// <param name="orderid"></param>
  489. /// <param name="BomId"></param>
  490. /// <param name="Quantity"></param>
  491. public void BomPretreatment(long? orderid, long? BomId, int Quantity)
  492. {
  493. if (orderid == null)
  494. {
  495. //throw new bu
  496. }
  497. if (BomId == null)
  498. {
  499. //throw new bu
  500. }
  501. //var query = (await _ic_bom.GetQueryableAsync()).WhereIf(true, a => a.bom_id == BomId).ToList();
  502. var help = new SnowFlake();
  503. var bomlist = _ic_bom.GetAll().Result;
  504. var bomchildlist = _ic_bom_child.GetAll().Result;
  505. var icitemlist = _ic_item.GetAll().Result;
  506. List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  507. var dto = new BomChildExamineDto();
  508. dto.bom_id = BomId.Value;
  509. dto.level = 1;
  510. dto.id = help.NextId();
  511. dto.parent_id = help.NextId();
  512. dto.qty = 1;
  513. dto.num = "1";
  514. dto.isbom = 1;
  515. dto.is_replace = 0;
  516. dto.haveicsubs = 0;
  517. dto.substitute_code = "";
  518. dto.icitem_ids = "";
  519. int type = 0;
  520. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist, type);
  521. }
  522. /// <summary>
  523. /// BOM预处理层级组装
  524. /// </summary>
  525. /// <param name="bomlist"></param>
  526. /// <param name="bomchildlist"></param>
  527. /// <param name="icitemlist"></param>
  528. /// <param name="dto"></param>
  529. /// <param name="returnlist"></param>
  530. public void GetBomList(List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist, BomChildExamineDto dto, List<BomChildExamineDto> returnlist, int type)
  531. {
  532. int level = dto.level++;//初始化定义level层级
  533. var help = new SnowFlake();
  534. var bom = bomlist.WhereIf(true, s => s.Id == dto.bom_id).FirstOrDefault();
  535. var item = icitemlist.WhereIf(true, a => a.Id == bom.icitem_id).FirstOrDefault();
  536. if (bom == null || item == null)
  537. {
  538. }
  539. dto.item_id = bom.icitem_id;
  540. dto.item_name = bom.item_name;
  541. dto.item_code = bom.item_number;
  542. dto.model = item.model;
  543. dto.unit = bom.unit;
  544. dto.erp_cls = item.erp_cls;
  545. dto.erp_cls_name = item.erp_cls_name;
  546. dto.type = type;
  547. //var bdto = ObjectMapper.Map<ic_bom,BomChildExamineDto>(bom);
  548. returnlist.Add(dto);
  549. var childlist = bomchildlist.WhereIf(true, a => a.bom_id == bom.Id).ToList();
  550. int idx = 1;
  551. foreach (var c in childlist)
  552. {
  553. string childNum = dto.num + "." + idx.ToString();
  554. var icitem = icitemlist.WhereIf(true, a => a.Id == c.icitem_id).FirstOrDefault();
  555. var childBom = bomlist.WhereIf(true, a => a.icitem_id == c.icitem_id).FirstOrDefault();
  556. //如果此明细查的到BOM信息,则代表此child是一个子BOM。
  557. if (childBom != null)
  558. {
  559. var cdto = new BomChildExamineDto();
  560. cdto.id = help.NextId();
  561. cdto.level = level;
  562. cdto.parent_id = dto.id;
  563. cdto.bom_child_id = c.Id;
  564. cdto.qty = c.qty.Value;
  565. cdto.backflush = c.backflush;
  566. cdto.num = childNum;
  567. cdto.isbom = 1;
  568. cdto.is_replace = c.is_replace;
  569. cdto.haveicsubs = c.haveicsubs;
  570. cdto.substitute_code = c.substitute_code;
  571. cdto.icitem_ids = c.icitem_ids;
  572. cdto.type = type;
  573. //递归寻找子级
  574. GetBomList(bomlist, bomchildlist, icitemlist, cdto, returnlist, type);
  575. }
  576. else
  577. {
  578. if (icitem != null)
  579. {
  580. var childDto = new BomChildExamineDto();
  581. childDto.level = level++;
  582. childDto.bom_id = dto.bom_id;
  583. childDto.bom_child_id = c.Id;
  584. childDto.id = help.NextId();
  585. childDto.parent_id = dto.id;
  586. childDto.item_id = icitem.Id;
  587. childDto.item_name = icitem.name;
  588. childDto.item_code = icitem.number;
  589. childDto.num = childNum;
  590. childDto.model = icitem.model;
  591. childDto.unit = c.unit;
  592. childDto.erp_cls = item.erp_cls;
  593. childDto.erp_cls_name = item.erp_cls_name;
  594. childDto.backflush = c.backflush;
  595. childDto.qty = c.qty.Value;
  596. childDto.isbom = 0;
  597. childDto.is_replace = c.is_replace;
  598. childDto.haveicsubs = c.haveicsubs;
  599. childDto.substitute_code = c.substitute_code;
  600. childDto.icitem_ids = c.icitem_ids;
  601. childDto.type = type;
  602. returnlist.Add(childDto);
  603. }
  604. }
  605. idx++;
  606. }
  607. }
  608. /// <summary>
  609. /// BOM替代关系预处理
  610. /// </summary>
  611. public void BomSubstitute(List<BomChildExamineDto> returnlist, List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, List<ic_item> icitemlist)
  612. {
  613. var sublist = _ic_substitute.GetManyByCondition(s => s.substitute_code.IsIn(returnlist.Select(c => c.substitute_code))).Result.ToList();
  614. var suballlist = _ic_substitute_all.GetManyByCondition(s => s.substitute_id.IsIn(sublist.Select(c => c.Id))).Result.ToList();
  615. var subdtllist = _ic_substitute_all_dtl.GetManyByCondition(s => s.substitute_allid.IsIn(suballlist.Select(c => c.Id))).Result.ToList();
  616. List<long> childidList = new List<long>();
  617. var help = new SnowFlake();
  618. int type = 1;
  619. //除顶级外,其他层级关系全带出来。生成平铺
  620. foreach (var item in returnlist)
  621. {
  622. //最顶级、虚拟件
  623. if (item.level != 1 && item.erp_cls != 4 && !childidList.Contains(item.bom_child_id.GetValueOrDefault()))
  624. {
  625. //有替代关系
  626. if (item.haveicsubs == 1)
  627. {
  628. if (!string.IsNullOrEmpty(item.icitem_ids))
  629. {
  630. long cid = 1;
  631. var cids = item.icitem_ids.Split(',');
  632. foreach (var c in cids)
  633. {
  634. if (long.TryParse(c, out cid))
  635. {
  636. childidList.Add(cid);
  637. }
  638. }
  639. }
  640. //找到当前物料的替代群组关系集
  641. var sl = sublist.Find(s => s.substitute_code == item.substitute_code);
  642. if (sl != null)
  643. {
  644. var sall = suballlist.Where(s => s.substitute_id == sl.Id).ToList();
  645. foreach (var sal in sall)
  646. {
  647. var sadl = subdtllist.Where(s => s.substitute_allid == sal.Id).ToList();
  648. foreach (var dtl in sadl)
  649. {
  650. if (dtl.ismain != 0)//替代关系里,已经将BOM料当成主料存放于替代群组里了。
  651. {
  652. //递归将替代关系组装出来。
  653. SubstitutePretreatment(sl, sal, dtl, item, returnlist, icitemlist, bomlist, bomchildlist, type);
  654. }
  655. else {
  656. //将主料赋值上属性
  657. var dtlitem = returnlist.Find(s => s.item_id == dtl.icitem_id && s.level == item.level);
  658. dtlitem.substitute_all_num = sal.order_num;//群组优先级
  659. }
  660. }
  661. }
  662. }
  663. }
  664. }
  665. }
  666. }
  667. /// <summary>
  668. /// 替代关系递归组装出来
  669. /// </summary>
  670. /// <param name="sal"></param>
  671. /// <param name="dtl"></param>
  672. /// <param name="toDto"></param>
  673. /// <param name="returnlist"></param>
  674. /// <param name="icitemlist"></param>
  675. /// <param name="bomlist"></param>
  676. /// <param name="bomchildlist"></param>
  677. public void SubstitutePretreatment(ic_substitute sl, ic_substitute_all sal, ic_substitute_all_dtl dtl, BomChildExamineDto toDto, List<BomChildExamineDto> returnlist, List<ic_item> icitemlist, List<ic_bom> bomlist, List<ic_bom_child> bomchildlist, int type)
  678. {
  679. //如果dtl对应的icitem是BOM,还需要向下继续展开。
  680. var help = new SnowFlake();
  681. //List<BomChildExamineDto> returnlist = new List<BomChildExamineDto>();
  682. var dto = new BomChildExamineDto();
  683. var bom = bomlist.WhereIf(true, s => s.icitem_id == dtl.icitem_id).FirstOrDefault();
  684. var icitem = icitemlist.Find(s => s.Id == dtl.icitem_id);
  685. if (icitem == null)
  686. {
  687. return;
  688. }
  689. dto.id = help.NextId();
  690. dto.level = toDto.level;
  691. dto.parent_id = toDto.parent_id;
  692. dto.item_id = icitem.Id;
  693. dto.item_name = icitem.name;
  694. dto.item_code = icitem.number;
  695. dto.num = toDto.num;
  696. dto.model = icitem.model;
  697. dto.unit = icitem.unit;
  698. dto.erp_cls = icitem.erp_cls;
  699. dto.erp_cls_name = icitem.erp_cls_name;
  700. dto.backflush = toDto.backflush;
  701. //dto.qty = toDto.qty;
  702. //dto.replace_amount = dtl.replace_amount.Value;
  703. dto.qty = dtl.replace_amount.Value;
  704. dto.is_replace = 0;
  705. dto.haveicsubs = 0;
  706. dto.substitute_code = "";
  707. dto.icitem_ids = "";
  708. dto.substitute_strategy = sl.substitute_strategy.Value;//替代策略
  709. dto.substitute_mode = sl.substitute_mode.Value;//替代方式
  710. dto.type = type;
  711. dto.substitute_all_num = sal.order_num;//群组优先级
  712. if (bom != null)
  713. {
  714. dto.bom_id = bom.Id;
  715. dto.qty = dtl.replace_amount.Value;
  716. dto.isbom = 1;
  717. dto.is_replace = 0;
  718. dto.haveicsubs = 0;
  719. dto.substitute_code = "";
  720. dto.icitem_ids = "";
  721. GetBomList(bomlist, bomchildlist, icitemlist, dto, returnlist, type);
  722. }
  723. else
  724. {
  725. dto.bom_id = null;
  726. dto.isbom = 0;
  727. returnlist.Add(dto);
  728. }
  729. }
  730. /// <summary>
  731. /// 计算物料库存量
  732. /// </summary>
  733. /// <param name="returnlist"></param>
  734. public void BomStock(List<BomChildExamineDto> returnlist, long bangid, long orderid, long orderentryid, long factoryid)
  735. {
  736. returnlist = returnlist.OrderBy(s => s.num).ToList();
  737. //取物料库存记录
  738. FilterDefinition<ic_item_stock> filter = Builders<ic_item_stock>.Filter.In(s => s.icitem_id, returnlist.Select(c => c.item_id).ToList());
  739. var stocklist = _ic_item_stock.GetManyByIds(filter).Result;
  740. //取当前订单的物料库存占用记录
  741. var occupylist = _ic_item_stockoccupy.GetManyByCondition(p => p.bang_id == bangid && p.order_id == orderid).Result;
  742. //计算剩余库存
  743. foreach (var item in returnlist)
  744. {
  745. if (item.erp_cls == 4)//虚拟件不计算
  746. {
  747. continue;
  748. }
  749. //非虚拟件
  750. //当前物料的库存数量
  751. decimal stockQty = stocklist.Where(s => s.icitem_id == item.item_id).Sum(p => p.sqty.GetValueOrDefault());
  752. //获取当前订单其他订单行当前物料的占用数量
  753. decimal otherStockQty = occupylist.Where(s => s.icitem_id == item.item_id).Sum(p => p.quantity);
  754. //当前订单行物料库存情况
  755. item.sqty = stockQty - otherStockQty;
  756. }
  757. }
  758. public void calcTest(List<BomChildExamineDto> returnlist, long bangid, long orderid)
  759. {
  760. //占用情况
  761. var occupylist = _ic_item_stockoccupy.GetManyByCondition(p => p.icitem_id == bangid && p.order_id == orderid).Result;
  762. //第一级
  763. var childList = returnlist.Where(s => s.parent_id == returnlist[0].id && s.type == 0).ToList();
  764. //这是从上往下展开计算缺料和可制
  765. calcTest2(returnlist[0], childList, returnlist, occupylist);
  766. returnlist[0].kz = childList.Min(s => s.kz);//得到最小可制数量。
  767. //再加个循环,来根据替代关系里的检查结果,根据规则明确使用和生成占用关系。
  768. foreach (var item in childList)
  769. {
  770. //提取群组关系
  771. var sublist = returnlist.Where(s => s.parent_id == item.parent_id && s.num == item.num && s.level == item.level).OrderBy(c => c.substitute_all_num).ToList();
  772. int idx;
  773. List<BomChildExamineDto> select = new List<BomChildExamineDto>();
  774. switch (item.substitute_strategy)
  775. {
  776. case 0://整批
  777. //循环检查哪一批可以直接使用,不需要采购
  778. for (idx = 0; idx < 99; idx++)
  779. {
  780. var list = sublist.Where(s => s.substitute_all_num == idx).ToList();
  781. if (list.Any())
  782. {
  783. if (list.Where(s => s.stock_state != 1 && s.stock_state != 2 && s.stock_state != 3).Count() == 0)
  784. {
  785. //只满足充足或可制
  786. //无缺料情况
  787. select = list;
  788. break;
  789. }
  790. idx++;
  791. }
  792. else
  793. {
  794. idx = 99;
  795. }
  796. }
  797. //如果都需要采购的情况下,则默认使用优先级最高的
  798. if (select.Count() == 0)
  799. {
  800. //如果为空,则默认使用优先级为0的集合作为替代关系
  801. select = sublist.Where(s => s.substitute_all_num == 0).ToList();
  802. select.ForEach(s => { s.is_use = true; s.stock_state = 4; });
  803. }
  804. break;
  805. case 1://混用
  806. for (idx = 0; idx < 99; idx++)
  807. {
  808. var list = sublist.Where(s => s.substitute_all_num == idx).ToList();
  809. if (list.Any())
  810. {
  811. foreach (var hy in list)
  812. {
  813. //如果自己是可制,并且有子集,则取出自己父级的缺料数量和可制数量,使用掉自己的末级。
  814. }
  815. idx++;
  816. }
  817. }
  818. break;
  819. case 2://整批加混用
  820. break;
  821. }
  822. }
  823. }
  824. public void calcTest2(BomChildExamineDto parent, List<BomChildExamineDto> bzlist, List<BomChildExamineDto> returnlist, List<ic_item_stockoccupy> sockoccupyList)
  825. {
  826. //从第二级开始循环
  827. foreach (var item in bzlist)
  828. {
  829. var childList = returnlist.Where(s => s.parent_id == item.id).ToList();
  830. //存在替代关系
  831. if (item.haveicsubs == 1)
  832. {
  833. //提取群组关系
  834. var sublist = returnlist.Where(s => s.parent_id == item.parent_id && s.num == item.num && s.level == item.level).OrderBy(c => c.substitute_all_num).ToList();
  835. //循环计算群组,看哪个群组满足要求,然后使用此群组,将群组的库存和子物料占用掉。
  836. //计算此次群组是否有符合
  837. MaterialCalc(sublist, returnlist, sockoccupyList);
  838. }
  839. }
  840. }
  841. /// <summary>
  842. /// 计算
  843. /// </summary>
  844. /// <param name="sublist"></param>
  845. public void MaterialCalc(List<BomChildExamineDto> sublist, List<BomChildExamineDto> returnlist, List<ic_item_stockoccupy> sockoccupyList)
  846. {
  847. int maxIdx = sublist.Max(s => s.substitute_all_num);
  848. for (int i = 0; i <= maxIdx; i++)
  849. {
  850. var group = sublist.Where(s => s.substitute_all_num == i).ToList();
  851. int boolCount = 0;//代表某一颗物料无需采购,可以自制,则集合+1。
  852. //如果替代料库存不够,但是可制够,则也考虑使用优先级最高
  853. foreach (var g in group)
  854. {
  855. //根据占用情况计算库存
  856. Calczykc(g, sockoccupyList);
  857. if (g.stock_state != 1)
  858. {
  859. //判断此料是否BOM,如果是BOM,就考虑自制是否足够,此处递归检查子集
  860. var gChildList = returnlist.Where(s => s.parent_id == g.id).ToList();
  861. if (gChildList.Count > 0)
  862. {
  863. calcTest2(g, gChildList, returnlist, sockoccupyList);
  864. //如果是自制,则考虑自制够不够
  865. if (g.erp_cls == 1)
  866. {
  867. g.kz = gChildList.Min(s => s.kz);//得到最小可制数量。
  868. //todo:如果是群组替代,可制数量应该跟随着主料来,不应该受限于最小可制单元。
  869. g.stock_state = gChildList.Max(s => s.stock_state);
  870. if (g.lack_qty < g.kz)
  871. {
  872. g.stock_state = 2;
  873. boolCount++;
  874. }
  875. }
  876. }
  877. else {
  878. g.kz = Math.Floor(g.sqty / g.qty);//自己不是BOM的情况下,算一下自己可制父级可以制造多少个,这个可制只是基于父级BOM才用来运算。
  879. //todo:申老师说,如果是苏州工厂,原材料有可能也是自制的。
  880. //所以这里在计算时,还可以直接拿缺料数量,去丢给苏州计算方法,得出原材料的库存加可制。
  881. }
  882. }
  883. else {
  884. boolCount++;
  885. }
  886. }
  887. if (boolCount == group.Count())
  888. {
  889. //如果检查集合满足条数与群组里的物料条数相同,则代表这个群组的数量是满足的。
  890. group.ForEach(s =>
  891. {
  892. s.is_show = true;
  893. });
  894. break;//如果已经找到合适的替代群组关系,并且都不需要采购,则直接不继续检查了。
  895. }
  896. }
  897. }
  898. //根据每个物料来实时计算占用情况
  899. public void Calczykc(BomChildExamineDto item, List<ic_item_stockoccupy> sockoccupyList)
  900. {
  901. //找到当前物料的占用记录
  902. var itemSockoccupy = sockoccupyList.Where(s => s.icitem_id == item.item_id).ToList();
  903. //计算库存减去占用
  904. item.sqty -= itemSockoccupy.Sum(s => s.quantity);
  905. //如果库存
  906. item.sqty = item.sqty < 0 ? 0 : item.sqty;
  907. //判断缺料数量
  908. item.lack_qty = item.needCount - item.sqty;
  909. //判断状态
  910. item.stock_state = item.lack_qty > 0 ? 0 : 1;
  911. }
  912. #region 替代检查第一版,屏蔽
  913. /*/// <summary>
  914. /// 替代关系检查计算
  915. /// </summary>
  916. public void CalcIcitemSubstitute(List<BomChildExamineDto> returnlist, int count)
  917. {
  918. returnlist = returnlist.OrderBy(s => s.num).ToList();
  919. //1.如果主料够的时候,不需要显示替代料的平铺视图,如果主料不够,显示替代料的平铺视图。
  920. //2.替代策略和替代方式,影响到的是甲乙组概念,替代按主料有限,取代按组的优先级。A与B的替代,则A和B各自会存在一个组。
  921. List<long> calcIds = new List<long>();
  922. //先处理下最顶级的产品需要数量
  923. returnlist[0].needCount = returnlist[0].qty * count;
  924. returnlist[0].satisfyNum = returnlist[0].sqty;
  925. returnlist[0].lack_qty = returnlist[0].needCount - returnlist[0].satisfyNum;
  926. returnlist[0].is_show = true;
  927. foreach (var item in returnlist)
  928. {
  929. //循环平铺整个资源检查的物料库存情况、缺料情况
  930. CaclMaterialShortage(returnlist, item, count);
  931. }
  932. foreach (var item in returnlist)
  933. {
  934. //替代件不计算,替代件通过标准件的替代关系,去计算需要使用哪些物料
  935. if (item.type == 1)
  936. {
  937. continue;
  938. }
  939. CaclBomChildUseShortage(returnlist, item);
  940. }
  941. }
  942. /// <summary>
  943. /// 物料计算
  944. /// </summary>
  945. /// <param name="returnlist"></param>
  946. /// <param name="item"></param>
  947. /// <param name="count"></param>
  948. public void CaclBomChildUseShortage(List<BomChildExamineDto> returnlist, BomChildExamineDto item)
  949. {
  950. //判断是否是BOM,如果是BOM,还需要向下展开
  951. var bomchild = returnlist.Where(s => s.parent_id == item.id).ToList();
  952. if (bomchild.Count > 0)
  953. {
  954. foreach (var child in bomchild)
  955. {
  956. CaclBomChildUseShortage(returnlist, item);
  957. //取子级的最高状态
  958. item.stock_state = bomchild.Max(s => s.stock_state);
  959. item.kitting_time = bomchild.Max(s => s.kitting_time);
  960. }
  961. }
  962. //var tolist = returnlist.Where(s => s.parent_id == item.parent_id && s.num == item.num).ToList();
  963. //有替代关系
  964. if (item.haveicsubs == 1)
  965. {
  966. //首先判断标准件库存是否满足。
  967. //如果是BOM,也需要向下展开,看子物料是否满足。
  968. //不满足的情况下,则需要展开替代关系,根据替代策略和替代方式,来判定替代件的库存。
  969. //假设子BOM有替代关系,则子BOM的子物料,也是有替代关系存在的,需要展开来看。
  970. //1.假设标准件库存满足,则不计算替代件关系。
  971. //2.如果标准件不满足,则替代件开始计算并展示。
  972. //注意:不管是否需要替代件,标准件都需要展开。没有使用到的物料,定义为无需求。如果子BOM库存足够,则不需要显示子物料和替代关系。
  973. var sublist = returnlist.Where(s => s.parent_id == item.parent_id && s.num == item.num && s.level == item.level).OrderBy(c => c.substitute_all_num).ToList();
  974. //sublist找出当前含替代关系的标准件和替代件。
  975. switch (item.substitute_strategy)
  976. {
  977. case 0://整批
  978. break;
  979. case 1://混用
  980. break;
  981. case 2://整批加混用
  982. break;
  983. }
  984. if (item.substitute_mode == 0)
  985. {
  986. //替代
  987. }
  988. else {
  989. //取代
  990. }
  991. }
  992. else//无替代关系
  993. {
  994. if (item.stock_state==0)
  995. {
  996. item.use_qty = item.needCount;
  997. item.is_show = true;
  998. }
  999. else if (item.stock_state == 1)
  1000. {
  1001. item.use_qty = item.sqty;
  1002. // 缺料数量 item.lack_qty
  1003. // 使用数量将库存全部使用了 item.use_qty = item.sqty;
  1004. if (item.erp_cls == 2 || item.erp_cls == 3)
  1005. {
  1006. //委外、外购
  1007. //生成委外工单、采购申请单
  1008. //得到单据到货时间。
  1009. //todo:初步设置为7天到货期,后期根据实际业务来补充修改。
  1010. item.kitting_time = DateTime.Now.AddDays(7);
  1011. item.stock_state = 2;
  1012. }
  1013. else if (item.erp_cls == 1)
  1014. {
  1015. //自制
  1016. //调用产能计算,得到物料自制后的齐套时间。
  1017. //todo:初步设置为7天完成,等沟通调用方法,来修改此处。
  1018. item.kitting_time = DateTime.Now.AddDays(7);
  1019. item.stock_state = 3;
  1020. }
  1021. }
  1022. }
  1023. }
  1024. /// <summary>
  1025. /// 平铺物料缺料情况,展示所有主料+替代料的库存情况、缺料情况---需要修改成 库存情况、占用情况
  1026. /// </summary>
  1027. /// <param name="returnlist"></param>
  1028. /// <param name="item"></param>
  1029. /// <param name="count"></param>
  1030. public void CaclMaterialShortage(List<BomChildExamineDto> returnlist, BomChildExamineDto item, int count)
  1031. {
  1032. var parent = returnlist.Find(s => s.id == item.parent_id);
  1033. //当前物料总共需要数量
  1034. item.needCount = parent.needCount * item.qty;
  1035. //当前库存可以满足的数量
  1036. item.satisfyNum = item.sqty;
  1037. //总需要数量减去库存量,得出缺料数量
  1038. item.lack_qty = item.needCount - item.satisfyNum;
  1039. //如果不满足,计算子物料,或者计算替代料
  1040. if (item.lack_qty < 0)
  1041. {
  1042. //库存满足
  1043. item.stock_state = 0;
  1044. item.lack_qty = 0;
  1045. }
  1046. else
  1047. {
  1048. //找出自己的子集,存在子集则是BOM,不存在子集,则自己非BOM。
  1049. var childList = returnlist.Where(s => s.parent_id == item.id && s.type == item.type).ToList();
  1050. if (childList.Count > 0)
  1051. {
  1052. //自己是BOM
  1053. decimal kz = 0;//当前BOM可制数量
  1054. foreach (var child in childList)
  1055. {
  1056. //循环子集判断库存
  1057. CaclMaterialShortage(returnlist, child, count);
  1058. kz = kz > ((child.sqty+ child.kz) / child.qty) ? Math.Floor((child.sqty + child.kz) / child.qty) : kz;
  1059. }
  1060. //向下取整
  1061. item.kz =Math.Floor(kz);
  1062. }
  1063. else
  1064. {
  1065. //原材料没有可制数量。
  1066. item.kz = 0;
  1067. item.stock_state = 1;
  1068. }
  1069. }
  1070. }*/
  1071. #endregion
  1072. }
  1073. }