DeliveryRackList.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using Comal.Classes;
  5. using InABox.Clients;
  6. using InABox.Core;
  7. using InABox.DynamicGrid;
  8. using InABox.WPF;
  9. using InABox.Wpf;
  10. using System.Threading;
  11. namespace PRSDesktop;
  12. internal class DeliveryRackList : DynamicDataGrid<Shipment>, IMasterDetailControl<Delivery,Shipment>
  13. {
  14. public Delivery? Master { get; set; }
  15. public Filter<Shipment> MasterDetailFilter => Master != null && Master.ID != Guid.Empty
  16. ? new Filter<Shipment>(x => x.Delivery.ID).IsEqualTo(Master.ID)
  17. : new Filter<Shipment>().None();
  18. public DeliveryRackList()
  19. {
  20. HiddenColumns.Add(x => x.Delivery.ID);
  21. ColumnsTag = "DeliveryRack";
  22. }
  23. protected override void DoReconfigure(DynamicGridOptions options)
  24. {
  25. base.DoReconfigure(options);
  26. options.RecordCount = true;
  27. options.AddRows = true;
  28. options.DeleteRows = true;
  29. options.SelectColumns = true;
  30. }
  31. protected override DynamicGridColumns LoadColumns()
  32. {
  33. var columns = new DynamicGridColumns
  34. {
  35. new() { ColumnName = "Code", Width = 40, Caption = "Rack", Alignment = Alignment.MiddleCenter },
  36. new() { ColumnName = "Description", Width = 0, Caption = "Description" },
  37. new() { ColumnName = "ItemCount", Width = 25, Caption = "#", Alignment = Alignment.MiddleCenter }
  38. };
  39. return columns;
  40. }
  41. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  42. {
  43. if ((Master?.ID ?? Guid.Empty) == Guid.Empty)
  44. {
  45. MessageBox.Show("Please select a Delivery First");
  46. return;
  47. }
  48. if (!Master.Completed.IsEmpty())
  49. {
  50. MessageBox.Show("You cannot modify a completed delivery!");
  51. return;
  52. }
  53. var grid = new MultiSelectDialog<Shipment>(
  54. new Filter<Shipment>(x => x.Delivery).NotLinkValid(),
  55. Columns.Required<Shipment>().Add(x => x.ID)
  56. .Add(x => x.Delivery.ID)
  57. );
  58. if (grid.ShowDialog())
  59. {
  60. Progress.Show("Adding Racks to Delivery");
  61. var shipments = grid.Data().ToArray<Shipment>();
  62. var filter = new Filter<DeliveryItem>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  63. foreach (var shipment in shipments)
  64. {
  65. filter = filter.Or(x => x.ShipmentLink.ID).IsEqualTo(shipment.ID);
  66. shipment.Delivery.ID = Master.ID;
  67. shipment.Delivery.Synchronise(Master);
  68. }
  69. var itemtable = new Client<DeliveryItem>().Query(filter,
  70. Columns.Required<DeliveryItem>().Add(x => x.Delivery.ID));
  71. var items = itemtable.ToArray<DeliveryItem>();
  72. foreach (var item in items)
  73. {
  74. item.Delivery.ID = Master.ID;
  75. item.Delivery.Synchronise(Master);
  76. }
  77. new Client<DeliveryItem>().Save(items, "Added to Delivery");
  78. new Client<Shipment>().Save(shipments, "Added to Delivery");
  79. DoChanged();
  80. Progress.Close();
  81. }
  82. }
  83. public override void DeleteItems(params CoreRow[] rows)
  84. {
  85. if ((Master?.ID ?? Guid.Empty) == Guid.Empty)
  86. {
  87. MessageBox.Show("Please select a Delivery First");
  88. return;
  89. }
  90. if (rows?.Any() != true)
  91. {
  92. MessageBox.Show("Please select a row first");
  93. return;
  94. }
  95. if (!Master.Completed.IsEmpty())
  96. {
  97. MessageBox.Show("You cannot modify a completed delivery!");
  98. return;
  99. }
  100. Progress.Show("Removing Items from Delivery");
  101. DeliveryItem[] items = { };
  102. foreach (var row in rows)
  103. {
  104. var rackid = row.Get<Shipment, Guid>(x => x.ID);
  105. items = new Client<DeliveryItem>().Query(
  106. new Filter<DeliveryItem>(x => x.ShipmentLink.ID).IsEqualTo(rackid),
  107. Columns.Required<DeliveryItem>().Add(x => x.Delivery.ID).Add(x => x.ID)).ToArray<DeliveryItem>();
  108. var shipment = new Client<Shipment>().Load(new Filter<Shipment>(x => x.ID).IsEqualTo(rackid)).FirstOrDefault();
  109. if (shipment != null)
  110. {
  111. shipment.Delivery.ID = Guid.Empty;
  112. new Client<Shipment>().Save(shipment, "Removed from Delivery");
  113. }
  114. if (items.Any())
  115. {
  116. foreach (var item in items)
  117. item.Delivery.ID = Guid.Empty;
  118. new Client<DeliveryItem>().Save(items, "Removed From Delivery");
  119. }
  120. }
  121. DoChanged();
  122. Progress.Close();
  123. }
  124. protected override void Reload(
  125. Filters<Shipment> criteria, Columns<Shipment> columns, ref SortOrder<Shipment>? sort,
  126. CancellationToken token, Action<CoreTable?, Exception?> action)
  127. {
  128. criteria.Add(MasterDetailFilter);
  129. base.Reload(criteria, columns, ref sort, token, action);
  130. }
  131. }