src/Controller/FactureAafController.php line 126

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\FactureAaf;
  4. use App\Entity\Installateur;
  5. use App\Entity\Docfacture;
  6. use App\Form\FactureAafType;
  7. use App\Repository\FactureAafRepository;
  8. use App\Repository\DocfactureRepository;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use App\Service\FileUploader;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\String\Slugger\SluggerInterface;
  16. use Symfony\Component\HttpFoundation\File\UploadedFile;
  17. use Datetime;
  18. use Doctrine\Common\Collections\Collection;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use App\Repository\NotificationsRepository;
  21. use App\Repository\InstallateurRepository;
  22. use App\Repository\EquipeRepository;
  23. use Symfony\Component\Process\Process;
  24. use Symfony\Component\Filesystem\Filesystem;
  25. use Symfony\Component\Finder\Finder;
  26. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  27. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  28. use PhpOffice\PhpSpreadsheet\Style\Fill;
  29.  /**
  30.  * @Route("/facture")
  31.  */
  32. class FactureAafController extends AbstractController
  33. {
  34.     
  35.     /**
  36.      * @Route("/", name="app_facture_aaf_index", methods={"GET", "POST"})
  37.      */
  38.     public function index(FactureAafRepository $factureAafRepository): Response
  39.     {
  40.         if ($this->isGranted('ROLE_INSTALLATEUR')){
  41.             $factureAaf=[];
  42.             foreach ($factureAafRepository->findAll() as $key => $aaf) {
  43.                 foreach ($aaf->getDossier() as $key => $doss) {
  44.                    if ($doss->getDeal()->getInstallateur()->getId() == $this->getUser()->getInstallateur()->getId() ) {
  45.                     $factureAaf[]=$aaf;
  46.                    }
  47.                 }
  48.                 
  49.             }
  50.         }else{
  51.             $factureAaf$factureAafRepository->findAll();
  52.         }
  53.         
  54.         $tab=[];
  55.         $factureAafs=[];
  56.         // id,type as owner,client as name ,date,file ,batiment
  57.         foreach ($factureAaf as  $value) {
  58.             $j 0;
  59.             $dossi "";
  60.             if ($value->getNumero() != null) {
  61.                 $tab['id']=$value->getId();
  62.                  //$tab['id_dossier']=$value->getDossier()->getId();
  63.                 $tab['name']=$value->getNumero();
  64.                  $tab['owner']=$value->getDepotaaf();
  65.                 
  66.                 $tab['count']=count($value->getDossier());
  67.                 foreach ($value->getDossier() as $key => $doss) {
  68.                     
  69.                     if ($j==0) {
  70.                         $dossi$doss->getDeal()->getDealoblige()->getNom();
  71.                         
  72.                        
  73.                         $j=$j+1;
  74.                     }
  75.                    
  76.                 }
  77.                 $tab['industry_type']=$dossi;
  78.                 $factureAafs[]=$tab;
  79.             }
  80.            
  81.         }
  82.        //dd($factureAafs);
  83.         $clientjson json_encode($factureAafs);
  84.         // var_dump(json_encode($clientjson ));
  85.         file_put_contents('assets/json/factureaaf-list.json'$clientjson);
  86.         return $this->render('facture_aaf/index.html.twig', [
  87.             'facture_aafs' => $factureAafRepository->findAll(),
  88.         ]);
  89.     }
  90.     
  91.     /**
  92.      * @Route("/new", name="app_facture_aaf_new", methods={"GET", "POST"})
  93.      */
  94.     public function new(Request $requestEntityManagerInterface $entityManager): Response
  95.     {
  96.         $factureAaf = new FactureAaf();
  97.         $form $this->createForm(FactureAafType::class, $factureAaf);
  98.         $form->handleRequest($request);
  99.         if ($form->isSubmitted() && $form->isValid()) {
  100.             $entityManager->persist($factureAaf);
  101.             $entityManager->flush();
  102.             return $this->redirectToRoute('app_facture_aaf_index', [], Response::HTTP_SEE_OTHER);
  103.         }
  104.         return $this->renderForm('facture_aaf/new.html.twig', [
  105.             'facture_aaf' => $factureAaf,
  106.             'form' => $form,
  107.         ]);
  108.     }
  109.    
  110.     /**
  111.      * @Route("/{id}", name="app_facture_aaf_show", methods={"GET"})
  112.      */
  113.     public function show(FactureAaf $factureAaf): Response
  114.     {
  115.         // $factureAaf= $factureAafRepository->findAll();
  116.         if ($this->isGranted('ROLE_INSTALLATEUR')){
  117.             $parameters = ['facture' => $factureAaf->getNumero()];
  118.             return $this->redirectToRoute('app_facture_aaf_installateur_show',  ['id' => $this->getUser()->getInstallateur()->getId()]+$parametersResponse::HTTP_SEE_OTHER);
  119.         }
  120.         $tab=[];
  121.         $factureAafs=[];
  122.         $cumacTotal=0;
  123.         $installateur=[];
  124.         $instid=[];
  125.         $oblige ="";
  126.         foreach ($factureAaf->getDossier() as  $value) {
  127.             
  128.             if ($value->getNumero() != null) {
  129.                 $tab['id']=$value->getDeal()->getInstallateur()->getId();
  130.                 $tab['id_dossier']=$value->getId();
  131.                 $tab['owner']=$value->getNumero();
  132.                 $tab['name']=$value->getDeal()->getInstallateur()->getSociete();
  133.                 if (!in_array($value->getDeal()->getInstallateur()->getId(), $instid)) {
  134.                     $installateur[]=$value->getDeal()->getInstallateur();
  135.                     $instid[]=$value->getDeal()->getInstallateur()->getId();
  136.                 }
  137.                
  138.                 $tab['industry_type']=$value->getCumac();
  139.                 $tab['location']=$factureAaf->getNumero();
  140.                 $factureAafs[]=$tab;
  141.                 $cumacTotal =$cumacTotal+$value->getCumac();
  142.                 $oblige $value->getDeal()->getDealoblige()->getNom();
  143.             }
  144.            
  145.         }
  146.        //dd($installateur);
  147.         $clientjson json_encode($factureAafs);
  148.         // var_dump(json_encode($clientjson ));
  149.         file_put_contents('assets/json/showaaf-list.json'$clientjson);
  150.         return $this->render('facture_aaf/show.html.twig', [
  151.             'facture_aaf' => $factureAaf,
  152.             'TotalCumac' => $cumacTotal,
  153.             'installateur' => $installateur,
  154.             'oblige' =>$oblige
  155.         ]);
  156.     }
  157.      /**
  158.      * @Route("/installateur/{id}", name="app_facture_aaf_installateur_show", methods={"GET"})
  159.      */
  160.     public function showinst(Request $request,Installateur $installateur,FactureAafRepository $factureAafRepository,DocfactureRepository $docfactureRepository): Response
  161.     {
  162.         // $factureAaf= $factureAafRepository->findAll();
  163.         $form $request->request
  164.         
  165.         $factureAaf $factureAafRepository->findOneBy(["numero" => $request->get("facture")]);
  166.         
  167.         $tab=[];
  168.         $factureAafs=[];
  169.         $cumacTotal=0;
  170.        
  171.         $oblige ="";
  172.         $taux =0;
  173.         $docfacture=[];
  174.         foreach ($factureAaf->getDossier() as  $value) {
  175.             
  176.             if ($value->getNumero() != null) {
  177.                 if ($value->getDeal()->getInstallateur()->getId() == $installateur->getId()) {
  178.                   
  179.                     $factureAafs[]=$value;
  180.                     $cumacTotal =$cumacTotal+$value->getCumac();
  181.                     $oblige $value->getDeal()->getDealoblige()->getNom();
  182.                     $taux  =$value->getDeal()->getTaux();
  183.                 }
  184.               
  185.             }
  186.            
  187.         }
  188.         foreach ($factureAaf->getDocfactures() as $key => $doc) {
  189.             if ($doc->getInstallateur()->getId() == $installateur->getId()) {
  190.                 $docfacture$doc;
  191.             }
  192.         }
  193.         
  194.        // dd($factureAafs);
  195.         // $clientjson = json_encode($factureAafs);
  196.         // var_dump(json_encode($clientjson ));
  197.         // file_put_contents('assets/json/showaaf-list.json', $clientjson);
  198.         return $this->render('facture_aaf/showinst.html.twig', [
  199.             'installateur_aaf' => $installateur,
  200.             'TotalCumac' => $cumacTotal,
  201.             'oblige' =>$oblige,
  202.             'facture_aafs' => $factureAafs,
  203.             'factureAaf' => $factureAaf,
  204.             'taux' =>$taux,
  205.             'docfacture' => $docfacture
  206.         ]);
  207.     }
  208.       /**
  209.      * @Route("/installateur/exportedepot/{id}", name="app_facture_aaf_installateur_exporte",  methods={"GET"})
  210.      */
  211.     public function exporte(Request $request,Installateur $installateur,FactureAafRepository $factureAafRepository,DocfactureRepository $docfactureRepository): Response
  212.     {
  213.         
  214.         
  215.       //  $jsonFilePath = 'assets/json/dossier-list.json';
  216.         // Étape 1 : Récupérer le contenu JSON depuis le fichier
  217.        // $jsonContent = file_get_contents($jsonFilePath);
  218.         // Étape 2 : Décoder le contenu JSON en tableau PHP
  219.         $factureAaf $factureAafRepository->findOneBy(["numero" => $request->get("facture")]);
  220.         $numeroaaf=$factureAaf->getNumero();
  221.         $tab=[];
  222.         $factureAafs=[];
  223.         $cumacTotal=0;
  224.        
  225.         $oblige ="";
  226.         $taux =0;
  227.         $docfacture=[];
  228.         foreach ($factureAaf->getDossier() as  $value) {
  229.             
  230.             if ($value->getNumero() != null) {
  231.                 if ($value->getDeal()->getInstallateur()->getId() == $installateur->getId()) {
  232.                   
  233.                     $factureAafs[]=$value;
  234.                     $cumacTotal =$cumacTotal+$value->getCumac();
  235.                     $oblige $value->getDeal()->getDealoblige()->getNom();
  236.                     $taux  =$value->getDeal()->getTaux();
  237.                 }
  238.               
  239.             }
  240.            
  241.         }
  242.         foreach ($factureAaf->getDocfactures() as $key => $doc) {
  243.             if ($doc->getInstallateur()->getId() == $installateur->getId()) {
  244.                 $docfacture$doc;
  245.             }
  246.         }
  247.         $phpArray $factureAafs;
  248.       
  249.         // Créer un nouveau classeur Excel
  250.         $spreadsheet = new Spreadsheet();
  251.                 
  252.         // Obtenir la feuille active
  253.         $sheet $spreadsheet->getActiveSheet();
  254.         // Écrire des données dans la feuille
  255.        
  256.        $totalcumac 0;
  257.         $taux 0;
  258.         $sheet->setCellValue('A1''Numéro dossier');
  259.         $sheet->getStyle('A1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('BAE1FF'); 
  260.         $sheet->getStyle('A1')->getFont()->setBold(true);
  261.         $sheet->setCellValue('B1''Nom du Beneficiaire');
  262.         $sheet->getStyle('B1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('BAE1FF'); 
  263.         $sheet->getStyle('B1')->getFont()->setBold(true);
  264.         $sheet->setCellValue('C1''Deal associé');
  265.         $sheet->getStyle('C1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('BAE1FF'); 
  266.         $sheet->getStyle('C1')->getFont()->setBold(true);
  267.         $sheet->setCellValue('D1''KWh Cumac');
  268.         $sheet->getStyle('D1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('BAE1FF'); 
  269.         $sheet->getStyle('D1')->getFont()->setBold(true);
  270.         $sheet->setCellValue('E1''Taux');
  271.         $sheet->getStyle('E1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('BAE1FF'); 
  272.         $sheet->getStyle('E1')->getFont()->setBold(true);
  273.         $sheet->setCellValue('F1''Prime');
  274.         $sheet->getStyle('F1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('BAE1FF'); 
  275.         $sheet->getStyle('F1')->getFont()->setBold(true);
  276.         $sheet->getColumnDimension('A')->setWidth(15);
  277.         $sheet->getColumnDimension('B')->setWidth(18);
  278.         $sheet->getColumnDimension('C')->setWidth(15);
  279.         $sheet->getColumnDimension('D')->setWidth(15);
  280.         $sheet->getColumnDimension('E')->setWidth(15);
  281.         $sheet->getColumnDimension('F')->setWidth(15);
  282.         
  283.     
  284.     
  285.         $i=2;
  286.         
  287.         if ($phpArray === null) {
  288.             echo "Erreur Tableau vide: ";
  289.         } else {
  290.             // Utilisez le tableau PHP $phpArray comme bon vous semble
  291.             
  292.             foreach ($phpArray as $dl) {
  293.                 // $dl=$depotDossierRepository->findOneBy(["id"=>intval($value["id"])]);
  294.                 $sheet->setCellValue('A'.$i$dl->getNumero());
  295.               
  296.                 $sheet->setCellValue('B'.$i$dl->getPrenomclient().' '.$dl->getNomclient() );
  297.                 $sheet->setCellValue('C'.$i$dl->getDeal()->getDealoblige()->getNom());
  298.                 $sheet->setCellValue('D'.$i,  $dl->getCumac());
  299.                 $sheet->setCellValue('E'.$i$dl->getDeal()->getTaux());
  300.                 $sheet->setCellValue('F'.$i$dl->getCumac() * ($dl->getDeal()->getTaux() / 1000));
  301.                 $totalcumac $totalcumac $dl->getCumac();
  302.                 $taux $dl->getDeal()->getTaux() / 1000;
  303.                
  304.                 $i=$i+1;
  305.             }
  306.         }
  307.         $sheet->setCellValue('A'.$i+2'Total Cumac');
  308.         $sheet->getStyle('A'.$i+2)->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('96CEB4'); 
  309.         $sheet->getStyle('A'.$i+2)->getFont()->setBold(true);
  310.         $sheet->setCellValue('B'.$i+2$totalcumac);
  311.         $sheet->getStyle('B'.$i+2)->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('96CEB4'); 
  312.         $sheet->getStyle('B'.$i+2)->getFont()->setBold(true);
  313.         $sheet->setCellValue('A'.$i+3'Total Prime');
  314.         $sheet->getStyle('A'.$i+3)->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('96CEB4'); 
  315.         $sheet->getStyle('A'.$i+3)->getFont()->setBold(true);
  316.         $sheet->setCellValue('B'.$i+3$totalcumac $taux);
  317.         $sheet->getStyle('B'.$i+3)->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('96CEB4'); 
  318.         $sheet->getStyle('B'.$i+3)->getFont()->setBold(true);
  319.         
  320.        
  321.         // Créer un objet Writer pour enregistrer le fichier
  322.         $writer = new Xlsx($spreadsheet);
  323.         // Enregistrer le fichier à un emplacement
  324.         $now = new DateTime();
  325.                 $date $now->format('d-m-Y H-i-s');
  326.         $nomexecel "factureAAf-".$numeroaaf."-".$installateur->getSociete()."-".$date;
  327.         $cheminFichier =$this->getParameter('installateur_directory') . "/export/export".$nomexecel.".xlsx";
  328.         $writer->save($cheminFichier);
  329.         // $filtre->setExporte("export".$nomexecel.".xlsx");
  330.         // $filtreRepository->add($filtre, true);
  331.         // Renvoyer la réponse avec le fichier Excel en pièce jointe
  332.         return $this->file($cheminFichier);
  333.     }
  334.     
  335.     /**
  336.      * @Route("/{id}/edit", name="app_facture_aaf_edit", methods={"GET", "POST"})
  337.      */
  338.     public function edit(Request $requestFactureAaf $factureAafEntityManagerInterface $entityManager): Response
  339.     {
  340.         $form $this->createForm(FactureAafType::class, $factureAaf);
  341.         $form->handleRequest($request);
  342.         if ($form->isSubmitted() && $form->isValid()) {
  343.             $entityManager->flush();
  344.             return $this->redirectToRoute('app_facture_aaf_index', [], Response::HTTP_SEE_OTHER);
  345.         }
  346.         return $this->renderForm('facture_aaf/edit.html.twig', [
  347.             'facture_aaf' => $factureAaf,
  348.             'form' => $form,
  349.         ]);
  350.     }
  351.    
  352.     /**
  353.      * @Route("/{id}", name="app_facture_aaf_delete", methods={"POST"})
  354.      */
  355.     public function delete(Request $requestFactureAaf $factureAafEntityManagerInterface $entityManager): Response
  356.     {
  357.         if ($this->isCsrfTokenValid('delete'.$factureAaf->getId(), $request->request->get('_token'))) {
  358.             $entityManager->remove($factureAaf);
  359.             $entityManager->flush();
  360.         }
  361.         return $this->redirectToRoute('app_facture_aaf_index', [], Response::HTTP_SEE_OTHER);
  362.     }
  363.        /**
  364.      * @Route("/upload/{id}", methods={"POST"})
  365.      */
  366.     public function upload(Request $request ,FactureAaf $factureAaf,InstallateurRepository $installateurRepository,
  367.     FileUploader $uploader,EquipeRepository $equipeRepository,NotificationsRepository $notificationsRepository,DocfactureRepository $docfactureRepository): JsonResponse
  368.     
  369.         
  370.        
  371.         if ($request->getMethod() == "POST") {
  372.             
  373.             $file=$request->files->get('file');
  374.              if ($file) {
  375.                 $installateur $installateurRepository->findOneBy(["id" => $request->get("idinstallateur")]);
  376.                 if ($request->get("iddocfacture")) {
  377.                     $docfacture $docfactureRepository->findOneBy(["id" => $request->get("iddocfacture")]);
  378.                 }else{
  379.                     $docfacture = new Docfacture();
  380.                     $docfacture->setAaf($factureAaf);
  381.                     $docfacture->setInstallateur($installateur);
  382.                 }
  383.                 $now = new DateTime();
  384.                 $date $now->format('d-m-Y');
  385.                    
  386.                     $destination $this->getParameter('installateur_directory');
  387.                     $fileName explode('.',$file->getClientOriginalName())[0].'.'.$file->getClientOriginalExtension();
  388.                     $fileName $request->get("donne")."-".$factureAaf->getNumero()."-".$installateur->getSociete().'-'.$date.'.'.$file->getClientOriginalExtension();
  389.                    
  390.                     $file->move($destination$fileName);
  391.                     if ($request->get("donne")=='model') {
  392.                         $docfacture->setModel($fileName);
  393.                     }
  394.                     if ($request->get("donne")=='facture') {
  395.                         $docfacture->setFacture($fileName);
  396.                     }
  397.                     $docfactureRepository->add($docfacturetrue);
  398.                   
  399.                     // $activiteDeal = new ActiviteDeal();
  400.                     if ($this->isGranted('ROLE_INSTALLATEUR')) {
  401.                        
  402.                         $admins $equipeRepository->findAll();
  403.                                 
  404.                                 foreach ($admins as $key => $value) {
  405.                                     $listrole $value->getRole();
  406.                                   
  407.                                        
  408.                                         if($listrole == "ROLE_MANAGER" || $listrole == "ROLE_ADMIN" ){
  409.                                             $notifications = new Notifications();
  410.                                             $notifications->setUserId($value->getUser());
  411.                                             $notifications->setAction("app_facture_aaf_show");
  412.                                             $notifications->setMessage($installateur->getSociete()."à ajoutés sa facture AAf : ".$factureAaf->getNumero());
  413.                                             $notifications->setActionId($factureAaf->getId());
  414.                                             $notifications->setSender($this->getUser());
  415.                                             $notifications->setStatuts("non lu");
  416.                                             $notifications->setDate(new DateTime());
  417.         
  418.                                             $notificationsRepository->add($notificationstrue);
  419.                                         }
  420.                                     
  421.                                     
  422.                                     
  423.                                 }
  424.                         // $admins = $equipeRepository->findAll();
  425.                         // foreach ($admins as $key => $value) {
  426.                         //     $listPermissions = $value->getPermissions();
  427.                         //     foreach ($listPermissions as $v) {
  428.                         //         if($v == "NOTIFICATION" ){
  429.                         //             $notifications = new Notifications();
  430.                         //             $notifications->setUserId($value->getUser());
  431.                         //             $notifications->setAction("app_deal_edit");
  432.                         //             $notifications->setMessage("Des documents ont été ajoutés pour le deal ".$champsDeal->getNumero());
  433.                         //             $notifications->setActionId($champsDeal->getId());
  434.                         //             $notifications->setSender($this->getUser());
  435.                         //             $notifications->setStatuts("non lu");
  436.                         //             $notifications->setDate(new DateTime());
  437.                         //             $notificationsRepository->add($notifications, true);
  438.                         //         }
  439.                         //     }
  440.                         // }
  441.                         // $activiteDeal->setFonction('installateur');
  442.                         // $activiteDeal->setInstallateur($this->getUser()->getInstallateur());
  443.                     }else{
  444.                        
  445.                         // $activiteDeal->setFonction('equipe');
  446.                         // $activiteDeal->setEquipe($this->getUser()->getEquipe());
  447.                     }
  448.                    
  449.                     // $activiteDeal->setCommentaire("À déposé le document ".$champsDeal->getNom());
  450.                     // $activiteDeal->setDate(new Datetime());
  451.                     // $activiteDeal->setDeal($champsDeal->getDeal());
  452.                     // $activiteDealRepository->add($activiteDeal, true);
  453.                     // Historique
  454.                     // $historiqueDeal = new HistoriqueDeal();
  455.                     // $historiqueDeal->setUser($this->getUser());
  456.                     // $historiqueDeal->setDeal($champsDeal->getDeal());
  457.                     // $historiqueDeal->setDate(new DateTime());
  458.                     // $historiqueDeal->setAction("Importation du document ".$champsDeal->getNom()." dans le Deal ".$champsDeal->getDeal()->getNumero());
  459.                     // $historiqueDealRepository->add($historiqueDeal, true);
  460.                     return $this->json(['success' => true'message' => 'Fichier téléchargé et ajouté en base de données avec succès']);
  461.              }
  462.         }
  463.         return $this->json(['success' => false'error' => 'Erreur lors du téléchargement du fichier']);
  464.     }
  465. }