DeliveryList.cs 12 KB

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