|
@@ -9,60 +9,92 @@ using InABox.Database;
|
|
|
|
|
|
namespace PRSStores;
|
|
|
|
|
|
+using HoldingDictionary = Dictionary<(Guid product, Guid style, Guid location, Guid job, StockDimensions dimensions), StockHolding>;
|
|
|
+
|
|
|
public class StockMovementStore : BaseStore<StockMovement>
|
|
|
{
|
|
|
- protected override void BeforeSave(StockMovement sm)
|
|
|
- {
|
|
|
- base.BeforeSave(sm);
|
|
|
-
|
|
|
- // If this movement is an Update (instead of Insert),
|
|
|
- // we need to reduce the old stock holding before updating the new one
|
|
|
- if (sm.ID != Guid.Empty)
|
|
|
- StockHoldingStore.UpdateStockHolding(this, sm.ID,StockHoldingStore.Action.Decrease);
|
|
|
+ // These will be initialised in BeforeSave
|
|
|
+ HoldingDictionary holdingData = null!;
|
|
|
+ StockMovement[] mvtData = null!;
|
|
|
|
|
|
- if(sm.Job.HasOriginalValue(x => x.ID) && sm.Job.ID != Guid.Empty && sm.JobScope.ID == Guid.Empty)
|
|
|
+ protected override void BeforeSave(IEnumerable<StockMovement> entities)
|
|
|
+ {
|
|
|
+ foreach(var entity in entities)
|
|
|
{
|
|
|
- // 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.
|
|
|
+ // Calling base BeforeSave so that it doesn't call our other BeforeSave method.
|
|
|
+ base.BeforeSave(entity);
|
|
|
+ }
|
|
|
|
|
|
- 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)
|
|
|
+ mvtData = StockHoldingStore.LoadMovementData(this, entities.Select(x => x.ID).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)
|
|
|
{
|
|
|
- // 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;
|
|
|
+ // 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 AfterSave(StockMovement sm)
|
|
|
+ protected override void BeforeSave(StockMovement sm)
|
|
|
+ {
|
|
|
+ BeforeSave(CoreUtils.One(sm));
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void AfterSave(IEnumerable<StockMovement> entities)
|
|
|
{
|
|
|
// Update the Relevant StockHolding with the details of this movement
|
|
|
- StockHoldingStore.UpdateStockHolding(this, sm.ID,StockHoldingStore.Action.Increase);
|
|
|
-
|
|
|
- // Update the Job requisition item status (if applicable)
|
|
|
- if (sm.JobRequisitionItem.ID != Guid.Empty)
|
|
|
- JobRequisitionItemStore.UpdateStatus(
|
|
|
- this,
|
|
|
- sm.JobRequisitionItem.ID,
|
|
|
- sm.HasOriginalValue(x=>x.ID)
|
|
|
- ? JobRequisitionItemAction.Created
|
|
|
- : JobRequisitionItemAction.Updated
|
|
|
- );
|
|
|
- base.AfterSave(sm);
|
|
|
+ StockHoldingStore.ModifyHoldings(mvtData, holdingData, StockHoldingStore.Action.Increase);
|
|
|
+ StockHoldingStore.SaveHoldings(this, holdingData);
|
|
|
+
|
|
|
+ foreach(var sm in entities)
|
|
|
+ {
|
|
|
+ // Update the Job requisition item status (if applicable)
|
|
|
+ if (sm.JobRequisitionItem.ID != Guid.Empty)
|
|
|
+ JobRequisitionItemStore.UpdateStatus(
|
|
|
+ this,
|
|
|
+ sm.JobRequisitionItem.ID,
|
|
|
+ sm.HasOriginalValue(x=>x.ID)
|
|
|
+ ? JobRequisitionItemAction.Created
|
|
|
+ : JobRequisitionItemAction.Updated
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)
|