<?php
/**
* Created by PhpStorm.
* User: vad
* Date: 8/29/18
* Time: 8:28 PM
*/
namespace App\Controller\Registry;
use App\Entity\Institution;
use App\Repository\AppointmentRepository;
use App\Repository\DepartmentRepository;
use App\Repository\DoctorRepository;
use App\Repository\InstitutionRepository;
use App\Repository\PlaceRepository;
use DateTimeImmutable;
use Exception;
use Mpdf\Mpdf;
use Mpdf\MpdfException;
use Mpdf\Output\Destination;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
class RegistryController extends AbstractController
{
/**
* @Route("", methods={"GET"}, name="homepage")
* @Template("registry/patient-appointment.html.twig")
*/
public function showAction(Request $request,
DoctorRepository $doctorRepository,
DepartmentRepository $departmentRepository,
InstitutionRepository $institutionRepository,
PlaceRepository $placeRepository,
SessionInterface $session): array
{
$doctorId = $request->query->get('doctor-id');
$departmentId = $request->query->get('department-id');
$institutionId = $request->query->get('institution-id');
$placeId = $request->query->get('place-id');
$appointmentDate = $request->query->get('appointment-date');
if ($session->has('doctor-id')) {
if (is_null($doctorId)) {
$doctorId = $session->get('doctor-id');
}
$session->remove('doctor-id');
}
if ($session->has('appointment-date')) {
if (is_null($appointmentDate)) {
$appointmentDate = $session->get('appointment-date');
}
$session->remove('appointment-date');
}
if (!is_null($doctorId)) {
$doctor = $doctorRepository->find($doctorId);
if (!is_null($doctor)) {
$department = $doctor->getDepartment();
$departmentId = $department->getId();
$institution = $department->getInstitution();
$institutionId = $institution->getId();
$placeId = $institution->getPlace()->getId();
} else {
$doctorId = null;
}
} else if (!is_null($departmentId)) {
$department = $departmentRepository->find($departmentId);
if (!is_null($department)) {
$institution = $department->getInstitution();
$institutionId = $institution->getId();
$placeId = $institution->getPlace()->getId();
} else {
$departmentId = null;
}
} else if (!is_null($institutionId)) {
$institution = $institutionRepository->find($institutionId);
if (!is_null($institution)) {
$placeId = $institution->getPlace()->getId();
} else {
$institutionId = null;
}
} else if (!is_null($placeId)) {
$place = $placeRepository->find($placeId);
if (is_null($place)) {
$placeId = null;
}
}
if ((!is_null($appointmentDate)) &&
(is_null($doctorId) || ($appointmentDate !== DateTimeImmutable::createFromFormat('!Y-m-d', $appointmentDate)->format('Y-m-d')))) {
$appointmentDate = null;
}
return [
'placeId' => $placeId,
'institutionId' => $institutionId,
'departmentId' => $departmentId,
'doctorId' => $doctorId,
'appointmentDate' => $appointmentDate,
];
}
/**
* @Route("appointment-by-institution/{id}", methods={"GET"}, name="patient-appointment-by-institution")
* @Template("registry/patient-appointment.html.twig")
*/
public function appointmentByInstitutionAction(Institution $institution): array
{
return [
'placeId' => $institution->getPlace()->getId(),
'institutionId' => $institution->getId(),
];
}
/**
* @Route("/sign-up", methods={"GET"}, name="sign-up")
* @Template("registry/sign-up.html.twig")
*/
public function signUpAction(Request $request,
SessionInterface $session): array
{
if ($request->query->has('doctor-id')) {
$session->set('doctor-id', $request->query->get('doctor-id'));
}
if ($request->query->has('appointment-date')) {
$session->set('appointment-date', $request->query->get('appointment-date'));
}
return [];
}
/**
* @Route("/appointment-info-pdf/{appointmentId}", methods={"GET"}, name="appointment-info-pdf")
* @throws MpdfException
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
* @throws Exception
*/
public function getAppointmentInfoPdfAction($appointmentId, AppointmentRepository $appointmentRepository): Response
{
$appointmentInfo = $appointmentRepository->getAppointmentInfo($appointmentId);
$pdf = new Mpdf(['tempDir' => $this->getParameter('kernel.project_dir') . '/var/temp/mpdf']);
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 10, 'Запис на прийом', 0, 1);
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(0, 10,
'Лікар: ' .
$appointmentInfo['doctor_last_name'] . ' ' .
$appointmentInfo['doctor_first_name'] . ' ' .
$appointmentInfo['doctor_second_name'],
0, 1);
$appointmentDate = new DateTimeImmutable($appointmentInfo['appointment_date']);
$appointmentTime = new DateTimeImmutable($appointmentInfo['appointment_time']);
$pdf->Cell(0, 10,
'Дата: ' . $appointmentDate->format('d.m.Y') . ', ' .
'Час: ' . $appointmentTime->format('H:i') . ', ' .
'Кабінет: ' . $appointmentInfo['cabinet_number'],
0, 1);
if ($appointmentInfo['registry_phone_number']) {
$pdf->Cell(0, 10, 'Телефон реєстратури: ' .
$appointmentInfo['registry_phone_number'], 0, 1);
}
return new Response(
$pdf->Output('appointment.pdf', Destination::INLINE),
Response::HTTP_OK,
array('content-type' => 'application/pdf')
);
}
/**
* @Route("/restore-password", methods={"GET"}, name="restore-password")
* @Template("registry/restore-password.html.twig")
*/
public function restorePasswordAction(): array
{
return [];
}
}