<?php
namespace App\Controller;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Entreprise;
use App\Entity\CommercialEntreprise;
use App\Entity\Reseau;
use App\Form\EntrepriseType;
use App\Form\CommercialType;
use App\Repository\EntrepriseRepository;
/**
* @Route("/dashboard/entreprise")
*/
class EntrepriseController extends AbstractController
{
private $entrepriseRepository;
private $entityManager;
public function __construct(
EntrepriseRepository $entrepriseRepository,
EntityManagerInterface $entityManager
) {
$this->entrepriseRepository = $entrepriseRepository;
$this->entityManager = $entityManager;
}
/**
* @Route("/", name="entreprise_index", methods={"GET"})
*
*/
public function index(Request $request): Response
{
$results = $this->entrepriseRepository->all($request->query->getInt('page', 1));
return $this->render('entreprise/index.html.twig', [
'results' => $results
]);
}
/**
* @Route("/recherche", name="entreprise_search", methods={"GET"})
*/
public function search(Request $request): Response
{
$raisonSociale = $request->query->get('raisonSociale');
$siret = $request->query->get('siret');
$results = $this->entrepriseRepository->search($raisonSociale, $siret, $request->query->getInt('page', 1));
return $this->render('entreprise/index.html.twig', [
'results' => $results
]);
}
/**
* @Route("/creation", name="entreprise_new_step1", methods={"GET", "POST"})
*/
public function newStep1(Request $request): Response
{
$entreprise = new Entreprise();
$reseau = $this->entityManager->getRepository(Reseau::class)->findFirst();
$reseauDivisions = $reseau->getReseauDivisions();
$reseauDivision = $reseauDivisions[0];
$entreprise->setReseau($reseau);
$entreprise->setReseauDivision($reseauDivision);
$formEntreprise = $this->createForm(EntrepriseType::class, $entreprise);
$formEntreprise->handleRequest($request);
if ($formEntreprise->isSubmitted() && $formEntreprise->isValid()) {
$this->entityManager->persist($entreprise);
$this->entityManager->flush();
return $this->redirectToRoute('entreprise_new_reseau_commercial', ['id' => $entreprise->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('entreprise/new.html.twig', [
'entreprise' => $entreprise,
'formEntreprise' => $formEntreprise,
]);
}
/**
* @Route("/creation/{id}/reseau_commercial", name="entreprise_new_reseau_commercial", methods={"GET", "POST"})
* @Entity("entreprise", expr="repository.find(id)")
*/
public function newStep2(Request $request, Entreprise $entreprise): Response
{
$formCommercial = $this->createForm(CommercialType::class, $entreprise);
$formCommercial->handleRequest($request);
if ($formCommercial->isSubmitted() && $formCommercial->isValid()) {
$commercialEntreprise = new CommercialEntreprise();
$commercialEntreprise->setEntreprise($entreprise);
if($entreprise->getReseau()->getSaisieProducteur()) {
// Producteur librement renseigné
$producteurCode = $entreprise->getProducteurCode();
$nom = $entreprise->getProducteurNom();
$prenom = $entreprise->getProducteurPrenom();
$email = $entreprise->getProducteurEmail();
$telephone = $entreprise->getProducteurTelephone();
} else {
// Producteur en provenance de la table commercial
$producteurCode = $entreprise->getCommercial()->getCode();
$nom = $entreprise->getCommercial()->getNom();
$prenom = $entreprise->getCommercial()->getPrenom();
$email = $entreprise->getCommercial()->getEmail();
$telephone = $entreprise->getCommercial()->getTelephone();
}
$commercialEntreprise->setProducteurCode($producteurCode);
$commercialEntreprise->setNom($nom);
$commercialEntreprise->setPrenom($prenom);
$commercialEntreprise->setEmail($email);
$commercialEntreprise->setTelephone($telephone);
$this->entityManager->persist($commercialEntreprise);
$this->entityManager->flush();
return $this->redirectToRoute('entreprise_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('entreprise/new_reseau.html.twig', [
'entreprise' => $entreprise,
'formCommercial' => $formCommercial,
]);
}
/**
* @Route("/{id}", name="entreprise_show", methods={"GET"})
* @Entity("entreprise", expr="repository.find(id)")
*/
public function show(Entreprise $entreprise): Response
{
return $this->render('entreprise/show.html.twig', [
'entreprise' => $entreprise,
]);
}
/**
* @Route("/{id}/edit", name="entreprise_edit", methods={"GET", "POST"})
* @Entity("entreprise", expr="repository.find(id)")
*/
public function edit(Request $request, Entreprise $entreprise): Response
{
$formEntreprise = $this->createForm(EntrepriseType::class, $entreprise);
$formEntreprise->handleRequest($request);
if ($formEntreprise->isSubmitted() && $formEntreprise->isValid()) {
$this->entityManager->flush();
}
$commercialEntreprise = $this->entityManager->getRepository(CommercialEntreprise::class)->findOneBy(['entreprise' => $entreprise]);
if($commercialEntreprise instanceof CommercialEntreprise) {
$entreprise->setProducteurCode($commercialEntreprise->getProducteurCode());
$entreprise->setProducteurCodeIsWaiting($commercialEntreprise->getProducteurCodeIsWaiting());
$entreprise->setProducteurNom($commercialEntreprise->getNom());
$entreprise->setProducteurPrenom($commercialEntreprise->getPrenom());
$entreprise->setProducteurEmail($commercialEntreprise->getEmail());
$entreprise->setProducteurTelephone($commercialEntreprise->getTelephone());
}
$formCommercial = $this->createForm(CommercialType::class, $entreprise);
$formCommercial->handleRequest($request);
if ($formCommercial->isSubmitted() && $formCommercial->isValid()) {
$reseau = $entreprise->getReseau();
if(is_null($entreprise->getCommercialEntreprise())) {
$commercialEntreprise = new CommercialEntreprise();
$commercialEntreprise->setEntreprise($entreprise);
} else {
$commercialEntreprise = $entreprise->getCommercialEntreprise();
}
if($reseau->getSaisieProducteur()) {
// Producteur librement renseigné
$producteurCode = $entreprise->getProducteurCode();
$producteurCodeIsWaiting = $entreprise->getProducteurCodeIsWaiting();
$nom = $entreprise->getProducteurNom();
$prenom = $entreprise->getProducteurPrenom();
$email = $entreprise->getProducteurEmail();
$telephone = $entreprise->getProducteurTelephone();
} else {
// Producteur en provenance de la table commercial
$producteurCode = $entreprise->getCommercial()->getCode();
$producteurCodeIsWaiting = false;
$nom = $entreprise->getCommercial()->getNom();
$prenom = $entreprise->getCommercial()->getPrenom();
$email = $entreprise->getCommercial()->getEmail();
$telephone = $entreprise->getCommercial()->getTelephone();
}
$commercialEntreprise->setProducteurCodeIsWaiting($producteurCodeIsWaiting);
$commercialEntreprise->setProducteurCode($producteurCode);
$commercialEntreprise->setNom($nom);
$commercialEntreprise->setPrenom($prenom);
$commercialEntreprise->setEmail($email);
$commercialEntreprise->setTelephone($telephone);
$this->entityManager->persist($commercialEntreprise);
$this->entityManager->flush();
}
return $this->renderForm('entreprise/edit.html.twig', [
'entreprise' => $entreprise,
'formEntreprise' => $formEntreprise,
'formCommercial' => $formCommercial,
]);
}
/**
* @Route("/{id}", name="entreprise_delete", methods={"POST"})
* @Entity("entreprise", expr="repository.find(id)")
*/
public function delete(Request $request, Entreprise $entreprise): Response
{
if ($this->isCsrfTokenValid('delete'.$entreprise->getId(), $request->request->get('_token'))) {
$entreprise->setReseau(null);
$entreprise->setReseauDivision(null);
$this->entityManager->persist($entreprise);
$this->entityManager->remove($entreprise);
$this->entityManager->flush();
}
return $this->redirectToRoute('entreprise_index', [], Response::HTTP_SEE_OTHER);
}
}