InvoiceTimeGrid.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media.Imaging;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.WPF;
  11. namespace PRSDesktop
  12. {
  13. public class InvoiceTimeGrid : DynamicDataGrid<TimeSheet>
  14. {
  15. private Button ClearAllButton;
  16. private readonly BitmapImage gray = PRSDesktop.Resources.tick.AsGrayScale().AsBitmapImage();
  17. private readonly BitmapImage green = PRSDesktop.Resources.tick.AsBitmapImage();
  18. private readonly Button SelectAllButton;
  19. public InvoiceTimeGrid()
  20. {
  21. ColumnsTag = "InvoiceTimeSheets";
  22. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns, DynamicGridOption.MultiSelect);
  23. HiddenColumns.Add(x => x.InvoiceLink.ID);
  24. HiddenColumns.Add(x => x.InvoiceLink.Deleted);
  25. ActionColumns.Add(new DynamicActionColumn(InvoicedImage, InvoiceOne));
  26. SelectAllButton = AddButton("Select All", green, InvoiceAll);
  27. ClearAllButton = AddButton("Clear All", gray, InvoiceAll);
  28. }
  29. public Guid JobID { get; set; }
  30. public Guid InvoiceID { get; set; }
  31. private bool InvoiceAll(Button sender, CoreRow[] rows)
  32. {
  33. if (InvoiceID != Guid.Empty)
  34. {
  35. Progress.Show("Please wait..");
  36. var bAdd = sender == SelectAllButton;
  37. var filter = new Filter<TimeSheet>(x => x.JobLink.ID).IsEqualTo(JobID).And(x => x.Approved).IsNotEqualTo(DateTime.MinValue);
  38. if (bAdd)
  39. filter = filter.And(x => x.InvoiceLink).NotLinkValid();
  40. else
  41. filter = filter.And(x => x.InvoiceLink).LinkValid(InvoiceID);
  42. var timesheets = new Client<TimeSheet>().Load(filter);
  43. foreach (var timesheet in timesheets)
  44. timesheet.InvoiceLink.ID = bAdd ? InvoiceID : Guid.Empty;
  45. new Client<TimeSheet>().Save(timesheets, bAdd ? "Added to Invoice" : "Removed From Invoice");
  46. Progress.Close();
  47. return true;
  48. }
  49. MessageBox.Show("Please Select or Create an Invoice First!");
  50. return false;
  51. }
  52. private bool InvoiceOne(CoreRow arg)
  53. {
  54. if (InvoiceID != Guid.Empty)
  55. {
  56. if (arg == null)
  57. return false;
  58. var id = arg.Get<TimeSheet, Guid>(x => x.ID);
  59. var timesheet = new Client<TimeSheet>().Load(new Filter<TimeSheet>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  60. if (timesheet != null)
  61. {
  62. timesheet.InvoiceLink.ID = !timesheet.InvoiceLink.IsValid() ? InvoiceID : Guid.Empty;
  63. new Client<TimeSheet>().Save(timesheet, "Added to Invoice");
  64. return true;
  65. }
  66. }
  67. MessageBox.Show("Please Select or Create an Invoice First!");
  68. return false;
  69. }
  70. private BitmapImage InvoicedImage(CoreRow arg)
  71. {
  72. if (arg == null)
  73. return green;
  74. if (!Entity.IsEntityLinkValid<TimeSheet, InvoiceLink>(x => x.InvoiceLink, arg))
  75. return gray;
  76. return green;
  77. }
  78. protected override void Reload(Filters<TimeSheet> criteria, Columns<TimeSheet> columns, ref SortOrder<TimeSheet> sort,
  79. Action<CoreTable, Exception> action)
  80. {
  81. var filter = new Filter<TimeSheet>(x => x.JobLink.ID).IsEqualTo(JobID).And(x => x.Approved).IsNotEqualTo(DateTime.MinValue);
  82. filter.Ands.Add(new Filter<TimeSheet>(x => x.InvoiceLink.ID).IsEqualTo(InvoiceID).Or(x => x.InvoiceLink).NotLinkValid());
  83. criteria.Add(filter);
  84. base.Reload(criteria, columns, ref sort, action);
  85. }
  86. }
  87. }