StockHoldingRelocationWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using Microsoft.CodeAnalysis.VisualBasic.Syntax;
  6. using Org.BouncyCastle.Asn1.Mozilla;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Runtime.CompilerServices;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Shapes;
  23. namespace PRSDesktop.Panels.Products.Locations;
  24. /// <summary>
  25. /// Interaction logic for StockHoldingRelocationWindow.xaml
  26. /// </summary>
  27. public partial class StockHoldingRelocationWindow : Window, INotifyPropertyChanged
  28. {
  29. public StockHolding From { get; private init; }
  30. public enum RelocationMode
  31. {
  32. Issue,
  33. Transfer,
  34. ReleaseAllocations
  35. }
  36. private RelocationMode _mode;
  37. public RelocationMode Mode
  38. {
  39. get => _mode;
  40. set
  41. {
  42. _mode = value;
  43. UpdateMode();
  44. }
  45. }
  46. public bool ShowHeader => ShowFrom || IsTargetEditable || IsJobEditable;
  47. private bool _showFrom = true;
  48. public bool ShowFrom
  49. {
  50. get => _showFrom;
  51. set
  52. {
  53. _showFrom = value;
  54. OnPropertyChanged();
  55. OnPropertyChanged(nameof(ShowHeader));
  56. }
  57. }
  58. private bool _isTargetEditable = true;
  59. public bool IsTargetEditable
  60. {
  61. get => _isTargetEditable;
  62. set
  63. {
  64. _isTargetEditable = value;
  65. OnPropertyChanged();
  66. OnPropertyChanged(nameof(ShowHeader));
  67. }
  68. }
  69. private bool _isJobEditable = false;
  70. public bool IsJobEditable
  71. {
  72. get => _isJobEditable;
  73. set
  74. {
  75. _isJobEditable = value;
  76. OnPropertyChanged();
  77. OnPropertyChanged(nameof(ShowHeader));
  78. }
  79. }
  80. private StockLocation? _originalTarget;
  81. public StockLocation? OriginalTarget
  82. {
  83. get => _originalTarget;
  84. set
  85. {
  86. _originalTarget = value;
  87. To = value;
  88. }
  89. }
  90. private Job? _originalJob;
  91. public Job? OriginalJob
  92. {
  93. get => _originalJob;
  94. set
  95. {
  96. _originalJob = value;
  97. Job = value;
  98. }
  99. }
  100. private Job? _job;
  101. public Job? Job
  102. {
  103. get => _job;
  104. set
  105. {
  106. _job = value;
  107. OnPropertyChanged();
  108. OnPropertyChanged(nameof(CanSave));
  109. OnPropertyChanged($"{nameof(Job)}.{nameof(Job.JobNumber)}");
  110. OnPropertyChanged($"{nameof(Job)}.{nameof(Job.Name)}");
  111. UpdateMode();
  112. }
  113. }
  114. private StockLocation? _to;
  115. public StockLocation? To
  116. {
  117. get => _to;
  118. set
  119. {
  120. _to = value;
  121. OnPropertyChanged();
  122. OnPropertyChanged(nameof(CanSave));
  123. OnPropertyChanged($"{nameof(To)}.{nameof(To.Code)}");
  124. OnPropertyChanged($"{nameof(To)}.{nameof(To.Description)}");
  125. }
  126. }
  127. private bool _requireChangeTarget = false;
  128. /// <summary>
  129. /// Require that the target location be changed before allowing the "Save" button to be clicked.
  130. /// </summary>
  131. public bool RequireChangeTarget
  132. {
  133. get => _requireChangeTarget;
  134. set
  135. {
  136. _requireChangeTarget = value;
  137. OnPropertyChanged();
  138. OnPropertyChanged(nameof(CanSave));
  139. }
  140. }
  141. public bool CanSave
  142. {
  143. get
  144. {
  145. if(Grid.TotalIssued <= 0)
  146. {
  147. return false;
  148. }
  149. if(IsTargetEditable && (To?.ID ?? Guid.Empty) == Guid.Empty)
  150. {
  151. return false;
  152. }
  153. if (RequireChangeTarget)
  154. {
  155. if(IsTargetEditable && IsJobEditable)
  156. {
  157. return (To?.ID ?? Guid.Empty) != (OriginalTarget?.ID ?? Guid.Empty)
  158. || (Job?.ID ?? Guid.Empty) != (OriginalJob?.ID ?? Guid.Empty);
  159. }
  160. else if (IsTargetEditable)
  161. {
  162. return (To?.ID ?? Guid.Empty) != (OriginalTarget?.ID ?? Guid.Empty);
  163. }
  164. else if (IsJobEditable)
  165. {
  166. return (Job?.ID ?? Guid.Empty) != (OriginalJob?.ID ?? Guid.Empty);
  167. }
  168. }
  169. return true;
  170. }
  171. }
  172. public StockHoldingRelocationWindow(StockHolding from, IEnumerable<JobRequisitionItem> items, RelocationMode mode)
  173. {
  174. Client.EnsureColumns(from, Columns.None<StockHolding>().Add(x => x.Location.Code));
  175. From = from;
  176. InitializeComponent();
  177. SetRequisitionItems(items);
  178. Mode = mode;
  179. }
  180. private void UpdateMode()
  181. {
  182. if(Mode == RelocationMode.Issue)
  183. {
  184. var jobID = Job?.ID ?? Guid.Empty;
  185. foreach(var item in Grid.Items)
  186. {
  187. if(item.JRI.ID != Guid.Empty && item.JRI.Job.ID != jobID)
  188. {
  189. item.Editable = false;
  190. item.Issued = 0;
  191. }
  192. else
  193. {
  194. item.Editable = true;
  195. }
  196. }
  197. Title = "Issue Items";
  198. IsJobEditable = true;
  199. IsTargetEditable = false;
  200. ShowFrom = true;
  201. }
  202. else if(Mode == RelocationMode.Transfer)
  203. {
  204. foreach(var item in Grid.Items)
  205. {
  206. item.Editable = item.JRI.ID == Guid.Empty || Security.IsAllowed<CanEditAllocatedJobRequisitions>();
  207. }
  208. IsJobEditable = true;
  209. IsTargetEditable = true;
  210. ShowFrom = false;
  211. RequireChangeTarget = true;
  212. Title = "Transfer Stock";
  213. }
  214. else if(Mode == RelocationMode.ReleaseAllocations)
  215. {
  216. ShowFrom = false;
  217. IsTargetEditable = false;
  218. IsJobEditable = false;
  219. Title = "Release Stock";
  220. }
  221. }
  222. private void Grid_PropertyChanged(object sender, PropertyChangedEventArgs e)
  223. {
  224. if(e.PropertyName == nameof(Grid.TotalIssued))
  225. {
  226. OnPropertyChanged(nameof(CanSave));
  227. }
  228. }
  229. public void SetRequisitionItems(IEnumerable<JobRequisitionItem> items)
  230. {
  231. var rItems = items.AsIList();
  232. var rIDs = rItems.Select(x => x.ID).Where(x => x != Guid.Empty).ToArray();
  233. var quantities = Client.Query(
  234. StockHolding.GetFilter(From)
  235. .Combine(new Filter<StockMovement>(x => x.JobRequisitionItem.ID).InList(rIDs)),
  236. Columns.None<StockMovement>().Add(x => x.Units).Add(x => x.JobRequisitionItem.ID))
  237. .ToObjects<StockMovement>().GroupBy(x => x.JobRequisitionItem.ID).ToDictionary(x => x.Key, x => x.Sum(x => x.Units));
  238. var requidItems = new List<StockHoldingRelocationItem>();
  239. var newItems = new List<StockHoldingRelocationItem>();
  240. foreach(var item in rItems)
  241. {
  242. var qty = item.ID != Guid.Empty ? quantities.GetValueOrDefault(item.ID) : item.Qty;
  243. if(item.ID != Guid.Empty && qty <= 0)
  244. {
  245. // We can't do anything with this; it shouldn't be shown.
  246. continue;
  247. }
  248. var newItem = new StockHoldingRelocationItem
  249. {
  250. Issued = 0,
  251. Quantity = qty,
  252. MaxValue = item.ID == Guid.Empty ? double.MaxValue : qty,
  253. Text = item.ID == Guid.Empty
  254. ? "Unrequisitioned Items"
  255. : $"{item.Job.JobNumber}: #{item.Requisition.Number} ({item.Requisition.Description})",
  256. JRI = item
  257. };
  258. if(item.ID == Guid.Empty)
  259. {
  260. newItems.Add(newItem);
  261. }
  262. else
  263. {
  264. requidItems.Add(newItem);
  265. }
  266. }
  267. int i = 1;
  268. foreach(var item in requidItems)
  269. {
  270. item.ItemNumber = $"{i}. ";
  271. newItems.Add(item);
  272. ++i;
  273. }
  274. Grid.Items = newItems;
  275. Grid.Refresh(true, true);
  276. }
  277. public Dictionary<Guid, double> GetQuantities()
  278. {
  279. return Grid.Items.ToDictionary(x => x.JRI.ID, x => x.Issued);
  280. }
  281. public StockLocation GetTargetLocation()
  282. {
  283. return To ?? new StockLocation();
  284. }
  285. public event PropertyChangedEventHandler? PropertyChanged;
  286. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  287. {
  288. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  289. }
  290. #region Target Location
  291. public static StockLocation? LookupLocation(string? column = null, string? value = null)
  292. {
  293. var grid = new MultiSelectDialog<StockLocation>(
  294. LookupFactory.DefineFilter<StockLocation>(),
  295. Columns.None<StockLocation>().Add(x => x.ID).Add(x => x.Code).Add(x => x.Description),
  296. multiselect: false);
  297. if (grid.ShowDialog(column, value))
  298. {
  299. return grid.Data().Rows.First().ToObject<StockLocation>();
  300. }
  301. else
  302. {
  303. return null;
  304. }
  305. }
  306. private bool DoLookupLocation(string? column, string? value)
  307. {
  308. if(LookupLocation(column, value) is StockLocation location)
  309. {
  310. To = location;
  311. return true;
  312. }
  313. else
  314. {
  315. return false;
  316. }
  317. }
  318. private void ToBox_LostFocus(object sender, RoutedEventArgs e)
  319. {
  320. DoSearchLocation();
  321. }
  322. private void ToButton_Click(object sender, RoutedEventArgs e)
  323. {
  324. DoLookupLocation(null, null);
  325. }
  326. private void DoSearchLocation()
  327. {
  328. if (ToBox.Text.IsNullOrWhiteSpace() || ToBox.Text == To?.Code)
  329. {
  330. To = null;
  331. return;
  332. }
  333. var location = Client.Query(
  334. new Filter<StockLocation>(x => x.Code).IsEqualTo(ToBox.Text),
  335. Columns.None<StockLocation>().Add(x => x.ID).Add(x => x.Code).Add(x => x.Description))
  336. .ToObjects<StockLocation>().FirstOrDefault();
  337. if(location is not null)
  338. {
  339. To = location;
  340. }
  341. else
  342. {
  343. if(!DoLookupLocation(nameof(To.Code), ToBox.Text))
  344. {
  345. To = null;
  346. }
  347. }
  348. }
  349. private void ToBox_OnKeyUp(object sender, KeyEventArgs e)
  350. {
  351. if (e.Key == Key.Enter)
  352. DoSearchLocation();
  353. }
  354. #endregion
  355. #region Job
  356. private bool DoLookupJob(string? column, string? value)
  357. {
  358. var grid = new MultiSelectDialog<Job>(
  359. LookupFactory.DefineFilter<Job>(),
  360. Columns.None<Job>().Add(x => x.ID).Add(x => x.JobNumber).Add(x => x.Name),
  361. multiselect: false);
  362. if (grid.ShowDialog(column, value))
  363. {
  364. Job = grid.Data().Rows.First().ToObject<Job>();
  365. return true;
  366. }
  367. else
  368. {
  369. return false;
  370. }
  371. }
  372. private void JobBox_LostFocus(object sender, RoutedEventArgs e)
  373. {
  374. DoSearchJob();
  375. }
  376. private void JobButton_Click(object sender, RoutedEventArgs e)
  377. {
  378. DoLookupJob(null, null);
  379. }
  380. private void DoSearchJob()
  381. {
  382. if (JobBox.Text.IsNullOrWhiteSpace() || JobBox.Text == To?.Code)
  383. {
  384. Job = null;
  385. return;
  386. }
  387. var job = Client.Query(
  388. new Filter<Job>(x => x.JobNumber).IsEqualTo(JobBox.Text),
  389. Columns.None<Job>().Add(x => x.ID).Add(x => x.JobNumber).Add(x => x.Name))
  390. .ToObjects<Job>().FirstOrDefault();
  391. if(job is not null)
  392. {
  393. Job = job;
  394. }
  395. else
  396. {
  397. if(!DoLookupJob(nameof(Job.JobNumber), JobBox.Text))
  398. {
  399. Job = null;
  400. }
  401. }
  402. }
  403. private void JobBox_OnKeyUp(object sender, KeyEventArgs e)
  404. {
  405. if (e.Key == Key.Enter)
  406. DoSearchJob();
  407. }
  408. #endregion
  409. private void OKButton_Click(object sender, RoutedEventArgs e)
  410. {
  411. DialogResult = true;
  412. Close();
  413. }
  414. private void CancelButton_Click(object sender, RoutedEventArgs e)
  415. {
  416. DialogResult = false;
  417. Close();
  418. }
  419. }