StudentContractService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Services;
  3. use App\Models\StudentContract;
  4. use App\Models\StudentMedia;
  5. use Illuminate\Database\Eloquent\Collection;
  6. use Illuminate\Support\Facades\Storage;
  7. class StudentContractService
  8. {
  9. public function getAll(int $unitId, ?int $studentId = null): Collection
  10. {
  11. return StudentContract::with(['student.city', 'student.state', 'classPackageUnit'])
  12. ->where('unit_id', $unitId)
  13. ->when($studentId, fn ($q) => $q->where('student_id', $studentId))
  14. ->orderBy('created_at', 'desc')
  15. ->get();
  16. }
  17. public function findById(int $id): ?StudentContract
  18. {
  19. return StudentContract::with(['student', 'classPackageUnit'])->find($id);
  20. }
  21. public function create(array $data): StudentContract
  22. {
  23. if (!empty($data['due_day'])) {
  24. $data['recurring_day'] = (int) $data['due_day'];
  25. }
  26. unset($data['due_day']);
  27. return StudentContract::create($data);
  28. }
  29. public function update(int $id, array $data): ?StudentContract
  30. {
  31. $model = $this->findById($id);
  32. if (!$model) {
  33. return null;
  34. }
  35. if (!empty($data['due_day'])) {
  36. $data['recurring_day'] = (int) $data['due_day'];
  37. }
  38. unset($data['due_day']);
  39. $model->update($data);
  40. return $model->fresh();
  41. }
  42. public function attachFile(int $id, $file): ?StudentContract
  43. {
  44. $model = $this->findById($id);
  45. if (!$model) {
  46. return null;
  47. }
  48. /** @var \Illuminate\Http\UploadedFile $file */
  49. $path = $file->store('student-media');
  50. StudentMedia::create([
  51. 'student_id' => $model->student_id,
  52. 'student_contract_id' => $model->id,
  53. 'url' => $path,
  54. 'file_type' => $file->getMimeType(),
  55. 'type' => 'contract',
  56. ]);
  57. $model->update(['file_url' => Storage::url($path), 'file_type' => $file->getMimeType()]);
  58. return $model->fresh();
  59. }
  60. public function freeze(int $id): ?StudentContract
  61. {
  62. $model = $this->findById($id);
  63. if (!$model) {
  64. return null;
  65. }
  66. $model->update(['status' => 'frozen']);
  67. return $model->fresh();
  68. }
  69. public function cancel(int $id): ?StudentContract
  70. {
  71. $model = $this->findById($id);
  72. if (!$model) {
  73. return null;
  74. }
  75. $model->update(['status' => 'cancelled']);
  76. return $model->fresh();
  77. }
  78. public function reactivate(int $id): ?StudentContract
  79. {
  80. $model = $this->findById($id);
  81. if (!$model) {
  82. return null;
  83. }
  84. $model->update(['status' => 'active']);
  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->file_url) {
  94. Storage::delete($model->file_url);
  95. }
  96. return $model->delete();
  97. }
  98. }