DeliveryList.cs 13 KB

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