ProductGroupSelector.xaml.cs 6.9 KB

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