StagingPanel.xaml.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. using System.ComponentModel;
  15. using InABox.Scripting;
  16. using System.Reflection;
  17. using System.Collections.Immutable;
  18. using FastReport.Data;
  19. using Microsoft.Xaml.Behaviors.Core;
  20. namespace PRSDesktop
  21. {
  22. [Caption("Staging Panel Settings")]
  23. public class StagingPanellSettings : BaseObject, IGlobalConfigurationSettings
  24. {
  25. [Caption("PDF Markup Program Pathway", IncludePath = false)]
  26. [FileNameEditor]
  27. public string MarkupPathway { get; set; }
  28. [FolderEditor(Environment.SpecialFolder.CommonDocuments)]
  29. public string SetoutsFolder { get; set; }
  30. [ScriptEditor]
  31. public string? Script { get; set; }
  32. public StagingPanellSettings()
  33. {
  34. MarkupPathway = "";
  35. SetoutsFolder = "";
  36. Script = null;
  37. }
  38. public string DefaultScript()
  39. {
  40. return @"
  41. using PRSDesktop;
  42. using InABox.Core;
  43. using System.Collections.Generic;
  44. public class Module
  45. {
  46. /*public void CustomiseSetouts(CustomiseSetoutsArgs args)
  47. {
  48. // Perform customisation on the setouts when they are added to the 'Staged Documents' grid.
  49. }*/
  50. }";
  51. }
  52. }
  53. public class CustomiseSetoutsArgs
  54. {
  55. public IReadOnlyList<Tuple<StagingSetout, Document>> Setouts;
  56. public CustomiseSetoutsArgs(IReadOnlyList<Tuple<StagingSetout, Document>> setouts)
  57. {
  58. Setouts = setouts;
  59. }
  60. }
  61. /// <summary>
  62. /// Interaction logic for StagingPanel.xaml
  63. /// </summary>
  64. public partial class StagingPanel : UserControl, IPanel<StagingSetout>
  65. {
  66. private StagingPanellSettings _settings = new StagingPanellSettings();
  67. /// <summary>
  68. /// The currently selected setout.
  69. /// </summary>
  70. private StagingSetout? selectedSetout;
  71. /// <summary>
  72. /// All currently selected setouts; <see cref="selectedSetout"/> will be a member of this list.
  73. /// </summary>
  74. private List<StagingSetout> selectedSetouts = new();
  75. private CoreTable? _templateGroups = null;
  76. #region Script Stuff
  77. private MethodInfo? _customiseSetoutsMethod;
  78. private MethodInfo? CustomiseSetoutsMethod
  79. {
  80. get
  81. {
  82. EnsureScript();
  83. return _customiseSetoutsMethod;
  84. }
  85. }
  86. private object? _scriptObject;
  87. private object? ScriptObject
  88. {
  89. get
  90. {
  91. EnsureScript();
  92. return _scriptObject;
  93. }
  94. }
  95. private ScriptDocument? _script;
  96. private ScriptDocument? Script
  97. {
  98. get
  99. {
  100. EnsureScript();
  101. return _script;
  102. }
  103. }
  104. private void EnsureScript()
  105. {
  106. if (_script is null && !_settings.Script.IsNullOrWhiteSpace())
  107. {
  108. _script = new ScriptDocument(_settings.Script);
  109. if (!_script.Compile())
  110. {
  111. throw new Exception("Script in Staging Panel Settings failed to compile!");
  112. }
  113. _scriptObject = _script?.GetObject();
  114. _customiseSetoutsMethod = _script?.GetMethod(methodName: "CustomiseSetouts");
  115. }
  116. }
  117. #endregion
  118. public StagingPanel()
  119. {
  120. InitializeComponent();
  121. SectionName = nameof(StagingPanel);
  122. }
  123. public void Setup()
  124. {
  125. _settings = new GlobalConfiguration<StagingPanellSettings>().Load();
  126. _templateGroups = new Client<ManufacturingTemplateGroup>().Query();
  127. MarkUpButton.Visibility = Security.IsAllowed<CanMarkUpSetouts>() ? Visibility.Visible : Visibility.Hidden;
  128. RejectButton.Visibility = Security.IsAllowed<CanApproveSetouts>() ? Visibility.Visible : Visibility.Hidden;
  129. ApproveButton.Visibility = Security.IsAllowed<CanApproveSetouts>() ? Visibility.Visible : Visibility.Hidden;
  130. ProcessButton.Visibility = Security.IsAllowed<CanApproveSetouts>() ? Visibility.Visible : Visibility.Hidden;
  131. //stagingSetoutGrid.ScanFiles(_settings.SetoutsFolder);
  132. stagingSetoutGrid.Refresh(true, true);
  133. stagingSetoutGrid.OnSelectItem += StagingSetoutGrid_OnSelectItem;
  134. }
  135. private void NestedPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  136. {
  137. if(e.View != DynamicSplitPanelView.Master && ManufacturingPacketList.Setout != selectedSetout)
  138. {
  139. ManufacturingPacketList.Setout = selectedSetout;
  140. }
  141. }
  142. #region Document Viewer
  143. public enum DocumentMode
  144. {
  145. Markup,
  146. Complete,
  147. Locked
  148. }
  149. private DocumentMode _mode;
  150. private DocumentMode Mode
  151. {
  152. get => _mode;
  153. set
  154. {
  155. _mode = value;
  156. if (_mode == DocumentMode.Markup)
  157. {
  158. MarkUpButton.Content = "Mark Up";
  159. MarkUpButton.IsEnabled = Document != null && !Document.Approved;
  160. ProcessButton.IsEnabled = Document != null && Document.Approved;
  161. RejectButton.IsEnabled = Document != null && !Document.Approved;
  162. ApproveButton.IsEnabled = Document != null;
  163. }
  164. else if (_mode == DocumentMode.Complete)
  165. {
  166. MarkUpButton.Content = "Complete";
  167. MarkUpButton.IsEnabled = Document != null;
  168. ProcessButton.IsEnabled = false;
  169. RejectButton.IsEnabled = false;
  170. ApproveButton.IsEnabled = false;
  171. }
  172. else if (_mode == DocumentMode.Locked)
  173. {
  174. MarkUpButton.Content = "Locked";
  175. MarkUpButton.IsEnabled = false;
  176. ProcessButton.IsEnabled = false;
  177. RejectButton.IsEnabled = false;
  178. ApproveButton.IsEnabled = false;
  179. }
  180. }
  181. }
  182. private StagingSetoutDocument? _document;
  183. private StagingSetoutDocument? Document
  184. {
  185. get => _document;
  186. set
  187. {
  188. if(_document != value)
  189. {
  190. _document = value;
  191. RenderDocument(value);
  192. }
  193. }
  194. }
  195. private void RenderDocument(StagingSetoutDocument? document)
  196. {
  197. DocumentViewer.Children.Clear();
  198. if (document is null)
  199. {
  200. return;
  201. }
  202. var table = new Client<Document>().Query(
  203. new Filter<Document>(x => x.ID).IsEqualTo(document.DocumentLink.ID),
  204. new Columns<Document>(x => x.Data));
  205. var first = table.Rows.FirstOrDefault();
  206. if (first is null)
  207. return;
  208. var data = first.Get<Document, byte[]>(x => x.Data);
  209. var images = ImageUtils.RenderPDFToImages(data);
  210. foreach (var image in images)
  211. {
  212. DocumentViewer.Children.Add(new Image
  213. {
  214. Source = ImageUtils.LoadImage(image),
  215. Margin = new Thickness(0, 0, 0, 20)
  216. });
  217. }
  218. }
  219. private void ProcessButton_Click(object sender, RoutedEventArgs e)
  220. {
  221. bool bulkApprove = false;
  222. if (selectedSetouts.Count > 1)
  223. {
  224. if (MessageBox.Show("Bulk approve? (Skip individual setout approval)", "Continue?", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  225. {
  226. bulkApprove = true;
  227. Progress.Show("Approving Setouts..");
  228. }
  229. }
  230. if(selectedSetouts.Any(x => x.UnapprovedDocuments > 0))
  231. {
  232. MessageBox.Show("Cannot process setouts with unapproved documents.");
  233. Progress.Close();
  234. return;
  235. }
  236. else if(selectedSetouts.Any(x => x.Packets == 0))
  237. {
  238. if(MessageBox.Show("Warning: some setouts do not have any manufacturing packets: are you sure you wish to proceed?", "Warning", MessageBoxButton.YesNoCancel) != MessageBoxResult.Yes)
  239. {
  240. Progress.Close();
  241. return;
  242. }
  243. }
  244. string message = "Result: " + Environment.NewLine;
  245. foreach (var item in selectedSetouts)
  246. {
  247. if (bulkApprove)
  248. Progress.Show("Working on " + item.Number);
  249. var returnstring = ApproveSetout(item, bulkApprove);
  250. if (!string.IsNullOrWhiteSpace(returnstring))
  251. message = message + returnstring + Environment.NewLine;
  252. }
  253. if (bulkApprove)
  254. Progress.Close();
  255. new Client<StagingSetout>().Save(selectedSetouts, "Updated from staging screen");
  256. selectedSetout = null;
  257. Refresh();
  258. MessageBox.Show(message);
  259. MainPanel.View = DynamicSplitPanelView.Combined;
  260. NestedPanel.View = DynamicSplitPanelView.Master;
  261. }
  262. private string ApproveSetout(StagingSetout item, bool bulkapprove)
  263. {
  264. if (item.Group.ID == Guid.Empty)
  265. {
  266. var message = "Setout has no group assigned";
  267. if (Security.IsAllowed<CanApproveSetoutsWithoutGroup>())
  268. {
  269. if (MessageBox.Show(message + ", continue?", "Continue?", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
  270. return "";
  271. }
  272. else
  273. {
  274. MessageBox.Show(message + ", please assign a group!");
  275. return "";
  276. }
  277. }
  278. var setoutDocument = new Client<StagingSetoutDocument>()
  279. .Query(
  280. new Filter<StagingSetoutDocument>(x => x.EntityLink.ID).IsEqualTo(item.ID),
  281. new Columns<StagingSetoutDocument>(x => x.ID, x => x.DocumentLink.ID, x => x.DocumentLink.FileName))
  282. .ToObjects<StagingSetoutDocument>().FirstOrDefault();
  283. if (setoutDocument is null)
  284. return "";
  285. var setout = new Client<Setout>()
  286. .Query(
  287. new Filter<Setout>(x => x.Number).IsEqualTo(item.Number),
  288. new Columns<Setout>(x => x.ID))
  289. .ToObjects<Setout>().FirstOrDefault();
  290. string result;
  291. //setout already exists - create new setoutdoc and supercede old ones
  292. if (setout is not null)
  293. {
  294. if (!bulkapprove)
  295. if (MessageBox.Show("Supercede existing documents?", "Proceed?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  296. return "";
  297. setout.Group.ID = item.Group.ID;
  298. item.Setout.ID = setout.ID;
  299. var setoutdoc = new SetoutDocument();
  300. setoutdoc.EntityLink.ID = setout.ID;
  301. setoutdoc.DocumentLink.ID = setoutDocument.DocumentLink.ID;
  302. var setoutdocs = new List<SetoutDocument>
  303. {
  304. setoutdoc
  305. };
  306. CoreTable oldDocsTable = new Client<SetoutDocument>().Query(
  307. new Filter<SetoutDocument>(x => x.EntityLink.ID).IsEqualTo((Guid)setout.ID)
  308. .And(x => x.DocumentLink.ID).IsNotEqualTo(item.Group.OptimizationDocument.ID)
  309. );
  310. foreach (var row in oldDocsTable.Rows)
  311. {
  312. var oldDoc = row.ToObject<SetoutDocument>();
  313. if (oldDoc.Superceded == DateTime.MinValue)
  314. {
  315. oldDoc.Superceded = DateTime.Now;
  316. setoutdocs.Add(oldDoc);
  317. }
  318. }
  319. new Client<SetoutDocument>().Save(setoutdocs, "Updated from Staging Screen");
  320. new Client<Setout>().Save((Setout)setout, "Updated from Staging Screen");
  321. result = item.Number + " Superceded";
  322. }
  323. //no setout for this pdf - create new
  324. else
  325. {
  326. setout = new Setout
  327. {
  328. Number = item.Number
  329. };
  330. setout.JobLink.ID = item.JobLink.ID;
  331. setout.Group.ID = item.Group.ID;
  332. var editor = new DynamicDataGrid<Setout>();
  333. editor.OnAfterSave += (editor, items) =>
  334. {
  335. CreateSetoutDocument(setout, item, setoutDocument);
  336. };
  337. if (!bulkapprove)
  338. {
  339. if (!editor.EditItems(new[] { setout }))
  340. {
  341. MessageBox.Show("Setout Creation Cancelled");
  342. return "";
  343. }
  344. else
  345. result = item.Number + " Created";
  346. }
  347. else
  348. {
  349. new Client<Setout>().Save(setout, "Added from staging screen");
  350. CreateSetoutDocument(setout, item, setoutDocument);
  351. result = item.Number + " Created";
  352. }
  353. }
  354. var packets = new List<Tuple<ManufacturingPacket, StagingManufacturingPacket>>();
  355. var stagingPackets = new Client<StagingManufacturingPacket>()
  356. .Query(
  357. new Filter<StagingManufacturingPacket>(x => x.StagingSetout.ID).IsEqualTo(item.ID),
  358. new Columns<StagingManufacturingPacket>(x => x.ID)
  359. .Add(x => x.Serial)
  360. .Add(x => x.Title)
  361. .Add(x => x.Quantity)
  362. .Add(x => x.BarcodeQuantity)
  363. .Add(x => x.Watermark.ID)
  364. .Add(x => x.Location)
  365. .Add(x => x.ITP.ID)
  366. .Add(x => x.Job.ID)
  367. .Add(x => x.Template.ID));
  368. foreach(var stagingPacket in stagingPackets.ToObjects<StagingManufacturingPacket>())
  369. {
  370. if(stagingPacket.ManufacturingPacket.ID != Guid.Empty)
  371. {
  372. MessageBox.Show($"A manufacturing packet already exists for {stagingPacket.Serial}; skipping packet.");
  373. continue;
  374. }
  375. var packet = new ManufacturingPacket
  376. {
  377. Serial = stagingPacket.Serial,
  378. Title = stagingPacket.Title,
  379. Quantity = stagingPacket.Quantity,
  380. BarcodeQty = stagingPacket.BarcodeQuantity != 0 ? stagingPacket.BarcodeQuantity : stagingPacket.Quantity,
  381. WaterMark = stagingPacket.Watermark.ToString(),
  382. Location = stagingPacket.Location
  383. };
  384. packet.SetoutLink.ID = setout.ID;
  385. packet.ITP.ID = stagingPacket.ITP.ID;
  386. packet.JobLink.ID = stagingPacket.Job.ID;
  387. packet.ManufacturingTemplateLink.ID = stagingPacket.Template.ID;
  388. packets.Add(new(packet, stagingPacket));
  389. }
  390. new Client<ManufacturingPacket>().Save(packets.Select(x => x.Item1), "Created from Design Management Panel");
  391. var newStages = new List<ManufacturingPacketStage>();
  392. foreach(var (packet, stagingPacket) in packets)
  393. {
  394. stagingPacket.ManufacturingPacket.ID = packet.ID;
  395. var stages = new Client<StagingManufacturingPacketStage>()
  396. .Query(
  397. new Filter<StagingManufacturingPacketStage>(x => x.Packet.ID).IsEqualTo(stagingPacket.ID),
  398. IManufacturingPacketGeneratorExtensions.GetPacketGeneratorRequiredColumns<StagingManufacturingPacketStage>());
  399. newStages.AddRange(stages.ToObjects<StagingManufacturingPacketStage>()
  400. .Select(x =>
  401. {
  402. var stage = x.CreateManufacturingPacketStage();
  403. stage.Parent.ID = packet.ID;
  404. return stage;
  405. }));
  406. }
  407. new Client<ManufacturingPacketStage>().Save(newStages, "Created from Design Management");
  408. new Client<StagingManufacturingPacket>().Save(packets.Select(x => x.Item2), "Created from Design Management");
  409. //currently not creating packets due to temporary change in requirements - to uncomment and check for validity when required
  410. //CreatePackets(setout);
  411. return result;
  412. }
  413. private static void CreateSetoutDocument(Setout setout, StagingSetout item, StagingSetoutDocument stagingsetoutdocument)
  414. {
  415. item.Setout.ID = setout.ID;
  416. var setoutdoc = new SetoutDocument();
  417. setoutdoc.EntityLink.ID = setout.ID;
  418. setoutdoc.DocumentLink.ID = stagingsetoutdocument.DocumentLink.ID;
  419. new Client<SetoutDocument>().Save(setoutdoc, "Added from staging screen");
  420. }
  421. private void RejectButton_Click(object sender, RoutedEventArgs e)
  422. {
  423. if (selectedSetout is null || Document is null)
  424. {
  425. MessageBox.Show("Please select a setout");
  426. return;
  427. }
  428. //dont create setout - setout.id remains blank
  429. //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
  430. //attach the document to the task for reference
  431. var task = new Kanban
  432. {
  433. Title = "Setout Review Task (setout rejected)",
  434. Description = "Please review the attached document for setout " + selectedSetout.Number
  435. };
  436. task.ManagerLink.ID = App.EmployeeID;
  437. var page = new KanbanGrid();
  438. page.MyID = App.EmployeeID;
  439. if (page.EditItems(new[] { task }))
  440. {
  441. var doc = new KanbanDocument();
  442. doc.EntityLink.ID = task.ID;
  443. doc.DocumentLink.ID = Document.DocumentLink.ID;
  444. new Client<KanbanDocument>().Save(doc, "Created from staging screen");
  445. selectedSetout.Task.ID = task.ID;
  446. new Client<StagingSetout>().Save(selectedSetout, "Updated from staging screen");
  447. MessageBox.Show("Success - Task Created for Document " + selectedSetout.Number + " (Task no. " + task.Number + " assigned to " + task.EmployeeLink.Name + ")");
  448. selectedSetout = null;
  449. Document = null;
  450. stagingSetoutGrid.Refresh(false, true);
  451. }
  452. else
  453. {
  454. MessageBox.Show("Task creation cancelled - setout not rejected");
  455. }
  456. }
  457. private void MarkUpButton_Click(object sender, RoutedEventArgs e)
  458. {
  459. if (Mode == DocumentMode.Markup)
  460. {
  461. Mode = DocumentMode.Complete;
  462. MessageBox.Show("IMPORTANT - press save in your document editor, then press the Complete Button in PRS");
  463. OnMarkupSelected();
  464. }
  465. else
  466. {
  467. OnMarkupComplete();
  468. Mode = DocumentMode.Markup;
  469. }
  470. }
  471. private void ApproveButton_Click(object sender, RoutedEventArgs e)
  472. {
  473. if (Document is null || selectedSetout is null)
  474. {
  475. MessageBox.Show("Please select a setout first.");
  476. return;
  477. }
  478. Document.Approved = !Document.Approved;
  479. new Client<StagingSetoutDocument>().Save(Document, "");
  480. stagingSetoutGrid.Refresh(false, true);
  481. }
  482. private void OnMarkupSelected()
  483. {
  484. if (Document is null || selectedSetout is null)
  485. {
  486. MessageBox.Show("Please select a setout first.");
  487. return;
  488. }
  489. var doc = new Client<Document>()
  490. .Query(
  491. new Filter<Document>(x => x.ID).IsEqualTo(Document.DocumentLink.ID))
  492. .ToObjects<Document>().FirstOrDefault();
  493. if (doc is null)
  494. {
  495. Logger.Send(LogType.Error, "", $"Document with ID {Document.DocumentLink.ID} could not be found.");
  496. MessageBox.Show("Error: the selected document could not be found in the database.");
  497. return;
  498. }
  499. var tempdocpath = Path.Combine(Path.GetTempPath(), doc.FileName);
  500. selectedSetout.SavePath = tempdocpath;
  501. selectedSetout.Locked = DateTime.Now;
  502. selectedSetout.LockedBy.ID = App.EmployeeID;
  503. new Client<StagingSetout>().Save(selectedSetout, "Locked from Staging Screen");
  504. File.WriteAllBytes(tempdocpath, doc.Data);
  505. using (var p = new Process())
  506. {
  507. p.StartInfo = new ProcessStartInfo()
  508. {
  509. UseShellExecute = true,
  510. FileName = tempdocpath
  511. };
  512. p.Start();
  513. }
  514. stagingSetoutGrid.Refresh(false, true);
  515. }
  516. private void OnMarkupComplete()
  517. {
  518. if (selectedSetout is null)
  519. {
  520. MessageBox.Show("Please select a setout first.");
  521. return;
  522. }
  523. StagingSetoutGrid.ReloadFile(selectedSetout);
  524. stagingSetoutGrid.Refresh(false, true);
  525. }
  526. #endregion
  527. private void StagingSetoutGrid_OnSelectItem(object sender, InABox.DynamicGrid.DynamicGridSelectionEventArgs e)
  528. {
  529. selectedSetouts.Clear();
  530. foreach (var row in e.Rows ?? Enumerable.Empty<CoreRow>())
  531. selectedSetouts.Add(row.ToObject<StagingSetout>());
  532. selectedSetout = selectedSetouts.FirstOrDefault();
  533. AddPacketButton.IsEnabled = selectedSetout is not null;
  534. if(selectedSetout is null)
  535. {
  536. Document = null;
  537. ManufacturingPacketList.Setout = null;
  538. CollapsePacketsButton.IsEnabled = false;
  539. return;
  540. }
  541. var doc = new Client<StagingSetoutDocument>()
  542. .Query(
  543. new Filter<StagingSetoutDocument>(x => x.EntityLink.ID).IsEqualTo(selectedSetout.ID),
  544. new Columns<StagingSetoutDocument>(x => x.ID, x => x.DocumentLink.ID, x => x.DocumentLink.FileName, x => x.Approved))
  545. .ToObjects<StagingSetoutDocument>().FirstOrDefault();
  546. if(doc is null)
  547. {
  548. MessageBox.Show("No document found for this setout.");
  549. Document = null;
  550. ManufacturingPacketList.Setout = null;
  551. CollapsePacketsButton.IsEnabled = false;
  552. return;
  553. }
  554. Document = doc;
  555. if(MainPanel.View != DynamicSplitPanelView.Master && NestedPanel.View != DynamicSplitPanelView.Master)
  556. {
  557. ManufacturingPacketList.Setout = selectedSetout;
  558. }
  559. CollapsePacketsButton.IsEnabled = true;
  560. Mode =
  561. selectedSetout.Locked == DateTime.MinValue ?
  562. DocumentMode.Markup :
  563. selectedSetout.LockedBy.ID == App.EmployeeID ?
  564. DocumentMode.Complete :
  565. DocumentMode.Locked;
  566. ApproveButton.Content = Document.Approved ? "Unapprove" : "Approve";
  567. }
  568. public bool IsReady { get; set; }
  569. public string SectionName { get; }
  570. public event DataModelUpdateEvent? OnUpdateDataModel;
  571. #region Settings
  572. public void CreateToolbarButtons(IPanelHost host)
  573. {
  574. host.CreateSetupAction(
  575. new PanelAction()
  576. {
  577. Caption = "Setouts Configuration",
  578. Image = PRSDesktop.Resources.specifications,
  579. OnExecute = ConfigSettingsClick
  580. }
  581. );
  582. host.CreateSetupAction(
  583. new PanelAction()
  584. {
  585. Caption = "Template Products",
  586. Image = PRSDesktop.Resources.specifications,
  587. OnExecute =
  588. action =>
  589. {
  590. var list = new MasterList(typeof(ManufacturingTemplateGroupProducts));
  591. list.ShowDialog();
  592. }
  593. }
  594. );
  595. }
  596. private void ConfigSettingsClick(PanelAction obj)
  597. {
  598. var pages = new DynamicEditorPages();
  599. var propertyEditor = new DynamicEditorForm(typeof(StagingPanellSettings), pages);
  600. propertyEditor.OnDefineLookups += sender =>
  601. {
  602. var editor = sender.EditorDefinition as ILookupEditor;
  603. var colname = sender.ColumnName;
  604. var values = editor.Values(colname, new[] { _settings });
  605. sender.LoadLookups(values);
  606. };
  607. propertyEditor.OnEditorValueChanged += (sender, name, value) =>
  608. {
  609. CoreUtils.SetPropertyValue(_settings, name, value);
  610. return new Dictionary<string, object?>();
  611. };
  612. propertyEditor.OnFormCustomiseEditor += Settings_OnFormCustomiseEditor;
  613. propertyEditor.Items = new BaseObject[] { _settings };
  614. if (propertyEditor.ShowDialog() == true)
  615. {
  616. new GlobalConfiguration<StagingPanellSettings>().Save(_settings);
  617. _script = null;
  618. }
  619. }
  620. private void Settings_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
  621. {
  622. if (items?.FirstOrDefault() is not StagingPanellSettings settings) return;
  623. if (column.ColumnName == nameof(StagingPanellSettings.Script) && editor is ScriptEditor scriptEditor)
  624. {
  625. scriptEditor.Type = ScriptEditorType.TemplateEditor;
  626. scriptEditor.OnEditorClicked += () =>
  627. {
  628. var script = settings.Script.NotWhiteSpaceOr()
  629. ?? settings.DefaultScript();
  630. var editor = new ScriptEditorWindow(script, SyntaxLanguage.CSharp);
  631. if (editor.ShowDialog() == true)
  632. {
  633. sender.SetEditorValue(column.ColumnName, editor.Script);
  634. settings.Script = editor.Script;
  635. }
  636. };
  637. }
  638. }
  639. #endregion
  640. public void Heartbeat(TimeSpan time)
  641. {
  642. }
  643. public void Refresh()
  644. {
  645. //stagingSetoutGrid.ScanFiles(_settings.SetoutsFolder);
  646. stagingSetoutGrid.Refresh(false, true);
  647. Document = null;
  648. ManufacturingPacketList.Setout = null;
  649. CalculateTime();
  650. }
  651. public Dictionary<string, object[]> Selected()
  652. {
  653. return new();
  654. }
  655. public void Shutdown(CancelEventArgs? cancel)
  656. {
  657. }
  658. public DataModel DataModel(Selection selection)
  659. {
  660. return new AutoDataModel<StagingSetout>(new Filter<StagingSetout>().All());
  661. }
  662. private void AddPacketButton_Click(object sender, RoutedEventArgs e)
  663. {
  664. if (_templateGroups.Rows.Any())
  665. {
  666. ContextMenu menu = new ContextMenu();
  667. foreach (var row in _templateGroups.Rows)
  668. {
  669. MenuItem item = new MenuItem()
  670. {
  671. Header =
  672. $"{row.Get<ManufacturingTemplateGroup, String>(x => x.Code)}: {row.Get<ManufacturingTemplateGroup, String>(x => x.Description)}",
  673. Command = new ActionCommand((obj) =>
  674. {
  675. ManufacturingPacketList.Add(row.ToObject<ManufacturingTemplateGroup>());
  676. stagingSetoutGrid.Refresh(false, true);
  677. })
  678. };
  679. menu.Items.Add(item);
  680. }
  681. menu.Items.Add(new Separator());
  682. MenuItem misc = new MenuItem()
  683. {
  684. Header = "Miscellaneous Item",
  685. Command = new ActionCommand((obj) =>
  686. {
  687. ManufacturingPacketList.Add(null);
  688. stagingSetoutGrid.Refresh(false, true);
  689. })
  690. };
  691. menu.Items.Add(misc);
  692. menu.IsOpen = true;
  693. }
  694. else
  695. {
  696. ManufacturingPacketList.Add(null);
  697. stagingSetoutGrid.Refresh(false, true);
  698. }
  699. }
  700. private void CollapsePacketsButton_Click(object sender, RoutedEventArgs e)
  701. {
  702. if (ManufacturingPacketList.Collapsed())
  703. {
  704. ManufacturingPacketList.Uncollapse();
  705. }
  706. else
  707. {
  708. ManufacturingPacketList.Collapse();
  709. }
  710. }
  711. private void ManufacturingPacketList_OnCollapsed(bool collapsed)
  712. {
  713. if (collapsed)
  714. {
  715. CollapsePacketsButton.Content = "Expand";
  716. }
  717. else
  718. {
  719. CollapsePacketsButton.Content = "Collapse";
  720. }
  721. }
  722. private void stagingSetoutGrid_OnCustomiseSetouts(IReadOnlyList<StagingSetoutGrid.SetoutDocument> setouts)
  723. {
  724. if(CustomiseSetoutsMethod != null && ScriptObject != null)
  725. {
  726. CustomiseSetoutsMethod?.Invoke(ScriptObject, new object?[]
  727. {
  728. new CustomiseSetoutsArgs(setouts.Select(x => new Tuple<StagingSetout, Document>(x.Setout, x.Document)).ToImmutableList())
  729. });
  730. }
  731. }
  732. private void StagingSetoutGrid_OnOnDoubleClick(object sender, HandledEventArgs args)
  733. {
  734. ManufacturingPacketList.Setout = selectedSetout;
  735. MainPanel.View = DynamicSplitPanelView.Detail;
  736. NestedPanel.View = DynamicSplitPanelView.Combined;
  737. args.Handled = true;
  738. }
  739. private void CalculateTime()
  740. {
  741. if (selectedSetout != null)
  742. {
  743. var time = ManufacturingPacketList.TimeRequired();
  744. TimeRequired.Content = $"{time.TotalHours:F2} hours";
  745. }
  746. else
  747. TimeRequired.Content = "N/A";
  748. }
  749. private void ManufacturingPacketList_OnChanged(object? sender, EventArgs e)
  750. {
  751. CalculateTime();
  752. }
  753. }
  754. }