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.         $query0 $this->em->createQuery($sql0);
  175.         // Prochaine 5jours
  176.         $sql1 "SELECT
  177.                 DATE(campagneprospect.daterappel) AS periode,
  178.                 COUNT(campagneprospect.id) AS nombre
  179.                 FROM 
  180.                 ".Campagneprospect::class." AS campagneprospect
  181.                 LEFT JOIN ".Appelstatut::class." AS appelstatut WITH campagneprospect.appelstatut = appelstatut.id
  182.                 ".$where."
  183.                 AND appelstatut.rappel=TRUE AND campagneprospect.daterappel >= now()
  184.                 GROUP BY periode
  185.                 ORDER BY periode ASC
  186.                 ";
  187.         $query1 $this->em->createQuery($sql1);
  188.         $query1->setFirstResult(0);
  189.         $query1->setMaxResults(5);
  190.         // Par mois
  191.         $sql2 "SELECT
  192.                 date_format(campagneprospect.daterappel,'%Y-%m') AS periode,
  193.                 COUNT(campagneprospect.id) AS nombre
  194.                 FROM 
  195.                 ".Campagneprospect::class." AS campagneprospect
  196.                 LEFT JOIN ".Appelstatut::class." AS appelstatut WITH campagneprospect.appelstatut = appelstatut.id
  197.                 ".$where."
  198.                 AND appelstatut.rappel=TRUE AND campagneprospect.daterappel >= now()
  199.                 GROUP BY periode
  200.                 ORDER BY periode ASC
  201.                 ";
  202.         $query2 $this->em->createQuery($sql2);
  203.         $reponse0 $query0->getResult();
  204.         $reponse1 $query1->getResult();
  205.         $reponse2 $query2->getResult();
  206.         $reponse[] = [
  207.             "id" => 1,
  208.             "data"=> $reponse0
  209.         ];
  210.         $reponse[] = [
  211.             "id" => 2,
  212.             "data"=> $reponse1
  213.         ];
  214.         $reponse[] = [
  215.             "id" => 3,
  216.             "data"=> $reponse2
  217.         ];
  218.         return $this->apiOk($reponse);
  219.     }
  220.     /**
  221.      * @Route("/collaborateurappels", name="api_Statistique_collaborateurappels", methods={"POST"})
  222.      * @param Request $request
  223.      * @return JsonResponse
  224.      */
  225.     public function collaborateurappels(Request $request)
  226.     {
  227.         $data json_decode($request->getContent(), true);
  228.         $where $this->paramstowhere($data);
  229.         $sql "SELECT
  230.                 utilisateur.id AS idutilisateur,
  231.                 CONCAT(utilisateur.nom,' ',utilisateur.prenom) AS nomutilisateur,
  232.                 DATE_FORMAT(appel.dateappel, '%Y-%m-%d') AS datetraitement,
  233.                 COUNT(appel.id) AS nombreappel,
  234.                 SUM(appel.dureeappel) dureetraitement
  235.                 FROM ".Appel::class." as appel
  236.                 LEFT JOIN ".Campagneprospect::class." as campagneprospect WITH campagneprospect.id = appel.campagneprospect
  237.                 LEFT JOIN ".Utilisateur::class." as utilisateur WITH utilisateur.id = appel.utilisateur
  238.                 ".$where."
  239.                 GROUP BY idutilisateur,nomutilisateur, datetraitement
  240.                 ORDER BY appel.dateappel asc";
  241.         $query $this->em->createQuery($sql);
  242.         $reponse $query->getResult();
  243.         return $this->apiOk($reponse);
  244.     }
  245.     ////////////////////////////STATISTIQUE GLOBAL ////////////////////////
  246.     /**
  247.      * @Route("/prospectsglobal", name="api_Statistique_prospectsglobal", methods={"GET"})
  248.      * @return JsonResponse
  249.      */
  250.     public function prospectsglobal()
  251.     {
  252.         $sqlprospects "SELECT
  253.                 COUNT(prospect.id) AS total,
  254.                 CASE prospect.email WHEN '' then 0 ELSE 1 END AS avecemail,
  255.                 CASE prospect.standard WHEN '' then 0 ELSE 1 END AS avecstandard
  256.                 FROM ".Prospect::class." as prospect
  257.                 GROUP BY avecemail,avecstandard";
  258.         $sqlcontacts "SELECT
  259.                 COUNT(contact.id) AS total,
  260.                 CASE contact.email WHEN '' then 0 ELSE 1 END AS avecemail,
  261.                 CASE contact.fixe WHEN '' then 0 ELSE 1 END AS avecfixe,
  262.                 CASE contact.mobile WHEN '' then 0 ELSE 1 END AS avecmobile
  263.                 FROM ".Contact::class." as contact
  264.                 GROUP BY avecemail,avecfixe,avecmobile";
  265.         $queryprospects $this->em->createQuery($sqlprospects);
  266.         $querycontacts $this->em->createQuery($sqlcontacts);
  267.         $prospects $queryprospects->getResult();
  268.         $contacts $querycontacts->getResult();
  269.         $reponse = [
  270.             "prospects" => $prospects,
  271.             "contacts" => $contacts,
  272.         ];
  273.         return $this->apiOk($reponse);
  274.     }
  275.     /**
  276.      * @Route("/prospectsevolution", name="api_Statistique_prospectsevolution", methods={"POST"})
  277.      * @param Request $request
  278.      * @return JsonResponse
  279.      */
  280.     public function prospectsevolution(Request $request)
  281.     {
  282.         $date date("Ymd"strtotime("-365 days"strtotime(date("Ymd"))));
  283.         $where " WHERE appel.dateappel >= ".$date;
  284.         $sqlcreation "SELECT
  285.                         COUNT(prospect.id) AS total,
  286.                         year(prospect.datecreation) AS annee,
  287.                         month(prospect.datecreation) as mois
  288.                         FROM ".Prospect::class." as prospect
  289.                         WHERE prospect.datecreation >= ".$date."
  290.                         GROUP BY annee,mois
  291.                         ORDER BY annee, mois asc";
  292.         $sqlmodification "SELECT
  293.                         COUNT(prospect.id) AS total,
  294.                         year(prospect.datemodification) AS annee,
  295.                         month(prospect.datemodification) as mois
  296.                         FROM ".Prospect::class." as prospect
  297.                         WHERE prospect.datemodification >= ".$date."
  298.                         GROUP BY annee,mois
  299.                         ORDER BY annee, mois asc";
  300.         $querycreation $this->em->createQuery($sqlcreation);
  301.         $querymodification $this->em->createQuery($sqlmodification);
  302.         $creation $querycreation->getResult();
  303.         $modification $querymodification->getResult();
  304.         $reponse = [
  305.             "creation" => $creation,
  306.             "modification" => $modification,
  307.         ];
  308.         return $this->apiOk($reponse);
  309.     }
  310.     /**
  311.      * @Route("/prospectscp", name="api_Statistique_prospectscp", methods={"GET"})
  312.      * @return JsonResponse
  313.      */
  314.     public function prospectscp()
  315.     {
  316.         $sqltotal "SELECT
  317.                 COUNT(prospect.id) as total,
  318.                 SUBSTRING(ville.codepostal,1,2) as cp
  319.                 FROM ".Prospect::class." as prospect
  320.                 LEFT JOIN ".Ville::class." as ville WITH ville.id = prospect.ville
  321.                 GROUP BY cp";
  322.         $sqlavecemail "SELECT
  323.                 COUNT(prospect.id) as total,
  324.                 SUBSTRING(ville.codepostal,1,2) as cp
  325.                 FROM ".Prospect::class." as prospect
  326.                 LEFT JOIN ".Ville::class." as ville WITH ville.id = prospect.ville
  327.                 WHERE prospect.email not like ''
  328.                 GROUP BY cp";
  329.         $sqlavecstandard "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.                 WHERE prospect.standard not like ''
  335.                 GROUP BY cp";
  336.         $querytotal $this->em->createQuery($sqltotal);
  337.         $queryavecemail $this->em->createQuery($sqlavecemail);
  338.         $queryavecstandard $this->em->createQuery($sqlavecstandard);
  339.         $total $querytotal->getResult();
  340.         $totalavecemail $queryavecemail->getResult();
  341.         $totalavecstandard $queryavecstandard->getResult();
  342.         $reponse = [
  343.             "total" => $total,
  344.             "totalavecemail" => $totalavecemail,
  345.             "totalavecstandard" => $totalavecstandard,
  346.         ];
  347.         return $this->apiOk($reponse);
  348.     }
  349.     /***************************** HELPERS *****************/
  350.     /**
  351.      * @param array $data
  352.      * @return string
  353.      */
  354.     public function paramstowhere(array $data) : string{
  355.         //AND
  356.         $and " 1=1";
  357.         if (isset($data['filter']['and'])) {
  358.             $filtres $data['filter']['and'];
  359.             if (sizeof($filtres) != 0) {
  360.                 $and "( 1=1";
  361.                 foreach ($filtres as $key => $value) {
  362.                     $and .= " AND " $key " " $value;
  363.                 }
  364.                 $and .= " )";
  365.             }
  366.         }
  367.         //OR
  368.         $or "";
  369.         if (isset($data['filter']['or'])) {
  370.             $filtres $data['filter']['or'];
  371.             if (sizeof($filtres) != 0) {
  372.                 $or " ( 1=0";
  373.                 foreach ($filtres as $key => $value) {
  374.                     $or .= " OR " $key " " $value;
  375.                 }
  376.                 $or .= " )";
  377.             }
  378.         }
  379.         $where " WHERE ".$and;
  380.         if ($or != "") {
  381.             $where .= " AND " $or;
  382.         }
  383.         return $where;
  384.     }
  385. }