CustomerReceipts.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Controls;
  5. using Comal.Classes;
  6. using InABox.Core;
  7. using InABox.DynamicGrid;
  8. using InABox.WPF;
  9. namespace PRSDesktop;
  10. public class CustomerReceipts : DynamicDataGrid<Receipt>, IPanel<Receipt>
  11. {
  12. private bool Outstanding = true;
  13. public CustomerReceipts()
  14. {
  15. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.FilterRows, DynamicGridOption.SelectColumns, DynamicGridOption.MultiSelect);
  16. AddButton("Show All", PRSDesktop.Resources.view.AsBitmapImage(), ToggleView);
  17. OnBeforeSave += BeforeSave;
  18. }
  19. public bool IsReady { get; set; }
  20. public event DataModelUpdateEvent OnUpdateDataModel;
  21. public void CreateToolbarButtons(IPanelHost host)
  22. {
  23. //
  24. }
  25. public Dictionary<string, object[]> Selected()
  26. {
  27. return new Dictionary<string, object[]> { { typeof(Receipt).EntityName(), SelectedRows } };
  28. }
  29. public string SectionName => "Customer Receipts";
  30. public DataModel DataModel(Selection selection)
  31. {
  32. var ids = ExtractValues(x => x.ID, selection).ToArray();
  33. return new BaseDataModel<Receipt>(new Filter<Receipt>(x => x.ID).InList(ids));
  34. }
  35. public bool Focus()
  36. {
  37. return true;
  38. }
  39. public void Refresh()
  40. {
  41. Refresh(false, true);
  42. }
  43. public void Setup()
  44. {
  45. Refresh(true, false);
  46. }
  47. public void Shutdown()
  48. {
  49. }
  50. public void Heartbeat(TimeSpan time)
  51. {
  52. }
  53. private void BeforeSave(DynamicEditorForm editor, Receipt[] items)
  54. {
  55. if (items == null || items.Length != 1)
  56. return;
  57. if (!string.IsNullOrWhiteSpace(items[0].Notes))
  58. return;
  59. var page =
  60. editor.Pages.FirstOrDefault(x => x is DynamicManyToManyGrid<InvoiceReceipt, Receipt>) as DynamicManyToManyGrid<InvoiceReceipt, Receipt>;
  61. if (page != null)
  62. {
  63. var numbers = page.Data.Rows.Select(r => r.Get<InvoiceReceipt, int>(c => c.InvoiceLink.Number)).ToArray();
  64. items[0].Notes = string.Format("Invoice{0} {1}", numbers.Length > 1 ? "s" : "", string.Join(", ", numbers));
  65. }
  66. }
  67. private bool ToggleView(Button sender, CoreRow[] rows)
  68. {
  69. Outstanding = !Outstanding;
  70. UpdateButton(sender, PRSDesktop.Resources.view.AsBitmapImage(), Outstanding ? "Show All" : "Outstanding");
  71. return true;
  72. }
  73. protected override void Reload(Filters<Receipt> criteria, Columns<Receipt> columns, ref SortOrder<Receipt> sort,
  74. Action<CoreTable, Exception> action)
  75. {
  76. if (Outstanding)
  77. criteria.Add(new Filter<Receipt>(x => x.Reconciled).IsEqualTo(DateTime.MinValue));
  78. base.Reload(criteria, columns, ref sort, action);
  79. }
  80. }