StagingPanel.xaml.cs 36 KB

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