DeliveryRequiList.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. {
  12. internal class DeliveryRequiList : DynamicDataGrid<Requisition>, IMasterDetailControl<Delivery,Requisition>
  13. {
  14. public Delivery? Master { get; set; }
  15. public Filter<Requisition> MasterDetailFilter => Master != null && Master.ID != Guid.Empty
  16. ? new Filter<Requisition>(x => x.Delivery.ID).IsEqualTo(Master.ID)
  17. : new Filter<Requisition>().None();
  18. public DeliveryRequiList()
  19. {
  20. HiddenColumns.Add(x => x.Delivery.ID);
  21. ColumnsTag = "DeliveryRequi";
  22. }
  23. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  24. {
  25. base.DoReconfigure(options);
  26. options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.DeleteRows, DynamicGridOption.SelectColumns);
  27. }
  28. protected override DynamicGridColumns LoadColumns()
  29. {
  30. var columns = new DynamicGridColumns
  31. {
  32. new() { ColumnName = "Number", Width = 40, Caption = "Req.", Alignment = Alignment.MiddleCenter },
  33. new() { ColumnName = "Title", Width = 0, Caption = "Requisition" },
  34. new() { ColumnName = "Filled", Width = 50, Caption = "Done", Format = "dd MMM", Alignment = Alignment.MiddleCenter },
  35. new() { ColumnName = "Boxes", Width = 25, Caption = "#", Alignment = Alignment.MiddleCenter }
  36. };
  37. return columns;
  38. }
  39. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  40. {
  41. if (Master.ID == 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<Requisition>(
  52. new Filter<Requisition>(x => x.JobLink.ID).IsEqualTo(Master.Job.ID).And(x => x.Archived).IsEqualTo(DateTime.MinValue)
  53. .And(x => x.Delivery).NotLinkValid(),
  54. null
  55. );
  56. if (grid.ShowDialog())
  57. {
  58. Progress.Show("Adding Requsition Items to Delivery");
  59. var requis = grid.Items();
  60. var filter = new Filter<DeliveryItem>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  61. foreach (var requi in requis)
  62. {
  63. filter = filter.Or(x => x.RequisitionLink.ID).IsEqualTo(requi.ID);
  64. requi.Delivery.ID = Master.ID;
  65. }
  66. var items = new Client<DeliveryItem>().Query(
  67. filter,
  68. new Columns<DeliveryItem>(x => x.Delivery.ID).Add(x => x.ID)
  69. ).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<Requisition>().Save(requis, "Added to Delivery");
  77. DoChanged();
  78. Progress.Close();
  79. }
  80. }
  81. protected override void DeleteItems(params CoreRow[] rows)
  82. {
  83. if (rows == null || !rows.Any())
  84. {
  85. MessageBox.Show("Please select a row first");
  86. return;
  87. }
  88. if (!Master.Completed.IsEmpty())
  89. {
  90. MessageBox.Show("You cannot modify a completed delivery!");
  91. return;
  92. }
  93. Progress.Show("Removing Items from Delivery");
  94. DeliveryItem[] items = { };
  95. foreach (var row in rows)
  96. {
  97. var reqid = row.Get<Requisition, Guid>(x => x.ID);
  98. items = new Client<DeliveryItem>().Query(
  99. new Filter<DeliveryItem>(x => x.RequisitionLink.ID).IsEqualTo(reqid),
  100. new Columns<DeliveryItem>(x => x.Delivery.ID).Add(x => x.ID)
  101. ).ToArray<DeliveryItem>();
  102. var requisition = new Client<Requisition>().Load(new Filter<Requisition>(x => x.ID).IsEqualTo(reqid)).FirstOrDefault();
  103. if (requisition != null)
  104. {
  105. requisition.Delivery.ID = Guid.Empty;
  106. new Client<Requisition>().Save(requisition, "Removed from Delivery");
  107. }
  108. if (items.Any())
  109. {
  110. foreach (var item in items)
  111. item.Delivery.ID = Guid.Empty;
  112. new Client<DeliveryItem>().Save(items, "Removed From Delivery");
  113. }
  114. }
  115. DoChanged();
  116. Progress.Close();
  117. }
  118. protected override void Reload(Filters<Requisition> criteria, Columns<Requisition> columns, ref SortOrder<Requisition>? sort, Action<CoreTable?, Exception?> action)
  119. {
  120. criteria.Add(MasterDetailFilter);
  121. base.Reload(criteria, columns, ref sort, action);
  122. }
  123. }
  124. }