| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 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<TimeSheet>
- {
- 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<TimeSheet>(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<TimeSheet>().Load(filter);
- foreach (var timesheet in timesheets)
- timesheet.InvoiceLink.ID = bAdd ? InvoiceID : Guid.Empty;
- new Client<TimeSheet>().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<TimeSheet, Guid>(x => x.ID);
- var timesheet = new Client<TimeSheet>().Load(new Filter<TimeSheet>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
- if (timesheet != null)
- {
- timesheet.InvoiceLink.ID = !timesheet.InvoiceLink.IsValid() ? InvoiceID : Guid.Empty;
- new Client<TimeSheet>().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<TimeSheet, InvoiceLink>(x => x.InvoiceLink, arg))
- return gray;
- return green;
- }
- protected override void Reload(Filters<TimeSheet> criteria, Columns<TimeSheet> columns, ref SortOrder<TimeSheet> sort,
- Action<CoreTable, Exception> action)
- {
- var filter = new Filter<TimeSheet>(x => x.JobLink.ID).IsEqualTo(JobID).And(x => x.Approved).IsNotEqualTo(DateTime.MinValue);
- filter.Ands.Add(new Filter<TimeSheet>(x => x.InvoiceLink.ID).IsEqualTo(InvoiceID).Or(x => x.InvoiceLink).NotLinkValid());
- criteria.Add(filter);
- base.Reload(criteria, columns, ref sort, action);
- }
- }
- }
|