InvoicePartsGrid.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 InvoicePartsGrid : DynamicDataGrid<DeliveryItem>
  14. {
  15. private readonly BitmapImage gray = PRSDesktop.Resources.tick.AsGrayScale().AsBitmapImage();
  16. private readonly BitmapImage green = PRSDesktop.Resources.tick.AsBitmapImage();
  17. public InvoicePartsGrid()
  18. {
  19. ColumnsTag = "InvoiceParts";
  20. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns, DynamicGridOption.MultiSelect);
  21. HiddenColumns.Add(x => x.InvoiceLink.ID);
  22. HiddenColumns.Add(x => x.InvoiceLink.Deleted);
  23. ActionColumns.Add(new DynamicActionColumn(InvoicedImage, InvoiceOne));
  24. AddButton("Select All", green, InvoiceAll);
  25. AddButton("Clear All", gray, InvoiceAll);
  26. }
  27. public Guid JobID { get; set; }
  28. public Guid InvoiceID { get; set; }
  29. private bool InvoiceAll(Button sender, CoreRow[] rows)
  30. {
  31. if (InvoiceID != Guid.Empty)
  32. {
  33. Progress.Show("Please wait..");
  34. var bAdd = sender.Content.Equals("Select All");
  35. var filter = new Filter<DeliveryItem>(x => x.JobLink.ID).IsEqualTo(JobID);
  36. if (bAdd)
  37. filter = filter.And(x => x.InvoiceLink).NotLinkValid();
  38. else
  39. filter = filter.And(x => x.InvoiceLink).LinkValid(InvoiceID);
  40. var items = new Client<DeliveryItem>().Load(filter);
  41. foreach (var item in items)
  42. item.InvoiceLink.ID = bAdd ? InvoiceID : Guid.Empty;
  43. new Client<DeliveryItem>().Save(items, bAdd ? "Added to Invoice" : "Removed From Invoice");
  44. Progress.Close();
  45. return true;
  46. }
  47. MessageBox.Show("Please Select or Create an Invoice First!");
  48. return false;
  49. }
  50. private bool InvoiceOne(CoreRow arg)
  51. {
  52. if (InvoiceID != Guid.Empty)
  53. {
  54. if (arg == null)
  55. return false;
  56. var id = arg.Get<TimeSheet, Guid>(x => x.ID);
  57. var item = new Client<DeliveryItem>().Load(new Filter<DeliveryItem>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  58. if (item != null)
  59. {
  60. item.InvoiceLink.ID = InvoiceID;
  61. new Client<DeliveryItem>().Save(item, "Added to Invoice");
  62. return true;
  63. }
  64. }
  65. MessageBox.Show("Please Select or Create an Invoice First!");
  66. return false;
  67. }
  68. private BitmapImage InvoicedImage(CoreRow arg)
  69. {
  70. if (arg == null)
  71. return green;
  72. if (!Entity.IsEntityLinkValid<TimeSheet, InvoiceLink>(x => x.InvoiceLink, arg))
  73. return gray;
  74. return green;
  75. }
  76. protected override void Reload(Filters<DeliveryItem> criteria, Columns<DeliveryItem> columns, ref SortOrder<DeliveryItem> sort,
  77. Action<CoreTable, Exception> action)
  78. {
  79. var filter = new Filter<DeliveryItem>(x => x.JobLink.ID).IsEqualTo(JobID); //.And(x => x.DeliveredDate).IsNotEqualTo(DateTime.MinValue);
  80. filter.Ands.Add(new Filter<DeliveryItem>(x => x.InvoiceLink.ID).IsEqualTo(InvoiceID).Or(x => x.InvoiceLink.ID).IsEqualTo(Guid.Empty));
  81. criteria.Add(filter);
  82. base.Reload(criteria, columns, ref sort, action);
  83. }
  84. }
  85. }