using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using InABox.Wpf; using Syncfusion.Linq; using InABox.Configuration; using System.Threading; namespace PRSDesktop; public delegate void DeliveryChangedEvent(object sender); public class DeliveryListSettings : IUserConfigurationSettings { public DynamicGridSelectedFilterSettings Filters { get; set; } = new(); } public class DeliveryList : DynamicDataGrid, IMasterDetailControl { private readonly BitmapImage docs = PRSDesktop.Resources.doc_png.AsBitmapImage(); private DeliveryListSettings _settings; public DateTime BookingSlot { get; set; } public Guid EmployeeID { get; set; } public Job? Master { get; set; } public Filter MasterDetailFilter => Master != null ? Master.ID != Guid.Empty ? new Filter(x => x.Job.ID).IsEqualTo(Master.ID) : new Filter().None() : new Filter().All(); public bool AllJobs { get; } public DeliveryList() { _settings = new UserConfiguration().Load(); FilterComponent.SetSettings(_settings.Filters, false); FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected; ActionColumns.Add(new DynamicImageColumn(DocumentsImage, DocumentsClick) { Position = DynamicActionColumnPosition.Start }); HiddenColumns.Add(x => x.Notes); HiddenColumns.Add(x => x.Job.ID); HiddenColumns.Add(x => x.Completed); HiddenColumns.Add(x => x.Documents); HiddenColumns.Add(x => x.Contact.ID); HiddenColumns.Add(x => x.Contact.Name); OnCustomiseEditor += CustomiseEditor; } private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings filters) { _settings.Filters = filters; _settings.SaveUser(); } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.RecordCount = true; options.AddRows = true; options.EditRows = true; options.SelectColumns = true; options.FilterRows = true; if (Security.CanDelete()) options.DeleteRows = true; } public event DeliveryChangedEvent DeliveryChanged; private void CustomiseEditor(IDynamicEditorForm sender, Delivery[]? items, DynamicGridColumn column, BaseEditor editor) { if (column.ColumnName.Equals("Completed")) { editor.Editable = Security.IsAllowed() ? Editable.Enabled : Editable.Disabled; } // else if (column.ColumnName.Equals("Contact.ID")) // { // editor.Editable = Editable.Hidden; // } // else if (column.ColumnName.Equals("Contact.Name")) // { // editor.Editable = Editable.Enabled; // (editor as TextBoxEditor).Buttons = new[] // { // new(items?.FirstOrDefault(), "Select", 50, ContactNameLookup, true), // new EditorButton(items?.FirstOrDefault(), "Clear", 50, ContactNameClear, true) // }; // } } // private void ContactNameClear(object editor, object? item) // { // if (item is not Delivery delivery) // return; // // delivery.Contact.ID = Guid.Empty; // delivery.Address.Street = ""; // delivery.Address.City = ""; // delivery.Address.State = ""; // delivery.Address.PostCode = ""; // // if(editor is BaseDynamicEditorControl baseEditor) // { // baseEditor.SetValue(baseEditor.ColumnName, ""); // } // } // // private void ContactNameLookup(object editor, object? item) // { // var contacts = new MultiSelectDialog( // null, // null, // false // ); // // if (contacts.ShowDialog() != true) // return; // // var contact = contacts.Items().FirstOrDefault(); // if (contact == null) // return; // if (item is not Delivery delivery) // return; // // delivery.Contact.ID = contact.ID; // delivery.Address.Street = contact.Address.Street; // delivery.Address.City = contact.Address.City; // delivery.Address.State = contact.Address.State; // delivery.Address.PostCode = contact.Address.PostCode; // // if (editor is BaseDynamicEditorControl baseEditor) // { // baseEditor.SetValue(baseEditor.ColumnName, contact.Name); // } // //SetEditorValue(item, "Contact.Name", contact.Name); // //SetEditorValue(item,"Address.Street",contact.Address.Street); // //SetEditorValue(item, "Address.City",contact.Address.City); // //SetEditorValue(item, "Address.State",contact.Address.State); // //SetEditorValue(item, "Address.PostCode",contact.Address.PostCode); // } // protected override Dictionary EditorValueChanged(IDynamicEditorForm editor, Delivery[] items, string name, object value) // { // var result = base.EditorValueChanged(editor, items, name, value); // if (name.Equals("Job.ID")) // { // items.ForEach(item => { item.Contact.ID = Guid.Empty; }); // editor.SetEditorValue("Contact.Name", ""); // } // else if (name.Equals("Contact.Name")) // { // var street = ""; // var city = ""; // var state = ""; // var postcode = ""; // if (items.First().Contact.IsValid()) // { // street = items.First().Address.Street; // city = items.First().Address.City; // state = items.First().Address.State; // postcode = items.First().Address.PostCode; // } // else // { // CoreRow row = null; // if (items.First().ID != Guid.Empty) // { // row = new Client().Query( // new Filter(x => x.ID).IsEqualTo(items.First().Job.ID), // new Columns(x => x.ID, x => x.SiteAddress.Street, x => x.SiteAddress.City, x => x.SiteAddress.State, // x => x.SiteAddress.PostCode) // ).Rows.FirstOrDefault(); // street = row != null ? row.Get(x => x.SiteAddress.Street) : ""; // city = row != null ? row.Get(x => x.SiteAddress.City) : ""; // state = row != null ? row.Get(x => x.SiteAddress.State) : ""; // postcode = row != null ? row.Get(x => x.SiteAddress.PostCode) : ""; // } // } // // editor.SetEditorValue("Address.Street", street); // editor.SetEditorValue("Address.City", city); // editor.SetEditorValue("Address.State", state); // editor.SetEditorValue("Address.PostCode", postcode); // } // // return result; // } private bool DocumentsClick(CoreRow? arg) { if (arg == null) return false; var docs = new List(); using (new WaitCursor()) { var deliveryid = arg.Get(x => x.ID); var table = new Client().Query( new Filter(x => x.EntityLink.ID).IsEqualTo(deliveryid) ); foreach (var row in table.Rows) docs.Add(row.ToObject()); } if (docs.Any()) { var editor = new DocumentEditor(docs.ToArray()); //editor.PrintAllowed = Security.IsAllowed(); editor.SaveAllowed = Security.IsAllowed(); editor.ShowDialog(); } else { MessageBox.Show("No Documents Available!"); } return false; } private BitmapImage? DocumentsImage(CoreRow? arg) { if (arg == null) return docs; return arg.Get(x => x.Documents) > 0 ? docs : null; } public override Delivery CreateItem() { var result = base.CreateItem(); result.Job.ID = Master?.ID ?? Guid.Empty; result.Job.Synchronise(Master ?? new Job()); return result; } private AssignmentGrid ag = null; public bool CreateBooking(CoreRow row, Guid employeeid, DateTime time) { Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("{0:dd-MMM-yy hh-mm-ss} -> {1}", BookingSlot, EmployeeID)); var assignment = new Assignment { Date = time.Date, Description = string.Format("Delivery #{0}", row.Get(x => x.Number)) }; assignment.Booked.Start = time.TimeOfDay; assignment.Booked.Finish = time.TimeOfDay.Add(new TimeSpan(2, 0, 0)); assignment.Delivery.ID = row.Get(x => x.ID); assignment.EmployeeLink.ID = employeeid; assignment.JobLink.ID = row.Get(x => x.Job.ID); Logger.Send(LogType.Information, ClientFactory.UserID, "- Creating Assignment Grid"); if (ag == null) ag = new AssignmentGrid(); Logger.Send(LogType.Information, ClientFactory.UserID, "- Editing Assignment"); if (ag.EditItems(new[] { assignment })) { using (new WaitCursor()) { new Client().Save(assignment, "Created for Delivery"); var del = new Client().Load(new Filter(x => x.ID).IsEqualTo(row.Get(x => x.ID))) .FirstOrDefault(); del.Assignment.ID = assignment.ID; new Client().Save(del, "Booked via Scheduler"); } DeliveryChanged?.Invoke(this); return true; } Logger.Send(LogType.Information, ClientFactory.UserID, "- Cancelled Edit"); return false; } private bool BookClick(CoreRow arg) { return CreateBooking(arg, EmployeeID, BookingSlot); } private BitmapImage BookImage(CoreRow arg) { if (arg == null) { Logger.Send(LogType.Information, ClientFactory.UserID, "BookImage: Row is null!"); return null; } Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("BookImage {0} -> {1:dd-MMM-yy hh-mm-ss}", arg.Get(x => x.Number), BookingSlot)); if (BookingSlot.IsEmpty()) { Logger.Send(LogType.Information, ClientFactory.UserID, "BookImage: BookingSlot is Empty!"); return null; } if (arg.Get(x => x.Assignment.Date).IsEmpty()) { Logger.Send(LogType.Information, ClientFactory.UserID, "BookImage: Assignment Date is Empty!"); return PRSDesktop.Resources.rightarrow.AsBitmapImage(); } Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("BookImage Assignment Date is {0:dd-MMM-yy hh-mm-ss}", arg.Get(x => x.Assignment.Date))); return null; } protected override void Reload( Filters criteria, Columns columns, ref SortOrder? sort, CancellationToken token, Action action) { criteria.Add(MasterDetailFilter); base.Reload(criteria, columns, ref sort, token, action); } }