DeliveryRackList.cs 4.9 KB

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