fix closing year
Tests / PHPUnit (push) Successful in 1m20s

This commit is contained in:
root
2026-08-10 01:11:16 -04:00
parent 1ef2800f12
commit c12bb59372
5 changed files with 416 additions and 126 deletions
@@ -14,11 +14,13 @@ class SchoolYearClosingController extends BaseController
$targetId = $this->normalizeInt($this->request->getGet('target_school_year_id'));
$preview = service('schoolYearClosing')->preview($id, $targetId);
$promotionTable = $this->promotionTablePayload($preview['promotion']['rows'] ?? []);
$carryForwardTable = $this->carryForwardTablePayload($preview['carry_forward'] ?? []);
$latestBatch = service('schoolYearClosing')->latestBatch($id);
return view('school_years/closing_preview', [
'preview' => $preview,
'promotionTable' => $promotionTable,
'carryForwardTable' => $carryForwardTable,
'latestBatch' => $latestBatch,
'missingCarryForwardInvoices' => $this->missingCarryForwardInvoiceCount($latestBatch),
'schoolYears' => (new SchoolYearModel())->orderBy('name', 'DESC')->findAll(),
@@ -118,13 +120,10 @@ class SchoolYearClosingController extends BaseController
private function promotionTablePayload(array $rows): array
{
$allowedSorts = ['student', 'school_id', 'class', 'year_score', 'decision', 'source', 'queue', 'target', 'status'];
$sort = (string) ($this->request->getGet('sort') ?? 'class');
$sort = in_array($sort, $allowedSorts, true) ? $sort : 'class';
$order = strtolower((string) ($this->request->getGet('order') ?? 'asc')) === 'desc' ? 'desc' : 'asc';
$page = max(1, (int) ($this->request->getGet('page') ?? 1));
$perPage = (int) ($this->request->getGet('per_page') ?? 25);
$sort = 'class';
$order = 'asc';
$perPage = 25;
$allowedPerPage = [10, 25, 50, 100];
$perPage = in_array($perPage, $allowedPerPage, true) ? $perPage : 25;
usort($rows, function (array $a, array $b) use ($sort, $order): int {
$comparison = $this->comparePromotionRows($a, $b, $sort);
@@ -142,21 +141,18 @@ class SchoolYearClosingController extends BaseController
});
$total = count($rows);
$pageCount = max(1, (int) ceil($total / $perPage));
$page = min($page, $pageCount);
$offset = ($page - 1) * $perPage;
return [
'rows' => array_slice($rows, $offset, $perPage),
'rows' => $rows,
'sort' => $sort,
'order' => $order,
'page' => $page,
'page' => 1,
'perPage' => $perPage,
'total' => $total,
'pageCount' => $pageCount,
'pageCount' => 1,
'allowedPerPage' => $allowedPerPage,
'from' => $total === 0 ? 0 : $offset + 1,
'to' => min($offset + $perPage, $total),
'from' => $total === 0 ? 0 : 1,
'to' => $total,
];
}
@@ -198,6 +194,58 @@ class SchoolYearClosingController extends BaseController
};
}
private function carryForwardTablePayload(array $rows): array
{
$allowedSorts = ['family', 'parent', 'source_balance', 'credits', 'adjustments', 'net'];
$sort = (string) ($this->request->getGet('cf_sort') ?? 'family');
$sort = in_array($sort, $allowedSorts, true) ? $sort : 'family';
$order = strtolower((string) ($this->request->getGet('cf_order') ?? 'asc')) === 'desc' ? 'desc' : 'asc';
usort($rows, function (array $a, array $b) use ($sort, $order): int {
$comparison = $this->compareCarryForwardRows($a, $b, $sort);
if ($comparison === 0 && $sort !== 'family') {
$comparison = $this->compareCarryForwardRows($a, $b, 'family');
}
if ($comparison === 0) {
$comparison = ((int) ($a['family_id'] ?? 0)) <=> ((int) ($b['family_id'] ?? 0));
}
return $order === 'desc' ? -$comparison : $comparison;
});
return [
'rows' => $rows,
'sort' => $sort,
'order' => $order,
];
}
private function compareCarryForwardRows(array $a, array $b, string $sort): int
{
$numericSorts = ['source_balance', 'credits', 'adjustments', 'net'];
$aValue = $this->carryForwardSortValue($a, $sort);
$bValue = $this->carryForwardSortValue($b, $sort);
if (in_array($sort, $numericSorts, true)) {
return (float) $aValue <=> (float) $bValue;
}
return strnatcasecmp((string) $aValue, (string) $bValue);
}
private function carryForwardSortValue(array $row, string $sort): mixed
{
return match ($sort) {
'family' => (string) ($row['family'] ?? ''),
'parent' => trim((string) ($row['parent'] ?? '') . ' ' . (string) ($row['parent_email'] ?? '')),
'source_balance' => (float) ($row['source_balance'] ?? 0),
'credits' => (float) ($row['credit_amount'] ?? 0),
'adjustments' => (float) ($row['adjustment_amount'] ?? 0),
'net' => (float) ($row['carry_forward_amount'] ?? 0),
default => '',
};
}
private function userId(): ?int
{
$id = session('user_id') ?? session('id');