1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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<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);
- 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 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<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;
- }
- protected override void AfterDelete(StockMovement sm)
- {
- if (sm.JobRequisitionItem.ID != Guid.Empty)
- JobRequisitionItemStore.UpdateStatus(this, sm.JobRequisitionItem.ID, JobRequisitionItemAction.Deleted);
- base.AfterDelete(sm);
- }
- }
|