StockHoldingStore.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.Database;
  4. using System;
  5. using System.Linq;
  6. namespace Comal.Stores;
  7. public class StockHoldingStore : BaseStore<StockHolding>
  8. {
  9. public enum Action
  10. {
  11. Increase,
  12. Decrease
  13. }
  14. /// <summary>
  15. /// Maintains the Stock Holding Table when manipulating Stock Movements
  16. /// We only accept an ID, because the rest of the movement is pulled from the database
  17. /// (slower, but more reliable)
  18. /// </summary>
  19. /// <param name="id">The id of the Stock Movement to query</param>
  20. /// <param name="action">The action to perform (increase / decrease)</param>
  21. public static void UpdateStockHolding(Guid id, Action action)
  22. {
  23. var movement = DbFactory.Provider.Query(
  24. new Filter<StockMovement>(x => x.ID).IsEqualTo(id),
  25. new Columns<StockMovement>(x => x.ID)
  26. .Add(x => x.Location.ID)
  27. .Add(x => x.Product.ID)
  28. .Add(x => x.Style.ID)
  29. .Add(x => x.Job.ID)
  30. .Add(x => x.Dimensions.Unit.ID)
  31. .Add(x => x.Dimensions.Quantity)
  32. .Add(x => x.Dimensions.Height)
  33. .Add(x => x.Dimensions.Width)
  34. .Add(x => x.Dimensions.Length)
  35. .Add(x => x.Dimensions.Weight)
  36. .Add(x => x.Dimensions.UnitSize)
  37. .Add(x => x.Dimensions.Value)
  38. .Add(x => x.JobRequisitionItem.ID)
  39. .Add(x => x.Units)
  40. .Add(x => x.Cost)
  41. ).Rows
  42. .FirstOrDefault()?
  43. .ToObject<StockMovement>();
  44. if (movement == null)
  45. return;
  46. var holding = DbFactory.Provider.Query(new Filter<StockHolding>(x => x.Product.ID).IsEqualTo(movement.Product.ID)
  47. .And(x => x.Location.ID).IsEqualTo(movement.Location.ID)
  48. .And(x => x.Style.ID).IsEqualTo(movement.Style.ID)
  49. .And(x => x.Job.ID).IsEqualTo(movement.Job.ID)
  50. .And(x => x.Dimensions.Unit.ID).IsEqualTo(movement.Dimensions.Unit.ID)
  51. .And(x => x.Dimensions.UnitSize).IsEqualTo(movement.Dimensions.UnitSize),
  52. new Columns<StockHolding>(x => x.ID)
  53. .Add(x => x.Units)
  54. .Add(x => x.Qty)
  55. .Add(x => x.Value)
  56. .Add(x => x.Available)
  57. ).Rows
  58. .FirstOrDefault()?
  59. .ToObject<StockHolding>();
  60. if (holding == null)
  61. {
  62. holding = new();
  63. holding.Location.ID = movement.Location.ID;
  64. holding.Product.ID = movement.Product.ID;
  65. holding.Style.ID = movement.Style.ID;
  66. holding.Job.ID = movement.Job.ID;
  67. holding.Dimensions.CopyFrom(movement.Dimensions);
  68. }
  69. double multiplier = action == Action.Increase ? 1F : -1F;
  70. holding.Units += (multiplier * movement.Units);
  71. holding.Qty += (multiplier * movement.Units * movement.Dimensions.Value);
  72. holding.Value += (multiplier * movement.Units * movement.Cost);
  73. holding.Available += (multiplier * (movement.JobRequisitionItem.ID == Guid.Empty ? movement.Units : 0.0));
  74. holding.Weight = holding.Qty * holding.Dimensions.Weight;
  75. holding.AverageValue = holding.Units != 0 ? holding.Value / holding.Units : 0.0F;
  76. // Automagically clean up empty holdings
  77. if (holding.Units.IsEffectivelyEqual(0.0F))
  78. {
  79. if (holding.ID != Guid.Empty)
  80. DbFactory.Provider.Delete(holding, "");
  81. }
  82. else
  83. DbFactory.Provider.Save(holding);
  84. }
  85. }