src/Controller/Api/Prive/ApiStatistiqueController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\Prive;
  3. use App\Entity\Appel;
  4. use App\Entity\Appelstatut;
  5. use App\Entity\Campagne;
  6. use App\Entity\Campagneprospect;
  7. use App\Entity\Client;
  8. use App\Entity\Contact;
  9. use App\Entity\Prospect;
  10. use App\Entity\Rdv;
  11. use App\Entity\Rdvstatut;
  12. use App\Entity\Utilisateur;
  13. use App\Entity\Ville;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. /**
  18.  * @Route("/statistique")
  19.  */
  20. class ApiStatistiqueController extends ApiController
  21. {
  22.    
  23.     /**
  24.      * @Route("/appelparstatut", name="api_Statistique_appelparstatut", methods={"POST"})
  25.      * @param Request $request
  26.      * @return JsonResponse
  27.      */
  28.     public function appelparstatut(Request $request)
  29.     {
  30.         $data json_decode($request->getContent(), true);
  31.         $date date("Ymd"strtotime("-30 days"strtotime(date("Ymd"))));
  32.         $where " WHERE appel.dateappel >= ".$date;
  33.             if (isset($data['params']['collaborateurId'])) {
  34.                 $where .=  ' AND appel.utilisateur='.$data['params']['collaborateurId'];
  35.             }else {
  36.                 if (isset($data['params']['espaceagent']) && $data['params']['espaceagent']) {
  37.                     $where .=  ' AND appel.utilisateur='.$this->getUtilisateurConnecte()->getId();
  38.                 }
  39.             }
  40.         $sql "
  41.                 SELECT       
  42.                 DATE_FORMAT(appel.dateappel, '%d/%m/%Y') AS jour,
  43.                 appelstatut.libelle AS statutappel,
  44.                 appelstatut.couleur AS couleurstatut,                
  45.                 count(appel.id) AS nombreappel    
  46.                 FROM ".Appel::class." AS appel
  47.                 LEFT JOIN ".Appelstatut::class." as appelstatut WITH appel.appelstatut = appelstatut.id
  48.                 LEFT JOIN ".Campagneprospect::class." AS campagneprospect WITH appel.campagneprospect = campagneprospect.id
  49.                 LEFT JOIN ".Campagne::class." AS campagne WITH campagneprospect.campagne = campagne.id
  50.                 LEFT JOIN ".Client::class." AS client WITH campagne.client = client.id
  51.                 ".$where."
  52.                 GROUP BY jour, statutappel
  53.                 ORDER BY appel.dateappel asc
  54.                 ";
  55.         $query $this->em->createQuery($sql);
  56.         $reponse $query->getResult();
  57.         return $this->apiOk($reponse);
  58.     }
  59.     /**
  60.      * @Route("/campagneparstatut/{id}", name="api_Statistique_campagneparstatut", methods={"GET"})
  61.      * @param int $id
  62.      * @return JsonResponse
  63.      */
  64.     public function campagneparstatut(int $id)
  65.     {
  66.         if ($id == null || $id <= 0) {
  67.             return $this->apiKo("Campagne non-trouvĂ©e !");
  68.         }
  69.         $where "WHERE campagneprospect.campagne = ".$id ;
  70.         $sql "
  71.                 SELECT 
  72.                 (case when appelstatut.couleur IS NULL then '#7c65ad' ELSE appelstatut.couleur END) AS couleurstatut ,
  73.                 (case when appelstatut.libelle IS NULL then 'Pas encore contactĂ©' ELSE appelstatut.libelle END) AS statutappel ,
  74.                 COUNT(campagneprospect.id) AS nombreprospect
  75.                 FROM
  76.                 ".Campagneprospect::class." AS campagneprospect
  77.                 LEFT join ".Appelstatut::class." AS appelstatut WITH appelstatut.id = campagneprospect.appelstatut
  78.                 ".$where."
  79.                 GROUP BY appelstatut.libelle, appelstatut.couleur
  80.                 ";
  81.         $query $this->em->createQuery($sql);
  82.         $reponse $query->getResult();
  83.         return $this->apiOk($reponse);
  84.     }
  85.     /**
  86.      * @Route("/avancementcampagneappel", name="api_Statistique_avancementcampagneappel", methods={"POST"})
  87.      * @param Request $request
  88.      * @return JsonResponse
  89.      */
  90.     public function avancementcampagneappel(Request $request)
  91.     {
  92.         $data json_decode($request->getContent(), true);
  93.         $where $this->paramstowhere($data);
  94.         $sql "SELECT
  95.                 (case when campagneprospect.utilisateur IS NULL then 'NON' ELSE 'OUI' END) AS estcontacte,
  96.                 COUNT(campagneprospect.id) AS nombreprospect
  97.                 FROM
  98.                 ".Campagneprospect::class." AS campagneprospect
  99.                 ".$where."
  100.                 GROUP BY estcontacte
  101.                 ";
  102.         $query $this->em->createQuery($sql);
  103.         $reponse $query->getResult();
  104.         return $this->apiOk($reponse);
  105.     }
  106.     /**
  107.      * @Route("/avancementcampagneobjectif", name="api_Statistique_avancementcampagneobjectif", methods={"POST"})
  108.      * @param Request $request
  109.      * @return JsonResponse
  110.      */
  111.     public function avancementcampagneobjectif(Request $request)
  112.     {
  113.         $data json_decode($request->getContent(), true);
  114.         $where $this->paramstowhere($data);
  115.         $sql "SELECT
  116.                 campagne.id AS idcampagne,
  117.                 campagne.objectif AS totalarealiser,
  118.                 COUNT(rdv.id) AS totalrealise
  119.                 FROM
  120.                 ".Rdv::class." AS rdv
  121.                 LEFT JOIN ".Rdvstatut::class." AS rdvstatut WITH rdv.rdvstatut = rdvstatut.id
  122.                 LEFT JOIN ".Campagne::class." AS campagne WITH rdv.campagne = campagne.id
  123.                 ".$where."
  124.                 GROUP BY campagne.id";
  125.         $query $this->em->createQuery($sql);
  126.         $reponse $query->getResult();
  127.         return $this->apiOk($reponse);
  128.     }
  129.    /**
  130.      * @Route("/collaborateurappelstatut", name="api_Statistique_collaborateurappelstatut", methods={"POST"})
  131.      * @param Request $request
  132.      * @return JsonResponse
  133.      */
  134.     public function collaborateurappelstatut(Request $request)
  135.     {
  136.         $data json_decode($request->getContent(), true);
  137.         $where $this->paramstowhere($data);
  138.         $sql "SELECT
  139.                 CASE WHEN utilisateur.id IS NULL then 0 ELSE utilisateur.id end AS idcollaborateur , 
  140.                 CASE WHEN utilisateur.id IS NULL then 'PAS ENCORE AFFECTE' ELSE UPPER(concat(utilisateur.nom,' ',utilisateur.prenom)) end AS collaborateur , 
  141.                 CASE WHEN appelstatut.id IS NULL then 0 ELSE appelstatut.id end AS idappelstatut ,
  142.                 CASE WHEN appelstatut.id IS NULL then 'PAS ENCORE APPELE' ELSE UPPER(appelstatut.libelle) end AS statut ,
  143.                 COUNT(campagneprospect.id) AS nombreprospect
  144.                 FROM 
  145.                 ".Campagneprospect::class." AS campagneprospect
  146.                 LEFT JOIN ".Utilisateur::class." AS utilisateur WITH campagneprospect.traitementpar = utilisateur.id
  147.                 LEFT JOIN ".Appelstatut::class." AS appelstatut WITH campagneprospect.appelstatut = appelstatut.id
  148.                 ".$where."
  149.                 GROUP BY utilisateur.id,appelstatut.id";
  150.         $query $this->em->createQuery($sql);
  151.         $reponse $query->getResult();
  152.         return $this->apiOk($reponse);
  153.     }
  154.     /**
  155.      * @Route("/previsionrappel", name="api_Statistique_previsionrappel", methods={"POST"})
  156.      * @param Request $request
  157.      * @return JsonResponse
  158.      */
  159.     public function previsionrappel(Request $request)
  160.     {
  161.         $data json_decode($request->getContent(), true);
  162.         $where $this->paramstowhere($data);
  163.         // Rappels passĂ©s
  164.         $sql0 "SELECT
  165.                 'Rappels ratĂ©s' AS periode,
  166.                 COUNT(campagneprospect.id) AS nombre
  167.                 FROM 
  168.                 ".Campagneprospect::class." AS campagneprospect
  169.                 LEFT JOIN ".Appelstatut::class." AS appelstatut WITH campagneprospect.appelstatut = appelstatut.id
  170.                 ".$where."
  171.                 AND appelstatut.rappel=TRUE AND campagneprospect.daterappel < now()
  172.                 GROUP BY periode
  173.                 ";
  174.         $sql0_ids "SELECT distinct 
  175.                 campagneprospect.id AS ids
  176.                 FROM 
  177.                 ".Campagneprospect::class." AS campagneprospect
  178.                 LEFT JOIN ".Appelstatut::class." AS appelstatut WITH campagneprospect.appelstatut = appelstatut.id
  179.                 ".$where."
  180.                 AND appelstatut.rappel=TRUE AND campagneprospect.daterappel < now()
  181.                 ";
  182.         $query0 $this->em->createQuery($sql0);
  183.         $query0_ids $this->em->createQuery($sql0_ids);
  184.         // Prochaine 5jours
  185.         $sql1 "SELECT
  186.                 DATE(campagneprospect.daterappel) AS periode,
  187.                 COUNT(campagneprospect.id) AS nombre
  188.                 FROM 
  189.                 ".Campagneprospect::class." AS campagneprospect
  190.                 LEFT JOIN ".Appelstatut::class." AS appelstatut WITH campagneprospect.appelstatut = appelstatut.id
  191.                 ".$where."
  192.                 AND appelstatut.rappel=TRUE AND campagneprospect.daterappel >= now()
  193.                 GROUP BY periode
  194.                 ORDER BY periode ASC
  195.                 ";
  196.         $query1 $this->em->createQuery($sql1);
  197.         $query1->setFirstResult(0);
  198.         $query1->setMaxResults(5);
  199.         // Par mois
  200.         $sql2 "SELECT
  201.                 date_format(campagneprospect.daterappel,'%Y-%m') AS periode,
  202.                 COUNT(campagneprospect.id) AS nombre
  203.                 FROM 
  204.                 ".Campagneprospect::class." AS campagneprospect
  205.                 LEFT JOIN ".Appelstatut::class." AS appelstatut WITH campagneprospect.appelstatut = appelstatut.id
  206.                 ".$where."
  207.                 AND appelstatut.rappel=TRUE AND campagneprospect.daterappel >= now()
  208.                 GROUP BY periode
  209.                 ORDER BY periode ASC
  210.                 ";
  211.         $query2 $this->em->createQuery($sql2);
  212.         $reponse0 $query0->getResult();
  213.         $reponse0_ids $query0_ids->getResult();
  214.         $reponse1 $query1->getResult();
  215.         $reponse2 $query2->getResult();
  216.         $reponse[] = [
  217.             "id" => 1,
  218.             "data"=> $reponse0,
  219.             "ids" => array_column($reponse0_ids'ids')
  220.         ];
  221.         $reponse[] = [
  222.             "id" => 2,
  223.             "data"=> $reponse1,
  224.             "ids" => []
  225.         ];
  226.         $reponse[] = [
  227.             "id" => 3,
  228.             "data"=> $reponse2,
  229.             "ids" => []
  230.         ];
  231.         return $this->apiOk($reponse);
  232.     }
  233.     /**
  234.      * @Route("/collaborateurappels", name="api_Statistique_collaborateurappels", methods={"POST"})
  235.      * @param Request $request
  236.      * @return JsonResponse
  237.      */
  238.     public function collaborateurappels(Request $request)
  239.     {
  240.         $data json_decode($request->getContent(), true);
  241.         $where $this->paramstowhere($data);
  242.         $sql "SELECT
  243.                 utilisateur.id AS idutilisateur,
  244.                 CONCAT(utilisateur.nom,' ',utilisateur.prenom) AS nomutilisateur,
  245.                 DATE_FORMAT(appel.dateappel, '%Y-%m-%d') AS datetraitement,
  246.                 COUNT(appel.id) AS nombreappel,
  247.                 SUM(appel.dureeappel) dureetraitement
  248.                 FROM ".Appel::class." as appel
  249.                 LEFT JOIN ".Campagneprospect::class." as campagneprospect WITH campagneprospect.id = appel.campagneprospect
  250.                 LEFT JOIN ".Utilisateur::class." as utilisateur WITH utilisateur.id = appel.utilisateur
  251.                 ".$where."
  252.                 GROUP BY idutilisateur,nomutilisateur, datetraitement
  253.                 ORDER BY appel.dateappel asc";
  254.         $query $this->em->createQuery($sql);
  255.         $reponse $query->getResult();
  256.         return $this->apiOk($reponse);
  257.     }
  258.     ////////////////////////////STATISTIQUE GLOBAL ////////////////////////
  259.     /**
  260.      * @Route("/prospectsglobal", name="api_Statistique_prospectsglobal", methods={"GET"})
  261.      * @return JsonResponse
  262.      */
  263.     public function prospectsglobal()
  264.     {
  265.         $sqlprospects "SELECT
  266.                 COUNT(prospect.id) AS total,
  267.                 CASE prospect.email WHEN '' then 0 ELSE 1 END AS avecemail,
  268.                 CASE prospect.standard WHEN '' then 0 ELSE 1 END AS avecstandard
  269.                 FROM ".Prospect::class." as prospect
  270.                 GROUP BY avecemail,avecstandard";
  271.         $sqlcontacts "SELECT
  272.                 COUNT(contact.id) AS total,
  273.                 CASE contact.email WHEN '' then 0 ELSE 1 END AS avecemail,
  274.                 CASE contact.fixe WHEN '' then 0 ELSE 1 END AS avecfixe,
  275.                 CASE contact.mobile WHEN '' then 0 ELSE 1 END AS avecmobile
  276.                 FROM ".Contact::class." as contact
  277.                 GROUP BY avecemail,avecfixe,avecmobile";
  278.         $queryprospects $this->em->createQuery($sqlprospects);
  279.         $querycontacts $this->em->createQuery($sqlcontacts);
  280.         $prospects $queryprospects->getResult();
  281.         $contacts $querycontacts->getResult();
  282.         $reponse = [
  283.             "prospects" => $prospects,
  284.             "contacts" => $contacts,
  285.         ];
  286.         return $this->apiOk($reponse);
  287.     }
  288.     /**
  289.      * @Route("/prospectsevolution", name="api_Statistique_prospectsevolution", methods={"POST"})
  290.      * @param Request $request
  291.      * @return JsonResponse
  292.      */
  293.     public function prospectsevolution(Request $request)
  294.     {
  295.         $date date("Ymd"strtotime("-365 days"strtotime(date("Ymd"))));
  296.         $where " WHERE appel.dateappel >= ".$date;
  297.         $sqlcreation "SELECT
  298.                         COUNT(prospect.id) AS total,
  299.                         year(prospect.datecreation) AS annee,
  300.                         month(prospect.datecreation) as mois
  301.                         FROM ".Prospect::class." as prospect
  302.                         WHERE prospect.datecreation >= ".$date."
  303.                         GROUP BY annee,mois
  304.                         ORDER BY annee, mois asc";
  305.         $sqlmodification "SELECT
  306.                         COUNT(prospect.id) AS total,
  307.                         year(prospect.datemodification) AS annee,
  308.                         month(prospect.datemodification) as mois
  309.                         FROM ".Prospect::class." as prospect
  310.                         WHERE prospect.datemodification >= ".$date."
  311.                         GROUP BY annee,mois
  312.                         ORDER BY annee, mois asc";
  313.         $querycreation $this->em->createQuery($sqlcreation);
  314.         $querymodification $this->em->createQuery($sqlmodification);
  315.         $creation $querycreation->getResult();
  316.         $modification $querymodification->getResult();
  317.         $reponse = [
  318.             "creation" => $creation,
  319.             "modification" => $modification,
  320.         ];
  321.         return $this->apiOk($reponse);
  322.     }
  323.     /**
  324.      * @Route("/prospectscp", name="api_Statistique_prospectscp", methods={"GET"})
  325.      * @return JsonResponse
  326.      */
  327.     public function prospectscp()
  328.     {
  329.         $sqltotal "SELECT
  330.                 COUNT(prospect.id) as total,
  331.                 SUBSTRING(ville.codepostal,1,2) as cp
  332.                 FROM ".Prospect::class." as prospect
  333.                 LEFT JOIN ".Ville::class." as ville WITH ville.id = prospect.ville
  334.                 GROUP BY cp";
  335.         $sqlavecemail "SELECT
  336.                 COUNT(prospect.id) as total,
  337.                 SUBSTRING(ville.codepostal,1,2) as cp
  338.                 FROM ".Prospect::class." as prospect
  339.                 LEFT JOIN ".Ville::class." as ville WITH ville.id = prospect.ville
  340.                 WHERE prospect.email not like ''
  341.                 GROUP BY cp";
  342.         $sqlavecstandard "SELECT
  343.                 COUNT(prospect.id) as total,
  344.                 SUBSTRING(ville.codepostal,1,2) as cp
  345.                 FROM ".Prospect::class." as prospect
  346.                 LEFT JOIN ".Ville::class." as ville WITH ville.id = prospect.ville
  347.                 WHERE prospect.standard not like ''
  348.                 GROUP BY cp";
  349.         $querytotal $this->em->createQuery($sqltotal);
  350.         $queryavecemail $this->em->createQuery($sqlavecemail);
  351.         $queryavecstandard $this->em->createQuery($sqlavecstandard);
  352.         $total $querytotal->getResult();
  353.         $totalavecemail $queryavecemail->getResult();
  354.         $totalavecstandard $queryavecstandard->getResult();
  355.         $reponse = [
  356.             "total" => $total,
  357.             "totalavecemail" => $totalavecemail,
  358.             "totalavecstandard" => $totalavecstandard,
  359.         ];
  360.         return $this->apiOk($reponse);
  361.     }
  362.     /***************************** HELPERS *****************/
  363.     /**
  364.      * @param array $data
  365.      * @return string
  366.      */
  367.     public function paramstowhere(array $data) : string{
  368.         //AND
  369.         $and " 1=1";
  370.         if (isset($data['filter']['and'])) {
  371.             $filtres $data['filter']['and'];
  372.             if (sizeof($filtres) != 0) {
  373.                 $and "( 1=1";
  374.                 foreach ($filtres as $key => $value) {
  375.                     $and .= " AND " $key " " $value;
  376.                 }
  377.                 $and .= " )";
  378.             }
  379.         }
  380.         //OR
  381.         $or "";
  382.         if (isset($data['filter']['or'])) {
  383.             $filtres $data['filter']['or'];
  384.             if (sizeof($filtres) != 0) {
  385.                 $or " ( 1=0";
  386.                 foreach ($filtres as $key => $value) {
  387.                     $or .= " OR " $key " " $value;
  388.                 }
  389.                 $or .= " )";
  390.             }
  391.         }
  392.         $where " WHERE ".$and;
  393.         if ($or != "") {
  394.             $where .= " AND " $or;
  395.         }
  396.         return $where;
  397.     }
  398. }