using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Comal.Classes; using InABox.Configuration; using InABox.Core; using InABox.Mobile; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace PRS.Mobile { public class ConsignmentModuleSettings : ILocalConfigurationSettings { public String FilterName { get; set; } } [XamlCompilation(XamlCompilationOptions.Compile)] public partial class ConsignmentsModule : MobilePage { private ConsignmentModuleSettings _settings; private CoreFilterDefinitions _filters; private ConsignmentModel _consignments = new ConsignmentModel(App.Data, () => null) { FileName = "consignments.index" }; public ConsignmentsModule() { Task[] _setup = new Task[] { Task.Run(() => { _settings = new LocalConfiguration().Load(); }), Task.Run(() => { _filters = _consignments.AvailableFilters(); }), }; InitializeComponent(); Task.WaitAll(_setup); SetupFilters(); } private void SetupFilters() { _filter.Items.Clear(); foreach (var group in _filters) { var item = new MobileMenuItem() { Text = group.Name }; item.Clicked += (sender, args) => { var text = (sender as MobileMenuItem)?.Text ?? string.Empty; _settings.FilterName = text; new LocalConfiguration().Save(_settings); RefreshData(true, false); }; _filter.Items.Add(item); } _filter.IsVisible = _filter.Items.Any(); } private void RefreshData(bool force, bool async) { _consignments.SelectFilter(_settings.FilterName); if (async) { _consignments.Refresh(force, () => Device.BeginInvokeOnMainThread(Refresh)); } else { _consignments.Refresh(true); Refresh(); } } private void Refresh() { _consignmentlist.LastUpdated = _consignments.LastUpdated; Title = String.IsNullOrWhiteSpace(_settings.FilterName) ? "All Consignments" : _settings.FilterName; _consignments.Search(FilterShell); _consignmentlist.ItemsSource = _consignments.Items; } private string _currentfilter = ""; private bool FilterShell(ConsignmentShell shell) { if (String.IsNullOrWhiteSpace(_currentfilter)) return true; return shell.Number.ToUpper().Contains(_currentfilter.ToUpper()) || shell.SupplierName.ToUpper().Contains(_currentfilter.ToUpper()) || shell.TypeDescription.ToUpper().Contains(_currentfilter.ToUpper()); } private void _search_OnTextChanged(object sender, MobileSearchBarTextChangedArgs args) { _currentfilter = args.Text; Refresh(); } private void MobileList_OnRefresh(object sender, MobileListRefreshEventArgs args) { RefreshData(true, false); } private void Consignment_Clicked(object sender, EventArgs e) { if ((sender as MobileCard)?.BindingContext is ConsignmentShell shell) { var editor = new ConsignmentEdit(shell); Navigation.PushAsync(editor); } } private ConsignmentShell? _newshell; protected override void OnAppearing() { base.OnAppearing(); if ((_newshell != null) && (_newshell.ID != Guid.Empty)) _consignments.CommitItem(_newshell); _newshell = null; RefreshData(true, false); } private void AddConsignment_Clicked(object sender, MobileMenuButtonClickedEventArgs args) { ShowPopup(() => SelectionView.Execute( (columns) => { columns.Add(new MobileGridTextColumn() { Column = x => x.PONumber, Width = GridLength.Auto, Caption = "Number", Alignment = TextAlignment.Start }); columns.Add(new MobileGridTextColumn() { Column = x => x.SupplierName, Width = GridLength.Star, Caption = "Select Purchase Order", Alignment = TextAlignment.Start }); }, (refresh) => { var model = new PurchaseOrderModel(App.Data, () => new Filter(x => x.IssuedDate).IsNotEqualTo(DateTime.MinValue) .And(x=>x.ClosedDate).IsEqualTo(DateTime.MinValue) .And(x=>x.CancelledDate).IsEqualTo(DateTime.MinValue) .And(x=>x.Unreceived).IsNotEqualTo(FilterConstant.Null)) { FileName = "consigment_orders.index" }; return model.Refresh(false); }, (orders) => { var order = orders.FirstOrDefault() ?? new PurchaseOrderShell(); _newshell = _consignments.CreateItem(); _newshell.SupplierID = order.SupplierID; _newshell.SupplierName = order.SupplierName; _newshell.Number = order.PONumber; _newshell.EstimatedWarehouseArrival = order.DueDate; _newshell.ActualWarehouseArrival = DateTime.Now; _newshell.Description = $"Goods Received on PO {order.PONumber}"; Navigation.PushAsync(new ConsignmentEdit(_newshell, orders.FirstOrDefault())); DismissPopup(); })); } } }