add all controllers logic

This commit is contained in:
root
2026-03-11 17:53:15 -04:00
parent 3e6c577085
commit 2ef71cc92b
421 changed files with 12009 additions and 5211 deletions
+100
View File
@@ -0,0 +1,100 @@
<?php
namespace App\Services\Staff;
use App\Models\Staff;
use App\Models\User;
use App\Services\System\GlobalConfigService;
use Illuminate\Support\Facades\DB;
use RuntimeException;
class StaffCommandService
{
public function __construct(private GlobalConfigService $configService)
{
}
public function create(array $payload): Staff
{
return DB::transaction(function () use ($payload) {
$userId = $this->resolveUserId($payload);
$roleName = (string) ($payload['role_name'] ?? '');
$activeRole = strtolower($roleName);
if (!empty($payload['status']) && strtolower((string) $payload['status']) === 'inactive') {
$activeRole = 'inactive';
}
$schoolYear = (string) ($payload['school_year'] ?? $this->configService->getSchoolYear() ?? '');
$staff = Staff::query()->create([
'user_id' => $userId,
'firstname' => (string) $payload['firstname'],
'lastname' => (string) $payload['lastname'],
'email' => (string) $payload['email'],
'phone' => $payload['phone'] ?? null,
'role_name' => $roleName,
'active_role' => $activeRole,
'school_year' => $schoolYear,
'created_at' => now(),
'updated_at' => now(),
]);
if (!$staff) {
throw new RuntimeException('Failed to create staff member.');
}
return $staff;
});
}
public function update(Staff $staff, array $payload): Staff
{
return DB::transaction(function () use ($staff, $payload) {
$updates = [];
foreach (['firstname', 'lastname', 'email', 'phone', 'role_name', 'school_year'] as $field) {
if (array_key_exists($field, $payload) && $payload[$field] !== '') {
$updates[$field] = $payload[$field];
}
}
if (array_key_exists('role_name', $updates)) {
$updates['active_role'] = strtolower((string) $updates['role_name']);
}
if (!empty($payload['status']) && strtolower((string) $payload['status']) === 'inactive') {
$updates['active_role'] = 'inactive';
}
$updates['updated_at'] = now();
if (!empty($updates)) {
$staff->fill($updates);
$staff->save();
}
return $staff->fresh();
});
}
public function delete(Staff $staff): bool
{
return (bool) $staff->delete();
}
private function resolveUserId(array $payload): int
{
if (!empty($payload['user_id'])) {
return (int) $payload['user_id'];
}
$email = $payload['email'] ?? null;
if ($email) {
$userId = User::query()->where('email', $email)->value('id');
if ($userId) {
return (int) $userId;
}
}
throw new RuntimeException('A valid user_id or existing email is required.');
}
}
+112
View File
@@ -0,0 +1,112 @@
<?php
namespace App\Services\Staff;
use App\Models\Staff;
use App\Models\TeacherClass;
use App\Models\User;
use App\Services\System\GlobalConfigService;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
class StaffQueryService
{
public function __construct(private GlobalConfigService $configService)
{
}
public function paginate(array $filters, int $page, int $perPage): array
{
$excluded = ['student', 'parent', 'guest', 'inactive'];
$query = Staff::query()
->whereNotIn(DB::raw('LOWER(active_role)'), $excluded);
if (!empty($filters['role'])) {
$role = strtolower(trim((string) $filters['role']));
$query->whereRaw('LOWER(active_role) = ?', [$role]);
}
$sortBy = $filters['sort_by'] ?? 'created_at';
$sortDir = strtolower((string) ($filters['sort_dir'] ?? 'desc')) === 'asc' ? 'asc' : 'desc';
$paginator = $query
->orderBy($sortBy, $sortDir)
->paginate($perPage, ['*'], 'page', $page);
$schoolYear = (string) ($filters['school_year'] ?? $this->configService->getSchoolYear() ?? '');
$semester = (string) ($filters['semester'] ?? $this->configService->getSemester() ?? '');
$assignByTeacher = $this->buildAssignments($schoolYear);
$issuesCount = 0;
$items = $paginator->getCollection()->map(function ($staff) use ($assignByTeacher, &$issuesCount) {
$staff->school_id = User::getSchoolIdByUserId((int) ($staff->user_id ?? 0));
$role = strtolower((string) ($staff->active_role ?? ''));
if (in_array($role, ['teacher', 'teacher_assistant'], true)) {
$tid = (int) ($staff->user_id ?? 0);
$labels = $assignByTeacher[$tid] ?? [];
if (!empty($labels)) {
$staff->class_section = implode(', ', array_unique($labels));
$staff->verification_issue = false;
} else {
$staff->class_section = 'No class assigned';
$staff->verification_issue = true;
$issuesCount++;
}
} else {
$staff->class_section = '—';
$staff->verification_issue = false;
}
return $staff;
});
$paginator->setCollection($items);
return [
'paginator' => $paginator,
'issues_count' => $issuesCount,
'semester' => $semester,
'school_year' => $schoolYear,
];
}
public function find(int $id): ?Staff
{
return Staff::query()->find($id);
}
private function buildAssignments(string $schoolYear): array
{
if ($schoolYear === '') {
return [];
}
$rows = TeacherClass::query()
->select('teacher_class.teacher_id', 'teacher_class.position', 'classSection.class_section_name')
->leftJoin('classSection', 'classSection.class_section_id', '=', 'teacher_class.class_section_id')
->where('teacher_class.school_year', $schoolYear)
->get();
$assignByTeacher = [];
foreach ($rows as $row) {
$tid = (int) ($row->teacher_id ?? 0);
if ($tid <= 0) {
continue;
}
$name = trim((string) ($row->class_section_name ?? ''));
if ($name === '') {
continue;
}
$pos = strtolower((string) ($row->position ?? ''));
if (!in_array($pos, ['main', 'ta'], true)) {
continue;
}
$assignByTeacher[$tid][] = $name . ' (' . $pos . ')';
}
return $assignByTeacher;
}
}