src/Controller/EntrepriseController.php line 218

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CSPE;
  4. use App\Entity\Note;
  5. use App\Entity\User;
  6. use App\Entity\Rappel;
  7. use App\Entity\Contact;
  8. use App\Entity\GasMeter;
  9. use App\Entity\Entreprise;
  10. use App\Entity\EspaceClient;
  11. use App\Entity\ElectricMeter;
  12. use App\Form\EntrepriseType;
  13. use App\Repository\EntrepriseRepository;
  14. use Symfony\Contracts\HttpClient\HttpClientInterface;
  15. use Doctrine\Persistence\ManagerRegistry;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. class EntrepriseController extends AbstractController
  23. {
  24.     /**
  25.      * @Route("/entreprise", name="app_entreprise")
  26.      */
  27.     public function index(ManagerRegistry $doctrine): Response
  28.     {
  29.         $entreprises $doctrine->getRepository(Entreprise::class)->findAll();
  30.         $array_entreprises = array();
  31.         
  32.         for ($i=0$i count($entreprises); $i++) { 
  33.             $user_name $doctrine->getRepository(User::class)->findOneBy(['id' => $entreprises[$i]->getUtilisateur()]);
  34.             $user_name $user_name $user_name->getUsername() : '';
  35.             $entreprise_contacts $doctrine->getRepository(Contact::class)->findBy(['entreprise_id' => $entreprises[$i]->getId()]);
  36.             $entreprise_contacts implode(" "array_map(function($contact) {
  37.                 return $contact->getNom()." ".$contact->getPrenom();
  38.             }, $entreprise_contacts));
  39.             $array_entreprises[$i] = [
  40.                 $entreprises[$i]->getRaisonSociale(),
  41.                 $entreprises[$i]->getCommune(),
  42.                 $entreprises[$i]->getSiret(),
  43.                 $entreprise_contacts,
  44.                 $user_name,
  45.                 '<a href="'.$this->generateUrl('app_entreprise_details',['id' => $entreprises[$i]->getId()]).'" type="button" class="btn btn-sm btn-primary mb-1">Consulter</a>
  46.                 <a href="'.$this->generateUrl('app_entreprise_edit',['id' => $entreprises[$i]->getId()]).'" type="button" class="btn btn-sm btn-warning mb-1">Modifier</a>
  47.                 <a href="'.$this->generateUrl('app_entreprise_suppr',['id' => $entreprises[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
  48.             ];
  49.         };
  50.         return $this->render('entreprise/index.html.twig', [
  51.             'entreprises' => $array_entreprises,
  52.         ]);
  53.     }
  54.     
  55.     /**
  56.      * @Route("/entreprise/add", name="app_entreprise_add")
  57.      */
  58.     public function add(Request $requestEntityManagerInterface $entityManager): Response
  59.     {
  60.         $entreprises = new Entreprise();
  61.         // using createQueryBuilder
  62.         $utilisateurs $entityManager->createQueryBuilder()
  63.             ->select('u')
  64.             ->from(User::class, 'u')
  65.             ->where('u.roles LIKE :roles')
  66.             ->setParameter('roles''%ROLE_TEAM%')
  67.             ->getQuery()
  68.             ->getResult();
  69.         $array_utilisateurs = array();
  70.         for ($i=0$i count($utilisateurs); $i++) { 
  71.             $array_utilisateurs[$utilisateurs[$i]->getUsername()] = $utilisateurs[$i]->getId();
  72.         }
  73.         $form $this->createForm(EntrepriseType::class, $entreprises, ['utilisateurs' => $array_utilisateurs]);
  74.         $form->handleRequest($request);
  75.         if ($form->isSubmitted() && $form->isValid()) {
  76.             $entityManager->persist($entreprises);
  77.             $entityManager->flush();
  78.             return $this->redirectToRoute('app_entreprise');
  79.         }
  80.         return $this->render('entreprise/add.html.twig', [
  81.             'entrepriseForm' => $form->createView(),
  82.             'array_utilisateurs' => $array_utilisateurs,
  83.         ]);
  84.     }
  85.     /**
  86.      * @Route("/entreprise/edit/{id}", name="app_entreprise_edit")
  87.      */
  88.     public function edit(int $idRequest $requestEntityManagerInterface $entityManagerManagerRegistry $doctrine): Response
  89.     {
  90.         $entreprise $doctrine->getRepository(Entreprise::class)->findOneBy(['id' => $id]);
  91.         $utilisateurs $entityManager->createQueryBuilder()
  92.             ->select('u')
  93.             ->from(User::class, 'u')
  94.             ->where('u.roles LIKE :roles')
  95.             ->setParameter('roles''%ROLE_TEAM%')
  96.             ->getQuery()
  97.             ->getResult();
  98.         $array_utilisateurs = array();
  99.         for ($i=0$i count($utilisateurs); $i++) { 
  100.             $array_utilisateurs[$utilisateurs[$i]->getUsername()] = $utilisateurs[$i]->getId();
  101.         }
  102.         $form $this->createForm(EntrepriseType::class, $entreprise, ['utilisateurs' => $array_utilisateurs]);
  103.         $form->handleRequest($request);
  104.         $entreprise_entity = [ 'raison_sociale' => $entreprise->getRaisonSociale(),
  105.             'siret' => $entreprise->getSiret(),
  106.             'naf' => $entreprise->getNaf(),
  107.             'rcs' => $entreprise->getRcs(),
  108.             'num_voie' => $entreprise->getNumVoie(),
  109.             'adresse' => $entreprise->getAdresse(),
  110.             'code_postal' => $entreprise->getCodePostal(),
  111.             'commune' => $entreprise->getCommune(),
  112.             'code_insee' => $entreprise->getCodeInsee(),
  113.             'statut' => $entreprise->getStatut(),
  114.             'utilisateur' => $entreprise->getUtilisateur(),
  115.         ];
  116.         if ($form->isSubmitted() && $form->isValid()) {
  117.             $entreprisesEdit $form->getData();
  118.             if($entreprisesEdit->getRaisonSociale() != null){
  119.                 $entreprise->setRaisonSociale($entreprisesEdit->getRaisonSociale());
  120.             }
  121.             if($entreprisesEdit->getSiret() != null){
  122.                 $entreprise->setSiret($entreprisesEdit->getSiret());
  123.             }
  124.             if($entreprisesEdit->getNaf() != null){
  125.                 $entreprise->setNaf($entreprisesEdit->getNaf());
  126.             }
  127.             if($entreprisesEdit->getRcs() != null){
  128.                 $entreprise->setRcs($entreprisesEdit->getRcs());
  129.             }
  130.             if($entreprisesEdit->getNumVoie() != null){
  131.                 $entreprise->setNumVoie($entreprisesEdit->getNumVoie());
  132.             }
  133.             if($entreprisesEdit->getAdresse() != null){
  134.                 $entreprise->setAdresse($entreprisesEdit->getAdresse());
  135.             }
  136.             if($entreprisesEdit->getCodePostal() != null){
  137.                 $entreprise->setCodePostal($entreprisesEdit->getCodePostal());
  138.             }
  139.             if($entreprisesEdit->getCommune() != null){
  140.                 $entreprise->setCommune($entreprisesEdit->getCommune());
  141.             }
  142.             if($entreprisesEdit->getCodeInsee() != null){
  143.                 $entreprise->setCodeInsee($entreprisesEdit->getCodeInsee());
  144.             }
  145.             if($entreprisesEdit->getStatut() != null){
  146.                 $entreprise->setStatut($entreprisesEdit->getStatut());
  147.             }
  148.             if($entreprisesEdit->getUtilisateur() != null){
  149.                 $entreprise->setUtilisateur($entreprisesEdit->getUtilisateur());
  150.             }
  151.             $entityManager->persist($entreprise);
  152.             $entityManager->flush();
  153.             return $this->redirectToRoute('app_entreprise');
  154.         }
  155.         return $this->render('entreprise/edit.html.twig', [
  156.             'entrepriseForm' => $form->createView(),
  157.             'entreprise' => $entreprise_entity
  158.         ]);
  159.     }
  160.     /**
  161.      * @Route("/entreprise/suppr/{id}", name="app_entreprise_suppr")
  162.      */
  163.     public function suppr(int $idRequest $requestEntityManagerInterface $entityManagerManagerRegistry $doctrine): Response
  164.     {
  165.         $notes $doctrine->getRepository(Note::class)->findBy(['entreprise_id' => $id]);
  166.         for ($i=0$i count($notes); $i++) { 
  167.             $entityManager->remove($notes[$i]);
  168.             $entityManager->flush();
  169.         };
  170.         $rappels $doctrine->getRepository(Rappel::class)->findBy(['entreprise_id' => $id]);
  171.         for ($i=0$i count($rappels); $i++) { 
  172.             $entityManager->remove($rappels[$i]);
  173.             $entityManager->flush();
  174.         };
  175.         $electric_meters $doctrine->getRepository(ElectricMeter::class)->findBy(['entreprise_id' => $id]);
  176.         for ($i=0$i count($electric_meters); $i++) { 
  177.             $entityManager->remove($electric_meters[$i]);
  178.             $entityManager->flush();
  179.         };
  180.         $gas_meters $doctrine->getRepository(GasMeter::class)->findBy(['entreprise_id' => $id]);
  181.         for ($i=0$i count($gas_meters); $i++) { 
  182.             $entityManager->remove($gas_meters[$i]);
  183.             $entityManager->flush();
  184.         };
  185.         $cspes $doctrine->getRepository(CSPE::class)->findBy(['entreprise_id' => $id]);
  186.         for ($i=0$i count($cspes); $i++) { 
  187.             $entityManager->remove($cspes[$i]);
  188.             $entityManager->flush();
  189.         };
  190.         $espace_clients $doctrine->getRepository(EspaceClient::class)->findBy(['entreprise_id' => $id]);
  191.         for ($i=0$i count($espace_clients); $i++) { 
  192.             $entityManager->remove($espace_clients[$i]);
  193.             $entityManager->flush();
  194.         };
  195.         $contacts $doctrine->getRepository(Contact::class)->findBy(['entreprise_id' => $id]);
  196.         for ($i=0$i count($contacts); $i++) { 
  197.             $entityManager->remove($contacts[$i]);
  198.             $entityManager->flush();
  199.         };
  200.         $entreprise $doctrine->getRepository(Entreprise::class)->findOneBy(['id' => $id]);
  201.         $entityManager->remove($entreprise);
  202.         $entityManager->flush();
  203.         return $this->redirectToRoute('app_entreprise');
  204.     }
  205.     /**
  206.      * @Route("/entreprise/details/{id}", name="app_entreprise_details")
  207.      */
  208.     public function details(int $idManagerRegistry $doctrine): Response
  209.     {
  210.         $entreprise $doctrine->getRepository(Entreprise::class)->findOneBy(['id' => $id]);
  211.         if(!$entreprise){
  212.             return $this->redirectToRoute('app_requetes');
  213.         }
  214.         
  215.         $user_name $doctrine->getRepository(User::class)->findOneBy(['id' => $entreprise->getUtilisateur()]);
  216.         $user_name $user_name $user_name->getUsername() : '';
  217.         $entreprise_entity = [ 'id' => $entreprise->getId(),
  218.             'raison_sociale' => $entreprise->getRaisonSociale(),
  219.             'siret' => $entreprise->getSiret(),
  220.             'naf' => $entreprise->getNaf(),
  221.             'rcs' => $entreprise->getRcs(),
  222.             'num_voie' => $entreprise->getNumVoie(),
  223.             'adresse' => $entreprise->getAdresse(),
  224.             'code_postal' => $entreprise->getCodePostal(),
  225.             'commune' => $entreprise->getCommune(),
  226.             'code_insee' => $entreprise->getCodeInsee(),
  227.             'statut' => $entreprise->getStatut(),
  228.             'utilisateur' => $user_name,
  229.         ];
  230.         $notes $doctrine->getRepository(Note::class)->findBy(['entreprise_id' => $id], ['date_creation' => 'DESC']);
  231.         $array_notes = array();
  232.         foreach ($notes as $note) {
  233.             $user $note->getUser();
  234.             $array_notes[] = [
  235.                 'date_creation' => $note->getDateCreation() ? $note->getDateCreation()->format('Y-m-d H:i:s') : '',
  236.                 'texte' => $note->getTexte(),
  237.                 'user' => $user $user->getUsername() : 'Non défini',
  238.                 'edit_url' => $this->generateUrl('app_note_edit', ['id' => $note->getId()]),
  239.                 'delete_url' => $this->generateUrl('app_note_suppr', ['id' => $note->getId()]),
  240.             ];
  241.         };
  242.         $rappels $doctrine->getRepository(Rappel::class)->findBy(['entreprise_id' => $id]);
  243.         $array_rappels = array();
  244.         for ($i=0$i count($rappels); $i++) { 
  245.             $array_rappels[$i] = [
  246.                 $rappels[$i]->getTitre(),
  247.                 $rappels[$i]->getDescription(),
  248.                 date_format($rappels[$i]->getEcheance(),'d/m/Y'),
  249.                 $rappels[$i]->isCompleter(),
  250.                 '<a href="'.$this->generateUrl('app_rappel_edit',['id' => $rappels[$i]->getId()]).'" type="button" class="btn btn-sm btn-primary mb-1">Modifier</a>
  251.                 <a href="'.$this->generateUrl('app_rappel_suppr',['id' => $rappels[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
  252.             ];
  253.         };
  254.         $electric_meters $doctrine->getRepository(ElectricMeter::class)->findBy(['entreprise_id' => $id]);
  255.         $array_electric_meters = array();
  256.         for ($i=0$i count($electric_meters); $i++) { 
  257.             $array_electric_meters[$i] = [
  258.                 $electric_meters[$i]->getAdresseCompteur(),
  259.                 $electric_meters[$i]->getPDL(),
  260.                 $electric_meters[$i]->getDateDebut()? date_format($electric_meters[$i]->getDateDebut(),'d/m/Y') : '',
  261.                 $electric_meters[$i]->getDateFin()? date_format($electric_meters[$i]->getDateFin(),'d/m/Y') : '',
  262.                 $electric_meters[$i]->getPS(),
  263.                 $electric_meters[$i]->getProfil(),
  264.                 $electric_meters[$i]->getCAR(),
  265.                 $electric_meters[$i]->getFournisseur1(),
  266.                 $electric_meters[$i]->getPrix1(),
  267.                 $electric_meters[$i]->getFournisseur2(),
  268.                 $electric_meters[$i]->getPrix2(),
  269.                 $electric_meters[$i]->getDateTransfert()? date_format($electric_meters[$i]->getDateTransfert(),'d/m/Y') : '',
  270.                 '<a href="'.$this->generateUrl('app_electric_meter_details',['id' => $electric_meters[$i]->getId()]).'" type="button" class="btn btn-sm btn-primary mb-1">Consulter</a>
  271.                 <a href="'.$this->generateUrl('app_electric_meter_edit',['id' => $electric_meters[$i]->getId()]).'" type="button" class="btn btn-sm btn-warning mb-1">Modifier</a>
  272.                 <a href="'.$this->generateUrl('app_electric_meter_suppr',['id' => $electric_meters[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
  273.             ];
  274.         };
  275.         $gas_meters $doctrine->getRepository(GasMeter::class)->findBy(['entreprise_id' => $id]);
  276.         $array_gas_meters = array();
  277.         for ($i=0$i count($gas_meters); $i++) { 
  278.             $array_gas_meters[$i] = [
  279.                 $gas_meters[$i]->getAdresseCompteur(),
  280.                 $gas_meters[$i]->getPDL(),
  281.                 $gas_meters[$i]->getDateDebut()? date_format($gas_meters[$i]->getDateDebut(),'d/m/Y') : '',
  282.                 $gas_meters[$i]->getDateFin()? date_format($gas_meters[$i]->getDateFin(),'d/m/Y') : '',
  283.                 $gas_meters[$i]->getProfil(),
  284.                 $gas_meters[$i]->getCAR(),
  285.                 $gas_meters[$i]->getFournisseur1(),
  286.                 $gas_meters[$i]->getPrix1(),
  287.                 $gas_meters[$i]->getFournisseur2(),
  288.                 $gas_meters[$i]->getPrix2(),
  289.                 $gas_meters[$i]->getDateTransfert()? date_format($gas_meters[$i]->getDateTransfert(),'d/m/Y') : '',
  290.                 '<a href="'.$this->generateUrl('app_gas_meter_details',['id' => $gas_meters[$i]->getId()]).'" type="button" class="btn btn-sm btn-primary mb-1">Consulter</a>
  291.                 <a href="'.$this->generateUrl('app_gas_meter_edit',['id' => $gas_meters[$i]->getId()]).'" type="button" class="btn btn-sm btn-warning mb-1">Modifier</a>
  292.                 <a href="'.$this->generateUrl('app_gas_meter_suppr',['id' => $gas_meters[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
  293.             ];
  294.         };
  295.         $cspes $doctrine->getRepository(CSPE::class)->findBy(['entreprise_id' => $id]);
  296.         $array_cspes = array();
  297.         for ($i=0$i count($cspes); $i++) { 
  298.             $array_cspes[$i] = [
  299.                 $cspes[$i]->getNotes(),
  300.                 date_format($cspes[$i]->getDate(),'d/m/Y'),
  301.                 $cspes[$i]->getValeur(),
  302.                 '<a href="'.$this->generateUrl('app_cspe_edit',['id' => $cspes[$i]->getId()]).'" type="button" class="btn btn-sm btn-warning mb-1">Modifier</a>
  303.                 <a href="'.$this->generateUrl('app_cspe_suppr',['id' => $cspes[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
  304.             ];
  305.         };
  306.         $espace_clients $doctrine->getRepository(EspaceClient::class)->findBy(['entreprise_id' => $id]);
  307.         $array_espace_clients = array();
  308.         for ($i=0$i count($espace_clients); $i++) {
  309.             if(strpos($espace_clients[$i]->getLien(), "http://") === false && strpos($espace_clients[$i]->getLien(), "https://") === false ){
  310.                 $lien "http://".$espace_clients[$i]->getLien();
  311.             }else{
  312.                 $lien $espace_clients[$i]->getLien();
  313.             }
  314.             $array_espace_clients[$i] = [
  315.                 $espace_clients[$i]->getFournisseur(),
  316.                 $espace_clients[$i]->getLogin(),
  317.                 $espace_clients[$i]->getMdp(),
  318.                 '<a href='.$lien.' target="_blank" type="button" class="btn btn-primary">'.explode("."$espace_clients[$i]->getLien())[1].'</a>',
  319.                 '<a href="'.$this->generateUrl('app_espace_client_edit',['id' => $espace_clients[$i]->getId()]).'" type="button" class="btn btn-sm btn-warning mb-1">Modifier</a>
  320.                 <a href="'.$this->generateUrl('app_espace_client_suppr',['id' => $espace_clients[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
  321.             ];
  322.         };
  323.         $contacts $doctrine->getRepository(Contact::class)->findBy(['entreprise_id' => $id]);
  324.         $array_contacts = array();
  325.         for ($i=0$i count($contacts); $i++) { 
  326.             $array_contacts[$i] = [
  327.                 $contacts[$i]->getNom(),
  328.                 $contacts[$i]->getPrenom(),
  329.                 $contacts[$i]->getCivilite(),
  330.                 $contacts[$i]->getFonction(),
  331.                 $contacts[$i]->getFixe(),
  332.                 $contacts[$i]->getEmail(),
  333.                 $contacts[$i]->getPortable(),
  334.                 '<a href="'.$this->generateUrl('app_contact_edit',['id' => $contacts[$i]->getId()]).'" type="button" class="btn btn-sm btn-warning mb-1">Modifier</a>
  335.                 <a href="'.$this->generateUrl('app_contact_suppr',['id' => $contacts[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
  336.             ];
  337.         };
  338.         return $this->render('entreprise/details.html.twig', [
  339.             'entreprise' => $entreprise_entity,
  340.             'notes' => $array_notes,
  341.             'rappels' => $array_rappels,
  342.             'electric_meters' => $array_electric_meters,
  343.             'gas_meters' => $array_gas_meters,
  344.             'cspes' => $array_cspes,
  345.             'espace_clients' => $array_espace_clients,
  346.             'contacts' => $array_contacts,
  347.         ]);
  348.     }
  349.     /**
  350.      * @Route("/entreprise/infoclient", name="app_entreprise_infoclient")
  351.      */
  352.     public function infoclient(HttpClientInterface $httpClient): JsonResponse
  353.     {
  354.         // Utilisez les paramètres appropriés pour votre requête
  355.         $url 'https://api.societe.com/api/v1/infoclient';
  356.         $headers = [
  357.             'headers' => [
  358.                 'X-Authorization' => 'socapi 8938e836988619dc20be14360fba30e3',
  359.             ],
  360.         ];
  361.         // Effectuez la requête
  362.         $response $httpClient->request('GET'$url$headers);
  363.         // Renvoie la réponse JSON
  364.         return new JsonResponse($response->toArray());
  365.     }
  366.     /**
  367.      * @Route("/entreprise/info_entreprise/{numero}", name="app_entreprise_info_entreprise")
  368.      */
  369.     public function info_entreprise(HttpClientInterface $httpClientManagerRegistry $doctrine$numero): JsonResponse
  370.     {
  371.         // Vérification que l'entreprise n'est pas déjà enregistré en BDD
  372.         $entreprise $doctrine->getRepository(Entreprise::class)->findOneBy(['Siret' => $numero]);
  373.         if($entreprise !=null){
  374.             return new JsonResponse("existant");
  375.         }
  376.         $url 'https://api.societe.com/api/v1/entreprise/'.$numero.'/infoslegales';
  377.         $headers = [
  378.             'headers' => [
  379.                 'X-Authorization' => 'socapi 8938e836988619dc20be14360fba30e3',
  380.             ],
  381.         ];
  382.         // Effectuez la requête
  383.         $response $httpClient->request('GET'$url$headers);
  384.         // Renvoie la réponse JSON
  385.         return new JsonResponse($response->toArray());
  386.     }
  387.     
  388. }