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.

AccountBalanceReportTransformer.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Transformers;
  3. use App\DTO\AccountDTO;
  4. use App\DTO\ReportCategoryDTO;
  5. use App\Support\Column;
  6. class AccountBalanceReportTransformer extends BaseReportTransformer
  7. {
  8. public function getTitle(): string
  9. {
  10. return 'Account Balances';
  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. 'starting_balance' => $account->balance->startingBalance ?? '',
  42. 'debit_balance' => $account->balance->debitBalance,
  43. 'credit_balance' => $account->balance->creditBalance,
  44. 'net_movement' => $account->balance->netMovement ?? '',
  45. 'ending_balance' => $account->balance->endingBalance ?? '',
  46. default => '',
  47. };
  48. }
  49. return $row;
  50. }, $accountCategory->accounts);
  51. $summary = [];
  52. foreach ($this->getColumns() as $column) {
  53. $summary[] = match ($column->getName()) {
  54. 'account_name' => 'Total ' . $accountCategoryName,
  55. 'starting_balance' => $accountCategory->summary->startingBalance ?? '',
  56. 'debit_balance' => $accountCategory->summary->debitBalance,
  57. 'credit_balance' => $accountCategory->summary->creditBalance,
  58. 'net_movement' => $accountCategory->summary->netMovement ?? '',
  59. 'ending_balance' => $accountCategory->summary->endingBalance ?? '',
  60. default => '',
  61. };
  62. }
  63. $categories[] = new ReportCategoryDTO(
  64. header: $header,
  65. data: $data,
  66. summary: $summary,
  67. );
  68. }
  69. return $categories;
  70. }
  71. public function getOverallTotals(): array
  72. {
  73. $totals = [];
  74. foreach ($this->getColumns() as $column) {
  75. $totals[] = match ($column->getName()) {
  76. 'account_name' => 'Total for all accounts',
  77. 'debit_balance' => $this->report->overallTotal->debitBalance,
  78. 'credit_balance' => $this->report->overallTotal->creditBalance,
  79. default => '',
  80. };
  81. }
  82. return $totals;
  83. }
  84. }