12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250 |
- using Comal.Classes;
- using InABox.Core;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.DynamicGrid;
- using System.Diagnostics;
- using System.IO;
- using InABox.WPF;
- using InABox.Wpf;
- using System.ComponentModel;
- using InABox.Scripting;
- using System.Reflection;
- using System.Collections.Immutable;
- using StagingManufacturingPacketComponent = Comal.Classes.StagingManufacturingPacketComponent;
- using System.Threading.Tasks;
- using NPOI.SS.Formula.Functions;
- using Columns = InABox.Core.Columns;
- namespace PRSDesktop
- {
- [Caption("Staging Panel Settings")]
- public class StagingPanellSettings : BaseObject, IGlobalConfigurationSettings
- {
- [Caption("PDF Markup Program Pathway", IncludePath = false)]
- [FileNameEditor]
- public string MarkupPathway { get; set; }
- [FolderEditor(Environment.SpecialFolder.CommonDocuments)]
- public string SetoutsFolder { get; set; }
- [ScriptEditor]
- public string? Script { get; set; }
- [IntegerEditor(Caption = "Maximum Document Size (MB)", ToolTip = "The user will be warned when uploading documents which are larger than this size in megabytes. Set to zero for no maximum.")]
- public int MaximumDocumentSize { get; set; }
- public StagingPanellSettings()
- {
- MarkupPathway = "";
- SetoutsFolder = "";
- Script = null;
- MaximumDocumentSize = 0;
- }
- public string DefaultScript()
- {
- return @"
- using PRSDesktop;
- using InABox.Core;
- using System.Collections.Generic;
- public class Module
- {
- /*public void CustomiseSetouts(CustomiseSetoutsArgs args)
- {
- // Perform customisation on the setouts when they are added to the 'Staged Documents' grid.
- }*/
- }";
- }
- }
- public class CustomiseSetoutsArgs
- {
- public IReadOnlyList<Tuple<StagingSetout, Document>> Setouts;
- public CustomiseSetoutsArgs(IReadOnlyList<Tuple<StagingSetout, Document>> setouts)
- {
- Setouts = setouts;
- }
- }
- /// <summary>
- /// Interaction logic for StagingPanel.xaml
- /// </summary>
- public partial class StagingPanel : UserControl, IPanel<StagingSetout>
- {
- private StagingPanellSettings _settings = new StagingPanellSettings();
-
- /// <summary>
- /// The currently selected setout.
- /// </summary>
- private StagingSetout? selectedSetout;
- /// <summary>
- /// All currently selected setouts; <see cref="selectedSetout"/> will be a member of this list.
- /// </summary>
- private List<StagingSetout> selectedSetouts = new();
- private CoreTable _templateGroups = null!;
- #region Script Stuff
- private MethodInfo? _customiseSetoutsMethod;
- private MethodInfo? CustomiseSetoutsMethod
- {
- get
- {
- EnsureScript();
- return _customiseSetoutsMethod;
- }
- }
- private object? _scriptObject;
- private object? ScriptObject
- {
- get
- {
- EnsureScript();
- return _scriptObject;
- }
- }
- private ScriptDocument? _script;
- private ScriptDocument? Script
- {
- get
- {
- EnsureScript();
- return _script;
- }
- }
- private void EnsureScript()
- {
- if (_script is null && !_settings.Script.IsNullOrWhiteSpace())
- {
- _script = new ScriptDocument(_settings.Script);
- if (!_script.Compile())
- {
- throw new Exception("Script in Staging Panel Settings failed to compile!");
- }
- _scriptObject = _script?.GetObject();
- _customiseSetoutsMethod = _script?.GetMethod(methodName: "CustomiseSetouts");
- }
- }
- #endregion
- public StagingPanel()
- {
- InitializeComponent();
- SectionName = nameof(StagingPanel);
- }
- public void Setup()
- {
- _settings = new GlobalConfiguration<StagingPanellSettings>().Load();
-
- _templateGroups = new Client<ManufacturingTemplateGroup>().Query();
-
- MarkUpButton.Visibility = Security.IsAllowed<CanMarkUpSetouts>() ? Visibility.Visible : Visibility.Hidden;
- RejectButton.Visibility = Security.IsAllowed<CanApproveSetouts>() ? Visibility.Visible : Visibility.Hidden;
- ApproveButton.Visibility = Security.IsAllowed<CanApproveSetouts>() ? Visibility.Visible : Visibility.Hidden;
- ProcessButton.Visibility = Security.IsAllowed<CanApproveSetouts>() ? Visibility.Visible : Visibility.Hidden;
- //stagingSetoutGrid.ScanFiles(_settings.SetoutsFolder);
- stagingSetoutGrid.PanelSettings = _settings;
- stagingSetoutGrid.Refresh(true, false);
- SetoutComponentGrid.Refresh(true, false);
-
- }
- private bool CanViewPackets() => MainPanel.View != DynamicSplitPanelView.Master && NestedPanel.View != DynamicSplitPanelView.Master;
- private void NestedPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
- {
- if(CanViewPackets())
- {
- ManufacturingPacketList.Setout = selectedSetout;
- SetoutComponentGrid.StagingSetout = selectedSetout;
- }
- }
- #region Document Viewer
- public enum DocumentMode
- {
- Markup,
- Complete,
- Locked
- }
- private DocumentMode _mode;
- private DocumentMode Mode
- {
- get => _mode;
- set => SetMode(value);
- }
- private void SetMode(DocumentMode mode)
- {
- _mode = mode;
- if (_mode == DocumentMode.Markup)
- {
- MarkUpButton.Content = "Mark Up";
- MarkUpButton.IsEnabled = Document != null && !Document.Approved;
- UpdateOriginalButton.Visibility =
- Document != null && !String.Equals(Document.DocumentLink.CRC, selectedSetout?.OriginalCRC)
- ? Visibility.Visible
- : Visibility.Collapsed;
- ProcessButton.IsEnabled = Document != null && Document.Approved;
- RejectButton.IsEnabled = Document != null && !Document.Approved;
- ApproveButton.IsEnabled = Document != null;
- }
- else if (_mode == DocumentMode.Complete)
- {
- MarkUpButton.Content = "Complete";
- MarkUpButton.IsEnabled = Document != null;
- UpdateOriginalButton.Visibility = Visibility.Collapsed;
- ProcessButton.IsEnabled = false;
- RejectButton.IsEnabled = false;
- ApproveButton.IsEnabled = false;
- }
- else if (_mode == DocumentMode.Locked)
- {
- MarkUpButton.Content = "Locked";
- MarkUpButton.IsEnabled = false;
- UpdateOriginalButton.Visibility = Visibility.Collapsed;
- ProcessButton.IsEnabled = false;
- RejectButton.IsEnabled = false;
- ApproveButton.IsEnabled = false;
- }
- }
- private StagingSetoutDocument? _document;
- private StagingSetoutDocument? Document
- {
- get => _document;
- set
- {
- _document = value;
- if(_document is not null)
- {
- ApproveButton.Content = _document.Approved ? "Unapprove" : "Approve";
- }
- }
- }
- private byte[]? _documentdata = null;
- private void ClearDocuments()
- {
- Document = null;
- RenderDocuments(null);
- }
- private List<byte[]>? GetDocuments(StagingSetoutDocument? document)
- {
- if(document is null)
- {
- return null;
- }
- var table = new Client<Document>().Query(
- new Filter<Document>(x => x.ID).IsEqualTo(document.DocumentLink.ID),
- Columns.None<Document>().Add(x => x.Data));
- var first = table.Rows.FirstOrDefault();
- if (first is null)
- return null;
- _documentdata = first.Get<Document, byte[]>(x => x.Data);
- return ImageUtils.RenderPDFToImageBytes(_documentdata);
- }
-
- private void RenderDocuments(List<byte[]>? documents)
- {
- DocumentViewer.Children.Clear();
- if(documents is not null)
- {
- foreach (var image in documents)
- {
- DocumentViewer.Children.Add(new Image
- {
- Source = ImageUtils.LoadImage(image),
- Margin = new Thickness(0, 0, 0, 20)
- });
- }
- }
- }
- private void ProcessButton_Click(object sender, RoutedEventArgs e)
- {
- bool bulkApprove = false;
- if (selectedSetouts.Count > 1)
- {
- if (MessageBox.Show("Bulk approve? (Skip individual setout approval)", "Continue?", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
- {
- bulkApprove = true;
- Progress.Show("Approving Setouts..");
- }
- }
- if (selectedSetouts.Any(x => x.UnapprovedDocuments > 0))
- {
- MessageBox.Show("Cannot process setouts with unapproved documents.");
- Progress.Close();
- return;
- }
- if (selectedSetouts.Any(x => x.JobLink.ID == Guid.Empty))
- {
- MessageBox.Show("Cannot process setout without a linked job.");
- Progress.Close();
- return;
- }
- if (ManufacturingPacketList.Packets.Any(x => x.Template.ID == Guid.Empty))
- {
- MessageBox.Show("Cannot process manufacturing packets without templates.");
- Progress.Close();
- return;
- }
- if (selectedSetouts.Any(x => x.Packets == 0))
- {
- if (MessageBox.Show("Warning: some setouts do not have any manufacturing packets: are you sure you wish to proceed?", "Warning", MessageBoxButton.YesNoCancel) != MessageBoxResult.Yes)
- {
- Progress.Close();
- return;
- }
- }
- string message = "";
- foreach (var item in selectedSetouts)
- {
- if (bulkApprove)
- Progress.Show("Working on " + item.Number);
- var returnstring = ApproveSetout(item, bulkApprove);
- if (!string.IsNullOrWhiteSpace(returnstring))
- message = message + returnstring + Environment.NewLine;
- }
- if (bulkApprove)
- Progress.Close();
- new Client<StagingSetout>().Save(selectedSetouts, "Updated from staging screen");
- selectedSetout = null;
- Refresh();
- if (!message.IsNullOrWhiteSpace())
- {
- MessageBox.Show($"Result:\n{message}");
- }
- MainPanel.View = DynamicSplitPanelView.Combined;
- NestedPanel.View = DynamicSplitPanelView.Master;
- }
- private string ApproveSetout(StagingSetout item, bool bulkapprove)
- {
- if (item.Group.ID == Guid.Empty)
- {
- var message = "Setout has no group assigned";
- if (Security.IsAllowed<CanApproveSetoutsWithoutGroup>())
- {
- if (MessageBox.Show(message + ", continue?", "Continue?", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
- return "";
- }
- else
- {
- MessageBox.Show(message + ", please assign a group!");
- return "";
- }
- }
- var setoutDocument = new Client<StagingSetoutDocument>()
- .Query(
- new Filter<StagingSetoutDocument>(x => x.EntityLink.ID).IsEqualTo(item.ID),
- Columns.None<StagingSetoutDocument>().Add(x => x.ID, x => x.DocumentLink.ID, x => x.DocumentLink.FileName))
- .ToObjects<StagingSetoutDocument>().FirstOrDefault();
- if (setoutDocument is null)
- return "";
- var setout = new Client<Setout>()
- .Query(
- new Filter<Setout>(x => x.Number).IsEqualTo(item.Number),
- Columns.Required<Setout>().Add(x => x.ID))
- .ToObjects<Setout>().FirstOrDefault();
- string result;
- //setout already exists - create new setoutdoc and supercede old ones
- if (setout is not null)
- {
- if (!bulkapprove)
- if (MessageBox.Show("Supercede existing documents?", "Proceed?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
- return "";
- setout.Group.ID = item.Group.ID;
- item.Setout.ID = setout.ID;
- var setoutdoc = new SetoutDocument();
- setoutdoc.EntityLink.ID = setout.ID;
- setoutdoc.DocumentLink.ID = setoutDocument.DocumentLink.ID;
- var setoutdocs = new List<SetoutDocument>
- {
- setoutdoc
- };
- CoreTable oldDocsTable = new Client<SetoutDocument>().Query(
- new Filter<SetoutDocument>(x => x.EntityLink.ID).IsEqualTo((Guid)setout.ID)
- .And(x => x.DocumentLink.ID).IsNotEqualTo(item.Group.OptimizationDocument.ID)
- );
- foreach (var row in oldDocsTable.Rows)
- {
- var oldDoc = row.ToObject<SetoutDocument>();
- if (oldDoc.Superceded == DateTime.MinValue)
- {
- oldDoc.Superceded = DateTime.Now;
- setoutdocs.Add(oldDoc);
- }
- }
- new Client<SetoutDocument>().Save(setoutdocs, "Updated from Staging Screen");
- new Client<Setout>().Save((Setout)setout, "Updated from Staging Screen");
- result = item.Number + " Superceded";
- }
- //no setout for this pdf - create new
- else
- {
- setout = new Setout
- {
- Number = item.Number
- };
- setout.JobLink.ID = item.JobLink.ID;
- setout.Group.ID = item.Group.ID;
- var editor = new DynamicDataGrid<Setout>();
- editor.OnAfterSave += (editor, items) =>
- {
- CreateSetoutDocument(setout, item, setoutDocument);
- };
- if (!bulkapprove)
- {
- if (!editor.EditItems(new[] { setout }))
- {
- MessageBox.Show("Setout Creation Cancelled");
- return "";
- }
- else
- result = item.Number + " Created";
- }
- else
- {
- new Client<Setout>().Save(setout, "Added from staging screen");
- CreateSetoutDocument(setout, item, setoutDocument);
- result = item.Number + " Created";
- }
- }
- var tuples = new List<Tuple<ManufacturingPacket, StagingManufacturingPacket>>();
- var stagingPackets = new Client<StagingManufacturingPacket>()
- .Query(
- new Filter<StagingManufacturingPacket>(x => x.StagingSetout.ID).IsEqualTo(item.ID),
- Columns.Required<StagingManufacturingPacket>().Add(x => x.ID)
- .Add(x => x.Serial)
- .Add(x => x.Title)
- .Add(x => x.Quantity)
- .Add(x => x.BarcodeQuantity)
- .Add(x => x.Watermark)
- .Add(x => x.Group.Watermark)
- .Add(x => x.Location)
- .Add(x => x.ITP.ID)
- .Add(x => x.Job.ID)
- .Add(x => x.Template.ID));
- foreach(var stagingPacket in stagingPackets.ToObjects<StagingManufacturingPacket>())
- {
- if(stagingPacket.ManufacturingPacket.ID != Guid.Empty)
- {
- MessageBox.Show($"A manufacturing packet already exists for {stagingPacket.Serial}; skipping packet.");
- continue;
- }
- var packet = new ManufacturingPacket
- {
- Serial = stagingPacket.Serial,
- Title = stagingPacket.Title,
- Quantity = stagingPacket.Quantity,
- BarcodeQty = string.IsNullOrWhiteSpace(stagingPacket.BarcodeQuantity)
- ? stagingPacket.Quantity
- : int.Parse(stagingPacket.BarcodeQuantity),
- WaterMark = string.IsNullOrWhiteSpace(stagingPacket.Watermark)
- ? stagingPacket.Group.Watermark
- : stagingPacket.Watermark,
- Location = stagingPacket.Location
- };
- packet.SetoutLink.ID = setout.ID;
- packet.ITP.ID = stagingPacket.ITP.ID;
- packet.JobLink.ID = stagingPacket.Job.ID;
- packet.ManufacturingTemplateLink.ID = stagingPacket.Template.ID;
- tuples.Add(new(packet, stagingPacket));
- }
- new Client<ManufacturingPacket>().Save(tuples.Select(x => x.Item1), "Created from Design Management Panel");
- var newStages = new List<ManufacturingPacketStage>();
- var newComponents = new List<ManufacturingPacketComponent>();
- var newTreatments = new List<ManufacturingTreatment>();
- foreach(var (packet, stagingPacket) in tuples)
- {
- stagingPacket.ManufacturingPacket.ID = packet.ID;
-
- var stages = new Client<StagingManufacturingPacketStage>()
- .Query(
- new Filter<StagingManufacturingPacketStage>(x => x.Packet.ID).IsEqualTo(stagingPacket.ID),
- IManufacturingPacketGeneratorExtensions.GetPacketGeneratorRequiredColumns<StagingManufacturingPacketStage>());
- newStages.AddRange(stages.ToObjects<StagingManufacturingPacketStage>()
- .Select(x =>
- {
- var stage = x.CreateManufacturingPacketStage();
- stage.Parent.ID = packet.ID;
- return stage;
- }));
-
- var components = new Client<StagingManufacturingPacketComponent>()
- .Query(
- new Filter<StagingManufacturingPacketComponent>(x => x.Packet.ID).IsEqualTo(stagingPacket.ID),
- Columns.None<StagingManufacturingPacketComponent>().Add(x=>x.Packet.ID)
- .Add(x => x.Product.ID)
- .Add(x => x.Quantity)
- .Add(x => x.Dimensions.Unit.ID)
- .Add(x => x.Dimensions.Quantity)
- .Add(x => x.Dimensions.Length)
- .Add(x => x.Dimensions.Width)
- .Add(x => x.Dimensions.Height)
- .Add(x => x.Dimensions.Weight)
- .Add(x => x.Dimensions.Value)
- .Add(x => x.Dimensions.UnitSize)
- );
-
- newComponents.AddRange(components.ToObjects<StagingManufacturingPacketComponent>()
- .Select(x => x.CreateComponent(packet.ID)));
-
- var treatments = new Client<StagingManufacturingPacketTreatment>()
- .Query(
- new Filter<StagingManufacturingPacketTreatment>(x => x.Packet.ID).IsEqualTo(stagingPacket.ID),
- Columns.None<StagingManufacturingPacketTreatment>().Add(x=>x.Packet.ID)
- .Add(x=>x.Product.ID)
- .Add(x=>x.Parameter)
- );
-
- newTreatments.AddRange(treatments.ToObjects<StagingManufacturingPacketTreatment>()
- .Select(x => x.CreateTreatment(packet.ID)));
-
- }
- new Client<ManufacturingPacketStage>().Save(newStages, "Created from Design Management");
- new Client<ManufacturingPacketComponent>().Save(newComponents, "Created from Design Management");
- new Client<ManufacturingTreatment>().Save(newTreatments, "Created from Design Management");
- new Client<StagingManufacturingPacket>().Save(tuples.Select(x => x.Item2), "Created from Design Management");
- //currently not creating packets due to temporary change in requirements - to uncomment and check for validity when required
- //CreatePackets(setout);
- return result;
- }
- private static void CreateSetoutDocument(Setout setout, StagingSetout item, StagingSetoutDocument stagingsetoutdocument)
- {
- item.Setout.ID = setout.ID;
- var setoutdoc = new SetoutDocument();
- setoutdoc.EntityLink.ID = setout.ID;
- setoutdoc.DocumentLink.ID = stagingsetoutdocument.DocumentLink.ID;
- new Client<SetoutDocument>().Save(setoutdoc, "Added from staging screen");
- }
- private void RejectButton_Click(object sender, RoutedEventArgs e)
- {
- if (selectedSetout is null || Document is null)
- {
- MessageBox.Show("Please select a setout");
- return;
- }
- //dont create setout - setout.id remains blank
- //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
- //attach the document to the task for reference
- var task = new Kanban
- {
- Title = "Setout Review Task (setout rejected)",
- Description = "Please review the attached document for setout " + selectedSetout.Number
- };
- task.ManagerLink.ID = App.EmployeeID;
- var page = new TaskGrid();
- if (page.EditItems(new[] { task }))
- {
- var doc = new KanbanDocument();
- doc.EntityLink.ID = task.ID;
- doc.DocumentLink.ID = Document.DocumentLink.ID;
- new Client<KanbanDocument>().Save(doc, "Created from staging screen");
- selectedSetout.Task.ID = task.ID;
- new Client<StagingSetout>().Save(selectedSetout, "Updated from staging screen");
- MessageBox.Show("Success - Task Created for Document " + selectedSetout.Number + " (Task no. " + task.Number + " assigned to " + task.EmployeeLink.Name + ")");
- selectedSetout = null;
- ClearDocuments();
- refreshing = true;
- stagingSetoutGrid.Refresh(false, true);
- }
- else
- {
- MessageBox.Show("Task creation cancelled - setout not rejected");
- }
- }
- private void MarkUpButton_Click(object sender, RoutedEventArgs e)
- {
- if (Mode == DocumentMode.Markup)
- {
- Mode = DocumentMode.Complete;
- MessageBox.Show("IMPORTANT - press save in your document editor, then press the Complete Button in PRS");
- OnMarkupSelected();
- }
- else
- {
- OnMarkupComplete();
- Mode = DocumentMode.Markup;
- }
- }
- private void UpdateOriginalButton_Click(object sender, RoutedEventArgs e)
- {
- if ((_documentdata?.Any() == true) && !String.IsNullOrWhiteSpace(selectedSetout?.OriginalPath))
- {
- try
- {
- File.WriteAllBytes(selectedSetout.OriginalPath, _documentdata);
- selectedSetout.OriginalCRC = CoreUtils.CalculateCRC(_documentdata);
- new Client<StagingSetout>().Save(selectedSetout,"Updated Source File with markups");
- UpdateOriginalButton.Visibility = Visibility.Collapsed;
- }
- catch (Exception _exception)
- {
- MessageBox.Show($"Unable to update {selectedSetout?.OriginalPath}!\n\n{_exception.Message}");
- }
- return;
- }
- MessageBox.Show("Please select a design first!");
- }
- private void ApproveButton_Click(object sender, RoutedEventArgs e)
- {
- if (Document is null || selectedSetout is null)
- {
- MessageBox.Show("Please select a setout first.");
- return;
- }
- if (selectedSetouts.Count > 1)
- {
- var msg = Document.Approved
- ? "Bulk unapprove?"
- : "Bulk approve? (Skip individual setout approval)";
- if (MessageBox.Show(msg, "Continue?", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
- {
- Progress.Show("Approving Setouts..");
- var documents = Client.Query(
- new Filter<StagingSetoutDocument>(x => x.EntityLink.ID).InList(selectedSetouts.Select(x => x.ID).ToArray()),
- Columns.Required<StagingSetoutDocument>().Add(x => x.ID, x => x.Approved)
- ).ToObjects<StagingSetoutDocument>().ToList();
- foreach(var document in documents)
- {
- document.Approved = !Document.Approved;
- }
- Client.Save(documents, "Approved by user.");
- Progress.Close();
- refreshing = true;
- stagingSetoutGrid.Refresh(false, true);
- }
- }
- else
- {
- Document.Approved = !Document.Approved;
- new Client<StagingSetoutDocument>().Save(Document, "");
- refreshing = true;
- stagingSetoutGrid.Refresh(false, true);
- }
- }
- private void OnMarkupSelected()
- {
- if (Document is null || selectedSetout is null)
- {
- MessageBox.Show("Please select a setout first.");
- return;
- }
- var doc = new Client<Document>()
- .Query(
- new Filter<Document>(x => x.ID).IsEqualTo(Document.DocumentLink.ID))
- .ToObjects<Document>().FirstOrDefault();
- if (doc is null)
- {
- Logger.Send(LogType.Error, "", $"Document with ID {Document.DocumentLink.ID} could not be found.");
- MessageBox.Show("Error: the selected document could not be found in the database.");
- return;
- }
- var tempdocpath = Path.Combine(Path.GetTempPath(), doc.FileName);
- selectedSetout.SavePath = tempdocpath;
- selectedSetout.LockedBy.ID = App.EmployeeID;
- selectedSetout.LockedBy.Name = App.EmployeeName;
- new Client<StagingSetout>().Save(selectedSetout, "Locked from Staging Screen");
- File.WriteAllBytes(tempdocpath, doc.Data);
- using (var p = new Process())
- {
- p.StartInfo = new ProcessStartInfo()
- {
- UseShellExecute = true,
- FileName = tempdocpath
- };
- p.Start();
- }
- refreshing = true;
- stagingSetoutGrid.Refresh(false, true);
- }
- private void OnMarkupComplete()
- {
- if (selectedSetout is null)
- {
- MessageBox.Show("Please select a setout first.");
- return;
- }
- StagingSetoutGrid.ReloadFile(selectedSetout);
- refreshing = true;
- stagingSetoutGrid.Refresh(false, true);
- }
- #endregion
- private bool refreshing = false;
- private void stagingSetoutGrid_AfterRefresh(object sender, AfterRefreshEventArgs args)
- {
- refreshing = false;
- }
- private void StagingSetoutGrid_OnSelectItem(object sender, InABox.DynamicGrid.DynamicGridSelectionEventArgs e)
- {
- var newSetouts = new List<StagingSetout>();
- foreach (var row in e.Rows ?? Enumerable.Empty<CoreRow>())
- newSetouts.Add(row.ToObject<StagingSetout>());
- if(!refreshing && (selectedSetouts.Count == newSetouts.Count
- && !selectedSetouts.Any(x => !newSetouts.Any(y => x.ID == y.ID))))
- {
- selectedSetouts = newSetouts;
- selectedSetout = selectedSetouts.FirstOrDefault();
- return;
- }
- selectedSetouts = newSetouts;
- selectedSetout = selectedSetouts.FirstOrDefault();
- AddPacketButton.IsEnabled = selectedSetout is not null;
- if(selectedSetout is null)
- {
- ClearDocuments();
- ManufacturingPacketList.Setout = null;
- CollapsePacketsButton.IsEnabled = false;
- SetoutComponentGrid.StagingSetout = null;
- SetMode(DocumentMode.Markup);
- return;
- }
- var doc = new Client<StagingSetoutDocument>()
- .Query(
- new Filter<StagingSetoutDocument>(x => x.EntityLink.ID).IsEqualTo(selectedSetout.ID),
- Columns.None<StagingSetoutDocument>().Add(x => x.ID)
- .Add(x => x.DocumentLink.ID)
- .Add(x => x.DocumentLink.FileName)
- .Add(x => x.Approved)
- .Add(x=>x.DocumentLink.CRC)
- ).ToObjects<StagingSetoutDocument>().FirstOrDefault();
- if(doc is null)
- {
- MessageBox.Show("No document found for this setout.");
- ClearDocuments();
- ManufacturingPacketList.Setout = null;
- CollapsePacketsButton.IsEnabled = false;
- SetoutComponentGrid.StagingSetout = null;
- return;
- }
- Document = doc;
- var docTask = Task.Run(() => GetDocuments(doc));
- if(CanViewPackets())
- {
- ManufacturingPacketList.Setout = selectedSetout;
- SetoutComponentGrid.StagingSetout = selectedSetout;
- }
- CollapsePacketsButton.IsEnabled = true;
- var mode =
- selectedSetout.LockedBy.ID == Guid.Empty ?
- DocumentMode.Markup :
- selectedSetout.LockedBy.ID == App.EmployeeID ?
- DocumentMode.Complete :
- DocumentMode.Locked;
- docTask.Wait();
- RenderDocuments(docTask.Result);
- SetMode(mode);
- }
- public bool IsReady { get; set; }
- public string SectionName { get; }
- public event DataModelUpdateEvent? OnUpdateDataModel;
- #region Settings
- public void CreateToolbarButtons(IPanelHost host)
- {
- ProjectSetupActions.JobStatuses(host);
- ProjectSetupActions.DrawingTemplates(host);
- host.CreateSetupSeparator();
- ProjectSetupActions.JobSpreadsheetTemplates(host);
- host.CreateSetupSeparator();
- ProjectSetupActions.SetoutGroups(host);
- host.CreateSetupSeparator();
- host.CreateSetupAction(
- new PanelAction()
- {
- Caption = "Setouts Configuration",
- Image = PRSDesktop.Resources.specifications,
- OnExecute = ConfigSettingsClick
- }
- );
- host.CreateSetupAction(
- new PanelAction()
- {
- Caption = "Component Import Profiles",
- Image = PRSDesktop.Resources.doc_xls,
- OnExecute = ConfigComponentProfiles
- });
- host.CreateSetupAction(
- new PanelAction()
- {
- Caption = "Template Products",
- Image = PRSDesktop.Resources.specifications,
- OnExecute =
- action =>
- {
- var list = new MasterList(typeof(ManufacturingTemplateGroupProducts));
- list.ShowDialog();
- }
- }
- );
-
- SystemSetupActions.ERPStatuses(host);
-
- }
- private void ConfigComponentProfiles(PanelAction obj)
- {
- var list = new DynamicImportList(
- typeof(StagingSetoutComponent),
- Guid.Empty,
- canImport: false
- );
- list.ShowDialog();
- }
- private void ConfigSettingsClick(PanelAction obj)
- {
- var grid = new DynamicItemsListGrid<StagingPanellSettings>();
- grid.OnCustomiseEditor += Grid_OnCustomiseEditor;
- if(grid.EditItems(new[] { _settings }))
- {
- new GlobalConfiguration<StagingPanellSettings>().Save(_settings);
- _script = null;
- }
- }
- private void Grid_OnCustomiseEditor(IDynamicEditorForm sender, StagingPanellSettings[]? items, DynamicGridColumn column, BaseEditor editor)
- {
- if (items?.FirstOrDefault() is not StagingPanellSettings settings) return;
- if (column.ColumnName == nameof(StagingPanellSettings.Script) && editor is ScriptEditor scriptEditor)
- {
- scriptEditor.Type = ScriptEditorType.TemplateEditor;
- scriptEditor.OnEditorClicked += () =>
- {
- var script = settings.Script.NotWhiteSpaceOr()
- ?? settings.DefaultScript();
- var editor = new ScriptEditorWindow(script, SyntaxLanguage.CSharp);
- if (editor.ShowDialog() == true)
- {
- sender.SetEditorValue(column.ColumnName, editor.Script);
- settings.Script = editor.Script;
- }
- };
- }
- }
- #endregion
- public void Heartbeat(TimeSpan time)
- {
- }
- public void Refresh()
- {
- //stagingSetoutGrid.ScanFiles(_settings.SetoutsFolder);
- refreshing = true;
- stagingSetoutGrid.Refresh(false, true);
- /*Document = null;
- selectedSetout = null;
- ManufacturingPacketList.Setout = null;
- SetoutComponentGrid.StagingSetout = null;*/
- CalculateTime();
- }
- private void stagingSetoutGrid_OnRefreshPackets()
- {
- if (CanViewPackets())
- {
- ManufacturingPacketList.Refresh();
- }
- }
- public Dictionary<string, object[]> Selected()
- {
- return new();
- }
- public void Shutdown(CancelEventArgs? cancel)
- {
- }
- public DataModel DataModel(Selection selection)
- {
- return new AutoDataModel<StagingSetout>(new Filter<StagingSetout>().All());
- }
- private void AddPacketButton_Click(object sender, RoutedEventArgs e)
- {
- if (_templateGroups.Rows.Any() == true)
- {
- ContextMenu menu = new ContextMenu();
- foreach (var row in _templateGroups.Rows)
- {
- menu.AddItem(
- $"{row.Get<ManufacturingTemplateGroup, String>(x => x.Code)}: {row.Get<ManufacturingTemplateGroup, String>(x => x.Description)}",
- null,
- () =>
- {
- ManufacturingPacketList.Add(
- selectedSetout?.JobLink.ID ?? Guid.Empty,
- row.ToObject<ManufacturingTemplateGroup>()
- );
- UpdateStagingSetoutGrid();
- });
- }
- menu.AddSeparator();
- menu.AddItem("Miscellaneous Item", null, () =>
- {
- ManufacturingPacketList.Add(
- selectedSetout?.JobLink.ID ?? Guid.Empty,
- null
- );
- UpdateStagingSetoutGrid();
- });
- menu.IsOpen = true;
- }
- else
- {
- ManufacturingPacketList.Add(
- selectedSetout?.JobLink.ID ?? Guid.Empty,
- null
- );
- UpdateStagingSetoutGrid();
- }
- }
- private void UpdateStagingSetoutGrid()
- {
- var selected = stagingSetoutGrid.SelectedRows.FirstOrDefault();
- if (selected != null)
- {
- var packets = ManufacturingPacketList.Packets;
- selected.Set<StagingSetout, int>(x => x.Packets, packets.Length);
- selected.Set<StagingSetout, int>(x => x.UnprocessedPackets, packets.Count(x => x.ManufacturingPacket.ID == Guid.Empty));
- stagingSetoutGrid.InvalidateRow(selected);
- }
- }
- private void CollapsePacketsButton_Click(object sender, RoutedEventArgs e)
- {
- if (ManufacturingPacketList.Collapsed())
- {
- ManufacturingPacketList.Uncollapse();
- }
- else
- {
- ManufacturingPacketList.Collapse();
- }
- }
- private void ManufacturingPacketList_OnCollapsed(bool collapsed)
- {
- if (collapsed)
- {
- CollapsePacketsButton.Content = "Expand";
- }
- else
- {
- CollapsePacketsButton.Content = "Collapse";
- }
- }
- private void stagingSetoutGrid_OnCustomiseSetouts(IReadOnlyList<StagingSetoutGrid.SetoutDocument> setouts)
- {
- if(CustomiseSetoutsMethod != null && ScriptObject != null)
- {
- CustomiseSetoutsMethod?.Invoke(ScriptObject, new object?[]
- {
- new CustomiseSetoutsArgs(setouts.Select(x => new Tuple<StagingSetout, Document>(x.Setout, x.Document)).ToImmutableList())
- });
- }
- }
- private void StagingSetoutGrid_OnOnDoubleClick(object sender, HandledEventArgs args)
- {
- ManufacturingPacketList.Setout = selectedSetout;
- SetoutComponentGrid.StagingSetout = selectedSetout;
- MainPanel.View = DynamicSplitPanelView.Detail;
- NestedPanel.View = DynamicSplitPanelView.Combined;
- args.Handled = true;
- }
- private void CalculateTime()
- {
- if (selectedSetout != null)
- {
- var time = ManufacturingPacketList.TimeRequired();
- TimeRequired.Content = $"{time.TotalHours:F2} hours";
- }
- else
- TimeRequired.Content = "N/A";
- }
- private void ManufacturingPacketList_OnChanged(object? sender, EventArgs e)
- {
- CalculateTime();
- UpdateStagingSetoutGrid();
- }
- private void DoImport(Importer importer, string? componentFileName, Guid setoutID)
- {
- var success = DynamicImportGrid.CreateImporter(importer, ref componentFileName, out var iimporter);
- if (!success)
- {
- return;
- }
- var errors = new List<string>();
- var stagingSetoutComponents = new List<StagingSetoutComponent>();
- iimporter.OnLoad += Iimporter_OnLoad;
- iimporter.OnSave += (_, entity) => stagingSetoutComponents.Add((entity as StagingSetoutComponent)!);
- iimporter.OnError += (_, error) => errors.Add(error);
- using var stream = new FileStream(componentFileName!, FileMode.Open, FileAccess.Read);
- if (iimporter.Open(stream))
- {
- if (iimporter.ReadHeader())
- {
- var mismatches = iimporter.Mappings.Where(x =>
- !string.IsNullOrWhiteSpace(x.Field) &&
- !iimporter.Fields.Contains(x.Field)
- ).Select(x => x.Field).ToArray();
- if (!mismatches.Any())
- {
- var imported = iimporter.Import();
- if (errors.Any())
- {
- MessageBox.Show($"Import for component file {componentFileName} failed:\nSome errors occurred: {string.Join('\n', errors)}", "Import failed");
- }
- else
- {
- var valid = true;
- var conflicts = false;
- if (setoutID != Guid.Empty)
- {
- var newComponents = new List<StagingSetoutComponent>();
- foreach (var component in stagingSetoutComponents)
- {
- if (component.StagingSetout.ID == Guid.Empty)
- {
- component.StagingSetout.ID = setoutID;
- newComponents.Add(component);
- }
- else if (component.StagingSetout.ID != setoutID)
- {
- conflicts = true;
- // Ignoring this item.
- }
- else
- {
- newComponents.Add(component);
- }
- }
- stagingSetoutComponents = newComponents;
- if (conflicts)
- {
- MessageBox.Show($"Warning: the lines in this file have conflicting setout numbers.", "Warning");
- }
- }
- if (valid)
- {
- foreach (var component in stagingSetoutComponents)
- {
- if (component.StagingSetout.ID == Guid.Empty)
- {
- MessageBox.Show($"Component with no related setout cannot be imported.");
- valid = false;
- break;
- }
- else if (component.Description.IsNullOrWhiteSpace())
- {
- MessageBox.Show($"Component with no description cannot be imported.");
- valid = false;
- break;
- }
- else if (component.Dimensions.Unit.ID == Guid.Empty)
- {
- MessageBox.Show($"Component with no dimensions unit cannot be imported.");
- valid = false;
- break;
- }
- }
- }
- if (valid)
- {
- new Client<StagingSetoutComponent>().Save(stagingSetoutComponents, $"Imported from {componentFileName}");
- SetoutComponentGrid.Refresh(false, true);
- }
- else
- {
- MessageBox.Show($"Import for component file {componentFileName} failed.", "Import failed");
- }
- }
- }
- else
- {
- MessageBox.Show("Import Mappings do not match file headers!\n\n- " + string.Join("\n- ", mismatches),
- "Import Failed");
- }
- }
- else
- {
- MessageBox.Show("Unable to Read Headers from {0}", Path.GetFileName(componentFileName));
- }
- }
- else
- {
- MessageBox.Show("Unable to Open {0}", Path.GetFileName(componentFileName));
- }
- iimporter.Close();
- }
- private CoreTable Iimporter_OnLoad(object sender, Type type, string[] fields, string ID)
- {
- var result = new CoreTable();
- result.LoadColumns(Columns.None<StagingSetoutComponent>().Add(fields));
- return result;
- }
- private void stagingSetoutGrid_OnParseComponentFile(string componentFileName, Guid setoutID)
- {
- try
- {
- var entityName = typeof(StagingSetoutComponent).EntityName();
- var importers = new Client<Importer>()
- .Query(
- new Filter<Importer>(x => x.EntityName).IsEqualTo(entityName),
- Columns.None<Importer>().Add(x => x.ID));
- if (importers.Rows.Count == 0)
- {
- var importer = new Importer
- {
- EntityName = entityName,
- FileName = componentFileName
- };
- var form = new DynamicImportForm(importer);
- if (form.ShowDialog() == true)
- {
- new Client<Importer>().Save(importer, "");
- DoImport(importer, componentFileName, setoutID);
- return;
- }
- }
- else if (importers.Rows.Count == 1)
- {
- var importer = new Client<Importer>().Load(new Filter<Importer>(x => x.ID).IsEqualTo(importers.Rows[0].Get<Importer, Guid>(x => x.ID))).First();
- DoImport(importer, componentFileName, setoutID);
- }
- else
- {
- var list = new PopupList(
- typeof(Importer),
- Guid.Empty,
- Array.Empty<string>());
- list.OnDefineFilter += t => new Filter<Importer>(x => x.EntityName).IsEqualTo(entityName);
- if (list.ShowDialog() == true)
- {
- var importer = new Client<Importer>().Load(new Filter<Importer>(x => x.ID).IsEqualTo(list.ID)).First();
- DoImport(importer, componentFileName, setoutID);
- }
- }
- }
- catch(Exception e)
- {
- Logger.Send(LogType.Error, "", $"Error in file {componentFileName}: {CoreUtils.FormatException(e)}");
- MessageBox.Show($"Error opening {componentFileName}: {e.Message}");
- }
- }
- }
- }
|