DigitalFormsDock.xaml.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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 System.Windows.Threading;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. using InABox.Reports;
  14. using InABox.Core.Reports;
  15. using InABox.Scripting;
  16. using InABox.Wpf.Reports;
  17. using InABox.WPF;
  18. using PRSDesktop.Configuration;
  19. using System.Diagnostics;
  20. using System.Threading.Tasks;
  21. namespace PRSDesktop
  22. {
  23. public class DigitalFormDockModel : BaseObject
  24. {
  25. [NullEditor]
  26. public Guid ID { get; set; }
  27. [NullEditor]
  28. public Guid FormID { get; set; }
  29. [CodeEditor(Visible = Visible.Default, Width=80)]
  30. [EditorSequence(1)]
  31. public string Number { get; set; }
  32. [TextBoxEditor(Visible = Visible.Default)]
  33. [EditorSequence(2)]
  34. public string FormName { get; set; }
  35. [DateTimeEditor(Visible=Visible.Default, Width=100, Format = "dd MMM yy HH:mm")]
  36. [EditorSequence(3)]
  37. public DateTime Completed { get; set; }
  38. [TextBoxEditor(Visible=Visible.Default, Width=80, Alignment = Alignment.MiddleCenter)]
  39. [EditorSequence(4)]
  40. public string CompletedBy { get; set; }
  41. [NullEditor]
  42. public Type FormType { get; set; }
  43. [NullEditor]
  44. public DateTime Processed { get; set; }
  45. [NullEditor]
  46. public Guid ParentID { get; set; }
  47. }
  48. public class DigitalFormDockGrid : DynamicGrid<DigitalFormDockModel>
  49. {
  50. private CoreFieldMap<IDigitalFormInstance, DigitalFormDockModel> _mappings;
  51. private IEnumerable<Type> _types;
  52. private MultiQuery _query;
  53. private CoreTable _data;
  54. public Dictionary<Type, System.Drawing.Bitmap> Images { get; private set; }
  55. public List<Type> ExcludedTypes { get; private set; }
  56. public DateTime StartDate { get; set; }
  57. public DigitalFormDockGrid() : base()
  58. {
  59. StartDate = DateTime.Today;
  60. ExcludedTypes = new List<Type>();
  61. Images = new Dictionary<Type, System.Drawing.Bitmap>()
  62. {
  63. { typeof(AssignmentForm), PRSDesktop.Resources.assignments },
  64. { typeof(KanbanForm), PRSDesktop.Resources.kanban },
  65. { typeof(JobForm), PRSDesktop.Resources.project },
  66. { typeof(JobITPForm), PRSDesktop.Resources.checklist },
  67. { typeof(EmployeeForm), PRSDesktop.Resources.employees },
  68. { typeof(LeaveRequestForm), PRSDesktop.Resources.leave },
  69. { typeof(ManufacturingPacketStage), PRSDesktop.Resources.factory },
  70. { typeof(TimeSheetForm), PRSDesktop.Resources.time },
  71. { typeof(PurchaseOrderItemForm), PRSDesktop.Resources.purchase }
  72. };
  73. ActionColumns.Add(new DynamicImageColumn(TypeImage)
  74. {
  75. Position = DynamicActionColumnPosition.Start,
  76. Filters = Images.Keys.Select(x=>x.EntityName().Split('.').Last().SplitCamelCase()).ToArray(),
  77. FilterRecord = TypeFilter
  78. });
  79. ActionColumns.Add(new DynamicMenuColumn(MenuBuild, MenuStatus) { Position = DynamicActionColumnPosition.End });
  80. _mappings = new CoreFieldMap<IDigitalFormInstance, DigitalFormDockModel>()
  81. .Add(x => x.ID, x => x.ID)
  82. .Add(x => x.Form.ID, x => x.FormID)
  83. .Add(x => x.Number, x => x.Number)
  84. .Add(x => x.Form.Description, x => x.FormName)
  85. .Add(x => x.FormCompleted, x => x.Completed)
  86. .Add(x => x.FormCompletedBy.UserID, x => x.CompletedBy)
  87. .Add(x => x.FormProcessed, x => x.Processed)
  88. .Add(x => x.Parent.ID, x => x.ParentID);
  89. _query = new MultiQuery();
  90. _types = CoreUtils.TypeList(e =>
  91. e.IsSubclassOf(typeof(Entity))
  92. && e.GetInterfaces().Contains(typeof(IRemotable))
  93. && e.GetInterfaces().Contains(typeof(IPersistent))
  94. && e.GetInterfaces().Contains(typeof(IDigitalFormInstance))
  95. );
  96. _data = new CoreTable();
  97. _data.LoadColumns(typeof(DigitalFormDockModel));
  98. }
  99. protected override void Init()
  100. {
  101. }
  102. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  103. {
  104. options.BeginUpdate();
  105. options.Clear();
  106. options.Add(DynamicGridOption.FilterRows);
  107. options.EndUpdate();
  108. }
  109. protected override DynamicGridStyle GetRowStyle(CoreRow row, DynamicGridStyle style)
  110. {
  111. var result = base.GetRowStyle(row, style);
  112. if (!row.Get<DigitalFormDockModel, DateTime>(x => x.Processed).IsEmpty())
  113. result = new DynamicGridRowStyle(result)
  114. {
  115. Background = new SolidColorBrush(Colors.LightGray),
  116. };
  117. return result;
  118. }
  119. private bool TypeFilter(CoreRow row, string[] filter)
  120. {
  121. string typename = row.Get<DigitalFormDockModel, Type>(x => x.FormType).EntityName().Split('.').Last().SplitCamelCase();
  122. return filter.Contains(typename);
  123. }
  124. private BitmapImage? TypeImage(CoreRow? arg)
  125. {
  126. if (arg == null)
  127. return null;
  128. var type = arg.Get<DigitalFormDockModel, Type>(x => x.FormType);
  129. return Images.GetValueOrDefault(type)?.AsBitmapImage();
  130. }
  131. private void MenuBuild(DynamicMenuColumn column, CoreRow? row)
  132. {
  133. if (row == null) return;
  134. var instanceID = row.Get<DigitalFormDockModel, Guid>(x => x.ID);
  135. var formID = row.Get<DigitalFormDockModel, Guid>(x => x.FormID);
  136. var formType = row.Get<DigitalFormDockModel, Type>(x => x.FormType);
  137. var filter = Filter.Create<IDigitalFormInstance>(formType, x => x.ID).IsEqualTo(instanceID);
  138. var model = (Activator.CreateInstance(typeof(DigitalFormReportDataModel<>).MakeGenericType(formType), filter, formID) as DataModel)!;
  139. var modules = new Client<CustomModule>().Query(
  140. new Filter<CustomModule>(x => x.DataModel).IsEqualTo(model.Name)
  141. .And(x => x.Section).IsEqualTo(formID.ToString())
  142. .And(x => x.Visible).IsEqualTo(true)
  143. ).Rows.Select(x => x.ToObject<CustomModule>());
  144. foreach(var module in modules)
  145. {
  146. column.AddItem(
  147. module.Name,
  148. PRSDesktop.Resources.edit,
  149. row =>
  150. {
  151. try
  152. {
  153. if(ScriptDocument.RunCustomModule(model, new Dictionary<string, object[]>(), module.Script))
  154. {
  155. Refresh(false, true);
  156. }
  157. }
  158. catch(CompileException c)
  159. {
  160. MessageBox.Show(c.Message);
  161. }
  162. catch(Exception e)
  163. {
  164. MessageBox.Show(CoreUtils.FormatException(e));
  165. }
  166. },
  167. null,
  168. row.Get<DigitalFormDockModel, DateTime>(x => x.Processed).IsEmpty()
  169. );
  170. }
  171. if (modules.Any())
  172. column.AddSeparator();
  173. if (row.Get<DigitalFormDockModel, DateTime>(x => x.Processed).IsEmpty())
  174. {
  175. column.AddItem(
  176. "Mark As Processed",
  177. PRSDesktop.Resources.lock_sml,
  178. (row) =>
  179. {
  180. var form = (Activator.CreateInstance(formType) as IDigitalFormInstance)!;
  181. form.ID = row!.Get<DigitalFormDockModel, Guid>(x => x.ID);
  182. form.FormProcessed = row.Get<DigitalFormDockModel, DateTime>(x => x.Processed);
  183. form.CommitChanges();
  184. form.FormProcessed = DateTime.Now;
  185. using (new WaitCursor())
  186. {
  187. ClientFactory.CreateClient(formType).Save(form, "Marked As Processed");
  188. Refresh(false, true);
  189. }
  190. }
  191. );
  192. }
  193. else
  194. {
  195. column.AddItem(
  196. "Clear Processed Flag",
  197. PRSDesktop.Resources.lock_sml,
  198. (row) =>
  199. {
  200. var form = (Activator.CreateInstance(formType) as IDigitalFormInstance)!;
  201. form.ID = row!.Get<DigitalFormDockModel, Guid>(x => x.ID);
  202. form.FormProcessed = row.Get<DigitalFormDockModel, DateTime>(x => x.Processed);
  203. form.CommitChanges();
  204. form.FormProcessed = DateTime.MinValue;
  205. using (new WaitCursor())
  206. {
  207. ClientFactory.CreateClient(formType).Save(form, "Processed Flag Cleared");
  208. Refresh(false, true);
  209. }
  210. }
  211. );
  212. }
  213. var entityType = DFUtils.FormEntityType(formType);
  214. if(entityType.HasInterface<IJobScopedItem>())
  215. {
  216. var entityID = row.Get<DigitalFormDockModel, Guid>(x => x.ParentID);
  217. column.AddSeparator();
  218. column.AddItem("Set Job", PRSDesktop.Resources.project, (row) =>
  219. {
  220. var entity = (Client.Create(entityType)
  221. .Query(
  222. Filter.Create<Entity>(entityType, x => x.ID).IsEqualTo(entityID),
  223. LookupFactory.DefineFilterColumns(typeof(Job), entityType)
  224. .Add<Entity>(x => x.ID)
  225. .Add<IJobScopedItem>(x => x.JobLink.ID)
  226. .Add<IJobScopedItem>(x => x.JobLink.JobNumber))
  227. .ToObjects(entityType).First() as Entity)!;
  228. var item = (entity as IJobScopedItem)!;
  229. var window = new MultiSelectDialog<Job>(
  230. LookupFactory.DefineFilter<Job>(entityType, CoreUtils.One(entity)),
  231. new Columns<Job>(x => x.DefaultScope.ID).Add(x => x.JobNumber),
  232. multiselect: false);
  233. if (!window.ShowDialog(nameof(Job.JobNumber), item.JobLink.JobNumber, Syncfusion.Data.FilterType.Equals))
  234. {
  235. return;
  236. }
  237. var job = window.Data().ToObjects<Job>().First();
  238. item.JobLink.ID = job.ID;
  239. item.JobLink.Synchronise(job);
  240. Client.Create(entityType).Save(entity, "Linked job set by user from Digital forms dock.");
  241. MessageBox.Show($"{entityType.Name} has been assigned to job {job.JobNumber}.");
  242. });
  243. column.AddItem("Set Job Scope", PRSDesktop.Resources.project, (row) =>
  244. {
  245. var entity = (Client.Create(entityType)
  246. .Query(
  247. Filter.Create<Entity>(entityType, x => x.ID).IsEqualTo(entityID),
  248. LookupFactory.DefineFilterColumns(typeof(JobScope), entityType)
  249. .Add<Entity>(x => x.ID)
  250. .Add<IJobScopedItem>(x => x.JobLink.ID)
  251. .Add<IJobScopedItem>(x => x.JobScope.ID)
  252. .Add<IJobScopedItem>(x => x.JobScope.Number))
  253. .ToObjects(entityType).First() as Entity)!;
  254. var item = (entity as IJobScopedItem)!;
  255. if(item.JobLink.ID == Guid.Empty)
  256. {
  257. MessageBox.Show($"{entityType.Name} is not linked to a job. Please select a job first.");
  258. return;
  259. }
  260. var window = new MultiSelectDialog<JobScope>(
  261. new Filters<JobScope>()
  262. .Add(LookupFactory.DefineFilter<JobScope>(entityType, CoreUtils.One(entity)))
  263. .Add(new Filter<JobScope>(x => x.Job.ID).IsEqualTo(item.JobLink.ID))
  264. .Combine(),
  265. new Columns<JobScope>(x => x.ID).Add(x => x.Number),
  266. multiselect: false);
  267. if (!window.ShowDialog(nameof(JobScope.Number), item.JobScope.Number, Syncfusion.Data.FilterType.Equals))
  268. {
  269. return;
  270. }
  271. var scope = window.Data().ToObjects<JobScope>().First();
  272. item.JobScope.ID = scope.ID;
  273. Client.Create(entityType).Save(entity, "Linked scope set by user from Digital forms dock.");
  274. MessageBox.Show($"{entityType.Name} has been assigned to scope {scope.Number}.");
  275. });
  276. }
  277. else if(entityType == typeof(Job) && formType == typeof(JobForm))
  278. {
  279. column.AddSeparator();
  280. column.AddItem("Set Job Scope", PRSDesktop.Resources.project, (row) =>
  281. {
  282. var instance = Client.Query(
  283. new Filter<JobForm>(x => x.ID).IsEqualTo(instanceID),
  284. LookupFactory.DefineFilterColumns<JobForm, JobScope>()
  285. .Add(x => x.ID)
  286. .Add(x => x.Parent.ID))
  287. .ToObjects<JobForm>().First();
  288. var job = Client.Query(
  289. new Filter<Job>(x => x.ID).IsEqualTo(instance.Parent.ID),
  290. LookupFactory.DefineFilterColumns<Job, JobScope>()).ToObjects<Job>().First();
  291. var window = new MultiSelectDialog<JobScope>(
  292. new Filters<JobScope>()
  293. .Add(LookupFactory.DefineFilter<Job, JobScope>(new Job[] { job }))
  294. .Add(new Filter<JobScope>(x => x.Job.ID).IsEqualTo(instance.Parent.ID))
  295. .Combine(),
  296. new Columns<JobScope>(x => x.ID).Add(x => x.Number), multiselect: false);
  297. if (!window.ShowDialog())
  298. {
  299. return;
  300. }
  301. var scope = window.Data().ToObjects<JobScope>().First();
  302. instance.JobScope.ID = scope.ID;
  303. Client.Save(instance, "Linked scope set by user from Digital forms dock.");
  304. MessageBox.Show($"Form has been assigned to scope {scope.Number}.");
  305. });
  306. }
  307. if (Security.IsAllowed<CanCustomiseModules>())
  308. {
  309. column.AddSeparator();
  310. column.AddItem("Customise Modules", PRSDesktop.Resources.script, row =>
  311. {
  312. var manager = new CustomModuleManager
  313. {
  314. Section = formID.ToString(),
  315. DataModel = model
  316. };
  317. manager.ShowDialog();
  318. });
  319. }
  320. if (Security.IsAllowed<CanPrintReports>())
  321. {
  322. column.AddSeparator();
  323. var printItem = column.AddItem("Print", PRSDesktop.Resources.printer, null);
  324. ReportUtils.PopulateMenu(printItem, formID.ToString(), model, Security.IsAllowed<CanDesignReports>(), true);
  325. }
  326. }
  327. /*private void AttachToScope(CoreRow row, Type formType, DataModel model)
  328. {
  329. var instance = Client.Create(formType)
  330. .Query(
  331. Filter.Create<IDigitalFormInstance>(formType, x => x.ID).IsEqualTo(row.Get<DigitalFormDockModel, Guid>(x => x.ID)),
  332. Columns.Create<IDigitalFormInstance>(formType)
  333. .Add<IDigitalFormInstance>(x => x.ID)
  334. .Add<IDigitalFormInstance>(x => x.Number))
  335. .Rows.FirstOrDefault()
  336. ?.ToObject(formType) as IDigitalFormInstance;
  337. if (instance is null)
  338. {
  339. MessageBox.Show("Form does not exist!");
  340. return;
  341. }
  342. var formID = row.Get<DigitalFormDockModel, Guid>(x => x.FormID);
  343. var entityColumns = JobScopeForms.GetEntityColumns(formType)?.Add<Entity>(x => x.ID);
  344. var entityObj = Client.Create(instance.ParentType())
  345. .Query(
  346. Filter.Create<Entity>(instance.ParentType(), x => x.ID).IsEqualTo(row.Get<DigitalFormDockModel, Guid>(x => x.ParentID)),
  347. entityColumns).ToObjects(instance.ParentType()).FirstOrDefault();
  348. if (entityObj is not Entity entity)
  349. {
  350. MessageBox.Show($"Attached {instance.ParentType().Name} does not exist!");
  351. return;
  352. }
  353. #region Selecting Scope
  354. // JobScopes obviously can't have an empty ID, so it's fine that I turn null into Guid.Empty here.
  355. var scopeID = JobScopeForms.GetJobScopeID(formType, instance, entity) ?? Guid.Empty;
  356. if(scopeID == Guid.Empty)
  357. {
  358. // JobScopes can't have an empty job, so it's fine that I turn null into Guid.Empty here.
  359. var jobID = JobScopeForms.GetJobID(formType, instance, entity) ?? Guid.Empty;
  360. if(jobID == Guid.Empty)
  361. {
  362. var jobs = new MultiSelectDialog<Job>(null, null, multiselect: false);
  363. if(!jobs.ShowDialog("Select a job"))
  364. {
  365. MessageBox.Show("No job selected; process cancelled.");
  366. return;
  367. }
  368. jobID = jobs.IDs().First();
  369. }
  370. var scopes = new MultiSelectDialog<JobScope>(new Filter<JobScope>(x => x.Job.ID).IsEqualTo(jobID), null, multiselect: false);
  371. if (!scopes.ShowDialog("Select a scope"))
  372. {
  373. MessageBox.Show("Process cancelled.");
  374. return;
  375. }
  376. scopeID = scopes.IDs().First();
  377. }
  378. var scopeTask = Task.Run(() => Client.QueryMultiple(
  379. new KeyedQueryDef<JobScope>(
  380. new Filter<JobScope>(x => x.ID).IsEqualTo(scopeID),
  381. new Columns<JobScope>(x => x.ID).Add(x => x.Number)),
  382. new KeyedQueryDef<Job>(
  383. new Filter<Job>(x => x.ID).InQuery(new Filter<JobScope>(x => x.ID).IsEqualTo(scopeID), x => x.Job.ID),
  384. new Columns<Job>(x => x.JobNumber))));
  385. #endregion
  386. #region Selecting Report
  387. var reports = new List<ReportTemplate>();
  388. Progress.ShowModal("Loading Document", (progress) =>
  389. {
  390. reports = ReportUtils.LoadReports(formID.ToString(), model).ToList();
  391. });
  392. ReportTemplate report;
  393. if (reports.Count == 0)
  394. {
  395. ReportTemplate reportTemplate = null!;
  396. Progress.ShowModal("Loading form layout", (progress) =>
  397. {
  398. progress.Report("Loading form layout");
  399. var layouts = Client.Query(
  400. new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(formID),
  401. new Columns<DigitalFormLayout>(x => x.Layout)
  402. .Add(x => x.Code)
  403. .Add(x => x.Type)
  404. .Add(x => x.Description))
  405. .ToObjects<DigitalFormLayout>().ToArray();
  406. var layout = layouts.FirstOrDefault(x => x.Type == DFLayoutType.Desktop) ?? layouts.FirstOrDefault();
  407. if (layout is null)
  408. {
  409. MessageBox.Show("This form has no report or layout, so it cannot be attached to a job scope!");
  410. return;
  411. }
  412. progress.Report("Generating report");
  413. report = new ReportTemplate
  414. {
  415. DataModel = model.Name,
  416. Name = layout.Description.NotWhiteSpaceOr(layout.Code),
  417. RDL = DigitalFormUtils.GenerateReport(layout, model)?.SaveToString()
  418. };
  419. });
  420. report = reportTemplate;
  421. }
  422. else if (reports.Count == 1)
  423. {
  424. report = reports[0];
  425. }
  426. else
  427. {
  428. var selectedReports = new MultiSelectDialog<ReportTemplate>(
  429. ReportUtils.GetReportFilter(formID.ToString(), model),
  430. null,
  431. multiselect: false);
  432. if (!selectedReports.ShowDialog())
  433. {
  434. return;
  435. }
  436. report = selectedReports.Items().First();
  437. }
  438. #endregion
  439. Document document = null!;
  440. Progress.ShowModal("Loading Document", (progress) =>
  441. {
  442. progress.Report("Generating Document");
  443. var data = ReportUtils.ReportToPDF(report, model, true);
  444. progress.Report("Saving Document");
  445. document = new Document
  446. {
  447. FileName = $"{instance.Number}: {report.Name}.pdf",
  448. Data = data,
  449. CRC = CoreUtils.CalculateCRC(data)
  450. };
  451. Client.Save(document, "");
  452. progress.Report("Creating Job Scope Document");
  453. var jobScopeDocument = new JobScopeDocument();
  454. jobScopeDocument.DocumentLink.ID = document.ID;
  455. jobScopeDocument.EntityLink.ID = scopeID;
  456. Client.Save(jobScopeDocument, $"Generated from form: {instance.Number}");
  457. });
  458. scopeTask.Wait();
  459. var scopeResults = scopeTask.Result;
  460. var scope = scopeResults.GetObjects<JobScope>().First();
  461. var job = scopeResults.GetObjects<Job>().First();
  462. MessageBox.Show($"Attached as '{document.FileName}' to job scope {scope.Number} for job {job.JobNumber}");
  463. }*/
  464. private DynamicMenuStatus MenuStatus(CoreRow row)
  465. {
  466. if (row == null) return DynamicMenuStatus.Hidden;
  467. return DynamicMenuStatus.Enabled;
  468. }
  469. protected override void Reload(Filters<DigitalFormDockModel> criteria, Columns<DigitalFormDockModel> columns, ref SortOrder<DigitalFormDockModel>? sort, Action<CoreTable, Exception?> action)
  470. {
  471. _query.Clear();
  472. foreach (var type in _types.Where(x => !ExcludedTypes.Contains(x)))
  473. {
  474. var filter = Filter.Create<IDigitalFormInstance>(type, x => x.FormCompleted).IsGreaterThanOrEqualTo(StartDate)
  475. .And<IDigitalFormInstance>(x=>x.FormCancelled).IsEqualTo(DateTime.MinValue);
  476. var cols = Columns.Create<IDigitalFormInstance>(type)
  477. .Add<IDigitalFormInstance>(c => c.ID)
  478. .Add<IDigitalFormInstance>(c => c.Parent.ID)
  479. .Add<IDigitalFormInstance>(c => c.Form.ID)
  480. .Add<IDigitalFormInstance>(c => c.Number)
  481. .Add<IDigitalFormInstance>(c => c.Form.Description)
  482. .Add<IDigitalFormInstance>(c => c.FormCompleted)
  483. .Add<IDigitalFormInstance>(c => c.FormCompletedBy.UserID)
  484. .Add<IDigitalFormInstance>(c => c.FormProcessed);
  485. var sorts = SortOrder.Create<IDigitalFormInstance>(type, x => x.FormCompleted, SortDirection.Descending);
  486. _query.Add(
  487. new QueryDef(type)
  488. {
  489. Filter = filter,
  490. Columns = cols,
  491. SortOrder = sorts
  492. },
  493. type
  494. );
  495. }
  496. _query.Query((q) =>
  497. {
  498. _data.Rows.Clear();
  499. foreach (var type in _types.Where(x=>!ExcludedTypes.Contains(x)))
  500. _data.LoadFrom(
  501. _query.Get(type),
  502. _mappings,
  503. (r) => r.Set<DigitalFormDockModel,Type>(x=>x.FormType,type)
  504. );
  505. action.Invoke(_data, null);
  506. });
  507. }
  508. protected override DigitalFormDockModel LoadItem(CoreRow row)
  509. {
  510. return row.ToObject<DigitalFormDockModel>();
  511. }
  512. public override void SaveItem(DigitalFormDockModel item)
  513. {
  514. //throw new NotImplementedException();
  515. }
  516. protected override void DeleteItems(params CoreRow[] rows)
  517. {
  518. //throw new NotImplementedException();
  519. }
  520. protected override void DoDoubleClick(object sender)
  521. {
  522. base.DoDoubleClick(sender);
  523. var row = SelectedRows.FirstOrDefault();
  524. if (row is null)
  525. return;
  526. var instanceID = row.Get<DigitalFormDockModel, Guid>(x => x.ID);
  527. var formID = row.Get<DigitalFormDockModel, Guid>(x => x.FormID);
  528. var formType = row.Get<DigitalFormDockModel, Type>(x => x.FormType);
  529. var formInstance = Client.Create(formType)
  530. .Query(
  531. Filter.Create<IDigitalFormInstance>(formType, x => x.ID).IsEqualTo(instanceID),
  532. DynamicFormEditWindow.FormColumns(formType))
  533. .Rows.FirstOrDefault()
  534. ?.ToObject(formType) as IDigitalFormInstance;
  535. if(formInstance is null)
  536. {
  537. return;
  538. }
  539. if (formID == Guid.Empty)
  540. {
  541. var window = new DeletedFormWindow();
  542. window.FormData = formInstance?.FormData ?? "";
  543. window.ShowDialog();
  544. return;
  545. }
  546. if (DynamicFormEditWindow.EditDigitalForm(formInstance, out var model))
  547. {
  548. model.Update(null);
  549. }
  550. }
  551. }
  552. public partial class DigitalFormsDock : UserControl, IDockPanel
  553. {
  554. private static SolidColorBrush EnabledBrush = new SolidColorBrush(Colors.LightYellow);
  555. private static SolidColorBrush DisabledBrush = new SolidColorBrush(Colors.LightGray);
  556. public DigitalFormsDock()
  557. {
  558. InitializeComponent();
  559. RefreshButton.Content = new Image() { Source = PRSDesktop.Resources.refresh.AsBitmapImage() };
  560. foreach (var type in Items.Images.Keys)
  561. {
  562. Button button = new Button();
  563. button.Background = EnabledBrush;
  564. button.Content = new Image() { Source = Items.Images[type].AsBitmapImage() };
  565. button.BorderBrush = new SolidColorBrush(Colors.Gray);
  566. button.BorderThickness = new Thickness(0.75);
  567. button.Margin = new Thickness(2, 0, 0, 0);
  568. button.Width = 25D;
  569. button.Padding = new Thickness(2);
  570. button.ToolTip = type.EntityName().Split('.').Last().SplitCamelCase();
  571. button.Click += TypeFilterClick;
  572. button.Tag = type;
  573. TypeStack.Children.Add(button);
  574. }
  575. }
  576. private void TypeFilterClick(object sender, RoutedEventArgs e)
  577. {
  578. var button = (sender as Button)!;
  579. var type = (button.Tag as Type)!;
  580. if (Items.ExcludedTypes.Contains(type))
  581. Items.ExcludedTypes.Remove(type);
  582. else
  583. Items.ExcludedTypes.Add(type);
  584. button.Background = Items.ExcludedTypes.Contains(type)
  585. ? DisabledBrush
  586. : EnabledBrush;
  587. Refresh();
  588. }
  589. private void RefreshButton_OnClick(object sender, RoutedEventArgs e)
  590. {
  591. Refresh();
  592. }
  593. public void Setup()
  594. {
  595. Items.Refresh(true, false);
  596. }
  597. public void Refresh()
  598. {
  599. Items.Refresh(false, true);
  600. }
  601. private void AgeCombo_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  602. {
  603. var startdate = AgeCombo.SelectedIndex == 2
  604. ? DateTime.Today.AddDays(-30)
  605. : AgeCombo.SelectedIndex == 1
  606. ? DateTime.Today.AddDays(-7)
  607. : DateTime.Today;
  608. if ((Items != null) && (startdate != Items.StartDate))
  609. {
  610. Items.StartDate = startdate;
  611. Refresh();
  612. }
  613. }
  614. }
  615. }