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