using Comal.Classes; using Comal.Stores; using InABox.Core; using System.Collections.Generic; using System.Linq; using System.Text; using System; 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(sm.ID,StockHoldingStore.Action.Decrease); } protected override void AfterSave(StockMovement sm) { // Update the Relevant StockHolding with the details of this movement StockHoldingStore.UpdateStockHolding(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 aotherwise we wont have the data // that we need to pull to properly update the stockholdings StockHoldingStore.UpdateStockHolding(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); } }