InvoiceAssignmentGrid.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.Invoice.Deleted);
  28. HiddenColumns.Add(x => x.Charge.Chargeable);
  29. ActionColumns.Add(new DynamicImageColumn(IncludeImage, IncludeOne));
  30. AddButton("Exclude", chargeable_excluded, IncludeSelected, DynamicGridButtonPosition.Right);
  31. IncludeButton = AddButton("Include", chargeable_included, IncludeSelected, DynamicGridButtonPosition.Right);
  32. ShowAllButton = AddButton("Show All", null, ToggleShowAll);
  33. }
  34. protected override void DoReconfigure(DynamicGridOptions options)
  35. {
  36. base.DoReconfigure(options);
  37. options.Clear();
  38. options.EditRows = true;
  39. options.RecordCount = true;
  40. options.SelectColumns = true;
  41. options.MultiSelect = true;
  42. options.FilterRows = true;
  43. }
  44. public Invoice Invoice { get; set; }
  45. private bool IncludeSelected(Button sender, CoreRow[] rows)
  46. {
  47. if (Invoice.ID != Guid.Empty)
  48. {
  49. using (new WaitCursor())
  50. {
  51. var bAdd = sender == IncludeButton;
  52. var assignments = rows.Select(r => r.ToObject<Assignment>()).ToArray();
  53. foreach (var assignment in assignments)
  54. {
  55. if (bAdd)
  56. {
  57. assignment.Invoice.ID = Invoice.ID;
  58. assignment.Invoice.Synchronise(Invoice);
  59. }
  60. else
  61. {
  62. assignment.Invoice.ID = Guid.Empty;
  63. assignment.Invoice.Synchronise(new Invoice());
  64. }
  65. }
  66. new Client<Assignment>().Save(assignments, bAdd ? "Added to Invoice" : "Removed From Invoice", (o, e) => { });
  67. foreach (var row in rows)
  68. {
  69. row.Set<Assignment, Guid>(x => x.Invoice.ID, bAdd ? Invoice.ID : Guid.Empty);
  70. InvalidateRow(row);
  71. }
  72. }
  73. }
  74. else
  75. MessageBox.Show("Please Select or Create an Invoice First!");
  76. return false;
  77. }
  78. private bool IncludeOne(CoreRow arg)
  79. {
  80. if (arg == null)
  81. return false;
  82. if (Invoice.ID != Guid.Empty)
  83. {
  84. var id = arg.Get<Assignment, Guid>(x => x.ID);
  85. var assignment = arg.ToObject<Assignment>();
  86. if (assignment != null)
  87. {
  88. if (assignment.Invoice.IsValid())
  89. {
  90. assignment.Invoice.ID = Invoice.ID;
  91. assignment.Invoice.Synchronise(Invoice);
  92. }
  93. else
  94. {
  95. assignment.Invoice.ID = Guid.Empty;
  96. assignment.Invoice.Synchronise(new Invoice());
  97. }
  98. new Client<Assignment>().Save(assignment, "Added to Invoice", (o,e) => { });
  99. arg.Set<Assignment, Guid>(x=>x.Invoice.ID, assignment.Invoice.ID);
  100. InvalidateRow(arg);
  101. }
  102. }
  103. else
  104. MessageBox.Show("Please Select or Create an Invoice First!");
  105. return false;
  106. }
  107. private bool ToggleShowAll(Button arg1, CoreRow[] rows)
  108. {
  109. _showall = !_showall;
  110. UpdateButton(ShowAllButton, null, _showall ? "Selected Only" : "Show All");
  111. return true;
  112. }
  113. private BitmapImage IncludeImage(CoreRow arg)
  114. {
  115. if (arg == null)
  116. return chargeable_included;
  117. bool chargeable = arg.Get<Assignment, bool>(x => x.Charge.Chargeable);
  118. bool included = Entity.IsEntityLinkValid<Assignment, InvoiceLink>(x => x.Invoice, arg);
  119. return chargeable
  120. ? included
  121. ? chargeable_included
  122. : chargeable_excluded
  123. : included
  124. ? unchargeable_included
  125. : unchargeable_excluded;
  126. }
  127. protected override void Reload(
  128. Filters<Assignment> criteria, Columns<Assignment> columns, ref SortOrder<Assignment>? sort,
  129. CancellationToken token, Action<CoreTable?, Exception?> action)
  130. {
  131. if (Invoice.ID == Guid.Empty)
  132. criteria.Add(new Filter<Assignment>().None());
  133. else
  134. {
  135. criteria.Add(new Filter<Assignment>(x => x.JobLink.ID).IsEqualTo(Invoice.JobLink.ID));
  136. if (_showall)
  137. criteria.Add(new Filter<Assignment>(x => x.Invoice.ID).IsEqualTo(Invoice.ID).Or(x => x.Invoice).NotLinkValid());
  138. else
  139. criteria.Add(new Filter<Assignment>(x => x.Invoice.ID).IsEqualTo(Invoice.ID));
  140. }
  141. base.Reload(criteria, columns, ref sort, token, action);
  142. }
  143. }
  144. }