buckets/app/Services/Streams/StatsService.php

34 lines
1.1 KiB
PHP
Raw Normal View History

2025-12-31 00:02:54 +01:00
<?php
namespace App\Services\Streams;
use App\Models\Stream;
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', Stream::TYPE_INCOME)
->sum(fn($stream) => $stream->getMonthlyEquivalent());
$totalMonthlyExpenses = $streams
->where('type', Stream::TYPE_EXPENSE)
->sum(fn($stream) => $stream->getMonthlyEquivalent());
return [
'total_streams' => $streams->count(),
'active_streams' => $streams->where('is_active', true)->count(),
'income_streams' => $streams->where('type', Stream::TYPE_INCOME)->count(),
'expense_streams' => $streams->where('type', Stream::TYPE_EXPENSE)->count(),
'monthly_income' => $totalMonthlyIncome,
'monthly_expenses' => $totalMonthlyExpenses,
'monthly_net' => $totalMonthlyIncome - $totalMonthlyExpenses,
];
}
}