DigitalFormReportGrid.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using FastReport;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.Core.Reports;
  5. using InABox.Wpf.Reports;
  6. using InABox.WPF;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Windows.Controls;
  12. using System.Windows.Media.Imaging;
  13. using FastReport.Table;
  14. using FastReport.Utils;
  15. using Border = System.Windows.Controls.Border;
  16. using Size = System.Windows.Size;
  17. using netDxf.Objects;
  18. namespace InABox.DynamicGrid
  19. {
  20. public class DigitalFormReportGrid : DynamicItemsListGrid<ReportTemplate>, IDynamicEditorPage
  21. {
  22. public DynamicEditorGrid EditorGrid { get; set; }
  23. public PageType PageType => PageType.Other;
  24. public bool Ready { get; set; }
  25. private DigitalForm Form { get; set; }
  26. private List<ReportTemplate> OriginalItems { get; set; } = new();
  27. private bool _readOnly;
  28. public bool ReadOnly
  29. {
  30. get => _readOnly;
  31. set
  32. {
  33. if (_readOnly != value)
  34. {
  35. _readOnly = value;
  36. Reconfigure();
  37. }
  38. }
  39. }
  40. protected override void Init()
  41. {
  42. base.Init();
  43. if (Security.CanEdit<ReportTemplate>())
  44. {
  45. ActionColumns.Add(new DynamicImageColumn(ScriptImage, ScriptClick));
  46. ActionColumns.Add(new DynamicImageColumn(Wpf.Resources.pencil.AsBitmapImage(), DesignClick));
  47. }
  48. }
  49. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  50. {
  51. base.DoReconfigure(options);
  52. options.BeginUpdate();
  53. options.Add(DynamicGridOption.ShowHelp);
  54. if (Security.CanEdit<ReportTemplate>() && !ReadOnly)
  55. {
  56. options.Add(DynamicGridOption.AddRows);
  57. options.Add(DynamicGridOption.EditRows);
  58. }
  59. if (Security.CanDelete<ReportTemplate>() && !ReadOnly)
  60. options.Add(DynamicGridOption.DeleteRows);
  61. options.EndUpdate();
  62. }
  63. public void Load(object item, Func<Type, CoreTable?>? PageDataHandler)
  64. {
  65. Form = (DigitalForm)item;
  66. CoreTable data;
  67. if (PageDataHandler != null)
  68. data = PageDataHandler?.Invoke(typeof(ReportTemplate));
  69. else if (Form.ID == Guid.Empty)
  70. {
  71. data = new CoreTable();
  72. data.LoadColumns(typeof(ReportTemplate));
  73. }
  74. else
  75. {
  76. data = new Client<ReportTemplate>()
  77. .Query(new Filter<ReportTemplate>(x => x.Section).IsEqualTo(Form.ID.ToString()));
  78. }
  79. OriginalItems = data.Rows.Select(x => x.ToObject<ReportTemplate>()).ToList();
  80. Items = OriginalItems.ToList();
  81. Refresh(true, true);
  82. Ready = true;
  83. }
  84. private DynamicVariableGrid? GetVariableGrid()
  85. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
  86. as DynamicVariableGrid;
  87. private List<DigitalFormVariable> GetVariables()
  88. => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
  89. private DynamicFormLayoutGrid? GetLayoutGrid()
  90. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicFormLayoutGrid)
  91. as DynamicFormLayoutGrid;
  92. private List<DigitalFormLayout> GetLayouts()
  93. => GetLayoutGrid()?.Items.ToList() ?? new List<DigitalFormLayout>();
  94. private BitmapImage? ScriptImage(CoreRow? arg)
  95. {
  96. return arg == null ? Wpf.Resources.edit.AsBitmapImage() :
  97. arg.Get<ReportTemplate, bool>(x => x.IsRDL) ? null : Wpf.Resources.edit.AsBitmapImage();
  98. }
  99. private bool ScriptClick(CoreRow? arg)
  100. {
  101. if (arg != null && !ReadOnly)
  102. {
  103. if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
  104. {
  105. Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
  106. return false;
  107. }
  108. var template = LoadItem(arg);
  109. var script = template.Script;
  110. if (string.IsNullOrWhiteSpace(script))
  111. script = string.Format(ReportTemplate.DefaultScriptTemplate, model.GetType().Name.Split('.').Last());
  112. var editor = new ScriptEditorWindow(script);
  113. if (editor.ShowDialog() == true)
  114. {
  115. template.Script = editor.Script;
  116. SaveItem(template);
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. private bool DesignClick(CoreRow? arg)
  123. {
  124. if (arg is null && !ReadOnly) return false;
  125. if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
  126. {
  127. Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
  128. return false;
  129. }
  130. var template = LoadItem(arg);
  131. ReportUtils.DesignReport(template, model, true, saveTemplate: (template) => SaveItem(template));
  132. return false;
  133. }
  134. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  135. {
  136. var layouts = GetLayouts();
  137. if(layouts.Count == 0)
  138. {
  139. base.DoAdd(OpenEditorOnDirectEdit);
  140. }
  141. else
  142. {
  143. var menu = new ContextMenu();
  144. foreach (var layout in layouts)
  145. {
  146. menu.AddItem($"{layout.Code}: {layout.Description}", null, layout, AddLayout);
  147. }
  148. menu.AddSeparatorIfNeeded();
  149. menu.AddItem("Create blank report", null, () => base.DoAdd());
  150. menu.IsOpen = true;
  151. }
  152. }
  153. private void AddLayout(DigitalFormLayout layout)
  154. {
  155. var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables());
  156. var item = CreateItem();
  157. item.DataModel = model.Name;
  158. item.Name = string.IsNullOrWhiteSpace(layout.Description) ? layout.Code : layout.Description;
  159. item.RDL = DigitalFormUtils.GenerateReport(layout, model)?.SaveToString();
  160. if (EditItems(new[] { item }))
  161. {
  162. SaveItem(item);
  163. Refresh(false, true);
  164. }
  165. }
  166. public void BeforeSave(object item)
  167. {
  168. }
  169. public void AfterSave(object item)
  170. {
  171. // First remove any deleted files
  172. foreach (var original in OriginalItems)
  173. if (!Items.Contains(original))
  174. new Client<ReportTemplate>().Delete(original, typeof(ReportTemplate).Name + " Deleted by User");
  175. foreach (var template in Items)
  176. {
  177. template.Section = Form.ID.ToString();
  178. }
  179. new Client<ReportTemplate>().Save(Items.Where(x => x.IsChanged()), "Updated by User");
  180. }
  181. public override ReportTemplate CreateItem()
  182. {
  183. var item = base.CreateItem();
  184. var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables());
  185. item.DataModel = model.Name;
  186. if (Form.ID != Guid.Empty)
  187. {
  188. item.Section = Form.ID.ToString();
  189. }
  190. return item;
  191. }
  192. public string Caption() => "Reports";
  193. public Size MinimumSize()
  194. {
  195. return new Size(400, 400);
  196. }
  197. public int Order()
  198. {
  199. return int.MinValue;
  200. }
  201. }
  202. }