src/Controller/DefaultController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\DotationType;
  4. use App\Services\ArticleStockService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use App\Entity\Article;
  10. use App\Entity\Reseau;
  11. use App\Entity\User;
  12. use App\Entity\Dotation;
  13. use App\Entity\Commande;
  14. use App\Form\CommandeClassementType;
  15. use App\Form\DotationListType;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class DefaultController extends AbstractController
  19. {
  20.     /**
  21.      * @Route("/", name="app_homepage")
  22.      */
  23.     public function indexAction(
  24.         Request                $request,
  25.         EntityManagerInterface $em,
  26.         ArticleStockService    $articleStockService
  27.     )
  28.     {
  29.         if ($this->getUser() != null) {
  30.             // Affichage du tableau de bord
  31.             if ($this->getUser()->getProfil()->getAccesBo()) {
  32.                 $date date("Y-m-d");
  33.                 $dateMonthAfter date('Y-m-d'strtotime('+6 month'strtotime($date)));
  34.                 //// récupère les articles en fin de visa (6 mois)
  35.                 $articles $em->getRepository(Article::class)->getArticleFinVisa($date$dateMonthAfter);
  36.                 //// Date il y a 6 mois
  37.                 $dateSixMonths date('Y-m-d'strtotime('-6 month'strtotime($date)));
  38.                 $classementArticles $em->getRepository(Article::class)->getArticlesThird($date$dateSixMonths);
  39.                 $cptReseau 0;
  40.                 $tabClassement = [];
  41.                 $nbArticleClassement 3;
  42.                 $reseau "";
  43.                 foreach ($classementArticles as $lignes) {
  44.                     if ($reseau != $lignes['Reseau']) {
  45.                         $cptReseau 0;
  46.                         $reseau $lignes['Reseau'];
  47.                     }
  48.                     if ($reseau == $lignes['Reseau']) {
  49.                         if ($cptReseau $nbArticleClassement) {
  50.                             $tabClassement[$lignes['Reseau']][] = $lignes;
  51.                             $cptReseau++;
  52.                         }
  53.                     }
  54.                 }
  55.                 $commandeValidationAdmin $em->getRepository(Commande::class)->getCommandeAttenteValidationAdmin();
  56.                 $commandeValidationResponsable $em->getRepository(Commande::class)
  57.                                                     ->getCommandeAttenteValidationResponsable();
  58.                 $nbCommandeValAdmin count($commandeValidationAdmin);
  59.                 $nbCommandeValResponsable count($commandeValidationResponsable);
  60.                 //// SELECT DR / Fichier Js (commande_classement.js = ajax) / CommandeClassementType.php (form)
  61.                 $form $this->createForm(CommandeClassementType::class);
  62.                 $formdotation $this->createForm(DotationListType::class);
  63.                 // Statistiques articles alertes
  64.                 $articlesAlerteStockTemps $em->getRepository(Article::class)->getArticleAlerteStockTDB();
  65.                 $artAlerte = [];
  66.                 $tabNbVM $articleStockService->getNbVMParReseau();
  67.                 foreach ($articlesAlerteStockTemps as $art) {
  68.                     $infoStock $articleStockService->getInfoStockArticle($art);
  69.                     $nbVM $articleStockService->getNBVMArticle($art$tabNbVM);
  70.                     $qteParVm 0;
  71.                     if ($nbVM && $art->getConditionnement() > 0) {
  72.                         $qteParVm floor(($infoStock["total"] / $nbVM) / $art->getConditionnement()) * $art->getConditionnement();
  73.                     }
  74.                     if ($qteParVm $art->getQteMin()) {
  75.                         $art->datas = [
  76.                             "disponible" => $infoStock["disponible"],
  77.                             "reserve" => $infoStock["reserve"],
  78.                             "reserveCommande" => $infoStock["reserveCommande"],
  79.                             "reassort" => $infoStock["reassort"],
  80.                             "total" => $infoStock["total"],
  81.                             "nbVM" => $nbVM,
  82.                             "quantiteParVM" => $qteParVm,
  83.                         ];
  84.                         $artAlerte[] = $art;
  85.                     }
  86.                 }
  87.                 $nowTime time();
  88.                 $date5Months date('Y-m-d'strtotime('-5 month'$nowTime));
  89.                 $dateDebut date('Y-m-d'strtotime('first day of this month'strtotime($date5Months)));
  90.                 $dateFin date('Y-m-d'strtotime('last day of this month'$nowTime));
  91.                 //$dateDebut = "2018-01-01";
  92.                 //$dateFin = "2018-07-31";
  93.                 //$dateDebut = "2018-12-01";
  94.                 //$dateFin = "2019-05-01";
  95.                 // echo $dateDebut." * ".$dateFin;
  96.                 //exit;
  97.                 $dateCptTimeStamp strtotime($dateDebut);
  98.                 $dateFinTimeStamp strtotime($dateFin);
  99.                 $moisStatsArticles = array();
  100.                 while ($dateCptTimeStamp $dateFinTimeStamp) {
  101.                     $dateCptStr date('Y-m-d'$dateCptTimeStamp);
  102.                     $moisStatsArticles[] = date_format(date_create_from_format("Y-m-d"$dateCptStr), "m/Y");
  103.                     $dateCptTimeStamp strtotime('+1 month'$dateCptTimeStamp);
  104.                 }
  105.                 $reseauxStatsArticles $em->getRepository(Reseau::class)->getAllForStatsOrder();
  106.                 $top3articlesByReseauAndByMois $em->getRepository(Article::class)
  107.                                                     ->getArticlesClassementByReseauAndByMois($dateDebut$dateFin);
  108.                 //Renvoie un tableau de la forme : $tab["NOM_RESEAU"]["MM/YYYY"]["CODE_THEA"] = {"CodeThea"=>"XX", "libelle"=>"XXX", "qte"=>X}
  109.             } else {
  110.                 $repoDotation $em->getRepository(Dotation::class);
  111.                 $dotations $repoDotation->getDotationOuverteForUser($this->getUser());
  112.                 if (count($dotations) == 1) {
  113.                     $request->getSession()->set('dotation'$dotations[0]);
  114.                 }
  115.                 return $this->redirect($this->generateUrl('frontoffice'));
  116.             }
  117.             return $this->render('Default/index.html.twig',
  118.                 array(
  119.                     'articles' => $articles,
  120.                     'classementArticles' => $tabClassement,
  121.                     'nbCommandeValAdmin' => $nbCommandeValAdmin,
  122.                     'nbCommandeValResponsable' => $nbCommandeValResponsable,
  123.                     'form' => $form->createView(),
  124.                     'formdotation' => $formdotation->createView(),
  125.                     'articlesAlerteStock' => $artAlerte,
  126.                     'top3articlesByReseauAndByMois' => $top3articlesByReseauAndByMois,
  127.                     'reseauxStatsArticles' => $reseauxStatsArticles,
  128.                     'moisStatsArticles' => $moisStatsArticles,
  129.                 )
  130.             );
  131.         } else {
  132.             return $this->redirect($this->generateUrl('app_login'));
  133.         }
  134.     }
  135.     /**
  136.      * Fonction qui retourne la liste des commandes en fonction du DR
  137.      * Ne prends pas en compte le DR dans l'affichage du classement
  138.      *
  139.      * @Route("/getCommandeClassement/", name="getCommandeClassement", methods={"POST"}, options={"expose"= "true"})
  140.      *
  141.      * @param Request $request [description]
  142.      * @return [type]           [description]
  143.      */
  144.     public function getCommandeClassement(
  145.         Request                $request,
  146.         EntityManagerInterface $entityManager
  147.     )
  148.     {
  149.         if (!$request->isXmlHttpRequest()) {
  150.             return new Response("Not Found"404);
  151.         }
  152.         $em $entityManager;
  153.         $em->getFilters()->disable('softdeleteable');
  154.         /// Récupère l'id du DR select
  155.         $userId $request->request->get('user_id');
  156.         $date date("Y-m-d");
  157.         $dateCommandes date('Y-m-d'strtotime('-6 month'strtotime($date)));
  158.         $ligneClassements $em->getRepository(Commande::class)->getCommmandeClassement($userId$date$dateCommandes);
  159.         $tabClassement = [];
  160.         $reseau null;
  161.         $user null;
  162.         foreach ($ligneClassements as $ligne) {
  163.             if ($reseau != $ligne['Reseau']) {
  164.                 $reseau $ligne['Reseau'];
  165.                 $tabClassement[$reseau] = [];
  166.             }
  167.             if ($user !== $ligne['idUser']) {
  168.                 $user $ligne['idUser'];
  169.                 $tabClassement[$reseau][$user]["commandeTotale"] = 0;
  170.                 $tabClassement[$reseau][$user]["nbCommandeSieCgs"] = 0;
  171.                 $tabClassement[$reseau][$user]["nbCommandeSysRea"] = 0;
  172.                 $tabClassement[$reseau][$user]["michel"] = 0;
  173.                 $tabClassement[$reseau][$user]["name"] = $ligne['name'];
  174.                 $tabClassement[$reseau][$user]["firstname"] = $ligne['firstname'];
  175.                 $tabClassement[$reseau][$user]["couleur"] = $ligne['couleur'];
  176.                 $tabClassement[$reseau][$user]["reseau"] = $ligne['Reseau'];
  177.             }
  178.             $tabClassement[$reseau][$user]['commandeTotale']++;
  179.             if ($ligne['DotationType'] == DotationType::SIEGE) {
  180.                 $tabClassement[$reseau][$user]['nbCommandeSieCgs']++;
  181.             } else if ($ligne['DotationType'] == DotationType::CONGRES) {
  182.                 $tabClassement[$reseau][$user]['nbCommandeSieCgs']++;
  183.             } else if ($ligne['DotationType'] == DotationType::REAPPRO) {
  184.                 $tabClassement[$reseau][$user]['nbCommandeSysRea']++;
  185.             } else if ($ligne['DotationType'] == DotationType::SYSTEMATIQUE) {
  186.                 $tabClassement[$reseau][$user]['nbCommandeSysRea']++;
  187.             }
  188.         }
  189.         foreach ($ligneClassements as $ligne) {
  190.             usort($tabClassement[$ligne['Reseau']], [$this'triTableau']);
  191.         }
  192.         return new JsonResponse($tabClassement);
  193.     }
  194.     public function triTableau(
  195.         $a,
  196.         $b
  197.     )
  198.     {
  199.         if ($a["commandeTotale"] == $b["commandeTotale"]) {
  200.             return 0;
  201.         }
  202.         return ($a["commandeTotale"] > $b["commandeTotale"]) ? -1;
  203.     }
  204.     /**
  205.      * Fonction qui retourn le nombre de délégué ou nombre de commande selon type
  206.      *
  207.      * @Route("/getInfoDotByType", name="getInfoDotByType", methods={"POST"}, options={"expose"= "true"})
  208.      *
  209.      * @param Request $request [description]
  210.      * @return [type]           [description]
  211.      */
  212.     public function getInfoDotByType(
  213.         Request                $request,
  214.         EntityManagerInterface $entityManager
  215.     )
  216.     {
  217.         if (!$request->isXmlHttpRequest()) {
  218.             return new Response("Not Found"404);
  219.         } else {
  220.             $em $entityManager;
  221.             $em->getFilters()->disable('softdeleteable');
  222.             $id $request->request->get('dotation_id');
  223.             $dotation $em->getRepository(Dotation::class)->getInfoDotation($id);
  224.             $type $dotation->getType()->getCode();
  225.             $totalCommande null;
  226.             $tab null;
  227.             if ($type == "REA") {
  228.                 $users $em->getRepository(User::class)->getNBCommandeByReseau($dotation);
  229.                 foreach ($dotation->getReseaux() as $reseau) {
  230.                     $tab[$reseau->getId()] = [];
  231.                     $tab[$reseau->getId()]['reseaux'] = $reseau;
  232.                     $tab[$reseau->getId()]['utilisateurs'] = [];
  233.                     foreach ($users as $user) {
  234.                         if ($user->getReseau() == $reseau) {
  235.                             $tab[$user->getReseau()->getId()]['utilisateurs'][] = $user;
  236.                         }
  237.                     }
  238.                     $tab[$reseau->getId()]['nbVM'] = count($tab[$reseau->getId()]['utilisateurs']);
  239.                 }
  240.             } elseif ($type == "CGS" || $type == "SIE") {
  241.                 $commandes $em->getRepository(Commande::class)->getCommandesByTypeDotation($id);
  242.                 $totalCommande count($commandes);
  243.             }
  244.         }
  245.         $response = new JsonResponse(array("Dotation" => $dotation"Total" => $totalCommande"NombreDelegues" => $tab));
  246.         return $response;
  247.     }
  248.     /**
  249.      * @Route("/conditions", name="condition_page")
  250.      */
  251.     public function getConditionPageAction()
  252.     {
  253.         return $this->render('Default/condition.html.twig');
  254.     }
  255. }