123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using Comal.Classes;
- using InABox.Core;
- using InABox.DynamicGrid;
- using Syncfusion.Windows.Shared;
- namespace PRSDesktop
- {
- public class EmployeeRosterGrid : DynamicOneToManyGrid<Employee, EmployeeRoster>
- {
- private readonly UpDown _length;
- private readonly DatePicker _start;
- private readonly DynamicGridStyle off = new NewDynamicGridStyle
- {
- Background = new SolidColorBrush(Colors.LightYellow),
- Foreground = new SolidColorBrush(Colors.Black)
- };
- private readonly DynamicGridStyle on = new NewDynamicGridStyle
- {
- Background = new SolidColorBrush(Colors.LightGreen),
- Foreground = new SolidColorBrush(Colors.Black)
- };
- public EmployeeRosterGrid()
- {
- Options.AddRange(DynamicGridOption.DirectEdit);
- var lblLength = new Label
- {
- Content = "Roster Length",
- VerticalContentAlignment = VerticalAlignment.Center
- };
- lblLength.SetValue(DockPanel.DockProperty, Dock.Left);
- _length = new UpDown
- {
- Width = 50.0F,
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalContentAlignment = HorizontalAlignment.Center,
- NumberDecimalDigits = 0,
- TextAlignment = TextAlignment.Center,
- MinValue = 0,
- AllowEdit = false
- };
- _length.SetValue(DockPanel.DockProperty, Dock.Left);
- _start = new DatePicker
- {
- Width = 100.0F,
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalContentAlignment = HorizontalAlignment.Center
- };
- _start.SetValue(DockPanel.DockProperty, Dock.Right);
- var lblStart = new Label
- {
- Content = "Start Date",
- VerticalContentAlignment = VerticalAlignment.Center
- };
- lblStart.SetValue(DockPanel.DockProperty, Dock.Right);
- var lblPadding = new Label
- {
- Content = "",
- VerticalContentAlignment = VerticalAlignment.Center
- };
- lblPadding.SetValue(DockPanel.DockProperty, Dock.Left);
- var dock = new DockPanel();
- dock.Children.Add(lblLength);
- dock.Children.Add(_length);
- dock.Children.Add(_start);
- dock.Children.Add(lblStart);
- dock.Children.Add(lblPadding);
- var border = new Border
- {
- //BorderBrush = new SolidColorBrush(Colors.Gray),
- BorderThickness = new Thickness(0),
- //CornerRadius = new System.Windows.CornerRadius(5,5,0,0),
- //Margin = new System.Windows.Thickness(0, 0, 0, 2),
- Padding = new Thickness(0, 5, 0, 5),
- Child = dock
- };
- Header = border;
- }
- public override void Load(object item, Func<Type, CoreTable> PageDataHandler)
- {
- base.Load(item, PageDataHandler);
- _length.Value = (item as Employee).RosterLength;
- _length.ValueChanged += Length_ValueChanged;
- _start.SelectedDate = (item as Employee).RosterStart;
- _start.SelectedDateChanged += Start_SelectedDateChanged;
- }
- private void Length_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- Item.RosterLength = (int)Math.Truncate((double)e.NewValue);
- ReconfigureRoster();
- }
- private void ReconfigureRoster()
- {
- var count = (int)_length.Value.Value;
- var start = _start.SelectedDate.Value;
- while (Items.Count < count)
- Items.Add(new EmployeeRoster { Day = Items.Count + 1 });
- while (Items.Count > count)
- Items.RemoveAt(Items.Count - 1);
- for (var i = 0; i < count; i++)
- Items[i].Description = Items.Count % 7 == 0
- ? start.AddDays(i).DayOfWeek.ToString()
- : string.Format("Day {0}", i + 1);
- Refresh(false, true);
- }
- private void Start_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
- {
- Item.RosterStart = _start.SelectedDate.Value;
- ReconfigureRoster();
- }
- protected override DynamicGridStyle GetStyle(CoreRow row, DynamicGridStyle style)
- {
- Expression<Func<EmployeeRoster, TimeSpan>>[] times =
- {
- x => x.Start,
- x => x.Finish,
- x => x.Break,
- x => x.Start2,
- x => x.Finish2,
- x => x.Break2
- };
- var sel = times.Any(x => row.Get(x).Ticks != 0L) ? on : off;
- return base.GetStyle(row, sel);
- }
- public override void ConfigureColumns(DynamicGridColumns columns)
- {
- var col = columns.FirstOrDefault(x => string.Equals(x.ColumnName, "UsualActivity.ID"));
- if (col != null)
- (col.Editor as ComboLookupEditor).OnBeforeGenerateLookups += (generator, entries) =>
- {
- (generator as LookupGenerator<Employee>).Items = new[] { Item };
- };
- SetVisibility<AllowEmployeeRosterSplitShifts>(columns.FirstOrDefault(x => string.Equals(x.ColumnName, "SplitShift")));
- SetVisibility<AllowEmployeeRosterSplitShifts>(columns.FirstOrDefault(x => string.Equals(x.ColumnName, "Start2")));
- SetVisibility<AllowEmployeeRosterSplitShifts>(columns.FirstOrDefault(x => string.Equals(x.ColumnName, "Finish2")));
- SetVisibility<AllowEmployeeRosterSplitShifts>(columns.FirstOrDefault(x => string.Equals(x.ColumnName, "Break2")));
- base.ConfigureColumns(columns);
- }
- private void SetVisibility<T>(DynamicGridColumn column) where T : ISecurityDescriptor, new()
- {
- if (column?.Editor == null)
- return;
- column.Editor.Visible = Security.IsAllowed<T>() ? Visible.Default : Visible.Hidden;
- }
- }
- }
|