JobRequisitionItemSelectionGrid.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.Wpf;
  6. using InABox.WPF;
  7. using Syncfusion.Data;
  8. using Syncfusion.UI.Xaml.Grid;
  9. using Syncfusion.Windows.Shared;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Linq;
  16. using System.Runtime.CompilerServices;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Data;
  23. using System.Windows.Media;
  24. namespace PRSDesktop;
  25. public class JobRequisitionItemSelectionItem : BaseObject
  26. {
  27. public double Quantity { get; set; }
  28. private double _issued;
  29. public double Issued
  30. {
  31. get => _issued;
  32. set
  33. {
  34. var oldValue = _issued;
  35. var canMinus = CanMinus;
  36. var canPlus = CanPlus;
  37. _issued = value;
  38. if(oldValue != value)
  39. {
  40. OnPropertyChanged(nameof(Issued), oldValue, value);
  41. }
  42. if(canMinus != CanMinus)
  43. {
  44. OnPropertyChanged(nameof(CanMinus), canMinus, CanMinus);
  45. }
  46. if(canPlus != CanPlus)
  47. {
  48. OnPropertyChanged(nameof(CanPlus), canPlus, CanPlus);
  49. }
  50. }
  51. }
  52. public double MaxValue { get; set; }
  53. private bool _editable = true;
  54. public bool Editable
  55. {
  56. get => _editable;
  57. set
  58. {
  59. var oldValue = _editable;
  60. var canMinus = CanMinus;
  61. var canPlus = CanPlus;
  62. _editable = value;
  63. if(oldValue != value)
  64. {
  65. OnPropertyChanged(nameof(Editable), oldValue, value);
  66. }
  67. if(canMinus != CanMinus)
  68. {
  69. OnPropertyChanged(nameof(CanMinus), canMinus, CanMinus);
  70. }
  71. if(canPlus != CanPlus)
  72. {
  73. OnPropertyChanged(nameof(CanPlus), canPlus, CanPlus);
  74. }
  75. }
  76. }
  77. public bool CanMinus => Editable && Issued > 0;
  78. public bool CanPlus => Editable && Issued < MaxValue;
  79. public JobRequisitionItem? JRI { get; set; }
  80. }
  81. public abstract class JobRequisitionItemSelectionGrid<T> : DynamicItemsListGrid<T>, INotifyPropertyChanged
  82. where T : JobRequisitionItemSelectionItem, new()
  83. {
  84. private double _totalQuantity;
  85. public double TotalQuantity
  86. {
  87. get => _totalQuantity;
  88. set
  89. {
  90. _totalQuantity = value;
  91. OnPropertyChanged();
  92. }
  93. }
  94. private double _totalIssued;
  95. public double TotalIssued
  96. {
  97. get => _totalIssued;
  98. set
  99. {
  100. _totalIssued = value;
  101. OnPropertyChanged();
  102. }
  103. }
  104. private bool _observing = true;
  105. public void SetObserving(bool observing)
  106. {
  107. if(_observing != observing)
  108. {
  109. _observing = observing;
  110. if (_observing)
  111. {
  112. Recalculate();
  113. }
  114. }
  115. }
  116. protected override void Init()
  117. {
  118. base.Init();
  119. ActionColumns.Add(new DynamicTemplateColumn(TotalColumn_Template)
  120. {
  121. HeaderText = "Total",
  122. Width = 60,
  123. GetSummary = () =>
  124. {
  125. return new DynamicGridTemplateSummary(TotalQuantity_Template);
  126. }
  127. });
  128. ActionColumns.Add(new DynamicTemplateColumn(QuantityColumn_Template)
  129. {
  130. HeaderText = "Quantity",
  131. Width = 230,
  132. GetSummary = () =>
  133. {
  134. return new DynamicGridTemplateSummary(TotalIssued_Template);
  135. }
  136. });
  137. }
  138. protected override void DoReconfigure(DynamicGridOptions options)
  139. {
  140. base.DoReconfigure(options);
  141. options.Clear();
  142. }
  143. private FrameworkElement TotalQuantity_Template()
  144. {
  145. var quantity = new DoubleTextBox
  146. {
  147. HorizontalContentAlignment = HorizontalAlignment.Center,
  148. VerticalContentAlignment = VerticalAlignment.Center,
  149. VerticalAlignment = VerticalAlignment.Stretch,
  150. HorizontalAlignment = HorizontalAlignment.Stretch,
  151. Background = new SolidColorBrush(Colors.WhiteSmoke),
  152. IsReadOnly = true,
  153. Margin = new Thickness(0, 5, 0, 5)
  154. };
  155. quantity.Bind(DoubleTextBox.ValueProperty, this, x => x.TotalQuantity);
  156. return quantity;
  157. }
  158. private FrameworkElement TotalIssued_Template()
  159. {
  160. var quantity = new DoubleTextBox
  161. {
  162. HorizontalContentAlignment = HorizontalAlignment.Center,
  163. VerticalContentAlignment = VerticalAlignment.Center,
  164. VerticalAlignment = VerticalAlignment.Stretch,
  165. HorizontalAlignment = HorizontalAlignment.Stretch,
  166. Background = new SolidColorBrush(Colors.LightGreen),
  167. IsReadOnly = true,
  168. Margin = new Thickness(50 + 30 + 10, 5, 50 + 30 + 5, 5)
  169. };
  170. quantity.Bind(DoubleTextBox.ValueProperty, this, x => x.TotalIssued);
  171. return quantity;
  172. }
  173. private class TotalAggregate : ISummaryAggregate
  174. {
  175. public double Sum { get; private set; }
  176. private JobRequisitionItemSelectionGrid<T> Grid;
  177. public enum TotalColumn
  178. {
  179. Quantity,
  180. Issued
  181. }
  182. public TotalColumn Type { get; set; }
  183. public TotalAggregate(TotalColumn type, JobRequisitionItemSelectionGrid<T> grid)
  184. {
  185. Type = type;
  186. Grid = grid;
  187. }
  188. public Action<IEnumerable, string, PropertyDescriptor> CalculateAggregateFunc()
  189. {
  190. return AggregateFunc;
  191. }
  192. private void AggregateFunc(IEnumerable items, string property, PropertyDescriptor args)
  193. {
  194. if (items is IEnumerable<DataRowView> rows)
  195. {
  196. Sum = Type switch
  197. {
  198. TotalColumn.Quantity => Grid.TotalQuantity,
  199. TotalColumn.Issued or _ => Grid.TotalIssued
  200. };
  201. }
  202. else
  203. {
  204. Logger.Send(LogType.Error, "", $"Attempting to calculate aggregate on invalid data type '{items.GetType()}'.");
  205. }
  206. }
  207. }
  208. protected class UIComponent : DynamicGridGridUIComponent<T>
  209. {
  210. public UIComponent(JobRequisitionItemSelectionGrid<T> grid)
  211. {
  212. Parent = grid;
  213. GridLines = DynamicGridLines.None;
  214. RowHeight = 40;
  215. HeaderRowHeight = 40;
  216. }
  217. protected override Brush? GetCellSelectionBackgroundBrush()
  218. {
  219. return null;
  220. }
  221. protected override Brush? GetCellSelectionForegroundBrush()
  222. {
  223. return null;
  224. }
  225. protected override Brush? GetCellBackground(CoreRow row, DynamicColumnBase column)
  226. {
  227. return base.GetCellBackground(row, column);
  228. }
  229. protected override Style GetHeaderCellStyle(DynamicColumnBase column)
  230. {
  231. var style = base.GetHeaderCellStyle(column);
  232. style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Silver)));
  233. style.Setters.Add(new Setter(Control.FontWeightProperty, FontWeights.Bold));
  234. style.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0.0)));
  235. style.Setters.Add(new Setter(Control.MarginProperty, new Thickness(0.0)));
  236. return style;
  237. }
  238. protected override Style GetSummaryRowStyle()
  239. {
  240. var style = base.GetSummaryRowStyle();
  241. style.Setters.Add(new Setter(Control.BorderBrushProperty, new SolidColorBrush(Colors.Silver)));
  242. style.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(1, 1, 0, 1)));
  243. style.Setters.Add(new Setter(Control.MarginProperty, new Thickness(0, 1, 0, 0)));
  244. style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.WhiteSmoke)));
  245. return style;
  246. }
  247. protected override Style GetSummaryCellStyle(DynamicColumnBase column)
  248. {
  249. var style = base.GetSummaryCellStyle(column);
  250. style.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0)));
  251. style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Transparent)));
  252. style.Setters.Add(new Setter(GridTableSummaryCell.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch));
  253. style.Setters.Add(new Setter(Control.VerticalContentAlignmentProperty, VerticalAlignment.Stretch));
  254. style.Setters.Add(new Setter(Control.PaddingProperty, new Thickness(0)));
  255. return style;
  256. }
  257. }
  258. protected override IDynamicGridUIComponent<T> CreateUIComponent()
  259. {
  260. return new UIComponent(this);
  261. }
  262. private FrameworkElement? TotalColumn_Template(CoreRow row)
  263. {
  264. var item = LoadItem(row);
  265. var quantity = new DoubleTextBox
  266. {
  267. HorizontalContentAlignment = HorizontalAlignment.Center,
  268. VerticalContentAlignment = VerticalAlignment.Center,
  269. VerticalAlignment = VerticalAlignment.Stretch,
  270. Background = new SolidColorBrush(Colors.WhiteSmoke),
  271. IsReadOnly = true,
  272. Margin = new Thickness(0, 5, 0, 5)
  273. };
  274. quantity.Bind(DoubleTextBox.ValueProperty, item, x => x.Quantity);
  275. quantity.Bind(Button.IsEnabledProperty, item, x => x.Editable);
  276. return quantity;
  277. }
  278. private FrameworkElement? QuantityColumn_Template(CoreRow row)
  279. {
  280. var item = LoadItem(row);
  281. var grid = new Grid();
  282. grid.Margin = new Thickness(5);
  283. grid.AddColumn(50);
  284. grid.AddColumn(30);
  285. grid.AddColumn(60);
  286. grid.AddColumn(30);
  287. grid.AddColumn(50);
  288. var none = new Button
  289. {
  290. Content = "None",
  291. };
  292. none.Bind(Button.IsEnabledProperty, item, x => x.Editable);
  293. none.Margin = new Thickness(0);
  294. none.Click += (o, e) => None_Click(item);
  295. grid.AddChild(none, 0, 0);
  296. var minus = new Button
  297. {
  298. Content = "-",
  299. };
  300. minus.Bind(Button.IsEnabledProperty, item, x => x.CanMinus);
  301. minus.Margin = new Thickness(5, 0, 0, 0);
  302. minus.Click += (o, e) => Minus_Click(item);
  303. grid.AddChild(minus, 0, 1);
  304. var quantity = new DoubleTextBox
  305. {
  306. HorizontalContentAlignment = HorizontalAlignment.Center,
  307. VerticalContentAlignment = VerticalAlignment.Center,
  308. VerticalAlignment = VerticalAlignment.Stretch,
  309. Margin = new Thickness(5, 0, 0, 0)
  310. };
  311. var style = new Style(typeof(DoubleTextBox));
  312. style.AddSetter(BackgroundProperty, Colors.LightYellow.ToBrush());
  313. style.AddDataTrigger()
  314. .Bind(item, x => x.Issued, true, new FuncConverter<double, bool>(issued => issued > item.Quantity && issued > 0))
  315. .AddSetter(BackgroundProperty, Colors.Salmon.ToBrush());
  316. quantity.Style = style;
  317. quantity.Bind(DoubleTextBox.ValueProperty, item, x => x.Issued);
  318. quantity.MinValue = 0;
  319. quantity.Bind(DoubleTextBox.MaxValueProperty, item, x => x.MaxValue);
  320. quantity.Bind(Button.IsEnabledProperty, item, x => x.Editable);
  321. grid.AddChild(quantity, 0, 2);
  322. var plus = new Button
  323. {
  324. Content = "+",
  325. };
  326. plus.Bind(Button.IsEnabledProperty, item, x => x.CanPlus);
  327. plus.Margin = new Thickness(5, 0, 0, 0);
  328. plus.Click += (o, e) => Plus_Click(item);
  329. grid.AddChild(plus, 0, 3);
  330. var all = new Button
  331. {
  332. Content = "All",
  333. };
  334. all.Bind(Button.IsEnabledProperty, item, x => x.Editable);
  335. all.Margin = new Thickness(5, 0, 0, 0);
  336. all.Click += (o, e) => All_Click(item);
  337. grid.AddChild(all, 0, 4);
  338. return grid;
  339. }
  340. private void None_Click(T item)
  341. {
  342. item.Issued = 0;
  343. }
  344. private void Minus_Click(T item)
  345. {
  346. item.Issued = Math.Max(0, item.Issued - 1);
  347. }
  348. private void Plus_Click(T item)
  349. {
  350. item.Issued = Math.Min(item.Issued + 1, item.MaxValue);
  351. }
  352. private void All_Click(T item)
  353. {
  354. item.Issued = Math.Min(Math.Max(0, item.Quantity), item.MaxValue);
  355. }
  356. protected override void Reload(
  357. Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort,
  358. CancellationToken token, Action<CoreTable?, Exception?> action)
  359. {
  360. foreach(var item in Items)
  361. {
  362. item.PropertyChanged += (o, e) => Recalculate();
  363. }
  364. Recalculate();
  365. base.Reload(criteria, columns, ref sort, token, action);
  366. }
  367. public Dictionary<Guid, double> GetQuantities()
  368. {
  369. return Items.ToDictionary(x => x.JRI.ID, x => x.Issued);
  370. }
  371. private void Recalculate()
  372. {
  373. if (!_observing) return;
  374. TotalQuantity = Items.Sum(x => x.Quantity);
  375. TotalIssued = Items.Sum(x => x.Issued);
  376. }
  377. public event PropertyChangedEventHandler? PropertyChanged;
  378. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  379. {
  380. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  381. }
  382. }