StagingPanel.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using InABox.Clients;
  9. using InABox.Configuration;
  10. using InABox.DynamicGrid;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using InABox.WPF;
  14. namespace PRSDesktop
  15. {
  16. public class StagingPanellSettings : BaseObject, IGlobalConfigurationSettings
  17. {
  18. [Caption("PDF Markup Program Pathway", IncludePath = false)]
  19. [FileNameEditor]
  20. public string MarkupPathway { get; set; }
  21. [FolderEditor(Environment.SpecialFolder.CommonDocuments)]
  22. public string SetoutsFolder { get; set; }
  23. public StagingPanellSettings()
  24. {
  25. MarkupPathway = "";
  26. SetoutsFolder = "";
  27. }
  28. }
  29. /// <summary>
  30. /// Interaction logic for StagingPanel.xaml
  31. /// </summary>
  32. public partial class StagingPanel : UserControl, IPanel<StagingSetout>
  33. {
  34. private StagingPanellSettings _settings = new StagingPanellSettings();
  35. StagingSetout _item = new StagingSetout();
  36. List<StagingSetout> _items = new List<StagingSetout>();
  37. DynamicDataGrid<Document> _materialDocs;
  38. public StagingPanel()
  39. {
  40. InitializeComponent();
  41. SectionName = nameof(StagingPanel);
  42. }
  43. private void DocumentPreviewer_OnApproved(IEntityDocument stagingsetoutdocument)
  44. {
  45. bool bulkApprove = false;
  46. if (_items.Count > 1)
  47. {
  48. if (MessageBox.Show("Bulk approve? (Skip individual setout approval)", "Continue?", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  49. {
  50. bulkApprove = true;
  51. Progress.Show("Approving Setouts..");
  52. }
  53. }
  54. string message = "Result: " + Environment.NewLine;
  55. foreach (var item in _items)
  56. {
  57. if (bulkApprove)
  58. Progress.Show("Working on " + item.Number);
  59. var returnstring = ApproveSetout(item, bulkApprove);
  60. if (!string.IsNullOrWhiteSpace(returnstring))
  61. message = message + returnstring + Environment.NewLine;
  62. }
  63. if(bulkApprove)
  64. Progress.Close();
  65. foreach (var item in _items)
  66. DeleteAndRefresh(item);
  67. MessageBox.Show(message);
  68. }
  69. private string ApproveSetout(StagingSetout item, bool bulkapprove)
  70. {
  71. if (item.Group.ID == Guid.Empty)
  72. {
  73. var message = "Setout has no group assigned";
  74. if (Security.IsAllowed<CanApproveSetoutsWithoutGroup>())
  75. {
  76. if (MessageBox.Show(message + ", continue?", "Continue?", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
  77. return "";
  78. }
  79. else
  80. {
  81. MessageBox.Show(message + ", please assign a group!");
  82. return "";
  83. }
  84. }
  85. var setoutdoctable = new Client<StagingSetoutDocument>().Query(
  86. new Filter<StagingSetoutDocument>(x => x.EntityLink.ID).IsEqualTo(item.ID),
  87. new Columns<StagingSetoutDocument>(x => x.ID, x => x.DocumentLink.ID, x => x.DocumentLink.FileName));
  88. if (!setoutdoctable.Rows.Any())
  89. return "";
  90. var stagingsetoutdocument = setoutdoctable.Rows.FirstOrDefault().ToObject<StagingSetoutDocument>();
  91. var table = new Client<Setout>().Query(new Filter<Setout>(x => x.Number).IsEqualTo(item.Number),
  92. new Columns<Setout>(x => x.ID));
  93. var setout = new Setout();
  94. //setout already exists - create new setoutdoc and supercede old ones
  95. if (table.Rows.Any())
  96. {
  97. if (!bulkapprove)
  98. if (MessageBox.Show("Supercede existing documents?", "Proceed?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  99. return "";
  100. setout = table.Rows.FirstOrDefault().ToObject<Setout>();
  101. setout.Group.ID = item.Group.ID;
  102. item.Setout.ID = setout.ID;
  103. var setoutdoc = new SetoutDocument();
  104. setoutdoc.EntityLink.ID = setout.ID;
  105. setoutdoc.DocumentLink.ID = stagingsetoutdocument.DocumentLink.ID;
  106. List<SetoutDocument> setoutdocs = new List<SetoutDocument>
  107. {
  108. setoutdoc
  109. };
  110. CoreTable oldDocsTable = new Client<SetoutDocument>().Query(
  111. new Filter<SetoutDocument>(x => x.EntityLink.ID).IsEqualTo(setout.ID)
  112. .And(x => x.DocumentLink.ID).IsNotEqualTo(item.Group.OptimizationDocument.ID)
  113. );
  114. foreach (var row in oldDocsTable.Rows)
  115. {
  116. var oldDoc = row.ToObject<SetoutDocument>();
  117. if (oldDoc.Superceded == DateTime.MinValue)
  118. {
  119. oldDoc.Superceded = DateTime.Now;
  120. setoutdocs.Add(oldDoc);
  121. }
  122. }
  123. new Client<SetoutDocument>().Save(setoutdocs, "Updated from Staging Screen");
  124. new Client<Setout>().Save(setout, "Updated from Staging Screen");
  125. return item.Number + " Superceded";
  126. }
  127. //no setout for this pdf - create new
  128. else
  129. {
  130. setout.Number = item.Number;
  131. setout.JobLink.ID = item.JobLink.ID;
  132. setout.Group.ID = item.Group.ID;
  133. var editor = new DynamicDataGrid<Setout>();
  134. editor.OnAfterSave += (editor, items) =>
  135. {
  136. CreateSetoutDocument(setout, item, stagingsetoutdocument);
  137. };
  138. if (!bulkapprove)
  139. {
  140. if (!editor.EditItems(new[] { setout }))
  141. {
  142. MessageBox.Show("Setout Creation Cancelled");
  143. return "";
  144. }
  145. else
  146. return item.Number + " Created";
  147. }
  148. else
  149. {
  150. new Client<Setout>().Save(setout, "Added from staging screen");
  151. CreateSetoutDocument(setout, item, stagingsetoutdocument);
  152. return item.Number + " Created";
  153. }
  154. }
  155. //currently not creating packets due to temporary change in requirements - to uncomment and check for validity when required
  156. //CreatePackets(setout);
  157. }
  158. private void CreateSetoutDocument(Setout setout, StagingSetout item, StagingSetoutDocument stagingsetoutdocument)
  159. {
  160. item.Setout.ID = setout.ID;
  161. var setoutdoc = new SetoutDocument();
  162. setoutdoc.EntityLink.ID = setout.ID;
  163. setoutdoc.DocumentLink.ID = stagingsetoutdocument.DocumentLink.ID;
  164. new Client<SetoutDocument>().Save(setoutdoc, "Added from staging screen");
  165. }
  166. private void CreatePackets(Setout setout)
  167. {
  168. List<ManufacturingPacket> packets = new List<ManufacturingPacket>();
  169. List<StagingManufacturingPacket> stagingPackets = new List<StagingManufacturingPacket>();
  170. //create new manufacturing packets from the staging packets
  171. foreach (var row in manufacturingControl.Data.Rows)
  172. {
  173. var staging = row.ToObject<StagingManufacturingPacket>();
  174. if (staging.ManufacturingPacket.ID != Guid.Empty)
  175. continue;
  176. var packet = new ManufacturingPacket();
  177. packet.SetoutLink.ID = setout.ID;
  178. packet.Serial = staging.Serial;
  179. packet.ITP.ID = staging.ITP.ID;
  180. packet.JobLink.ID = staging.Job.ID;
  181. packet.ManufacturingTemplateLink.ID = staging.Template.ID;
  182. packet.Title = staging.Title;
  183. packet.Quantity = staging.Quantity;
  184. packet.BarcodeQty = staging.BarcodeQuantity != 0 ? staging.BarcodeQuantity : packet.Quantity;
  185. packet.WaterMark = staging.Watermark.ToString();
  186. packet.Location = staging.Location;
  187. packets.Add(packet);
  188. stagingPackets.Add(staging);
  189. }
  190. //save the newly created packets to provide an ID
  191. new Client<ManufacturingPacket>().Save(packets, "Created from Design Management Panel");
  192. //now work on their stages with the provided packet.ID
  193. List<ManufacturingPacketStage> stages = new List<ManufacturingPacketStage>();
  194. Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent> stagingToActualComponents = new Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent>();
  195. //Handled in this loop:
  196. // - staging packets (update packet.ID)
  197. // - creation of template stages
  198. // - creation of components from staging components
  199. foreach (var stagingPacket in stagingPackets)
  200. {
  201. var packet = packets.FirstOrDefault(x => x.Serial == stagingPacket.Serial);
  202. stagingPacket.ManufacturingPacket.ID = packet.ID;
  203. stages.AddRange(CreateStagesForTemplate(packet.ManufacturingTemplateLink.ID, packet.ID));
  204. CreateComponents(stagingPacket.ID, packet.ID, stagingToActualComponents);
  205. }
  206. //save everything
  207. MultiSave save = new MultiSave();
  208. save.Add(typeof(ManufacturingPacketStage), stages.ToArray());
  209. save.Add(typeof(StagingManufacturingPacket), stagingPackets.ToArray());
  210. save.Add(typeof(ManufacturingPacketComponent), stagingToActualComponents.Values.ToArray());
  211. save.Save(null, "Updated from setout staging screen");
  212. foreach (var pair in stagingToActualComponents)
  213. {
  214. var stagingComponent = pair.Key;
  215. stagingComponent.ComponentID = pair.Value.ID;
  216. }
  217. new Client<StagingManufacturingPacketComponent>().Save(stagingToActualComponents.Keys.ToArray(), "Updated from setout staging screen");
  218. }
  219. private void CreateComponents(Guid stagingPacketID, Guid packetID, Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent> stagingToActualComponents)
  220. {
  221. var components = new List<ManufacturingPacketComponent>();
  222. CoreTable table = new Client<StagingManufacturingPacketComponent>().Query(
  223. new Filter<StagingManufacturingPacketComponent>(x => x.StagingPacket.ID).IsEqualTo(stagingPacketID)
  224. .And(x => x.ComponentID).IsEqualTo(Guid.Empty),
  225. new Columns<StagingManufacturingPacketComponent>(
  226. x => x.Product.ID,
  227. x => x.Quantity,
  228. x => x.Length,
  229. x => x.Height,
  230. x => x.Width
  231. ));
  232. foreach (var row in table.Rows)
  233. {
  234. var stagingComponent = row.ToObject<StagingManufacturingPacketComponent>();
  235. var component = stagingComponent.CreateComponent(packetID);
  236. components.Add(component);
  237. stagingToActualComponents.Add(stagingComponent, component);
  238. }
  239. }
  240. private List<ManufacturingPacketStage> CreateStagesForTemplate(Guid templateID, Guid packetID)
  241. {
  242. var stages = new List<ManufacturingPacketStage>();
  243. CoreTable table = new Client<ManufacturingTemplateStage>().Query(
  244. new Filter<ManufacturingTemplateStage>(x => x.Template.ID).IsEqualTo(templateID),
  245. new Columns<ManufacturingTemplateStage>
  246. (
  247. x => x.Time,
  248. x => x.Sequence,
  249. x => x.SequenceType,
  250. x => x.Section.ID,
  251. x => x.Section.Name
  252. ));
  253. foreach (var row in table.Rows)
  254. {
  255. var templateStage = row.ToObject<ManufacturingTemplateStage>();
  256. var packetStage = templateStage.CreateManufacturingPacketStage();
  257. packetStage.ManufacturingPacketLink.ID = packetID;
  258. stages.Add(packetStage);
  259. }
  260. return stages;
  261. }
  262. private void DeleteAndRefresh(StagingSetout item)
  263. {
  264. new Client<StagingSetout>().Save(item, "Updated from staging screen");
  265. _item = new StagingSetout();
  266. documentPreviewer.Document = new StagingSetoutDocument();
  267. Refresh();
  268. }
  269. private void DocumentPreviewer_OnRejected(IEntityDocument stagingsetoutdocument)
  270. {
  271. //dont create setout - setout.id remains blank
  272. //create kanban and populate task.id - this prevents it from appearing on the stagingsetout grid, and allows a new staging setout to be created when the file is saved to the folder again
  273. //attach the document to the task for reference
  274. Kanban task = new Kanban();
  275. task.Title = "Setout Review Task (setout rejected)";
  276. task.Description = "Please review the attached document for setout " + _item.Number;
  277. task.ManagerLink.ID = App.EmployeeID;
  278. var page = new KanbanGrid();
  279. page.MyID = App.EmployeeID;
  280. page.OnAfterSave += (editor, items) =>
  281. {
  282. Kanban savedTask = (Kanban)items[0];
  283. KanbanDocument doc = new KanbanDocument();
  284. doc.EntityLink.ID = savedTask.ID;
  285. doc.DocumentLink.ID = stagingsetoutdocument.DocumentLink.ID;
  286. new Client<KanbanDocument>().Save(doc, "Created from staging screen");
  287. _item.Task.ID = savedTask.ID;
  288. new Client<StagingSetout>().Save(_item, "Updated from staging screen");
  289. MessageBox.Show("Success - Task Created for Document " + _item.Number + " (Task no. " + savedTask.Number + " assigned to " + savedTask.EmployeeLink.Name + ")");
  290. _item = new StagingSetout();
  291. documentPreviewer.Document = new StagingSetoutDocument();
  292. stagingSetoutGrid.Refresh(false, true);
  293. };
  294. if (!page.EditItems(new[] { task }))
  295. MessageBox.Show("Task creation cancelled - setout not rejected");
  296. }
  297. private void StagingSetoutGrid_OnSelectItem(object sender, InABox.DynamicGrid.DynamicGridSelectionEventArgs e)
  298. {
  299. _items.Clear();
  300. foreach (var row in stagingSetoutGrid.SelectedRows)
  301. _items.Add(row.ToObject<StagingSetout>());
  302. _item = stagingSetoutGrid.SelectedRows.FirstOrDefault().ToObject<StagingSetout>();
  303. var doc = new Client<StagingSetoutDocument>().Query(
  304. new Filter<StagingSetoutDocument>(x => x.EntityLink.ID)
  305. .IsEqualTo(_item.ID),
  306. new Columns<StagingSetoutDocument>(x => x.ID, x => x.DocumentLink.ID, x => x.DocumentLink.FileName))
  307. .Rows.FirstOrDefault().ToObject<StagingSetoutDocument>();
  308. documentPreviewer.Document = doc;
  309. manufacturingControl.StagingSetout = _item;
  310. documentPreviewer.Mode =
  311. _item.Locked == DateTime.MinValue ? InABox.Wpf.DocumentApprovalControl.ControlMode.Markup :
  312. _item.Locked != DateTime.MinValue && _item.LockedBy.ID == App.EmployeeID ? InABox.Wpf.DocumentApprovalControl.ControlMode.Complete :
  313. _item.Locked != DateTime.MinValue && _item.LockedBy.ID != App.EmployeeID ? InABox.Wpf.DocumentApprovalControl.ControlMode.Locked :
  314. InABox.Wpf.DocumentApprovalControl.ControlMode.Markup;
  315. }
  316. public bool IsReady { get; set; }
  317. public string SectionName { get; }
  318. public event DataModelUpdateEvent? OnUpdateDataModel;
  319. public void CreateToolbarButtons(IPanelHost host)
  320. {
  321. host.CreateSetupAction(new PanelAction() { Caption = "Setouts Configuration", Image = PRSDesktop.Resources.specifications, OnExecute = ConfigSettingsClick });
  322. }
  323. private void ConfigSettingsClick(PanelAction obj)
  324. {
  325. var pages = new DynamicEditorPages();
  326. var propertyEditor = new DynamicEditorForm(typeof(StagingPanellSettings), pages);
  327. propertyEditor.OnDefineLookups += sender =>
  328. {
  329. var editor = sender.EditorDefinition as ILookupEditor;
  330. var colname = sender.ColumnName;
  331. var values = editor.Values(colname, new[] { _settings });
  332. sender.LoadLookups(values);
  333. };
  334. propertyEditor.OnEditorValueChanged += (sender, name, value) =>
  335. {
  336. CoreUtils.SetPropertyValue(_settings, name, value);
  337. return new Dictionary<string, object?>();
  338. };
  339. propertyEditor.OnFormCustomiseEditor += PropertyEditor_OnFormCustomiseEditor;
  340. propertyEditor.Items = new BaseObject[] { _settings };
  341. if (propertyEditor.ShowDialog() == true)
  342. {
  343. new GlobalConfiguration<StagingPanellSettings>().Save(_settings);
  344. }
  345. }
  346. private void PropertyEditor_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
  347. {
  348. }
  349. public void Heartbeat(TimeSpan time)
  350. {
  351. }
  352. public void Refresh()
  353. {
  354. //stagingSetoutGrid.ScanFiles(_settings.SetoutsFolder);
  355. stagingSetoutGrid.Refresh(false, true);
  356. documentPreviewer.Document = new StagingSetoutDocument();
  357. manufacturingControl.StagingSetout = new StagingSetout();
  358. manufacturingControl.Refresh();
  359. }
  360. public Dictionary<string, object[]> Selected()
  361. {
  362. return new();
  363. }
  364. public void Setup()
  365. {
  366. _settings = new GlobalConfiguration<StagingPanellSettings>().Load();
  367. documentPreviewer.SetupButtons(
  368. markupVisible: Security.IsAllowed<CanMarkUpSetouts>(),
  369. rejectVisible: Security.IsAllowed<CanApproveSetouts>(),
  370. approveVisible: Security.IsAllowed<CanApproveSetouts>()
  371. );
  372. documentPreviewer.OnMarkupSelected += DocumentPreviewer_OnMarkupSelected;
  373. documentPreviewer.OnMarkupComplete += DocumentPreviewer_OnMarkupComplete;
  374. documentPreviewer.OnRejected += DocumentPreviewer_OnRejected;
  375. documentPreviewer.OnApproved += DocumentPreviewer_OnApproved;
  376. //stagingSetoutGrid.ScanFiles(_settings.SetoutsFolder);
  377. stagingSetoutGrid.Refresh(true, true);
  378. stagingSetoutGrid.OnSelectItem += StagingSetoutGrid_OnSelectItem;
  379. }
  380. private void DocumentPreviewer_OnMarkupSelected(IEntityDocument document)
  381. {
  382. var doc = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(document.DocumentLink.ID)).Rows.FirstOrDefault().ToObject<Document>();
  383. var tempdocpath = Path.GetTempPath() + @"\" + doc.FileName;
  384. _item.SavePath = tempdocpath;
  385. _item.LockedBy.ID = App.EmployeeID;
  386. new Client<StagingSetout>().Save(_item, "Locked from Staging Screen");
  387. File.WriteAllBytes(tempdocpath, doc.Data);
  388. using (Process p = new Process())
  389. {
  390. p.StartInfo = new ProcessStartInfo()
  391. {
  392. UseShellExecute = true,
  393. FileName = tempdocpath
  394. };
  395. p.Start();
  396. }
  397. stagingSetoutGrid.Refresh(false, true);
  398. }
  399. private void DocumentPreviewer_OnMarkupComplete(IEntityDocument document)
  400. {
  401. stagingSetoutGrid.ReloadFile(_item);
  402. stagingSetoutGrid.Refresh(false, true);
  403. }
  404. public void Shutdown()
  405. {
  406. }
  407. public DataModel DataModel(Selection selection)
  408. {
  409. return new AutoDataModel<StagingSetout>(null);
  410. }
  411. }
  412. }