CustomerReceipts.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows.Controls;
  6. using Comal.Classes;
  7. using InABox.Configuration;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.WPF;
  11. namespace PRSDesktop;
  12. public class ReceiptGridSettings : IUserConfigurationSettings
  13. {
  14. [Obsolete]
  15. private CoreFilterDefinition? _currentFilter;
  16. [Obsolete]
  17. public CoreFilterDefinition? CurrentFilter
  18. {
  19. get => _currentFilter;
  20. set
  21. {
  22. if (value is not null)
  23. {
  24. Filters = new DynamicGridSelectedFilterSettings(new List<CoreFilterDefinition> { value }, false, null);
  25. }
  26. }
  27. }
  28. public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
  29. }
  30. public class CustomerReceipts : DynamicDataGrid<Receipt>, IPanel<Receipt>
  31. {
  32. //private bool Outstanding = true;
  33. private ReceiptGridSettings _settings;
  34. public CustomerReceipts()
  35. {
  36. //AddButton("Show All", PRSDesktop.Resources.view.AsBitmapImage(), ToggleView);
  37. OnBeforeSave += BeforeSave;
  38. }
  39. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  40. {
  41. base.DoReconfigure(options);
  42. options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.FilterRows, DynamicGridOption.SelectColumns, DynamicGridOption.MultiSelect);
  43. }
  44. protected override void Init()
  45. {
  46. base.Init();
  47. _settings = new UserConfiguration<ReceiptGridSettings>().Load();
  48. FilterComponent.SetSettings(_settings.Filters, refresh: false);
  49. FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected;
  50. }
  51. private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings)
  52. {
  53. _settings.Filters = settings;
  54. new UserConfiguration<ReceiptGridSettings>().Save(_settings);
  55. }
  56. public bool IsReady { get; set; }
  57. public event DataModelUpdateEvent? OnUpdateDataModel;
  58. public void CreateToolbarButtons(IPanelHost host)
  59. {
  60. AccountsSetupActions.Standard(host);
  61. //
  62. }
  63. public Dictionary<string, object[]> Selected()
  64. {
  65. return new Dictionary<string, object[]> { { typeof(Receipt).EntityName(), SelectedRows } };
  66. }
  67. public string SectionName => "Customer Receipts";
  68. public DataModel DataModel(Selection selection)
  69. {
  70. var ids = ExtractValues(x => x.ID, selection).ToArray();
  71. return new BaseDataModel<Receipt>(new Filter<Receipt>(x => x.ID).InList(ids));
  72. }
  73. public void Refresh()
  74. {
  75. Refresh(false, true);
  76. }
  77. public void Setup()
  78. {
  79. Refresh(true, false);
  80. }
  81. public void Shutdown(CancelEventArgs? cancel)
  82. {
  83. }
  84. public void Heartbeat(TimeSpan time)
  85. {
  86. }
  87. private void BeforeSave(IDynamicEditorForm editor, BaseObject[] items)
  88. {
  89. var receipt = items?.FirstOrDefault() as Receipt;
  90. if (receipt == null)
  91. return;
  92. if (!string.IsNullOrWhiteSpace(receipt.Notes))
  93. return;
  94. var page =
  95. editor.Pages.FirstOrDefault(x => x is DynamicManyToManyGrid<InvoiceReceipt, Receipt>) as DynamicManyToManyGrid<InvoiceReceipt, Receipt>;
  96. if (page != null)
  97. {
  98. var numbers = page.Data.Rows.Select(r => r.Get<InvoiceReceipt, int>(c => c.InvoiceLink.Number)).ToArray();
  99. receipt.Notes = string.Format("Invoice{0} {1}", numbers.Length > 1 ? "s" : "", string.Join(", ", numbers));
  100. }
  101. }
  102. }