StudentService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, array $filters = []): Collection
  12. {
  13. $unitId = $this->resolveUnitId($user);
  14. $startDate = $filters['contract_start_date'] ?? null;
  15. $endDate = $filters['contract_end_date'] ?? null;
  16. return Student::where('unit_id', $unitId)
  17. ->when($startDate || $endDate, function ($query) use ($startDate, $endDate) {
  18. $query->whereHas('contracts', function ($contractQuery) use ($startDate, $endDate) {
  19. $contractQuery->where('status', 'active');
  20. if ($endDate) {
  21. $contractQuery->where('started_date', '<=', $endDate);
  22. }
  23. if ($startDate) {
  24. $contractQuery->where(function ($periodQuery) use ($startDate) {
  25. $periodQuery
  26. ->whereNull('end_date')
  27. ->orWhere('end_date', '>=', $startDate);
  28. });
  29. }
  30. });
  31. })
  32. ->orderBy('created_at', 'desc')
  33. ->get();
  34. }
  35. public function getFranchisorActive(array $unitIds = []): Collection
  36. {
  37. return Student::with('unit')
  38. ->whereHas('contracts', fn ($q) => $q->where('status', 'active'))
  39. ->when(!empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds))
  40. ->orderBy('name')
  41. ->get();
  42. }
  43. public function getFranchisorStudentDetail(int $id): ?array
  44. {
  45. $student = Student::with('unit')->find($id);
  46. if (!$student) {
  47. return null;
  48. }
  49. $contract = StudentContract::where('student_id', $id)
  50. ->where('status', 'active')
  51. ->first();
  52. return [
  53. 'id' => $student->id,
  54. 'name' => $student->name,
  55. 'phone' => $student->phone,
  56. 'unit' => $student->unit ? ['fantasy_name' => $student->unit->fantasy_name] : null,
  57. 'protocol' => $contract?->protocol,
  58. ];
  59. }
  60. public function getFranchisorSummary(array $unitIds = []): array
  61. {
  62. $query = Student::query()->when(!empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds));
  63. $total = $query->count();
  64. $active = (clone $query)->where('status', 'active')->count();
  65. return ['total' => $total, 'active' => $active];
  66. }
  67. public function findById(int $id): ?Student
  68. {
  69. return Student::find($id);
  70. }
  71. public function create(User $user, array $data): Student
  72. {
  73. $unitId = $this->resolveUnitId($user);
  74. $data = $this->handlePhoto($data);
  75. return Student::create(array_merge($data, ['unit_id' => $unitId]));
  76. }
  77. public function update(int $id, array $data): ?Student
  78. {
  79. $model = $this->findById($id);
  80. if (!$model) {
  81. return null;
  82. }
  83. $data = $this->handlePhoto($data, $model->photo_url);
  84. $model->update($data);
  85. return $model->fresh();
  86. }
  87. public function delete(int $id): bool
  88. {
  89. $model = $this->findById($id);
  90. if (!$model) {
  91. return false;
  92. }
  93. if ($model->photo_url) {
  94. Storage::delete($model->photo_url);
  95. }
  96. return $model->delete();
  97. }
  98. private function handlePhoto(array $data, ?string $oldPhotoPath = null): array
  99. {
  100. if (!isset($data['avatar'])) {
  101. return $data;
  102. }
  103. if ($data['avatar'] instanceof UploadedFile) {
  104. if ($oldPhotoPath) {
  105. Storage::delete($oldPhotoPath);
  106. }
  107. $data['photo_url'] = $data['avatar']->store('students/photos');
  108. } elseif (is_null($data['avatar'])) {
  109. if ($oldPhotoPath) {
  110. Storage::delete($oldPhotoPath);
  111. }
  112. $data['photo_url'] = null;
  113. }
  114. unset($data['avatar']);
  115. return $data;
  116. }
  117. private function resolveUnitId(User $user): int
  118. {
  119. $activeUnitId = request()->input('active_unit_id');
  120. if ($activeUnitId) {
  121. $unit = $user->units()->where('units.id', $activeUnitId)->first();
  122. abort_if(!$unit, 403, 'Unidade não autorizada para este usuário.');
  123. return $unit->id;
  124. }
  125. $unit = $user->units()->first();
  126. abort_if(!$unit, 403, 'Usuário sem unidade associada.');
  127. return $unit->id;
  128. }
  129. }