StockMovementStore.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // We load the movements according to the data currently in the database (i.e, before we've saved). This is to decrease the old holdings.
  26. mvtData = StockHoldingStore.LoadMovementData(this, currentIDs.ToArray());
  27. holdingData = StockHoldingStore.LoadStockHoldings(this, mvtData);
  28. StockHoldingStore.ModifyHoldings(mvtData, holdingData, StockHoldingStore.Action.Decrease);
  29. foreach(var sm in entities)
  30. {
  31. if(sm.Job.HasOriginalValue(x => x.ID) && sm.Job.ID != Guid.Empty && sm.JobScope.ID == Guid.Empty)
  32. {
  33. // If we have updated the Job.ID to a non-empty value, we should
  34. // update the JobScope to the default job scope for that job.
  35. var scopeID = Guid.Empty;
  36. if(sm.ID != Guid.Empty)
  37. {
  38. // It's possible that the JobScope ID just wasn't passed up, if
  39. // this entity already exists; hence, we shall load the scopeID
  40. // from the database just in case.
  41. scopeID = Provider.Query(
  42. new Filter<StockMovement>(x => x.ID).IsEqualTo(sm.ID),
  43. Columns.None<StockMovement>().Add(x => x.JobScope.ID))
  44. .Rows.FirstOrDefault()?.Get<StockMovement, Guid>(x => x.JobScope.ID) ?? Guid.Empty;
  45. }
  46. if(scopeID == Guid.Empty)
  47. {
  48. // No scope has been assigned; however, we have a job, so we
  49. // load the default scope for the job.
  50. sm.JobScope.ID = Provider.Query(
  51. new Filter<Job>(x => x.ID).IsEqualTo(sm.Job.ID),
  52. Columns.None<Job>().Add(x => x.DefaultScope.ID))
  53. .Rows.FirstOrDefault()?.Get<Job, Guid>(x => x.DefaultScope.ID) ?? Guid.Empty;
  54. }
  55. }
  56. }
  57. }
  58. protected override void BeforeSave(StockMovement sm)
  59. {
  60. BeforeSave(CoreUtils.One(sm));
  61. }
  62. protected override void AfterSave(IEnumerable<StockMovement> entities)
  63. {
  64. // Update the old movement data with the changes we've made.
  65. foreach(var mvt in entities)
  66. {
  67. var dataMvt = mvtData.FirstOrDefault(x => x.ID == mvt.ID);
  68. if (dataMvt is null) continue;
  69. // Need to stop observing, because otherwise the order of property setting could and would break things.
  70. dataMvt.SetObserving(false);
  71. foreach(var (key, value) in mvt.OriginalValueList)
  72. {
  73. if(DatabaseSchema.Property(typeof(StockMovement), key) is IProperty prop)
  74. {
  75. prop.Setter()(dataMvt, prop.Getter()(mvt));
  76. }
  77. }
  78. dataMvt.SetObserving(true);
  79. }
  80. // Find all movements that weren't loaded in BeforeSave - i.e., they are new stock movements.
  81. var newIDs = entities.Select(x => x.ID).Where(x => !currentIDs.Contains(x)).ToArray();
  82. // Grab the data for these movements.
  83. var newMvts = StockHoldingStore.LoadMovementData(this, newIDs);
  84. // Load all the stock holdings for these movements, but only if we haven't loaded them already.
  85. StockHoldingStore.LoadStockHoldings(this, newMvts, holdingData);
  86. // Add the new movement data to our data. Note that the above line does the same for holdings.
  87. mvtData = mvtData.Concatenate(newMvts);
  88. // Update the Relevant StockHolding with the details of this movement
  89. StockHoldingStore.ModifyHoldings(mvtData, holdingData, StockHoldingStore.Action.Increase);
  90. StockHoldingStore.SaveHoldings(this, holdingData);
  91. foreach(var entity in entities)
  92. {
  93. // Calling base AfterSave so that it doesn't call our other AfterSave method.
  94. base.AfterSave(entity);
  95. }
  96. }
  97. protected override void AfterSave(StockMovement sm)
  98. {
  99. AfterSave(CoreUtils.One(sm));
  100. }
  101. protected override void BeforeDelete(StockMovement entity)
  102. {
  103. base.BeforeDelete(entity);
  104. // We need to do this in before delete, because otherwise we wont have
  105. // the data that we need to pull to properly update the stockholdings
  106. StockHoldingStore.UpdateStockHolding(this, entity.ID,StockHoldingStore.Action.Decrease);
  107. // Now let's pull the requisition ID, so that we can clean this up
  108. // properly in AfterDelete()
  109. entity.JobRequisitionItem.ID = Provider.Query(
  110. new Filter<StockMovement>(x => x.ID).IsEqualTo(entity.ID),
  111. Columns.None<StockMovement>().Add(x => x.JobRequisitionItem.ID)
  112. ).Rows
  113. .FirstOrDefault()?
  114. .Get<StockMovement, Guid>(x => x.JobRequisitionItem.ID) ?? Guid.Empty;
  115. }
  116. }