StockLocationGrid.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. using Comal.Classes;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.WPF;
  12. using NPOI.SS.Formula.Functions;
  13. using Column = InABox.Core.Column;
  14. namespace PRSDesktop;
  15. public class StockLocationGrid : DynamicDataGrid<StockLocation>
  16. {
  17. public Button StockTakeBtn;
  18. private BitmapImage _stocktakeheader = PRSDesktop.Resources.schedule_enabled.AsBitmapImage();
  19. private BitmapImage _stocktakedisabled = PRSDesktop.Resources.schedule_disabled.AsBitmapImage();
  20. private BitmapImage _stocktakeinprogress = PRSDesktop.Resources.notification.AsBitmapImage();
  21. private BitmapImage _stocktakenotyetdue = PRSDesktop.Resources.schedule_enabled.AsBitmapImage();
  22. private BitmapImage _stocktakeoverdue = PRSDesktop.Resources.schedule_overdue.AsBitmapImage();
  23. protected override void Init()
  24. {
  25. base.Init();
  26. HiddenColumns.Add(x => x.Job.ID);
  27. HiddenColumns.Add(x => x.StocktakeFrequency);
  28. HiddenColumns.Add(x => x.LastStocktake);
  29. HiddenColumns.Add(x => x.CurrentStocktake);
  30. HiddenColumns.Add(x => x.NextStocktake);
  31. ActionColumns.Add(new DynamicImageColumn(StockTakeStatusImage) { ToolTip = StockTakeStatusToolTip, Position = DynamicActionColumnPosition.Start });
  32. if (Security.CanEdit<StockMovement>())
  33. StockTakeBtn = AddButton("Stock Take", PRSDesktop.Resources.rack.AsBitmapImage(Color.White), DoStockTake);
  34. OnCustomiseEditor += CustomiseEditor;
  35. }
  36. private FrameworkElement? StockTakeStatusToolTip(DynamicActionColumn column, CoreRow? row)
  37. {
  38. return row == null
  39. ? column.TextToolTip("Indicates the Stocktake status of this location")
  40. : !row.Get<StockLocation, DateTime>(x => x.CurrentStocktake).IsEmpty()
  41. ? column.TextToolTip("Stocktake in progress")
  42. : !row.Get<StockLocation, DateTime>(x => x.NextStocktake).IsEmpty()
  43. ? column.TextToolTip(
  44. $"Stocktake due on {row.Get<StockLocation, DateTime>(x => x.NextStocktake).Date:dd MMM yy}")
  45. : column.TextToolTip("No Stocktake scheduled");
  46. }
  47. private BitmapImage? StockTakeStatusImage(CoreRow? row)
  48. {
  49. return row == null
  50. ? _stocktakeheader
  51. : !row.Get<StockLocation, DateTime>(x => x.CurrentStocktake).IsEmpty()
  52. ? _stocktakeinprogress
  53. : !row.Get<StockLocation, DateTime>(x => x.NextStocktake).IsEmpty()
  54. ? row.Get<StockLocation, DateTime>(x => x.NextStocktake).Date > DateTime.Today
  55. ? _stocktakenotyetdue
  56. : _stocktakeoverdue
  57. : _stocktakedisabled;
  58. }
  59. protected override void DoReconfigure(DynamicGridOptions options)
  60. {
  61. base.DoReconfigure(options);
  62. options.MultiSelect = true;
  63. options.FilterRows = true;
  64. options.SelectColumns = true;
  65. options.RecordCount = true;
  66. }
  67. private bool DoStockTake(Button sender, CoreRow[] rows)
  68. {
  69. if (!rows.Any())
  70. {
  71. MessageBox.Show("Please select at least one row for Stocktake!");
  72. return false;
  73. }
  74. var page = new StockTakeWindow(rows);
  75. page.Show();
  76. return true;
  77. }
  78. protected override void DoValidate(StockLocation[] items, List<string> errors)
  79. {
  80. base.DoValidate(items, errors);
  81. var bHoldings = false;
  82. var bDefault = false;
  83. var bTransient = false;
  84. foreach (var item in items)
  85. {
  86. bHoldings = bHoldings;
  87. bDefault = bDefault || (item.Default && !item.Active);
  88. bTransient = bTransient || (item.Default && item.Type.Equals(StockLocationType.Transient));
  89. }
  90. if (bHoldings)
  91. errors.Add("Non-Empty locations must always be [Active]!");
  92. if (bDefault)
  93. errors.Add("Default locations must always be [Active]!");
  94. if (bTransient)
  95. errors.Add("Default locations must always be [Permanent]!");
  96. }
  97. private void CustomiseEditor(IDynamicEditorForm sender, StockLocation[]? items, DynamicGridColumn column, BaseEditor editor)
  98. {
  99. if (string.Equals(column.ColumnName, "Active"))
  100. {
  101. //var bHasHoldings = items.FirstOrDefault().Holdings != 0;
  102. //editor.Editable = bHasHoldings ? Editable.Disabled : Editable.Enabled;
  103. //if (bHasHoldings)
  104. // var item = items?.FirstOrDefault();
  105. // if (item is not null)
  106. // item.Active = true;
  107. }
  108. }
  109. }