فهرست منبع

Fix to stock movement store

Kenric Nugteren 4 ماه پیش
والد
کامیت
70c71ac0b2
2فایلهای تغییر یافته به همراه21 افزوده شده و 3 حذف شده
  1. 2 3
      prs.stores/StockHoldingStore.cs
  2. 19 0
      prs.stores/StockMovementStore.cs

+ 2 - 3
prs.stores/StockHoldingStore.cs

@@ -60,8 +60,7 @@ public class StockHoldingStore : BaseStore<StockHolding>
         foreach(var mvt in mvts)
         {
             var key = (mvt.Product.ID, mvt.Style.ID, mvt.Location.ID, mvt.Job.ID, mvt.Dimensions);
-            var holding = holdings.GetValueOrDefault(key);
-            if(holding is null)
+            if(!holdings.TryGetValue(key, out var holding))
             {
                 holding = new();
                 holding.Location.ID = mvt.Location.ID;
@@ -72,7 +71,7 @@ public class StockHoldingStore : BaseStore<StockHolding>
                 holdings[key] = holding;
             }
 
-            double multiplier = action == Action.Increase ? 1F : -1F;
+            var multiplier = action == Action.Increase ? 1F : -1F;
             holding.Units += (multiplier * mvt.Units);
             holding.Qty += (multiplier * mvt.Units * mvt.Dimensions.Value);
             holding.Value += (multiplier * mvt.Units * mvt.Cost);

+ 19 - 0
prs.stores/StockMovementStore.cs

@@ -28,6 +28,7 @@ public class StockMovementStore : BaseStore<StockMovement>
 
         currentIDs = entities.Select(x => x.ID).Where(x => x != Guid.Empty).ToHashSet();
 
+        // We load the movements according to the data currently in the database (i.e, before we've saved). This is to decrease the old holdings.
         mvtData = StockHoldingStore.LoadMovementData(this, currentIDs.ToArray());
         holdingData = StockHoldingStore.LoadStockHoldings(this, mvtData);
 
@@ -71,6 +72,24 @@ public class StockMovementStore : BaseStore<StockMovement>
 
     protected override void AfterSave(IEnumerable<StockMovement> entities)
     {
+        // Update the old movement data with the changes we've made.
+        foreach(var mvt in entities)
+        {
+            var dataMvt = mvtData.FirstOrDefault(x => x.ID == mvt.ID);
+            if (dataMvt is null) continue;
+
+            // Need to stop observing, because otherwise the order of property setting could and would break things.
+            dataMvt.SetObserving(false);
+            foreach(var (key, value) in mvt.OriginalValueList)
+            {
+                if(DatabaseSchema.Property(typeof(StockMovement), key) is IProperty prop)
+                {
+                    prop.Setter()(dataMvt, prop.Getter()(mvt));
+                }
+            }
+            dataMvt.SetObserving(true);
+        }
+
         // Find all movements that weren't loaded in BeforeSave - i.e., they are new stock movements.
         var newIDs = entities.Select(x => x.ID).Where(x => !currentIDs.Contains(x)).ToArray();
         // Grab the data for these movements.