StockSelectionPage.xaml.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using com.sun.tools.javac.util;
  12. using Comal.Classes;
  13. using InABox.Clients;
  14. using InABox.Core;
  15. using InABox.DynamicGrid;
  16. using InABox.Wpf;
  17. using javax.swing;
  18. namespace PRSDesktop;
  19. public class StockSelectionItem : JobRequisitionItemSelectionItem
  20. {
  21. public string Area => Holding?.Location?.Area?.Code ?? "";
  22. public string Requisition => JRI is null
  23. ? "Unrequisitioned Items"
  24. : $"{JRI.Job.JobNumber}: #{JRI.Requisition.Number} ({JRI.Requisition.Description})";
  25. public string Location => Holding?.Location?.Code ?? "";
  26. public string Style => Holding?.Style?.Code ?? "";
  27. public StockHolding? Holding { get; set; }
  28. }
  29. public class StockSelectionGrid : JobRequisitionItemSelectionGrid<StockSelectionItem>
  30. {
  31. public bool ShowRequisition { get; set; } = false;
  32. public override DynamicGridColumns GenerateColumns()
  33. {
  34. var columns = new DynamicGridColumns();
  35. if (ShowRequisition)
  36. {
  37. columns.Add<StockSelectionItem, string>(x => x.Area, 120, "Area", "", Alignment.MiddleCenter);
  38. columns.Add<StockSelectionItem, string>(x => x.Location, 120, "Location", "", Alignment.MiddleCenter);
  39. columns.Add<StockSelectionItem, string>(x => x.Style, 120, "Style", "", Alignment.MiddleCenter);
  40. columns.Add<StockSelectionItem, string>(x => x.Requisition, 0, "Requisition", "", Alignment.MiddleLeft);
  41. }
  42. else
  43. {
  44. columns.Add<StockSelectionItem, string>(x => x.Area, 0, "Area", "", Alignment.MiddleCenter);
  45. columns.Add<StockSelectionItem, string>(x => x.Location, 0, "Location", "", Alignment.MiddleCenter);
  46. columns.Add<StockSelectionItem, string>(x => x.Style, 0, "Style", "", Alignment.MiddleCenter);
  47. }
  48. return columns;
  49. }
  50. protected override void Reload(
  51. Filters<StockSelectionItem> criteria, Columns<StockSelectionItem> columns, ref SortOrder<StockSelectionItem>? sort,
  52. CancellationToken token, Action<CoreTable?, Exception?> action)
  53. {
  54. foreach(var item in Items)
  55. {
  56. item.Issued = 0;
  57. item.Quantity = item.Holding.Units;
  58. item.MaxValue = item.Quantity;
  59. }
  60. base.Reload(criteria, columns, ref sort, token, action);
  61. }
  62. }
  63. /// <summary>
  64. /// Interaction logic for JobRequisitionStockSelectionPage.xaml
  65. /// </summary>
  66. public partial class StockSelectionPage : ThemableWindow, INotifyPropertyChanged
  67. {
  68. public class Holding(StockHolding holding, Guid jobRequisitionItemID)
  69. {
  70. public StockHolding StockHolding { get; set; } = holding;
  71. public Guid JobRequisitionItemID { get; set; } = jobRequisitionItemID;
  72. }
  73. public bool CanSave => Grid.TotalIssued > 0;
  74. public JobRequisitionItem Item { get; set; }
  75. public Guid IssuingJobID { get; set; }
  76. private bool Allocated = false;
  77. private bool _canEdit = true;
  78. public bool CanEdit
  79. {
  80. get => _canEdit;
  81. private set
  82. {
  83. _canEdit = value;
  84. UpdateItems();
  85. }
  86. }
  87. private void UpdateItems()
  88. {
  89. foreach(var item in Grid.Items)
  90. {
  91. item.Editable = _canEdit && item.Quantity.IsEffectivelyGreaterThan(0.0);
  92. }
  93. }
  94. public bool ShowRequisition
  95. {
  96. get => Grid.ShowRequisition;
  97. set => Grid.ShowRequisition = value;
  98. }
  99. public StockSelectionPage(IEnumerable<Holding> holdings, JobRequisitionItem item, Job issuingJob, bool allocated = false)
  100. {
  101. InitializeComponent();
  102. Item = item;
  103. var jris = Client.Query(
  104. new Filter<JobRequisitionItem>(x => x.ID).InList(holdings.Select(x => x.JobRequisitionItemID).Where(x => x != Guid.Empty).ToArray()),
  105. Columns.None<JobRequisitionItem>()
  106. .Add(x => x.ID)
  107. .Add(x => x.Job.JobNumber)
  108. .Add(x => x.Requisition.Number)
  109. .Add(x => x.Requisition.Description))
  110. .ToObjects<JobRequisitionItem>().ToDictionary(x => x.ID);
  111. Grid.Items = holdings.Select(x => new StockSelectionItem
  112. {
  113. JRI = jris.GetValueOrDefault(x.JobRequisitionItemID)
  114. ?? new(),
  115. Holding = x.StockHolding
  116. }).ToList();
  117. IssuingJobID = issuingJob.ID;
  118. jobLbl.Text = $"Taking stock from Job: {issuingJob.Name} ({issuingJob.JobNumber})";
  119. Allocated = allocated;
  120. if (allocated)
  121. {
  122. CanEdit = Security.IsAllowed<CanEditAllocatedJobRequisitions>();
  123. ShowRequisition = true;
  124. // availableUnitsLbl.Text = "Allocated";
  125. if (CanEdit)
  126. {
  127. // allocateLabel.Text = "Take";
  128. }
  129. }
  130. //UpdateItems();
  131. Grid.Refresh(true, true);
  132. }
  133. private void Grid_PropertyChanged(object sender, PropertyChangedEventArgs e)
  134. {
  135. if(e.PropertyName == nameof(Grid.TotalIssued))
  136. {
  137. OnPropertyChanged(nameof(CanSave));
  138. }
  139. }
  140. private void OKButton_Click(object sender, RoutedEventArgs e)
  141. {
  142. if(Grid.TotalIssued <= 0)
  143. {
  144. MessageWindow.ShowMessage("Please select from at least one holding to reserve.", "Error", image: MessageWindow.WarningImage);
  145. return;
  146. }
  147. if (Allocated)
  148. {
  149. var result = MessageWindow.ShowYesNoCancel("You are attempting to take reserved stock that has already been allocated to job requisition items. " +
  150. "Are you sure you wish to proceed? (This will decrease the allocation for the job requisition item being taken from.)",
  151. "Confirm re-allocation");
  152. if(result != MessageWindowResult.Yes)
  153. {
  154. return;
  155. }
  156. }
  157. foreach (var item in Grid.Items.Where(x => x.Issued > 0))
  158. {
  159. CreateStockMovements(item);
  160. }
  161. DialogResult = true;
  162. }
  163. private void CreateStockMovements(StockSelectionItem item)
  164. {
  165. var batch = new StockMovementBatch
  166. {
  167. TimeStamp = DateTime.Now,
  168. Type = StockMovementBatchType.Transfer,
  169. Notes = $"Allocated to Job {Item.Job.JobNumber}"
  170. };
  171. Client.Save(batch, "Created for requisitioning stock");
  172. var issuing = CreateBaseMovement(item, batch.ID, item.JRI);
  173. issuing.Job.ID = IssuingJobID;
  174. issuing.Issued = item.Issued;
  175. issuing.JobRequisitionItem.ID = item.JRI?.ID ?? Guid.Empty;
  176. issuing.Type = StockMovementType.TransferOut;
  177. var receiving = CreateBaseMovement(item, batch.ID, item.JRI);
  178. receiving.Job.ID = Item.Job.ID;
  179. receiving.Received = item.Issued;
  180. receiving.JobRequisitionItem.ID = Item.ID;
  181. receiving.Type = StockMovementType.TransferIn;
  182. receiving.Transaction = issuing.Transaction;
  183. Client.Save(new StockMovement[] { issuing, receiving }, "Created from Reservation Management Screen");
  184. }
  185. private StockMovement CreateBaseMovement(StockSelectionItem item, Guid batchid, JobRequisitionItem? fromJRI)
  186. {
  187. var mvt = item.Holding.CreateMovement();
  188. mvt.Batch.ID = batchid;
  189. mvt.Employee.ID = App.EmployeeID;
  190. mvt.Date = DateTime.Now;
  191. mvt.Product.ID = Item.Product.ID;
  192. if(fromJRI is null)
  193. {
  194. mvt.Notes = $"Reservation Management Screen - allocating to Job {Item.Job.JobNumber} for Requisition Line";
  195. }
  196. else
  197. {
  198. mvt.Notes = $"Reservation Management Screen - allocating to Job {Item.Job.JobNumber} for Requisition Line from " +
  199. $"requisition '{fromJRI.Requisition.Number} - {fromJRI.Requisition.Description}' for Job {fromJRI.Job.JobNumber}";
  200. }
  201. mvt.Cost = item.Holding.AverageValue;
  202. return mvt;
  203. }
  204. private void CancelButton_Click(object sender, RoutedEventArgs e)
  205. {
  206. DialogResult = false;
  207. }
  208. public event PropertyChangedEventHandler? PropertyChanged;
  209. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  210. {
  211. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  212. }
  213. private void Grid_OnAfterRefresh(object sender, AfterRefreshEventArgs args)
  214. {
  215. UpdateItems();
  216. }
  217. }