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.

TrialBalanceReportTransformer.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Transformers;
  3. use App\DTO\AccountDTO;
  4. use App\DTO\ReportCategoryDTO;
  5. use App\Support\Column;
  6. class TrialBalanceReportTransformer extends BaseReportTransformer
  7. {
  8. public function getTitle(): string
  9. {
  10. return match ($this->report->reportType) {
  11. 'postClosing' => 'Post-Closing Trial Balance',
  12. default => 'Standard Trial Balance',
  13. };
  14. }
  15. public function getHeaders(): array
  16. {
  17. return array_map(fn (Column $column) => $column->getLabel(), $this->getColumns());
  18. }
  19. /**
  20. * @return ReportCategoryDTO[]
  21. */
  22. public function getCategories(): array
  23. {
  24. $categories = [];
  25. foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
  26. // Initialize header with empty strings
  27. $header = [];
  28. foreach ($this->getColumns() as $index => $column) {
  29. if ($column->getName() === 'account_name') {
  30. $header[$index] = $accountCategoryName;
  31. } else {
  32. $header[$index] = '';
  33. }
  34. }
  35. $data = array_map(function (AccountDTO $account) {
  36. $row = [];
  37. foreach ($this->getColumns() as $column) {
  38. $row[] = match ($column->getName()) {
  39. 'account_code' => $account->accountCode,
  40. 'account_name' => [
  41. 'name' => $account->accountName,
  42. 'id' => $account->accountId ?? null,
  43. 'start_date' => $account->startDate,
  44. 'end_date' => $account->endDate,
  45. ],
  46. 'debit_balance' => $account->balance->debitBalance,
  47. 'credit_balance' => $account->balance->creditBalance,
  48. default => '',
  49. };
  50. }
  51. return $row;
  52. }, $accountCategory->accounts);
  53. $summary = [];
  54. foreach ($this->getColumns() as $column) {
  55. $summary[] = match ($column->getName()) {
  56. 'account_name' => 'Total ' . $accountCategoryName,
  57. 'debit_balance' => $accountCategory->summary->debitBalance,
  58. 'credit_balance' => $accountCategory->summary->creditBalance,
  59. default => '',
  60. };
  61. }
  62. $categories[] = new ReportCategoryDTO(
  63. header: $header,
  64. data: $data,
  65. summary: $summary,
  66. );
  67. }
  68. return $categories;
  69. }
  70. public function getOverallTotals(): array
  71. {
  72. $totals = [];
  73. foreach ($this->getColumns() as $column) {
  74. $totals[] = match ($column->getName()) {
  75. 'account_name' => 'Total for all accounts',
  76. 'debit_balance' => $this->report->overallTotal->debitBalance,
  77. 'credit_balance' => $this->report->overallTotal->creditBalance,
  78. default => '',
  79. };
  80. }
  81. return $totals;
  82. }
  83. }