DeliveryItemsList.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. using InABox.WPF;
  10. namespace PRSDesktop
  11. {
  12. public class DeliveryItemsList : DynamicDataGrid<DeliveryItem>
  13. {
  14. public DeliveryItemsList()
  15. {
  16. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns);
  17. HiddenColumns.Add(x => x.ShipmentLink.ID);
  18. HiddenColumns.Add(x => x.ShipmentCode);
  19. HiddenColumns.Add(x => x.RequisitionLink.ID);
  20. //AddButton("Add Rack", null, AddRack);
  21. //AddButton("Add Requi", null, AddRequi);
  22. //AddButton("Remove", null, RemoveItems);
  23. }
  24. public Guid JobID { get; set; }
  25. public Guid DeliveryID { get; set; }
  26. public DateTime Completed { get; set; }
  27. private bool AddRack(Button arg1, CoreRow[] arg2)
  28. {
  29. if (DeliveryID == Guid.Empty)
  30. {
  31. MessageBox.Show("Please select a Delivery First");
  32. return false;
  33. }
  34. if (!Completed.IsEmpty())
  35. {
  36. MessageBox.Show("You cannot modify a completed delivery!");
  37. return false;
  38. }
  39. var grid = new MultiSelectDialog<Shipment>(
  40. new Filter<Shipment>(x => x.Delivery).NotLinkValid(),
  41. null
  42. );
  43. if (grid.ShowDialog())
  44. {
  45. Progress.Show("Adding Rack Items to Delivery");
  46. var shipments = grid.Items();
  47. var filter = new Filter<DeliveryItem>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  48. foreach (var shipment in shipments)
  49. {
  50. filter = filter.Or(x => x.ShipmentLink.ID).IsEqualTo(shipment.ID);
  51. shipment.Delivery.ID = DeliveryID;
  52. }
  53. var items = new Client<DeliveryItem>().Load(filter);
  54. foreach (var item in items)
  55. item.Delivery.ID = DeliveryID;
  56. new Client<DeliveryItem>().Save(items, "Added to Delivery");
  57. new Client<Shipment>().Save(shipments, "Added to Delivery");
  58. Progress.Close();
  59. return true;
  60. }
  61. return false;
  62. }
  63. private bool AddRequi(Button arg1, CoreRow[] arg2)
  64. {
  65. if (DeliveryID == Guid.Empty)
  66. {
  67. MessageBox.Show("Please select a Delivery First");
  68. return false;
  69. }
  70. if (!Completed.IsEmpty())
  71. {
  72. MessageBox.Show("You cannot modify a completed delivery!");
  73. return false;
  74. }
  75. var grid = new MultiSelectDialog<Requisition>(
  76. new Filter<Requisition>(x => x.JobLink.ID).IsEqualTo(JobID).And(x => x.Archived).IsEqualTo(DateTime.MinValue)
  77. .And(x => x.Delivery).NotLinkValid(),
  78. null
  79. );
  80. if (grid.ShowDialog())
  81. {
  82. Progress.Show("Adding Requisition Items to Delivery");
  83. var requis = grid.Items();
  84. var filter = new Filter<DeliveryItem>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  85. foreach (var requi in requis)
  86. {
  87. filter = filter.Or(x => x.RequisitionLink.ID).IsEqualTo(requi.ID);
  88. requi.Delivery.ID = DeliveryID;
  89. }
  90. var items = new Client<DeliveryItem>().Load(filter);
  91. foreach (var item in items)
  92. item.Delivery.ID = DeliveryID;
  93. new Client<DeliveryItem>().Save(items, "Added to Delivery");
  94. new Client<Requisition>().Save(requis, "Added to Delivery");
  95. Progress.Close();
  96. return true;
  97. }
  98. return false;
  99. }
  100. private bool RemoveItems(Button arg1, CoreRow[] arg2)
  101. {
  102. if (arg2 == null || !arg2.Any())
  103. {
  104. MessageBox.Show("Please select a row first");
  105. return false;
  106. }
  107. if (!Completed.IsEmpty())
  108. {
  109. MessageBox.Show("You cannot modify a completed delivery!");
  110. return false;
  111. }
  112. var bResult = false;
  113. Progress.Show("Removing Items from Delivery");
  114. DeliveryItem[] items = { };
  115. var requi = arg2.First().EntityLinkID<DeliveryItem, RequisitionLink>(x => x.RequisitionLink) ?? Guid.Empty;
  116. if (requi != Guid.Empty)
  117. {
  118. items = new Client<DeliveryItem>().Load(new Filter<DeliveryItem>(x => x.RequisitionLink.ID).IsEqualTo(requi));
  119. var requisition = new Client<Requisition>().Load(new Filter<Requisition>(x => x.ID).IsEqualTo(requi)).FirstOrDefault();
  120. if (requisition != null)
  121. {
  122. requisition.Delivery.ID = Guid.Empty;
  123. new Client<Requisition>().Save(requisition, "Removed from Delivery");
  124. }
  125. }
  126. else
  127. {
  128. var rack = arg2.First().EntityLinkID<DeliveryItem, ShipmentLink>(x => x.ShipmentLink) ?? Guid.Empty;
  129. if (rack != Guid.Empty)
  130. {
  131. items = new Client<DeliveryItem>().Load(new Filter<DeliveryItem>(x => x.ShipmentLink.ID).IsEqualTo(rack));
  132. var shipment = new Client<Shipment>().Load(new Filter<Shipment>(x => x.ID).IsEqualTo(rack)).FirstOrDefault();
  133. if (shipment != null)
  134. {
  135. shipment.Delivery.ID = Guid.Empty;
  136. new Client<Shipment>().Save(shipment, "Removed from Delivery");
  137. }
  138. }
  139. }
  140. if (items.Any())
  141. {
  142. foreach (var item in items)
  143. item.Delivery.ID = Guid.Empty;
  144. new Client<DeliveryItem>().Save(items, "Removed From Delivery");
  145. bResult = true;
  146. }
  147. Progress.Close();
  148. return bResult;
  149. }
  150. protected override void Reload(Filters<DeliveryItem> criteria, Columns<DeliveryItem> columns, ref SortOrder<DeliveryItem> sort,
  151. Action<CoreTable, Exception> action)
  152. {
  153. if (DeliveryID != Guid.Empty)
  154. criteria.Add(new Filter<DeliveryItem>(x => x.Delivery.ID).IsEqualTo(DeliveryID));
  155. base.Reload(criteria, columns, ref sort, action);
  156. }
  157. }
  158. }