CustomerReceipts.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 void Refresh()
  36. {
  37. Refresh(false, true);
  38. }
  39. public void Setup()
  40. {
  41. Refresh(true, false);
  42. }
  43. public void Shutdown()
  44. {
  45. }
  46. public void Heartbeat(TimeSpan time)
  47. {
  48. }
  49. private void BeforeSave(IDynamicEditorForm editor, BaseObject[] items)
  50. {
  51. var receipt = items?.FirstOrDefault() as Receipt;
  52. if (receipt == null)
  53. return;
  54. if (!string.IsNullOrWhiteSpace(receipt.Notes))
  55. return;
  56. var page =
  57. editor.Pages.FirstOrDefault(x => x is DynamicManyToManyGrid<InvoiceReceipt, Receipt>) as DynamicManyToManyGrid<InvoiceReceipt, Receipt>;
  58. if (page != null)
  59. {
  60. var numbers = page.Data.Rows.Select(r => r.Get<InvoiceReceipt, int>(c => c.InvoiceLink.Number)).ToArray();
  61. receipt.Notes = string.Format("Invoice{0} {1}", numbers.Length > 1 ? "s" : "", string.Join(", ", numbers));
  62. }
  63. }
  64. private bool ToggleView(Button sender, CoreRow[] rows)
  65. {
  66. Outstanding = !Outstanding;
  67. UpdateButton(sender, PRSDesktop.Resources.view.AsBitmapImage(), Outstanding ? "Show All" : "Outstanding");
  68. return true;
  69. }
  70. protected override void Reload(Filters<Receipt> criteria, Columns<Receipt> columns, ref SortOrder<Receipt> sort,
  71. Action<CoreTable, Exception> action)
  72. {
  73. if (Outstanding)
  74. criteria.Add(new Filter<Receipt>(x => x.Reconciled).IsEqualTo(DateTime.MinValue));
  75. base.Reload(criteria, columns, ref sort, action);
  76. }
  77. }