|
@@ -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.
|