InvoiceStockMovementGrid.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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>, ISpecificGrid
  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. public override DynamicGridColumns GenerateColumns()
  32. {
  33. return StockMovementGrid.StandardColumns();
  34. }
  35. protected override void DoReconfigure(DynamicGridOptions options)
  36. {
  37. base.DoReconfigure(options);
  38. options.Clear();
  39. options.EditRows = true;
  40. options.RecordCount = true;
  41. options.SelectColumns = true;
  42. options.MultiSelect = true;
  43. options.FilterRows = true;
  44. options.PageSize = 1000;
  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.ToArray<StockMovement>();
  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. Client.Save(items, bAdd ? "Added to Invoice" : "Removed From Invoice", (o, e) => { });
  69. foreach (var row in rows)
  70. {
  71. row.Set<StockMovement, 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<StockMovement, Guid>(x => x.ID);
  87. var item = arg.ToObject<StockMovement>();
  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. Client.Save(item, "Added to Invoice", (o,e) => { });
  101. arg.Set<StockMovement, 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<StockMovement, bool>(x => x.Charge.Chargeable);
  114. bool included = Entity.IsEntityLinkValid<StockMovement, 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(
  130. Filters<StockMovement> criteria, Columns<StockMovement> columns, ref SortOrder<StockMovement>? sort,
  131. CancellationToken token, Action<CoreTable?, Exception?> action)
  132. {
  133. if (Invoice.ID == Guid.Empty)
  134. criteria.Add(new Filter<StockMovement>().None());
  135. else
  136. {
  137. criteria.Add(new Filter<StockMovement>(x => x.Job.ID).IsEqualTo(Invoice.JobLink.ID));
  138. criteria.Add(new Filter<StockMovement>(x => x.Type).IsEqualTo(StockMovementType.Issue));
  139. if (_showall)
  140. criteria.Add(new Filter<StockMovement>(x => x.Invoice.ID).IsEqualTo(Invoice.ID).Or(x => x.Invoice).NotLinkValid());
  141. else
  142. criteria.Add(new Filter<StockMovement>(x => x.Invoice.ID).IsEqualTo(Invoice.ID));
  143. }
  144. base.Reload(criteria, columns, ref sort, token, action);
  145. }
  146. }