api = $api; } public function index() { $groups = $this->api->get('/employee/groups')->json(); $directions = $this->api->get('/employee/directions')->json(); $teachers = $this->api->get('/employee/teachers')->json(); return view('statistics.index', [ 'groups' => $groups, 'directions' => $directions, 'teachers' => $teachers, 'statistics' => request()->has('type') ? $this->getStatistics(request()) : null ]); } public function generate(Request $request) { $validated = $request->validate([ 'type' => 'required|string|in:group,direction,teacher', 'id' => 'required|integer', 'start_date' => 'required|date', 'end_date' => 'required|date|after_or_equal:start_date', ]); if ($validated['type'] === 'teacher') { $teachersResponse = $this->api->get('/employee/teachers'); if (!$teachersResponse->successful()) { return back()->withErrors('Не удалось получить список преподавателей'); } $teachers = $teachersResponse->json()['data'] ?? []; $teacher = collect($teachers)->firstWhere('teacher_id', $validated['id']); if (!$teacher) { return back()->withErrors('Преподаватель не найден'); } $validated['id'] = $teacher['user_id']; } $response = $this->api->get('/employee/statements/statistics', $validated); if ($response->successful()) { $content = $response->body(); $contentType = $response->header('Content-Type'); $contentDisposition = $response->header('Content-Disposition'); return response($content) ->header('Content-Type', $contentType) ->header('Content-Disposition', $contentDisposition); } return back()->withErrors($response->json()['error'] ?? 'Ошибка при генерации статистики'); } /** * Получает статистику через API на основе параметров запроса. */ protected function getStatistics(Request $request): array { $validated = $request->validate([ 'type' => 'required|string|in:group,direction,teacher', 'id' => 'required|integer', 'start_date' => 'required|date', 'end_date' => 'required|date|after_or_equal:start_date', ]); $response = $this->api->get('/employee/statistics', $validated); if ($response->successful()) { return $response->json(); } return [ 'error' => $response->json()['error'] ?? 'Не удалось загрузить статистику' ]; } }