InvoiceStockMovementGrid.cs 5.3 KB

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