fix db tables to have school year

This commit is contained in:
root
2026-07-12 01:02:04 -04:00
parent ed11cccecc
commit ec9fca8c45
42 changed files with 988 additions and 195 deletions
+3 -24
View File
@@ -21,8 +21,6 @@ class StaffModel extends Model
'phone',
'role_name',
'active_role',
'status',
'school_year',
'created_at',
'updated_at'
];
@@ -44,7 +42,7 @@ class StaffModel extends Model
}
/**
* Insert or update a staff row keyed by (user_id, school_year).
* Insert or update a staff row keyed by user_id.
* Ensures created_at is only set on insert and updated_at can be provided by caller.
*/
public function upsert(array $data): bool
@@ -53,27 +51,9 @@ class StaffModel extends Model
return false;
}
// If school_year not provided, try to keep existing or fall back to current year string
$schoolYear = $data['school_year'] ?? null;
unset($data['school_year'], $data['status']);
if ($schoolYear === null) {
// Try to find any existing row by user_id to reuse its school_year
$existingAny = $this->where('user_id', $data['user_id'])->orderBy('id', 'DESC')->first();
if ($existingAny && isset($existingAny['school_year'])) {
$schoolYear = $existingAny['school_year'];
$data['school_year'] = $schoolYear;
}
}
$existing = null;
if ($schoolYear !== null) {
$existing = $this->where('user_id', $data['user_id'])
->where('school_year', $schoolYear)
->first();
} else {
// If no school_year, treat (user_id) as key
$existing = $this->where('user_id', $data['user_id'])->first();
}
$existing = $this->where('user_id', $data['user_id'])->first();
$now = utc_now();
@@ -100,4 +80,3 @@ class StaffModel extends Model
return $this->insert($data) !== false;
}
}