buckets/app/Services/Streams/StatsService.php
2026-01-01 03:18:40 +01:00

34 lines
1.1 KiB
PHP

<?php
namespace App\Services\Streams;
use App\Enums\StreamTypeEnum;
use App\Models\Scenario;
readonly class StatsService
{
public function getSummaryStats(Scenario $scenario): array
{
$streams = $scenario->streams()
->where('is_active', true)
->get();
$totalMonthlyIncome = $streams
->where('type', StreamTypeEnum::INCOME)
->sum(fn($stream) => $stream->getMonthlyEquivalent());
$totalMonthlyExpenses = $streams
->where('type', StreamTypeEnum::EXPENSE)
->sum(fn($stream) => $stream->getMonthlyEquivalent());
return [
'total_streams' => $streams->count(),
'active_streams' => $streams->where('is_active', true)->count(),
'income_streams' => $streams->where('type', StreamTypeEnum::INCOME)->count(),
'expense_streams' => $streams->where('type', StreamTypeEnum::EXPENSE)->count(),
'monthly_income' => $totalMonthlyIncome,
'monthly_expenses' => $totalMonthlyExpenses,
'monthly_net' => $totalMonthlyIncome - $totalMonthlyExpenses,
];
}
}