StockSelectionPage.xaml.cs 8.3 KB

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