Răsfoiți Sursa

feat(filter): adiciona filtros de unidades e grupos em dash

ebagabee 3 săptămâni în urmă
părinte
comite
c88176876f

+ 6 - 3
app/Http/Controllers/StudentContractController.php

@@ -17,18 +17,21 @@ public function __construct(
 
     public function franchisorSummary(): JsonResponse
     {
-        return $this->successResponse(payload: $this->service->getFranchisorSummary());
+        $unitIds = array_filter((array) request()->input('unit_ids', []));
+        return $this->successResponse(payload: $this->service->getFranchisorSummary($unitIds));
     }
 
     public function franchisorFrozen(): JsonResponse
     {
-        $items = $this->service->getFranchisorByStatus('frozen');
+        $unitIds = array_filter((array) request()->input('unit_ids', []));
+        $items   = $this->service->getFranchisorByStatus('frozen', $unitIds);
         return $this->successResponse(payload: StudentContractResource::collection($items));
     }
 
     public function franchisorCancelled(): JsonResponse
     {
-        $items = $this->service->getFranchisorByStatus('cancelled');
+        $unitIds = array_filter((array) request()->input('unit_ids', []));
+        $items   = $this->service->getFranchisorByStatus('cancelled', $unitIds);
         return $this->successResponse(payload: StudentContractResource::collection($items));
     }
 

+ 4 - 2
app/Http/Controllers/StudentController.php

@@ -22,7 +22,8 @@ public function index(): JsonResponse
 
     public function franchisorActive(): JsonResponse
     {
-        $items = $this->service->getFranchisorActive();
+        $unitIds = array_filter((array) request()->input('unit_ids', []));
+        $items   = $this->service->getFranchisorActive($unitIds);
         return $this->successResponse(payload: StudentResource::collection($items));
     }
 
@@ -34,7 +35,8 @@ public function franchisorStudentDetail(int $id): JsonResponse
 
     public function franchisorSummary(): JsonResponse
     {
-        $summary = $this->service->getFranchisorSummary();
+        $unitIds = array_filter((array) request()->input('unit_ids', []));
+        $summary = $this->service->getFranchisorSummary($unitIds);
         return $this->successResponse(payload: $summary);
     }
 

+ 8 - 4
app/Services/StudentContractService.php

@@ -9,18 +9,22 @@
 
 class StudentContractService
 {
-    public function getFranchisorSummary(): array
+    public function getFranchisorSummary(array $unitIds = []): array
     {
+        $base = StudentContract::query()
+            ->when(!empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds));
+
         return [
-            'frozen'    => StudentContract::where('status', 'frozen')->count(),
-            'cancelled' => StudentContract::where('status', 'cancelled')->count(),
+            'frozen'    => (clone $base)->where('status', 'frozen')->count(),
+            'cancelled' => (clone $base)->where('status', 'cancelled')->count(),
         ];
     }
 
-    public function getFranchisorByStatus(string $status): Collection
+    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();
     }

+ 7 - 4
app/Services/StudentService.php

@@ -20,10 +20,11 @@ public function getAll(User $user): Collection
             ->get();
     }
 
-    public function getFranchisorActive(): Collection
+    public function getFranchisorActive(array $unitIds = []): Collection
     {
         return Student::with('unit')
             ->where('status', 'active')
+            ->when(!empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds))
             ->orderBy('name')
             ->get();
     }
@@ -49,10 +50,12 @@ public function getFranchisorStudentDetail(int $id): ?array
         ];
     }
 
-    public function getFranchisorSummary(): array
+    public function getFranchisorSummary(array $unitIds = []): array
     {
-        $total  = Student::count();
-        $active = Student::where('status', 'active')->count();
+        $query = Student::query()->when(!empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds));
+
+        $total  = $query->count();
+        $active = (clone $query)->where('status', 'active')->count();
 
         return ['total' => $total, 'active' => $active];
     }