StudentContractService.php 3.8 KB

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