123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
-
- namespace App\Transformers;
-
- use App\DTO\AccountDTO;
- use App\DTO\ReportCategoryDTO;
- use App\DTO\ReportDTO;
- use App\Utilities\Currency\CurrencyAccessor;
-
- class IncomeStatementReportTransformer extends SummaryReportTransformer
- {
- protected string $totalRevenue;
-
- protected string $totalCogs;
-
- protected string $totalExpenses;
-
- public function __construct(ReportDTO $report)
- {
- parent::__construct($report);
-
- $this->calculateTotals();
- }
-
- public function getTitle(): string
- {
- return 'Income Statement';
- }
-
- public function calculateTotals(): void
- {
- foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
- match ($accountCategoryName) {
- 'Revenue' => $this->totalRevenue = $accountCategory->summary->netMovement ?? '',
- 'Cost of Goods Sold' => $this->totalCogs = $accountCategory->summary->netMovement ?? '',
- 'Expenses' => $this->totalExpenses = $accountCategory->summary->netMovement ?? '',
- };
- }
- }
-
- public function getCategories(): array
- {
- $categories = [];
-
- foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
- $header = [];
-
- foreach ($this->getColumns() as $column) {
- $header[$column->getName()] = $column->getName() === 'account_name' ? $accountCategoryName : '';
- }
-
- $data = array_map(function (AccountDTO $account) {
- $row = [];
-
- foreach ($this->getColumns() as $column) {
- $row[$column->getName()] = match ($column->getName()) {
- 'account_code' => $account->accountCode,
- 'account_name' => [
- 'name' => $account->accountName,
- 'id' => $account->accountId ?? null,
- 'start_date' => $account->startDate,
- 'end_date' => $account->endDate,
- ],
- 'net_movement' => $account->balance->netMovement ?? '',
- default => '',
- };
- }
-
- return $row;
- }, $accountCategory->accounts);
-
- $summary = [];
-
- foreach ($this->getColumns() as $column) {
- $summary[$column->getName()] = match ($column->getName()) {
- 'account_name' => 'Total ' . $accountCategoryName,
- 'net_movement' => $accountCategory->summary->netMovement ?? '',
- default => '',
- };
- }
-
- $categories[] = new ReportCategoryDTO(
- header: $header,
- data: $data,
- summary: $summary,
- );
- }
-
- return $categories;
- }
-
- public function getSummaryCategories(): array
- {
- $summaryCategories = [];
-
- $columns = $this->getSummaryColumns();
-
- foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
- // Header for the main category
- $categoryHeader = [];
-
- foreach ($columns as $column) {
- $categoryHeader[$column->getName()] = $column->getName() === 'account_name' ? $accountCategoryName : '';
- }
-
- // Category-level summary
- $categorySummary = [];
- foreach ($columns as $column) {
- $categorySummary[$column->getName()] = match ($column->getName()) {
- 'account_name' => $accountCategoryName,
- 'net_movement' => $accountCategory->summary->netMovement ?? '',
- default => '',
- };
- }
-
- // Add the category summary to the final array
- $summaryCategories[$accountCategoryName] = new ReportCategoryDTO(
- header: $categoryHeader,
- data: [], // No direct accounts are needed here, only summaries
- summary: $categorySummary,
- types: [] // No types for the income statement
- );
- }
-
- return $summaryCategories;
- }
-
- public function getGrossProfit(): array
- {
- $grossProfit = [];
-
- $columns = $this->getSummaryColumns();
-
- $revenue = money($this->totalRevenue, CurrencyAccessor::getDefaultCurrency())->getAmount();
- $cogs = money($this->totalCogs, CurrencyAccessor::getDefaultCurrency())->getAmount();
-
- $grossProfitAmount = $revenue - $cogs;
- $grossProfitFormatted = money($grossProfitAmount, CurrencyAccessor::getDefaultCurrency(), true)->format();
-
- foreach ($columns as $column) {
- $grossProfit[$column->getName()] = match ($column->getName()) {
- 'account_name' => 'Gross Profit',
- 'net_movement' => $grossProfitFormatted,
- default => '',
- };
- }
-
- return $grossProfit;
- }
-
- public function getOverallTotals(): array
- {
- $totals = [];
-
- foreach ($this->getColumns() as $column) {
- $totals[$column->getName()] = match ($column->getName()) {
- 'account_name' => 'Net Earnings',
- 'net_movement' => $this->report->overallTotal->netMovement ?? '',
- default => '',
- };
- }
-
- return $totals;
- }
-
- public function getSummaryOverallTotals(): array
- {
- $totals = [];
- $columns = $this->getSummaryColumns();
-
- foreach ($columns as $column) {
- $totals[$column->getName()] = match ($column->getName()) {
- 'account_name' => 'Net Earnings',
- 'net_movement' => $this->report->overallTotal->netMovement ?? '',
- default => '',
- };
- }
-
- return $totals;
- }
-
- public function getSummary(): array
- {
- return [
- [
- 'label' => 'Revenue',
- 'value' => $this->totalRevenue,
- ],
- [
- 'label' => 'Cost of Goods Sold',
- 'value' => $this->totalCogs,
- ],
- [
- 'label' => 'Expenses',
- 'value' => $this->totalExpenses,
- ],
- [
- 'label' => 'Net Earnings',
- 'value' => $this->report->overallTotal->netMovement ?? '',
- ],
- ];
- }
- }
|