JobPickingListItemGrid.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reactive.Linq;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.Wpf;
  13. using InABox.WPF;
  14. using PRSDesktop.Panels.Requisitions;
  15. namespace PRSDesktop;
  16. internal class JobPickingListItemGrid : DynamicDataGrid<RequisitionItem>, ISpecificGrid, IMasterDetailControl<Requisition, RequisitionItem>
  17. {
  18. public Requisition? Master { get; set; }
  19. private Task<List<Product>>? _products;
  20. public Filter<RequisitionItem> MasterDetailFilter => Master != null
  21. ? new Filter<RequisitionItem>(x => x.RequisitionLink.ID).IsEqualTo(Master.ID)
  22. : new Filter<RequisitionItem>().None();
  23. protected override void Init()
  24. {
  25. base.Init();
  26. HiddenColumns.Add(x => x.RequisitionLink.JobLink.ID);
  27. HiddenColumns.Add(x => x.Product.ID);
  28. HiddenColumns.Add(x => x.Style.ID);
  29. HiddenColumns.Add(x => x.JobLink.ID);
  30. HiddenColumns.Add(x =>x.Dimensions.Unit.ID);
  31. HiddenColumns.Add(x => x.Dimensions.UnitSize);
  32. HiddenColumns.Add(x => x.JobRequisitionItem.ID);
  33. HiddenColumns.Add(x => x.Location.ID);
  34. HiddenColumns.Add(x => x.EditType);
  35. HiddenColumns.Add(x => x.Done);
  36. }
  37. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  38. {
  39. base.DoReconfigure(options);
  40. options.AddRange(
  41. DynamicGridOption.SelectColumns,
  42. DynamicGridOption.FilterRows
  43. );
  44. }
  45. protected override void Reload(Filters<RequisitionItem> criteria, Columns<RequisitionItem> columns,
  46. ref SortOrder<RequisitionItem>? sort,
  47. Action<CoreTable?, Exception?> action)
  48. {
  49. criteria.Add(MasterDetailFilter);
  50. base.Reload(criteria, columns, ref sort, action);
  51. }
  52. private List<Product> GetProducts()
  53. {
  54. _products ??= Task.Run(() =>
  55. {
  56. return Client.Query(null, new Columns<Product>(x => x.Code).Add(x => x.Name).Add(x => x.ID)).ToList<Product>();
  57. });
  58. return _products.Result;
  59. }
  60. private void DoEdit(CoreRow? row)
  61. {
  62. var item = row?.ToObject<RequisitionItem>();
  63. if(item is null)
  64. {
  65. if (!CanCreateItems())
  66. return;
  67. item = CreateItem();
  68. }
  69. var editor = new RequisitionItemEditor(GetProducts, item, false);
  70. if(editor.ShowDialog() == true && editor.Result is not null)
  71. {
  72. Refresh(false, true);
  73. DoChanged();
  74. }
  75. }
  76. protected override void DoEdit()
  77. {
  78. var rows = SelectedRows;
  79. if (rows.Length != 1) return;
  80. if (rows[0].Get<RequisitionItem, bool>(x => x.Done))
  81. {
  82. base.DoEdit();
  83. }
  84. else
  85. {
  86. DoEdit(rows[0]);
  87. }
  88. }
  89. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  90. {
  91. DoEdit(null);
  92. }
  93. public override void InitialiseEditorForm(IDynamicEditorForm editor, RequisitionItem[] items, Func<Type, CoreTable?>? pageDataHandler = null, bool preloadPages = false)
  94. {
  95. base.InitialiseEditorForm(editor, items, pageDataHandler, preloadPages);
  96. if(items.Any(x => x.Done))
  97. {
  98. editor.ReadOnly = true;
  99. }
  100. }
  101. protected override bool CanCreateItems()
  102. {
  103. return base.CanCreateItems() && (Master?.ID ?? Guid.Empty) != Guid.Empty;
  104. }
  105. public override RequisitionItem CreateItem()
  106. {
  107. var result = base.CreateItem();
  108. result.RequisitionLink.ID = Master?.ID ?? Guid.Empty;
  109. result.RequisitionLink.Synchronise(Master ?? new Requisition());
  110. result.JobLink.ID = Master?.JobLink.ID ?? Guid.Empty;
  111. result.JobLink.Synchronise(Master?.JobLink ?? new JobLink());
  112. return result;
  113. }
  114. }