src/Controller/EntrepriseController.php line 157

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use App\Entity\Entreprise;
  11. use App\Entity\CommercialEntreprise;
  12. use App\Entity\Reseau;
  13. use App\Form\EntrepriseType;
  14. use App\Form\CommercialType;
  15. use App\Repository\EntrepriseRepository;
  16. /**
  17.  * @Route("/dashboard/entreprise")
  18.  */
  19. class EntrepriseController extends AbstractController
  20. {
  21.     private $entrepriseRepository;
  22.     private $entityManager;
  23.     public function __construct(
  24.         EntrepriseRepository $entrepriseRepository
  25.         EntityManagerInterface $entityManager
  26.     ) {
  27.         $this->entrepriseRepository $entrepriseRepository;
  28.         $this->entityManager $entityManager;
  29.     }
  30.     /**
  31.      * @Route("/", name="entreprise_index", methods={"GET"})
  32.      *
  33.      */
  34.     public function index(Request $request): Response
  35.     {
  36.         $results $this->entrepriseRepository->all($request->query->getInt('page'1));
  37.         return $this->render('entreprise/index.html.twig', [
  38.             'results' => $results
  39.         ]);
  40.     }
  41.     /**
  42.      * @Route("/recherche", name="entreprise_search", methods={"GET"})
  43.      */
  44.     public function search(Request $request): Response
  45.     {
  46.         $raisonSociale $request->query->get('raisonSociale');
  47.         $siret $request->query->get('siret');
  48.         $results $this->entrepriseRepository->search($raisonSociale$siret$request->query->getInt('page'1));
  49.         return $this->render('entreprise/index.html.twig', [
  50.             'results' => $results
  51.         ]);
  52.     }
  53.     /**
  54.      * @Route("/creation", name="entreprise_new_step1", methods={"GET", "POST"})
  55.      */
  56.     public function newStep1(Request $request): Response
  57.     {
  58.         $entreprise = new Entreprise();
  59.         $reseau $this->entityManager->getRepository(Reseau::class)->findFirst();
  60.         $reseauDivisions $reseau->getReseauDivisions();
  61.         $reseauDivision $reseauDivisions[0];
  62.         $entreprise->setReseau($reseau);
  63.         $entreprise->setReseauDivision($reseauDivision);
  64.         $formEntreprise $this->createForm(EntrepriseType::class, $entreprise);
  65.         $formEntreprise->handleRequest($request);
  66.         if ($formEntreprise->isSubmitted() && $formEntreprise->isValid()) {
  67.             $this->entityManager->persist($entreprise);
  68.             $this->entityManager->flush();
  69.             return $this->redirectToRoute('entreprise_new_reseau_commercial', ['id' => $entreprise->getId()], Response::HTTP_SEE_OTHER);
  70.         }
  71.         
  72.         return $this->renderForm('entreprise/new.html.twig', [
  73.             'entreprise' => $entreprise,
  74.             'formEntreprise' => $formEntreprise,
  75.         ]);
  76.     }
  77.     /**
  78.      * @Route("/creation/{id}/reseau_commercial", name="entreprise_new_reseau_commercial", methods={"GET", "POST"})
  79.      * @Entity("entreprise", expr="repository.find(id)")
  80.      */
  81.     public function newStep2(Request $requestEntreprise $entreprise): Response
  82.     {
  83.         $formCommercial $this->createForm(CommercialType::class, $entreprise);
  84.         $formCommercial->handleRequest($request);
  85.         if ($formCommercial->isSubmitted() && $formCommercial->isValid()) {
  86.             
  87.             $commercialEntreprise = new CommercialEntreprise();
  88.             $commercialEntreprise->setEntreprise($entreprise);
  89.             
  90.             if($entreprise->getReseau()->getSaisieProducteur()) {
  91.                 // Producteur librement renseigné
  92.                 $producteurCode $entreprise->getProducteurCode();
  93.                 $nom $entreprise->getProducteurNom();
  94.                 $prenom $entreprise->getProducteurPrenom();
  95.                 $email $entreprise->getProducteurEmail();
  96.                 $telephone $entreprise->getProducteurTelephone(); 
  97.             } else {
  98.                 // Producteur en provenance de la table commercial
  99.                 $producteurCode $entreprise->getCommercial()->getCode();
  100.                 $nom =  $entreprise->getCommercial()->getNom();
  101.                 $prenom $entreprise->getCommercial()->getPrenom();
  102.                 $email $entreprise->getCommercial()->getEmail();
  103.                 $telephone $entreprise->getCommercial()->getTelephone();
  104.             }
  105.             $commercialEntreprise->setProducteurCode($producteurCode);
  106.             $commercialEntreprise->setNom($nom);
  107.             $commercialEntreprise->setPrenom($prenom);
  108.             $commercialEntreprise->setEmail($email);
  109.             $commercialEntreprise->setTelephone($telephone);
  110.             $this->entityManager->persist($commercialEntreprise);
  111.             $this->entityManager->flush();
  112.             return $this->redirectToRoute('entreprise_index', [], Response::HTTP_SEE_OTHER);
  113.         }
  114.         return $this->renderForm('entreprise/new_reseau.html.twig', [
  115.             'entreprise' => $entreprise,
  116.             'formCommercial' => $formCommercial,
  117.         ]);
  118.     }
  119.     /**
  120.      * @Route("/{id}", name="entreprise_show", methods={"GET"})
  121.      * @Entity("entreprise", expr="repository.find(id)")
  122.      */
  123.     public function show(Entreprise $entreprise): Response
  124.     {
  125.         return $this->render('entreprise/show.html.twig', [
  126.             'entreprise' => $entreprise,
  127.         ]);
  128.     }
  129.     /**
  130.      * @Route("/{id}/edit", name="entreprise_edit", methods={"GET", "POST"})
  131.      * @Entity("entreprise", expr="repository.find(id)")
  132.      */
  133.     public function edit(Request $requestEntreprise $entreprise): Response
  134.     {
  135.         $formEntreprise $this->createForm(EntrepriseType::class, $entreprise);
  136.         $formEntreprise->handleRequest($request);
  137.         if ($formEntreprise->isSubmitted() && $formEntreprise->isValid()) {
  138.             $this->entityManager->flush();
  139.         }
  140.         $commercialEntreprise $this->entityManager->getRepository(CommercialEntreprise::class)->findOneBy(['entreprise' => $entreprise]);
  141.         if($commercialEntreprise instanceof CommercialEntreprise) {
  142.             $entreprise->setProducteurCode($commercialEntreprise->getProducteurCode());
  143.             $entreprise->setProducteurCodeIsWaiting($commercialEntreprise->getProducteurCodeIsWaiting());
  144.             $entreprise->setProducteurNom($commercialEntreprise->getNom());
  145.             $entreprise->setProducteurPrenom($commercialEntreprise->getPrenom());
  146.             $entreprise->setProducteurEmail($commercialEntreprise->getEmail());
  147.             $entreprise->setProducteurTelephone($commercialEntreprise->getTelephone()); 
  148.         }
  149.         $formCommercial $this->createForm(CommercialType::class, $entreprise);
  150.         $formCommercial->handleRequest($request);
  151.         if ($formCommercial->isSubmitted() && $formCommercial->isValid()) {
  152.             $reseau $entreprise->getReseau();
  153.             if(is_null($entreprise->getCommercialEntreprise())) {
  154.                 $commercialEntreprise = new CommercialEntreprise();
  155.                 $commercialEntreprise->setEntreprise($entreprise);
  156.             } else {
  157.                 $commercialEntreprise $entreprise->getCommercialEntreprise();
  158.             }
  159.             if($reseau->getSaisieProducteur()) {
  160.                 // Producteur librement renseigné
  161.                 $producteurCode $entreprise->getProducteurCode();
  162.                 $producteurCodeIsWaiting $entreprise->getProducteurCodeIsWaiting();
  163.                 $nom $entreprise->getProducteurNom();
  164.                 $prenom $entreprise->getProducteurPrenom();
  165.                 $email $entreprise->getProducteurEmail();
  166.                 $telephone $entreprise->getProducteurTelephone(); 
  167.             } else {
  168.                 // Producteur en provenance de la table commercial
  169.                 $producteurCode $entreprise->getCommercial()->getCode();
  170.                 $producteurCodeIsWaiting false;
  171.                 $nom =  $entreprise->getCommercial()->getNom();
  172.                 $prenom $entreprise->getCommercial()->getPrenom();
  173.                 $email $entreprise->getCommercial()->getEmail();
  174.                 $telephone $entreprise->getCommercial()->getTelephone();
  175.             }
  176.             $commercialEntreprise->setProducteurCodeIsWaiting($producteurCodeIsWaiting);
  177.             $commercialEntreprise->setProducteurCode($producteurCode);
  178.             $commercialEntreprise->setNom($nom);
  179.             $commercialEntreprise->setPrenom($prenom);
  180.             $commercialEntreprise->setEmail($email);
  181.             $commercialEntreprise->setTelephone($telephone);
  182.             $this->entityManager->persist($commercialEntreprise);
  183.             $this->entityManager->flush();
  184.         }
  185.         return $this->renderForm('entreprise/edit.html.twig', [
  186.             'entreprise' => $entreprise,
  187.             'formEntreprise' => $formEntreprise,
  188.             'formCommercial' => $formCommercial,
  189.         ]);
  190.         
  191.     }
  192.     /**
  193.      * @Route("/{id}", name="entreprise_delete", methods={"POST"})
  194.      * @Entity("entreprise", expr="repository.find(id)")
  195.      */
  196.     public function delete(Request $requestEntreprise $entreprise): Response
  197.     {
  198.         if ($this->isCsrfTokenValid('delete'.$entreprise->getId(), $request->request->get('_token'))) {
  199.             $entreprise->setReseau(null);
  200.             $entreprise->setReseauDivision(null);
  201.             $this->entityManager->persist($entreprise);
  202.             $this->entityManager->remove($entreprise);
  203.             $this->entityManager->flush();
  204.         } 
  205.         return $this->redirectToRoute('entreprise_index', [], Response::HTTP_SEE_OTHER);
  206.     }
  207. }