CustomerReceipts.cs 3.7 KB

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