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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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' => $account->accountName,
  38. 'debit_balance' => $account->balance->debitBalance,
  39. 'credit_balance' => $account->balance->creditBalance,
  40. default => '',
  41. };
  42. }
  43. return $row;
  44. }, $accountCategory->accounts);
  45. $summary = [];
  46. foreach ($this->getColumns() as $column) {
  47. $summary[] = match ($column->getName()) {
  48. 'account_name' => 'Total ' . $accountCategoryName,
  49. 'debit_balance' => $accountCategory->summary->debitBalance,
  50. 'credit_balance' => $accountCategory->summary->creditBalance,
  51. default => '',
  52. };
  53. }
  54. $categories[] = new ReportCategoryDTO(
  55. header: $header,
  56. data: $data,
  57. summary: $summary,
  58. );
  59. }
  60. return $categories;
  61. }
  62. public function getOverallTotals(): array
  63. {
  64. $totals = [];
  65. foreach ($this->getColumns() as $column) {
  66. $totals[] = match ($column->getName()) {
  67. 'account_name' => 'Total for all accounts',
  68. 'debit_balance' => $this->report->overallTotal->debitBalance,
  69. 'credit_balance' => $this->report->overallTotal->creditBalance,
  70. default => '',
  71. };
  72. }
  73. return $totals;
  74. }
  75. }