JobRequisitionStockSelectionPage.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using com.sun.tools.javac.util;
  10. using Comal.Classes;
  11. using InABox.Clients;
  12. using InABox.Core;
  13. using InABox.Wpf;
  14. using javax.swing;
  15. namespace PRSDesktop
  16. {
  17. /// <summary>
  18. /// Interaction logic for JobRequisitionStockSelectionPage.xaml
  19. /// </summary>
  20. public partial class JobRequisitionStockSelectionPage : ThemableWindow
  21. {
  22. ObservableCollection<JobRequistionStockSelectionViewModel> ViewModels = new ObservableCollection<JobRequistionStockSelectionViewModel>();
  23. JobRequisitionItem Item = new JobRequisitionItem();
  24. public Guid EmpID { get; set; }
  25. public Guid IssuingJobID { get; set; }
  26. public bool Requisitioned = false;
  27. public JobRequisitionStockSelectionPage(IEnumerable<StockHolding> holdings, JobRequisitionItem item, Job issuingJob, bool requisitioned = false)
  28. {
  29. InitializeComponent();
  30. Item = item;
  31. IssuingJobID = issuingJob.ID;
  32. jobLbl.Text = "Taking stock from Job: " + issuingJob.Name + " (" + issuingJob.JobNumber + ")";
  33. foreach (var holding in holdings)
  34. {
  35. ViewModels.Add(new JobRequistionStockSelectionViewModel
  36. {
  37. Location = holding.Location.Description,
  38. Area = holding.Location.Area.Description,
  39. Units = holding.Units,
  40. Style = holding.Style.Description,
  41. Holding = holding,
  42. }) ;
  43. }
  44. listView.ItemsSource = ViewModels;
  45. Task.Run(() =>
  46. {
  47. EmpID = new Client<Employee>().Query(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid), new Columns<Employee>(x => x.ID)).Rows.FirstOrDefault().Get<Employee, Guid>(x => x.ID);
  48. });
  49. if (requisitioned)
  50. {
  51. availableUnitsLbl.Text = "Requisitioned";
  52. listView.IsEnabled = false;
  53. okButton.IsEnabled = false;
  54. }
  55. }
  56. private void SaveButton_Click(object sender, RoutedEventArgs e)
  57. {
  58. var models = ViewModels.Where(x => x.ChosenUnits > 0);
  59. foreach (var model in models)
  60. CreateStockMovements(model);
  61. DialogResult = true;
  62. }
  63. private void CreateStockMovements(JobRequistionStockSelectionViewModel model)
  64. {
  65. StockMovementBatch batch = new StockMovementBatch();
  66. batch.TimeStamp = DateTime.Now;
  67. batch.Type = StockMovementBatchType.Transfer;
  68. batch.Notes = "Requisitioned for Job " + Item.Job.JobNumber;
  69. new Client<StockMovementBatch>().Save(batch, "Created for requisitioning stock");
  70. var issuing = CreateBaseMovement(model, batch.ID);
  71. issuing.Job.ID = IssuingJobID;
  72. issuing.Issued = model.ChosenUnits;
  73. var receiving = CreateBaseMovement(model, batch.ID);
  74. receiving.Job.ID = Item.Job.ID;
  75. receiving.Received = model.ChosenUnits;
  76. receiving.JobRequisitionItem.ID = Item.ID;
  77. new Client<StockMovement>().Save(new StockMovement[] { issuing, receiving }, "Created from Job Requisition Stock Reserve Screen");
  78. }
  79. private StockMovement CreateBaseMovement(JobRequistionStockSelectionViewModel model, Guid batchid)
  80. {
  81. StockMovement 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 = EmpID;
  87. mvt.IsTransfer = true;
  88. mvt.Date = DateTime.Now;
  89. mvt.Product.ID = Item.Product.ID;
  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. var box = sender as TextBox;
  99. if (!string.IsNullOrWhiteSpace(box.Text))
  100. {
  101. if (double.TryParse(box.Text, out double value) == false)
  102. box.Text = "0";
  103. else
  104. {
  105. var holdingUnits = (box.DataContext as JobRequistionStockSelectionViewModel).Holding.Units;
  106. if (holdingUnits >= value && value >= 0)
  107. (box.DataContext as JobRequistionStockSelectionViewModel).ChosenUnits = value;
  108. else
  109. (box.DataContext as JobRequistionStockSelectionViewModel).ChosenUnits = holdingUnits;
  110. }
  111. }
  112. }
  113. private void All_Clicked(object sender, RoutedEventArgs e)
  114. {
  115. var holdingUnits = ((sender as Button).DataContext as JobRequistionStockSelectionViewModel).Holding.Units;
  116. ((sender as Button).DataContext as JobRequistionStockSelectionViewModel).ChosenUnits = holdingUnits;
  117. }
  118. private void Minus_Click(object sender, RoutedEventArgs e)
  119. {
  120. var model = (sender as Button).DataContext as JobRequistionStockSelectionViewModel;
  121. if (model.ChosenUnits > 0)
  122. model.ChosenUnits--;
  123. }
  124. private void Plus_Click(object sender, RoutedEventArgs e)
  125. {
  126. var holdingUnits = ((sender as Button).DataContext as JobRequistionStockSelectionViewModel).Holding.Units;
  127. var model = (sender as Button).DataContext as JobRequistionStockSelectionViewModel;
  128. if (model.ChosenUnits < holdingUnits)
  129. model.ChosenUnits++;
  130. }
  131. }
  132. public class JobRequistionStockSelectionViewModel : INotifyPropertyChanged
  133. {
  134. public event PropertyChangedEventHandler? PropertyChanged;
  135. public string Location { get; set; }
  136. public string Area { get; set; }
  137. public string Style { get; set; }
  138. public double Units { get; set; }
  139. private double chosenUnits;
  140. public double ChosenUnits
  141. {
  142. get => chosenUnits;
  143. set
  144. {
  145. chosenUnits = value;
  146. OnPropertyChanged("ChosenUnits");
  147. }
  148. }
  149. public StockHolding Holding { get; set; }
  150. public JobRequistionStockSelectionViewModel()
  151. {
  152. Location = "";
  153. Area = "";
  154. Style = "";
  155. Units = 0;
  156. ChosenUnits = 0;
  157. }
  158. protected virtual void OnPropertyChanged(string propertyName)
  159. {
  160. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  161. }
  162. }
  163. }