<?php
namespace App\Controller;
use App\Entity\Document;
use App\Tools\JsonRes;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
class GestionDocumentController 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("/gestion/{type}", methods={"GET"}, name="gestion_document")
*/
public function gestionDocument($type): Response
{
//TODO: page erreur entity manager
if(null===$this->em) return $this->render('misc/404.html.twig');
$url = $type;
$filename = preg_replace('/-/', '_', $type);
$type = strtoupper(preg_replace('/-/', ' ', $type));
$labelType = $this->getLabelType($type);
$documentRepository = $this->em->getRepository(Document::class);
$allowedType = $documentRepository->getAllType();
if(!in_array($type, $allowedType)) return $this->render('misc/404.html.twig');
$filtre = ['type_document' => $type];
$view = 'document/gestion_document.html.twig';
switch ($type){
case 'FACTURE PROFORMA':
$filtre['id_doc_origine'] = 'NULL';
break;
case 'FACTURE':
$filtre['statut_document'] = 'envoyé';
break;
case 'COMMANDE PRESTATAIRE':
$filtre['statut_document'] = 'Non payée';
break;
case 'NDF':
$view = 'document/ndf/gestion_ndf.html.twig';
$filtre['type_document'] = 'TABLEAU VIDE';
break;
default: break;
}
$docList = $documentRepository->findByFilter($filtre);
$totalHt = $this->getTotalAmount('HT', $docList);
$totalTva = $this->getTotalAmount('TVA', $docList);
$totalTtc = $this->getTotalAmount('TTC', $docList);
return $this->render($view, [
'id_menu' => 'gestion_'. $filename,
'url' => $url,
'type' => $type,
'label_type' => $labelType,
'doc_list' => $docList,
'total_ht' => $totalHt,
'total_tva' => $totalTva,
'total_ttc' => $totalTtc,
]);
}
/**
* @Route("/gestion/{type}/reload-list", methods={"POST"}, name="gestion_document_reload")
*/
public function reloadList($type, Request $request): Response
{
$filename = preg_replace('/-/', '_', $type);
$type = strtoupper(preg_replace('/-/', ' ', $type));
$labelType = $this->getLabelType($type);
$documentRepository = $this->em->getRepository(Document::class);
$allowedType = $documentRepository->getAllType();
if(!in_array($type, $allowedType)) return $this->render('misc/404.html.twig');
$res = new JsonRes($request->request->all());
$filtre = $res->getData();
$filtre['type_document'] = $type;
$view = 'document/document_list.html.twig';
switch ($type){
case 'FACTURE PROFORMA':
$filtre['id_doc_origine'] = $filtre['etat_document'];
break;
case 'FACTURE':
case 'COMMANDE PRESTATAIRE':
$filtre['statut_document'] = $filtre['etat_document'];
if('avoir'===$filtre['etat_document']) $filtre['type_document'] = 'AVOIR';
break;
case 'NDF':
$view = 'document/ndf/ndf_list.html.twig';
break;
default: break;
}
$orderBy = [];
if(!empty($filtre['order_by'])) $orderBy = $filtre['order_by'];
$docList = $documentRepository->findByFilter($filtre, $orderBy);
$totalHt = $this->getTotalAmount('HT', $docList);
$totalTva = $this->getTotalAmount('TVA', $docList);
$totalTtc = $this->getTotalAmount('TTC', $docList);
return $this->render($view, [
'type' => $type,
'label_type' => $labelType,
'doc_list' => $docList,
'total_ht' => $totalHt,
'total_tva' => $totalTva,
'total_ttc' => $totalTtc,
]);
}
/**
* @param string $type
* @return string
*/
private function getLabelType(string $type): string
{
if('NDF'===$type) return 'NOTES DE FRAIS';
return join(' ',
array_map(function ($e) {
return $e !== 'PROFORMA' ? $e . 'S' : $e;
}, explode(' ', $type)
));
}
/**
* @param string $type
* @param array $docList
* @return float
*/
private function getTotalAmount(string $type, array $docList): float
{
if(empty($type)) return 0.0;
$sumHt = $sumTva = 0.0;
/** @var Document $document */
foreach ($docList as $document){
$sumHt+= $document->getMontantHt();
$sumTva+= $document->getMontantTva();
}
if ('HT'===$type) return $sumHt;
if ('TVA'===$type) return $sumTva;
if ('TTC'===$type) return $sumHt + $sumTva;
return 0.0;
}
}