fix tests
This commit is contained in:
@@ -3,13 +3,13 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class StaffAttendance extends BaseModel
|
||||
{
|
||||
protected $table = 'staff_attendance';
|
||||
|
||||
public $timestamps = true;
|
||||
|
||||
/**
|
||||
@@ -38,13 +38,13 @@ class StaffAttendance extends BaseModel
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'user_id' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'class_section_id' => 'integer',
|
||||
'created_by' => 'integer',
|
||||
'updated_by' => 'integer',
|
||||
'date' => 'date:Y-m-d',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
'created_by' => 'integer',
|
||||
'updated_by' => 'integer',
|
||||
'date' => 'date:Y-m-d',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
/* ============================================================
|
||||
@@ -52,11 +52,14 @@ class StaffAttendance extends BaseModel
|
||||
* ============================================================
|
||||
*/
|
||||
public const STATUS_PRESENT = 'present';
|
||||
public const STATUS_ABSENT = 'absent';
|
||||
public const STATUS_LATE = 'late';
|
||||
|
||||
public const STATUS_ABSENT = 'absent';
|
||||
|
||||
public const STATUS_LATE = 'late';
|
||||
|
||||
public const POS_MAIN = 'main';
|
||||
public const POS_TA = 'ta';
|
||||
|
||||
public const POS_TA = 'ta';
|
||||
|
||||
public static function allowedStatuses(): array
|
||||
{
|
||||
@@ -118,12 +121,14 @@ class StaffAttendance extends BaseModel
|
||||
protected static function normStatus(?string $status): ?string
|
||||
{
|
||||
$s = strtolower(trim((string) $status));
|
||||
|
||||
return in_array($s, self::allowedStatuses(), true) ? $s : null;
|
||||
}
|
||||
|
||||
protected static function isValidDate(string $d): bool
|
||||
{
|
||||
$dt = date_create_from_format('Y-m-d', $d);
|
||||
|
||||
return $dt && $dt->format('Y-m-d') === $d;
|
||||
}
|
||||
|
||||
@@ -146,7 +151,7 @@ class StaffAttendance extends BaseModel
|
||||
?string $reason,
|
||||
?int $editorId = null
|
||||
): ?self {
|
||||
if ($userId <= 0 || !self::isValidDate($date) || $semester === '' || $schoolYear === '') {
|
||||
if ($userId <= 0 || ! self::isValidDate($date) || $semester === '' || $schoolYear === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -158,17 +163,17 @@ class StaffAttendance extends BaseModel
|
||||
$editorId = $editorId ?: (auth()->id() ?? null);
|
||||
|
||||
$keys = [
|
||||
'user_id' => $userId,
|
||||
'date' => $date,
|
||||
'semester' => $semester,
|
||||
'user_id' => $userId,
|
||||
'date' => $date,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
];
|
||||
|
||||
$payload = [
|
||||
'role_name' => $roleName,
|
||||
'status' => $norm,
|
||||
'reason' => $reason ?: null,
|
||||
'updated_by' => $editorId,
|
||||
'role_name' => $roleName,
|
||||
'status' => $norm,
|
||||
'reason' => $reason ?: null,
|
||||
'updated_by' => $editorId,
|
||||
];
|
||||
|
||||
// Ensure created_by is set on create
|
||||
@@ -194,7 +199,7 @@ class StaffAttendance extends BaseModel
|
||||
?string $reason = null,
|
||||
?int $editorId = null
|
||||
): bool {
|
||||
if ($userId <= 0 || !self::isValidDate($date) || $semester === '' || $schoolYear === '') {
|
||||
if ($userId <= 0 || ! self::isValidDate($date) || $semester === '' || $schoolYear === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -212,26 +217,28 @@ class StaffAttendance extends BaseModel
|
||||
if ($existing) {
|
||||
return (bool) $existing->delete();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
$keys = [
|
||||
'user_id' => $userId,
|
||||
'date' => $date,
|
||||
'semester' => $semester,
|
||||
'user_id' => $userId,
|
||||
'date' => $date,
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
];
|
||||
|
||||
$payload = [
|
||||
'role_name' => $roleName ?? ($existing->role_name ?? null),
|
||||
'role_name' => $roleName ?? ($existing->role_name ?? null),
|
||||
'class_section_id' => $classSectionId,
|
||||
'position' => $position,
|
||||
'status' => $norm,
|
||||
'reason' => $reason,
|
||||
'updated_by' => $editorId,
|
||||
'position' => $position,
|
||||
'status' => $norm,
|
||||
'reason' => $reason,
|
||||
'updated_by' => $editorId,
|
||||
];
|
||||
|
||||
static::query()->updateOrCreate($keys, $payload + ['created_by' => $editorId]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -243,6 +250,7 @@ class StaffAttendance extends BaseModel
|
||||
public static function deleteOne(int $userId, string $date): bool
|
||||
{
|
||||
$row = static::getOne($userId, $date);
|
||||
|
||||
return $row ? (bool) $row->delete() : true;
|
||||
}
|
||||
|
||||
@@ -276,27 +284,27 @@ class StaffAttendance extends BaseModel
|
||||
->leftJoinSub($roleSub, 'rs', fn ($j) => $j->on('rs.user_id', '=', 'u.id'))
|
||||
->leftJoin('staff_attendance as sa', function ($j) use ($date, $semester, $schoolYear) {
|
||||
$j->on('sa.user_id', '=', 'u.id')
|
||||
->whereDate('sa.date', '=', $date)
|
||||
->where('sa.semester', '=', $semester)
|
||||
->where('sa.school_year', '=', $schoolYear);
|
||||
->whereDate('sa.date', '=', $date)
|
||||
->where('sa.semester', '=', $semester)
|
||||
->where('sa.school_year', '=', $schoolYear);
|
||||
})
|
||||
->whereNotIn(DB::raw('LOWER(COALESCE(rs.role_name,""))'), ['parent', 'guest'])
|
||||
->orderBy('u.firstname')
|
||||
->orderBy('u.lastname');
|
||||
|
||||
if (!empty($excludeUserIds)) {
|
||||
if (! empty($excludeUserIds)) {
|
||||
$q->whereNotIn('u.id', array_values(array_map('intval', $excludeUserIds)));
|
||||
}
|
||||
|
||||
return $q->get()->map(function ($r) {
|
||||
return [
|
||||
'user_id' => (int) $r->user_id,
|
||||
'firstname' => (string) ($r->firstname ?? ''),
|
||||
'lastname' => (string) ($r->lastname ?? ''),
|
||||
'role_name' => (string) ($r->role_name ?? ''),
|
||||
'satt_id' => $r->satt_id ? (int) $r->satt_id : null,
|
||||
'status' => $r->status ? (string) $r->status : null,
|
||||
'reason' => $r->reason,
|
||||
'user_id' => (int) $r->user_id,
|
||||
'firstname' => (string) ($r->firstname ?? ''),
|
||||
'lastname' => (string) ($r->lastname ?? ''),
|
||||
'role_name' => (string) ($r->role_name ?? ''),
|
||||
'satt_id' => $r->satt_id ? (int) $r->satt_id : null,
|
||||
'status' => $r->status ? (string) $r->status : null,
|
||||
'reason' => $r->reason,
|
||||
];
|
||||
})->all();
|
||||
}
|
||||
@@ -318,10 +326,10 @@ class StaffAttendance extends BaseModel
|
||||
->get()
|
||||
->map(fn ($r) => [
|
||||
'class_section_id' => (int) $r->class_section_id,
|
||||
'teacher_id' => (int) $r->teacher_id,
|
||||
'position' => (string) $r->position,
|
||||
'firstname' => (string) ($r->firstname ?? ''),
|
||||
'lastname' => (string) ($r->lastname ?? ''),
|
||||
'teacher_id' => (int) $r->teacher_id,
|
||||
'position' => (string) $r->position,
|
||||
'firstname' => (string) ($r->firstname ?? ''),
|
||||
'lastname' => (string) ($r->lastname ?? ''),
|
||||
])->all();
|
||||
}
|
||||
|
||||
@@ -350,20 +358,20 @@ class StaffAttendance extends BaseModel
|
||||
string $schoolYear
|
||||
): array {
|
||||
return DB::table('teacher_class as tc')
|
||||
->selectRaw("
|
||||
->selectRaw('
|
||||
tc.class_section_id,
|
||||
tc.teacher_id as teacher_id,
|
||||
tc.position,
|
||||
u.firstname, u.lastname,
|
||||
t.id as satt_id, t.status, t.reason
|
||||
")
|
||||
')
|
||||
->join('users as u', 'u.id', '=', 'tc.teacher_id')
|
||||
->leftJoin('staff_attendance as t', function ($j) use ($date, $semester, $schoolYear) {
|
||||
$j->on('t.user_id', '=', 'tc.teacher_id')
|
||||
->on('t.class_section_id', '=', 'tc.class_section_id')
|
||||
->whereDate('t.date', '=', $date)
|
||||
->where('t.semester', '=', $semester)
|
||||
->where('t.school_year', '=', $schoolYear);
|
||||
->on('t.class_section_id', '=', 'tc.class_section_id')
|
||||
->whereDate('t.date', '=', $date)
|
||||
->where('t.semester', '=', $semester)
|
||||
->where('t.school_year', '=', $schoolYear);
|
||||
})
|
||||
->where('tc.class_section_id', $classSectionId)
|
||||
->where('tc.school_year', $schoolYear)
|
||||
@@ -372,13 +380,13 @@ class StaffAttendance extends BaseModel
|
||||
->get()
|
||||
->map(fn ($r) => [
|
||||
'class_section_id' => (int) $r->class_section_id,
|
||||
'teacher_id' => (int) $r->teacher_id,
|
||||
'position' => (string) $r->position,
|
||||
'firstname' => (string) ($r->firstname ?? ''),
|
||||
'lastname' => (string) ($r->lastname ?? ''),
|
||||
'satt_id' => $r->satt_id ? (int) $r->satt_id : null,
|
||||
'status' => $r->status ? strtolower((string) $r->status) : null,
|
||||
'reason' => $r->reason,
|
||||
'teacher_id' => (int) $r->teacher_id,
|
||||
'position' => (string) $r->position,
|
||||
'firstname' => (string) ($r->firstname ?? ''),
|
||||
'lastname' => (string) ($r->lastname ?? ''),
|
||||
'satt_id' => $r->satt_id ? (int) $r->satt_id : null,
|
||||
'status' => $r->status ? strtolower((string) $r->status) : null,
|
||||
'reason' => $r->reason,
|
||||
])->all();
|
||||
}
|
||||
|
||||
@@ -393,7 +401,9 @@ class StaffAttendance extends BaseModel
|
||||
->where('school_year', $schoolYear)
|
||||
->count();
|
||||
|
||||
if ($assigned <= 0) return true;
|
||||
if ($assigned <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$present = (int) DB::table('staff_attendance')
|
||||
->where('class_section_id', $classSectionId)
|
||||
@@ -418,7 +428,9 @@ class StaffAttendance extends BaseModel
|
||||
string $schoolYear
|
||||
): array {
|
||||
$codes = array_values(array_unique(array_filter(array_map('intval', $sectionCodes))));
|
||||
if (empty($codes)) return [];
|
||||
if (empty($codes)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rows = DB::table('staff_attendance')
|
||||
->selectRaw('class_section_id, user_id, DATE_FORMAT(date,"%Y-%m-%d") as d, status, reason')
|
||||
@@ -440,6 +452,7 @@ class StaffAttendance extends BaseModel
|
||||
'reason' => $r->reason,
|
||||
];
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
@@ -452,20 +465,20 @@ class StaffAttendance extends BaseModel
|
||||
$req = $updating ? 'sometimes' : 'required';
|
||||
|
||||
return [
|
||||
'user_id' => [$req, 'integer', 'min:1', 'exists:users,id'],
|
||||
'date' => [$req, 'date_format:Y-m-d'],
|
||||
'semester' => [$req, 'string', 'max:20'],
|
||||
'user_id' => [$req, 'integer', 'min:1', 'exists:users,id'],
|
||||
'date' => [$req, 'date_format:Y-m-d'],
|
||||
'semester' => [$req, 'string', 'max:20'],
|
||||
'school_year' => [$req, 'string', 'max:20'],
|
||||
|
||||
'role_name' => ['nullable', 'string', 'max:80'],
|
||||
'status' => ['nullable', 'string', 'in:' . implode(',', self::allowedStatuses())],
|
||||
'reason' => ['nullable', 'string', 'max:2000'],
|
||||
'role_name' => ['nullable', 'string', 'max:80'],
|
||||
'status' => ['nullable', 'string', 'in:'.implode(',', self::allowedStatuses())],
|
||||
'reason' => ['nullable', 'string', 'max:2000'],
|
||||
|
||||
'class_section_id' => ['nullable', 'integer'],
|
||||
'position' => ['nullable', 'string', 'in:' . implode(',', [self::POS_MAIN, self::POS_TA])],
|
||||
'position' => ['nullable', 'string', 'in:'.implode(',', [self::POS_MAIN, self::POS_TA])],
|
||||
|
||||
'created_by' => ['nullable', 'integer'],
|
||||
'updated_by' => ['nullable', 'integer'],
|
||||
'created_by' => ['nullable', 'integer'],
|
||||
'updated_by' => ['nullable', 'integer'],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user