EmployeeRosterGrid.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using Comal.Classes;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using Syncfusion.Windows.Shared;
  11. namespace PRSDesktop
  12. {
  13. public class EmployeeRosterGrid : DynamicOneToManyGrid<Employee, EmployeeRoster>
  14. {
  15. private readonly UpDown _length;
  16. private readonly DatePicker _start;
  17. private readonly DynamicGridStyle off = new NewDynamicGridStyle
  18. {
  19. Background = new SolidColorBrush(Colors.LightYellow),
  20. Foreground = new SolidColorBrush(Colors.Black)
  21. };
  22. private readonly DynamicGridStyle on = new NewDynamicGridStyle
  23. {
  24. Background = new SolidColorBrush(Colors.LightGreen),
  25. Foreground = new SolidColorBrush(Colors.Black)
  26. };
  27. public EmployeeRosterGrid()
  28. {
  29. Options.AddRange(DynamicGridOption.DirectEdit);
  30. var lblLength = new Label
  31. {
  32. Content = "Roster Length",
  33. VerticalContentAlignment = VerticalAlignment.Center
  34. };
  35. lblLength.SetValue(DockPanel.DockProperty, Dock.Left);
  36. _length = new UpDown
  37. {
  38. Width = 50.0F,
  39. VerticalContentAlignment = VerticalAlignment.Center,
  40. HorizontalContentAlignment = HorizontalAlignment.Center,
  41. NumberDecimalDigits = 0,
  42. TextAlignment = TextAlignment.Center,
  43. MinValue = 0,
  44. AllowEdit = false
  45. };
  46. _length.SetValue(DockPanel.DockProperty, Dock.Left);
  47. _start = new DatePicker
  48. {
  49. Width = 100.0F,
  50. VerticalContentAlignment = VerticalAlignment.Center,
  51. HorizontalContentAlignment = HorizontalAlignment.Center
  52. };
  53. _start.SetValue(DockPanel.DockProperty, Dock.Right);
  54. var lblStart = new Label
  55. {
  56. Content = "Start Date",
  57. VerticalContentAlignment = VerticalAlignment.Center
  58. };
  59. lblStart.SetValue(DockPanel.DockProperty, Dock.Right);
  60. var lblPadding = new Label
  61. {
  62. Content = "",
  63. VerticalContentAlignment = VerticalAlignment.Center
  64. };
  65. lblPadding.SetValue(DockPanel.DockProperty, Dock.Left);
  66. var dock = new DockPanel();
  67. dock.Children.Add(lblLength);
  68. dock.Children.Add(_length);
  69. dock.Children.Add(_start);
  70. dock.Children.Add(lblStart);
  71. dock.Children.Add(lblPadding);
  72. var border = new Border
  73. {
  74. //BorderBrush = new SolidColorBrush(Colors.Gray),
  75. BorderThickness = new Thickness(0),
  76. //CornerRadius = new System.Windows.CornerRadius(5,5,0,0),
  77. //Margin = new System.Windows.Thickness(0, 0, 0, 2),
  78. Padding = new Thickness(0, 5, 0, 5),
  79. Child = dock
  80. };
  81. Header = border;
  82. }
  83. public override void Load(object item, Func<Type, CoreTable> PageDataHandler)
  84. {
  85. base.Load(item, PageDataHandler);
  86. _length.Value = (item as Employee).RosterLength;
  87. _length.ValueChanged += Length_ValueChanged;
  88. _start.SelectedDate = (item as Employee).RosterStart;
  89. _start.SelectedDateChanged += Start_SelectedDateChanged;
  90. }
  91. private void Length_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  92. {
  93. Item.RosterLength = (int)Math.Truncate((double)e.NewValue);
  94. ReconfigureRoster();
  95. }
  96. private void ReconfigureRoster()
  97. {
  98. var count = (int)_length.Value.Value;
  99. var start = _start.SelectedDate.Value;
  100. while (Items.Count < count)
  101. Items.Add(new EmployeeRoster { Day = Items.Count + 1 });
  102. while (Items.Count > count)
  103. Items.RemoveAt(Items.Count - 1);
  104. for (var i = 0; i < count; i++)
  105. Items[i].Description = Items.Count % 7 == 0
  106. ? start.AddDays(i).DayOfWeek.ToString()
  107. : string.Format("Day {0}", i + 1);
  108. Refresh(false, true);
  109. }
  110. private void Start_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
  111. {
  112. Item.RosterStart = _start.SelectedDate.Value;
  113. ReconfigureRoster();
  114. }
  115. protected override DynamicGridStyle GetStyle(CoreRow row, DynamicGridStyle style)
  116. {
  117. Expression<Func<EmployeeRoster, TimeSpan>>[] times =
  118. {
  119. x => x.Start,
  120. x => x.Finish,
  121. x => x.Break,
  122. x => x.Start2,
  123. x => x.Finish2,
  124. x => x.Break2
  125. };
  126. var sel = times.Any(x => row.Get(x).Ticks != 0L) ? on : off;
  127. return base.GetStyle(row, sel);
  128. }
  129. public override void ConfigureColumns(DynamicGridColumns columns)
  130. {
  131. var col = columns.FirstOrDefault(x => string.Equals(x.ColumnName, "UsualActivity.ID"));
  132. if (col != null)
  133. (col.Editor as ComboLookupEditor).OnBeforeGenerateLookups += (generator, entries) =>
  134. {
  135. (generator as LookupGenerator<Employee>).Items = new[] { Item };
  136. };
  137. SetVisibility<AllowEmployeeRosterSplitShifts>(columns.FirstOrDefault(x => string.Equals(x.ColumnName, "SplitShift")));
  138. SetVisibility<AllowEmployeeRosterSplitShifts>(columns.FirstOrDefault(x => string.Equals(x.ColumnName, "Start2")));
  139. SetVisibility<AllowEmployeeRosterSplitShifts>(columns.FirstOrDefault(x => string.Equals(x.ColumnName, "Finish2")));
  140. SetVisibility<AllowEmployeeRosterSplitShifts>(columns.FirstOrDefault(x => string.Equals(x.ColumnName, "Break2")));
  141. base.ConfigureColumns(columns);
  142. }
  143. private void SetVisibility<T>(DynamicGridColumn column) where T : ISecurityDescriptor, new()
  144. {
  145. if (column?.Editor == null)
  146. return;
  147. column.Editor.Visible = Security.IsAllowed<T>() ? Visible.Default : Visible.Hidden;
  148. }
  149. }
  150. }