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; public class StockMovementStore : BaseStore { 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); 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(x => x.ID).IsEqualTo(sm.ID), Columns.None().Add(x => x.JobScope.ID)) .Rows.FirstOrDefault()?.Get(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(x => x.ID).IsEqualTo(sm.Job.ID), Columns.None().Add(x => x.DefaultScope.ID)) .Rows.FirstOrDefault()?.Get(x => x.DefaultScope.ID) ?? Guid.Empty; } } } protected override void AfterSave(StockMovement sm) { // 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); } 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(x => x.ID).IsEqualTo(entity.ID), Columns.None().Add(x => x.JobRequisitionItem.ID) ).Rows .FirstOrDefault()? .Get(x => x.JobRequisitionItem.ID) ?? Guid.Empty; } protected override void AfterDelete(StockMovement sm) { if (sm.JobRequisitionItem.ID != Guid.Empty) JobRequisitionItemStore.UpdateStatus(this, sm.JobRequisitionItem.ID, JobRequisitionItemAction.Deleted); base.AfterDelete(sm); } }