123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using Plugin.Media;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace comal.timesheets
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class PurchaseOrderPage : ContentPage
- {
- List<PurchaseOrderShell> purchaseOrderShells = new List<PurchaseOrderShell>();
- bool bLoading = true;
- List<string> filterOptions = new List<string>();
- bool firstLoad = true;
- public PurchaseOrderPage()
- {
- InitializeComponent();
- LoadList();
- }
- protected override void OnAppearing()
- {
- base.OnAppearing();
- if (!firstLoad)
- UpdateListWithNumberOfItems();
- }
- private async void LoadList()
- {
- await Task.Run(() =>
- {
- bLoading = true;
- purchaseOrderShells.Clear();
- filterOptions.Clear();
- filterOptions.Add("All");
- CoreTable table = QueryTable();
- while (table == null)
- table = QueryTable();
- if (table.Rows.Any())
- {
- foreach (CoreRow row in table.Rows)
- {
- List<object> list = row.Values;
- if (list[0] == null) { list[0] = Guid.Empty; } //0
- if (list[1] == null) { list[1] = ""; } //1
- if (list[2] == null) { list[2] = DateTime.MinValue; } //2
- if (list[3] == null) { list[3] = ""; } //3
- if (list[4] == null) { list[4] = Guid.Empty; } //4
- if (list[5] == null) { list[5] = ""; } //5
- if (list[6] == null) { list[6] = ""; } //6
- PurchaseOrderShell purchaseOrderShell = new PurchaseOrderShell()
- {
- ID = Guid.Parse(list[0].ToString()),
- PONumber = list[1].ToString(),
- DueDate = "Due: " + DateTime.Parse(list[2].ToString()).ToString("dd-MMMM-yy"),
- Status = list[3].ToString(),
- SupplierID = Guid.Parse(list[4].ToString()),
- SupplierName = list[5].ToString(),
- Notes = list[6].ToString()
- };
- if (!string.IsNullOrWhiteSpace(purchaseOrderShell.SupplierName))
- {
- if (!filterOptions.Contains(purchaseOrderShell.SupplierName))
- {
- filterOptions.Add(purchaseOrderShell.SupplierName);
- }
- }
- purchaseOrderShells.Add(purchaseOrderShell);
- }
- firstLoad = false;
- Device.BeginInvokeOnMainThread(() =>
- {
- searchEnt.Text = "";
- filterOptionsControl.Options = filterOptions;
- filterOptionsControl.CreateRadioButtonsAndSetDefault(filterOptions.First());
- purchaseOrderListView.ItemsSource = purchaseOrderShells;
- filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged;
- bLoading = false;
- });
- UpdateListWithNumberOfItems();
- }
- });
- }
- private CoreTable QueryTable()
- {
- try
- {
- return new Client<PurchaseOrder>().Query
- (
- new Filter<PurchaseOrder>(x => x.ClosedDate).IsEqualTo(DateTime.MinValue),
- new Columns<PurchaseOrder>(
- x => x.ID, //0
- x => x.PONumber, //1
- x => x.DueDate, //2
- x => x.Status, //3
- x => x.SupplierLink.ID, //4
- x => x.SupplierName, //5
- x => x.Notes //6
- ),
- new SortOrder<PurchaseOrder>(x => x.DueDate, SortDirection.Descending)
- );
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Query, "QueryTable()", ex.Message + ex.StackTrace, this.GetType().Name);
- return null;
- }
- }
- private void UpdateListWithNumberOfItems()
- {
- Task.Run(() =>
- {
- List<PurchaseOrderShell> toAdd = new List<PurchaseOrderShell>();
- foreach (PurchaseOrderShell shell1 in purchaseOrderShells)
- {
- shell1.Items = 0;
- }
- CoreTable table = new Client<PurchaseOrderItem>().Query(new Filter<PurchaseOrderItem>(x => x.ReceivedDate).IsEqualTo(DateTime.MinValue),
- new Columns<PurchaseOrderItem>(x => x.PurchaseOrderLink.ID));
- foreach (CoreRow row in table.Rows)
- {
- if (purchaseOrderShells.Contains(purchaseOrderShells.Find(x => x.ID == Guid.Parse(row.Values[0].ToString()))))
- {
- PurchaseOrderShell shell = purchaseOrderShells.Find(x => x.ID == Guid.Parse(row.Values[0].ToString()));
- shell.Items++;
- shell.NumberOfItems = "Number of Items Unreceived: " + shell.Items;
- if (!toAdd.Contains(shell))
- toAdd.Add(shell);
- }
- }
- purchaseOrderShells.Clear();
- foreach (PurchaseOrderShell shell2 in toAdd)
- {
- purchaseOrderShells.Add(shell2);
- }
- Device.BeginInvokeOnMainThread(() =>
- {
- purchaseOrderListView.ItemsSource = null;
- if (!string.IsNullOrWhiteSpace(searchEnt.Text))
- {
- RunSearch();
- }
- else
- {
- if (filterOptionsControl.CurrentOption == "All")
- {
- purchaseOrderListView.ItemsSource = purchaseOrderShells;
- listCountLbl.Text = "Number of items in list: " + purchaseOrderShells.Count;
- }
- else
- {
- var list = purchaseOrderShells.Where(x => x.SupplierName.Equals(filterOptionsControl.CurrentOption));
- purchaseOrderListView.ItemsSource = list;
- listCountLbl.Text = "Number of items in list: " + list.Count();
- }
- }
- });
- });
- }
- private void FilterOptionsControl_OnFilterOptionChanged(string filterOption)
- {
- if (filterOption == filterOptionsControl.CurrentOption)
- return;
- bLoading = true;
- searchEnt.Text = "";
- bLoading = false;
- filterOptionsControl.CurrentOption = filterOption;
- if (filterOption == "All")
- {
- purchaseOrderListView.ItemsSource = purchaseOrderShells;
- listCountLbl.Text = "Number of items in list: " + purchaseOrderShells.Count;
- }
- else
- {
- var list = purchaseOrderShells.Where(x => x.SupplierName.Equals(filterOption));
- purchaseOrderListView.ItemsSource = list;
- listCountLbl.Text = "Number of items in list: " + list.Count();
- }
- }
- private async void PurchaseOrder_Clicked(object sender, EventArgs e)
- {
- PurchaseOrderShell purchaseOrderShell = purchaseOrderListView.SelectedItem as PurchaseOrderShell;
- Receivals receivalsPage = new Receivals(purchaseOrderShell.ID, purchaseOrderShell.PONumber, purchaseOrderShell.SupplierID);
- Navigation.PushAsync(receivalsPage);
- }
- private void SearchEnt_Changed(object sender, EventArgs e)
- {
- if (bLoading)
- return;
- if (!string.IsNullOrWhiteSpace(searchEnt.Text))
- {
- RunSearch();
- }
- else
- {
- listCountLbl.Text = "Number of items in list: " + purchaseOrderShells.Count;
- purchaseOrderListView.ItemsSource = purchaseOrderShells;
- }
- }
- private async void RunSearch()
- {
- await Task.Run(() =>
- {
- List<PurchaseOrderShell> searchList = new List<PurchaseOrderShell>();
- if (filterOptionsControl.CurrentOption == "All")
- {
- searchList = purchaseOrderShells;
- }
- else
- {
- var newList = purchaseOrderShells.Where(x => x.SupplierName.Equals(filterOptionsControl.CurrentOption));
- foreach (var shell in newList)
- {
- searchList.Add(shell);
- }
- }
- var list = searchList.Where(x =>
- x.PONumber.Contains(searchEnt.Text) || x.PONumber.Contains(UpperCaseFirst(searchEnt.Text)) || x.PONumber.Contains(searchEnt.Text.ToLower()) || x.PONumber.Contains(searchEnt.Text.ToUpper()) ||
- x.Notes.Contains(searchEnt.Text) || x.Notes.Contains(UpperCaseFirst(searchEnt.Text)) || x.Notes.Contains(searchEnt.Text.ToLower()) || x.Notes.Contains(searchEnt.Text.ToUpper()) ||
- x.SupplierName.Contains(searchEnt.Text) || x.SupplierName.Contains(UpperCaseFirst(searchEnt.Text)) || x.SupplierName.Contains(searchEnt.Text.ToLower()) || x.SupplierName.Contains(searchEnt.Text.ToUpper()) ||
- x.Status.Contains(searchEnt.Text) || x.Status.Contains(UpperCaseFirst(searchEnt.Text)) || x.Status.Contains(searchEnt.Text.ToLower()) || x.Status.Contains(searchEnt.Text.ToUpper()) ||
- x.DueDate.Contains(searchEnt.Text) || x.DueDate.Contains(UpperCaseFirst(searchEnt.Text)) || x.DueDate.Contains(searchEnt.Text.ToLower()) || x.DueDate.Contains(searchEnt.Text.ToUpper())
- );
- Device.BeginInvokeOnMainThread(() =>
- {
- listCountLbl.Text = "Number of items in list: " + list.Count();
- purchaseOrderListView.ItemsSource = list;
- });
- });
- }
- static String UpperCaseFirst(string s)
- {
- char[] a = s.ToCharArray();
- a[0] = char.ToUpper(a[0]);
- return new string(a);
- }
- }
- public class PurchaseOrderShell
- {
- public Guid ID { get; set; }
- public string PONumber { get; set; }
- public string DueDate { get; set; }
- public string Status { get; set; }
- public Guid SupplierID { get; set; }
- public string SupplierName { get; set; }
- public string Notes { get; set; }
- public int Items { get; set; }
- public string NumberOfItems { get; set; }
- public PurchaseOrderShell()
- {
- ID = Guid.Empty;
- PONumber = "";
- DueDate = "";
- Status = "";
- SupplierID = Guid.Empty;
- SupplierName = "";
- Notes = "";
- Items = 0;
- NumberOfItems = "";
- }
- }
- }
|