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 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 'Trial Balance';
  11. }
  12. public function getHeaders(): array
  13. {
  14. return array_map(fn (Column $column) => $column->getLabel(), $this->getColumns());
  15. }
  16. /**
  17. * @return ReportCategoryDTO[]
  18. */
  19. public function getCategories(): array
  20. {
  21. $categories = [];
  22. foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
  23. // Initialize header with empty strings
  24. $header = [];
  25. foreach ($this->getColumns() as $index => $column) {
  26. if ($column->getName() === 'account_name') {
  27. $header[$index] = $accountCategoryName;
  28. } else {
  29. $header[$index] = '';
  30. }
  31. }
  32. $data = array_map(function (AccountDTO $account) {
  33. $row = [];
  34. foreach ($this->getColumns() as $column) {
  35. $row[] = match ($column->getName()) {
  36. 'account_code' => $account->accountCode,
  37. 'account_name' => [
  38. 'name' => $account->accountName,
  39. 'id' => $account->accountId ?? null,
  40. ],
  41. 'debit_balance' => $account->balance->debitBalance,
  42. 'credit_balance' => $account->balance->creditBalance,
  43. default => '',
  44. };
  45. }
  46. return $row;
  47. }, $accountCategory->accounts);
  48. $summary = [];
  49. foreach ($this->getColumns() as $column) {
  50. $summary[] = match ($column->getName()) {
  51. 'account_name' => 'Total ' . $accountCategoryName,
  52. 'debit_balance' => $accountCategory->summary->debitBalance,
  53. 'credit_balance' => $accountCategory->summary->creditBalance,
  54. default => '',
  55. };
  56. }
  57. $categories[] = new ReportCategoryDTO(
  58. header: $header,
  59. data: $data,
  60. summary: $summary,
  61. );
  62. }
  63. return $categories;
  64. }
  65. public function getOverallTotals(): array
  66. {
  67. $totals = [];
  68. foreach ($this->getColumns() as $column) {
  69. $totals[] = match ($column->getName()) {
  70. 'account_name' => 'Total for all accounts',
  71. 'debit_balance' => $this->report->overallTotal->debitBalance,
  72. 'credit_balance' => $this->report->overallTotal->creditBalance,
  73. default => '',
  74. };
  75. }
  76. return $totals;
  77. }
  78. }