StockHoldingRelocationWindow.xaml.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Runtime.CompilerServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Shapes;
  21. namespace PRSDesktop.Panels.Products.Locations;
  22. public class StockHoldingRelocationItem : INotifyPropertyChanged
  23. {
  24. public string ItemNumber { get; set; }
  25. public string Text { get; set; }
  26. public double Quantity { get; set; }
  27. private double _issued;
  28. public double Issued
  29. {
  30. get => _issued;
  31. set
  32. {
  33. _issued = value;
  34. OnPropertyChanged();
  35. }
  36. }
  37. public Guid ID { get; set; }
  38. public event PropertyChangedEventHandler? PropertyChanged;
  39. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  40. {
  41. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  42. }
  43. }
  44. /// <summary>
  45. /// Interaction logic for StockHoldingRelocationWindow.xaml
  46. /// </summary>
  47. public partial class StockHoldingRelocationWindow : Window, INotifyPropertyChanged
  48. {
  49. public ObservableCollection<StockHoldingRelocationItem> Items { get; set; } = new();
  50. private double _totalQuantity;
  51. public double TotalQuantity
  52. {
  53. get => _totalQuantity;
  54. set
  55. {
  56. _totalQuantity = value;
  57. OnPropertyChanged();
  58. }
  59. }
  60. private double _totalIssued;
  61. public double TotalIssued
  62. {
  63. get => _totalIssued;
  64. set
  65. {
  66. _totalIssued = value;
  67. OnPropertyChanged();
  68. OnPropertyChanged(nameof(CanSave));
  69. }
  70. }
  71. public StockHolding From { get; set; }
  72. private bool _isTargetEditable = true;
  73. public bool IsTargetEditable
  74. {
  75. get => _isTargetEditable;
  76. set
  77. {
  78. _isTargetEditable = value;
  79. OnPropertyChanged();
  80. }
  81. }
  82. private StockLocation? _to;
  83. public StockLocation? To
  84. {
  85. get => _to;
  86. set
  87. {
  88. _to = value;
  89. OnPropertyChanged();
  90. OnPropertyChanged(nameof(CanSave));
  91. OnPropertyChanged($"{nameof(To)}.{nameof(To.Code)}");
  92. OnPropertyChanged($"{nameof(To)}.{nameof(To.Description)}");
  93. }
  94. }
  95. public bool CanSave => (!IsTargetEditable || To is not null) && TotalIssued > 0;
  96. public StockHoldingRelocationWindow(StockHolding from, IEnumerable<JobRequisitionItem> items)
  97. {
  98. Client.EnsureColumns(from, new Columns<StockHolding>(x => x.Location.Code));
  99. From = from;
  100. InitializeComponent();
  101. SetRequisitionItems(items);
  102. Items.CollectionChanged += (o, e) => Recalculate();
  103. }
  104. private bool _observing = false;
  105. public void SetObserving(bool observing)
  106. {
  107. if(_observing != observing)
  108. {
  109. _observing = observing;
  110. if (_observing)
  111. {
  112. Recalculate();
  113. }
  114. }
  115. }
  116. public void SetRequisitionItems(IEnumerable<JobRequisitionItem> items)
  117. {
  118. SetObserving(false);
  119. var rItems = items.AsIList();
  120. var rIDs = rItems.Select(x => x.ID).Where(x => x != Guid.Empty).ToArray();
  121. var quantities = Client.Query(
  122. StockHolding.GetFilter(From)
  123. .Combine(new Filter<StockMovement>(x => x.JobRequisitionItem.ID).InList(rIDs)),
  124. new Columns<StockMovement>(x => x.Units).Add(x => x.JobRequisitionItem.ID))
  125. .ToObjects<StockMovement>().GroupBy(x => x.JobRequisitionItem.ID).ToDictionary(x => x.Key, x => x.Sum(x => x.Units));
  126. var requidItems = new List<StockHoldingRelocationItem>();
  127. foreach(var item in rItems)
  128. {
  129. var qty = item.ID != Guid.Empty ? quantities.GetValueOrDefault(item.ID) : item.Qty;
  130. var newItem = new StockHoldingRelocationItem
  131. {
  132. Issued = 0,
  133. Quantity = qty,
  134. Text = item.ID == Guid.Empty
  135. ? "Unrequisitioned Items"
  136. : $"{item.Job.JobNumber}:{item.Requisition.Number} {item.Requisition.Description} ({qty})",
  137. ID = item.ID
  138. };
  139. newItem.PropertyChanged += (o, e) => Recalculate();
  140. if(item.ID == Guid.Empty)
  141. {
  142. Items.Add(newItem);
  143. }
  144. else
  145. {
  146. requidItems.Add(newItem);
  147. }
  148. }
  149. int i = 1;
  150. foreach(var item in requidItems)
  151. {
  152. item.ItemNumber = $"{i}. ";
  153. Items.Add(item);
  154. ++i;
  155. }
  156. SetObserving(true);
  157. }
  158. public Dictionary<Guid, double> GetQuantities()
  159. {
  160. return Items.ToDictionary(x => x.ID, x => x.Issued);
  161. }
  162. public StockLocation GetTargetLocation()
  163. {
  164. return To ?? new StockLocation();
  165. }
  166. private void Recalculate()
  167. {
  168. if (!_observing) return;
  169. TotalQuantity = Items.Sum(x => x.Quantity);
  170. TotalIssued = Items.Sum(x => x.Issued);
  171. }
  172. public event PropertyChangedEventHandler? PropertyChanged;
  173. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  174. {
  175. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  176. }
  177. private void Minus_Click(object sender, RoutedEventArgs e)
  178. {
  179. if (sender is not FrameworkElement element || element.Tag is not StockHoldingRelocationItem item) return;
  180. item.Issued = Math.Max(0, item.Issued - 1);
  181. }
  182. private void Plus_Click(object sender, RoutedEventArgs e)
  183. {
  184. if (sender is not FrameworkElement element || element.Tag is not StockHoldingRelocationItem item) return;
  185. item.Issued = Math.Min(item.Issued + 1, item.Quantity);
  186. }
  187. private void None_Click(object sender, RoutedEventArgs e)
  188. {
  189. if (sender is not FrameworkElement element || element.Tag is not StockHoldingRelocationItem item) return;
  190. item.Issued = 0;
  191. }
  192. private void All_Click(object sender, RoutedEventArgs e)
  193. {
  194. if (sender is not FrameworkElement element || element.Tag is not StockHoldingRelocationItem item) return;
  195. item.Issued = item.Quantity;
  196. }
  197. private bool DoLookup(string? column, string? value)
  198. {
  199. var grid = new MultiSelectDialog<StockLocation>(
  200. LookupFactory.DefineFilter<StockLocation>(),
  201. new Columns<StockLocation>(x => x.ID).Add(x => x.Code).Add(x => x.Description),
  202. multiselect: false);
  203. if (grid.ShowDialog(column, value))
  204. {
  205. To = grid.Data().Rows.First().ToObject<StockLocation>();
  206. return true;
  207. }
  208. else
  209. {
  210. return false;
  211. }
  212. }
  213. private void ToButton_Click(object sender, RoutedEventArgs e)
  214. {
  215. DoLookup(null, null);
  216. }
  217. private void OKButton_Click(object sender, RoutedEventArgs e)
  218. {
  219. DialogResult = true;
  220. Close();
  221. }
  222. private void CancelButton_Click(object sender, RoutedEventArgs e)
  223. {
  224. DialogResult = false;
  225. Close();
  226. }
  227. private void ToBox_LostFocus(object sender, RoutedEventArgs e)
  228. {
  229. if (ToBox.Text.IsNullOrWhiteSpace() || ToBox.Text == To?.Code) return;
  230. var location = Client.Query(
  231. new Filter<StockLocation>(x => x.Code).IsEqualTo(ToBox.Text),
  232. new Columns<StockLocation>(x => x.ID).Add(x => x.Code).Add(x => x.Description))
  233. .ToObjects<StockLocation>().FirstOrDefault();
  234. if(location is not null)
  235. {
  236. To = location;
  237. }
  238. else
  239. {
  240. if(!DoLookup(nameof(To.Code), ToBox.Text))
  241. {
  242. To = null;
  243. }
  244. }
  245. }
  246. }