<?php
namespace App\Controller;
use App\Entity\SimulSitePanneaux;
use App\Entity\SimulSitePanneauxAutoconso;
use App\Form\SimulPanneauxFormType;
use App\Service\Session;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class SimulPanneauxController extends AbstractController
{
/**
* @Route("/simulation_panneaux", name="simulPanneaux")
* @param Request $request
* @return Response
* @throws Exception
*/
public function new(Session $session,Request $request): Response
{
$session->Session($request);
$simul = new SimulSitePanneaux();
$random = sha1(random_bytes(10));
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->get('random_simul_id')->setData($random);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $form->getData() holds the submitted values
// but, the original `$task` variable has also been updated
$task = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $task->getRandomSimulId()]);
if ($verif != null) {
$verif->setHabitant($task->getHabitant());
$entityManager->persist($verif);
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux2', ['simul_id' => $verif->getId(), 'random_simul_id' => $task->getRandomSimulId()]);
} else {
// ... perform some action, such as saving the task to the database
// for example, if Task is a Doctrine entity, save it!
$entityManager->persist($task);
$entityManager->flush();
}
return $this->redirectToRoute('simulPanneaux2', ['simul_id' => $task->getId(), 'random_simul_id' => $task->getRandomSimulId()]);
}
return $this->render('simulPanneaux/new.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step2/{random_simul_id}", name="simulPanneaux2")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step2(Request $request, $simul_id, $random_simul_id): Response
{
$simul = new SimulSitePanneaux();
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
$simul_2 = $entityManager->getRepository(SimulSitePanneaux::class)->find($simul_id);
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$simul_2->setCp($task->getCp());
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux3', ['simul_id' => $simul_id, 'random_simul_id' => $random_simul_id]);
}
return $this->render('simulPanneaux/step2.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step3/{random_simul_id}", name="simulPanneaux3")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step3(Request $request, $simul_id, $random_simul_id): Response
{
$simul = new SimulSitePanneaux();
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$simul_2 = $entityManager->getRepository(SimulSitePanneaux::class)->find($simul_id);
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$simul_2->setDomicile($task->getDomicile());
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux4', ['simul_id' => $simul_id, 'random_simul_id' => $random_simul_id]);
}
return $this->render('simulPanneaux/step3.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step4/{random_simul_id}", name="simulPanneaux4")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step4(Request $request, $simul_id, $random_simul_id): Response
{
$simul = new SimulSitePanneaux();
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$simul_2 = $entityManager->getRepository(SimulSitePanneaux::class)->find($simul_id);
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$simul_2->setDateConstruction($task->getDateConstruction());
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux5', ['simul_id' => $simul_id, 'random_simul_id' => $random_simul_id]);
}
return $this->render('simulPanneaux/step4.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step5/{random_simul_id}", name="simulPanneaux5")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step5(Request $request, $simul_id, $random_simul_id): Response
{
$simul = new SimulSitePanneaux();
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$simul_2 = $entityManager->getRepository(SimulSitePanneaux::class)->find($simul_id);
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$simul_2->setChauffage($task->getChauffage());
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux6', ['simul_id' => $simul_id, 'random_simul_id' => $random_simul_id]);
}
return $this->render('simulPanneaux/step5.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step6/{random_simul_id}", name="simulPanneaux6")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step6(Request $request, $simul_id, $random_simul_id): Response
{
$simul = new SimulSitePanneaux();
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$simul_2 = $entityManager->getRepository(SimulSitePanneaux::class)->find($simul_id);
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$simul_2->setOrientationToiture($task->getOrientationToiture());
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux7', ['simul_id' => $simul_id, 'random_simul_id' => $random_simul_id]);
}
return $this->render('simulPanneaux/step6.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step7/{random_simul_id}", name="simulPanneaux7")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step7(Request $request, $simul_id, $random_simul_id): Response
{
$simul = new SimulSitePanneaux();
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$simul_2 = $entityManager->getRepository(SimulSitePanneaux::class)->find($simul_id);
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$simul_2->setSurfacePanToiture($task->getSurfacePanToiture());
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux8', ['simul_id' => $simul_id, 'random_simul_id' => $random_simul_id]);
}
return $this->render('simulPanneaux/step7.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step8/{random_simul_id}", name="simulPanneaux8")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step8(Request $request, $simul_id, $random_simul_id): Response
{
$simul = new SimulSitePanneaux();
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$simul_2 = $entityManager->getRepository(SimulSitePanneaux::class)->find($simul_id);
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$simul_2->setInclinaisonToiture($task->getInclinaisonToiture());
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux9', ['simul_id' => $simul_id, 'random_simul_id' => $random_simul_id]);
}
return $this->render('simulPanneaux/step8.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step9/{random_simul_id}", name="simulPanneaux9")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step9(Request $request, $simul_id, $random_simul_id): Response
{
$simul = new SimulSitePanneaux();
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$simul_2 = $entityManager->getRepository(SimulSitePanneaux::class)->find($simul_id);
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$simul_2->setConsoElecMois($task->getConsoElecMois());
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux12', ['simul_id' => $simul_id, 'random_simul_id' => $random_simul_id]);
}
return $this->render('simulPanneaux/step9.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step10/{random_simul_id}", name="simulPanneaux10")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step10(Request $request, $simul_id, $random_simul_id): Response
{
$simul = new SimulSitePanneaux();
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$simul_2 = $entityManager->getRepository(SimulSitePanneaux::class)->find($simul_id);
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$simul_2->setNom($task->getNom());
$simul_2->setPrenom($task->getPrenom());
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux11', ['simul_id' => $simul_id, 'random_simul_id' => $random_simul_id]);
}
return $this->render('simulPanneaux/step10.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step11/{random_simul_id}", name="simulPanneaux11")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step11(Request $request, $simul_id, $random_simul_id): Response
{
$simul = new SimulSitePanneaux();
$form = $this->createForm(SimulPanneauxFormType::class, $simul);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$simul_2 = $entityManager->getRepository(SimulSitePanneaux::class)->find($simul_id);
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData();
$simul_2->setMail($task->getMail());
$simul_2->setTel($task->getTel());
$entityManager->flush();
return $this->redirectToRoute('simulPanneaux12', ['simul_id' => $simul_id, 'random_simul_id' => $random_simul_id]);
}
return $this->render('simulPanneaux/step11.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/simulation_panneaux/{simul_id}/step12/{random_simul_id}", name="simulPanneaux12")
* @param Request $request
* @param $simul_id
* @param $random_simul_id
* @return Response
*/
public function step12(Request $request, $simul_id, $random_simul_id): Response
{
$entityManager = $this->getDoctrine()->getManager();
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id]);
if ($verif->getId() != $simul_id) {
throw $this->createNotFoundException('Tu ne devrais pas être ici !');
}
return $this->render('simulPanneaux/step12.html.twig', [
'id' => $simul_id, 'random_id' => $random_simul_id
]);
}
/**
* @Route("/simulation_panneaux/resultats", name="simulpanneauxloading")
*/
public function resultats(Request $request): Response
{
$id = $request->get("id");
$randomid = $request->get("randomid");
$response = array(
"code" => 200,
"id" => $id,
"randomid" => $randomid);
return new JsonResponse($response);
}
/**
* @Route("/simulation_panneaux/{simul_id}/affichage_resultat/{random_simul_id}", name="simulPanneauxResultats")
*/
public function affichage_resultat(Request $request, $simul_id, $random_simul_id): Response
{
$entityManager = $this->getDoctrine()->getManager();
$verif = $entityManager->getRepository(SimulSitePanneaux::class)->findOneBy(['random_simul_id' => $random_simul_id, 'id' => $simul_id]);
$orientation = $verif->getOrientationToiture();
$conso_elec_an = $verif->getConsoElecMois();
$autoconso = $entityManager->getRepository(SimulSitePanneauxAutoconso::class)->findOneBy(['conso_annuelle' => $conso_elec_an]);
$tx_autoconso_3kw = $autoconso->getTxAutoconso3kw();
$tx_autoconso_6kw = $autoconso->getTxAutoconso6kw();
/* Calcul des résultats de la simulation */
switch ($orientation) {
case "Est":
case "Ouest":
$prodkwc3 = 1056;
break;
case "Sud-Est":
case "Sud-Ouest":
$prodkwc3 = 1132;
break;
case "Sud":
$prodkwc3 = 1196;
break;
}
$production3 = 3 * $prodkwc3;
$production6 = 6 * $prodkwc3;
$tx_instal3 = $tx_autoconso_6kw + (($tx_autoconso_3kw - $tx_autoconso_6kw) * ((6 - 3) / 3));
$tx_instal6 = $tx_autoconso_6kw + (($tx_autoconso_3kw - $tx_autoconso_6kw) * ((6 - 6) / 3));
$kw_autoconso3 = round($production3 * ($tx_instal3 / 100));
$kw_autoconso6 = round($production6 * ($tx_instal6 / 100));
$kw_revente3 = $production3 - $kw_autoconso3;
$kw_revente6 = $production6 - $kw_autoconso6;
$economies_autoconso_3 = round($kw_autoconso3 * 0.1841);
$economies_autoconso_6 = round($kw_autoconso6 * 0.1841);
$elec_revendu_3 = round($kw_revente3 * 0.1);
$elec_revendu_6 = round($kw_revente6 * 0.1);
$total_eco_an_3 = $economies_autoconso_3 + $elec_revendu_3;
$total_eco_an_6 = $economies_autoconso_6 + $elec_revendu_6;
$prime_autoconso3 = 660; //3 * 220
$prime_autoconso6 = 960; //6 * 160
return $this->render('simulPanneaux/resultats.html.twig', [
'production3' => $production3,
'production6' => $production6,
'total_eco_an_3' => $total_eco_an_3,
'total_eco_an_6' => $total_eco_an_6,
'prime_autoconso3' => $prime_autoconso3,
'prime_autoconso6' => $prime_autoconso6,
'random_simul_id'=> $random_simul_id
]);
}
}