DeliveryList.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media.Imaging;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.WPF;
  12. using InABox.Wpf;
  13. using Syncfusion.Linq;
  14. using InABox.Configuration;
  15. namespace PRSDesktop;
  16. public delegate void DeliveryChangedEvent(object sender);
  17. public class DeliveryListSettings : IUserConfigurationSettings
  18. {
  19. public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
  20. }
  21. public class DeliveryList : DynamicDataGrid<Delivery>, IMasterDetailControl<Job,Delivery>
  22. {
  23. private readonly BitmapImage docs = PRSDesktop.Resources.doc_png.AsBitmapImage();
  24. private DeliveryListSettings _settings;
  25. public DateTime BookingSlot { get; set; }
  26. public Guid EmployeeID { get; set; }
  27. public Job? Master { get; set; }
  28. public Filter<Delivery> MasterDetailFilter => Master != null
  29. ? Master.ID != Guid.Empty
  30. ? new Filter<Delivery>(x => x.Job.ID).IsEqualTo(Master.ID)
  31. : new Filter<Delivery>().None()
  32. : new Filter<Delivery>().All();
  33. public bool AllJobs { get; }
  34. public DeliveryList()
  35. {
  36. _settings = new UserConfiguration<DeliveryListSettings>().Load();
  37. FilterComponent.SetSettings(_settings.Filters, false);
  38. FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected;
  39. ActionColumns.Add(new DynamicImageColumn(DocumentsImage, DocumentsClick) { Position = DynamicActionColumnPosition.Start });
  40. HiddenColumns.Add(x => x.Notes);
  41. HiddenColumns.Add(x => x.Job.ID);
  42. HiddenColumns.Add(x => x.Completed);
  43. HiddenColumns.Add(x => x.Documents);
  44. HiddenColumns.Add(x => x.Contact.ID);
  45. HiddenColumns.Add(x => x.Contact.Name);
  46. OnCustomiseEditor += CustomiseEditor;
  47. }
  48. private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings filters)
  49. {
  50. _settings.Filters = filters;
  51. _settings.SaveUser();
  52. }
  53. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  54. {
  55. base.DoReconfigure(options);
  56. options.BeginUpdate();
  57. options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.EditRows, DynamicGridOption.SelectColumns,
  58. DynamicGridOption.FilterRows);
  59. if (Security.CanDelete<Delivery>())
  60. options.Add(DynamicGridOption.DeleteRows);
  61. options.EndUpdate();
  62. }
  63. public event DeliveryChangedEvent DeliveryChanged;
  64. private void CustomiseEditor(IDynamicEditorForm sender, Delivery[]? items, DynamicGridColumn column, BaseEditor editor)
  65. {
  66. if (column.ColumnName.Equals("Completed"))
  67. {
  68. editor.Editable = Security.IsAllowed<CanSkipDeliveryPhotos>() ? Editable.Enabled : Editable.Disabled;
  69. }
  70. // else if (column.ColumnName.Equals("Contact.ID"))
  71. // {
  72. // editor.Editable = Editable.Hidden;
  73. // }
  74. // else if (column.ColumnName.Equals("Contact.Name"))
  75. // {
  76. // editor.Editable = Editable.Enabled;
  77. // (editor as TextBoxEditor).Buttons = new[]
  78. // {
  79. // new(items?.FirstOrDefault(), "Select", 50, ContactNameLookup, true),
  80. // new EditorButton(items?.FirstOrDefault(), "Clear", 50, ContactNameClear, true)
  81. // };
  82. // }
  83. }
  84. // private void ContactNameClear(object editor, object? item)
  85. // {
  86. // if (item is not Delivery delivery)
  87. // return;
  88. //
  89. // delivery.Contact.ID = Guid.Empty;
  90. // delivery.Address.Street = "";
  91. // delivery.Address.City = "";
  92. // delivery.Address.State = "";
  93. // delivery.Address.PostCode = "";
  94. //
  95. // if(editor is BaseDynamicEditorControl baseEditor)
  96. // {
  97. // baseEditor.SetValue(baseEditor.ColumnName, "");
  98. // }
  99. // }
  100. //
  101. // private void ContactNameLookup(object editor, object? item)
  102. // {
  103. // var contacts = new MultiSelectDialog<Contact>(
  104. // null,
  105. // null,
  106. // false
  107. // );
  108. //
  109. // if (contacts.ShowDialog() != true)
  110. // return;
  111. //
  112. // var contact = contacts.Items().FirstOrDefault();
  113. // if (contact == null)
  114. // return;
  115. // if (item is not Delivery delivery)
  116. // return;
  117. //
  118. // delivery.Contact.ID = contact.ID;
  119. // delivery.Address.Street = contact.Address.Street;
  120. // delivery.Address.City = contact.Address.City;
  121. // delivery.Address.State = contact.Address.State;
  122. // delivery.Address.PostCode = contact.Address.PostCode;
  123. //
  124. // if (editor is BaseDynamicEditorControl baseEditor)
  125. // {
  126. // baseEditor.SetValue(baseEditor.ColumnName, contact.Name);
  127. // }
  128. // //SetEditorValue(item, "Contact.Name", contact.Name);
  129. // //SetEditorValue(item,"Address.Street",contact.Address.Street);
  130. // //SetEditorValue(item, "Address.City",contact.Address.City);
  131. // //SetEditorValue(item, "Address.State",contact.Address.State);
  132. // //SetEditorValue(item, "Address.PostCode",contact.Address.PostCode);
  133. // }
  134. // protected override Dictionary<string, object?> EditorValueChanged(IDynamicEditorForm editor, Delivery[] items, string name, object value)
  135. // {
  136. // var result = base.EditorValueChanged(editor, items, name, value);
  137. // if (name.Equals("Job.ID"))
  138. // {
  139. // items.ForEach(item => { item.Contact.ID = Guid.Empty; });
  140. // editor.SetEditorValue("Contact.Name", "");
  141. // }
  142. // else if (name.Equals("Contact.Name"))
  143. // {
  144. // var street = "";
  145. // var city = "";
  146. // var state = "";
  147. // var postcode = "";
  148. // if (items.First().Contact.IsValid())
  149. // {
  150. // street = items.First().Address.Street;
  151. // city = items.First().Address.City;
  152. // state = items.First().Address.State;
  153. // postcode = items.First().Address.PostCode;
  154. // }
  155. // else
  156. // {
  157. // CoreRow row = null;
  158. // if (items.First().ID != Guid.Empty)
  159. // {
  160. // row = new Client<Job>().Query(
  161. // new Filter<Job>(x => x.ID).IsEqualTo(items.First().Job.ID),
  162. // new Columns<Job>(x => x.ID, x => x.SiteAddress.Street, x => x.SiteAddress.City, x => x.SiteAddress.State,
  163. // x => x.SiteAddress.PostCode)
  164. // ).Rows.FirstOrDefault();
  165. // street = row != null ? row.Get<Job, string>(x => x.SiteAddress.Street) : "";
  166. // city = row != null ? row.Get<Job, string>(x => x.SiteAddress.City) : "";
  167. // state = row != null ? row.Get<Job, string>(x => x.SiteAddress.State) : "";
  168. // postcode = row != null ? row.Get<Job, string>(x => x.SiteAddress.PostCode) : "";
  169. // }
  170. // }
  171. //
  172. // editor.SetEditorValue("Address.Street", street);
  173. // editor.SetEditorValue("Address.City", city);
  174. // editor.SetEditorValue("Address.State", state);
  175. // editor.SetEditorValue("Address.PostCode", postcode);
  176. // }
  177. //
  178. // return result;
  179. // }
  180. private bool DocumentsClick(CoreRow? arg)
  181. {
  182. if (arg == null)
  183. return false;
  184. var docs = new List<IEntityDocument>();
  185. using (new WaitCursor())
  186. {
  187. var deliveryid = arg.Get<Delivery, Guid>(x => x.ID);
  188. var table = new Client<DeliveryDocument>().Query(
  189. new Filter<DeliveryDocument>(x => x.EntityLink.ID).IsEqualTo(deliveryid)
  190. );
  191. foreach (var row in table.Rows)
  192. docs.Add(row.ToObject<DeliveryDocument>());
  193. }
  194. if (docs.Any())
  195. {
  196. var editor = new DocumentEditor(docs.ToArray());
  197. //editor.PrintAllowed = Security.IsAllowed<CanPrintFactoryFloorDrawings>();
  198. editor.SaveAllowed = Security.IsAllowed<CanSaveFactoryFloorDrawings>();
  199. editor.ShowDialog();
  200. }
  201. else
  202. {
  203. MessageBox.Show("No Documents Available!");
  204. }
  205. return false;
  206. }
  207. private BitmapImage? DocumentsImage(CoreRow? arg)
  208. {
  209. if (arg == null)
  210. return docs;
  211. return arg.Get<Delivery, int>(x => x.Documents) > 0 ? docs : null;
  212. }
  213. public override Delivery CreateItem()
  214. {
  215. var result = base.CreateItem();
  216. result.Job.ID = Master?.ID ?? Guid.Empty;
  217. result.Job.Synchronise(Master ?? new Job());
  218. return result;
  219. }
  220. private AssignmentGrid ag = null;
  221. public bool CreateBooking(CoreRow row, Guid employeeid, DateTime time)
  222. {
  223. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("{0:dd-MMM-yy hh-mm-ss} -> {1}", BookingSlot, EmployeeID));
  224. var assignment = new Assignment
  225. {
  226. Date = time.Date,
  227. Description = string.Format("Delivery #{0}", row.Get<Delivery, int>(x => x.Number))
  228. };
  229. assignment.Booked.Start = time.TimeOfDay;
  230. assignment.Booked.Finish = time.TimeOfDay.Add(new TimeSpan(2, 0, 0));
  231. assignment.Delivery.ID = row.Get<Delivery, Guid>(x => x.ID);
  232. assignment.EmployeeLink.ID = employeeid;
  233. assignment.JobLink.ID = row.Get<Delivery, Guid>(x => x.Job.ID);
  234. Logger.Send(LogType.Information, ClientFactory.UserID, "- Creating Assignment Grid");
  235. if (ag == null)
  236. ag = new AssignmentGrid();
  237. Logger.Send(LogType.Information, ClientFactory.UserID, "- Editing Assignment");
  238. if (ag.EditItems(new[] { assignment }))
  239. {
  240. using (new WaitCursor())
  241. {
  242. new Client<Assignment>().Save(assignment, "Created for Delivery");
  243. var del = new Client<Delivery>().Load(new Filter<Delivery>(x => x.ID).IsEqualTo(row.Get<Delivery, Guid>(x => x.ID)))
  244. .FirstOrDefault();
  245. del.Assignment.ID = assignment.ID;
  246. new Client<Delivery>().Save(del, "Booked via Scheduler");
  247. }
  248. DeliveryChanged?.Invoke(this);
  249. return true;
  250. }
  251. Logger.Send(LogType.Information, ClientFactory.UserID, "- Cancelled Edit");
  252. return false;
  253. }
  254. private bool BookClick(CoreRow arg)
  255. {
  256. return CreateBooking(arg, EmployeeID, BookingSlot);
  257. }
  258. private BitmapImage BookImage(CoreRow arg)
  259. {
  260. if (arg == null)
  261. {
  262. Logger.Send(LogType.Information, ClientFactory.UserID, "BookImage: Row is null!");
  263. return null;
  264. }
  265. Logger.Send(LogType.Information, ClientFactory.UserID,
  266. string.Format("BookImage {0} -> {1:dd-MMM-yy hh-mm-ss}", arg.Get<Delivery, int>(x => x.Number), BookingSlot));
  267. if (BookingSlot.IsEmpty())
  268. {
  269. Logger.Send(LogType.Information, ClientFactory.UserID, "BookImage: BookingSlot is Empty!");
  270. return null;
  271. }
  272. if (arg.Get<Delivery, DateTime>(x => x.Assignment.Date).IsEmpty())
  273. {
  274. Logger.Send(LogType.Information, ClientFactory.UserID, "BookImage: Assignment Date is Empty!");
  275. return PRSDesktop.Resources.rightarrow.AsBitmapImage();
  276. }
  277. Logger.Send(LogType.Information, ClientFactory.UserID,
  278. string.Format("BookImage Assignment Date is {0:dd-MMM-yy hh-mm-ss}", arg.Get<Delivery, DateTime>(x => x.Assignment.Date)));
  279. return null;
  280. }
  281. protected override void Reload(Filters<Delivery> criteria, Columns<Delivery> columns, ref SortOrder<Delivery>? sort, Action<CoreTable?, Exception?> action)
  282. {
  283. criteria.Add(MasterDetailFilter);
  284. base.Reload(criteria, columns, ref sort, action);
  285. }
  286. }