<?php
namespace App\Controller;
use App\Entity\Entreprise;
use App\Entity\GasMeter;
use App\Form\GasMeterType;
use App\Repository\GasMeterRepository;
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;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Dompdf\Dompdf;
class GasMeterController extends AbstractController
{
/**
* @Route("/entreprise/gas_meter/add/{id}", name="app_gas_meter_add")
*/
public function add(int $id, Request $request, EntityManagerInterface $entityManager): Response
{
$gas_meters = new GasMeter();
$gas_meters->setEntrepriseId($id);
$form = $this->createForm(GasMeterType::class, $gas_meters);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($gas_meters);
$entityManager->flush();
// Redirect to the new contract page
return $this->redirectToRoute('app_entreprise_new_contrat', [
'id' => $id,
'meterId' => $gas_meters->getId(),
'meterType' => 'gas'
]);
}
return $this->render('entreprise/gas_meter/add.html.twig', [
'gas_meterForm' => $form->createView(),
'entreprise_id' => $id
]);
}
/**
* @Route("/entreprise/gas_meter/edit/{id}", name="app_gas_meter_edit")
*/
public function edit(int $id, Request $request, EntityManagerInterface $entityManager, ManagerRegistry $doctrine): Response
{
$gas_meter = $doctrine->getRepository(GasMeter::class)->findOneBy(['id' => $id]);
if (!$gas_meter) {
throw $this->createNotFoundException('Gas meter not found');
}
$form = $this->createForm(GasMeterType::class, $gas_meter);
$form->handleRequest($request);
$gas_meter_entity = [
'adresse_compteur' => $gas_meter->getAdresseCompteur(),
'PDL' => $gas_meter->getPDL(),
'date_debut' => $gas_meter->getDateDebut() ? date_format($gas_meter->getDateDebut(),"d/m/Y") : "",
'date_fin' => $gas_meter->getDateFin() ? date_format($gas_meter->getDateFin(),"d/m/Y") : "",
'profil' => $gas_meter->getProfil(),
'CAR' => $gas_meter->getCAR(),
'fournisseur' => $gas_meter->getFournisseur(),
'prix' => $gas_meter->getPrix(),
];
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_entreprise_details',['id' => $gas_meter->getEntrepriseId()]);
}
return $this->render('entreprise/gas_meter/edit.html.twig', [
'gas_meterForm' => $form->createView(),
'gas_meter' => $gas_meter_entity,
'entreprise_id' => $gas_meter->getEntrepriseId()
]);
}
/**
* @Route("/entreprise/gas_meter/suppr/{id}", name="app_gas_meter_suppr")
*/
public function suppr(int $id, Request $request, EntityManagerInterface $entityManager, ManagerRegistry $doctrine): Response
{
$gas_meter = $doctrine->getRepository(GasMeter::class)->findOneBy(['id' => $id]);
if (!$gas_meter) {
throw $this->createNotFoundException('Gas meter not found');
}
$entreprise_id = $gas_meter->getEntrepriseId();
$entityManager->remove($gas_meter);
$entityManager->flush();
return $this->redirectToRoute('app_entreprise_details',['id' => $entreprise_id]);
}
/**
* @Route("/entreprise/gas_meter/details/{id}", name="app_gas_meter_details")
*/
public function details(int $id, Request $request, ManagerRegistry $doctrine): JsonResponse
{
// Get PDL from query parameters
$pdl = $request->query->get('pdl');
if (!$pdl) {
return new JsonResponse(['error' => 'PDL parameter is required'], 400);
}
// Find gas meter by PDL and enterprise ID
$gas_meter = $doctrine->getRepository(GasMeter::class)->findOneBy([
'PDL' => $pdl,
'entreprise_id' => $id
]);
if (!$gas_meter) {
return new JsonResponse(['error' => 'Gas meter not found'], 404);
}
// Return gas meter details as JSON
return new JsonResponse([
'adresseCompteur' => $gas_meter->getAdresseCompteur(),
'PDL' => $gas_meter->getPDL(),
'dateDebut' => $gas_meter->getDateDebut() ? $gas_meter->getDateDebut()->format('d/m/Y') : null,
'dateFin' => $gas_meter->getDateFin() ? $gas_meter->getDateFin()->format('d/m/Y') : null,
'profil' => $gas_meter->getProfil(),
'CAR' => $gas_meter->getCAR(),
'fournisseur' => $gas_meter->getFournisseur(),
'prix' => $gas_meter->getPrix()
]);
}
/**
* @Route("/entreprise/gas_meter/check_pdl/{pdl}", name="app_gas_meter_check_pdl")
*/
public function checkPDL($pdl, ManagerRegistry $doctrine): Response
{
$gas_meter = $doctrine->getRepository(GasMeter::class)->findOneBy(['PDL' => $pdl]);
if($gas_meter){
return new Response("exist");
}
return new Response("available");
}
/**
* @Route("/entreprise/gas_meter/pre_etude_c5_base/{pdl}", name="app_gas_meter_pre_etude_t2")
*/
public function preEtudeT2(string $pdl=null, ManagerRegistry $doctrine): Response
{
$gas_meter = $doctrine->getRepository(GasMeter::class)->findOneBy(['PDL' => $pdl]);
if (!$gas_meter) {
throw $this->createNotFoundException('Gas meter not found');
}
$entreprise = $doctrine->getRepository(Entreprise::class)->findOneBy(['id' => $gas_meter->getEntrepriseId()]);
if (!$entreprise) {
throw $this->createNotFoundException('Entreprise not found');
}
$templatePath = $this->getParameter('kernel.project_dir') . '/assets/templates/ETUDE T2 Vierge.xlsx';
$spreadsheet = IOFactory::load($templatePath);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('B2', $entreprise->getRaisonSociale() ?? '');
$sheet->setCellValue('C7', $entreprise->getSIRET() ?? '');
$sheet->setCellValue('C8', $gas_meter->getPDL() ?? '');
$sheet->setCellValue('F7', $entreprise->getNaf() ?? '');
$sheet->setCellValue('F10', $gas_meter->getDateFin() ? date_format($gas_meter->getDateFin(),"d/m/Y") : "");
$address = $gas_meter->getAdresseCompteur() ?? '';
$address_with_no_postal_code_and_city = '';
$postal_code = '';
$city = '';
if ($address) {
if (preg_match('/\b\d{5}\b/', $address, $matches)) {
$postal_code = $matches[0];
$postal_code_position = strpos($address, $postal_code);
$city = trim(substr($address, $postal_code_position + strlen($postal_code)));
$address_with_no_postal_code_and_city = trim(substr($address, 0, $postal_code_position));
}
}
$sheet->setCellValue('F8', $address_with_no_postal_code_and_city);
$sheet->setCellValue('F9', ($postal_code && $city) ? $postal_code . ' ' . $city : '');
$sheet->setCellValue('B12', $gas_meter->getCAR() ?? '');
// Générer la réponse pour le téléchargement du fichier
$response = new Response();
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
ob_start();
$writer->save('php://output');
$excelData = ob_get_contents();
ob_end_clean();
// Définir les en-têtes pour le téléchargement
$RaisonSociale = str_replace(' ', '_', $entreprise->getRaisonSociale() ?? 'unknown');
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$pdl_last_4_digits = substr($gas_meter->getPDL() ?? '', -4);
$response->headers->set('Content-Disposition', 'attachment;filename="Etude_T2_'.$pdl_last_4_digits.'_'.$RaisonSociale.'.xlsx"');
$response->headers->set('Cache-Control', 'max-age=0');
// Envoyer le contenu généré en tant que réponse
$response->setContent($excelData);
return $response;
}
}