123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- 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;
- namespace PRSDesktop
- {
- 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; }
- public StagingPanellSettings()
- {
- MarkupPathway = "";
- SetoutsFolder = "";
- }
- }
- /// <summary>
- /// Interaction logic for StagingPanel.xaml
- /// </summary>
- public partial class StagingPanel : UserControl, IPanel<StagingSetout>
- {
- private StagingPanellSettings _settings = new StagingPanellSettings();
- StagingSetout _item = new StagingSetout();
- List<StagingSetout> _items = new List<StagingSetout>();
- DynamicDataGrid<Document> _materialDocs;
- public StagingPanel()
- {
- InitializeComponent();
- SectionName = nameof(StagingPanel);
- }
- private void DocumentPreviewer_OnApproved(IEntityDocument stagingsetoutdocument)
- {
- string message = "";
- foreach (var item in _items)
- {
- var returnstring = ApproveSetout(item);
- if (!string.IsNullOrWhiteSpace(returnstring))
- message = message + returnstring + Environment.NewLine;
- }
- foreach (var item in _items)
- DeleteAndRefresh(item);
- MessageBox.Show(message);
- }
- private string ApproveSetout(StagingSetout item)
- {
- 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 setoutdoctable = new Client<StagingSetoutDocument>().Query(
- new Filter<StagingSetoutDocument>(x => x.EntityLink.ID).IsEqualTo(item.ID),
- new Columns<StagingSetoutDocument>(x => x.ID, x => x.DocumentLink.ID, x => x.DocumentLink.FileName));
- if (!setoutdoctable.Rows.Any())
- return "";
- var stagingsetoutdocument = setoutdoctable.Rows.FirstOrDefault().ToObject<SetoutDocument>();
- var table = new Client<Setout>().Query(new Filter<Setout>(x => x.Number).IsEqualTo(item.Number),
- new Columns<Setout>(x => x.ID));
- var setout = new Setout();
- //setout already exists - create new setoutdoc and supercede old ones
- if (table.Rows.Any())
- {
- if (MessageBox.Show("Supercede existing documents?", "Proceed?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
- return "";
- setout = table.Rows.FirstOrDefault().ToObject<Setout>();
- setout.Group.ID = item.Group.ID;
- item.Setout.ID = setout.ID;
- var setoutdoc = new SetoutDocument();
- setoutdoc.EntityLink.ID = setout.ID;
- setoutdoc.DocumentLink.ID = stagingsetoutdocument.DocumentLink.ID;
- List<SetoutDocument> setoutdocs = new List<SetoutDocument>
- {
- setoutdoc
- };
- CoreTable oldDocsTable = new Client<SetoutDocument>().Query(
- new Filter<SetoutDocument>(x => x.EntityLink.ID).IsEqualTo(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, "Updated from Staging Screen");
- return item.Number + " Superceded";
- }
- //no setout for this pdf - create new
- else
- {
- 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) =>
- {
- 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");
- };
- if (!editor.EditItems(new[] { setout }))
- {
- MessageBox.Show("Setout Creation Cancelled");
- return "";
- }
- else
- return item.Number + " Created";
- }
- //currently not creating packets due to temporary change in requirements - to uncomment and check for validity when required
- //CreatePackets(setout);
- }
- private void CreatePackets(Setout setout)
- {
- List<ManufacturingPacket> packets = new List<ManufacturingPacket>();
- List<StagingManufacturingPacket> stagingPackets = new List<StagingManufacturingPacket>();
- //create new manufacturing packets from the staging packets
- foreach (var row in manufacturingControl.Data.Rows)
- {
- var staging = row.ToObject<StagingManufacturingPacket>();
- if (staging.ManufacturingPacket.ID != Guid.Empty)
- continue;
- var packet = new ManufacturingPacket();
- packet.SetoutLink.ID = setout.ID;
- packet.Serial = staging.Serial;
- packet.ITP.ID = staging.ITP.ID;
- packet.JobLink.ID = staging.Job.ID;
- packet.ManufacturingTemplateLink.ID = staging.Template.ID;
- packet.Title = staging.Title;
- packet.Quantity = staging.Quantity;
- packet.BarcodeQty = staging.BarcodeQuantity != 0 ? staging.BarcodeQuantity : packet.Quantity;
- packet.WaterMark = staging.Watermark.ToString();
- packet.Location = staging.Location;
- packets.Add(packet);
- stagingPackets.Add(staging);
- }
- //save the newly created packets to provide an ID
- new Client<ManufacturingPacket>().Save(packets, "Created from Design Management Panel");
- //now work on their stages with the provided packet.ID
- List<ManufacturingPacketStage> stages = new List<ManufacturingPacketStage>();
- Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent> stagingToActualComponents = new Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent>();
- //Handled in this loop:
- // - staging packets (update packet.ID)
- // - creation of template stages
- // - creation of components from staging components
- foreach (var stagingPacket in stagingPackets)
- {
- var packet = packets.FirstOrDefault(x => x.Serial == stagingPacket.Serial);
- stagingPacket.ManufacturingPacket.ID = packet.ID;
- stages.AddRange(CreateStagesForTemplate(packet.ManufacturingTemplateLink.ID, packet.ID));
- CreateComponents(stagingPacket.ID, packet.ID, stagingToActualComponents);
- }
- //save everything
- MultiSave save = new MultiSave();
- save.Add(typeof(ManufacturingPacketStage), stages.ToArray());
- save.Add(typeof(StagingManufacturingPacket), stagingPackets.ToArray());
- save.Add(typeof(ManufacturingPacketComponent), stagingToActualComponents.Values.ToArray());
- save.Save(null, "Updated from setout staging screen");
- foreach (var pair in stagingToActualComponents)
- {
- var stagingComponent = pair.Key;
- stagingComponent.ComponentID = pair.Value.ID;
- }
- new Client<StagingManufacturingPacketComponent>().Save(stagingToActualComponents.Keys.ToArray(), "Updated from setout staging screen");
- }
- private void CreateComponents(Guid stagingPacketID, Guid packetID, Dictionary<StagingManufacturingPacketComponent, ManufacturingPacketComponent> stagingToActualComponents)
- {
- var components = new List<ManufacturingPacketComponent>();
- CoreTable table = new Client<StagingManufacturingPacketComponent>().Query(
- new Filter<StagingManufacturingPacketComponent>(x => x.StagingPacket.ID).IsEqualTo(stagingPacketID)
- .And(x => x.ComponentID).IsEqualTo(Guid.Empty),
- new Columns<StagingManufacturingPacketComponent>(
- x => x.Product.ID,
- x => x.Quantity,
- x => x.Length,
- x => x.Height,
- x => x.Width
- ));
- foreach (var row in table.Rows)
- {
- var stagingComponent = row.ToObject<StagingManufacturingPacketComponent>();
- var component = stagingComponent.CreateComponent(packetID);
- components.Add(component);
- stagingToActualComponents.Add(stagingComponent, component);
- }
- }
- private List<ManufacturingPacketStage> CreateStagesForTemplate(Guid templateID, Guid packetID)
- {
- var stages = new List<ManufacturingPacketStage>();
- CoreTable table = new Client<ManufacturingTemplateStage>().Query(
- new Filter<ManufacturingTemplateStage>(x => x.Template.ID).IsEqualTo(templateID),
- new Columns<ManufacturingTemplateStage>
- (
- x => x.Time,
- x => x.Sequence,
- x => x.SequenceType,
- x => x.Section.ID,
- x => x.Section.Name
- ));
- foreach (var row in table.Rows)
- {
- var templateStage = row.ToObject<ManufacturingTemplateStage>();
- var packetStage = templateStage.CreateManufacturingPacketStage();
- packetStage.ManufacturingPacketLink.ID = packetID;
- stages.Add(packetStage);
- }
- return stages;
- }
- private void DeleteAndRefresh(StagingSetout item)
- {
- new Client<StagingSetout>().Save(item, "Updated from staging screen");
- _item = new StagingSetout();
- documentPreviewer.Document = new StagingSetoutDocument();
- Refresh();
- }
- private void DocumentPreviewer_OnRejected(IEntityDocument stagingsetoutdocument)
- {
- //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
- Kanban task = new Kanban();
- task.Title = "Setout Review Task (setout rejected)";
- task.Description = "Please review the attached document for setout " + _item.Number;
- task.ManagerLink.ID = App.EmployeeID;
- var page = new KanbanGrid();
- page.MyID = App.EmployeeID;
- page.OnAfterSave += (editor, items) =>
- {
- Kanban savedTask = (Kanban)items[0];
- KanbanDocument doc = new KanbanDocument();
- doc.EntityLink.ID = savedTask.ID;
- doc.DocumentLink.ID = stagingsetoutdocument.DocumentLink.ID;
- new Client<KanbanDocument>().Save(doc, "Created from staging screen");
- _item.Task.ID = savedTask.ID;
- new Client<StagingSetout>().Save(_item, "Updated from staging screen");
- MessageBox.Show("Success - Task Created for Document " + _item.Number + " (Task no. " + savedTask.Number + " assigned to " + savedTask.EmployeeLink.Name + ")");
- _item = new StagingSetout();
- documentPreviewer.Document = new StagingSetoutDocument();
- stagingSetoutGrid.Refresh(false, true);
- };
- if (!page.EditItems(new[] { task }))
- MessageBox.Show("Task creation cancelled - setout not rejected");
- }
- private void StagingSetoutGrid_OnSelectItem(object sender, InABox.DynamicGrid.DynamicGridSelectionEventArgs e)
- {
- _items.Clear();
- foreach (var row in stagingSetoutGrid.SelectedRows)
- _items.Add(row.ToObject<StagingSetout>());
- _item = stagingSetoutGrid.SelectedRows.FirstOrDefault().ToObject<StagingSetout>();
- var doc = new Client<StagingSetoutDocument>().Query(
- new Filter<StagingSetoutDocument>(x => x.EntityLink.ID)
- .IsEqualTo(_item.ID),
- new Columns<StagingSetoutDocument>(x => x.ID, x => x.DocumentLink.ID, x => x.DocumentLink.FileName))
- .Rows.FirstOrDefault().ToObject<StagingSetoutDocument>();
- documentPreviewer.Document = doc;
- manufacturingControl.StagingSetout = _item;
- documentPreviewer.Mode =
- _item.Locked == DateTime.MinValue ? InABox.Wpf.DocumentApprovalControl.ControlMode.Markup :
- _item.Locked != DateTime.MinValue && _item.LockedBy.ID == App.EmployeeID ? InABox.Wpf.DocumentApprovalControl.ControlMode.Complete :
- _item.Locked != DateTime.MinValue && _item.LockedBy.ID != App.EmployeeID ? InABox.Wpf.DocumentApprovalControl.ControlMode.Locked :
- InABox.Wpf.DocumentApprovalControl.ControlMode.Markup;
- }
- public bool IsReady { get; set; }
- public string SectionName { get; }
- public event DataModelUpdateEvent? OnUpdateDataModel;
- public void CreateToolbarButtons(IPanelHost host)
- {
- host.CreateSetupAction(new PanelAction() { Caption = "Setouts Configuration", Image = PRSDesktop.Resources.specifications, OnExecute = ConfigSettingsClick });
- }
- private void ConfigSettingsClick(PanelAction obj)
- {
- //var editor = new ObjectEditor(_settings, "Setouts Configuration");
- //if (editor.ShowDialog() == true)
- //{
- // _settings = (StagingPanellSettings)editor.Input;
- // new GlobalConfiguration<StagingPanellSettings>().Save(_settings);
- //}
- var pages = new DynamicEditorPages();
- var propertyEditor = new DynamicEditorForm(typeof(StagingPanellSettings), pages);
- propertyEditor.OnDefineLookups += sender =>
- {
- var editor = sender.EditorDefinition as ILookupEditor;
- var colname = sender.ColumnName;
- var values = editor.Values(colname, new[] { _settings });
- sender.LoadLookups(values);
- };
- propertyEditor.OnEditorValueChanged += (sender, name, value) =>
- {
- CoreUtils.SetPropertyValue(_settings, name, value);
- return new Dictionary<string, object?>();
- };
- propertyEditor.OnFormCustomiseEditor += PropertyEditor_OnFormCustomiseEditor;
- propertyEditor.Items = new BaseObject[] { _settings };
- if (propertyEditor.ShowDialog() == true)
- {
- new GlobalConfiguration<StagingPanellSettings>().Save(_settings);
- }
- }
- private void PropertyEditor_OnFormCustomiseEditor(IDynamicEditorForm sender, object[] items, DynamicGridColumn column, BaseEditor editor)
- {
- }
- public void Heartbeat(TimeSpan time)
- {
- }
- public void Refresh()
- {
- //stagingSetoutGrid.ScanFiles(_settings.SetoutsFolder);
- stagingSetoutGrid.Refresh(false, true);
- manufacturingControl.StagingSetout = new StagingSetout();
- manufacturingControl.Refresh();
- }
- public Dictionary<string, object[]> Selected()
- {
- return new();
- }
- public void Setup()
- {
- _settings = new GlobalConfiguration<StagingPanellSettings>().Load();
- documentPreviewer.SetupButtons(
- markupVisible: Security.IsAllowed<CanMarkUpSetouts>(),
- rejectVisible: Security.IsAllowed<CanApproveSetouts>(),
- approveVisible: Security.IsAllowed<CanApproveSetouts>()
- );
- documentPreviewer.OnMarkupSelected += DocumentPreviewer_OnMarkupSelected;
- documentPreviewer.OnMarkupComplete += DocumentPreviewer_OnMarkupComplete;
- documentPreviewer.OnRejected += DocumentPreviewer_OnRejected;
- documentPreviewer.OnApproved += DocumentPreviewer_OnApproved;
- //stagingSetoutGrid.ScanFiles(_settings.SetoutsFolder);
- stagingSetoutGrid.Refresh(true, true);
- stagingSetoutGrid.OnSelectItem += StagingSetoutGrid_OnSelectItem;
- }
- private void DocumentPreviewer_OnMarkupSelected(IEntityDocument document)
- {
- var doc = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(document.DocumentLink.ID)).Rows.FirstOrDefault().ToObject<Document>();
- var tempdocpath = Path.GetTempPath() + @"\" + doc.FileName;
- _item.SavePath = tempdocpath;
- _item.LockedBy.ID = App.EmployeeID;
- new Client<StagingSetout>().Save(_item, "Locked from Staging Screen");
- File.WriteAllBytes(tempdocpath, doc.Data);
- using (Process p = new Process())
- {
- p.StartInfo = new ProcessStartInfo()
- {
- UseShellExecute = true,
- FileName = tempdocpath
- };
- p.Start();
- }
- stagingSetoutGrid.Refresh(false, true);
- }
- private void DocumentPreviewer_OnMarkupComplete(IEntityDocument document)
- {
- stagingSetoutGrid.ReloadFile(_item);
- stagingSetoutGrid.Refresh(false, true);
- }
- public void Shutdown()
- {
- }
- public DataModel DataModel(Selection selection)
- {
- return new AutoDataModel<StagingSetout>(null);
- }
- }
- }
|