RequisitionGrid.cs 16 KB

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