IssuesGrid.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.DynamicGrid;
  4. using InABox.WPF;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Controls;
  13. using System.Windows.Media;
  14. using InABox.Wpf;
  15. using System.Windows;
  16. namespace PRSDesktop.Forms.Issues;
  17. public class IssuesGrid : DynamicGrid<Kanban>, ISpecificGrid
  18. {
  19. private readonly int ChunkSize = 500;
  20. public IQueryProviderFactory ClientFactory { get; set; }
  21. private IQueryProvider<Kanban>? _kanbanClient;
  22. private IQueryProvider<Kanban> KanbanClient
  23. {
  24. get
  25. {
  26. _kanbanClient ??= ClientFactory.Create<Kanban>();
  27. return _kanbanClient;
  28. }
  29. }
  30. private IQueryProvider<Job>? _jobClient;
  31. private IQueryProvider<Job> JobClient
  32. {
  33. get
  34. {
  35. _jobClient ??= ClientFactory.Create<Job>();
  36. return _jobClient;
  37. }
  38. }
  39. public Guid CustomerID { get; set; }
  40. // public static CustomProperty CustomerProperty = new CustomProperty
  41. // {
  42. // Name = "CustomerID",
  43. // PropertyType = typeof(string),
  44. // ClassType = typeof(Kanban)
  45. // };
  46. private String _baseDirectory;
  47. public IssuesGrid() : base()
  48. {
  49. var cols = LookupFactory.DefineColumns<Kanban>();
  50. // Minimum Columns for Lookup values
  51. foreach (var col in cols)
  52. HiddenColumns.Add(col);
  53. HiddenColumns.Add(x => x.Notes);
  54. ActionColumns.Add(new DynamicMenuColumn(BuildMenu) { Position = DynamicActionColumnPosition.End });
  55. }
  56. private class UIComponent : DynamicGridGridUIComponent<Kanban>
  57. {
  58. private IssuesGrid Grid;
  59. public UIComponent(IssuesGrid grid)
  60. {
  61. Grid = grid;
  62. Parent = grid;
  63. }
  64. protected override Brush? GetCellBackground(CoreRow row, DynamicColumnBase column)
  65. {
  66. var status = row.Get<Kanban, KanbanStatus>(x => x.Status);
  67. var color = status == KanbanStatus.Open
  68. ? Colors.Orange
  69. : status == KanbanStatus.InProgress
  70. ? Colors.Plum
  71. : status == KanbanStatus.Waiting
  72. ? Colors.LightGreen
  73. : Colors.Silver;
  74. return color.ToBrush(0.5);
  75. }
  76. }
  77. protected override IDynamicGridUIComponent<Kanban> CreateUIComponent()
  78. {
  79. return new UIComponent(this);
  80. }
  81. protected override void Init()
  82. {
  83. base.Init();
  84. AddButton("Check for Updates", PRSDesktop.Resources.autoupdate.AsBitmapImage(), CheckForUpdates);
  85. AddButton("Open Support Session", PRSDesktop.Resources.appicon.AsBitmapImage(), OpenSupportSession);
  86. _baseDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) ?? "";
  87. if (File.Exists(Path.Combine(_baseDirectory, "PRSAvalonia", "PRS.Avalonia.Desktop.exe")))
  88. {
  89. var btn = AddButton("PRS Mobile App", PRSDesktop.Resources.map.AsBitmapImage(), LaunchPRSMobile);
  90. btn.Margin = new Thickness(20, btn.Margin.Top, btn.Margin.Right, btn.Margin.Bottom);
  91. }
  92. if (File.Exists(Path.Combine(_baseDirectory, "PRSDigitalKey", "PRS.DigitalKey.Desktop.exe")))
  93. AddButton("PRS Digital Key App", PRSDesktop.Resources.key.AsBitmapImage(), LaunchPRSDigitalKey);
  94. }
  95. private bool LaunchPRSMobile(Button button, CoreRow[] rows)
  96. {
  97. var _mobileApp = System.IO.Path.Combine(_baseDirectory, "PRSAvalonia", "PRS.Avalonia.Desktop.exe");
  98. var _info = new ProcessStartInfo(_mobileApp);
  99. Process.Start(_info);
  100. return false;
  101. }
  102. private bool LaunchPRSDigitalKey(Button button, CoreRow[] rows)
  103. {
  104. var _mobileApp = Path.Combine(_baseDirectory, "PRSDigitalKey", "PRS.DigitalKey.Desktop.exe");
  105. var _info = new ProcessStartInfo(_mobileApp);
  106. Process.Start(_info);
  107. return false;
  108. }
  109. private bool OpenSupportSession(Button button, CoreRow[] rows)
  110. {
  111. SupportUtils.OpenSupportSession();
  112. return false;
  113. }
  114. private bool CheckForUpdates(Button button, CoreRow[] rows)
  115. {
  116. if (SupportUtils.CheckForUpdates())
  117. {
  118. Application.Current.Shutdown();
  119. }
  120. else
  121. {
  122. if (MessageWindow.ShowYesNo(
  123. "You appear to be using the latest version already!\n\nRun the installer anyway?", "Update"))
  124. {
  125. if (SupportUtils.DownloadAndRunInstaller())
  126. {
  127. Application.Current.Shutdown();
  128. }
  129. }
  130. }
  131. return false;
  132. }
  133. protected override void DoReconfigure(DynamicGridOptions options)
  134. {
  135. options.Clear();
  136. options.AddRows = true;
  137. options.EditRows = true;
  138. options.FilterRows = true;
  139. options.HideDatabaseFilters = true;
  140. }
  141. private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
  142. {
  143. if (row is null) return;
  144. var menu = column.GetMenu();
  145. menu.AddItem("Add note", null, row, AddNote_Click);
  146. menu.AddItem("Attach system logs", null, row, AttachLogs_Click);
  147. menu.AddSeparator();
  148. menu.AddItem("Close issue", null, row, CloseTask_Click);
  149. }
  150. private void AttachLogs_Click(CoreRow row)
  151. {
  152. var logFile = CoreUtils.GetLogFile();
  153. var data = File.ReadAllBytes(logFile);
  154. var doc = new Document();
  155. doc.Data = data;
  156. doc.CRC = CoreUtils.CalculateCRC(data);
  157. doc.FileName = Path.GetFileName(logFile);
  158. doc.TimeStamp = File.GetLastWriteTime(logFile);
  159. ClientFactory.Save(doc, "Attached logs to task.");
  160. var kanbanDocument = new KanbanDocument();
  161. kanbanDocument.DocumentLink.CopyFrom(doc);
  162. kanbanDocument.EntityLink.CopyFrom(row.ToObject<Kanban>());
  163. ClientFactory.Save(kanbanDocument, "Attached logs to task.");
  164. }
  165. public override Kanban CreateItem()
  166. {
  167. var item = base.CreateItem();
  168. item.UserProperties["CustomerID"] = CustomerID.ToString();
  169. item.Notes = [
  170. $"Created on PRS {CoreUtils.GetVersion()} by {App.EmployeeName} ({App.EmployeeEmail})"
  171. ];
  172. // item.Status = KanbanStatus.Open;
  173. return item;
  174. }
  175. private void AddNote_Click(CoreRow row)
  176. {
  177. var kanban = row.ToObject<Kanban>();
  178. var text = "";
  179. if(TextBoxDialog.Execute("Enter note:", ref text))
  180. {
  181. text = string.Format("{0:yyyy-MM-dd HH:mm:ss}: {1}", DateTime.Now, text);
  182. kanban.Notes = kanban.Notes.Concatenate([text]);
  183. kanban.Status = KanbanStatus.InProgress;
  184. SaveItem(kanban);
  185. Refresh(false, true);
  186. }
  187. }
  188. private void CloseTask_Click(CoreRow row)
  189. {
  190. var kanban = row.ToObject<Kanban>();
  191. kanban.Completed = DateTime.Now;
  192. kanban.Closed = DateTime.Now;
  193. SaveItem(kanban);
  194. Refresh(false, true);
  195. }
  196. private Column<Kanban>[] AllowedColumns = [
  197. new(x => x.Number),
  198. new(x => x.Title),
  199. new(x => x.Description),
  200. new(x => x.Notes)];
  201. protected override void CustomiseEditor(IDynamicEditorForm form, Kanban[] items, DynamicGridColumn column, BaseEditor editor)
  202. {
  203. base.CustomiseEditor(form, items, column, editor);
  204. if(!AllowedColumns.Any(x => x.Property == column.ColumnName))
  205. {
  206. editor.Editable = editor.Editable.Combine(Editable.Hidden);
  207. }
  208. }
  209. public virtual CoreTable LookupValues(DataLookupEditor editor, Type parent, string columnname, BaseObject[]? items)
  210. {
  211. var client = ClientFactory.Create(editor.Type);
  212. var filter = LookupFactory.DefineLookupFilter(parent, editor.Type, columnname, items ?? (Array.CreateInstance(parent, 0) as BaseObject[])!);
  213. var columns = LookupFactory.DefineLookupColumns(parent, editor.Type, columnname);
  214. foreach (var key in editor.OtherColumns.Keys)
  215. columns.Add(key);
  216. var sort = LookupFactory.DefineSort(editor.Type);
  217. var result = client.Query(filter, columns, sort);
  218. result.Columns.Add(new CoreColumn { ColumnName = "Display", DataType = typeof(string) });
  219. foreach (var row in result.Rows)
  220. {
  221. row["Display"] = LookupFactory.FormatLookup(parent, editor.Type, row, columnname);
  222. }
  223. return result;
  224. }
  225. protected override void DefineLookups(ILookupEditorControl sender, Kanban[] items, bool async = true)
  226. {
  227. if (sender.EditorDefinition is not DataLookupEditor editor)
  228. {
  229. base.DefineLookups(sender, items, async: async);
  230. return;
  231. }
  232. var colname = sender.ColumnName;
  233. if (async)
  234. {
  235. Task.Run(() =>
  236. {
  237. try
  238. {
  239. var values = LookupValues(editor, typeof(Kanban), colname, items);
  240. Dispatcher.Invoke(
  241. () =>
  242. {
  243. try
  244. {
  245. //Logger.Send(LogType.Information, typeof(T).Name, "Dispatching Results" + colname);
  246. sender.LoadLookups(values);
  247. }
  248. catch (Exception e2)
  249. {
  250. Logger.Send(LogType.Information, typeof(Kanban).Name,
  251. "Exception (2) in LoadLookups: " + e2.Message + "\n" + e2.StackTrace);
  252. }
  253. }
  254. );
  255. }
  256. catch (Exception e)
  257. {
  258. Logger.Send(LogType.Information, typeof(Kanban).Name,
  259. "Exception (1) in LoadLookups: " + e.Message + "\n" + e.StackTrace);
  260. }
  261. });
  262. }
  263. else
  264. {
  265. var values = LookupValues(editor, typeof(Kanban), colname, items);
  266. sender.LoadLookups(values);
  267. }
  268. }
  269. public override DynamicEditorPages LoadEditorPages(Kanban item)
  270. {
  271. var pages = new DynamicEditorPages
  272. {
  273. new DynamicDocumentGrid<KanbanDocument, Kanban, KanbanLink>
  274. {
  275. Client = ClientFactory
  276. }
  277. };
  278. return pages;
  279. }
  280. protected override DynamicGridColumns LoadColumns()
  281. {
  282. var columns = new DynamicGridColumns<Kanban>();
  283. columns.Add(x => x.Number, caption: "Ticket", width: 60, alignment: Alignment.MiddleCenter);
  284. columns.Add(x => x.Title);
  285. columns.Add(x => x.CreatedBy, caption: "Created By", width: 150);
  286. columns.Add(x => x.EmployeeLink.Name, caption: "Assigned To", width: 150);
  287. columns.Add(x => x.Type.Description, caption: "Type", width: 100, alignment: Alignment.MiddleCenter);
  288. columns.Add(x => x.Status, caption: "Status", width: 80, alignment: Alignment.MiddleCenter);
  289. return columns;
  290. }
  291. #region Grid Stuff
  292. protected override string FormatRecordCount(int count)
  293. {
  294. return IsPaging
  295. ? $"{base.FormatRecordCount(count)} (loading..)"
  296. : base.FormatRecordCount(count);
  297. }
  298. protected override void Reload(
  299. Filters<Kanban> criteria, Columns<Kanban> columns, ref SortOrder<Kanban>? sort,
  300. CancellationToken token, Action<CoreTable?, Exception?> action)
  301. {
  302. criteria.Add(new Filter<Kanban>(x => x.Closed).IsEqualTo(Guid.Empty));
  303. criteria.Add(new Filter<Kanban>(x => x.Status).IsNotEqualTo(KanbanStatus.Complete));
  304. criteria.Add(new Filter<Kanban>(x => x.JobLink.Customer.ID).IsEqualTo(CustomerID));
  305. //criteria.Add(new Filter<Kanban>(CustomerProperty).IsEqualTo(CustomerID.ToString()));
  306. if(Options.PageSize > 0)
  307. {
  308. var inSort = sort;
  309. Task.Run(() =>
  310. {
  311. var page = CoreRange.Database(Options.PageSize);
  312. var filter = criteria.Combine();
  313. IsPaging = true;
  314. while (!token.IsCancellationRequested)
  315. {
  316. try
  317. {
  318. var data = KanbanClient.Query(filter, columns, inSort, page);
  319. data.Offset = page.Offset;
  320. IsPaging = data.Rows.Count == page.Limit;
  321. if (token.IsCancellationRequested)
  322. {
  323. break;
  324. }
  325. action(data, null);
  326. if (!IsPaging)
  327. break;
  328. // Proposal - Let's slow it down a bit to enhance UI responsiveness?
  329. Thread.Sleep(100);
  330. page.Next();
  331. }
  332. catch (Exception e)
  333. {
  334. action(null, e);
  335. break;
  336. }
  337. }
  338. }, token);
  339. }
  340. else
  341. {
  342. KanbanClient.Query(criteria.Combine(), columns, sort, null, action);
  343. }
  344. }
  345. public override Kanban[] LoadItems(IList<CoreRow> rows)
  346. {
  347. var results = new List<Kanban>(rows.Count);
  348. for (var i = 0; i < rows.Count; i += ChunkSize)
  349. {
  350. var chunk = rows.Skip(i).Take(ChunkSize);
  351. var filter = new Filter<Kanban>(x => x.ID).InList(chunk.Select(x => x.Get<Kanban, Guid>(x => x.ID)).ToArray());
  352. var columns = DynamicGridUtils.LoadEditorColumns(Columns.None<Kanban>());
  353. var data = KanbanClient.Query(filter, columns);
  354. results.AddRange(data.ToObjects<Kanban>());
  355. }
  356. return results.ToArray();
  357. }
  358. public override Kanban LoadItem(CoreRow row)
  359. {
  360. var id = row.Get<Kanban, Guid>(x => x.ID);
  361. return KanbanClient.Query(
  362. new Filter<Kanban>(x => x.ID).IsEqualTo(id),
  363. DynamicGridUtils.LoadEditorColumns(Columns.None<Kanban>())).ToObjects<Kanban>().FirstOrDefault()
  364. ?? throw new Exception($"No Kanban with ID {id}");
  365. }
  366. public override void SaveItem(Kanban item)
  367. {
  368. CheckJob(item);
  369. KanbanClient.Save(item, "Edited by User");
  370. }
  371. private void CheckJob(Kanban item)
  372. {
  373. if (item.ID == Guid.Empty)
  374. {
  375. item.CreatedBy = App.EmployeeName;
  376. // Check if there is an open Project Job (ie installation or periodic billing) for this Client
  377. var job = JobClient.Query(
  378. new Filter<Job>(x => x.Customer.ID).IsEqualTo(CustomerID)
  379. .And(x => x.JobType).IsEqualTo(JobType.Project)
  380. .And(x => x.JobStatus.Active).IsEqualTo(true),
  381. Columns.None<Job>()
  382. .Add(x => x.ID)
  383. .Add(x=>x.DefaultScope.ID)
  384. ).ToObjects<Job>().FirstOrDefault();
  385. // No Job ? Create a service job for this ticket
  386. if (job == null)
  387. {
  388. job = new Job();
  389. job.Name = item.Title;
  390. job.Customer.ID = CustomerID;
  391. job.JobType = JobType.Service;
  392. job.Notes = item.Notes?.ToList().ToArray() ?? [];
  393. job.UserProperties.Clear();
  394. JobClient.Save(job, "Created by Client Issues Screen");
  395. }
  396. // Created Tickets should always have a job #!
  397. item.JobLink.ID = job.ID;
  398. item.JobScope.ID = job.DefaultScope.ID;
  399. }
  400. }
  401. public override void SaveItems(IEnumerable<Kanban> items)
  402. {
  403. var list = items.ToArray();
  404. foreach (var item in list)
  405. CheckJob(item);
  406. KanbanClient.Save(list, "Edited by User");
  407. }
  408. public override void DeleteItems(params CoreRow[] rows)
  409. {
  410. var deletes = new List<Kanban>();
  411. foreach (var row in rows)
  412. {
  413. var delete = new Kanban
  414. {
  415. ID = row.Get<Kanban, Guid>(x => x.ID)
  416. };
  417. deletes.Add(delete);
  418. }
  419. KanbanClient.Delete(deletes, "Deleted on User Request");
  420. }
  421. #endregion
  422. }