Files
PiAPS_University_Web/University.Web/app/Http/Controllers/StatementController.php

232 lines
8.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Http\Controllers;
use App\Services\ApiService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class StatementController extends Controller
{
protected ApiService $api;
public function __construct(ApiService $api)
{
$this->api = $api;
}
public function index()
{
$response = $this->api->get('/employee/statements');
if ($response->successful()) {
$statements = $response->json();
return view('statements.index', compact('statements'));
}
abort($response->status());
}
public function create()
{
$groups = $this->api->get('/employee/groups')->json();
$disciplines = $this->api->get('/employee/disciplines')->json();
$types = $this->api->get('/employee/types')->json();
$teachers = $this->api->get('/employee/teachers')->json();
return view('statements.create', [
'groups' => $groups,
'disciplines' => $disciplines,
'types' => $types,
'teachers' => $teachers
]);
}
public function store(Request $request)
{
$response = $this->api->post('/employee/statements', $request->all());
if ($response->successful()) {
return redirect()->route('statements.index')
->with('success', 'Ведомость успешно создана');
}
return back()->withErrors($response->json()['errors'] ?? []);
}
public function edit($id)
{
$estimations = $this->api->get("/employee/estimations");
$statementResponse = $this->api->get("/employee/statements/{$id}");
$groupsResponse = $this->api->get('/employee/groups');
$disciplinesResponse = $this->api->get('/employee/disciplines');
$typesResponse = $this->api->get('/employee/types');
$teachersResponse = $this->api->get('/employee/teachers');
if ($statementResponse->successful() &&
$groupsResponse->successful() &&
$disciplinesResponse->successful() &&
$typesResponse->successful() &&
$teachersResponse->successful() &&
$estimations->successful()) {
return view('statements.form', [
'statement' => $statementResponse->json(),
'groups' => $groupsResponse->json(),
'disciplines' => $disciplinesResponse->json(),
'types' => $typesResponse->json(),
'teachers' => $teachersResponse->json(),
'isEdit' => true,
'estimations' => $estimations->json()
]);
}
$estimations2 = $this->api->get("/teacher/estimations");
$statementResponse2 = $this->api->get("/teacher/statements/{$id}");
$groupsResponse2 = $this->api->get('/teacher/groups');
$disciplinesResponse2 = $this->api->get('/teacher/disciplines');
$typesResponse2 = $this->api->get('/teacher/types');
$teachersResponse2 = $this->api->get('/teacher/teachers');
if ($statementResponse2->successful() &&
$groupsResponse2->successful() &&
$disciplinesResponse2->successful() &&
$typesResponse2->successful() &&
$teachersResponse2->successful() &&
$estimations2->successful()) {
return view('statements.form', [
'statement' => $statementResponse2->json(),
'groups' => $groupsResponse2->json(),
'disciplines' => $disciplinesResponse2->json(),
'types' => $typesResponse2->json(),
'teachers' => $teachersResponse2->json(),
'isEdit' => true,
'estimations' => $estimations2->json()
]);
}
abort($statementResponse->status() ?? $statementResponse2->status() ?? 500);
}
public function update(Request $request, $id)
{
$data = $request->all();
$data['is_finalized'] = $data['is_finalized'] ?? false;
Log::info('Updating statement data:', $data);
$response = $this->api->patch("/employee/statements/{$id}", $data);
if ($response->successful()) {
return redirect()->route('statements.index')
->with('success', 'Ведомость успешно обновлена');
}
Log::error('Failed to update statement:', [
'status' => $response->status(),
'response' => $response->json()
]);
return back()->withErrors($response->json()['errors'] ?? []);
}
public function destroy($id)
{
$response = $this->api->delete("/employee/statements/{$id}");
if ($response->successful()) {
return redirect()->route('statements.index')
->with('success', 'Ведомость удалена');
}
return back()->withErrors($response->json()['error'] ?? 'Ошибка при удалении');
}
public function search()
{
$disciplinesResponse = $this->api->get("/teacher/disciplines/my")->json();
$disciplines = $disciplinesResponse['data'] ?? [];
return view('teacher.statements.search', [
'disciplines' => $disciplines,
]);
}
/**
* Генерация PDF ведомости
*/
public function generatePdf(int $statementId, Request $request)
{
try {
$withGrades = $request->input('with_grades', true);
$response = $this->api->get("/employee/statements/{$statementId}/pdf", [
'with_grades' => $withGrades
]);
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'] ?? 'Ошибка при генерации PDF');
} catch (\Exception $e) {
return back()->withErrors('Ошибка при генерации PDF: ' . $e->getMessage());
}
}
/**
* Финализация ведомости
*/
public function finalize(int $statementId)
{
try {
$response = $this->api->post("/teacher/statements/{$statementId}/finalize");
if ($response->successful()) {
return redirect()->back()->with('success', 'Ведомость успешно финализирована');
}
return back()->withErrors($response->json()['error'] ?? 'Ошибка при финализации ведомости');
} catch (\Exception $e) {
return back()->withErrors('Ошибка при финализации ведомости: ' . $e->getMessage());
}
}
public function myStatements(Request $request)
{
try {
$disciplinesResponse = $this->api->get('/teacher/disciplines/my');
$disciplines = $disciplinesResponse->json()['data'] ?? [];
$apiParams = [
'academic_year' => $request->input('academic_year'),
'discipline_id' => $request->input('discipline_id'),
'group_id' => $request->input('group_id')
];
$statementsResponse = $this->api->get('/teacher/statements', $apiParams);
if (!$statementsResponse->successful()) {
throw new \Exception('Не удалось загрузить ведомости');
}
$groupsResponse = $this->api->get('/teacher/groups');
$groups = $groupsResponse->json()['data'] ?? $groupsResponse->json();
return view('teacher.statements.index', [
'statements' => $statementsResponse->json()['data'] ?? $statementsResponse->json(),
'disciplines' => $disciplines,
'filters' => $request->only(['academic_year', 'discipline_id', 'group_id']),
'groups' => $groups
]);
} catch (\Exception $e) {
Log::error('Ошибка в myStatements: ' . $e->getMessage());
return back()->with('error', $e->getMessage())->withInput();
}
}
}