123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using Microsoft.CodeAnalysis.VisualBasic.Syntax;
- using org.omg.PortableInterceptor;
- using PRSDesktop.Forms;
- using PRSDesktop.WidgetGroups;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Globalization;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using InABox.Configuration;
- using Client = InABox.Clients.Client;
- namespace PRSDesktop.Dashboards
- {
- public class EquipmentSchedulesDashboardProperties : IUserConfigurationSettings, IDashboardProperties
- {
- }
- public class ScheduleViewModel : INotifyPropertyChanged
- {
- private Guid employeeID;
- private string? employeeName;
- private string? employeeMobile;
- private string? employeeEmail;
- private BitmapImage? employeeImage;
- public bool HasEmployee => EmployeeID != Guid.Empty;
- public Guid ID { get; set; }
- public string Title { get; set; }
- public DateTime DueDate { get; set; }
- public BitmapImage? EmployeeImage
- {
- get => employeeImage;
- set
- {
- employeeImage = value;
- NotifyPropertyChanged();
- }
- }
- public Guid EmployeeID
- {
- get => employeeID;
- set
- {
- employeeID = value;
- NotifyPropertyChanged(nameof(HasEmployee));
- }
- }
- public string? EmployeeName
- {
- get => employeeName;
- set
- {
- employeeName = value;
- NotifyPropertyChanged();
- }
- }
- public string? EmployeeMobile
- {
- get => employeeMobile;
- set
- {
- employeeMobile = value;
- NotifyPropertyChanged();
- NotifyPropertyChanged(nameof(EmployeeContact));
- }
- }
- public string? EmployeeEmail
- {
- get => employeeEmail;
- set
- {
- employeeEmail = value;
- NotifyPropertyChanged();
- NotifyPropertyChanged(nameof(EmployeeContact));
- }
- }
- public string? EmployeeContact
- {
- get
- {
- var str = "";
- if (EmployeeEmail is not null) str = EmployeeEmail;
- if(EmployeeMobile is not null)
- {
- if (string.IsNullOrWhiteSpace(str))
- {
- str = EmployeeMobile;
- }
- else
- {
- str += $", {EmployeeMobile}";
- }
- }
- return string.IsNullOrWhiteSpace(str) ? null : str;
- }
- }
- public event PropertyChangedEventHandler? PropertyChanged;
- private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public class EquipmentScheduleViewModel
- {
- public bool HasLocation { get; set; }
- public Guid ID { get; set; }
- public string Code { get; set; }
- public string Description { get; set; }
- public List<ScheduleViewModel> Schedules { get; set; }
- }
- [ValueConversion(typeof(DateTime), typeof(SolidColorBrush))]
- class ScheduleBackgroundConverter : IValueConverter
- {
- public Color GetColor(DateTime date)
- {
- var diff = date - DateTime.Today;
- if (diff < TimeSpan.Zero)
- return Colors.Salmon;
- else if (diff.TotalDays <= 7)
- return Colors.Orange;
- else if (diff.Days <= 30)
- return Colors.LightYellow;
- else
- return Colors.LightGreen;
- }
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- var date = (DateTime)value;
- var color = GetColor(date);
- return new SolidColorBrush(color);
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- /// <summary>
- /// Interaction logic for EquipmentSchedulesDashboard.xaml
- /// </summary>
- public partial class EquipmentSchedulesDashboard : UserControl, IDashboardWidget<EquipmentDashboardGroup, EquipmentSchedulesDashboardProperties>
- {
- public EquipmentSchedulesDashboardProperties Properties { get; set; } = null!;
- public event LoadSettings<EquipmentSchedulesDashboardProperties>? LoadSettings;
- public event SaveSettings<EquipmentSchedulesDashboardProperties>? SaveSettings;
- private Dictionary<Guid, Equipment> Equipment = new();
- private Dictionary<Guid, BitmapImage?> EmployeeImages = new();
- private Dictionary<Guid, Employee> Employees = new();
- private List<EquipmentScheduleViewModel> Models = new();
- public EquipmentSchedulesDashboard()
- {
- InitializeComponent();
- }
- public void Setup()
- {
- LoadEmployeeImages();
- }
- private void LoadEmployeeImages()
- {
- Task.Run(() =>
- {
- var employees = new Client<Employee>()
- .Query(
- LookupFactory.DefineFilter<Employee>(),
- new Columns<Employee>(
- x => x.ID,
- x => x.Thumbnail.ID,
- x => x.Name,
- x => x.Mobile,
- x => x.Email))
- .ToObjects<Employee>().ToList();
- var documents = new Client<Document>()
- .Query(
- new Filter<Document>(x => x.ID).InList(employees.Select(x => x.Thumbnail.ID).ToArray()),
- new Columns<Document>(x => x.ID).Add(x => x.Data))
- .ToDictionary<Document, Guid, byte[]>(x => x.ID, x => x.Data);
- return new { Employees = employees, Documents = documents };
- }).ContinueWith((task) =>
- {
- EmployeeImages = task.Result.Employees.ToDictionary(
- x => x.ID,
- x =>
- {
- var document = task.Result.Documents.GetValueOrDefault(x.Thumbnail.ID);
- if (document is null)
- return null;
- return ImageUtils.BitmapImageFromBytes(document);
- });
- Employees = task.Result.Employees.ToDictionary(x => x.ID, x => x);
- lock (Models)
- {
- var anonymous = PRSDesktop.Resources.anonymous.AsBitmapImage();
- foreach (var model in Models)
- {
- foreach (var schedule in model.Schedules)
- {
- var employee = Employees?.GetValueOrDefault(schedule.EmployeeID);
- schedule.EmployeeImage = EmployeeImages?.GetValueOrDefault(schedule.EmployeeID) ?? anonymous;
- schedule.EmployeeMobile = employee?.Mobile;
- schedule.EmployeeName = employee?.Name;
- schedule.EmployeeEmail = employee?.Email;
- }
- }
- }
- }, TaskScheduler.FromCurrentSynchronizationContext());
- }
- public void Refresh()
- {
- var results = Client.QueryMultiple(
- new KeyedQueryDef<Equipment>(
- new Filter<Equipment>().All(),
- new Columns<Equipment>(x => x.ID)
- .Add(x => x.Code)
- .Add(x => x.Description)
- .Add(x => x.TrackerLink.ID)
- .Add(x => x.TrackerLink.Location.Latitude)
- .Add(x => x.TrackerLink.Location.Longitude)
- .Add(x => x.TrackerLink.Location.Timestamp)),
- new KeyedQueryDef<Schedule>(
- new Filter<Schedule>(x => x.DocumentClass).IsEqualTo(typeof(Equipment).EntityName()),
- new Columns<Schedule>(x => x.ID)
- .Add(x => x.Title)
- .Add(x => x.DueDate)
- .Add(x => x.DocumentID)
- .Add(x => x.EmployeeLink.ID)));
- var equipmentItems = results.Get<Equipment>().Rows.Select(x => x.ToObject<Equipment>());
- Equipment = equipmentItems.ToDictionary(x => x.ID, x => x);
- var schedules = results.Get<Schedule>().Rows
- .Select(x => x.ToObject<Schedule>())
- .GroupBy(x => x.DocumentID)
- .ToDictionary(x => x.Key, x => x.OrderBy(x => x.DueDate).ToList());
- var anonymous = PRSDesktop.Resources.anonymous.AsBitmapImage();
- lock (Models)
- {
- Models = new List<EquipmentScheduleViewModel>();
- foreach (var equipmentItem in equipmentItems)
- {
- var equipmentSchedules = schedules.GetValueOrDefault(equipmentItem.ID);
- if (equipmentSchedules is not null)
- {
- var model = new EquipmentScheduleViewModel
- {
- Code = equipmentItem.Code,
- Description = equipmentItem.Description,
- ID = equipmentItem.ID,
- HasLocation = equipmentItem.TrackerLink.ID != Guid.Empty
- };
- model.Schedules = equipmentSchedules
- .Select(x =>
- {
- var employee = Employees.GetValueOrDefault(x.EmployeeLink.ID);
- return new ScheduleViewModel
- {
- Title = x.Title,
- DueDate = x.DueDate,
- ID = x.ID,
- EmployeeImage = EmployeeImages.GetValueOrDefault(x.EmployeeLink.ID) ?? anonymous,
- EmployeeID = x.EmployeeLink.ID,
- EmployeeEmail = employee?.Email,
- EmployeeMobile = employee?.Mobile,
- EmployeeName = employee?.Name
- };
- })
- .ToList() ?? new List<ScheduleViewModel>();
- Models.Add(model);
- }
- }
- }
- EquipmentList.ItemsSource = Models;
- }
- public void Shutdown()
- {
- }
- private void ViewEquipment_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- var equipmentID = (Guid)(sender as MenuItem)!.Tag;
- var equipment = new Client<Equipment>().Load(
- new Filter<Equipment>(x => x.ID).IsEqualTo(equipmentID)).FirstOrDefault();
- if(equipment != null)
- {
- var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(Equipment));
- if(grid.EditItems(new[] { equipment }))
- {
- new Client<Equipment>().Save(equipment, "Edited by user from schedules dashboard");
- Refresh();
- }
- }
- }
- private void ViewSchedule_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- var scheduleID = (Guid)(sender as MenuItem)!.Tag;
- var schedule = new Client<Schedule>().Load(
- new Filter<Schedule>(x => x.ID).IsEqualTo(scheduleID)).FirstOrDefault();
- if (schedule != null)
- {
- var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(Schedule));
- if (grid.EditItems(new[] { schedule }))
- {
- new Client<Schedule>().Save(schedule, "Edited by user from schedules dashboard");
- Refresh();
- }
- }
- }
- private void ViewEmployee_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- var employeeID = (Guid)(sender as MenuItem)!.Tag;
- var employee = new Client<Employee>().Load(
- new Filter<Employee>(x => x.ID).IsEqualTo(employeeID)).FirstOrDefault();
- if (employee != null)
- {
- var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(Employee));
- if (grid.EditItems(new[] { employee }))
- {
- new Client<Employee>().Save(employee, "Edited by user from schedules dashboard");
- Refresh();
- }
- }
- }
- private void EquipmentLocation_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- var equipmentID = (Guid)(sender as Button)!.Tag;
- if (!Equipment.TryGetValue(equipmentID, out var equipment))
- return;
- var form = new MapForm(
- equipment.TrackerLink.Location.Latitude,
- equipment.TrackerLink.Location.Longitude,
- equipment.TrackerLink.Location.Timestamp);
- form.ShowDialog();
- }
- }
- public class EquipmentSchedulesDashboardElement :
- DashboardElement<EquipmentSchedulesDashboard, EquipmentDashboardGroup, EquipmentSchedulesDashboardProperties> { }
- }
|