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