src/Controller/IndexController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use App\Entity\Exploitant;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. use App\Entity\UserExploitant;
  8. use Doctrine\ORM\Query\ResultSetMapping;
  9. class IndexController extends AbstractController
  10. {
  11.   /**
  12.    * @Route("/", name="home")
  13.    */
  14.    public function home(TranslatorInterface $translator)
  15.    {
  16.         $translated $translator->trans('user.login');
  17.         
  18.         //var_dump($translated);exit;
  19.         return $this->render('home.html.twig', [
  20.         ]);
  21.    }
  22.    
  23.    function execute($sql){
  24.       $em $this->getDoctrine()->getManager();
  25.       $query $em->getConnection()->prepare($sql);
  26.       $query->execute();
  27.       return $query->fetchAll();
  28.     }
  29.     
  30.    /**
  31.      * @Route("/generate_my_sql", name="generate_my_sql")
  32.      *
  33.      */
  34.     public function generate_my_sql(){
  35.         ini_set('memory_limit''-1');
  36.      /*$setttings = [
  37.         ["as" => ["farm","farm_","F"], "ignore" => ["updated_at"]],
  38.         ["as" => ["farm_infos","farm_infos_","FI"], "ignore" => ["updated_at"]],
  39.         ["as" => ["groupement","groupement_","GR"], "ignore" => ["updated_at"]],
  40.         ["as" => ["groupement_infos","groupement_infos_","GRI"], "ignore" => ["updated_at"]],
  41.       ];
  42.       
  43.       $fields_as = [];
  44.       $extra_jointure = "";
  45.       foreach ($setttings as $S) {
  46.         $fields_as[] = $this->list_fields($S["as"][0],$S["as"][1],$S["as"][2], $S["ignore"], $extra_jointure);
  47.       }
  48. */
  49.         
  50.       $query "SELECT lsc.value from list_sous_charge lsc LEFT JOIN list_type_charge ltc ON ltc.id = lsc.type_charge_id where ltc.is_vegetal = true ORDER BY lsc.id asc";
  51.       $result $this->execute($query);
  52.       $chargeV "";
  53.       foreach ($result as $res){
  54.           $string str_replace(' ''_'$res["value"]); // Replaces all spaces with hyphens.
  55.           $string str_replace('-''_'$string);
  56.           $retour preg_replace('/[^A-Za-z0-9\-]/'''$string);
  57.           
  58.           $chargeV .= 'charge_agriculture_'.strtolower($retour).' character varying(255), ';
  59.       }
  60.       
  61.       $query "SELECT lsc.value from list_sous_charge lsc LEFT JOIN list_type_charge ltc ON ltc.id = lsc.type_charge_id where ltc.is_vegetal = false ORDER BY lsc.id asc";
  62.       $result $this->execute($query);
  63.       $chargeE "";
  64.       foreach ($result as $res){
  65.           $string str_replace(' ''_'$res["value"]); // Replaces all spaces with hyphens.
  66.           $string str_replace('-''_'$string); // Replaces all spaces with hyphens.
  67.           $retour preg_replace('/[^A-Za-z0-9\-]/'''$string);
  68.           
  69.           $chargeE .= 'charge_elevage_'.strtolower($retour).' character varying(255), ';
  70.       }
  71.        
  72.       $query "SELECT value from list_production  where is_vegetal = true ORDER BY id asc";
  73.       $result $this->execute($query);
  74.       $prod "";
  75.       foreach ($result as $res){
  76.           $string str_replace(' ''_'$res["value"]); // Replaces all spaces with hyphens.
  77.           $string str_replace('-''_'$string);
  78.           $retour preg_replace('/[^A-Za-z0-9\-]/'''$string);
  79.           
  80.           $prod .= 'produit_agriculture_'.strtolower($retour).' character varying(255), ';
  81.       }
  82.       $query "SELECT value from list_production  where is_vegetal = false ORDER BY id asc";
  83.       $result $this->execute($query);
  84.       foreach ($result as $res){
  85.           $string str_replace(' ''_'$res["value"]); // Replaces all spaces with hyphens.
  86.           $string str_replace('-''_'$string);
  87.           $retour preg_replace('/[^A-Za-z0-9\-]/'''$string);
  88.           
  89.           $prod .= 'produit_elevage_'.strtolower($retour).' character varying(255), ';
  90.       }
  91.       //echo $chargeE;
  92.       $reqDrop =
  93.         "DROP TABLE IF EXISTS public.datamart;"
  94.       ;
  95.       $reqCreate =
  96.         "CREATE TABLE IF NOT EXISTS public.datamart (
  97.         exploitant_id integer,
  98.         exploitant_nom character varying(255) ,
  99.         exploitant_prenom character varying(255),
  100.         exploitant_latitude numeric(17,14) ,
  101.         exploitant_longitude numeric(17,14),
  102.         
  103.         exploitant_identite_id integer,        
  104.         exploitant_identite_annee integer,
  105.         exploitant_identite_date_saisie date,
  106.         exploitant_identite_organisation character varying(255),
  107.         exploitant_identite_fonction_accompagnateur character varying(255),
  108.         exploitant_identite_nom_accompagnateur character varying(255),
  109.         exploitant_identite_id_accompagnateur character varying(255),  
  110.         exploitant_identite_profil text,
  111.         exploitant_identite_sexe_id character varying(255),        
  112.         exploitant_identite_annee_naissance integer,
  113.         exploitant_identite_region character varying(255),
  114.         exploitant_identite_district character varying(255),
  115.         exploitant_identite_commune character varying(255),
  116.         exploitant_identite_fokontany character varying(255),
  117.         exploitant_identite_telephone integer,
  118.         exploitant_identite_adhesion_op text,
  119.         exploitant_identite_nom_op character varying(255),
  120.         exploitant_identite_responsabilite_op character varying(255),
  121.         exploitant_identite_taille_menage integer,
  122.         exploitant_identite_periode_soudure character varying(255),
  123.         exploitant_identite_eaf character varying(255),
  124.         exploitant_identite_production character varying(255),
  125.         
  126.         production_id integer,
  127.         production_agriculture_variete character varying(255),
  128.         production_agriculture_saison character varying(255),
  129.         production_agriculture_eau character varying(255),
  130.         production_agriculture_surface character varying(255),
  131.         production_agriculture_date_semis date,
  132.         production_agriculture_date_repiquage date,
  133.         production_agriculture_date_recolte date,
  134.         production_agriculture_volume_recolte character varying(255),
  135.         production_agriculture_obs_itk character varying(255) ,
  136.         production_agriculture_obs_contexte_campagne character varying(255),
  137.         production_agriculture_obs_contexte_commerce character varying(255),
  138.         production_agriculture_code_analytique_a character varying(255),
  139.         production_agriculture_code_analytique_b character varying(255),
  140.         
  141.         production_elevage_race character varying(255),
  142.         production_elevage_elevage character varying(255),
  143.         production_elevage_duree_cycle character varying(255),
  144.         production_elevage_nb_reproducteurs character varying(255),
  145.         production_elevage_investissements_reproducteurs character varying(255),
  146.         production_elevage_date_debut date,
  147.         production_elevage_date_fin date,
  148.         production_elevage_nbre_cheptel character varying(255),
  149.         production_elevage_obs_itk character varying(255) ,
  150.         production_elevage_obs_contexte_campagne character varying(255),
  151.         production_elevage_obs_contexte_commerce character varying(255),
  152.         production_elevage_code_analytique_a character varying(255),
  153.         production_elevage_code_analytique_b character varying(255),
  154.         
  155.         $chargeV
  156.         $chargeE
  157.         $prod
  158.          retour integer,
  159.          
  160.         stat_agri_surface_reelle_surface integer,
  161.         stat_agri_surface_reelle_charges integer,
  162.         stat_agri_surface_reelle_produits integer,
  163.         stat_agri_surface_reelle_marge integer,
  164.         stat_agri_surface_reelle_depense integer,
  165.         stat_agri_surface_reelle_recette integer,
  166.         stat_agri_surface_reelle_benefice integer,
  167.         
  168.         stat_agri_are_charges integer,
  169.         stat_agri_are_produits integer,
  170.         stat_agri_are_marge integer,
  171.         stat_agri_are_depense integer,
  172.         stat_agri_are_recette integer,
  173.         stat_agri_are_benefice integer,
  174.         stat_agri_are_cout_production integer,
  175.         stat_agri_are_prix_vente integer,
  176.         stat_agri_are_rendement integer,
  177.         
  178.         stat_elevage_taille_reelle_nb_repro integer,
  179.         stat_elevage_taille_reelle_nb_cheptel integer,
  180.         stat_elevage_taille_reelle_nb_jeunes_vendus integer,
  181.         stat_elevage_taille_reelle_nb_adultes_vendus integer,
  182.         stat_elevage_taille_reelle_nb_autres_vendus integer,
  183.         stat_elevage_taille_reelle_taux_perte integer,
  184.         stat_elevage_taille_reelle_charges integer,
  185.         stat_elevage_taille_reelle_produits integer,
  186.         stat_elevage_taille_reelle_marge integer,
  187.         stat_elevage_taille_reelle_depense integer,
  188.         stat_elevage_taille_reelle_recette integer,
  189.         stat_elevage_taille_reelle_benefice integer,
  190.         
  191.         stat_elevage_ratio_charge_reproducteur integer,
  192.         stat_elevage_ratio_produit_reproducteur integer,
  193.         stat_elevage_ratio_marge_reproducteur integer,
  194.         stat_elevage_ratio_depense_reproducteur integer,
  195.         stat_elevage_ratio_recette_reproducteur integer,
  196.         stat_elevage_ratio_benefice_reproducteur integer,
  197.         stat_elevage_ratio_prix_jeune integer,
  198.         stat_elevage_ratio_prix_adulte integer,
  199.         stat_elevage_ratio_prix_autre integer
  200.        );"
  201.       ;
  202.       //echo $reqCreate;exit;
  203.       try {
  204.         $resultDrop $this->execute($reqDrop);
  205.         $resultCreate $this->execute($reqCreate);
  206.       } catch (\Exception $e) {
  207.         print_r($e);
  208.       }
  209.       
  210.       
  211.       $query "SELECT DISTINCT(id), specialite1_id, specialite2_id, specialite3_id, specialite4_id from exploitant_identite";
  212.       $result $this->execute($query);
  213.       
  214.       foreach ($result as $res) {
  215.           
  216.           //var_dump($res);
  217.           
  218.           if ($res['specialite1_id'] == null && $res['specialite2_id'] == null && $res['specialite3_id'] == null && $res['specialite4_id'] == null ) {
  219.               $reqInsert =
  220.               "INSERT INTO datamart (
  221.               SELECT f.id,
  222.                   f.nom,
  223.                   f.prenom,
  224.                   f.latitude,
  225.                   f.longitude,
  226.                   expi.id ,
  227.                   expi.annee ,
  228.                   expi.date_collecte ,
  229.                   organisation.value,
  230.                   technicien.value ,
  231.                   expi.nom_enqueteur ,
  232.                   expi.id_collecteur ,
  233.                   (select array_to_string(array_agg(distinct(lp.value)),',') from exploitant_identite ei
  234.                   LEFT JOIN exploitant_identite_list_profil eilp ON eilp.exploitant_identite_id = ei.id
  235.                   LEFT JOIN list_profil lp ON eilp.list_profil_id = lp.id where ei.id = ".$res['id']." ),
  236.                   sexe.value ,
  237.                   expi.annee_naissance ,
  238.                   region.value,
  239.                   district.value ,
  240.                   commune.value ,
  241.                   expi.fokontany ,
  242.                   expi.telephone ,
  243.                   (select array_to_string(array_agg(distinct(lto.value)),',') from exploitant_identite ei
  244.                   LEFT JOIN exploitant_identite_list_type_op eilto ON eilto.exploitant_identite_id = ei.id
  245.                   LEFT JOIN list_type_op lto ON eilto.list_type_op_id = lto.id where ei.id = ".$res['id']." ),
  246.                   expi.nom_op ,
  247.                   adhesion_structure.value ,
  248.                   expi.taille_menage ,
  249.                   periode_soudure.value ,
  250.                   eaf.value 
  251.                  FROM exploitant f
  252.                  LEFT JOIN exploitant_identite expi ON f.id = expi.exploitant_id
  253.                  LEFT JOIN list_region region ON region.id = expi.region_exp_id
  254.                  LEFT JOIN list_district district ON district.id = expi.district_exp_id
  255.                  LEFT JOIN list_commune commune ON commune.id = expi.commune_exp_id
  256.                  LEFT JOIN list_organisation organisation ON organisation.id = expi.organisation_id
  257.                  LEFT JOIN list_technicien technicien ON technicien.id = expi.technicien_id
  258.                  LEFT JOIN list_sexe sexe ON sexe.id = expi.sexe_id
  259.                  LEFT JOIN list_responsabilite_op adhesion_structure ON adhesion_structure.id = expi.responsabilite_op_id
  260.                  LEFT JOIN list_periode_soudure periode_soudure ON periode_soudure.id = expi.periode_soudure_id
  261.                  LEFT JOIN list_economie_exploitation economie_exploitation ON economie_exploitation.id = expi.economie_exploitation_id
  262.                  LEFT JOIN list_niveau_vie niveau_vie ON niveau_vie.id = expi.niveau_vie_id
  263.                  LEFT JOIN list_eaf eaf ON eaf.id = expi.eaf_id
  264.                  LEFT JOIN list_responsabilite_op responsabilite_op ON responsabilite_op.id = expi.responsabilite_op_id
  265.                  WHERE expi.id = ".$res['id']."  
  266.               -- WITH DATA;
  267.               );"
  268.             ;
  269.               
  270.               echo 'i';
  271.               try {
  272.                   // var_dump($reqInsert);exit;
  273.                   $resultInsert $this->execute($reqInsert);
  274.                 } catch (\Exception $e) {
  275.                   print_r($e);
  276.                 }
  277.           } else {
  278.             $query "SELECT id, exploitant_identite_id from exploitant_speculation where exploitant_identite_id = ".$res['id']." ORDER BY id";
  279.             $resultExp $this->execute($query);
  280.             foreach ($resultExp as $key=>$resExp) {
  281.                 
  282.                 $query "SELECT lsc.id from list_sous_charge lsc LEFT JOIN list_type_charge ltc ON ltc.id = lsc.type_charge_id where ltc.is_vegetal = true ORDER BY lsc.id asc";
  283.                 $resultC $this->execute($query);
  284.                 $totStatCV $totStatPV $depense $recette $qteProduitV $sommePrix $nbVente $totStatCP $depenseP 0;
  285.                 $chargeV "";
  286.                 $statV "spec.pv_surface, ";
  287.                 foreach ($resultC as $resC){
  288.                     $query 'select * from charge where exploitant_speculation_id = '.$resExp["id"].' AND sous_charge_id = '.$resC["id"].' ';
  289.                     $resultCh $this->execute($query);
  290.                     $so 0;
  291.                     foreach ($resultCh as $charge){
  292.                         for ($w=1;$w<13;$w++){
  293.                             //$method_set = "getMois".$w;
  294.                             $so += (float) $charge["mois".$w];
  295.                             if ($charge["type_depense_id"] == ){
  296.                                 $depense += (float) $charge["mois".$w];
  297.                             }
  298.                         }
  299.                     }
  300.                     $chargeV .= "'".$so."' , ";
  301.                     $totStatCV += (float) $so;
  302.                     
  303.                     
  304.                 }
  305.                 
  306.                 $statV .= "'".$totStatCV."' ,";
  307.                         
  308.                 $query "SELECT lsc.id from list_sous_charge lsc LEFT JOIN list_type_charge ltc ON ltc.id = lsc.type_charge_id where ltc.is_vegetal = false ORDER BY lsc.id asc";
  309.                 $resultC $this->execute($query);
  310.                 $chargeC "";
  311.                 foreach ($resultC as $resC){
  312.                     $query 'select * from charge where exploitant_speculation_id = '.$resExp["id"].' AND sous_charge_id = '.$resC["id"].' ';
  313.                     $resultCh $this->execute($query);
  314.                     $so 0;
  315.                     
  316.                     foreach ($resultCh as $charge){
  317.                         for ($w=1;$w<13;$w++){
  318.                             //$method_set = "getMois".$w;
  319.                             $so += (float) $charge["mois".$w];
  320.                             
  321.                             if ($charge["type_depense_id"] == ){
  322.                                 $depenseP += (float) $charge["mois".$w];
  323.                             }
  324.                         }
  325.                     }
  326.                     $chargeC .= "'".$so."' , ";
  327.                     $totStatCP += (float) $so;
  328.                 }
  329.                 
  330.                 $query "SELECT id  from list_production  where is_vegetal = true ORDER BY id asc";
  331.                 $resultC $this->execute($query);
  332.                 $prodC "";
  333.                 foreach ($resultC as $resC){
  334.                     $query 'select * from produit where exploitant_speculation_id = '.$resExp["id"].' AND production_id = '.$resC["id"].' ';
  335.                     $resultCh $this->execute($query);
  336.                     $so 0;
  337.                     foreach ($resultCh as $charge){
  338.                         $so += (float) $charge["somme"];
  339.                         $qteProduitV += (float) $charge["quantite"];
  340.                         if ($charge["production_id"] == ){
  341.                                 $recette += (float) $charge["somme"];
  342.                                 $sommePrix += (float) $charge["pu"];
  343.                                 $nbVente++;
  344.                             }
  345.                     }
  346.                     $prodC .= "'".$so."' , ";
  347.                     
  348.                     $totStatPV += (float) $so;
  349.                 }
  350.                 
  351.                 $statV .= "'".$totStatPV."' ,";
  352.                 $marge = (float) $totStatPV - (float) $totStatCV ;
  353.                 $statV .= "'".$marge."' , '".$depense."' ,'".$recette."', ";
  354.                 $benefice = (float) $recette - (float) $depense;
  355.                 $statV .= "'".$benefice."',  ceil(".(float) $totStatCV ."/spec.pv_surface),  ceil(".(float) $totStatPV ."/spec.pv_surface) ,  ceil(".(float) $marge ."/spec.pv_surface) ,";
  356.                 $statV .= " ceil(".(float) $depense ."/spec.pv_surface),  ceil(".(float) $recette ."/spec.pv_surface) ,  ceil(".(float) $benefice ."/spec.pv_surface) ,";
  357.                 
  358.                 if ($qteProduitV == 0) {
  359.                     $coutprod 0;
  360.                 } else {
  361.                     $coutprod = ((float) $totStatCV / (float) $qteProduitV);
  362.                 }
  363.                 $statV .= " ceil($coutprod) ,";
  364.                 
  365.                 if ($nbVente ==0) {
  366.                     $prvente 0;
  367.                 } else {
  368.                     $prvente = ((float) $sommePrix / (float) $nbVente);
  369.                 }
  370.                 $statV .= " ceil($prvente), ceil(".(float)$qteProduitV."/spec.pv_surface )";
  371.                 
  372.                 
  373.                 $qteProduit $qteJeune $qteAdulte $qteAutre $perte $totStatPP $recetteP $recettePP $nbVenteJ $nbVenteA $nbVenteAu $sommePrixJ $sommePrixA $sommePrixAu 0;
  374.                 
  375.                 $query "SELECT id from list_production  where is_vegetal = false ORDER BY id asc";
  376.                 $resultC $this->execute($query);
  377.                 foreach ($resultC as $resC){
  378.                     $query 'select * from produit where exploitant_speculation_id = '.$resExp["id"].' AND production_id = '.$resC["id"].' ';
  379.                     $resultCh $this->execute($query);
  380.                     $so 0;
  381.                     foreach ($resultCh as $charge){
  382.                         $so += (float) $charge["somme"];
  383.                         $recettePP $charge["somme"];
  384.                         if ($charge["production_id"] < 12){
  385.                             $qteProduit += (float) $charge["quantite"];
  386.                             
  387.                         }
  388.                         if ($charge["production_id"] == 5){
  389.                             $qteJeune += (float) $charge["quantite"];
  390.                             $recetteP += (float) $charge["somme"];
  391.                             $sommePrixJ += (float) $charge["pu"];
  392.                             $nbVenteJ++ ;
  393.                         }
  394.                         if ($charge["production_id"] == 9){
  395.                             $qteAdulte += (float) $charge["quantite"];
  396.                             $recetteP += (float) $charge["somme"];
  397.                             $sommePrixA += (float) $charge["pu"];
  398.                             $nbVenteA++;
  399.                         }
  400.                         if ($charge["production_id"] == 13){
  401.                             $qteAutre += (float) $charge["quantite"];
  402.                             $recetteP += (float) $charge["somme"];
  403.                             $sommePrixAu += (float) $charge["pu"];
  404.                             $nbVenteAu++;
  405.                         }
  406.                         if ($charge["production_id"] == || $charge["production_id"] == 10 || $charge["production_id"] == 14){
  407.                             $perte += (float) $charge["quantite"];
  408.                         }
  409.                     }
  410.                     $prodC .= "'".$so."' , ";
  411.                     $totStatPP += (float) $so;
  412.                 }
  413.       
  414.                 
  415.                     $statP " spec.pa_cheptel,  '".$qteProduit."',  '".$qteJeune."' ,  '".$qteAdulte."' ,  '".$qteAutre."', ";
  416.                     
  417.                     if ($qteProduit != 0) {
  418.                         $statP .= " ceil(".(float)$perte/(float)$qteProduit.") ,";
  419.                     } else {
  420.                         $statP .= " '0', ";
  421.                     }
  422.                     
  423.                     $statP .= "'".$totStatCP."' , '".$totStatPP."' , ";
  424.                     
  425.                     $marge = (float)$totStatPP - (float)$totStatCP;
  426.                     
  427.                     $ben = (float) $recetteP - (float) $depenseP;
  428.                     $statP .= "'".$marge."' , '".$depenseP."', '".$recetteP."', '".$ben."', ";
  429.                     
  430.                     
  431.                     $statP .= " CASE WHEN spec.pa_cheptel=0 THEN '0' 
  432.                                     WHEN spec.pa_cheptel is null THEN '0' 
  433.                         ELSE ceil(".(float) $totStatCP."/spec.pa_cheptel) END ,";
  434.                     $statP .= " CASE WHEN spec.pa_cheptel=0 THEN '0' 
  435.                                     WHEN spec.pa_cheptel is null THEN '0' 
  436.                         ELSE ceil(".(float) $totStatPP."/spec.pa_cheptel) END ,";
  437.                     $statP .= " CASE WHEN spec.pa_cheptel=0 THEN '0' 
  438.                                     WHEN spec.pa_cheptel is null THEN '0' 
  439.                         ELSE ceil(".(float) $marge."/spec.pa_cheptel) END ,";
  440.                     $statP .= " CASE WHEN spec.pa_cheptel=0 THEN '0' 
  441.                                     WHEN spec.pa_cheptel is null THEN '0' 
  442.                         ELSE ceil(".(float) $depenseP."/spec.pa_cheptel) END ,";
  443.                     $statP .= " CASE WHEN spec.pa_cheptel=0 THEN '0' 
  444.                                     WHEN spec.pa_cheptel is null THEN '0' 
  445.                         ELSE ceil(".(float) $recettePP."/spec.pa_cheptel) END ,";
  446.                     $statP .= " CASE WHEN spec.pa_cheptel=0 THEN '0' 
  447.                                     WHEN spec.pa_cheptel is null THEN '0' 
  448.                         ELSE ceil((".(float) $recettePP." - ".$depenseP.")/spec.pa_cheptel) END , ";
  449.                     
  450.                     if ($nbVenteJ !=0) {
  451.                         $statP .= " ceil(".(float)$sommePrixJ/ (float) $nbVenteJ.") , ";
  452.                     } else {
  453.                         $statP .= "'0', ";
  454.                     }
  455.                     if ($nbVenteA !=0) {
  456.                         $statP .= " ceil(".(float)$sommePrixA/ (float) $nbVenteA.") , ";
  457.                     } else {
  458.                         $statP .= "'0', ";
  459.                     }
  460.                     if ($nbVenteAu !=0) {
  461.                         $statP .= " ceil(".(float)$sommePrixAu/ (float) $nbVenteAu.")  ";
  462.                     } else {
  463.                         $statP .= "'0' ";
  464.                     }
  465.                     
  466.                 $myKey $key+1;
  467.                 $reqInsert =
  468.               "INSERT INTO datamart (
  469.               SELECT f.id,
  470.                   f.nom,
  471.                   f.prenom,
  472.                   f.latitude,
  473.                   f.longitude,
  474.                   expi.id ,
  475.                   expi.annee ,
  476.                   expi.date_collecte ,
  477.                   organisation.value,
  478.                   technicien.value ,
  479.                   expi.nom_enqueteur ,
  480.                   expi.id_collecteur ,
  481.                   (select array_to_string(array_agg(distinct(lp.value)),',') from exploitant_identite ei
  482.                   LEFT JOIN exploitant_identite_list_profil eilp ON eilp.exploitant_identite_id = ei.id
  483.                   LEFT JOIN list_profil lp ON eilp.list_profil_id = lp.id where ei.id = ".$resExp['exploitant_identite_id']." ),
  484.                   sexe.value ,
  485.                   expi.annee_naissance ,
  486.                   region.value,
  487.                   district.value ,
  488.                   commune.value ,
  489.                   expi.fokontany ,
  490.                   expi.telephone ,
  491.                   (select array_to_string(array_agg(distinct(lto.value)),',') from exploitant_identite ei
  492.                   LEFT JOIN exploitant_identite_list_type_op eilto ON eilto.exploitant_identite_id = ei.id
  493.                   LEFT JOIN list_type_op lto ON eilto.list_type_op_id = lto.id where ei.id = ".$resExp['exploitant_identite_id']." ),
  494.                   expi.nom_op ,
  495.                   adhesion_structure.value ,
  496.                   expi.taille_menage ,
  497.                   periode_soudure.value ,
  498.                   eaf.value ,
  499.                   specialite.value, 
  500.                   
  501.                   spec.id,
  502.                   spec.pv_variete,
  503.                   saison.value,
  504.                   eau.value, 
  505.                   spec.pv_surface,
  506.                   spec.pv_date_semis,
  507.                   spec.pv_date_repiquage,
  508.                   spec.pv_date_recolte,
  509.                   spec.pv_volume,
  510.                   spec.pv_obs_itk,
  511.                   spec.pv_obs_contexte,
  512.                   spec.pv_obs_commerce,
  513.                   spec.pv_obs_code_a,
  514.                   spec.pv_obs_code_b,
  515.                   
  516.                   spec.pa_race,
  517.                   elevage.value,
  518.                   spec.pa_duree_cycle,
  519.                   spec.pa_cheptel,
  520.                   spec.pa_investi_repro,
  521.                   spec.pa_date_debut,
  522.                   spec.pa_date_fin,
  523.                   spec.pa_nb_cheptel,
  524.                   spec.pa_obs_itk,
  525.                   spec.pa_obs_contexte,
  526.                   spec.pa_obs_commerce,
  527.                   spec.pa_obs_code_a,
  528.                   spec.pa_obs_code_b,
  529.                   
  530.                 $chargeV
  531.                 $chargeC
  532.                 $prodC
  533.                     1,
  534.                     $statV,
  535.                         $statP
  536.                     
  537.                  FROM exploitant f
  538.                  LEFT JOIN exploitant_identite expi ON f.id = expi.exploitant_id
  539.                  LEFT JOIN exploitant_speculation spec ON expi.id = spec.exploitant_identite_id
  540.                  LEFT JOIN list_region region ON region.id = expi.region_exp_id
  541.                  LEFT JOIN list_district district ON district.id = expi.district_exp_id
  542.                  LEFT JOIN list_commune commune ON commune.id = expi.commune_exp_id
  543.                  LEFT JOIN list_organisation organisation ON organisation.id = expi.organisation_id
  544.                  LEFT JOIN list_technicien technicien ON technicien.id = expi.technicien_id
  545.                  LEFT JOIN list_sexe sexe ON sexe.id = expi.sexe_id
  546.                  LEFT JOIN list_responsabilite_op adhesion_structure ON adhesion_structure.id = expi.responsabilite_op_id
  547.                  LEFT JOIN list_periode_soudure periode_soudure ON periode_soudure.id = expi.periode_soudure_id
  548.                  LEFT JOIN list_economie_exploitation economie_exploitation ON economie_exploitation.id = expi.economie_exploitation_id
  549.                  LEFT JOIN list_niveau_vie niveau_vie ON niveau_vie.id = expi.niveau_vie_id
  550.                  LEFT JOIN list_eaf eaf ON eaf.id = expi.eaf_id
  551.                  LEFT JOIN list_produit specialite ON specialite.id = expi.specialite".$myKey."_id
  552.                  LEFT JOIN list_responsabilite_op responsabilite_op ON responsabilite_op.id = expi.responsabilite_op_id
  553.                  LEFT JOIN list_type_culture culture  ON culture.id = spec.pv_type_culture_id
  554.                  LEFT JOIN list_saison saison ON saison.id = spec.pv_saison_id
  555.                  LEFT JOIN list_eau eau  ON eau.id = spec.pv_eau_id
  556.                  LEFT JOIN list_elevage elevage  ON elevage.id = spec.pa_elevage_id
  557.                  WHERE spec.id = ".$resExp['id']."  AND expi.specialite".$myKey."_id is not null
  558.               -- WITH DATA;
  559.               );"
  560.             ;
  561.            echo 'a';
  562.               
  563.                try {
  564.                   // var_dump($reqInsert);exit;
  565.                   $resultInsert $this->execute($reqInsert);
  566.                 } catch (\Exception $e) {
  567.                   print_r($e);
  568.                 }
  569.             }
  570.           }
  571.       }
  572.       //return new Response($req);
  573.       If($resultInsert) {
  574.         return $this->render('datamart/sql.html.twig');
  575.       }
  576.       else{
  577.         return $this->render('datamart/sql.html.twig',
  578.           [
  579.             "result" => $resultInsert
  580.         ]);
  581.       }
  582.     }
  583.     /**
  584.      * @Route("/dashboard", name="dashboard")
  585.      */
  586.     public function dashboard()
  587.     {
  588.         $em $this->getDoctrine()->getManager();
  589.         $sql "";
  590.         if(in_array('ROLE_USER'$this->getUser()->getRoles()) || in_array('ROLE_OPERATOR'$this->getUser()->getRoles())){
  591.             $user $this->getUser();
  592.             $listeExp $em->getRepository(UserExploitant::class)->findBy(array('userId'=>$user->getId()));
  593.             $myLis "";
  594.             $last_key = @end(array_keys($listeExp));
  595.             foreach ($listeExp as $key => $value) {
  596.                 if ($key == $last_key) {
  597.                     $myLis .= $value->getExpId();
  598.                 } else {
  599.                     $myLis .= $value->getExpId().",";
  600.                 }
  601.             }
  602.             
  603.             if ($myLis == "" ){
  604.                 $sql .= " AND exp.id = 0 ";
  605.             } else {
  606.                 $sql .= " AND exp.id IN ($myLis) ";                 
  607.             }
  608.         }
  609.         
  610.         if( in_array('ROLE_REGIONAL'$this->getUser()->getRoles())){
  611.             $user $this->getUser();
  612.             $region $user->getRegion();
  613.             
  614.             if ($region == "" ){
  615.                 $sql .= " AND expi.region_exp_id = 0 ";
  616.             } else {
  617.                 $regions explode('||'$region);
  618.                 $list '';
  619.                 $i 0;
  620.                 foreach($regions as $r) {
  621.                     if ($r != '' && $i == 0){
  622.                         $list .= $r;
  623.                         $i++;
  624.                     } else {
  625.                         if ($r != '') {
  626.                             $list .= ','.$r;
  627.                             $i++;
  628.                         }
  629.                         
  630.                     }
  631.                 }
  632.                 $sql .= " AND expi.region_exp_id IN ($list) ";                 
  633.             }
  634.         }
  635.         
  636.         $rsm = new ResultSetMapping();
  637.         $rsm->addScalarResult('nom''nom');
  638.         $rsm->addScalarResult('prenom''prenom');
  639.         $rsm->addScalarResult('id''id');
  640.         $rsm->addScalarResult('annee''annee');
  641.         $rsm->addScalarResult('latitude''latitude');
  642.         $rsm->addScalarResult('longitude''longitude');
  643.         $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  distinct(exp.id) as id, exp.nom as nom, exp.prenom as prenom,
  644.                 max(expi.annee) as annee, exp.latitude, exp.longitude, expi.region_exp_id
  645.                 FROM exploitant exp 
  646.                 JOIN exploitant_identite expi ON expi.exploitant_id = exp.id 
  647.                 WHERE 1=1  $sql group by exp.id,expi.region_exp_id ORDER BY nom asc "$rsm);
  648.         $exps $query->getResult();
  649.         
  650.         $nb_exp $em->getRepository(Exploitant::class)
  651.             ->createQueryBuilder('a')->select('count(a.id)')
  652.             ->getQuery()->getSingleScalarResult();
  653.         $farms $em->getRepository(Exploitant::class)->findAll();
  654.         return $this->render('index.html.twig', [
  655.             "nb" => count($exps),
  656.             "farms" => $exps,
  657.         ]);
  658.     }
  659.     public function sidebar()
  660.     {
  661.         $em $this->getDoctrine()->getManager();
  662.         $farms $em->getRepository(Exploitant::class)->findAll();
  663.         return $this->render('sidebar.html.twig', [
  664.           "farms" => $farms,
  665.         ]);
  666.     }
  667. }