StagingPanel.xaml.cs 19 KB

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