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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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' => $account->accountName,
  38. 'starting_balance' => $account->balance->startingBalance ?? '',
  39. 'debit_balance' => $account->balance->debitBalance,
  40. 'credit_balance' => $account->balance->creditBalance,
  41. 'net_movement' => $account->balance->netMovement ?? '',
  42. 'ending_balance' => $account->balance->endingBalance ?? '',
  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. 'starting_balance' => $accountCategory->summary->startingBalance ?? '',
  53. 'debit_balance' => $accountCategory->summary->debitBalance,
  54. 'credit_balance' => $accountCategory->summary->creditBalance,
  55. 'net_movement' => $accountCategory->summary->netMovement ?? '',
  56. 'ending_balance' => $accountCategory->summary->endingBalance ?? '',
  57. default => '',
  58. };
  59. }
  60. $categories[] = new ReportCategoryDTO(
  61. header: $header,
  62. data: $data,
  63. summary: $summary,
  64. );
  65. }
  66. return $categories;
  67. }
  68. public function getOverallTotals(): array
  69. {
  70. $totals = [];
  71. foreach ($this->getColumns() as $column) {
  72. $totals[] = match ($column->getName()) {
  73. 'account_name' => 'Total for all accounts',
  74. 'debit_balance' => $this->report->overallTotal->debitBalance,
  75. 'credit_balance' => $this->report->overallTotal->creditBalance,
  76. default => '',
  77. };
  78. }
  79. return $totals;
  80. }
  81. }