ProductGroupSelector.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Configuration;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using NPOI.HSSF.Util;
  13. using Syncfusion.Windows.Tools.Controls;
  14. namespace PRSDesktop
  15. {
  16. public class ProductGroupSelectorData
  17. {
  18. // FullGuid => All Staff
  19. public Guid[] Groups { get; set; }
  20. public ProductGroupSelectorData(Guid[] groups)
  21. {
  22. Groups = groups;
  23. }
  24. public ProductGroupSelectorData() : this(new[] { CoreUtils.FullGuid })
  25. {
  26. }
  27. }
  28. public class ProductGroupSelectorSettings
  29. {
  30. public ProductGroupSelectorSettings()
  31. {
  32. }
  33. }
  34. public class ProductGroupSelectorSettingsChangedArgs : EventArgs
  35. {
  36. public ProductGroupSelectorSettings Settings { get; private set; }
  37. public ProductGroupSelectorSettingsChangedArgs(ProductGroupSelectorSettings settings)
  38. {
  39. Settings = settings;
  40. }
  41. }
  42. public delegate void ProductGroupSelectorSettingsChanged(object sender, ProductGroupSelectorSettingsChangedArgs args);
  43. public class ProductGroupSelectorSelectionChangedArgs : EventArgs
  44. {
  45. public ProductGroupSelectorData Selection { get; private set; }
  46. public ProductGroupSelectorSelectionChangedArgs(ProductGroupSelectorData selection)
  47. {
  48. Selection = selection;
  49. }
  50. }
  51. public delegate void ProductGroupSelectorSelectionChanged(object sender, ProductGroupSelectorSelectionChangedArgs args);
  52. public partial class ProductGroupSelector : UserControl
  53. {
  54. private enum Suppress
  55. {
  56. This
  57. }
  58. private class ListEntry
  59. {
  60. public Guid Key { get; set; }
  61. public String Value { get; set; }
  62. }
  63. public ProductGroupSelectorSettings Settings { get; set; }
  64. public event ProductGroupSelectorSettingsChanged SettingsChanged;
  65. public event ProductGroupSelectorSelectionChanged SelectionChanged;
  66. private void UpdateSettings()
  67. {
  68. bool changed = false;
  69. if (changed && (!EventSuppressor.IsSet(Suppress.This)))
  70. SettingsChanged?.Invoke(this, new ProductGroupSelectorSettingsChangedArgs(Settings));
  71. }
  72. private CoreTable _productgroups;
  73. public ProductGroupSelector()
  74. {
  75. Settings = new ProductGroupSelectorSettings();
  76. using (new EventSuppressor(Suppress.This))
  77. InitializeComponent();
  78. }
  79. public void Setup()
  80. {
  81. using (new EventSuppressor(Suppress.This))
  82. {
  83. MultiQuery query = new MultiQuery();
  84. query.Add<ProductGroup>(
  85. LookupFactory.DefineFilter<ProductGroup>(),
  86. new Columns<ProductGroup>(x => x.ID)
  87. .Add(x => x.Code)
  88. .Add(x=>x.Description),
  89. new SortOrder<ProductGroup>(x => x.Code)
  90. );
  91. query.Query();
  92. _productgroups = query.Get<ProductGroup>();
  93. SelectedGroups.ItemsSource = _productgroups.Rows.Select(
  94. r => new ListEntry()
  95. {
  96. Key = r.Get<ProductGroup, Guid>(c => c.ID),
  97. Value = String.Format("{0}: {1}", r.Get<ProductGroup, String>(c => c.Code), r.Get<ProductGroup, String>(c => c.Description))
  98. }
  99. ).ToArray();
  100. }
  101. }
  102. public T[] GetGroupData<T>(Func<CoreRow, T> transform)
  103. {
  104. List<T> result = new List<T>();
  105. var groups = GetSelectedGroups();
  106. var rows = _productgroups.Rows.Where(r => groups.Contains(CoreUtils.FullGuid) || groups.Contains(r.Get<ProductGroup, Guid>(c => c.ID)));
  107. foreach (var row in rows)
  108. result.Add(transform.Invoke(row));
  109. return result.ToArray();
  110. }
  111. private Guid[] GetSelectedGroups()
  112. {
  113. var jobs = (SelectedGroups.SelectedItems.Count == SelectedGroups.Items.Count)
  114. ? new Guid[] { CoreUtils.FullGuid }
  115. : SelectedGroups.SelectedItems
  116. .OfType<ListEntry>()
  117. .Select(x => x.Key)
  118. .ToArray();
  119. return jobs;
  120. }
  121. public ProductGroupSelectorData Selection
  122. {
  123. get => GetSelectionData();
  124. set => SelectData(value);
  125. }
  126. private ProductGroupSelectorData GetSelectionData()
  127. {
  128. return new ProductGroupSelectorData(
  129. GetSelectedGroups()
  130. );
  131. }
  132. private void SelectData(ProductGroupSelectorData selection)
  133. {
  134. using (new EventSuppressor(Suppress.This))
  135. {
  136. var sels = SelectedGroups.Items
  137. .OfType<ListEntry>()
  138. .Where(x => selection.Groups.Contains(CoreUtils.FullGuid) || selection.Groups.Contains(x.Key));
  139. SelectedGroups.SelectedItems.Clear();
  140. foreach (var sel in sels)
  141. SelectedGroups.SelectedItems.Add(sel);
  142. }
  143. }
  144. private void SelectedGroups_OnItemChecked(object? sender, ItemCheckedEventArgs e)
  145. {
  146. if (EventSuppressor.IsSet(Suppress.This))
  147. return;
  148. UpdateSettings();
  149. SelectionChanged?.Invoke(this, new ProductGroupSelectorSelectionChangedArgs(GetSelectionData()));
  150. }
  151. private void SelectAll_OnClick(object sender, RoutedEventArgs e)
  152. {
  153. using (new EventSuppressor(Suppress.This))
  154. {
  155. var selected = SelectedGroups.SelectedItems
  156. .OfType<ListEntry>()
  157. .Select(x=>x.Key)
  158. .ToArray();
  159. var items = SelectedGroups.Items
  160. .OfType<ListEntry>()
  161. .ToArray();
  162. if (items.Length == selected.Length)
  163. {
  164. SelectedGroups.SelectedItems.Clear();
  165. SelectAll.Content = "All";
  166. }
  167. else
  168. {
  169. var unselected = items.Where(p => !selected.Contains(p.Key));
  170. foreach (var item in unselected)
  171. SelectedGroups.SelectedItems.Add(item);
  172. SelectAll.Content = "None";
  173. }
  174. UpdateSettings();
  175. SelectionChanged?.Invoke(this, new ProductGroupSelectorSelectionChangedArgs(GetSelectionData()));
  176. }
  177. }
  178. }
  179. }