InvoiceRequisitionGrid.cs 5.7 KB

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