DigitalFormReportGrid.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(DynamicGridOptions options)
  50. {
  51. base.DoReconfigure(options);
  52. options.ShowHelp = true;
  53. if (Security.CanEdit<ReportTemplate>() && !ReadOnly)
  54. {
  55. options.AddRows = true;
  56. options.EditRows = true;
  57. }
  58. if (Security.CanDelete<ReportTemplate>() && !ReadOnly)
  59. options.DeleteRows = true;
  60. }
  61. public void Load(object item, Func<Type, CoreTable?>? PageDataHandler)
  62. {
  63. Form = (DigitalForm)item;
  64. CoreTable data;
  65. if (PageDataHandler != null)
  66. data = PageDataHandler?.Invoke(typeof(ReportTemplate));
  67. else if (Form.ID == Guid.Empty)
  68. {
  69. data = new CoreTable();
  70. data.LoadColumns(typeof(ReportTemplate));
  71. }
  72. else
  73. {
  74. data = new Client<ReportTemplate>()
  75. .Query(new Filter<ReportTemplate>(x => x.Section).IsEqualTo(Form.ID.ToString()));
  76. }
  77. OriginalItems = data.Rows.Select(x => x.ToObject<ReportTemplate>()).ToList();
  78. Items = OriginalItems.ToList();
  79. Refresh(true, true);
  80. Ready = true;
  81. }
  82. private DynamicVariableGrid? GetVariableGrid()
  83. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
  84. as DynamicVariableGrid;
  85. private List<DigitalFormVariable> GetVariables()
  86. => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
  87. private DynamicFormLayoutGrid? GetLayoutGrid()
  88. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicFormLayoutGrid)
  89. as DynamicFormLayoutGrid;
  90. private List<DigitalFormLayout> GetLayouts()
  91. => GetLayoutGrid()?.Items.ToList() ?? new List<DigitalFormLayout>();
  92. private BitmapImage? ScriptImage(CoreRow? arg)
  93. {
  94. return arg == null ? Wpf.Resources.edit.AsBitmapImage() :
  95. arg.Get<ReportTemplate, bool>(x => x.IsRDL) ? null : Wpf.Resources.edit.AsBitmapImage();
  96. }
  97. private bool ScriptClick(CoreRow? arg)
  98. {
  99. if (arg != null && !ReadOnly)
  100. {
  101. if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
  102. {
  103. Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
  104. return false;
  105. }
  106. var template = LoadItem(arg);
  107. var script = template.Script;
  108. if (string.IsNullOrWhiteSpace(script))
  109. script = string.Format(ReportTemplate.DefaultScriptTemplate, model.GetType().Name.Split('.').Last());
  110. var editor = new ScriptEditorWindow(script);
  111. if (editor.ShowDialog() == true)
  112. {
  113. template.Script = editor.Script;
  114. SaveItem(template);
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. private bool DesignClick(CoreRow? arg)
  121. {
  122. if (arg is null && !ReadOnly) return false;
  123. if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
  124. {
  125. Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
  126. return false;
  127. }
  128. var template = LoadItem(arg);
  129. ReportUtils.DesignReport(template, model, true, saveTemplate: (template) => SaveItem(template));
  130. return false;
  131. }
  132. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  133. {
  134. var layouts = GetLayouts();
  135. if(layouts.Count == 0)
  136. {
  137. base.DoAdd(OpenEditorOnDirectEdit);
  138. }
  139. else
  140. {
  141. var menu = new ContextMenu();
  142. foreach (var layout in layouts)
  143. {
  144. menu.AddItem($"{layout.Code}: {layout.Description}", null, layout, AddLayout);
  145. }
  146. menu.AddSeparatorIfNeeded();
  147. menu.AddItem("Create blank report", null, () => base.DoAdd());
  148. menu.IsOpen = true;
  149. }
  150. }
  151. private void AddLayout(DigitalFormLayout layout)
  152. {
  153. var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables());
  154. var item = CreateItem();
  155. item.DataModel = model.Name;
  156. item.Name = string.IsNullOrWhiteSpace(layout.Description) ? layout.Code : layout.Description;
  157. item.RDL = DigitalFormUtils.GenerateReport(layout, model)?.SaveToString();
  158. if (EditItems(new[] { item }))
  159. {
  160. SaveItem(item);
  161. Refresh(false, true);
  162. }
  163. }
  164. public void BeforeSave(object item)
  165. {
  166. }
  167. public void AfterSave(object item)
  168. {
  169. // First remove any deleted files
  170. foreach (var original in OriginalItems)
  171. if (!Items.Contains(original))
  172. new Client<ReportTemplate>().Delete(original, typeof(ReportTemplate).Name + " Deleted by User");
  173. foreach (var template in Items)
  174. {
  175. template.Section = Form.ID.ToString();
  176. }
  177. new Client<ReportTemplate>().Save(Items.Where(x => x.IsChanged()), "Updated by User");
  178. }
  179. public override ReportTemplate CreateItem()
  180. {
  181. var item = base.CreateItem();
  182. var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables());
  183. item.DataModel = model.Name;
  184. if (Form.ID != Guid.Empty)
  185. {
  186. item.Section = Form.ID.ToString();
  187. }
  188. return item;
  189. }
  190. public string Caption() => "Reports";
  191. public Size MinimumSize()
  192. {
  193. return new Size(400, 400);
  194. }
  195. public int Order()
  196. {
  197. return int.MinValue;
  198. }
  199. }
  200. }