42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
final class FinancialCategorySummaryService
|
|
{
|
|
/**
|
|
* Classify grouped rows from the legacy expenses table by accounting meaning.
|
|
* Donation rows are incoming funds, not school expenses.
|
|
*/
|
|
public static function summarize(array $rows): array
|
|
{
|
|
$expenseCategories = [];
|
|
$totalExpenses = 0.0;
|
|
$donationIncome = 0.0;
|
|
|
|
foreach ($rows as $row) {
|
|
$category = trim((string) ($row['category'] ?? '')) ?: 'Uncategorized';
|
|
$amount = round((float) ($row['amount'] ?? $row['total_amount'] ?? 0), 2);
|
|
if ($amount <= 0) {
|
|
continue;
|
|
}
|
|
|
|
if (strcasecmp($category, 'Donation') === 0) {
|
|
$donationIncome += $amount;
|
|
continue;
|
|
}
|
|
|
|
$expenseCategories[$category] = round(($expenseCategories[$category] ?? 0) + $amount, 2);
|
|
$totalExpenses += $amount;
|
|
}
|
|
|
|
arsort($expenseCategories, SORT_NUMERIC);
|
|
|
|
return [
|
|
'totalExpenses' => round($totalExpenses, 2),
|
|
'donationIncome' => round($donationIncome, 2),
|
|
'expenseCategories' => $expenseCategories,
|
|
];
|
|
}
|
|
}
|