DeliveryRequiList.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 DeliveryRequiList : DynamicDataGrid<Requisition>
  12. {
  13. public DeliveryRequiList()
  14. {
  15. HiddenColumns.Add(x => x.Delivery.ID);
  16. ColumnsTag = "DeliveryRequi";
  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 = "Number", Width = 40, Caption = "Req.", Alignment = Alignment.MiddleCenter },
  29. new() { ColumnName = "Title", Width = 0, Caption = "Requisition" },
  30. new() { ColumnName = "Filled", Width = 50, Caption = "Done", Format = "dd MMM", Alignment = Alignment.MiddleCenter },
  31. new() { ColumnName = "Boxes", Width = 25, Caption = "#", Alignment = Alignment.MiddleCenter }
  32. };
  33. return columns;
  34. }
  35. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  36. {
  37. if (Delivery.ID == Guid.Empty)
  38. {
  39. MessageBox.Show("Please select a Delivery First");
  40. return;
  41. }
  42. if (!Delivery.Completed.IsEmpty())
  43. {
  44. MessageBox.Show("You cannot modify a completed delivery!");
  45. return;
  46. }
  47. var grid = new MultiSelectDialog<Requisition>(
  48. new Filter<Requisition>(x => x.JobLink.ID).IsEqualTo(Delivery.Job.ID).And(x => x.Archived).IsEqualTo(DateTime.MinValue)
  49. .And(x => x.Delivery).NotLinkValid(),
  50. null
  51. );
  52. if (grid.ShowDialog())
  53. {
  54. Progress.Show("Adding Requsition Items to Delivery");
  55. var requis = grid.Items();
  56. var filter = new Filter<DeliveryItem>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  57. foreach (var requi in requis)
  58. {
  59. filter = filter.Or(x => x.RequisitionLink.ID).IsEqualTo(requi.ID);
  60. requi.Delivery.ID = Delivery.ID;
  61. }
  62. var items = new Client<DeliveryItem>().Load(filter);
  63. foreach (var item in items)
  64. {
  65. item.Delivery.ID = Delivery.ID;
  66. item.Delivery.Synchronise(Delivery);
  67. }
  68. new Client<DeliveryItem>().Save(items, "Added to Delivery");
  69. new Client<Requisition>().Save(requis, "Added to Delivery");
  70. DoChanged();
  71. Progress.Close();
  72. }
  73. }
  74. protected override void DeleteItems(params CoreRow[] rows)
  75. {
  76. if (rows == null || !rows.Any())
  77. {
  78. MessageBox.Show("Please select a row first");
  79. return;
  80. }
  81. if (!Delivery.Completed.IsEmpty())
  82. {
  83. MessageBox.Show("You cannot modify a completed delivery!");
  84. return;
  85. }
  86. Progress.Show("Removing Items from Delivery");
  87. DeliveryItem[] items = { };
  88. foreach (var row in rows)
  89. {
  90. var reqid = row.Get<Requisition, Guid>(x => x.ID);
  91. items = new Client<DeliveryItem>().Load(new Filter<DeliveryItem>(x => x.RequisitionLink.ID).IsEqualTo(reqid));
  92. var requisition = new Client<Requisition>().Load(new Filter<Requisition>(x => x.ID).IsEqualTo(reqid)).FirstOrDefault();
  93. if (requisition != null)
  94. {
  95. requisition.Delivery.ID = Guid.Empty;
  96. new Client<Requisition>().Save(requisition, "Removed from Delivery");
  97. }
  98. if (items.Any())
  99. {
  100. foreach (var item in items)
  101. item.Delivery.ID = Guid.Empty;
  102. new Client<DeliveryItem>().Save(items, "Removed From Delivery");
  103. }
  104. }
  105. DoChanged();
  106. Progress.Close();
  107. }
  108. protected override void Reload(Filters<Requisition> criteria, Columns<Requisition> columns, ref SortOrder<Requisition> sort,
  109. Action<CoreTable, Exception> action)
  110. {
  111. if (Delivery.ID == Guid.Empty)
  112. criteria.Add(new Filter<Requisition>(x => x.Delivery.ID).None());
  113. else
  114. criteria.Add(new Filter<Requisition>(x => x.Delivery.ID).IsEqualTo(Delivery.ID));
  115. base.Reload(criteria, columns, ref sort, action);
  116. }
  117. }
  118. }