buckets/app/Services/Streams/StatsService.php

35 lines
1.1 KiB
PHP
Raw Normal View History

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