StudentService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Student;
  4. use App\Models\StudentContract;
  5. use App\Models\User;
  6. use Illuminate\Database\Eloquent\Collection;
  7. use Illuminate\Http\UploadedFile;
  8. use Illuminate\Support\Facades\Storage;
  9. class StudentService
  10. {
  11. public function getAll(User $user): Collection
  12. {
  13. $unitId = $this->resolveUnitId($user);
  14. return Student::where('unit_id', $unitId)
  15. ->orderBy('created_at', 'desc')
  16. ->get();
  17. }
  18. public function getFranchisorActive(array $unitIds = []): Collection
  19. {
  20. return Student::with('unit')
  21. ->whereHas('contracts', fn ($q) => $q->where('status', 'active'))
  22. ->when(!empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds))
  23. ->orderBy('name')
  24. ->get();
  25. }
  26. public function getFranchisorStudentDetail(int $id): ?array
  27. {
  28. $student = Student::with('unit')->find($id);
  29. if (!$student) {
  30. return null;
  31. }
  32. $contract = StudentContract::where('student_id', $id)
  33. ->where('status', 'active')
  34. ->first();
  35. return [
  36. 'id' => $student->id,
  37. 'name' => $student->name,
  38. 'phone' => $student->phone,
  39. 'unit' => $student->unit ? ['fantasy_name' => $student->unit->fantasy_name] : null,
  40. 'protocol' => $contract?->protocol,
  41. ];
  42. }
  43. public function getFranchisorSummary(array $unitIds = []): array
  44. {
  45. $query = Student::query()->when(!empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds));
  46. $total = $query->count();
  47. $active = (clone $query)->where('status', 'active')->count();
  48. return ['total' => $total, 'active' => $active];
  49. }
  50. public function findById(int $id): ?Student
  51. {
  52. return Student::find($id);
  53. }
  54. public function create(User $user, array $data): Student
  55. {
  56. $unitId = $this->resolveUnitId($user);
  57. $data = $this->handlePhoto($data);
  58. return Student::create(array_merge($data, ['unit_id' => $unitId]));
  59. }
  60. public function update(int $id, array $data): ?Student
  61. {
  62. $model = $this->findById($id);
  63. if (!$model) {
  64. return null;
  65. }
  66. $data = $this->handlePhoto($data, $model->photo_url);
  67. $model->update($data);
  68. return $model->fresh();
  69. }
  70. public function delete(int $id): bool
  71. {
  72. $model = $this->findById($id);
  73. if (!$model) {
  74. return false;
  75. }
  76. if ($model->photo_url) {
  77. Storage::delete($model->photo_url);
  78. }
  79. return $model->delete();
  80. }
  81. private function handlePhoto(array $data, ?string $oldPhotoPath = null): array
  82. {
  83. if (!isset($data['avatar'])) {
  84. return $data;
  85. }
  86. if ($data['avatar'] instanceof UploadedFile) {
  87. if ($oldPhotoPath) {
  88. Storage::delete($oldPhotoPath);
  89. }
  90. $data['photo_url'] = $data['avatar']->store('students/photos');
  91. } elseif (is_null($data['avatar'])) {
  92. if ($oldPhotoPath) {
  93. Storage::delete($oldPhotoPath);
  94. }
  95. $data['photo_url'] = null;
  96. }
  97. unset($data['avatar']);
  98. return $data;
  99. }
  100. private function resolveUnitId(User $user): int
  101. {
  102. $activeUnitId = request()->input('active_unit_id');
  103. if ($activeUnitId) {
  104. $unit = $user->units()->where('units.id', $activeUnitId)->first();
  105. abort_if(!$unit, 403, 'Unidade não autorizada para este usuário.');
  106. return $unit->id;
  107. }
  108. $unit = $user->units()->first();
  109. abort_if(!$unit, 403, 'Usuário sem unidade associada.');
  110. return $unit->id;
  111. }
  112. }