InvoiceAssignmentGrid.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media.Imaging;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.WPF;
  12. namespace PRSDesktop
  13. {
  14. public class InvoiceAssignmentGrid : DynamicDataGrid<Assignment>
  15. {
  16. private readonly BitmapImage chargeable_excluded = PRSDesktop.Resources.tick.Fade(0.8F).AsGrayScale().AsBitmapImage();
  17. private readonly BitmapImage chargeable_included = PRSDesktop.Resources.tick.AsBitmapImage();
  18. private readonly BitmapImage unchargeable_excluded = PRSDesktop.Resources.warning.Fade(0.8F).AsGrayScale().AsBitmapImage();
  19. private readonly BitmapImage unchargeable_included = PRSDesktop.Resources.warning.AsBitmapImage();
  20. private readonly Button IncludeButton;
  21. private readonly Button ShowAllButton;
  22. private bool _showall = false;
  23. public InvoiceAssignmentGrid()
  24. {
  25. ColumnsTag = "InvoiceTimeSheets";
  26. HiddenColumns.Add(x => x.Invoice.ID);
  27. HiddenColumns.Add(x => x.Charge.Chargeable);
  28. ActionColumns.Add(new DynamicImageColumn(IncludeImage, IncludeOne));
  29. AddButton("Exclude", chargeable_excluded, IncludeSelected, DynamicGridButtonPosition.Right);
  30. IncludeButton = AddButton("Include", chargeable_included, IncludeSelected, DynamicGridButtonPosition.Right);
  31. ShowAllButton = AddButton("Show All", null, ToggleShowAll);
  32. }
  33. protected override void DoReconfigure(DynamicGridOptions options)
  34. {
  35. base.DoReconfigure(options);
  36. options.Clear();
  37. options.EditRows = true;
  38. options.RecordCount = true;
  39. options.SelectColumns = true;
  40. options.MultiSelect = true;
  41. options.FilterRows = true;
  42. }
  43. public Invoice Invoice { get; set; }
  44. private bool IncludeSelected(Button sender, CoreRow[] rows)
  45. {
  46. if (Invoice.ID != Guid.Empty)
  47. {
  48. using (new WaitCursor())
  49. {
  50. var bAdd = sender == IncludeButton;
  51. var assignments = rows.Select(r => r.ToObject<Assignment>()).ToArray();
  52. foreach (var assignment in assignments)
  53. {
  54. if (bAdd)
  55. {
  56. assignment.Invoice.ID = Invoice.ID;
  57. assignment.Invoice.Synchronise(Invoice);
  58. }
  59. else
  60. {
  61. assignment.Invoice.ID = Guid.Empty;
  62. assignment.Invoice.Synchronise(new Invoice());
  63. }
  64. }
  65. new Client<Assignment>().Save(assignments, bAdd ? "Added to Invoice" : "Removed From Invoice", (o, e) => { });
  66. foreach (var row in rows)
  67. {
  68. row.Set<Assignment, Guid>(x => x.Invoice.ID, bAdd ? Invoice.ID : Guid.Empty);
  69. InvalidateRow(row);
  70. }
  71. }
  72. }
  73. else
  74. MessageBox.Show("Please Select or Create an Invoice First!");
  75. return false;
  76. }
  77. private bool IncludeOne(CoreRow arg)
  78. {
  79. if (arg == null)
  80. return false;
  81. if (Invoice.ID != Guid.Empty)
  82. {
  83. var id = arg.Get<Assignment, Guid>(x => x.ID);
  84. var assignment = arg.ToObject<Assignment>();
  85. if (assignment != null)
  86. {
  87. if (assignment.Invoice.IsValid())
  88. {
  89. assignment.Invoice.ID = Invoice.ID;
  90. assignment.Invoice.Synchronise(Invoice);
  91. }
  92. else
  93. {
  94. assignment.Invoice.ID = Guid.Empty;
  95. assignment.Invoice.Synchronise(new Invoice());
  96. }
  97. new Client<Assignment>().Save(assignment, "Added to Invoice", (o,e) => { });
  98. arg.Set<Assignment, Guid>(x=>x.Invoice.ID, assignment.Invoice.ID);
  99. InvalidateRow(arg);
  100. }
  101. }
  102. else
  103. MessageBox.Show("Please Select or Create an Invoice First!");
  104. return false;
  105. }
  106. private bool ToggleShowAll(Button arg1, CoreRow[] rows)
  107. {
  108. _showall = !_showall;
  109. UpdateButton(ShowAllButton, null, _showall ? "Selected Only" : "Show All");
  110. return true;
  111. }
  112. private BitmapImage IncludeImage(CoreRow arg)
  113. {
  114. if (arg == null)
  115. return chargeable_included;
  116. bool chargeable = arg.Get<Assignment, bool>(x => x.Charge.Chargeable);
  117. bool included = Entity.IsEntityLinkValid<Assignment, InvoiceLink>(x => x.Invoice, arg);
  118. return chargeable
  119. ? included
  120. ? chargeable_included
  121. : chargeable_excluded
  122. : included
  123. ? unchargeable_included
  124. : unchargeable_excluded;
  125. }
  126. protected override void Reload(
  127. Filters<Assignment> criteria, Columns<Assignment> columns, ref SortOrder<Assignment>? sort,
  128. CancellationToken token, Action<CoreTable?, Exception?> action)
  129. {
  130. if (Invoice.ID == Guid.Empty)
  131. criteria.Add(new Filter<Assignment>().None());
  132. else
  133. {
  134. criteria.Add(new Filter<Assignment>(x => x.JobLink.ID).IsEqualTo(Invoice.JobLink.ID));
  135. if (_showall)
  136. criteria.Add(new Filter<Assignment>(x => x.Invoice.ID).IsEqualTo(Invoice.ID).Or(x => x.Invoice).NotLinkValid());
  137. else
  138. criteria.Add(new Filter<Assignment>(x => x.Invoice.ID).IsEqualTo(Invoice.ID));
  139. }
  140. base.Reload(criteria, columns, ref sort, token, action);
  141. }
  142. }
  143. }