SupplierBillApprovalGrid.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.DynamicGrid;
  4. using InABox.WPF;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Controls;
  12. using System.Windows.Media;
  13. namespace PRSDesktop;
  14. public class SupplierBillApprovalGrid : DynamicDataGrid<BillApproval>, ISpecificGrid
  15. {
  16. private Bill Bill;
  17. public SupplierBillApprovalGrid(Bill bill)
  18. {
  19. Bill = bill;
  20. }
  21. private Button ApproveButton = null!;
  22. protected override void Init()
  23. {
  24. base.Init();
  25. HiddenColumns.Add(x => x.Employee.ID);
  26. HiddenColumns.Add(x => x.IsCustom);
  27. ApproveButton = AddButton("Mark as Approved", null, ApproveButton_Click);
  28. ApproveButton.IsEnabled = false;
  29. }
  30. private bool ApproveButton_Click(Button button, CoreRow[] rows)
  31. {
  32. var items = LoadItems(rows);
  33. if(items.Any(x => x.Approved == DateTime.MinValue))
  34. {
  35. foreach(var item in items)
  36. {
  37. item.Approved = DateTime.Now;
  38. }
  39. }
  40. else
  41. {
  42. foreach(var item in items)
  43. {
  44. item.Approved = DateTime.MinValue;
  45. }
  46. }
  47. SaveItems(items);
  48. DoChanged();
  49. return true;
  50. }
  51. protected override void SelectItems(CoreRow[]? rows)
  52. {
  53. base.SelectItems(rows);
  54. if(rows is null || rows.Length == 0)
  55. {
  56. ApproveButton.IsEnabled = false;
  57. }
  58. else
  59. {
  60. ApproveButton.IsEnabled = true;
  61. if(rows.Any(x => x.Get<BillApproval, DateTime>(x => x.Approved) == DateTime.MinValue))
  62. {
  63. ApproveButton.Content = "Mark as Approved";
  64. }
  65. else
  66. {
  67. ApproveButton.Content = "Mark as Unapproved";
  68. }
  69. }
  70. }
  71. #region UI Component
  72. private class UIComponent : DynamicGridGridUIComponent<BillApproval>
  73. {
  74. protected override Brush? GetCellBackground(CoreRow row, DynamicColumnBase column)
  75. {
  76. if(row.Get<BillApproval, bool>(x => x.IsCustom))
  77. {
  78. return null;
  79. }
  80. else
  81. {
  82. return Colors.Gainsboro.ToBrush(0.5);
  83. }
  84. }
  85. }
  86. protected override IDynamicGridUIComponent<BillApproval> CreateUIComponent()
  87. {
  88. return new UIComponent { Parent = this };
  89. }
  90. #endregion
  91. protected override void DoReconfigure(DynamicGridOptions options)
  92. {
  93. base.DoReconfigure(options);
  94. options.AddRows = Security.IsAllowed<ManageBillApprovals>();
  95. options.EditRows = Security.IsAllowed<ManageBillApprovals>();
  96. options.DeleteRows = Security.IsAllowed<ManageBillApprovals>();
  97. }
  98. public override BillApproval CreateItem()
  99. {
  100. var item = base.CreateItem();
  101. item.Bill.CopyFrom(Bill);
  102. item.IsCustom = true;
  103. return item;
  104. }
  105. protected override void Reload(Filters<BillApproval> criteria, Columns<BillApproval> columns, ref SortOrder<BillApproval>? sort, CancellationToken token, Action<CoreTable?, Exception?> action)
  106. {
  107. criteria.Add(new Filter<BillApproval>(x => x.Bill.ID).IsEqualTo(Bill.ID));
  108. base.Reload(criteria, columns, ref sort, token, action);
  109. }
  110. protected override void DoEdit()
  111. {
  112. if(SelectedRows.Any(x => !x.Get<BillApproval, bool>(x => x.IsCustom)))
  113. {
  114. return;
  115. }
  116. else
  117. {
  118. base.DoEdit();
  119. }
  120. }
  121. protected override void DoDelete()
  122. {
  123. if(SelectedRows.Any(x => !x.Get<BillApproval, bool>(x => x.IsCustom)))
  124. {
  125. return;
  126. }
  127. else
  128. {
  129. base.DoDelete();
  130. }
  131. }
  132. protected override void DoAdd(bool openEditorOnDirectEdit = false)
  133. {
  134. if (MultiSelectDialog<Employee>.SelectItem(out var employee,
  135. LookupFactory.DefineLookupFilter<BillApproval, Employee, EmployeeLink>(x => x.Employee, []),
  136. LookupFactory.DefineLookupColumns<BillApproval, Employee, EmployeeLink>(x => x.Employee),
  137. "Select Bill Approval Employee:"))
  138. {
  139. CreateItems(() =>
  140. {
  141. var approval = CreateItem();
  142. approval.Employee.CopyFrom(employee);
  143. return CoreUtils.One(approval);
  144. });
  145. }
  146. }
  147. }