Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

IncomeStatementReportTransformer.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. 'start_date' => $account->startDate,
  51. 'end_date' => $account->endDate,
  52. ],
  53. 'net_movement' => $account->balance->netMovement ?? '',
  54. default => '',
  55. };
  56. }
  57. return $row;
  58. }, $accountCategory->accounts);
  59. $summary = [];
  60. foreach ($this->getColumns() as $column) {
  61. $summary[] = match ($column->getName()) {
  62. 'account_name' => 'Total ' . $accountCategoryName,
  63. 'net_movement' => $accountCategory->summary->netMovement ?? '',
  64. default => '',
  65. };
  66. }
  67. $categories[] = new ReportCategoryDTO(
  68. header: $header,
  69. data: $data,
  70. summary: $summary,
  71. );
  72. }
  73. return $categories;
  74. }
  75. public function getOverallTotals(): array
  76. {
  77. $totals = [];
  78. foreach ($this->getColumns() as $column) {
  79. $totals[] = match ($column->getName()) {
  80. 'account_name' => 'Net Earnings',
  81. 'net_movement' => $this->report->overallTotal->netMovement ?? '',
  82. default => '',
  83. };
  84. }
  85. return $totals;
  86. }
  87. public function getSummary(): array
  88. {
  89. $this->calculateTotals();
  90. return [
  91. [
  92. 'label' => 'Revenue',
  93. 'value' => $this->totalRevenue,
  94. ],
  95. [
  96. 'label' => 'Cost of Goods Sold',
  97. 'value' => $this->totalCogs,
  98. ],
  99. [
  100. 'label' => 'Expenses',
  101. 'value' => $this->totalExpenses,
  102. ],
  103. [
  104. 'label' => 'Net Earnings',
  105. 'value' => $this->report->overallTotal->netMovement ?? '',
  106. ],
  107. ];
  108. }
  109. }