StockSelectionPage.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using com.sun.tools.javac.util;
  11. using Comal.Classes;
  12. using InABox.Clients;
  13. using InABox.Core;
  14. using InABox.Wpf;
  15. using javax.swing;
  16. namespace PRSDesktop;
  17. /// <summary>
  18. /// Interaction logic for JobRequisitionStockSelectionPage.xaml
  19. /// </summary>
  20. public partial class StockSelectionPage : ThemableWindow, INotifyPropertyChanged
  21. {
  22. public ObservableCollection<StockSelectionViewModel> ViewModels { get; } = new ObservableCollection<StockSelectionViewModel>();
  23. private readonly JobRequisitionItem Item;
  24. public Guid IssuingJobID { get; set; }
  25. public bool Allocated = false;
  26. public double TotalChosen => ViewModels.Sum(x => x.ChosenUnits);
  27. public StockSelectionPage(IEnumerable<StockHolding> holdings, JobRequisitionItem item, Job issuingJob, bool requisitioned = false)
  28. {
  29. Item = item;
  30. foreach (var holding in holdings)
  31. {
  32. ViewModels.Add(new StockSelectionViewModel(holding));
  33. }
  34. InitializeComponent();
  35. IssuingJobID = issuingJob.ID;
  36. jobLbl.Text = $"Taking stock from Job: {issuingJob.Name} ({issuingJob.JobNumber})";
  37. if (requisitioned)
  38. {
  39. availableUnitsLbl.Text = "Allocated";
  40. listView.IsEnabled = false;
  41. okButton.IsEnabled = false;
  42. }
  43. }
  44. private void SaveButton_Click(object sender, RoutedEventArgs e)
  45. {
  46. if(TotalChosen <= 0)
  47. {
  48. MessageWindow.ShowMessage("Please select at least from at least one holding to reserve.", "Error", image: MessageWindow.WarningImage);
  49. return;
  50. }
  51. var models = ViewModels.Where(x => x.ChosenUnits > 0);
  52. foreach (var model in models)
  53. {
  54. CreateStockMovements(model);
  55. }
  56. DialogResult = true;
  57. }
  58. private void CreateStockMovements(StockSelectionViewModel model)
  59. {
  60. var batch = new StockMovementBatch
  61. {
  62. TimeStamp = DateTime.Now,
  63. Type = StockMovementBatchType.Transfer,
  64. Notes = $"Allocated to Job {Item.Job.JobNumber}"
  65. };
  66. Client.Save(batch, "Created for requisitioning stock");
  67. var issuing = CreateBaseMovement(model, batch.ID);
  68. issuing.Job.ID = IssuingJobID;
  69. issuing.Issued = model.ChosenUnits;
  70. issuing.Type = StockMovementType.TransferOut;
  71. var receiving = CreateBaseMovement(model, batch.ID);
  72. receiving.Job.ID = Item.Job.ID;
  73. receiving.Received = model.ChosenUnits;
  74. receiving.JobRequisitionItem.ID = Item.ID;
  75. receiving.Type = StockMovementType.TransferIn;
  76. receiving.Transaction = issuing.Transaction;
  77. Client.Save(new StockMovement[] { issuing, receiving }, "Created from Reservation Management Screen");
  78. }
  79. private StockMovement CreateBaseMovement(StockSelectionViewModel model, Guid batchid)
  80. {
  81. var mvt = new StockMovement();
  82. mvt.Style.ID = model.Holding.Style.ID;
  83. mvt.Location.ID = model.Holding.Location.ID;
  84. mvt.Dimensions.CopyFrom(model.Holding.Dimensions);
  85. mvt.Batch.ID = batchid;
  86. mvt.Employee.ID = App.EmployeeID;
  87. mvt.Date = DateTime.Now;
  88. mvt.Product.ID = Item.Product.ID;
  89. mvt.Notes = $"Reservation Management Screen - allocating to Job {Item.Job.JobNumber} for Requisition Line";
  90. return mvt;
  91. }
  92. private void Cancel_Click(object sender, RoutedEventArgs e)
  93. {
  94. DialogResult = false;
  95. }
  96. private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
  97. {
  98. if (sender is not TextBox box || box.Tag is not StockSelectionViewModel model) return;
  99. if (box.Text.IsNullOrWhiteSpace()) return;
  100. if (double.TryParse(box.Text, out double value))
  101. {
  102. model.ChosenUnits = value;
  103. OnPropertyChanged(nameof(TotalChosen));
  104. }
  105. else
  106. {
  107. // This'll call the event handler again.
  108. box.Text = "0";
  109. }
  110. }
  111. private void All_Clicked(object sender, RoutedEventArgs e)
  112. {
  113. if (sender is not FrameworkElement element || element.Tag is not StockSelectionViewModel model) return;
  114. model.ChosenUnits = model.Holding.Units;
  115. OnPropertyChanged(nameof(TotalChosen));
  116. }
  117. private void Minus_Click(object sender, RoutedEventArgs e)
  118. {
  119. if (sender is not FrameworkElement element || element.Tag is not StockSelectionViewModel model) return;
  120. model.ChosenUnits--;
  121. OnPropertyChanged(nameof(TotalChosen));
  122. }
  123. private void Plus_Click(object sender, RoutedEventArgs e)
  124. {
  125. if (sender is not FrameworkElement element || element.Tag is not StockSelectionViewModel model) return;
  126. model.ChosenUnits++;
  127. OnPropertyChanged(nameof(TotalChosen));
  128. }
  129. public event PropertyChangedEventHandler? PropertyChanged;
  130. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  131. {
  132. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  133. if(propertyName == nameof(TotalChosen))
  134. {
  135. okButton.IsEnabled = TotalChosen > 0;
  136. }
  137. }
  138. }
  139. public class StockSelectionViewModel : INotifyPropertyChanged
  140. {
  141. public event PropertyChangedEventHandler? PropertyChanged;
  142. public string Location => Holding.Location.Description;
  143. public string Area => Holding.Location.Area.Description;
  144. public string Style => Holding.Style.Description;
  145. public double Units => Holding.Units;
  146. private double _chosenUnits;
  147. public double ChosenUnits
  148. {
  149. get => _chosenUnits;
  150. set
  151. {
  152. _chosenUnits = Math.Clamp(value, 0, Holding.Units);
  153. OnPropertyChanged();
  154. }
  155. }
  156. public StockHolding Holding { get; set; }
  157. public StockSelectionViewModel(StockHolding holding)
  158. {
  159. Holding = holding;
  160. ChosenUnits = 0;
  161. }
  162. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  163. {
  164. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  165. }
  166. }