<?php
namespace App\Controller;
use App\Entity\CSPE;
use App\Entity\Note;
use App\Entity\User;
use App\Entity\Rappel;
use App\Entity\Contact;
use App\Entity\GasMeter;
use App\Entity\Entreprise;
use App\Entity\EspaceClient;
use App\Entity\ElectricMeter;
use App\Form\EntrepriseType;
use App\Repository\EntrepriseRepository;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class EntrepriseController extends AbstractController
{
/**
* @Route("/entreprise", name="app_entreprise")
*/
public function index(ManagerRegistry $doctrine): Response
{
$entreprises = $doctrine->getRepository(Entreprise::class)->findAll();
$array_entreprises = array();
for ($i=0; $i < count($entreprises); $i++) {
$user_name = $doctrine->getRepository(User::class)->findOneBy(['id' => $entreprises[$i]->getUtilisateur()]);
$user_name = $user_name ? $user_name->getUsername() : '';
$entreprise_contacts = $doctrine->getRepository(Contact::class)->findBy(['entreprise_id' => $entreprises[$i]->getId()]);
$entreprise_contacts = implode(" ", array_map(function($contact) {
return $contact->getNom()." ".$contact->getPrenom();
}, $entreprise_contacts));
$array_entreprises[$i] = [
$entreprises[$i]->getRaisonSociale(),
$entreprises[$i]->getCommune(),
$entreprises[$i]->getSiret(),
$entreprise_contacts,
$user_name,
'<a href="'.$this->generateUrl('app_entreprise_details',['id' => $entreprises[$i]->getId()]).'" type="button" class="btn btn-sm btn-primary mb-1">Consulter</a>
<a href="'.$this->generateUrl('app_entreprise_edit',['id' => $entreprises[$i]->getId()]).'" type="button" class="btn btn-sm btn-warning mb-1">Modifier</a>
<a href="'.$this->generateUrl('app_entreprise_suppr',['id' => $entreprises[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
];
};
return $this->render('entreprise/index.html.twig', [
'entreprises' => $array_entreprises,
]);
}
/**
* @Route("/entreprise/add", name="app_entreprise_add")
*/
public function add(Request $request, EntityManagerInterface $entityManager): Response
{
$entreprises = new Entreprise();
// using createQueryBuilder
$utilisateurs = $entityManager->createQueryBuilder()
->select('u')
->from(User::class, 'u')
->where('u.roles LIKE :roles')
->setParameter('roles', '%ROLE_TEAM%')
->getQuery()
->getResult();
$array_utilisateurs = array();
for ($i=0; $i < count($utilisateurs); $i++) {
$array_utilisateurs[$utilisateurs[$i]->getUsername()] = $utilisateurs[$i]->getId();
}
$form = $this->createForm(EntrepriseType::class, $entreprises, ['utilisateurs' => $array_utilisateurs]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($entreprises);
$entityManager->flush();
return $this->redirectToRoute('app_entreprise');
}
return $this->render('entreprise/add.html.twig', [
'entrepriseForm' => $form->createView(),
'array_utilisateurs' => $array_utilisateurs,
]);
}
/**
* @Route("/entreprise/edit/{id}", name="app_entreprise_edit")
*/
public function edit(int $id, Request $request, EntityManagerInterface $entityManager, ManagerRegistry $doctrine): Response
{
$entreprise = $doctrine->getRepository(Entreprise::class)->findOneBy(['id' => $id]);
$utilisateurs = $entityManager->createQueryBuilder()
->select('u')
->from(User::class, 'u')
->where('u.roles LIKE :roles')
->setParameter('roles', '%ROLE_TEAM%')
->getQuery()
->getResult();
$array_utilisateurs = array();
for ($i=0; $i < count($utilisateurs); $i++) {
$array_utilisateurs[$utilisateurs[$i]->getUsername()] = $utilisateurs[$i]->getId();
}
$form = $this->createForm(EntrepriseType::class, $entreprise, ['utilisateurs' => $array_utilisateurs]);
$form->handleRequest($request);
$entreprise_entity = [ 'raison_sociale' => $entreprise->getRaisonSociale(),
'siret' => $entreprise->getSiret(),
'naf' => $entreprise->getNaf(),
'rcs' => $entreprise->getRcs(),
'num_voie' => $entreprise->getNumVoie(),
'adresse' => $entreprise->getAdresse(),
'code_postal' => $entreprise->getCodePostal(),
'commune' => $entreprise->getCommune(),
'code_insee' => $entreprise->getCodeInsee(),
'statut' => $entreprise->getStatut(),
'utilisateur' => $entreprise->getUtilisateur(),
];
if ($form->isSubmitted() && $form->isValid()) {
$entreprisesEdit = $form->getData();
if($entreprisesEdit->getRaisonSociale() != null){
$entreprise->setRaisonSociale($entreprisesEdit->getRaisonSociale());
}
if($entreprisesEdit->getSiret() != null){
$entreprise->setSiret($entreprisesEdit->getSiret());
}
if($entreprisesEdit->getNaf() != null){
$entreprise->setNaf($entreprisesEdit->getNaf());
}
if($entreprisesEdit->getRcs() != null){
$entreprise->setRcs($entreprisesEdit->getRcs());
}
if($entreprisesEdit->getNumVoie() != null){
$entreprise->setNumVoie($entreprisesEdit->getNumVoie());
}
if($entreprisesEdit->getAdresse() != null){
$entreprise->setAdresse($entreprisesEdit->getAdresse());
}
if($entreprisesEdit->getCodePostal() != null){
$entreprise->setCodePostal($entreprisesEdit->getCodePostal());
}
if($entreprisesEdit->getCommune() != null){
$entreprise->setCommune($entreprisesEdit->getCommune());
}
if($entreprisesEdit->getCodeInsee() != null){
$entreprise->setCodeInsee($entreprisesEdit->getCodeInsee());
}
if($entreprisesEdit->getStatut() != null){
$entreprise->setStatut($entreprisesEdit->getStatut());
}
if($entreprisesEdit->getUtilisateur() != null){
$entreprise->setUtilisateur($entreprisesEdit->getUtilisateur());
}
$entityManager->persist($entreprise);
$entityManager->flush();
return $this->redirectToRoute('app_entreprise');
}
return $this->render('entreprise/edit.html.twig', [
'entrepriseForm' => $form->createView(),
'entreprise' => $entreprise_entity
]);
}
/**
* @Route("/entreprise/suppr/{id}", name="app_entreprise_suppr")
*/
public function suppr(int $id, Request $request, EntityManagerInterface $entityManager, ManagerRegistry $doctrine): Response
{
$notes = $doctrine->getRepository(Note::class)->findBy(['entreprise_id' => $id]);
for ($i=0; $i < count($notes); $i++) {
$entityManager->remove($notes[$i]);
$entityManager->flush();
};
$rappels = $doctrine->getRepository(Rappel::class)->findBy(['entreprise_id' => $id]);
for ($i=0; $i < count($rappels); $i++) {
$entityManager->remove($rappels[$i]);
$entityManager->flush();
};
$electric_meters = $doctrine->getRepository(ElectricMeter::class)->findBy(['entreprise_id' => $id]);
for ($i=0; $i < count($electric_meters); $i++) {
$entityManager->remove($electric_meters[$i]);
$entityManager->flush();
};
$gas_meters = $doctrine->getRepository(GasMeter::class)->findBy(['entreprise_id' => $id]);
for ($i=0; $i < count($gas_meters); $i++) {
$entityManager->remove($gas_meters[$i]);
$entityManager->flush();
};
$cspes = $doctrine->getRepository(CSPE::class)->findBy(['entreprise_id' => $id]);
for ($i=0; $i < count($cspes); $i++) {
$entityManager->remove($cspes[$i]);
$entityManager->flush();
};
$espace_clients = $doctrine->getRepository(EspaceClient::class)->findBy(['entreprise_id' => $id]);
for ($i=0; $i < count($espace_clients); $i++) {
$entityManager->remove($espace_clients[$i]);
$entityManager->flush();
};
$contacts = $doctrine->getRepository(Contact::class)->findBy(['entreprise_id' => $id]);
for ($i=0; $i < count($contacts); $i++) {
$entityManager->remove($contacts[$i]);
$entityManager->flush();
};
$entreprise = $doctrine->getRepository(Entreprise::class)->findOneBy(['id' => $id]);
$entityManager->remove($entreprise);
$entityManager->flush();
return $this->redirectToRoute('app_entreprise');
}
/**
* @Route("/entreprise/details/{id}", name="app_entreprise_details")
*/
public function details(int $id, ManagerRegistry $doctrine): Response
{
$entreprise = $doctrine->getRepository(Entreprise::class)->findOneBy(['id' => $id]);
if(!$entreprise){
return $this->redirectToRoute('app_requetes');
}
$user_name = $doctrine->getRepository(User::class)->findOneBy(['id' => $entreprise->getUtilisateur()]);
$user_name = $user_name ? $user_name->getUsername() : '';
$entreprise_entity = [ 'id' => $entreprise->getId(),
'raison_sociale' => $entreprise->getRaisonSociale(),
'siret' => $entreprise->getSiret(),
'naf' => $entreprise->getNaf(),
'rcs' => $entreprise->getRcs(),
'num_voie' => $entreprise->getNumVoie(),
'adresse' => $entreprise->getAdresse(),
'code_postal' => $entreprise->getCodePostal(),
'commune' => $entreprise->getCommune(),
'code_insee' => $entreprise->getCodeInsee(),
'statut' => $entreprise->getStatut(),
'utilisateur' => $user_name,
];
$notes = $doctrine->getRepository(Note::class)->findBy(['entreprise_id' => $id], ['date_creation' => 'DESC']);
$array_notes = array();
foreach ($notes as $note) {
$user = $note->getUser();
$array_notes[] = [
'date_creation' => $note->getDateCreation() ? $note->getDateCreation()->format('Y-m-d H:i:s') : '',
'texte' => $note->getTexte(),
'user' => $user ? $user->getUsername() : 'Non défini',
'edit_url' => $this->generateUrl('app_note_edit', ['id' => $note->getId()]),
'delete_url' => $this->generateUrl('app_note_suppr', ['id' => $note->getId()]),
];
};
$rappels = $doctrine->getRepository(Rappel::class)->findBy(['entreprise_id' => $id]);
$array_rappels = array();
for ($i=0; $i < count($rappels); $i++) {
$array_rappels[$i] = [
$rappels[$i]->getTitre(),
$rappels[$i]->getDescription(),
date_format($rappels[$i]->getEcheance(),'d/m/Y'),
$rappels[$i]->isCompleter(),
'<a href="'.$this->generateUrl('app_rappel_edit',['id' => $rappels[$i]->getId()]).'" type="button" class="btn btn-sm btn-primary mb-1">Modifier</a>
<a href="'.$this->generateUrl('app_rappel_suppr',['id' => $rappels[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
];
};
$electric_meters = $doctrine->getRepository(ElectricMeter::class)->findBy(['entreprise_id' => $id]);
$array_electric_meters = array();
for ($i=0; $i < count($electric_meters); $i++) {
$array_electric_meters[$i] = [
$electric_meters[$i]->getAdresseCompteur(),
$electric_meters[$i]->getPDL(),
$electric_meters[$i]->getDateDebut()? date_format($electric_meters[$i]->getDateDebut(),'d/m/Y') : '',
$electric_meters[$i]->getDateFin()? date_format($electric_meters[$i]->getDateFin(),'d/m/Y') : '',
$electric_meters[$i]->getPS(),
$electric_meters[$i]->getProfil(),
$electric_meters[$i]->getCAR(),
$electric_meters[$i]->getFournisseur1(),
$electric_meters[$i]->getPrix1(),
$electric_meters[$i]->getFournisseur2(),
$electric_meters[$i]->getPrix2(),
$electric_meters[$i]->getDateTransfert()? date_format($electric_meters[$i]->getDateTransfert(),'d/m/Y') : '',
'<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>
<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>
<a href="'.$this->generateUrl('app_electric_meter_suppr',['id' => $electric_meters[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
];
};
$gas_meters = $doctrine->getRepository(GasMeter::class)->findBy(['entreprise_id' => $id]);
$array_gas_meters = array();
for ($i=0; $i < count($gas_meters); $i++) {
$array_gas_meters[$i] = [
$gas_meters[$i]->getAdresseCompteur(),
$gas_meters[$i]->getPDL(),
$gas_meters[$i]->getDateDebut()? date_format($gas_meters[$i]->getDateDebut(),'d/m/Y') : '',
$gas_meters[$i]->getDateFin()? date_format($gas_meters[$i]->getDateFin(),'d/m/Y') : '',
$gas_meters[$i]->getProfil(),
$gas_meters[$i]->getCAR(),
$gas_meters[$i]->getFournisseur1(),
$gas_meters[$i]->getPrix1(),
$gas_meters[$i]->getFournisseur2(),
$gas_meters[$i]->getPrix2(),
$gas_meters[$i]->getDateTransfert()? date_format($gas_meters[$i]->getDateTransfert(),'d/m/Y') : '',
'<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>
<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>
<a href="'.$this->generateUrl('app_gas_meter_suppr',['id' => $gas_meters[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
];
};
$cspes = $doctrine->getRepository(CSPE::class)->findBy(['entreprise_id' => $id]);
$array_cspes = array();
for ($i=0; $i < count($cspes); $i++) {
$array_cspes[$i] = [
$cspes[$i]->getNotes(),
date_format($cspes[$i]->getDate(),'d/m/Y'),
$cspes[$i]->getValeur(),
'<a href="'.$this->generateUrl('app_cspe_edit',['id' => $cspes[$i]->getId()]).'" type="button" class="btn btn-sm btn-warning mb-1">Modifier</a>
<a href="'.$this->generateUrl('app_cspe_suppr',['id' => $cspes[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
];
};
$espace_clients = $doctrine->getRepository(EspaceClient::class)->findBy(['entreprise_id' => $id]);
$array_espace_clients = array();
for ($i=0; $i < count($espace_clients); $i++) {
if(strpos($espace_clients[$i]->getLien(), "http://") === false && strpos($espace_clients[$i]->getLien(), "https://") === false ){
$lien = "http://".$espace_clients[$i]->getLien();
}else{
$lien = $espace_clients[$i]->getLien();
}
$array_espace_clients[$i] = [
$espace_clients[$i]->getFournisseur(),
$espace_clients[$i]->getLogin(),
$espace_clients[$i]->getMdp(),
'<a href='.$lien.' target="_blank" type="button" class="btn btn-primary">'.explode(".", $espace_clients[$i]->getLien())[1].'</a>',
'<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>
<a href="'.$this->generateUrl('app_espace_client_suppr',['id' => $espace_clients[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
];
};
$contacts = $doctrine->getRepository(Contact::class)->findBy(['entreprise_id' => $id]);
$array_contacts = array();
for ($i=0; $i < count($contacts); $i++) {
$array_contacts[$i] = [
$contacts[$i]->getNom(),
$contacts[$i]->getPrenom(),
$contacts[$i]->getCivilite(),
$contacts[$i]->getFonction(),
$contacts[$i]->getFixe(),
$contacts[$i]->getEmail(),
$contacts[$i]->getPortable(),
'<a href="'.$this->generateUrl('app_contact_edit',['id' => $contacts[$i]->getId()]).'" type="button" class="btn btn-sm btn-warning mb-1">Modifier</a>
<a href="'.$this->generateUrl('app_contact_suppr',['id' => $contacts[$i]->getId()]).'" type="button" class="btn btn-sm btn-danger ">Supprimer</a>',
];
};
return $this->render('entreprise/details.html.twig', [
'entreprise' => $entreprise_entity,
'notes' => $array_notes,
'rappels' => $array_rappels,
'electric_meters' => $array_electric_meters,
'gas_meters' => $array_gas_meters,
'cspes' => $array_cspes,
'espace_clients' => $array_espace_clients,
'contacts' => $array_contacts,
]);
}
/**
* @Route("/entreprise/infoclient", name="app_entreprise_infoclient")
*/
public function infoclient(HttpClientInterface $httpClient): JsonResponse
{
// Utilisez les paramètres appropriés pour votre requête
$url = 'https://api.societe.com/api/v1/infoclient';
$headers = [
'headers' => [
'X-Authorization' => 'socapi 8938e836988619dc20be14360fba30e3',
],
];
// Effectuez la requête
$response = $httpClient->request('GET', $url, $headers);
// Renvoie la réponse JSON
return new JsonResponse($response->toArray());
}
/**
* @Route("/entreprise/info_entreprise/{numero}", name="app_entreprise_info_entreprise")
*/
public function info_entreprise(HttpClientInterface $httpClient, ManagerRegistry $doctrine, $numero): JsonResponse
{
// Vérification que l'entreprise n'est pas déjà enregistré en BDD
$entreprise = $doctrine->getRepository(Entreprise::class)->findOneBy(['Siret' => $numero]);
if($entreprise !=null){
return new JsonResponse("existant");
}
$url = 'https://api.societe.com/api/v1/entreprise/'.$numero.'/infoslegales';
$headers = [
'headers' => [
'X-Authorization' => 'socapi 8938e836988619dc20be14360fba30e3',
],
];
// Effectuez la requête
$response = $httpClient->request('GET', $url, $headers);
// Renvoie la réponse JSON
return new JsonResponse($response->toArray());
}
}