StockMovementStore.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Find all movements that weren't loaded in BeforeSave - i.e., they are new stock movements.
  64. var newIDs = entities.Select(x => x.ID).Where(x => !currentIDs.Contains(x)).ToArray();
  65. // Grab the data for these movements.
  66. var newMvts = StockHoldingStore.LoadMovementData(this, newIDs);
  67. // Load all the stock holdings for these movements, but only if we haven't loaded them already.
  68. StockHoldingStore.LoadStockHoldings(this, newMvts, holdingData);
  69. // Add the new movement data to our data. Note that the above line does the same for holdings.
  70. mvtData = mvtData.Concatenate(newMvts);
  71. // Update the Relevant StockHolding with the details of this movement
  72. StockHoldingStore.ModifyHoldings(mvtData, holdingData, StockHoldingStore.Action.Increase);
  73. StockHoldingStore.SaveHoldings(this, holdingData);
  74. foreach(var entity in entities)
  75. {
  76. // Calling base AfterSave so that it doesn't call our other AfterSave method.
  77. base.AfterSave(entity);
  78. }
  79. }
  80. protected override void AfterSave(StockMovement sm)
  81. {
  82. AfterSave(CoreUtils.One(sm));
  83. }
  84. protected override void BeforeDelete(StockMovement entity)
  85. {
  86. base.BeforeDelete(entity);
  87. // We need to do this in before delete, because otherwise we wont have
  88. // the data that we need to pull to properly update the stockholdings
  89. StockHoldingStore.UpdateStockHolding(this, entity.ID,StockHoldingStore.Action.Decrease);
  90. // Now let's pull the requisition ID, so that we can clean this up
  91. // properly in AfterDelete()
  92. entity.JobRequisitionItem.ID = Provider.Query(
  93. new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.ID),
  94. Columns.None<StockMovement>().Add(x => x.JobRequisitionItem.ID)
  95. ).Rows
  96. .FirstOrDefault()?
  97. .Get<StockMovement, Guid>(x => x.JobRequisitionItem.ID) ?? Guid.Empty;
  98. }
  99. }