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 InvoiceTimeGrid : DynamicDataGrid { private Button ClearAllButton; private readonly BitmapImage gray = PRSDesktop.Resources.tick.AsGrayScale().AsBitmapImage(); private readonly BitmapImage green = PRSDesktop.Resources.tick.AsBitmapImage(); private readonly Button SelectAllButton; public InvoiceTimeGrid() { ColumnsTag = "InvoiceTimeSheets"; 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)); SelectAllButton = AddButton("Select All", green, InvoiceAll); ClearAllButton = 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 == SelectAllButton; var filter = new Filter(x => x.JobLink.ID).IsEqualTo(JobID).And(x => x.Approved).IsNotEqualTo(DateTime.MinValue); if (bAdd) filter = filter.And(x => x.InvoiceLink).NotLinkValid(); else filter = filter.And(x => x.InvoiceLink).LinkValid(InvoiceID); var timesheets = new Client().Load(filter); foreach (var timesheet in timesheets) timesheet.InvoiceLink.ID = bAdd ? InvoiceID : Guid.Empty; new Client().Save(timesheets, 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 timesheet = new Client().Load(new Filter(x => x.ID).IsEqualTo(id)).FirstOrDefault(); if (timesheet != null) { timesheet.InvoiceLink.ID = !timesheet.InvoiceLink.IsValid() ? InvoiceID : Guid.Empty; new Client().Save(timesheet, "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.Approved).IsNotEqualTo(DateTime.MinValue); filter.Ands.Add(new Filter(x => x.InvoiceLink.ID).IsEqualTo(InvoiceID).Or(x => x.InvoiceLink).NotLinkValid()); criteria.Add(filter); base.Reload(criteria, columns, ref sort, action); } } }