Files
alrahma_sunday_school/app/Services/FinancialCategorySummaryService.php
T
root c3a30989b2
Deploy to Shared Hosting / Shared hosting deploy (push) Failing after 51s
Tests / PHPUnit (push) Successful in 1m25s
add new ststs feature
2026-09-13 02:20:14 -04:00

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,
];
}
}