Explorar o código

Added superceding document on staging grid when completing mark-up; added "Add" button to add a new setout document

Kenric Nugteren hai 1 semana
pai
achega
c5f78e804f

+ 93 - 38
prs.desktop/Panels/Staging/Setouts/StagingSetoutGrid.cs

@@ -31,6 +31,7 @@ using LogikalDrawingFormat = Comal.Classes.LogikalDrawingFormat;
 using PdfGraphics = InABox.Dxf.PdfGraphics;
 using PRSDimensionUtils;
 using System.Diagnostics.CodeAnalysis;
+using InABox.Wpf.Editors;
 
 namespace PRSDesktop;
 
@@ -1146,14 +1147,19 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
     }
 
     private void ImportDesignFiles()
+    {
+        ImportDesignFiles(allowComponents: true, AddDocuments, doRefresh: true);
+    }
+
+    private void ImportDesignFiles(bool allowComponents, Action<IEnumerable<Tuple<string, Document>>> addAction, bool doRefresh)
     {
         var dlg = new OpenFileDialog
         {
             Multiselect = true,
             Title = "Add Staging Files",
-            Filter = "PDF and Component Files (*.pdf;*.xls;*.xlsx;*.csv;*.txt)|*.pdf;*.xls;*.xlsx;*.csv;*.txt|" +
+            Filter = (allowComponents ? "PDF and Component Files (*.pdf;*.xls;*.xlsx;*.csv;*.txt)|*.pdf;*.xls;*.xlsx;*.csv;*.txt|" : "") +
                      "PDF Files (*.pdf)|*.pdf|" +
-                     "Component Files (*.xls;*.xlsx;*.csv;*.txt)|*.xls;*.xlsx;*.csv;*.txt|" +
+                     (allowComponents ? "Component Files (*.xls;*.xlsx;*.csv;*.txt)|*.xls;*.xlsx;*.csv;*.txt|" : "") +
                      "Image Files (*.bmp;*.png;*.jpg;*.jpeg)|*.bmp;*.png;*.jpg;*.jpeg|" +
                      "DXF Files (*.dxf)|*.dxf|" +
                      "All Files (*.*)|*.*"
@@ -1185,16 +1191,22 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
                 }
             }
 
-            AddPDFFiles(pdfs, doRefresh: false);
-            AddImageFiles(images, doRefresh: false);
-            AddDXFFiles(dxfs, doRefresh: false);
-            AddComponentFiles(components, Guid.Empty);
+            AddPDFFiles(pdfs, addAction);
+            AddImageFiles(images, addAction);
+            AddDXFFiles(dxfs, addAction);
+            if (allowComponents)
+            {
+                AddComponentFiles(components, Guid.Empty);
+            }
 
-            Refresh(false, true);
+            if (doRefresh)
+            {
+                Refresh(false, true);
+            }
         }
     }
 
-    private void AddDocuments(IEnumerable<Tuple<string, Document>> documents, bool doRefresh)
+    private void AddDocuments(IEnumerable<Tuple<string, Document>> documents)
     {
         if (!documents.Any())
             return;
@@ -1285,16 +1297,11 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
         {
             Client.Delete(setoutsToDelete, "Replaced by new setout");
         }
-
-        if (doRefresh)
-        {
-            Refresh(false, true);
-        }
     }
 
-    public void AddDXFFiles(IEnumerable<string> fileNames, bool doRefresh = true)
+    public void AddDXFFiles(IEnumerable<string> fileNames, Action<IEnumerable<Tuple<string, Document>>> addAction)
     {
-        AddDocuments(fileNames.Select(x =>
+        addAction(fileNames.Select(x =>
         {
             var dxf = DxfUtils.LoadDxf(x);
             var pdf = DxfUtils.ProcessPdf(dxf);
@@ -1308,10 +1315,10 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
                     CRC = CoreUtils.CalculateCRC(data),
                     TimeStamp = new FileInfo(x).LastWriteTime
                 });
-        }), doRefresh: doRefresh);
+        }));
     }
 
