src/Controller/ExploitantController.php line 610

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 Symfony\Component\HttpFoundation\Request;
  6. use App\Entity\Exploitant;
  7. use App\Entity\ExploitantIdentite;
  8. use App\Entity\ExploitantPratique;
  9. use App\Entity\ExploitantSpeculation;
  10. use App\Entity\ListRegion;
  11. use App\Entity\ListDistrict;
  12. use App\Entity\ListCommune;
  13. use App\Entity\ListTypeCharge;
  14. use App\Entity\ListSousCharge;
  15. use App\Entity\ListTypeDepense;
  16. use App\Entity\ListTypeProduit;
  17. use App\Entity\ListProduit;
  18. use App\Entity\ListUnite;
  19. use App\Entity\Charge;
  20. use App\Entity\ListMois;
  21. use App\Form\ExploitantNewType;
  22. use App\Form\ExploitantIdentiteNewType;
  23. use App\Form\ExploitantPratiqueNewType;
  24. use App\Form\ExploitantEditType;
  25. use Doctrine\ORM\Query\ResultSetMapping;
  26. use App\Entity\Produit;
  27. use App\Entity\ListProduction;
  28. use App\Entity\UserExploitant;
  29. /**
  30.  * @Route("/dashboard/exploitant/", name="exp")
  31.  */
  32. class ExploitantController extends AbstractController
  33. {
  34.     /**
  35.      * @Route("{id}/details", name="_details")
  36.      */
  37.     public function details(Request $request$id)
  38.     {
  39.       $em $this->getDoctrine()->getManager();
  40.       
  41.       $new_farm_infos = new ExploitantIdentite();
  42.       $form NULL;
  43.       $farm $em->getRepository(Exploitant::class)->find($id);
  44.       
  45.       $retour $this->checkSecurity($idtrue);
  46.         if (!$retour){
  47.             return $this->redirectToRoute("exp_crud");
  48.         }
  49.       $last_farm_infos $em->getRepository(ExploitantIdentite::class)->findOneBy(["exploitant" => $farm],["annee" => "DESC"]);
  50.       
  51.       if($last_farm_infos){
  52.         $new_farm_infos = clone $last_farm_infos;
  53.         $new_farm_infos->setAnnee($last_farm_infos->getAnnee()+1);
  54.       }
  55.       if(!$farm) return $this->render('error/blank.html.twig', []);
  56.       $form $this->createForm(ExploitantIdentiteNewType::class, $new_farm_infos);
  57.       $form->handleRequest($request);
  58.       if ($form->isSubmitted() && $form->isValid()) {
  59.           $new_farm_infos->setExploitant($farm);
  60.           $em->persist($new_farm_infos);
  61.           $em->flush();
  62.           return $this->redirectToRoute("exp_details", ["id" => $farm->getId()]);
  63.       }
  64.       
  65.       return $this->render('exp/details.html.twig', [
  66.         "farm" => $farm,
  67.         'form' => $form->createView(),
  68.       ]);
  69.     }
  70.     /**
  71.      * @Route("crud", name="_crud")
  72.      */
  73.     public function crud(Request $request)
  74.     {
  75.         $em $this->getDoctrine()->getManager();
  76.         //$exps = $em->getRepository(Exploitant::class)->findAll(["name" => "ASC"]);
  77.         $regions $em->getRepository(ListRegion::class)->findBy(["isProjet"=>true],["value" => "ASC"]);
  78.         
  79.         $annee date("Y");
  80.         $regionEnCours "";
  81.         $sql "";
  82.         $user $this->getUser();
  83.         
  84.         if(in_array('ROLE_USER'$this->getUser()->getRoles()) || in_array('ROLE_OPERATOR'$this->getUser()->getRoles())){
  85.             
  86.             $listeExp $em->getRepository(UserExploitant::class)->findBy(array('userId'=>$user->getId()));
  87.             $myLis "";
  88.             $last_key = @end(array_keys($listeExp));
  89.             foreach ($listeExp as $key => $value) {
  90.                 if ($key == $last_key) {
  91.                     $myLis .= $value->getExpId();
  92.                 } else {
  93.                     $myLis .= $value->getExpId().",";
  94.                 }
  95.             }
  96.             
  97.             if ($myLis == "" ){
  98.                 $sql .= " AND exp.id = 0 ";
  99.             } else {
  100.                 $sql .= " AND exp.id IN ($myLis) ";                 
  101.             }
  102.         }
  103.         
  104.         if( in_array('ROLE_REGIONAL'$this->getUser()->getRoles())){
  105.             $region $user->getRegion();
  106.             
  107.             if ($region == "" ){
  108.                 $sql .= " AND expi.region_exp_id = 0 ";
  109.             } else {
  110.                 $regions explode('||'$region);
  111.                 $list '';
  112.                 $i 0;
  113.                 foreach($regions as $r) {
  114.                     if ($r != '' && $i == 0){
  115.                         $list .= $r;
  116.                         $i++;
  117.                     } else {
  118.                         if ($r != '') {
  119.                             $list .= ','.$r;
  120.                             $i++;
  121.                         }
  122.                         
  123.                     }
  124.                 }
  125.                 $sql .= " AND expi.region_exp_id IN ($list) ";                 
  126.             }
  127.         }
  128.         
  129.         if (isset($_GET["myregion"]) && $_GET["myregion"] != "") {
  130.             $region $_GET["myregion"];
  131.             $sql .= " AND expi.region_exp_id = $region ";
  132.             $regionEnCours $region;
  133.         } 
  134.       
  135.         $rsm = new ResultSetMapping();
  136.         $rsm->addScalarResult('annee''annee');
  137.         $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT distinct(annee) 
  138.               FROM exploitant_identite gri
  139.               WHERE 1=1 ORDER BY annee desc"$rsm);
  140.         $annees $query->getResult();
  141.         
  142.         $anneeEnCours =  $annees[0]["annee"];
  143.         if (isset($_GET["myannee"]) && $_GET["myannee"] != "") {
  144.             $annee $_GET["myannee"];
  145.             
  146.             $anneeEnCours $annee;
  147.         }
  148.         
  149.         $sql .= " AND expi.annee = $anneeEnCours ";
  150.         
  151.         $rsm = new ResultSetMapping();
  152.         $rsm->addScalarResult('nom''nom');
  153.         $rsm->addScalarResult('prenom''prenom');
  154.         $rsm->addScalarResult('id''id');
  155.         $rsm->addScalarResult('annee''annee');
  156.         $rsm->addScalarResult('commune''commune');
  157.         $rsm->addScalarResult('eaf''eaf');
  158.         $rsm->addScalarResult('updated_at''updated_at');
  159.         $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  distinct(exp.id) as id, exp.nom as nom, exp.prenom as prenom,
  160.                 max(expi.annee) as annee, (array_agg(lc.value ORDER BY lc.value ASC))[1] as commune, 
  161.                 (array_agg(eaf.value ORDER BY eaf.value ASC))[1]  as eaf,
  162.                 ( GREATEST(exp.updated_at, expi.updated_at)  ) as updated_at
  163.                 FROM exploitant exp 
  164.                 JOIN exploitant_identite expi ON expi.exploitant_id = exp.id 
  165.                 LEFT JOIN list_commune lc ON lc.id = expi.commune_exp_id
  166.                 LEFT JOIN list_eaf eaf ON eaf.id = expi.eaf_id
  167.                 WHERE 1=1  $sql group by exp.id, expi.updated_at ORDER BY nom asc "$rsm);
  168.         $exps $query->getResult();
  169.         return $this->render('exp/crud.html.twig', [
  170.           "exps" => $exps,
  171.           "regions" => $regions,
  172.           "annees" => $annees
  173.           "anneeEnCours" => $anneeEnCours
  174.           "regionEnCours" => $regionEnCours
  175.           "user"  =>  $user,
  176.         ]);
  177.     }
  178.     /**
  179.      * @Route("new", name="_new")
  180.      */
  181.     public function new(Request $request)
  182.     {
  183.         $retour $this->checkSecurity(null);
  184.         if (!$retour){
  185.             return $this->redirectToRoute("exp_crud");
  186.         }
  187.       $em $this->getDoctrine()->getManager();
  188.       
  189.       $charges $em->getRepository(ListTypeCharge::class)->findBy(array(), ["value" => "ASC"]);
  190.       $sscharges $em->getRepository(ListSousCharge::class)->findBy(array(), ["value" => "ASC"]);
  191.       $depenses $em->getRepository(ListTypeDepense::class)->findBy(array(), ["value" => "ASC"]);
  192.       $produits $em->getRepository(ListTypeProduit::class)->findBy(array(), ["value" => "ASC"]);
  193.       $unites $em->getRepository(ListUnite::class)->findBy(array(), ["value" => "ASC"]);
  194.       $mois $em->getRepository(ListMois::class)->findBy(array(), ["id" => "ASC"]);
  195.       $listeProduits $em->getRepository(ListProduit::class)->findBy(["isVegetal" => false]);
  196.       $production $em->getRepository(ListProduction::class)->findBy([]);
  197.       
  198.       $listeIsVegetal = array();
  199.       foreach($listeProduits as $pr){
  200.           $listeIsVegetal[] = $pr->getId();
  201.       }
  202.       $new_farm = new Exploitant();
  203.       $new_farm_identite = new ExploitantIdentite();
  204.       //$new_farm_pratique = new ExploitantPratique();
  205.       $new_farm_speculation1 = new ExploitantSpeculation();
  206.       $new_farm_speculation2 = new ExploitantSpeculation();
  207.       $new_farm_speculation3 = new ExploitantSpeculation();
  208.       $new_farm_speculation4 = new ExploitantSpeculation();
  209.       //$new_farm_identite->addExploitantPratique($new_farm_pratique);
  210.       $new_farm_identite->addExploitantSpeculation($new_farm_speculation1);
  211.       $new_farm_identite->addExploitantSpeculation($new_farm_speculation2);
  212.       $new_farm_identite->addExploitantSpeculation($new_farm_speculation3);
  213.       $new_farm_identite->addExploitantSpeculation($new_farm_speculation4);
  214.       $new_farm->addExploitantIdentite($new_farm_identite);
  215.       $form $this->createForm(ExploitantNewType::class, $new_farm);
  216.       $form->handleRequest($request);
  217.       if ($form->isSubmitted() && $form->isValid()) {
  218.           
  219.         //var_dump($_POST);exit;
  220.         for ($i=0;$i<4;$i++) {
  221.             $tab = array();
  222.             if (isset($_POST["type".$i])) {
  223.                 //var_dump($_POST["type".$i]);exit;
  224.                 foreach ($_POST["type".$i] as $j=>$type){
  225.                     $k $i+1;
  226.                     $sscharge $_POST["sscharge".$i][$j];
  227.                     $produit $_POST["produit".$i][$j];
  228.                     $depense $_POST["depense".$i][$j];
  229.                     $unite $_POST["unite".$i][$j];
  230.                     $index "".$type.$sscharge.$produit.$depense.$unite."";
  231.                     //var_dump($_POST);exit;
  232.                     //$moisList = $em->getRepository(ListMois::class)->findOneByValue($_POST["mois".$i][$j]);
  233.                     if (isset($tab[$index])) {
  234.                         $retour $tab[$index];
  235.                         $retour["mois".$_POST["mois".$i][$j]] = ($_POST["somme".$i][$j]=="")?0:$_POST["somme".$i][$j];
  236.                         $retour["sscharge"] = $_POST["sscharge".$i][$j];
  237.                         $retour["produit"] = $_POST["produit".$i][$j];
  238.                         $retour["depense"] = $_POST["depense".$i][$j];
  239.                         $retour["unite"] = $_POST["unite".$i][$j];
  240.                         $retour["pu"] = ($_POST["pu".$i][$j]=="")?0:$_POST["pu".$i][$j];
  241.                         $retour["quantite"] = ($_POST["quantite".$i][$j]=="")?0:$_POST["quantite".$i][$j];
  242.                         $retour["type"] = $_POST["type".$i][$j];
  243.                         //$tab[$index] = $retour;
  244.                         
  245.                     } else {
  246.                         $retour = array(1=>02=>03=>04=>05=>06=>07=>08=>09=>010=>011=>012=>0);
  247.                         for($w=1;$w<13$w++) {
  248.                             $retour["mois".$w] = 0;
  249.                         }
  250.                         $retour["mois".$_POST["mois".$i][$j]] = ($_POST["somme".$i][$j]=="")?0:$_POST["somme".$i][$j];
  251.                         $retour["type"] = $_POST["type".$i][$j];
  252.                         $retour["sscharge"] = $_POST["sscharge".$i][$j];
  253.                         $retour["produit"] = $_POST["produit".$i][$j];
  254.                         $retour["depense"] = $_POST["depense".$i][$j];
  255.                         $retour["unite"] = $_POST["unite".$i][$j];
  256.                         $retour["pu"] = ($_POST["pu".$i][$j]=="")?0:$_POST["pu".$i][$j];
  257.                         $retour["quantite"] = ($_POST["quantite".$i][$j]=="")?0:$_POST["quantite".$i][$j];
  258.                         $tab[$j] = $retour;
  259.                     }
  260.                     
  261.                 }
  262.             }
  263.             //var_dump($tab);exit;
  264.             foreach ($tab as $key=>$t){
  265.                 
  266.                 $charge = new Charge();
  267.                 $typeList $em->getRepository(ListTypeCharge::class)->findOneById($t["type"]);
  268.                 $charge->setTypeCharge($typeList);
  269.                 $typeList $em->getRepository(ListSousCharge::class)->findOneById($t["sscharge"]);
  270.                 $charge->setSousCharge($typeList);
  271.                 //$typeList = $em->getRepository(ListTypeProduit::class)->findOneById($t["produit"]);
  272.                 $charge->setTypeProduit($t["produit"]);
  273.                 $typeList $em->getRepository(ListTypeDepense::class)->findOneById($t["depense"]);
  274.                 $charge->setTypeDepense($typeList);
  275.                 $typeList $em->getRepository(ListUnite::class)->findOneById($t["unite"]);
  276.                 $charge->setUnite($typeList);
  277.                 $mychar "";
  278.                 for($q=1;$q<13$q++) {
  279.                     if ($q==1) {
  280.                         $mychar .= $t["mois".$q];
  281.                     } else {
  282.                         $mychar .= "//".$t["mois".$q];
  283.                     }
  284.                     $methodName 'setMois'.$q;//this is the good way
  285.                     $charge->$methodName($t["mois".$q]);
  286.                 }
  287.                 $charge->setValue($mychar);
  288.                 $charge->setPu($t["pu"]);
  289.                 $charge->setQuantite($t["quantite"]);
  290.                 ${"new_farm_speculation".$k}->addCharge($charge);
  291.             }
  292.         }  
  293.         
  294.         for ($i=0;$i<4;$i++) {
  295.               
  296.             $tab = array();
  297.             
  298.             if (isset($_POST["production".$i])) {
  299.                 //var_dump($_POST["type".$i]);exit;
  300.                 foreach ($_POST["production".$i] as $j=>$type){
  301.                     $k $i+1;
  302.                     $index "".$type."";
  303.                     $retour=array();
  304.                     //var_dump($type);exit;
  305.                     $retour["type"] = $type;
  306.                     $retour["detail"] = $_POST["detailproduction".$i][$j];
  307.                     $retour["somme"] = ($_POST["sommeproduction".$i][$j]=="")?0:$_POST["sommeproduction".$i][$j];;
  308.                     $retour["unite"] = $_POST["uniteproduction".$i][$j];
  309.                     $retour["pu"] = ($_POST["puproduction".$i][$j]=="")?0:$_POST["puproduction".$i][$j];
  310.                     $retour["quantite"] = ($_POST["quantiteproduction".$i][$j]=="")?0:$_POST["quantiteproduction".$i][$j];
  311.                     $tab[] = $retour;
  312.                     
  313.                 }
  314.             }
  315.             //var_dump($tab);exit;
  316.             foreach ($tab as $key=>$t){
  317.                 $typeList $em->getRepository(ListProduction::class)->findOneById($t["type"]);
  318.                 $unite $em->getRepository(ListUnite::class)->findOneById($t["unite"]);
  319.                 
  320.                 $charge = new Produit();
  321.                 $charge->setProduction($typeList);
  322.                 $charge->setDetail($t["detail"]);
  323.                 $charge->setUnite($unite);
  324.                 $charge->setSomme($t["somme"]);
  325.                 $charge->setQuantite($t["quantite"]);
  326.                 $charge->setPu($t["pu"]);
  327.                 
  328.                 //if (isset($farm_infos->getExploitantSpeculation()[$i])) {
  329.                     ${"new_farm_speculation".$k}->addProduit($charge);
  330.                 //}
  331.             }
  332.         } 
  333.         
  334.       //var_dump($_POST);exit;
  335.           $em->persist($new_farm_identite);
  336.           $new_farm->setUpdatedAt(new \Datetime());
  337.           $em->persist($new_farm);
  338.           $em->flush();
  339.           if(in_array('ROLE_OPERATOR'$this->getUser()->getRoles())){
  340.             $user =$this->getUser()->getId();
  341.             $value = new UserExploitant();
  342.             $value->setUserId($user);
  343.             $value->setExpId($new_farm->getId());
  344.             $em->persist($value);
  345.             $em->flush($value);
  346.         }
  347.         
  348.           return $this->redirectToRoute("exp_details", ["id" => $new_farm->getId()]);
  349.       }
  350.       return $this->render('exp/new.html.twig', [
  351.         'form' => $form->createView(),
  352.         'charges' => $charges,
  353.         'sscharges' => $sscharges,
  354.         'depenses' => $depenses,
  355.         'produits' => $produits,
  356.         'unites' => $unites,
  357.         'production' => $production,
  358.         'mois'  => $mois,  
  359.         'listeIsVegetal'  => $listeIsVegetal,  
  360.       ]);
  361.     }
  362.     /**
  363.      * @Route("{id}/edit", name="_edit")
  364.      */
  365.     public function edit(Request $request$id)
  366.     {
  367.         $retour $this->checkSecurity($id);
  368.         if (!$retour){
  369.             return $this->redirectToRoute("exp_crud");
  370.         }
  371.       $em $this->getDoctrine()->getManager();
  372.       $form NULL;
  373.       $farm $em->getRepository(Exploitant::class)->find($id);
  374.       if(!$farm) return $this->render('error/blank.html.twig', []);
  375.       $form $this->createForm(ExploitantEditType::class, $farm);
  376.       $form->handleRequest($request);
  377.       if ($form->isSubmitted() && $form->isValid()) {
  378.           $farm->setUpdatedAt(new \Datetime());
  379.            $em->persist($farm);
  380.           $em->flush();
  381.           return $this->redirectToRoute("exp_details", ["id" => $farm->getId()]);
  382.       }
  383.       return $this->render('exp/edit.html.twig', [
  384.         "farm" => $farm,
  385.         'form' => $form->createView(),
  386.       ]);
  387.     }
  388.     /**
  389.      * @Route("{id}/delete", name="_delete")
  390.      */
  391.      public function delete($id)
  392.      {
  393.         $retour $this->checkSecurity($id);
  394.         if (!$retour){
  395.             return $this->redirectToRoute("exp_crud");
  396.         }
  397.         
  398.        $em $this->getDoctrine()->getManager();
  399.        $farm $em->getRepository(Exploitant::class)->find($id);
  400.        $em->remove($farm);
  401.        $em->flush();
  402.        return $this->redirectToRoute("exp_crud");
  403.      }
  404.          /**
  405.      * @Route("import", name="_import")
  406.      */
  407.     public function import(Request $request)
  408.     {
  409.         $em $this->getDoctrine()->getManager();
  410.         $row $i 0;
  411.         
  412.         $arrayRegion = [];
  413.         $arrayDistrict = [];
  414.         $arrayCommune = [];
  415.         // Import du fichier CSV
  416.         if (($handle fopen("comall.csv""r")) !== FALSE) { // Lecture du fichier, Ã  adapter
  417.             while (($data fgetcsv($handle10000",")) !== FALSE) { // Eléments séparés par une virgule, Ã  modifier si necessaire
  418.                 $num count($data); // Nombre d'éléments sur la ligne traitée
  419.                 if ($row != 0) {
  420.                     //var_dump($data);exit;
  421.                      $idRegion $data[2];
  422.                      $nomRegion $data[4];
  423.                      
  424.                      $idDistrict $data[1];
  425.                      $nomDistrict $data[5];
  426.                     
  427.                      if (!in_array($idRegion$arrayRegion)) {
  428.                          $rsm = new ResultSetMapping();
  429.                          $rsm->addScalarResult('value''value');
  430.         
  431.                         $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  value
  432.                                 FROM list_region 
  433.                                 WHERE id = $idRegion "$rsm);
  434.                         $exps $query->getResult();
  435.                         
  436.                         if ($nomRegion != $exps[0]['value']) {
  437.                             echo $idRegion.' -- '.$nomRegion.' :  '.$exps[0]['value'].'  Pas le même nom<br />';
  438.                         } else {
  439.                             echo $idRegion.' -- '.$nomRegion.' :  '.$exps[0]['value'].'<br />';
  440.                         }
  441.                         
  442.                      }
  443.                      $arrayRegion[] = $idRegion;
  444.                      
  445.                      
  446.                      
  447.                 }
  448.                 $row++;
  449.             }
  450.         }
  451.         
  452.         echo '<br />DISTRICT<br />';
  453.         $row $i 0;
  454.         if (($handle fopen("comall.csv""r")) !== FALSE) { // Lecture du fichier, Ã  adapter
  455.             while (($data fgetcsv($handle10000",")) !== FALSE) { // Eléments séparés par une virgule, Ã  modifier si necessaire
  456.                 $num count($data); // Nombre d'éléments sur la ligne traitée
  457.                 if ($row != 0) {
  458.                     //var_dump($data);exit;
  459.                      $idRegion $data[2];
  460.                      $nomRegion $data[4];
  461.                      
  462.                      $idDistrict $data[1];
  463.                      $nomDistrict $data[5];
  464.                     
  465.                      
  466.                      if (!in_array($idDistrict$arrayDistrict)) {
  467.                          $rsm = new ResultSetMapping();
  468.                          $rsm->addScalarResult('value''value');
  469.         
  470.                         $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  value
  471.                                 FROM list_district 
  472.                                 WHERE id = $idDistrict "$rsm);
  473.                         $exps $query->getResult();
  474.                         
  475.                         if ($nomDistrict != $exps[0]['value']) {
  476.                             echo $idDistrict.' -- '.$nomDistrict.' :  '.$exps[0]['value'].'  Pas le même nom<br />';
  477.                         } else {
  478.                             echo $idDistrict.' -- '.$nomDistrict.' :  '.$exps[0]['value'].'<br />';
  479.                         }
  480.                         
  481.                      }
  482.                      $arrayDistrict[] = $idDistrict;
  483.                      
  484.                 }
  485.                 $row++;
  486.             }
  487.         }
  488.         
  489.         echo '<br />COMMUNE<br />';
  490.         $row $i 0;
  491.         if (($handle fopen("comall.csv""r")) !== FALSE) { // Lecture du fichier, Ã  adapter
  492.             while (($data fgetcsv($handle10000",")) !== FALSE) { // Eléments séparés par une virgule, Ã  modifier si necessaire
  493.                 $num count($data); // Nombre d'éléments sur la ligne traitée
  494.                 if ($row != 0) {
  495.                     //var_dump($data);exit;
  496.                      $idRegion $data[2];
  497.                      $nomRegion $data[4];
  498.                      
  499.                      $idDistrict $data[1];
  500.                      $nomCommune $data[6];
  501.                     
  502.                      
  503.                      //if (!in_array($idDistrict, $arrayDistrict)) {
  504.                          $rsm = new ResultSetMapping();
  505.                          $rsm->addScalarResult('value''value');
  506.         
  507.                         $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  value
  508.                                 FROM list_commune
  509.                                 WHERE value = upper('".pg_escape_string($nomCommune)."') AND district_id =  $idDistrict "$rsm);
  510.                         
  511.                          
  512.                         $exps $query->getResult();
  513.                         
  514.                         if (isset($exps[0]) ) {
  515.                             if (strtoupper($nomCommune) != $exps[0]['value']) {
  516.                                 echo $idDistrict.' -- '.$nomCommune.' :  '.$exps[0]['value'].'  Pas le même nom<br />';
  517.                             } else {
  518.                                 echo $idDistrict.' -- '.$nomCommune.' :  '.$exps[0]['value'].'<br />';
  519.                             }
  520.                         } else {
  521.                             //echo 'Pas de correpondance '.$nomCommune.' ---- ';
  522.                             
  523.                             $rsm = new ResultSetMapping();
  524.                             $rsm->addScalarResult('value''value');
  525.                            $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  value
  526.                                    FROM list_commune
  527.                                    WHERE levenshtein(value,upper('".pg_escape_string($nomCommune)."')) <3 AND district_id =  $idDistrict "$rsm);
  528.                            $exps $query->getResult();
  529.                            
  530.                            $exps $query->getResult();
  531.                         
  532.                         if (isset($exps[0]) ) {
  533.                             if (strtoupper($nomCommune) != $exps[0]['value']) {
  534.                                 echo $idDistrict.' -- '.strtoupper($nomCommune).' :  '.strtoupper($exps[0]['value']).'  Pas le même nom<br />';
  535.                             } else {
  536.                                 echo $idDistrict.' -- '.strtoupper($nomCommune).' :  '.$exps[0]['value'].'<br />';
  537.                             }
  538.                         } else {
  539.                             $i++;
  540.                             echo 'Pas de correpondance '.$nomCommune.' '.$i.'<br />';
  541.                             
  542.                             
  543.                             
  544.                         }
  545.                             
  546.                         }
  547.                      //}
  548.                      $arrayDistrict[] = $idDistrict;
  549.                      
  550.                 }
  551.                 
  552.                 
  553.                 $row++;
  554.             }
  555.         }
  556.         echo 'Il y a '.$i.' communes sans correspondance';
  557.         exit;
  558.     }
  559.     
  560.     /**
  561.      * @Route("type/{id}/district", name="_type_district")
  562.      */
  563.     public function type_varieties($id)
  564.     {
  565.         $em $this->getDoctrine()->getManager();
  566.         $region $em->getRepository(ListDistrict::class)->findBy(["region"=>$id"isProjet"=>true], ["value"=>"ASC"]);
  567.         return $this->render('region/select_district.html.twig', [
  568.           "region" => $region
  569.         ]);
  570.     }
  571.     
  572.     /**
  573.      * @Route("type/{id}/commune", name="_type_commune")
  574.      */
  575.     public function type_commune($id)
  576.     {
  577.         $em $this->getDoctrine()->getManager();
  578.         //var_dump($id);exit;
  579.         if (isset($id) && $id != "null") {
  580.             $district $em->getRepository(ListCommune::class)->findBy(["district"=>$id"isProjet"=>true], ["value"=>"ASC"]);
  581.         } else {
  582.             $district null;
  583.         }
  584.         
  585.         return $this->render('region/select_district.html.twig', [
  586.           "region" => $district
  587.         ]);
  588.     }
  589.     
  590.     /**
  591.      * @Route("/getSecteur", name="_get_district")
  592.      */
  593.     public function getSecteur(Request $request) {
  594.         if (isset($_POST["region"])) {
  595.             $region implode($_POST["region"], ",");
  596.             
  597.             
  598.             $rsm = new ResultSetMapping();
  599.             $rsm->addScalarResult('id''id');
  600.             $rsm->addScalarResult('value''value');
  601.             $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  id, value from list_district where region_id IN (".$region.") ORDER BY value asc "$rsm);
  602.             $secteurs $query->getResult();
  603.             $list "<optgroup label='Sélection District(s)'>";
  604.             foreach ($secteurs as $secteur) {
  605.                 $list .= "<option value=".$secteur["id"].">".$secteur["value"]."</option>";
  606.             }
  607.             $list .= "</optgroup>";
  608.             echo ($list);exit;
  609.         }
  610.     }
  611.     
  612.     /**
  613.      * @Route("/getCommune", name="_get_commune")
  614.      */
  615.     public function getCommune(Request $request) {
  616.         if (isset($_POST["secteur"])) {
  617.             $region implode($_POST["secteur"], ",");
  618.             $commune null;
  619.             if (isset($_POST["commune"])){
  620.                 $commune implode($_POST["commune"], ",");
  621.             }
  622.             $sql "";
  623.             if (isset($commune) && $commune != "null" ) {
  624.                 $sql "AND id NOT IN (".$commune.")";
  625.             }
  626.             
  627.             $rsm = new ResultSetMapping();
  628.             $rsm->addScalarResult('id''id');
  629.             $rsm->addScalarResult('value''value');
  630.             $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  id, value from list_commune where district_id IN (".$region.") $sql ORDER BY value asc "$rsm);
  631.             $secteurs $query->getResult();
  632.            $list "<optgroup label='Sélection Commune(s)'>";
  633.            foreach ($secteurs as $secteur) {
  634.                $list .= "<option value=".$secteur["id"].">".$secteur["value"]."</option>";
  635.            }
  636.            $list .= "</optgroup>";
  637.             echo ($list);exit;
  638.         }
  639.         echo '';exit;
  640.     }
  641.     
  642.     /**
  643.      * @Route("/getExp", name="_get_exp")
  644.      */
  645.     public function getExp(Request $request) {
  646.         
  647.         $sql2 " ";
  648.         if (isset($_POST["myregion"])){
  649.             $mycommune $_POST["myregion"];
  650.             
  651.             if ($mycommune != 0)
  652.                 $sql2 " AND lc.id = $mycommune ";
  653.         }
  654.         
  655.         $list "";
  656.         $list "<optgroup label='Sélection Exploitant(s)'>";
  657.         if (isset($_POST["commune"])) {
  658.             $region implode($_POST["commune"], ",");
  659.             
  660.            
  661.             $sql "";
  662.             if (isset($region) && $region != "null" ) {
  663.                 $sql "AND exp.id NOT IN (".$region.")";
  664.             }
  665.             
  666.             $rsm = new ResultSetMapping();
  667.             $rsm->addScalarResult('id''id');
  668.             $rsm->addScalarResult('nom''nom');
  669.             $rsm->addScalarResult('prenom''prenom');
  670.             $rsm->addScalarResult('commune''commune');
  671.             $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  distinct exp.id as id, nom, prenom, lc.value as commune from exploitant exp
  672.                  LEFT JOIN exploitant_identite expi ON expi.exploitant_id = exp.id 
  673.                 LEFT JOIN list_commune lc ON lc.id = expi.commune_exp_id
  674.                     where 1=1 $sql $sql2
  675.                     ORDER BY nom asc "$rsm);
  676.             $secteurs $query->getResult();
  677.             
  678.             
  679.            foreach ($secteurs as $secteur) {
  680.                $list .= "<option value=".$secteur["id"].">#".$secteur["id"]." - ".$secteur["nom"]."  ".$secteur["prenom"]." - ".$secteur["commune"]."</option>";
  681.            }
  682.             echo ($list);exit;
  683.         } else {
  684.             $rsm = new ResultSetMapping();
  685.             $rsm->addScalarResult('id''id');
  686.             $rsm->addScalarResult('nom''nom');
  687.             $rsm->addScalarResult('prenom''prenom');
  688.             $rsm->addScalarResult('commune''commune');
  689.             $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  distinct exp.id as id, nom, prenom, lc.value as commune from exploitant exp
  690.                  LEFT JOIN exploitant_identite expi ON expi.exploitant_id = exp.id 
  691.                  LEFT JOIN list_commune lc ON lc.id = expi.commune_exp_id
  692.                     where 1=1 $sql2
  693.                     ORDER BY nom asc "$rsm);
  694.             $secteurs $query->getResult();
  695.             
  696.            foreach ($secteurs as $secteur) {
  697.                $list .= "<option value=".$secteur["id"].">#".$secteur["id"]." - ".$secteur["nom"]."  ".$secteur["prenom"]." - ".$secteur["commune"]."</option>";
  698.            }
  699.             echo ($list);exit;
  700.         }
  701.         echo '';exit;
  702.     }
  703.     
  704.     /**
  705.      * @Route("/addCommune", name="_add_commune")
  706.      */
  707.     public function addCommune(Request $request) {
  708.          //var_dump($_POST);exit;
  709.         if (isset($_POST["region"])) {
  710.             $region implode($_POST["region"], ",");
  711.             
  712.             $rsm = new ResultSetMapping();
  713.             $rsm->addScalarResult('id''id');
  714.             $rsm->addScalarResult('value''value');
  715.             $rsm->addScalarResult('region''region');
  716.             $rsm->addScalarResult('district''district');
  717.             $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  com.id, com.value, dis.value as district, reg.value as region from list_commune com 
  718.                     LEFT JOIN list_district dis ON com.district_id = dis.id
  719.                     LEFT JOIN list_region reg ON dis.region_id = reg.id
  720.                     where com.id IN (".$region.") 
  721.                     ORDER BY value asc "$rsm);
  722.             $secteurs $query->getResult();
  723.             
  724.             $list "";
  725.            foreach ($secteurs as $secteur) {
  726.                $list .= "<option value=".$secteur["id"].">".$secteur["region"]." > ".$secteur["district"]." > ".$secteur["value"]."</option>";
  727.            }
  728.             echo ($list);exit;
  729.         }
  730.     }
  731.     
  732.     /**
  733.      * @Route("/addExp", name="_add_exp")
  734.      */
  735.     public function addExp(Request $request) {
  736.          //var_dump($_POST);exit;
  737.         
  738.         $sql2 " ";
  739.         if (isset($_POST["myregion"])){
  740.             $mycommune $_POST["myregion"];
  741.             
  742.             if ($mycommune != 0)
  743.                 $sql2 " AND lc.id = $mycommune ";
  744.         }
  745.         if (isset($_POST["region"])) {
  746.             $region implode($_POST["region"], ",");
  747.             
  748.             $rsm = new ResultSetMapping();
  749.             $rsm->addScalarResult('id''id');
  750.             $rsm->addScalarResult('nom''nom');
  751.             $rsm->addScalarResult('prenom''prenom');
  752.             $rsm->addScalarResult('commune''commune');
  753.             $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  distinct exp.id as id, nom, prenom, lc.value as commune from exploitant exp
  754.                  LEFT JOIN exploitant_identite expi ON expi.exploitant_id = exp.id 
  755.                  LEFT JOIN list_commune lc ON lc.id = expi.commune_exp_id
  756.                     where exp.id IN (".$region.")  $sql2
  757.                     ORDER BY nom asc "$rsm);
  758.             $secteurs $query->getResult();
  759.             
  760.             $list "";
  761.            foreach ($secteurs as $secteur) {
  762.                $list .= "<option value=".$secteur["id"].">#".$secteur["id"]." - ".$secteur["nom"]."  ".$secteur["prenom"]." - ".$secteur["commune"]."</option>";
  763.            }
  764.             echo ($list);exit;
  765.         }
  766.     }
  767.     
  768.     /**
  769.      * @Route("/addCommuneAll", name="_all_commune")
  770.      */
  771.     public function addCommuneAll(Request $request) {
  772.          //var_dump($_POST);exit;
  773.         if (isset($_POST["region"])) {
  774.             $region $_POST["region"];
  775.             $rsm = new ResultSetMapping();
  776.             $rsm->addScalarResult('id''id');
  777.             $rsm->addScalarResult('value''value');
  778.             $rsm->addScalarResult('region''region');
  779.             $rsm->addScalarResult('district''district');
  780.             $query $this->getDoctrine()->getManager()->createNativeQuery("SELECT  com.id, com.value, dis.value as district, reg.value as region from list_commune com 
  781.                     LEFT JOIN list_district dis ON com.district_id = dis.id
  782.                     LEFT JOIN list_region reg ON dis.region_id = reg.id
  783.                     where com.id IN (".$region.") 
  784.                     ORDER BY value asc "$rsm);
  785.             $secteurs $query->getResult();
  786.             $list "";
  787.            foreach ($secteurs as $secteur) {
  788.                $list .= "<option value=".$secteur["id"].">".$secteur["region"]." > ".$secteur["district"]." > ".$secteur["value"]."</option>";
  789.            }
  790.             echo ($list);exit;
  791.         }
  792.     }
  793.     
  794.     public function checkSecurity($id null$action false){
  795.         if(in_array('ROLE_ADMIN'$this->getUser()->getRoles())){
  796.             return true;        
  797.         }
  798.         if(in_array('ROLE_OPERATOR'$this->getUser()->getRoles())){
  799.              if ($id == null){
  800.                  return true;
  801.              } else {
  802.                 $user $this->getUser();
  803.                 $em $this->getDoctrine()->getManager();
  804.                 $listeExp $em->getRepository(UserExploitant::class)->findOneBy(array('userId'=>$user->getId(), "expId" => $id));
  805.                 
  806.                 if (isset($listeExp)){
  807.                     return true;
  808.                 } else {
  809.                     return false;
  810.                 }
  811.              }
  812.         }
  813.         
  814.         if(in_array('ROLE_REGIONAL'$this->getUser()->getRoles())){
  815.              return true;
  816.         }
  817.         
  818.         if(in_array('ROLE_USER'$this->getUser()->getRoles())){
  819.             if (!$action) {
  820.                 return false;
  821.             } else {
  822.                 if ($id == null){
  823.                     return false;
  824.                  } else {
  825.                     $user $this->getUser();
  826.                     $em $this->getDoctrine()->getManager();
  827.                     $listeExp $em->getRepository(UserExploitant::class)->findOneBy(array('userId'=>$user->getId(), "expId" => $id));
  828.                     if (isset($listeExp)){
  829.                         return true;
  830.                     } else {
  831.                         return false;
  832.                     }
  833.                  }
  834.             }
  835.              
  836.         }
  837.         
  838.     }
  839.     
  840.     /**
  841.      * @Route("{id}/stat2", name="_stat2")
  842.      */
  843.     public function stat2(Request $request$id)
  844.     {
  845.         $em $this->getDoctrine()->getManager();
  846.       
  847.         $farm $em->getRepository(Exploitant::class)->find($id);
  848.         $retour $this->checkSecurity($idtrue);
  849.           if (!$retour){
  850.               return $this->redirectToRoute("exp_crud");
  851.           }
  852.           
  853.           $tab $tabFinal3 $tabFinal $tabFinal2  $tabFinal4 $tabFinal5 $tabFinal6 = array();
  854.           
  855.           $sousCharges $em->getRepository(ListSousCharge::class)->findAll();
  856.           
  857.           $retourListeZero "[";
  858.           $retourListeAZero "[";
  859.           foreach ($sousCharges as $ssch){
  860.             if ($ssch->getTypeCharge()->getIsVegetal()) {
  861.                if ($retourListeZero == "["){
  862.                    $retourListeZero .= "0";
  863.                } else {
  864.                    $retourListeZero .= ",0";
  865.                }
  866.             } else {
  867.                 if ($retourListeAZero == "["){
  868.                    $retourListeAZero .= "0";
  869.                } else {
  870.                    $retourListeAZero .= ",0";
  871.                }
  872.             }
  873.         }
  874.         
  875.         $retourListeAZero .= "]";
  876.         $retourListeZero .= "]";
  877.                         
  878.           foreach ($farm->getExploitantIdentite() as $identite){
  879.               
  880.               $testNull true;
  881.               foreach ($identite->getExploitantSpeculation() as $key=>$speculation) {
  882.                   $tab2 = array();
  883.                   $qteProduit $nbVente $sommePrix $surface $chargeTot $produitTot $recette $depense$qteJeune $qteAdulte $qteAutre $perte $repro$mySomme $sommePrixJ $sommePrixA $sommePrixAu $nbVenteAu $nbVenteA $nbVenteJ 0;
  884.                   $cle $key+1;
  885.                   $method_set "getSpecialite".$cle;
  886.                   $vegouani $identite->$method_set();
  887.                   
  888.                   
  889.                   
  890.                   if (isset($vegouani)) {
  891.                       
  892.                     if ($vegouani->getIsVegetal()) {
  893.                         foreach ($sousCharges as $ssch){
  894.                             if ($ssch->getTypeCharge()->getIsVegetal()) {
  895.                               $idch $ssch->getValue();
  896.                               $annee $identite->getAnnee();
  897.                               $tab2["".$idch.""] = 0;
  898.                             }
  899.                         }
  900.                         $tabFinal[$vegouani->getId()]["nom"] = $vegouani->getValue();
  901.                         $tabFinal2[$vegouani->getId()]["nom"] = $vegouani->getValue();
  902.                         $tabFinal3[$vegouani->getId()]["nom"] = $vegouani->getValue();
  903.                         if ($speculation->getPvSurface() != null) {
  904.                             $surface += (float) $speculation->getPvSurface();
  905.                         }
  906.                         
  907.                        
  908.                         foreach ($speculation->getCharge() as $charge) {
  909.                             $so 0;
  910.                             for ($w=1;$w<13;$w++){
  911.                                 $method_set "getMois".$w;
  912.                                 $so += (float) $charge->$method_set();
  913.                             }
  914.                             $chargeTot += (float) $so;
  915.                             
  916.                             if ($charge->getTypeDepense()->getId() == ){
  917.                                 $depense += (float) $so;
  918.                             }
  919.                             
  920.                             if ($charge->getTypeCharge()->getIsVegetal()) {
  921.                                 $tab2[$charge->getSousCharge()->getValue()] += (float) $so;
  922.                                 if ($so != 0){
  923.                                     $testNull false;
  924.                                 }
  925.                             }
  926.                         }
  927.                         
  928.                        
  929.                         foreach ($speculation->getProduit() as $charge) {
  930.                             $mySomme $charge->getSomme();
  931.                             $produitTot += (float) $mySomme;
  932.                             $qteProduit += (float) $charge->getQuantite();
  933.                             if ($charge->getProduction()->getId() == 1){
  934.                                 $recette += (float) $mySomme;
  935.                                 $sommePrix += (float) $charge->getPu();
  936.                                 $nbVente++;
  937.                             }
  938.                             
  939.                         }
  940.                         
  941.                         
  942.                         $tabFinal[$vegouani->getId()]["tab2"][$identite->getAnnee()] = $tab2
  943.                         
  944.                         
  945.                         $tabFinal2[$vegouani->getId()]["tab1"]["annee"][] = $identite->getAnnee();
  946.                         $tabFinal2[$vegouani->getId()]["tab1"]["surface"][] = $surface;
  947.                         $tabFinal2[$vegouani->getId()]["tab1"]["charge"][] = number_format($chargeTot0','' ');
  948.                         $tabFinal2[$vegouani->getId()]["tab1"]["produit"][] = number_format($produitTot0','' ');
  949.                         $tabFinal2[$vegouani->getId()]["tab1"]["depense"][] = number_format($depense0','' ');
  950.                         $tabFinal2[$vegouani->getId()]["tab1"]["recette"][] = number_format($recette0','' ');
  951.                         $tabFinal2[$vegouani->getId()]["tab1"]["marge"][] = number_format($produitTot $chargeTot0','' ');
  952.                         $tabFinal2[$vegouani->getId()]["tab1"]["benefice"][] = number_format($recette $depense0','' ');
  953.                         
  954.                         if ($surface == && $chargeTot == && $produitTot == && $depense == && $recette == 0){
  955.                             if (!isset($tabFinal2[$vegouani->getId()]["affiche"]) || $tabFinal2[$vegouani->getId()]["affiche"]!=true)
  956.                             $tabFinal2[$vegouani->getId()]["affiche"]= false;
  957.                         } else {
  958.                             $tabFinal2[$vegouani->getId()]["affiche"] = true;
  959.                         }
  960.                         
  961.                         $tabFinal3[$vegouani->getId()]["tab3"]["annee"][] = $identite->getAnnee();
  962.                         if ($surface != 0) {
  963.                             $tabFinal3[$vegouani->getId()]["tab3"]["marge"][] = number_format(($produitTot $chargeTot)/$surface0','' ');
  964.                             $tabFinal3[$vegouani->getId()]["tab3"]["charge"][] = number_format($chargeTot/$surface0','' ');
  965.                             $tabFinal3[$vegouani->getId()]["tab3"]["produit"][] = number_format($produitTot/$surface0','' ');
  966.                             $tabFinal3[$vegouani->getId()]["tab3"]["recette"][] = number_format($recette/$surface0','' ');
  967.                             $tabFinal3[$vegouani->getId()]["tab3"]["benefice"][] = number_format(($recette $depense)/$surface0','' ');
  968.                             $tabFinal3[$vegouani->getId()]["tab3"]["depense"][] = number_format($depense/$surface0','' ');
  969.                             $tabFinal3[$vegouani->getId()]["tab3"]["coutprod"][] = ($qteProduit != 0)?number_format($chargeTot/$qteProduit0','' '):0;
  970.                             $tabFinal3[$vegouani->getId()]["tab3"]["prixVente"][] = ($nbVente !=0)?number_format($sommePrix/$nbVente0','' '):0;
  971.                             $tabFinal3[$vegouani->getId()]["tab3"]["rendement"][] = number_format($qteProduit/$surface0','' ');
  972.                         } else {
  973.                             $tabFinal3[$vegouani->getId()]["tab3"]["marge"][] = 0;
  974.                             $tabFinal3[$vegouani->getId()]["tab3"]["charge"][] = 0;
  975.                             $tabFinal3[$vegouani->getId()]["tab3"]["produit"][] = 0;
  976.                             $tabFinal3[$vegouani->getId()]["tab3"]["recette"][] = 0;
  977.                             $tabFinal3[$vegouani->getId()]["tab3"]["benefice"][] = 0;
  978.                             $tabFinal3[$vegouani->getId()]["tab3"]["depense"][] = 0;
  979.                             $tabFinal3[$vegouani->getId()]["tab3"]["rendement"][] = 0;
  980.                             $tabFinal3[$vegouani->getId()]["tab3"]["coutprod"][] = ($qteProduit != 0)?number_format($chargeTot/$qteProduit0','' '):0;
  981.                             $tabFinal3[$vegouani->getId()]["tab3"]["prixVente"][] = ($nbVente !=0)?number_format($sommePrix/$nbVente0','' '):0;
  982.                         }
  983.                         if ($surface == && $chargeTot == && $produitTot == && $depense == && $recette == 0){
  984.                             if (!isset($tabFinal3[$vegouani->getId()]["affiche"]) || $tabFinal3[$vegouani->getId()]["affiche"]!=true)
  985.                             $tabFinal3[$vegouani->getId()]["affiche"]= false;
  986.                         } else {
  987.                             $tabFinal3[$vegouani->getId()]["affiche"] = true;
  988.                         }
  989.                         
  990.                     }
  991.                     
  992.                     if (!$vegouani->getIsVegetal()) {
  993.                         foreach ($sousCharges as $ssch){
  994.                             if (!$ssch->getTypeCharge()->getIsVegetal()) {
  995.                               $idch $ssch->getValue();
  996.                               $annee $identite->getAnnee();
  997.                               $tab2["".$idch.""] = 0;
  998.                             }
  999.                         }
  1000.                         $tabFinal4[$vegouani->getId()]["nom"] = $vegouani->getValue();
  1001.                         $tabFinal5[$vegouani->getId()]["nom"] = $vegouani->getValue();
  1002.                         $tabFinal6[$vegouani->getId()]["nom"] = $vegouani->getValue();
  1003.                         if ($speculation->getPaCheptel() != null) {
  1004.                             $repro += (float) $speculation->getPaCheptel();
  1005.                         }
  1006.                         
  1007.                        
  1008.                         foreach ($speculation->getCharge() as $charge) {
  1009.                             $so 0;
  1010.                             for ($w=1;$w<13;$w++){
  1011.                                 $method_set "getMois".$w;
  1012.                                 $so += (float) $charge->$method_set();
  1013.                             }
  1014.                             $chargeTot += (float) $so;
  1015.                             
  1016.                             if ($charge->getTypeDepense()->getId() == ){
  1017.                                 $depense += (float) $so;
  1018.                             }
  1019.                             
  1020.                             if (!$charge->getTypeCharge()->getIsVegetal()) {
  1021.                                $tab2[$charge->getSousCharge()->getValue()] += (float) $so;
  1022.                                 if ($so != 0){
  1023.                                     $testNull false;
  1024.                                 }
  1025.                             }
  1026.                         }
  1027.                         
  1028.                        
  1029.                         foreach ($speculation->getProduit() as $charge) {
  1030.                             $mySomme $charge->getSomme();
  1031.                             $produitTot += (float) $mySomme;
  1032.                             
  1033.                             if ($charge->getProduction()->getId() < 12){
  1034.                             $qteProduit += (float) $charge->getQuantite();
  1035.                             }
  1036.                             if ($charge->getProduction()->getId() == 5){
  1037.                                 $recette += (float) $charge->getSomme();
  1038.                                 $qteJeune += (float) $charge->getQuantite();
  1039.                                 $sommePrix += (float) $charge->getPu();
  1040.                                 $nbVente++;
  1041.                                 $sommePrixJ += (float) $charge->getPu();
  1042.                                 $nbVenteJ++;
  1043.                             }
  1044.                             if ($charge->getProduction()->getId() == 9){
  1045.                                 $recette += (float) $charge->getSomme();
  1046.                                 $qteAdulte += (float) $charge->getQuantite();
  1047.                                 $sommePrixA += (float) $charge->getPu();
  1048.                                 $nbVenteA++;
  1049.                             }
  1050.                             if ($charge->getProduction()->getId() == 13){
  1051.                                 $recette += (float) $charge->getSomme();
  1052.                                 $qteAutre += (float) $charge->getQuantite();
  1053.                                 $sommePrixAu += (float) $charge->getPu();
  1054.                                 $nbVenteAu++;
  1055.                             }
  1056.                             if ($charge->getProduction()->getId() == || $charge->getProduction()->getId() == 10 || $charge->getProduction()->getId() == 14){
  1057.                                 $perte += (float) $charge->getQuantite();
  1058.                             }
  1059.                         }
  1060.                         
  1061.                         $tabFinal5[$vegouani->getId()]["tab2"][$identite->getAnnee()] = $tab2
  1062.                         
  1063.                         //var_dump($tabFinal5);exit;
  1064.                         $tabFinal4[$vegouani->getId()]["tab1"]["annee"][] = $identite->getAnnee();
  1065.                         $tabFinal4[$vegouani->getId()]["tab1"]["repro"][] = $repro;
  1066.                         $tabFinal4[$vegouani->getId()]["tab1"]["qteProduit"][] = $qteProduit;
  1067.                         $tabFinal4[$vegouani->getId()]["tab1"]["jeuneVendu"][] = $qteJeune;
  1068.                         $tabFinal4[$vegouani->getId()]["tab1"]["adulteVendu"][] = $qteAdulte;
  1069.                         $tabFinal4[$vegouani->getId()]["tab1"]["autreVendu"][] = $qteAutre;
  1070.                         $tabFinal4[$vegouani->getId()]["tab1"]["perte"][] = ($qteProduit != 0)?$perte/$qteProduit:0;
  1071.                         $tabFinal4[$vegouani->getId()]["tab1"]["chargeTot"][] = number_format($chargeTot0','' ');
  1072.                         $tabFinal4[$vegouani->getId()]["tab1"]["produitTot"][] = number_format($produitTot0','' ');
  1073.                         $tabFinal4[$vegouani->getId()]["tab1"]["marge"][] = number_format($produitTot $chargeTot0','' ');
  1074.                         $tabFinal4[$vegouani->getId()]["tab1"]["depense"][] = number_format($depense0','' ');
  1075.                         $tabFinal4[$vegouani->getId()]["tab1"]["recette"][] = number_format($recette0','' ');
  1076.                         $tabFinal4[$vegouani->getId()]["tab1"]["benefice"][] = number_format($recette $depense0','' ');
  1077.                         
  1078.                         
  1079.                         if ($repro == && $qteProduit == && $qteJeune == && $qteAdulte == && $qteAutre == && $chargeTot == && $produitTot == 0){
  1080.                             if (!isset($tabFinal4[$vegouani->getId()]["affiche"]) || $tabFinal4[$vegouani->getId()]["affiche"]!=true)
  1081.                             $tabFinal4[$vegouani->getId()]["affiche"]= false;
  1082.                         } else {
  1083.                             $tabFinal4[$vegouani->getId()]["affiche"] = true;
  1084.                         }
  1085.                         
  1086.                         $tabFinal6[$vegouani->getId()]["tab7"]["annee"][] = $identite->getAnnee();
  1087.                         $tabFinal6[$vegouani->getId()]["tab7"]["charge"][] = ($repro != 0)?number_format($chargeTot/$repro0','' '):0;
  1088.                         $tabFinal6[$vegouani->getId()]["tab7"]["produit"][] = ($repro != 0)?number_format($produitTot/$repro0','' '):0;
  1089.                         $tabFinal6[$vegouani->getId()]["tab7"]["marge"][] = ($repro != 0)?number_format(($produitTot $chargeTot)/$repro0','' '):0;
  1090.                         $tabFinal6[$vegouani->getId()]["tab7"]["depense"][] = ($repro != 0)?number_format($depense/$repro0','' '):0;
  1091.                         $tabFinal6[$vegouani->getId()]["tab7"]["recette"][] = ($repro != 0)?number_format($mySomme/$repro0','' '):0;
  1092.                         $tabFinal6[$vegouani->getId()]["tab7"]["benefice"][] = ($repro != 0)?number_format(($mySomme $depense)/$repro0','' '):0;
  1093.                         $tabFinal6[$vegouani->getId()]["tab7"]["venteJ"][] = ($nbVenteJ !=0)?number_format($sommePrixJ/$nbVenteJ0','' '):0;
  1094.                         $tabFinal6[$vegouani->getId()]["tab7"]["venteA"][] = ($nbVenteA !=0)?number_format($sommePrixA/$nbVenteA0','' '):0;
  1095.                         $tabFinal6[$vegouani->getId()]["tab7"]["venteAu"][] = ($nbVenteAu !=0)?number_format($sommePrixAu/$nbVenteAu0','' '):0;
  1096.                         
  1097.                         if ($repro == && $chargeTot == && $depense == && $mySomme == && $chargeTot == && $produitTot == 0){
  1098.                             if (!isset($tabFinal6[$vegouani->getId()]["affiche"]) || $tabFinal6[$vegouani->getId()]["affiche"]!=true)
  1099.                             $tabFinal6[$vegouani->getId()]["affiche"]= false;
  1100.                         } else {
  1101.                             $tabFinal6[$vegouani->getId()]["affiche"] = true;
  1102.                         }
  1103.                     }     
  1104.                   }
  1105.               }
  1106.           }
  1107.           
  1108.        
  1109.           return $this->render('exp/stat2.html.twig', [
  1110.             "farm" => $farm,
  1111.             "tabFinal2" => $tabFinal2
  1112.             "tabFinal" => $tabFinal
  1113.             "tabFinal3" => $tabFinal3
  1114.             "tabFinal4" => $tabFinal4
  1115.             "tabFinal5" => $tabFinal5
  1116.             "tabFinal6" => $tabFinal6
  1117.             "retourListeAZero" => $retourListeAZero
  1118.             "retourListeZero" => $retourListeZero
  1119.           ]);
  1120.     }
  1121.     
  1122.     /**
  1123.      * @Route("{id}/stat", name="_stat")
  1124.      */
  1125.     public function stat(Request $request$id)
  1126.     {
  1127.         $em $this->getDoctrine()->getManager();
  1128.       
  1129.         $farm $em->getRepository(Exploitant::class)->find($id);
  1130.         $retour $this->checkSecurity($idtrue);
  1131.           if (!$retour){
  1132.               return $this->redirectToRoute("exp_crud");
  1133.           }
  1134.           
  1135.           $tab $tab2 $tab3 $tab4 $tab5 $tab6 $tab7 = array();
  1136.           
  1137.           $sousCharges $em->getRepository(ListSousCharge::class)->findAll();
  1138.           
  1139.           foreach ($farm->getExploitantIdentite() as $identite){
  1140.               $surface $chargeTotE $produitTotE $recetteE $depenseE $qteProduitA $qteProduitE $chargeTotA $produitTotA $recetteA $depenseA $qteJeune $qteAdulte $qteAutre $perte $repro =0;
  1141.               //$tab2[$identite->getAnnee()] = 0;
  1142.               
  1143.               foreach ($sousCharges as $ssch){
  1144.                   if ($ssch->getTypeCharge()->getIsVegetal()) {
  1145.                     $idch $ssch->getValue();
  1146.                     $annee $identite->getAnnee();
  1147.                     $tab2["".$annee.""]["".$idch.""] = 0;
  1148.                   }
  1149.                   
  1150.               }
  1151.               
  1152.               $qteProduit $nbVente $sommePrix $qteProduitJ $nbVenteJ $sommePrixJ $qteProduitA $nbVenteA $sommePrixA$qteProduitAu $nbVenteAu $sommePrixAu0;
  1153.                $testNull true;
  1154.               foreach ($identite->getExploitantSpeculation() as $key=>$speculation) {
  1155.                   $cle $key+1;
  1156.                   $method_set "getSpecialite".$cle;
  1157.                   $vegouani $identite->$method_set();
  1158.                   
  1159.                   
  1160.                   if (isset($vegouani)) {
  1161.                     if ($vegouani->getIsVegetal()) {
  1162.                         if ($speculation->getPvSurface() != null) {
  1163.                             $surface += (float) $speculation->getPvSurface();
  1164.                         }
  1165.                         
  1166.                         //charges
  1167.                        
  1168.                         foreach ($speculation->getCharge() as $charge) {
  1169.                             
  1170.                             $so 0;
  1171.                             for ($w=1;$w<13;$w++){
  1172.                                 $method_set "getMois".$w;
  1173.                                 $so += (float) $charge->$method_set();
  1174.                             }
  1175.                             $chargeTotA += (float) $so;
  1176.                             
  1177.                             if ($charge->getTypeDepense()->getId() == ){
  1178.                                 $depenseA += (float) $so;
  1179.                             }
  1180.                             
  1181.                             if ($charge->getTypeCharge()->getIsVegetal()) {
  1182.                                 $tab2[$identite->getAnnee()][$charge->getSousCharge()->getValue()] += (float) $so;
  1183.                                 if ($so != 0){
  1184.                                     $testNull false;
  1185.                                 }
  1186.                             }
  1187.                         }
  1188.                         
  1189.                        
  1190.                         
  1191.                         foreach ($speculation->getProduit() as $charge) {
  1192.                             $mySomme $charge->getSomme();
  1193.                             $produitTotA += (float) $mySomme;
  1194.                             $qteProduitA += (float) $charge->getQuantite();
  1195.                             if ($charge->getProduction()->getId() == 1){
  1196.                                 $recetteA += (float) $mySomme;
  1197.                                 $sommePrix += (float) $charge->getPu();
  1198.                                 $nbVente++;
  1199.                             }
  1200.                         }
  1201.                     }
  1202.                     
  1203.                     if (!$vegouani->getIsVegetal()) {
  1204.                         foreach ($sousCharges as $ssch){
  1205.                             if (!$ssch->getTypeCharge()->getIsVegetal()) {
  1206.                               $idch $ssch->getValue();
  1207.                               $annee $identite->getAnnee();
  1208.                               $tab6["".$annee.""]["".$idch.""] = 0;
  1209.                             }
  1210.                         }
  1211.                         $tabFinal4[$vegouani->getId()]["nom"] = $vegouani->getValue();
  1212.                         $tabFinal5[$vegouani->getId()]["nom"] = $vegouani->getValue();
  1213.                         if ($speculation->getPaCheptel() != null) {
  1214.                             $repro += (float) $speculation->getPaCheptel();
  1215.                         }
  1216.                         
  1217.                        
  1218.                         foreach ($speculation->getCharge() as $charge) {
  1219.                             $so 0;
  1220.                             for ($w=1;$w<13;$w++){
  1221.                                 $method_set "getMois".$w;
  1222.                                 $so += (float) $charge->$method_set();
  1223.                             }
  1224.                             $chargeTotE += (float) $so;
  1225.                             
  1226.                             if ($charge->getTypeDepense()->getId() == ){
  1227.                                 $depenseE += (float) $so;
  1228.                             }
  1229.                             
  1230.                             if (!$charge->getTypeCharge()->getIsVegetal()) {
  1231.                                $tab6[$identite->getAnnee()][$charge->getSousCharge()->getValue()] += (float) $so;
  1232.                                 if ($so != 0){
  1233.                                     $testNull false;
  1234.                                 }
  1235.                             }
  1236.                         }
  1237.                         
  1238.                        
  1239.                         foreach ($speculation->getProduit() as $charge) {
  1240.                             $mySomme $charge->getSomme();
  1241.                             $produitTotE += (float) $mySomme;
  1242.                             $qteProduitE += (float) $charge->getQuantite();
  1243.                             if ($charge->getProduction()->getId() == 5){
  1244.                                 $recetteE += (float) $mySomme;
  1245.                                 $qteJeune += (float) $charge->getQuantite();
  1246.                                 $sommePrixJ += (float) $charge->getPu();
  1247.                                 $nbVenteJ++;
  1248.                             }
  1249.                             if ($charge->getProduction()->getId() == 9){
  1250.                                 $qteAdulte += (float) $charge->getQuantite();
  1251.                                 $recetteE += (float) $mySomme;
  1252.                                 $sommePrixA+= (float) $charge->getPu();
  1253.                                 $nbVenteA++;
  1254.                             }
  1255.                             if ($charge->getProduction()->getId() == 13){
  1256.                                 $qteAutre += (float) $charge->getQuantite();
  1257.                                 $recetteE += (float) $mySomme;
  1258.                                 $sommePrixAu += (float) $charge->getPu();
  1259.                                 $nbVenteAu++;
  1260.                             }
  1261.                             if ($charge->getProduction()->getId() == || $charge->getProduction()->getId() == 10 || $charge->getProduction()->getId() == 14){
  1262.                                 $perte += (float) $charge->getQuantite();
  1263.                             }
  1264.                         }
  1265.                            
  1266.                     }   
  1267.                     
  1268.                   }
  1269.               }
  1270.                if ($testNull) {
  1271.                     unset($tab2[$identite->getAnnee()]);
  1272.                     unset($tab6[$identite->getAnnee()]);
  1273.                 }
  1274.               $tab[$identite->getAnnee()]["surface"] = $surface
  1275.               $tab[$identite->getAnnee()]["charge"] = $chargeTotA
  1276.               $tab[$identite->getAnnee()]["produit"] = $produitTotA
  1277.               $tab[$identite->getAnnee()]["marge"] = $produitTotA $chargeTotA
  1278.               $tab[$identite->getAnnee()]["depense"] = $depenseA
  1279.               $tab[$identite->getAnnee()]["recette"] = $recetteA
  1280.               $tab[$identite->getAnnee()]["benefice"] = $recetteA $depenseA
  1281.               $tab[$identite->getAnnee()]["qteProduit"] = $qteProduitA;
  1282.               $tab[$identite->getAnnee()]["prixVente"] = ($nbVente !=0)?$sommePrix/$nbVente:0;
  1283.               $tab[$identite->getAnnee()]["nbVente"] = $nbVente;
  1284.               $tab[$identite->getAnnee()]["sommePrix"] = $sommePrix;
  1285.               
  1286.             $tab4[$identite->getAnnee()]["annee"] = $identite->getAnnee();
  1287.             $tab4[$identite->getAnnee()]["repro"] = $repro;
  1288.             $tab4[$identite->getAnnee()]["qteProduit"] = $qteProduitE;
  1289.             $tab4[$identite->getAnnee()]["jeuneVendu"] = $qteJeune;
  1290.             $tab4[$identite->getAnnee()]["adulteVendu"] = $qteAdulte;
  1291.             $tab4[$identite->getAnnee()]["autreVendu"] = $qteAutre;
  1292.             $tab4[$identite->getAnnee()]["perte"] = ($qteProduitE != 0)?$perte/$qteProduitE:0;
  1293.             $tab4[$identite->getAnnee()]["chargeTot"] = $chargeTotE;
  1294.             $tab4[$identite->getAnnee()]["produitTot"] = $produitTotE;
  1295.             $tab4[$identite->getAnnee()]["marge"] = $produitTotE $chargeTotE;
  1296.             $tab4[$identite->getAnnee()]["depense"] = $depenseE;
  1297.             $tab4[$identite->getAnnee()]["recette"] = $recetteE;
  1298.             $tab4[$identite->getAnnee()]["benefice"] = $recetteE $depenseE;
  1299.             $tab4[$identite->getAnnee()]["nbVenteJ"] = $nbVenteJ;
  1300.             $tab4[$identite->getAnnee()]["sommePrixJ"] = $sommePrixJ;
  1301.             $tab4[$identite->getAnnee()]["nbVenteA"] = $nbVenteA;
  1302.             $tab4[$identite->getAnnee()]["sommePrixA"] = $sommePrixA;
  1303.             $tab4[$identite->getAnnee()]["nbVenteAu"] = $nbVenteAu;
  1304.             $tab4[$identite->getAnnee()]["sommePrixAu"] = $sommePrixAu;
  1305.           }
  1306.           
  1307.           $tab1 = array();
  1308.           foreach ($tab as $key=>$t) {
  1309.               $tab1["annee"][] = $key;
  1310.               $tab1["surface"][] = $t["surface"];
  1311.               $tab1["charge"][] = number_format($t["charge"], 0','' ');
  1312.               $tab1["produit"][] = number_format($t["produit"], 0','' ');
  1313.               $tab1["depense"][] = number_format($t["depense"], 0','' ');
  1314.               $tab1["recette"][] = number_format($t["recette"], 0','' ');
  1315.               $tab1["marge"][] = number_format($t["marge"], 0','' ');
  1316.               $tab1["benefice"][] = number_format($t["benefice"], 0','' ');
  1317.               
  1318.               $tab3["annee"][] = $key;
  1319.               if ($t["surface"] != 0) {
  1320.                   $tab3["marge"][] = number_format($t["marge"]/$t["surface"], 0','' ');
  1321.                   $tab3["charge"][] = number_format($t["charge"]/$t["surface"], 0','' ');
  1322.                   $tab3["produit"][] = number_format($t["produit"]/$t["surface"], 0','' ');
  1323.                   $tab3["recette"][] = number_format($t["recette"]/$t["surface"], 0','' ');
  1324.                   $tab3["benefice"][] = number_format($t["benefice"]/$t["surface"], 0','' ');
  1325.                   $tab3["depense"][] = number_format($t["depense"]/$t["surface"], 0','' ');
  1326.                   $tab3["coutprod"][] = ($t["qteProduit"] != 0)?number_format($t["charge"]/$t["qteProduit"], 0','' '):0;
  1327.                   $tab3["prixVente"][] = ($t["nbVente"] !=0)?number_format($t["sommePrix"]/$t["nbVente"], 0','' '):0;
  1328.                   $tab3["rendement"][] = number_format($t["qteProduit"]/$t["surface"], 0','' ');
  1329.               } else {
  1330.                   $tab3["marge"][] = 0;
  1331.                   $tab3["charge"][] = 0;
  1332.                   $tab3["produit"][] = 0;
  1333.                   $tab3["recette"][] = 0;
  1334.                   $tab3["benefice"][] = 0;
  1335.                   $tab3["depense"][] = 0;
  1336.                   $tab3["rendement"][] = 0;
  1337.                   $tab3["coutprod"][] = ($t["qteProduit"] != 0)?$t["charge"]/$t["qteProduit"]:0;
  1338.                   $tab3["prixVente"][] =  ($t["nbVente"] !=0)?$t["sommePrix"]/$t["nbVente"]:0;
  1339.               }
  1340.           }
  1341.           
  1342.           foreach ($tab4 as $key=>$t) {
  1343.               $tab5["annee"][] = $key;
  1344.               $tab5["repro"][] = $t["repro"];
  1345.               $tab5["qteProduit"][] = $t["qteProduit"];
  1346.               $tab5["jeuneVendu"][] = $t["jeuneVendu"];
  1347.               $tab5["adulteVendu"][] = $t["adulteVendu"];
  1348.               $tab5["autreVendu"][] = $t["autreVendu"];
  1349.               $tab5["perte"][] = $t["perte"];
  1350.               $tab5["chargeTot"][] = number_format($t["chargeTot"], 0','' ');
  1351.               $tab5["produitTot"][] = number_format($t["produitTot"], 0','' ');
  1352.               $tab5["marge"][] = number_format($t["marge"], 0','' ');
  1353.               $tab5["depense"][] = number_format($t["depense"], 0','' ');
  1354.               $tab5["recette"][] = number_format($t["recette"], 0','' ');
  1355.               $tab5["benefice"][] = number_format($t["benefice"], 0','' ');
  1356.               
  1357.               $tab7["annee"][] = $key;
  1358.               $tab7["charge"][] = ($t["repro"] != 0)?number_format($t["chargeTot"]/$t["repro"], 0','' '):0;
  1359.               $tab7["produit"][] = ($t["repro"] != 0)?number_format($t["produitTot"]/$t["repro"], 0','' '):0;
  1360.               $tab7["marge"][] = ($t["repro"] != 0)?number_format($t["marge"]/$t["repro"], 0','' '):0;
  1361.               $tab7["depense"][] = ($t["repro"] != 0)?number_format($t["depense"]/$t["repro"], 0','' '):0;
  1362.               $tab7["recette"][] = ($t["repro"] != 0)?number_format($t["recette"]/$t["repro"], 0','' '):0;
  1363.               $tab7["benefice"][] = ($t["repro"] != 0)?number_format($t["benefice"]/$t["repro"], 0','' '):0;
  1364.               $tab7["venteJ"][] = ($t["nbVenteJ"] !=0)?number_format($t["sommePrixJ"]/$t["nbVenteJ"], 0','' '):0;
  1365.               $tab7["venteA"][] = ($t["nbVenteA"] !=0)?number_format($t["sommePrixA"]/$t["nbVenteA"], 0','' '):0;
  1366.               $tab7["venteAu"][] = ($t["nbVenteAu"] !=0)?number_format($t["sommePrixAu"]/$t["nbVenteAu"], 0','' '):0;
  1367.              
  1368.           }
  1369.       
  1370.           return $this->render('exp/stat.html.twig', [
  1371.             "farm" => $farm,
  1372.             "tab1" => $tab1
  1373.             "tab2" => $tab2
  1374.             "tab3" => $tab3
  1375.             "tab5" => $tab5
  1376.             "tab6" => $tab6
  1377.             "tab7" => $tab7
  1378.           ]);
  1379.     }
  1380. }