SelectFromHoldingView.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using InABox.Core;
  8. using InABox.Mobile;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. namespace PRS.Mobile
  12. {
  13. public class SelectFromHoldingAllocation : BindableObject
  14. {
  15. private bool _selected;
  16. public bool Selected
  17. {
  18. get => _selected;
  19. set
  20. {
  21. if (value == _selected) return;
  22. _selected = value;
  23. OnPropertyChanged();
  24. OnPropertyChanged(nameof(SelectFromHoldingViewModel.HasAllocation));
  25. }
  26. }
  27. public Guid JobID { get; set; }
  28. public Guid JobRequisitionItemID { get; set; }
  29. public String Description { get; set; }
  30. public double Quantity { get; set; }
  31. }
  32. public class SelectFromHoldingViewModel : BindableObject
  33. {
  34. public RequisitionItemModel? RequisitionItems { get; set; }
  35. private StockHoldingShell? _holding;
  36. private bool bChanging;
  37. public StockHoldingShell? Holding
  38. {
  39. get => _holding;
  40. set
  41. {
  42. if (Equals(value, _holding)) return;
  43. _holding = value;
  44. Allocations.Clear();
  45. if (_holding != null)
  46. {
  47. foreach (var alloc in _holding.Allocations)
  48. {
  49. var picked = RequisitionItems?.Items.Where(x =>
  50. x.LocationID == _holding.LocationID
  51. && x.ProductID == _holding.ProductID
  52. && x.StyleID == _holding.StyleID
  53. && x.JobID == _holding.JobID
  54. && x.DimensionsLength.IsEffectivelyEqual(_holding.DimensionsLength)
  55. && x.DimensionsWidth.IsEffectivelyEqual(_holding.DimensionsWidth)
  56. && x.DimensionsHeight.IsEffectivelyEqual(_holding.DimensionsHeight)
  57. && x.DimensionsWeight .IsEffectivelyEqual(_holding.DimensionsWeight)
  58. && x.DimensionsQuantity.IsEffectivelyEqual(_holding.DimensionsQuantity)
  59. && x.ID == alloc.ID)
  60. .Sum(x => x.ActualQuantity) ?? 0.0;
  61. if (!alloc.Maximum.IsEffectivelyGreaterThan(picked))
  62. continue;
  63. var allocation = new SelectFromHoldingAllocation()
  64. { JobID = alloc.JobID, JobRequisitionItemID = alloc.ID, Description = alloc.Description, Quantity = alloc.Maximum - picked };
  65. allocation.PropertyChanged += (sender, args) =>
  66. {
  67. if (bChanging)
  68. return;
  69. if (args.PropertyName.Equals(nameof(SelectFromHoldingAllocation.Selected)))
  70. {
  71. bChanging = true;
  72. foreach (var a in Allocations)
  73. a.Selected = a == sender;
  74. bChanging = false;
  75. OnPropertyChanged(nameof(HasAllocation));
  76. }
  77. Quantity = Math.Max(0.0F,Math.Min(RequestedQuantity,(sender as SelectFromHoldingAllocation)?.Quantity ?? 0.0F));
  78. };
  79. Allocations.Add(allocation);
  80. }
  81. if (Allocations.Count == 1)
  82. Allocations.First().Selected = true;
  83. }
  84. OnPropertyChanged();
  85. }
  86. }
  87. public ObservableCollection<SelectFromHoldingAllocation> Allocations { get; private set; }
  88. public SelectFromHoldingViewModel()
  89. {
  90. Allocations = new ObservableCollection<SelectFromHoldingAllocation>();
  91. }
  92. public double RequestedQuantity { get; set; }
  93. public double Quantity { get; set; }
  94. public bool HasAllocation => Allocations.Any(x => x.Selected);
  95. public double GetMaximum()
  96. {
  97. var alloc = Allocations.FirstOrDefault(x => x.Selected);
  98. return alloc?.Quantity ?? 0.0F;
  99. }
  100. }
  101. [XamlCompilation(XamlCompilationOptions.Compile)]
  102. public partial class SelectFromHoldingView : ContentView
  103. {
  104. public event RequisitionItemScannerSaveEvent? OnSave;
  105. public event EventHandler? OnCancel;
  106. public SelectFromHoldingView(StockHoldingShell? holding, double requestedquantity, RequisitionItemModel? requisitionItems = null)
  107. {
  108. InitializeComponent();
  109. _viewModel.RequisitionItems = requisitionItems;
  110. _viewModel.Holding = holding;
  111. _viewModel.RequestedQuantity = requestedquantity;
  112. }
  113. private void Cancel_Click(object sender, MobileButtonClickEventArgs args)
  114. {
  115. OnCancel?.Invoke(this, EventArgs.Empty);
  116. }
  117. private void OK_Click(object sender, MobileButtonClickEventArgs args)
  118. {
  119. if (_viewModel.Holding == null)
  120. return;
  121. var alloc = _viewModel.Allocations.FirstOrDefault(x => x.Selected);
  122. if (alloc == null)
  123. return;
  124. OnSave?.Invoke(this, new RequisitionItemScannerSaveArgs(_viewModel.Holding, alloc.JobID, alloc.JobRequisitionItemID, _viewModel.Quantity));
  125. }
  126. private void TakeNone_Click(object sender, MobileButtonClickEventArgs args)
  127. {
  128. _viewModel.Quantity = 0.0F;
  129. }
  130. private void SubtractOne_Click(object sender, MobileButtonClickEventArgs args)
  131. {
  132. _viewModel.Quantity -= 1.0F;
  133. }
  134. private void AddOne_Click(object sender, MobileButtonClickEventArgs args)
  135. {
  136. _viewModel.Quantity = Math.Min(_viewModel.GetMaximum(), _viewModel.Quantity + 1.0F);
  137. }
  138. private void TakeAll_Click(object sender, MobileButtonClickEventArgs args)
  139. {
  140. _viewModel.Quantity = Math.Min(_viewModel.RequestedQuantity,_viewModel.GetMaximum());
  141. }
  142. }
  143. }