You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IncomeStatementReportTransformer.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Transformers;
  3. use App\DTO\AccountDTO;
  4. use App\DTO\ReportCategoryDTO;
  5. use App\Support\Column;
  6. class IncomeStatementReportTransformer extends BaseReportTransformer
  7. {
  8. protected string $totalRevenue = '$0.00';
  9. protected string $totalCogs = '$0.00';
  10. protected string $totalExpenses = '$0.00';
  11. public function getTitle(): string
  12. {
  13. return 'Income Statement';
  14. }
  15. public function getHeaders(): array
  16. {
  17. return array_map(fn (Column $column) => $column->getLabel(), $this->getColumns());
  18. }
  19. public function calculateTotals(): void
  20. {
  21. foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
  22. match ($accountCategoryName) {
  23. 'Revenue' => $this->totalRevenue = $accountCategory->summary->netMovement ?? '',
  24. 'Cost of Goods Sold' => $this->totalCogs = $accountCategory->summary->netMovement ?? '',
  25. 'Expenses' => $this->totalExpenses = $accountCategory->summary->netMovement ?? '',
  26. };
  27. }
  28. }
  29. public function getCategories(): array
  30. {
  31. $categories = [];
  32. foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
  33. // Initialize header with empty strings
  34. $header = [];
  35. foreach ($this->getColumns() as $index => $column) {
  36. if ($column->getName() === 'account_name') {
  37. $header[$index] = $accountCategoryName;
  38. } else {
  39. $header[$index] = '';
  40. }
  41. }
  42. $data = array_map(function (AccountDTO $account) {
  43. $row = [];
  44. foreach ($this->getColumns() as $column) {
  45. $row[] = match ($column->getName()) {
  46. 'account_code' => $account->accountCode,
  47. 'account_name' => [
  48. 'name' => $account->accountName,
  49. 'id' => $account->accountId ?? null,
  50. ],
  51. 'net_movement' => $account->balance->netMovement ?? '',
  52. default => '',
  53. };
  54. }
  55. return $row;
  56. }, $accountCategory->accounts);
  57. $summary = [];
  58. foreach ($this->getColumns() as $column) {
  59. $summary[] = match ($column->getName()) {
  60. 'account_name' => 'Total ' . $accountCategoryName,
  61. 'net_movement' => $accountCategory->summary->netMovement ?? '',
  62. default => '',
  63. };
  64. }
  65. $categories[] = new ReportCategoryDTO(
  66. header: $header,
  67. data: $data,
  68. summary: $summary,
  69. );
  70. }
  71. return $categories;
  72. }
  73. public function getOverallTotals(): array
  74. {
  75. $totals = [];
  76. foreach ($this->getColumns() as $column) {
  77. $totals[] = match ($column->getName()) {
  78. 'account_name' => 'Net Earnings',
  79. 'net_movement' => $this->report->overallTotal->netMovement ?? '',
  80. default => '',
  81. };
  82. }
  83. return $totals;
  84. }
  85. public function getSummary(): array
  86. {
  87. $this->calculateTotals();
  88. return [
  89. [
  90. 'label' => 'Revenue',
  91. 'value' => $this->totalRevenue,
  92. ],
  93. [
  94. 'label' => 'Cost of Goods Sold',
  95. 'value' => $this->totalCogs,
  96. ],
  97. [
  98. 'label' => 'Expenses',
  99. 'value' => $this->totalExpenses,
  100. ],
  101. [
  102. 'label' => 'Net Earnings',
  103. 'value' => $this->report->overallTotal->netMovement ?? '',
  104. ],
  105. ];
  106. }
  107. }