DeliveryList.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. delivery.Contact.ID = Guid.Empty;
  75. delivery.Address.Street = "";
  76. delivery.Address.City = "";
  77. delivery.Address.State = "";
  78. delivery.Address.PostCode = "";
  79. if(editor is BaseDynamicEditorControl baseEditor)
  80. {
  81. baseEditor.SetValue(baseEditor.ColumnName, "");
  82. }
  83. }
  84. private void ContactNameLookup(object editor, object? item)
  85. {
  86. var contacts = new MultiSelectDialog<Contact>(
  87. null,
  88. null,
  89. false
  90. );
  91. if (contacts.ShowDialog() != true)
  92. return;
  93. var contact = contacts.Items().FirstOrDefault();
  94. if (contact == null)
  95. return;
  96. if (item is not Delivery delivery)
  97. return;
  98. delivery.Contact.ID = contact.ID;
  99. delivery.Address.Street = contact.Address.Street;
  100. delivery.Address.City = contact.Address.City;
  101. delivery.Address.State = contact.Address.State;
  102. delivery.Address.PostCode = contact.Address.PostCode;
  103. if (editor is BaseDynamicEditorControl baseEditor)
  104. {
  105. baseEditor.SetValue(baseEditor.ColumnName, contact.Name);
  106. }
  107. //SetEditorValue(item, "Contact.Name", contact.Name);
  108. //SetEditorValue(item,"Address.Street",contact.Address.Street);
  109. //SetEditorValue(item, "Address.City",contact.Address.City);
  110. //SetEditorValue(item, "Address.State",contact.Address.State);
  111. //SetEditorValue(item, "Address.PostCode",contact.Address.PostCode);
  112. }
  113. protected override Dictionary<string, object?> EditorValueChanged(IDynamicEditorForm editor, Delivery[] items, string name, object value)
  114. {
  115. var result = base.EditorValueChanged(editor, items, name, value);
  116. if (name.Equals("Job.ID"))
  117. {
  118. items.ForEach(item => { item.Contact.ID = Guid.Empty; });
  119. editor.SetEditorValue("Contact.Name", "");
  120. }
  121. else if (name.Equals("Contact.Name"))
  122. {
  123. var street = "";
  124. var city = "";
  125. var state = "";
  126. var postcode = "";
  127. if (items.First().Contact.IsValid())
  128. {
  129. street = items.First().Address.Street;
  130. city = items.First().Address.City;
  131. state = items.First().Address.State;
  132. postcode = items.First().Address.PostCode;
  133. }
  134. else
  135. {
  136. CoreRow row = null;
  137. if (items.First().ID != Guid.Empty)
  138. {
  139. row = new Client<Job>().Query(
  140. new Filter<Job>(x => x.ID).IsEqualTo(items.First().Job.ID),
  141. new Columns<Job>(x => x.ID, x => x.SiteAddress.Street, x => x.SiteAddress.City, x => x.SiteAddress.State,
  142. x => x.SiteAddress.PostCode)
  143. ).Rows.FirstOrDefault();
  144. street = row != null ? row.Get<Job, string>(x => x.SiteAddress.Street) : "";
  145. city = row != null ? row.Get<Job, string>(x => x.SiteAddress.City) : "";
  146. state = row != null ? row.Get<Job, string>(x => x.SiteAddress.State) : "";
  147. postcode = row != null ? row.Get<Job, string>(x => x.SiteAddress.PostCode) : "";
  148. }
  149. }
  150. editor.SetEditorValue("Address.Street", street);
  151. editor.SetEditorValue("Address.City", city);
  152. editor.SetEditorValue("Address.State", state);
  153. editor.SetEditorValue("Address.PostCode", postcode);
  154. }
  155. return result;
  156. }
  157. private bool DocumentsClick(CoreRow? arg)
  158. {
  159. if (arg == null)
  160. return false;
  161. var docs = new List<IEntityDocument>();
  162. using (new WaitCursor())
  163. {
  164. var deliveryid = arg.Get<Delivery, Guid>(x => x.ID);
  165. var table = new Client<DeliveryDocument>().Query(
  166. new Filter<DeliveryDocument>(x => x.EntityLink.ID).IsEqualTo(deliveryid)
  167. );
  168. foreach (var row in table.Rows)
  169. docs.Add(row.ToObject<DeliveryDocument>());
  170. }
  171. if (docs.Any())
  172. {
  173. var editor = new DocumentEditor(docs.ToArray());
  174. //editor.PrintAllowed = Security.IsAllowed<CanPrintFactoryFloorDrawings>();
  175. editor.SaveAllowed = Security.IsAllowed<CanSaveFactoryFloorDrawings>();
  176. editor.ShowDialog();
  177. }
  178. else
  179. {
  180. MessageBox.Show("No Documents Available!");
  181. }
  182. return false;
  183. }
  184. private BitmapImage? DocumentsImage(CoreRow? arg)
  185. {
  186. if (arg == null)
  187. return docs;
  188. return arg.Get<Delivery, int>(x => x.Documents) > 0 ? docs : null;
  189. }
  190. protected override Delivery CreateItem()
  191. {
  192. var result = base.CreateItem();
  193. result.Job.ID = Job?.ID ?? Guid.Empty;
  194. result.Job.Synchronise(Job ?? new Job());
  195. return result;
  196. }
  197. private AssignmentGrid ag = null;
  198. public bool CreateBooking(CoreRow row, Guid employeeid, DateTime time)
  199. {
  200. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("{0:dd-MMM-yy hh-mm-ss} -> {1}", BookingSlot, EmployeeID));
  201. var assignment = new Assignment
  202. {
  203. Date = time.Date,
  204. Description = string.Format("Delivery #{0}", row.Get<Delivery, int>(x => x.Number))
  205. };
  206. assignment.Booked.Start = time.TimeOfDay;
  207. assignment.Booked.Finish = time.TimeOfDay.Add(new TimeSpan(2, 0, 0));
  208. assignment.Delivery.ID = row.Get<Delivery, Guid>(x => x.ID);
  209. assignment.EmployeeLink.ID = employeeid;
  210. assignment.JobLink.ID = row.Get<Delivery, Guid>(x => x.Job.ID);
  211. Logger.Send(LogType.Information, ClientFactory.UserID, "- Creating Assignment Grid");
  212. if (ag == null)
  213. ag = new AssignmentGrid();
  214. Logger.Send(LogType.Information, ClientFactory.UserID, "- Editing Assignment");
  215. if (ag.EditItems(new[] { assignment }))
  216. {
  217. using (new WaitCursor())
  218. {
  219. new Client<Assignment>().Save(assignment, "Created for Delivery");
  220. var del = new Client<Delivery>().Load(new Filter<Delivery>(x => x.ID).IsEqualTo(row.Get<Delivery, Guid>(x => x.ID)))
  221. .FirstOrDefault();
  222. del.Assignment.ID = assignment.ID;
  223. new Client<Delivery>().Save(del, "Booked via Scheduler");
  224. }
  225. DeliveryChanged?.Invoke(this);
  226. return true;
  227. }
  228. Logger.Send(LogType.Information, ClientFactory.UserID, "- Cancelled Edit");
  229. return false;
  230. }
  231. private bool BookClick(CoreRow arg)
  232. {
  233. return CreateBooking(arg, EmployeeID, BookingSlot);
  234. }
  235. private BitmapImage BookImage(CoreRow arg)
  236. {
  237. if (arg == null)
  238. {
  239. Logger.Send(LogType.Information, ClientFactory.UserID, "BookImage: Row is null!");
  240. return null;
  241. }
  242. Logger.Send(LogType.Information, ClientFactory.UserID,
  243. string.Format("BookImage {0} -> {1:dd-MMM-yy hh-mm-ss}", arg.Get<Delivery, int>(x => x.Number), BookingSlot));
  244. if (BookingSlot.IsEmpty())
  245. {
  246. Logger.Send(LogType.Information, ClientFactory.UserID, "BookImage: BookingSlot is Empty!");
  247. return null;
  248. }
  249. if (arg.Get<Delivery, DateTime>(x => x.Assignment.Date).IsEmpty())
  250. {
  251. Logger.Send(LogType.Information, ClientFactory.UserID, "BookImage: Assignment Date is Empty!");
  252. return PRSDesktop.Resources.rightarrow.AsBitmapImage();
  253. }
  254. Logger.Send(LogType.Information, ClientFactory.UserID,
  255. string.Format("BookImage Assignment Date is {0:dd-MMM-yy hh-mm-ss}", arg.Get<Delivery, DateTime>(x => x.Assignment.Date)));
  256. return null;
  257. }
  258. private bool ToggleCompleted(Button arg1, CoreRow[] arg2)
  259. {
  260. ShowAll = !ShowAll;
  261. UpdateButton(arg1, null, !ShowAll ? "Show All" : "Hide Completed");
  262. return true;
  263. }
  264. protected override void Reload(Filters<Delivery> criteria, Columns<Delivery> columns, ref SortOrder<Delivery> sort,
  265. Action<CoreTable, Exception> action)
  266. {
  267. if (Job != null)
  268. {
  269. if (Job.ID == Guid.Empty)
  270. criteria.Add(new Filter<Delivery>(x => x.Job.ID).None());
  271. else
  272. criteria.Add(new Filter<Delivery>(x => x.Job.ID).IsEqualTo(Job.ID));
  273. }
  274. if (!ShowAll)
  275. criteria.Add(new Filter<Delivery>(x => x.Completed).IsEqualTo(DateTime.MinValue));
  276. base.Reload(criteria, columns, ref sort, action);
  277. }
  278. }
  279. }