StudentContractService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 getFranchisorSummary(): array
  10. {
  11. return [
  12. 'frozen' => StudentContract::where('status', 'frozen')->count(),
  13. 'cancelled' => StudentContract::where('status', 'cancelled')->count(),
  14. ];
  15. }
  16. public function getFranchisorByStatus(string $status): Collection
  17. {
  18. return StudentContract::with(['student', 'unit'])
  19. ->where('status', $status)
  20. ->orderBy('created_at', 'desc')
  21. ->get();
  22. }
  23. public function getAll(int $unitId, ?int $studentId = null): Collection
  24. {
  25. return StudentContract::with(['student.city', 'student.state', 'classPackageUnit'])
  26. ->where('unit_id', $unitId)
  27. ->when($studentId, fn ($q) => $q->where('student_id', $studentId))
  28. ->orderBy('created_at', 'desc')
  29. ->get();
  30. }
  31. public function findById(int $id): ?StudentContract
  32. {
  33. return StudentContract::with(['student', 'classPackageUnit'])->find($id);
  34. }
  35. public function create(array $data): StudentContract
  36. {
  37. if (!empty($data['due_day'])) {
  38. $data['recurring_day'] = (int) $data['due_day'];
  39. }
  40. unset($data['due_day']);
  41. return StudentContract::create($data);
  42. }
  43. public function update(int $id, array $data): ?StudentContract
  44. {
  45. $model = $this->findById($id);
  46. if (!$model) {
  47. return null;
  48. }
  49. if (!empty($data['due_day'])) {
  50. $data['recurring_day'] = (int) $data['due_day'];
  51. }
  52. unset($data['due_day']);
  53. $model->update($data);
  54. return $model->fresh();
  55. }
  56. public function attachFile(int $id, $file): ?StudentContract
  57. {
  58. $model = $this->findById($id);
  59. if (!$model) {
  60. return null;
  61. }
  62. /** @var \Illuminate\Http\UploadedFile $file */
  63. $path = $file->store('student-media');
  64. StudentMedia::create([
  65. 'student_id' => $model->student_id,
  66. 'student_contract_id' => $model->id,
  67. 'url' => $path,
  68. 'file_type' => $file->getMimeType(),
  69. 'type' => 'contract',
  70. ]);
  71. $model->update(['file_url' => Storage::url($path), 'file_type' => $file->getMimeType()]);
  72. return $model->fresh();
  73. }
  74. public function freeze(int $id): ?StudentContract
  75. {
  76. $model = $this->findById($id);
  77. if (!$model) {
  78. return null;
  79. }
  80. $model->update(['status' => 'frozen']);
  81. return $model->fresh();
  82. }
  83. public function cancel(int $id): ?StudentContract
  84. {
  85. $model = $this->findById($id);
  86. if (!$model) {
  87. return null;
  88. }
  89. $model->update(['status' => 'cancelled']);
  90. return $model->fresh();
  91. }
  92. public function reactivate(int $id): ?StudentContract
  93. {
  94. $model = $this->findById($id);
  95. if (!$model) {
  96. return null;
  97. }
  98. $model->update(['status' => 'active']);
  99. return $model->fresh();
  100. }
  101. public function delete(int $id): bool
  102. {
  103. $model = $this->findById($id);
  104. if (!$model) {
  105. return false;
  106. }
  107. if ($model->file_url) {
  108. Storage::delete($model->file_url);
  109. }
  110. return $model->delete();
  111. }
  112. }