fix tests

This commit is contained in:
root
2026-06-11 11:46:12 -04:00
parent c91fa2ce4d
commit 5ead80fdc7
1489 changed files with 11349 additions and 10305 deletions
@@ -2,13 +2,14 @@
namespace App\Services\Attendance;
use App\Controllers\View\AttendanceController;
use App\Models\AttendanceDay;
use App\Models\ClassSection;
use App\Models\Configuration;
use App\Models\StaffAttendance;
use App\Models\TeacherClass;
use Carbon\Carbon;
use Illuminate\Contracts\Auth\Authenticatable;
use App\Models\ClassSection;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -23,7 +24,7 @@ class StaffAttendanceService
) {}
/**
* legacy {@see \App\Controllers\View\AttendanceController::index} — single date / section teacher grid JSON.
* legacy {@see AttendanceController::index} — single date / section teacher grid JSON.
*/
public function teacherAttendanceDayIndex(?string $semester, ?string $schoolYear, string $dateYmd, int $sectionCode): array
{
@@ -44,7 +45,7 @@ class StaffAttendanceService
->where('class_section_id', $code)
->value('class_section_name');
$name = trim((string) $name);
$labels[$code] = $name !== '' ? $name : ('Section #' . $code);
$labels[$code] = $name !== '' ? $name : ('Section #'.$code);
}
$grid = [];
@@ -76,7 +77,7 @@ class StaffAttendanceService
$tid = (int) ($t['teacher_id'] ?? 0);
$posRaw = strtolower((string) ($t['position'] ?? 'main'));
$pos = in_array($posRaw, ['main', 'ta'], true) ? $posRaw : 'main';
$name = trim(($t['firstname'] ?? '') . ' ' . ($t['lastname'] ?? '')) ?: ('User#' . $tid);
$name = trim(($t['firstname'] ?? '').' '.($t['lastname'] ?? '')) ?: ('User#'.$tid);
$cell = $statusMap[$tid] ?? ['status' => null, 'reason' => null];
@@ -108,7 +109,7 @@ class StaffAttendanceService
}
/**
* Bulk save for legacy {@see \App\Controllers\View\AttendanceController::save} (method missing in legacy; implemented here).
* Bulk save for legacy {@see AttendanceController::save} (method missing in legacy; implemented here).
*/
public function saveTeacherAttendanceBulk(Authenticatable $user, array $payload): array
{
@@ -181,7 +182,7 @@ class StaffAttendanceService
public function monthData(?string $semester, ?string $schoolYear): array
{
$semester = $semester ?: (string)$this->configuration->getConfig('semester');
$semester = $semester ?: (string) $this->configuration->getConfig('semester');
[
'school_year' => $schoolYear,
'current_year' => $currentYear,
@@ -232,7 +233,7 @@ class StaffAttendanceService
$sectionCodes = array_keys($assignedBySection);
$labels = [];
if (!empty($sectionCodes)) {
if (! empty($sectionCodes)) {
$rows = $this->classSection->query()
->select(['class_section_id', 'class_section_name'])
->whereIn('class_section_id', $sectionCodes)
@@ -240,20 +241,20 @@ class StaffAttendanceService
->toArray();
foreach ($rows as $row) {
$labels[(int)$row['class_section_id']] = trim((string)$row['class_section_name']) ?: 'Section #' . (int)$row['class_section_id'];
$labels[(int) $row['class_section_id']] = trim((string) $row['class_section_name']) ?: 'Section #'.(int) $row['class_section_id'];
}
}
$teacherIds = [];
foreach ($assignedBySection as $rows) {
foreach ($rows as $row) {
$teacherIds[(int)$row['teacher_id']] = true;
$teacherIds[(int) $row['teacher_id']] = true;
}
}
$teacherIds = array_keys($teacherIds);
$statusByTeacher = [];
if (!empty($teacherIds) && !empty($days)) {
if (! empty($teacherIds) && ! empty($days)) {
$query = DB::table('staff_attendance')
->select('user_id', 'date', 'status', 'reason')
->where('school_year', $schoolYear)
@@ -267,8 +268,8 @@ class StaffAttendanceService
$rows = $query->get();
foreach ($rows as $row) {
$statusByTeacher[(int)$row->user_id][(string)$row->date] = [
'status' => strtolower((string)$row->status),
$statusByTeacher[(int) $row->user_id][(string) $row->date] = [
'status' => strtolower((string) $row->status),
'reason' => $row->reason,
];
}
@@ -284,7 +285,7 @@ class StaffAttendanceService
$noSchoolDays = [];
foreach ($noSchoolRows as $row) {
$noSchoolDays[] = (string)$row->date;
$noSchoolDays[] = (string) $row->date;
}
$sections = [];
@@ -292,9 +293,9 @@ class StaffAttendanceService
$teachersPayload = [];
foreach (($assignedBySection[$code] ?? []) as $row) {
$teacherId = (int)$row['teacher_id'];
$name = trim(($row['firstname'] ?? '') . ' ' . ($row['lastname'] ?? '')) ?: ('User#' . $teacherId);
$position = strtolower((string)($row['position'] ?? 'main'));
$teacherId = (int) $row['teacher_id'];
$name = trim(($row['firstname'] ?? '').' '.($row['lastname'] ?? '')) ?: ('User#'.$teacherId);
$position = strtolower((string) ($row['position'] ?? 'main'));
$position = in_array($position, ['main', 'ta'], true) ? $position : 'main';
$cells = [];
@@ -302,7 +303,7 @@ class StaffAttendanceService
foreach ($days as $date) {
$cell = $statusByTeacher[$teacherId][$date] ?? null;
if (!$cell || empty($cell['status'])) {
if (! $cell || empty($cell['status'])) {
continue;
}
@@ -327,15 +328,15 @@ class StaffAttendanceService
}
$sections[] = [
'section_code' => (int)$code,
'label' => $labels[$code] ?? ('Section #' . (int)$code),
'section_code' => (int) $code,
'label' => $labels[$code] ?? ('Section #'.(int) $code),
'teachers' => $teachersPayload,
];
}
$adminStatusByUser = [];
$adminIds = [];
if (!empty($days)) {
if (! empty($days)) {
$query = DB::table('staff_attendance')
->select('user_id', 'date', 'status', 'reason')
->where('school_year', $schoolYear)
@@ -361,7 +362,7 @@ class StaffAttendanceService
$adminIds = array_keys($adminIds);
$admins = [];
if (!empty($adminIds)) {
if (! empty($adminIds)) {
$rolesRows = DB::table('user_roles as ur')
->select('ur.user_id', 'r.name as role_name', 'r.slug as role_slug', 'r.priority')
->join('roles as r', 'r.id', '=', 'ur.role_id')
@@ -389,20 +390,20 @@ class StaffAttendanceService
$namesByUser = [];
foreach ($userRows as $row) {
$namesByUser[(int) $row->id] = trim(((string) ($row->firstname ?? '')) . ' ' . ((string) ($row->lastname ?? '')));
$namesByUser[(int) $row->id] = trim(((string) ($row->firstname ?? '')).' '.((string) ($row->lastname ?? '')));
}
foreach ($adminIds as $uid) {
$picked = null;
foreach (($rolesByUser[$uid] ?? []) as $role) {
$slug = strtolower((string) ($role['role_slug'] ?? $role['role_name'] ?? ''));
if (!in_array($slug, $excludeSlugs, true)) {
if (! in_array($slug, $excludeSlugs, true)) {
$picked = $role;
break;
}
}
if (!$picked) {
if (! $picked) {
continue;
}
@@ -410,7 +411,7 @@ class StaffAttendanceService
$cells = [];
foreach ($days as $date) {
$cell = $adminStatusByUser[$uid][$date] ?? null;
if (!$cell || empty($cell['status'])) {
if (! $cell || empty($cell['status'])) {
continue;
}
$cells[$date] = $cell;
@@ -425,7 +426,7 @@ class StaffAttendanceService
$admins[] = [
'user_id' => $uid,
'name' => $namesByUser[$uid] !== '' ? $namesByUser[$uid] : ('User#' . $uid),
'name' => $namesByUser[$uid] !== '' ? $namesByUser[$uid] : ('User#'.$uid),
'role' => (string) ($picked['role_name'] ?? 'Admin'),
'cells' => $cells,
'totals' => $totals,
@@ -508,7 +509,7 @@ class StaffAttendanceService
}
}
if ($currentYear !== '' && !in_array($currentYear, $schoolYears, true)) {
if ($currentYear !== '' && ! in_array($currentYear, $schoolYears, true)) {
array_unshift($schoolYears, $currentYear);
}
@@ -523,7 +524,7 @@ class StaffAttendanceService
? $requestedSchoolYear
: ($currentYear !== '' ? $currentYear : ($schoolYears[0] ?? ''));
if ($resolvedYear !== '' && !in_array($resolvedYear, $schoolYears, true)) {
if ($resolvedYear !== '' && ! in_array($resolvedYear, $schoolYears, true)) {
array_unshift($schoolYears, $resolvedYear);
}
@@ -539,7 +540,7 @@ class StaffAttendanceService
protected function buildRangeLabel(string $schoolYear, string $semesterNorm, string $rangeStart, string $rangeEnd): string
{
if ($semesterNorm === '') {
return 'School Year ' . $schoolYear;
return 'School Year '.$schoolYear;
}
return sprintf(
@@ -553,14 +554,14 @@ class StaffAttendanceService
public function adminsAttendanceData(?string $date, ?string $semester, ?string $schoolYear): array
{
$date = $date ?: now()->toDateString();
$semester = $semester ?: (string)$this->configuration->getConfig('semester');
$schoolYear = $schoolYear ?: (string)$this->configuration->getConfig('school_year');
$semester = $semester ?: (string) $this->configuration->getConfig('semester');
$schoolYear = $schoolYear ?: (string) $this->configuration->getConfig('school_year');
$allStaff = $this->staffAttendance->staffWithStatusForDay($date, $semester, $schoolYear, []);
$userIds = [];
foreach ($allStaff as $row) {
$uid = (int)($row['user_id'] ?? 0);
$uid = (int) ($row['user_id'] ?? 0);
if ($uid > 0) {
$userIds[$uid] = true;
}
@@ -577,19 +578,19 @@ class StaffAttendanceService
$rolesByUser = [];
foreach ($rolesRows as $row) {
$rolesByUser[(int)$row->user_id][] = (array)$row;
$rolesByUser[(int) $row->user_id][] = (array) $row;
}
foreach ($rolesByUser as &$list) {
usort($list, fn($a, $b) => ((int)($a['priority'] ?? 100)) <=> ((int)($b['priority'] ?? 100)));
usort($list, fn ($a, $b) => ((int) ($a['priority'] ?? 100)) <=> ((int) ($b['priority'] ?? 100)));
}
$excludeSlugs = ['parent', 'guest', 'teacher', 'teacher_assistant', 'ta', 'assistant_teacher'];
$staffByUser = [];
foreach ($allStaff as $row) {
$uid = (int)($row['user_id'] ?? 0);
if ($uid > 0 && !isset($staffByUser[$uid])) {
$uid = (int) ($row['user_id'] ?? 0);
if ($uid > 0 && ! isset($staffByUser[$uid])) {
$staffByUser[$uid] = $row;
}
}
@@ -600,14 +601,14 @@ class StaffAttendanceService
$picked = null;
foreach ($roles as $role) {
$slug = strtolower((string)($role['role_slug'] ?? $role['role_name'] ?? ''));
if (!in_array($slug, $excludeSlugs, true)) {
$slug = strtolower((string) ($role['role_slug'] ?? $role['role_name'] ?? ''));
if (! in_array($slug, $excludeSlugs, true)) {
$picked = $role;
break;
}
}
if (!$picked) {
if (! $picked) {
continue;
}
@@ -618,8 +619,9 @@ class StaffAttendanceService
}
usort($adminsGrid, function ($a, $b) {
$an = trim(($a['firstname'] ?? '') . ' ' . ($a['lastname'] ?? ''));
$bn = trim(($b['firstname'] ?? '') . ' ' . ($b['lastname'] ?? ''));
$an = trim(($a['firstname'] ?? '').' '.($a['lastname'] ?? ''));
$bn = trim(($b['firstname'] ?? '').' '.($b['lastname'] ?? ''));
return strcasecmp($an, $bn);
});
@@ -633,20 +635,20 @@ class StaffAttendanceService
public function saveAdmins(Authenticatable $user, array $data): array
{
$date = (string)$data['date'];
$semester = (string)$data['semester'];
$schoolYear = (string)$data['school_year'];
$admins = (array)($data['admins'] ?? $data['staff'] ?? []);
$date = (string) $data['date'];
$semester = (string) $data['semester'];
$schoolYear = (string) $data['school_year'];
$admins = (array) ($data['admins'] ?? $data['staff'] ?? []);
$allowedStatuses = ['present', 'absent', 'late'];
$excludedRoleSlugs = ['parent', 'guest', 'teacher', 'teacher_assistant'];
DB::transaction(function () use ($admins, $date, $semester, $schoolYear, $allowedStatuses, $excludedRoleSlugs) {
foreach ($admins as $uid => $row) {
$uid = (int)$uid;
$status = strtolower(trim((string)($row['status'] ?? '')));
$reason = trim((string)($row['reason'] ?? ''));
$roleName = (string)($row['role_name'] ?? '');
$roleSlug = strtolower(str_replace(['-', ' '], '_', (string)($row['role_slug'] ?? $roleName)));
$uid = (int) $uid;
$status = strtolower(trim((string) ($row['status'] ?? '')));
$reason = trim((string) ($row['reason'] ?? ''));
$roleName = (string) ($row['role_name'] ?? '');
$roleSlug = strtolower(str_replace(['-', ' '], '_', (string) ($row['role_slug'] ?? $roleName)));
if (in_array($roleSlug, $excludedRoleSlugs, true)) {
continue;
@@ -659,10 +661,11 @@ class StaffAttendanceService
->where('semester', $semester)
->where('school_year', $schoolYear)
->delete();
continue;
}
if (!in_array($status, $allowedStatuses, true)) {
if (! in_array($status, $allowedStatuses, true)) {
continue;
}
@@ -692,13 +695,13 @@ class StaffAttendanceService
public function saveCell(Authenticatable $user, array $data): array
{
$date = (string)$data['date'];
$status = (string)($data['status'] ?? '');
$targetId = (int)$data['user_id'];
$roleName = trim((string)($data['role_name'] ?? ''));
$semester = (string)$data['semester'];
$schoolYear = (string)$data['school_year'];
$reason = trim((string)($data['reason'] ?? ''));
$date = (string) $data['date'];
$status = (string) ($data['status'] ?? '');
$targetId = (int) $data['user_id'];
$roleName = trim((string) ($data['role_name'] ?? ''));
$semester = (string) $data['semester'];
$schoolYear = (string) $data['school_year'];
$reason = trim((string) ($data['reason'] ?? ''));
if ($status === '') {
DB::table('staff_attendance')
@@ -732,9 +735,9 @@ class StaffAttendanceService
public function monthCsv(?string $semester, ?string $schoolYear, ?string $scope): StreamedResponse
{
$semester = $semester ?: (string)$this->configuration->getConfig('semester');
$schoolYear = $schoolYear ?: (string)$this->configuration->getConfig('school_year');
$scope = strtolower((string)$scope);
$semester = $semester ?: (string) $this->configuration->getConfig('semester');
$schoolYear = $schoolYear ?: (string) $this->configuration->getConfig('school_year');
$scope = strtolower((string) $scope);
[$rangeStart, $rangeEnd] = $this->semesterRangeService->getSchoolYearRange($schoolYear);
$semesterNorm = $this->semesterRangeService->normalizeSemester($semester);
@@ -748,9 +751,9 @@ class StaffAttendanceService
$daysList = $this->semesterRangeService->buildSundayList($rangeStart, $rangeEnd);
$filename = ($scope === 'admins' ? 'admin_attendance_' : 'teacher_attendance_')
. $semester . '_' . str_replace('/', '-', $schoolYear) . '.csv';
.$semester.'_'.str_replace('/', '-', $schoolYear).'.csv';
return response()->streamDownload(function () use ($semester, $schoolYear, $scope, $daysList, $semesterNorm, $rangeStart, $rangeEnd) {
return response()->streamDownload(function () use ($schoolYear, $scope, $semesterNorm, $rangeStart, $rangeEnd) {
$out = fopen('php://output', 'w');
if ($scope === 'admins') {
@@ -770,12 +773,13 @@ class StaffAttendanceService
fputcsv($out, [
$row->user_id,
$row->date,
strtoupper(substr((string)$row->status, 0, 1)),
strtoupper(substr((string) $row->status, 0, 1)),
$row->reason,
]);
}
fclose($out);
return;
}
@@ -795,7 +799,7 @@ class StaffAttendanceService
fputcsv($out, [
$row->user_id,
$row->date,
strtoupper(substr((string)$row->status, 0, 1)),
strtoupper(substr((string) $row->status, 0, 1)),
$row->reason,
]);
}