MinimumStockEditorControl.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Media;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. using InABox.DynamicGrid;
  7. using Syncfusion.Windows.Shared;
  8. namespace PRS.Shared;
  9. public class MinimumStockEditorControl : DynamicEnclosedEditorControl<ProductInstanceMinimumStock, MinimumStockEditor>
  10. {
  11. private readonly Grid _grid = new() {
  12. VerticalAlignment = VerticalAlignment.Stretch,
  13. HorizontalAlignment = HorizontalAlignment.Stretch
  14. };
  15. private readonly CheckBox _customStock = new() {
  16. Content = "Custom",
  17. IsThreeState = false,
  18. VerticalAlignment = VerticalAlignment.Center
  19. };
  20. private readonly IntegerTextBox _stockLevel = new(){
  21. Margin = new Thickness(5, 0, 0, 0),
  22. TextAlignment = TextAlignment.Center,
  23. VerticalAlignment = VerticalAlignment.Stretch,
  24. };
  25. private readonly CheckBox _customUsage = new()
  26. {
  27. Content = "# Days",
  28. Margin = new Thickness(5, 0, 0, 0),
  29. IsThreeState = false,
  30. VerticalAlignment = VerticalAlignment.Center
  31. };
  32. private readonly IntegerTextBox _usageDays = new()
  33. {
  34. Margin = new Thickness(5, 0, 0, 0),
  35. TextAlignment = TextAlignment.Center,
  36. VerticalAlignment = VerticalAlignment.Stretch,
  37. };
  38. private readonly Label _usageFactorLabel = new()
  39. {
  40. Content = "Usage Factor",
  41. VerticalAlignment = VerticalAlignment.Center,
  42. Margin = new Thickness(5, 0, 0, 0),
  43. };
  44. private readonly DoubleTextBox _usageFactor = new()
  45. {
  46. Margin = new Thickness(5, 0, 0, 0),
  47. TextAlignment = TextAlignment.Center,
  48. VerticalAlignment = VerticalAlignment.Stretch,
  49. };
  50. private readonly Label _stockFactorLabel = new()
  51. {
  52. Content = "Stock Factor",
  53. VerticalAlignment = VerticalAlignment.Center,
  54. Margin = new Thickness(5, 0, 0, 0)
  55. };
  56. private readonly DoubleTextBox _stockFactor = new()
  57. {
  58. Margin = new Thickness(5, 0, 0, 0),
  59. TextAlignment = TextAlignment.Center,
  60. VerticalAlignment = VerticalAlignment.Stretch,
  61. };
  62. public override void Configure()
  63. {
  64. }
  65. protected override FrameworkElement CreateEditor()
  66. {
  67. void AddEditor(FrameworkElement element, ref int col)
  68. {
  69. element.SetValue(Grid.ColumnProperty,col);
  70. _grid.Children.Add(element);
  71. col++;
  72. }
  73. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  74. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
  75. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  76. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
  77. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  78. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
  79. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  80. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
  81. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
  82. int col = 0;
  83. AddEditor(_customStock,ref col);
  84. _customStock.Checked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomStockLevel), true);
  85. _customStock.Unchecked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomStockLevel), true);
  86. AddEditor(_stockLevel,ref col);
  87. _stockLevel.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.StockLevel), false);
  88. AddEditor(_customUsage,ref col);
  89. _customUsage.Checked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomUsage), true);
  90. _customUsage.Unchecked += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.CustomUsage), true);
  91. AddEditor(_usageDays,ref col);
  92. _usageDays.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.UsageDays), false);
  93. AddEditor(_usageFactorLabel,ref col);
  94. AddEditor(_usageFactor,ref col);
  95. _usageFactor.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.UsageFactor), false);
  96. AddEditor(_stockFactorLabel,ref col);
  97. AddEditor(_stockFactor,ref col);
  98. _stockFactor.ValueChanged += (o,e) => UpdateValue(nameof(ProductInstanceMinimumStock.StockFactor), false);
  99. return _grid;
  100. }
  101. private void UpdateValue(string fieldname, bool checkenabled)
  102. {
  103. CheckChanged(fieldname);
  104. if (checkenabled)
  105. CheckEnabled(IsEnabled);
  106. }
  107. private void CheckEnabled(bool enabled)
  108. {
  109. _customUsage.IsEnabled = enabled;
  110. _usageDays.IsEnabled = enabled && _customUsage.IsChecked == true;
  111. _usageDays.Value = _customUsage.IsChecked != true ? 0 : (_usageDays.Value ?? 0) > 0 ? _usageDays.Value : 30;
  112. _usageFactor.IsEnabled = enabled && _customUsage.IsChecked == true;
  113. _usageFactor.Value = _customUsage.IsChecked != true ? 0 : (_usageFactor.Value ?? 0.0).IsEffectivelyGreaterThan(0.0) ? _usageFactor.Value : 1.0;
  114. _stockFactor.IsEnabled = enabled && _customUsage.IsChecked == true;
  115. _stockFactor.Value = _customUsage.IsChecked != true ? 0 : (_stockFactor.Value ?? 0.0).IsEffectivelyGreaterThan(0.0) ? _stockFactor.Value : 1.0;
  116. _customStock.IsEnabled = enabled;
  117. _stockLevel.IsEnabled = enabled && _customStock.IsChecked == true;
  118. _stockLevel.Value = _customStock.IsChecked != true ? 0 : (_stockLevel.Value ?? 0);
  119. }
  120. public override int DesiredWidth() => int.MaxValue;
  121. public override int DesiredHeight() => 30;
  122. public override void SetFocus() => _customUsage.Focus();
  123. public override void SetColor(Color color)
  124. {
  125. _usageDays.Background = new SolidColorBrush(color);
  126. _usageFactor.Background = new SolidColorBrush(color);
  127. _stockFactor.Background = new SolidColorBrush(color);
  128. _stockLevel.Background = new SolidColorBrush(color);
  129. }
  130. protected override IEnumerable<KeyValuePair<string, object?>> GetChildValues()
  131. {
  132. yield return new(nameof(ProductInstanceMinimumStock.CustomUsage), _customUsage.IsChecked ?? false);
  133. yield return new(nameof(ProductInstanceMinimumStock.UsageDays), (int)(_usageDays.Value ?? 0L));
  134. yield return new(nameof(ProductInstanceMinimumStock.UsageFactor), _usageFactor.Value ?? 0F);
  135. yield return new(nameof(ProductInstanceMinimumStock.StockFactor), _stockFactor.Value ?? 0F);
  136. yield return new(nameof(ProductInstanceMinimumStock.CustomStockLevel), _customStock.IsChecked ?? false);
  137. yield return new(nameof(ProductInstanceMinimumStock.StockLevel), (int)(_stockLevel.Value ?? 0L));
  138. }
  139. protected override object? GetChildValue(string property)
  140. {
  141. if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomUsage)))
  142. return _customUsage.IsChecked ?? false;
  143. if (string.Equals(property,nameof(ProductInstanceMinimumStock.UsageDays)))
  144. return (int)(_usageDays.Value ?? 0L);
  145. if (string.Equals(property,nameof(ProductInstanceMinimumStock.UsageFactor)))
  146. return _usageFactor.Value ?? 0F;
  147. if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockFactor)))
  148. return _stockFactor.Value ?? 0F;
  149. if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomStockLevel)))
  150. return _customStock.IsChecked ?? false;
  151. if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockLevel)))
  152. return (int)(_stockLevel.Value ?? 0L);
  153. return null;
  154. }
  155. protected override void SetChildValue(string property, object? value)
  156. {
  157. if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomUsage)))
  158. _customUsage.IsChecked = (bool)(value ?? false);
  159. else if(string.Equals(property,nameof(ProductInstanceMinimumStock.UsageDays)))
  160. _usageDays.Value = (int)(value ?? 0);
  161. else if (string.Equals(property,nameof(ProductInstanceMinimumStock.UsageFactor)))
  162. _usageFactor.Value = (double)(value ?? 0.0);
  163. else if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockFactor)))
  164. _stockFactor.Value = (double)(value ?? 0.0);
  165. else if (string.Equals(property,nameof(ProductInstanceMinimumStock.CustomStockLevel)))
  166. _customStock.IsChecked = (bool)(value ?? false);
  167. else if (string.Equals(property,nameof(ProductInstanceMinimumStock.StockLevel)))
  168. _stockLevel.Value = (int)(value ?? 0);
  169. }
  170. public override void SetEnabled(bool enabled)
  171. {
  172. base.SetEnabled(enabled);
  173. CheckEnabled(enabled);
  174. }
  175. }