| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace App\Services;
- use App\Models\StudentContract;
- use App\Models\StudentMedia;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Support\Facades\Storage;
- class StudentContractService
- {
- public function getFranchisorSummary(array $unitIds = []): array
- {
- $base = StudentContract::query()
- ->when(!empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds));
- return [
- 'frozen' => (clone $base)->where('status', 'frozen')->count(),
- 'cancelled' => (clone $base)->where('status', 'cancelled')->count(),
- ];
- }
- public function getFranchisorByStatus(string $status, array $unitIds = []): Collection
- {
- return StudentContract::with(['student', 'unit'])
- ->where('status', $status)
- ->when(!empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds))
- ->orderBy('created_at', 'desc')
- ->get();
- }
- public function getAll(int $unitId, ?int $studentId = null): Collection
- {
- return StudentContract::with(['student.city', 'student.state', 'classPackageUnit'])
- ->where('unit_id', $unitId)
- ->when($studentId, fn ($q) => $q->where('student_id', $studentId))
- ->orderBy('created_at', 'desc')
- ->get();
- }
- public function findById(int $id): ?StudentContract
- {
- return StudentContract::with(['student', 'classPackageUnit'])->find($id);
- }
- public function create(array $data): StudentContract
- {
- if (!empty($data['due_day'])) {
- $data['recurring_day'] = (int) $data['due_day'];
- }
- unset($data['due_day']);
- return StudentContract::create($data);
- }
- public function update(int $id, array $data): ?StudentContract
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- if (!empty($data['due_day'])) {
- $data['recurring_day'] = (int) $data['due_day'];
- }
- unset($data['due_day']);
- $model->update($data);
- return $model->fresh();
- }
- public function attachFile(int $id, $file): ?StudentContract
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- /** @var \Illuminate\Http\UploadedFile $file */
- $path = $file->store('student-media');
- StudentMedia::create([
- 'student_id' => $model->student_id,
- 'student_contract_id' => $model->id,
- 'url' => $path,
- 'file_type' => $file->getMimeType(),
- 'type' => 'contract',
- ]);
- $model->update(['file_url' => Storage::url($path), 'file_type' => $file->getMimeType()]);
- return $model->fresh();
- }
- public function freeze(int $id): ?StudentContract
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- $model->update(['status' => 'frozen']);
- return $model->fresh();
- }
- public function cancel(int $id): ?StudentContract
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- $model->update(['status' => 'cancelled']);
- return $model->fresh();
- }
- public function reactivate(int $id): ?StudentContract
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- $model->update(['status' => 'active']);
- return $model->fresh();
- }
- public function delete(int $id): bool
- {
- $model = $this->findById($id);
- if (!$model) {
- return false;
- }
- if ($model->file_url) {
- Storage::delete($model->file_url);
- }
- return $model->delete();
- }
- }
|