StockLocationSelectionViewModel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using InABox.Core;
  5. using Xamarin.CommunityToolkit.ObjectModel;
  6. using Xamarin.Forms;
  7. namespace PRS.Mobile
  8. {
  9. public class StockLocationSelectionViewModel : BindableObject
  10. {
  11. public bool MultiSelect { get; set; }
  12. public bool DisplayStockTakeInfo { get; set; }
  13. public bool CurrentStockTakesOnly { get; set; } = false;
  14. public bool IncludeInactive { get; set; }
  15. public bool PullToRefresh
  16. {
  17. get => _pullToRefresh;
  18. set
  19. {
  20. if (value == _pullToRefresh) return;
  21. _pullToRefresh = value;
  22. OnPropertyChanged();
  23. }
  24. }
  25. public DateTime LastUpdated { get; set; }
  26. private StockWarehouseShell _warehouse;
  27. public StockWarehouseShell Warehouse
  28. {
  29. get => _warehouse;
  30. set
  31. {
  32. if (Equals(value, _warehouse)) return;
  33. _warehouse = value;
  34. Area = new StockAreaShell();
  35. }
  36. }
  37. private StockAreaShell _area;
  38. public StockAreaShell Area
  39. {
  40. get => _area;
  41. set
  42. {
  43. if (Equals(value, _area)) return;
  44. _area = value;
  45. ReloadLocations();
  46. }
  47. }
  48. public int SelectedPage { get; set; }
  49. public ObservableRangeCollection<StockLocationShell> Locations { get; private set; }
  50. public ObservableRangeCollection<StockLocationShell> Favourites { get; private set; }
  51. public ObservableCollection<StockLocationShell> Selected { get; private set; }
  52. public StockLocationSelectionViewModel()
  53. {
  54. _excludedLocations = new Guid[] { };
  55. _warehouse = new StockWarehouseShell();
  56. _area = new StockAreaShell();
  57. Selected = new ObservableCollection<StockLocationShell>();
  58. Selected.CollectionChanged += (sender, args) => ReloadLocations();
  59. //App.Data.StockLocations.Refresh(false);
  60. Favourites = new ObservableRangeCollection<StockLocationShell>(App.Data.StockLocations.Where(x => x.Favourite));
  61. Locations = new ObservableRangeCollection<StockLocationShell>(App.Data.StockLocations.ToArray());
  62. }
  63. private Guid[] _excludedLocations;
  64. public Guid[] ExcludedLocations
  65. {
  66. get => _excludedLocations;
  67. set
  68. {
  69. _excludedLocations = value;
  70. Favourites.ReplaceRange(App.Data.StockLocations.Where(x => x.Favourite && !ExcludedLocations.Contains(x.ID)));
  71. Locations.ReplaceRange(App.Data.StockLocations.Where(x => !ExcludedLocations.Contains(x.ID)));
  72. }
  73. }
  74. public bool AddVisible { get; set; }
  75. private string _searchText;
  76. private bool _pullToRefresh;
  77. public string SearchText
  78. {
  79. get => _searchText;
  80. set
  81. {
  82. if (value == _searchText) return;
  83. _searchText = value;
  84. ReloadLocations();
  85. OnPropertyChanged();
  86. }
  87. }
  88. public void RefreshData(bool force)
  89. {
  90. App.Data.StockLocations.Refresh(force);
  91. if (PullToRefresh)
  92. LastUpdated = App.Data.StockLocations.LastUpdated;
  93. ReloadLocations();
  94. }
  95. public void ReloadLocations()
  96. {
  97. var favourites = App.Data.StockLocations.Where(x =>
  98. Selected.All(s => s.ID != x.ID)
  99. && (IncludeInactive || x.Active)
  100. && x.Favourite
  101. && !ExcludedLocations.Contains(x.ID)
  102. );
  103. if (DisplayStockTakeInfo && CurrentStockTakesOnly)
  104. favourites = favourites.Where(x => !x.CurrentStocktake.IsEmpty());
  105. Favourites.ReplaceRange(favourites);
  106. var locations = App.Data.StockLocations.Where(x =>
  107. Selected.All(s => s.ID != x.ID)
  108. && (IncludeInactive || x.Active)
  109. && (_area.ID == Guid.Empty || x.AreaID == _area.ID)
  110. && (_warehouse.ID == Guid.Empty || x.WarehouseID == _warehouse.ID)
  111. && !ExcludedLocations.Contains(x.ID)
  112. && (String.IsNullOrWhiteSpace(_searchText)
  113. || x.Code.ToUpper().Contains(_searchText.ToUpper())
  114. || x.Description.ToUpper().Contains(_searchText.ToUpper())
  115. )
  116. );
  117. if (DisplayStockTakeInfo && CurrentStockTakesOnly)
  118. locations = locations.Where(x => !x.CurrentStocktake.IsEmpty());
  119. Locations.ReplaceRange(locations);
  120. }
  121. }
  122. }