using System; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop { public class InvoicePartsGrid : DynamicDataGrid { private readonly BitmapImage gray = PRSDesktop.Resources.tick.AsGrayScale().AsBitmapImage(); private readonly BitmapImage green = PRSDesktop.Resources.tick.AsBitmapImage(); public InvoicePartsGrid() { ColumnsTag = "InvoiceParts"; Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns, DynamicGridOption.MultiSelect); HiddenColumns.Add(x => x.InvoiceLink.ID); HiddenColumns.Add(x => x.InvoiceLink.Deleted); ActionColumns.Add(new DynamicActionColumn(InvoicedImage, InvoiceOne)); AddButton("Select All", green, InvoiceAll); AddButton("Clear All", gray, InvoiceAll); } public Guid JobID { get; set; } public Guid InvoiceID { get; set; } private bool InvoiceAll(Button sender, CoreRow[] rows) { if (InvoiceID != Guid.Empty) { Progress.Show("Please wait.."); var bAdd = sender.Content.Equals("Select All"); var filter = new Filter(x => x.JobLink.ID).IsEqualTo(JobID); if (bAdd) filter = filter.And(x => x.InvoiceLink).NotLinkValid(); else filter = filter.And(x => x.InvoiceLink).LinkValid(InvoiceID); var items = new Client().Load(filter); foreach (var item in items) item.InvoiceLink.ID = bAdd ? InvoiceID : Guid.Empty; new Client().Save(items, bAdd ? "Added to Invoice" : "Removed From Invoice"); Progress.Close(); return true; } MessageBox.Show("Please Select or Create an Invoice First!"); return false; } private bool InvoiceOne(CoreRow arg) { if (InvoiceID != Guid.Empty) { if (arg == null) return false; var id = arg.Get(x => x.ID); var item = new Client().Load(new Filter(x => x.ID).IsEqualTo(id)).FirstOrDefault(); if (item != null) { item.InvoiceLink.ID = InvoiceID; new Client().Save(item, "Added to Invoice"); return true; } } MessageBox.Show("Please Select or Create an Invoice First!"); return false; } private BitmapImage InvoicedImage(CoreRow arg) { if (arg == null) return green; if (!Entity.IsEntityLinkValid(x => x.InvoiceLink, arg)) return gray; return green; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { var filter = new Filter(x => x.JobLink.ID).IsEqualTo(JobID); //.And(x => x.DeliveredDate).IsNotEqualTo(DateTime.MinValue); filter.Ands.Add(new Filter(x => x.InvoiceLink.ID).IsEqualTo(InvoiceID).Or(x => x.InvoiceLink.ID).IsEqualTo(Guid.Empty)); criteria.Add(filter); base.Reload(criteria, columns, ref sort, action); } } }