選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AccountBalanceReportTransformer.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. 'start_date' => $account->startDate,
  41. 'end_date' => $account->endDate,
  42. ],
  43. 'starting_balance' => $account->balance->startingBalance ?? '',
  44. 'debit_balance' => $account->balance->debitBalance,
  45. 'credit_balance' => $account->balance->creditBalance,
  46. 'net_movement' => $account->balance->netMovement ?? '',
  47. 'ending_balance' => $account->balance->endingBalance ?? '',
  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. 'starting_balance' => $accountCategory->summary->startingBalance ?? '',
  58. 'debit_balance' => $accountCategory->summary->debitBalance,
  59. 'credit_balance' => $accountCategory->summary->creditBalance,
  60. 'net_movement' => $accountCategory->summary->netMovement ?? '',
  61. 'ending_balance' => $accountCategory->summary->endingBalance ?? '',
  62. default => '',
  63. };
  64. }
  65. $categories[] = new ReportCategoryDTO(
  66. header: $header,
  67. data: $data,
  68. summary: $summary,
  69. );
  70. }
  71. return $categories;
  72. }
  73. public function getOverallTotals(): array
  74. {
  75. $totals = [];
  76. foreach ($this->getColumns() as $column) {
  77. $totals[] = match ($column->getName()) {
  78. 'account_name' => 'Total for all accounts',
  79. 'debit_balance' => $this->report->overallTotal->debitBalance,
  80. 'credit_balance' => $this->report->overallTotal->creditBalance,
  81. default => '',
  82. };
  83. }
  84. return $totals;
  85. }
  86. }