DeliveryRackList.cs 4.7 KB

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