DigitalFormReportGrid.cs 7.9 KB

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