using System; using System.Linq; using System.Threading; 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 InvoiceRequisitionItemGrid : DynamicDataGrid { private readonly BitmapImage chargeable_excluded = PRSDesktop.Resources.tick.Fade(0.8F).AsGrayScale().AsBitmapImage(); private readonly BitmapImage chargeable_included = PRSDesktop.Resources.tick.AsBitmapImage(); private readonly BitmapImage unchargeable_excluded = PRSDesktop.Resources.warning.Fade(0.8F).AsGrayScale().AsBitmapImage(); private readonly BitmapImage unchargeable_included = PRSDesktop.Resources.warning.AsBitmapImage(); private readonly Button IncludeButton; private readonly Button ShowAllButton; private bool _showall = false; public InvoiceRequisitionItemGrid() { ColumnsTag = "InvoiceParts"; HiddenColumns.Add(x => x.Invoice.ID); HiddenColumns.Add(x => x.Invoice.Deleted); HiddenColumns.Add(x => x.Charge.Chargeable); ActionColumns.Add(new DynamicImageColumn(IncludeImage, IncludeOne)); AddButton("Exclude", chargeable_excluded, IncludeSelected, DynamicGridButtonPosition.Right); IncludeButton = AddButton("Include", chargeable_included, IncludeSelected, DynamicGridButtonPosition.Right); ShowAllButton = AddButton("Show All", null, ToggleShowAll); } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.Clear(); options.EditRows = true; options.RecordCount = true; options.SelectColumns = true; options.MultiSelect = true; options.FilterRows = true; } public Invoice Invoice { get; set; } private bool IncludeSelected(Button sender, CoreRow[] rows) { if (Invoice.ID != Guid.Empty) { using (new WaitCursor()) { var bAdd = sender == IncludeButton; var items = rows.Select(r => r.ToObject()).ToArray(); foreach (var item in items) { if (bAdd) { item.Invoice.ID = Invoice.ID; item.Invoice.Synchronise(Invoice); } else { item.Invoice.ID = Guid.Empty; item.Invoice.Synchronise(new Invoice()); } } new Client().Save(items, bAdd ? "Added to Invoice" : "Removed From Invoice", (o, e) => { }); foreach (var row in rows) { row.Set(x => x.Invoice.ID, bAdd ? Invoice.ID : Guid.Empty); InvalidateRow(row); } } } else MessageBox.Show("Please Select or Create an Invoice First!"); return false; } private bool IncludeOne(CoreRow arg) { if (arg == null) return false; if (Invoice.ID != Guid.Empty) { var id = arg.Get(x => x.ID); var item = arg.ToObject(); if (item != null) { if (!item.Invoice.IsValid()) { item.Invoice.ID = Invoice.ID; item.Invoice.Synchronise(Invoice); } else { item.Invoice.ID = Guid.Empty; item.Invoice.Synchronise(new Invoice()); } new Client().Save(item, "Added to Invoice", (o,e) => { }); arg.Set(x=>x.Invoice.ID, item.Invoice.ID); InvalidateRow(arg); } } else MessageBox.Show("Please Select or Create an Invoice First!"); return false; } private BitmapImage IncludeImage(CoreRow arg) { if (arg == null) return chargeable_included; bool chargeable = arg.Get(x => x.Charge.Chargeable); bool included = Entity.IsEntityLinkValid(x => x.Invoice, arg); return chargeable ? included ? chargeable_included : chargeable_excluded : included ? unchargeable_included : unchargeable_excluded; } private bool ToggleShowAll(Button arg1, CoreRow[] rows) { _showall = !_showall; UpdateButton(ShowAllButton, null, _showall ? "Selected Only" : "Show All"); return true; } protected override void Reload( Filters criteria, Columns columns, ref SortOrder? sort, CancellationToken token, Action action) { if (Invoice.ID == Guid.Empty) criteria.Add(new Filter().None()); else { criteria.Add(new Filter(x => x.RequisitionLink.JobLink.ID).IsEqualTo(Invoice.JobLink.ID)); if (_showall) criteria.Add(new Filter(x => x.Invoice.ID).IsEqualTo(Invoice.ID).Or(x => x.Invoice).NotLinkValid()); else criteria.Add(new Filter(x => x.Invoice.ID).IsEqualTo(Invoice.ID)); } criteria.Add(new Filter(x => x.Done).IsEqualTo(true)); base.Reload(criteria, columns, ref sort, token, action); } } }