<?php
namespace App\Repository\Kanban;
use App\Entity\Kanban\Colonne;
use App\Entity\Kanban\Fiche;
use App\Entity\Kanban\StatutFiche;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* ColonneRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ColonneRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Colonne::class);
}
public function getTotalOffres($colonne, $param = [], $user)
{
$param["parametres"] = $param;
//and
//kf.statut_fiche_id is null
$where = "";
$inner = "";
$inner .= " LEFT JOIN kanban__statut_fiche sf ON sf.id = kf.statut_fiche_id";
$where .= " AND (sf.id IS NULL OR sf.id NOT IN (3))";
if (array_key_exists('favoris', $param['parametres']) and ($param['parametres']['favoris'] == "1")) {
$inner .= " INNER JOIN kanban__favoris ON kf.id = kanban__favoris.fiche_id";
}
if (array_key_exists('client', $param['parametres']) and ($param['parametres']['client'] != "")) {
$inner .= ' LEFT JOIN client__client cli ON cli.id = kf.client_id';
$where .= " AND cli.id = ".$param['parametres']['client'];
}
if (array_key_exists('u', $param['parametres']) and count($param['parametres']['u']) > 0) {
$where .= " AND kf.utilisateur_id IN (";
$in = "";
foreach ($param['parametres']['u'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= $in.")";
}
if (is_object($user->getType()) and ($user->getType()->getId() != "1" and $user->getType()->getId() != "2")) {
/*
$where .= " AND kf.utilisateur_id IN (";
$in = "";
$in .= $user->getId().",";
$in = trim($in,",");
$where .= $in.")";
*/
$where .= " AND (kf.utilisateur_id IN (".$user->getId().") or kf.id IN (select fiche_id FROM kanban__equipe WHERE utilisateur_id IN (".$user->getId().") ) )";
} elseif (array_key_exists('filtre-utilisateurs', $param['parametres']) and count($param['parametres']['filtre-utilisateurs']) > 0) {
/*
$where .= " AND kf.utilisateur IN (:utilisateurs)";
foreach($param['parametres']['filtre-utilisateurs'] as $key=>$value) {
$parametres['utilisateurs'][]=$value;
}
*/
//$where .= " AND kf.utilisateur_id IN (";
$in = "";
foreach ($param['parametres']['filtre-utilisateurs'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
//$where .= $in.")";
$where .= " AND (kf.utilisateur_id IN (".$in.") or kf.id IN (select fiche_id FROM kanban__equipe WHERE utilisateur_id IN (".$in.") ) )";
}
if (array_key_exists('filtre-apporteurs', $param['parametres']) and count($param['parametres']['filtre-apporteurs']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-apporteurs'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND kf.apporteur_id IN (".$in.")";
}
if (array_key_exists('filtre-commerciaux', $param['parametres']) and count($param['parametres']['filtre-commerciaux']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-commerciaux'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND kf.commercial_id IN (".$in.")";
}
if (array_key_exists('filtre-chefsProjet', $param['parametres']) and count($param['parametres']['filtre-chefsProjet']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-chefsProjet'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND kf.chef_projet_id IN (".$in.")";
}
if (array_key_exists('filtre-fiche', $param['parametres']) and ($param['parametres']['filtre-fiche'] != "")) {
$where .= " AND kf.id = ".$param['parametres']['filtre-fiche'];
//$parametres['fiche'] = $param['parametres']['filtre-fiche'];
}
/*
if(array_key_exists('or', $param['parametres']) and ($param['parametres']['or'] != "")) {
//$query->join('p.ficheOrigine', 'fo');
//$where .= " AND fo.origine = :origine";
//$parametres['origine'] = $param['parametres']['or'];
$where .= " AND kf.id IN (select fiche_id FROM kanban__fiche_origine WHERE fiche_id = kf.id and origine_id = '".$param['parametres']['or']."' )";
}
*/
if (array_key_exists('or', $param['parametres']) and ($param['parametres']['or'] != "")) {
$where .= " AND kf.origine_commande_id = ".$param['parametres']['or'];
}
if (array_key_exists('marche', $param['parametres']) and ($param['parametres']['marche'] != "")) {
$inner .= ' LEFT JOIN article__marche mr ON mr.id = kf.marche_id';
$where .= " AND mr.id = ".$param['parametres']['marche'];
}
if ( ! empty($param['parametres']['apporteur'])) {
$inner .= ' LEFT JOIN utilisateur__contact co ON co.id = kf.apporteur_id';
$where .= " AND co.id = ".$param['parametres']['apporteur'];
}
if (array_key_exists('domaine', $param['parametres']) and ($param['parametres']['domaine'] != "")) {
$inner .= ' LEFT JOIN projet__projet_domaine pd ON pd.fiche_id = kf.id';
$inner .= ' LEFT JOIN projet__domaine do ON pd.domaine_id = do.id';
$where .= " AND do.id = ".$param['parametres']['domaine'];
}
if (array_key_exists('activite', $param['parametres']) and ($param['parametres']['activite'] != "")) {
$inner .= ' LEFT JOIN projet__projet_activite pa ON pa.fiche_id = kf.id';
$inner .= ' LEFT JOIN projet__activite paa ON pa.activite_id = paa.id';
$where .= " AND paa.id = ".$param['parametres']['activite'];
}
if (array_key_exists('partenaire', $param['parametres']) and ($param['parametres']['partenaire'] != "")) {
$inner .= ' LEFT JOIN commerciale__commande cmd ON cmd.fiche_id = kf.id';
$inner .= ' LEFT JOIN commerciale__article_commande accmd ON accmd.commande_id = cmd.id';
$inner .= ' LEFT JOIN commerciale__statut_commande scmd ON cmd.statut_commande_id = scmd.id';
$inner .= ' LEFT JOIN commerciale__type_document_commercial tcmd ON cmd.type_document_commercial_id = tcmd.id';
$where .= " AND scmd.ordre NOT IN (0) AND tcmd.id IN (1) AND accmd.fournisseur_id = ".$param['parametres']['partenaire'];
}
$where .= " AND ";
$where .= '(kf.archive = 0 OR kf.archive IS NULL)';
$sql = 'select count(DISTINCT(kf.id)) as total
from kanban__fiche kf'
.' JOIN kanban__colonne kc ON kf.colonne_id = kc.id'
.' LEFT JOIN kanban__kanban k ON kc.kanban_id = k.id'
.' LEFT JOIN kanban__colonne kcc ON kcc.id = k.colonne_id'
.' LEFT JOIN kanban__kanban kk ON kcc.kanban_id = kk.id'
.' LEFT JOIN kanban__colonne kccc ON kccc.id = kk.colonne_id'
.$inner
.' where
kf.dateSuppression is NULL
and (kf.colonne_id = '.$colonne->getId().' OR kcc.id = '.$colonne->getId().' OR kccc.id = '.$colonne->getId().')'
.$where//.' GROUP By kf.id'
;
$em = $this->getEntityManager();
$stmt = $em->getConnection()->prepare($sql);
$res = $stmt->executeQuery()->fetchAllAssociative();
return $res[0]["total"];
}
public function getTotalBudgetColonne($in_colonne = "", $param = [], $user)
{
$param["parametres"] = $param;
$where = "";
$inner = "";
$inner .= " LEFT JOIN kanban__statut_fiche sf ON sf.id = kf.statut_fiche_id";
$where .= " AND (sf.id IS NULL OR sf.id NOT IN (3))";
if (array_key_exists('favoris', $param['parametres']) and ($param['parametres']['favoris'] == "1")) {
$inner .= "INNER JOIN kanban__favoris ON kf.id = kanban__favoris.fiche_id";
}
if (array_key_exists('client', $param['parametres']) and ($param['parametres']['client'] != "")) {
$inner .= ' LEFT JOIN client__client cli ON cli.id = kf.client_id';
$where .= " AND cli.id = ".$param['parametres']['client'];
}
if (array_key_exists('u', $param['parametres']) and count($param['parametres']['u']) > 0) {
$where .= " AND kf.utilisateur_id IN (";
$in = "";
foreach ($param['parametres']['u'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= $in.")";
}
if (array_key_exists('regions', $param["parametres"]) && $param["parametres"]["regions"] > 0) {
$where .= " AND kf.region_id = ".$param['parametres']['regions'];
}
if (is_object($user->getType()) and ($user->getType()->getId() != "1" and $user->getType()->getId() != "2")) {
/*
$where .= " AND kf.utilisateur_id IN (";
$in = "";
$in .= $user->getId().",";
$in = trim($in,",");
$where .= $in.")";
*/
$where .= " AND (kf.utilisateur_id IN (".$user->getId().") or kf.id IN (select fiche_id FROM kanban__equipe WHERE utilisateur_id IN (".$user->getId().") ) )";
} elseif (array_key_exists('filtre-utilisateurs', $param['parametres']) and count($param['parametres']['filtre-utilisateurs']) > 0) {
/*
$where .= " AND kf.utilisateur IN (:utilisateurs)";
foreach($param['parametres']['filtre-utilisateurs'] as $key=>$value) {
$parametres['utilisateurs'][]=$value;
}
*/
//$where .= " AND kf.utilisateur_id IN (";
$in = "";
foreach ($param['parametres']['filtre-utilisateurs'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
//$where .= $in.")";
$where .= " AND (kf.utilisateur_id IN (".$in.") or kf.id IN (select fiche_id FROM kanban__equipe WHERE utilisateur_id IN (".$in.") ) )";
}
if (array_key_exists('filtre-apporteurs', $param['parametres']) and count($param['parametres']['filtre-apporteurs']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-apporteurs'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND kf.apporteur_id IN (".$in.")";
}
if (array_key_exists('filtre-commerciaux', $param['parametres']) and count($param['parametres']['filtre-commerciaux']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-commerciaux'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND kf.commercial_id IN (".$in.")";
}
if (array_key_exists('filtre-chefsProjet', $param['parametres']) and count($param['parametres']['filtre-chefsProjet']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-chefsProjet'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND kf.chef_projet_id IN (".$in.")";
}
/*
if(array_key_exists('or', $param['parametres']) and ($param['parametres']['or'] != "")) {
//$query->join('p.ficheOrigine', 'fo');
//$where .= " AND fo.origine = :origine";
//$parametres['origine'] = $param['parametres']['or'];
$where .= " AND kf.id IN (select fiche_id FROM kanban__fiche_origine WHERE fiche_id = kf.id and origine_id = '".$param['parametres']['or']."' )";
}
*/
if (array_key_exists('or', $param['parametres']) and ($param['parametres']['or'] != "")) {
$where .= " AND kf.origine_commande_id = ".$param['parametres']['or'];
}
if (array_key_exists('marche', $param['parametres']) and ($param['parametres']['marche'] != "")) {
$inner .= ' LEFT JOIN article__marche mr ON mr.id = kf.marche_id';
$where .= " AND mr.id = ".$param['parametres']['marche'];
}
if ( ! empty($param['parametres']['apporteur'])) {
$inner .= ' LEFT JOIN utilisateur__contact co ON co.id = kf.apporteur_id';
$where .= " AND co.id = ".$param['parametres']['apporteur'];
}
if (array_key_exists('domaine', $param['parametres']) and ($param['parametres']['domaine'] != "")) {
$inner .= ' LEFT JOIN projet__projet_domaine pd ON pd.fiche_id = kf.id';
$inner .= ' LEFT JOIN projet__domaine do ON pd.domaine_id = do.id';
$where .= " AND do.id = ".$param['parametres']['domaine'];
}
if (array_key_exists('activite', $param['parametres']) and ($param['parametres']['activite'] != "")) {
$inner .= ' LEFT JOIN projet__projet_activite pa ON pa.fiche_id = kf.id';
$inner .= ' LEFT JOIN projet__activite paa ON pa.activite_id = paa.id';
$where .= " AND paa.id = ".$param['parametres']['activite'];
}
if (array_key_exists('partenaire', $param['parametres']) and ($param['parametres']['partenaire'] != "")) {
$inner .= ' LEFT JOIN commerciale__commande cmd ON cmd.fiche_id = kf.id';
$inner .= ' LEFT JOIN commerciale__article_commande accmd ON accmd.commande_id = cmd.id';
$inner .= ' LEFT JOIN commerciale__statut_commande scmd ON cmd.statut_commande_id = scmd.id';
$inner .= ' LEFT JOIN commerciale__type_document_commercial tcmd ON cmd.type_document_commercial_id = tcmd.id';
$where .= " AND scmd.ordre NOT IN (0) AND tcmd.id IN (1) AND accmd.fournisseur_id = ".$param['parametres']['partenaire'];
}
if (array_key_exists('filtre-fiche', $param['parametres']) and ($param['parametres']['filtre-fiche'] != "")) {
$where .= " AND kf.id = ".$param['parametres']['filtre-fiche'];
}
//if($where != "")
$where .= " AND ";
$where .= '(kf.archive = 0 OR kf.archive IS NULL)';
/*
$sql = 'select sum(budget) as total
from kanban__fiche kf
'.$inner.'
where
kf.dateSuppression is NULL
and
kf.colonne_id IN ('.$in_colonne.')
'.$where.'
';
$em = $this->getEntityManager();
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$res = $stmt->fetchAll();
if($res[0]["total"] == "") $res[0]["total"]="0";
return $res[0]["total"];
*/
if ($in_colonne != "") {
$sql = 'select kf.budget, kc.probabilite, kf.propabilite as ficheProbabilite, kcc.probabilite as colonneParenteProbabilite
from kanban__fiche kf
'.$inner
.' JOIN kanban__colonne kc ON kf.colonne_id = kc.id'
.' LEFT JOIN kanban__kanban k ON kc.kanban_id = k.id'
.' LEFT JOIN kanban__colonne kcc ON kcc.id = k.colonne_id'
.' LEFT JOIN kanban__kanban kk ON kcc.kanban_id = kk.id'
.' LEFT JOIN kanban__colonne kccc ON kccc.id = kk.colonne_id'
.' where
kf.dateSuppression is NULL'
//.' and kf.colonne_id IN ('.$in_colonne.')'
.' and (kf.colonne_id IN ('.$in_colonne.') OR kcc.id IN ('.$in_colonne.') OR kccc.id IN ('.$in_colonne.'))
'.$where
.' GROUP BY kf.id';
} else {
$sql = 'select kf.budget, kc.probabilite, kf.propabilite as ficheProbabilite
from kanban__fiche kf
'.$inner
.' JOIN kanban__colonne kc ON kf.colonne_id = kc.id'
.' where
kf.dateSuppression is NULL
'.$where
.' GROUP BY kf.id';
}
$em = $this->getEntityManager();
$stmt = $em->getConnection()->prepare($sql);
$res = $stmt->executeQuery()->fetchAllAssociative();
$total = 0;
foreach ($res as $resultat) {
if ( ! empty($resultat['ficheProbabilite'])) {
$total += floatval($resultat['ficheProbabilite']) * floatval($resultat['budget']) / 100;
} elseif ( ! empty($resultat['colonneParenteProbabilite'])) {
$total += floatval($resultat['colonneParenteProbabilite']) * floatval($resultat['budget']) / 100;
} else {
$total += floatval($resultat['probabilite']) * floatval($resultat['budget']) / 100;
}
}
return $total;
}
public function getTotalBudgetSansPourcentagesColonne($colonne, $parametres = [], $user, $chefProjet = '')
{
$param['parametres'] = $parametres;
$id = $colonne->getId();
$inner = "";
$where = 'c.id ='.$id.' AND (kf.archive = 0 OR kf.archive IS NULL)'
." AND (sf.id IS NULL OR sf.id NOT IN (3))";
if (array_key_exists('favoris', $param['parametres']) and ($param['parametres']['favoris'] == "1")) {
$inner .= " INNER JOIN kanban__favoris ON kf.id = kanban__favoris.fiche_id";
}
if (array_key_exists('client', $param['parametres']) and ($param['parametres']['client'] != "")) {
$inner .= ' LEFT JOIN client__client cli ON cli.id = kf.client_id';
$where .= " AND cli.id = ".$param['parametres']['client'];
}
if (array_key_exists('u', $param['parametres']) and count($param['parametres']['u']) > 0) {
$where .= " AND kf.utilisateur_id IN (";
$in = "";
foreach ($param['parametres']['u'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= $in.")";
}
if (is_object($user->getType()) and ($user->getType()->getId() != "1" and $user->getType()->getId() != "2")) {
/*
$where .= " AND kf.utilisateur_id IN (";
$in = "";
$in .= $user->getId().",";
$in = trim($in,",");
$where .= $in.")";
*/
$where .= " AND (kf.utilisateur_id IN (".$user->getId().") or kf.id IN (select fiche_id FROM kanban__equipe WHERE utilisateur_id IN (".$user->getId().") ) )";
} elseif (array_key_exists('filtre-utilisateurs', $param['parametres']) and count($param['parametres']['filtre-utilisateurs']) > 0) {
/*
$where .= " AND kf.utilisateur IN (:utilisateurs)";
foreach($param['parametres']['filtre-utilisateurs'] as $key=>$value) {
$parametres['utilisateurs'][]=$value;
}
*/
//$where .= " AND kf.utilisateur_id IN (";
$in = "";
foreach ($param['parametres']['filtre-utilisateurs'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
//$where .= $in.")";
$where .= " AND (kf.utilisateur_id IN (".$in.") or kf.id IN (select fiche_id FROM kanban__equipe WHERE utilisateur_id IN (".$in.") ) )";
}
if (array_key_exists('filtre-apporteurs', $param['parametres']) and count($param['parametres']['filtre-apporteurs']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-apporteurs'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND kf.apporteur_id IN (".$in.")";
}
if (array_key_exists('filtre-commerciaux', $param['parametres']) and count($param['parametres']['filtre-commerciaux']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-commerciaux'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND kf.commercial_id IN (".$in.")";
}
if (array_key_exists('filtre-chefsProjet', $param['parametres']) and count($param['parametres']['filtre-chefsProjet']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-chefsProjet'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND (kf.chef_projet_id IN (".$in.") OR (pu.id IS NOT NULL AND pu.utilisateur_id IN (".$in.") )";
if ( ! empty($chefProjet)) {
if ($where != '') {
$where .= ' and ';
}
$where .= "pu.role_id = ".$chefProjet;
}
$where .= ")";
}
if (array_key_exists('filtre-fiche', $param['parametres']) and ($param['parametres']['filtre-fiche'] != "")) {
$where .= " AND kf.id = ".$param['parametres']['filtre-fiche'];
//$parametres['fiche'] = $param['parametres']['filtre-fiche'];
}
/*
if(array_key_exists('or', $param['parametres']) and ($param['parametres']['or'] != "")) {
//$query->join('p.ficheOrigine', 'fo');
//$where .= " AND fo.origine = :origine";
//$parametres['origine'] = $param['parametres']['or'];
$where .= " AND kf.id IN (select fiche_id FROM kanban__fiche_origine WHERE fiche_id = kf.id and origine_id = '".$param['parametres']['or']."' )";
}
*/
if (array_key_exists('or', $param['parametres']) and ($param['parametres']['or'] != "")) {
$where .= " AND kf.origine_commande_id = ".$param['parametres']['or'];
}
if (array_key_exists('marche', $param['parametres']) and ($param['parametres']['marche'] != "")) {
$inner .= ' LEFT JOIN article__marche mr ON mr.id = kf.marche_id';
$where .= " AND mr.id = ".$param['parametres']['marche'];
}
if ( ! empty($param['parametres']['apporteur'])) {
$inner .= ' LEFT JOIN utilisateur__contact co ON co.id = kf.apporteur_id';
$where .= " AND co.id = ".$param['parametres']['apporteur'];
}
if (array_key_exists('domaine', $param['parametres']) and ($param['parametres']['domaine'] != "")) {
$inner .= ' LEFT JOIN projet__projet_domaine pd ON pd.fiche_id = kf.id';
$inner .= ' LEFT JOIN projet__domaine do ON pd.domaine_id = do.id';
$where .= " AND do.id = ".$param['parametres']['domaine'];
}
if (array_key_exists('activite', $param['parametres']) and ($param['parametres']['activite'] != "")) {
$inner .= ' LEFT JOIN projet__projet_activite pa ON pa.fiche_id = kf.id';
$inner .= ' LEFT JOIN projet__activite paa ON pa.activite_id = paa.id';
$where .= " AND paa.id = ".$param['parametres']['activite'];
}
if (array_key_exists('partenaire', $param['parametres']) and ($param['parametres']['partenaire'] != "")) {
$inner .= ' LEFT JOIN commerciale__commande cmd ON cmd.fiche_id = kf.id';
$inner .= ' LEFT JOIN commerciale__article_commande accmd ON accmd.commande_id = cmd.id';
$inner .= ' LEFT JOIN commerciale__statut_commande scmd ON cmd.statut_commande_id = scmd.id';
$inner .= ' LEFT JOIN commerciale__type_document_commercial tcmd ON cmd.type_document_commercial_id = tcmd.id';
$where .= " AND scmd.ordre NOT IN (0) AND tcmd.id IN (1) AND accmd.fournisseur_id = ".$param['parametres']['partenaire'];
}
$sql = 'SELECT kf.id FROM kanban__fiche as kf '
.'JOIN kanban__colonne c ON kf.colonne_id = c.id '
.'LEFT JOIN kanban__kanban k ON c.kanban_id = k.id '
//.'LEFT JOIN kanban__colonne kc ON kc.id = k.colonne_id '
//.'LEFT JOIN kanban__kanban kck ON kc.kanban_id = kck.id '
//.'LEFT JOIN kanban__colonne kckc ON kckc.id = kck.colonne_id '
."LEFT JOIN kanban__statut_fiche sf ON sf.id = kf.statut_fiche_id "
."LEFT JOIN projet__projet_utilisateur pu ON pu.fiche_id = kf.id "
//.'LEFT JOIN kanban__kanban kckck ON kckc.kanban_id = kckck.id '
//.'WHERE (c.id ='.$id.' or kc.id ='.$id.' or kckc.id ='.$id.') AND (kf.archive = 0 OR kf.archive IS NULL)'
//." AND (sf.id IS NULL OR sf.id NOT IN (3))"
.$inner
.' WHERE '.$where;
$em = $this->getEntityManager();
$repo_fiche = $em->getRepository(Fiche::class);
$stmt = $em->getConnection()->prepare($sql);
$res = $stmt->executeQuery()->fetchAllAssociative();
$total = 0;
foreach ($res as $ficheId) {
$fiche = $repo_fiche->find($ficheId);
$total += $fiche->getBudget();
}
return $total;
}
public function getRecherche($recherche, $kanban, $maxResults = 500)
{
//$recherche = "N";
$cond = "%".$recherche."%";
if (strpos($cond, "*") !== false) {
$cond = str_replace("*", "%", $cond);
}
$where = 'p.libelle LIKE :cond AND p.dateSuppression IS NULL';
$parameters = ['cond' => $cond];
if (is_object($kanban)) {
$where .= ' AND k = :kanban';
$parameters['kanban'] = $kanban->getId();
}
$query = $this->createQueryBuilder('p')
->select("CONCAT(p.libelle, ' (', COALESCE(p.probabilite, '--'), '%)') as libelle, p.id")
->join('p.kanban', 'k')
->where($where)
->setParameters($parameters)
//->orderBy('p.prenom', 'ASC');
->setFirstResult(0)
->setMaxResults($maxResults)
->orderBy('p.probabilite ASC, p.id', 'ASC');
$res = $query->getQuery()->getResult();
return $res;
}
public function getStatutFicheColonne($fiche, $colonne)
{
$em = $this->getEntityManager();
$repo_statutFiche = $em->getRepository(StatutFiche::class);
$statut = null;
if ($colonne->getId() == 11) {
$statut = $repo_statutFiche->find(1);//Gagné
} elseif ($colonne->getId() == 15) {
$statut = $repo_statutFiche->find(2);//Perdu
} elseif ($colonne->getId() == 14) {//Terminé, on garde le même statut
$statut = $fiche->getStatutFiche();
}
return $statut;
}
public function getTotalBudgetPondereColonne($colonne, $parametres = [], $user, $chefProjet = '')
{
$param['parametres'] = $parametres;
$id = $colonne->getId();
$inner = "";
$where = 'c.id ='.$id.' AND (kf.archive = 0 OR kf.archive IS NULL)'
." AND (sf.id IS NULL OR sf.id NOT IN (3))";
if (array_key_exists('favoris', $param['parametres']) and ($param['parametres']['favoris'] == "1")) {
$inner .= " INNER JOIN kanban__favoris ON kf.id = kanban__favoris.fiche_id";
}
if (array_key_exists('client', $param['parametres']) and ($param['parametres']['client'] != "")) {
$inner .= ' LEFT JOIN client__client cli ON cli.id = kf.client_id';
$where .= " AND cli.id = ".$param['parametres']['client'];
}
if (array_key_exists('u', $param['parametres']) and count($param['parametres']['u']) > 0) {
$where .= " AND kf.utilisateur_id IN (";
$in = "";
foreach ($param['parametres']['u'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= $in.")";
}
if (is_object($user->getType()) and ($user->getType()->getId() != "1" and $user->getType()->getId() != "2")) {
/*
$where .= " AND kf.utilisateur_id IN (";
$in = "";
$in .= $user->getId().",";
$in = trim($in,",");
$where .= $in.")";
*/
$where .= " AND (kf.utilisateur_id IN (".$user->getId().") or kf.id IN (select fiche_id FROM kanban__equipe WHERE utilisateur_id IN (".$user->getId().") ) )";
} elseif (array_key_exists('filtre-utilisateurs', $param['parametres']) and count($param['parametres']['filtre-utilisateurs']) > 0) {
/*
$where .= " AND kf.utilisateur IN (:utilisateurs)";
foreach($param['parametres']['filtre-utilisateurs'] as $key=>$value) {
$parametres['utilisateurs'][]=$value;
}
*/
//$where .= " AND kf.utilisateur_id IN (";
$in = "";
foreach ($param['parametres']['filtre-utilisateurs'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
//$where .= $in.")";
$where .= " AND (kf.utilisateur_id IN (".$in.") or kf.id IN (select fiche_id FROM kanban__equipe WHERE utilisateur_id IN (".$in.") ) )";
}
if (array_key_exists('filtre-apporteurs', $param['parametres']) and count($param['parametres']['filtre-apporteurs']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-apporteurs'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND kf.apporteur_id IN (".$in.")";
}
if (array_key_exists('filtre-commerciaux', $param['parametres']) and count($param['parametres']['filtre-commerciaux']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-commerciaux'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND kf.commercial_id IN (".$in.")";
}
if (array_key_exists('filtre-chefsProjet', $param['parametres']) and count($param['parametres']['filtre-chefsProjet']) > 0) {
$in = "";
foreach ($param['parametres']['filtre-chefsProjet'] as $key => $value) {
$in .= $value.",";
}
$in = trim($in, ",");
$where .= " AND (kf.chef_projet_id IN (".$in.") OR (pu.id IS NOT NULL AND pu.utilisateur_id IN (".$in.") )";
if ( ! empty($chefProjet)) {
if ($where != '') {
$where .= ' and ';
}
$where .= "pu.role_id = ".$chefProjet;
}
$where .= ")";
}
if (array_key_exists('filtre-fiche', $param['parametres']) and ($param['parametres']['filtre-fiche'] != "")) {
$where .= " AND kf.id = ".$param['parametres']['filtre-fiche'];
//$parametres['fiche'] = $param['parametres']['filtre-fiche'];
}
/*
if(array_key_exists('or', $param['parametres']) and ($param['parametres']['or'] != "")) {
//$query->join('p.ficheOrigine', 'fo');
//$where .= " AND fo.origine = :origine";
//$parametres['origine'] = $param['parametres']['or'];
$where .= " AND kf.id IN (select fiche_id FROM kanban__fiche_origine WHERE fiche_id = kf.id and origine_id = '".$param['parametres']['or']."' )";
}
*/
if (array_key_exists('or', $param['parametres']) and ($param['parametres']['or'] != "")) {
$where .= " AND kf.origine_commande_id = ".$param['parametres']['or'];
}
if (array_key_exists('marche', $param['parametres']) and ($param['parametres']['marche'] != "")) {
$inner .= ' LEFT JOIN article__marche mr ON mr.id = kf.marche_id';
$where .= " AND mr.id = ".$param['parametres']['marche'];
}
if ( ! empty($param['parametres']['apporteur'])) {
$inner .= ' LEFT JOIN utilisateur__contact co ON co.id = kf.apporteur_id';
$where .= " AND co.id = ".$param['parametres']['apporteur'];
}
if (array_key_exists('domaine', $param['parametres']) and ($param['parametres']['domaine'] != "")) {
$inner .= ' LEFT JOIN projet__projet_domaine pd ON pd.fiche_id = kf.id';
$inner .= ' LEFT JOIN projet__domaine do ON pd.domaine_id = do.id';
$where .= " AND do.id = ".$param['parametres']['domaine'];
}
if (array_key_exists('activite', $param['parametres']) and ($param['parametres']['activite'] != "")) {
$inner .= ' LEFT JOIN projet__projet_activite pa ON pa.fiche_id = kf.id';
$inner .= ' LEFT JOIN projet__activite paa ON pa.activite_id = paa.id';
$where .= " AND paa.id = ".$param['parametres']['activite'];
}
if (array_key_exists('partenaire', $param['parametres']) and ($param['parametres']['partenaire'] != "")) {
$inner .= ' LEFT JOIN commerciale__commande cmd ON cmd.fiche_id = kf.id';
$inner .= ' LEFT JOIN commerciale__article_commande accmd ON accmd.commande_id = cmd.id';
$inner .= ' LEFT JOIN commerciale__statut_commande scmd ON cmd.statut_commande_id = scmd.id';
$inner .= ' LEFT JOIN commerciale__type_document_commercial tcmd ON cmd.type_document_commercial_id = tcmd.id';
$where .= " AND scmd.ordre NOT IN (0) AND tcmd.id IN (1) AND accmd.fournisseur_id = ".$param['parametres']['partenaire'];
}
$sql = 'SELECT kf.id FROM kanban__fiche as kf '
.'JOIN kanban__colonne c ON kf.colonne_id = c.id '
.'LEFT JOIN kanban__kanban k ON c.kanban_id = k.id '
//.'LEFT JOIN kanban__colonne kc ON kc.id = k.colonne_id '
//.'LEFT JOIN kanban__kanban kck ON kc.kanban_id = kck.id '
//.'LEFT JOIN kanban__colonne kckc ON kckc.id = kck.colonne_id '
."LEFT JOIN kanban__statut_fiche sf ON sf.id = kf.statut_fiche_id "
."LEFT JOIN projet__projet_utilisateur pu ON pu.fiche_id = kf.id "
//.'LEFT JOIN kanban__kanban kckck ON kckc.kanban_id = kckck.id '
.$inner
.' WHERE '.$where;
$em = $this->getEntityManager();
$repo_fiche = $em->getRepository(Fiche::class);
$stmt = $em->getConnection()->prepare($sql);
$res = $stmt->executeQuery()->fetchAllAssociative();
$totalPondere = 0;
foreach ($res as $ficheId) {
$fiche = $repo_fiche->find($ficheId);
$totalPondere += $repo_fiche->getMontantPondereFiche($fiche);
}
return $totalPondere;
}
}