Update_8_57.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using com.sun.org.apache.xml.@internal.security.utils;
  2. using Comal.Classes;
  3. using InABox.Core;
  4. using InABox.Database;
  5. using Syncfusion.XlsIO.FormatParser.FormatTokens;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace PRS.Shared.Database_Update_Scripts;
  12. internal class Update_8_57 : DatabaseUpdateScript
  13. {
  14. public override VersionNumber Version => new(8, 57);
  15. private void ApproveOldSetouts(IProvider provider)
  16. {
  17. Logger.Send(LogType.Information, "", "Approving all old setouts");
  18. var setouts = provider.Query(
  19. Filter<Setout>.Where(x => x.Status).IsEqualTo(SetoutStatus.Unapproved)
  20. .And(x => x.ID).NotInQuery(
  21. Filter<StagingSetout>.Where(x => x.Task.ID).IsNotEqualTo(Guid.Empty)
  22. .Or(x => x.UnapprovedDocuments).IsGreaterThan(0),
  23. x => x.Setout.ID),
  24. Columns.None<Setout>()
  25. .Add(x => x.ID)
  26. .Add(x => x.Status))
  27. .ToArray<Setout>();
  28. foreach(var setout in setouts)
  29. {
  30. setout.Status = SetoutStatus.Approved;
  31. }
  32. provider.Save(setouts);
  33. Logger.Send(LogType.Information, "", "Approving all old manufacturing packets");
  34. var packets = provider.Query(
  35. Filter<ManufacturingPacket>.Where(x => x.Approved).IsEqualTo(DateTime.MinValue),
  36. Columns.None<ManufacturingPacket>()
  37. .Add(x => x.ID)
  38. .Add(x => x.Approved)
  39. .Add(x => x.Created))
  40. .ToArray<ManufacturingPacket>();
  41. foreach(var packet in packets)
  42. {
  43. packet.Approved = packet.Created;
  44. }
  45. provider.Save(packets);
  46. }
  47. public override bool Update()
  48. {
  49. var provider = DbFactory.NewProvider(Logger.Main);
  50. ApproveOldSetouts(provider);
  51. var stagingSetoutsQueue = provider.Query(
  52. Filter<StagingSetout>.Where(x => x.Setout.ID).IsEqualTo(Guid.Empty)
  53. .Or(x => x.Task.ID).IsNotEqualTo(Guid.Empty)
  54. .Or(x => x.UnapprovedDocuments).IsGreaterThan(0),
  55. Columns.None<StagingSetout>()
  56. .Add(x => x.ID)
  57. .Add(x => x.Created)
  58. .Add(x => x.CreatedBy)
  59. .Add(x => x.Number)
  60. .Add(x => x.JobLink.ID)
  61. .Add(x => x.Group.ID)
  62. .Add(x => x.OriginalPath)
  63. .Add(x => x.OriginalCRC)
  64. .Add(x => x.SavePath)
  65. .Add(x => x.Archived)
  66. .Add(x => x.LockedBy.ID)
  67. .Add(x => x.Task.ID)
  68. .Add(x => x.Task.EmployeeLink.ID)
  69. .Add(x => x.Task.Title)
  70. .Add(x => x.Task.Description)
  71. .Add(x => x.Task.Notes)
  72. .Add(x => x.Task.Completed)
  73. .Add(x => x.JobScope.ID)
  74. .Add(x => x.Setout.ID))
  75. .ToArray<StagingSetout>()
  76. .ToQueue();
  77. Logger.Send(LogType.Information, "", $"Updating {stagingSetoutsQueue.Count} staging setouts");
  78. var total = stagingSetoutsQueue.Count;
  79. var processed = 0;
  80. while(stagingSetoutsQueue.Count > 0)
  81. {
  82. Logger.Send(LogType.Information, "", $"Updating {stagingSetoutsQueue.Count} staging setouts; {processed}/{total}");
  83. var stagingSetouts = stagingSetoutsQueue.Dequeue(100).ToArray();
  84. ProcessStagingSetouts(provider, stagingSetouts);
  85. processed += stagingSetouts.Length;
  86. }
  87. return true;
  88. }
  89. void ProcessStagingSetouts(IProvider provider, StagingSetout[] stagingSetouts)
  90. {
  91. var setoutIDs = stagingSetouts.ToArray(x => x.ID);
  92. var stagingSetoutDocuments = provider.Query(
  93. Filter<StagingSetoutDocument>.Where(x => x.EntityLink.ID).InList(setoutIDs),
  94. Columns.None<StagingSetoutDocument>()
  95. .Add(x => x.Approved)
  96. .Add(x => x.Notes)
  97. .Add(x => x.Superceded)
  98. .Add(x => x.Thumbnail)
  99. .Add(x => x.Type.ID)
  100. .Add(x => x.EntityLink.ID)
  101. .Add(x => x.DocumentLink.ID))
  102. .ToObjects<StagingSetoutDocument>()
  103. .GroupByDictionary(x => x.EntityLink.ID);
  104. var stagingSetoutForms = provider.Query(
  105. Filter<StagingSetoutForm>.Where(x => x.Parent.ID).InList(setoutIDs),
  106. Columns.None<StagingSetoutForm>()
  107. .Add(x => x.Parent.ID)
  108. .Add(x => x.Number)
  109. .Add(x => x.Description)
  110. .Add(x => x.Form.ID)
  111. .Add(x => x.FormData)
  112. .Add(x => x.BlobData)
  113. .Add(x => x.FormStarted)
  114. .Add(x => x.FormCompleted)
  115. .Add(x => x.FormProcessed)
  116. .Add(x => x.FormCancelled)
  117. .Add(x => x.FormCompletedBy.ID)
  118. .Add(x => x.Location.Address)
  119. .Add(x => x.Location.Latitude)
  120. .Add(x => x.Location.Longitude)
  121. .Add(x => x.Location.Timestamp)
  122. .Add(x => x.FormOpen)
  123. .Add(x => x.Sequence))
  124. .ToObjects<StagingSetoutForm>()
  125. .GroupByDictionary(x => x.Parent.ID);
  126. var stagingSetoutComponents = provider.Query(
  127. Filter<StagingSetoutComponent>.Where(x => x.StagingSetout.ID).InList(setoutIDs)
  128. .And(x => x.StagingManufacturingPacketComponent.ID).IsEqualTo(Guid.Empty),
  129. Columns.None<StagingSetoutComponent>()
  130. .Add(x => x.StagingSetout.ID)
  131. .Add(x => x.Product.ID)
  132. .Add(x => x.Description)
  133. .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local)
  134. .Add(x => x.Quantity)
  135. .Add(x => x.Sequence))
  136. .ToObjects<StagingSetoutComponent>()
  137. .GroupByDictionary(x => x.StagingSetout.ID);
  138. var stagingManufacturingPackets = provider.Query(
  139. Filter<StagingManufacturingPacket>.Where(x => x.StagingSetout.ID).InList(setoutIDs),
  140. Columns.None<StagingManufacturingPacket>()
  141. .Add(x => x.ID)
  142. .Add(x => x.Title)
  143. .Add(x => x.Serial)
  144. .Add(x => x.ITP.ID)
  145. .Add(x => x.StagingSetout.ID)
  146. .Add(x => x.Watermark)
  147. .Add(x => x.Location)
  148. .Add(x => x.Quantity)
  149. .Add(x => x.BarcodeQuantity)
  150. .Add(x => x.Group.ID)
  151. .Add(x => x.Group.Watermark)
  152. .Add(x => x.Template.ID)
  153. .Add(x => x.ManufacturingPacket.ID))
  154. .ToObjects<StagingManufacturingPacket>()
  155. .GroupByDictionary(x => x.StagingSetout.ID);
  156. var manufacturingPackets = provider.Query(
  157. Filter<ManufacturingPacket>.Where(x => x.SetoutLink.ID).IsEqualTo(Guid.Empty)
  158. .And(x => x.ID).InQuery(
  159. Filter<StagingManufacturingPacket>.Where(x => x.StagingSetout.ID).InList(setoutIDs),
  160. x => x.ManufacturingPacket.ID),
  161. Columns.None<ManufacturingPacket>()
  162. .Add(x => x.ID)
  163. .Add(x => x.SetoutLink.ID))
  164. .ToObjects<ManufacturingPacket>()
  165. .ToDictionary(x => x.ID);
  166. var stagingManufacturingPacketComponents = provider.Query(
  167. Filter<StagingManufacturingPacketComponent>.Where(x => x.Packet.ID).InQuery(
  168. Filter<StagingManufacturingPacket>.Where(x => x.StagingSetout.ID).InList(setoutIDs),
  169. x => x.ID),
  170. Columns.None<StagingManufacturingPacketComponent>()
  171. .Add(x => x.Packet.ID)
  172. .Add(x => x.Description)
  173. .Add(x => x.Product.ID)
  174. .Add(x => x.Quantity)
  175. .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local))
  176. .ToObjects<StagingManufacturingPacketComponent>()
  177. .GroupByDictionary(x => x.Packet.ID);
  178. var stagingManufacturingPacketStages = provider.Query(
  179. Filter<StagingManufacturingPacketStage>.Where(x => x.Packet.ID).InQuery(
  180. Filter<StagingManufacturingPacket>.Where(x => x.StagingSetout.ID).InList(setoutIDs),
  181. x => x.ID),
  182. Columns.None<StagingManufacturingPacketStage>()
  183. .Add(x => x.Packet.ID)
  184. .Add(x => x.Time)
  185. .Add(x => x.Sequence)
  186. .Add(x => x.SequenceType)
  187. .Add(x => x.QualityChecks)
  188. .Add(x => x.Section.ID))
  189. .ToObjects<StagingManufacturingPacketStage>()
  190. .GroupByDictionary(x => x.Packet.ID);
  191. var stagingManufacturingPacketTreatments = provider.Query(
  192. Filter<StagingManufacturingPacketTreatment>.Where(x => x.Packet.ID).InQuery(
  193. Filter<StagingManufacturingPacket>.Where(x => x.StagingSetout.ID).InList(setoutIDs),
  194. x => x.ID),
  195. Columns.None<StagingManufacturingPacketTreatment>()
  196. .Add(x => x.Packet.ID)
  197. .Add(x => x.Product.ID)
  198. .Add(x => x.Parameter))
  199. .ToObjects<StagingManufacturingPacketTreatment>()
  200. .GroupByDictionary(x => x.Packet.ID);
  201. var oldSetouts = provider.Query(
  202. Filter<Setout>.Where(x => x.ID).InList(stagingSetouts.ToArray(x => x.Setout.ID)),
  203. Columns.None<Setout>()
  204. .Add(x => x.ID))
  205. .ToObjects<Setout>()
  206. .ToDictionary(x => x.ID);
  207. var oldMap = new Dictionary<Setout, StagingSetout>();
  208. var setouts = new List<Setout>();
  209. var setoutDocuments = new Dictionary<Setout, List<SetoutDocument>>();
  210. var setoutForms = new Dictionary<Setout, List<SetoutForm>>();
  211. var components = new Dictionary<Setout, List<ManufacturingPacketComponent>>();
  212. var packets = new Dictionary<Setout, List<ManufacturingPacket>>();
  213. var packetComponents = new Dictionary<Setout, Dictionary<ManufacturingPacket, List<ManufacturingPacketComponent>>>();
  214. var packetStages = new Dictionary<ManufacturingPacket, List<ManufacturingPacketStage>>();
  215. var packetTreatments = new Dictionary<ManufacturingPacket, List<ManufacturingTreatment>>();
  216. foreach(var stagingSetout in stagingSetouts)
  217. {
  218. if(stagingSetout.Setout.ID == Guid.Empty
  219. || !oldSetouts.TryGetValue(stagingSetout.Setout.ID, out var setout))
  220. {
  221. setout = new Setout();
  222. setout.Created = stagingSetout.Created;
  223. setout.CreatedBy = stagingSetout.CreatedBy;
  224. setout.Number = stagingSetout.Number;
  225. setout.JobLink.ID = stagingSetout.JobLink.ID;
  226. setout.Group.ID = stagingSetout.Group.ID;
  227. setout.Status = SetoutStatus.Unapproved;
  228. setout.JobScope.ID = stagingSetout.JobScope.ID;
  229. }
  230. else
  231. {
  232. }
  233. if (oldMap.TryAdd(setout, stagingSetout))
  234. {
  235. setouts.Add(setout);
  236. }
  237. if(stagingSetoutDocuments.TryGetValue(stagingSetout.ID, out var documents))
  238. {
  239. var newSetoutDocuments = setoutDocuments.GetValueOrAdd(setout);
  240. if(documents.All(x => x.Approved))
  241. {
  242. setout.Status = SetoutStatus.Approved;
  243. }
  244. foreach(var stagingSetoutDocument in documents)
  245. {
  246. var setoutDocument = new SetoutDocument();
  247. setoutDocument.DocumentLink.ID = stagingSetoutDocument.DocumentLink.ID;
  248. setoutDocument.Type.ID = stagingSetoutDocument.Type.ID;
  249. setoutDocument.Thumbnail = stagingSetoutDocument.Thumbnail;
  250. setoutDocument.Superceded = stagingSetoutDocument.Superceded;
  251. setoutDocument.Notes = stagingSetoutDocument.Notes;
  252. setoutDocument.Staging.OriginalCRC = stagingSetout.OriginalCRC;
  253. setoutDocument.Staging.OriginalPath = stagingSetout.OriginalPath;
  254. setoutDocument.Staging.SavePath = stagingSetout.SavePath;
  255. setoutDocument.Staging.LockedBy.ID = stagingSetout.LockedBy.ID;
  256. newSetoutDocuments.Add(setoutDocument);
  257. }
  258. }
  259. if(stagingSetout.Task.ID != Guid.Empty)
  260. {
  261. setout.Status = SetoutStatus.Cancelled;
  262. setout.Problem.AssignedTo.ID = stagingSetout.Task.EmployeeLink.ID;
  263. setout.Problem.Notes = new string[]
  264. {
  265. stagingSetout.Task.Title,
  266. stagingSetout.Task.Description,
  267. }.Concatenate(stagingSetout.Task.Notes);
  268. setout.Problem.Resolved = stagingSetout.Task.Completed;
  269. }
  270. if(stagingSetoutForms.TryGetValue(stagingSetout.ID, out var forms))
  271. {
  272. var newSetoutForms = setoutForms.GetValueOrAdd(setout);
  273. foreach(var form in forms)
  274. {
  275. var newForm = new SetoutForm();
  276. newForm.Number = form.Number;
  277. newForm.Description = form.Description;
  278. newForm.Form.ID = form.Form.ID;
  279. newForm.FormData = form.FormData;
  280. newForm.BlobData = form.BlobData;
  281. newForm.FormStarted = form.FormStarted;
  282. newForm.FormCompleted = form.FormCompleted;
  283. newForm.FormProcessed = form.FormProcessed;
  284. newForm.FormCancelled = form.FormCancelled;
  285. newForm.FormCompletedBy.ID = form.FormCompletedBy.ID;
  286. newForm.Location.CopyFrom(form.Location);
  287. newForm.FormOpen = form.FormOpen;
  288. newForm.Sequence = form.Sequence;
  289. newSetoutForms.Add(newForm);
  290. }
  291. }
  292. if(stagingSetoutComponents.TryGetValue(stagingSetout.ID, out var componentsList))
  293. {
  294. var newComponents = components.GetValueOrAdd(setout);
  295. foreach(var component in componentsList)
  296. {
  297. var newComponent = new ManufacturingPacketComponent();
  298. newComponent.Product.ID = component.Product.ID;
  299. newComponent.Description = component.Description;
  300. newComponent.Dimensions.CopyFrom(component.Dimensions);
  301. newComponent.Quantity = component.Quantity;
  302. newComponent.Sequence = component.Sequence;
  303. newComponents.Add(newComponent);
  304. }
  305. }
  306. if(stagingManufacturingPackets.TryGetValue(stagingSetout.ID, out var stagingPackets))
  307. {
  308. var newPackets = packets.GetValueOrAdd(setout);
  309. var newSetoutPacketComponents = packetComponents.GetValueOrAdd(setout);
  310. foreach(var stagingPacket in stagingPackets)
  311. {
  312. if(stagingPacket.ManufacturingPacket.ID != Guid.Empty)
  313. {
  314. if(manufacturingPackets.TryGetValue(stagingPacket.ManufacturingPacket.ID, out var oldPacket))
  315. {
  316. newPackets.Add(oldPacket);
  317. }
  318. }
  319. else
  320. {
  321. var newPacket = new ManufacturingPacket();
  322. newPacket.Title = stagingPacket.Title;
  323. newPacket.Serial = stagingPacket.Serial;
  324. newPacket.ITP.ID = stagingPacket.ITP.ID;
  325. newPacket.WaterMark = stagingPacket.Watermark.NotWhiteSpaceOr(stagingPacket.Group.Watermark);
  326. newPacket.Location = stagingPacket.Location;
  327. newPacket.Quantity = stagingPacket.Quantity;
  328. newPacket.BarcodeQty = stagingPacket.BarcodeQuantity.IsNullOrWhiteSpace()
  329. ? stagingPacket.Quantity
  330. : int.Parse(stagingPacket.BarcodeQuantity);
  331. newPacket.TemplateGroup.ID = stagingPacket.Group.ID;
  332. newPacket.ManufacturingTemplateLink.ID = stagingPacket.Template.ID;
  333. newPackets.Add(newPacket);
  334. if(stagingManufacturingPacketComponents.TryGetValue(stagingPacket.ID, out var stagingComponents))
  335. {
  336. var newPacketComponents = new List<ManufacturingPacketComponent>();
  337. newSetoutPacketComponents.Add(newPacket, newPacketComponents);
  338. foreach(var stagingComponent in stagingComponents)
  339. {
  340. var component = new ManufacturingPacketComponent();
  341. component.Description = stagingComponent.Description;
  342. component.Product.ID = stagingComponent.Product.ID;
  343. component.Quantity = stagingComponent.Quantity;
  344. component.Dimensions.CopyFrom(stagingComponent.Dimensions);
  345. newPacketComponents.Add(component);
  346. }
  347. }
  348. if(stagingManufacturingPacketStages.TryGetValue(stagingPacket.ID, out var stagingStages))
  349. {
  350. var newStages = new List<ManufacturingPacketStage>();
  351. packetStages.Add(newPacket, newStages);
  352. foreach(var stagingStage in stagingStages)
  353. {
  354. var stage = new ManufacturingPacketStage
  355. {
  356. Time = stagingStage.Time,
  357. Sequence = stagingStage.Sequence,
  358. SequenceType = stagingStage.SequenceType,
  359. Started = DateTime.MinValue,
  360. PercentageComplete = 0.0F,
  361. Completed = DateTime.MinValue,
  362. QualityChecks = stagingStage.QualityChecks,
  363. QualityStatus = QualityStatus.NotChecked,
  364. QualityNotes = ""
  365. };
  366. stage.ManufacturingSectionLink.ID = stagingStage.Section.ID;
  367. newStages.Add(stage);
  368. }
  369. }
  370. if(stagingManufacturingPacketTreatments.TryGetValue(stagingPacket.ID, out var stagingTreatments))
  371. {
  372. var newTreatments = new List<ManufacturingTreatment>();
  373. packetTreatments.Add(newPacket, newTreatments);
  374. foreach(var stagingTreatment in stagingTreatments)
  375. {
  376. var treatment = new ManufacturingTreatment();
  377. treatment.Product.ID = stagingTreatment.Product.ID;
  378. treatment.Parameter = stagingTreatment.Parameter;
  379. newTreatments.Add(treatment);
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }
  386. provider.Save(setouts);
  387. foreach(var setout in setouts)
  388. {
  389. if(oldMap.TryGetValue(setout, out var stagingSetout))
  390. {
  391. stagingSetout.Setout.ID = setout.ID;
  392. }
  393. if(setoutDocuments.TryGetValue(setout, out var documents))
  394. {
  395. foreach(var document in documents)
  396. {
  397. document.EntityLink.ID = setout.ID;
  398. }
  399. }
  400. if(setoutForms.TryGetValue(setout, out var forms))
  401. {
  402. foreach(var form in forms)
  403. {
  404. form.Parent.ID = setout.ID;
  405. }
  406. }
  407. if(components.TryGetValue(setout, out var setoutComponents))
  408. {
  409. foreach(var component in setoutComponents)
  410. {
  411. component.Setout.ID = setout.ID;
  412. }
  413. }
  414. if(packets.TryGetValue(setout, out var setoutPackets))
  415. {
  416. var setoutPacketComponents = packetComponents.GetValueOrDefault(setout);
  417. foreach(var packet in setoutPackets)
  418. {
  419. packet.SetoutLink.ID = setout.ID;
  420. }
  421. }
  422. }
  423. provider.Save(stagingSetouts);
  424. provider.Save(setoutDocuments.SelectMany(x => x.Value).Where(x => x.EntityLink.ID != Guid.Empty));
  425. provider.Save(setoutForms.SelectMany(x => x.Value).Where(x => x.Parent.ID != Guid.Empty));
  426. provider.Save(packets.SelectMany(x => x.Value).Where(x => x.SetoutLink.ID != Guid.Empty));
  427. foreach(var (setout, setoutPackets) in packets)
  428. {
  429. var setoutComponents = packetComponents.GetValueOrDefault(setout);
  430. foreach(var packet in setoutPackets)
  431. {
  432. if (packet.SetoutLink.ID == Guid.Empty) continue;
  433. if(setoutComponents is not null
  434. && setoutComponents.TryGetValue(packet, out var manufacturingPacketComponents))
  435. {
  436. foreach(var component in manufacturingPacketComponents)
  437. {
  438. component.Setout.ID = setout.ID;
  439. component.Packet.ID = packet.ID;
  440. }
  441. }
  442. if(packetStages.TryGetValue(packet, out var stages))
  443. {
  444. foreach(var stage in stages)
  445. {
  446. stage.Parent.ID = packet.ID;
  447. }
  448. }
  449. if(packetTreatments.TryGetValue(packet, out var treatments))
  450. {
  451. foreach(var treatment in treatments)
  452. {
  453. treatment.Packet.ID = packet.ID;
  454. }
  455. }
  456. }
  457. }
  458. provider.Save(components.SelectMany(x => x.Value).Where(x => x.Setout.ID != Guid.Empty)
  459. .Concat(packetComponents.SelectMany(x => x.Value).SelectMany(x => x.Value).Where(x => x.Packet.ID != Guid.Empty && x.Setout.ID != Guid.Empty)));
  460. provider.Save(packetStages.SelectMany(x => x.Value).Where(x => x.Parent.ID != Guid.Empty));
  461. provider.Save(packetTreatments.SelectMany(x => x.Value).Where(x => x.Packet.ID != Guid.Empty));
  462. }
  463. }