RequisitionGrid.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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;
  7. using System.Windows.Media.Imaging;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.WPF;
  13. using Color = System.Drawing.Color;
  14. namespace PRSDesktop
  15. {
  16. internal class RequisitionGrid : DynamicDataGrid<Requisition>
  17. {
  18. //public Dictionary<Object, Object> _Employees = new Dictionary<object, object>();
  19. //public Dictionary<Object, Object> _Jobs = new Dictionary<object, object>();
  20. private readonly BitmapImage barcode = PRSDesktop.Resources.barcode.AsBitmapImage();
  21. private bool bSplitting;
  22. private readonly BitmapImage docs = PRSDesktop.Resources.doc_png.AsBitmapImage();
  23. private readonly BitmapImage forklift = PRSDesktop.Resources.forklift.AsBitmapImage(Color.White);
  24. private readonly BitmapImage printer = PRSDesktop.Resources.printer.AsBitmapImage();
  25. private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage(Color.White);
  26. private readonly BitmapImage truck = PRSDesktop.Resources.truck.AsBitmapImage();
  27. public RequisitionGrid()
  28. {
  29. ActionColumns.Add(new DynamicImageColumn(DocumentsImage, DocumentsClick) { Position = DynamicActionColumnPosition.Start });
  30. ActionColumns.Add(new DynamicImageColumn(FilledImage));
  31. ActionColumns.Add(new DynamicImageColumn(DeliveryImage));
  32. ActionColumns.Add(new DynamicImageColumn(StockImage));
  33. //ActionColumns.Add(new DynamicImageColumn() { Action = LabelClick, Image = GetLabelImage });
  34. //ActionColumns.Add(new DynamicImageColumn() { Action = DeliveryDocketClick, Image = GetPrinterImage });
  35. HiddenColumns.Add(x => x.JobLink.ID);
  36. HiddenColumns.Add(x => x.JobLink.JobNumber);
  37. HiddenColumns.Add(x => x.JobLink.Name);
  38. HiddenColumns.Add(x => x.JobScope.ID);
  39. HiddenColumns.Add(x => x.Boxes);
  40. HiddenColumns.Add(x => x.Filled);
  41. HiddenColumns.Add(x => x.Title);
  42. HiddenColumns.Add(x => x.Request);
  43. HiddenColumns.Add(x => x.Notes);
  44. HiddenColumns.Add(x => x.Number);
  45. HiddenColumns.Add(x => x.Employee.ID);
  46. HiddenColumns.Add(x => x.Employee.Name);
  47. HiddenColumns.Add(x => x.Due);
  48. HiddenColumns.Add(x => x.RequestedBy.ID);
  49. HiddenColumns.Add(x => x.RequestedBy.Name);
  50. HiddenColumns.Add(x => x.Documents);
  51. HiddenColumns.Add(x => x.Archived);
  52. HiddenColumns.Add(x => x.TakenBy.ID);
  53. HiddenColumns.Add(x => x.TakenBy.Name);
  54. HiddenColumns.Add(x => x.Delivery.ID);
  55. HiddenColumns.Add(x => x.Delivery.Number);
  56. HiddenColumns.Add(x => x.Delivery.Completed);
  57. HiddenColumns.Add(x => x.StockUpdated);
  58. //CoreTable employees = new Client<Employee>().Query(
  59. // null,
  60. // new Columns<Employee>(x => x.ID, x => x.Name),
  61. // new SortOrder<Employee>(x => x.Name)
  62. //);
  63. //foreach (CoreRow row in employees.Rows)
  64. // _Employees[row.Get<Employee, Guid>(x => x.ID)] = row.Get<Employee, String>(x => x.Name);
  65. //Job[] jobs = new Client<Job>().Load(null, new SortOrder<Job>(x => x.JobNumber));
  66. //foreach (Job job in jobs)
  67. //_Jobs[job] = job.ToString();
  68. AddButton("-", PRSDesktop.Resources.box.AsBitmapImage(Color.White), DelBoxClick);
  69. AddButton("+", PRSDesktop.Resources.box.AsBitmapImage(Color.White), AddBoxClick);
  70. AddButton("Split", PRSDesktop.Resources.split.AsBitmapImage(), SplitRequiClick);
  71. //AddButton("Labels", PRSDesktop.Resources.barcode.AsBitmapImage(System.Drawing.Color.White), LabelClick);
  72. //AddButton("Del Dkt", PRSDesktop.Resources.printer.AsBitmapImage(System.Drawing.Color.White), DeliveryDocketClick);
  73. //AddButton("Complete", PRSDesktop.Resources.tick.AsBitmapImage(System.Drawing.Color.White), FilledClick);
  74. OnCustomiseEditor += CustomiseEditor;
  75. }
  76. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  77. {
  78. base.DoReconfigure(options);
  79. options.BeginUpdate();
  80. options.AddRange(DynamicGridOption.SelectColumns, DynamicGridOption.AddRows, DynamicGridOption.EditRows, DynamicGridOption.FilterRows);
  81. if (Security.IsAllowed<CanDeleteStoresRequisitions>())
  82. options.Add(DynamicGridOption.DeleteRows);
  83. options.EndUpdate();
  84. }
  85. public CoreTable Requisitions { get; private set; }
  86. private void CustomiseEditor(IDynamicEditorForm sender, Requisition[]? items, DynamicGridColumn column, BaseEditor editor)
  87. {
  88. if (column.ColumnName.Equals("Notes"))
  89. {
  90. if (editor is NotesEditor notes)
  91. notes.AlwaysEnabled = bSplitting;
  92. }
  93. else if (column.ColumnName.Equals("Filled"))
  94. {
  95. editor.Editable = Security.IsAllowed<CanSkipRequisitionPhotos>() || (items != null && items.Any() && items.First().Documents > 0)
  96. ? Editable.Enabled
  97. : Editable.Hidden;
  98. }
  99. else if (column.ColumnName.Equals("TakenBy.ID"))
  100. {
  101. editor.Editable = items != null && items.Any() && !items.First().Filled.IsEmpty() ? Editable.Enabled : Editable.Disabled;
  102. }
  103. }
  104. protected override Dictionary<string, object?> EditorValueChanged(IDynamicEditorForm editor, Requisition[] items, string name, object value)
  105. {
  106. var result = base.EditorValueChanged(editor, items, name, value);
  107. if (name.Equals("TakenBy.ID") && (value == null || Equals(value, Guid.Empty)))
  108. editor.SetEditorValue("Archived", DateTime.MinValue);
  109. else if (name.Equals("JobLink.ID"))
  110. {
  111. // false here because a job has a defaultscope
  112. // and we need to load the lookups before we set the default value
  113. var scope = editor.FindEditor("JobScope.ID") as ILookupEditorControl;
  114. if (scope != null)
  115. DefineLookups(scope,items,false);
  116. }
  117. return result;
  118. }
  119. private bool DocumentsClick(CoreRow? arg)
  120. {
  121. if (arg == null)
  122. return false;
  123. var docs = new List<IEntityDocument>();
  124. using (new WaitCursor())
  125. {
  126. var deliveryid = arg.Get<Requisition, Guid>(x => x.ID);
  127. var table = new Client<RequisitionDocument>().Query(
  128. new Filter<RequisitionDocument>(x => x.EntityLink.ID).IsEqualTo(deliveryid)
  129. );
  130. foreach (var row in table.Rows)
  131. docs.Add(row.ToObject<RequisitionDocument>());
  132. }
  133. if (docs.Any())
  134. {
  135. var editor = new DocumentEditor(docs.ToArray());
  136. //editor.PrintAllowed = Security.IsAllowed<CanPrintFactoryFloorDrawings>();
  137. editor.SaveAllowed = Security.IsAllowed<CanSaveFactoryFloorDrawings>();
  138. editor.ShowDialog();
  139. }
  140. else
  141. {
  142. MessageBox.Show("No Documents Available!");
  143. }
  144. return false;
  145. }
  146. private BitmapImage? DocumentsImage(CoreRow? arg)
  147. {
  148. if (arg is null)
  149. return docs;
  150. return arg.Get<Requisition, int>(x => x.Documents) > 0 ? docs : null;
  151. }
  152. private bool SplitRequiClick(Button sender, CoreRow[] rows)
  153. {
  154. if (rows == null || rows.Length != 1)
  155. {
  156. MessageBox.Show("Please select a single Requisition to Split!");
  157. return false;
  158. }
  159. var requi = rows.First().ToObject<Requisition>();
  160. requi.Number = 0;
  161. requi.ID = Guid.Empty;
  162. requi.Filled = DateTime.MinValue;
  163. requi.Notes = new[]
  164. {
  165. string.Format("Items unavailable on Requisition #{0}:\n{1}",
  166. rows.First().Get<Requisition, int>(x => x.Number),
  167. string.Join("\n\n==============================", requi.Notes)
  168. )
  169. };
  170. requi.CommitChanges();
  171. bSplitting = true;
  172. if (EditItems(new[] { requi }))
  173. {
  174. bSplitting = false;
  175. new Client<Requisition>().Save(requi, "Created by Splitting Requi #" + rows.First().Get<Requisition, int>(x => x.Number));
  176. var newrequi = requi.Number;
  177. var notes = requi.Notes.First();
  178. requi = rows.First().ToObject<Requisition>();
  179. var oldnotes = requi.Notes.ToList();
  180. oldnotes.Insert(0,
  181. string.Format("Created secondary Requisition #{0} for further action:\n{1}\n\nOriginal Requisition Notes:\n", newrequi, notes));
  182. requi.Notes = oldnotes.ToArray();
  183. new Client<Requisition>().Save(requi, "Split Requisition to #" + newrequi);
  184. return true;
  185. }
  186. bSplitting = false;
  187. return false;
  188. }
  189. private bool AddBoxClick(Button btn, CoreRow[] rows)
  190. {
  191. if (rows.Length != 1)
  192. {
  193. MessageBox.Show("Please select one row to process!");
  194. return false;
  195. }
  196. var row = rows.First();
  197. var id = row.Get<Requisition, Guid>(x => x.ID);
  198. var req = new Client<Requisition>().Load(
  199. new Filter<Requisition>(x => x.ID).IsEqualTo(id)
  200. ).FirstOrDefault();
  201. if (req != null)
  202. {
  203. req.Boxes++;
  204. new Client<Requisition>().Save(req, string.Format("Set Number of Boxes to {0}", req.Boxes));
  205. //OnRequisitionBoxesChanged?.Invoke(req.ID, req.Boxes);
  206. return true;
  207. }
  208. MessageBox.Show("Cannot locate Requisition");
  209. return true;
  210. }
  211. private bool DelBoxClick(Button btn, CoreRow[] rows)
  212. {
  213. if (rows.Length != 1)
  214. {
  215. MessageBox.Show("Please select one row to process!");
  216. return false;
  217. }
  218. var row = rows.First();
  219. var id = row.Get<Requisition, Guid>(x => x.ID);
  220. var req = new Client<Requisition>().Load(
  221. new Filter<Requisition>(x => x.ID).IsEqualTo(id)
  222. ).FirstOrDefault();
  223. if (req != null)
  224. {
  225. if (req.Boxes > 0)
  226. {
  227. req.Boxes--;
  228. new Client<Requisition>().Save(req, string.Format("Set Number of Boxes to {0}", req.Boxes));
  229. //OnRequisitionBoxesChanged?.Invoke(req.ID, req.Boxes);
  230. }
  231. return true;
  232. }
  233. MessageBox.Show("Cannot Locate Requisition!");
  234. return true;
  235. }
  236. protected override Requisition CreateItem()
  237. {
  238. var requi = base.CreateItem();
  239. var role = new Client<Role>().Load(new Filter<Role>(x => x.Code).IsEqualTo("STORES")).FirstOrDefault();
  240. if (role != null)
  241. {
  242. var emprole = new Client<EmployeeRole>().Load(new Filter<EmployeeRole>(x => x.RoleLink.ID).IsEqualTo(role.ID)).FirstOrDefault();
  243. if (emprole != null)
  244. requi.Employee.ID = emprole.EmployeeLink.ID;
  245. }
  246. return requi;
  247. }
  248. private BitmapImage? GetBitmapImage(CoreRow? row, BitmapImage image)
  249. {
  250. if (row == null)
  251. return image;
  252. var filled = row.Get<Requisition, DateTime>(x => x.Filled);
  253. return filled.IsEmpty() ? null : image;
  254. }
  255. private BitmapImage? GetLabelImage(CoreRow row)
  256. {
  257. return GetBitmapImage(row, barcode);
  258. }
  259. private BitmapImage? GetPrinterImage(CoreRow row)
  260. {
  261. return GetBitmapImage(row, printer);
  262. }
  263. private BitmapImage? FilledImage(CoreRow? row)
  264. {
  265. if (row == null)
  266. return tick;
  267. var filled = row.Get<Requisition, DateTime>(x => x.Filled);
  268. return filled.IsEmpty() ? null : tick;
  269. }
  270. private BitmapImage? StockImage(CoreRow? row)
  271. {
  272. if (row == null)
  273. return forklift;
  274. var stockupdated = row.Get<Requisition, DateTime>(x => x.StockUpdated);
  275. return stockupdated.IsEmpty() ? null : forklift;
  276. }
  277. private BitmapImage? DeliveryImage(CoreRow? row)
  278. {
  279. if (row == null)
  280. return truck;
  281. var archived = row.Get<Requisition, DateTime>(x => x.Archived);
  282. return archived.IsEmpty() ? null : truck;
  283. }
  284. private void SendNotifications(CoreRow row)
  285. {
  286. var updates = new List<Notification>();
  287. var roles = new Client<EmployeeRole>().Query(new Filter<EmployeeRole>(x => x.RoleLink.Code).IsEqualTo("DELIVERIES"));
  288. foreach (var role in roles.Rows)
  289. {
  290. var notification = new Notification
  291. {
  292. Title = string.Format("Requi #{0} is ready", row.Get<Requisition, int>(x => x.Number)),
  293. Description = "The requisition has been packed and labelled, and is ready to be assigned to a Delivery"
  294. };
  295. notification.Sender.ID = row.Get<Requisition, Guid>(x => x.Employee.ID);
  296. notification.Employee.ID = role.Get<EmployeeRole, Guid>(x => x.EmployeeLink.ID);
  297. notification.Job.ID = row.Get<Requisition, Guid>(x => x.JobLink.ID);
  298. updates.Add(notification);
  299. }
  300. new Client<Notification>().Save(updates, "Sent Notification");
  301. }
  302. protected override void Reload(Filters<Requisition> criteria, Columns<Requisition> columns, ref SortOrder<Requisition>? sort,
  303. Action<CoreTable?, Exception?> action)
  304. {
  305. sort = new SortOrder<Requisition>(x => x.Number, SortDirection.Descending);
  306. base.Reload(criteria, columns, ref sort, action);
  307. }
  308. }
  309. }