-    public void AddPDFFiles(IEnumerable<string> fileNames, bool doRefresh = true)
+    public void AddPDFFiles(IEnumerable<string> fileNames, Action<IEnumerable<Tuple<string, Document>>> addAction)
     {
         var cancel = new CancelEventArgs();
 
@@ -1333,15 +1340,15 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
             }
         }
 
-        AddDocuments(fileNames.Select(x =>
+        addAction(fileNames.Select(x =>
         {
             return new Tuple<string, Document>(x, Document.FromFile(x));
-        }), doRefresh: doRefresh);
+        }));
     }
 
-    public void AddImageFiles(IEnumerable<string> fileNames, bool doRefresh = true)
+    public void AddImageFiles(IEnumerable<string> fileNames, Action<IEnumerable<Tuple<string, Document>>> addAction)
     {
-        AddDocuments(fileNames.Select(x =>
+        addAction(fileNames.Select(x =>
         {
             var pdf = DataEntryReGroupWindow.RenderToPDF(x);
             var data = pdf.SaveToBytes();
@@ -1354,7 +1361,7 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
                     CRC = CoreUtils.CalculateCRC(data),
                     TimeStamp = new FileInfo(x).LastWriteTime
                 });
-        }), doRefresh: doRefresh);
+        }));
     }
 
     public void AddComponentFiles(IEnumerable<string> fileNames, Guid setoutID)
@@ -1365,22 +1372,6 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
         }
     }
 
-    /// <summary>
-    /// Requires the fully qualified file name
-    /// </summary>
-    /// <param name="file"></param>
-    /// <returns></returns>
-    private static Document ReadFile(string file)
-    {
-        var doc = new Document
-        {
-            FileName = Path.GetFileName(file),
-            Data = GetData(file)
-        };
-        doc.CRC = CoreUtils.CalculateCRC(doc.Data);
-        return doc;
-    }
-
     private static byte[] GetData(string file)
     {
         Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
@@ -1390,6 +1381,70 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
         return pdfData;
     }
 
