ADD SCHOOL YEAR MANAGEMENT
Tests / PHPUnit (push) Failing after 40s

This commit is contained in:
root
2026-07-12 02:21:39 -04:00
parent c7f67da9bf
commit e06ccc9cc0
36 changed files with 6722 additions and 327 deletions
+208
View File
@@ -0,0 +1,208 @@
<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<?php
$source = $preview['source'] ?? [];
$target = $preview['target'] ?? null;
$overview = $preview['overview'] ?? [];
$finance = $preview['finance'] ?? [];
$blockers = $preview['blockers'] ?? [];
$warnings = $preview['warnings'] ?? [];
$carryForward = $preview['carry_forward'] ?? [];
$status = (string) ($source['status'] ?? '');
$batchStatus = (string) ($latestBatch['status'] ?? '');
$money = static fn ($value): string => '$' . number_format((float) $value, 2);
?>
<?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">Closing Preview: <?= esc($source['name'] ?? '') ?></h2>
<div class="text-muted">
Status: <span class="fw-semibold"><?= esc(ucfirst($status)) ?></span>
<?php if ($target): ?>
<span class="mx-2">Target: <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 Management</a>
</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">Target 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 Preview</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>Closing Progress</h5>
<ol class="mb-0">
<li>Preview generated</li>
<li class="<?= $blockers === [] ? '' : 'text-muted' ?>">Validation <?= $blockers === [] ? 'passed' : 'has blockers' ?></li>
<li class="<?= $batchStatus !== '' ? '' : 'text-muted' ?>">Closing batch <?= $batchStatus !== '' ? esc($batchStatus) : 'not created' ?></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">
<h5>Carry-Forward Families</h5>
<div class="table-responsive">
<table class="table table-sm align-middle">
<thead>
<tr>
<th>Family</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 if ($carryForward === []): ?>
<tr><td colspan="5" class="text-muted">No carry-forward balances found.</td></tr>
<?php endif; ?>
<?php foreach ($carryForward as $row): ?>
<tr>
<td><?= esc($row['family']) ?></td>
<td class="text-end"><?= esc($money($row['source_balance'])) ?></td>
<td class="text-end"><?= esc($money($row['credit_amount'])) ?></td>
<td class="text-end"><?= esc($money($row['adjustment_amount'])) ?></td>
<td class="text-end"><?= 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">Begin Closing</button>
</form>
<?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">Execute 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">Complete Closing</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 Closing</button>
</form>
<?php endif; ?>
</div>
</div>
<?= $this->endSection() ?>
+336
View File
@@ -0,0 +1,336 @@
<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<?php
$statusClasses = [
'draft' => 'bg-info text-dark',
'active' => 'bg-success',
'closing' => 'bg-warning text-dark',
'closed' => 'bg-secondary',
'archived' => 'bg-dark',
];
$formatDate = static fn ($value): string => $value ? esc((string) $value) : 'Not set';
?>
<?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">School Year Management</h2>
<div class="text-muted">
Active year:
<?php if (! empty($activeYear)): ?>
<span class="badge bg-success"><?= esc($activeYear['name']) ?></span>
<?php else: ?>
<span class="badge bg-danger">None</span>
<?php endif; ?>
</div>
</div>
<button class="btn btn-success" type="button" data-bs-toggle="collapse" data-bs-target="#schoolYearCreateForm">
Add School Year
</button>
</div>
<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">Active Year</div>
<div class="fs-5 fw-semibold"><?= esc($activeYear['name'] ?? 'None') ?></div>
</div>
</div>
<div class="col-md-3">
<div class="border rounded bg-white p-3 h-100">
<div class="text-muted small">Next Draft</div>
<div class="fs-5 fw-semibold"><?= esc($nextDraftYear['name'] ?? 'None') ?></div>
</div>
</div>
<div class="col-md-3">
<div class="border rounded bg-white p-3 h-100">
<div class="text-muted small">Currently Closing</div>
<div class="fs-5 fw-semibold"><?= esc($closingYear['name'] ?? 'None') ?></div>
</div>
</div>
<div class="col-md-3">
<div class="border rounded bg-white p-3 h-100">
<div class="text-muted small">Archived Years</div>
<div class="fs-5 fw-semibold"><?= esc((string) ($archivedCount ?? 0)) ?></div>
</div>
</div>
</div>
<div class="collapse mb-4" id="schoolYearCreateForm">
<div class="border rounded bg-white p-3">
<form action="<?= site_url('administrator/school-years/store') ?>" method="post" class="row g-3 align-items-end">
<?= csrf_field() ?>
<div class="col-md-2">
<label class="form-label" for="new_school_year_name">School Year</label>
<input class="form-control" id="new_school_year_name" name="name" placeholder="2026-2027" required pattern="\d{4}-\d{4}">
</div>
<div class="col-md-2">
<label class="form-label" for="new_school_year_starts_on">Starts On</label>
<input class="form-control" id="new_school_year_starts_on" name="starts_on" type="date">
</div>
<div class="col-md-2">
<label class="form-label" for="new_school_year_ends_on">Ends On</label>
<input class="form-control" id="new_school_year_ends_on" name="ends_on" type="date">
</div>
<div class="col-md-2">
<label class="form-label" for="new_registration_starts_on">Registration Starts</label>
<input class="form-control" id="new_registration_starts_on" name="registration_starts_on" type="date">
</div>
<div class="col-md-2">
<label class="form-label" for="new_registration_ends_on">Registration Ends</label>
<input class="form-control" id="new_registration_ends_on" name="registration_ends_on" type="date">
</div>
<div class="col-md-2 d-flex gap-2">
<button class="btn btn-primary" type="submit">Save Draft</button>
<button class="btn btn-secondary" type="button" data-bs-toggle="collapse" data-bs-target="#schoolYearCreateForm">Cancel</button>
</div>
<div class="col-12">
<label class="form-label" for="new_school_year_description">Description</label>
<textarea class="form-control" id="new_school_year_description" name="description" rows="2"></textarea>
</div>
</form>
</div>
</div>
<div class="table-responsive">
<table id="schoolYearsTable" class="table table-striped table-hover align-middle">
<thead>
<tr>
<th>School Year</th>
<th>Dates</th>
<th>Status</th>
<th>Students</th>
<th>Families</th>
<th>Financial Balance</th>
<th>Last Transition</th>
<th>Updated</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach (($schoolYears ?? []) as $year): ?>
<?php
$id = (int) ($year['id'] ?? 0);
$status = (string) ($year['status'] ?? 'draft');
$readonly = in_array($status, ['closed', 'archived'], true);
$transition = $latestTransitions[$id] ?? null;
?>
<tr>
<td class="fw-semibold"><?= esc($year['name'] ?? '') ?></td>
<td>
<div><?= $formatDate($year['starts_on'] ?? null) ?></div>
<div class="text-muted small"><?= $formatDate($year['ends_on'] ?? null) ?></div>
</td>
<td>
<span class="badge <?= esc($statusClasses[$status] ?? 'bg-secondary') ?>">
<?= esc(ucfirst($status)) ?>
</span>
</td>
<td class="text-muted">-</td>
<td class="text-muted">-</td>
<td class="text-muted">Preview</td>
<td>
<?php if ($transition): ?>
<div><?= esc($transition['action'] ?? '') ?></div>
<div class="text-muted small"><?= esc($transition['created_at'] ?? '') ?></div>
<?php else: ?>
<span class="text-muted">-</span>
<?php endif; ?>
</td>
<td><?= esc($year['updated_at'] ?? '') ?></td>
<td>
<div class="dropdown">
<button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
Actions
</button>
<ul class="dropdown-menu dropdown-menu-end">
<?php if (! $readonly && $status !== 'closing'): ?>
<li>
<button class="dropdown-item" type="button" data-bs-toggle="modal" data-bs-target="#editSchoolYearModal<?= $id ?>">
Edit metadata
</button>
</li>
<?php endif; ?>
<?php if ($status === 'draft'): ?>
<li>
<button class="dropdown-item" type="button" data-bs-toggle="modal" data-bs-target="#activateSchoolYearModal<?= $id ?>">
Activate
</button>
</li>
<li><hr class="dropdown-divider"></li>
<li>
<form action="<?= site_url('administrator/school-years/' . $id . '/delete-draft') ?>" method="post" onsubmit="return confirm('Delete this unused draft school year?');">
<?= csrf_field() ?>
<button class="dropdown-item text-danger" type="submit">Delete draft</button>
</form>
</li>
<?php elseif ($status === 'active'): ?>
<li>
<a class="dropdown-item" href="<?= site_url('administrator/school-years/' . $id . '/closing/preview') ?>">Preview closing</a>
</li>
<?php elseif ($status === 'closing'): ?>
<li>
<a class="dropdown-item" href="<?= site_url('administrator/school-years/' . $id . '/closing/preview') ?>">View closing</a>
</li>
<li>
<form action="<?= site_url('administrator/school-years/' . $id . '/closing/cancel') ?>" method="post" onsubmit="return confirm('Cancel closing for this school year?');">
<?= csrf_field() ?>
<button class="dropdown-item" type="submit">Cancel closing</button>
</form>
</li>
<?php elseif ($status === 'closed'): ?>
<li>
<a class="dropdown-item" href="<?= site_url('administrator/school-years/' . $id . '/closing/preview') ?>">View closing report</a>
</li>
<li>
<button class="dropdown-item" type="button" data-bs-toggle="modal" data-bs-target="#reopenSchoolYearModal<?= $id ?>">
Reopen
</button>
</li>
<li>
<form action="<?= site_url('administrator/school-years/' . $id . '/archive') ?>" method="post" onsubmit="return confirm('Archive this closed school year?');">
<?= csrf_field() ?>
<button class="dropdown-item" type="submit">Archive</button>
</form>
</li>
<?php else: ?>
<li>
<a class="dropdown-item" href="<?= site_url('administrator/school-years/' . $id . '/closing/preview') ?>">View audit trail</a>
</li>
<?php endif; ?>
</ul>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php foreach (($schoolYears ?? []) as $year): ?>
<?php
$id = (int) ($year['id'] ?? 0);
$status = (string) ($year['status'] ?? 'draft');
$readonly = in_array($status, ['closed', 'archived', 'closing'], true);
?>
<?php if (! $readonly): ?>
<div class="modal fade" id="editSchoolYearModal<?= $id ?>" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<form class="modal-content" action="<?= site_url('administrator/school-years/' . $id . '/update') ?>" method="post">
<?= csrf_field() ?>
<div class="modal-header">
<h5 class="modal-title">Edit School Year Metadata</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-4 mb-3">
<label class="form-label" for="school_year_name_<?= $id ?>">School Year</label>
<input class="form-control" id="school_year_name_<?= $id ?>" name="name" value="<?= esc($year['name'] ?? '') ?>" required pattern="\d{4}-\d{4}">
</div>
<div class="col-md-4 mb-3">
<label class="form-label" for="school_year_starts_on_<?= $id ?>">Starts On</label>
<input class="form-control" id="school_year_starts_on_<?= $id ?>" name="starts_on" type="date" value="<?= esc($year['starts_on'] ?? '') ?>">
</div>
<div class="col-md-4 mb-3">
<label class="form-label" for="school_year_ends_on_<?= $id ?>">Ends On</label>
<input class="form-control" id="school_year_ends_on_<?= $id ?>" name="ends_on" type="date" value="<?= esc($year['ends_on'] ?? '') ?>">
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label" for="registration_starts_on_<?= $id ?>">Registration Starts</label>
<input class="form-control" id="registration_starts_on_<?= $id ?>" name="registration_starts_on" type="date" value="<?= esc($year['registration_starts_on'] ?? '') ?>">
</div>
<div class="col-md-6 mb-3">
<label class="form-label" for="registration_ends_on_<?= $id ?>">Registration Ends</label>
<input class="form-control" id="registration_ends_on_<?= $id ?>" name="registration_ends_on" type="date" value="<?= esc($year['registration_ends_on'] ?? '') ?>">
</div>
</div>
<label class="form-label" for="description_<?= $id ?>">Description</label>
<textarea class="form-control" id="description_<?= $id ?>" name="description" rows="3"><?= esc($year['description'] ?? '') ?></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save Changes</button>
</div>
</form>
</div>
</div>
<?php endif; ?>
<?php if ($status === 'draft'): ?>
<div class="modal fade" id="activateSchoolYearModal<?= $id ?>" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<form class="modal-content" action="<?= site_url('administrator/school-years/' . $id . '/activate') ?>" method="post">
<?= csrf_field() ?>
<div class="modal-header">
<h5 class="modal-title">Activate <?= esc($year['name'] ?? '') ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p class="mb-2">The selected school year will become active.</p>
<?php if (! empty($activeYear)): ?>
<p class="mb-3">Current active year <strong><?= esc($activeYear['name']) ?></strong> will move to <strong>closing</strong>.</p>
<?php endif; ?>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="1" name="confirm_activation" id="confirm_activation_<?= $id ?>" required>
<label class="form-check-label" for="confirm_activation_<?= $id ?>">
I understand activation does not complete closing for the previous year.
</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success">Activate</button>
</div>
</form>
</div>
</div>
<?php endif; ?>
<?php if ($status === 'closed'): ?>
<div class="modal fade" id="reopenSchoolYearModal<?= $id ?>" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<form class="modal-content" action="<?= site_url('administrator/school-years/' . $id . '/reopen') ?>" method="post">
<?= csrf_field() ?>
<div class="modal-header">
<h5 class="modal-title">Reopen <?= esc($year['name'] ?? '') ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<label class="form-label" for="reopen_reason_<?= $id ?>">Reason</label>
<textarea class="form-control" id="reopen_reason_<?= $id ?>" name="reason" rows="3" required></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-warning">Reopen</button>
</div>
</form>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?= $this->endSection() ?>
<?= $this->section('scripts') ?>
<script>
document.addEventListener('DOMContentLoaded', function () {
if (window.jQuery && window.jQuery.fn && window.jQuery.fn.DataTable) {
window.jQuery('#schoolYearsTable').DataTable({
pageLength: 25,
order: [[0, 'desc']]
});
}
});
</script>
<?= $this->endSection() ?>