ReservationManagementHoldingsGrid.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.Wpf;
  5. using org.omg.PortableInterceptor;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. namespace PRSDesktop;
  13. public delegate void HoldingsReviewRefresh();
  14. /// <summary>
  15. /// Interaction logic for JobRequisitionHoldingsReview.xaml
  16. /// </summary>
  17. public partial class ReservationManagementHoldingsGrid
  18. {
  19. private ProductStyleLink companyDefaultStyle = new ProductStyleLink();
  20. public ProductStyleLink CompanyDefaultStyle
  21. {
  22. get => companyDefaultStyle;
  23. set
  24. {
  25. companyDefaultStyle = value;
  26. DefaultOrNoStyleText.Text = String.IsNullOrWhiteSpace(companyDefaultStyle?.Code)
  27. ? "No Style"
  28. : $"{companyDefaultStyle.Code} / None";
  29. }
  30. }
  31. public event HoldingsReviewRefresh? OnHoldingsReviewRefresh;
  32. private JobRequisitionItem? item;
  33. public JobRequisitionItem? Item
  34. {
  35. get
  36. {
  37. return item;
  38. }
  39. set
  40. {
  41. if(item?.ID != value?.ID)
  42. {
  43. item = value;
  44. CalculateHoldings();
  45. SetStyle();
  46. SetProductName();
  47. }
  48. }
  49. }
  50. private void SetProductName()
  51. {
  52. productLbl.Text = Item != null
  53. ? Item.Product.Name + " (" + Item.Product.Code + ")"
  54. : "No Product Selected";
  55. }
  56. private void SetStyle()
  57. {
  58. if ((Item != null) && (Item.Style.ID != Guid.Empty))
  59. styleLbl.Text = Item.Style.Description + " (" + Item.Style.Code + ")";
  60. else
  61. styleLbl.Text = "No Style Selected";
  62. }
  63. public ReservationManagementHoldingsGrid()
  64. {
  65. InitializeComponent();
  66. greenList.CollectionChanged += (s, e) =>
  67. {
  68. availableTab.Visibility = greenList.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
  69. };
  70. yellowList.CollectionChanged += (s, e) =>
  71. {
  72. reservedTab.Visibility = yellowList.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
  73. };
  74. redList.CollectionChanged += (s, e) =>
  75. {
  76. requisitionedTab.Visibility = redList.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
  77. };
  78. }
  79. private readonly ObservableCollection<ReservationManagementHoldingsModel> greenList = new();
  80. private readonly ObservableCollection<ReservationManagementHoldingsModel> yellowList = new();
  81. private readonly ObservableCollection<ReservationManagementHoldingsModel> redList = new();
  82. private void DistributeMovements(Dictionary<Guid, List<StockMovement>>? movements, Guid selectedStyleID, ReservationManagementHoldingsModel model)
  83. {
  84. if (movements is null) return;
  85. foreach(var (id, mvts) in movements)
  86. {
  87. if(id == Guid.Empty || id == CompanyDefaultStyle.ID)
  88. {
  89. model.StockOfNoStyle.AddRange(mvts);
  90. }
  91. else if (id == selectedStyleID)
  92. {
  93. model.StockOfCurrentStyle.AddRange(mvts);
  94. }
  95. else
  96. {
  97. model.StockOfOtherStyles.AddRange(mvts);
  98. }
  99. }
  100. model.UnitsOfCurrentStyle = Math.Max(model.StockOfCurrentStyle.Sum(x => x.Units), 0);
  101. model.UnitsOfNoStyle = Math.Max(model.StockOfNoStyle.Sum(x => x.Units), 0);
  102. model.UnitsOfOtherStyles = Math.Max(model.StockOfOtherStyles.Sum(x => x.Units), 0);
  103. }
  104. private void CalculateHoldings()
  105. {
  106. if(item is not null)
  107. {
  108. var stockMovements = Client.Query(
  109. new Filter<StockMovement>(x => x.Product.ID).IsEqualTo(item.Product.ID)
  110. .And(x => x.Dimensions.UnitSize).IsEqualTo(item.Dimensions.UnitSize)
  111. .And(x => x.Location.ID).IsNotEqualTo(Guid.Empty),
  112. new Columns<StockMovement>(
  113. x => x.ID,
  114. x => x.Style.ID,
  115. x => x.Style.Description,
  116. x => x.Style.Code,
  117. x => x.Units,
  118. x => x.Location.ID,
  119. x => x.Location.Code,
  120. x => x.Location.Description,
  121. x => x.Location.Area.Description,
  122. x => x.Job.ID,
  123. x => x.JobRequisitionItem.ID)
  124. .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Data))
  125. .ToObjects<StockMovement>()
  126. .GroupBy(x => new { JobID = x.Job.ID, Allocated = x.JobRequisitionItem.ID != Guid.Empty })
  127. .ToDictionary(
  128. x => x.Key,
  129. x => x.GroupBy(x => x.Style.ID)
  130. .Select(x =>
  131. {
  132. var mvts = x.ToList();
  133. return new
  134. {
  135. Style = x.Key,
  136. Movements = mvts
  137. };
  138. })
  139. .ToDictionary(
  140. x => x.Style,
  141. x => x.Movements));
  142. var jobs = Client.Query(
  143. new Filter<Job>(x => x.ID).InList(stockMovements.Keys.Select(x => x.JobID).ToArray()),
  144. new Columns<Job>(x => x.ID).Add(x => x.JobNumber).Add(x => x.Name))
  145. .ToObjects<Job>()
  146. .ToDictionary(x => x.ID, x => x);
  147. if(!stockMovements.ContainsKey(new { JobID = item.Job.ID, Allocated = false }))
  148. {
  149. stockMovements.Add(new { JobID = item.Job.ID, Allocated = false }, new Dictionary<Guid, List<StockMovement>>());
  150. }
  151. if (!stockMovements.ContainsKey(new { JobID = Guid.Empty, Allocated = false }))
  152. {
  153. stockMovements.Add(new { JobID = Guid.Empty, Allocated = false }, new Dictionary<Guid, List<StockMovement>>());
  154. }
  155. greenList.Clear();
  156. yellowList.Clear();
  157. redList.Clear();
  158. foreach (var (key, movements) in stockMovements)
  159. {
  160. var job = jobs.GetValueOrDefault(key.JobID);
  161. ReservationManagementHoldingsModel holding;
  162. if (key.Allocated)
  163. {
  164. holding = new ReservationManagementHoldingsModel(key.JobID, job?.JobNumber ?? "", job?.Name ?? "")
  165. {
  166. AlreadyAllocated = true
  167. };
  168. DistributeMovements(movements, item.Style.ID, holding);
  169. if (!holding.Empty)
  170. {
  171. redList.Add(holding);
  172. }
  173. }
  174. else if (key.JobID == Guid.Empty)
  175. {
  176. holding = new ReservationManagementHoldingsModel(Guid.Empty, "Free Stock", "Free Stock");
  177. DistributeMovements(movements, item.Style.ID, holding);
  178. greenList.Add(holding);
  179. }
  180. else if(key.JobID == item.Job.ID)
  181. {
  182. holding = new ReservationManagementHoldingsModel(item.Job.ID, item.Job.JobNumber, item.Job.Name);
  183. DistributeMovements(movements, item.Style.ID, holding);
  184. greenList.Add(holding);
  185. }
  186. else
  187. {
  188. holding = new ReservationManagementHoldingsModel(key.JobID, job?.JobNumber ?? "", job?.Name ?? "");
  189. DistributeMovements(movements, item.Style.ID, holding);
  190. if (!holding.Empty)
  191. {
  192. yellowList.Add(holding);
  193. }
  194. }
  195. }
  196. if (greenList.Count == 1)
  197. greenList.Add(new ReservationManagementHoldingsModel());
  198. if (redList.Count == 1)
  199. redList.Add(new ReservationManagementHoldingsModel());
  200. if (yellowList.Count == 1)
  201. yellowList.Add(new ReservationManagementHoldingsModel());
  202. }
  203. else
  204. {
  205. greenList.Clear();
  206. yellowList.Clear();
  207. redList.Clear();
  208. }
  209. listViewGreen.ItemsSource = greenList;
  210. listViewYellow.ItemsSource = yellowList;
  211. listViewRed.ItemsSource = redList;
  212. }
  213. private void Take_Click(object sender, RoutedEventArgs e)
  214. {
  215. if (sender is not FrameworkElement element
  216. || element.DataContext is not ReservationManagementHoldingsModel model
  217. || element.Tag is not List<StockMovement> mvts) return;
  218. LaunchStockSelectionPage(model, mvts);
  219. }
  220. private void LaunchStockSelectionPage(ReservationManagementHoldingsModel model, List<StockMovement> mvts)
  221. {
  222. if (Item is null) return;
  223. var locationIDs = new List<Guid>();
  224. var holdings = new List<StockHolding>();
  225. foreach (var mvt in mvts)
  226. {
  227. if (!locationIDs.Contains(mvt.Location.ID))
  228. {
  229. var holding = new StockHolding();
  230. holding.Location.ID = mvt.Location.ID;
  231. holding.Location.Description = mvt.Location.Description;
  232. holding.Location.Area.Description = mvt.Location.Area.Description;
  233. holding.Style.ID = mvt.Style.ID;
  234. holding.Style.Description = mvt.Style.Description;
  235. holding.Style.Code = mvt.Style.Code;
  236. holding.Dimensions.CopyFrom(mvt.Dimensions);
  237. if (mvt.JobRequisitionItem.ID != Guid.Empty && model.AlreadyAllocated)
  238. {
  239. holding.Units = mvt.Units;
  240. holdings.Add(holding);
  241. locationIDs.Add(mvt.Location.ID);
  242. }
  243. else if (mvt.JobRequisitionItem.ID == Guid.Empty && !model.AlreadyAllocated)
  244. {
  245. holding.Units = mvt.Units;
  246. holdings.Add(holding);
  247. locationIDs.Add(mvt.Location.ID);
  248. }
  249. }
  250. else
  251. {
  252. if (mvt.JobRequisitionItem.ID != Guid.Empty && model.AlreadyAllocated)
  253. {
  254. var holding = holdings.First(x => x.Location.ID == mvt.Location.ID);
  255. holding.Units += mvt.Units;
  256. }
  257. else if (mvt.JobRequisitionItem.ID == Guid.Empty && !model.AlreadyAllocated)
  258. {
  259. var holding = holdings.First(x => x.Location.ID == mvt.Location.ID);
  260. holding.Units += mvt.Units;
  261. }
  262. }
  263. }
  264. var filteredHoldings = holdings.Where(x => x.Units > 0);
  265. var page = new StockSelectionPage(
  266. filteredHoldings,
  267. Item,
  268. new Job { ID = model.JobID, Name = model.JobName, JobNumber = model.JobNumber },
  269. model.AlreadyAllocated);
  270. if (page.ShowDialog() == true)
  271. {
  272. //MessageWindow.ShowMessage("Success - stock allocated to requisition line", "Success");
  273. CalculateHoldings();
  274. OnHoldingsReviewRefresh?.Invoke();
  275. }
  276. }
  277. }