MYOBCompanyFileSelectionDialog.xaml.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using InABox.Core;
  2. using MYOB.AccountRight.SDK.Contracts;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. namespace InABox.Poster.MYOB;
  19. /// <summary>
  20. /// Interaction logic for MYOBCompanyFileSelectionDialog.xaml
  21. /// </summary>
  22. public partial class MYOBCompanyFileSelectionDialog : Window, INotifyPropertyChanged
  23. {
  24. private bool _canSave;
  25. public bool CanSave
  26. {
  27. get => _canSave;
  28. set
  29. {
  30. _canSave = true;
  31. OnPropertyChanged();
  32. }
  33. }
  34. public List<MYOBCompanyFile> Items
  35. {
  36. get => Grid.Items;
  37. set => Grid.Items = value;
  38. }
  39. public MYOBCompanyFile? Result
  40. {
  41. get
  42. {
  43. var row = Grid.SelectedRows.FirstOrDefault();
  44. if(row is not null)
  45. {
  46. return Grid.LoadItem(row);
  47. }
  48. return null;
  49. }
  50. }
  51. public MYOBCompanyFileSelectionDialog()
  52. {
  53. InitializeComponent();
  54. }
  55. public event PropertyChangedEventHandler? PropertyChanged;
  56. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  57. {
  58. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  59. }
  60. private void Grid_OnSelectItem(object sender, DynamicGrid.DynamicGridSelectionEventArgs e)
  61. {
  62. CanSave = e.Rows is not null && e.Rows.Length > 0;
  63. }
  64. private void Window_Loaded(object sender, RoutedEventArgs e)
  65. {
  66. Grid.Refresh(true, true);
  67. }
  68. private void CancelButton_Click(object sender, RoutedEventArgs e)
  69. {
  70. DialogResult = false;
  71. Close();
  72. }
  73. private void OKButton_Click(object sender, RoutedEventArgs e)
  74. {
  75. DialogResult = true;
  76. Close();
  77. }
  78. public static CompanyFile? SelectCompanyFile(IPosterDispatcher dispatcher)
  79. {
  80. var data = MYOBPosterEngine.GetConnectionData(dispatcher);
  81. var window = new MYOBCompanyFileSelectionDialog();
  82. var files = data.CompanyFileService.GetRange();
  83. window.Items = files.Select(x =>
  84. {
  85. return new MYOBCompanyFile
  86. {
  87. ID = x.Id,
  88. Name = x.Name
  89. };
  90. }).ToList();
  91. if(window.ShowDialog() == true)
  92. {
  93. var result = window.Result;
  94. if(result is not null)
  95. {
  96. return files.FirstOrDefault(x => x.Id == result.ID);
  97. }
  98. else
  99. {
  100. return null;
  101. }
  102. }
  103. else
  104. {
  105. return null;
  106. }
  107. }
  108. }