123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- 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 Syncfusion.Linq;
- namespace PRSDesktop
- {
- public delegate void DeliveryChangedEvent(object sender);
- public class DeliveryList : DynamicDataGrid<Delivery>, IJobControl
- {
- private readonly BitmapImage docs = PRSDesktop.Resources.doc_png.AsBitmapImage();
- private bool ShowAll;
- public DeliveryList()
- {
- Options.BeginUpdate();
- Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.EditRows, DynamicGridOption.SelectColumns,
- DynamicGridOption.FilterRows);
- if (Security.CanDelete<Delivery>())
- Options.Add(DynamicGridOption.DeleteRows);
- Options.EndUpdate();
- ActionColumns.Add(new DynamicActionColumn(DocumentsImage, DocumentsClick) { Position = DynamicActionColumnPosition.Start });
- //ActionColumns.Add(new DynamicActionColumn(BookImage, BookClick));
- HiddenColumns.Add(x => x.Notes);
- HiddenColumns.Add(x => x.Job.ID);
- HiddenColumns.Add(x => x.Job.Deleted);
- HiddenColumns.Add(x => x.Completed);
- HiddenColumns.Add(x => x.Documents);
- HiddenColumns.Add(x => x.Contact.ID);
- HiddenColumns.Add(x => x.Contact.Deleted);
- HiddenColumns.Add(x => x.Contact.Name);
-
- AddButton("Show All", null, ToggleCompleted);
- OnCustomiseEditor += CustomiseEditor;
- }
- public DateTime BookingSlot { get; set; }
- public Guid EmployeeID { get; set; }
- public Guid JobID { get; set; }
- public event DeliveryChangedEvent DeliveryChanged;
- private void CustomiseEditor(IDynamicEditorForm sender, Delivery[]? items, DynamicGridColumn column, BaseEditor editor)
- {
- if (column.ColumnName.Equals("Completed"))
- {
- editor.Editable = Security.IsAllowed<CanSkipDeliveryPhotos>() ? 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 = "";
- (editor as BaseDynamicEditorControl).SetValue("");
- }
- private void ContactNameLookup(object editor, object? item)
- {
- var contacts = new MultiSelectDialog<Contact>(
- 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;
- (editor as BaseDynamicEditorControl).SetValue(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<string, object> EditorValueChanged(DynamicEditorForm 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.FindEditor("Contact.Name").SetValue("");
- }
- 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<Job>().Query(
- new Filter<Job>(x => x.ID).IsEqualTo(items.First().Job.ID),
- new Columns<Job>(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<Job, string>(x => x.SiteAddress.Street) : "";
- city = row != null ? row.Get<Job, string>(x => x.SiteAddress.City) : "";
- state = row != null ? row.Get<Job, string>(x => x.SiteAddress.State) : "";
- postcode = row != null ? row.Get<Job, string>(x => x.SiteAddress.PostCode) : "";
- }
- }
- editor.FindEditor("Address.Street").SetValue(street);
- editor.FindEditor("Address.City").SetValue(city);
- editor.FindEditor("Address.State").SetValue(state);
- editor.FindEditor("Address.PostCode").SetValue(postcode);
- }
- return result;
- }
- private bool DocumentsClick(CoreRow arg)
- {
- if (arg == null)
- return false;
- var docs = new List<IEntityDocument>();
- using (new WaitCursor())
- {
- var deliveryid = arg.Get<Delivery, Guid>(x => x.ID);
- var table = new Client<DeliveryDocument>().Query(
- new Filter<DeliveryDocument>(x => x.EntityLink.ID).IsEqualTo(deliveryid)
- );
- foreach (var row in table.Rows)
- docs.Add(row.ToObject<DeliveryDocument>());
- }
- if (docs.Any())
- {
- var editor = new DocumentEditor(docs.ToArray());
- editor.PrintAllowed = Security.IsAllowed<CanPrintFactoryFloorDrawings>();
- editor.SaveAllowed = Security.IsAllowed<CanSaveFactoryFloorDrawings>();
- editor.ShowDialog();
- }
- else
- {
- MessageBox.Show("No Documents Available!");
- }
- return false;
- }
- private BitmapImage DocumentsImage(CoreRow arg)
- {
- if (arg == null)
- return docs;
- return arg.Get<Delivery, int>(x => x.Documents) > 0 ? docs : null;
- }
- 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,
- Start = time.TimeOfDay,
- Finish = time.TimeOfDay.Add(new TimeSpan(2, 0, 0)),
- Description = string.Format("Delivery #{0}", row.Get<Delivery, int>(x => x.Number))
- };
- assignment.Delivery.ID = row.Get<Delivery, Guid>(x => x.ID);
- assignment.EmployeeLink.ID = employeeid;
- assignment.JobLink.ID = row.Get<Delivery, Guid>(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<Assignment>().Save(assignment, "Created for Delivery");
- var del = new Client<Delivery>().Load(new Filter<Delivery>(x => x.ID).IsEqualTo(row.Get<Delivery, Guid>(x => x.ID)))
- .FirstOrDefault();
- del.Assignment.ID = assignment.ID;
- new Client<Delivery>().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<Delivery, int>(x => x.Number), BookingSlot));
- if (BookingSlot.IsEmpty())
- {
- Logger.Send(LogType.Information, ClientFactory.UserID, "BookImage: BookingSlot is Empty!");
- return null;
- }
- if (arg.Get<Delivery, DateTime>(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<Delivery, DateTime>(x => x.Assignment.Date)));
- return null;
- }
- private bool ToggleCompleted(Button arg1, CoreRow[] arg2)
- {
- ShowAll = !ShowAll;
- UpdateButton(arg1, null, !ShowAll ? "Show All" : "Hide Completed");
- return true;
- }
- protected override void Reload(Filters<Delivery> criteria, Columns<Delivery> columns, ref SortOrder<Delivery> sort,
- Action<CoreTable, Exception> action)
- {
- if (JobID != Guid.Empty)
- criteria.Add(new Filter<Delivery>(x => x.Job.ID).IsEqualTo(JobID));
- if (!ShowAll)
- criteria.Add(new Filter<Delivery>(x => x.Completed).IsEqualTo(DateTime.MinValue));
- base.Reload(criteria, columns, ref sort, action);
- }
- protected override void SelectItems(CoreRow[] rows)
- {
- base.SelectItems(rows);
- }
- }
- }
|