using System; using System.Linq; using System.Windows; using System.Windows.Controls; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop { public class DeliveryItemsList : DynamicDataGrid { public DeliveryItemsList() { Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns); HiddenColumns.Add(x => x.ShipmentLink.ID); HiddenColumns.Add(x => x.ShipmentCode); HiddenColumns.Add(x => x.RequisitionLink.ID); //AddButton("Add Rack", null, AddRack); //AddButton("Add Requi", null, AddRequi); //AddButton("Remove", null, RemoveItems); } public Guid JobID { get; set; } public Guid DeliveryID { get; set; } public DateTime Completed { get; set; } private bool AddRack(Button arg1, CoreRow[] arg2) { if (DeliveryID == Guid.Empty) { MessageBox.Show("Please select a Delivery First"); return false; } if (!Completed.IsEmpty()) { MessageBox.Show("You cannot modify a completed delivery!"); return false; } var grid = new MultiSelectDialog( new Filter(x => x.Delivery).NotLinkValid(), null ); if (grid.ShowDialog()) { Progress.Show("Adding Rack Items to Delivery"); var shipments = grid.Items(); var filter = new Filter(x => x.ID).IsEqualTo(CoreUtils.FullGuid); foreach (var shipment in shipments) { filter = filter.Or(x => x.ShipmentLink.ID).IsEqualTo(shipment.ID); shipment.Delivery.ID = DeliveryID; } var items = new Client().Load(filter); foreach (var item in items) item.Delivery.ID = DeliveryID; new Client().Save(items, "Added to Delivery"); new Client().Save(shipments, "Added to Delivery"); Progress.Close(); return true; } return false; } private bool AddRequi(Button arg1, CoreRow[] arg2) { if (DeliveryID == Guid.Empty) { MessageBox.Show("Please select a Delivery First"); return false; } if (!Completed.IsEmpty()) { MessageBox.Show("You cannot modify a completed delivery!"); return false; } var grid = new MultiSelectDialog( new Filter(x => x.JobLink.ID).IsEqualTo(JobID).And(x => x.Archived).IsEqualTo(DateTime.MinValue) .And(x => x.Delivery).NotLinkValid(), null ); if (grid.ShowDialog()) { Progress.Show("Adding Requisition Items to Delivery"); var requis = grid.Items(); var filter = new Filter(x => x.ID).IsEqualTo(CoreUtils.FullGuid); foreach (var requi in requis) { filter = filter.Or(x => x.RequisitionLink.ID).IsEqualTo(requi.ID); requi.Delivery.ID = DeliveryID; } var items = new Client().Load(filter); foreach (var item in items) item.Delivery.ID = DeliveryID; new Client().Save(items, "Added to Delivery"); new Client().Save(requis, "Added to Delivery"); Progress.Close(); return true; } return false; } private bool RemoveItems(Button arg1, CoreRow[] arg2) { if (arg2 == null || !arg2.Any()) { MessageBox.Show("Please select a row first"); return false; } if (!Completed.IsEmpty()) { MessageBox.Show("You cannot modify a completed delivery!"); return false; } var bResult = false; Progress.Show("Removing Items from Delivery"); DeliveryItem[] items = { }; var requi = arg2.First().EntityLinkID(x => x.RequisitionLink) ?? Guid.Empty; if (requi != Guid.Empty) { items = new Client().Load(new Filter(x => x.RequisitionLink.ID).IsEqualTo(requi)); var requisition = new Client().Load(new Filter(x => x.ID).IsEqualTo(requi)).FirstOrDefault(); if (requisition != null) { requisition.Delivery.ID = Guid.Empty; new Client().Save(requisition, "Removed from Delivery"); } } else { var rack = arg2.First().EntityLinkID(x => x.ShipmentLink) ?? Guid.Empty; if (rack != Guid.Empty) { items = new Client().Load(new Filter(x => x.ShipmentLink.ID).IsEqualTo(rack)); var shipment = new Client().Load(new Filter(x => x.ID).IsEqualTo(rack)).FirstOrDefault(); if (shipment != null) { shipment.Delivery.ID = Guid.Empty; new Client().Save(shipment, "Removed from Delivery"); } } } if (items.Any()) { foreach (var item in items) item.Delivery.ID = Guid.Empty; new Client().Save(items, "Removed From Delivery"); bResult = true; } Progress.Close(); return bResult; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { if (DeliveryID != Guid.Empty) criteria.Add(new Filter(x => x.Delivery.ID).IsEqualTo(DeliveryID)); base.Reload(criteria, columns, ref sort, action); } } }