src/Controller/GasMeterController.php line 107

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Entreprise;
  4. use App\Entity\GasMeter;
  5. use App\Form\GasMeterType;
  6. use App\Repository\GasMeterRepository;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  15. use PhpOffice\PhpSpreadsheet\IOFactory;
  16. use Dompdf\Dompdf;
  17. class GasMeterController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/entreprise/gas_meter/add/{id}", name="app_gas_meter_add")
  21.      */
  22.     public function add(int $idRequest $requestEntityManagerInterface $entityManager): Response
  23.     {
  24.         $gas_meters = new GasMeter();
  25.         $gas_meters->setEntrepriseId($id);
  26.         $form $this->createForm(GasMeterType::class, $gas_meters);
  27.         $form->handleRequest($request);
  28.         if ($form->isSubmitted() && $form->isValid()) {
  29.             $entityManager->persist($gas_meters);
  30.             $entityManager->flush();
  31.             
  32.             // Redirect to the new contract page
  33.             return $this->redirectToRoute('app_entreprise_new_contrat', [
  34.                 'id' => $id,
  35.                 'meterId' => $gas_meters->getId(),
  36.                 'meterType' => 'gas'
  37.             ]);
  38.         }
  39.         return $this->render('entreprise/gas_meter/add.html.twig', [
  40.             'gas_meterForm' => $form->createView(),
  41.             'entreprise_id' => $id
  42.         ]);
  43.     }
  44.     /**
  45.      * @Route("/entreprise/gas_meter/edit/{id}", name="app_gas_meter_edit")
  46.      */
  47.     public function edit(int $idRequest $requestEntityManagerInterface $entityManagerManagerRegistry $doctrine): Response
  48.     {
  49.         $gas_meter $doctrine->getRepository(GasMeter::class)->findOneBy(['id' => $id]);
  50.         
  51.         if (!$gas_meter) {
  52.             throw $this->createNotFoundException('Gas meter not found');
  53.         }
  54.         $form $this->createForm(GasMeterType::class, $gas_meter);
  55.         $form->handleRequest($request);
  56.         $gas_meter_entity = [ 
  57.             'adresse_compteur' => $gas_meter->getAdresseCompteur(),
  58.             'PDL' => $gas_meter->getPDL(),
  59.             'date_debut' => $gas_meter->getDateDebut() ? date_format($gas_meter->getDateDebut(),"d/m/Y") : "",
  60.             'date_fin' => $gas_meter->getDateFin() ? date_format($gas_meter->getDateFin(),"d/m/Y") : "",
  61.             'profil' => $gas_meter->getProfil(),
  62.             'CAR' => $gas_meter->getCAR(),
  63.             'fournisseur' => $gas_meter->getFournisseur(),
  64.             'prix' => $gas_meter->getPrix(),
  65.         ];
  66.         if ($form->isSubmitted() && $form->isValid()) {
  67.             $entityManager->flush();
  68.             return $this->redirectToRoute('app_entreprise_details',['id' => $gas_meter->getEntrepriseId()]);
  69.         }
  70.         return $this->render('entreprise/gas_meter/edit.html.twig', [
  71.             'gas_meterForm' => $form->createView(),
  72.             'gas_meter' => $gas_meter_entity,
  73.             'entreprise_id' => $gas_meter->getEntrepriseId()
  74.         ]);
  75.     }
  76.     /**
  77.      * @Route("/entreprise/gas_meter/suppr/{id}", name="app_gas_meter_suppr")
  78.      */
  79.     public function suppr(int $idRequest $requestEntityManagerInterface $entityManagerManagerRegistry $doctrine): Response
  80.     {
  81.         $gas_meter $doctrine->getRepository(GasMeter::class)->findOneBy(['id' => $id]);
  82.         
  83.         if (!$gas_meter) {
  84.             throw $this->createNotFoundException('Gas meter not found');
  85.         }
  86.         $entreprise_id $gas_meter->getEntrepriseId();
  87.         $entityManager->remove($gas_meter);
  88.         $entityManager->flush();
  89.         return $this->redirectToRoute('app_entreprise_details',['id' => $entreprise_id]);
  90.     }
  91.     /**
  92.      * @Route("/entreprise/gas_meter/details/{id}", name="app_gas_meter_details")
  93.      */
  94.     public function details(int $idRequest $requestManagerRegistry $doctrine): JsonResponse
  95.     {
  96.         // Get PDL from query parameters
  97.         $pdl $request->query->get('pdl');
  98.         if (!$pdl) {
  99.             return new JsonResponse(['error' => 'PDL parameter is required'], 400);
  100.         }
  101.         // Find gas meter by PDL and enterprise ID
  102.         $gas_meter $doctrine->getRepository(GasMeter::class)->findOneBy([
  103.             'PDL' => $pdl,
  104.             'entreprise_id' => $id
  105.         ]);
  106.         
  107.         if (!$gas_meter) {
  108.             return new JsonResponse(['error' => 'Gas meter not found'], 404);
  109.         }
  110.         // Return gas meter details as JSON
  111.         return new JsonResponse([
  112.             'adresseCompteur' => $gas_meter->getAdresseCompteur(),
  113.             'PDL' => $gas_meter->getPDL(),
  114.             'dateDebut' => $gas_meter->getDateDebut() ? $gas_meter->getDateDebut()->format('d/m/Y') : null,
  115.             'dateFin' => $gas_meter->getDateFin() ? $gas_meter->getDateFin()->format('d/m/Y') : null,
  116.             'profil' => $gas_meter->getProfil(),
  117.             'CAR' => $gas_meter->getCAR(),
  118.             'fournisseur' => $gas_meter->getFournisseur(),
  119.             'prix' => $gas_meter->getPrix()
  120.         ]);
  121.     }
  122.     /**
  123.      * @Route("/entreprise/gas_meter/check_pdl/{pdl}", name="app_gas_meter_check_pdl")
  124.      */
  125.     public function checkPDL($pdlManagerRegistry $doctrine): Response
  126.     {
  127.         $gas_meter $doctrine->getRepository(GasMeter::class)->findOneBy(['PDL' => $pdl]);
  128.         if($gas_meter){
  129.             return new Response("exist");
  130.         }
  131.         return new Response("available");
  132.     }
  133.     /**
  134.      * @Route("/entreprise/gas_meter/pre_etude_c5_base/{pdl}", name="app_gas_meter_pre_etude_t2")
  135.      */
  136.     public function preEtudeT2(string $pdl=nullManagerRegistry $doctrine): Response
  137.     {
  138.         $gas_meter $doctrine->getRepository(GasMeter::class)->findOneBy(['PDL' => $pdl]);
  139.         
  140.         if (!$gas_meter) {
  141.             throw $this->createNotFoundException('Gas meter not found');
  142.         }
  143.         $entreprise $doctrine->getRepository(Entreprise::class)->findOneBy(['id' => $gas_meter->getEntrepriseId()]);
  144.         
  145.         if (!$entreprise) {
  146.             throw $this->createNotFoundException('Entreprise not found');
  147.         }
  148.         $templatePath $this->getParameter('kernel.project_dir') . '/assets/templates/ETUDE T2 Vierge.xlsx';
  149.         $spreadsheet IOFactory::load($templatePath);
  150.         $sheet $spreadsheet->getActiveSheet();
  151.         
  152.         $sheet->setCellValue('B2'$entreprise->getRaisonSociale() ?? '');
  153.         $sheet->setCellValue('C7'$entreprise->getSIRET() ?? '');
  154.         $sheet->setCellValue('C8'$gas_meter->getPDL() ?? '');
  155.         $sheet->setCellValue('F7'$entreprise->getNaf() ?? '');
  156.         $sheet->setCellValue('F10'$gas_meter->getDateFin() ? date_format($gas_meter->getDateFin(),"d/m/Y") : "");
  157.         $address $gas_meter->getAdresseCompteur() ?? '';
  158.         $address_with_no_postal_code_and_city '';
  159.         $postal_code '';
  160.         $city '';
  161.         if ($address) {
  162.             if (preg_match('/\b\d{5}\b/'$address$matches)) {
  163.                 $postal_code $matches[0];
  164.                 $postal_code_position strpos($address$postal_code);
  165.                 $city trim(substr($address$postal_code_position strlen($postal_code)));
  166.                 $address_with_no_postal_code_and_city trim(substr($address0$postal_code_position));
  167.             }
  168.         }
  169.         $sheet->setCellValue('F8'$address_with_no_postal_code_and_city);
  170.         $sheet->setCellValue('F9', ($postal_code && $city) ? $postal_code ' ' $city '');
  171.         $sheet->setCellValue('B12'$gas_meter->getCAR() ?? '');
  172.         // Générer la réponse pour le téléchargement du fichier
  173.         $response = new Response();
  174.         $writer IOFactory::createWriter($spreadsheet'Xlsx');
  175.         ob_start();
  176.         $writer->save('php://output');
  177.         $excelData ob_get_contents();
  178.         ob_end_clean();
  179.         // Définir les en-têtes pour le téléchargement
  180.         $RaisonSociale str_replace(' ''_'$entreprise->getRaisonSociale() ?? 'unknown');
  181.         $response->headers->set('Content-Type''application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  182.         $pdl_last_4_digits substr($gas_meter->getPDL() ?? '', -4);
  183.         $response->headers->set('Content-Disposition''attachment;filename="Etude_T2_'.$pdl_last_4_digits.'_'.$RaisonSociale.'.xlsx"');
  184.         $response->headers->set('Cache-Control''max-age=0');
  185.         // Envoyer le contenu généré en tant que réponse
  186.         $response->setContent($excelData);
  187.         return $response;
  188.     }
  189. }