+    /// <summary>
+    /// Upload a bunch of documents, and return unsaved <see cref="SetoutDocument"/>s, that will need to be linked to a setout.
+    /// </summary>
+    public IList<SetoutDocument> UploadNew()
+    {
+        var docs = new List<Tuple<string, Document>>();
+        ImportDesignFiles(allowComponents: false, doRefresh: false, addAction: docs.AddRange);
+
+        Client.Save(docs.Select(x => x.Item2), "Uploaded to setout");
+
+        return docs.ToArray(doc =>
+        {
+            var newDoc = new SetoutDocument();
+            newDoc.DocumentLink.CopyFrom(doc.Item2);
+            newDoc.Staging.OriginalPath = doc.Item1;
+            newDoc.Staging.OriginalCRC = doc.Item2.CRC;
+            return newDoc;
+        });
+    }
+
+    public static void SupercedeFile(SetoutDocument setoutDocument, Setout setout)
+    {
+        if (string.IsNullOrWhiteSpace(setoutDocument.Staging.SavePath))
+            return;
+        
+        try
+        {
+            var file = new FileInfo(setoutDocument.Staging.SavePath);
+            var doc = Document.FromFile(setoutDocument.Staging.SavePath);
+
+            var filename = $"{Path.GetFileNameWithoutExtension(doc.FileName)}_Revision{Path.GetExtension(doc.FileName)}";
+            if(TextEdit.Execute("New filename: ", ref filename))
+            {
+                doc.FileName = filename;
+            }
+            else
+            {
+                MessageWindow.ShowMessage("Process cancelled.", "Cancelled");
+                return;
+            }
+
+            setoutDocument.Staging.SavePath = "";
+            setoutDocument.Staging.LockedBy.CopyFrom(new Employee());
+            setoutDocument.Superceded = DateTime.Now;
+
+            var newSetoutDocument = new SetoutDocument();
+            newSetoutDocument.EntityLink.CopyFrom(setout);
+            newSetoutDocument.Staging.OriginalPath = setoutDocument.Staging.OriginalPath;
+            newSetoutDocument.Staging.OriginalCRC = setoutDocument.Staging.OriginalCRC;
+
+            file.Attributes = FileAttributes.Normal;
+            file.Delete();
+
+            Client.Save(doc, "Superceded staging setout");
+
+            newSetoutDocument.DocumentLink.CopyFrom(doc);
+            Client.Save([newSetoutDocument, setoutDocument], "Superceded staging setout");
+        }
+        catch (Exception ex)
+        {
+            MessageWindow.ShowError("Error superceding document", ex);
+        }
+    }
+
     public static void ReloadFile(SetoutDocument setoutDocument)
     {
         if (string.IsNullOrWhiteSpace(setoutDocument.Staging.SavePath))

+ 82 - 34
prs.desktop/Panels/Staging/StagingPanel.xaml.cs

@@ -25,6 +25,7 @@ using Syncfusion.Data.Extensions;
 using System.Windows.Data;
 using System.Runtime.CompilerServices;
 using System.Diagnostics.CodeAnalysis;
+using System.Collections.ObjectModel;
 
 namespace PRSDesktop;
 
@@ -215,7 +216,7 @@ public partial class StagingPanel : UserControl, IPanel<Setout>
 
         public bool DocumentsLoaded { get; set; } = false;
 
-        public CoreObservableCollection<BitmapImage> Documents { get; private set; } = new();
+        public ObservableCollection<BitmapImage> Documents { get; private set; } = new();
 
         public DocumentTab(SetoutDocument document)
         {
@@ -327,6 +328,17 @@ public partial class StagingPanel : UserControl, IPanel<Setout>
     private void DocumentTabs_OnCreateTab(object sender, DynamicTabControlEventArgs args)
     {
         args.Cancel = true;
+        if (selectedSetout is null) return;
+
+        var setoutDocs = stagingSetoutGrid.UploadNew();
+        foreach(var setoutDoc in setoutDocs)
+        {
+            setoutDoc.EntityLink.CopyFrom(selectedSetout);
+        }
+        Client.Save(setoutDocs, "Uploaded to setout");
+
+        refreshing = true;
+        stagingSetoutGrid.Refresh(false, true);
     }
 
     private void TabItem_ContextMenuOpening(object sender, ContextMenuEventArgs e)
@@ -336,29 +348,7 @@ public partial class StagingPanel : UserControl, IPanel<Setout>
 
         e.Handled = true;
 
-        var menu = new ContextMenu();
-        if (tab.Document.Superceded.IsEmpty())
-        {
-            menu.AddItem("Supercede", null, tab, SupercedeDocument);
-        }
-        else
-        {
-            menu.AddItem("Set not superceded", null, tab, UnsupercedeDocument);
-        }
-        menu.IsOpen = true;
-    }
-
-
-    private void UnsupercedeDocument(DocumentTab tab)
-    {
-        tab.Superceded = false;
-        Client.Save(tab.Document, "Set document to not be superceded");
-    }
-
-    private void SupercedeDocument(DocumentTab tab)
-    {
-        tab.Superceded = true;
-        Client.Save(tab.Document, "Superceded document");
+        DocumentContextMenu(tab);
     }
 
     private void TabSelected(DocumentTab tab)
@@ -372,7 +362,10 @@ public partial class StagingPanel : UserControl, IPanel<Setout>
 
         if (!tab.DocumentsLoaded)
         {
-            tab.Documents.AddRange(RenderDocuments(GetDocuments(tab.Document)));
+            foreach(var doc in RenderDocuments(GetDocuments(tab.Document)))
+            {
+                tab.Documents.Add(doc);
+            }
             tab.DocumentsLoaded = true;
         }
 
@@ -384,15 +377,11 @@ public partial class StagingPanel : UserControl, IPanel<Setout>
         TabSelected(tab);
     }
 
