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 ConsignmentItemGrid : DynamicDataGrid { private readonly Button receiveall; private readonly Button receiveselected; public ConsignmentItemGrid() { Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns, DynamicGridOption.AddRows, DynamicGridOption.DeleteRows, DynamicGridOption.MultiSelect); receiveall = AddButton("Receive All", null, ReceiveAll); receiveall.IsEnabled = false; receiveselected = AddButton("Receive Selected", null, ReceiveSelected); receiveselected.IsEnabled = false; } public Guid ConsignmentID { get; set; } public bool Completed { get; set; } protected override void SelectItems(CoreRow[] rows) { receiveselected.IsEnabled = rows != null && rows.Any(r => r.Get(c => c.ReceivedDate).IsEmpty()); receiveall.IsEnabled = Data.Rows.Any(r => r.Get(c => c.ReceivedDate).IsEmpty()); base.SelectItems(rows); } private bool ReceiveAll(Button sender, CoreRow[] rows) { var unreceived = Data.Rows.Where(r => r.Get(c => c.ReceivedDate).IsEmpty()); if (!unreceived.Any()) { MessageBox.Show("No Items to Receive"); return false; } var now = DateTime.Now; using (new WaitCursor()) { var items = unreceived.Select(x => x.ToObject()); foreach (var item in items) item.ReceivedDate = now; new Client().Save(items, "Consignment Items Received"); } return true; } private bool ReceiveSelected(Button sender, CoreRow[] rows) { if (!rows.Any()) { MessageBox.Show("Please select a row first"); return false; } var now = DateTime.Now; using (new WaitCursor()) { var items = LoadItems(rows); foreach (var item in items) item.ReceivedDate = now; new Client().Save(items, "Consignment Items Received"); } return true; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { criteria.Add(new Filter(x => x.Consignment.ID).IsEqualTo( ConsignmentID == Guid.Empty ? CoreUtils.FullGuid : ConsignmentID)); base.Reload(criteria, columns, ref sort, action); } protected override void DoAdd() { if (ConsignmentID.Equals(Guid.Empty)) { MessageBox.Show("Please select a Requisition first!"); return; } if (Completed) { MessageBox.Show("Cannot Modify a Completed Requisition"); return; } var dialog = new MultiSelectDialog( new Filter(x => x.Consignment.ID).IsEqualTo(Guid.Empty).And(x => x.ReceivedDate).IsEqualTo(DateTime.MinValue), new Columns(x => x.PurchaseOrderLink.PONumber, x => x.Product.Code, x => x.Product.Name, x => x.Description, x => x.Qty) //new System.Linq.Expressions.Expression>[] { x => x.PurchaseOrderLink.PONumber, x => x.ProductLink.Code, x=>x.ProductLink.Name, x=>x.Description, x=>x.Qty} ); if (dialog.ShowDialog()) { Progress.Show("Adding Order Items"); var items = dialog.Items(); foreach (var item in items) item.Consignment.ID = ConsignmentID; Progress.SetMessage("Updating Items"); new Client().Save(items, "Added to Consignment"); Refresh(false, true); Progress.Close(); MessageBox.Show(string.Format("{0} order items added", items.Length)); } } public override bool EditItems(PurchaseOrderItem[] items, Func PageDataHandler, bool PreloadPages = false) { if (ConsignmentID.Equals(Guid.Empty)) { MessageBox.Show("Please select a Requisition first!"); return false; } if (Completed) { MessageBox.Show("Cannot Modify a Completed Requisition"); return false; } return base.EditItems(items, PageDataHandler, PreloadPages); } protected override bool CanDeleteItems(CoreRow[] rows) { if (ConsignmentID.Equals(Guid.Empty)) { MessageBox.Show("Please select a Requisition first!"); return false; } if (Completed) { MessageBox.Show("Cannot Modify a Closed Requisition"); return false; } return base.CanDeleteItems(rows); } protected override void DeleteItems(params CoreRow[] rows) { using (new WaitCursor()) { var items = LoadItems(rows); foreach (var item in items) item.Consignment.ID = Guid.Empty; new Client().Save(items, "Removed from Consignment"); } } }