StockHoldingGrid.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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.Received == 0 && item.Issued == 0);
  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. {
  205. DoChanged();
  206. SaveBatch(StockMovementBatchType.Receipt, new StockMovement[] { movement});
  207. }
  208. return result;
  209. }
  210. private void SaveBatch(StockMovementBatchType type, StockMovement[] movements)
  211. {
  212. StockMovementBatch batch = new StockMovementBatch();
  213. batch.Type = type;
  214. batch.Notes = batch.Type + " batch created from Desktop Stock Location Screen";
  215. batch.Employee.ID = _employeeid;
  216. new Client<StockMovementBatch>().Save(batch, "created from Desktop Stock Location Screen");
  217. foreach (var mvt in movements)
  218. {
  219. mvt.Batch.ID = batch.ID;
  220. }
  221. new Client<StockMovement>().Save(movements, "Updating batch from Desktop Stock Location Screen");
  222. }
  223. private bool IssueStock(Button arg1, CoreRow[] rows)
  224. {
  225. if (!rows.Any())
  226. {
  227. MessageBox.Show("Please select an item to issue");
  228. return false;
  229. }
  230. CheckStockMovementGrid();
  231. var holding = rows.First().ToObject<StockHolding>();
  232. // var jobid = rows.First().Get<StockHolding, Guid>(x => x.Job.ID);
  233. // var jobno = rows.First().Get<StockHolding, string>(x => x.Job.JobNumber);
  234. // var productid = rows.First().Get<StockHolding, Guid>(x => x.Product.ID);
  235. // var styleid = rows.First().Get<StockHolding, Guid>(x => x.Style.ID);
  236. // var stylecode = rows.First().Get<StockHolding, string>(x => x.Style.Code);
  237. // var size = rows.First().Get<StockHolding, double>(x => x.UnitSize);
  238. var movement = new StockMovement();
  239. movement.Date = DateTime.Now;
  240. movement.Location.ID = Location.ID;
  241. movement.Location.Code = Location.Code;
  242. movement.Location.Description = Location.Description;
  243. movement.Product.ID = holding.Product.ID;
  244. movement.Job.ID = holding.Job.ID;
  245. movement.Job.JobNumber = holding.Job.JobNumber;
  246. movement.Style.ID = holding.Style.ID;
  247. movement.Style.Code = holding.Style.Code;
  248. movement.Dimensions.CopyFrom(holding.Dimensions);
  249. movement.IsTransfer = false;
  250. movement.Employee.ID = _employeeid;
  251. movement.CommitChanges();
  252. bIssuing = true;
  253. var result = smg.EditItems(new[] { movement });
  254. bIssuing = false;
  255. var mvts = new List<StockMovement>
  256. {
  257. movement
  258. };
  259. // Issuing to a different job than you received into?
  260. if (result && holding.Job.ID != movement.Job.ID)
  261. {
  262. // Issue from Old Job (Hidden)
  263. var issue = new StockMovement();
  264. issue.Date = movement.Date;
  265. issue.Product.ID = holding.Product.ID;
  266. issue.Job.ID = holding.Job.ID;
  267. issue.Location.ID = Location.ID;
  268. issue.Style.ID = holding.Style.ID;
  269. issue.Issued = movement.Issued;
  270. issue.Transaction = movement.Transaction;
  271. issue.Employee.ID = movement.Employee.ID;
  272. issue.Dimensions.CopyFrom(holding.Dimensions);
  273. issue.IsTransfer = true;
  274. issue.Notes = string.Format("Transferred from {0}", holding.Job.ID.Equals(Guid.Empty) ? "General Stock" : "Job " + holding.Job.JobNumber);
  275. issue.System = true;
  276. // Receive to New Job (Hidden)
  277. var receive = new StockMovement();
  278. receive.Date = movement.Date;
  279. receive.Product.ID = holding.Product.ID;
  280. receive.Job.ID = movement.Job.ID;
  281. receive.Location.ID = Location.ID;
  282. receive.Style.ID = holding.Style.ID;
  283. receive.Received = movement.Issued;
  284. receive.Transaction = movement.Transaction;
  285. receive.Employee.ID = movement.Employee.ID;
  286. receive.Dimensions.CopyFrom(holding.Dimensions);
  287. receive.IsTransfer = true;
  288. receive.Notes = string.Format("Transferred to {0}",
  289. !movement.Job.IsValid() ? "General Stock" : "Job " + movement.Job.JobNumber);
  290. receive.System = true;
  291. new Client<StockMovement>().Save(new[] { issue, receive }, "");
  292. mvts.Add(issue);
  293. mvts.Add(receive);
  294. }
  295. if (result)
  296. {
  297. DoChanged();
  298. SaveBatch(StockMovementBatchType.Issue, mvts.ToArray());
  299. }
  300. return result;
  301. }
  302. private bool TransferStock(Button arg1, CoreRow[] rows)
  303. {
  304. CheckStockMovementGrid();
  305. var holding = rows.First().ToObject<StockHolding>();
  306. // var jobid = rows.First().Get<StockHolding, Guid>(x => x.Job.ID);
  307. // var jobno = rows.First().Get<StockHolding, string>(x => x.Job.JobNumber);
  308. // var productid = rows.First().Get<StockHolding, Guid>(x => x.Product.ID);
  309. // var styleid = rows.First().Get<StockHolding, Guid>(x => x.Style.ID);
  310. // var stylecode = rows.First().Get<StockHolding, string>(x => x.Style.Code);
  311. // var units = rows.First().Get<StockHolding, double>(x => x.Units);
  312. // var size = rows.First().Get<StockHolding, double>(x => x.UnitSize);
  313. var movement = new StockMovement();
  314. movement.Date = DateTime.Now;
  315. movement.Location.ID = Location.ID;
  316. movement.Location.Code = Location.Code;
  317. movement.Location.Description = Location.Description;
  318. movement.Product.ID = holding.Product.ID;
  319. movement.Job.ID = holding.Job.ID;
  320. movement.Job.JobNumber = holding.Job.JobNumber;
  321. movement.Style.ID = holding.Style.ID;
  322. movement.Style.Code = holding.Style.Code;
  323. movement.Employee.ID = _employeeid;
  324. // Must happen before Received gets set so that OnPropertyChanged has stuff to work with and thus Qty is not zero.
  325. movement.Dimensions.CopyFrom(holding.Dimensions);
  326. movement.Received = holding.Units;
  327. movement.IsTransfer = true;
  328. movement.CommitChanges();
  329. bTransferring = true;
  330. var result = smg.EditItems(new[] { movement });
  331. bTransferring = false;
  332. var mvts = new List<StockMovement>
  333. {
  334. movement
  335. };
  336. if (result)
  337. {
  338. var other = new StockMovement();
  339. other.Date = movement.Date;
  340. other.Product.ID = holding.Product.ID;
  341. other.Job.ID = holding.Job.ID;
  342. other.Location.ID = Location.ID;
  343. //other.Location.Code = Location.Code;
  344. //other.Location.Description = Location.Description;
  345. other.Style.ID = holding.Style.ID;
  346. other.Issued = movement.Received;
  347. other.Transaction = movement.Transaction;
  348. other.Employee.ID = movement.Employee.ID;
  349. other.Dimensions.CopyFrom(holding.Dimensions);
  350. other.IsTransfer = true;
  351. var changes = new List<string>();
  352. if (movement.Location.ID != other.Location.ID)
  353. changes.Add(movement.Location.Code);
  354. if (movement.Job.ID != other.Job.ID)
  355. changes.Add(string.IsNullOrEmpty(movement.Job.JobNumber) ? "General Stock" : movement.Job.JobNumber);
  356. if (movement.Style.ID != other.Style.ID)
  357. changes.Add(movement.Style.Code);
  358. //other.Notes = "Transferred to "+String.Join(" / ",changes);
  359. other.Notes = string.Format("Transferred to {0}{1}{2}", string.Join(" / ",
  360. changes.Where(x => !string.IsNullOrWhiteSpace(x))),
  361. string.IsNullOrWhiteSpace(movement.Notes) ? "" : "\n",
  362. string.Join("\n", movement.Notes.Split('\n').Where(x => !x.StartsWith("Transferred from ")))
  363. );
  364. other.System = true;
  365. new Client<StockMovement>().Save(other, "");
  366. mvts.Add(other);
  367. }
  368. if (result)
  369. {
  370. DoChanged();
  371. SaveBatch(StockMovementBatchType.Transfer, mvts.ToArray());
  372. }
  373. return result;
  374. }
  375. //private bool ReserveStock(Button arg1, CoreRow[] rows)
  376. //{
  377. // if (!rows.Any())
  378. // {
  379. // MessageBox.Show("Please select an item to transfer");
  380. // return false;
  381. // }
  382. // CheckStockMovementGrid();
  383. // Guid jobid = rows.First().Get<StockHolding, Guid>(x => x.Job.ID);
  384. // String jobno = rows.First().Get<StockHolding, String>(x => x.Job.JobNumber);
  385. // Guid productid = rows.First().Get<StockHolding, Guid>(x => x.Product.ID);
  386. // Guid styleid = rows.First().Get<StockHolding, Guid>(x => x.Style.ID);
  387. // double units = rows.First().Get<StockHolding, double>(x => x.Units);
  388. // double size = rows.First().Get<StockHolding, double>(x => x.UnitSize);
  389. // StockMovement movement = new StockMovement();
  390. // movement.Date = DateTime.Now;
  391. // movement.Location.ID = LocationID;
  392. // movement.Product.ID = productid;
  393. // movement.Style.ID = styleid;
  394. // movement.Employee.ID = GetEmployeeID();
  395. // movement.Job.ID = Guid.Empty;
  396. // movement.Received = units;
  397. // movement.UnitSize = size;
  398. // bReserving = jobid.Equals(Guid.Empty);
  399. // bReleasing = !bReserving;
  400. // movement.Notes = bReserving ? "Reserving Stock" : "Releasing from Job " + jobno;
  401. // bool result = smg.EditItems(new StockMovement[] { movement });
  402. // Issuing to a different job than you received into ?
  403. // if (result)
  404. // {
  405. // Issue from Old Job(Hidden)
  406. // StockMovement other = new StockMovement();
  407. // other.Date = movement.Date;
  408. // other.Product.ID = productid;
  409. // other.Job.ID = jobid;
  410. // other.Location.ID = LocationID;
  411. // other.Style.ID = styleid;
  412. // other.Issued = movement.Received;
  413. // other.Transaction = movement.Transaction;
  414. // other.Employee.ID = movement.Employee.ID;
  415. // other.UnitSize = size;
  416. // other.Notes = movement.Job.ID.Equals(Guid.Empty) ? "Releasing back to General Stock" : "Reserving for Job " + movement.Job.JobNumber;
  417. // other.System = true;
  418. // new Client<StockMovement>().Save(other, "");
  419. // }
  420. // bReserving = false;
  421. // bReleasing = false;
  422. // if (result)
  423. // OnChanged?.Invoke(this);
  424. // return result;
  425. //}
  426. protected override void Reload(Filters<StockHolding> criteria, Columns<StockHolding> columns, ref SortOrder<StockHolding> sort,
  427. Action<CoreTable, Exception> action)
  428. {
  429. ReceiveButton.IsEnabled = Location != null && Location.ID != Guid.Empty;
  430. if (Location == null)
  431. criteria.Add(new Filter<StockHolding>().None());
  432. else
  433. criteria.Add(new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(Location.ID));
  434. base.Reload(criteria, columns, ref sort, action);
  435. }
  436. protected override bool FilterRecord(CoreRow row)
  437. {
  438. // Hackety Hackety Hack Hack stupid doubles not totalling zero when they're supposed to
  439. var result = base.FilterRecord(row);
  440. if (result)
  441. result = !row.Get<StockHolding, double>(x => x.Qty).ToString("F8").Equals("0.00000000");
  442. return result;
  443. }
  444. }
  445. }