DeliveryRequiList.cs 4.6 KB

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