InvoiceAssignmentGrid.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 InvoiceAssignmentGrid : DynamicDataGrid<Assignment>
  14. {
  15. private readonly BitmapImage chargeable_excluded = PRSDesktop.Resources.tick.Fade(0.8F).AsGrayScale().AsBitmapImage();
  16. private readonly BitmapImage chargeable_included = PRSDesktop.Resources.tick.AsBitmapImage();
  17. private readonly BitmapImage unchargeable_excluded = PRSDesktop.Resources.warning.Fade(0.8F).AsGrayScale().AsBitmapImage();
  18. private readonly BitmapImage unchargeable_included = PRSDesktop.Resources.warning.AsBitmapImage();
  19. private readonly Button IncludeButton;
  20. private readonly Button ShowAllButton;
  21. private bool _showall = false;
  22. public InvoiceAssignmentGrid()
  23. {
  24. ColumnsTag = "InvoiceTimeSheets";
  25. HiddenColumns.Add(x => x.Invoice.ID);
  26. HiddenColumns.Add(x => x.Invoice.Deleted);
  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(FluentList<DynamicGridOption> options)
  34. {
  35. base.DoReconfigure(options);
  36. options
  37. .BeginUpdate()
  38. .Clear()
  39. .Add(DynamicGridOption.EditRows)
  40. .Add(DynamicGridOption.RecordCount)
  41. .Add(DynamicGridOption.SelectColumns)
  42. .Add(DynamicGridOption.MultiSelect)
  43. .Add(DynamicGridOption.FilterRows)
  44. .EndUpdate();
  45. }
  46. public Invoice Invoice { get; set; }
  47. private bool IncludeSelected(Button sender, CoreRow[] rows)
  48. {
  49. if (Invoice.ID != Guid.Empty)
  50. {
  51. using (new WaitCursor())
  52. {
  53. var bAdd = sender == IncludeButton;
  54. var assignments = rows.Select(r => r.ToObject<Assignment>()).ToArray();
  55. foreach (var assignment in assignments)
  56. {
  57. if (bAdd)
  58. {
  59. assignment.Invoice.ID = Invoice.ID;
  60. assignment.Invoice.Synchronise(Invoice);
  61. }
  62. else
  63. {
  64. assignment.Invoice.ID = Guid.Empty;
  65. assignment.Invoice.Synchronise(new Invoice());
  66. }
  67. }
  68. new Client<Assignment>().Save(assignments, bAdd ? "Added to Invoice" : "Removed From Invoice", (o, e) => { });
  69. foreach (var row in rows)
  70. {
  71. row.Set<Assignment, Guid>(x => x.Invoice.ID, bAdd ? Invoice.ID : Guid.Empty);
  72. InvalidateRow(row);
  73. }
  74. }
  75. }
  76. else
  77. MessageBox.Show("Please Select or Create an Invoice First!");
  78. return false;
  79. }
  80. private bool IncludeOne(CoreRow arg)
  81. {
  82. if (arg == null)
  83. return false;
  84. if (Invoice.ID != Guid.Empty)
  85. {
  86. var id = arg.Get<Assignment, Guid>(x => x.ID);
  87. var assignment = arg.ToObject<Assignment>();
  88. if (assignment != null)
  89. {
  90. if (assignment.Invoice.IsValid())
  91. {
  92. assignment.Invoice.ID = Invoice.ID;
  93. assignment.Invoice.Synchronise(Invoice);
  94. }
  95. else
  96. {
  97. assignment.Invoice.ID = Guid.Empty;
  98. assignment.Invoice.Synchronise(new Invoice());
  99. }
  100. new Client<Assignment>().Save(assignment, "Added to Invoice", (o,e) => { });
  101. arg.Set<Assignment, Guid>(x=>x.Invoice.ID, assignment.Invoice.ID);
  102. InvalidateRow(arg);
  103. }
  104. }
  105. else
  106. MessageBox.Show("Please Select or Create an Invoice First!");
  107. return false;
  108. }
  109. private bool ToggleShowAll(Button arg1, CoreRow[] rows)
  110. {
  111. _showall = !_showall;
  112. UpdateButton(ShowAllButton, null, _showall ? "Selected Only" : "Show All");
  113. return true;
  114. }
  115. private BitmapImage IncludeImage(CoreRow arg)
  116. {
  117. if (arg == null)
  118. return chargeable_included;
  119. bool chargeable = arg.Get<Assignment, bool>(x => x.Charge.Chargeable);
  120. bool included = Entity.IsEntityLinkValid<Assignment, InvoiceLink>(x => x.Invoice, arg);
  121. return chargeable
  122. ? included
  123. ? chargeable_included
  124. : chargeable_excluded
  125. : included
  126. ? unchargeable_included
  127. : unchargeable_excluded;
  128. }
  129. protected override void Reload(Filters<Assignment> criteria, Columns<Assignment> columns, ref SortOrder<Assignment>? sort, 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, action);
  142. }
  143. }
  144. }