ConsignmentsModule.xaml.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Comal.Classes;
  7. using InABox.Configuration;
  8. using InABox.Core;
  9. using InABox.Mobile;
  10. using Xamarin.Forms;
  11. using Xamarin.Forms.Xaml;
  12. namespace PRS.Mobile
  13. {
  14. public class ConsignmentModuleSettings : ILocalConfigurationSettings
  15. {
  16. public String FilterName { get; set; }
  17. }
  18. [XamlCompilation(XamlCompilationOptions.Compile)]
  19. public partial class ConsignmentsModule : MobilePage
  20. {
  21. private ConsignmentModuleSettings _settings;
  22. private CoreFilterDefinitions _filters;
  23. private ConsignmentModel _consignments = new ConsignmentModel(App.Data, () => null) { FileName = "consignments.index" };
  24. public ConsignmentsModule()
  25. {
  26. Task[] _setup = new Task[]
  27. {
  28. Task.Run(() => { _settings = new LocalConfiguration<ConsignmentModuleSettings>().Load(); }),
  29. Task.Run(() => { _filters = _consignments.AvailableFilters(); }),
  30. };
  31. InitializeComponent();
  32. Task.WaitAll(_setup);
  33. SetupFilters();
  34. }
  35. private void SetupFilters()
  36. {
  37. _filter.Items.Clear();
  38. foreach (var group in _filters)
  39. {
  40. var item = new MobileMenuItem() { Text = group.Name };
  41. item.Clicked += (sender, args) =>
  42. {
  43. var text = (sender as MobileMenuItem)?.Text ?? string.Empty;
  44. _settings.FilterName = text;
  45. new LocalConfiguration<ConsignmentModuleSettings>().Save(_settings);
  46. RefreshData(true, false);
  47. };
  48. _filter.Items.Add(item);
  49. }
  50. _filter.IsVisible = _filter.Items.Any();
  51. }
  52. private void RefreshData(bool force, bool async)
  53. {
  54. _consignments.SelectFilter(_settings.FilterName);
  55. if (async)
  56. {
  57. _consignments.Refresh(force, () => Device.BeginInvokeOnMainThread(Refresh));
  58. }
  59. else
  60. {
  61. _consignments.Refresh(true);
  62. Refresh();
  63. }
  64. }
  65. private void Refresh()
  66. {
  67. _consignmentlist.LastUpdated = _consignments.LastUpdated;
  68. Title = String.IsNullOrWhiteSpace(_settings.FilterName) ? "All Consignments" : _settings.FilterName;
  69. _consignments.Search(FilterShell);
  70. _consignmentlist.ItemsSource = _consignments.Items;
  71. }
  72. private string _currentfilter = "";
  73. private bool FilterShell(ConsignmentShell shell)
  74. {
  75. if (String.IsNullOrWhiteSpace(_currentfilter))
  76. return true;
  77. return shell.Number.ToUpper().Contains(_currentfilter.ToUpper())
  78. || shell.SupplierName.ToUpper().Contains(_currentfilter.ToUpper())
  79. || shell.TypeDescription.ToUpper().Contains(_currentfilter.ToUpper());
  80. }
  81. private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args)
  82. {
  83. _currentfilter = args.Text;
  84. Refresh();
  85. }
  86. private void MobileList_OnRefresh(object sender, MobileListRefreshEventArgs args)
  87. {
  88. RefreshData(true, false);
  89. }
  90. private void Consignment_Clicked(object sender, EventArgs e)
  91. {
  92. if ((sender as MobileCard)?.BindingContext is ConsignmentShell shell)
  93. {
  94. var editor = new ConsignmentEdit(shell);
  95. Navigation.PushAsync(editor);
  96. }
  97. }
  98. private ConsignmentShell? _newshell;
  99. protected override void OnAppearing()
  100. {
  101. base.OnAppearing();
  102. if ((_newshell != null) && (_newshell.ID != Guid.Empty))
  103. _consignments.CommitItem(_newshell);
  104. _newshell = null;
  105. RefreshData(true, false);
  106. }
  107. private void AddConsignment_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
  108. {
  109. ShowPopup(() => SelectionView.Execute<PurchaseOrderShell>(
  110. (columns) =>
  111. {
  112. columns.Add(new MobileGridTextColumn<PurchaseOrderShell>()
  113. {
  114. Column = x => x.PONumber,
  115. Width = GridLength.Auto,
  116. Caption = "Number",
  117. Alignment = TextAlignment.Start
  118. });
  119. columns.Add(new MobileGridTextColumn<PurchaseOrderShell>()
  120. {
  121. Column = x => x.SupplierName,
  122. Width = GridLength.Star,
  123. Caption = "Select Purchase Order",
  124. Alignment = TextAlignment.Start
  125. });
  126. },
  127. (refresh) =>
  128. {
  129. var model = new PurchaseOrderModel(App.Data,
  130. () => new Filter<PurchaseOrder>(x => x.IssuedDate).IsNotEqualTo(DateTime.MinValue)
  131. .And(x=>x.ClosedDate).IsEqualTo(DateTime.MinValue)
  132. .And(x=>x.CancelledDate).IsEqualTo(DateTime.MinValue)
  133. .And(x=>x.Unreceived).IsNotEqualTo(FilterConstant.Null)) { FileName = "consigment_orders.index" };
  134. return model.Refresh(false);
  135. },
  136. (orders) =>
  137. {
  138. _newshell = _consignments.CreateItem();
  139. Navigation.PushAsync(new ConsignmentEdit(_newshell));
  140. var model = new PurchaseOrderItemModel(App.Data,
  141. () => new Filter<PurchaseOrderItem>(x => x.PurchaseOrderLink.ID).IsEqualTo(orders.FirstOrDefault()?.ID ?? Guid.Empty));
  142. model.Refresh(false);
  143. foreach (var item in model.Items)
  144. {
  145. var newitem = ViewModel.Items.AddItem();
  146. newitem.Row.LoadValues(item.Row.Values);
  147. }
  148. Dispatcher.BeginInvokeOnMainThread(() =>
  149. {
  150. _itemsList.ItemsSource = null;
  151. _itemsList.ItemsSource = ViewModel.Items;
  152. });
  153. DismissPopup();
  154. }));
  155. }
  156. }
  157. }