InvoiceLineGrid.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using Comal.Classes;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. using InABox.WPF;
  10. using PRS.Shared;
  11. using PRSDesktop.Utils;
  12. namespace PRSDesktop
  13. {
  14. internal class InvoiceLineGrid : DynamicDataGrid<InvoiceLine>
  15. {
  16. private Job? _job;
  17. public Job? Job
  18. {
  19. get => _job;
  20. set
  21. {
  22. _job = value;
  23. Reconfigure();
  24. }
  25. }
  26. protected override void Init()
  27. {
  28. base.Init();
  29. AddEditButton("Calculate", PRSDesktop.Resources.costcentre.AsBitmapImage(), CalculateLines,
  30. isVisible: options => Job is not null && Job.JobType == JobType.Service);
  31. }
  32. protected override void DoReconfigure(DynamicGridOptions options)
  33. {
  34. base.DoReconfigure(options);
  35. options.RecordCount = true;
  36. options.AddRows = true;
  37. options.DeleteRows = true;
  38. options.EditRows = true;
  39. options.SelectColumns = true;
  40. options.MultiSelect = true;
  41. }
  42. public Invoice? Invoice { get; set; }
  43. private bool CalculateLines(Button sender, CoreRow[] rows)
  44. {
  45. if(Invoice is null)
  46. {
  47. MessageBox.Show("Please Select or Create an Invoice First!");
  48. return false;
  49. }
  50. InvoiceCalculationSelector selector = new InvoiceCalculationSelector()
  51. {
  52. TimeCalculation = InvoiceTimeCalculation.Activity,
  53. MaterialCalculation = InvoiceMaterialCalculation.Product,
  54. ExpensesCalculation = InvoiceExpensesCalculation.Detailed
  55. };
  56. if (selector.ShowDialog() == true)
  57. {
  58. var time = selector.TimeCalculation;
  59. var materials = selector.MaterialCalculation;
  60. var expenses = selector.ExpensesCalculation;
  61. Progress.ShowModal("Calculating Invoice", progress => InvoiceUtilities.GenerateInvoiceLines(Invoice.ID, time, materials, expenses, progress));
  62. return true;
  63. }
  64. else
  65. {
  66. return false;
  67. }
  68. }
  69. protected override void Reload(
  70. Filters<InvoiceLine> criteria, Columns<InvoiceLine> columns, ref SortOrder<InvoiceLine>? sort,
  71. CancellationToken token, Action<CoreTable?, Exception?> action)
  72. {
  73. if(Invoice is null)
  74. {
  75. criteria.Add(Filter.None<InvoiceLine>());
  76. }
  77. else
  78. {
  79. criteria.Add(Filter<InvoiceLine>.Where(x => x.InvoiceLink.ID).IsEqualTo(Invoice.ID));
  80. }
  81. base.Reload(criteria, columns, ref sort, token, action);
  82. }
  83. protected override bool CanCreateItems()
  84. {
  85. return Invoice is not null;
  86. }
  87. public override InvoiceLine CreateItem()
  88. {
  89. var result = base.CreateItem();
  90. if(Invoice is not null)
  91. {
  92. result.InvoiceLink.CopyFrom(Invoice);
  93. result.SellGL.ID = Invoice.SellGL.ID;
  94. }
  95. return result;
  96. }
  97. }
  98. }