123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using Plugin.Media;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- namespace comal.timesheets
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class StockLocationSelectionPage : ContentPage
- {
- #region Constructor and Fields
- public delegate void LocationSelected(StockLocationShell shell);
- public delegate void MultiLocationSelected(List<StockLocationShell> shells);
- public event LocationSelected OnLocationSelected;
- public event MultiLocationSelected OnMultiLocationSelected;
- Color tealColor = Color.FromHex("#15C7C1");
- Color purpleColor = Color.FromHex("#a2006d");
- List<StockWarehouseShell> stockWarehouses = new List<StockWarehouseShell>();
- List<StockAreaShell> stockAreas = new List<StockAreaShell>();
- List<StockLocationShell> stockLocations = new List<StockLocationShell>();
- List<StockLocationShell> currentList = new List<StockLocationShell>();
- List<StockLocationShell> confirmedList = new List<StockLocationShell>();
- List<Button> buttons = new List<Button>();
- bool multiselect = false;
- public StockLocationSelectionPage(bool _multiselect = false)
- {
- InitializeComponent();
- multiselect = _multiselect;
- if (multiselect)
- {
- Timer timer = new Timer(Timercallback, null, 0, 3000);
- DisplayAlert("Info:", "Multiple selection of locations is enabled.", "OK");
- }
- LoadScreen();
- }
- private async void Timercallback(object o)
- {
- await multiLocationConfirmBtn.TranslateTo(0, -10, 250);
- await multiLocationConfirmBtn.TranslateTo(0, 0, 250);
- await multiLocationConfirmBtn.TranslateTo(0, -10, 250);
- await multiLocationConfirmBtn.TranslateTo(0, 0, 250);
- }
- #endregion
- #region Taps
- private void ResetScreen(object sender, EventArgs e)
- {
- try
- {
- Button button2 = buttons.Find(x => x.BackgroundColor.Equals(purpleColor));
- button2.BackgroundColor = tealColor;
- }
- catch { }
- try
- {
- StockAreaShell shell2 = stockAreas.Find(x => x.Color.Equals(tealColor));
- shell2.Color = Color.Default;
- }
- catch { }
- areaListView.ItemsSource = null;
- areaListView.ItemsSource = stockAreas;
- currentList.Clear();
- confirmedList.Clear();
- multiLocationConfirmBtn.IsVisible = false;
- foreach (StockLocationShell location in stockLocations)
- {
- if (location.Color.Equals(tealColor))
- {
- location.Color = Color.Default;
- }
- currentList.Add(location);
- }
- locationListView.ItemsSource = stockLocations;
- }
- private void WarehouseButton_Clicked(object sender, EventArgs e)
- {
- //handle color change
- try
- {
- Button button2 = buttons.Find(x => x.BackgroundColor.Equals(purpleColor));
- button2.BackgroundColor = tealColor;
- }
- catch { }
- string text = (sender as Button).Text;
- Button button = buttons.Find(x => x.Text == text);
- button.BackgroundColor = purpleColor;
- //check if area button needs to be cleared
- StockAreaShell shell = stockAreas.Find(x => x.Color.Equals(tealColor));
- if (shell != null)
- {
- shell.Color = Color.Default;
- }
- //arealist
- areaSearchEnt.Text = "";
- areaListView.ItemsSource = stockAreas.Where(x => x.WarehouseDescription.Equals(text));
- //locationlist
- locationSearchEnt.Text = "";
- locationListView.ItemsSource = stockLocations.Where(x => x.Warehouse.Contains(text));
- currentList.Clear();
- var list = stockLocations.Where(x => x.Warehouse.Contains(text));
- foreach (StockLocationShell location in list)
- {
- currentList.Add(location);
- }
- }
- private void AreaList_Tapped(object sender, EventArgs e)
- {
- //handle color change
- try
- {
- StockAreaShell shell2 = stockAreas.Find(x => x.Color.Equals(tealColor));
- shell2.Color = Color.Default;
- }
- catch { }
- string description = (areaListView.SelectedItem as StockAreaShell).Description;
- StockAreaShell shell = stockAreas.Find(x => x.Description.Equals(description));
- shell.Color = tealColor;
- //check if warehouse is selected
- Button button = buttons.Find(x => x.BackgroundColor.Equals(purpleColor));
- if (button != null) //warehouse is selected
- {
- if (!string.IsNullOrWhiteSpace(areaSearchEnt.Text))
- {
- AreaSearch();
- }
- else
- {
- areaListView.ItemsSource = null;
- areaListView.ItemsSource = stockAreas.Where(x => x.WarehouseDescription.Contains(button.Text));
- }
- }
- else
- {
- if (!string.IsNullOrWhiteSpace(areaSearchEnt.Text))
- {
- AreaSearch();
- }
- else
- {
- areaListView.ItemsSource = null;
- areaListView.ItemsSource = stockAreas;
- }
- }
- //locationlist
- locationSearchEnt.Text = "";
- locationListView.ItemsSource = stockLocations.Where(x => x.Area.Contains(description));
- currentList.Clear();
- var list = stockLocations.Where(x => x.Area.Contains(description));
- foreach (StockLocationShell location in list)
- {
- currentList.Add(location);
- }
- }
- private void LocationList_Tapped(object sender, EventArgs e)
- {
- StockLocationShell shell = locationListView.SelectedItem as StockLocationShell;
- if (!multiselect)
- {
- OnLocationSelected?.Invoke(shell);
- Navigation.PopAsync();
- }
- else
- {
- StockLocationShell shell1 = currentList.Find(x => x.ID.Equals(shell.ID));
- StockLocationShell shell2 = stockLocations.Find(x => x.ID.Equals(shell.ID));
- if (shell.Color.Equals(tealColor))
- {
- confirmedList.Remove(shell);
- shell1.Color = Color.Default;
- shell2.Color = Color.Default;
- }
- else
- {
- confirmedList.Add(shell);
- shell1.Color = tealColor;
- shell2.Color = tealColor;
- }
- if (string.IsNullOrWhiteSpace(locationSearchEnt.Text))
- {
- locationListView.ItemsSource = null;
- locationListView.ItemsSource = currentList;
- }
- else
- {
- locationListView.ItemsSource = null;
- RunLocationSearch(currentList);
- }
- multiLocationConfirmBtn.Text = "Confirm " + confirmedList.Count + " Location(s)";
- if (confirmedList.Count > 0)
- multiLocationConfirmBtn.IsVisible = true;
- else if (confirmedList.Count == 0)
- multiLocationConfirmBtn.IsVisible = false;
- }
- }
- private void MultiLocationConfirm_Clicked(object sender, EventArgs e)
- {
- OnMultiLocationSelected?.Invoke(confirmedList);
- Navigation.PopAsync();
- }
- #endregion
- #region Loading
- private void LoadScreen()
- {
- LoadWarehouses();
- LoadAreas();
- LoadLocations();
- }
- private void LoadWarehouses()
- {
- Task.Run(() =>
- {
- CoreTable table = new Client<StockWarehouse>().Query(
- new Filter<StockWarehouse>(x => x.Active).IsEqualTo(true),
- new Columns<StockWarehouse>(x => x.Description)
- );
- foreach (CoreRow row in table.Rows)
- {
- List<object> list = row.Values;
- if (list[0] == null) list[0] = "";
- StockWarehouseShell shell = new StockWarehouseShell()
- {
- Description = list[0].ToString(),
- };
- stockWarehouses.Add(shell);
- }
- foreach (StockWarehouseShell shell in stockWarehouses)
- {
- Button button = new Button
- {
- Text = shell.Description,
- TextColor = Color.White,
- BackgroundColor = tealColor,
- CornerRadius = 5,
- Margin = 1,
- FontAttributes = FontAttributes.Bold,
- VerticalOptions = LayoutOptions.Center,
- HorizontalOptions = LayoutOptions.Center,
- Padding = new Thickness(0)
- };
- button.Clicked += WarehouseButton_Clicked;
- buttons.Add(button);
- }
- Device.BeginInvokeOnMainThread(() =>
- {
- foreach (Button button in buttons)
- {
- warehouseFlexLayout.Children.Add(button);
- }
- });
- });
- }
- private void LoadAreas()
- {
- Task.Run(() =>
- {
- CoreTable table = new Client<StockArea>().Query
- (
- new Filter<StockArea>(x => x.Active).IsEqualTo(true),
- new Columns<StockArea>(x => x.Description, x => x.Warehouse.Description),
- new SortOrder<StockArea>(x => x.Description, SortDirection.Ascending)
- );
- foreach (CoreRow row in table.Rows)
- {
- List<object> list = row.Values;
- if (list[0] == null) list[0] = "";
- if (list[1] == null) list[1] = "";
- StockAreaShell shell = new StockAreaShell
- {
- Description = list[0].ToString(),
- WarehouseDescription = list[1].ToString(),
- };
- stockAreas.Add(shell);
- }
- Device.BeginInvokeOnMainThread(() =>
- {
- areaLoadingLbl.IsVisible = false;
- areaListView.ItemsSource = stockAreas;
- });
- });
- }
- private void LoadLocations()
- {
- Task.Run(() =>
- {
- CoreTable table = new Client<StockLocation>().Query(
- new Filter<StockLocation>(x => x.Active).IsEqualTo(true),
- new Columns<StockLocation>(
- x => x.ID, //0
- x => x.Description, //1
- x => x.Code, //2
- x => x.Area.Description, //3
- x => x.Warehouse.Description, //4
- x => x.Job.JobNumber, //5
- x => x.Job.ID, //6
- x => x.Job.Name, //7
- x => x.Holdings
- ));
- 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] = ""; //2
- if (list[3] == null) list[3] = ""; //3
- if (list[4] == null) list[4] = ""; //4
- if (list[5] == null) list[5] = ""; //5
- if (list[6] == null) list[6] = Guid.Empty; //6
- if (list[7] == null) list[7] = ""; //7
- StockLocationShell shell = new StockLocationShell
- {
- ID = Guid.Parse(list[0].ToString()),
- Description = list[1].ToString(),
- Code = list[2].ToString(),
- Area = "Area: " + list[3].ToString(),
- Warehouse = "Warehouse: " + list[4].ToString(),
- JobNumber = "Job: " + list[5].ToString(),
- JobID = Guid.Parse(list[6].ToString()),
- JobName = list[7].ToString(),
- NumberOfHoldings = "Holdings: " + row.Get<StockLocation, double>(x => x.Holdings)
- };
- stockLocations.Add(shell);
- currentList.Add(shell);
- }
- Device.BeginInvokeOnMainThread(() =>
- {
- locationLoadingLbl.IsVisible = false;
- locationListView.ItemsSource = stockLocations;
- });
- });
- //Task.Run(() =>
- //{
- // CoreTable table = new Client<StockLocation>().Query(
- // new Filter<StockLocation>(x => x.Active).IsEqualTo(true),
- // new Columns<StockLocation>(
- // x => x.ID, //0
- // x => x.Holdings //1
- // ));
- // foreach (CoreRow row in table.Rows)
- // {
- // Guid id = row.Get<StockLocation, Guid>(x => x.ID);
- // double holdings = row.Get<StockLocation, double>(x => x.Holdings);
- // StockLocationShell shell = stockLocations.Find(x => x.ID == id);
- // if (shell != null)
- // shell.NumberOfHoldings = "Holdings: " + holdings;
- // }
- // foreach (StockLocationShell shell in currentList)
- // {
- // StockLocationShell shell2 = stockLocations.Find(x => x.ID.Equals(shell.ID));
- // shell.NumberOfHoldings = shell2.NumberOfHoldings;
- // }
- // Device.BeginInvokeOnMainThread(() =>
- // {
- // if (!string.IsNullOrWhiteSpace(locationSearchEnt.Text))
- // {
- // locationListView.ItemsSource = null;
- // RunLocationSearch(currentList);
- // }
- // else
- // {
- // locationListView.ItemsSource = null;
- // locationListView.ItemsSource = currentList;
- // }
- // });
- //});
- }
- #endregion
- #region Searching
- private void AreaSearchEnt_Changed(object sender, EventArgs e)
- {
- AreaSearch();
- }
- private void AreaSearch()
- {
- //check if warehouse is selected
- Button button = buttons.Find(x => x.BackgroundColor.Equals(purpleColor));
- if (button != null) //warehouse is selected
- {
- var list = stockAreas.Where(x => x.WarehouseDescription.Contains(button.Text));
- List<StockAreaShell> searchList = new List<StockAreaShell>();
- foreach (var area in list)
- {
- searchList.Add(area);
- }
- RunAreaSearch(searchList);
- }
- else
- {
- RunAreaSearch(stockAreas);
- }
- }
- private void RunAreaSearch(List<StockAreaShell> list)
- {
- string text = areaSearchEnt.Text;
- areaListView.ItemsSource = list.Where(x =>
- x.Description.Contains(text) || x.Description.Contains(UpperCaseFirst(text)) ||
- x.Description.Contains(text.ToUpper()) || x.Description.Contains(text.ToLower())
- );
- }
- static String UpperCaseFirst(string s)
- {
- char[] a = s.ToCharArray();
- a[0] = char.ToUpper(a[0]);
- return new string(a);
- }
- private void LocationSearchEnt_Changed(object sender, EventArgs e)
- {
- StockAreaShell shell = stockAreas.Find(x => x.Color.Equals(tealColor));
- List<StockLocationShell> searchList = new List<StockLocationShell>();
- //check if warehouse is selected
- Button button = buttons.Find(x => x.BackgroundColor.Equals(purpleColor));
- if (button != null) //warehouse is selected
- {
- if (shell != null) //area is also selected
- {
- var list = stockLocations.Where(x => x.Area.Contains(shell.Description));
- foreach (var location in list)
- {
- searchList.Add(location);
- }
- RunLocationSearch(searchList);
- }
- else //only warehouse is selected
- {
- var list = stockLocations.Where(x => x.Warehouse.Contains(button.Text));
- foreach (var location in list)
- {
- searchList.Add(location);
- }
- RunLocationSearch(searchList);
- }
- }
- else if (shell != null) //only area is selected
- {
- var list = stockLocations.Where(x => x.Area.Contains(shell.Description));
- foreach (var location in list)
- {
- searchList.Add(location);
- }
- RunLocationSearch(searchList);
- }
- else
- {
- RunLocationSearch(stockLocations);
- }
- }
- private void RunLocationSearch(List<StockLocationShell> list)
- {
- string text = locationSearchEnt.Text;
- locationListView.ItemsSource = list.Where(x =>
- x.Description.Contains(text) || x.Description.Contains(UpperCaseFirst(text)) ||
- x.Description.Contains(text.ToLower()) || x.Description.Contains(text.ToUpper())
- );
- }
- #endregion
- }
- #region Shell Classes
- public class StockWarehouseShell
- {
- public string Description { get; set; }
- public StockWarehouseShell()
- {
- Description = "";
- }
- }
- public class StockAreaShell
- {
- public string Description { get; set; }
- public string WarehouseDescription { get; set; }
- public Color Color { get; set; }
- public StockAreaShell()
- {
- Description = "";
- WarehouseDescription = "";
- Color = Color.Default;
- }
- }
- #endregion
- }
|