RequisitionGrid.cs 14 KB

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