StockHoldingStore.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. ).Rows
  53. .FirstOrDefault()?
  54. .ToObject<StockHolding>();
  55. if (holding == null)
  56. {
  57. holding = new();
  58. holding.Location.ID = movement.Location.ID;
  59. holding.Product.ID = movement.Product.ID;
  60. holding.Style.ID = movement.Style.ID;
  61. holding.Job.ID = movement.Job.ID;
  62. holding.Dimensions.CopyFrom(movement.Dimensions);
  63. }
  64. double multiplier = action == Action.Increase ? 1F : -1F;
  65. holding.Units += (multiplier * movement.Units);
  66. holding.Qty += (multiplier * movement.Units * movement.Dimensions.Value);
  67. holding.Value += (multiplier * movement.Units * movement.Cost);
  68. holding.Available += (multiplier * (movement.JobRequisitionItem.ID == Guid.Empty ? movement.Units : 0.0));
  69. holding.Weight = holding.Qty * holding.Dimensions.Weight;
  70. holding.AverageValue = holding.Units != 0 ? holding.Value / holding.Units : 0.0F;
  71. // Automagically clean up empty holdings
  72. if (holding.Units.EqualsWithTolerance(0.0F))
  73. {
  74. if (holding.ID != Guid.Empty)
  75. DbFactory.Provider.Delete(holding, "");
  76. }
  77. else
  78. DbFactory.Provider.Save(holding);
  79. }
  80. }