-    private void Border_ContextMenuOpening(object sender, ContextMenuEventArgs e)
+    private void DocumentContextMenu(DocumentTab tab)
     {
-        if ((sender as FrameworkElement)?.Tag is not DocumentTab tab) return;
-
-        e.Handled = true;
-
         var menu = new ContextMenu();
 
-        if (Security.IsAllowed<CanMarkUpSetouts>())
+        if (Security.IsAllowed<CanMarkUpSetouts>() && tab == DocumentTabs.SelectedItem)
         {
             if(_mode == DocumentMode.Markup)
             {
@@ -406,15 +395,47 @@ public partial class StagingPanel : UserControl, IPanel<Setout>
             else if(_mode == DocumentMode.Complete)
             {
                 menu.AddItem("Complete", null, tab.Document, Complete_Click);
+                menu.AddItem("Cancel Mark Up", null, tab.Document, CancelMarkUp_Click);
             }
         }
 
+        menu.AddSeparatorIfNeeded();
+        if (tab.Document.Superceded.IsEmpty())
+        {
+            menu.AddItem("Supercede", null, tab, SupercedeDocument);
+        }
+        else
+        {
+            menu.AddItem("Set not superceded", null, tab, UnsupercedeDocument);
+        }
+
         if(menu.Items.Count > 0)
         {
             menu.IsOpen = true;
         }
     }
 
+    private void Border_ContextMenuOpening(object sender, ContextMenuEventArgs e)
+    {
+        if ((sender as FrameworkElement)?.Tag is not DocumentTab tab) return;
+
+        e.Handled = true;
+
+        DocumentContextMenu(tab);
+    }
+
+    private void UnsupercedeDocument(DocumentTab tab)
+    {
+        tab.Superceded = false;
+        Client.Save(tab.Document, "Set document to not be superceded");
+    }
+
+    private void SupercedeDocument(DocumentTab tab)
+    {
+        tab.Superceded = true;
+        Client.Save(tab.Document, "Superceded document");
+    }
+
     private void ApproveAllButton_Click(object sender, RoutedEventArgs e)
     {
         ManufacturingPacketList.ApproveAll();
@@ -429,9 +450,27 @@ public partial class StagingPanel : UserControl, IPanel<Setout>
         OnMarkupSelected(document);
     }
 
+    private void CancelMarkUp_Click(SetoutDocument document)
+    {
+        document.Staging.SavePath = "";
+        document.Staging.LockedBy.CopyFrom(new Employee());
+        Client.Save(document, "Cancelled mark up");
+        refreshing = true;
+        stagingSetoutGrid.Refresh(false, true);
+    }
+
     private void Complete_Click(SetoutDocument document)
     {
-        OnMarkupComplete(document);
+        var result = MessageWindow.New()
+            .Title("Supercede?")
+            .Message("Do you wish to supercede the old document, or replace it?")
+            .AddYesButton("Supercede")
+            .AddNoButton("Replace")
+            .AddCancelButton()
+            .Display()
+            .Result;
+        if (result == MessageWindowResult.Cancel) return;
+        OnMarkupComplete(document, result == MessageWindowResult.Yes);
         Mode = DocumentMode.Markup;
     }
 
@@ -492,9 +531,18 @@ public partial class StagingPanel : UserControl, IPanel<Setout>
         refreshing = true;
         stagingSetoutGrid.Refresh(false, true);
     }
-    private void OnMarkupComplete(SetoutDocument _document)
+    private void OnMarkupComplete(SetoutDocument _document, bool supercede)
     {
-        StagingSetoutGrid.ReloadFile(_document);
+        if (selectedSetout is null) return;
+
+        if (supercede)
+        {
+            StagingSetoutGrid.SupercedeFile(_document, selectedSetout);
+        }
+        else
+        {
+            StagingSetoutGrid.ReloadFile(_document);
+        }
         refreshing = true;
         stagingSetoutGrid.Refresh(false, true);
     }