using System; using System.Linq; using System.Windows; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop { internal class DeliveryRequiList : DynamicDataGrid { public DeliveryRequiList() { Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.DeleteRows, DynamicGridOption.SelectColumns); HiddenColumns.Add(x => x.Delivery.ID); ColumnsTag = "DeliveryRequi"; } public Guid JobID { get; set; } public Guid DeliveryID { get; set; } public DateTime Completed { get; set; } protected override DynamicGridColumns LoadColumns() { var columns = new DynamicGridColumns { new() { ColumnName = "Number", Width = 40, Caption = "Req.", Alignment = Alignment.MiddleCenter }, new() { ColumnName = "Title", Width = 0, Caption = "Requisition" }, new() { ColumnName = "Filled", Width = 50, Caption = "Done", Format = "dd MMM", Alignment = Alignment.MiddleCenter }, new() { ColumnName = "Boxes", Width = 25, Caption = "#", Alignment = Alignment.MiddleCenter } }; return columns; } protected override void DoAdd(bool OpenEditorOnDirectEdit = false) { if (DeliveryID == Guid.Empty) { MessageBox.Show("Please select a Delivery First"); return; } if (!Completed.IsEmpty()) { MessageBox.Show("You cannot modify a completed delivery!"); return; } 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 Requsition 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"); DoChanged(); Progress.Close(); } } protected override void DeleteItems(params CoreRow[] rows) { if (rows == null || !rows.Any()) { MessageBox.Show("Please select a row first"); return; } if (!Completed.IsEmpty()) { MessageBox.Show("You cannot modify a completed delivery!"); return; } Progress.Show("Removing Items from Delivery"); DeliveryItem[] items = { }; foreach (var row in rows) { var reqid = row.Get(x => x.ID); items = new Client().Load(new Filter(x => x.RequisitionLink.ID).IsEqualTo(reqid)); var requisition = new Client().Load(new Filter(x => x.ID).IsEqualTo(reqid)).FirstOrDefault(); if (requisition != null) { requisition.Delivery.ID = Guid.Empty; new Client().Save(requisition, "Removed from Delivery"); } if (items.Any()) { foreach (var item in items) item.Delivery.ID = Guid.Empty; new Client().Save(items, "Removed From Delivery"); } } DoChanged(); Progress.Close(); } 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); } } }