using System; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Controls; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.Wpf; using InABox.WPF; namespace PRSDesktop; public class ConsignmentItemGrid : DynamicDataGrid { private Button receiveall; private Button receiveselected; protected override void Init() { base.Init(); receiveall = AddButton("Receive All", null, ReceiveAll); receiveall.IsEnabled = false; receiveselected = AddButton("Receive Selected", null, ReceiveSelected); receiveselected.IsEnabled = false; AddButton("Assign Location", null, SupplierPurchaseOrderItemOneToMany.AssignLocation); } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.RecordCount = true; options.SelectColumns = true; options.AddRows = true; options.DeleteRows = true; options.MultiSelect = true; } 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, CancellationToken token, Action action) { criteria.Add(new Filter(x => x.Consignment.ID).IsEqualTo( ConsignmentID == Guid.Empty ? CoreUtils.FullGuid : ConsignmentID)); base.Reload(criteria, columns, ref sort, token, action); } protected override void DoAdd(bool OpenEditorOnDirectEdit = false) { 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), Columns.None().Add(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(Columns.Required().Add(x => x.ID).Add(x => x.Consignment.ID)); 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)) { MessageWindow.ShowMessage("Please select a Requisition first!", "Invalid Action"); return false; } if (Completed) { MessageWindow.ShowMessage("Cannot Modify a Completed Requisition", "Invalid Action"); 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); } public 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"); } } }