DeliveryRackList.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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<Shipment>();
  34. columns.Add(x => x.Code, width: 40, caption: "Rack", alignment: Alignment.MiddleCenter);
  35. columns.Add(x => x.Description, width: 0, caption: "Description");
  36. columns.Add(x => x.ItemCount, width: 25, caption: "#", alignment: Alignment.MiddleCenter);
  37. return columns;
  38. }
  39. protected override void DoAdd(bool openEditorOnDirectEdit = false)
  40. {
  41. if ((Master?.ID ?? Guid.Empty) == Guid.Empty)
  42. {
  43. MessageBox.Show("Please select a Delivery First");
  44. return;
  45. }
  46. if (!Master.Completed.IsEmpty())
  47. {
  48. MessageBox.Show("You cannot modify a completed delivery!");
  49. return;
  50. }
  51. var grid = new MultiSelectDialog<Shipment>(
  52. new Filter<Shipment>(x => x.Delivery).NotLinkValid(),
  53. Columns.Required<Shipment>().Add(x => x.ID)
  54. .Add(x => x.Delivery.ID)
  55. );
  56. if (grid.ShowDialog())
  57. {
  58. Progress.Show("Adding Racks to Delivery");
  59. var shipments = grid.Data().ToArray<Shipment>();
  60. var filter = new Filter<DeliveryItem>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  61. foreach (var shipment in shipments)
  62. {
  63. filter = filter.Or(x => x.ShipmentLink.ID).IsEqualTo(shipment.ID);
  64. shipment.Delivery.ID = Master.ID;
  65. shipment.Delivery.Synchronise(Master);
  66. }
  67. var itemtable = new Client<DeliveryItem>().Query(filter,
  68. Columns.Required<DeliveryItem>().Add(x => x.Delivery.ID));
  69. var items = itemtable.ToArray<DeliveryItem>();
  70. foreach (var item in items)
  71. {
  72. item.Delivery.ID = Master.ID;
  73. item.Delivery.Synchronise(Master);
  74. }
  75. new Client<DeliveryItem>().Save(items, "Added to Delivery");
  76. new Client<Shipment>().Save(shipments, "Added to Delivery");
  77. DoChanged();
  78. Progress.Close();
  79. }
  80. }
  81. public override void DeleteItems(params CoreRow[] rows)
  82. {
  83. if ((Master?.ID ?? Guid.Empty) == Guid.Empty)
  84. {
  85. MessageBox.Show("Please select a Delivery First");
  86. return;
  87. }
  88. if (rows?.Any() != true)
  89. {
  90. MessageBox.Show("Please select a row first");
  91. return;
  92. }
  93. if (!Master.Completed.IsEmpty())
  94. {
  95. MessageBox.Show("You cannot modify a completed delivery!");
  96. return;
  97. }
  98. Progress.Show("Removing Items from Delivery");
  99. DeliveryItem[] items = { };
  100. foreach (var row in rows)
  101. {
  102. var rackid = row.Get<Shipment, Guid>(x => x.ID);
  103. items = new Client<DeliveryItem>().Query(
  104. new Filter<DeliveryItem>(x => x.ShipmentLink.ID).IsEqualTo(rackid),
  105. Columns.Required<DeliveryItem>().Add(x => x.Delivery.ID).Add(x => x.ID)).ToArray<DeliveryItem>();
  106. var shipment = new Client<Shipment>().Load(new Filter<Shipment>(x => x.ID).IsEqualTo(rackid)).FirstOrDefault();
  107. if (shipment != null)
  108. {
  109. shipment.Delivery.ID = Guid.Empty;
  110. new Client<Shipment>().Save(shipment, "Removed from Delivery");
  111. }
  112. if (items.Any())
  113. {
  114. foreach (var item in items)
  115. item.Delivery.ID = Guid.Empty;
  116. new Client<DeliveryItem>().Save(items, "Removed From Delivery");
  117. }
  118. }
  119. DoChanged();
  120. Progress.Close();
  121. }
  122. protected override void Reload(
  123. Filters<Shipment> criteria, Columns<Shipment> columns, ref SortOrder<Shipment>? sort,
  124. CancellationToken token, Action<CoreTable?, Exception?> action)
  125. {
  126. criteria.Add(MasterDetailFilter);
  127. base.Reload(criteria, columns, ref sort, token, action);
  128. }
  129. }