DigitalFormReportGrid.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 : DynamicGrid<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 bool _readOnly;
  27. public bool ReadOnly
  28. {
  29. get => _readOnly;
  30. set
  31. {
  32. if (_readOnly != value)
  33. {
  34. _readOnly = value;
  35. Reconfigure();
  36. }
  37. }
  38. }
  39. protected new DynamicGridMemoryEntityDataComponent<ReportTemplate> DataComponent;
  40. protected override void Init()
  41. {
  42. if (Security.CanEdit<ReportTemplate>())
  43. {
  44. ActionColumns.Add(new DynamicImageColumn(ScriptImage, ScriptClick));
  45. ActionColumns.Add(new DynamicImageColumn(Wpf.Resources.pencil.AsBitmapImage(), DesignClick));
  46. }
  47. DataComponent = new DynamicGridMemoryEntityDataComponent<ReportTemplate>(this);
  48. base.DataComponent = DataComponent;
  49. }
  50. protected override void DoReconfigure(DynamicGridOptions options)
  51. {
  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. Refresh(true, false);
  65. var data = PageDataHandler?.Invoke(typeof(ReportTemplate));
  66. if(data is null && Form.ID == Guid.Empty)
  67. {
  68. data = new CoreTable();
  69. data.LoadColumns(typeof(ReportTemplate));
  70. }
  71. if(data is not null)
  72. {
  73. DataComponent.LoadData(data);
  74. }
  75. else
  76. {
  77. DataComponent.LoadData(
  78. new Filter<ReportTemplate>(x => x.Section).IsEqualTo(Form.ID.ToString()),
  79. Columns.All<ReportTemplate>(),
  80. null);
  81. }
  82. Refresh(false, true);
  83. Ready = true;
  84. }
  85. private DynamicVariableGrid? GetVariableGrid()
  86. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicVariableGrid)
  87. as DynamicVariableGrid;
  88. private List<DigitalFormVariable> GetVariables()
  89. => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
  90. private DynamicFormLayoutGrid? GetLayoutGrid()
  91. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicFormLayoutGrid)
  92. as DynamicFormLayoutGrid;
  93. private List<DigitalFormLayout> GetLayouts()
  94. => GetLayoutGrid()?.Items.ToList() ?? new List<DigitalFormLayout>();
  95. private BitmapImage? ScriptImage(CoreRow? arg)
  96. {
  97. return arg == null ? Wpf.Resources.edit.AsBitmapImage() :
  98. arg.Get<ReportTemplate, bool>(x => x.IsRDL) ? null : Wpf.Resources.edit.AsBitmapImage();
  99. }
  100. private bool ScriptClick(CoreRow? arg)
  101. {
  102. if (arg != null && !ReadOnly)
  103. {
  104. if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
  105. {
  106. Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
  107. return false;
  108. }
  109. var template = DataComponent.LoadItem(arg);
  110. var script = template.Script;
  111. if (string.IsNullOrWhiteSpace(script))
  112. script = string.Format(ReportTemplate.DefaultScriptTemplate, model.GetType().Name.Split('.').Last());
  113. var editor = new ScriptEditorWindow(script);
  114. if (editor.ShowDialog() == true)
  115. {
  116. template.Script = editor.Script;
  117. DataComponent.SaveItem(template);
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. private bool DesignClick(CoreRow? arg)
  124. {
  125. if (arg is null && !ReadOnly) return false;
  126. if (DigitalFormUtils.GetDataModel(Form.AppliesTo,GetVariables()) is not DataModel model)
  127. {
  128. Logger.Send(LogType.Error, "", "Invalid entity type for data model.");
  129. return false;
  130. }
  131. var template = DataComponent.LoadItem(arg);
  132. ReportUtils.DesignReport(template, model, true, saveTemplate: (template) => DataComponent.SaveItem(template));
  133. return false;
  134. }
  135. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  136. {
  137. var layouts = GetLayouts();
  138. if(layouts.Count == 0)
  139. {
  140. base.DoAdd(OpenEditorOnDirectEdit);
  141. }
  142. else
  143. {
  144. var menu = new ContextMenu();
  145. foreach (var layout in layouts)
  146. {
  147. menu.AddItem($"{layout.Code}: {layout.Description}", null, layout, AddLayout);
  148. }
  149. menu.AddSeparatorIfNeeded();
  150. menu.AddItem("Create blank report", null, () => base.DoAdd());
  151. menu.IsOpen = true;
  152. }
  153. }
  154. private void AddLayout(DigitalFormLayout layout)
  155. {
  156. var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables());
  157. var item = CreateItem();
  158. item.DataModel = model.Name;
  159. item.Name = string.IsNullOrWhiteSpace(layout.Description) ? layout.Code : layout.Description;
  160. item.RDL = DigitalFormUtils.GenerateReport(layout, model)?.SaveToString();
  161. if (EditItems(new[] { item }))
  162. {
  163. DataComponent.SaveItem(item);
  164. Refresh(false, true);
  165. }
  166. }
  167. public void BeforeSave(object item)
  168. {
  169. }
  170. public void AfterSave(object item)
  171. {
  172. foreach (var template in DataComponent.Items)
  173. {
  174. template.Section = Form.ID.ToString();
  175. }
  176. DataComponent.SaveItems();
  177. }
  178. public override ReportTemplate CreateItem()
  179. {
  180. var item = base.CreateItem();
  181. var model = DigitalFormUtils.GetDataModel(Form.AppliesTo, GetVariables());
  182. item.DataModel = model.Name;
  183. if (Form.ID != Guid.Empty)
  184. {
  185. item.Section = Form.ID.ToString();
  186. }
  187. return item;
  188. }
  189. public string Caption() => "Reports";
  190. public Size MinimumSize()
  191. {
  192. return new Size(400, 400);
  193. }
  194. public int Order()
  195. {
  196. return int.MinValue;
  197. }
  198. }
  199. }