DigitalFormReportGrid.cs 7.7 KB

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