Files
alrahma_sunday_school/app/Views/school_years/closing_preview.php
T
root c12bb59372
Tests / PHPUnit (push) Successful in 1m20s
fix closing year
2026-08-10 01:11:16 -04:00

455 lines
21 KiB
PHP

<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<?php
$source = $preview['source'] ?? [];
$target = $preview['target'] ?? null;
$overview = $preview['overview'] ?? [];
$finance = $preview['finance'] ?? [];
$promotion = $preview['promotion'] ?? ['summary' => [], 'rows' => []];
$promotionSummary = $promotion['summary'] ?? [];
$promotionRows = $promotion['rows'] ?? [];
$promotionTotal = count($promotionRows);
$blockers = $preview['blockers'] ?? [];
$warnings = $preview['warnings'] ?? [];
$carryForward = $preview['carry_forward'] ?? [];
$status = (string) ($source['status'] ?? '');
$batchStatus = (string) ($latestBatch['status'] ?? '');
$missingCarryForwardInvoices = (int) ($missingCarryForwardInvoices ?? 0);
$money = static fn ($value): string => '$' . number_format((float) $value, 2);
$score = static fn ($value): string => is_numeric($value) ? number_format((float) $value, 2) : '-';
$ageBySchoolYearSecondSeptember = static function (array $row) use ($source): string {
$dob = trim((string) ($row['dob'] ?? ''));
$schoolYear = (string) ($source['name'] ?? '');
if ($dob === '' || preg_match('/^\d{4}-(\d{4})$/', $schoolYear, $matches) !== 1) {
return '';
}
try {
$birthDate = new DateTimeImmutable($dob);
$cutoff = new DateTimeImmutable($matches[1] . '-09-01');
} catch (Throwable) {
return '';
}
if ($birthDate > $cutoff) {
return '';
}
return (string) $birthDate->diff($cutoff)->y;
};
$kgPlacementFallback = static function (array $row) use ($target): string {
$classText = strtoupper(trim(
(string) ($row['class_name'] ?? '') . ' '
. (string) ($row['class_section_name'] ?? '')
));
if (preg_match('/(^|[^A-Z0-9])KG([^A-Z0-9]|$)/', $classText) !== 1 && ! str_contains($classText, 'KINDERGARTEN')) {
return '';
}
$dob = trim((string) ($row['dob'] ?? ''));
if ($dob === '') {
return '';
}
try {
$targetName = (string) ($target['name'] ?? '');
if (preg_match('/^(\d{4})-\d{4}$/', $targetName, $matches) === 1) {
$cutoff = new DateTimeImmutable($matches[1] . '-09-01');
} else {
$today = new DateTimeImmutable('today');
$cutoff = new DateTimeImmutable($today->format('Y') . '-09-01');
if ($today > $cutoff) {
$cutoff = $cutoff->modify('+1 year');
}
}
$birthDate = new DateTimeImmutable($dob);
} catch (Throwable) {
return '';
}
if ($birthDate > $cutoff) {
return '';
}
return $birthDate->diff($cutoff)->y >= 6 ? '1' : 'KG';
};
?>
<?php if (session()->getFlashdata('success')): ?>
<div class="alert alert-success"><?= esc(session()->getFlashdata('success')) ?></div>
<?php elseif (session()->getFlashdata('error')): ?>
<div class="alert alert-danger"><?= esc(session()->getFlashdata('error')) ?></div>
<?php endif; ?>
<div class="container-fluid py-3">
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
<div>
<h2 class="mb-1">End-Year Checklist: <?= esc($source['name'] ?? '') ?></h2>
<div class="text-muted">
Status: <span class="fw-semibold"><?= esc(ucfirst($status)) ?></span>
<?php if ($target): ?>
<span class="mx-2">Next year: <span class="fw-semibold"><?= esc($target['name'] ?? '') ?></span></span>
<?php endif; ?>
<span>Generated: <?= esc($preview['generated_at'] ?? '') ?></span>
</div>
</div>
<a class="btn btn-outline-secondary" href="<?= site_url('administrator/school-years') ?>">Back to School Years</a>
</div>
<div class="border rounded bg-white p-3 mb-4">
<h5 class="mb-2">How to End This Year</h5>
<ol class="mb-0">
<li>Clear all blocking issues below.</li>
<li>Start the end-year process to lock the checklist.</li>
<li>Confirm carry-forward balances, then mark this school year closed.</li>
</ol>
</div>
<form class="row g-2 align-items-end mb-4" method="get" action="<?= site_url('administrator/school-years/' . (int) ($source['id'] ?? 0) . '/closing/preview') ?>">
<div class="col-md-4">
<label class="form-label" for="target_school_year_id">Next school year</label>
<select class="form-select" id="target_school_year_id" name="target_school_year_id">
<option value="">Auto-select next draft year</option>
<?php foreach (($schoolYears ?? []) as $year): ?>
<?php if ((int) ($year['id'] ?? 0) !== (int) ($source['id'] ?? 0)): ?>
<option value="<?= (int) $year['id'] ?>" <?= $target && (int) $target['id'] === (int) $year['id'] ? 'selected' : '' ?>>
<?= esc($year['name'] ?? '') ?> (<?= esc($year['status'] ?? '') ?>)
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-3">
<button class="btn btn-primary" type="submit">Refresh Checklist</button>
</div>
</form>
<div class="row g-3 mb-4">
<div class="col-md-3">
<div class="border rounded bg-white p-3 h-100">
<div class="text-muted small">Students</div>
<div class="fs-5 fw-semibold"><?= esc((string) ($overview['students'] ?? 0)) ?></div>
</div>
</div>
<div class="col-md-3">
<div class="border rounded bg-white p-3 h-100">
<div class="text-muted small">Families</div>
<div class="fs-5 fw-semibold"><?= esc((string) ($overview['families'] ?? 0)) ?></div>
</div>
</div>
<div class="col-md-3">
<div class="border rounded bg-white p-3 h-100">
<div class="text-muted small">Invoices</div>
<div class="fs-5 fw-semibold"><?= esc((string) ($overview['invoices'] ?? 0)) ?></div>
</div>
</div>
<div class="col-md-3">
<div class="border rounded bg-white p-3 h-100">
<div class="text-muted small">Outstanding</div>
<div class="fs-5 fw-semibold"><?= esc($money($overview['total_outstanding'] ?? 0)) ?></div>
</div>
</div>
</div>
<div class="row g-3 mb-4">
<div class="col-lg-6">
<div class="border rounded bg-white p-3 h-100">
<h5>Blocking Issues</h5>
<?php if ($blockers === []): ?>
<p class="text-muted mb-0">No blocking issues found.</p>
<?php else: ?>
<ul class="mb-0">
<?php foreach ($blockers as $finding): ?>
<li><strong><?= esc($finding['title']) ?>:</strong> <?= esc($finding['detail']) ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
<div class="col-lg-6">
<div class="border rounded bg-white p-3 h-100">
<h5>Warnings</h5>
<?php if ($warnings === []): ?>
<p class="text-muted mb-0">No warnings found.</p>
<?php else: ?>
<ul class="mb-0">
<?php foreach ($warnings as $finding): ?>
<li><strong><?= esc($finding['title']) ?>:</strong> <?= esc($finding['detail']) ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
<div class="row g-3 mb-4">
<div class="col-lg-5">
<div class="border rounded bg-white p-3 h-100">
<h5>Finance Summary</h5>
<dl class="row mb-0">
<dt class="col-7">Total invoiced</dt>
<dd class="col-5 text-end"><?= esc($money($finance['total_invoiced'] ?? 0)) ?></dd>
<dt class="col-7">Total paid</dt>
<dd class="col-5 text-end"><?= esc($money($finance['total_paid'] ?? 0)) ?></dd>
<dt class="col-7">Positive balances</dt>
<dd class="col-5 text-end"><?= esc($money($finance['positive_balance'] ?? 0)) ?></dd>
<dt class="col-7">Credits</dt>
<dd class="col-5 text-end"><?= esc($money($finance['credit_balance'] ?? 0)) ?></dd>
</dl>
</div>
</div>
<div class="col-lg-7">
<div class="border rounded bg-white p-3 h-100">
<h5>End-Year Progress</h5>
<ol class="mb-0">
<li>Checklist generated</li>
<li class="<?= $blockers === [] ? '' : 'text-muted' ?>">Validation <?= $blockers === [] ? 'passed' : 'has blockers' ?></li>
<li class="<?= $batchStatus !== '' ? '' : 'text-muted' ?>">End-year process <?= $batchStatus !== '' ? esc($batchStatus) : 'not started' ?></li>
<li class="<?= in_array($batchStatus, ['executed', 'completed'], true) ? '' : 'text-muted' ?>">Carry-forward completed</li>
<li class="<?= $status === 'closed' ? '' : 'text-muted' ?>">Year closed</li>
</ol>
</div>
</div>
</div>
<div class="border rounded bg-white p-3 mb-4" id="promotion-table">
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
<div>
<h5 class="mb-1">Student Promotion Preview</h5>
<div class="text-muted small">Promotion decisions must be generated and resolved before this school year can end.</div>
</div>
<a class="btn btn-outline-primary btn-sm" href="<?= site_url('grading/below-60/decisions') ?>">
Manage Promotion Decisions
</a>
</div>
<div class="row g-3 mb-3">
<div class="col-lg-2 col-md-3 col-6">
<div class="border rounded p-2 h-100">
<div class="text-muted small">Students</div>
<div class="fw-semibold"><?= esc((string) ($promotionSummary['total_students'] ?? 0)) ?></div>
</div>
</div>
<div class="col-lg-2 col-md-3 col-6">
<div class="border rounded p-2 h-100">
<div class="text-muted small">Decided</div>
<div class="fw-semibold"><?= esc((string) ($promotionSummary['with_decision'] ?? 0)) ?></div>
</div>
</div>
<div class="col-lg-2 col-md-3 col-6">
<div class="border rounded p-2 h-100">
<div class="text-muted small">Pass</div>
<div class="fw-semibold"><?= esc((string) ($promotionSummary['pass'] ?? 0)) ?></div>
</div>
</div>
<div class="col-lg-2 col-md-3 col-6">
<div class="border rounded p-2 h-100">
<div class="text-muted small">Pending</div>
<div class="fw-semibold text-danger"><?= esc((string) ($promotionSummary['pending_decision'] ?? 0)) ?></div>
</div>
</div>
<div class="col-lg-2 col-md-3 col-6">
<div class="border rounded p-2 h-100">
<div class="text-muted small">Other</div>
<div class="fw-semibold"><?= esc((string) ($promotionSummary['other_decision'] ?? 0)) ?></div>
</div>
</div>
</div>
<div class="table-responsive">
<table id="promotionPreviewTable" class="table table-sm align-middle no-mgmt-sticky" data-no-mgmt-sticky>
<thead>
<tr>
<th>Student</th>
<th>School ID</th>
<th>Class</th>
<th class="text-end">Age Sep 1</th>
<th class="text-end">Year Score</th>
<th>Decision</th>
<th>Source</th>
<th>Next Year Placement</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($promotionRows as $row): ?>
<?php
$rowStatus = (string) ($row['status'] ?? 'missing');
$statusClass = $rowStatus === 'decided' ? 'success' : 'danger';
$sourceLabel = (string) ($row['source'] ?? '');
if ($sourceLabel === 'automatic_kg_age') {
$sourceLabel = 'Auto KG age';
}
$targetLabel = trim((string) ($row['target_section'] ?? ''));
if ($targetLabel === '') {
$targetLabel = trim((string) ($row['target_class'] ?? ''));
}
if ($targetLabel === '') {
$targetLabel = $kgPlacementFallback($row);
}
$ageSepOne = $ageBySchoolYearSecondSeptember($row);
?>
<tr>
<td><?= esc($row['student_name'] ?? ('Student #' . (int) ($row['student_id'] ?? 0))) ?></td>
<td><?= esc($row['school_id'] ?? '') ?></td>
<td><?= esc($row['class_section_name'] ?? '') ?></td>
<td class="text-end" data-order="<?= esc($ageSepOne, 'attr') ?>"><?= esc($ageSepOne !== '' ? $ageSepOne : '-') ?></td>
<td class="text-end" data-order="<?= esc(is_numeric($row['year_score'] ?? null) ? (string) $row['year_score'] : '', 'attr') ?>"><?= esc($score($row['year_score'] ?? null)) ?></td>
<td><?= esc(($row['decision'] ?? '') !== '' ? $row['decision'] : '-') ?></td>
<td><?= esc($sourceLabel) ?></td>
<td data-order="<?= esc($targetLabel, 'attr') ?>"><?= esc($targetLabel !== '' ? $targetLabel : '-') ?></td>
<td data-order="<?= esc($rowStatus, 'attr') ?>"><span class="badge bg-<?= esc($statusClass) ?>"><?= esc(ucfirst($rowStatus)) ?></span></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<div class="border rounded bg-white p-3 mb-4" id="carry-forward-table">
<h5>Carry-Forward Families</h5>
<div class="table-responsive">
<table id="carryForwardFamiliesTable" class="table table-sm align-middle no-mgmt-sticky" data-no-mgmt-sticky>
<thead>
<tr>
<th>Family</th>
<th>Parent</th>
<th class="text-end">Source Balance</th>
<th class="text-end">Credits</th>
<th class="text-end">Adjustments</th>
<th class="text-end">Net Carry-Forward</th>
</tr>
</thead>
<tbody>
<?php foreach ($carryForward as $row): ?>
<tr>
<td><?= esc($row['family']) ?></td>
<td>
<div><?= esc($row['parent'] ?? ('Parent #' . (int) ($row['family_id'] ?? 0))) ?></div>
<?php if (! empty($row['parent_email'])): ?>
<div class="text-muted small"><?= esc($row['parent_email']) ?></div>
<?php endif; ?>
</td>
<td class="text-end" data-order="<?= esc((string) (float) ($row['source_balance'] ?? 0), 'attr') ?>"><?= esc($money($row['source_balance'])) ?></td>
<td class="text-end" data-order="<?= esc((string) (float) ($row['credit_amount'] ?? 0), 'attr') ?>"><?= esc($money($row['credit_amount'])) ?></td>
<td class="text-end" data-order="<?= esc((string) (float) ($row['adjustment_amount'] ?? 0), 'attr') ?>"><?= esc($money($row['adjustment_amount'])) ?></td>
<td class="text-end" data-order="<?= esc((string) (float) ($row['carry_forward_amount'] ?? 0), 'attr') ?>"><?= esc($money($row['carry_forward_amount'])) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<div class="d-flex flex-wrap gap-2 mb-4">
<?php if ($status === 'active' && $target && $blockers === []): ?>
<form action="<?= site_url('administrator/school-years/' . (int) $source['id'] . '/closing/start') ?>" method="post">
<?= csrf_field() ?>
<input type="hidden" name="target_school_year_id" value="<?= (int) $target['id'] ?>">
<button class="btn btn-warning" type="submit">Close Year</button>
</form>
<?php elseif ($status === 'active'): ?>
<button class="btn btn-warning" type="button" disabled>Close Year</button>
<span class="align-self-center text-muted small">
<?php if (! $target): ?>
Create or select the next school year first.
<?php elseif ($blockers !== []): ?>
Resolve the blocking issues above first.
<?php endif; ?>
</span>
<?php endif; ?>
<?php if ($status === 'closing' && $batchStatus === 'started'): ?>
<form action="<?= site_url('administrator/school-years/' . (int) $source['id'] . '/closing/execute') ?>" method="post">
<?= csrf_field() ?>
<button class="btn btn-primary" type="submit">Confirm Carry-Forward</button>
</form>
<?php endif; ?>
<?php if ($status === 'closing' && $batchStatus === 'executed'): ?>
<form action="<?= site_url('administrator/school-years/' . (int) $source['id'] . '/closing/complete') ?>" method="post">
<?= csrf_field() ?>
<button class="btn btn-success" type="submit">Close Year</button>
</form>
<?php endif; ?>
<?php if (in_array($batchStatus, ['executed', 'completed'], true)): ?>
<form action="<?= site_url('administrator/school-years/' . (int) $source['id'] . '/closing/execute') ?>" method="post">
<?= csrf_field() ?>
<button class="btn btn-primary" type="submit">
<?= $missingCarryForwardInvoices > 0
? 'Repair Carry-Forward Invoices (' . $missingCarryForwardInvoices . ')'
: 'Re-run Carry-Forward Sync' ?>
</button>
</form>
<?php endif; ?>
<?php if ($status === 'closing' && ! in_array($batchStatus, ['executed', 'completed'], true)): ?>
<form action="<?= site_url('administrator/school-years/' . (int) $source['id'] . '/closing/cancel') ?>" method="post">
<?= csrf_field() ?>
<button class="btn btn-outline-secondary" type="submit">Cancel End-Year Process</button>
</form>
<?php endif; ?>
</div>
</div>
<?= $this->endSection() ?>
<?= $this->section('scripts') ?>
<script>
document.addEventListener('DOMContentLoaded', function () {
if (!window.jQuery || !window.jQuery.fn || !window.jQuery.fn.DataTable) {
return;
}
try {
Object.keys(window.localStorage || {}).forEach(function (key) {
if (key.indexOf('DataTables_promotionPreviewTable_') === 0 || key.indexOf('DataTables_carryForwardFamiliesTable_') === 0) {
window.localStorage.removeItem(key);
}
});
} catch (error) {
// Ignore storage restrictions; DataTables can still initialize without saved state.
}
var baseOptions = {
paging: true,
pageLength: 25,
lengthMenu: [[25, 50, 100, 200, -1], [25, 50, 100, 200, 'All']],
pagingType: 'full_numbers',
stateSave: false,
autoWidth: false,
dom: "<'row mb-2'<'col-sm-12 col-md-6'l><'col-sm-12 col-md-6'f>>" +
"t" +
"<'row mt-2'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
language: {
lengthMenu: 'Show _MENU_ entries',
paginate: {
first: 'First',
previous: 'Previous',
next: 'Next',
last: 'Last'
}
}
};
window.jQuery('#promotionPreviewTable').DataTable(Object.assign({}, baseOptions, {
order: [[2, 'asc'], [0, 'asc']],
language: {
lengthMenu: 'Show _MENU_ entries',
emptyTable: 'No active class assignments found for this school year.'
}
}));
window.jQuery('#carryForwardFamiliesTable').DataTable(Object.assign({}, baseOptions, {
order: [[0, 'asc']],
language: {
lengthMenu: 'Show _MENU_ entries',
emptyTable: 'No carry-forward balances found.'
}
}));
});
</script>
<?= $this->endSection() ?>