<?php
namespace App\Controller;
use App\Entity\Contrat;
use App\Entity\Document;
use App\Entity\Projet;
use App\Entity\Societe;
use App\Entity\Tiers;
use App\Model\NoteDeFrais\NDF;
use App\Service\Pdf;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
class PdfController extends AbstractController
{
private ?ObjectManager $em = null;
private ?string $prefixSociete = null;
/**
* Stockage des informations de la société pour affichage sur entête document
* @var object|mixed|null
*/
private ?object $dataSociete = null;
public function __construct(RequestStack $requestStack, ManagerRegistry $doctrine)
{
$request = $requestStack->getCurrentRequest();
//TODO: page erreur subdomain
if(null===$request->getSession()->get('societe')) return $this->render('misc/404.html.twig');
// on récupère le sous-domaine stocké en session pour pointer vers le bon entity manager
$subdomain = $request->getSession()->get('societe')->getBase();
$this->em = $doctrine->getManager($subdomain);
$this->prefixSociete = $request->getSession()->get('societe')->getPrefix();
if(null!==$this->prefixSociete) $this->prefixSociete = strtoupper($this->prefixSociete);
$jsonString = $request->getSession()->get('societe')->getData();
if(null!==$jsonString) {
$this->dataSociete = json_decode($jsonString);
$this->dataSociete->lib_societe = $request->getSession()->get('societe')->getLibSociete();
$this->dataSociete->logo_path = $request->getSession()->get('societe')->getLogoPath();
}
}
/**
* @Route("/pdf/facture/{id}", methods={"GET"}, name="pdf_document")
*/
public function getDocumenntPdf($id, Request $request, Pdf $pdf)
{
// définir le type de document
$documentRepository = $this->em->getRepository(Document::class);
/** @var Document $document */
$document = $documentRepository->find($id);
$lang = $request->query->get('lang','');
if(!empty($lang)) {
$type = $document->getTypeDocument();
$document->setTypeDocument(
str_replace(['FACTURE','AVOIR'], ['INVOICE','CREDIT NOTE'], $type)
);
$lang .= '/';
}
$template = $this->renderView('pdf/'.$lang.'facture.html.twig', [
'societe' => $this->dataSociete,
'document' => $document,
]);
$pdf->create($this->dataSociete);
return $pdf->generatePdf($template, $document->getNumPiece() );
}
/**
* @Route("/pdf/contrat/{id}", methods={"GET"}, name="pdf_contrat")
*/
public function getContratPdf($id, Request $request, Pdf $pdf)
{
$contratRepository = $this->em->getRepository(Contrat::class);
/** @var Contrat $contrat */
$contrat = $contratRepository->find($id);
if(null!==$contrat->getSalarie()->getInfoSalarie()) {
$webPath = $this->getParameter('kernel.project_dir');
$contrat->getSalarie()->getInfoSalarie()->initEncryptionKey($webPath)->decryptContent(); // déchiffrage des données info_salariés
}
$projetRepository = $this->em->getRepository(Projet::class);
/** @var Projet $projet */
$projet = $projetRepository->find( $contrat->getIdProjet() );
$lang = $request->query->get('lang','');
if(!empty($lang)) $lang.= '/';
$template = $this->renderView('pdf/'.$lang.'contrat.html.twig', [
'societe' => $this->dataSociete,
'projet' => $projet,
'contrat' => $contrat,
'only_first_page' => false,
]);
$pdf->create($this->dataSociete);
return $pdf->generatePdf($template, $contrat->getId(), true);
}
/**
* @Route("pdf/contrat-mensuel/{date}", methods={"GET"}, name="pdf_contrat_mensuel")
*/
public function getContratMensuelPdf($date, Request $request, Pdf $pdf)
{
$date = explode('-', $date);
if(2!==count($date)) die('Erreur dans la date. Format correct : aaaa-mm');
$annee = $date[0];
$mois = $date[1];
$contratRepository = $this->em->getRepository(Contrat::class);
$contratList = $contratRepository->findBy([
'annee' => $annee,
'mois' => intval($mois),
]);
if(empty($contratList)) die('Aucun contrat sur ce mois : '. $mois .'-'. $annee);
$lang = $request->query->get('lang','');
if(!empty($lang)) $lang.= '/';
$webPath = $this->getParameter('kernel.project_dir');
$projetRepository = $this->em->getRepository(Projet::class);
$pdf->create($this->dataSociete);
/** @var Contrat $contrat */
foreach ($contratList as $contrat){
if(null!==$contrat->getSalarie()->getInfoSalarie())
$contrat->getSalarie()->getInfoSalarie()->initEncryptionKey($webPath)->decryptContent();
/** @var Projet $projet */
$projet = $projetRepository->find( $contrat->getIdProjet() );
$template = $this->renderView('pdf/'.$lang.'contrat.html.twig', [
'societe' => $this->dataSociete,
'projet' => $projet,
'contrat' => $contrat,
'only_first_page' => true,
]);
$pdf->writeHTML($template);
}
$nomFichier = 'Contrats-'. $annee .'-'. $mois;
return $pdf->generatePdf('', $nomFichier, true);
}
/**
* @Route("pdf/ndf", methods={"GET"}, name="pdf_ndf")
*/
public function getNdfPdf(Request $request, Pdf $pdf)
{
$filtre = $request->query->all();
$format = '';
if(isset($filtre['id_document'])) $format = 'by-id';
if(!empty($filtre['id_projet']) && !empty($filtre['id_salarie']) && !empty($filtre['date_ndf'])) $format = 'by-projet-salarie-mois';
if(empty($filtre['id_projet']) && empty($filtre['id_salarie']) && !empty($filtre['date_ndf'])) $format = 'by-mois';
if(!empty($filtre['id_projet']) && empty($filtre['id_salarie']) && empty($filtre['date_ndf'])) $format = 'by-projet';
if(empty($filtre['id_projet']) && !empty($filtre['id_salarie']) && !empty($filtre['date_ndf'])) $format = 'by-salarie-mois';
if(!empty($filtre['date_ndf']) && preg_match('/&lang=en_GB/', $filtre['date_ndf'])) {
$filtre['date_ndf'] = preg_replace('/&lang=en_GB/', '', $filtre['date_ndf']);
$request->query->set('lang','en_GB');
}
$documentRepository = $this->em->getRepository(Document::class);
/** @var Document $document */
$docList = $documentRepository->findByFilter($filtre);
$ndf = new NDF($docList[0]->getProjet(), new Societe(), new Tiers(), 0);
$lang = $request->query->get('lang','');
if(!empty($lang)) $lang.= '/';
$template = $this->renderView('pdf/'.$lang.'ndf.html.twig', [
'societe' => $this->dataSociete,
'format_ndf' => $format,
'document_list' => $docList,
'type_list' => $ndf->getTypeList(),
'periode' => $filtre['date_ndf'] ?? '',
'projet' => $docList[0]->getProjet()->getNumProd() ?? '',
]);
$pdf->create($this->dataSociete,'L');
$name = 'NDF-';
switch ($format) {
case 'by-id':
case 'by-projet-salarie-mois':
$name.= urlencode($docList[0]->getSalarie()->getNomTiers()) .'-'. urlencode($docList[0]->getSalarie()->getPrenomTiers()) .'-'. $docList[0]->getProjet()->getNumProd();
break;
case 'by-salarie-mois':
$name.= urlencode($docList[0]->getSalarie()->getNomTiers()) .'-'. urlencode($docList[0]->getSalarie()->getPrenomTiers()) .'-'. $filtre['date_ndf'];
break;
case 'by-projet':
$name.= $docList[0]->getProjet()->getNumProd();
break;
case 'by-mois':
$name.= $filtre['date_ndf'];
break;
}
return $pdf->generatePdf($template, $name);
}
}