123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Threading;
- using com.sun.jmx.mbeanserver;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using Syncfusion.UI.Xaml.Scheduler;
- using Action = System.Action;
- using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;
- namespace PRSDesktop
- {
-
- public partial class AssignmentView2 : UserControl
- {
- #region Private Fields
-
- private class AssignmentViewModel : ScheduleAppointment, IAssignmentViewModel
- {
- public Guid AssignmentID { get; set; }
- public Guid EmployeeID { get; set; }
- public Brush AppointmentForeground { get; set; }
- public int Number { get; set; }
- public string JobNumber { get; set; }
- public BitmapImage Completed { get; set; }
- public BitmapImage HasDelivery { get; set; }
- public BitmapImage Locked { get; set; }
- public BitmapImage TimeSheet { get; set; }
- }
-
-
- private readonly BitmapImage back = PRSDesktop.Resources.back.AsBitmapImage(32, 32);
- private readonly BitmapImage box = PRSDesktop.Resources.box_sml.AsBitmapImage(32, 32);
- private readonly BitmapImage first = PRSDesktop.Resources.first.AsBitmapImage(32, 32);
- private readonly BitmapImage last = PRSDesktop.Resources.last.AsBitmapImage(32, 32);
- private readonly BitmapImage locked = PRSDesktop.Resources.lock_sml.AsBitmapImage(32, 32);
- private readonly BitmapImage next = PRSDesktop.Resources.next.AsBitmapImage(32, 32);
- private readonly BitmapImage tick = PRSDesktop.Resources.tick_sml.AsBitmapImage(32, 32);
- private readonly BitmapImage timesheet = PRSDesktop.Resources.clock_sml.AsBitmapImage(32, 32);
-
- private bool bReady = false;
- private Dictionary<Guid, String> _teams = new Dictionary<Guid, string>() { { CoreUtils.FullGuid, "All Teams" } };
- private CoreTable _employees = new CoreTable(typeof(EmployeeTeam));
- private CoreTable _assignments = new CoreTable(typeof(Assignment));
-
- private Guid _selectedassignment = Guid.Empty;
- private DateTime _selecteddate = DateTime.MinValue;
- private Guid _selectedteam = Guid.Empty;
- private Guid[] _selectedemployees = new Guid[] { };
- private double _zoom = 100.0D;
- private bool _sidebaropen = false;
-
- private AssignmentGrid _assignmentgrid = null;
-
- #endregion
-
- #region Events
-
- public event CustomiseAssignments CustomiseAssignments;
- public event CustomiseAssignmentsContextMenu CustomiseContextMenu;
-
- public event AssignmentSelectionChangedEvent AssignmentSelectionChanged;
- public event AssignmentChangedEvent AssignmentChanged;
- public event AssignmentViewSettingsChanged SettingsChanged;
-
- #endregion
-
- public AssignmentView2()
- {
-
- InitializeComponent();
- Loaded += OnLoaded;
- PrevMonth.Content = new Image { Source = first };
- PrevDay.Content = new Image { Source = back };
- NextDay.Content = new Image { Source = next };
- NextMonth.Content = new Image { Source = last };
- }
- private void OnLoaded(object sender, RoutedEventArgs args)
- {
- Loaded -= OnLoaded;
- UpdateUI(UpdateDateVisible);
- UpdateUI(UpdateCanShowHideSideBar);
- UpdateUI(UpdateTimeInterval);
- UpdateUI(UpdateStartHour);
- UpdateUI(UpdateEndHour);
- UpdateUI(UpdateWorkingHourColors);
- }
- #region Settings
- private void DoSettingsChanged()
- {
- var settings = new AssignmentViewSettings()
- {
- SelectedDate = this.SelectedDate,
- SelectedTeam = this.SelectedTeam,
- SelectedEmployees = this.SelectedEmployees,
- Zoom = this.Zoom,
- SideBarOpen = this.SideBarOpen
- };
- SettingsChanged?.Invoke(this, settings);
- }
-
- #endregion
-
- #region Public Properties
-
- public DateTime SelectedDate
- {
- get => _selecteddate;
- set
- {
- if (value != _selecteddate)
- {
- _selecteddate = value;
- UpdateUI(UpdateSelectedDate);
- DoSettingsChanged();
- }
- }
- }
-
- public Guid SelectedTeam
- {
- get => _selectedteam;
- set
- {
- if (value != _selectedteam)
- {
- _selectedteam = value;
- UpdateUI(UpdateSelectedTeam);
- DoSettingsChanged();
- }
- }
- }
-
- public Guid[] SelectedEmployees
- {
- get => _selectedemployees;
- set
- {
- if (new HashSet<Guid>(value).SetEquals(_selectedemployees) == false)
- {
- _selectedemployees = value;
- UpdateUI(UpdateSelectedEmployees);
- DoSettingsChanged();
- }
- }
- }
-
- public double Zoom
- {
- get => _zoom;
- set
- {
- if (Math.Abs(value - _zoom) > 0.001D)
- {
- _zoom = value;
- UpdateUI(UpdateZoom);
- DoSettingsChanged();
- }
- }
- }
- public bool SideBarOpen
- {
- get => _sidebaropen;
- set
- {
- if (value != _sidebaropen)
- {
- _sidebaropen = value;
- UpdateUI(UpdateSideBarOpen);
- DoSettingsChanged();
- }
- }
- }
-
- public int StartHour
- {
- get => (int)GetValue(StartHourProperty);
- set
- {
- if (value != StartHour)
- {
- SetValue(StartHourProperty, value);
- UpdateUI(UpdateStartHour);
- }
- }
- }
-
- public int EndHour
- {
- get => (int)GetValue(EndHourProperty);
- set
- {
- if (value != EndHour)
- {
- SetValue(EndHourProperty, value);
- UpdateUI(UpdateEndHour);
- }
- }
- }
-
- public Color WorkingHourColor
- {
- get => (Color)GetValue(WorkingHourColorProperty);
- set
- {
- if (!Color.Equals(value, WorkingHourColor))
- {
- SetValue(WorkingHourColorProperty, value);
- UpdateUI(UpdateWorkingHourColors);
- }
- }
- }
-
- public Color NonWorkingHourColor
- {
- get => (Color)GetValue(NonWorkingHourColorProperty);
- set
- {
- if (Color.Equals(value, NonWorkingHourColor))
- {
- SetValue(NonWorkingHourColorProperty, value);
- UpdateUI(UpdateWorkingHourColors);
- }
- }
- }
-
- public AssignmentViewTimeInterval TimeInterval
- {
- get => (AssignmentViewTimeInterval)GetValue(TimeIntervalProperty);
- set
- {
- if (value != TimeInterval)
- {
- SetValue(TimeIntervalProperty, value);
- UpdateUI(UpdateTimeInterval);
- }
- }
- }
-
- public bool CanShowHideSideBar
- {
- get => (bool)GetValue(CanShowHideSideBarProperty);
- set
- {
- if (value != CanShowHideSideBar)
- {
- SetValue(CanShowHideSideBarProperty, value);
- UpdateUI(UpdateCanShowHideSideBar);
- }
- }
- }
-
- public bool DateVisible
- {
- get => (bool)GetValue(DateVisibleProperty);
- set
- {
- if (value != DateVisible)
- {
- SetValue(DateVisibleProperty, value);
- UpdateUI(UpdateDateVisible);
- }
- }
- }
-
-
-
- #endregion
-
- #region Dependency Properties
-
- public static readonly DependencyProperty StartHourProperty =
- DependencyProperty.Register(
- "StartHour",
- typeof(int),
- typeof(AssignmentView2),
- new PropertyMetadata(6)
- );
-
- public static readonly DependencyProperty EndHourProperty =
- DependencyProperty.Register(
- "EndHour",
- typeof(int),
- typeof(AssignmentView2),
- new PropertyMetadata(18)
- );
-
- public static readonly DependencyProperty WorkingHourColorProperty =
- DependencyProperty.Register(
- "WorkingHourColor",
- typeof(Color),
- typeof(AssignmentView2),
- new PropertyMetadata(Colors.Transparent)
- );
-
- public static readonly DependencyProperty NonWorkingHourColorProperty =
- DependencyProperty.Register(
- "NonWorkingHourColor",
- typeof(Color),
- typeof(AssignmentView2),
- new PropertyMetadata(Colors.Gainsboro)
- );
-
- public static readonly DependencyProperty TimeIntervalProperty =
- DependencyProperty.Register(
- "TimeInterval",
- typeof(AssignmentViewTimeInterval),
- typeof(AssignmentView2),
- new PropertyMetadata(AssignmentViewTimeInterval.FifteenMinutes)
- );
- public static readonly DependencyProperty CanShowHideSideBarProperty =
- DependencyProperty.Register(
- "CanShowHideSideBar",
- typeof(bool),
- typeof(AssignmentView2),
- new PropertyMetadata(true)
- );
-
- public static readonly DependencyProperty DateVisibleProperty =
- DependencyProperty.Register(
- "DateVisible",
- typeof(bool),
- typeof(AssignmentView2),
- new PropertyMetadata(true)
- );
-
- #endregion
-
- #region UI Sync Stuff
-
- // These functions should "push" property values to the UI
- // They should _not_ update the properties themselves
- // to avoid circular updates
- private bool _updatingUI = false;
- private void UpdateUI(Action action)
- {
- _updatingUI = true;
- try
- {
- action?.Invoke();
- }
- finally
- {
- _updatingUI = false;
- }
- }
-
- private void UpdateStartHour()
- {
- StartTime.Value = new TimeSpan(StartHour, 0, 0);
- StartAMPM.Content = StartHour < 12 ? " AM " : StartHour > 12 ? " PM " : " NN ";
- UpdateWorkingHourColors();
- Bookings.SelectedDate = _selecteddate.Date;
- var margin = IntervalToTimeSpan(TimeInterval);
- var start = new TimeSpan(StartHour, 0, 0);
- if (start >= margin)
- start -= margin;
- Bookings.DisplayDate = _selecteddate.Add(start);
- }
-
- private void UpdateEndHour()
- {
- EndTime.Value = new TimeSpan(EndHour, 0, 0);
- EndAMPM.Content = EndHour < 12 ? " AM " : EndHour > 12 ? " PM " : " NN ";
- UpdateWorkingHourColors();
- }
-
- private void UpdateWorkingHourColors()
- {
-
- if (Bookings.ResourceCollection == null)
- return;
-
- var resources = Bookings.ResourceCollection.OfType<SchedulerResource>();
- if (!resources.Any())
- return;
-
- var regions = new ObservableCollection<SpecialTimeRegion>();
- foreach (var resource in resources)
- {
- if (StartHour > 0)
- {
- var before = new SpecialTimeRegion
- {
- StartTime = SelectedDate.Date,
- EndTime = SelectedDate.Date.AddHours(StartHour),
- ResourceIdCollection = new ObservableCollection<object> { resource.Id },
- Background = new SolidColorBrush(NonWorkingHourColor),
- Foreground = new SolidColorBrush(Colors.Black),
- CanMergeAdjacentRegions = true
- };
- regions.Add(before);
- }
- if (EndHour < 24)
- {
- var after = new SpecialTimeRegion
- {
- StartTime = SelectedDate.Date.AddHours(EndHour),
- EndTime = SelectedDate.Date.AddDays(1).AddMilliseconds(-1),
- ResourceIdCollection = new ObservableCollection<object> { resource.Id },
- Background = new SolidColorBrush(NonWorkingHourColor),
- Foreground = new SolidColorBrush(Colors.Black),
- CanMergeAdjacentRegions = true
- };
- regions.Add(after);
- }
- if (StartHour < EndHour)
- {
- var working = new SpecialTimeRegion
- {
- StartTime = SelectedDate.Date.AddHours(StartHour),
- EndTime = SelectedDate.Date.AddHours(EndHour),
- ResourceIdCollection = new ObservableCollection<object> { resource.Id },
- Background = new SolidColorBrush(WorkingHourColor),
- Foreground = new SolidColorBrush(Colors.Black),
- CanMergeAdjacentRegions = true
- };
- regions.Add(working);
- }
- }
- Bookings.DaysViewSettings.SpecialTimeRegions = regions;
- }
-
- private void UpdateSelectedTeam()
- {
- Teams.SelectedValue = SelectedTeam;
- LoadEmployees();
- }
-
- private void UpdateTimeInterval()
- {
- try
- {
- Bookings.DaysViewSettings.TimeInterval = IntervalToTimeSpan(TimeInterval);
- }
- catch (Exception e)
- {
- // If the view is invisible, setting the time interval throws an exeception (internal scroll viewer is still set to null)
- // So we only want to log an error if the view is actually visible
- if (Bookings.IsVisible)
- Logger.Send(LogType.Error, "", String.Format("Unable to update TimeInterval!\n{0}\n\n{1}", e.Message, e.StackTrace));
- }
-
- }
- private void UpdateCanShowHideSideBar()
- {
- ShowSideBar.Visibility = CanShowHideSideBar && !SideBarOpen
- ? Visibility.Visible
- : Visibility.Collapsed;
- HideSideBar.Visibility = CanShowHideSideBar && SideBarOpen
- ? Visibility.Visible
- : Visibility.Collapsed;
- }
- private void UpdateDateVisible()
- {
- DateRow.Height = DateVisible
- ? new GridLength(35, GridUnitType.Pixel)
- : new GridLength(0, GridUnitType.Pixel);
- }
-
- private void UpdateSideBarOpen()
- {
- SideBar.Width = SideBarOpen
- ? new GridLength(200, GridUnitType.Pixel)
- : new GridLength(0, GridUnitType.Pixel);
- DateBorder.BorderThickness = SideBarOpen
- ? new Thickness(0.75, 0.75, 0, 0.75)
- : new Thickness(0.75, 0.75, 0.75, 0.75);
- UpdateCanShowHideSideBar();
- }
- private void UpdateZoom()
- {
- ResizeIntervals(Bookings.ActualHeight);
- }
- private void UpdateSelectedDate()
- {
- CurrentDate.Text = string.Format("{0:ddd, dd MMMM yyyy}", SelectedDate);
- Bookings.SelectedDate = _selecteddate.Date;
-
- var margin = IntervalToTimeSpan(TimeInterval);
- var start = new TimeSpan(StartHour, 0, 0);
- if (start >= margin)
- start -= margin;
- Bookings.DisplayDate = _selecteddate.Add(start);
- }
-
- public void SetDisplayWindow(int start, int end)
- {
- if (!double.IsNaN(ActualHeight))
- {
- ResizeIntervals(ActualHeight);
- SelectedDate = SelectedDate; // to trigger a reset?
- }
- }
- private void LoadEmployees()
- {
- var emps = new Dictionary<Guid, string>();
- foreach (var row in _employees.Rows.Where(r =>
- r.Get<EmployeeTeam, Guid>(c => c.TeamLink.ID).Equals(SelectedTeam) || SelectedTeam.Equals(CoreUtils.FullGuid)))
- emps[row.Get<EmployeeTeam, Guid>(c => c.EmployeeLink.ID)] = row.Get<EmployeeTeam, string>(c => c.EmployeeLink.Name);
- Employees.ItemsSource = emps;
- }
- private void UpdateSelectedEmployees()
- {
- List<SchedulerResource> resources = new List<SchedulerResource>();
- Employees.SelectedItems.Clear();
- foreach (var item in Employees.Items)
- {
- var emp = (KeyValuePair<Guid, string>)item;
- if (SelectedEmployees.Contains(emp.Key))
- {
- Employees.SelectedItems.Add(item);
- resources.Add(
- new SchedulerResource
- {
- Name = emp.Value,
- Id = emp.Key.ToString()
- }
- );
- }
- }
- Bookings.ResourceCollection = new ObservableCollection<object>(resources);
- Bookings.DaysViewSettings.ResourceHeaderSize = resources.Count <= 1 ? 0 : 50;
- }
-
-
- #endregion
-
- public Assignment FindAssignment(Guid id)
- {
- return _assignments.Rows.FirstOrDefault(r => r.Get<Assignment, Guid>(c => c.ID).Equals(id))?.ToObject<Assignment>();
- }
- private IAssignmentViewModel FindModel(Guid id)
- {
- var appointments = Bookings.ItemsSource as ScheduleAppointmentCollection;
- return appointments.FirstOrDefault(x => (x as IAssignmentViewModel).AssignmentID.Equals(id)) as IAssignmentViewModel;
- }
-
- public void AddAssignment(Assignment assignment)
- {
- var row = _assignments.NewRow();
- _assignments.LoadRow(row, assignment);
- _assignments.Rows.Add(row);
- LoadModels(assignment.ID);
- }
- public void GetWindow(TimeSpan time, ref TimeSpan start, ref TimeSpan finish)
- {
-
- foreach (var row in _assignments.Rows)
- {
- var curstart = row.Get<Assignment, TimeSpan>(x => x.Start);
- var curfinish = row.Get<Assignment, TimeSpan>(x => x.Finish);
- if ((curfinish <= time) && (curfinish >= start))
- start = curfinish;
- if ((curstart >= time) && (curstart <= finish))
- finish = curstart;
- }
- }
-
- public CoreTable Data() => _assignments;
- public void Setup()
- {
- _employees = new Client<EmployeeTeam>().Query(
- null,
- new Columns<EmployeeTeam>(
- x => x.TeamLink.ID,
- x => x.TeamLink.Code,
- x => x.TeamLink.Name,
- x => x.EmployeeLink.ID,
- x => x.EmployeeLink.Name
- ),
- new SortOrder<EmployeeTeam>(x => x.TeamLink.Code).ThenBy(x => x.EmployeeLink.Name)
- );
-
- _employees.IntoDictionary<EmployeeTeam, Guid, String>(
- _teams,
- x => x.TeamLink.ID,
- r => r.Get<EmployeeTeam,String>(c=>c.TeamLink.Name)
- );
- Teams.ItemsSource = _teams;
-
- bReady = true;
- }
-
- private void ResizeIntervals(double height)
- {
- if (double.IsNaN(height) || height == 0.0F)
- return;
- var zoomedheight = height * (Zoom / 100.0F);
- Bookings.DaysViewSettings.TimeIntervalSize = zoomedheight / 24;
- }
- private void Bookings_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- ResizeIntervals(e.NewSize.Height);
- }
-
- public void Refresh()
- {
-
- var filter = new Filter<Assignment>(x => x.Date).IsGreaterThanOrEqualTo(SelectedDate.Date)
- .And(x => x.Date).IsLessThanOrEqualTo(SelectedDate.Date.AddDays(1))
- .And(x =>x.EmployeeLink.ID).InList(SelectedEmployees);
-
- _assignments = new Client<Assignment>().Query(
- filter,
- new Columns<Assignment>(
- x => x.ID,
- x => x.Title,
- x => x.ActivityLink.ID,
- x => x.ActivityLink.Color,
- x => x.ActivityLink.Description,
- x => x.ActivityLink.Deleted,
- x => x.JobLink.ID,
- x => x.JobLink.Name,
- x => x.JobLink.JobNumber,
- x => x.JobLink.Deleted,
- x => x.ITP.ID,
- x => x.ITP.Code,
- x => x.ITP.Description,
- x => x.ITP.Deleted,
- x => x.Number,
- x => x.Description,
- x => x.Date,
- x => x.Start,
- x => x.Duration,
- x => x.Finish,
- x => x.LeaveRequestLink.ID,
- x => x.Completed,
- x => x.Delivery.ID,
- x => x.Delivery.Notes,
- x => x.Delivery.Deleted,
- x => x.TimeSheetLink.ID,
- x => x.TimeSheetLink.Deleted,
- x => x.EmployeeLink.ID,
- x => x.Task.ID,
- x => x.Task.Number
- )
- );
- CustomiseAssignments?.Invoke(this, _assignments);
- LoadModels(Guid.Empty);
- }
- private void LoadModels(Guid id)
- {
- IAssignmentViewModel selected = null;
- var appointments = new ScheduleAppointmentCollection();
- foreach (var row in _assignments.Rows)
- {
- var assignment = row.ToObject<Assignment>();
- var sColor = assignment.ActivityLink.Color;
- var bgColor = string.IsNullOrWhiteSpace(sColor) ? Colors.White : (Color)ColorConverter.ConvertFromString(sColor);
- var jobdesc = new List<string>();
- var itask = assignment.Task.Number;
- if (itask > 0)
- jobdesc.Add(string.Format("Task #{0}", itask));
- if (assignment.JobLink.IsValid())
- {
- var job = assignment.JobLink.JobNumber;
- if (assignment.ITP.IsValid())
- job = string.Format("{0} {1}", job, assignment.ITP.Code);
- job = string.Format("({0})", job);
- jobdesc.Add(job);
- }
- var date = assignment.Date;
- var sNote = assignment.Description + (assignment.Delivery.IsValid()
- ? "\n" + assignment.Delivery.Notes
- : "");
- var model = new AssignmentViewModel
- {
- Subject = string.Format("{0}", assignment.Title?.Trim()),
- JobNumber = string.Format(" {0} ", string.Join(" ", jobdesc)),
- Number = assignment.Number,
- Notes = sNote.Trim(),
- StartTime = date.Add(assignment.Start),
- EndTime = date.Add(assignment.Finish),
- AssignmentID = assignment.ID,
- EmployeeID = assignment.EmployeeLink.ID,
- //AllDay = false,
- AppointmentForeground = new SolidColorBrush(Colors.Black),
- AppointmentBackground = new SolidColorBrush(bgColor),
- Locked = assignment.LeaveRequestLink.IsValid() ? locked : null,
- Completed = !assignment.LeaveRequestLink.IsValid() && !assignment.Completed.IsEmpty() ? tick : null,
- HasDelivery = !assignment.LeaveRequestLink.IsValid() && assignment.Delivery.IsValid() ? box : null,
- TimeSheet = assignment.TimeSheetLink.IsValid() ? timesheet : null,
- ResourceIdCollection = new ObservableCollection<object>() { assignment.EmployeeLink.ID.ToString() }
- };
- appointments.Add(model);
- if (model.AssignmentID == id)
- selected = model;
- }
-
- Bookings.ItemsSource = appointments;
- ResizeIntervals(Bookings.ActualHeight);
- UpdateWorkingHourColors();
- }
- public void UpdateAssignment(Assignment assignment)
- {
- var row = _assignments.Rows.FirstOrDefault(r => r.Get<Assignment, Guid>(x => x.ID).Equals(assignment.ID));
- _assignments.LoadRow(row, assignment);
- var model = FindModel(assignment.ID);
-
- var sColor = assignment.ActivityLink.Color;
- var bgColor = string.IsNullOrWhiteSpace(sColor) ? Colors.White : (Color)ColorConverter.ConvertFromString(sColor);
- var date = assignment.Date;
- var leaveid = assignment.LeaveRequestLink.ID;
- var job = "";
- if (assignment.JobLink.IsValid())
- {
- job = assignment.JobLink.JobNumber;
- if (assignment.ITP.IsValid())
- job = string.Format("{0} {1}", job, assignment.ITP.Code);
- job = string.Format(" ({0}) ", job);
- }
- model.Subject = assignment.Title;
- model.Number = assignment.Number;
- model.JobNumber = job;
- model.Notes = assignment.Description;
- model.StartTime = date.Add(assignment.Start);
- model.EndTime = date.Add(assignment.Finish);
- model.AppointmentBackground = new SolidColorBrush(bgColor);
- model.Locked = assignment.LeaveRequestLink.IsValid() ? locked : null;
- model.Completed = !assignment.LeaveRequestLink.IsValid() && !assignment.Completed.IsEmpty() ? tick : null;
- model.HasDelivery = !assignment.LeaveRequestLink.IsValid() && assignment.Delivery.IsValid() ? box : null;
- model.TimeSheet = assignment.TimeSheetLink.IsValid() ? timesheet : null;
- //Bookings.UpdateLayout();
- }
-
- #region Responding to UI Events
-
- private void PrevMonth_Click(object sender, RoutedEventArgs e)
- {
- SelectedDate = SelectedDate.AddDays(-7);
- AssignmentSelectionChanged?.Invoke(this, null);
- Refresh();
- }
- private void PrevDay_Click(object sender, RoutedEventArgs e)
- {
- SelectedDate = SelectedDate.AddDays(-1);
- AssignmentSelectionChanged?.Invoke(this, null);
- Refresh();
- }
- private void CurrentDate_MouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- SelectedDate = DateTime.Today;
- AssignmentSelectionChanged?.Invoke(this, null);
- Refresh();
- }
- private void NextDay_Click(object sender, RoutedEventArgs e)
- {
- SelectedDate = SelectedDate.AddDays(1);
- AssignmentSelectionChanged?.Invoke(this, null);
- Refresh();
- }
- private void NextMonth_Click(object sender, RoutedEventArgs e)
- {
- SelectedDate = SelectedDate.AddDays(7);
- AssignmentSelectionChanged?.Invoke(this, null);
- Refresh();
- }
- private void Teams_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (_updatingUI)
- return;
- SelectedTeam = (Guid)Teams.SelectedValue;
- SelectedEmployees = _employees.Rows
- .Where(r => (SelectedTeam == CoreUtils.FullGuid) || (r.Get<EmployeeTeam, Guid>(c => c.TeamLink.ID) == SelectedTeam))
- .Select(r => r.Get<EmployeeTeam, Guid>(c => c.EmployeeLink.ID))
- .Distinct()
- .ToArray();
- Refresh();
- }
-
- private void Employees_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (_updatingUI)
- return;
- SelectedEmployees = Employees.SelectedItems.Select(x => ((KeyValuePair<Guid, String>)x).Key).ToArray();
- Refresh();
- }
-
- private void StartTime_OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (_updatingUI)
- return;
- StartHour = (int)StartTime.Value.Value.TotalHours;
- }
-
- private void EndTime_OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (_updatingUI)
- return;
- EndHour = (int)EndTime.Value.Value.TotalHours;
- }
-
- private void OpenSideBar_Click(object sender, RoutedEventArgs e)
- {
- SideBarOpen = true;
- }
- private void CloseSideBar_Click(object sender, RoutedEventArgs e)
- {
- SideBarOpen = false;
- }
-
- #endregion
-
- private void Schedule_AppointmentEditorOpening(object sender, AppointmentEditorOpeningEventArgs e)
- {
- if (e.Appointment != null)
- {
- var model = (AssignmentViewModel)e.Appointment;
- Assignment ass = null;
- using (new WaitCursor())
- {
- ass = new Client<Assignment>().Load(new Filter<Assignment>(x => x.ID).IsEqualTo(model.AssignmentID))
- .FirstOrDefault();
- }
- if (ass != null)
- EditAssignment(ass);
- else
- MessageBox.Show("Cannot Load Assignment!");
- }
- else
- {
- var res = e.Resource;
- if (res != null)
- {
- var ass = new Assignment();
- ass.EmployeeLink.ID = Guid.Parse(res.Id.ToString());
- ass.Date = e.DateTime;
- ass.Duration = new TimeSpan(1, 0, 0);
- var ag = new DynamicDataGrid<Assignment>();
- if (ag.EditItems(new[] { ass }))
- Refresh();
- }
- else
- {
- MessageBox.Show("No Employee Selected!");
- }
- }
- e.Cancel = true;
- }
-
- // private void Schedule_AppointmentStartDragging(object sender, AppointmentStartDraggingEventArgs e)
- // {
- // var model = e.Appointment as AssignmentViewModel;
- // if (model == null || model.Locked != null || model.TimeSheet != null)
- // e.Cancel = true;
- // }
- //
- // private void Schedule_AppointmentEndDragging(object sender, AppointmentEndDraggingEventArgs e)
- // {
- // var model = (AssignmentViewModel)e.Appointment;
- //
- // var row = model != null ? Assignments.Rows.FirstOrDefault(r => r.Get<Assignment, Guid>(x => x.ID).Equals(model.AssignmentID)) : null;
- // if (row != null)
- // {
- // Assignment ass = null;
- // using (new WaitCursor())
- // {
- // ass = new Client<Assignment>().Load(new Filter<Assignment>(x => x.ID).IsEqualTo(model.AssignmentID)).FirstOrDefault();
- // }
- //
- // if (ass != null)
- // {
- // var resource = e.Resources.FirstOrDefault() as Resource;
- // var employeeid = Guid.Parse(resource.ResourceName);
- // ass.EmployeeLink.ID = employeeid;
- //
- // var newtime = e.To.TimeOfDay;
- // if (e.To.Minute >= 45)
- // newtime = new TimeSpan(e.To.Hour, 45, 0);
- // else if (e.To.Minute >= 30)
- // newtime = new TimeSpan(e.To.Hour, 30, 0);
- // else if (e.To.Minute >= 15)
- // newtime = new TimeSpan(e.To.Hour, 15, 0);
- // else
- // newtime = new TimeSpan(e.To.Hour, 0, 0);
- // ass.Date = e.To.Date;
- // ass.Start = newtime;
- //
- // using (new WaitCursor())
- // {
- // new Client<Assignment>().Save(ass, "Dragged by User", (obj, args) => { });
- // }
- //
- // AssignmentChanged?.Invoke(this, ass);
- //
- // Refresh();
- //
- // e.Cancel = true;
- // }
- // else
- // {
- // MessageBox.Show("Unable to load Assigmment!");
- // }
- // }
- // else
- // {
- // MessageBox.Show("Unable to locate Assignment!");
- // }
- // }
- //
- // private void Schedule_AppointmentEndResizing(object sender, AppointmentEndResizingEventArgs e)
- // {
- // var model = (AssignmentViewModel)e.Appointment;
- // var row = model != null ? Assignments.Rows.FirstOrDefault(r => r.Get<Assignment, Guid>(x => x.ID).Equals(model.AssignmentID)) : null;
- // if (row != null)
- // {
- // Assignment ass = null;
- // using (new WaitCursor())
- // {
- // ass = new Client<Assignment>().Load(new Filter<Assignment>(x => x.ID).IsEqualTo(model.AssignmentID)).FirstOrDefault();
- // }
- //
- // if (ass != null)
- // {
- // var newtime = e.To;
- // if (e.To.Minute >= 45)
- // newtime = new DateTime(e.To.Year, e.To.Month, e.To.Day, e.To.Hour, 45, 0);
- // else if (e.To.Minute >= 30)
- // newtime = new DateTime(e.To.Year, e.To.Month, e.To.Day, e.To.Hour, 30, 0);
- // else if (e.To.Minute >= 15)
- // newtime = new DateTime(e.To.Year, e.To.Month, e.To.Day, e.To.Hour, 15, 0);
- // else
- // newtime = new DateTime(e.To.Year, e.To.Month, e.To.Day, e.To.Hour, 0, 0);
- // ass.Date = newtime;
- // using (new WaitCursor())
- // {
- // new Client<Assignment>().Save(ass, "Dragged by User", (obj, args) => { });
- // }
- //
- // AssignmentChanged?.Invoke(this, ass);
- //
- // Refresh();
- // e.Cancel = true;
- // }
- // else
- // {
- // MessageBox.Show("Unable to load Assigmment!");
- // }
- // }
- // else
- // {
- // MessageBox.Show("Unable to locate Assignment!");
- // }
- // }
- //
- // private AssignmentViewTimeInterval TimeSpanToInterval(TimeSpan time)
- // {
- // if (time <= new TimeSpan(0, 5, 0))
- // return AssignmentViewTimeInterval.FiveMinutes;
- // if (time <= new TimeSpan(0, 6, 0))
- // return AssignmentViewTimeInterval.SixMinutes;
- // if (time <= new TimeSpan(0, 10, 0))
- // return AssignmentViewTimeInterval.TenMinutes;
- // if (time <= new TimeSpan(0, 15, 0))
- // return AssignmentViewTimeInterval.FifteenMinutes;
- // if (time <= new TimeSpan(0, 20, 0))
- // return AssignmentViewTimeInterval.TwentyMinutes;
- // if (time <= new TimeSpan(0, 30, 0))
- // return AssignmentViewTimeInterval.ThirtyMinutes;
- // return AssignmentViewTimeInterval.SixyMinutes;
- // }
- private TimeSpan IntervalToTimeSpan(AssignmentViewTimeInterval interval)
- {
- switch (interval)
- {
- case AssignmentViewTimeInterval.FiveMinutes:
- return new TimeSpan(0, 5, 0);
- case AssignmentViewTimeInterval.SixMinutes:
- return new TimeSpan(0, 6, 0);
- case AssignmentViewTimeInterval.TenMinutes:
- return new TimeSpan(0, 10, 0);
- case AssignmentViewTimeInterval.FifteenMinutes:
- return new TimeSpan(0, 15, 0);
- case AssignmentViewTimeInterval.TwentyMinutes:
- return new TimeSpan(0, 20, 0);
- case AssignmentViewTimeInterval.ThirtyMinutes:
- return new TimeSpan(0, 30, 0);
- default:
- return new TimeSpan(1, 0, 0);
- }
- }
-
- // private int GetDivider(AssignmentViewTimeInterval interval)
- // {
- // switch (interval)
- // {
- // case AssignmentViewTimeInterval.FiveMinutes:
- // return 12;
- // case AssignmentViewTimeInterval.SixMinutes:
- // return 10;
- // case AssignmentViewTimeInterval.TenMinutes:
- // return 6;
- // case AssignmentViewTimeInterval.FifteenMinutes:
- // return 4;
- // case AssignmentViewTimeInterval.TwentyMinutes:
- // return 3;
- // case AssignmentViewTimeInterval.ThirtyMinutes:
- // return 2;
- // default:
- // return 1;
- // }
- // }
- //
- // private TimeSpan GetTime(int row)
- // {
- // int divider = GetDivider(TimeInterval);
- // var hours = row / divider;
- // var mins = row % divider * (60 / divider);
- // return new TimeSpan(hours, mins, 0);
- // }
- // private TimeSpan GetTime(Point point)
- // {
- // var view = Bookings.FindVisualChildren<ScrollViewer>().LastOrDefault(x => x.Name.Equals("PART_ScrollViewer"));
- // var intervalheight = (Bookings.ActualHeight - 30.0F) / (24 * 4);
- // var row = (int)Math.Truncate((view.VerticalOffset + point.Y - 30.0F) / Bookings.DaysViewSettings.TimeIntervalSize);
- // var result = GetTime(row);
- // return result;
- // }
- // private void Bookings_Drop(object sender, DragEventArgs e)
- // {
- // if (e.Data.GetDataPresent("Comal.Classes.Delivery"))
- // {
- // var delivery = (e.Data.GetData("Comal.Classes.Delivery") as CoreRow[]).FirstOrDefault();
- // if (delivery == null)
- // return;
- //
- // var assignment = new Assignment();
- //
- // var resource = Bookings.ResourceCollection.OfType<SchedulerResource>().ToArray();
- // var colwidth = Bookings.ActualWidth / resource.Length;
- // var column = (int)Math.Truncate(e.GetPosition(Bookings).X / colwidth);
- // var employee = resource[column];
- //
- // assignment.EmployeeLink.ID = Guid.Parse(employee.Id.ToString());
- //
- // assignment.Date = SelectedDate.Date;
- //
- // //double intervalheight = (Bookings.ActualHeight - 30.0F) / (double)((Bookings.WorkEndHour - Bookings.WorkStartHour) * 4);
- // //int row = (int)Math.Truncate((e.GetPosition(Bookings).Y - 30.0F) / intervalheight);
- // //int hour = Bookings.WorkStartHour + (row / 4);
- // //int min = (row % 4) * 15;
- // //assignment.Start = new TimeSpan(hour, min, 0);
- // assignment.Start = GetTime(e.GetPosition(Bookings));
- //
- // assignment.Duration = new TimeSpan(2, 0, 0);
- //
- // assignment.JobLink.ID = delivery.Get<Delivery, Guid>(x => x.Job.ID);
- //
- // assignment.Description = string.Format("Delivery Docket #{0}", delivery.Get<Delivery, int>(x => x.Number));
- //
- // using (new WaitCursor())
- // {
- // new Client<Assignment>().Save(assignment, "Created for Delivery");
- // var del = new Client<Delivery>().Load(new Filter<Delivery>(x => x.ID).IsEqualTo(delivery.Get<Delivery, Guid>(x => x.ID)))
- // .FirstOrDefault();
- // del.Assignment.ID = assignment.ID;
- // new Client<Delivery>().Save(del, "Booked via Scheduler");
- // }
- //
- // AssignmentChanged?.Invoke(this, assignment);
- //
- // Refresh();
- //
- // AssignmentSelectionChanged?.Invoke(this, assignment);
- // }
- // else if (e.Data.GetDataPresent("Comal.Classes.Kanban"))
- // {
- // var kanban = e.Data.GetData("Comal.Classes.Kanban") as Kanban;
- // if (kanban == null)
- // return;
- //
- // var assignment = new Assignment();
- //
- // var resources = Bookings.ResourceCollection.OfType<SchedulerResource>().ToArray();
- // var colwidth = Bookings.ActualWidth / resources.Length;
- // var column = (int)Math.Truncate(e.GetPosition(Bookings).X / colwidth);
- // var employee = resources[column];
- //
- // assignment.EmployeeLink.ID = Guid.Parse(employee.Id.ToString());
- //
- // assignment.Date = SelectedDate.Date;
- //
- // var view = Bookings.FindVisualChildren<ScrollViewer>().FirstOrDefault(x => x.Name.Equals("Scrollviewer1"));
- // var intervalheight = (Bookings.ActualHeight - 30.0F) / (24 * 4);
- // var row = (int)Math.Truncate((view.VerticalOffset + e.GetPosition(Bookings).Y - 30.0F) / Bookings.DaysViewSettings.TimeIntervalSize);
- // assignment.Start = GetTime(row);
- //
- // assignment.Duration = new TimeSpan(0, 30, 0);
- //
- // assignment.JobLink.ID = kanban.JobLink.ID;
- // assignment.Task.ID = kanban.ID;
- //
- // assignment.Title = kanban.Title;
- //
- // AssignmentChanged?.Invoke(this, assignment);
- //
- // Refresh();
- //
- // AssignmentSelectionChanged?.Invoke(this, assignment);
- // }
- // }
- private void Bookings_OnAppointmentTapped(object? sender, AppointmentTappedArgs e)
- {
- var model = e.Appointment as IAssignmentViewModel;
-
- if (model != null)
- {
- var ass = FindAssignment(model.AssignmentID);
- if (
- ((ass == null) && (_selectedassignment != null))
- || ((ass != null) && (ass.ID != _selectedassignment))
- )
- {
- _selectedassignment = ass != null ? ass.ID : Guid.Empty;
- AssignmentSelectionChanged?.Invoke(this, ass);
- }
- }
- else
- {
- if (_selectedassignment != Guid.Empty)
- {
- _selectedassignment = Guid.Empty;
- AssignmentSelectionChanged?.Invoke(this, null);
- }
- }
- }
-
- #region Context Menu Handling
-
- private void Bookings_OnSchedulerContextMenuOpening(object? sender, SchedulerContextMenuOpeningEventArgs e)
- {
- IAssignmentData args = null;
- e.ContextMenu.Items.Clear();
- if (e.MenuType == SchedulerContextMenuType.Appointment)
- {
- var model = e.MenuInfo.Appointment as IAssignmentViewModel;
- args = new AssignmentData(model, Guid.Empty, DateTime.MinValue);
- CreateEditMenu(e.ContextMenu, args);
- e.ContextMenu.Items.Add(new Separator());
- CreateDeleteMenu(e.ContextMenu, args);
- e.ContextMenu.Items.Add(new Separator());
- }
- else if (e.MenuType == SchedulerContextMenuType.TimeSlotCell)
- {
- if (Guid.TryParse(e.MenuInfo.Resource.Id?.ToString(), out Guid employeeid))
- {
- args = new AssignmentData(null, employeeid, e.MenuInfo.DateTime.Value);
- CreateAddMenu(e.ContextMenu, args);
- e.ContextMenu.Items.Add(new Separator());
- }
- }
-
- CreateZoomMenus(e.ContextMenu, args);
-
- CustomiseContextMenu?.Invoke(e.ContextMenu, args);
-
- }
-
- private void CreateMenu(ContextMenu menu, string name, string header, IAssignmentData args, RoutedEventHandler action,
- Visibility visibility)
- {
- var item = new MenuItem();
- item.Name = name;
- item.Header = header;
- item.Click += action;
- item.Visibility = visibility;
- item.Tag = args;
- menu.Items.Add(item);
- }
-
- public bool CreateAddMenu(ContextMenu menu, IAssignmentData args)
- {
- if (args.Model == null)
- {
- CreateMenu(menu, "CreateAssignment", "Create Assignment", args, CreateAssignment_Click,
- Visibility.Visible);
- return true;
- }
- return false;
- }
-
- public bool CreateEditMenu(ContextMenu menu, IAssignmentData args)
- {
- if (args.Model != null)
- {
- CreateMenu(menu, "EditAssignment", "Edit Assignment", args, EditAssignment_Click,
- Visibility.Visible);
- return true;
- }
- return false;
- }
-
- public bool CreateDeleteMenu(ContextMenu menu, IAssignmentData args)
- {
- if (args.Model != null)
- {
- CreateMenu(menu, "DeleteAssignment", "Delete Assignment", args, DeleteAssignment_Click,
- Visibility.Visible);
- return true;
- }
- return false;
- }
- public bool CreateZoomMenus(ContextMenu menu, IAssignmentData args)
- {
- CreateMenu(menu, "ZoomIn", "Zoom In", args, ZoomIn_Click, Visibility.Visible);
- CreateMenu(menu, "ZoomOut", "Zoom Out", args, ZoomOut_Click, Visibility.Visible);
- CreateMenu(menu, "ZoomReset", "Reset Zoom", args, ZoomReset_Click, Visibility.Visible);
- return true;
- }
-
-
- private void EditAssignment(Assignment ass)
- {
- if (_assignmentgrid == null)
- _assignmentgrid = new AssignmentGrid();
- if (_assignmentgrid.EditItems(new[] { ass }))
- {
- AssignmentChanged?.Invoke(this, ass);
- Refresh();
- }
- }
-
- private void CreateAssignment_Click(object sender, RoutedEventArgs e)
- {
- var data = (sender as MenuItem).Tag as IAssignmentData;
- if (data != null)
- {
- var ass = new Assignment();
- ass.Date = data.Time.Date;
- ass.Start = data.Time.TimeOfDay;
- ass.Duration = new TimeSpan(1, 0, 0);
- ass.EmployeeLink.ID = data.EmployeeID;
- EditAssignment(ass);
- }
- else
- {
- MessageBox.Show("Please select an employee first!");
- }
- }
-
- private void EditAssignment_Click(object sender, RoutedEventArgs e)
- {
- var data = (sender as MenuItem).Tag as IAssignmentData;
- Assignment ass = null;
- using (new WaitCursor())
- {
- ass = new Client<Assignment>().Load(new Filter<Assignment>(x => x.ID).IsEqualTo(data.Model.AssignmentID))
- .FirstOrDefault();
- }
- if (ass != null)
- EditAssignment(ass);
- else
- MessageBox.Show("Cannot Load Assignment!");
- }
-
- private void DeleteAssignment_Click(object sender, RoutedEventArgs e)
- {
- var data = (sender as MenuItem).Tag as IAssignmentData;
- if ((data == null) || (data.Model == null))
- {
- MessageBox.Show("No Assignment to Delete");
- return;
- }
- if (MessageBox.Show("Are you sure you wish to delete this assignment?", "Confirm Delete", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
- {
- Assignment ass = new Assignment() { ID = data.Model.AssignmentID};
- using (new WaitCursor())
- new Client<Assignment>().Delete(ass, "Assignment Deleted from Scheduler");
- AssignmentChanged?.Invoke(this, ass);
- Refresh();
- }
- }
-
- private void ZoomIn_Click(object sender, RoutedEventArgs e)
- {
- Zoom *= 1.25F;
- }
- private void ZoomOut_Click(object sender, RoutedEventArgs e)
- {
- Zoom /= 1.25F;
- }
- private void ZoomReset_Click(object sender, RoutedEventArgs e)
- {
- Zoom = 100.0F;
- }
- #endregion
- private void Bookings_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- e.Handled = e.Key == Key.Delete;
- }
- }
- }
|