| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 | 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;namespace PRSDesktop;public class ConsignmentItemGrid : DynamicDataGrid<PurchaseOrderItem>{    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<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty());        receiveall.IsEnabled = Data.Rows.Any(r => r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty());        base.SelectItems(rows);    }    private bool ReceiveAll(Button sender, CoreRow[] rows)    {        var unreceived = Data.Rows.Where(r => r.Get<PurchaseOrderItem, DateTime>(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<PurchaseOrderItem>());            foreach (var item in items)                item.ReceivedDate = now;            new Client<PurchaseOrderItem>().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<PurchaseOrderItem>().Save(items, "Consignment Items Received");        }        return true;    }    protected override void Reload(    	Filters<PurchaseOrderItem> criteria, Columns<PurchaseOrderItem> columns, ref SortOrder<PurchaseOrderItem>? sort,    	CancellationToken token, Action<CoreTable?, Exception?> action)    {        criteria.Add(new Filter<PurchaseOrderItem>(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<PurchaseOrderItem>(            new Filter<PurchaseOrderItem>(x => x.Consignment.ID).IsEqualTo(Guid.Empty).And(x => x.ReceivedDate).IsEqualTo(DateTime.MinValue),            Columns.None<PurchaseOrderItem>().Add(x => x.PurchaseOrderLink.PONumber, x => x.Product.Code, x => x.Product.Name, x => x.Description,                x => x.Qty)            //new System.Linq.Expressions.Expression<Func<PurchaseOrderItem, object>>[] { 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<PurchaseOrderItem>().Add(x => x.ID).Add(x => x.Consignment.ID));            foreach (var item in items)                item.Consignment.ID = ConsignmentID;            Progress.SetMessage("Updating Items");            new Client<PurchaseOrderItem>().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<Type, CoreTable?>? 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);    }    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<PurchaseOrderItem>().Save(items, "Removed from Consignment");        }    }}
 |