<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Exploitant;
use App\Entity\ExploitantIdentite;
use App\Entity\ExploitantPratique;
use App\Entity\ExploitantSpeculation;
use App\Entity\ListRegion;
use App\Entity\ListDistrict;
use App\Entity\ListCommune;
use App\Entity\ListTypeCharge;
use App\Entity\ListSousCharge;
use App\Entity\ListTypeDepense;
use App\Entity\ListTypeProduit;
use App\Entity\ListProduit;
use App\Entity\ListUnite;
use App\Entity\Charge;
use App\Entity\ListMois;
use App\Form\ExploitantNewType;
use App\Form\ExploitantIdentiteNewType;
use App\Form\ExploitantPratiqueNewType;
use App\Form\ExploitantEditType;
use Doctrine\ORM\Query\ResultSetMapping;
use App\Entity\Produit;
use App\Entity\ListProduction;
use App\Entity\UserExploitant;
/**
* @Route("/dashboard/exploitant/", name="exp")
*/
class ExploitantController extends AbstractController
{
/**
* @Route("{id}/details", name="_details")
*/
public function details(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$new_farm_infos = new ExploitantIdentite();
$form = NULL;
$farm = $em->getRepository(Exploitant::class)->find($id);
$retour = $this->checkSecurity($id, true);
if (!$retour){
return $this->redirectToRoute("exp_crud");
}
$last_farm_infos = $em->getRepository(ExploitantIdentite::class)->findOneBy(["exploitant" => $farm],["annee" => "DESC"]);
if($last_farm_infos){
$new_farm_infos = clone $last_farm_infos;
$new_farm_infos->setAnnee($last_farm_infos->getAnnee()+1);
}
if(!$farm) return $this->render('error/blank.html.twig', []);
$form = $this->createForm(ExploitantIdentiteNewType::class, $new_farm_infos);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$new_farm_infos->setExploitant($farm);
$em->persist($new_farm_infos);
$em->flush();
return $this->redirectToRoute("exp_details", ["id" => $farm->getId()]);
}
return $this->render('exp/details.html.twig', [
"farm" => $farm,
'form' => $form->createView(),
]);
}
/**
* @Route("crud", name="_crud")
*/
public function crud(Request $request)
{
$em = $this->getDoctrine()->getManager();
//$exps = $em->getRepository(Exploitant::class)->findAll(["name" => "ASC"]);
$regions = $em->getRepository(ListRegion::class)->findBy(["isProjet"=>true],["value" => "ASC"]);
$annee = date("Y");
$regionEnCours = "";
$sql = "";
$user = $this->getUser();
if(in_array('ROLE_USER', $this->getUser()->getRoles()) || in_array('ROLE_OPERATOR', $this->getUser()->getRoles())){
$listeExp = $em->getRepository(UserExploitant::class)->findBy(array('userId'=>$user->getId()));
$myLis = "";
$last_key = @end(array_keys($listeExp));
foreach ($listeExp as $key => $value) {
if ($key == $last_key) {
$myLis .= $value->getExpId();
} else {
$myLis .= $value->getExpId().",";
}
}
if ($myLis == "" ){
$sql .= " AND exp.id = 0 ";
} else {
$sql .= " AND exp.id IN ($myLis) ";
}
}
if( in_array('ROLE_REGIONAL', $this->getUser()->getRoles())){
$region = $user->getRegion();
if ($region == "" ){
$sql .= " AND expi.region_exp_id = 0 ";
} else {
$regions = explode('||', $region);
$list = '';
$i = 0;
foreach($regions as $r) {
if ($r != '' && $i == 0){
$list .= $r;
$i++;
} else {
if ($r != '') {
$list .= ','.$r;
$i++;
}
}
}
$sql .= " AND expi.region_exp_id IN ($list) ";
}
}
if (isset($_GET["myregion"]) && $_GET["myregion"] != "") {
$region = $_GET["myregion"];
$sql .= " AND expi.region_exp_id = $region ";
$regionEnCours = $region;
}
$rsm = new ResultSetMapping();
$rsm->addScalarResult('annee', 'annee');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT distinct(annee)
FROM exploitant_identite gri
WHERE 1=1 ORDER BY annee desc", $rsm);
$annees = $query->getResult();
$anneeEnCours = $annees[0]["annee"];
if (isset($_GET["myannee"]) && $_GET["myannee"] != "") {
$annee = $_GET["myannee"];
$anneeEnCours = $annee;
}
$sql .= " AND expi.annee = $anneeEnCours ";
$rsm = new ResultSetMapping();
$rsm->addScalarResult('nom', 'nom');
$rsm->addScalarResult('prenom', 'prenom');
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('annee', 'annee');
$rsm->addScalarResult('commune', 'commune');
$rsm->addScalarResult('eaf', 'eaf');
$rsm->addScalarResult('updated_at', 'updated_at');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT distinct(exp.id) as id, exp.nom as nom, exp.prenom as prenom,
max(expi.annee) as annee, (array_agg(lc.value ORDER BY lc.value ASC))[1] as commune,
(array_agg(eaf.value ORDER BY eaf.value ASC))[1] as eaf,
( GREATEST(exp.updated_at, expi.updated_at) ) as updated_at
FROM exploitant exp
JOIN exploitant_identite expi ON expi.exploitant_id = exp.id
LEFT JOIN list_commune lc ON lc.id = expi.commune_exp_id
LEFT JOIN list_eaf eaf ON eaf.id = expi.eaf_id
WHERE 1=1 $sql group by exp.id, expi.updated_at ORDER BY nom asc ", $rsm);
$exps = $query->getResult();
return $this->render('exp/crud.html.twig', [
"exps" => $exps,
"regions" => $regions,
"annees" => $annees,
"anneeEnCours" => $anneeEnCours,
"regionEnCours" => $regionEnCours,
"user" => $user,
]);
}
/**
* @Route("new", name="_new")
*/
public function new(Request $request)
{
$retour = $this->checkSecurity(null);
if (!$retour){
return $this->redirectToRoute("exp_crud");
}
$em = $this->getDoctrine()->getManager();
$charges = $em->getRepository(ListTypeCharge::class)->findBy(array(), ["value" => "ASC"]);
$sscharges = $em->getRepository(ListSousCharge::class)->findBy(array(), ["value" => "ASC"]);
$depenses = $em->getRepository(ListTypeDepense::class)->findBy(array(), ["value" => "ASC"]);
$produits = $em->getRepository(ListTypeProduit::class)->findBy(array(), ["value" => "ASC"]);
$unites = $em->getRepository(ListUnite::class)->findBy(array(), ["value" => "ASC"]);
$mois = $em->getRepository(ListMois::class)->findBy(array(), ["id" => "ASC"]);
$listeProduits = $em->getRepository(ListProduit::class)->findBy(["isVegetal" => false]);
$production = $em->getRepository(ListProduction::class)->findBy([]);
$listeIsVegetal = array();
foreach($listeProduits as $pr){
$listeIsVegetal[] = $pr->getId();
}
$new_farm = new Exploitant();
$new_farm_identite = new ExploitantIdentite();
//$new_farm_pratique = new ExploitantPratique();
$new_farm_speculation1 = new ExploitantSpeculation();
$new_farm_speculation2 = new ExploitantSpeculation();
$new_farm_speculation3 = new ExploitantSpeculation();
$new_farm_speculation4 = new ExploitantSpeculation();
//$new_farm_identite->addExploitantPratique($new_farm_pratique);
$new_farm_identite->addExploitantSpeculation($new_farm_speculation1);
$new_farm_identite->addExploitantSpeculation($new_farm_speculation2);
$new_farm_identite->addExploitantSpeculation($new_farm_speculation3);
$new_farm_identite->addExploitantSpeculation($new_farm_speculation4);
$new_farm->addExploitantIdentite($new_farm_identite);
$form = $this->createForm(ExploitantNewType::class, $new_farm);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//var_dump($_POST);exit;
for ($i=0;$i<4;$i++) {
$tab = array();
if (isset($_POST["type".$i])) {
//var_dump($_POST["type".$i]);exit;
foreach ($_POST["type".$i] as $j=>$type){
$k = $i+1;
$sscharge = $_POST["sscharge".$i][$j];
$produit = $_POST["produit".$i][$j];
$depense = $_POST["depense".$i][$j];
$unite = $_POST["unite".$i][$j];
$index = "".$type.$sscharge.$produit.$depense.$unite."";
//var_dump($_POST);exit;
//$moisList = $em->getRepository(ListMois::class)->findOneByValue($_POST["mois".$i][$j]);
if (isset($tab[$index])) {
$retour = $tab[$index];
$retour["mois".$_POST["mois".$i][$j]] = ($_POST["somme".$i][$j]=="")?0:$_POST["somme".$i][$j];
$retour["sscharge"] = $_POST["sscharge".$i][$j];
$retour["produit"] = $_POST["produit".$i][$j];
$retour["depense"] = $_POST["depense".$i][$j];
$retour["unite"] = $_POST["unite".$i][$j];
$retour["pu"] = ($_POST["pu".$i][$j]=="")?0:$_POST["pu".$i][$j];
$retour["quantite"] = ($_POST["quantite".$i][$j]=="")?0:$_POST["quantite".$i][$j];
$retour["type"] = $_POST["type".$i][$j];
//$tab[$index] = $retour;
} else {
$retour = array(1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0, 8=>0, 9=>0, 10=>0, 11=>0, 12=>0);
for($w=1;$w<13; $w++) {
$retour["mois".$w] = 0;
}
$retour["mois".$_POST["mois".$i][$j]] = ($_POST["somme".$i][$j]=="")?0:$_POST["somme".$i][$j];
$retour["type"] = $_POST["type".$i][$j];
$retour["sscharge"] = $_POST["sscharge".$i][$j];
$retour["produit"] = $_POST["produit".$i][$j];
$retour["depense"] = $_POST["depense".$i][$j];
$retour["unite"] = $_POST["unite".$i][$j];
$retour["pu"] = ($_POST["pu".$i][$j]=="")?0:$_POST["pu".$i][$j];
$retour["quantite"] = ($_POST["quantite".$i][$j]=="")?0:$_POST["quantite".$i][$j];
$tab[$j] = $retour;
}
}
}
//var_dump($tab);exit;
foreach ($tab as $key=>$t){
$charge = new Charge();
$typeList = $em->getRepository(ListTypeCharge::class)->findOneById($t["type"]);
$charge->setTypeCharge($typeList);
$typeList = $em->getRepository(ListSousCharge::class)->findOneById($t["sscharge"]);
$charge->setSousCharge($typeList);
//$typeList = $em->getRepository(ListTypeProduit::class)->findOneById($t["produit"]);
$charge->setTypeProduit($t["produit"]);
$typeList = $em->getRepository(ListTypeDepense::class)->findOneById($t["depense"]);
$charge->setTypeDepense($typeList);
$typeList = $em->getRepository(ListUnite::class)->findOneById($t["unite"]);
$charge->setUnite($typeList);
$mychar = "";
for($q=1;$q<13; $q++) {
if ($q==1) {
$mychar .= $t["mois".$q];
} else {
$mychar .= "//".$t["mois".$q];
}
$methodName = 'setMois'.$q;//this is the good way
$charge->$methodName($t["mois".$q]);
}
$charge->setValue($mychar);
$charge->setPu($t["pu"]);
$charge->setQuantite($t["quantite"]);
${"new_farm_speculation".$k}->addCharge($charge);
}
}
for ($i=0;$i<4;$i++) {
$tab = array();
if (isset($_POST["production".$i])) {
//var_dump($_POST["type".$i]);exit;
foreach ($_POST["production".$i] as $j=>$type){
$k = $i+1;
$index = "".$type."";
$retour=array();
//var_dump($type);exit;
$retour["type"] = $type;
$retour["detail"] = $_POST["detailproduction".$i][$j];
$retour["somme"] = ($_POST["sommeproduction".$i][$j]=="")?0:$_POST["sommeproduction".$i][$j];;
$retour["unite"] = $_POST["uniteproduction".$i][$j];
$retour["pu"] = ($_POST["puproduction".$i][$j]=="")?0:$_POST["puproduction".$i][$j];
$retour["quantite"] = ($_POST["quantiteproduction".$i][$j]=="")?0:$_POST["quantiteproduction".$i][$j];
$tab[] = $retour;
}
}
//var_dump($tab);exit;
foreach ($tab as $key=>$t){
$typeList = $em->getRepository(ListProduction::class)->findOneById($t["type"]);
$unite = $em->getRepository(ListUnite::class)->findOneById($t["unite"]);
$charge = new Produit();
$charge->setProduction($typeList);
$charge->setDetail($t["detail"]);
$charge->setUnite($unite);
$charge->setSomme($t["somme"]);
$charge->setQuantite($t["quantite"]);
$charge->setPu($t["pu"]);
//if (isset($farm_infos->getExploitantSpeculation()[$i])) {
${"new_farm_speculation".$k}->addProduit($charge);
//}
}
}
//var_dump($_POST);exit;
$em->persist($new_farm_identite);
$new_farm->setUpdatedAt(new \Datetime());
$em->persist($new_farm);
$em->flush();
if(in_array('ROLE_OPERATOR', $this->getUser()->getRoles())){
$user =$this->getUser()->getId();
$value = new UserExploitant();
$value->setUserId($user);
$value->setExpId($new_farm->getId());
$em->persist($value);
$em->flush($value);
}
return $this->redirectToRoute("exp_details", ["id" => $new_farm->getId()]);
}
return $this->render('exp/new.html.twig', [
'form' => $form->createView(),
'charges' => $charges,
'sscharges' => $sscharges,
'depenses' => $depenses,
'produits' => $produits,
'unites' => $unites,
'production' => $production,
'mois' => $mois,
'listeIsVegetal' => $listeIsVegetal,
]);
}
/**
* @Route("{id}/edit", name="_edit")
*/
public function edit(Request $request, $id)
{
$retour = $this->checkSecurity($id);
if (!$retour){
return $this->redirectToRoute("exp_crud");
}
$em = $this->getDoctrine()->getManager();
$form = NULL;
$farm = $em->getRepository(Exploitant::class)->find($id);
if(!$farm) return $this->render('error/blank.html.twig', []);
$form = $this->createForm(ExploitantEditType::class, $farm);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$farm->setUpdatedAt(new \Datetime());
$em->persist($farm);
$em->flush();
return $this->redirectToRoute("exp_details", ["id" => $farm->getId()]);
}
return $this->render('exp/edit.html.twig', [
"farm" => $farm,
'form' => $form->createView(),
]);
}
/**
* @Route("{id}/delete", name="_delete")
*/
public function delete($id)
{
$retour = $this->checkSecurity($id);
if (!$retour){
return $this->redirectToRoute("exp_crud");
}
$em = $this->getDoctrine()->getManager();
$farm = $em->getRepository(Exploitant::class)->find($id);
$em->remove($farm);
$em->flush();
return $this->redirectToRoute("exp_crud");
}
/**
* @Route("import", name="_import")
*/
public function import(Request $request)
{
$em = $this->getDoctrine()->getManager();
$row = $i = 0;
$arrayRegion = [];
$arrayDistrict = [];
$arrayCommune = [];
// Import du fichier CSV
if (($handle = fopen("comall.csv", "r")) !== FALSE) { // Lecture du fichier, Ã adapter
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) { // Eléments séparés par une virgule, à modifier si necessaire
$num = count($data); // Nombre d'éléments sur la ligne traitée
if ($row != 0) {
//var_dump($data);exit;
$idRegion = $data[2];
$nomRegion = $data[4];
$idDistrict = $data[1];
$nomDistrict = $data[5];
if (!in_array($idRegion, $arrayRegion)) {
$rsm = new ResultSetMapping();
$rsm->addScalarResult('value', 'value');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT value
FROM list_region
WHERE id = $idRegion ", $rsm);
$exps = $query->getResult();
if ($nomRegion != $exps[0]['value']) {
echo $idRegion.' -- '.$nomRegion.' : '.$exps[0]['value'].' Pas le même nom<br />';
} else {
echo $idRegion.' -- '.$nomRegion.' : '.$exps[0]['value'].'<br />';
}
}
$arrayRegion[] = $idRegion;
}
$row++;
}
}
echo '<br />DISTRICT<br />';
$row = $i = 0;
if (($handle = fopen("comall.csv", "r")) !== FALSE) { // Lecture du fichier, Ã adapter
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) { // Eléments séparés par une virgule, à modifier si necessaire
$num = count($data); // Nombre d'éléments sur la ligne traitée
if ($row != 0) {
//var_dump($data);exit;
$idRegion = $data[2];
$nomRegion = $data[4];
$idDistrict = $data[1];
$nomDistrict = $data[5];
if (!in_array($idDistrict, $arrayDistrict)) {
$rsm = new ResultSetMapping();
$rsm->addScalarResult('value', 'value');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT value
FROM list_district
WHERE id = $idDistrict ", $rsm);
$exps = $query->getResult();
if ($nomDistrict != $exps[0]['value']) {
echo $idDistrict.' -- '.$nomDistrict.' : '.$exps[0]['value'].' Pas le même nom<br />';
} else {
echo $idDistrict.' -- '.$nomDistrict.' : '.$exps[0]['value'].'<br />';
}
}
$arrayDistrict[] = $idDistrict;
}
$row++;
}
}
echo '<br />COMMUNE<br />';
$row = $i = 0;
if (($handle = fopen("comall.csv", "r")) !== FALSE) { // Lecture du fichier, Ã adapter
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) { // Eléments séparés par une virgule, à modifier si necessaire
$num = count($data); // Nombre d'éléments sur la ligne traitée
if ($row != 0) {
//var_dump($data);exit;
$idRegion = $data[2];
$nomRegion = $data[4];
$idDistrict = $data[1];
$nomCommune = $data[6];
//if (!in_array($idDistrict, $arrayDistrict)) {
$rsm = new ResultSetMapping();
$rsm->addScalarResult('value', 'value');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT value
FROM list_commune
WHERE value = upper('".pg_escape_string($nomCommune)."') AND district_id = $idDistrict ", $rsm);
$exps = $query->getResult();
if (isset($exps[0]) ) {
if (strtoupper($nomCommune) != $exps[0]['value']) {
echo $idDistrict.' -- '.$nomCommune.' : '.$exps[0]['value'].' Pas le même nom<br />';
} else {
echo $idDistrict.' -- '.$nomCommune.' : '.$exps[0]['value'].'<br />';
}
} else {
//echo 'Pas de correpondance '.$nomCommune.' ---- ';
$rsm = new ResultSetMapping();
$rsm->addScalarResult('value', 'value');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT value
FROM list_commune
WHERE levenshtein(value,upper('".pg_escape_string($nomCommune)."')) <3 AND district_id = $idDistrict ", $rsm);
$exps = $query->getResult();
$exps = $query->getResult();
if (isset($exps[0]) ) {
if (strtoupper($nomCommune) != $exps[0]['value']) {
echo $idDistrict.' -- '.strtoupper($nomCommune).' : '.strtoupper($exps[0]['value']).' Pas le même nom<br />';
} else {
echo $idDistrict.' -- '.strtoupper($nomCommune).' : '.$exps[0]['value'].'<br />';
}
} else {
$i++;
echo 'Pas de correpondance '.$nomCommune.' '.$i.'<br />';
}
}
//}
$arrayDistrict[] = $idDistrict;
}
$row++;
}
}
echo 'Il y a '.$i.' communes sans correspondance';
exit;
}
/**
* @Route("type/{id}/district", name="_type_district")
*/
public function type_varieties($id)
{
$em = $this->getDoctrine()->getManager();
$region = $em->getRepository(ListDistrict::class)->findBy(["region"=>$id, "isProjet"=>true], ["value"=>"ASC"]);
return $this->render('region/select_district.html.twig', [
"region" => $region
]);
}
/**
* @Route("type/{id}/commune", name="_type_commune")
*/
public function type_commune($id)
{
$em = $this->getDoctrine()->getManager();
//var_dump($id);exit;
if (isset($id) && $id != "null") {
$district = $em->getRepository(ListCommune::class)->findBy(["district"=>$id, "isProjet"=>true], ["value"=>"ASC"]);
} else {
$district = null;
}
return $this->render('region/select_district.html.twig', [
"region" => $district
]);
}
/**
* @Route("/getSecteur", name="_get_district")
*/
public function getSecteur(Request $request) {
if (isset($_POST["region"])) {
$region = implode($_POST["region"], ",");
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('value', 'value');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT id, value from list_district where region_id IN (".$region.") ORDER BY value asc ", $rsm);
$secteurs = $query->getResult();
$list = "<optgroup label='Sélection District(s)'>";
foreach ($secteurs as $secteur) {
$list .= "<option value=".$secteur["id"].">".$secteur["value"]."</option>";
}
$list .= "</optgroup>";
echo ($list);exit;
}
}
/**
* @Route("/getCommune", name="_get_commune")
*/
public function getCommune(Request $request) {
if (isset($_POST["secteur"])) {
$region = implode($_POST["secteur"], ",");
$commune = null;
if (isset($_POST["commune"])){
$commune = implode($_POST["commune"], ",");
}
$sql = "";
if (isset($commune) && $commune != "null" ) {
$sql = "AND id NOT IN (".$commune.")";
}
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('value', 'value');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT id, value from list_commune where district_id IN (".$region.") $sql ORDER BY value asc ", $rsm);
$secteurs = $query->getResult();
$list = "<optgroup label='Sélection Commune(s)'>";
foreach ($secteurs as $secteur) {
$list .= "<option value=".$secteur["id"].">".$secteur["value"]."</option>";
}
$list .= "</optgroup>";
echo ($list);exit;
}
echo '';exit;
}
/**
* @Route("/getExp", name="_get_exp")
*/
public function getExp(Request $request) {
$sql2 = " ";
if (isset($_POST["myregion"])){
$mycommune = $_POST["myregion"];
if ($mycommune != 0)
$sql2 = " AND lc.id = $mycommune ";
}
$list = "";
$list = "<optgroup label='Sélection Exploitant(s)'>";
if (isset($_POST["commune"])) {
$region = implode($_POST["commune"], ",");
$sql = "";
if (isset($region) && $region != "null" ) {
$sql = "AND exp.id NOT IN (".$region.")";
}
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('nom', 'nom');
$rsm->addScalarResult('prenom', 'prenom');
$rsm->addScalarResult('commune', 'commune');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT distinct exp.id as id, nom, prenom, lc.value as commune from exploitant exp
LEFT JOIN exploitant_identite expi ON expi.exploitant_id = exp.id
LEFT JOIN list_commune lc ON lc.id = expi.commune_exp_id
where 1=1 $sql $sql2
ORDER BY nom asc ", $rsm);
$secteurs = $query->getResult();
foreach ($secteurs as $secteur) {
$list .= "<option value=".$secteur["id"].">#".$secteur["id"]." - ".$secteur["nom"]." ".$secteur["prenom"]." - ".$secteur["commune"]."</option>";
}
echo ($list);exit;
} else {
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('nom', 'nom');
$rsm->addScalarResult('prenom', 'prenom');
$rsm->addScalarResult('commune', 'commune');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT distinct exp.id as id, nom, prenom, lc.value as commune from exploitant exp
LEFT JOIN exploitant_identite expi ON expi.exploitant_id = exp.id
LEFT JOIN list_commune lc ON lc.id = expi.commune_exp_id
where 1=1 $sql2
ORDER BY nom asc ", $rsm);
$secteurs = $query->getResult();
foreach ($secteurs as $secteur) {
$list .= "<option value=".$secteur["id"].">#".$secteur["id"]." - ".$secteur["nom"]." ".$secteur["prenom"]." - ".$secteur["commune"]."</option>";
}
echo ($list);exit;
}
echo '';exit;
}
/**
* @Route("/addCommune", name="_add_commune")
*/
public function addCommune(Request $request) {
//var_dump($_POST);exit;
if (isset($_POST["region"])) {
$region = implode($_POST["region"], ",");
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('value', 'value');
$rsm->addScalarResult('region', 'region');
$rsm->addScalarResult('district', 'district');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT com.id, com.value, dis.value as district, reg.value as region from list_commune com
LEFT JOIN list_district dis ON com.district_id = dis.id
LEFT JOIN list_region reg ON dis.region_id = reg.id
where com.id IN (".$region.")
ORDER BY value asc ", $rsm);
$secteurs = $query->getResult();
$list = "";
foreach ($secteurs as $secteur) {
$list .= "<option value=".$secteur["id"].">".$secteur["region"]." > ".$secteur["district"]." > ".$secteur["value"]."</option>";
}
echo ($list);exit;
}
}
/**
* @Route("/addExp", name="_add_exp")
*/
public function addExp(Request $request) {
//var_dump($_POST);exit;
$sql2 = " ";
if (isset($_POST["myregion"])){
$mycommune = $_POST["myregion"];
if ($mycommune != 0)
$sql2 = " AND lc.id = $mycommune ";
}
if (isset($_POST["region"])) {
$region = implode($_POST["region"], ",");
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('nom', 'nom');
$rsm->addScalarResult('prenom', 'prenom');
$rsm->addScalarResult('commune', 'commune');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT distinct exp.id as id, nom, prenom, lc.value as commune from exploitant exp
LEFT JOIN exploitant_identite expi ON expi.exploitant_id = exp.id
LEFT JOIN list_commune lc ON lc.id = expi.commune_exp_id
where exp.id IN (".$region.") $sql2
ORDER BY nom asc ", $rsm);
$secteurs = $query->getResult();
$list = "";
foreach ($secteurs as $secteur) {
$list .= "<option value=".$secteur["id"].">#".$secteur["id"]." - ".$secteur["nom"]." ".$secteur["prenom"]." - ".$secteur["commune"]."</option>";
}
echo ($list);exit;
}
}
/**
* @Route("/addCommuneAll", name="_all_commune")
*/
public function addCommuneAll(Request $request) {
//var_dump($_POST);exit;
if (isset($_POST["region"])) {
$region = $_POST["region"];
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('value', 'value');
$rsm->addScalarResult('region', 'region');
$rsm->addScalarResult('district', 'district');
$query = $this->getDoctrine()->getManager()->createNativeQuery("SELECT com.id, com.value, dis.value as district, reg.value as region from list_commune com
LEFT JOIN list_district dis ON com.district_id = dis.id
LEFT JOIN list_region reg ON dis.region_id = reg.id
where com.id IN (".$region.")
ORDER BY value asc ", $rsm);
$secteurs = $query->getResult();
$list = "";
foreach ($secteurs as $secteur) {
$list .= "<option value=".$secteur["id"].">".$secteur["region"]." > ".$secteur["district"]." > ".$secteur["value"]."</option>";
}
echo ($list);exit;
}
}
public function checkSecurity($id = null, $action = false){
if(in_array('ROLE_ADMIN', $this->getUser()->getRoles())){
return true;
}
if(in_array('ROLE_OPERATOR', $this->getUser()->getRoles())){
if ($id == null){
return true;
} else {
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$listeExp = $em->getRepository(UserExploitant::class)->findOneBy(array('userId'=>$user->getId(), "expId" => $id));
if (isset($listeExp)){
return true;
} else {
return false;
}
}
}
if(in_array('ROLE_REGIONAL', $this->getUser()->getRoles())){
return true;
}
if(in_array('ROLE_USER', $this->getUser()->getRoles())){
if (!$action) {
return false;
} else {
if ($id == null){
return false;
} else {
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$listeExp = $em->getRepository(UserExploitant::class)->findOneBy(array('userId'=>$user->getId(), "expId" => $id));
if (isset($listeExp)){
return true;
} else {
return false;
}
}
}
}
}
/**
* @Route("{id}/stat2", name="_stat2")
*/
public function stat2(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$farm = $em->getRepository(Exploitant::class)->find($id);
$retour = $this->checkSecurity($id, true);
if (!$retour){
return $this->redirectToRoute("exp_crud");
}
$tab = $tabFinal3 = $tabFinal = $tabFinal2 = $tabFinal4 = $tabFinal5 = $tabFinal6 = array();
$sousCharges = $em->getRepository(ListSousCharge::class)->findAll();
$retourListeZero = "[";
$retourListeAZero = "[";
foreach ($sousCharges as $ssch){
if ($ssch->getTypeCharge()->getIsVegetal()) {
if ($retourListeZero == "["){
$retourListeZero .= "0";
} else {
$retourListeZero .= ",0";
}
} else {
if ($retourListeAZero == "["){
$retourListeAZero .= "0";
} else {
$retourListeAZero .= ",0";
}
}
}
$retourListeAZero .= "]";
$retourListeZero .= "]";
foreach ($farm->getExploitantIdentite() as $identite){
$testNull = true;
foreach ($identite->getExploitantSpeculation() as $key=>$speculation) {
$tab2 = array();
$qteProduit = $nbVente = $sommePrix = $surface = $chargeTot = $produitTot = $recette = $depense= $qteJeune = $qteAdulte = $qteAutre = $perte = $repro= $mySomme = $sommePrixJ = $sommePrixA = $sommePrixAu = $nbVenteAu = $nbVenteA = $nbVenteJ = 0;
$cle = $key+1;
$method_set = "getSpecialite".$cle;
$vegouani = $identite->$method_set();
if (isset($vegouani)) {
if ($vegouani->getIsVegetal()) {
foreach ($sousCharges as $ssch){
if ($ssch->getTypeCharge()->getIsVegetal()) {
$idch = $ssch->getValue();
$annee = $identite->getAnnee();
$tab2["".$idch.""] = 0;
}
}
$tabFinal[$vegouani->getId()]["nom"] = $vegouani->getValue();
$tabFinal2[$vegouani->getId()]["nom"] = $vegouani->getValue();
$tabFinal3[$vegouani->getId()]["nom"] = $vegouani->getValue();
if ($speculation->getPvSurface() != null) {
$surface += (float) $speculation->getPvSurface();
}
foreach ($speculation->getCharge() as $charge) {
$so = 0;
for ($w=1;$w<13;$w++){
$method_set = "getMois".$w;
$so += (float) $charge->$method_set();
}
$chargeTot += (float) $so;
if ($charge->getTypeDepense()->getId() == 2 ){
$depense += (float) $so;
}
if ($charge->getTypeCharge()->getIsVegetal()) {
$tab2[$charge->getSousCharge()->getValue()] += (float) $so;
if ($so != 0){
$testNull = false;
}
}
}
foreach ($speculation->getProduit() as $charge) {
$mySomme = $charge->getSomme();
$produitTot += (float) $mySomme;
$qteProduit += (float) $charge->getQuantite();
if ($charge->getProduction()->getId() == 1){
$recette += (float) $mySomme;
$sommePrix += (float) $charge->getPu();
$nbVente++;
}
}
$tabFinal[$vegouani->getId()]["tab2"][$identite->getAnnee()] = $tab2;
$tabFinal2[$vegouani->getId()]["tab1"]["annee"][] = $identite->getAnnee();
$tabFinal2[$vegouani->getId()]["tab1"]["surface"][] = $surface;
$tabFinal2[$vegouani->getId()]["tab1"]["charge"][] = number_format($chargeTot, 0, ',', ' ');
$tabFinal2[$vegouani->getId()]["tab1"]["produit"][] = number_format($produitTot, 0, ',', ' ');
$tabFinal2[$vegouani->getId()]["tab1"]["depense"][] = number_format($depense, 0, ',', ' ');
$tabFinal2[$vegouani->getId()]["tab1"]["recette"][] = number_format($recette, 0, ',', ' ');
$tabFinal2[$vegouani->getId()]["tab1"]["marge"][] = number_format($produitTot - $chargeTot, 0, ',', ' ');
$tabFinal2[$vegouani->getId()]["tab1"]["benefice"][] = number_format($recette - $depense, 0, ',', ' ');
if ($surface == 0 && $chargeTot == 0 && $produitTot == 0 && $depense == 0 && $recette == 0){
if (!isset($tabFinal2[$vegouani->getId()]["affiche"]) || $tabFinal2[$vegouani->getId()]["affiche"]!=true)
$tabFinal2[$vegouani->getId()]["affiche"]= false;
} else {
$tabFinal2[$vegouani->getId()]["affiche"] = true;
}
$tabFinal3[$vegouani->getId()]["tab3"]["annee"][] = $identite->getAnnee();
if ($surface != 0) {
$tabFinal3[$vegouani->getId()]["tab3"]["marge"][] = number_format(($produitTot - $chargeTot)/$surface, 0, ',', ' ');
$tabFinal3[$vegouani->getId()]["tab3"]["charge"][] = number_format($chargeTot/$surface, 0, ',', ' ');
$tabFinal3[$vegouani->getId()]["tab3"]["produit"][] = number_format($produitTot/$surface, 0, ',', ' ');
$tabFinal3[$vegouani->getId()]["tab3"]["recette"][] = number_format($recette/$surface, 0, ',', ' ');
$tabFinal3[$vegouani->getId()]["tab3"]["benefice"][] = number_format(($recette - $depense)/$surface, 0, ',', ' ');
$tabFinal3[$vegouani->getId()]["tab3"]["depense"][] = number_format($depense/$surface, 0, ',', ' ');
$tabFinal3[$vegouani->getId()]["tab3"]["coutprod"][] = ($qteProduit != 0)?number_format($chargeTot/$qteProduit, 0, ',', ' '):0;
$tabFinal3[$vegouani->getId()]["tab3"]["prixVente"][] = ($nbVente !=0)?number_format($sommePrix/$nbVente, 0, ',', ' '):0;
$tabFinal3[$vegouani->getId()]["tab3"]["rendement"][] = number_format($qteProduit/$surface, 0, ',', ' ');
} else {
$tabFinal3[$vegouani->getId()]["tab3"]["marge"][] = 0;
$tabFinal3[$vegouani->getId()]["tab3"]["charge"][] = 0;
$tabFinal3[$vegouani->getId()]["tab3"]["produit"][] = 0;
$tabFinal3[$vegouani->getId()]["tab3"]["recette"][] = 0;
$tabFinal3[$vegouani->getId()]["tab3"]["benefice"][] = 0;
$tabFinal3[$vegouani->getId()]["tab3"]["depense"][] = 0;
$tabFinal3[$vegouani->getId()]["tab3"]["rendement"][] = 0;
$tabFinal3[$vegouani->getId()]["tab3"]["coutprod"][] = ($qteProduit != 0)?number_format($chargeTot/$qteProduit, 0, ',', ' '):0;
$tabFinal3[$vegouani->getId()]["tab3"]["prixVente"][] = ($nbVente !=0)?number_format($sommePrix/$nbVente, 0, ',', ' '):0;
}
if ($surface == 0 && $chargeTot == 0 && $produitTot == 0 && $depense == 0 && $recette == 0){
if (!isset($tabFinal3[$vegouani->getId()]["affiche"]) || $tabFinal3[$vegouani->getId()]["affiche"]!=true)
$tabFinal3[$vegouani->getId()]["affiche"]= false;
} else {
$tabFinal3[$vegouani->getId()]["affiche"] = true;
}
}
if (!$vegouani->getIsVegetal()) {
foreach ($sousCharges as $ssch){
if (!$ssch->getTypeCharge()->getIsVegetal()) {
$idch = $ssch->getValue();
$annee = $identite->getAnnee();
$tab2["".$idch.""] = 0;
}
}
$tabFinal4[$vegouani->getId()]["nom"] = $vegouani->getValue();
$tabFinal5[$vegouani->getId()]["nom"] = $vegouani->getValue();
$tabFinal6[$vegouani->getId()]["nom"] = $vegouani->getValue();
if ($speculation->getPaCheptel() != null) {
$repro += (float) $speculation->getPaCheptel();
}
foreach ($speculation->getCharge() as $charge) {
$so = 0;
for ($w=1;$w<13;$w++){
$method_set = "getMois".$w;
$so += (float) $charge->$method_set();
}
$chargeTot += (float) $so;
if ($charge->getTypeDepense()->getId() == 2 ){
$depense += (float) $so;
}
if (!$charge->getTypeCharge()->getIsVegetal()) {
$tab2[$charge->getSousCharge()->getValue()] += (float) $so;
if ($so != 0){
$testNull = false;
}
}
}
foreach ($speculation->getProduit() as $charge) {
$mySomme = $charge->getSomme();
$produitTot += (float) $mySomme;
if ($charge->getProduction()->getId() < 12){
$qteProduit += (float) $charge->getQuantite();
}
if ($charge->getProduction()->getId() == 5){
$recette += (float) $charge->getSomme();
$qteJeune += (float) $charge->getQuantite();
$sommePrix += (float) $charge->getPu();
$nbVente++;
$sommePrixJ += (float) $charge->getPu();
$nbVenteJ++;
}
if ($charge->getProduction()->getId() == 9){
$recette += (float) $charge->getSomme();
$qteAdulte += (float) $charge->getQuantite();
$sommePrixA += (float) $charge->getPu();
$nbVenteA++;
}
if ($charge->getProduction()->getId() == 13){
$recette += (float) $charge->getSomme();
$qteAutre += (float) $charge->getQuantite();
$sommePrixAu += (float) $charge->getPu();
$nbVenteAu++;
}
if ($charge->getProduction()->getId() == 6 || $charge->getProduction()->getId() == 10 || $charge->getProduction()->getId() == 14){
$perte += (float) $charge->getQuantite();
}
}
$tabFinal5[$vegouani->getId()]["tab2"][$identite->getAnnee()] = $tab2;
//var_dump($tabFinal5);exit;
$tabFinal4[$vegouani->getId()]["tab1"]["annee"][] = $identite->getAnnee();
$tabFinal4[$vegouani->getId()]["tab1"]["repro"][] = $repro;
$tabFinal4[$vegouani->getId()]["tab1"]["qteProduit"][] = $qteProduit;
$tabFinal4[$vegouani->getId()]["tab1"]["jeuneVendu"][] = $qteJeune;
$tabFinal4[$vegouani->getId()]["tab1"]["adulteVendu"][] = $qteAdulte;
$tabFinal4[$vegouani->getId()]["tab1"]["autreVendu"][] = $qteAutre;
$tabFinal4[$vegouani->getId()]["tab1"]["perte"][] = ($qteProduit != 0)?$perte/$qteProduit:0;
$tabFinal4[$vegouani->getId()]["tab1"]["chargeTot"][] = number_format($chargeTot, 0, ',', ' ');
$tabFinal4[$vegouani->getId()]["tab1"]["produitTot"][] = number_format($produitTot, 0, ',', ' ');
$tabFinal4[$vegouani->getId()]["tab1"]["marge"][] = number_format($produitTot - $chargeTot, 0, ',', ' ');
$tabFinal4[$vegouani->getId()]["tab1"]["depense"][] = number_format($depense, 0, ',', ' ');
$tabFinal4[$vegouani->getId()]["tab1"]["recette"][] = number_format($recette, 0, ',', ' ');
$tabFinal4[$vegouani->getId()]["tab1"]["benefice"][] = number_format($recette - $depense, 0, ',', ' ');
if ($repro == 0 && $qteProduit == 0 && $qteJeune == 0 && $qteAdulte == 0 && $qteAutre == 0 && $chargeTot == 0 && $produitTot == 0){
if (!isset($tabFinal4[$vegouani->getId()]["affiche"]) || $tabFinal4[$vegouani->getId()]["affiche"]!=true)
$tabFinal4[$vegouani->getId()]["affiche"]= false;
} else {
$tabFinal4[$vegouani->getId()]["affiche"] = true;
}
$tabFinal6[$vegouani->getId()]["tab7"]["annee"][] = $identite->getAnnee();
$tabFinal6[$vegouani->getId()]["tab7"]["charge"][] = ($repro != 0)?number_format($chargeTot/$repro, 0, ',', ' '):0;
$tabFinal6[$vegouani->getId()]["tab7"]["produit"][] = ($repro != 0)?number_format($produitTot/$repro, 0, ',', ' '):0;
$tabFinal6[$vegouani->getId()]["tab7"]["marge"][] = ($repro != 0)?number_format(($produitTot - $chargeTot)/$repro, 0, ',', ' '):0;
$tabFinal6[$vegouani->getId()]["tab7"]["depense"][] = ($repro != 0)?number_format($depense/$repro, 0, ',', ' '):0;
$tabFinal6[$vegouani->getId()]["tab7"]["recette"][] = ($repro != 0)?number_format($mySomme/$repro, 0, ',', ' '):0;
$tabFinal6[$vegouani->getId()]["tab7"]["benefice"][] = ($repro != 0)?number_format(($mySomme - $depense)/$repro, 0, ',', ' '):0;
$tabFinal6[$vegouani->getId()]["tab7"]["venteJ"][] = ($nbVenteJ !=0)?number_format($sommePrixJ/$nbVenteJ, 0, ',', ' '):0;
$tabFinal6[$vegouani->getId()]["tab7"]["venteA"][] = ($nbVenteA !=0)?number_format($sommePrixA/$nbVenteA, 0, ',', ' '):0;
$tabFinal6[$vegouani->getId()]["tab7"]["venteAu"][] = ($nbVenteAu !=0)?number_format($sommePrixAu/$nbVenteAu, 0, ',', ' '):0;
if ($repro == 0 && $chargeTot == 0 && $depense == 0 && $mySomme == 0 && $chargeTot == 0 && $produitTot == 0){
if (!isset($tabFinal6[$vegouani->getId()]["affiche"]) || $tabFinal6[$vegouani->getId()]["affiche"]!=true)
$tabFinal6[$vegouani->getId()]["affiche"]= false;
} else {
$tabFinal6[$vegouani->getId()]["affiche"] = true;
}
}
}
}
}
return $this->render('exp/stat2.html.twig', [
"farm" => $farm,
"tabFinal2" => $tabFinal2,
"tabFinal" => $tabFinal,
"tabFinal3" => $tabFinal3,
"tabFinal4" => $tabFinal4,
"tabFinal5" => $tabFinal5,
"tabFinal6" => $tabFinal6,
"retourListeAZero" => $retourListeAZero,
"retourListeZero" => $retourListeZero,
]);
}
/**
* @Route("{id}/stat", name="_stat")
*/
public function stat(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$farm = $em->getRepository(Exploitant::class)->find($id);
$retour = $this->checkSecurity($id, true);
if (!$retour){
return $this->redirectToRoute("exp_crud");
}
$tab = $tab2 = $tab3 = $tab4 = $tab5 = $tab6 = $tab7 = array();
$sousCharges = $em->getRepository(ListSousCharge::class)->findAll();
foreach ($farm->getExploitantIdentite() as $identite){
$surface = $chargeTotE = $produitTotE = $recetteE = $depenseE = $qteProduitA = $qteProduitE = $chargeTotA = $produitTotA = $recetteA = $depenseA = $qteJeune = $qteAdulte = $qteAutre = $perte = $repro =0;
//$tab2[$identite->getAnnee()] = 0;
foreach ($sousCharges as $ssch){
if ($ssch->getTypeCharge()->getIsVegetal()) {
$idch = $ssch->getValue();
$annee = $identite->getAnnee();
$tab2["".$annee.""]["".$idch.""] = 0;
}
}
$qteProduit = $nbVente = $sommePrix = $qteProduitJ = $nbVenteJ = $sommePrixJ = $qteProduitA = $nbVenteA = $sommePrixA= $qteProduitAu = $nbVenteAu = $sommePrixAu= 0;
$testNull = true;
foreach ($identite->getExploitantSpeculation() as $key=>$speculation) {
$cle = $key+1;
$method_set = "getSpecialite".$cle;
$vegouani = $identite->$method_set();
if (isset($vegouani)) {
if ($vegouani->getIsVegetal()) {
if ($speculation->getPvSurface() != null) {
$surface += (float) $speculation->getPvSurface();
}
//charges
foreach ($speculation->getCharge() as $charge) {
$so = 0;
for ($w=1;$w<13;$w++){
$method_set = "getMois".$w;
$so += (float) $charge->$method_set();
}
$chargeTotA += (float) $so;
if ($charge->getTypeDepense()->getId() == 2 ){
$depenseA += (float) $so;
}
if ($charge->getTypeCharge()->getIsVegetal()) {
$tab2[$identite->getAnnee()][$charge->getSousCharge()->getValue()] += (float) $so;
if ($so != 0){
$testNull = false;
}
}
}
foreach ($speculation->getProduit() as $charge) {
$mySomme = $charge->getSomme();
$produitTotA += (float) $mySomme;
$qteProduitA += (float) $charge->getQuantite();
if ($charge->getProduction()->getId() == 1){
$recetteA += (float) $mySomme;
$sommePrix += (float) $charge->getPu();
$nbVente++;
}
}
}
if (!$vegouani->getIsVegetal()) {
foreach ($sousCharges as $ssch){
if (!$ssch->getTypeCharge()->getIsVegetal()) {
$idch = $ssch->getValue();
$annee = $identite->getAnnee();
$tab6["".$annee.""]["".$idch.""] = 0;
}
}
$tabFinal4[$vegouani->getId()]["nom"] = $vegouani->getValue();
$tabFinal5[$vegouani->getId()]["nom"] = $vegouani->getValue();
if ($speculation->getPaCheptel() != null) {
$repro += (float) $speculation->getPaCheptel();
}
foreach ($speculation->getCharge() as $charge) {
$so = 0;
for ($w=1;$w<13;$w++){
$method_set = "getMois".$w;
$so += (float) $charge->$method_set();
}
$chargeTotE += (float) $so;
if ($charge->getTypeDepense()->getId() == 2 ){
$depenseE += (float) $so;
}
if (!$charge->getTypeCharge()->getIsVegetal()) {
$tab6[$identite->getAnnee()][$charge->getSousCharge()->getValue()] += (float) $so;
if ($so != 0){
$testNull = false;
}
}
}
foreach ($speculation->getProduit() as $charge) {
$mySomme = $charge->getSomme();
$produitTotE += (float) $mySomme;
$qteProduitE += (float) $charge->getQuantite();
if ($charge->getProduction()->getId() == 5){
$recetteE += (float) $mySomme;
$qteJeune += (float) $charge->getQuantite();
$sommePrixJ += (float) $charge->getPu();
$nbVenteJ++;
}
if ($charge->getProduction()->getId() == 9){
$qteAdulte += (float) $charge->getQuantite();
$recetteE += (float) $mySomme;
$sommePrixA+= (float) $charge->getPu();
$nbVenteA++;
}
if ($charge->getProduction()->getId() == 13){
$qteAutre += (float) $charge->getQuantite();
$recetteE += (float) $mySomme;
$sommePrixAu += (float) $charge->getPu();
$nbVenteAu++;
}
if ($charge->getProduction()->getId() == 6 || $charge->getProduction()->getId() == 10 || $charge->getProduction()->getId() == 14){
$perte += (float) $charge->getQuantite();
}
}
}
}
}
if ($testNull) {
unset($tab2[$identite->getAnnee()]);
unset($tab6[$identite->getAnnee()]);
}
$tab[$identite->getAnnee()]["surface"] = $surface;
$tab[$identite->getAnnee()]["charge"] = $chargeTotA;
$tab[$identite->getAnnee()]["produit"] = $produitTotA;
$tab[$identite->getAnnee()]["marge"] = $produitTotA - $chargeTotA;
$tab[$identite->getAnnee()]["depense"] = $depenseA;
$tab[$identite->getAnnee()]["recette"] = $recetteA;
$tab[$identite->getAnnee()]["benefice"] = $recetteA - $depenseA;
$tab[$identite->getAnnee()]["qteProduit"] = $qteProduitA;
$tab[$identite->getAnnee()]["prixVente"] = ($nbVente !=0)?$sommePrix/$nbVente:0;
$tab[$identite->getAnnee()]["nbVente"] = $nbVente;
$tab[$identite->getAnnee()]["sommePrix"] = $sommePrix;
$tab4[$identite->getAnnee()]["annee"] = $identite->getAnnee();
$tab4[$identite->getAnnee()]["repro"] = $repro;
$tab4[$identite->getAnnee()]["qteProduit"] = $qteProduitE;
$tab4[$identite->getAnnee()]["jeuneVendu"] = $qteJeune;
$tab4[$identite->getAnnee()]["adulteVendu"] = $qteAdulte;
$tab4[$identite->getAnnee()]["autreVendu"] = $qteAutre;
$tab4[$identite->getAnnee()]["perte"] = ($qteProduitE != 0)?$perte/$qteProduitE:0;
$tab4[$identite->getAnnee()]["chargeTot"] = $chargeTotE;
$tab4[$identite->getAnnee()]["produitTot"] = $produitTotE;
$tab4[$identite->getAnnee()]["marge"] = $produitTotE - $chargeTotE;
$tab4[$identite->getAnnee()]["depense"] = $depenseE;
$tab4[$identite->getAnnee()]["recette"] = $recetteE;
$tab4[$identite->getAnnee()]["benefice"] = $recetteE - $depenseE;
$tab4[$identite->getAnnee()]["nbVenteJ"] = $nbVenteJ;
$tab4[$identite->getAnnee()]["sommePrixJ"] = $sommePrixJ;
$tab4[$identite->getAnnee()]["nbVenteA"] = $nbVenteA;
$tab4[$identite->getAnnee()]["sommePrixA"] = $sommePrixA;
$tab4[$identite->getAnnee()]["nbVenteAu"] = $nbVenteAu;
$tab4[$identite->getAnnee()]["sommePrixAu"] = $sommePrixAu;
}
$tab1 = array();
foreach ($tab as $key=>$t) {
$tab1["annee"][] = $key;
$tab1["surface"][] = $t["surface"];
$tab1["charge"][] = number_format($t["charge"], 0, ',', ' ');
$tab1["produit"][] = number_format($t["produit"], 0, ',', ' ');
$tab1["depense"][] = number_format($t["depense"], 0, ',', ' ');
$tab1["recette"][] = number_format($t["recette"], 0, ',', ' ');
$tab1["marge"][] = number_format($t["marge"], 0, ',', ' ');
$tab1["benefice"][] = number_format($t["benefice"], 0, ',', ' ');
$tab3["annee"][] = $key;
if ($t["surface"] != 0) {
$tab3["marge"][] = number_format($t["marge"]/$t["surface"], 0, ',', ' ');
$tab3["charge"][] = number_format($t["charge"]/$t["surface"], 0, ',', ' ');
$tab3["produit"][] = number_format($t["produit"]/$t["surface"], 0, ',', ' ');
$tab3["recette"][] = number_format($t["recette"]/$t["surface"], 0, ',', ' ');
$tab3["benefice"][] = number_format($t["benefice"]/$t["surface"], 0, ',', ' ');
$tab3["depense"][] = number_format($t["depense"]/$t["surface"], 0, ',', ' ');
$tab3["coutprod"][] = ($t["qteProduit"] != 0)?number_format($t["charge"]/$t["qteProduit"], 0, ',', ' '):0;
$tab3["prixVente"][] = ($t["nbVente"] !=0)?number_format($t["sommePrix"]/$t["nbVente"], 0, ',', ' '):0;
$tab3["rendement"][] = number_format($t["qteProduit"]/$t["surface"], 0, ',', ' ');
} else {
$tab3["marge"][] = 0;
$tab3["charge"][] = 0;
$tab3["produit"][] = 0;
$tab3["recette"][] = 0;
$tab3["benefice"][] = 0;
$tab3["depense"][] = 0;
$tab3["rendement"][] = 0;
$tab3["coutprod"][] = ($t["qteProduit"] != 0)?$t["charge"]/$t["qteProduit"]:0;
$tab3["prixVente"][] = ($t["nbVente"] !=0)?$t["sommePrix"]/$t["nbVente"]:0;
}
}
foreach ($tab4 as $key=>$t) {
$tab5["annee"][] = $key;
$tab5["repro"][] = $t["repro"];
$tab5["qteProduit"][] = $t["qteProduit"];
$tab5["jeuneVendu"][] = $t["jeuneVendu"];
$tab5["adulteVendu"][] = $t["adulteVendu"];
$tab5["autreVendu"][] = $t["autreVendu"];
$tab5["perte"][] = $t["perte"];
$tab5["chargeTot"][] = number_format($t["chargeTot"], 0, ',', ' ');
$tab5["produitTot"][] = number_format($t["produitTot"], 0, ',', ' ');
$tab5["marge"][] = number_format($t["marge"], 0, ',', ' ');
$tab5["depense"][] = number_format($t["depense"], 0, ',', ' ');
$tab5["recette"][] = number_format($t["recette"], 0, ',', ' ');
$tab5["benefice"][] = number_format($t["benefice"], 0, ',', ' ');
$tab7["annee"][] = $key;
$tab7["charge"][] = ($t["repro"] != 0)?number_format($t["chargeTot"]/$t["repro"], 0, ',', ' '):0;
$tab7["produit"][] = ($t["repro"] != 0)?number_format($t["produitTot"]/$t["repro"], 0, ',', ' '):0;
$tab7["marge"][] = ($t["repro"] != 0)?number_format($t["marge"]/$t["repro"], 0, ',', ' '):0;
$tab7["depense"][] = ($t["repro"] != 0)?number_format($t["depense"]/$t["repro"], 0, ',', ' '):0;
$tab7["recette"][] = ($t["repro"] != 0)?number_format($t["recette"]/$t["repro"], 0, ',', ' '):0;
$tab7["benefice"][] = ($t["repro"] != 0)?number_format($t["benefice"]/$t["repro"], 0, ',', ' '):0;
$tab7["venteJ"][] = ($t["nbVenteJ"] !=0)?number_format($t["sommePrixJ"]/$t["nbVenteJ"], 0, ',', ' '):0;
$tab7["venteA"][] = ($t["nbVenteA"] !=0)?number_format($t["sommePrixA"]/$t["nbVenteA"], 0, ',', ' '):0;
$tab7["venteAu"][] = ($t["nbVenteAu"] !=0)?number_format($t["sommePrixAu"]/$t["nbVenteAu"], 0, ',', ' '):0;
}
return $this->render('exp/stat.html.twig', [
"farm" => $farm,
"tab1" => $tab1,
"tab2" => $tab2,
"tab3" => $tab3,
"tab5" => $tab5,
"tab6" => $tab6,
"tab7" => $tab7,
]);
}
}