<?php
namespace App\Controller\Registration;
use Knp\Snappy\Pdf;
use App\Entity\User;
use App\Service\SMSSender;
use App\Entity\SettingLevel;
use App\Entity\Establishment;
use App\Repository\SettingLevelRepository;
use App\Repository\EstablishmentRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Common\Collections\ArrayCollection;
use App\Repository\RegistrationStudentRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\RegistrationStudentPreRegistration;
use App\Entity\SchoolYear;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use App\Repository\RegistrationStudentPreRegistrationRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Form\Registration\PrimaireRegistrationStudentPreRegistrationType;
use App\Form\Registration\SecondaireRegistrationStudentPreRegistrationType;
use App\Repository\SchoolYearRepository;
/**
* @Route("/online")
*/
class PreRegistrationController extends AbstractController
{
/**
* @Route("/pre-registration", name="pre_registration", methods={"GET","POST"})
*/
public function index(EstablishmentRepository $establishmentRepository): Response
{
return $this->render('pre_registration/index.html.twig', [
'establishments' => $establishmentRepository->findBy([],['name' => 'ASC']),
]);
}
/**
* @Route("/pre-registration/verification/{id}", name="pre_registration_verification", methods={"GET","POST"})
*/
public function verification(Establishment $establishment): Response
{
$template = 'pre_registration/verification.html.twig';
return $this->render($template, [
'establishment' => $establishment,
]);
}
/**
* @Route("/pre-registration/etablissement/{id}", name="pre_registration_ets", methods={"GET","POST"})
*/
public function preRegistration(Request $request, Establishment $establishment, RegistrationStudentRepository $registrationStudentRepository,SMSSender $smsSender, RegistrationStudentPreRegistrationRepository $registrationStudentPreRegistrationRepository, SettingLevelRepository $settingLevelRepository, SchoolYearRepository $schoolYearRepository): Response
{
/**@var User $user */
$user = $this->getUser();
//$schoolYear = $user->getSchoolYear();
$template = 'pre_registration/newPrimaire.html.twig';
if ($establishment->getType() != $establishment::ESTABLISHMENT_PRESCOLAIRE_PRIMAIRE_TYPES) {
$template = 'pre_registration/newSecondaire.html.twig';
}
$registrationStudent = $registrationStudentRepository->findOneBy(['establishment' => $establishment, 'registration_number' => $request->get('matricule'), 'is_archived' => 0], []);
if (null == $registrationStudent) {
$registrationStudent = $registrationStudentRepository->findOneBy(['establishment' => $establishment, 'code' => $request->get('matricule'), 'is_archived' => 0], []);
}
if ($request->get('type') == 'old' && $registrationStudent == null) {
$this->addFlash('warning', "Matricule incorrect");
return $this->redirectToRoute('pre_registration_verification', ['id' => $establishment->getId()]);
}
$registrationStudentPreRegistration = new RegistrationStudentPreRegistration();
if ($registrationStudent == null) {
$registrationStudentPreRegistration->setRegistrationNumber('AUCUN');
$registrationStudentPreRegistration->setLastSchoolAttended('AUCUN');
}else {
$registrationStudentPreRegistration->setHoldStudentId($registrationStudent->getId());
$registrationStudentPreRegistration->setAccompanyingPersonName($registrationStudent->getAccompanyingPersonName());
$registrationStudentPreRegistration->setAccompanyingPersonPhoneNumber($registrationStudent->getAccompanyingPersonPhoneNumber());
$registrationStudentPreRegistration->setFatherAddress($registrationStudent->getFatherAddress());
$registrationStudentPreRegistration->setFatherEmail($registrationStudent->getFatherEmail());
$registrationStudentPreRegistration->setFatherFirstName($registrationStudent->getFatherFirstName());
$registrationStudentPreRegistration->setFatherLastName($registrationStudent->getFatherLastName());
$registrationStudentPreRegistration->setFatherPhoneNumber($registrationStudent->getFatherPhoneNumber());
$registrationStudentPreRegistration->setIsFatherCallToEmergency($registrationStudent->getIsFatherCallToEmergency());
$registrationStudentPreRegistration->setIsParntalAuthorityFather($registrationStudent->getIsParntalAuthorityFather());
$registrationStudentPreRegistration->setFatherLocation($registrationStudent->getFatherLocation());
$registrationStudentPreRegistration->setFatherPost($registrationStudent->getFatherPost());
$registrationStudentPreRegistration->setMotherAddress($registrationStudent->getMotherAddress());
$registrationStudentPreRegistration->setMotherEmail($registrationStudent->getMotherEmail());
$registrationStudentPreRegistration->setMotherFirstName($registrationStudent->getMotherFirstName());
$registrationStudentPreRegistration->setMotherLastName($registrationStudent->getMotherLastName());
$registrationStudentPreRegistration->setMotherPhoneNumber($registrationStudent->getMotherPhoneNumber());
$registrationStudentPreRegistration->setIsMotherCallToEmergency($registrationStudent->getIsMotherCallToEmergency());
$registrationStudentPreRegistration->setIsParntalAuthorityMother($registrationStudent->getIsParntalAuthorityMother());
$registrationStudentPreRegistration->setMotherLocation($registrationStudent->getMotherLocation());
$registrationStudentPreRegistration->setMotherPost($registrationStudent->getMotherPost());
$registrationStudentPreRegistration->setFirstName($registrationStudent->getFirstName());
$registrationStudentPreRegistration->setLastName($registrationStudent->getLastName());
$registrationStudentPreRegistration->setImage($registrationStudent->getImage());
$registrationStudentPreRegistration->setIsAffected($registrationStudent->getIsAffected());
$registrationStudentPreRegistration->setLocation($registrationStudent->getLocation());
$registrationStudentPreRegistration->setLv1($registrationStudent->getLv1());
$registrationStudentPreRegistration->setLv2($registrationStudent->getLv2());
$registrationStudentPreRegistration->setNationality($registrationStudent->getNationality());
$registrationStudentPreRegistration->setBirthday($registrationStudent->getBirthday());
$registrationStudentPreRegistration->setBirthLocation($registrationStudent->getBirthLocation());
$registrationStudentPreRegistration->setBirthCertificateNumber($registrationStudent->getBirthCertificateNumber());
$registrationStudentPreRegistration->setRegistrationNumber($registrationStudent->getRegistrationNumber());
$registrationStudentPreRegistration->setLastSchoolAttended($registrationStudent->getLastSchoolAttended());
$registrationStudentPreRegistration->setLastSchoolAttendedType($registrationStudent->getLastSchoolAttendedType());
$registrationStudentPreRegistration->setNotificationWatsappNumber($registrationStudent->getNotificationWatsappNumber());
$registrationStudentPreRegistration->setNotificationPhoneNumber($registrationStudent->getNotificationPhoneNumber());
$registrationStudentPreRegistration->setObservation($registrationStudent->getObservation());
$registrationStudentPreRegistration->setParentStatus($registrationStudent->getParentStatus());
$registrationStudentPreRegistration->setResponsibleOfSchooling($registrationStudent->getResponsibleOfSchooling());
$registrationStudentPreRegistration->setResponsibleOfSchoolingAddress($registrationStudent->getResponsibleOfSchoolingAddress());
$registrationStudentPreRegistration->setResponsibleOfSchoolingFirstName($registrationStudent->getResponsibleOfSchoolingFirstName());
$registrationStudentPreRegistration->setResponsibleOfSchoolingLastName($registrationStudent->getResponsibleOfSchoolingLastName());
$registrationStudentPreRegistration->setResponsibleOfSchoolingLocation($registrationStudent->getResponsibleOfSchoolingLocation());
$registrationStudentPreRegistration->setResponsibleOfSchoolingPhoneNumber($registrationStudent->getResponsibleOfSchoolingPhoneNumber());
$registrationStudentPreRegistration->setResponsibleOfSchoolingPost($registrationStudent->getResponsibleOfSchoolingPost());
}
$entryLevels = new ArrayCollection();
foreach ($establishment->getSettingLevels() as $key => $level) {
if (count($level->getSettingClassrooms()) > 0) {
$entryLevels->add($level);
}
}
$form = $this->createForm(PrimaireRegistrationStudentPreRegistrationType::class, $registrationStudentPreRegistration)
->add('entryLevel', EntityType::class, [
'class' => SettingLevel::class,
'choices' => $entryLevels,
'required' => true
])
->add('classJumps', EntityType::class, [
'class' => SettingLevel::class,
'choices' => $entryLevels,
'required' => false,
'multiple' => true,
])
->add('schoolYear', EntityType::class, [
'class' => SchoolYear::class,
'choices' => $schoolYearRepository->findBy([], ['id' => 'DESC']),
'required' => true,
]);
if ($establishment->getType() != $establishment::ESTABLISHMENT_PRESCOLAIRE_PRIMAIRE_TYPES) {
$form = $this->createForm(SecondaireRegistrationStudentPreRegistrationType::class, $registrationStudentPreRegistration)
->add('entryLevel', EntityType::class, [
'class' => SettingLevel::class,
'choices' => $entryLevels,
'required' => true
])
->add('schoolYear', EntityType::class, [
'class' => SchoolYear::class,
'choices' => $schoolYearRepository->findBy([], ['id' => 'DESC']),
'required' => true,
]);
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$nextIndex = count($registrationStudentPreRegistrationRepository->findBy([], [])) + 1;
$registrationStudentPreRegistration->setCode('PRE-'.date('Y').'-'.sprintf("%'05s", $nextIndex));
$registrationStudentPreRegistration->setEstablishment($establishment);
$registrationStudentPreRegistration->setToxicologicalTestResult('EN COURS');
$registrationStudentPreRegistration->setPsychologicalTestResult('EN COURS');
$registrationStudentPreRegistration->setEntryTestResult('EN COURS');
//$registrationStudentPreRegistration->setSchoolYear($currentYear);
if ($registrationStudentPreRegistration->getEntryLevel()->getIsStudentEntryTestDo() && null == $registrationStudent) {
$registrationStudentPreRegistration->setIsSubjectEntryTest(true);
}
if ($registrationStudentPreRegistration->getEntryLevel()->getIsStudentPsychologicalTestDo() && null == $registrationStudent) {
$registrationStudentPreRegistration->setIsSubjectPsychologicalTest(true);
}
if ($registrationStudentPreRegistration->getEntryLevel()->getIsStudentToxicologicalTestDo()) {
$registrationStudentPreRegistration->setIsSubjectToxicologicalTest(true);
}
/* 28/07/2022 ajout d'une nouvelle condition verifiant que tous les niveaux de l'établissement soient soumis au test */
if (count($settingLevelRepository->findBy(['establishment' => $registrationStudentPreRegistration->getEstablishment(), 'is_student_toxicological_test_do' => 1], [])) > 0) {
if (!$registrationStudentPreRegistration->getEntryLevel()->getIsStudentToxicologicalTestDo() && $registrationStudentPreRegistration->getIsRedoubling()) {
$registrationStudentPreRegistration->setIsSubjectToxicologicalTest(true);
}
}
$entityManager->persist($registrationStudentPreRegistration);
$message = "";
$contacts = [];
$contacts[] = '225'.str_replace(' ', '',trim($registrationStudentPreRegistration->getNotificationPhoneNumber()));
try {
if ($registrationStudentPreRegistration->getIsSubjectEntryTest()) {
$message = "BIENVENUE, Votre pre-inscription a été effectuée avec succès, merci de vous présenter pour le test d'entrée avec le code: %s. Contact: %s";
}
if ($registrationStudentPreRegistration->getIsSubjectPsychologicalTest()) {
$message = "BIENVENUE, Votre pre-inscription a été effectuée avec succès, merci de vous présenter pour le test Psychologique avec le code: %s. Contact: %s";
}
if ($registrationStudentPreRegistration->getIsSubjectToxicologicalTest()) {
$message = "BIENVENUE, Votre pre-inscription a été effectuée avec succès, merci de vous présenter pour le Test Toxicologique avec le code: %s. Contact: %s";
}
if (!$registrationStudentPreRegistration->getIsSubjectToxicologicalTest() && !$registrationStudentPreRegistration->getIsSubjectPsychologicalTest() && !$registrationStudentPreRegistration->getIsSubjectEntryTest()) {
$message = "BIENVENUE, Votre pre-inscription a été effectuée avec succès, merci de vous présenter au Service Accueil avec le code: %s. Contact: %s";
}
$entityManager->flush();
$registrationStudentPreRegistration->setCode('PRE-'.date('Y').'-'.sprintf("%'05s", $registrationStudentPreRegistration->getId()));
$message = sprintf($message, $registrationStudentPreRegistration->getCode(), $registrationStudentPreRegistration->getEstablishment()->getMobileNumber());
$response = $smsSender->sendSmsByEstablishment($establishment, $message, $contacts, $smsSender::UNICODE_CHARSET);
$entityManager->flush();
$this->addFlash('success', $message);
} catch (\Throwable $th) {
$this->addFlash('warning', "Le serveur a rencontré un problème, veuillez rééssayer plus tard.");
$this->addFlash('info', $th->getMessage());
return $this->redirectToRoute('pre_registration_ets', ['id' => $establishment->getId()]);
}
return $this->redirectToRoute('pre_registration_result', ['id' => $registrationStudentPreRegistration->getId()]);
}
return $this->renderForm($template, [
'registration_student_pre_pegistration' => $registrationStudentPreRegistration,
'establishment' => $establishment,
'form' => $form,
]);
}
/**
* @Route("/pre-registration/result/{id}", name="pre_registration_result", methods={"GET"})
*/
public function preRegistrationResult(RegistrationStudentPreRegistration $registrationStudentPreRegistration): Response
{
return $this->renderForm('pre_registration/result.html.twig', [
'pregistration_student_pre_pegistration' => $registrationStudentPreRegistration,
]);
}
/**
* @Route("/pre-registration/{id}/print", name="online_registration_student_pre_registration_print", methods={"GET"})
*/
public function print(Pdf $knpSnappyPdf, RegistrationStudentPreRegistration $registrationStudentPreRegistration)
{
/**@var User $user */
$user = $this->getUser();
//$schoolYear = $user->getSchoolYear();
$setting = $registrationStudentPreRegistration->getEstablishment();
$template = 'registration/print/prePrimaire.html.twig';
if ($setting->getType() != $setting::ESTABLISHMENT_PRESCOLAIRE_PRIMAIRE_TYPES) {
$template = 'registration/print/preSecondaire.html.twig';
}
$html = $this->renderView($template, [
'registration_student_pre_registration' => $registrationStudentPreRegistration,
'school_year' => $registrationStudentPreRegistration->getSchoolYear(),
'setting' => $setting,
]);
$file_name = 'PRE_INSCRIPTION_N_'.$registrationStudentPreRegistration->getCode().".pdf";
return new PdfResponse(
$knpSnappyPdf->getOutputFromHtml($html),
$file_name,
'application/pdf',
'attachment'
);
}
}