StockUtils.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Comal.Classes;
  5. using InABox.Clients;
  6. using InABox.Core;
  7. using InABox.Wpf;
  8. using InABox.WPF;
  9. namespace PRSDesktop;
  10. public static class StockUtils
  11. {
  12. // public static bool RecalculateHoldingsOfLocations(Guid[] locationIDs, out List<String> report)
  13. // {
  14. // return RecalculateHoldings(new Filter<IStockHolding>(x => x.Location.ID).InList(locationIDs), out report);
  15. // }
  16. // public static bool RecalculateHoldingsOfProducts(Guid[] productIDs, out List<String> report)
  17. // {
  18. // return RecalculateHoldings(new Filter<IStockHolding>(x => x.Product.ID).InList(productIDs), out report);
  19. // }
  20. // public static bool RecalculateHoldings(Filter<IStockHolding> filter, out List<String> report)
  21. // {
  22. // bool result = false;
  23. // Dictionary<String, int> messages = new();
  24. //
  25. // void AddMessage(String type)
  26. // {
  27. // messages.TryGetValue(type, out int count);
  28. // messages[type] = ++count;
  29. // }
  30. //
  31. // Progress.ShowModal("Recalculating", progress =>
  32. // {
  33. // progress.Report("Loading Data");
  34. // MultiQuery query = new MultiQuery();
  35. //
  36. // query.Add(
  37. // filter.Cast<StockHolding>(),
  38. // Columns.Required<StockHolding>().Add(x => x.ID)
  39. // .Add(x => x.Location.ID)
  40. // .Add(x => x.Product.ID)
  41. // .Add(x => x.Job.ID)
  42. // .Add(x => x.Style.ID)
  43. // .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local)
  44. // .Add(x => x.Units)
  45. // .Add(x => x.AverageValue)
  46. // .Add(x => x.Available)
  47. // .Add(x => x.Qty)
  48. // .Add(x => x.Weight)
  49. // .Add(x => x.Value)
  50. // );
  51. //
  52. // query.Add(
  53. // filter.Cast<StockMovement>(),
  54. // Columns.None<StockMovement>().Add(x => x.ID)
  55. // .Add(x=>x.Location.ID)
  56. // .Add(x => x.Product.ID)
  57. // .Add(x => x.Job.ID)
  58. // .Add(x => x.Style.ID)
  59. // .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local)
  60. // .Add(x => x.Units)
  61. // .Add(x => x.Cost)
  62. // .Add(x => x.JobRequisitionItem.ID)
  63. // );
  64. // query.Query();
  65. //
  66. // var holdings = query.Get<StockHolding>();
  67. // var toDelete = new List<StockHolding>();
  68. // var movements = query.Get<StockMovement>();
  69. //
  70. // double movementcount = (double)movements.Rows.Count;
  71. // progress.Report("Processing");
  72. // var updates = new List<StockHolding>();
  73. //
  74. // while (movements.Rows.Any())
  75. // {
  76. // double percent = ((movementcount - movements.Rows.Count) / movementcount) * 100.0;
  77. // progress.Report($"Processing ({percent:F2}% complete)");
  78. // var first = movements.Rows.First();
  79. // var selected = movements.Rows.Where(x => x.IsEqualTo<StockMovement, StockMovement>(first)).ToList();
  80. //
  81. // var holdingrow = holdings.Rows.FirstOrDefault(x => x.IsEqualTo<StockHolding, StockMovement>(first));
  82. // StockHolding holding = null;
  83. // if (holdingrow != null)
  84. // {
  85. // holdings.Rows.Remove(holdingrow);
  86. // holding = holdingrow.ToObject<StockHolding>();
  87. // }
  88. //
  89. // if (holding == null)
  90. // {
  91. // var firstmovement = first.ToObject<StockMovement>();
  92. // holding = new StockHolding();
  93. // holding.Location.ID = firstmovement.Location.ID;
  94. // holding.Product.ID = firstmovement.Product.ID;
  95. // holding.Style.ID = firstmovement.Style.ID;
  96. // holding.Job.ID = firstmovement.Job.ID;
  97. // holding.Dimensions.CopyFrom(firstmovement.Dimensions);
  98. // }
  99. //
  100. // holding.Recalculate(selected.ToObjects<StockMovement>());
  101. //
  102. // if (holding.Units.IsEffectivelyEqual(0.0) && holding.Available.IsEffectivelyEqual(0.0))
  103. // {
  104. // if (holding.ID != Guid.Empty)
  105. // toDelete.Add(holding);
  106. // }
  107. // else if (holding.IsChanged())
  108. // {
  109. // AddMessage(holding.ID != Guid.Empty ? "updated" : "added");
  110. // updates.Add(holding);
  111. // }
  112. //
  113. // foreach (var row in selected)
  114. // movements.Rows.Remove(row);
  115. // }
  116. //
  117. // toDelete.AddRange(holdings.Rows.ToObjects<StockHolding>());
  118. // foreach (var holding in toDelete)
  119. // AddMessage("deleted");
  120. //
  121. // if (updates.Any())
  122. // {
  123. // result = true;
  124. // progress.Report($"Updating {updates.Count} Holdings");
  125. // new Client<StockHolding>().Save(updates.Where(x => x.IsChanged()), "Updated by Recalculation");
  126. // }
  127. //
  128. // if (toDelete.Any())
  129. // {
  130. // result = true;
  131. // progress.Report($"Deleting {toDelete.Count} Holdings");
  132. // new Client<StockHolding>().Delete(toDelete, "Removed by Recalculation");
  133. // }
  134. //
  135. // });
  136. // report = new List<String>();
  137. // if (messages.Any())
  138. // report.AddRange(messages.Select(x => $"{x.Value} holdings {x.Key}"));
  139. // else
  140. // report.Add("Nothing to Update");
  141. //
  142. // return result;
  143. // }
  144. }