StockMovementStore.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Comal.Classes;
  2. using Comal.Stores;
  3. using InABox.Core;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System;
  8. using InABox.Database;
  9. namespace PRSStores;
  10. public class StockMovementStore : BaseStore<StockMovement>
  11. {
  12. protected override void BeforeSave(StockMovement sm)
  13. {
  14. base.BeforeSave(sm);
  15. // If this movement is an Update (instead of Insert),
  16. // we need to reduce the old stock holding before updating the new one
  17. if (sm.ID != Guid.Empty)
  18. StockHoldingStore.UpdateStockHolding(this, sm.ID,StockHoldingStore.Action.Decrease);
  19. if(sm.Job.HasOriginalValue(x => x.ID) && sm.Job.ID != Guid.Empty && sm.JobScope.ID == Guid.Empty)
  20. {
  21. // If we have updated the Job.ID to a non-empty value, we should
  22. // update the JobScope to the default job scope for that job.
  23. var scopeID = Guid.Empty;
  24. if(sm.ID != Guid.Empty)
  25. {
  26. // It's possible that the JobScope ID just wasn't passed up, if
  27. // this entity already exists; hence, we shall load the scopeID
  28. // from the database just in case.
  29. scopeID = Provider.Query(
  30. new Filter<StockMovement>(x => x.ID).IsEqualTo(sm.ID),
  31. Columns.None<StockMovement>().Add(x => x.JobScope.ID))
  32. .Rows.FirstOrDefault()?.Get<StockMovement, Guid>(x => x.JobScope.ID) ?? Guid.Empty;
  33. }
  34. if(scopeID == Guid.Empty)
  35. {
  36. // No scope has been assigned; however, we have a job, so we
  37. // load the default scope for the job.
  38. sm.JobScope.ID = Provider.Query(
  39. new Filter<Job>(x => x.ID).IsEqualTo(sm.Job.ID),
  40. Columns.None<Job>().Add(x => x.DefaultScope.ID))
  41. .Rows.FirstOrDefault()?.Get<Job, Guid>(x => x.DefaultScope.ID) ?? Guid.Empty;
  42. }
  43. }
  44. }
  45. protected override void AfterSave(StockMovement sm)
  46. {
  47. // Update the Relevant StockHolding with the details of this movement
  48. StockHoldingStore.UpdateStockHolding(this, sm.ID,StockHoldingStore.Action.Increase);
  49. // Update the Job requisition item status (if applicable)
  50. if (sm.JobRequisitionItem.ID != Guid.Empty)
  51. JobRequisitionItemStore.UpdateStatus(
  52. this,
  53. sm.JobRequisitionItem.ID,
  54. sm.HasOriginalValue(x=>x.ID)
  55. ? JobRequisitionItemAction.Created
  56. : JobRequisitionItemAction.Updated
  57. );
  58. base.AfterSave(sm);
  59. }
  60. protected override void BeforeDelete(StockMovement entity)
  61. {
  62. base.BeforeDelete(entity);
  63. // We need to do this in before delete, because otherwise we wont have
  64. // the data that we need to pull to properly update the stockholdings
  65. StockHoldingStore.UpdateStockHolding(this, entity.ID,StockHoldingStore.Action.Decrease);
  66. // Now let's pull the requisition ID, so that we can clean this up
  67. // properly in AfterDelete()
  68. entity.JobRequisitionItem.ID = Provider.Query(
  69. new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.ID),
  70. Columns.None<StockMovement>().Add(x => x.JobRequisitionItem.ID)
  71. ).Rows
  72. .FirstOrDefault()?
  73. .Get<StockMovement, Guid>(x => x.JobRequisitionItem.ID) ?? Guid.Empty;
  74. }
  75. protected override void AfterDelete(StockMovement sm)
  76. {
  77. if (sm.JobRequisitionItem.ID != Guid.Empty)
  78. JobRequisitionItemStore.UpdateStatus(this, sm.JobRequisitionItem.ID, JobRequisitionItemAction.Deleted);
  79. base.AfterDelete(sm);
  80. }
  81. }