RequisitionGrid.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 ComboBox _filter;
  21. private readonly BitmapImage barcode = PRSDesktop.Resources.barcode.AsBitmapImage();
  22. private bool bSplitting;
  23. private readonly BitmapImage docs = PRSDesktop.Resources.doc_png.AsBitmapImage();
  24. private readonly BitmapImage forklift = PRSDesktop.Resources.forklift.AsBitmapImage(Color.White);
  25. private readonly BitmapImage printer = PRSDesktop.Resources.printer.AsBitmapImage();
  26. private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage(Color.White);
  27. private readonly BitmapImage truck = PRSDesktop.Resources.truck.AsBitmapImage();
  28. public RequisitionGrid()
  29. {
  30. var left = new Border
  31. {
  32. Height = 30,
  33. Width = 30,
  34. Margin = new Thickness(0, 0, 2, 2),
  35. BorderBrush = new SolidColorBrush(Colors.Gray),
  36. BorderThickness = new Thickness(0.75),
  37. Background = new SolidColorBrush(Colors.WhiteSmoke),
  38. CornerRadius = new CornerRadius(5, 0, 0, 0)
  39. };
  40. left.SetValue(DockPanel.DockProperty, Dock.Left);
  41. var right = new Border
  42. {
  43. Height = 30,
  44. Width = 30,
  45. Margin = new Thickness(2, 0, 0, 2),
  46. BorderBrush = new SolidColorBrush(Colors.Gray),
  47. BorderThickness = new Thickness(0.75),
  48. Background = new SolidColorBrush(Colors.WhiteSmoke),
  49. CornerRadius = new CornerRadius(0, 5, 0, 0)
  50. };
  51. right.SetValue(DockPanel.DockProperty, Dock.Right);
  52. _filter = new ComboBox
  53. {
  54. ItemsSource = new List<string>
  55. {
  56. "All Requisitions",
  57. "Unfilled Requisitions",
  58. "Requisitions needing Delivery",
  59. "Requisitions to be Archived"
  60. },
  61. Margin = new Thickness(0, 0, 0, 2),
  62. VerticalContentAlignment = VerticalAlignment.Center,
  63. HorizontalContentAlignment = HorizontalAlignment.Center,
  64. SelectedIndex = 2,
  65. Height = 30
  66. };
  67. _filter.SetValue(DockPanel.DockProperty, Dock.Left);
  68. _filter.SelectionChanged += (o, e) => Refresh(false, true);
  69. var header = new DockPanel();
  70. header.Children.Add(left);
  71. header.Children.Add(right);
  72. header.Children.Add(_filter);
  73. Header = header;
  74. ActionColumns.Add(new DynamicImageColumn(DocumentsImage, DocumentsClick) { Position = DynamicActionColumnPosition.Start });
  75. ActionColumns.Add(new DynamicImageColumn(FilledImage));
  76. ActionColumns.Add(new DynamicImageColumn(DeliveryImage));
  77. ActionColumns.Add(new DynamicImageColumn(StockImage));
  78. //ActionColumns.Add(new DynamicImageColumn() { Action = LabelClick, Image = GetLabelImage });
  79. //ActionColumns.Add(new DynamicImageColumn() { Action = DeliveryDocketClick, Image = GetPrinterImage });
  80. HiddenColumns.Add(x => x.JobLink.ID);
  81. HiddenColumns.Add(x => x.JobLink.JobNumber);
  82. HiddenColumns.Add(x => x.JobLink.Name);
  83. HiddenColumns.Add(x => x.Boxes);
  84. HiddenColumns.Add(x => x.Filled);
  85. HiddenColumns.Add(x => x.Title);
  86. HiddenColumns.Add(x => x.Request);
  87. HiddenColumns.Add(x => x.Notes);
  88. HiddenColumns.Add(x => x.Number);
  89. HiddenColumns.Add(x => x.Employee.ID);
  90. HiddenColumns.Add(x => x.Employee.Name);
  91. HiddenColumns.Add(x => x.Due);
  92. HiddenColumns.Add(x => x.RequestedBy.ID);
  93. HiddenColumns.Add(x => x.RequestedBy.Name);
  94. HiddenColumns.Add(x => x.Documents);
  95. HiddenColumns.Add(x => x.Archived);
  96. HiddenColumns.Add(x => x.TakenBy.ID);
  97. HiddenColumns.Add(x => x.TakenBy.Name);
  98. HiddenColumns.Add(x => x.TakenBy.Deleted);
  99. HiddenColumns.Add(x => x.Delivery.ID);
  100. HiddenColumns.Add(x => x.Delivery.Number);
  101. HiddenColumns.Add(x => x.Delivery.Completed);
  102. HiddenColumns.Add(x => x.Delivery.Deleted);
  103. HiddenColumns.Add(x => x.StockUpdated);
  104. //CoreTable employees = new Client<Employee>().Query(
  105. // null,
  106. // new Columns<Employee>(x => x.ID, x => x.Name),
  107. // new SortOrder<Employee>(x => x.Name)
  108. //);
  109. //foreach (CoreRow row in employees.Rows)
  110. // _Employees[row.Get<Employee, Guid>(x => x.ID)] = row.Get<Employee, String>(x => x.Name);
  111. //Job[] jobs = new Client<Job>().Load(null, new SortOrder<Job>(x => x.JobNumber));
  112. //foreach (Job job in jobs)
  113. //_Jobs[job] = job.ToString();
  114. AddButton("-", PRSDesktop.Resources.box.AsBitmapImage(Color.White), DelBoxClick);
  115. AddButton("+", PRSDesktop.Resources.box.AsBitmapImage(Color.White), AddBoxClick);
  116. AddButton("Split", PRSDesktop.Resources.split.AsBitmapImage(), SplitRequiClick);
  117. //AddButton("Labels", PRSDesktop.Resources.barcode.AsBitmapImage(System.Drawing.Color.White), LabelClick);
  118. //AddButton("Del Dkt", PRSDesktop.Resources.printer.AsBitmapImage(System.Drawing.Color.White), DeliveryDocketClick);
  119. //AddButton("Complete", PRSDesktop.Resources.tick.AsBitmapImage(System.Drawing.Color.White), FilledClick);
  120. OnCustomiseEditor += CustomiseEditor;
  121. }
  122. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  123. {
  124. base.DoReconfigure(options);
  125. options.BeginUpdate();
  126. options.AddRange(DynamicGridOption.SelectColumns, DynamicGridOption.AddRows, DynamicGridOption.EditRows, DynamicGridOption.FilterRows);
  127. if (Security.IsAllowed<CanDeleteStoresRequisitions>())
  128. options.Add(DynamicGridOption.DeleteRows);
  129. options.EndUpdate();
  130. }
  131. public CoreTable Requisitions { get; private set; }
  132. private void CustomiseEditor(IDynamicEditorForm sender, Requisition[]? items, DynamicGridColumn column, BaseEditor editor)
  133. {
  134. if (column.ColumnName.Equals("Notes"))
  135. {
  136. if (editor is NotesEditor notes)
  137. notes.AlwaysEnabled = bSplitting;
  138. }
  139. else if (column.ColumnName.Equals("Filled"))
  140. {
  141. editor.Editable = Security.IsAllowed<CanSkipRequisitionPhotos>() || (items != null && items.Any() && items.First().Documents > 0)
  142. ? Editable.Enabled
  143. : Editable.Hidden;
  144. }
  145. else if (column.ColumnName.Equals("TakenBy.ID"))
  146. {
  147. editor.Editable = items != null && items.Any() && !items.First().Filled.IsEmpty() ? Editable.Enabled : Editable.Disabled;
  148. }
  149. }
  150. protected override Dictionary<string, object?> EditorValueChanged(IDynamicEditorForm editor, Requisition[] items, string name, object value)
  151. {
  152. var result = base.EditorValueChanged(editor, items, name, value);
  153. if (name.Equals("TakenBy.ID") && (value == null || Equals(value, Guid.Empty)))
  154. editor.SetEditorValue("Archived", DateTime.MinValue);
  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<Requisition, Guid>(x => x.ID);
  165. var table = new Client<RequisitionDocument>().Query(
  166. new Filter<RequisitionDocument>(x => x.EntityLink.ID).IsEqualTo(deliveryid)
  167. );
  168. foreach (var row in table.Rows)
  169. docs.Add(row.ToObject<RequisitionDocument>());
  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 is null)
  187. return docs;
  188. return arg.Get<Requisition, int>(x => x.Documents) > 0 ? docs : null;
  189. }
  190. private bool SplitRequiClick(Button sender, CoreRow[] rows)
  191. {
  192. if (rows == null || rows.Length != 1)
  193. {
  194. MessageBox.Show("Please select a single Requisition to Split!");
  195. return false;
  196. }
  197. var requi = rows.First().ToObject<Requisition>();
  198. requi.Number = 0;
  199. requi.ID = Guid.Empty;
  200. requi.Filled = DateTime.MinValue;
  201. requi.Notes = new[]
  202. {
  203. string.Format("Items unavailable on Requisition #{0}:\n{1}",
  204. rows.First().Get<Requisition, int>(x => x.Number),
  205. string.Join("\n\n==============================", requi.Notes)
  206. )
  207. };
  208. requi.CommitChanges();
  209. bSplitting = true;
  210. if (EditItems(new[] { requi }))
  211. {
  212. bSplitting = false;
  213. new Client<Requisition>().Save(requi, "Created by Splitting Requi #" + rows.First().Get<Requisition, int>(x => x.Number));
  214. var newrequi = requi.Number;
  215. var notes = requi.Notes.First();
  216. requi = rows.First().ToObject<Requisition>();
  217. var oldnotes = requi.Notes.ToList();
  218. oldnotes.Insert(0,
  219. string.Format("Created secondary Requisition #{0} for further action:\n{1}\n\nOriginal Requisition Notes:\n", newrequi, notes));
  220. requi.Notes = oldnotes.ToArray();
  221. new Client<Requisition>().Save(requi, "Split Requisition to #" + newrequi);
  222. return true;
  223. }
  224. bSplitting = false;
  225. return false;
  226. }
  227. private bool AddBoxClick(Button btn, CoreRow[] rows)
  228. {
  229. if (rows.Length != 1)
  230. {
  231. MessageBox.Show("Please select one row to process!");
  232. return false;
  233. }
  234. var row = rows.First();
  235. var id = row.Get<Requisition, Guid>(x => x.ID);
  236. var req = new Client<Requisition>().Load(
  237. new Filter<Requisition>(x => x.ID).IsEqualTo(id)
  238. ).FirstOrDefault();
  239. if (req != null)
  240. {
  241. req.Boxes++;
  242. new Client<Requisition>().Save(req, string.Format("Set Number of Boxes to {0}", req.Boxes));
  243. //OnRequisitionBoxesChanged?.Invoke(req.ID, req.Boxes);
  244. return true;
  245. }
  246. MessageBox.Show("Cannot locate Requisition");
  247. return true;
  248. }
  249. private bool DelBoxClick(Button btn, CoreRow[] rows)
  250. {
  251. if (rows.Length != 1)
  252. {
  253. MessageBox.Show("Please select one row to process!");
  254. return false;
  255. }
  256. var row = rows.First();
  257. var id = row.Get<Requisition, Guid>(x => x.ID);
  258. var req = new Client<Requisition>().Load(
  259. new Filter<Requisition>(x => x.ID).IsEqualTo(id)
  260. ).FirstOrDefault();
  261. if (req != null)
  262. {
  263. if (req.Boxes > 0)
  264. {
  265. req.Boxes--;
  266. new Client<Requisition>().Save(req, string.Format("Set Number of Boxes to {0}", req.Boxes));
  267. //OnRequisitionBoxesChanged?.Invoke(req.ID, req.Boxes);
  268. }
  269. return true;
  270. }
  271. MessageBox.Show("Cannot Locate Requisition!");
  272. return true;
  273. }
  274. protected override Requisition CreateItem()
  275. {
  276. var requi = base.CreateItem();
  277. var role = new Client<Role>().Load(new Filter<Role>(x => x.Code).IsEqualTo("STORES")).FirstOrDefault();
  278. if (role != null)
  279. {
  280. var emprole = new Client<EmployeeRole>().Load(new Filter<EmployeeRole>(x => x.RoleLink.ID).IsEqualTo(role.ID)).FirstOrDefault();
  281. if (emprole != null)
  282. requi.Employee.ID = emprole.EmployeeLink.ID;
  283. }
  284. return requi;
  285. }
  286. private BitmapImage? GetBitmapImage(CoreRow? row, BitmapImage image)
  287. {
  288. if (row == null)
  289. return image;
  290. var filled = row.Get<Requisition, DateTime>(x => x.Filled);
  291. return filled.IsEmpty() ? null : image;
  292. }
  293. private BitmapImage? GetLabelImage(CoreRow row)
  294. {
  295. return GetBitmapImage(row, barcode);
  296. }
  297. private BitmapImage? GetPrinterImage(CoreRow row)
  298. {
  299. return GetBitmapImage(row, printer);
  300. }
  301. private BitmapImage? FilledImage(CoreRow? row)
  302. {
  303. if (row == null)
  304. return tick;
  305. var filled = row.Get<Requisition, DateTime>(x => x.Filled);
  306. return filled.IsEmpty() ? null : tick;
  307. }
  308. private BitmapImage? StockImage(CoreRow? row)
  309. {
  310. if (row == null)
  311. return forklift;
  312. var stockupdated = row.Get<Requisition, DateTime>(x => x.StockUpdated);
  313. return stockupdated.IsEmpty() ? null : forklift;
  314. }
  315. private BitmapImage? DeliveryImage(CoreRow? row)
  316. {
  317. if (row == null)
  318. return truck;
  319. var archived = row.Get<Requisition, DateTime>(x => x.Archived);
  320. return archived.IsEmpty() ? null : truck;
  321. }
  322. private void SendNotifications(CoreRow row)
  323. {
  324. var updates = new List<Notification>();
  325. var roles = new Client<EmployeeRole>().Query(new Filter<EmployeeRole>(x => x.RoleLink.Code).IsEqualTo("DELIVERIES"));
  326. foreach (var role in roles.Rows)
  327. {
  328. var notification = new Notification
  329. {
  330. Title = string.Format("Requi #{0} is ready", row.Get<Requisition, int>(x => x.Number)),
  331. Description = "The requisition has been packed and labelled, and is ready to be assigned to a Delivery"
  332. };
  333. notification.Sender.ID = row.Get<Requisition, Guid>(x => x.Employee.ID);
  334. notification.Employee.ID = role.Get<EmployeeRole, Guid>(x => x.EmployeeLink.ID);
  335. notification.Job.ID = row.Get<Requisition, Guid>(x => x.JobLink.ID);
  336. updates.Add(notification);
  337. }
  338. new Client<Notification>().Save(updates, "Sent Notification");
  339. }
  340. protected override void Reload(Filters<Requisition> criteria, Columns<Requisition> columns, ref SortOrder<Requisition>? sort,
  341. Action<CoreTable?, Exception?> action)
  342. {
  343. // Unfilled
  344. if (_filter.SelectedIndex == 1)
  345. criteria.Add(new Filter<Requisition>(x => x.Filled).IsEqualTo(DateTime.MinValue));
  346. // Undelivered
  347. else if (_filter.SelectedIndex == 2)
  348. criteria.Add(new Filter<Requisition>(x => x.Archived).IsEqualTo(DateTime.MinValue).Or(x => x.Filled).IsEqualTo(DateTime.MinValue));
  349. // Unarchived
  350. else if (_filter.SelectedIndex == 3)
  351. criteria.Add(new Filter<Requisition>(x => x.StockUpdated).IsEqualTo(DateTime.MinValue).Or(x => x.Archived)
  352. .IsEqualTo(DateTime.MinValue)
  353. .Or(x => x.Filled).IsEqualTo(DateTime.MinValue));
  354. sort = new SortOrder<Requisition>(x => x.Number, SortDirection.Descending);
  355. base.Reload(criteria, columns, ref sort, action);
  356. }
  357. }
  358. }