JobScopeCostCentreGrid.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Windows.Controls;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. namespace PRSDesktop;
  10. public class JobScopeCostCentreGrid : DynamicDataGrid<JobScopeCostCentre>, IJobScopeGrid
  11. {
  12. public JobScope? Scope { get; set; }
  13. protected override void Init()
  14. {
  15. base.Init();
  16. HiddenColumns.Add(x=>x.CostCentre.ID);
  17. HiddenColumns.Add(x => x.Cost);
  18. HiddenColumns.Add(x => x.Sell);
  19. AddButton("Reset", null, ResetGLCodes);
  20. if (Security.CanView<CostCentre>())
  21. AddButton("Manage", null, ManageCostCentres, DynamicGridButtonPosition.Right);
  22. }
  23. protected override void DoReconfigure(DynamicGridOptions options)
  24. {
  25. base.DoReconfigure(options);
  26. options.SelectColumns = true;
  27. }
  28. public override DynamicGridColumns GenerateColumns()
  29. {
  30. var _columns = new DynamicGridColumns();
  31. _columns.Add<JobScopeCostCentre>(x => x.CostCentre.Code, 120, "Code", "", Alignment.MiddleLeft);
  32. _columns.Add<JobScopeCostCentre>(x => x.CostCentre.Description, 0, "Description", "", Alignment.MiddleLeft);
  33. _columns.Add<JobScopeCostCentre>(x => x.Cost, 70, "Cost", "F2", Alignment.MiddleCenter);
  34. _columns.Add<JobScopeCostCentre>(x => x.Markup, 70, "M/Up", "F2", Alignment.MiddleCenter);
  35. _columns.Add<JobScopeCostCentre>(x => x.Sell, 70, "Sell", "F2", Alignment.MiddleCenter);
  36. return _columns;
  37. }
  38. protected override void Reload(
  39. Filters<JobScopeCostCentre> criteria, Columns<JobScopeCostCentre> columns, ref SortOrder<JobScopeCostCentre>? sort,
  40. CancellationToken token, Action<CoreTable?, Exception?> action)
  41. {
  42. if ((Scope == null) || (Scope.ID == Guid.Empty))
  43. criteria.Add(Filter.None<JobScopeCostCentre>());
  44. else
  45. criteria.Add(Filter<JobScopeCostCentre>.Where(x => x.Scope.ID).IsEqualTo(Scope.ID));
  46. base.Reload(criteria, columns, ref sort, token, action);
  47. }
  48. protected override bool CanCreateItems()
  49. {
  50. return base.CanCreateItems() && Scope != null;
  51. }
  52. public override JobScopeCostCentre CreateItem()
  53. {
  54. var _result = base.CreateItem();
  55. _result.Scope.Synchronise(Scope ?? new JobScope());
  56. return _result;
  57. }
  58. private bool ManageCostCentres(Button button, CoreRow[] rows)
  59. {
  60. var list = new MasterList(typeof(CostCentre));
  61. list.ShowDialog();
  62. return true;
  63. }
  64. private bool ResetGLCodes(Button button, CoreRow[] rows)
  65. {
  66. var _current = ExtractValues(x => x.CostCentre.ID, Selection.All).ToArray();
  67. var _masters = Client.Query(
  68. Filter<CostCentre>.Where(x => x.IsJobCostCentre).IsEqualTo(true),
  69. Columns.None<CostCentre>().Add(x => x.ID)
  70. ).Rows
  71. .Select(row =>
  72. {
  73. var result = new JobScopeCostCentre();
  74. result.Job.ID = Scope.Job.ID;
  75. result.Scope.ID = Scope.ID;
  76. result.CostCentre.ID = row.Get<CostCentre, Guid>(x => x.ID);
  77. return result;
  78. }
  79. ).ToArray();
  80. var _new = _masters.Where(a => _current.All(x => x != a.CostCentre.ID)).ToList();
  81. if (_new.Any())
  82. Client.Save(_new, "Updated from Master CostCentre List");
  83. var _unused = Data.ToObjects<JobScopeCostCentre>()
  84. .Where(x => x.Cost.IsEffectivelyEqual(0.0F) && x.Sell.IsEffectivelyEqual(0.0F))
  85. .Where(x => _masters.All(m => m.CostCentre.ID != x.CostCentre.ID))
  86. .ToArray();
  87. if (_unused.Any())
  88. Client.Delete(_unused, "Deleted from Master CostCentre List");
  89. return _new.Any() || _unused.Any();
  90. }
  91. }