123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666 |
- using comal.timesheets.StoreRequis;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- using static comal.timesheets.RequiItems;
- namespace comal.timesheets
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class StoreRequiScannerPage : ContentPage
- {
- #region Fields / Constructor
- public delegate bool OnScanEvent(object sender, string barcode);
- public event OnScanEvent OnScan;
- List<StoreRequiItemShell> shells = new List<StoreRequiItemShell>();
- List<StoreRequiItemShell> oldShells = new List<StoreRequiItemShell>();
- Requisition requisition = new Requisition();
- bool loading = false;
- Dictionary<StoreRequiItemShell, string> itemRowScannerRawResultPairs = new Dictionary<StoreRequiItemShell, string>();
- Dictionary<StoreRequiItemShell, string> itemRowScannerProcessedResultPairs = new Dictionary<StoreRequiItemShell, string>();
- bool firstLoad = true;
- bool containsNotes = false;
- public StoreRequiScannerPage(Guid requiID)
- {
- InitializeComponent();
- ConfigAll(requiID);
- }
- #endregion
- #region Config
- void ConfigAll(Guid requiID)
- {
- NavigationPage.SetHasBackButton(this, false);
- SetScannerOptions();
- if (requiID != Guid.Empty)
- {
- requisition.ID = requiID;
- LoadExistingRequi();
- }
- if (!HoldingsLoaded)
- {
- Timer t = null;
- t = new Timer(new TimerCallback((object o) =>
- {
- if (HoldingsLoaded)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ConfigDisplay();
- });
- t.Dispose();
- }
- }), null, 0, 500);
- }
- else
- {
- loadingLbl.IsVisible = false;
- addBtn.IsEnabled = true;
- Task.Run(() =>
- {
- Thread.Sleep(1500);
- Device.BeginInvokeOnMainThread(() => { ConfigDisplay(); });
- });
- }
- }
- void SetScannerOptions()
- {
- var options = new ZXing.Mobile.MobileBarcodeScanningOptions()
- {
- PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE },
- AutoRotate = false,
- TryInverted = true,
- TryHarder = true,
- };
- _scanView.Options = options;
- _scanView.IsAnalyzing = true;
- _scanView.IsScanning = true;
- _scanView.AutoFocus();
- _scanView.OnScanResult += ScanView_OnScanResult;
- }
- protected override void OnAppearing()
- {
- base.OnAppearing();
- if (!firstLoad)
- _scanView.IsAnalyzing = true;
- }
- protected override void OnDisappearing()
- {
- _scanView.IsAnalyzing = false;
- base.OnDisappearing();
- }
- async void LoadExistingRequi()
- {
- await Task.Run(() =>
- {
- requisition = new Client<Requisition>().Query(
- new Filter<Requisition>(x => x.ID).IsEqualTo(requisition.ID)
- ).Rows.FirstOrDefault().ToObject<Requisition>();
- if (!string.IsNullOrWhiteSpace(requisition.Request))
- {
- StoreRequiItemShell shell1 = new StoreRequiItemShell()
- {
- IsNotes = true,
- IsNotNotes = false,
- Summary = requisition.Request,
- BorderColor = Color.FromHex("#9f4576")
- };
- shells.Insert(0, shell1);
- containsNotes = true;
- }
- });
- await Task.Run(() =>
- {
- CoreTable table = new Client<RequisitionItem>().Query
- (
- new Filter<RequisitionItem>(x => x.RequisitionLink.ID).IsEqualTo(requisition.ID),
- new Columns<RequisitionItem>(
- x => x.ID,
- x => x.Product.ID,
- x => x.Product.Name,
- x => x.Product.Code,
- x => x.Quantity,
- x => x.Location.ID,
- x => x.Location.Description,
- x => x.Picked
- )
- );
- if (table.Rows.Any())
- {
- Device.BeginInvokeOnMainThread(() => { saveBtn.IsVisible = true; });
- foreach (CoreRow row in table.Rows)
- {
- StoreRequiItemShell shell = new StoreRequiItemShell()
- {
- ID = row.Get<RequisitionItem, Guid>(x => x.ID),
- ProductID = row.Get<RequisitionItem, Guid>(x => x.Product.ID),
- ProductName = row.Get<RequisitionItem, string>(x => x.Product.Name),
- ProductCode = row.Get<RequisitionItem, string>(x => x.Product.Code),
- Quantity = row.Get<RequisitionItem, double>(x => x.Quantity),
- LocationID = row.Get<RequisitionItem, Guid>(x => x.Location.ID),
- LocationName = row.Get<RequisitionItem, string>(x => x.Location.Description),
- Picked = row.Get<RequisitionItem, DateTime>(x => x.Picked),
- };
- shells.Add(shell);
- oldShells.Add(shell);
- }
- }
- Device.BeginInvokeOnMainThread(() =>
- {
- requiItemListView.ItemsSource = shells;
- if (containsNotes)
- {
- countLbl.Text = "Number of items: " + (shells.Count - 1);
- }
- else
- {
- countLbl.Text = "Number of items: " + shells.Count;
- }
- });
- });
- }
- void ConfigDisplay()
- {
- loadingLbl.IsVisible = false;
- addBtn.IsEnabled = true;
- double width = scannerGrid.Width / 6;
- double height = scannerGrid.Height / 6;
- lblv1.WidthRequest = height / 2;
- lblh1.IsVisible = true;
- lblv1.IsVisible = true;
- lblv2.WidthRequest = height / 2;
- lblh2.IsVisible = true;
- lblv2.IsVisible = true;
- lblv3.WidthRequest = height / 2;
- lblh3.IsVisible = true;
- lblv3.IsVisible = true;
- lblv4.WidthRequest = height / 2;
- lblh4.IsVisible = true;
- lblv4.IsVisible = true;
- scannerGrid.RaiseChild(lblv1);
- scannerGrid.RaiseChild(lblh1);
- scannerGrid.RaiseChild(lblv2);
- scannerGrid.RaiseChild(lblh2);
- scannerGrid.RaiseChild(lblv3);
- scannerGrid.RaiseChild(lblh3);
- scannerGrid.RaiseChild(lblv4);
- scannerGrid.RaiseChild(lblh4);
- firstLoad = false;
- }
- #endregion
- #region Scanning
- private void ScanView_OnScanResult(ZXing.Result result)
- {
- Device.BeginInvokeOnMainThread(async () =>
- {
- if (!loading)
- {
- loading = true;
- if (RequiItems.HoldingsLoaded)
- {
- bool bOK = true;
- if (OnScan != null)
- bOK = OnScan(this, result.Text);
- if (bOK)
- {
- if (!itemRowScannerRawResultPairs.Values.Contains(result.Text))
- {
- if (!itemRowScannerProcessedResultPairs.Values.Contains(result.Text))
- {
- Vibration.Vibrate();
- var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
- player.Load("requiitemadded.mp3");
- player.Play();
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding"))
- {
- string rawResult = result.Text;
- Tuple<string, double> tuple = ProcessResult(result.Text);
- LoadProduct(tuple, rawResult);
- }
- }
- else
- {
- loading = false;
- }
- }
- else
- {
- loading = false;
- }
- }
- else
- {
- loading = false;
- }
- }
- else
- {
- loading = false;
- }
- }
- });
- }
- private Tuple<string, double> ProcessResult(string result)
- {
- double qty = 1;
- if (result.Contains("*"))
- {
- try
- {
- int i = result.IndexOf("*");
- string remainder = result.Substring(i);
- result = result.Remove(i);
- string s1 = remainder.Substring(1);
- qty = Convert.ToDouble(s1);
- }
- catch
- {
- loading = false;
- }
- }
- Tuple<string, double> tuple = new Tuple<string, double>(result, qty);
- return tuple;
- }
- private void LoadProduct(Tuple<string, double> processedResultQtyTuple, string rawResult)
- {
- Device.BeginInvokeOnMainThread(async () =>
- {
- //lookup product in productshells cache
- ProductShell product = GlobalVariables.ProductShells.Find(x => x.Code.Equals(processedResultQtyTuple.Item1));
- //lookup holding for product in holdings cache
- try
- {
- var list = CreateHoldingsList(product.ID);
- if (list.Count == 1) //one stockholding - auto choose holding
- AddStoreRequiItemShell(list.First(), product, rawResult, processedResultQtyTuple);
- else if (list.Count > 1) //more than one stockholding - user choose shelf
- UserSelectFromList(list, product, rawResult, processedResultQtyTuple);
-
- else if (list.Count == 0)
- DisplayAlert("No Holdings Found for Product", "", "OK");
-
- loading = false;
- }
- catch (Exception e)
- {
- DisplayAlert("Error", e.Message, "OK");
- loading = false;
- return;
- }
- });
- }
- private void UserSelectFromList(List<StoreRequiIHoldingShell> list, ProductShell product, string rawResult, Tuple<string, double> processedResultQtyTuple)
- {
- Dictionary<string, Guid> holdingLocationIDs = new Dictionary<string, Guid>();
- foreach (StoreRequiIHoldingShell holding in list)
- {
- if (!holdingLocationIDs.ContainsKey(holding.LocationName))
- holdingLocationIDs.Add(holding.LocationName, holding.LocationID);
- }
- List<string> options = holdingLocationIDs.Keys.ToList();
- ListSelectionPage page = new ListSelectionPage(options);
- page.OnSimpleListTapped += (locationName) =>
- {
- AddStoreRequiItemShell(list.Find(x => x.LocationName == locationName), product, rawResult, processedResultQtyTuple);
- };
- Navigation.PushAsync(page);
- }
- private List<StoreRequiIHoldingShell> CreateHoldingsList(Guid productID)
- {
- List<StoreRequiIHoldingShell> list = new List<StoreRequiIHoldingShell>();
- CoreTable table = DoHoldingsQuery(productID);
- foreach (CoreRow row in table.Rows)
- {
- if (row.Get<StockHolding, double>(x => x.Units) == 0.0)
- continue;
-
- list.Add(CreateHoldingShell(row));
- }
- return list;
- }
- private CoreTable DoHoldingsQuery(Guid productID)
- {
- return new Client<StockHolding>().Query(
- new Filter<StockHolding>(x => x.Product.ID).IsEqualTo(productID),
- new Columns<StockHolding>(
- x => x.ID,
- x => x.Product.ID,
- x => x.Location.ID,
- x => x.Location.Description,
- x => x.Units,
- x => x.Job.ID,
- x => x.Job.JobNumber,
- x => x.Job.Name,
- x => x.Style.ID,
- x => x.Style.Code,
- x => x.Style.Description,
- x => x.Dimensions.Unit.ID,
- x => x.Dimensions.Unit.HasQuantity,
- x => x.Dimensions.Unit.HasLength,
- x => x.Dimensions.Unit.HasHeight,
- x => x.Dimensions.Unit.HasWeight,
- x => x.Dimensions.Unit.HasWidth,
- x => x.Dimensions.Quantity,
- x => x.Dimensions.Length,
- x => x.Dimensions.Height,
- x => x.Dimensions.Weight,
- x => x.Dimensions.Width,
- x => x.Dimensions.Unit.Format,
- x => x.Dimensions.Unit.Formula,
- x => x.Dimensions.UnitSize
- )
- );
- }
- private StoreRequiIHoldingShell CreateHoldingShell(CoreRow row)
- {
- StoreRequiIHoldingShell holding = new StoreRequiIHoldingShell()
- {
- ProductID = row.Get<StockHolding, Guid>(x => x.Product.ID),
- LocationID = row.Get<StockHolding, Guid>(x => x.Location.ID),
- LocationName = row.Get<StockHolding, string>(x => x.Location.Description),
- Units = row.Get<StockHolding, double>(x => x.Units).ToString(),
- JobID = row.Get<StockHolding, Guid>(x => x.Job.ID),
- JobNumber = row.Get<StockHolding, string>(x => x.Job.JobNumber),
- JobName = row.Get<StockHolding, string>(x => x.Job.Name),
- StyleID = row.Get<StockHolding, Guid>(x => x.Style.ID),
- StyleCode = row.Get<StockHolding, string>(x => x.Style.Code),
- StyleDescription = row.Get<StockHolding, string>(x => x.Style.Description)
- };
- holding.Dimensions.Unit.ID = row.Get<StockHolding, Guid>(x => x.Dimensions.Unit.ID);
- holding.Dimensions.Unit.HasQuantity = row.Get<StockHolding, bool>(x => x.Dimensions.Unit.HasQuantity);
- holding.Dimensions.Unit.HasLength = row.Get<StockHolding, bool>(x => x.Dimensions.Unit.HasLength);
- holding.Dimensions.Unit.HasHeight = row.Get<StockHolding, bool>(x => x.Dimensions.Unit.HasHeight);
- holding.Dimensions.Unit.HasWeight = row.Get<StockHolding, bool>(x => x.Dimensions.Unit.HasWeight);
- holding.Dimensions.Unit.HasWidth = row.Get<StockHolding, bool>(x => x.Dimensions.Unit.HasWidth);
- holding.Dimensions.Quantity = row.Get<StockHolding, double>(x => x.Dimensions.Quantity);
- holding.Dimensions.Length = row.Get<StockHolding, double>(x => x.Dimensions.Length);
- holding.Dimensions.Height = row.Get<StockHolding, double>(x => x.Dimensions.Height);
- holding.Dimensions.Weight = row.Get<StockHolding, double>(x => x.Dimensions.Weight);
- holding.Dimensions.Width = row.Get<StockHolding, double>(x => x.Dimensions.Width);
- holding.Dimensions.Unit.Format = row.Get<StockHolding, string>(x => x.Dimensions.Unit.Format);
- holding.Dimensions.Unit.Formula = row.Get<StockHolding, string>(x => x.Dimensions.Unit.Formula);
- holding.Dimensions.UnitSize = row.Get<StockHolding, string>(x => x.Dimensions.UnitSize);
- holding.LocationName = holding.LocationName + " (Units: " + holding.Units + ")" +
- Environment.NewLine + "Style: " + holding.StyleDescription
- + Environment.NewLine + "Job: " + holding.JobNumber
- + Environment.NewLine + "Size: " + holding.Dimensions.UnitSize;
- return holding;
- }
- private void AddStoreRequiItemShell(StoreRequiIHoldingShell holding, ProductShell product, string rawResult, Tuple<string, double> processedResultQtyTuple)
- {
- StoreRequiItemShell shell = new StoreRequiItemShell
- {
- ProductID = product.ID,
- ProductName = product.Name,
- ProductCode = product.Code,
- Quantity = 1,
- LocationID = holding.LocationID,
- LocationName = holding.LocationName,
- StyleID = holding.StyleID,
- JobID = holding.StyleID
- };
- shell.Dimensions.Unit.ID = holding.Dimensions.Unit.ID;
- shell.Dimensions.Unit.HasQuantity = holding.Dimensions.Unit.HasQuantity;
- shell.Dimensions.Unit.HasLength = holding.Dimensions.Unit.HasLength;
- shell.Dimensions.Unit.HasHeight = holding.Dimensions.Unit.HasHeight;
- shell.Dimensions.Unit.HasWeight = holding.Dimensions.Unit.HasWeight;
- shell.Dimensions.Unit.HasWidth = holding.Dimensions.Unit.HasWidth;
- shell.Dimensions.Quantity = holding.Dimensions.Quantity;
- shell.Dimensions.Length = holding.Dimensions.Length;
- shell.Dimensions.Height = holding.Dimensions.Height;
- shell.Dimensions.Weight = holding.Dimensions.Weight;
- shell.Dimensions.Width = holding.Dimensions.Width;
- shell.Dimensions.Unit.Format = holding.Dimensions.Unit.Format;
- shell.Dimensions.Unit.Formula = holding.Dimensions.Unit.Formula;
- shell.Dimensions.UnitSize = holding.Dimensions.UnitSize;
- shells.Add(shell);
- itemRowScannerRawResultPairs.Add(shell, rawResult);
- itemRowScannerProcessedResultPairs.Add(shell, processedResultQtyTuple.Item1);
- requiItemListView.ItemsSource = null;
- requiItemListView.ItemsSource = shells;
- countLbl.IsVisible = true;
- if (containsNotes)
- {
- countLbl.Text = "Number of items: " + (shells.Count - 1);
- }
- else
- {
- countLbl.Text = "Number of items: " + shells.Count;
- }
- saveBtn.IsVisible = true;
- loading = false;
- }
- #endregion
- #region Button Presses
- private async void ExitBtn_Clicked(object sender, EventArgs e)
- {
- if (shells.Count > 0)
- {
- if (containsNotes && shells.Count > 1)
- {
- string chosenOption = await DisplayActionSheet("Leave without saving?", "Cancel", null, "Yes", "No");
- switch (chosenOption)
- {
- case "Cancel":
- return;
- case "Yes":
- Navigation.PopAsync();
- break;
- case "No":
- return;
- default:
- return;
- }
- }
- else if (containsNotes && shells.Count == 1)
- {
- Navigation.PopAsync();
- }
- else
- {
- string chosenOption = await DisplayActionSheet("Leave without saving?", "Cancel", null, "Yes", "No");
- switch (chosenOption)
- {
- case "Cancel":
- return;
- case "Yes":
- Navigation.PopAsync();
- break;
- case "No":
- return;
- default:
- return;
- }
- }
- }
- else
- Navigation.PopAsync();
- }
- void SaveBtn_Clicked(object sender, EventArgs e)
- {
- StoreRequiConfirmationPage page = new StoreRequiConfirmationPage(requisition, shells, oldShells);
- page.OnSaveSelected += () => { Navigation.PopAsync(); };
- Navigation.PushAsync(page);
- }
- private async void RequiItem_Tapped(object sender, EventArgs e)
- {
- var shell = requiItemListView.SelectedItem as StoreRequiItemShell;
- if (shell == null) return;
- await RequiItemTappedAsync(shell);
- }
- private async Task RequiItemTappedAsync(StoreRequiItemShell shell)
- {
- string pickstatus = shell.Picked == DateTime.MinValue ? "not picked" : "picked (" + shell.Picked.ToString("dd MMM yy") + ")";
- string options = shell.Picked == DateTime.MinValue ? ". Mark as Picked?" : ". Remove Picked status?";
- string chosenOption = await DisplayActionSheet("Line is " + pickstatus + options, "Cancel", null, "Yes", "No");
- if (chosenOption != "Yes")
- return;
- shell.Picked = shell.Picked == DateTime.MinValue ? DateTime.Today : DateTime.MinValue;
- shell.Colour = shell.Picked == DateTime.MinValue ? Color.Default : Color.FromHex("#8fbc8f");
- requiItemListView.ItemsSource = null;
- requiItemListView.ItemsSource = shells;
- }
- void ReduceQtyBtn_Clicked(object sender, EventArgs e)
- {
- var shell = ((TappedEventArgs)e).Parameter as StoreRequiItemShell;
- if (shell == null) return;
- if (shell.Quantity <= 1)
- {
- shells.Remove(shell);
- itemRowScannerRawResultPairs.Remove(shell);
- itemRowScannerProcessedResultPairs.Remove(shell);
- if (containsNotes)
- {
- countLbl.Text = "Number of items: " + (shells.Count - 1);
- if (shells.Count == 1)
- {
- saveBtn.IsVisible = false;
- }
- }
- else
- {
- countLbl.Text = "Number of items: " + shells.Count;
- if (shells.Count == 0)
- {
- saveBtn.IsVisible = false;
- }
- }
- }
- else
- {
- shell.Quantity--;
- }
- requiItemListView.ItemsSource = null;
- requiItemListView.ItemsSource = shells;
- }
- void IncreaseQtyBtn_Clicked(object sender, EventArgs e)
- {
- var shell = ((TappedEventArgs)e).Parameter as StoreRequiItemShell;
- if (shell == null)
- return;
- shell.Quantity++;
- requiItemListView.ItemsSource = null;
- requiItemListView.ItemsSource = shells;
- }
- void AddItem_Clicked(object sender, EventArgs e)
- {
- if (GlobalVariables.ProductsLoaded)
- {
- if (loading)
- return;
- loading = true;
- ProductList products = new ProductList(GlobalVariables.ProductShells, true);
- products.OnProductSelected += () =>
- {
- Tuple<string, double> tuple = new Tuple<string, double>(products.SelectedProduct.Code, 1);
- LoadProduct(new Tuple<string, double>(products.SelectedProduct.Code, 1), products.SelectedProduct.Code);
- };
- Navigation.PushAsync(products);
- }
- }
- void Qty_Changed(object sender, EventArgs e)
- {
- }
- #endregion
- }
- [DoNotPersist]
- public class StoreRequiItemShell : Entity
- {
- public Guid ProductID { get; set; }
- public string ProductCode { get; set; }
- public string ProductName { get; set; }
- public string LocationName { get; set; }
- public double Quantity { get; set; }
- public Guid LocationID { get; set; }
- public Color BorderColor { get; set; }
- public bool IsNotes { get; set; }
- public bool IsNotNotes { get; set; }
- public string Summary { get; set; }
- public DateTime Picked { get; set; }
- public Color Colour { get; set; }
- public Guid JobID { get; set; }
- public Guid StyleID { get; set; }
- public StockDimensions Dimensions { get; set; }
- public StoreRequiItemShell()
- {
- ProductID = Guid.Empty;
- ProductCode = "";
- ProductName = "";
- LocationName = "";
- Quantity = 0;
- ID = Guid.Empty;
- LocationID = Guid.Empty;
- BorderColor = Color.FromHex("#15C7C1");
- IsNotes = false;
- IsNotNotes = true;
- Picked = DateTime.MinValue;
- Colour = Color.Default;
- JobID = Guid.Empty;
- StyleID = Guid.Empty;
- Dimensions = new StockDimensions(() => this);
- }
- }
- }
|