DigitalFormsDashboard.xaml.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. using com.sun.tools.doclets.formats.html;
  2. using Comal.Classes;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. using InABox.DynamicGrid;
  6. using InABox.Reports;
  7. using InABox.Reports.Common;
  8. using InABox.Scripting;
  9. using InABox.WPF;
  10. using PRSDesktop.Configuration;
  11. using PRSDesktop.Forms;
  12. using PRSDesktop.WidgetGroups;
  13. using Syncfusion.UI.Xaml.Grid;
  14. using Syncfusion.UI.Xaml.Grid.Converter;
  15. using Syncfusion.Windows.Shared;
  16. using Syncfusion.XlsIO;
  17. using System;
  18. using System.Collections;
  19. using System.Collections.Generic;
  20. using System.Data;
  21. using System.Diagnostics;
  22. using System.Diagnostics.CodeAnalysis;
  23. using System.Linq;
  24. using System.Linq.Expressions;
  25. using System.Reflection;
  26. using System.Text;
  27. using System.Text.RegularExpressions;
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using System.Windows;
  31. using System.Windows.Controls;
  32. using System.Windows.Data;
  33. using System.Windows.Documents;
  34. using System.Windows.Input;
  35. using System.Windows.Media;
  36. using System.Windows.Media.Imaging;
  37. using System.Windows.Navigation;
  38. using System.Windows.Shapes;
  39. using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;
  40. namespace PRSDesktop
  41. {
  42. public enum DateFilterType
  43. {
  44. Today,
  45. Yesterday,
  46. Week,
  47. SevenDays,
  48. Month,
  49. ThirtyDays,
  50. Year,
  51. TwelveMonths,
  52. Custom
  53. }
  54. public class DFFilter : BaseObject
  55. {
  56. [EditorSequence(1)]
  57. [TextBoxEditor]
  58. public string Name { get; set; }
  59. [EditorSequence(2)]
  60. [FilterEditor]
  61. public string Filter { get; set; }
  62. protected override void Init()
  63. {
  64. base.Init();
  65. Name = "";
  66. Filter = "";
  67. }
  68. }
  69. public class DigitalFormsDashboardProperties : IDashboardProperties
  70. {
  71. public bool ShowJobFilter { get; set; } = false;
  72. public bool ShowDateFilter { get; set; } = true;
  73. public Guid JobID { get; set; }
  74. public DateFilterType DateFilterType { get; set; } = DateFilterType.Today;
  75. public DateTime FromDate { get; set; }
  76. public DateTime ToDate { get; set; }
  77. public Dictionary<string, List<DFFilter>> Filters { get; set; } = new();
  78. }
  79. public class DigitalFormsDashboardElement : DashboardElement<DigitalFormsDashboard, Common, DigitalFormsDashboardProperties> { }
  80. /// <summary>
  81. /// Interaction logic for DigitalFormsDashboard.xaml
  82. /// </summary>
  83. public partial class DigitalFormsDashboard : UserControl,
  84. IDashboardWidget<Common, DigitalFormsDashboardProperties>,
  85. IRequiresSecurity<CanViewDigitalFormsDashbaord>,
  86. IHeaderDashboard, IActionsDashboard
  87. {
  88. public DigitalFormsDashboardProperties Properties { get; set; }
  89. private List<DigitalForm> DigitalForms;
  90. private List<Job> Jobs;
  91. private Dictionary<string, string> Categories;
  92. public DashboardHeader Header { get; set; } = new();
  93. private bool IsQAForm = false;
  94. private List<QAQuestion> Questions = new();
  95. public DigitalFormsDashboard()
  96. {
  97. InitializeComponent();
  98. }
  99. public void Setup()
  100. {
  101. var results = Client.QueryMultiple(
  102. new KeyedQueryDef<DigitalForm>(new Filter<DigitalForm>(x => x.Active).IsEqualTo(true)),
  103. new KeyedQueryDef<Job>(
  104. LookupFactory.DefineFilter<Job>(),
  105. new Columns<Job>(x => x.ID)
  106. .Add(x => x.JobNumber)
  107. .Add(x => x.Name)));
  108. DigitalForms = results.Get<DigitalForm>().ToList<DigitalForm>();
  109. var categories = new DigitalFormCategoryLookups(null);
  110. categories.OnAfterGenerateLookups += (sender, entries) => { entries.Insert(0, new LookupEntry("", "Select Category")); };
  111. Categories = categories.AsTable("AppliesTo")
  112. .ToDictionary("AppliesTo", "Display")
  113. .Cast<KeyValuePair<object, string>>()
  114. .ToDictionary(x => (x.Key as string)!, x => x.Value);
  115. Jobs = results.Get<Job>().ToList<Job>();
  116. Jobs.Insert(0, new Job { ID = Guid.Empty, JobNumber = "ALL", Name = "All Jobs" });
  117. SetupHeader();
  118. SetupFilters();
  119. }
  120. #region Header
  121. private ComboBox CategoryBox;
  122. private ComboBox FormBox;
  123. private ComboBox JobBox;
  124. private ComboBox DateTypeBox;
  125. private Label FromLabel;
  126. private DatePicker FromPicker;
  127. private Label ToLabel;
  128. private DatePicker ToPicker;
  129. private Button Print;
  130. private static Dictionary<DateFilterType, string> FilterTypes = new()
  131. {
  132. { DateFilterType.Today, "Today" },
  133. { DateFilterType.Yesterday, "Yesterday" },
  134. { DateFilterType.Week, "Week to Date" },
  135. { DateFilterType.SevenDays, "Last 7 Days" },
  136. { DateFilterType.Month, "Month to Date" },
  137. { DateFilterType.ThirtyDays, "Last 30 Days" },
  138. { DateFilterType.Year, "Year to Date" },
  139. { DateFilterType.TwelveMonths, "Last 12 Months" },
  140. { DateFilterType.Custom, "Custom" }
  141. };
  142. public void SetupHeader()
  143. {
  144. CategoryBox = new ComboBox {
  145. Width = 150,
  146. VerticalContentAlignment = VerticalAlignment.Center,
  147. Margin = new Thickness(0, 0, 5, 0)
  148. };
  149. CategoryBox.ItemsSource = Categories;
  150. CategoryBox.SelectedValuePath = "Key";
  151. CategoryBox.DisplayMemberPath = "Value";
  152. CategoryBox.SelectionChanged += Category_SelectionChanged;
  153. FormBox = new ComboBox
  154. {
  155. Width = 250,
  156. VerticalContentAlignment = VerticalAlignment.Center,
  157. Margin = new Thickness(0, 0, 5, 0),
  158. IsEnabled = false
  159. };
  160. FormBox.SelectionChanged += FormBox_SelectionChanged;
  161. FormBox.ItemsSource = new Dictionary<DigitalForm, string> { };
  162. JobBox = new ComboBox
  163. {
  164. Width = 250,
  165. Margin = new Thickness(0, 0, 5, 0),
  166. VerticalContentAlignment = VerticalAlignment.Center
  167. };
  168. JobBox.ItemsSource = Jobs.ToDictionary(x => x.ID, x => $"{x.JobNumber} : {x.Name}");
  169. JobBox.SelectedIndex = 0;
  170. JobBox.SelectedValuePath = "Key";
  171. JobBox.DisplayMemberPath = "Value";
  172. JobBox.SelectionChanged += JobBox_SelectionChanged;
  173. DateTypeBox = new ComboBox
  174. {
  175. Width = 120,
  176. VerticalContentAlignment = VerticalAlignment.Center
  177. };
  178. DateTypeBox.ItemsSource = FilterTypes;
  179. DateTypeBox.SelectedValuePath = "Key";
  180. DateTypeBox.DisplayMemberPath = "Value";
  181. DateTypeBox.SelectedValue = Properties.DateFilterType;
  182. DateTypeBox.SelectionChanged += DateTypeBox_SelectionChanged;
  183. FromLabel = new Label { Content = "From", VerticalContentAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 5, 0) };
  184. FromPicker = new DatePicker {
  185. Width = 100,
  186. Background = new SolidColorBrush(Colors.LightYellow),
  187. VerticalContentAlignment = VerticalAlignment.Center,
  188. FirstDayOfWeek = DayOfWeek.Monday,
  189. Margin = new Thickness(0, 0, 5, 0)
  190. };
  191. FromPicker.SelectedDateChanged += FromPicker_SelectedDateChanged;
  192. ToLabel = new Label { Content = "To", VerticalContentAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 5, 0) };
  193. ToPicker = new DatePicker
  194. {
  195. Width = 100,
  196. Background = new SolidColorBrush(Colors.LightYellow),
  197. VerticalContentAlignment = VerticalAlignment.Center,
  198. FirstDayOfWeek = DayOfWeek.Monday,
  199. Margin = new Thickness(0, 0, 5, 0)
  200. };
  201. ToPicker.SelectedDateChanged += ToPicker_SelectedDateChanged;
  202. Print = new Button
  203. {
  204. Width = 25,
  205. Height = 25,
  206. Content = new Image { Source = PRSDesktop.Resources.printer.AsBitmapImage() }
  207. };
  208. Print.Click += Print_Click;
  209. Header.BeginUpdate()
  210. .Clear()
  211. .Add(CategoryBox)
  212. .Add(FormBox)
  213. .Add(JobBox)
  214. .Add(DateTypeBox)
  215. .Add(FromLabel)
  216. .Add(FromPicker)
  217. .Add(ToLabel)
  218. .Add(ToPicker)
  219. .AddRight(Print);
  220. Header.EndUpdate();
  221. }
  222. private void Print_Click(object sender, RoutedEventArgs e)
  223. {
  224. var menu = new ContextMenu();
  225. foreach (var report in ReportUtils.LoadReports(SectionName, DataModel(Selection.None)))
  226. {
  227. menu.AddItem(report.Name, PRSDesktop.Resources.printer, report, PrintReport_Click);
  228. }
  229. if (Security.IsAllowed<CanDesignReports>())
  230. {
  231. menu.AddSeparatorIfNeeded();
  232. menu.AddItem("Manage Reports", PRSDesktop.Resources.printer, ManageReports_Click);
  233. }
  234. menu.IsOpen = true;
  235. }
  236. private void PrintReport_Click(ReportTemplate obj)
  237. {
  238. Selection selection;
  239. if (obj.SelectedRecords && obj.AllRecords)
  240. selection = RecordSelectionDialog.Execute();
  241. else if (obj.SelectedRecords)
  242. selection = Selection.Selected;
  243. else if (obj.AllRecords)
  244. selection = Selection.All;
  245. else
  246. selection = Selection.None;
  247. ReportUtils.PreviewReport(obj, DataModel(selection), false, Security.IsAllowed<CanDesignReports>());
  248. }
  249. private void ManageReports_Click()
  250. {
  251. var manager = new ReportManager()
  252. {
  253. DataModel = DataModel(Selection.None),
  254. Section = SectionName,
  255. Populate = true
  256. };
  257. manager.ShowDialog();
  258. }
  259. private void Search_KeyUp(object sender, KeyEventArgs e)
  260. {
  261. Refresh();
  262. }
  263. private void JobBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  264. {
  265. Properties.JobID = (Guid)JobBox.SelectedValue;
  266. Refresh();
  267. }
  268. private void SetDateFilterVisibility(bool visible)
  269. {
  270. var visibility = visible ? Visibility.Visible : Visibility.Collapsed;
  271. FromLabel.Visibility = visibility;
  272. FromPicker.Visibility = visibility;
  273. ToLabel.Visibility = visibility;
  274. ToPicker.Visibility = visibility;
  275. DateTypeBox.Visibility = visibility;
  276. }
  277. private void SetJobFilterVisibility(bool visible)
  278. {
  279. var visibility = visible ? Visibility.Visible : Visibility.Collapsed;
  280. JobBox.Visibility = visibility;
  281. }
  282. private void DateTypeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  283. {
  284. var filterType = (DateFilterType)DateTypeBox.SelectedValue;
  285. Properties.DateFilterType = filterType;
  286. if(filterType == DateFilterType.Custom)
  287. {
  288. if (FromPicker.SelectedDate == null || FromPicker.SelectedDate == DateTime.MinValue)
  289. {
  290. Properties.FromDate = DateTime.Today;
  291. }
  292. else
  293. {
  294. Properties.FromDate = FromPicker.SelectedDate.Value;
  295. }
  296. if (ToPicker.SelectedDate == null || ToPicker.SelectedDate == DateTime.MinValue)
  297. {
  298. Properties.ToDate = DateTime.Today;
  299. }
  300. else
  301. {
  302. Properties.ToDate = ToPicker.SelectedDate.Value;
  303. }
  304. }
  305. SetupDateFilters();
  306. Refresh();
  307. }
  308. private void FromPicker_SelectedDateChanged(object? sender, SelectionChangedEventArgs e)
  309. {
  310. Properties.FromDate = FromPicker.SelectedDate ?? DateTime.Today;
  311. }
  312. private void ToPicker_SelectedDateChanged(object? sender, SelectionChangedEventArgs e)
  313. {
  314. Properties.ToDate = ToPicker.SelectedDate ?? DateTime.Today;
  315. }
  316. private void Category_SelectionChanged(object sender, SelectionChangedEventArgs e)
  317. {
  318. SetCategory((CategoryBox.SelectedValue as string)!);
  319. var jobLink = FormType is not null ? GetJobLink("", FormType) : "";
  320. if (string.IsNullOrWhiteSpace(jobLink))
  321. {
  322. var jobID = Properties.JobID;
  323. JobBox.SelectedValue = Guid.Empty;
  324. JobBox.IsEnabled = false;
  325. Properties.JobID = jobID;
  326. }
  327. else
  328. {
  329. JobBox.SelectedValue = Properties.JobID;
  330. JobBox.IsEnabled = true;
  331. }
  332. if (ParentType is null)
  333. {
  334. FormBox.IsEnabled = false;
  335. FormBox.ItemsSource = new Dictionary<DigitalForm, string> { };
  336. }
  337. else
  338. {
  339. var forms = DigitalForms.Where(x => x.AppliesTo == ParentType.Name).ToList();
  340. forms.Insert(0, new DigitalForm { ID = Guid.Empty, Description = "Select Form" });
  341. FormBox.ItemsSource = forms;
  342. FormBox.DisplayMemberPath = "Description";
  343. FormBox.SelectedIndex = 0;
  344. FormBox.IsEnabled = true;
  345. }
  346. Refresh();
  347. }
  348. private void FormBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  349. {
  350. Form = (FormBox.SelectedValue as DigitalForm)!;
  351. Refresh();
  352. }
  353. #endregion
  354. private string SectionName
  355. {
  356. get
  357. {
  358. if (Form is null || Form.ID == Guid.Empty)
  359. return "Digital Forms";
  360. return Form.ID.ToString() ?? "Digital Forms";
  361. }
  362. }
  363. private DataModel DataModel(Selection selection)
  364. {
  365. if(FormType is null || Form is null || Form.ID == Guid.Empty)
  366. {
  367. return new AutoDataModel<DigitalForm>(new Filter<DigitalForm>().None());
  368. }
  369. IFilter filter;
  370. switch (selection)
  371. {
  372. case Selection.Selected:
  373. var formids = DataGrid.SelectedItems.Select(x => (x as DataRowView)!.Row["ID"]).ToArray();
  374. filter = Filter.Create<Entity>(FormType, x => x.ID).InList(formids);
  375. break;
  376. case Selection.All:
  377. filter = Filter.Create(FormType).All();
  378. break;
  379. case Selection.None:
  380. default:
  381. filter = Filter.Create(FormType).None();
  382. break;
  383. }
  384. return (Activator.CreateInstance(typeof(DigitalFormReportDataModel<>)!
  385. .MakeGenericType(FormType), new object?[] { filter, Form.ID }) as DataModel)!;
  386. }
  387. public void BuildActionsMenu(ContextMenu menu)
  388. {
  389. menu.AddCheckItem<object?>("Show Date Filter", null, ToggleDateFilter, Properties.ShowDateFilter);
  390. menu.AddCheckItem<object?>("Show Job Filter", null, ToggleJobFilter, Properties.ShowJobFilter);
  391. menu.AddSeparator();
  392. if (ParentType is not null)
  393. {
  394. menu.AddSeparatorIfNeeded();
  395. if (Properties.Filters.TryGetValue(ParentType.Name, out var filters))
  396. {
  397. var i = 0;
  398. var items = new List<MenuItem>();
  399. foreach (var filter in filters)
  400. {
  401. items.Add(menu.AddCheckItem(
  402. filter.Name,
  403. new Tuple<int, string, List<MenuItem>>(i, filter.Filter, items),
  404. ExecuteFilter,
  405. i == CustomFilterIndex));
  406. ++i;
  407. }
  408. }
  409. menu.AddSeparatorIfNeeded();
  410. menu.AddItem("Manage Filters", InABox.DynamicGrid.Properties.Resources.filter, ManageFilters_Click);
  411. }
  412. menu.AddSeparatorIfNeeded();
  413. menu.AddItem("Export", InABox.DynamicGrid.Properties.Resources.doc_xls, Export_Click, Form is not null && Form.ID != Guid.Empty);
  414. foreach (var (module, image) in CustomModuleUtils.LoadCustomModuleThumbnails(SectionName, DataModel(Selection.None)))
  415. {
  416. menu.AddItem(module.Name, image, module, ExecuteModule_Click);
  417. }
  418. if (Security.IsAllowed<CanCustomiseModules>())
  419. {
  420. menu.AddSeparatorIfNeeded();
  421. menu.AddItem("Manage Modules", PRSDesktop.Resources.script, ManageModules_Click);
  422. }
  423. }
  424. private void Export_Click()
  425. {
  426. var formName = Regex.Replace(Form?.Description ?? "", "[^ a-zA-Z0-9]", string.Empty);
  427. var filename = string.Format("{0} - {1} - {2:yyyy-MM-dd} - {3:yyyy-MM-dd}.xlsx", ParentType!.Name, formName, From, To);
  428. var options = new ExcelExportingOptions();
  429. options.ExcelVersion = ExcelVersion.Excel2013;
  430. options.ExportStackedHeaders = true;
  431. var excelEngine = DataGrid.ExportToExcel(DataGrid.View, options);
  432. var workBook = excelEngine.Excel.Workbooks[0];
  433. var sheet = workBook.Worksheets[0];
  434. sheet.Name = "Summary";
  435. sheet.UsedRange.AutofitRows();
  436. sheet.UsedRange.AutofitColumns();
  437. sheet = workBook.Worksheets.Create("Questions");
  438. sheet.Move(0);
  439. var questions = new Client<QAQuestion>().Query(new Filter<QAQuestion>(x => x.Form.ID).IsEqualTo(Form!.ID));
  440. sheet.Range[1, 1].Text = Form?.Description ?? "";
  441. sheet.Range[1, 1, 1, 3].Merge();
  442. var i = 1;
  443. foreach (var row in questions.Rows)
  444. if (!row.Get<QAQuestion, QAAnswer>(x => x.Answer).Equals(QAAnswer.Comment))
  445. {
  446. sheet.Range[i + 2, 1].Text = string.Format("{0}.", i);
  447. sheet.Range[i + 2, 2].Text = string.Format("{0}", row.Get<QAQuestion, string>(x => x.Question));
  448. sheet.Range[i + 2, 3].Text = string.Format("[{0}]", row.Get<QAQuestion, string>(x => x.Code));
  449. i++;
  450. }
  451. sheet.UsedRange.AutofitRows();
  452. sheet.UsedRange.AutofitColumns();
  453. try
  454. {
  455. workBook.SaveAs(filename);
  456. var startInfo = new ProcessStartInfo(filename);
  457. startInfo.Verb = "open";
  458. startInfo.UseShellExecute = true;
  459. Process.Start(startInfo);
  460. }
  461. catch
  462. {
  463. MessageBox.Show(string.Format("Unable to Save/Launch [{0}]!\n\nIs the file already open?", filename));
  464. }
  465. }
  466. private void ManageFilters_Click()
  467. {
  468. var filters = Properties.Filters.GetValueOrDefault(ParentType!.Name) ?? new List<DFFilter>();
  469. var gridFilters = new DynamicGridFilters();
  470. gridFilters.AddRange(filters.Select(x => new DynamicGridFilter { Name = x.Name, Filter = x.Filter }));
  471. var grid = new DynamicGridFilterEditor(gridFilters, FormType!);
  472. if (grid.ShowDialog() == true)
  473. {
  474. Properties.Filters[ParentType!.Name] = grid.Filters.Select(x => new DFFilter { Name = x.Name, Filter = x.Filter }).ToList();
  475. if (CustomFilterIndex != null)
  476. {
  477. Refresh();
  478. }
  479. }
  480. }
  481. private void ExecuteFilter(Tuple<int, string, List<MenuItem>> tag, bool isChecked)
  482. {
  483. var (index, filter, items) = tag;
  484. if (isChecked)
  485. {
  486. var i = 0;
  487. foreach (var item in items)
  488. {
  489. item.IsChecked = i == index;
  490. ++i;
  491. }
  492. }
  493. if (isChecked)
  494. {
  495. CustomFilter = Serialization.Deserialize(typeof(Filter<>).MakeGenericType(FormType!), filter) as IFilter;
  496. CustomFilterIndex = index;
  497. Refresh();
  498. }
  499. else if (index == CustomFilterIndex)
  500. {
  501. CustomFilter = null;
  502. CustomFilterIndex = null;
  503. Refresh();
  504. }
  505. }
  506. private void ExecuteModule_Click(CustomModule obj)
  507. {
  508. if (!string.IsNullOrWhiteSpace(obj.Script))
  509. try
  510. {
  511. Selection selection;
  512. if (obj.SelectedRecords && obj.AllRecords)
  513. selection = RecordSelectionDialog.Execute();
  514. else if (obj.SelectedRecords)
  515. selection = Selection.Selected;
  516. else if (obj.AllRecords)
  517. selection = Selection.All;
  518. else
  519. selection = Selection.None;
  520. var result = ScriptDocument.RunCustomModule(DataModel(selection), new Dictionary<string, object[]>(), obj.Script);
  521. if (result)
  522. Refresh();
  523. }
  524. catch (CompileException c)
  525. {
  526. MessageBox.Show(c.Message);
  527. }
  528. catch (Exception err)
  529. {
  530. MessageBox.Show(CoreUtils.FormatException(err));
  531. }
  532. else
  533. MessageBox.Show("Unable to load " + obj.Name);
  534. }
  535. private void ManageModules_Click()
  536. {
  537. var section = SectionName;
  538. var dataModel = DataModel(Selection.Selected);
  539. var manager = new CustomModuleManager()
  540. {
  541. Section = section,
  542. DataModel = dataModel
  543. };
  544. manager.ShowDialog();
  545. }
  546. private void ToggleDateFilter(object? tag, bool isChecked)
  547. {
  548. Properties.ShowDateFilter = isChecked;
  549. SetDateFilterVisibility(Properties.ShowDateFilter);
  550. }
  551. private void ToggleJobFilter(object? tag, bool isChecked)
  552. {
  553. Properties.ShowJobFilter = isChecked;
  554. SetJobFilterVisibility(Properties.ShowJobFilter);
  555. Refresh();
  556. }
  557. #region Filtering
  558. private DateTime From { get; set; }
  559. private DateTime To { get; set; }
  560. private bool IsEntityForm { get; set; }
  561. private Type? ParentType { get; set; }
  562. private Type? FormType { get; set; }
  563. private DigitalForm? Form { get; set; }
  564. private IFilter? CustomFilter { get; set; }
  565. private int? CustomFilterIndex { get; set; }
  566. private readonly Dictionary<string, string> QuestionCodes = new();
  567. private static int WeekDay(DateTime date)
  568. {
  569. if (date.DayOfWeek == DayOfWeek.Sunday)
  570. return 7;
  571. return (int)date.DayOfWeek - 1;
  572. }
  573. private void SetupDateFilters()
  574. {
  575. switch (Properties.DateFilterType)
  576. {
  577. case DateFilterType.Today:
  578. From = DateTime.Today;
  579. To = DateTime.Today;
  580. break;
  581. case DateFilterType.Yesterday:
  582. From = DateTime.Today.AddDays(-1);
  583. To = DateTime.Today.AddDays(-1);
  584. break;
  585. case DateFilterType.Week:
  586. From = DateTime.Today.AddDays(-WeekDay(DateTime.Today));
  587. To = DateTime.Today;
  588. break;
  589. case DateFilterType.SevenDays:
  590. From = DateTime.Today.AddDays(-6);
  591. To = DateTime.Today;
  592. break;
  593. case DateFilterType.Month:
  594. From = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
  595. To = DateTime.Today;
  596. break;
  597. case DateFilterType.ThirtyDays:
  598. From = DateTime.Today.AddDays(-29);
  599. To = DateTime.Today;
  600. break;
  601. case DateFilterType.Year:
  602. From = new DateTime(DateTime.Today.Year, 1, 1);
  603. To = DateTime.Today;
  604. break;
  605. case DateFilterType.TwelveMonths:
  606. From = DateTime.Today.AddYears(-1).AddDays(1);
  607. To = DateTime.Today;
  608. break;
  609. case DateFilterType.Custom:
  610. From = Properties.FromDate;
  611. To = Properties.ToDate;
  612. break;
  613. }
  614. DateTypeBox.SelectedValue = Properties.DateFilterType;
  615. FromPicker.SelectedDate = From;
  616. ToPicker.SelectedDate = To;
  617. var enabledPicker = Properties.DateFilterType == DateFilterType.Custom;
  618. FromPicker.IsEnabled = enabledPicker;
  619. ToPicker.IsEnabled = enabledPicker;
  620. }
  621. private void SetupJobFilter()
  622. {
  623. JobBox.SelectedValue = Properties.JobID;
  624. }
  625. private void SetupFilters()
  626. {
  627. SetupDateFilters();
  628. SetupJobFilter();
  629. SetDateFilterVisibility(Properties.ShowDateFilter);
  630. SetJobFilterVisibility(Properties.ShowJobFilter);
  631. }
  632. #region Categories
  633. private static Dictionary<string, Tuple<Type, Type>>? FormInstanceTypes;
  634. private static readonly Dictionary<Type, List<Tuple<string, string>>> parentColumns = new()
  635. {
  636. { typeof(Kanban), new() { new("Parent.Number", "Task No") } },
  637. { typeof(Job), new() { new("Parent.JobNumber", "Job No") } },
  638. { typeof(JobITP), new() { new("Parent.Code", "Code") } },
  639. { typeof(Assignment), new() { new("Parent.Number", "Ass. No") } },
  640. { typeof(TimeSheet), new() { } },
  641. { typeof(LeaveRequest), new() { } },
  642. { typeof(Employee), new() { new("Parent.Code", "Employee") } },
  643. { typeof(PurchaseOrderItem), new() { new("Parent.PONumber", "PO No") } },
  644. };
  645. private static bool CategoryToType(string category, [NotNullWhen(true)] out Type? formType, [NotNullWhen(true)] out Type? parentType)
  646. {
  647. FormInstanceTypes ??= CoreUtils.TypeList(
  648. AppDomain.CurrentDomain.GetAssemblies(),
  649. x => !x.IsAbstract && x.GetInterfaces().Contains(typeof(IDigitalFormInstance))
  650. ).Select(x =>
  651. {
  652. var inter = x.GetInterfaces()
  653. .Where(x => x.IsGenericType && x.GetGenericTypeDefinition().Equals(typeof(IDigitalFormInstance<>))).FirstOrDefault();
  654. if (inter is not null)
  655. {
  656. var link = inter.GenericTypeArguments[0];
  657. var entityLinkDef = link.GetSuperclassDefinition(typeof(EntityLink<>));
  658. if (entityLinkDef is not null)
  659. {
  660. var entityType = entityLinkDef.GenericTypeArguments[0];
  661. return new Tuple<string, Type, Type>(entityType.Name, x, entityType);
  662. }
  663. }
  664. return null;
  665. }).Where(x => x is not null).ToDictionary(x => x!.Item1, x => new Tuple<Type, Type>(x!.Item2, x!.Item3));
  666. if (!FormInstanceTypes.TryGetValue(category, out var result))
  667. {
  668. formType = null;
  669. parentType = null;
  670. return false;
  671. }
  672. formType = result.Item1;
  673. parentType = result.Item2;
  674. return true;
  675. }
  676. private void SetCategory(string category)
  677. {
  678. CustomFilter = null;
  679. CustomFilterIndex = null;
  680. if (!CategoryToType(category, out var formType, out var parentType))
  681. {
  682. IsEntityForm = false;
  683. ParentType = null;
  684. FormType = null;
  685. return;
  686. }
  687. IsEntityForm = formType.IsSubclassOfRawGeneric(typeof(EntityForm<,>));
  688. ParentType = parentType;
  689. FormType = formType;
  690. }
  691. #endregion
  692. private string GetJobLink(string prefix, Type type)
  693. {
  694. var props = type.GetProperties().Where(x =>
  695. x.PropertyType.BaseType != null && x.PropertyType.BaseType.IsGenericType &&
  696. x.PropertyType.BaseType.GetGenericTypeDefinition() == typeof(EntityLink<>));
  697. foreach (var prop in props)
  698. {
  699. if (prop.PropertyType == typeof(JobLink))
  700. return (string.IsNullOrEmpty(prefix) ? "" : prefix + ".") + prop.Name;
  701. var result = GetJobLink((string.IsNullOrEmpty(prefix) ? "" : prefix + ".") + prop.Name, prop.PropertyType);
  702. if (!string.IsNullOrEmpty(result))
  703. return result;
  704. }
  705. return "";
  706. }
  707. /// <summary>
  708. /// Find a link from the form type to an associated <see cref="Job"/>, allowing us to filter based on jobs.
  709. /// </summary>
  710. /// <returns>The property name of the <see cref="JobLink"/>.</returns>
  711. private string GetJobLink<T>() where T : IDigitalFormInstance
  712. => GetJobLink("", typeof(T));
  713. private IKeyedQueryDef GetFormQuery<T>()
  714. where T : Entity, IRemotable, IPersistent, IDigitalFormInstance, new()
  715. {
  716. var sort = LookupFactory.DefineSort<T>();
  717. var jobLink = GetJobLink<T>();
  718. var filter = new Filter<T>(x => x.FormCompleted).IsGreaterThanOrEqualTo(From)
  719. .And(x => x.FormCompleted).IsLessThan(To.AddDays(1))
  720. .And(x => x.Form.ID).IsEqualTo(Form!.ID);
  721. if (Properties.JobID != Guid.Empty && Properties.ShowJobFilter)
  722. {
  723. filter.And(jobLink + ".ID").IsEqualTo(Properties.JobID);
  724. }
  725. if (CustomFilter is not null)
  726. {
  727. filter.And(CustomFilter);
  728. }
  729. var columns = new Columns<T>(x => x.ID)
  730. .Add(x => x.Form.ID)
  731. .Add(x => x.FormData)
  732. .Add(x => x.FormCompleted)
  733. .Add(x => x.FormCompletedBy.UserID)
  734. .Add(x => x.Location.Timestamp)
  735. .Add(x => x.Location.Latitude)
  736. .Add(x => x.Location.Longitude);
  737. var parentcols = LookupFactory.DefineColumns(ParentType!);
  738. foreach (var col in parentcols.ColumnNames())
  739. columns.Add("Parent." + col);
  740. if (parentColumns.TryGetValue(ParentType!, out var pColumns))
  741. {
  742. foreach (var (field, name) in pColumns)
  743. {
  744. columns.Add(field);
  745. }
  746. }
  747. if (IsEntityForm)
  748. columns.Add("Processed");
  749. if (!string.IsNullOrWhiteSpace(jobLink))
  750. columns.Add(jobLink + ".ID");
  751. return new KeyedQueryDef<T>(filter, columns, sort);
  752. }
  753. #endregion
  754. private void LoadDataIntoGrid(List<DigitalFormVariable> variables, List<QAQuestion> questions, CoreTable formData, List<string> additionalColumns, CoreTable? jobITPs)
  755. {
  756. var data = new DataTable();
  757. data.Columns.Add("ID", typeof(Guid));
  758. data.Columns.Add("Form_ID", typeof(Guid));
  759. data.Columns.Add("Parent_ID", typeof(Guid));
  760. data.Columns.Add("Location_Timestamp", typeof(DateTime));
  761. data.Columns.Add("Location_Latitude", typeof(double));
  762. data.Columns.Add("Location_Longitude", typeof(double));
  763. data.Columns.Add("FormData", typeof(string));
  764. if (ParentType == typeof(JobITP))
  765. {
  766. data.Columns.Add("Job No", typeof(string));
  767. }
  768. if (parentColumns.TryGetValue(ParentType!, out var pColumns))
  769. {
  770. foreach (var (field, name) in pColumns)
  771. {
  772. data.Columns.Add(name, typeof(string));
  773. }
  774. }
  775. data.Columns.Add("Description", typeof(string));
  776. data.Columns.Add("Completed", typeof(DateTime));
  777. data.Columns.Add("Completed By", typeof(string));
  778. if (IsEntityForm)
  779. data.Columns.Add("Processed", typeof(bool));
  780. if (variables.Any())
  781. {
  782. foreach (var variable in variables)
  783. {
  784. var code = variable.Code.Replace("/", " ");
  785. QuestionCodes[code] = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(code.ToLower());
  786. data.Columns.Add(code, typeof(string));
  787. }
  788. }
  789. else if (questions.Any())
  790. {
  791. Questions = questions;
  792. Progress.SetMessage("Loading Checks");
  793. QAGrid.Clear();
  794. QAGrid.LoadChecks(Form!.Description, Questions, new Dictionary<Guid, object>());
  795. QAGrid.CollapseMargins();
  796. var i = 1;
  797. foreach (var question in Questions)
  798. {
  799. var id = question.ID.ToString();
  800. if (!question.Answer.Equals(QAAnswer.Comment))
  801. {
  802. data.Columns.Add(id, typeof(string));
  803. var code = question.Code;
  804. QuestionCodes[id] = string.IsNullOrEmpty(code) ? string.Format("{0}.", i) : code;
  805. i++;
  806. }
  807. }
  808. }
  809. foreach (var row in formData.Rows)
  810. {
  811. var form = (row.ToObject(FormType!) as IDigitalFormInstance)!;
  812. if (!string.IsNullOrWhiteSpace(form.FormData))
  813. {
  814. var dataRow = data.NewRow();
  815. dataRow["ID"] = form.ID;
  816. dataRow["Form_ID"] = form.Form.ID;
  817. dataRow["Parent_ID"] = form.ParentID();
  818. dataRow["Location_Timestamp"] = form.Location.Timestamp;
  819. dataRow["Location_Latitude"] = form.Location.Latitude;
  820. dataRow["Location_Longitude"] = form.Location.Longitude;
  821. dataRow["FormData"] = form.FormData;
  822. var desc = new List<string>();
  823. foreach (var col in additionalColumns)
  824. {
  825. var val = row[col];
  826. if (val != null && val is not Guid)
  827. desc.Add(val.ToString() ?? "");
  828. }
  829. dataRow["Description"] = string.Join(" : ", desc);
  830. dataRow["Completed"] = form.FormCompleted;
  831. dataRow["Completed By"] = form.FormCompletedBy.UserID;
  832. if (IsEntityForm)
  833. dataRow["Processed"] = (bool?)row["Processed"] ?? false;
  834. if (ParentType == typeof(JobITP))
  835. {
  836. var jobITP = jobITPs!.Rows.FirstOrDefault(x => x.Get<JobITP, Guid>(x => x.ID) == form.ParentID());
  837. if(jobITP is not null)
  838. {
  839. var jobID = jobITP.Get<JobITP, Guid>(x => x.Job.ID);
  840. dataRow["Job No"] = Jobs.Where(x => x.ID == jobID).FirstOrDefault()?.JobNumber;
  841. }
  842. }
  843. if (pColumns != null)
  844. {
  845. foreach (var (field, name) in pColumns)
  846. {
  847. dataRow[name] = row[field]?.ToString();
  848. }
  849. }
  850. //datarow["Job No"] = (String)row[JobLink + ".JobNumber"];
  851. var bHasData = false;
  852. if (variables.Any())
  853. {
  854. var dict = Serialization.Deserialize<Dictionary<string, object>>(form.FormData);
  855. foreach (var key in dict.Keys)
  856. {
  857. var variable = variables.FirstOrDefault(x => string.Equals(key, x.Code));
  858. var type = variable?.FieldType();
  859. if (variable != null)
  860. {
  861. var value = variable.ParseValue(dict[key]);
  862. var format = variable.FormatValue(value);
  863. var sKey = key.Replace("/", " ");
  864. if (data.Columns.Contains(sKey))
  865. {
  866. dataRow[sKey] = format;
  867. bHasData = true;
  868. }
  869. }
  870. }
  871. }
  872. else
  873. {
  874. var dict = Serialization.Deserialize<Dictionary<Guid, object>>(form.FormData);
  875. foreach (var key in dict.Keys)
  876. if (data.Columns.Contains(key.ToString()))
  877. {
  878. dataRow[key.ToString()] = dict[key];
  879. bHasData = true;
  880. }
  881. }
  882. if (bHasData)
  883. data.Rows.Add(dataRow);
  884. }
  885. }
  886. DataGrid.ItemsSource = data;
  887. IsQAForm = !variables.Any() && questions.Any();
  888. QAGrid.Visibility = IsQAForm ? Visibility.Visible : Visibility.Collapsed;
  889. DataGrid.Visibility = Visibility.Visible;
  890. }
  891. private void RefreshData<TForm>()
  892. where TForm : Entity, IRemotable, IPersistent, IDigitalFormInstance, new()
  893. {
  894. var formQuery = GetFormQuery<TForm>();
  895. var queries = new List<IKeyedQueryDef>()
  896. {
  897. new KeyedQueryDef<QAQuestion>(new Filter<QAQuestion>(x => x.Form.ID).IsEqualTo(Form!.ID)),
  898. new KeyedQueryDef<DigitalFormVariable>(
  899. new Filter<DigitalFormVariable>(x => x.Form.ID).IsEqualTo(Form.ID),
  900. null,
  901. new SortOrder<DigitalFormVariable>(x => x.Sequence)),
  902. formQuery
  903. };
  904. if (ParentType == typeof(JobITPForm))
  905. {
  906. queries.Add(new KeyedQueryDef<JobITP>(
  907. new Filter<JobITP>(x => x.ID).InQuery((formQuery.Filter as Filter<JobITPForm>)!, x => x.Parent.ID),
  908. new Columns<JobITP>(x => x.ID, x => x.Job.JobNumber)));
  909. }
  910. var results = Client.QueryMultiple(queries);
  911. var questions = results.Get<QAQuestion>().ToList<QAQuestion>();
  912. var variables = results.Get<DigitalFormVariable>().ToList<DigitalFormVariable>();
  913. var formData = results.Get(formQuery.Key);
  914. LoadDataIntoGrid(
  915. variables, questions,
  916. formData,
  917. formQuery.Columns!.ColumnNames().Where(x => x != "ID" && x.StartsWith("Parent.")).ToList(),
  918. ParentType == typeof(JobITP) ? results.Get<JobITP>() : null);
  919. }
  920. public void Refresh()
  921. {
  922. Progress.Show("Refreshing");
  923. try
  924. {
  925. Questions.Clear();
  926. QAGrid.Clear();
  927. QAGrid.LoadChecks("", Array.Empty<QAQuestion>(), new Dictionary<Guid, object>());
  928. DataGrid.ItemsSource = null;
  929. if (ParentType is null || FormType is null || Form is null || Form.ID == Guid.Empty)
  930. {
  931. QAGrid.Visibility = Visibility.Collapsed;
  932. DataGrid.Visibility = Visibility.Collapsed;
  933. return;
  934. }
  935. Progress.SetMessage("Loading Data");
  936. var refreshMethod = typeof(DigitalFormsDashboard).GetMethod(nameof(RefreshData), BindingFlags.Instance | BindingFlags.NonPublic)!.MakeGenericMethod(FormType);
  937. refreshMethod.Invoke(this, Array.Empty<object?>());
  938. }
  939. finally
  940. {
  941. Progress.Close();
  942. }
  943. }
  944. public void Shutdown()
  945. {
  946. }
  947. #region DataGrid Configuration
  948. private void DataGrid_AutoGeneratingColumn(object sender, Syncfusion.UI.Xaml.Grid.AutoGeneratingColumnArgs e)
  949. {
  950. e.Column.TextAlignment = TextAlignment.Center;
  951. e.Column.HorizontalHeaderContentAlignment = HorizontalAlignment.Center;
  952. e.Column.ColumnSizer = GridLengthUnitType.None;
  953. var value = (e.Column.ValueBinding as Binding)!;
  954. if (value.Path.Path.Equals("ID") || value.Path.Path.Equals("Form_ID") || value.Path.Path.Equals("Parent_ID") ||
  955. value.Path.Path.Equals("FormData") || value.Path.Path.Equals("Location_Latitude") || value.Path.Path.Equals("Location_Longitude"))
  956. {
  957. e.Cancel = true;
  958. }
  959. else if (value.Path.Path.Equals("Location_Timestamp"))
  960. {
  961. e.Column = new GridImageColumn();
  962. e.Column.Width = DataGrid.RowHeight;
  963. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  964. e.Column.HeaderText = "";
  965. e.Column.Padding = new Thickness(4);
  966. e.Column.ValueBinding = new Binding
  967. {
  968. Path = new PropertyPath(value.Path.Path),
  969. Converter = new MileStoneImageConverter()
  970. };
  971. e.Column.MappingName = "Location.Timestamp";
  972. }
  973. else if (ParentType is not null && parentColumns.TryGetValue(ParentType, out var pColumns) && pColumns.Any(x => x.Item2.Equals(value.Path.Path)))
  974. {
  975. e.Column.ColumnSizer = GridLengthUnitType.Auto;
  976. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  977. }
  978. else if (value.Path.Path.Equals("Job No"))
  979. {
  980. e.Column.Width = 60;
  981. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  982. }
  983. else if (value.Path.Path.Equals("Description"))
  984. {
  985. e.Column.TextAlignment = TextAlignment.Left;
  986. e.Column.HorizontalHeaderContentAlignment = HorizontalAlignment.Left;
  987. e.Column.Width = 450;
  988. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  989. }
  990. else if (value.Path.Path.Equals("Completed"))
  991. {
  992. e.Column.Width = 100;
  993. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  994. (e.Column as GridDateTimeColumn)!.Pattern = DateTimePattern.CustomPattern;
  995. (e.Column as GridDateTimeColumn)!.CustomPattern = "dd MMM yy hh:mm";
  996. }
  997. else if (value.Path.Path.Equals("Completed By"))
  998. {
  999. e.Column.Width = 100;
  1000. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  1001. }
  1002. else if (value.Path.Path.Equals("Processed"))
  1003. {
  1004. e.Column.Width = 100;
  1005. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  1006. }
  1007. else
  1008. {
  1009. var data = DataGrid.ItemsSource as DataTable;
  1010. //int index = data.Columns.IndexOf(e.Column.MappingName) - 2;
  1011. //Style style = new Style(typeof(GridCell));
  1012. //e.Column.CellStyle = style;
  1013. e.Column.Width = 100;
  1014. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  1015. e.Column.HeaderText = QuestionCodes[e.Column.MappingName];
  1016. }
  1017. }
  1018. private Entity? GetEntityForm<T>(Guid id) where T : Entity, IDigitalFormInstance, IRemotable, IPersistent, new()
  1019. {
  1020. var columns = new Columns<T>(x => x.ID)
  1021. .Add(x => x.FormCompleted)
  1022. .Add(x => x.FormData)
  1023. .Add(x => x.Form.ID)
  1024. .Add(x => x.Form.Description);
  1025. if (typeof(T).HasInterface(typeof(IDigitalFormInstance<>)))
  1026. {
  1027. columns.Add("Parent.ID");
  1028. }
  1029. return new Client<T>().Query(
  1030. new Filter<T>(x => x.ID).IsEqualTo(id),
  1031. columns).Rows.FirstOrDefault()?.ToObject<T>();
  1032. }
  1033. private void DataGrid_CellDoubleTapped(object sender, Syncfusion.UI.Xaml.Grid.GridCellDoubleTappedEventArgs e)
  1034. {
  1035. if (e.RowColumnIndex.RowIndex == 0)
  1036. return;
  1037. var table = (DataGrid.ItemsSource as DataTable)!;
  1038. var formid = (Guid)table.Rows[e.RowColumnIndex.RowIndex - 1]["Form_ID"];
  1039. var formdata = (string)table.Rows[e.RowColumnIndex.RowIndex - 1]["FormData"];
  1040. var id = (Guid)table.Rows[e.RowColumnIndex.RowIndex - 1]["ID"];
  1041. if (FormType is null) return;
  1042. if (IsQAForm)
  1043. {
  1044. var formData = Serialization.Deserialize<Dictionary<string, object>>(formdata);
  1045. var values = new Dictionary<Guid, object>();
  1046. foreach(var (idStr, value) in formData)
  1047. {
  1048. if(Guid.TryParse(idStr, out var codeID))
  1049. {
  1050. values[codeID] = value;
  1051. }
  1052. }
  1053. QAGrid.Clear();
  1054. QAGrid.LoadChecks(Form!.Description, Questions, values);
  1055. QAGrid.CollapseMargins();
  1056. return;
  1057. }
  1058. var entityForm = typeof(DigitalFormsDashboard)
  1059. .GetMethod(nameof(GetEntityForm), BindingFlags.NonPublic | BindingFlags.Instance)!
  1060. .MakeGenericMethod(FormType)
  1061. .Invoke(this, new object[] { id }) as IDigitalFormInstance;
  1062. if (entityForm is not null)
  1063. {
  1064. if (DynamicFormEditWindow.EditDigitalForm(entityForm, out var dataModel))
  1065. {
  1066. dataModel.Update(null);
  1067. /*typeof(QADashboard)
  1068. .GetMethod(nameof(SaveEntityForm), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
  1069. .MakeGenericMethod(formType)
  1070. .Invoke(this, new object[] { entityForm });*/
  1071. Refresh();
  1072. }
  1073. }
  1074. }
  1075. private void DataGrid_CellTapped(object sender, Syncfusion.UI.Xaml.Grid.GridCellTappedEventArgs e)
  1076. {
  1077. if (e.RowColumnIndex.ColumnIndex == 0)
  1078. {
  1079. var timestamp = (DateTime)(e.Record as DataRowView)!.Row["Location_Timestamp"];
  1080. var latitude = (double)(e.Record as DataRowView)!.Row["Location_Latitude"];
  1081. var longitude = (double)(e.Record as DataRowView)!.Row["Location_Longitude"];
  1082. var form = new MapForm(latitude, longitude, timestamp);
  1083. form.ShowDialog();
  1084. }
  1085. }
  1086. #endregion
  1087. }
  1088. }