DigitalFormReportGrid.cs 7.9 KB

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