fix financial issues

This commit is contained in:
root
2026-06-01 02:08:27 -04:00
parent 090cb88573
commit 6444b61416
56 changed files with 4196 additions and 1824 deletions
+35 -9
View File
@@ -1849,15 +1849,42 @@ private function calculateTermRanking(
return null;
}
/*
* Rank must be limited to the actual selected class roster.
* Do NOT rank by every semester_scores row that happens to share class_section_id,
* because old/mismatched score rows can inflate the denominator (for example 18 students
* in 3-A showing as "out of 32"). The roster is the source of truth for class size.
*/
$rosterRows = $this->fetchStudentsByClass($sectionCode, $schoolYear);
if (empty($rosterRows) && $sectionId && $sectionId !== $sectionCode) {
$rosterRows = $this->fetchStudentsByClass($sectionId, $schoolYear);
}
$rosterByStudent = [];
foreach ($rosterRows as $row) {
$sid = (int)($row['id'] ?? 0);
if ($sid <= 0) {
continue;
}
$rosterByStudent[$sid] = [
'firstname' => trim((string)($row['firstname'] ?? '')),
'lastname' => trim((string)($row['lastname'] ?? '')),
];
}
if (empty($rosterByStudent) || !isset($rosterByStudent[$studentId])) {
return null;
}
$rosterStudentIds = array_keys($rosterByStudent);
$semesterForRank = trim((string)$semester);
$rankByFinalScore = $this->normalizeSemester($semesterForRank) === 'spring';
$builder = $this->db->table('semester_scores ss')
->select('ss.student_id, ss.semester, ss.semester_score, ss.updated_at, ss.id, s.firstname, s.lastname')
->join('students s', 's.id = ss.student_id', 'inner')
->where('s.is_active', 1)
->select('ss.student_id, ss.semester, ss.semester_score, ss.updated_at, ss.id')
->where('ss.school_year', $schoolYear)
->whereIn('ss.class_section_id', $sectionIds)
->whereIn('ss.student_id', $rosterStudentIds)
->orderBy('ss.updated_at', 'DESC')
->orderBy('ss.id', 'DESC');
@@ -1876,7 +1903,7 @@ private function calculateTermRanking(
foreach ($rows as $row) {
$sid = (int)($row['student_id'] ?? 0);
if ($sid <= 0 || isset($scoresByStudent[$sid])) {
if ($sid <= 0 || !isset($rosterByStudent[$sid]) || isset($scoresByStudent[$sid])) {
continue;
}
@@ -1892,8 +1919,8 @@ private function calculateTermRanking(
'student_id' => $sid,
'score' => $rawScore,
'rank_score' => round($rawScore, 1),
'firstname' => trim((string)($row['firstname'] ?? '')),
'lastname' => trim((string)($row['lastname'] ?? '')),
'firstname' => $rosterByStudent[$sid]['firstname'],
'lastname' => $rosterByStudent[$sid]['lastname'],
];
}
@@ -1910,7 +1937,6 @@ private function calculateTermRanking(
->select('ss.student_id, ss.semester, ss.semester_score, ss.updated_at, ss.id')
->where('ss.school_year', $schoolYear)
->whereIn('ss.student_id', $studentIds)
->whereIn('ss.class_section_id', $sectionIds)
->orderBy('ss.updated_at', 'DESC')
->orderBy('ss.id', 'DESC');
@@ -1925,7 +1951,7 @@ private function calculateTermRanking(
foreach ($firstRows as $row) {
$sid = (int)($row['student_id'] ?? 0);
if ($sid <= 0 || isset($firstScoresByStudent[$sid])) {
if ($sid <= 0 || !isset($rosterByStudent[$sid]) || isset($firstScoresByStudent[$sid])) {
continue;
}