031e499819
API CI/CD / Validate (composer + pint) (push) Successful in 3m10s
API CI/CD / Test (PHPUnit) (push) Failing after 6m49s
API CI/CD / Build frontend assets (push) Successful in 1m3s
API CI/CD / Security audit (push) Failing after 1m0s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
81 lines
2.9 KiB
PHP
81 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Families;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class FamilyFinanceService
|
|
{
|
|
public function loadFinancialsForParents(array $parentIds, string $schoolYear, int $paymentLimit = 10): array
|
|
{
|
|
$parentIds = array_values(array_filter(array_map('intval', $parentIds)));
|
|
$schoolYear = trim($schoolYear);
|
|
|
|
if (empty($parentIds)) {
|
|
return [
|
|
'invoices' => [],
|
|
'payments' => [],
|
|
'invoice_map' => [],
|
|
'summary' => [
|
|
'invoices_count' => 0,
|
|
'total_amount' => 0.0,
|
|
'paid_amount' => 0.0,
|
|
'balance' => 0.0,
|
|
'positive_unpaid_balance' => 0.0,
|
|
'credit_balance' => 0.0,
|
|
],
|
|
];
|
|
}
|
|
|
|
$invRows = DB::table('invoices')
|
|
->select('id', 'parent_id', 'invoice_number', 'status', 'total_amount', 'paid_amount', 'balance', 'issue_date', 'due_date')
|
|
->whereIn('parent_id', $parentIds)
|
|
->when($schoolYear !== '', fn ($query) => $query->where('school_year', $schoolYear))
|
|
->orderBy('issue_date', 'DESC')
|
|
->get()
|
|
->map(fn ($r) => (array) $r)
|
|
->all();
|
|
|
|
$invoiceMap = [];
|
|
$summary = [
|
|
'invoices_count' => 0,
|
|
'total_amount' => 0.0,
|
|
'paid_amount' => 0.0,
|
|
'balance' => 0.0,
|
|
'positive_unpaid_balance' => 0.0,
|
|
'credit_balance' => 0.0,
|
|
];
|
|
foreach ($invRows as $ir) {
|
|
$invoiceMap[(int) ($ir['id'] ?? 0)] = (string) ($ir['invoice_number'] ?? '');
|
|
$balance = (float) ($ir['balance'] ?? 0);
|
|
$summary['invoices_count']++;
|
|
$summary['total_amount'] += (float) ($ir['total_amount'] ?? 0);
|
|
$summary['paid_amount'] += (float) ($ir['paid_amount'] ?? 0);
|
|
$summary['balance'] += $balance;
|
|
if ($balance > 0) {
|
|
$summary['positive_unpaid_balance'] += $balance;
|
|
} elseif ($balance < 0) {
|
|
$summary['credit_balance'] += abs($balance);
|
|
}
|
|
}
|
|
|
|
$payRows = DB::table('payments as p')
|
|
->join('invoices as i', 'i.id', '=', 'p.invoice_id')
|
|
->select('p.id', 'p.parent_id', 'p.invoice_id', 'p.paid_amount', 'p.balance', 'p.payment_method', 'p.payment_date', 'p.status')
|
|
->whereIn('p.parent_id', $parentIds)
|
|
->when($schoolYear !== '', fn ($query) => $query->where('i.school_year', $schoolYear))
|
|
->orderBy('p.payment_date', 'DESC')
|
|
->limit($paymentLimit)
|
|
->get()
|
|
->map(fn ($r) => (array) $r)
|
|
->all();
|
|
|
|
return [
|
|
'invoices' => $invRows,
|
|
'payments' => $payRows,
|
|
'invoice_map' => $invoiceMap,
|
|
'summary' => $summary,
|
|
];
|
|
}
|
|
}
|