StockHoldingGrid.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.WPF;
  11. namespace PRSDesktop
  12. {
  13. public class StockHoldingGrid : DynamicDataGrid<StockHolding>
  14. {
  15. private readonly Guid _employeeid = Guid.Empty;
  16. private bool bIssuing;
  17. private bool bReceiving;
  18. //bool bReserving = false;
  19. private readonly bool bReleasing = false;
  20. private bool bTransferring;
  21. private readonly Button IssueButton;
  22. private readonly Button ReceiveButton;
  23. private DynamicDataGrid<StockMovement> smg;
  24. //Button ReserveButton = null;
  25. private readonly Button TransferButton;
  26. public StockHoldingGrid() : base()
  27. {
  28. ColumnsTag = "StockHolding";
  29. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns, DynamicGridOption.FilterRows);
  30. ReceiveButton = AddButton("Receive", PRSDesktop.Resources.add.AsBitmapImage(), ReceiveStock);
  31. ReceiveButton.IsEnabled = false;
  32. IssueButton = AddButton("Issue", PRSDesktop.Resources.delete.AsBitmapImage(), IssueStock);
  33. IssueButton.IsEnabled = false;
  34. //ReserveButton = AddButton("Reserve", PRSDesktop.Resources.project.AsBitmapImage(), ReserveStock);
  35. //ReserveButton.Margin = new Thickness(20, ReserveButton.Margin.Top, ReserveButton.Margin.Right, ReserveButton.Margin.Bottom);
  36. //ReserveButton.IsEnabled = false;
  37. TransferButton = AddButton("Transfer", PRSDesktop.Resources.split.AsBitmapImage(), TransferStock);
  38. TransferButton.Margin = new Thickness(20, TransferButton.Margin.Top, TransferButton.Margin.Right, TransferButton.Margin.Bottom);
  39. TransferButton.IsEnabled = false;
  40. OnSelectItem += SelectItem;
  41. HiddenColumns.Add(x => x.Product.ID);
  42. HiddenColumns.Add(x => x.Product.Deleted);
  43. HiddenColumns.Add(x => x.Job.ID);
  44. HiddenColumns.Add(x => x.Job.JobNumber);
  45. HiddenColumns.Add(x => x.Job.Deleted);
  46. HiddenColumns.Add(x => x.Location.ID);
  47. HiddenColumns.Add(x => x.Location.Code);
  48. HiddenColumns.Add(x => x.Location.Deleted);
  49. HiddenColumns.Add(x => x.Style.ID);
  50. HiddenColumns.Add(x => x.Style.Code);
  51. HiddenColumns.Add(x => x.Style.Deleted);
  52. HiddenColumns.Add(x => x.Qty);
  53. HiddenColumns.Add(x => x.Units);
  54. HiddenColumns.Add(x => x.Dimensions.Unit.ID);
  55. HiddenColumns.Add(x => x.Dimensions.Unit.Description);
  56. HiddenColumns.Add(x => x.Dimensions.Unit.HasHeight);
  57. HiddenColumns.Add(x => x.Dimensions.Unit.HasLength);
  58. HiddenColumns.Add(x => x.Dimensions.Unit.HasWidth);
  59. HiddenColumns.Add(x => x.Dimensions.Unit.HasHeight);
  60. HiddenColumns.Add(x => x.Dimensions.Unit.HasQuantity);
  61. HiddenColumns.Add(x => x.Dimensions.Unit.Format);
  62. HiddenColumns.Add(x => x.Dimensions.Unit.Formula);
  63. HiddenColumns.Add(x => x.Dimensions.Length);
  64. HiddenColumns.Add(x => x.Dimensions.Width);
  65. HiddenColumns.Add(x => x.Dimensions.Height);
  66. HiddenColumns.Add(x => x.Dimensions.Quantity);
  67. HiddenColumns.Add(x => x.Dimensions.Value);
  68. _employeeid = GetEmployeeID();
  69. }
  70. public IStockLocation Location { get; set; }
  71. private void SelectItem(object sender, DynamicGridSelectionEventArgs e)
  72. {
  73. ReceiveButton.IsEnabled = Location != null && Location.ID != Guid.Empty;
  74. IssueButton.IsEnabled = Location != null && Location.ID != Guid.Empty && e.Rows?.Any() == true;
  75. TransferButton.IsEnabled = Location != null && Location.ID != Guid.Empty && e.Rows?.Any() == true;
  76. }
  77. private void CheckStockMovementGrid()
  78. {
  79. if (smg == null)
  80. {
  81. smg = new DynamicDataGrid<StockMovement>();
  82. smg.OnCustomiseEditor += StockMovementCustomiseEditor;
  83. smg.OnValidate += StockMovementValidate;
  84. smg.OnEditorValueChanged += StockMovementValueChanged;
  85. }
  86. }
  87. private Guid GetEmployeeID()
  88. {
  89. var employee = new Client<Employee>().Query(
  90. new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid),
  91. new Columns<Employee>(x => x.ID)
  92. );
  93. return employee.Rows.Any() ? employee.Rows.First().Get<Employee, Guid>(x => x.ID) : Guid.Empty;
  94. }
  95. private Dictionary<string, object?> StockMovementValueChanged(object sender, string name, object value)
  96. {
  97. var result = new Dictionary<string, object>();
  98. if (name.Equals("Location.Job.ID"))
  99. {
  100. var form = sender as IDynamicEditorForm;
  101. var editor = form.FindEditor("Job.ID");
  102. if (!value.Equals(Guid.Empty))
  103. result = DynamicGridUtils.UpdateEditorValue(form.Items, "Job.ID", value);
  104. else
  105. foreach (StockMovement item in form.Items)
  106. result = DynamicGridUtils.UpdateEditorValue(new[] { item }, "Job.ID",
  107. item.Job.HasOriginalValue("ID") ? item.Job.GetOriginalValue(x => x.ID) : item.Job.ID);
  108. editor.IsEnabled = value.Equals(Guid.Empty);
  109. }
  110. return result;
  111. }
  112. private void StockMovementValidate(object sender, StockMovement[] items, List<string> errors)
  113. {
  114. var bQty = false;
  115. //bool bJob = false;
  116. var bProd = false;
  117. var bLocn = false;
  118. var bStyle = false;
  119. foreach (var item in items)
  120. {
  121. bQty = bQty || item.Qty == 0.0F;
  122. bProd = bProd || !item.Product.IsValid();
  123. bLocn = bLocn || !item.Location.IsValid();
  124. bStyle = bStyle || !item.Style.IsValid();
  125. }
  126. if (bQty)
  127. errors.Add("Quantity may not be zero");
  128. if (bProd)
  129. errors.Add("Product may not be blank");
  130. if (bLocn)
  131. errors.Add("Location may not be blank");
  132. //if (bStyle)
  133. // result.Add("Style may not be blank");
  134. if (!errors.Any() && bTransferring)
  135. foreach (var item in items)
  136. {
  137. var changes = new List<string>();
  138. if (item.Location.HasOriginalValue(x => x.ID))
  139. changes.Add(item.Location.GetOriginalValue(x => x.Code));
  140. if (item.Job.HasOriginalValue(x => x.ID))
  141. {
  142. var job = item.Job.GetOriginalValue(x => x.JobNumber);
  143. if (string.IsNullOrEmpty(job))
  144. job = "General Stock";
  145. changes.Add(job);
  146. }
  147. if (item.Style.HasOriginalValue(x => x.ID))
  148. changes.Add(item.Style.GetOriginalValue(x => x.Code));
  149. if (changes.Any())
  150. item.Notes = string.Format("Transferred from {0}{1}{2}",
  151. string.Join(" / ", changes.Where(x => !string.IsNullOrWhiteSpace(x))),
  152. string.IsNullOrWhiteSpace(item.Notes) ? "" : "\n", item.Notes);
  153. else
  154. errors.Add("Transfers must change either Location, Style or Job");
  155. }
  156. }
  157. private void StockMovementCustomiseEditor(IDynamicEditorForm sender, StockMovement[]? items, DynamicGridColumn column, BaseEditor editor)
  158. {
  159. if (column.ColumnName.Equals("Location.ID"))
  160. editor.Editable = bTransferring ? Editable.Enabled : Editable.Hidden;
  161. if (column.ColumnName.Equals("Product.NettCost"))
  162. editor.Editable = Editable.Hidden;
  163. if (column.ColumnName.Equals("Style.ID"))
  164. editor.Editable = bReceiving || bTransferring ? Editable.Enabled : Editable.Hidden;
  165. if (column.ColumnName.Equals("Received"))
  166. {
  167. editor.Editable = bReceiving || bTransferring || bReleasing ? Editable.Enabled : Editable.Hidden;
  168. editor.Caption = "Quantity";
  169. }
  170. if (column.ColumnName.Equals("Product.ID"))
  171. editor.Editable = bReceiving ? Editable.Enabled : Editable.Hidden;
  172. if (column.ColumnName.Equals("UnitSize"))
  173. editor.Editable = bReceiving ? Editable.Enabled : Editable.Hidden;
  174. if (column.ColumnName.Equals("Job.ID"))
  175. editor.Editable = bReceiving && items?.FirstOrDefault()?.Job.IsValid() == true ? Editable.Disabled : Editable.Enabled;
  176. if (column.ColumnName.Equals("Issued"))
  177. {
  178. editor.Editable = bIssuing ? Editable.Enabled : Editable.Hidden;
  179. editor.Caption = "Quantity";
  180. }
  181. if (column.ColumnName.Equals("Employee.ID"))
  182. editor.Editable = bTransferring || bReleasing ? Editable.Hidden : Editable.Enabled;
  183. if (column.ColumnName.Equals("Units"))
  184. editor.Editable = Editable.Hidden;
  185. }
  186. private bool ReceiveStock(Button arg1, CoreRow[] rows)
  187. {
  188. CheckStockMovementGrid();
  189. var movement = new StockMovement();
  190. movement.Location.ID = Location.ID;
  191. movement.Location.Code = Location.Code;
  192. movement.Job.ID = Location.Job.ID;
  193. movement.Job.JobNumber = Location.Job.JobNumber;
  194. movement.Job.Name = Location.Job.Name;
  195. movement.Location.Description = Location.Description;
  196. movement.Date = DateTime.Now;
  197. movement.IsTransfer = false;
  198. movement.Employee.ID = _employeeid;
  199. movement.CommitChanges();
  200. bReceiving = true;
  201. var result = smg.EditItems(new[] { movement });
  202. bReceiving = false;
  203. if (result)
  204. DoChanged();
  205. return result;
  206. }
  207. private bool IssueStock(Button arg1, CoreRow[] rows)
  208. {
  209. if (!rows.Any())
  210. {
  211. MessageBox.Show("Please select an item to issue");
  212. return false;
  213. }
  214. CheckStockMovementGrid();
  215. var holding = rows.First().ToObject<StockHolding>();
  216. // var jobid = rows.First().Get<StockHolding, Guid>(x => x.Job.ID);
  217. // var jobno = rows.First().Get<StockHolding, string>(x => x.Job.JobNumber);
  218. // var productid = rows.First().Get<StockHolding, Guid>(x => x.Product.ID);
  219. // var styleid = rows.First().Get<StockHolding, Guid>(x => x.Style.ID);
  220. // var stylecode = rows.First().Get<StockHolding, string>(x => x.Style.Code);
  221. // var size = rows.First().Get<StockHolding, double>(x => x.UnitSize);
  222. var movement = new StockMovement();
  223. movement.Date = DateTime.Now;
  224. movement.Location.ID = Location.ID;
  225. movement.Location.Code = Location.Code;
  226. movement.Location.Description = Location.Description;
  227. movement.Product.ID = holding.Product.ID;
  228. movement.Job.ID = holding.Job.ID;
  229. movement.Job.JobNumber = holding.Job.JobNumber;
  230. movement.Style.ID = holding.Style.ID;
  231. movement.Style.Code = holding.Style.Code;
  232. movement.Dimensions.CopyFrom(holding.Dimensions);
  233. movement.IsTransfer = false;
  234. movement.Employee.ID = _employeeid;
  235. movement.CommitChanges();
  236. bIssuing = true;
  237. var result = smg.EditItems(new[] { movement });
  238. bIssuing = false;
  239. // Issuing to a different job than you received into?
  240. if (result && holding.Job.ID != movement.Job.ID)
  241. {
  242. // Issue from Old Job (Hidden)
  243. var issue = new StockMovement();
  244. issue.Date = movement.Date;
  245. issue.Product.ID = holding.Product.ID;
  246. issue.Job.ID = holding.Job.ID;
  247. issue.Location.ID = Location.ID;
  248. issue.Style.ID = holding.Style.ID;
  249. issue.Issued = movement.Issued;
  250. issue.Transaction = movement.Transaction;
  251. issue.Employee.ID = movement.Employee.ID;
  252. issue.Dimensions.CopyFrom(holding.Dimensions);
  253. issue.IsTransfer = true;
  254. issue.Notes = string.Format("Transferred from {0}", holding.Job.ID.Equals(Guid.Empty) ? "General Stock" : "Job " + holding.Job.JobNumber);
  255. issue.System = true;
  256. // Receive to New Job (Hidden)
  257. var receive = new StockMovement();
  258. receive.Date = movement.Date;
  259. receive.Product.ID = holding.Product.ID;
  260. receive.Job.ID = movement.Job.ID;
  261. receive.Location.ID = Location.ID;
  262. receive.Style.ID = holding.Style.ID;
  263. receive.Received = movement.Issued;
  264. receive.Transaction = movement.Transaction;
  265. receive.Employee.ID = movement.Employee.ID;
  266. receive.Dimensions.CopyFrom(holding.Dimensions);
  267. receive.IsTransfer = true;
  268. receive.Notes = string.Format("Transferred to {0}",
  269. !movement.Job.IsValid() ? "General Stock" : "Job " + movement.Job.JobNumber);
  270. receive.System = true;
  271. new Client<StockMovement>().Save(new[] { issue, receive }, "");
  272. }
  273. if (result)
  274. DoChanged();
  275. return result;
  276. }
  277. private bool TransferStock(Button arg1, CoreRow[] rows)
  278. {
  279. CheckStockMovementGrid();
  280. var holding = rows.First().ToObject<StockHolding>();
  281. // var jobid = rows.First().Get<StockHolding, Guid>(x => x.Job.ID);
  282. // var jobno = rows.First().Get<StockHolding, string>(x => x.Job.JobNumber);
  283. // var productid = rows.First().Get<StockHolding, Guid>(x => x.Product.ID);
  284. // var styleid = rows.First().Get<StockHolding, Guid>(x => x.Style.ID);
  285. // var stylecode = rows.First().Get<StockHolding, string>(x => x.Style.Code);
  286. // var units = rows.First().Get<StockHolding, double>(x => x.Units);
  287. // var size = rows.First().Get<StockHolding, double>(x => x.UnitSize);
  288. var movement = new StockMovement();
  289. movement.Date = DateTime.Now;
  290. movement.Location.ID = Location.ID;
  291. movement.Location.Code = Location.Code;
  292. movement.Location.Description = Location.Description;
  293. movement.Product.ID = holding.Product.ID;
  294. movement.Job.ID = holding.Job.ID;
  295. movement.Job.JobNumber = holding.Job.JobNumber;
  296. movement.Style.ID = holding.Style.ID;
  297. movement.Style.Code = holding.Style.Code;
  298. movement.Employee.ID = _employeeid;
  299. // Must happen before Received gets set so that OnPropertyChanged has stuff to work with and thus Qty is not zero.
  300. movement.Dimensions.CopyFrom(holding.Dimensions);
  301. movement.Received = holding.Units;
  302. movement.IsTransfer = true;
  303. movement.CommitChanges();
  304. bTransferring = true;
  305. var result = smg.EditItems(new[] { movement });
  306. bTransferring = false;
  307. if (result)
  308. {
  309. var other = new StockMovement();
  310. other.Date = movement.Date;
  311. other.Product.ID = holding.Product.ID;
  312. other.Job.ID = holding.Job.ID;
  313. other.Location.ID = Location.ID;
  314. //other.Location.Code = Location.Code;
  315. //other.Location.Description = Location.Description;
  316. other.Style.ID = holding.Style.ID;
  317. other.Issued = movement.Received;
  318. other.Transaction = movement.Transaction;
  319. other.Employee.ID = movement.Employee.ID;
  320. other.Dimensions.CopyFrom(holding.Dimensions);
  321. other.IsTransfer = true;
  322. var changes = new List<string>();
  323. if (movement.Location.ID != other.Location.ID)
  324. changes.Add(movement.Location.Code);
  325. if (movement.Job.ID != other.Job.ID)
  326. changes.Add(string.IsNullOrEmpty(movement.Job.JobNumber) ? "General Stock" : movement.Job.JobNumber);
  327. if (movement.Style.ID != other.Style.ID)
  328. changes.Add(movement.Style.Code);
  329. //other.Notes = "Transferred to "+String.Join(" / ",changes);
  330. other.Notes = string.Format("Transferred to {0}{1}{2}", string.Join(" / ",
  331. changes.Where(x => !string.IsNullOrWhiteSpace(x))),
  332. string.IsNullOrWhiteSpace(movement.Notes) ? "" : "\n",
  333. string.Join("\n", movement.Notes.Split('\n').Where(x => !x.StartsWith("Transferred from ")))
  334. );
  335. other.System = true;
  336. new Client<StockMovement>().Save(other, "");
  337. }
  338. if (result)
  339. DoChanged();
  340. return result;
  341. }
  342. //private bool ReserveStock(Button arg1, CoreRow[] rows)
  343. //{
  344. // if (!rows.Any())
  345. // {
  346. // MessageBox.Show("Please select an item to transfer");
  347. // return false;
  348. // }
  349. // CheckStockMovementGrid();
  350. // Guid jobid = rows.First().Get<StockHolding, Guid>(x => x.Job.ID);
  351. // String jobno = rows.First().Get<StockHolding, String>(x => x.Job.JobNumber);
  352. // Guid productid = rows.First().Get<StockHolding, Guid>(x => x.Product.ID);
  353. // Guid styleid = rows.First().Get<StockHolding, Guid>(x => x.Style.ID);
  354. // double units = rows.First().Get<StockHolding, double>(x => x.Units);
  355. // double size = rows.First().Get<StockHolding, double>(x => x.UnitSize);
  356. // StockMovement movement = new StockMovement();
  357. // movement.Date = DateTime.Now;
  358. // movement.Location.ID = LocationID;
  359. // movement.Product.ID = productid;
  360. // movement.Style.ID = styleid;
  361. // movement.Employee.ID = GetEmployeeID();
  362. // movement.Job.ID = Guid.Empty;
  363. // movement.Received = units;
  364. // movement.UnitSize = size;
  365. // bReserving = jobid.Equals(Guid.Empty);
  366. // bReleasing = !bReserving;
  367. // movement.Notes = bReserving ? "Reserving Stock" : "Releasing from Job " + jobno;
  368. // bool result = smg.EditItems(new StockMovement[] { movement });
  369. // Issuing to a different job than you received into ?
  370. // if (result)
  371. // {
  372. // Issue from Old Job(Hidden)
  373. // StockMovement other = new StockMovement();
  374. // other.Date = movement.Date;
  375. // other.Product.ID = productid;
  376. // other.Job.ID = jobid;
  377. // other.Location.ID = LocationID;
  378. // other.Style.ID = styleid;
  379. // other.Issued = movement.Received;
  380. // other.Transaction = movement.Transaction;
  381. // other.Employee.ID = movement.Employee.ID;
  382. // other.UnitSize = size;
  383. // other.Notes = movement.Job.ID.Equals(Guid.Empty) ? "Releasing back to General Stock" : "Reserving for Job " + movement.Job.JobNumber;
  384. // other.System = true;
  385. // new Client<StockMovement>().Save(other, "");
  386. // }
  387. // bReserving = false;
  388. // bReleasing = false;
  389. // if (result)
  390. // OnChanged?.Invoke(this);
  391. // return result;
  392. //}
  393. protected override void Reload(Filters<StockHolding> criteria, Columns<StockHolding> columns, ref SortOrder<StockHolding> sort,
  394. Action<CoreTable, Exception> action)
  395. {
  396. ReceiveButton.IsEnabled = Location != null && Location.ID != Guid.Empty;
  397. if (Location == null)
  398. criteria.Add(new Filter<StockHolding>().None());
  399. else
  400. criteria.Add(new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(Location.ID));
  401. base.Reload(criteria, columns, ref sort, action);
  402. }
  403. protected override bool FilterRecord(CoreRow row)
  404. {
  405. // Hackety Hackety Hack Hack stupid doubles not totalling zero when they're supposed to
  406. var result = base.FilterRecord(row);
  407. if (result)
  408. result = !row.Get<StockHolding, double>(x => x.Qty).ToString("F8").Equals("0.00000000");
  409. return result;
  410. }
  411. }
  412. }