123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.Diagnostics.CodeAnalysis;
- using System.Globalization;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Input;
- using System.Windows.Media;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using Microsoft.Win32;
- using PRS.Shared;
- using PRSDesktop.Grids;
- using Syncfusion.UI.Xaml.Grid;
- using Syncfusion.UI.Xaml.Grid.Converter;
- using Syncfusion.UI.Xaml.Grid.Helpers;
- using Syncfusion.Windows.Tools.Controls;
- using Syncfusion.XlsIO;
- using Activity = Comal.Classes.Activity;
- using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;
- namespace PRSDesktop
- {
- public class LeaveValue
- {
- public LeaveValue(Guid id, Guid employee, DateTime date, Guid activity, LeaveRequestStatus status, bool standard)
- {
- ID = id;
- Employee = employee;
- Date = date;
- Activity = activity;
- Status = status;
- Standard = standard;
- }
- public Guid ID { get; set; }
- public Guid Employee { get; set; }
- public DateTime Date { get; set; }
- public Guid Activity { get; set; }
- public LeaveRequestStatus Status { get; set; }
- public bool Standard { get; set; }
- }
- public class LeaveBackgroundConverter : IValueConverter
- {
- public Dictionary<Guid, Color>? Colors { get; set; }
-
- // Guid - EmployeeID
- // DateTime - Employee Start Date
- // DateTime - Employee Finish Date
- // EmployeeRoster[] Roster Data
- // DateTime - Roster Start
- public List<Tuple<Guid, DateTime, DateTime, EmployeeRosterItem[], DateTime>> Rosters { get; set; }
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if(value is not LeaveValue val)
- return DependencyProperty.UnsetValue;
- if (val.ID == Guid.Empty)
- {
- var tuple = Rosters?.FirstOrDefault(x => x.Item1 == val.Employee);
- if (tuple == null)
- return new SolidColorBrush(System.Windows.Media.Colors.Red) { Opacity = 0.8 };
-
- if ( (!tuple.Item2.IsEmpty() && (tuple.Item2 > val.Date)) || (!tuple.Item3.IsEmpty() && (tuple.Item3 < val.Date)))
- return new SolidColorBrush(System.Windows.Media.Colors.LightGray) { Opacity = 0.8 };
-
- var roster = RosterUtils.GetRoster(tuple?.Item4, tuple?.Item5, val.Date);
- return (roster?.Enabled == true)
- ? roster?.Duration < 5.0F
- ? new System.Windows.Media.LinearGradientBrush(
- System.Windows.Media.Colors.LightYellow,
- System.Windows.Media.Colors.LightGray,
- 90.0F
- ) { Opacity = 0.8 }
- : new SolidColorBrush(System.Windows.Media.Colors.LightYellow) { Opacity = 0.8 }
- : new SolidColorBrush(System.Windows.Media.Colors.LightGray) { Opacity = 0.8 };
- }
- else
- {
- if (val.Status != LeaveRequestStatus.Approved)
- return new SolidColorBrush(System.Windows.Media.Colors.DimGray) { Opacity = 0.8 };
- if (Colors != null)
- if (Colors.ContainsKey(val.Activity))
- return new SolidColorBrush(Colors[val.Activity]) { Opacity = 0.5 };
- }
- return DependencyProperty.UnsetValue;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- public class LeaveForegroundConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value is LeaveValue val
- && val.ID != Guid.Empty
- && val.Status != LeaveRequestStatus.Approved)
- {
- return new SolidColorBrush(Colors.LightGray);
- }
- return DependencyProperty.UnsetValue;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- public class LeaveFontStyleConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value is LeaveValue val
- && val.ID != Guid.Empty
- && val.Status != LeaveRequestStatus.Approved)
- {
- return FontStyles.Italic;
- }
- return DependencyProperty.UnsetValue;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- public class LeaveFontWeightConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- return FontWeights.Bold;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- public class LeaveContentConverter : IValueConverter
- {
- public Dictionary<Guid, string>? Abbreviations { get; set; }
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value is not LeaveValue val)
- return DependencyProperty.UnsetValue;
- return Abbreviations?.ContainsKey(val.Activity) == true ? Abbreviations[val.Activity] : DependencyProperty.UnsetValue;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- /// <summary>
- /// Interaction logic for LeaveCalendar.xaml
- /// </summary>
- public partial class LeaveCalendar : UserControl
- {
- private readonly CoreTable _activities;
- private readonly Dictionary<Guid, string> _activityCodes = new();
- private readonly Dictionary<Guid, Color> _activityColors = new();
- private readonly Dictionary<Guid, string> _employees = new();
- private readonly List<Tuple<Guid, DateTime, DateTime, EmployeeRosterItem[], DateTime>> _rosters = new();
- private readonly CoreTable _employeeteams;
-
- private LeaveSettings _settings;
- private readonly Dictionary<Guid, string> _teams = new();
- private bool bReady;
- private LeaveRequestGrid? lg;
- private StandardLeaveGrid? sg;
-
- public LeaveCalendar()
- {
- InitializeComponent();
- //dataGrid.ScrollMode = ScrollMode.Async;
- LoadSettings();
- StartDate.DateTime = DateTime.Today;
- EndDate.DateTime = DateTime.Today.AddYears(1);
- var setups = new MultiQuery();
- setups.Add<EmployeeTeam>(null, null, new SortOrder<EmployeeTeam>(x => x.TeamLink.Code).ThenBy(x => x.EmployeeLink.Name));
- setups.Add<Activity>(new Filter<Activity>(x => x.IsLeave).IsEqualTo(true));
- setups.Query();
-
- _employeeteams = setups.Get<EmployeeTeam>();
- _activities = setups.Get<Activity>();
- _teams = _employeeteams.ToDictionary<EmployeeTeam, Guid, string>(c => c.TeamLink.ID, c => c.TeamLink.Name);
- if (_teams.ContainsKey(Guid.Empty))
- _teams.Remove(Guid.Empty);
- var teams = _employeeteams.ToDictionary<EmployeeTeam, Guid, string>(c => c.TeamLink.ID, c => c.TeamLink.Name);
- teams[CoreUtils.FullGuid] = "Multiple Teams";
- Teams.ItemsSource = teams;
- Teams.SelectedValue = _settings.GroupID;
- ChangeSelectedTeams(_settings.GroupID);
- SelectedTeams.ItemsSource = _teams;
- foreach (KeyValuePair<Guid, string> pair in SelectedTeams.Items)
- if (_settings.SelectedGroups.Contains(pair.Key))
- SelectedTeams.SelectedItems.Add(pair);
- LoadEmployees(false);
-
- foreach (var row in _activities.Rows)
- {
- var id = row.Get<Activity, Guid>(c => c.ID);
- var color = row.Get<Activity, string>(c => c.Color);
- if (!string.IsNullOrWhiteSpace(color))
- _activityColors[id] = (Color)ColorConverter.ConvertFromString(color);
- var code = Codify(row.Get<Activity, string>(c => c.Description));
- _activityCodes[id] = Codify(code);
- }
-
- var rosterstarts = _employeeteams.ToDictionary<EmployeeTeam, Guid, DateTime>(x => x.EmployeeLink.ID, x => x.EmployeeLink.RosterStart);
- var startdates = _employeeteams.ToDictionary<EmployeeTeam, Guid, DateTime>(x => x.EmployeeLink.ID, x => x.EmployeeLink.StartDate);
- var finishdates = _employeeteams.ToDictionary<EmployeeTeam, Guid, DateTime>(x => x.EmployeeLink.ID, x => x.EmployeeLink.FinishDate);
- var rosters = _employeeteams.ToDictionary<EmployeeTeam, Guid, String>(x => x.EmployeeLink.ID, x => x.EmployeeLink.Roster);
-
- foreach (var empid in startdates.Keys)
- {
- var startdate = startdates[empid];
- var finishdate = finishdates[empid];
- var roster = !String.IsNullOrWhiteSpace(rosters[empid])
- ? Serialization.Deserialize<List<EmployeeRosterItem>>(rosters[empid]).ToArray()
- : null;
- var rosterstart = rosterstarts[empid];
- _rosters.Add(new Tuple<Guid, DateTime, DateTime, EmployeeRosterItem[], DateTime>(empid, startdate, finishdate, roster, rosterstart));
- }
-
- bReady = true;
- }
- private static string Codify(string name)
- {
- if (string.IsNullOrWhiteSpace(name))
- return "??";
- var result = "";
- var comps = name.ToUpper().Split(' ');
- foreach (var comp in comps)
- if (comp.Any())
- result += comp.First();
- return string.IsNullOrWhiteSpace(result) ? "??" : result;
- }
- [MemberNotNull(nameof(_settings))]
- private void LoadSettings()
- {
- _settings = new UserConfiguration<LeaveSettings>().Load();
- if (!_settings.SelectedGroups.Any() && _settings.GroupID != Guid.Empty)
- _settings.SelectedGroups.Add(_settings.GroupID);
- if (_settings.ListSize == 0.0F)
- _settings.ListSize = 200.0F;
- }
- private void SaveSettings()
- {
- if (!bReady)
- return;
- try
- {
- _settings.GroupID = Teams.SelectedValue != null ? (Guid)Teams.SelectedValue : Guid.Empty;
- _settings.SelectedGroups.Clear();
- foreach (var sel in SelectedTeams.SelectedItems)
- {
- var grp = (KeyValuePair<Guid, string>)sel;
- _settings.SelectedGroups.Add(grp.Key);
- }
- _settings.SelectedEmployees.Clear();
- foreach (var sel in SelectedEmployees.SelectedItems)
- {
- var grp = (KeyValuePair<Guid, string>)sel;
- _settings.SelectedEmployees.Add(grp.Key);
- }
- if (EmployeeGrid.RowDefinitions[1].ActualHeight != 0.0F)
- _settings.ListSize = EmployeeGrid.RowDefinitions[1].ActualHeight;
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
- }
- Task.Run(() => { new UserConfiguration<LeaveSettings>().Save(_settings); });
- }
-
- public void Refresh()
- {
- if (!bReady)
- return;
-
- using (new WaitCursor())
- {
- _employees.Clear();
- var crosstab = new List<LeaveValue>();
- MultiQuery query = new MultiQuery();
- var empfilter = new Filter<LeaveRequest>(x => x.EmployeeLink.ID).IsEqualTo(CoreUtils.FullGuid);
- foreach (var sel in SelectedEmployees.Items)
- {
- var emp = (KeyValuePair<Guid, string>)sel;
- if (SelectedEmployees.SelectedItems.Contains(emp))
- {
- empfilter = empfilter.Or(x => x.EmployeeLink.ID).IsEqualTo(emp.Key);
- _employees[emp.Key] = emp.Value;
- }
- }
- var start = StartDate.DateTime ?? DateTime.Today;
- var end = EndDate.DateTime ?? DateTime.Today.AddYears(1);
- var leavefilter = new Filter<LeaveRequest>(x => x.To).IsGreaterThanOrEqualTo(start)
- .And(x => x.From).IsLessThanOrEqualTo(end)
- .And(x => x.Status).IsNotEqualTo(LeaveRequestStatus.Rejected);
- leavefilter.Ands.Add(empfilter);
- query.Add<LeaveRequest>(
- leavefilter,
- new Columns<LeaveRequest>(x => x.ID)
- .Add(x => x.EmployeeLink.ID)
- .Add(x => x.From)
- .Add(x=>x.FromTime)
- .Add(x => x.To)
- .Add(x=>x.ToTime)
- .Add(x => x.LeaveType.ID)
- .Add(x => x.LeaveType.Description)
- .Add(x => x.Status)
- );
- query.Add<StandardLeave>(
- new Filter<StandardLeave>(x => x.To).IsGreaterThanOrEqualTo(start).And(x => x.From).IsLessThanOrEqualTo(end),
- new Columns<StandardLeave>(x => x.ID)
- .Add(x => x.From)
- .Add(x=>x.FromTime)
- .Add(x => x.To)
- .Add(x=>x.ToTime)
- .Add(x => x.LeaveType.ID)
- );
- query.Query();
- var leaverequests = query.Get<LeaveRequest>();
- var standardleaves = query.Get<StandardLeave>();
- foreach (var standardrow in standardleaves.Rows)
- {
- var id = standardrow.Get<StandardLeave, Guid>(x => x.ID);
- var actid = standardrow.Get<StandardLeave, Guid>(x => x.LeaveType.ID);
-
- var from = standardrow.Get<StandardLeave, DateTime>(x => x.From);
- from = from < start ? start : from;
- var to = standardrow.Get<StandardLeave, DateTime>(x => x.To);
- to = to > end ? end : to;
-
- for (var date = from; date <= to; date = date.AddDays(1))
- foreach (var empid in _employees.Keys)
- crosstab.Add(new LeaveValue(id, empid, date, actid, LeaveRequestStatus.Approved, true));
- }
- foreach (var leaverow in leaverequests.Rows)
- {
- var id = leaverow.Get<LeaveRequest, Guid>(x => x.ID);
- var empid = leaverow.Get<LeaveRequest, Guid>(x => x.EmployeeLink.ID);
- var actid = leaverow.Get<LeaveRequest, Guid>(x => x.LeaveType.ID);
- var status = leaverow.Get<LeaveRequest, LeaveRequestStatus>(x => x.Status);
-
- var from = leaverow.Get<LeaveRequest, DateTime>(x => x.From);
- from = from < start ? start : from;
- var to = leaverow.Get<LeaveRequest, DateTime>(x => x.To);
- to = to > end ? end : to;
- var totime = leaverow.Get<LeaveRequest, TimeSpan>(x => x.ToTime);
- for (var date = from; date < to.Add(totime); date = date.AddDays(1))
- crosstab.Add(new LeaveValue(id, empid, date, actid, status, false));
- }
-
- var data = new DataTable();
- data.Columns.Add("Date", typeof(DateTime));
- foreach (var employee in _employees.Keys)
- data.Columns.Add(employee.ToString(), typeof(object));
- for (var date = start; date <= end; date = date.AddDays(1))
- {
- var values = new List<object> { date };
- foreach (var employee in _employees.Keys)
- {
- LeaveValue value = null;
- var roster = _rosters.FirstOrDefault(x => x.Item1 == employee);
- if (roster != null)
- {
- var curroster = RosterUtils.GetRoster(roster.Item4, roster.Item5, date);
- if (curroster?.Enabled == true)
- {
- value = crosstab.FirstOrDefault(x => x.Employee.Equals(employee) && x.Date.Equals(date) && x.Standard);
- if (value == null)
- value = crosstab.FirstOrDefault(x => x.Employee.Equals(employee) && x.Date.Equals(date) && !x.Standard);
- }
- }
- values.Add(value ?? new LeaveValue(Guid.Empty, employee, date, Guid.Empty, LeaveRequestStatus.InProgress, false));
-
- }
- data.Rows.Add(values.ToArray());
- }
- dataGrid.ItemsSource = data;
- }
- }
- private void DataGrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
- {
- e.Column.TextAlignment = TextAlignment.Center;
- e.Column.HorizontalHeaderContentAlignment = HorizontalAlignment.Center;
- e.Column.ColumnSizer = GridLengthUnitType.None;
- var value = (e.Column.ValueBinding as Binding)!;
- if (value.Path.Path.Equals("Date"))
- {
- e.Column.Width = 100;
- e.Column.HeaderStyle = Resources["DateHeaderStyle"] as Style;
- e.Column.AllowFocus = false;
- }
- else
- {
- var style = new Style(typeof(GridCell));
- style.Setters.Add(new Setter(BackgroundProperty,
- new Binding(value.Path.Path)
- {
- Converter = new LeaveBackgroundConverter
- {
- Colors = _activityColors,
- Rosters = _rosters
- }
- }));
- style.Setters.Add(new Setter(ForegroundProperty, new Binding(value.Path.Path) { Converter = new LeaveForegroundConverter() }));
- style.Setters.Add(new Setter(FontStyleProperty, new Binding(value.Path.Path) { Converter = new LeaveFontStyleConverter() }));
- style.Setters.Add(new Setter(FontWeightProperty, new Binding(value.Path.Path) { Converter = new LeaveFontWeightConverter() }));
- e.Column.CellStyle = style;
- e.Column.Width = 30;
- e.Column.HeaderStyle = Resources["ContentHeaderStyle"] as Style;
- e.Column.HeaderText = _employees[Guid.Parse(value.Path.Path)];
- e.Column.DisplayBinding = new Binding
- { Path = new PropertyPath(e.Column.MappingName), Converter = new LeaveContentConverter { Abbreviations = _activityCodes } };
- //e.Column.ValueBinding = new Binding() { Path = new PropertyPath(e.Column.MappingName), Converter = new LeaveContentConverter() };
- //e.Column.UseBindingValue = true;
- e.Column.AllowFocus = true;
- }
- }
- //private void DataGrid_FilterItemsPopulating(object sender, GridFilterItemsPopulatingEventArgs e)
- //{
- // e.FilterControl.FilterMode = FilterMode.AdvancedFilter;
- //}
- private void GetSelectionData(out DateTime from, out DateTime to, out Guid[] ids)
- {
- var emps = new List<Guid>();
- from = DateTime.MaxValue;
- to = DateTime.MinValue;
- foreach (var cell in dataGrid.GetSelectedCells())
- {
- var binding = (cell.Column.ValueBinding as Binding)!;
- if (Guid.TryParse(binding.Path.Path, out var emp))
- if (!emps.Contains(emp))
- emps.Add(emp);
- var row = (cell.RowData as DataRowView)!;
- var date = (DateTime)row.Row.ItemArray.First()!;
- if (date < from)
- from = date;
- if (date > to)
- to = date;
- }
- ids = emps.Any() ? emps.ToArray() : _employees.Keys.ToArray();
- }
- private bool HasData()
- {
- foreach (var cell in dataGrid.GetSelectedCells())
- {
- if (!cell.IsDataRowCell)
- continue;
- var propertyCollection = dataGrid.View.GetPropertyAccessProvider();
- var cellValue = propertyCollection.GetValue(cell.RowData, cell.Column.MappingName);
- if (cellValue is LeaveValue leaveValue && leaveValue.ID != Guid.Empty)
- return true;
- }
- return false;
- }
-
- private Guid[] GetLeaveIDs(bool standard)
- {
- List<Guid> result = new List<Guid>();
- foreach (var cell in dataGrid.GetSelectedCells())
- {
- if (cell.IsDataRowCell)
- {
- var propertyCollection = dataGrid.View.GetPropertyAccessProvider();
- var cellvalue = propertyCollection.GetValue(cell.RowData, cell.Column.MappingName) as LeaveValue;
- if (cellvalue != null && (cellvalue.ID != Guid.Empty) && (cellvalue.Standard == standard) && !result.Contains(cellvalue.ID))
- result.Add(cellvalue.ID);
- }
- }
- return result.ToArray();
- }
- private LeaveValue? GetData(GridCellInfo cell)
- {
- if (!cell.IsDataRowCell)
- return null;
- var propertyCollection = dataGrid.View.GetPropertyAccessProvider();
- var cellValue = propertyCollection.GetValue(cell.RowData, cell.Column.MappingName);
- return cellValue is LeaveValue leaveValue && leaveValue.ID != Guid.Empty ? leaveValue : null;
- }
- private bool HasData(GridCellInfo cell)
- {
- if (cell?.IsDataRowCell != true)
- return false;
- var propertyCollection = dataGrid.View.GetPropertyAccessProvider();
- var cellValue = propertyCollection.GetValue(cell.RowData, cell.Column.MappingName);
- return cellValue is LeaveValue leaveValue && leaveValue.ID != Guid.Empty;
- }
- private void CreateLeaveRequestClick(object sender, RoutedEventArgs e)
- {
- var activity = ((sender as MenuItem)!.Tag as Activity)!;
- GetSelectionData(out var from, out var to, out var ids);
- var leaves = new List<LeaveRequest>();
- foreach (var id in ids)
- {
- var leave = new LeaveRequest
- {
- From = from,
- To = to
- };
- leave.LeaveType.ID = activity.ID;
- leave.EmployeeLink.ID = id;
- leaves.Add(leave);
- }
- lg ??= new LeaveRequestGrid();
- if (lg.EditItems(leaves.ToArray()))
- {
- Progress.ShowModal("Checking Forms...", progress =>
- {
- List<LeaveRequestForm> updates = new List<LeaveRequestForm>();
- var table = new Client<ActivityForm>().Query(
- new Filter<ActivityForm>(x => x.Activity.ID).IsEqualTo(leaves.First().LeaveType.ID)
- .And(x => x.Form.AppliesTo)
- .IsEqualTo(typeof(LeaveRequest).EntityName().Split('.').Last())
- );
- foreach (var leave in leaves)
- {
- foreach (var row in table.Rows)
- {
- var activityform = row.ToObject<ActivityForm>();
- var leaveform = new LeaveRequestForm();
- leaveform.Parent.ID = leave.ID;
- leaveform.Form.ID = activityform.Form.ID;
- leaveform.Form.Synchronise(activityform.Form);
- var model =
- new DigitalFormDataModel<LeaveRequest, LeaveRequestLink, LeaveRequestForm>(leave,
- leaveform);
- leaveform.Description = model.EvaluateExpression(activityform.Form.DescriptionExpression) ?? activityform.Form.Description;
- updates.Add(leaveform);
- }
- }
- if (updates.Any())
- {
- progress.Report("Saving forms...");
- new Client<LeaveRequestForm>().Save(updates, "Created from Leave Calendar");
- }
- });
- Refresh();
- }
- }
-
- private void CreateStandardLeaveClick(object sender, RoutedEventArgs e)
- {
- GetSelectionData(out var from, out var to, out var ids);
- var leave = new StandardLeave
- {
- From = from,
- To = to
- };
- sg ??= new StandardLeaveGrid();
- if (sg.EditItems(new StandardLeave[] { leave }))
- Refresh();
- }
- private IEnumerable<LeaveRequest> GetSelectedLeaveRequests()
- {
-
- var ids = GetLeaveIDs(false);
- if (ids.Any())
- {
- lg ??= new LeaveRequests();
- var table = new Client<LeaveRequest>().Query(
- new Filter<LeaveRequest>(x => x.ID).InList(ids),
- lg.LoadEditorColumns()
- );
- var result = table.Rows.Select(x => x.ToObject<LeaveRequest>()).ToArray();
- return result;
- }
- return new LeaveRequest[] { };
- }
- private IEnumerable<StandardLeave> GetSelectedStandardLeaves()
- {
- var ids = GetLeaveIDs(true);
- if (ids.Any())
- {
- sg ??= new StandardLeaveGrid();
- var table = new Client<StandardLeave>().Query(
- new Filter<StandardLeave>(x => x.ID).InList(ids),
- sg.LoadEditorColumns()
- );
- var result = table.Rows.Select(x => x.ToObject<StandardLeave>()).ToArray();
- return result;
- }
- return new StandardLeave[] { };
- }
- private void EditLeaveClick(object sender, RoutedEventArgs e)
- {
- var requests = GetSelectedLeaveRequests().ToArray();
- if (requests.Any())
- {
- lg ??= new LeaveRequests();
- if (lg.EditItems(requests))
- Refresh();
- return;
- }
- var standards = GetSelectedStandardLeaves().ToArray();
- if (standards.Any())
- {
- sg ??= new StandardLeaveGrid();
- if (sg.EditItems(standards))
- Refresh();
- return;
- }
- MessageBox.Show("Nothing to edit!");
- }
- private void DeleteLeaveClick(object sender, RoutedEventArgs e)
- {
- var requests = GetSelectedLeaveRequests().ToArray();
- if (requests.Any())
- {
- if (MessageBox.Show("Delete Leave Request Information?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning) ==
- MessageBoxResult.Yes)
- {
- using (new WaitCursor())
- new Client<LeaveRequest>().Delete(requests, "Deleted from Calendar");
- Refresh();
- }
- return;
- }
-
- var standards = GetSelectedStandardLeaves().ToArray();
- if (standards.Any())
- {
- if (MessageBox.Show("Delete Standard Leave Information?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning) ==
- MessageBoxResult.Yes)
- {
- using (new WaitCursor())
- new Client<StandardLeave>().Delete(standards, "Deleted from Calendar");
- Refresh();
- }
- return;
- }
- MessageBox.Show("Nothing to delete!");
- }
- // private void DataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
- // {
- // if (e.RightButton == MouseButtonState.Pressed)
- // {
- // var vc = dataGrid.GetVisualContainer();
- // var point = e.GetPosition(vc);
- // var rci = vc.PointToCellRowColumnIndex(point);
- //
- // if (rci.RowIndex == 0 || rci.ColumnIndex == 0)
- // return;
- //
- // var menu = new ContextMenu();
- // if (!HasData(dataGrid.CurrentCellInfo))
- // foreach (var row in _activities.Rows)
- // {
- // var createleave = new MenuItem
- // {
- // Header = row.Get<Activity, string>(c => c.Description),
- // Tag = row.ToObject<Activity>()
- // };
- // createleave.Click += CreateLeaveRequestClick;
- // menu.Items.Add(createleave);
- // }
- //
- // if (HasData())
- // {
- // var deleteleave = new MenuItem { Header = "Delete Leave" };
- // deleteleave.Click += DeleteLeaveClick;
- // menu.Items.Add(deleteleave);
- // }
- //
- // dataGrid.ContextMenu = menu;
- // menu.IsOpen = true;
- // }
- // }
- private void DataGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
- {
- var vc = dataGrid.GetVisualContainer();
- var p = Mouse.GetPosition(vc);
- var rci = vc.PointToCellRowColumnIndex(p);
- if (rci.RowIndex < 1 || rci.ColumnIndex < 1)
- {
- e.Handled = true;
- return;
- }
- dataGrid.ContextMenu.Items.Clear();
- var bCreate = !HasData(dataGrid.CurrentCellInfo);
- var bEdit = HasData();
- if (bCreate)
- {
- var createleaveheader = new MenuItem { Header = "Create Leave Request" };
- foreach (var row in _activities.Rows)
- {
- var createleave = new MenuItem
- {
- Header = row.Get<Activity, string>(c => c.Description),
- Tag = row.ToObject<Activity>()
- };
- createleave.Click += CreateLeaveRequestClick;
- createleaveheader.Items.Add(createleave);
- }
- dataGrid.ContextMenu.Items.Add(createleaveheader);
-
- dataGrid.ContextMenu.Items.Add(new Separator());
- var createstandardleave = new MenuItem { Header = "Create Standard Leave" };
- createstandardleave.Click += CreateStandardLeaveClick;
- dataGrid.ContextMenu.Items.Add(createstandardleave);
- }
- if (bEdit && bCreate)
- dataGrid.ContextMenu.Items.Add(new Separator());
- if (bEdit)
- {
- var editleave = new MenuItem { Header = "Edit Leave" };
- editleave.Click += EditLeaveClick;
- dataGrid.ContextMenu.Items.Add(editleave);
- dataGrid.ContextMenu.Items.Add(new Separator());
- var deleteleave = new MenuItem { Header = "Delete Leave" };
- deleteleave.Click += DeleteLeaveClick;
- dataGrid.ContextMenu.Items.Add(deleteleave);
- var cell = GetData(dataGrid.CurrentCellInfo); // not-null because bEdit.
- if (cell != null)
- {
- dataGrid.ContextMenu.Items.Add(new Separator());
- var formsItem = new MenuItem { Header = "Digital Forms" };
- DynamicGridUtils.PopulateFormMenu<LeaveRequestForm, LeaveRequest, LeaveRequestLink>(
- formsItem,
- cell.ID,
- () => new Client<LeaveRequest>().Load(new Filter<LeaveRequest>(x => x.ID).IsEqualTo(cell.ID)).First());
- dataGrid.ContextMenu.Items.Add(formsItem);
- }
- }
- }
- private void DateTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (!bReady)
- return;
- Refresh();
- }
- private void SyncSelectedTeams(Guid teamid)
- {
- var bOldReady = bReady;
- bReady = false;
- if (teamid != CoreUtils.FullGuid)
- {
- SelectedTeams.SelectedItems.Clear();
- foreach (KeyValuePair<Guid, string> sel in SelectedTeams.Items)
- if (sel.Key == teamid)
- SelectedTeams.SelectedItems.Add(sel);
- }
- LoadEmployees(true);
- bReady = bOldReady;
- }
- private void GroupsSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (Teams.SelectedValue == null)
- return;
- var teamid = (Guid)Teams.SelectedValue;
- ChangeSelectedTeams(teamid);
- SyncSelectedTeams(teamid);
- }
- private void ChangeSelectedTeams(Guid teamid)
- {
- if (teamid == CoreUtils.FullGuid)
- {
- EmployeeGrid.RowDefinitions[1].Height = new GridLength(_settings.ListSize, GridUnitType.Pixel);
- EmployeeGrid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Auto);
- }
- else
- {
- var oldbReady = bReady;
- bReady = false;
- EmployeeGrid.RowDefinitions[1].Height = new GridLength(0, GridUnitType.Pixel);
- EmployeeGrid.RowDefinitions[2].Height = new GridLength(0, GridUnitType.Pixel);
- bReady = oldbReady;
- }
- //bHandled = false;
- }
- //bool bHandled = false;
- //private void SelectedTeams_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- //{
- // if (!bReady)
- // return;
- // if (!bHandled)
- // LoadEmployees();
- // bHandled = !bHandled;
- //}
- private void SelectedTeams_ItemChecked(object sender, ItemCheckedEventArgs e)
- {
- if (!bReady)
- return;
- LoadEmployees(true);
- }
- private void LoadEmployees(bool selectall)
- {
- var emps = new Dictionary<Guid, string>();
- foreach (var pair in SelectedTeams.SelectedItems)
- if (pair != null)
- {
- var key = ((KeyValuePair<Guid, string>)pair).Key;
- foreach (var row in _employeeteams.Rows.Where(r =>
- r.Get<EmployeeTeam, Guid>(c => c.TeamLink.ID).Equals(key) || key.Equals(CoreUtils.FullGuid)))
- emps[row.Get<EmployeeTeam, Guid>(c => c.EmployeeLink.ID)] = row.Get<EmployeeTeam, string>(c => c.EmployeeLink.Name);
- }
- var bOldReady = bReady;
- bReady = false;
- SelectedEmployees.ItemsSource = emps;
- foreach (var item in SelectedEmployees.Items)
- {
- var pair = (KeyValuePair<Guid, string>)item;
- if (_settings.SelectedEmployees.Contains(pair.Key) || selectall)
- SelectedEmployees.SelectedItems.Add(item);
- }
- bReady = bOldReady;
- SaveSettings();
- Refresh();
- }
- private void EmployeesSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!bReady || e.AddedItems.Count == 0 || e.AddedItems[0] == null)
- return;
- //ReloadColumns();
- SaveSettings();
- Refresh();
- }
- private void Export_Click(object sender, RoutedEventArgs e)
- {
- var options = new ExcelExportingOptions
- {
- ExcelVersion = ExcelVersion.Excel2013
- };
- var excelEngine = dataGrid.ExportToExcel(dataGrid.View, options);
- var workBook = excelEngine.Excel.Workbooks[0];
- var dlg = new SaveFileDialog
- {
- Filter = "Excel Files (*.xlsx)|*.xlsx",
- FileName = typeof(LeaveRequest).EntityName().Split('.').Last() + ".xlsx"
- };
- if (dlg.ShowDialog() == true)
- try
- {
- workBook.SaveAs(dlg.FileName);
- Process.Start(new ProcessStartInfo(dlg.FileName) { UseShellExecute = true });
- }
- catch (Exception e2)
- {
- MessageBox.Show("Error saving spreadsheet!\n\n" + e2.Message);
- }
- //workBook.SaveAs("Leave Requests.xlsx");
- }
- private void SelectedTeams_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- SaveSettings();
- }
- }
- }
|