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.

BalanceSheetReportTransformer.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Transformers;
  3. use App\DTO\AccountDTO;
  4. use App\DTO\ReportCategoryDTO;
  5. use App\DTO\ReportTypeDTO;
  6. use App\Support\Column;
  7. class BalanceSheetReportTransformer extends BaseReportTransformer
  8. {
  9. protected string $totalAssets = '$0.00';
  10. protected string $totalLiabilities = '$0.00';
  11. protected string $totalEquity = '$0.00';
  12. public function getTitle(): string
  13. {
  14. return 'Balance Sheet';
  15. }
  16. public function getHeaders(): array
  17. {
  18. return array_map(fn (Column $column) => $column->getLabel(), $this->getColumns());
  19. }
  20. public function calculateTotals(): void
  21. {
  22. foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
  23. match ($accountCategoryName) {
  24. 'Assets' => $this->totalAssets = $accountCategory->summary->endingBalance ?? '',
  25. 'Liabilities' => $this->totalLiabilities = $accountCategory->summary->endingBalance ?? '',
  26. 'Equity' => $this->totalEquity = $accountCategory->summary->endingBalance ?? '',
  27. };
  28. }
  29. }
  30. public function getCategories(): array
  31. {
  32. $categories = [];
  33. foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
  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. // Category-level summary
  43. $categorySummary = [];
  44. foreach ($this->getColumns() as $column) {
  45. $categorySummary[] = match ($column->getName()) {
  46. 'account_name' => 'Total ' . $accountCategoryName,
  47. 'ending_balance' => $accountCategory->summary->endingBalance ?? '',
  48. default => '',
  49. };
  50. }
  51. // Subcategories (types) under the main category
  52. $types = [];
  53. foreach ($accountCategory->types as $typeName => $type) {
  54. // Header for subcategory (type)
  55. $typeHeader = [];
  56. foreach ($this->getColumns() as $index => $column) {
  57. $typeHeader[$index] = $column->getName() === 'account_name' ? $typeName : '';
  58. }
  59. // Account data for the subcategory
  60. $data = array_map(function (AccountDTO $account) {
  61. $row = [];
  62. foreach ($this->getColumns() as $column) {
  63. $row[] = match ($column->getName()) {
  64. 'account_code' => $account->accountCode,
  65. 'account_name' => [
  66. 'name' => $account->accountName,
  67. 'id' => $account->accountId ?? null,
  68. 'start_date' => $account->startDate,
  69. 'end_date' => $account->endDate,
  70. ],
  71. 'ending_balance' => $account->balance->endingBalance ?? '',
  72. default => '',
  73. };
  74. }
  75. return $row;
  76. }, $type->accounts);
  77. // Subcategory (type) summary
  78. $typeSummary = [];
  79. foreach ($this->getColumns() as $column) {
  80. $typeSummary[] = match ($column->getName()) {
  81. 'account_name' => 'Total ' . $typeName,
  82. 'ending_balance' => $type->summary->endingBalance ?? '',
  83. default => '',
  84. };
  85. }
  86. // Add subcategory (type) to the list
  87. $types[$typeName] = new ReportTypeDTO(
  88. header: $typeHeader,
  89. data: $data,
  90. summary: $typeSummary,
  91. );
  92. }
  93. // Add the category to the final array with its subcategories (types)
  94. $categories[$accountCategoryName] = new ReportCategoryDTO(
  95. header: $header,
  96. data: [],
  97. summary: $categorySummary,
  98. types: $types,
  99. );
  100. }
  101. return $categories;
  102. }
  103. public function getOverallTotals(): array
  104. {
  105. return [];
  106. }
  107. public function getSummary(): array
  108. {
  109. $this->calculateTotals();
  110. return [
  111. [
  112. 'label' => 'Total Assets',
  113. 'value' => $this->totalAssets,
  114. ],
  115. [
  116. 'label' => 'Total Liabilities',
  117. 'value' => $this->totalLiabilities,
  118. ],
  119. [
  120. 'label' => 'Net Assets',
  121. 'value' => $this->report->overallTotal->endingBalance ?? '',
  122. ],
  123. ];
  124. }
  125. }