StockMovementStore.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. using HoldingDictionary = Dictionary<(Guid product, Guid style, Guid location, Guid job, StockDimensions dimensions), StockHolding>;
  11. public class StockMovementStore : BaseStore<StockMovement>
  12. {
  13. // These will be initialised in BeforeSave
  14. HoldingDictionary holdingData = null!;
  15. StockMovement[] mvtData = null!;
  16. protected override void BeforeSave(IEnumerable<StockMovement> entities)
  17. {
  18. foreach(var entity in entities)
  19. {
  20. // Calling base BeforeSave so that it doesn't call our other BeforeSave method.
  21. base.BeforeSave(entity);
  22. }
  23. mvtData = StockHoldingStore.LoadMovementData(this, entities.Select(x => x.ID).ToArray());
  24. holdingData = StockHoldingStore.LoadStockHoldings(this, mvtData);
  25. StockHoldingStore.ModifyHoldings(mvtData, holdingData, StockHoldingStore.Action.Decrease);
  26. foreach(var sm in entities)
  27. {
  28. if(sm.Job.HasOriginalValue(x => x.ID) && sm.Job.ID != Guid.Empty && sm.JobScope.ID == Guid.Empty)
  29. {
  30. // If we have updated the Job.ID to a non-empty value, we should
  31. // update the JobScope to the default job scope for that job.
  32. var scopeID = Guid.Empty;
  33. if(sm.ID != Guid.Empty)
  34. {
  35. // It's possible that the JobScope ID just wasn't passed up, if
  36. // this entity already exists; hence, we shall load the scopeID
  37. // from the database just in case.
  38. scopeID = Provider.Query(
  39. new Filter<StockMovement>(x => x.ID).IsEqualTo(sm.ID),
  40. Columns.None<StockMovement>().Add(x => x.JobScope.ID))
  41. .Rows.FirstOrDefault()?.Get<StockMovement, Guid>(x => x.JobScope.ID) ?? Guid.Empty;
  42. }
  43. if(scopeID == Guid.Empty)
  44. {
  45. // No scope has been assigned; however, we have a job, so we
  46. // load the default scope for the job.
  47. sm.JobScope.ID = Provider.Query(
  48. new Filter<Job>(x => x.ID).IsEqualTo(sm.Job.ID),
  49. Columns.None<Job>().Add(x => x.DefaultScope.ID))
  50. .Rows.FirstOrDefault()?.Get<Job, Guid>(x => x.DefaultScope.ID) ?? Guid.Empty;
  51. }
  52. }
  53. }
  54. }
  55. protected override void BeforeSave(StockMovement sm)
  56. {
  57. BeforeSave(CoreUtils.One(sm));
  58. }
  59. protected override void AfterSave(IEnumerable<StockMovement> entities)
  60. {
  61. // Update the Relevant StockHolding with the details of this movement
  62. StockHoldingStore.ModifyHoldings(mvtData, holdingData, StockHoldingStore.Action.Increase);
  63. StockHoldingStore.SaveHoldings(this, holdingData);
  64. foreach(var sm in entities)
  65. {
  66. // Update the Job requisition item status (if applicable)
  67. if (sm.JobRequisitionItem.ID != Guid.Empty)
  68. JobRequisitionItemStore.UpdateStatus(
  69. this,
  70. sm.JobRequisitionItem.ID,
  71. sm.HasOriginalValue(x=>x.ID)
  72. ? JobRequisitionItemAction.Created
  73. : JobRequisitionItemAction.Updated
  74. );
  75. }
  76. foreach(var entity in entities)
  77. {
  78. // Calling base AfterSave so that it doesn't call our other AfterSave method.
  79. base.AfterSave(entity);
  80. }
  81. }
  82. protected override void AfterSave(StockMovement sm)
  83. {
  84. AfterSave(CoreUtils.One(sm));
  85. }
  86. protected override void BeforeDelete(StockMovement entity)
  87. {
  88. base.BeforeDelete(entity);
  89. // We need to do this in before delete, because otherwise we wont have
  90. // the data that we need to pull to properly update the stockholdings
  91. StockHoldingStore.UpdateStockHolding(this, entity.ID,StockHoldingStore.Action.Decrease);
  92. // Now let's pull the requisition ID, so that we can clean this up
  93. // properly in AfterDelete()
  94. entity.JobRequisitionItem.ID = Provider.Query(
  95. new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.ID),
  96. Columns.None<StockMovement>().Add(x => x.JobRequisitionItem.ID)
  97. ).Rows
  98. .FirstOrDefault()?
  99. .Get<StockMovement, Guid>(x => x.JobRequisitionItem.ID) ?? Guid.Empty;
  100. }
  101. protected override void AfterDelete(StockMovement sm)
  102. {
  103. if (sm.JobRequisitionItem.ID != Guid.Empty)
  104. JobRequisitionItemStore.UpdateStatus(this, sm.JobRequisitionItem.ID, JobRequisitionItemAction.Deleted);
  105. base.AfterDelete(sm);
  106. }
  107. }