瀏覽代碼

Created SetoutGroupStore to fix synchronisation issues of setout group optimisation documents.

Kenric Nugteren 1 年之前
父節點
當前提交
0050abc0b4

+ 1 - 0
prs.classes/Entities/Manufacturing/ManufacturingPacket/ManufacturingPacket.cs

@@ -173,6 +173,7 @@ namespace Comal.Classes
         [LoggableProperty]
         public string Issues { get; set; }
 
+        [NullEditor]
         public SetoutGroupLink SetoutGroup { get; set; }
 
         public override string ToString()

+ 1 - 0
prs.stores/PRSStores.projitems

@@ -58,6 +58,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)SchedulableStore.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ScheduleActionStore.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ScheduleStore.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)SetoutGroupStore.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)SetoutStore.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ShipmentStore.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)StagingSetoutStore.cs" />

+ 26 - 0
prs.stores/SetoutGroupStore.cs

@@ -0,0 +1,26 @@
+using Comal.Classes;
+using InABox.Core;
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Comal.Stores;
+
+public class SetoutGroupStore : BaseStore<SetoutGroup>
+{
+    protected override void AfterSave(SetoutGroup entity)
+    {
+        if(entity.OptimizationDocument.HasOriginalValue(x => x.ID))
+        {
+            var setoutStore = FindSubStore<Setout>();
+            var setouts = setoutStore.Query(
+                new Filter<Setout>(x => x.Group.ID).IsEqualTo(entity.ID),
+                new Columns<Setout>(x => x.ID).Add(x => x.Group.ID));
+
+            foreach(var setout in setouts.ToObjects<Setout>())
+            {
+                SetoutStore.UpdateOptimisationDocument(FindSubStore<SetoutDocument>, setout, entity.OptimizationDocument.ID);
+            }
+        }
+    }
+}

+ 29 - 17
prs.stores/SetoutStore.cs

@@ -7,6 +7,7 @@ using Syncfusion.Pdf.Parsing;
 using System.Drawing;
 using System.IO;
 using System.Drawing.Imaging;
+using InABox.Database;
 
 namespace Comal.Stores
 {
@@ -23,28 +24,39 @@ namespace Comal.Stores
                     return;
 
                 var docID = table.Rows.FirstOrDefault().Get<SetoutGroup, Guid>(x => x.OptimizationDocument.ID);
-                if (docID == Guid.Empty)
-                    return;
 
-                List<Guid> ids = new List<Guid>();
-                var docsTable = Provider.Query<SetoutDocument>(
-                    new Filter<SetoutDocument>(x => x.EntityLink.ID).IsEqualTo(entity.ID),
-                    new Columns<SetoutDocument>(x => x.DocumentLink.ID)
-                    );
-                foreach (var row in docsTable.Rows)
-                    ids.Add(row.Get<SetoutDocument, Guid>(x => x.DocumentLink.ID));
+                UpdateOptimisationDocument(FindSubStore<SetoutDocument>, entity, docID);
 
-                if (!ids.Contains(docID))
-                {
-                    var setoutdoc = new SetoutDocument();
-                    setoutdoc.EntityLink.ID = entity.ID;
-                    setoutdoc.DocumentLink.ID = docID;
+            }
+            base.AfterSave(entity);
+        }
 
-                    FindSubStore<SetoutDocument>().Save(setoutdoc, "Added optimsation document from Group");
-                }
+        /// <summary>
+        /// Update the optimisation document ID for a setout. The setout must have a non-empty <see cref="Entity.ID"/>.
+        /// </summary>
+        /// <param name="docStore"></param>
+        /// <param name="setout">Setout with non-empty <see cref="Entity.ID"/></param>
+        /// <param name="optimisationDocumentID"></param>
+        public static void UpdateOptimisationDocument(Func<IStore<SetoutDocument>> docStore, Setout setout, Guid optimisationDocumentID)
+        {
+            if (optimisationDocumentID == Guid.Empty) return;
 
+            var ids = new List<Guid>();
+            var docsTable = DbFactory.Provider.Query<SetoutDocument>(
+                new Filter<SetoutDocument>(x => x.EntityLink.ID).IsEqualTo(setout.ID),
+                new Columns<SetoutDocument>(x => x.DocumentLink.ID)
+                );
+            foreach (var row in docsTable.Rows)
+                ids.Add(row.Get<SetoutDocument, Guid>(x => x.DocumentLink.ID));
+
+            if (!ids.Contains(optimisationDocumentID))
+            {
+                var setoutdoc = new SetoutDocument();
+                setoutdoc.EntityLink.ID = setout.ID;
+                setoutdoc.DocumentLink.ID = optimisationDocumentID;
+
+                docStore().Save(setoutdoc, "Added optimsation document from Group");
             }
-            base.AfterSave(entity);
         }
 
         protected override void BeforeDelete(Setout entity)