using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using InABox.Core; using InABox.Mobile; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace PRS.Mobile { public class SelectFromHoldingAllocation : BindableObject { private bool _selected; public bool Selected { get => _selected; set { if (value == _selected) return; _selected = value; OnPropertyChanged(); OnPropertyChanged(nameof(SelectFromHoldingViewModel.HasAllocation)); } } public Guid JobID { get; set; } public Guid JobRequisitionItemID { get; set; } public String Description { get; set; } public double Quantity { get; set; } } public class SelectFromHoldingViewModel : BindableObject { public RequisitionItemModel? RequisitionItems { get; set; } private StockHoldingShell? _holding; private bool bChanging; public StockHoldingShell? Holding { get => _holding; set { if (Equals(value, _holding)) return; _holding = value; Allocations.Clear(); if (_holding != null) { foreach (var alloc in _holding.Allocations) { var picked = RequisitionItems?.Items.Where(x => x.LocationID == _holding.LocationID && x.ProductID == _holding.ProductID && x.StyleID == _holding.StyleID && x.JobID == _holding.JobID && x.DimensionsLength.IsEffectivelyEqual(_holding.DimensionsLength) && x.DimensionsWidth.IsEffectivelyEqual(_holding.DimensionsWidth) && x.DimensionsHeight.IsEffectivelyEqual(_holding.DimensionsHeight) && x.DimensionsWeight .IsEffectivelyEqual(_holding.DimensionsWeight) && x.DimensionsQuantity.IsEffectivelyEqual(_holding.DimensionsQuantity) && x.ID == alloc.ID) .Sum(x => x.ActualQuantity) ?? 0.0; if (!alloc.Maximum.IsEffectivelyGreaterThan(picked)) continue; var allocation = new SelectFromHoldingAllocation() { JobID = alloc.JobID, JobRequisitionItemID = alloc.ID, Description = alloc.Description, Quantity = alloc.Maximum - picked }; allocation.PropertyChanged += (sender, args) => { if (bChanging) return; if (args.PropertyName.Equals(nameof(SelectFromHoldingAllocation.Selected))) { bChanging = true; foreach (var a in Allocations) a.Selected = a == sender; bChanging = false; OnPropertyChanged(nameof(HasAllocation)); } Quantity = Math.Max(0.0F,Math.Min(RequestedQuantity,(sender as SelectFromHoldingAllocation)?.Quantity ?? 0.0F)); }; Allocations.Add(allocation); } if (Allocations.Count == 1) Allocations.First().Selected = true; } OnPropertyChanged(); } } public ObservableCollection Allocations { get; private set; } public SelectFromHoldingViewModel() { Allocations = new ObservableCollection(); } public double RequestedQuantity { get; set; } public double Quantity { get; set; } public bool HasAllocation => Allocations.Any(x => x.Selected); public double GetMaximum() { var alloc = Allocations.FirstOrDefault(x => x.Selected); return alloc?.Quantity ?? 0.0F; } } [XamlCompilation(XamlCompilationOptions.Compile)] public partial class SelectFromHoldingView : ContentView { public event RequisitionItemScannerSaveEvent? OnSave; public event EventHandler? OnCancel; public SelectFromHoldingView(StockHoldingShell? holding, double requestedquantity, RequisitionItemModel? requisitionItems = null) { InitializeComponent(); _viewModel.RequisitionItems = requisitionItems; _viewModel.Holding = holding; _viewModel.RequestedQuantity = requestedquantity; } private void Cancel_Click(object sender, MobileButtonClickEventArgs args) { OnCancel?.Invoke(this, EventArgs.Empty); } private void OK_Click(object sender, MobileButtonClickEventArgs args) { if (_viewModel.Holding == null) return; var alloc = _viewModel.Allocations.FirstOrDefault(x => x.Selected); if (alloc == null) return; OnSave?.Invoke(this, new RequisitionItemScannerSaveArgs(_viewModel.Holding, alloc.JobID, alloc.JobRequisitionItemID, _viewModel.Quantity)); } private void TakeNone_Click(object sender, MobileButtonClickEventArgs args) { _viewModel.Quantity = 0.0F; } private void SubtractOne_Click(object sender, MobileButtonClickEventArgs args) { _viewModel.Quantity -= 1.0F; } private void AddOne_Click(object sender, MobileButtonClickEventArgs args) { _viewModel.Quantity = Math.Min(_viewModel.GetMaximum(), _viewModel.Quantity + 1.0F); } private void TakeAll_Click(object sender, MobileButtonClickEventArgs args) { _viewModel.Quantity = Math.Min(_viewModel.RequestedQuantity,_viewModel.GetMaximum()); } } }