src/Controller/IndexController.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Affaire;
  4. use App\Entity\Attribution;
  5. use App\Entity\CommandeFournisseur;
  6. use App\Entity\CommandePrestataire;
  7. use App\Entity\Devis;
  8. use App\Form\CreateAffaireType;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class IndexController extends AbstractController
  14. {
  15.     /**
  16.      * Méthode permettant de trier les devis par date
  17.      * @param $devis
  18.      */
  19.     private function sortDateDevis(&$devis)
  20.     {
  21.         usort($devis, function ($a$b) {
  22.             if (strtotime($a->getDateDevis()->format('Y-m-d H:i:s')) === strtotime($b->getDateDevis()->format('Y-m-d H:i:s'))) {
  23.                 return 0;
  24.             }
  25.             return (strtotime($a->getDateDevis()->format('Y-m-d H:i:s')) < strtotime($b->getDateDevis()->format('Y-m-d H:i:s'))) ? +: -1;
  26.         });
  27.     }
  28.     /**
  29.      * Méthode pour récupérer la liste des affaires
  30.      * @param $args
  31.      * @return array|void
  32.      */
  33.     private function getAffaireList($args$affaireList = [])
  34.     {
  35.         if (is_array($args)) {
  36.             foreach ($args as $key => $arg) {
  37.                 $affaire $arg->getAffaire();
  38.                 if (!in_array($affaire$affaireList)) {
  39.                     array_push($affaireList$affaire);
  40.                 }
  41.             }
  42.             return $affaireList;
  43.         } else {
  44.             $affaire $args->getFacture()->getAffaire();
  45.             if (!in_array($affaire$affaireList)) {
  46.                 return   $args->getFacture()->getAffaire();
  47.             }
  48.         }
  49.     }
  50.     private function countDevisToValidate()
  51.     {
  52.         $em $this->getDoctrine()->getManager();
  53.         $sql 'SELECT COUNT(*) FROM devis where affaire_id IS NULL and validation = 0';
  54.         $result $em->getConnection()->FetchAll($sql);
  55.         return $result[0]['count'];
  56.     }
  57.     private function countCommandeToValidate()
  58.     {
  59.         $em $this->getDoctrine()->getManager();
  60.         $sql 'SELECT COUNT(*) FROM commande_fournisseur where affaire_id IS NULL and validation = 0';
  61.         $result $em->getConnection()->FetchAll($sql);
  62.         return $result[0]['count'];
  63.     }
  64.     /**
  65.      * v2 - Page index des devis
  66.      *
  67.      * @param Request $request
  68.      * @return Response
  69.      * @Route("/", name="app_index", methods={"GET"})
  70.      */
  71.     public function appIndex(Request $request)
  72.     {
  73.         return $this->redirectToRoute('sudalys_dashboard_user');
  74.     }
  75.     /**
  76.      * Méthode de base qui permet de gérer le tableau de bord
  77.      * @Route("/affaire", name="sudalys_gestion_home", methods={"GET"})
  78.      * @return Response
  79.      */
  80.     public function indexAction()
  81.     {
  82.         // on liste toutes les affaires
  83.         $listeAffaire $this->getDoctrine()->getRepository(Affaire::class)->findAll();
  84.         // on récupère l'objet user via l'id de l'utilisateur connecté
  85.         $userId $this->getUser()->getId();
  86.         $devisToValidate $this->countDevisToValidate();
  87.         $commandeToValidate $this->countCommandeToValidate();
  88.         // on récupère les attributions de l'user connecté
  89.         $userAttribution $this->getDoctrine()->getRepository(Attribution::class)->findBy([
  90.             'user' => $userId
  91.         ]);
  92.         $commandeFournisseur null;
  93.         $commandePrestataire null;
  94.         // on récupère tous les devis n'appartenenant pas à une affaire
  95.         if ($this->getUser()->getRoles()[0] === 'ROLE_SUPER_SUDALYS' || $this->getUser()->getRoles()[0] === 'ROLE_SUDALYS_ADMINISTRATIF') {
  96.             $orphanDevisAll = [];
  97.             if ($this->getUser()->getRoles()[0] === 'ROLE_SUPER_SUDALYS') {
  98.                 $orphanDevisAll $this->getDoctrine()->getRepository(Devis::class)->findBy([
  99.                     'affaire' => null
  100.                 ]);
  101.             }
  102.             $commandeFournisseur $this->getDoctrine()->getRepository(CommandeFournisseur::class)->findBy(['affaire' => null]);
  103.             $commandePrestataire $this->getDoctrine()->getRepository(CommandePrestataire::class)->findBy(['affaire' => null]);
  104.         } else {
  105.             // on récupère les auteurs des devis orphelins
  106.             $orphanDevisAll $this->getDoctrine()->getRepository(Devis::class)->findBy([
  107.                 'affaire' => null,
  108.                 'user' => $userId
  109.             ]);
  110.         }
  111.         $this->sortDateDevis($orphanDevisAll);
  112.         return $this->render('affaire/index.html.twig', [
  113.             "listeAffaire" => $listeAffaire,
  114.             "userAttribution" => $userAttribution,
  115.             "orphanDevisAll" => $orphanDevisAll,
  116.             "commandeFournisseur" => $commandeFournisseur,
  117.             'devisToValidate' => $devisToValidate,
  118.             'commandeToValidate' => $commandeToValidate,
  119.             'commandePrestataire' => $commandePrestataire
  120.         ]);
  121.     }
  122.     /**
  123.      * Méthode de base qui permet de gérer le tableau de bord
  124.      * @return Response
  125.      */
  126.     public function indexActionOld()
  127.     {
  128.         // on liste toutes les affaires
  129.         $listeAffaire $this->getDoctrine()->getRepository(Affaire::class)->findAll();
  130.         // on récupère l'objet user via l'id de l'utilisateur connecté
  131.         $userId $this->getUser()->getId();
  132.         $devisToValidate $this->countDevisToValidate();
  133.         $commandeToValidate $this->countCommandeToValidate();
  134.         // on récupère les attributions de l'user connecté
  135.         $userAttribution $this->getDoctrine()->getRepository(Attribution::class)->findBy([
  136.             'user' => $userId
  137.         ]);
  138.         $commandeFournisseur null;
  139.         $commandePrestataire null;
  140.         // on récupère tous les devis n'appartenenant pas à une affaire
  141.         if ($this->getUser()->getRoles()[0] === 'ROLE_SUPER_SUDALYS' || $this->getUser()->getRoles()[0] === 'ROLE_SUDALYS_ADMINISTRATIF') {
  142.             $orphanDevisAll = [];
  143.             if ($this->getUser()->getRoles()[0] === 'ROLE_SUPER_SUDALYS') {
  144.                 $orphanDevisAll $this->getDoctrine()->getRepository(Devis::class)->findBy([
  145.                     'affaire' => null
  146.                 ]);
  147.             }
  148.             $commandeFournisseur $this->getDoctrine()->getRepository(CommandeFournisseur::class)->findBy(['affaire' => null]);
  149.             $commandePrestataire $this->getDoctrine()->getRepository(CommandePrestataire::class)->findBy(['affaire' => null]);
  150.         } else {
  151.             // on récupère les auteurs des devis orphelins
  152.             $orphanDevisAll $this->getDoctrine()->getRepository(Devis::class)->findBy([
  153.                 'affaire' => null,
  154.                 'user' => $userId
  155.             ]);
  156.         }
  157.         $this->sortDateDevis($orphanDevisAll);
  158.         return $this->render('affaire/index.html.twig', [
  159.             "listeAffaire" => $listeAffaire,
  160.             "userAttribution" => $userAttribution,
  161.             "orphanDevisAll" => $orphanDevisAll,
  162.             "commandeFournisseur" => $commandeFournisseur,
  163.             'devisToValidate' => $devisToValidate,
  164.             'commandeToValidate' => $commandeToValidate,
  165.             'commandePrestataire' => $commandePrestataire
  166.         ]);
  167.     }
  168. }