StagingPanel.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. namespace PRSDesktop
  13. {
  14. public class StagingPanellSettings : BaseObject, IGlobalConfigurationSettings
  15. {
  16. [Caption("PDF Markup Program Pathway", IncludePath = false)]
  17. [FileNameEditor]
  18. public string MarkupPathway { get; set; }
  19. [FolderEditor(Environment.SpecialFolder.CommonDocuments)]
  20. public string SetoutsFolder { get; set; }
  21. public StagingPanellSettings()
  22. {
  23. MarkupPathway = "";
  24. SetoutsFolder = "";
  25. }
  26. }
  27. /// <summary>
  28. /// Interaction logic for StagingPanel.xaml
  29. /// </summary>
  30. public partial class StagingPanel : UserControl, IPanel<StagingSetout>
  31. {
  32. private StagingPanellSettings _settings = new StagingPanellSettings();
  33. StagingSetout _item = new StagingSetout();
  34. public StagingPanel()
  35. {
  36. InitializeComponent();
  37. SectionName = nameof(StagingPanel);
  38. }
  39. private void DocumentPreviewer_OnMarkupComplete(IEntityDocument document)
  40. {
  41. stagingSetoutGrid.ReloadFile(document);
  42. stagingSetoutGrid.Refresh(false, true);
  43. }
  44. private void DocumentPreviewer_OnApproved(IEntityDocument stagingsetoutdocument)
  45. {
  46. var table = new Client<Setout>().Query(new Filter<Setout>(x => x.Number).IsEqualTo(_item.Number),
  47. new Columns<Setout>(x => x.ID));
  48. var setout = new Setout();
  49. //setout already exists - create new setoutdoc and supercede old ones
  50. if (table.Rows.Any())
  51. {
  52. if (MessageBox.Show("Supercede existing documents?", "Proceed?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  53. return;
  54. setout = table.Rows.FirstOrDefault().ToObject<Setout>();
  55. _item.Setout.ID = setout.ID;
  56. var setoutdoc = new SetoutDocument();
  57. setoutdoc.EntityLink.ID = setout.ID;
  58. setoutdoc.DocumentLink.ID = stagingsetoutdocument.DocumentLink.ID;
  59. List<SetoutDocument> setoutdocs = new List<SetoutDocument>
  60. {
  61. setoutdoc
  62. };
  63. CoreTable oldDocsTable = new Client<SetoutDocument>().Query(new Filter<SetoutDocument>(x => x.EntityLink.ID).IsEqualTo(setout.ID));
  64. foreach (var row in oldDocsTable.Rows)
  65. {
  66. var oldDoc = row.ToObject<SetoutDocument>();
  67. if (oldDoc.Superceded == DateTime.MinValue)
  68. {
  69. oldDoc.Superceded = DateTime.Now;
  70. setoutdocs.Add(oldDoc);
  71. }
  72. }
  73. new Client<SetoutDocument>().Save(setoutdocs, "Updated from Staging Screen");
  74. MessageBox.Show("Document for setout " + _item.Number + " superceded.", "Success");
  75. DeleteAndRefresh(stagingsetoutdocument);
  76. }
  77. //no setout for this pdf - create new
  78. else
  79. {
  80. if (manufacturingControl.Data.Rows.Count == 0)
  81. if (MessageBox.Show("No Packets created for this setout. Continue?", "Proceed?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  82. return;
  83. setout.Number = _item.Number;
  84. setout.JobLink.ID = _item.JobLink.ID;
  85. var editor = new DynamicDataGrid<Setout>();
  86. editor.OnAfterSave += (editor, items) =>
  87. {
  88. _item.Setout.ID = setout.ID;
  89. var setoutdoc = new SetoutDocument();
  90. setoutdoc.EntityLink.ID = setout.ID;
  91. setoutdoc.DocumentLink.ID = stagingsetoutdocument.DocumentLink.ID;
  92. new Client<SetoutDocument>().Save(setoutdoc, "Added from staging screen");
  93. MessageBox.Show("Setout created for document " + _item.Number, "Success");
  94. DeleteAndRefresh(stagingsetoutdocument);
  95. };
  96. if (!editor.EditItems(new[] { setout }))
  97. {
  98. MessageBox.Show("Setout Creation Cancelled");
  99. return;
  100. }
  101. }
  102. CreatePackets(setout);
  103. }
  104. private void CreatePackets(Setout setout)
  105. {
  106. List<ManufacturingPacket> packets = new List<ManufacturingPacket>();
  107. List<StagingManufacturingPacket> stagingPackets = new List<StagingManufacturingPacket>();
  108. //create new manufacturing packets from the staging packets
  109. foreach (var row in manufacturingControl.Data.Rows)
  110. {
  111. var staging = row.ToObject<StagingManufacturingPacket>();
  112. if (staging.ManufacturingPacket.ID != Guid.Empty)
  113. continue;
  114. var packet = new ManufacturingPacket();
  115. packet.SetoutLink.ID = setout.ID;
  116. packet.Serial = staging.Serial;
  117. packet.ITP.ID = staging.ITP.ID;
  118. packet.JobLink.ID = staging.Job.ID;
  119. packet.ManufacturingTemplateLink.ID = staging.Template.ID;
  120. packet.Title = staging.Title;
  121. packet.Quantity = staging.Quantity;
  122. packet.BarcodeQty = staging.BarcodeQuantity != 0 ? staging.BarcodeQuantity : packet.Quantity;
  123. packet.WaterMark = staging.Watermark.ToString();
  124. packet.Location = staging.Location;
  125. packets.Add(packet);
  126. stagingPackets.Add(staging);
  127. }
  128. //save the newly created packets to provide an ID
  129. new Client<ManufacturingPacket>().Save(packets, "Created from Design Management Panel");
  130. //now work on their stages with the provided packet.ID
  131. List<ManufacturingPacketStage> stages = new List<ManufacturingPacketStage>();
  132. Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent> stagingToActualComponents = new Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent>();
  133. //Handled in this loop:
  134. // - staging packets (update packet.ID)
  135. // - creation of template stages
  136. // - creation of components from staging components
  137. foreach (var stagingPacket in stagingPackets)
  138. {
  139. var packet = packets.FirstOrDefault(x => x.Serial == stagingPacket.Serial);
  140. stagingPacket.ManufacturingPacket.ID = packet.ID;
  141. stages.AddRange(CreateStagesForTemplate(packet.ManufacturingTemplateLink.ID, packet.ID));
  142. CreateComponents(stagingPacket.ID, packet.ID, stagingToActualComponents);
  143. }
  144. //save everything
  145. MultiSave save = new MultiSave();
  146. save.Add(typeof(ManufacturingPacketStage), stages.ToArray());
  147. save.Add(typeof(StagingManufacturingPacket), stagingPackets.ToArray());
  148. save.Add(typeof(ManufacturingPacketComponent), stagingToActualComponents.Values.ToArray());
  149. save.Save(null, "Updated from setout staging screen");
  150. foreach (var pair in stagingToActualComponents)
  151. {
  152. var stagingComponent = pair.Key;
  153. stagingComponent.ComponentID = pair.Value.ID;
  154. }
  155. new Client<StagingManufacturingPacketComponent>().Save(stagingToActualComponents.Keys.ToArray(), "Updated from setout staging screen");
  156. }
  157. private void CreateComponents(Guid stagingPacketID, Guid packetID, Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent> stagingToActualComponents)
  158. {
  159. var components = new List<ManufacturingPacketComponent>();
  160. CoreTable table = new Client<StagingManufacturingPacketComponent>().Query(
  161. new Filter<StagingManufacturingPacketComponent>(x => x.StagingPacket.ID).IsEqualTo(stagingPacketID)
  162. .And(x => x.ComponentID).IsEqualTo(Guid.Empty),
  163. new Columns<StagingManufacturingPacketComponent>(
  164. x => x.Product.ID,
  165. x => x.Quantity,
  166. x => x.Length,
  167. x => x.Height,
  168. x => x.Width
  169. ));
  170. foreach (var row in table.Rows)
  171. {
  172. var stagingComponent = row.ToObject<StagingManufacturingPacketComponent>();
  173. var component = stagingComponent.CreateComponent(packetID);
  174. components.Add(component);
  175. stagingToActualComponents.Add(stagingComponent, component);
  176. }
  177. }
  178. private List<ManufacturingPacketStage> CreateStagesForTemplate(Guid templateID, Guid packetID)
  179. {
  180. var stages = new List<ManufacturingPacketStage>();
  181. CoreTable table = new Client<ManufacturingTemplateStage>().Query(
  182. new Filter<ManufacturingTemplateStage>(x => x.Template.ID).IsEqualTo(templateID),
  183. new Columns<ManufacturingTemplateStage>
  184. (
  185. x => x.Time,
  186. x => x.Sequence,
  187. x => x.SequenceType,
  188. x => x.Section.ID,
  189. x => x.Section.Name
  190. ));
  191. foreach (var row in table.Rows)
  192. {
  193. var templateStage = row.ToObject<ManufacturingTemplateStage>();
  194. var packetStage = templateStage.CreateManufacturingPacketStage();
  195. packetStage.ManufacturingPacketLink.ID = packetID;
  196. stages.Add(packetStage);
  197. }
  198. return stages;
  199. }
  200. private void DeleteAndRefresh(IEntityDocument stagingsetoutdocument)
  201. {
  202. new Client<StagingSetout>().Save(_item, "Updated from staging screen");
  203. _item = new StagingSetout();
  204. stagingSetoutGrid.DeleteFile(stagingsetoutdocument);
  205. documentPreviewer.Document = new StagingSetoutDocument();
  206. Refresh();
  207. }
  208. private void DocumentPreviewer_OnRejected(IEntityDocument stagingsetoutdocument)
  209. {
  210. //dont create setout - setout.id remains blank
  211. //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
  212. //attach the document to the task for reference
  213. Kanban task = new Kanban();
  214. task.Title = "Setout Review Task (setout rejected)";
  215. task.Description = "Please review the attached document for setout " + _item.Number;
  216. task.ManagerLink.ID = App.EmployeeID;
  217. var page = new KanbanGrid();
  218. page.MyID = App.EmployeeID;
  219. page.OnAfterSave += (editor, items) =>
  220. {
  221. Kanban savedTask = (Kanban)items[0];
  222. KanbanDocument doc = new KanbanDocument();
  223. doc.EntityLink.ID = savedTask.ID;
  224. doc.DocumentLink.ID = stagingsetoutdocument.DocumentLink.ID;
  225. new Client<KanbanDocument>().Save(doc, "Created from staging screen");
  226. _item.Task.ID = savedTask.ID;
  227. new Client<StagingSetout>().Save(_item, "Updated from staging screen");
  228. MessageBox.Show("Success - Task Created for Document " + _item.Number + " (Task no. " + savedTask.Number + " assigned to " + savedTask.EmployeeLink.Name + ")");
  229. _item = new StagingSetout();
  230. stagingSetoutGrid.DeleteFile(stagingsetoutdocument);
  231. documentPreviewer.Document = new StagingSetoutDocument();
  232. stagingSetoutGrid.Refresh(false, true);
  233. };
  234. if (!page.EditItems(new[] { task }))
  235. MessageBox.Show("Task creation cancelled - setout not rejected");
  236. }
  237. private void StagingSetoutGrid_OnSelectItem(object sender, InABox.DynamicGrid.DynamicGridSelectionEventArgs e)
  238. {
  239. _item = stagingSetoutGrid.SelectedRows.FirstOrDefault().ToObject<StagingSetout>();
  240. var doc = new Client<StagingSetoutDocument>().Query(
  241. new Filter<StagingSetoutDocument>(x => x.EntityLink.ID)
  242. .IsEqualTo(_item.ID),
  243. new Columns<StagingSetoutDocument>(x => x.ID, x => x.DocumentLink.ID, x => x.DocumentLink.FileName))
  244. .Rows.FirstOrDefault().ToObject<StagingSetoutDocument>();
  245. documentPreviewer.Document = doc;
  246. manufacturingControl.StagingSetout = _item;
  247. }
  248. public bool IsReady { get; set; }
  249. public string SectionName { get; }
  250. public event DataModelUpdateEvent? OnUpdateDataModel;
  251. public void CreateToolbarButtons(IPanelHost host)
  252. {
  253. host.CreateSetupAction(new PanelAction() { Caption = "Setouts Configuration", Image = PRSDesktop.Resources.specifications, OnExecute = ConfigSettingsClick });
  254. }
  255. private void ConfigSettingsClick(PanelAction obj)
  256. {
  257. //var editor = new ObjectEditor(_settings, "Setouts Configuration");
  258. //if (editor.ShowDialog() == true)
  259. //{
  260. // _settings = (StagingPanellSettings)editor.Input;
  261. // new GlobalConfiguration<StagingPanellSettings>().Save(_settings);
  262. //}
  263. var pages = new DynamicEditorPages();
  264. var propertyEditor = new DynamicEditorForm(typeof(StagingPanellSettings), pages);
  265. propertyEditor.OnDefineLookups += sender =>
  266. {
  267. var editor = sender.EditorDefinition as ILookupEditor;
  268. var colname = sender.ColumnName;
  269. var values = editor.Values(colname, new[] { _settings });
  270. sender.LoadLookups(values);
  271. };
  272. propertyEditor.OnEditorValueChanged += (sender, name, value) =>
  273. {
  274. CoreUtils.SetPropertyValue(_settings, name, value);
  275. return new Dictionary<string, object?>();
  276. };
  277. propertyEditor.OnFormCustomiseEditor += PropertyEditor_OnFormCustomiseEditor;
  278. propertyEditor.Items = new BaseObject[] { _settings };
  279. if (propertyEditor.ShowDialog() == true)
  280. {
  281. new GlobalConfiguration<StagingPanellSettings>().Save(_settings);
  282. }
  283. }
  284. private void PropertyEditor_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
  285. {
  286. }
  287. public void Heartbeat(TimeSpan time)
  288. {
  289. }
  290. public void Refresh()
  291. {
  292. stagingSetoutGrid.ScanFiles(_settings.SetoutsFolder);
  293. stagingSetoutGrid.Refresh(false, true);
  294. manufacturingControl.StagingSetout = new StagingSetout();
  295. manufacturingControl.Refresh();
  296. }
  297. public Dictionary<string, object[]> Selected()
  298. {
  299. return new();
  300. }
  301. public void Setup()
  302. {
  303. _settings = new GlobalConfiguration<StagingPanellSettings>().Load();
  304. documentPreviewer.SetupButtons(
  305. markupVisible: Security.IsAllowed<CanMarkUpSetouts>(),
  306. rejectVisible: Security.IsAllowed<CanApproveSetouts>(),
  307. approveVisible: Security.IsAllowed<CanApproveSetouts>()
  308. );
  309. documentPreviewer.OnMarkupSelected += DocumentPreviewer_OnMarkupSelected;
  310. documentPreviewer.OnMarkupComplete += DocumentPreviewer_OnMarkupComplete;
  311. documentPreviewer.OnRejected += DocumentPreviewer_OnRejected;
  312. documentPreviewer.OnApproved += DocumentPreviewer_OnApproved;
  313. stagingSetoutGrid.ScanFiles(_settings.SetoutsFolder);
  314. stagingSetoutGrid.Refresh(true, true);
  315. stagingSetoutGrid.OnSelectItem += StagingSetoutGrid_OnSelectItem;
  316. }
  317. private void DocumentPreviewer_OnMarkupSelected(IEntityDocument document)
  318. {
  319. using (Process p = new Process())
  320. {
  321. p.StartInfo = new ProcessStartInfo()
  322. {
  323. UseShellExecute = true,
  324. FileName = _settings.SetoutsFolder + @"\" + document.DocumentLink.FileName
  325. };
  326. p.Start();
  327. }
  328. }
  329. public void Shutdown()
  330. {
  331. }
  332. public DataModel DataModel(Selection selection)
  333. {
  334. return new AutoDataModel<StagingSetout>(null);
  335. }
  336. }
  337. }