JobRequisitionStockSelectionPage.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. Units = holding.Units,
  39. Style = holding.Style.Description,
  40. Holding = holding,
  41. });
  42. }
  43. listView.ItemsSource = ViewModels;
  44. Task.Run(() =>
  45. {
  46. 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);
  47. });
  48. if(requisitioned)
  49. {
  50. availableUnitsLbl.Text = "Requisitioned";
  51. listView.IsEnabled = false;
  52. okButton.IsEnabled = false;
  53. }
  54. }
  55. private void SaveButton_Click(object sender, RoutedEventArgs e)
  56. {
  57. var models = ViewModels.Where(x => x.ChosenUnits > 0);
  58. foreach (var model in models)
  59. CreateStockMovements(model);
  60. DialogResult = true;
  61. }
  62. private void CreateStockMovements(JobRequistionStockSelectionViewModel model)
  63. {
  64. StockMovementBatch batch = new StockMovementBatch();
  65. batch.TimeStamp = DateTime.Now;
  66. batch.Type = StockMovementBatchType.Transfer;
  67. batch.Notes = "Requisitioned for Job " + Item.Job.JobNumber;
  68. new Client<StockMovementBatch>().Save(batch, "Created for requisitioning stock");
  69. var issuing = CreateBaseMovement(model, batch.ID);
  70. issuing.Job.ID = IssuingJobID;
  71. issuing.Issued = model.ChosenUnits;
  72. var receiving = CreateBaseMovement(model, batch.ID);
  73. receiving.Job.ID = Item.Job.ID;
  74. receiving.Received = model.ChosenUnits;
  75. receiving.JobRequisitionItem.ID = Item.ID;
  76. new Client<StockMovement>().Save(new StockMovement[] { issuing, receiving }, "Created from Job Requisition Stock Reserve Screen");
  77. }
  78. private StockMovement CreateBaseMovement(JobRequistionStockSelectionViewModel model, Guid batchid)
  79. {
  80. StockMovement mvt = new StockMovement();
  81. mvt.Style.ID = model.Holding.Style.ID;
  82. mvt.Location.ID = model.Holding.Location.ID;
  83. mvt.Dimensions.CopyFrom(model.Holding.Dimensions);
  84. mvt.Batch.ID = batchid;
  85. mvt.Employee.ID = EmpID;
  86. mvt.IsTransfer = true;
  87. mvt.Date = DateTime.Now;
  88. mvt.Product.ID = Item.Product.ID;
  89. return mvt;
  90. }
  91. private void Cancel_Click(object sender, RoutedEventArgs e)
  92. {
  93. DialogResult = false;
  94. }
  95. private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
  96. {
  97. var box = sender as TextBox;
  98. if (!string.IsNullOrWhiteSpace(box.Text))
  99. {
  100. if (double.TryParse(box.Text, out double value) == false)
  101. box.Text = "0";
  102. else
  103. {
  104. var holdingUnits = (box.DataContext as JobRequistionStockSelectionViewModel).Holding.Units;
  105. if (holdingUnits >= value && value >= 0)
  106. (box.DataContext as JobRequistionStockSelectionViewModel).ChosenUnits = value;
  107. else
  108. (box.DataContext as JobRequistionStockSelectionViewModel).ChosenUnits = holdingUnits;
  109. }
  110. }
  111. }
  112. private void All_Clicked(object sender, RoutedEventArgs e)
  113. {
  114. var holdingUnits = ((sender as Button).DataContext as JobRequistionStockSelectionViewModel).Holding.Units;
  115. ((sender as Button).DataContext as JobRequistionStockSelectionViewModel).ChosenUnits = holdingUnits;
  116. }
  117. private void Minus_Click(object sender, RoutedEventArgs e)
  118. {
  119. var model = (sender as Button).DataContext as JobRequistionStockSelectionViewModel;
  120. if (model.ChosenUnits > 0)
  121. model.ChosenUnits--;
  122. }
  123. private void Plus_Click(object sender, RoutedEventArgs e)
  124. {
  125. var holdingUnits = ((sender as Button).DataContext as JobRequistionStockSelectionViewModel).Holding.Units;
  126. var model = (sender as Button).DataContext as JobRequistionStockSelectionViewModel;
  127. if (model.ChosenUnits < holdingUnits)
  128. model.ChosenUnits++;
  129. }
  130. }
  131. public class JobRequistionStockSelectionViewModel : INotifyPropertyChanged
  132. {
  133. public event PropertyChangedEventHandler? PropertyChanged;
  134. public string Location { get; set; }
  135. public string Style { get; set; }
  136. public double Units { get; set; }
  137. private double chosenUnits;
  138. public double ChosenUnits
  139. {
  140. get => chosenUnits;
  141. set
  142. {
  143. chosenUnits = value;
  144. OnPropertyChanged("ChosenUnits");
  145. }
  146. }
  147. public StockHolding Holding { get; set; }
  148. public JobRequistionStockSelectionViewModel()
  149. {
  150. Location = "";
  151. Style = "";
  152. Units = 0;
  153. ChosenUnits = 0;
  154. }
  155. protected virtual void OnPropertyChanged(string propertyName)
  156. {
  157. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  158. }
  159. }
  160. }