| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 | 
							- using Comal.Classes;
 
- using Comal.Stores;
 
- using InABox.Core;
 
- using System.Collections.Generic;
 
- using System.Linq;
 
- using System.Text;
 
- using System;
 
- using InABox.Database;
 
- namespace PRSStores;
 
- using HoldingDictionary = Dictionary<(Guid product, Guid style, Guid location, Guid job, StockDimensions dimensions), StockHolding>;
 
- public class StockMovementStore : BaseStore<StockMovement>
 
- {
 
-     // These will be initialised in BeforeSave
 
-     HoldingDictionary holdingData = null!;
 
-     StockMovement[] mvtData = null!;
 
-     HashSet<Guid> currentIDs = new();
 
-     protected override void BeforeSave(IEnumerable<StockMovement> entities)
 
-     {
 
-         foreach(var entity in entities)
 
-         {
 
-             // Calling base BeforeSave so that it doesn't call our other BeforeSave method.
 
-             base.BeforeSave(entity);
 
-         }
 
-         currentIDs = entities.Select(x => x.ID).Where(x => x != Guid.Empty).ToHashSet();
 
-         mvtData = StockHoldingStore.LoadMovementData(this, currentIDs.ToArray());
 
-         holdingData = StockHoldingStore.LoadStockHoldings(this, mvtData);
 
-         StockHoldingStore.ModifyHoldings(mvtData, holdingData, StockHoldingStore.Action.Decrease);
 
-         foreach(var sm in entities)
 
-         {
 
-             if(sm.Job.HasOriginalValue(x => x.ID) && sm.Job.ID != Guid.Empty && sm.JobScope.ID == Guid.Empty)
 
-             {
 
-                 // If we have updated the Job.ID to a non-empty value, we should
 
-                 // update the JobScope to the default job scope for that job.
 
-                 var scopeID = Guid.Empty;
 
-                 if(sm.ID != Guid.Empty)
 
-                 {
 
-                     // It's possible that the JobScope ID just wasn't passed up, if
 
-                     // this entity already exists; hence, we shall load the scopeID
 
-                     // from the database just in case.
 
-                     scopeID = Provider.Query(
 
-                         new Filter<StockMovement>(x => x.ID).IsEqualTo(sm.ID),
 
-                         Columns.None<StockMovement>().Add(x => x.JobScope.ID))
 
-                         .Rows.FirstOrDefault()?.Get<StockMovement, Guid>(x => x.JobScope.ID) ?? Guid.Empty;
 
-                 }
 
-                 if(scopeID == Guid.Empty)
 
-                 {
 
-                     // No scope has been assigned; however, we have a job, so we
 
-                     // load the default scope for the job.
 
-                     sm.JobScope.ID = Provider.Query(
 
-                         new Filter<Job>(x => x.ID).IsEqualTo(sm.Job.ID),
 
-                         Columns.None<Job>().Add(x => x.DefaultScope.ID))
 
-                         .Rows.FirstOrDefault()?.Get<Job, Guid>(x => x.DefaultScope.ID) ?? Guid.Empty;
 
-                 }
 
-             }
 
-         }
 
-     }
 
-     protected override void BeforeSave(StockMovement sm)
 
-     {
 
-         BeforeSave(CoreUtils.One(sm));
 
-     }
 
-     protected override void AfterSave(IEnumerable<StockMovement> entities)
 
-     {
 
-         // 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.
 
-         var newMvts = StockHoldingStore.LoadMovementData(this, newIDs);
 
-         // Load all the stock holdings for these movements, but only if we haven't loaded them already.
 
-         StockHoldingStore.LoadStockHoldings(this, newMvts, holdingData);
 
-         // Add the new movement data to our data. Note that the above line does the same for holdings.
 
-         mvtData = mvtData.Concatenate(newMvts);
 
-         
 
-         // Update the Relevant StockHolding with the details of this movement
 
-         StockHoldingStore.ModifyHoldings(mvtData, holdingData, StockHoldingStore.Action.Increase);
 
-         StockHoldingStore.SaveHoldings(this, holdingData);
 
-         
 
-         foreach(var entity in entities)
 
-         {
 
-             // Calling base AfterSave so that it doesn't call our other AfterSave method.
 
-             base.AfterSave(entity);
 
-         }
 
-     }
 
-     protected override void AfterSave(StockMovement sm)
 
-     {
 
-         AfterSave(CoreUtils.One(sm));
 
-     }
 
-     protected override void BeforeDelete(StockMovement entity)
 
-     {
 
-         base.BeforeDelete(entity);
 
-         
 
-         // We need to do this in before delete, because otherwise we wont have
 
-         // the data that we need to pull to properly update the stockholdings
 
-         StockHoldingStore.UpdateStockHolding(this, entity.ID,StockHoldingStore.Action.Decrease);
 
-         
 
-         // Now let's pull the requisition ID, so that we can clean this up
 
-         // properly in AfterDelete()
 
-         entity.JobRequisitionItem.ID = Provider.Query(
 
-                 new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.ID),
 
-                 Columns.None<StockMovement>().Add(x => x.JobRequisitionItem.ID)
 
-             ).Rows
 
-             .FirstOrDefault()?
 
-             .Get<StockMovement, Guid>(x => x.JobRequisitionItem.ID) ?? Guid.Empty;
 
-     }
 
-     
 
- }
 
 
  |