InvoiceRequisitionGrid.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 InvoiceRequisitionItemGrid : DynamicDataGrid<RequisitionItem>
  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 InvoiceRequisitionItemGrid()
  23. {
  24. ColumnsTag = "InvoiceParts";
  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 items = rows.Select(r => r.ToObject<RequisitionItem>()).ToArray();
  55. foreach (var item in items)
  56. {
  57. if (bAdd)
  58. {
  59. item.Invoice.ID = Invoice.ID;
  60. item.Invoice.Synchronise(Invoice);
  61. }
  62. else
  63. {
  64. item.Invoice.ID = Guid.Empty;
  65. item.Invoice.Synchronise(new Invoice());
  66. }
  67. }
  68. new Client<RequisitionItem>().Save(items, bAdd ? "Added to Invoice" : "Removed From Invoice", (o, e) => { });
  69. foreach (var row in rows)
  70. {
  71. row.Set<RequisitionItem, 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<RequisitionItem, Guid>(x => x.ID);
  87. var item = arg.ToObject<RequisitionItem>();
  88. if (item != null)
  89. {
  90. if (!item.Invoice.IsValid())
  91. {
  92. item.Invoice.ID = Invoice.ID;
  93. item.Invoice.Synchronise(Invoice);
  94. }
  95. else
  96. {
  97. item.Invoice.ID = Guid.Empty;
  98. item.Invoice.Synchronise(new Invoice());
  99. }
  100. new Client<RequisitionItem>().Save(item, "Added to Invoice", (o,e) => { });
  101. arg.Set<RequisitionItem, Guid>(x=>x.Invoice.ID, item.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 BitmapImage IncludeImage(CoreRow arg)
  110. {
  111. if (arg == null)
  112. return chargeable_included;
  113. bool chargeable = arg.Get<RequisitionItem, bool>(x => x.Charge.Chargeable);
  114. bool included = Entity.IsEntityLinkValid<RequisitionItem, InvoiceLink>(x => x.Invoice, arg);
  115. return chargeable
  116. ? included
  117. ? chargeable_included
  118. : chargeable_excluded
  119. : included
  120. ? unchargeable_included
  121. : unchargeable_excluded;
  122. }
  123. private bool ToggleShowAll(Button arg1, CoreRow[] rows)
  124. {
  125. _showall = !_showall;
  126. UpdateButton(ShowAllButton, null, _showall ? "Selected Only" : "Show All");
  127. return true;
  128. }
  129. protected override void Reload(Filters<RequisitionItem> criteria, Columns<RequisitionItem> columns, ref SortOrder<RequisitionItem>? sort, Action<CoreTable?, Exception?> action)
  130. {
  131. if (Invoice.ID == Guid.Empty)
  132. criteria.Add(new Filter<RequisitionItem>().None());
  133. else
  134. {
  135. criteria.Add(new Filter<RequisitionItem>(x => x.RequisitionLink.JobLink.ID).IsEqualTo(Invoice.JobLink.ID));
  136. if (_showall)
  137. criteria.Add(new Filter<RequisitionItem>(x => x.Invoice.ID).IsEqualTo(Invoice.ID).Or(x => x.Invoice).NotLinkValid());
  138. else
  139. criteria.Add(new Filter<RequisitionItem>(x => x.Invoice.ID).IsEqualTo(Invoice.ID));
  140. }
  141. base.Reload(criteria, columns, ref sort, action);
  142. }
  143. }
  144. }