StockMovementStore.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Comal.Classes;
  2. using Comal.Stores;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace PRSStores;
  9. public class StockMovementStore : BaseStore<StockMovement>
  10. {
  11. protected override void BeforeSave(StockMovement sm)
  12. {
  13. base.BeforeSave(sm);
  14. // If this movement is an Update (instead of Insert),
  15. // we need to reduce the old stock holding before updating the new one
  16. if (sm.ID != Guid.Empty)
  17. StockHoldingStore.UpdateStockHolding(sm.ID,StockHoldingStore.Action.Decrease);
  18. }
  19. protected override void AfterSave(StockMovement sm)
  20. {
  21. // Update the Relevant StockHolding with the details of this movement
  22. StockHoldingStore.UpdateStockHolding(sm.ID,StockHoldingStore.Action.Increase);
  23. // Update the Job requisition item status (if applicable)
  24. if (sm.JobRequisitionItem.ID != Guid.Empty)
  25. JobRequisitionItemStore.UpdateStatus(
  26. this,
  27. sm.JobRequisitionItem.ID,
  28. sm.HasOriginalValue(x=>x.ID)
  29. ? JobRequisitionItemAction.Created
  30. : JobRequisitionItemAction.Updated
  31. );
  32. base.AfterSave(sm);
  33. }
  34. protected override void BeforeDelete(StockMovement entity)
  35. {
  36. base.BeforeDelete(entity);
  37. // We need to do this in before delete, because aotherwise we wont have the data
  38. // that we need to pull to properly update the stockholdings
  39. StockHoldingStore.UpdateStockHolding(entity.ID,StockHoldingStore.Action.Decrease);
  40. // Now let's pull the requisition ID, so that we can clean this up properly
  41. // in AfterDelete()
  42. entity.JobRequisitionItem.ID = Provider.Query(
  43. new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.ID),
  44. new Columns<StockMovement>(x => x.JobRequisitionItem.ID)
  45. ).Rows
  46. .FirstOrDefault()?
  47. .Get<StockMovement, Guid>(x => x.JobRequisitionItem.ID) ?? Guid.Empty;
  48. }
  49. protected override void AfterDelete(StockMovement sm)
  50. {
  51. if (sm.JobRequisitionItem.ID != Guid.Empty)
  52. JobRequisitionItemStore.UpdateStatus(this, sm.JobRequisitionItem.ID, JobRequisitionItemAction.Deleted);
  53. base.AfterDelete(sm);
  54. }
  55. }