apply the school year concept
Tests / PHPUnit (push) Failing after 1m19s

This commit is contained in:
root
2026-07-14 00:59:00 -04:00
parent 504c3bc9f9
commit feb1b29a32
73 changed files with 4288 additions and 620 deletions
+61 -39
View File
@@ -20,6 +20,60 @@ class FinancialController extends BaseController
$accept = strtolower((string)($this->request->getHeaderLine('Accept') ?? ''));
return $this->request->isAJAX() || str_contains($accept, 'application/json');
}
private function selectedSchoolYear(): string
{
$requested = trim((string) ($this->request->getGet('school_year') ?? ''));
if ($requested !== '') {
return $requested;
}
return $this->currentSchoolYearName();
}
private function financialSchoolYearOptions(?string $selectedYear = null): array
{
$schoolYears = [];
$addYear = static function (mixed $value) use (&$schoolYears): void {
$year = trim((string) $value);
if ($year !== '' && ! in_array($year, $schoolYears, true)) {
$schoolYears[] = $year;
}
};
$addYear($selectedYear);
$addYear($this->currentSchoolYearName());
try {
$db = \Config\Database::connect();
if ($db->tableExists('school_years')) {
$rows = $db->table('school_years')
->select('name')
->orderBy('name', 'DESC')
->get()
->getResultArray();
foreach ($rows as $row) {
$addYear($row['name'] ?? '');
}
}
if ($db->tableExists('invoices') && $db->fieldExists('school_year', 'invoices')) {
$rows = $db->table('invoices')
->select('DISTINCT school_year', false)
->where('school_year IS NOT NULL', null, false)
->orderBy('school_year', 'DESC')
->get()
->getResultArray();
foreach ($rows as $row) {
$addYear($row['school_year'] ?? '');
}
}
} catch (\Throwable $e) {
}
return $schoolYears;
}
public function financialReport()
{
@@ -32,7 +86,7 @@ public function financialReport()
$dateFrom = $this->request->getGet('date_from');
$dateTo = $this->request->getGet('date_to');
$schoolYear = $this->request->getGet('school_year');
$schoolYear = $this->selectedSchoolYear();
// === Apply filters to models (invoice/refund/discount/expense/reimb) ===
if ($schoolYear) {
@@ -235,22 +289,7 @@ public function financialReport()
->findAll();
// --- School year options (for detailed view & JSON) ---
$schoolYears = [];
try {
$db = \Config\Database::connect();
$rows = $db->table('invoices')
->select('DISTINCT school_year', false)
->where('school_year IS NOT NULL', null, false)
->orderBy('school_year', 'DESC')
->get()->getResultArray();
foreach ($rows as $r) {
$val = (string)($r['school_year'] ?? '');
if ($val !== '' && !in_array($val, $schoolYears, true)) $schoolYears[] = $val;
}
} catch (\Throwable $e) {}
if (empty($schoolYears) && !empty($schoolYear)) {
$schoolYears[] = (string)$schoolYear;
}
$schoolYears = $this->financialSchoolYearOptions($schoolYear);
$eventFeesTotal = $this->getEventFeesTotal($schoolYear, $dateFrom, $dateTo);
@@ -298,28 +337,11 @@ public function financialReport()
{
$dateFrom = $this->request->getGet('date_from');
$dateTo = $this->request->getGet('date_to');
$schoolYear = $this->request->getGet('school_year');
$schoolYear = $this->selectedSchoolYear();
$data = $this->getFinancialSummary($dateFrom, $dateTo, $schoolYear);
// Build school year options from invoices table (fallback to configured year)
$schoolYears = [];
try {
$db = \Config\Database::connect();
$rows = $db->table('invoices')
->select('DISTINCT school_year', false)
->where('school_year IS NOT NULL', null, false)
->orderBy('school_year', 'DESC')
->get()->getResultArray();
foreach ($rows as $r) {
$val = (string)($r['school_year'] ?? '');
if ($val !== '' && !in_array($val, $schoolYears, true)) $schoolYears[] = $val;
}
} catch (\Throwable $e) {}
if (empty($schoolYears) && !empty($data['schoolYear'])) {
$schoolYears[] = (string)$data['schoolYear'];
}
$data['schoolYears'] = $schoolYears;
$data['schoolYears'] = $this->financialSchoolYearOptions((string)($data['schoolYear'] ?? $schoolYear));
if ($this->wantsJson() || strtolower((string)($this->request->getGet('format') ?? '')) === 'json') {
return $this->response->setJSON(['ok' => true] + $data + [
'csrf_token' => csrf_token(),
@@ -524,7 +546,7 @@ public function financialReport()
private function getFinancialSummaryDetailSections(?string $dateFrom = null, ?string $dateTo = null, ?string $schoolYear = null): array
{
$configModel = new \App\Models\ConfigurationModel();
$schoolYear = $schoolYear ?: $configModel->getConfig('school_year');
$schoolYear = $schoolYear ?: $this->currentSchoolYearName();
$derivedDateFrom = null;
$derivedDateTo = null;
@@ -1612,7 +1634,7 @@ public function financialReport()
$additionalModel = new \App\Models\AdditionalChargeModel();
// Allow override via parameter; fallback to configured year
$schoolYear = $schoolYear ?: $configModel->getConfig('school_year');
$schoolYear = $schoolYear ?: $this->currentSchoolYearName();
$derivedDateFrom = null;
$derivedDateTo = null;
if (!empty($schoolYear) && empty($dateFrom) && empty($dateTo)) {
@@ -2068,7 +2090,7 @@ public function financialReport()
$configModel = new \App\Models\ConfigurationModel();
$schoolYear = trim((string)($this->request->getGet('school_year') ?? ''));
if ($schoolYear === '') {
$schoolYear = (string) ($configModel->getConfig('school_year') ?? date('Y'));
$schoolYear = $this->currentSchoolYearName((string) ($configModel->getConfig('school_year') ?? date('Y')));
}
// Build school year options from invoices