ConsignmentsModule.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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,
  24. null,
  25. CoreRepository.CacheFileName<Consignment>
  26. );
  27. public ConsignmentsModule()
  28. {
  29. Task[] _setup = new Task[]
  30. {
  31. Task.Run(() => { _settings = new LocalConfiguration<ConsignmentModuleSettings>().Load(); }),
  32. Task.Run(() => { _filters = _consignments.AvailableFilters(); }),
  33. };
  34. InitializeComponent();
  35. Task.WaitAll(_setup);
  36. SetupFilters();
  37. }
  38. private void SetupFilters()
  39. {
  40. _filter.Items.Clear();
  41. foreach (var group in _filters)
  42. {
  43. var item = new MobileMenuItem() { Text = group.Name };
  44. item.Clicked += (sender, args) =>
  45. {
  46. var text = (sender as MobileMenuItem)?.Text ?? string.Empty;
  47. _settings.FilterName = text;
  48. new LocalConfiguration<ConsignmentModuleSettings>().Save(_settings);
  49. RefreshData(true, false);
  50. };
  51. _filter.Items.Add(item);
  52. }
  53. _filter.IsVisible = _filter.Items.Any();
  54. }
  55. private void RefreshData(bool force, bool async)
  56. {
  57. _consignments.SelectFilter(_settings.FilterName);
  58. if (async)
  59. {
  60. _consignments.Refresh(force, () => Device.BeginInvokeOnMainThread(Refresh));
  61. }
  62. else
  63. {
  64. _consignments.Refresh(true);
  65. Refresh();
  66. }
  67. }
  68. private void Refresh()
  69. {
  70. _consignmentlist.LastUpdated = _consignments.LastUpdated;
  71. Title = String.IsNullOrWhiteSpace(_settings.FilterName) ? "All Consignments" : _settings.FilterName;
  72. _consignments.Search(FilterShell);
  73. _consignmentlist.ItemsSource = _consignments.Items;
  74. }
  75. private string _currentfilter = "";
  76. private bool FilterShell(ConsignmentShell shell)
  77. {
  78. if (String.IsNullOrWhiteSpace(_currentfilter))
  79. return true;
  80. return shell.Number.ToUpper().Contains(_currentfilter.ToUpper())
  81. || shell.SupplierName.ToUpper().Contains(_currentfilter.ToUpper())
  82. || shell.TypeDescription.ToUpper().Contains(_currentfilter.ToUpper());
  83. }
  84. private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args)
  85. {
  86. _currentfilter = args.Text;
  87. Refresh();
  88. }
  89. private void MobileList_OnRefresh(object sender, MobileListRefreshEventArgs args)
  90. {
  91. RefreshData(true, false);
  92. }
  93. private void Consignment_Clicked(object sender, EventArgs e)
  94. {
  95. if ((sender as MobileCard)?.BindingContext is ConsignmentShell shell)
  96. {
  97. var editor = new ConsignmentEdit(shell);
  98. Navigation.PushAsync(editor);
  99. }
  100. }
  101. private ConsignmentShell? _newshell;
  102. protected override void OnAppearing()
  103. {
  104. base.OnAppearing();
  105. if ((_newshell != null) && (_newshell.ID != Guid.Empty))
  106. _consignments.CommitItem(_newshell);
  107. _newshell = null;
  108. RefreshData(true, false);
  109. }
  110. private void AddConsignment_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
  111. {
  112. ShowPopup(() => SelectionView.Execute<PurchaseOrderShell>(
  113. (columns) =>
  114. {
  115. columns.Add(new MobileGridTextColumn<PurchaseOrderShell>()
  116. {
  117. Column = x => x.PONumber,
  118. Width = GridLength.Auto,
  119. Caption = "Number",
  120. Alignment = TextAlignment.Start
  121. });
  122. columns.Add(new MobileGridTextColumn<PurchaseOrderShell>()
  123. {
  124. Column = x => x.SupplierName,
  125. Width = GridLength.Star,
  126. Caption = "Select Purchase Order",
  127. Alignment = TextAlignment.Start
  128. });
  129. },
  130. (refresh) =>
  131. {
  132. var model = new PurchaseOrderModel(App.Data,
  133. () => new Filter<PurchaseOrder>(x => x.IssuedDate).IsNotEqualTo(DateTime.MinValue)
  134. .And(x=>x.ClosedDate).IsEqualTo(DateTime.MinValue)
  135. .And(x=>x.CancelledDate).IsEqualTo(DateTime.MinValue)
  136. .And(x=>x.Unreceived).IsNotEqualTo(FilterConstant.Null)
  137. );
  138. return model.Refresh(false);
  139. },
  140. (orders) =>
  141. {
  142. var order = orders.FirstOrDefault() ?? new PurchaseOrderShell();
  143. _newshell = _consignments.CreateItem();
  144. _newshell.SupplierID = order.SupplierID;
  145. _newshell.SupplierName = order.SupplierName;
  146. _newshell.Number = order.PONumber;
  147. _newshell.EstimatedWarehouseArrival = order.DueDate;
  148. _newshell.ActualWarehouseArrival = DateTime.Now;
  149. _newshell.Description = $"Goods Received on PO {order.PONumber}";
  150. Navigation.PushAsync(new ConsignmentEdit(_newshell, orders.FirstOrDefault()));
  151. DismissPopup();
  152. }));
  153. }
  154. }
  155. }