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 InvoiceAssignmentGrid : 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 InvoiceAssignmentGrid() { JobID = CoreUtils.FullGuid; InvoiceID = CoreUtils.FullGuid; ColumnsTag = "InvoiceTimeSheets"; Options .BeginUpdate() .Clear() .Add(DynamicGridOption.EditRows) .Add(DynamicGridOption.RecordCount) .Add(DynamicGridOption.SelectColumns) .Add(DynamicGridOption.MultiSelect) .Add(DynamicGridOption.FilterRows) .EndUpdate(); 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); } public Guid JobID { get; set; } public Guid InvoiceID { get; set; } private bool IncludeSelected(Button sender, CoreRow[] rows) { if (InvoiceID != Guid.Empty) { using (new WaitCursor()) { var bAdd = sender == IncludeButton; var assignments = rows.Select(r => r.ToObject()).ToArray(); foreach (var assignment in assignments) assignment.Invoice.ID = bAdd ? InvoiceID : Guid.Empty; new Client().Save(assignments, bAdd ? "Added to Invoice" : "Removed From Invoice", (o, e) => { }); foreach (var row in rows) { row.Set(x => x.Invoice.ID, bAdd ? InvoiceID : 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 (InvoiceID != Guid.Empty) { var id = arg.Get(x => x.ID); var assignment = arg.ToObject(); if (assignment != null) { assignment.Invoice.ID = !assignment.Invoice.IsValid() ? InvoiceID : Guid.Empty; new Client().Save(assignment, "Added to Invoice", (o,e) => { }); arg.Set(x=>x.Invoice.ID, assignment.Invoice.ID); InvalidateRow(arg); } } else MessageBox.Show("Please Select or Create an Invoice First!"); return false; } private bool ToggleShowAll(Button arg1, CoreRow[] rows) { _showall = !_showall; UpdateButton(ShowAllButton, null, _showall ? "Selected Only" : "Show All"); return true; } 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; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { if ((new Guid[] { CoreUtils.FullGuid, Guid.Empty }).Intersect(new Guid[] { JobID, InvoiceID }).Any()) criteria.Add(new Filter().None()); else { if (JobID != Guid.Empty) criteria.Add(new Filter(x => x.JobLink.ID).IsEqualTo(JobID)); if (_showall) criteria.Add(new Filter(x => x.Invoice.ID).IsEqualTo(InvoiceID).Or(x => x.Invoice).NotLinkValid()); else criteria.Add(new Filter(x => x.Invoice.ID).IsEqualTo(InvoiceID)); } base.Reload(criteria, columns, ref sort, action); } } }