DataEntryPanel.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.Wpf;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Media;
  13. using InABox.Configuration;
  14. using PRSDesktop.Panels.DataEntry.Grids;
  15. namespace PRSDesktop;
  16. public class DataEntryPanelSettings : BaseObject, IUserConfigurationSettings
  17. {
  18. private static readonly double DefaultCacheAge = 7;
  19. [NullEditor]
  20. public double PreviewWidth { get; set; }
  21. [Comment("Age of cached documents before they are deleted (days)")]
  22. [DoubleEditor]
  23. public double CacheAge { get; set; } = DefaultCacheAge;
  24. }
  25. /// <summary>
  26. /// Interaction logic for DataEntryPanel.xaml
  27. /// </summary>
  28. public partial class DataEntryPanel : UserControl, IBasePanel, IDynamicEditorHost
  29. {
  30. private DataEntryPanelSettings _settings;
  31. //IPopupEditorControl? _popup;
  32. private IDynamicDataGrid? _grid;
  33. private Type? _selectedType;
  34. // The id of the entity currently being edited.
  35. private Guid _entityID;
  36. // The id of the entity we selected from the grid, as it was when we selected it; this is to cancel, because the lookup for the item
  37. // updates _entityID, and we need to set it back when cancelling.
  38. private Guid _originalID;
  39. private bool _processenabled;
  40. private Entity? _entity;
  41. private Button? _process;
  42. private bool _isChanged;
  43. private bool IsChanged
  44. {
  45. get => _isChanged;
  46. set
  47. {
  48. if(_isChanged != value)
  49. {
  50. _isChanged = value;
  51. Editor.HideButtons = !value;
  52. if (_process != null)
  53. _process.IsEnabled = value;
  54. _documents._dataEntryGrid.IsEnabled = !value;
  55. }
  56. }
  57. }
  58. public void Select(Type? type, Guid id)
  59. {
  60. ClearEditor();
  61. _selectedType = type;
  62. _entityID = id;
  63. if (_selectedType != null)
  64. {
  65. CreateEditor();
  66. PopulateEditor();
  67. //LoadPopup();
  68. }
  69. }
  70. private void ScanPanel_OnSelectScan(string appliesto, Guid entityid, bool processenabled)
  71. {
  72. _processenabled = processenabled;
  73. _originalID = entityid;
  74. Select(
  75. CoreUtils.GetEntityOrNull(appliesto),
  76. entityid
  77. );
  78. }
  79. public DataEntryPanel()
  80. {
  81. InitializeComponent();
  82. LoadSettings();
  83. }
  84. private void LoadSettings()
  85. {
  86. _settings = new UserConfiguration<DataEntryPanelSettings>().Load();
  87. _panel.AnchorWidth = _settings.PreviewWidth > 0.0F
  88. ? _settings.PreviewWidth
  89. : 500F;
  90. }
  91. public void Setup()
  92. {
  93. _documents.Setup();
  94. IsChanged = false;
  95. }
  96. public void Refresh()
  97. {
  98. if (CheckSaved())
  99. {
  100. _documents.Refresh();
  101. IsChanged = false;
  102. }
  103. }
  104. public bool IsReady { get; set; }
  105. public string SectionName => "Data Entry";
  106. public DataModel DataModel(Selection selection)
  107. {
  108. return new EmptyDataModel();
  109. }
  110. public event DataModelUpdateEvent? OnUpdateDataModel;
  111. public void CreateToolbarButtons(IPanelHost host)
  112. {
  113. if (Security.IsAllowed<CanSetupDataEntryTags>())
  114. {
  115. host.CreateSetupAction(new PanelAction("Data Entry Tags", null, (action) =>
  116. {
  117. var list = new MasterList(typeof(DataEntryTag));
  118. list.ShowDialog();
  119. }));
  120. host.CreateSetupAction(new PanelAction("Settings", null, (action) =>
  121. {
  122. var settings = new UserConfiguration<DataEntryPanelSettings>().Load();
  123. var grid = new DynamicItemsListGrid<DataEntryPanelSettings>();
  124. if (grid.EditItems(new DataEntryPanelSettings[] { settings }))
  125. {
  126. new UserConfiguration<DataEntryPanelSettings>().Save(settings);
  127. LoadSettings();
  128. }
  129. }));
  130. }
  131. }
  132. public void Heartbeat(TimeSpan time)
  133. {
  134. }
  135. public Dictionary<string, object[]> Selected()
  136. {
  137. return new Dictionary<string, object[]>();
  138. }
  139. private void CheckSaved(CancelEventArgs cancel)
  140. {
  141. if (!_isChanged)
  142. {
  143. return;
  144. }
  145. var result = MessageBox.Show("You have changes that have not been saved; do you wish to save these changes?", "Save Changes?", MessageBoxButton.YesNoCancel);
  146. if (result == MessageBoxResult.Yes)
  147. {
  148. if (!Editor.Validate())
  149. {
  150. cancel.Cancel = true;
  151. return;
  152. }
  153. DoSave(false);
  154. if (!cancel.Cancel)
  155. MessageBox.Show("Item saved.");
  156. }
  157. else if (result == MessageBoxResult.Cancel)
  158. {
  159. cancel.Cancel = true;
  160. }
  161. }
  162. private bool CheckSaved()
  163. {
  164. var cancel = new CancelEventArgs();
  165. CheckSaved(cancel);
  166. return !cancel.Cancel;
  167. }
  168. public void Shutdown(CancelEventArgs? cancel)
  169. {
  170. if (cancel != null)
  171. CheckSaved(cancel);
  172. if (cancel?.Cancel != true)
  173. _documents.Shutdown(cancel);
  174. }
  175. #region Host
  176. public Type GetEditorType() => _selectedType ?? typeof(BaseObject);
  177. public void LoadLookups(ILookupEditorControl sender)
  178. {
  179. var colname = sender.ColumnName;
  180. var values = sender.LookupEditorDefinition.Values(colname, Editor.Items);
  181. sender.LoadLookups(values);
  182. }
  183. BaseObject[] IDynamicEditorHost.GetItems() => Editor.Items;
  184. public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor();
  185. #endregion
  186. private void ClearEditor()
  187. {
  188. DetailBorder.Child = null;
  189. IsChanged = false;
  190. //if (_popup is UIElement element)
  191. // DetailHeader.Children.Remove(element);
  192. }
  193. private void CreateEditor()
  194. {
  195. if (_selectedType == null)
  196. return;
  197. Editor = new EmbeddedDynamicEditorForm();
  198. if (_selectedType == typeof(Bill))
  199. Editor.SetLayoutType<SupplierBillEditDocumentLayout>();
  200. else
  201. Editor.SetLayoutType<VerticalDynamicEditorGridLayout>();
  202. Editor.HighlightButtons = true;
  203. Editor.HideButtons = true;
  204. Editor.SetValue(Grid.RowProperty, 1);
  205. Editor.SetValue(Grid.ColumnProperty, 0);
  206. Editor.SetValue(Grid.ColumnSpanProperty, 4);
  207. Editor.OnOK += () => { DoSave(false); };
  208. Editor.OnCancel += () =>
  209. {
  210. _entityID = _originalID;
  211. Select(_selectedType, _entityID);
  212. };
  213. Editor.OnChanged += (sender, args) => IsChanged = true;
  214. DetailBorder.Child = Editor;
  215. _grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), _selectedType) as IDynamicDataGrid)!;
  216. _grid.OnCustomiseEditor += (sender, items, column, editor) =>
  217. {
  218. if ((editor is BaseCodeEditor be) && editor.Editable.EditorVisible())
  219. {
  220. be.Buttons = new[]
  221. {
  222. new EditorButton(null, "..", 30, DoLookup, false)
  223. };
  224. }
  225. };
  226. _grid.OnAfterEditorValueChangedEvent += (sender, args) =>
  227. {
  228. IsChanged = IsChanged || (_entity?.IsChanged() == true || _originalID != _entityID);
  229. return null;
  230. };
  231. if (_grid is DynamicDataGrid<JobDocumentSetMileStone> milestoneGrid)
  232. {
  233. milestoneGrid.OnValidate += (o, items, err) =>
  234. {
  235. if (items.Any(x => x.ID == Guid.Empty))
  236. {
  237. err.Add("Cannot create a new milestone from Data Entry screen. Please select a milestone to edit.");
  238. }
  239. };
  240. }
  241. }
  242. private void DoLookup(object editor, object? item)
  243. {
  244. if (_selectedType == null)
  245. return;
  246. if (editor is CodeEditorControl ce)
  247. {
  248. Dictionary<string, string>? filter = null;
  249. if (ce.Value != null)
  250. {
  251. filter = new Dictionary<string, string>
  252. {
  253. [ce.ColumnName] = ce.Value
  254. };
  255. }
  256. Type? gridType = null;
  257. if (_selectedType == typeof(JobDocumentSetMileStone))
  258. {
  259. gridType = typeof(JobDocumentSetMileStoneDataEntryPopupGrid);
  260. }
  261. var popup = new PopupList(_selectedType, _entityID, Array.Empty<string>(), filter, gridType: gridType);
  262. popup.OnDefineFilter += LookupFactory.DefineFilter;
  263. if (popup.ShowDialog() == true)
  264. {
  265. _entityID = popup.ID;
  266. Select(_selectedType, _entityID);
  267. }
  268. }
  269. }
  270. private void SaveDocument(DataEntryDocument dataEntryDocument)
  271. {
  272. var doctype = CoreUtils.TypeList(x =>
  273. {
  274. var entityDoc = x.GetInterfaceDefinition(typeof(IEntityDocument<>));
  275. if (entityDoc is null)
  276. {
  277. return false;
  278. }
  279. var linkType = entityDoc.GenericTypeArguments[0].GetInterfaceDefinition(typeof(IEntityLink<>));
  280. if (linkType is null)
  281. {
  282. return false;
  283. }
  284. return linkType.GenericTypeArguments[0] == _selectedType;
  285. }).FirstOrDefault();
  286. if(doctype is null)
  287. {
  288. return;
  289. }
  290. var doc = (IEntityDocument)Activator.CreateInstance(doctype)!;
  291. CoreUtils.SetPropertyValue(doc, "EntityLink.ID", _entityID);
  292. doc.DocumentLink.ID = dataEntryDocument.Document.ID;
  293. doc.Thumbnail = dataEntryDocument.Thumbnail;
  294. ClientFactory.CreateClient(doctype).Save(doc,"Added from Data Entry Screen");
  295. }
  296. private void DoSave(bool markasprocessed)
  297. {
  298. var cancel = new System.ComponentModel.CancelEventArgs();
  299. if (markasprocessed && (_entity is IDataEntryInstance scannable))
  300. scannable.DataEntered = DateTime.Now;
  301. Editor.SaveItem(cancel);
  302. if (!cancel.Cancel)
  303. {
  304. _originalID = _entityID;
  305. _entityID = _entity.ID;
  306. IsChanged = false;
  307. var row = _documents._dataEntryGrid.SelectedRows.FirstOrDefault();
  308. if (row != null)
  309. {
  310. var scan = row.ToObject<DataEntryDocument>();
  311. scan.EntityID = _entity.ID;
  312. if (markasprocessed)
  313. {
  314. SaveDocument(scan);
  315. scan.Archived = DateTime.Now;
  316. }
  317. if (scan.IsChanged())
  318. {
  319. new Client<DataEntryDocument>().Save(scan, "Updated from Data Entry Screen");
  320. _documents.Refresh();
  321. }
  322. }
  323. }
  324. }
  325. private void PopulateEditor()
  326. {
  327. if (_selectedType == null)
  328. return;
  329. _entity = null;
  330. if (_entityID != Guid.Empty)
  331. {
  332. _entity = Client.Create(_selectedType)
  333. .Query(
  334. Filter.Create<Entity>(_selectedType, x => x.ID).IsEqualTo(_entityID),
  335. _grid.LoadEditorColumns()
  336. )
  337. .ToObjects(_selectedType)
  338. .OfType<Entity>()
  339. .FirstOrDefault();
  340. }
  341. _entity ??= Activator.CreateInstance(_selectedType) as Entity;
  342. if (_entity == null)
  343. return;
  344. _grid?.InitialiseEditorForm(Editor, new object[] { _entity }, null, true);
  345. _process = new Button()
  346. {
  347. Content = "Mark as Processed",
  348. BorderBrush = new SolidColorBrush(Colors.DarkBlue),
  349. Background = new SolidColorBrush(Colors.DodgerBlue),
  350. Margin = new Thickness(0, 0, 5, 0),
  351. Padding = new Thickness(10, 0, 10, 0),
  352. IsEnabled = _processenabled
  353. };
  354. _process.Click += (sender, args) =>
  355. {
  356. if (Editor.Validate())
  357. {
  358. DoSave(true);
  359. }
  360. };
  361. Editor.AddButton(_process);
  362. IsChanged = false;
  363. if (_selectedType == typeof(JobDocumentSetMileStone))
  364. {
  365. var disabled = _entityID == Guid.Empty;
  366. foreach (var page in Editor.Pages)
  367. {
  368. if (page is DynamicEditorGrid.DynamicEditPage editPage)
  369. {
  370. foreach (var editor in editPage.Editors)
  371. {
  372. if (editor.EditorDefinition is not BaseCodeEditor)
  373. {
  374. editor.IsEnabled = !disabled && editor.EditorDefinition.Editable.IsEditable();
  375. }
  376. }
  377. }
  378. else
  379. {
  380. page.ReadOnly = disabled;
  381. }
  382. }
  383. }
  384. }
  385. private void _panel_OnOnChanged(object sender, DynamicSplitPanelSettings e)
  386. {
  387. _settings.PreviewWidth = e.AnchorWidth;
  388. new UserConfiguration<DataEntryPanelSettings>().Save(_settings);
  389. }
  390. }