src/Controller/GestionDocumentController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Document;
  4. use App\Tools\JsonRes;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Doctrine\Persistence\ObjectManager;
  12. class GestionDocumentController extends AbstractController
  13. {
  14.     private ?ObjectManager $em null;
  15.     private ?string $prefixSociete null;
  16.     /**
  17.      * Stockage des informations de la société pour affichage sur entête document
  18.      * @var object|mixed|null
  19.      */
  20.     private ?object $dataSociete null;
  21.     public function __construct(RequestStack $requestStackManagerRegistry $doctrine)
  22.     {
  23.         $request $requestStack->getCurrentRequest();
  24.         //TODO: page erreur subdomain
  25.         if(null===$request->getSession()->get('societe')) return $this->render('misc/404.html.twig');
  26.         // on récupère le sous-domaine stocké en session pour pointer vers le bon entity manager
  27.         $subdomain $request->getSession()->get('societe')->getBase();
  28.         $this->em $doctrine->getManager($subdomain);
  29.         $this->prefixSociete $request->getSession()->get('societe')->getPrefix();
  30.         if(null!==$this->prefixSociete$this->prefixSociete strtoupper($this->prefixSociete);
  31.         $jsonString $request->getSession()->get('societe')->getData();
  32.         if(null!==$jsonString) {
  33.             $this->dataSociete json_decode($jsonString);
  34.             $this->dataSociete->lib_societe $request->getSession()->get('societe')->getLibSociete();
  35.             $this->dataSociete->logo_path $request->getSession()->get('societe')->getLogoPath();
  36.         }
  37.     }
  38.     /**
  39.      * @Route("/gestion/{type}", methods={"GET"}, name="gestion_document")
  40.      */
  41.     public function gestionDocument($type): Response
  42.     {
  43.         //TODO: page erreur entity manager
  44.         if(null===$this->em) return $this->render('misc/404.html.twig');
  45.         $url $type;
  46.         $filename preg_replace('/-/''_'$type);
  47.         $type strtoupper(preg_replace('/-/'' '$type));
  48.         $labelType $this->getLabelType($type);
  49.         $documentRepository $this->em->getRepository(Document::class);
  50.         $allowedType $documentRepository->getAllType();
  51.         if(!in_array($type$allowedType)) return $this->render('misc/404.html.twig');
  52.         $filtre = ['type_document' => $type];
  53.         $view 'document/gestion_document.html.twig';
  54.         switch ($type){
  55.             case 'FACTURE PROFORMA':
  56.                 $filtre['id_doc_origine'] = 'NULL';
  57.                 break;
  58.             case 'FACTURE':
  59.                 $filtre['statut_document'] = 'envoyé';
  60.                 break;
  61.             case 'COMMANDE PRESTATAIRE':
  62.                 $filtre['statut_document'] = 'Non payée';
  63.                 break;
  64.             case 'NDF':
  65.                 $view 'document/ndf/gestion_ndf.html.twig';
  66.                 $filtre['type_document'] = 'TABLEAU VIDE';
  67.                 break;
  68.             default: break;
  69.         }
  70.         $docList $documentRepository->findByFilter($filtre);
  71.         $totalHt $this->getTotalAmount('HT'$docList);
  72.         $totalTva $this->getTotalAmount('TVA'$docList);
  73.         $totalTtc $this->getTotalAmount('TTC'$docList);
  74.         return $this->render($view, [
  75.             'id_menu' => 'gestion_'$filename,
  76.             'url' => $url,
  77.             'type' => $type,
  78.             'label_type' => $labelType,
  79.             'doc_list' => $docList,
  80.             'total_ht' => $totalHt,
  81.             'total_tva' => $totalTva,
  82.             'total_ttc' => $totalTtc,
  83.         ]);
  84.     }
  85.     /**
  86.      * @Route("/gestion/{type}/reload-list", methods={"POST"}, name="gestion_document_reload")
  87.      */
  88.     public function reloadList($typeRequest $request): Response
  89.     {
  90.         $filename preg_replace('/-/''_'$type);
  91.         $type strtoupper(preg_replace('/-/'' '$type));
  92.         $labelType $this->getLabelType($type);
  93.         $documentRepository $this->em->getRepository(Document::class);
  94.         $allowedType $documentRepository->getAllType();
  95.         if(!in_array($type$allowedType)) return $this->render('misc/404.html.twig');
  96.         $res = new JsonRes($request->request->all());
  97.         $filtre $res->getData();
  98.         $filtre['type_document'] = $type;
  99.         $view 'document/document_list.html.twig';
  100.         switch ($type){
  101.             case 'FACTURE PROFORMA':
  102.                 $filtre['id_doc_origine'] = $filtre['etat_document'];
  103.                 break;
  104.             case 'FACTURE':
  105.             case 'COMMANDE PRESTATAIRE':
  106.                 $filtre['statut_document'] = $filtre['etat_document'];
  107.                 if('avoir'===$filtre['etat_document']) $filtre['type_document'] = 'AVOIR';
  108.                 break;
  109.             case 'NDF':
  110.                 $view 'document/ndf/ndf_list.html.twig';
  111.                 break;
  112.             default: break;
  113.         }
  114.         $orderBy = [];
  115.         if(!empty($filtre['order_by'])) $orderBy $filtre['order_by'];
  116.         $docList $documentRepository->findByFilter($filtre$orderBy);
  117.         $totalHt $this->getTotalAmount('HT'$docList);
  118.         $totalTva $this->getTotalAmount('TVA'$docList);
  119.         $totalTtc $this->getTotalAmount('TTC'$docList);
  120.         return $this->render($view, [
  121.             'type' => $type,
  122.             'label_type' => $labelType,
  123.             'doc_list' => $docList,
  124.             'total_ht' => $totalHt,
  125.             'total_tva' => $totalTva,
  126.             'total_ttc' => $totalTtc,
  127.         ]);
  128.     }
  129.     /**
  130.      * @param string $type
  131.      * @return string
  132.      */
  133.     private function getLabelType(string $type): string
  134.     {
  135.         if('NDF'===$type) return 'NOTES DE FRAIS';
  136.         return join(' ',
  137.             array_map(function ($e) {
  138.                 return $e !== 'PROFORMA' $e 'S' $e;
  139.             }, explode(' '$type)
  140.         ));
  141.     }
  142.     /**
  143.      * @param string $type
  144.      * @param array $docList
  145.      * @return float
  146.      */
  147.     private function getTotalAmount(string $type, array $docList): float
  148.     {
  149.         if(empty($type)) return 0.0;
  150.         $sumHt $sumTva 0.0;
  151.         /** @var Document $document */
  152.         foreach ($docList as $document){
  153.             $sumHt+= $document->getMontantHt();
  154.             $sumTva+= $document->getMontantTva();
  155.         }
  156.         if ('HT'===$type) return $sumHt;
  157.         if ('TVA'===$type) return $sumTva;
  158.         if ('TTC'===$type) return $sumHt $sumTva;
  159.         return 0.0;
  160.     }
  161. }