StockMovementStore.cs 5.2 KB

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