| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.Design;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Comal.Classes;
- using InABox.Core;
- using InABox.Mobile;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using ZXing;
- namespace PRS.Mobile
- {
- public class RequisitionItemScannerHoldingMatchConverter : AbstractConverter<StockHoldingShell, Color>
- {
- public RequisitionItemScannerViewModel ViewModel { get; set; }
-
- protected override Color Convert(StockHoldingShell? value, object? parameter = null)
- {
- bool bMatched = false;
- if (value != null)
- {
- if (ViewModel.SaveArgs != null)
- bMatched = value.ID == ViewModel.SaveArgs.Holding.ID;
- else if (ViewModel.Item != null)
- {
- bMatched =
- value.LocationID == ViewModel.Item.LocationID
- && value.ProductID == ViewModel.Item.ProductID
- && value.StyleID == ViewModel.Item.StyleID
- && value.DimensionsUnitID == ViewModel.Item.DimensionsUnitID
- && value.DimensionsUnitSize == ViewModel.Item.DimensionsUnitSize;
- }
- }
- return bMatched ? Color.LightGreen : Color.LightYellow;
- }
- }
-
- public class RequisitionItemScannerViewModel : BindableObject
- {
- private RequisitionItemShell? _item;
- private RequisitionItemScannerSaveArgs? _saveArgs;
- public RequisitionItemShell? Item
- {
- get => _item;
- set
- {
- if (Equals(value, _item)) return;
- _item = value;
- OnPropertyChanged();
- OnPropertyChanged(nameof(HasItem));
- }
- }
- public bool HasItem => Item != null;
- public RequisitionItemScannerSaveArgs? SaveArgs
- {
- get => _saveArgs;
- set
- {
- if (Equals(value, _saveArgs)) return;
- _saveArgs = value;
- OnPropertyChanged();
- OnPropertyChanged(nameof(HasSaveArgs));
- }
- }
- public bool HasSaveArgs => SaveArgs != null;
-
- public StockHoldingModel Holdings { get; private set; }
- public String FoundItem { get; set; }
- public RequisitionItemScannerViewModel()
- {
- Holdings = new StockHoldingModel(App.Data, () => new Filter<StockHolding>().None());
- }
- }
- public class RequisitionItemScannerSaveArgs : EventArgs
- {
-
- public StockHoldingShell Holding { get; private set; }
-
- public Guid JobID { get; private set; }
-
- public Guid JobRequisitionItemID { get; private set; }
- public double Quantity { get; set; }
- public RequisitionItemScannerSaveArgs(StockHoldingShell holding, Guid jobid, Guid jobrequisitionitemid, double quantity)
- {
- Holding = holding;
- JobID = jobid;
- JobRequisitionItemID = jobrequisitionitemid;
- Quantity = quantity;
- }
- }
-
- public delegate void RequisitionItemScannerSaveEvent(object sender, RequisitionItemScannerSaveArgs args);
-
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class RequisitionItemScanner : MobilePage
- {
-
- public event RequisitionItemScannerSaveEvent? OnSave;
- public RequisitionItemScanner(RequisitionItemShell? item)
- {
- InitializeComponent();
-
- HoldingMatchConverter.ViewModel = _viewModel;
-
- _viewModel.Item = item;
- 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 = false;
- _scanView.IsScanning = true;
- if ((item?.ProductID ?? Guid.Empty) != Guid.Empty)
- SearchProductID(item!.ProductID);
- }
- protected override void OnAppearing()
- {
- base.OnAppearing();
- _scanView.IsAnalyzing = true;
- }
- protected override void OnDisappearing()
- {
- _scanView.IsAnalyzing = false;
- base.OnDisappearing();
- }
- private bool bProcessing = false;
-
- private void ScanView_OnScanResult(ZXing.Result result)
- {
- if (bProcessing)
- return;
-
- if (!Guid.TryParse(result.Text, out Guid id) || id == Guid.Empty)
- {
- _viewModel.Holdings.Filter =
- () => new Filter<StockHolding>().None();
- _viewModel.Holdings.Refresh(true);
- return;
- }
-
- bProcessing = true;
- Vibration.Vibrate();
-
- var producttask = Task.Run(() => { SearchProductID(id); });
- var locationtask = Task.Run(() => { SearchLocationID(id); });
- Task.WaitAll(producttask,locationtask);
-
- bProcessing = false;
- }
- private void SearchLocationID(Guid id)
- {
- App.Data.StockLocations.Refresh(false);
- var location = App.Data.StockLocations.FirstOrDefault(x => Guid.Equals(x.ID,id));
- if (location != null)
- {
- _viewModel.Holdings.Filter = () =>
- new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(location.ID);
- _viewModel.Holdings.Refresh(true,
- () => Dispatcher.BeginInvokeOnMainThread(() =>
- {
- _items.ItemTemplate = _locationTemplate;
- _viewModel.FoundItem = location.Description;
- })
- );
- }
- }
- private void SearchProductID(Guid id)
- {
- App.Data.Products.Refresh(false);
- var product = App.Data.Products.FirstOrDefault(x => Guid.Equals(x.ID,id));
- if (product != null)
- {
- _viewModel.Holdings.Filter =
- () => new Filter<StockHolding>(x => x.Product.ID).IsEqualTo(product.ID);
- _viewModel.Holdings.Refresh(true,
- () => Dispatcher.BeginInvokeOnMainThread(() =>
- {
- _items.ItemTemplate = _productTemplate;
- _viewModel.FoundItem = product.Name;
- })
- );
- }
- }
- private void ProductSearch_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- ShowPopup(() => SelectionView.Execute<ProductShell>(
- columns =>
- {
- columns.Add(new MobileGridTextColumn<ProductShell>()
- { Column = x => x.Code, Width = 100 });
- columns.Add(new MobileGridTextColumn<ProductShell>()
- { Column = x => x.Name, Width = GridLength.Star });
- },
- refresh => App.Data.Products.Refresh(false),
- products =>
- {
- SearchProductID(products.FirstOrDefault()?.ID ?? Guid.Empty);
- DismissPopup();
- })
- );
- }
- private void LocationSearch_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- ShowPopup(() => SelectionView.Execute<StockLocationShell>(
- columns =>
- {
- columns.Add(new MobileGridTextColumn<StockLocationShell>()
- { Column = x => x.Code, Width = 100 });
- columns.Add(new MobileGridTextColumn<StockLocationShell>()
- { Column = x => x.Description, Width = GridLength.Star });
- },
- refresh => App.Data.StockLocations.Refresh(false),
- locations =>
- {
- SearchLocationID(locations.FirstOrDefault()?.ID ?? Guid.Empty);
- DismissPopup();
- })
- );
- }
- private void SelectFromHolding_Click(object sender, EventArgs e)
- {
- var holding = (sender as MobileCard)?.BindingContext as StockHoldingShell;
- if (holding == null)
- return;
- ShowPopup(() =>
- {
- var editor = new SelectFromHoldingView(holding, _viewModel.Item?.Quantity ?? 0.0F);
- editor.OnSave += (o, args) =>
- {
- if (!_viewModel.HasItem)
- OnSave?.Invoke(this, args);
- else
- _viewModel.SaveArgs = args;
- _viewModel.Holdings.Refresh(false);
- DismissPopup();
- };
- editor.OnCancel += (o, args) => DismissPopup();
- return editor;
- },
- new PopupManagerConfiguration()
- {
- RequestedHeight = Math.Min(700,500 + (holding.Allocations.Length * 55))
- });
- }
- private void SaveItem_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
- {
- if (_viewModel.HasSaveArgs)
- {
- OnSave?.Invoke(this, _viewModel.SaveArgs);
- Navigation.PopAsync();
- }
- }
- }
- }
|