src/Repository/Articles/ArticleRepository.php line 3824

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Articles;
  3. use App\Entity\Articles\Article;
  4. use App\Entity\Articles\ConditionAchat;
  5. use App\Entity\GestionComerciale\ArticleCommande;
  6. use App\Entity\GestionComerciale\StatutCommande;
  7. use App\Model\GestionCommerciale\TypeDocumentCommercial;
  8. use DateInterval;
  9. use DateTime;
  10. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Symfony\Component\Process\ProcessBuilder;
  13. /**
  14.  * ArticleRepository
  15.  *
  16.  * This class was generated by the Doctrine ORM. Add your own custom
  17.  * repository methods below.
  18.  */
  19. class ArticleRepository extends ServiceEntityRepository
  20. {
  21.     public function __construct(ManagerRegistry $registry)
  22.     {
  23.         parent::__construct($registryArticle::class);
  24.     }
  25.     public function getArticleParReferenceV2($q)
  26.     {
  27.         $query $this->createQueryBuilder('p')
  28.             //->select("COUNT(p.id) as total")
  29.             //->leftJoin('p.articleCategorie', 'ac')
  30.                       ->leftJoin('p.conditionnementAchatEan''ca')
  31.                       ->leftJoin('p.conditionsAchat''condHa')
  32.                       ->where(
  33.                           '(
  34.         p.reference = :q
  35.         or
  36.         p.referenceAppel1 = :q
  37.         or
  38.         p.referenceAppel2 = :q
  39.         or
  40.         p.referenceAppel3 = :q
  41.         or
  42.         p.referenceAppel4 = :q
  43.         or
  44.         p.referenceAppel5 = :q
  45.         or
  46.         p.referenceAppel6 = :q
  47.         or
  48.         p.referenceAppel7 = :q
  49.         or
  50.         p.referenceAppel8 = :q
  51.         or
  52.         p.referenceAppel9 = :q
  53.         or
  54.         p.referenceAppel10 = :q
  55.         or
  56.         p.referenceAppel11 = :q
  57.         or
  58.         ca.identifiant = :q
  59.         or
  60.         condHa.referenceFournisseur = :q
  61.         )
  62.         and
  63.         (p.archive is null or p.archive = :archive)'
  64.                       )
  65.                       ->setParameters(['q' => $q'archive' => '0']);
  66.         $res   $query->getQuery()->getOneOrNullResult();
  67.         return $res;
  68.     }
  69.     public function getArticleParReference($q$parametres = ["mouvemente" => "1"])
  70.     {
  71.         $where_mouvemente " (p.mouvemente = :mouvemente or p.mouvemente is null)";
  72.         if ($parametres["mouvemente"] == "1") {
  73.             $where_mouvemente " p.mouvemente = :mouvemente";
  74.         }
  75.         $query $this->createQueryBuilder('p')
  76.             //->select("COUNT(p.id) as total")
  77.             //->leftJoin('p.articleCategorie', 'ac')
  78.                       ->leftJoin('p.conditionnementAchatEan''ca')
  79.                       ->where(
  80.                           '(
  81.                 p.reference = :q
  82.                 or
  83.                 p.unspsc = :q
  84.                 or
  85.                 p.referenceAppel1 = :q
  86.                 or
  87.                 p.referenceAppel2 = :q
  88.                 or
  89.                 p.referenceAppel3 = :q
  90.                 or
  91.                 p.referenceAppel4 = :q
  92.                 or
  93.                 p.referenceAppel5 = :q
  94.                 or
  95.                 p.referenceAppel6 = :q
  96.                 or
  97.                 p.referenceAppel7 = :q
  98.                 or
  99.                 p.referenceAppel8 = :q
  100.                 or
  101.                 p.referenceAppel9 = :q
  102.                 or
  103.                 p.referenceAppel10 = :q
  104.                 or
  105.                 p.referenceAppel11 = :q
  106.                 or
  107.                 ca.identifiant = :q
  108.                 )
  109.         and ('.$where_mouvemente.')
  110.                 and
  111.                 (p.archive is null or p.archive = :archive)'
  112.                       )
  113.                       ->setParameters(['q' => $q'archive' => '0''mouvemente' => $parametres["mouvemente"]])
  114.                       ->setMaxResults(1);
  115.         //$res = $query->getQuery()->getOneOrNullResult();
  116.         $res $query->getQuery()->getOneOrNullResult();
  117.         return $res;
  118.     }
  119.     public function testReferenceArticleExiste($reference "")
  120.     {
  121.         $sql "select count(id) as total FROM article__article where date_supression is NULL and reference = '".$reference."'";
  122.         $em   $this->getEntityManager();
  123.         $stmt $em->getConnection()->prepare($sql);
  124.         $resultats $stmt->executeQuery()->fetchAllAssociative();
  125.         return $resultats;
  126.     }
  127.     public function getXmlArticles($param = [])
  128.     {
  129.         $where            'p.mouvemente = :mouvemente and (p.divers is null or p.divers = :zero) and (p.diversComposes is null or p.diversComposes = :zero) and (p.mainOeuvre is null or p.mainOeuvre = :zero) and (p.prestation is null or p.prestation = :zero) and (p.nonStocke is null or p.nonStocke = :zero)';
  130.         $parametres_query = ['zero' => '0''mouvemente' => 1];
  131.         $where            'p.surPrestashop = :surPrestashop and (p.divers is null or p.divers = :zero) and (p.diversComposes is null or p.diversComposes = :zero) and (p.mainOeuvre is null or p.mainOeuvre = :zero) and (p.prestation is null or p.prestation = :zero) and (p.nonStocke is null or p.nonStocke = :zero)';
  132.         $parametres_query = ['zero' => '0''surPrestashop' => 1];
  133.         if (array_key_exists('id_article'$param) && $param["id_article"] != "") {
  134.             $where                          .= " and p.id = :id_article";
  135.             $parametres_query["id_article"] = $param["id_article"];
  136.         } elseif (array_key_exists('type'$param) && $param["type"] == "date") {
  137.             $date = (new DateTime())->modify('-3 hours');
  138.             $where                       .= " and (p.dateMaj >= :dateMaj OR (p.stockReserve IS NOT NULL and p.stockReserve != 0) OR p.virtuel = 1)";
  139.             $parametres_query["dateMaj"] = $date;
  140.         } elseif (array_key_exists('type'$param) && $param["type"] == "ref-appel") {
  141.         }
  142.         //echo $where;
  143.         //exit;
  144.         $query $this->createQueryBuilder('p')
  145.             //->select("COUNT(p.id) as total")
  146.                       ->leftJoin('p.marque''m')
  147.                       ->where($where)
  148.                       ->setParameters($parametres_query)
  149.                       ->setMaxResults(10000000000);
  150.         $res   $query->getQuery()->getResult();
  151.         return $res;
  152.     }
  153.     public function getArticlesId()
  154.     {
  155.         $query $this->createQueryBuilder('p')
  156.                       ->select("p.id");
  157.         $res   $query->getQuery()->getResult();
  158.         return $res;
  159.     }
  160.     public function getArticlesIdPumpNull()
  161.     {
  162.         $query $this->createQueryBuilder('p')
  163.                       ->select("p.id")
  164.                       ->where('p.pump is null or p.pump = 0 or p.pumpHorsFrais is null or p.pumpHorsFrais = 0');
  165.         $res   $query->getQuery()->getResult();
  166.         return $res;
  167.     }
  168.     public function getTotalArticleCategorie($categorie)
  169.     {
  170.         $query $this->createQueryBuilder('p')
  171.                       ->select("COUNT(p.id) as total")
  172.                       ->leftJoin('p.articleCategorie''ac')
  173.                       ->where('ac.categorie = :categorie and p.temporaire is null and (p.archive = :archive or p.archive is null)')
  174.                       ->setParameters(['categorie' => $categorie'archive' => '0']);
  175.         $res   $query->getQuery()->getOneOrNullResult();
  176.         return $res;
  177.     }
  178.     public function getTotalArticleMarque($marque)
  179.     {
  180.         $query $this->createQueryBuilder('p')
  181.                       ->select("COUNT(p.id) as total")
  182.                       ->where('p.marque = :marque and p.temporaire is null and (p.archive = :archive or p.archive is null)')
  183.                       ->setParameters(['marque' => $marque'archive' => '0']);
  184.         $res   $query->getQuery()->getOneOrNullResult();
  185.         return $res;
  186.     }
  187.     public function getRechercheArticle($recherche$maxResults 500$parametres = ["mouvemente" => "1"])
  188.     {
  189.         $cond $recherche."%";
  190.         $where_mouvemente " (p.mouvemente = :mouvemente or p.mouvemente is null)";
  191.         if ($parametres["mouvemente"] == "1") {
  192.             $where_mouvemente " p.mouvemente = :mouvemente";
  193.         }
  194.         if (strpos($cond"*") !== false) {
  195.             //$cond = str_replace("*","%",$cond);
  196.         }
  197.         $query $this->createQueryBuilder('p')
  198.             //->select("CONCAT(CONCAT(p.prenom, ' '), p.nom) as libelle, p.id")
  199.             //->where('(p.libelle LIKE :cond or p.reference LIKE :cond) and p.temporaire is null and p.archive = :archive')
  200.                       ->where(
  201.                 '(    p.referenceAppel1 LIKE :cond
  202.             or p.referenceAppel2 LIKE :cond
  203.             or p.referenceAppel3 LIKE :cond
  204.             or p.referenceAppel4 LIKE :cond
  205.             or p.referenceAppel5 LIKE :cond
  206.             or p.referenceAppel6 LIKE :cond
  207.             or p.referenceAppel7 LIKE :cond
  208.             or p.referenceAppel8 LIKE :cond
  209.             or p.referenceAppel9 LIKE :cond
  210.             or p.referenceAppel10 LIKE :cond
  211.             or p.referenceAppel11 LIKE :cond
  212.             or p.reference LIKE :cond  )
  213.             and p.temporaire is null
  214.             and ('.$where_mouvemente.')
  215.             '
  216.             )
  217.             //->leftJoin('p.conditionsAchat', 'ha')
  218.                       ->setParameters(['cond' => $cond'mouvemente' => $parametres["mouvemente"]])
  219.             //->orderBy('p.prenom', 'ASC');
  220.                       ->setFirstResult(0)
  221.                       ->setMaxResults($maxResults)
  222.                       ->orderBy('p.reference''ASC');
  223.         //dump($query->getQuery()->getSql());
  224.         $res $query->getQuery()->getResult();
  225.         return $res;
  226.     }
  227.     public function oldgetRechercheArticle($recherche$maxResults 500$parametres = ["mouvemente" => "1"])
  228.     {
  229.         $cond $recherche."%";
  230.         $where_mouvemente " (p.mouvemente = :mouvemente or p.mouvemente is null)";
  231.         if ($parametres["mouvemente"] == "1") {
  232.             $where_mouvemente " p.mouvemente = :mouvemente";
  233.         }
  234.         if (strpos($cond"*") !== false) {
  235.             //$cond = str_replace("*","%",$cond);
  236.         }
  237.         $query $this->createQueryBuilder('p')
  238.             //->select("CONCAT(CONCAT(p.prenom, ' '), p.nom) as libelle, p.id")
  239.             //->where('(p.libelle LIKE :cond or p.reference LIKE :cond) and p.temporaire is null and p.archive = :archive')
  240.                       ->where(
  241.                 '(    p.referenceAppel1 LIKE :cond
  242.                         or
  243.                         p.referenceAppel2 LIKE :cond
  244.                         or
  245.                         p.referenceAppel3 LIKE :cond
  246.                         or
  247.                         p.referenceAppel4 LIKE :cond
  248.                         or
  249.                         p.referenceAppel5 LIKE :cond
  250.                         or
  251.                         p.referenceAppel6 LIKE :cond
  252.                         or
  253.                         p.referenceAppel7 LIKE :cond
  254.                         or
  255.                         p.referenceAppel8 LIKE :cond
  256.                         or
  257.                         p.referenceAppel9 LIKE :cond
  258.                         or
  259.                         p.referenceAppel10 LIKE :cond
  260.                         or
  261.                         p.referenceAppel11 LIKE :cond
  262.                         or
  263.                         p.libelle LIKE :cond or p.reference LIKE :cond or ha.referenceFournisseur LIKE :cond or ha.libelle LIKE :cond)
  264.             and p.temporaire is null
  265.             and ('.$where_mouvemente.')
  266.             and p.parent IS NULL'
  267.             )
  268.                       ->leftJoin('p.conditionsAchat''ha')
  269.                       ->setParameters(['cond' => $cond'mouvemente' => $parametres["mouvemente"]])
  270.             //->orderBy('p.prenom', 'ASC');
  271.                       ->setFirstResult(0)
  272.                       ->setMaxResults($maxResults)
  273.                       ->orderBy('p.reference''ASC');
  274.         $res $query->getQuery()->getResult();
  275.         return $res;
  276.     }
  277.     public function _____getRechercheArticle($recherche$maxResults 500)
  278.     {
  279.         $cond $recherche."%";
  280.         if (strpos($cond"*") !== false) {
  281.             $cond str_replace("*""%"$cond);
  282.         }
  283.         $query $this->createQueryBuilder('p')
  284.             //->select("CONCAT(CONCAT(p.prenom, ' '), p.nom) as libelle, p.id")
  285.             //->where('(p.libelle LIKE :cond or p.reference LIKE :cond) and p.temporaire is null and p.archive = :archive')
  286.                       ->where('(p.libelle LIKE :cond or p.reference LIKE :cond or p.referenceWeb = :recherche) and p.temporaire is null and p.parent IS NULL')
  287.             //->setParameters(array('cond' => $cond,'archive'=>'0'))
  288.                       ->setParameters(['cond' => $cond'recherche' => $recherche])
  289.             //->orderBy('p.prenom', 'ASC');
  290.                       ->setFirstResult(0)
  291.                       ->setMaxResults($maxResults)
  292.                       ->orderBy('p.reference''ASC');
  293.         $res $query->getQuery()->getResult();
  294.         return $res;
  295.     }
  296. //$resultats = $this->getRechercheArticleSelect($recherche, $produitInterne, $fournisseur, $articlesAExclure,$stock,$reference_search,$libelle_search,$prix_search,$tri_reference,$tri_libelle,$tri_prix,$tri_stock, $categorie, 0);
  297.     public function getRechercheArticleSelect($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock ""$reference_search ""$libelle_search "",
  298.                                               $prix_search ""$tri_reference ""$tri_libelle ""$tri_prix ""$tri_stock ""$categories ""$mouvemente 1$inventaire "",
  299.                                               $declinaisons false
  300.     ) {
  301. //$echo "(".$recherche.")";
  302.         $match                   'p.reference,p.libelle,p.reference_appel_1,p.reference_appel_2,p.reference_appel_3,p.reference_appel_4,p.reference_appel_5,p.reference_appel_6,p.reference_appel_7,p.reference_appel_8,p.reference_appel_9,p.reference_appel_10,p.reference_appel_11';
  303.         $match2                  'ca.reference_fournisseur, ca.reference_fournisseur_appel';
  304.         $select                  "";
  305.         $jointureFromInventaire  '';
  306.         $jointureWhereInventaire "";
  307.         $recherche str_replace("*""%"$recherche);
  308.         $recherche str_replace("."""$recherche);
  309.         $recherche str_replace("/""\/"$recherche);
  310.         $recherche str_replace("   "" "$recherche);
  311.         $recherche str_replace("  "" "$recherche);
  312.         $recherche_ref_fourn $recherche;
  313.         //$recherche = str_replace("-"," ",$recherche);
  314.         $recherche_tmp $recherche;
  315.         $avecEtoile    false;
  316.         $pos strpos($recherche"*");
  317.         if ($pos !== false) {
  318.             $avecEtoile true;
  319.         }
  320.         //echo "<div>R(".$recherche.")</div>";
  321.         $wherePrix        "";
  322.         $tampon_recherche $recherche;
  323.         $recherche        trim($recherche);
  324.         if ( ! $avecEtoile) {
  325.             $recherche str_replace(" ""* "$recherche);
  326.         }
  327.         if ( ! $avecEtoile) {
  328.             $recherche .= "*";
  329.         }
  330.         $whereSuppl " and p.parent_id is null and (p.statut is null or p.statut = 1)";
  331.         $whereStock "";
  332.         if ($stock == 1) {
  333.             $whereStock .= " and p.stock > 0";
  334.         }
  335.         $order "";
  336.         if ($tri_reference != "") {
  337.             $order "p.reference ".$tri_reference.", ";
  338.         }
  339.         if ($tri_prix != "") {
  340.             $order "p.prix_vente ".$tri_prix.", ";
  341.         }
  342.         if ($tri_stock != "") {
  343.             $order "p.stock ".$tri_stock.", ";
  344.         }
  345.         if ($tri_libelle != "") {
  346.             $order "p.libelle ".$tri_libelle.", ";
  347.         }
  348.         if ($order == "") {
  349.             $order "p.reference ASC ";
  350.         }
  351.         $order str_replace(","""$order);
  352.         //echo "<div>order : ".$order."</div>";
  353.         $jointureFrom  "";
  354.         $jointureWhere "";
  355.         $jointureFrom .= ' LEFT JOIN article__condition_achat ca on ca.article_id = p.id ';
  356.         if ( ! ($fournisseur == null || $fournisseur == '')) {
  357.             $match2       'ca.reference_fournisseur, ca.reference_fournisseur_appel';
  358.             $jointureFrom .= " INNER JOIN article__condition_achat as ca on ca.article_id = p.id";
  359.             //$jointureWhere .= " and (p.non_commandable = 0 or p.non_commandable = 1 or p.non_commandable is null)";
  360.             $jointureWhere .= "(p.id IN (select
  361.                     ca.article_id
  362.                     FROM article__condition_achat as ca
  363.                     WHERE
  364.                     ca.fournisseur_id = ".$fournisseur." )
  365.                     OR p.divers=1 ";
  366.             if ($categories != '') {
  367.                 $jointureFrom .= ' LEFT JOIN article__article_categorie acat on acat.article_id = p.id LEFT JOIN article__categorie as cat on cat.id = acat.categorie_id';
  368.                 foreach ($categories as $categorie) {
  369.                     $jointureWhere .= " OR cat.libelle LIKE '".$categorie."'";
  370.                 }
  371.             }
  372.             $jointureWhere .= ")";
  373.             $whereSuppl .= " and (p.arret_gamme != 1 or p.arret_gamme IS NULL)";
  374.         }
  375.         if (($fournisseur == null || $fournisseur == '') && $categories != '') {
  376.             $jointureFrom .= ' LEFT JOIN article__article_categorie acat on acat.article_id = p.id JOIN article__categorie as cat on cat.id = acat.categorie_id';
  377.             //$jointureWhere .= " and cat.libelle LIKE '".$categorie."%'";
  378.             foreach ($categories as $key => $categorie) {
  379.                 if ($key == 0) {
  380.                     $jointureWhere .= " and (";
  381.                 } else {
  382.                     $jointureWhere .= " OR";
  383.                 }
  384.                 $jointureWhere .= " cat.libelle LIKE '".$categorie."'";
  385.             }
  386.             $jointureWhere .= ")";
  387.         }
  388.         if ($produitInterne == true || $produitInterne == 'true') {
  389.             $whereSuppl .= ' and p.produit_interne =1 and (p.virtuel = 0 or p.virtuel is null)';
  390.         }
  391.         if (count($articlesAExclure) > 0) {
  392.             $whereSuppl .= " and p.id NOT IN ( '".implode($articlesAExclure"', '")."' )";// != :articleAExclure and p.id != :articleAExclure
  393.         }
  394.         /*
  395.         if ($mouvemente == "")
  396.             $whereSuppl .= " and p.divers = 1";
  397.         else
  398.             $whereSuppl .= " and (p.mouvemente = ".$mouvemente." or p.divers = 1)";
  399.          *
  400.          */
  401.         if ($mouvemente != "0" && $mouvemente !== null or ($mouvemente == "")) {
  402.             $mouvemente 1;
  403.             $whereSuppl .= " and (p.mouvemente = ".$mouvemente." or p.divers = 1)";
  404.         } else {
  405.             //echo "titit (".$mouvemente.")";
  406.             $whereSuppl .= " and (p.mouvemente = ".$mouvemente." or p.mouvemente =1 or p.mouvemente is null or p.divers = 1)";
  407.         }
  408.         //echo 'mouvemente : '.$mouvemente;
  409.         if ( ! $avecEtoile) {
  410.             //$recherche = str_replace("-", "", $recherche);
  411.         }
  412.         if ($inventaire != '') {
  413.             //$select .= ', count(ai.id) as nbArticleInventaire';
  414.             $select                 .= ', COUNT(CASE WHEN ai.inventaire_id = '.$inventaire.' then 1 ELSE NULL END) as "nbArticleInventaire"';
  415.             $jointureFromInventaire .= ' LEFT JOIN inventaire__article_inventaire ai ON ai.article_id = p.id';
  416.             //$jointureWhereInventaire .= ' and (ai.inventaire_id = '.$inventaire.' OR ai.inventaire_id IS NULL)';
  417.         }
  418.         //echo "<div>S : ".$recherche."</div>";
  419.         if ($recherche != "" and $recherche != "*") {
  420.             if ($prix_search == 1) {
  421.                 $sql "
  422.             select
  423.           p.libelle_secondaire,
  424.           p.reference_appel_1,
  425.           p.reference_appel_2,
  426.           p.reference_appel_3,
  427.           p.reference_appel_4,
  428.           p.reference_appel_5,
  429.           p.reference_appel_6,
  430.           p.reference_appel_7,
  431.           p.reference_appel_8,
  432.           p.reference_appel_9,
  433.           p.reference_appel_10,
  434.           p.reference_appel_11,
  435.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  436.             p.virtuel,
  437.             p.arret_gamme,
  438.             p.article_remplacement_id,
  439.             p.libelle,
  440.             p.id,
  441.             p.prix_vente as prixVente,
  442.             p.prix_base as prixAchat,
  443.             p.poids,
  444.             p.stock,
  445.             p.stock_reserve_fabrication,
  446.             p.stock_reserve,
  447.             p.reference,
  448.             p.pump,
  449.             p.cpump,
  450.                 MATCH (".$match.") AGAINST ('".$recherche."' IN BOOLEAN MODE) as ponderation"
  451.                        .$select
  452.                        ." FROM article__article as p"
  453.                        .$jointureFromInventaire
  454.                        ." WHERE
  455.             p.temporaire is null
  456.             and
  457.             (p.archive = 0 or p.archive is null)
  458.             and
  459.             p.prix_vente like '".$tampon_recherche."%'
  460.             ".$whereStock."
  461.             ".$whereSuppl.$jointureWhereInventaire."
  462.             GROUP BY p.id
  463.                     order by ".$order.", p.id desc
  464.             LIMIT 0,100
  465.             ";
  466.                 //print_r($sql);
  467.             } else {
  468.                 /*
  469.                  p.reference LIKE '".$recherche."'
  470.                         OR
  471.                         p.libelle LIKE '".$recherche."'
  472.                         OR
  473.                         reference_appel_1 LIKE '".$recherche."'
  474.                         OR
  475.                         reference_appel_2 LIKE '".$recherche."'
  476.                         OR
  477.                         reference_appel_3 LIKE '".$recherche."'
  478.                         OR
  479.                         reference_appel_4 LIKE '".$recherche."'
  480.                         OR
  481.                         reference_appel_5 LIKE '".$recherche."'
  482.                         OR
  483.                         reference_appel_6 LIKE '".$recherche."'
  484.                         OR
  485.                         reference_appel_7 LIKE '".$recherche."'
  486.                         OR
  487.                         reference_appel_8 LIKE '".$recherche."'
  488.                         OR
  489.                         reference_appel_9 LIKE '".$recherche."'
  490.                         OR
  491.                         reference_appel_10 LIKE '".$recherche."'
  492.                         OR
  493.                         reference_appel_11 LIKE '".$recherche."'
  494.                 */
  495.                 if ($avecEtoile) {
  496.                     if ($order == "") {
  497.                         $order "p.reference";
  498.                     }
  499.                     $order     str_replace(","""$order);
  500.                     $recherche .= "%";
  501.                     $sql       "
  502.                 select
  503.                  p.libelle_secondaire,
  504.             p.reference_appel_1,
  505.             p.reference_appel_2,
  506.             p.reference_appel_3,
  507.             p.reference_appel_4,
  508.             p.reference_appel_5,
  509.             p.reference_appel_6,
  510.             p.reference_appel_7,
  511.             p.reference_appel_8,
  512.             p.reference_appel_9,
  513.             p.reference_appel_10,
  514.             p.reference_appel_11,
  515.                 CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  516.                 p.arret_gamme,
  517.                 p.virtuel,
  518.                 p.article_remplacement_id,
  519.                 p.libelle,
  520.                 p.id,
  521.                 p.prix_vente as prixVente,
  522.                 p.prix_base as prixAchat,
  523.                 p.poids,
  524.                 p.stock,
  525.                 p.stock_reserve_fabrication,
  526.                 p.stock_reserve,
  527.                 p.pump,
  528.                 p.cpump,
  529.                 p.reference"
  530.                                  .$select
  531.                                  ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  532.                 WHERE
  533.                 p.temporaire is null
  534.                 and
  535.                 (p.archive = 0 or p.archive is null) ".$whereSuppl."
  536.                 and
  537.                 (
  538.                     p.reference LIKE '".$recherche."'
  539.                     OR
  540.                     p.libelle LIKE '".$recherche."'
  541.                     OR
  542.                     p.reference_appel_1 LIKE '".$recherche."'
  543.                     OR
  544.                     p.reference_appel_2 LIKE '".$recherche."'
  545.                     OR
  546.                     p.reference_appel_3 LIKE '".$recherche."'
  547.                     OR
  548.                     p.reference_appel_4 LIKE '".$recherche."'
  549.                     OR
  550.                     p.reference_appel_5 LIKE '".$recherche."'
  551.                     OR
  552.                     p.reference_appel_6 LIKE '".$recherche."'
  553.                     OR
  554.                     p.reference_appel_7 LIKE '".$recherche."'
  555.                     OR
  556.                     p.reference_appel_8 LIKE '".$recherche."'
  557.                     OR
  558.                     p.reference_appel_9 LIKE '".$recherche."'
  559.                     OR
  560.                     p.reference_appel_10 LIKE '".$recherche."'
  561.                     OR
  562.                     p.reference_appel_11 LIKE '".$recherche."'";
  563.                     if ($match2 != "") {
  564.                         $sql .= "OR ca.reference_fournisseur LIKE '".$recherche."'";
  565.                         $sql .= "OR ca.reference_fournisseur_appel LIKE '".$recherche."'";
  566.                     }
  567.                     $sql .= "   )
  568.                 ".$wherePrix.$whereStock.$jointureWhere.$jointureWhereInventaire."
  569.                 GROUP BY p.id
  570.                             order by ".$order."
  571.                 LIMIT 0,100
  572.                 ";
  573.                     //echo $sql;
  574.                     //exit;
  575.                 } else {
  576.                     //echo "OK";
  577.                     //exit;
  578.                     $recherche_contruit "";
  579.                     $recherche_tab      explode(" "$recherche);
  580.                     //print_r($recherche_tab);
  581.                     for ($r 0$r count($recherche_tab); $r++) {
  582.                         //$recherche_contruit .="+".$recherche_tab[$r]." ";
  583.                         $recherche_tab[$r]  = str_replace("+""#+"$recherche_tab[$r]);
  584.                         $recherche_contruit .= "+".$recherche_tab[$r]." ";
  585.                     }
  586.                     //echo "<div>SUPPL ".$whereSuppl."</div>";
  587.                     $recherche trim($recherche"*");
  588.                     $sql "
  589.         select
  590.         p.dateProchaineDispo,
  591.         p.reference_appel_1,
  592.          p.libelle_secondaire,
  593.         p.reference_appel_2,
  594.         p.reference_appel_3,
  595.         p.reference_appel_4,
  596.         p.reference_appel_5,
  597.         p.reference_appel_6,
  598.         p.reference_appel_7,
  599.         p.reference_appel_8,
  600.         p.reference_appel_9,
  601.         p.reference_appel_10,
  602.         p.reference_appel_11,
  603.         p.stock_reserve_fabrication,
  604.         p.virtuel,
  605.         CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  606.         p.article_remplacement_id,
  607.         p.libelle,
  608.         p.id,
  609.         p.prix_vente as prixVente,
  610.         p.prix_base as prixAchat,
  611.         p.poids,
  612.         p.stock,
  613.         p.stock_reserve,
  614.         p.reference,
  615.         p.pump,
  616.         p.cpump,
  617.         p.arret_gamme,
  618.         marque.libelle as libelle_marque,
  619.         marque.id as id_marque,
  620.         marque.logo as logo_marque,
  621.                             MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation"
  622.                            .$select
  623.                            ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  624.                            LEFT JOIN article__marque as marque ON marque.id = p.marque_id
  625.                             WHERE ";
  626.                             if ($jointureWhere != "") {
  627.                             $sql .= $jointureWhere." and ";
  628.                             }
  629.                             $sql .= " p.temporaire is null
  630.                             and ( marque.statut = 1 or marque.id is null) and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  631.                             and
  632.                             
  633.                             (MATCH (".$match.")
  634.                             AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)";
  635.                     if ($match2 != "" and != 1) {
  636.                         $sql .= "OR ca.reference_fournisseur LIKE '".$recherche_ref_fourn."%'";
  637.                         $sql .= "OR ca.reference_fournisseur_appel LIKE '".$recherche_ref_fourn."%'";
  638.                     }
  639.                     // if($match2 != "") $sql .= " OR MATCH (".$match2.") AGAINST ('".$recherche_ref_fourn."' IN BOOLEAN MODE) as ponderation2";
  640.                     $sql .= "";
  641.                     $sql .= $wherePrix.$whereStock;
  642.                     $sql .= " OR (p.reference LIKE '%".$recherche."%') ";
  643.                     $sql .= ")";
  644.                     $sql .= $jointureWhereInventaire;
  645.                     $sql .= " GROUP BY p.id
  646.         order by ".$order.", ponderation desc
  647.         LIMIT 0,100
  648.         ";
  649.                    // echo $sql;
  650.                     /*
  651.                     $jointureFrom = "";
  652.                     $sql = "
  653.                     select
  654.                     p.stock_reserve_fabrication,
  655.                     p.virtuel,
  656.                     CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  657.                     p.article_remplacement_id,
  658.                     p.libelle,
  659.                     p.id,
  660.                     p.prix_vente as prixVente,
  661.                     p.prix_base as prixAchat,
  662.                     p.poids,
  663.                     p.stock,
  664.                     p.stock_reserve,
  665.                     p.reference,
  666.                     p.pump,
  667.                     p.cpump,
  668.                     p.arret_gamme
  669.                     "
  670.                     .$select
  671.                     ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  672.                     WHERE ";
  673.                     if($jointureWhere != "") $sql .= $jointureWhere." and ";
  674.                     $sql .= " p.temporaire is null
  675.                     and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  676.                     and
  677.                     p.reference LIKE '".$recherche_tmp."%'
  678.                     ";
  679.                     if($match2 != "" and 1!=1) {
  680.                       $sql .= "OR ca.reference_fournisseur LIKE '".$recherche_ref_fourn."%'";
  681.                       $sql .= "OR ca.reference_fournisseur_appel LIKE '".$recherche_ref_fourn."%'";
  682.                     }
  683.                     // if($match2 != "") $sql .= " OR MATCH (".$match2.") AGAINST ('".$recherche_ref_fourn."' IN BOOLEAN MODE) ";
  684.                     $sql .= "";
  685.                     $sql .= $wherePrix.$whereStock;
  686.                     //$sql .= ")";
  687.                     $sql .= $jointureWhereInventaire;
  688.                     $sql .= " GROUP BY p.id
  689.                     order by ".$order."
  690.                     LIMIT 0,100
  691.                     ";
  692.                     */
  693.                     //echo $sql;
  694.                     //exit;
  695.                 }
  696.             }
  697.             //echo "ORDER(".$order.")";
  698.         } else {
  699.             //$order .= "p.reference asc";
  700.             $sql "
  701.             select
  702.           p.reference_appel_1,
  703.            p.libelle_secondaire,
  704.           p.reference_appel_2,
  705.           p.reference_appel_3,
  706.           p.reference_appel_4,
  707.           p.reference_appel_5,
  708.           p.reference_appel_6,
  709.           p.reference_appel_7,
  710.           p.reference_appel_8,
  711.           p.reference_appel_9,
  712.           p.reference_appel_10,
  713.           p.reference_appel_11,
  714.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  715.             p.arret_gamme,
  716.             p.virtuel,
  717.             p.article_remplacement_id,
  718.             p.virtuel,
  719.             p.libelle,
  720.             p.id,
  721.             p.prix_vente as prixVente,
  722.             p.prix_base as prixAchat,
  723.             p.poids,
  724.             p.stock,
  725.             p.stock_reserve_fabrication,
  726.             p.stock_reserve,
  727.             p.cpump,
  728.             p.reference"
  729.                    .$select
  730.                    ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  731.             WHERE ";
  732.             if ($jointureWhere != "") {
  733.                 $sql .= $jointureWhere." and ";
  734.             }
  735.             $sql .= " p.temporaire is null
  736.             and
  737.             (p.archive = 0 or p.archive is null)
  738.                         ".$whereSuppl.$whereStock.$jointureWhereInventaire."
  739.                             GROUP BY p.id
  740.             order by ".$order."
  741.             LIMIT 0,100
  742.             ";
  743.         }
  744.         //where p.libelle like '%".$recherche."%' or p.reference like '%".$recherche."%'
  745.         //echo $sql;
  746.         //exit;
  747.         $em   $this->getEntityManager();
  748.         $stmt $em->getConnection()->prepare($sql);
  749.         $resultats $stmt->executeQuery()->fetchAllAssociative();
  750.         //$resultats = array();
  751.         //echo $sql;
  752.         //exit;
  753.         //echo "<div>------(".$mouvemente.")</div>";
  754.         //Si la recherche dans les articles mouvementés ne donne rien, on effectue la requete sur les articles non mouvementés
  755.         if (count($resultats) == && $mouvemente == "1") {
  756.             //$mouvemente =0;
  757.             /*if(!$avecEtoile) {
  758.                 $recherche = str_replace("*","",$recherche);
  759.             }
  760.             */
  761.             //echo "<div>ototo ".$recherche."</div>";
  762.             //echo "--".$mouvemente;
  763.             //exit;
  764.             $resultats $this->getRechercheArticleSelect(
  765.                 $recherche_tmp,
  766.                 $produitInterne,
  767.                 $fournisseur,
  768.                 $articlesAExclure,
  769.                 $stock,
  770.                 $reference_search,
  771.                 $libelle_search,
  772.                 $prix_search,
  773.                 $tri_reference,
  774.                 $tri_libelle,
  775.                 $tri_prix,
  776.                 $tri_stock,
  777.                 $categories,
  778.                 "0",
  779.                 $inventaire
  780.             );
  781.             //exit;
  782.         }
  783.         //print_r($resultats);
  784.         //exit;
  785.         return $resultats;
  786.     }
  787.     public function getRechercheArticleSelect2($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock ""$reference_search ""$libelle_search "",
  788.                                                $prix_search ""$tri_reference ""$tri_libelle ""$tri_prix ""$tri_stock ""$categories ""$mouvemente 1$inventaire ''
  789.     ) {
  790.         $sql2                    "";
  791.         $sql3                    "";
  792.         $match                   'p.reference,p.libelle,p.reference_appel_1,p.reference_appel_2,p.reference_appel_3,p.reference_appel_4,p.reference_appel_5,p.reference_appel_6,p.reference_appel_7,p.reference_appel_8,p.reference_appel_9,p.reference_appel_10,p.reference_appel_11';
  793.         $match2                  "";
  794.         $select                  '';
  795.         $jointureFromInventaire  "";
  796.         $jointureWhereInventaire "";
  797.         $recherche str_replace("*""%"$recherche);
  798.         //$recherche = str_replace("-"," ",$recherche);
  799.         $recherche str_replace("."""$recherche);
  800.         $recherche str_replace("/""\/"$recherche);
  801.         $recherche str_replace("   "" "$recherche);
  802.         $recherche_tmp $recherche;
  803.         $avecEtoile    false;
  804.         $pos strpos($recherche"*");
  805.         if ($pos !== false) {
  806.             $avecEtoile true;
  807.         }
  808.         $recherche str_replace("*""%"$recherche);
  809.         $wherePrix        "";
  810.         $tampon_recherche $recherche;
  811.         $recherche        trim($recherche);
  812.         if ( ! $avecEtoile) {
  813.             $recherche str_replace(" ""* "$recherche);
  814.         }
  815.         if ( ! $avecEtoile) {
  816.             $recherche .= "*";
  817.         }
  818.         $whereSuppl " and p.parent_id is null and (p.statut = 1 or p.statut is null) ";
  819.         $whereStock "";
  820.         if ($stock == 1) {
  821.             $whereStock .= " and p.stock > 0";
  822.         }
  823.         $order "";
  824.         if ($tri_reference != "") {
  825.             $order "p.reference ".$tri_reference.", ";
  826.         }
  827.         if ($tri_prix != "") {
  828.             $order "p.prix_vente ".$tri_prix.", ";
  829.         }
  830.         if ($tri_stock != "") {
  831.             $order "p.stock ".$tri_stock.", ";
  832.         }
  833.         if ($tri_libelle != "") {
  834.             $order "p.libelle ".$tri_libelle.", ";
  835.         }
  836.         if ($order == "") {
  837.             $order "p.reference ASC, ";
  838.         }
  839.         //echo "<div>order : ".$order."</div>";
  840.         $jointureFrom   "";
  841.         $jointureWhere  "";
  842.         $jointureWhere2 "";
  843.         if ( ! ($fournisseur == null || $fournisseur == '')) {
  844.             $match2 'ca.reference_fournisseur, ca.reference_fournisseur_appel';
  845.             if ($categories != '') {
  846.                 $selectTemp "(select id FROM article__categorie cat WHERE ";
  847.                 foreach ($categories as $key => $categorie) {
  848.                     if ($key == 0) {
  849.                         $selectTemp .= " ";
  850.                     } else {
  851.                         $selectTemp .= " OR";
  852.                     }
  853.                     $selectTemp .= " cat.reference LIKE '".$categorie."'";
  854.                 }
  855.                 $selectTemp .= ") as tempAC ";
  856.                 $jointureFrom .= "LEFT JOIN ".$selectTemp." LEFT JOIN article__article_categorie as aac ON aac.categorie_id = tempAC.id";
  857.             }
  858.             $whereSuppl .= " and (p.arret_gamme != 1 or p.arret_gamme IS NULL)";
  859.         }
  860.         if ($produitInterne == true || $produitInterne == 'true') {
  861.             $whereSuppl .= ' and p.produit_interne =1';
  862.         }
  863.         if (count($articlesAExclure) > 0) {
  864.             $whereSuppl .= " and p.id NOT IN ( '".implode($articlesAExclure"', '")."' )";// != :articleAExclure and p.id != :articleAExclure
  865.         }
  866.         if ($mouvemente != "0" && $mouvemente !== null or ($mouvemente == "")) {
  867.             $whereSuppl .= " and (p.mouvemente = 1 or p.divers = 1)";
  868.         } else {
  869.             //$whereSuppl .= " and p.divers = 1";
  870.         }
  871.         if ( ! $avecEtoile) {
  872.             $recherche str_replace("-"""$recherche);
  873.         }
  874.         if ($inventaire != '') {
  875.             //$select .= ', count(ai.id) as nbArticleInventaire';
  876.             $select                 .= ', COUNT(CASE WHEN ai.inventaire_id = '.$inventaire.' then 1 ELSE NULL END) as "nbArticleInventaire"';
  877.             $jointureFromInventaire .= ' LEFT JOIN inventaire__article_inventaire ai ON ai.article_id = p.id';
  878.             //$jointureWhereInventaire .= ' and (ai.inventaire_id = '.$inventaire.' OR ai.inventaire_id IS NULL)';
  879.         }
  880.         if ($recherche != "" and $recherche != "*") {
  881.             if ($prix_search == 1) {
  882.                 $sql "
  883.             select
  884.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  885.           p.reference_appel_1,
  886.           p.reference_appel_2,
  887.           p.reference_appel_3,
  888.           p.reference_appel_4,
  889.           p.reference_appel_5,
  890.           p.reference_appel_6,
  891.           p.reference_appel_7,
  892.           p.reference_appel_8,
  893.           p.reference_appel_9,
  894.           p.reference_appel_10,
  895.           p.reference_appel_11,
  896.             p.arret_gamme,
  897.             p.virtuel,
  898.                 p.divers,
  899.             p.article_remplacement_id,
  900.             p.libelle,
  901.             p.id,
  902.             p.reference_fournisseur,
  903.             p.prix_vente as prixVente,
  904.             p.prix_base as prixAchat,
  905.             p.poids,
  906.             p.stock,
  907.             p.stock_reserve_fabrication,
  908.             p.stock_reserve,
  909.             p.reference,
  910.             p.pump,
  911.             p.cpump,
  912.             MATCH (".$match.") AGAINST ('".$recherche."' IN BOOLEAN MODE) as ponderation"
  913.                        .$select
  914.                        ." FROM article__article as p"
  915.                        .$jointureFromInventaire
  916.                        ." WHERE
  917.             p.temporaire is null
  918.             and
  919.             (p.archive = 0 or p.archive is null)
  920.             and
  921.             p.prix_vente like '".$tampon_recherche."%'
  922.             ".$whereStock."
  923.             ".$whereSuppl.$jointureWhereInventaire."
  924.             GROUP BY p.id
  925.                     order by ".$order." p.id desc
  926.             LIMIT 0,100
  927.             ";
  928.             } else {
  929.                 /*
  930.                  p.reference LIKE '".$recherche."'
  931.                         OR
  932.                         p.libelle LIKE '".$recherche."'
  933.                         OR
  934.                         reference_appel_1 LIKE '".$recherche."'
  935.                         OR
  936.                         reference_appel_2 LIKE '".$recherche."'
  937.                         OR
  938.                         reference_appel_3 LIKE '".$recherche."'
  939.                         OR
  940.                         reference_appel_4 LIKE '".$recherche."'
  941.                         OR
  942.                         reference_appel_5 LIKE '".$recherche."'
  943.                         OR
  944.                         reference_appel_6 LIKE '".$recherche."'
  945.                         OR
  946.                         reference_appel_7 LIKE '".$recherche."'
  947.                         OR
  948.                         reference_appel_8 LIKE '".$recherche."'
  949.                         OR
  950.                         reference_appel_9 LIKE '".$recherche."'
  951.                         OR
  952.                         reference_appel_10 LIKE '".$recherche."'
  953.                         OR
  954.                         reference_appel_11 LIKE '".$recherche."'
  955.                 */
  956.                 if ($avecEtoile) {
  957.                     if ($order == "") {
  958.                         $order "p.reference";
  959.                     }
  960.                     $order     str_replace(","""$order);
  961.                     $recherche .= "%";
  962.                     $sql       "
  963.                 select
  964.                 CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  965.             p.reference_appel_1,
  966.             p.reference_appel_2,
  967.             p.reference_appel_3,
  968.             p.reference_appel_4,
  969.             p.reference_appel_5,
  970.             p.reference_appel_6,
  971.             p.reference_appel_7,
  972.             p.reference_appel_8,
  973.             p.reference_appel_9,
  974.             p.reference_appel_10,
  975.             p.reference_appel_11,
  976.                 p.arret_gamme,
  977.                 p.virtuel,
  978.                         p.divers,
  979.                 p.article_remplacement_id,
  980.                 p.libelle,
  981.                 p.id,
  982.                 p.reference_fournisseur,
  983.                 p.prix_vente as prixVente,
  984.                 p.prix_base as prixAchat,
  985.                 p.poids,
  986.                 p.stock,
  987.                 p.stock_reserve_fabrication,
  988.                 p.stock_reserve,
  989.                 p.pump,
  990.                 p.cpump,
  991.                 p.reference"
  992.                                  .$select
  993.                                  ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  994.                 WHERE
  995.                 p.temporaire is null
  996.                 and
  997.                 (p.archive = 0 or p.archive is null) ".$whereSuppl."
  998.                 and
  999.                 (
  1000.                     p.reference LIKE '".$recherche."'
  1001.                     OR
  1002.                     p.libelle LIKE '".$recherche."'
  1003.                     OR
  1004.                     p.reference_appel_1 LIKE '".$recherche."'
  1005.                     OR
  1006.                     p.reference_appel_2 LIKE '".$recherche."'
  1007.                     OR
  1008.                     p.reference_appel_3 LIKE '".$recherche."'
  1009.                     OR
  1010.                     p.reference_appel_4 LIKE '".$recherche."'
  1011.                     OR
  1012.                     p.reference_appel_5 LIKE '".$recherche."'
  1013.                     OR
  1014.                     p.reference_appel_6 LIKE '".$recherche."'
  1015.                     OR
  1016.                     p.reference_appel_7 LIKE '".$recherche."'
  1017.                     OR
  1018.                     p.reference_appel_8 LIKE '".$recherche."'
  1019.                     OR
  1020.                     p.reference_appel_9 LIKE '".$recherche."'
  1021.                     OR
  1022.                     p.reference_appel_10 LIKE '".$recherche."'
  1023.                     OR
  1024.                     p.reference_appel_11 LIKE '".$recherche."'";
  1025.                     if ($match2 != "") {
  1026.                         $sql .= "OR ca.reference_fournisseur LIKE '".$recherche."'";
  1027.                         $sql .= "OR ca.reference_fournisseur_appel LIKE '".$recherche."'";
  1028.                     }
  1029.                     $sql .= "   )
  1030.                 ".$wherePrix.$whereStock.$jointureWhere.$jointureWhereInventaire."
  1031.                 GROUP BY p.id
  1032.                             order by ".$order."
  1033.                 LIMIT 0,100
  1034.                 ";
  1035.                     //echo $sql;
  1036.                     //exit;
  1037.                 } else {
  1038.                     $recherche_contruit "";
  1039.                     $recherche_tab      explode(" "$recherche);
  1040.                     for ($r 0$r count($recherche_tab); $r++) {
  1041.                         //$recherche_contruit .="+".$recherche_tab[$r]." ";
  1042.                         $recherche_tab[$r]  = str_replace("+""#+"$recherche_tab[$r]);
  1043.                         $recherche_contruit .= "+".$recherche_tab[$r]." ";
  1044.                     }
  1045.                     //echo "<div>SUPPL ".$whereSuppl."</div>";
  1046.                     /*
  1047.             $sql = "
  1048.             select
  1049.             p.stock_reserve_fabrication,
  1050.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1051.             p.article_remplacement_id,
  1052.             p.libelle,
  1053.             p.id,
  1054.             p.prix_vente as prixVente,
  1055.             p.prix_base as prixAchat,
  1056.             p.poids,
  1057.             p.stock,
  1058.             p.stock_reserve,
  1059.             p.reference,
  1060.             p.pump,
  1061.             p.arret_gamme,
  1062.             MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation
  1063.             FROM article__article as p ".$jointureFrom."
  1064.             WHERE ";
  1065.                         if($jointureWhere != "") $sql .= $jointureWhere." and ";
  1066.             $sql .= " p.temporaire is null
  1067.             and p.archive = 0".$whereSuppl."
  1068.             and
  1069.             (MATCH (".$match.")
  1070.             AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)";
  1071.              if($match2 != "") $sql .= " OR MATCH (".$match2.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) ";
  1072.             $sql .= "";
  1073.             $sql .= $wherePrix.$whereStock;
  1074.                     $sql .= ")
  1075.             GROUP BY p.id
  1076.                         order by ".$order.", ponderation desc
  1077.             LIMIT 0,100
  1078.             ";
  1079.             //echo $sql;
  1080.         }
  1081.         */
  1082.                     //pre select on condition achat before match
  1083.                     if ($match2 != '') {
  1084.                         $sql "select
  1085.                             p.libelle_secondaire,
  1086.                             p.reference_appel_1,
  1087.                             p.reference_appel_2,
  1088.                             p.reference_appel_3,
  1089.                             p.reference_appel_4,
  1090.                             p.reference_appel_5,
  1091.                             p.reference_appel_6,
  1092.                             p.reference_appel_7,
  1093.                             p.reference_appel_8,
  1094.                             p.reference_appel_9,
  1095.                             p.reference_appel_10,
  1096.                             p.reference_appel_11,
  1097.                                           temp.reference_fournisseur as reference_fournisseur,
  1098.                             p.stock_reserve_fabrication,
  1099.                             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1100.                             p.article_remplacement_id,
  1101.                             p.libelle,
  1102.                             p.virtuel,
  1103.                             p.divers,
  1104.                             p.id,
  1105.                             p.reference_fournisseur as old,
  1106.                             p.prix_vente as prixVente,
  1107.                             p.prix_base as prixAchat,
  1108.                             p.poids,
  1109.                             p.stock,
  1110.                             p.stock_reserve,
  1111.                             p.reference,
  1112.                             p.pump,
  1113.                             p.cpump,
  1114.                             p.arret_gamme,
  1115.                             MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation";
  1116.                         $sql .= $select;
  1117.                         $sql .= " FROM (select
  1118.                     *
  1119.                     FROM article__condition_achat
  1120.                     WHERE
  1121.                     date_supression is null and
  1122.                     fournisseur_id = ".$fournisseur." ) AS temp ";
  1123.                         $sql .= " LEFT JOIN article__article as p ON temp.article_id = p.id";
  1124.                         $sql .= $jointureFromInventaire
  1125.                                 //. " LEFT JOIN article__condition_achat ca ON ca.id = p.id"
  1126.                                 ." WHERE ";
  1127.                         if ($jointureWhere2 != "") {
  1128.                             $sql .= $jointureWhere2." and ";
  1129.                         }
  1130.                         $sql .= " p.temporaire is null
  1131.                 and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1132.                 and
  1133.                 (MATCH (".$match.")
  1134.                 AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)"
  1135.                                 ." OR MATCH (temp.reference_fournisseur, temp.reference_fournisseur_appel) AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) ";
  1136.                         $sql .= $wherePrix.$whereStock;
  1137.                         $sql .= ")";
  1138.                         $sql .= $jointureWhereInventaire;
  1139.                         $sql .= " GROUP BY p.id
  1140.                             order by ".$order." ponderation desc
  1141.                 LIMIT 0,100
  1142.                 ";
  1143.                         $sql2 "select
  1144.                             p.stock_reserve_fabrication,
  1145.                             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1146.                             p.reference_appel_1,
  1147.                             p.libelle_secondaire,
  1148.                             p.reference_appel_2,
  1149.                             p.reference_appel_3,
  1150.                             p.reference_appel_4,
  1151.                             p.reference_appel_5,
  1152.                             p.reference_appel_6,
  1153.                             p.reference_appel_7,
  1154.                             p.reference_appel_8,
  1155.                             p.reference_appel_9,
  1156.                             p.reference_appel_10,
  1157.                             p.reference_appel_11,
  1158.                             p.article_remplacement_id,
  1159.                             p.libelle,
  1160.                             p.virtuel,
  1161.                             p.divers,
  1162.                             p.id,
  1163.                                           p.reference_fournisseur,
  1164.                             p.prix_vente as prixVente,
  1165.                             p.prix_base as prixAchat,
  1166.                             p.poids,
  1167.                             p.stock,
  1168.                             p.stock_reserve,
  1169.                             p.reference,
  1170.                             p.pump,
  1171.                             p.cpump,
  1172.                             p.arret_gamme,
  1173.                             MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation ";
  1174.                         $sql2 .= "FROM ".$selectTemp;
  1175.                         $sql2 .= $jointureFromInventaire;
  1176.                         $sql2 .= "LEFT JOIN article__article_categorie as aac ON aac.categorie_id = tempAC.id LEFT JOIN article__article as p ON aac.article_id = p.id"
  1177.                                  //. " LEFT JOIN article__condition_achat ca ON ca.id = p.id"
  1178.                                  ." WHERE ";
  1179.                         if ($jointureWhere2 != "") {
  1180.                             $sql .= $jointureWhere2." and ";
  1181.                         }
  1182.                         $sql2 .= " p.temporaire is null
  1183.                 and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1184.                 and
  1185.                 (MATCH (".$match.")
  1186.                 AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)"//. " OR MATCH (ca.reference_fournisseur, ca.reference_fournisseur_appel) AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) ";
  1187.                         ;
  1188.                         $sql2 .= $wherePrix.$whereStock;
  1189.                         $sql2 .= ")";
  1190.                         $sql2 .= $jointureWhereInventaire;
  1191.                         $sql2 .= " GROUP BY p.id
  1192.                             order by ".$order." ponderation desc
  1193.                 LIMIT 0,100
  1194.                 ";
  1195.                         //sql3
  1196.                         $sql3 "select
  1197.                             p.stock_reserve_fabrication,
  1198.                             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1199.                             p.reference_appel_1,
  1200.                             p.reference_appel_2,
  1201.                             p.reference_appel_3,
  1202.                             p.reference_appel_4,
  1203.                             p.reference_appel_5,
  1204.                             p.reference_appel_6,
  1205.                             p.reference_appel_7,
  1206.                             p.reference_appel_8,
  1207.                             p.reference_appel_9,
  1208.                             p.reference_appel_10,
  1209.                             p.reference_appel_11,
  1210.                             p.article_remplacement_id,
  1211.                             p.libelle,
  1212.                             p.virtuel,
  1213.                             p.divers,
  1214.                             p.id,
  1215.                             p.reference_fournisseur,
  1216.                             p.prix_vente as prixVente,
  1217.                             p.prix_base as prixAchat,
  1218.                             p.poids,
  1219.                             p.stock,
  1220.                             p.stock_reserve,
  1221.                             p.reference,
  1222.                             p.pump,
  1223.                             p.cpump,
  1224.                             p.arret_gamme,
  1225.                             MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation ";
  1226.                         $preselectWhere "divers = 1 and parent_id IS NULL and temporaire is null ";
  1227.                         $sql3 .= "FROM (select * FROM article__article WHERE ".$preselectWhere." and (archive = 0 or archive is null)) AS p";
  1228.                         $sql3 .= $jointureFromInventaire;
  1229.                         $sql3 .= " WHERE (MATCH (".$match.")
  1230.                             AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)";
  1231.                         $sql3 .= $wherePrix.$whereStock;
  1232.                         $sql3 .= ")";
  1233.                         $sql3 .= $jointureWhereInventaire;
  1234.                         $sql3 .= " GROUP BY p.id
  1235.                                 order by ".$order." ponderation desc
  1236.                                 LIMIT 0,100
  1237.                                 ";
  1238.                     }
  1239.                 }
  1240.             }
  1241.         } else {
  1242.             $order .= "p.reference asc";
  1243.             $sql "
  1244.             select
  1245.           p.reference_appel_1,
  1246.           p.reference_appel_2,
  1247.           p.reference_appel_3,
  1248.           p.reference_appel_4,
  1249.           p.reference_appel_5,
  1250.           p.reference_appel_6,
  1251.           p.reference_appel_7,
  1252.           p.reference_appel_8,
  1253.           p.reference_appel_9,
  1254.           p.reference_appel_10,
  1255.           p.reference_appel_11,
  1256.             p.arret_gamme,
  1257.             p.virtuel,
  1258.                 p.divers,
  1259.             p.article_remplacement_id,
  1260.             p.libelle,
  1261.             p.id,
  1262.             p.reference_fournisseur,
  1263.             p.prix_vente as prixVente,
  1264.             p.prix_base as prixAchat,
  1265.             p.poids,
  1266.             p.stock,
  1267.             p.stock_reserve_fabrication,
  1268.         p.stock_reserve,
  1269.         p.pump,
  1270.         p.cpump,
  1271.             p.reference"
  1272.                    .$select
  1273.                    //." FROM ".$selectTemp." LEFT JOIN article__article_categorie as aac ON aac.categorie_id = tempAC.id LEFT JOIN article__article as p ON aac.article_id = p.id"
  1274.                    ." FROM (select
  1275.                     *
  1276.                     FROM article__condition_achat
  1277.                     WHERE
  1278.                     fournisseur_id = ".$fournisseur." ) AS temp "
  1279.                    ." LEFT JOIN article__article as p ON temp.article_id = p.id"
  1280.                    .$jointureFromInventaire
  1281.                    ." WHERE ";
  1282.             if ($jointureWhere != '') {
  1283.                 $sql .= $jointureWhere." and";
  1284.             }
  1285.             $sql .= " p.temporaire is null
  1286.             and
  1287.             (p.archive = 0 or p.archive is null)
  1288.                         "
  1289.                     //.$whereSuppl
  1290.                     .$whereStock.$jointureWhereInventaire.
  1291.                     "
  1292.                             GROUP BY p.id
  1293.             order by ".$order."
  1294.             LIMIT 0,100
  1295.             ";
  1296.         }
  1297.         //where p.libelle like '%".$recherche."%' or p.reference like '%".$recherche."%'
  1298.        // echo $sql;
  1299.         $em   $this->getEntityManager();
  1300.         $stmt $em->getConnection()->prepare($sql);
  1301.         $resultats1 $stmt->executeQuery()->fetchAllAssociative();
  1302.         $resultats  $resultats1;
  1303.         //var_dump($resultats);
  1304.         if ($sql3 != "") {
  1305.             $stmt $em->getConnection()->prepare($sql3);
  1306.             $resultats3 $stmt->executeQuery()->fetchAllAssociative();
  1307.             //var_dump($resultats2);
  1308.             $resultats array_merge($resultats$resultats3);
  1309.             //var_dump($resultats);
  1310.         }
  1311.         if ($sql2 != "" && count($resultats) == 0) {
  1312.             $stmt $em->getConnection()->prepare($sql2);
  1313.             $resultats2 $stmt->executeQuery()->fetchAllAssociative();
  1314.             //var_dump($resultats2);
  1315.             $resultats array_merge($resultats$resultats2);
  1316.             //var_dump($resultats);
  1317.         }
  1318.         //echo $sql;
  1319.         //exit;
  1320.         //echo "<div>------(".$mouvemente.")</div>";
  1321.         //Si la recherche dans les articles mouvementés ne donne rien, on effectue la requete sur les articles non mouvementés
  1322.         if (count($resultats1) == && count($resultats) == && $mouvemente == "1") {
  1323.             //$mouvemente =0;
  1324.             /*if(!$avecEtoile) {
  1325.                 $recherche = str_replace("*","",$recherche);
  1326.             }
  1327.             */
  1328.             //echo "<div>ototo ".$recherche."</div>";
  1329.             //echo "--".$mouvemente;
  1330.             //exit;
  1331.             $resultats $this->getRechercheArticleSelect2(
  1332.                 $recherche_tmp,
  1333.                 $produitInterne,
  1334.                 $fournisseur,
  1335.                 $articlesAExclure,
  1336.                 $stock,
  1337.                 $reference_search,
  1338.                 $libelle_search,
  1339.                 $prix_search,
  1340.                 $tri_reference,
  1341.                 $tri_libelle,
  1342.                 $tri_prix,
  1343.                 $tri_stock,
  1344.                 $categories,
  1345.                 "0",
  1346.                 $inventaire
  1347.             );
  1348.             //exit;
  1349.         }
  1350.         //exit;
  1351.         return $resultats;
  1352.     }
  1353.     public function getRechercheArticleSelect3($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock ""$reference_search ""$libelle_search "",
  1354.                                                $prix_search ""$tri_reference ""$tri_libelle ""$tri_prix ""$tri_stock ""$categories ""$mouvemente 1$inventaire ''
  1355.     ) {
  1356.         $match                   'p.reference,p.libelle,p.reference_appel_1,p.reference_appel_2,p.reference_appel_3,p.reference_appel_4,p.reference_appel_5,p.reference_appel_6,p.reference_appel_7,p.reference_appel_8,p.reference_appel_9,p.reference_appel_10,p.reference_appel_11';
  1357.         $select                  '';
  1358.         $jointureFromInventaire  "";
  1359.         $jointureWhereInventaire "";
  1360.         $recherche str_replace("*""%"$recherche);
  1361.         $recherche str_replace("-"" "$recherche);
  1362.         $recherche str_replace("."""$recherche);
  1363.         $recherche str_replace("/""\/"$recherche);
  1364.         $recherche str_replace("   "" "$recherche);
  1365.         $recherche str_replace("  "" "$recherche);
  1366.         $recherche_tmp $recherche;
  1367.         $avecEtoile    false;
  1368.         $pos strpos($recherche"*");
  1369.         if ($pos !== false) {
  1370.             $avecEtoile true;
  1371.         }
  1372.         $recherche str_replace("*""%"$recherche);
  1373.         $wherePrix        "";
  1374.         $tampon_recherche $recherche;
  1375.         $recherche        trim($recherche);
  1376.         if ( ! $avecEtoile) {
  1377.             $recherche str_replace(" ""* "$recherche);
  1378.         }
  1379.         if ( ! $avecEtoile) {
  1380.             $recherche .= "*";
  1381.         }
  1382.         $whereSuppl " and p.parent_id is null";
  1383.         $whereStock "";
  1384.         if ($stock == 1) {
  1385.             $whereStock .= " and p.stock > 0";
  1386.         }
  1387.         $order "";
  1388.         if ($tri_reference != "") {
  1389.             $order "p.reference ".$tri_reference.", ";
  1390.         }
  1391.         if ($tri_prix != "") {
  1392.             $order "p.prix_vente ".$tri_prix.", ";
  1393.         }
  1394.         if ($tri_stock != "") {
  1395.             $order "p.stock ".$tri_stock.", ";
  1396.         }
  1397.         if ($tri_libelle != "") {
  1398.             $order "p.libelle ".$tri_libelle.", ";
  1399.         }
  1400.         if ($order == "") {
  1401.             $order "p.reference ASC ";
  1402.         }
  1403.         //echo "<div>order : ".$order."</div>";
  1404.         $jointureFrom  "";
  1405.         $jointureWhere "";
  1406.         /*
  1407.      if($categories != ''){
  1408.          $jointureFrom .= ' LEFT JOIN article__article_categorie acat on acat.article_id = p.id JOIN article__categorie as cat on cat.id = acat.categorie_id';
  1409.          //$jointureWhere .= " and cat.libelle LIKE '".$categorie."%'";
  1410.          foreach($categories as $key=>$categorie){
  1411.              if($key == 0 )
  1412.                  $jointureWhere .= " and (";
  1413.              else
  1414.                  $jointureWhere .= " OR";
  1415.              $jointureWhere .= " cat.libelle LIKE '".$categorie."'";
  1416.          }
  1417.          $jointureWhere .= ")";
  1418.      }
  1419.          *
  1420.          */
  1421.         if ($categories != '') {
  1422.             /*
  1423.             $jointureFrom .= ' LEFT JOIN article__article_categorie acat on acat.article_id = p.id LEFT JOIN article__categorie as cat on cat.id = acat.categorie_id';
  1424.             foreach($categories as $categorie){
  1425.                 $jointureWhere .= " OR cat.libelle LIKE '".$categorie."'";
  1426.             }
  1427.                  *
  1428.                  */
  1429.             $selectTemp "(select id FROM article__categorie cat WHERE ";
  1430.             foreach ($categories as $key => $categorie) {
  1431.                 if ($key == 0) {
  1432.                     $selectTemp .= " ";
  1433.                 } else {
  1434.                     $selectTemp .= " OR";
  1435.                 }
  1436.                 $selectTemp .= " cat.reference LIKE '".$categorie."'";
  1437.             }
  1438.             $selectTemp .= ") as tempAC ";
  1439.             $jointureFrom .= "LEFT JOIN ".$selectTemp." LEFT JOIN article__article_categorie as aac ON aac.categorie_id = tempAC.id";
  1440.         }
  1441.         if ($produitInterne == true || $produitInterne == 'true') {
  1442.             $whereSuppl .= ' and p.produit_interne =1';
  1443.         }
  1444.         if (count($articlesAExclure) > 0) {
  1445.             $whereSuppl .= " and p.id NOT IN ( '".implode($articlesAExclure"', '")."' )";// != :articleAExclure and p.id != :articleAExclure
  1446.         }
  1447.         /*
  1448.         if ($mouvemente == "")
  1449.             $whereSuppl .= " and p.divers = 1";
  1450.         else
  1451.             $whereSuppl .= " and (p.mouvemente = ".$mouvemente." or p.divers = 1)";
  1452.          *
  1453.          */
  1454.         if ($mouvemente != "0" && $mouvemente !== null or ($mouvemente == "")) {
  1455.             $mouvemente 1;
  1456.             $whereSuppl .= " and (p.mouvemente = ".$mouvemente." or p.divers = 1)";
  1457.         } else {
  1458.             //echo "titit (".$mouvemente.")";
  1459.             $whereSuppl .= " and (p.mouvemente = ".$mouvemente." or p.mouvemente =1 or p.mouvemente is null or p.divers = 1)";
  1460.         }
  1461.         //echo 'mouvemente : '.$mouvemente;
  1462.         if ( ! $avecEtoile) {
  1463.             $recherche str_replace("-"""$recherche);
  1464.         }
  1465.         if ($inventaire != '') {
  1466.             //$select .= ', count(ai.id) as nbArticleInventaire';
  1467.             $select                 .= ', COUNT(CASE WHEN ai.inventaire_id = '.$inventaire.' then 1 ELSE NULL END) as "nbArticleInventaire"';
  1468.             $jointureFromInventaire .= ' LEFT JOIN inventaire__article_inventaire ai ON ai.article_id = p.id';
  1469.             //$jointureWhereInventaire .= ' and (ai.inventaire_id = '.$inventaire.' OR ai.inventaire_id IS NULL)';
  1470.         }
  1471.         //echo "<div>S : ".$recherche."</div>";
  1472.         if ($recherche != "" and $recherche != "*") {
  1473.             if ($prix_search == 1) {
  1474.                 $sql "
  1475.             select
  1476.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1477.             p.arret_gamme,
  1478.             p.virtuel,
  1479.             p.article_remplacement_id,
  1480.             p.libelle,
  1481.             p.id,
  1482.             p.prix_vente as prixVente,
  1483.             p.prix_base as prixAchat,
  1484.             p.poids,
  1485.             p.stock,
  1486.             p.stock_reserve_fabrication,
  1487.             p.stock_reserve,
  1488.             p.reference,
  1489.             p.pump,
  1490.             p.cpump,
  1491.             MATCH (".$match.") AGAINST ('".$recherche."' IN BOOLEAN MODE) as ponderation"
  1492.                        .$select
  1493.                        ." FROM article__article as p"
  1494.                        .$jointureFromInventaire
  1495.                        ." WHERE
  1496.             p.temporaire is null
  1497.             and
  1498.             (p.archive = 0 or p.archive is null)
  1499.             and
  1500.             p.prix_vente like '".$tampon_recherche."%'
  1501.             ".$whereStock."
  1502.             ".$whereSuppl.$jointureWhereInventaire."
  1503.             GROUP BY p.id
  1504.                     order by ".$order." p.id desc
  1505.             LIMIT 0,100
  1506.             ";
  1507.             } else {
  1508.                 /*
  1509.                  p.reference LIKE '".$recherche."'
  1510.                         OR
  1511.                         p.libelle LIKE '".$recherche."'
  1512.                         OR
  1513.                         reference_appel_1 LIKE '".$recherche."'
  1514.                         OR
  1515.                         reference_appel_2 LIKE '".$recherche."'
  1516.                         OR
  1517.                         reference_appel_3 LIKE '".$recherche."'
  1518.                         OR
  1519.                         reference_appel_4 LIKE '".$recherche."'
  1520.                         OR
  1521.                         reference_appel_5 LIKE '".$recherche."'
  1522.                         OR
  1523.                         reference_appel_6 LIKE '".$recherche."'
  1524.                         OR
  1525.                         reference_appel_7 LIKE '".$recherche."'
  1526.                         OR
  1527.                         reference_appel_8 LIKE '".$recherche."'
  1528.                         OR
  1529.                         reference_appel_9 LIKE '".$recherche."'
  1530.                         OR
  1531.                         reference_appel_10 LIKE '".$recherche."'
  1532.                         OR
  1533.                         reference_appel_11 LIKE '".$recherche."'
  1534.                 */
  1535.                 if ($avecEtoile) {
  1536.                     if ($order == "") {
  1537.                         $order "p.reference";
  1538.                     }
  1539.                     $order     str_replace(","""$order);
  1540.                     $recherche .= "%";
  1541.                     $sql       "
  1542.                 select
  1543.                 CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1544.                 p.arret_gamme,
  1545.                 p.virtuel,
  1546.                 p.article_remplacement_id,
  1547.                 p.libelle,
  1548.                 p.id,
  1549.                 p.prix_vente as prixVente,
  1550.                 p.prix_base as prixAchat,
  1551.                 p.poids,
  1552.                 p.stock,
  1553.                 p.stock_reserve_fabrication,
  1554.                 p.stock_reserve,
  1555.                 p.pump,
  1556.                 p.cpump,
  1557.                 p.reference"
  1558.                                  .$select
  1559.                                  ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  1560.                 WHERE
  1561.                 p.temporaire is null
  1562.                 and
  1563.                 (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1564.                 and
  1565.                 (
  1566.                     p.reference LIKE '".$recherche."'
  1567.                     OR
  1568.                     p.libelle LIKE '".$recherche."'
  1569.                     OR
  1570.                     p.reference_appel_1 LIKE '".$recherche."'
  1571.                     OR
  1572.                     p.reference_appel_2 LIKE '".$recherche."'
  1573.                     OR
  1574.                     p.reference_appel_3 LIKE '".$recherche."'
  1575.                     OR
  1576.                     p.reference_appel_4 LIKE '".$recherche."'
  1577.                     OR
  1578.                     p.reference_appel_5 LIKE '".$recherche."'
  1579.                     OR
  1580.                     p.reference_appel_6 LIKE '".$recherche."'
  1581.                     OR
  1582.                     p.reference_appel_7 LIKE '".$recherche."'
  1583.                     OR
  1584.                     p.reference_appel_8 LIKE '".$recherche."'
  1585.                     OR
  1586.                     p.reference_appel_9 LIKE '".$recherche."'
  1587.                     OR
  1588.                     p.reference_appel_10 LIKE '".$recherche."'
  1589.                     OR
  1590.                     p.reference_appel_11 LIKE '".$recherche."'";
  1591.                     if ($match2 != "") {
  1592.                         $sql .= "OR ca.reference_fournisseur LIKE '".$recherche."'";
  1593.                         $sql .= "OR ca.reference_fournisseur_appel LIKE '".$recherche."'";
  1594.                     }
  1595.                     $sql .= "   )
  1596.                 ".$wherePrix.$whereStock.$jointureWhere.$jointureWhereInventaire."
  1597.                 GROUP BY p.id
  1598.                             order by ".$order."
  1599.                 LIMIT 0,100
  1600.                 ";
  1601.                     //echo $sql;
  1602.                     //exit;
  1603.                 } else {
  1604.                     //echo "OK";
  1605.                     //exit;
  1606.                     $recherche_contruit "";
  1607.                     $recherche_tab      explode(" "$recherche);
  1608.                     for ($r 0$r count($recherche_tab); $r++) {
  1609.                         //$recherche_contruit .="+".$recherche_tab[$r]." ";
  1610.                         $recherche_tab[$r]  = str_replace("+""#+"$recherche_tab[$r]);
  1611.                         $recherche_contruit .= "+".$recherche_tab[$r]." ";
  1612.                     }
  1613.                     //echo "<div>SUPPL ".$whereSuppl."</div>";
  1614.                     $sql "
  1615.                 select
  1616.                 p.stock_reserve_fabrication,
  1617.                 CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1618.                 p.article_remplacement_id,
  1619.                 p.virtuel,
  1620.                 p.libelle,
  1621.                 p.id,
  1622.                 p.prix_vente as prixVente,
  1623.                 p.prix_base as prixAchat,
  1624.                 p.poids,
  1625.                 p.stock,
  1626.                 p.stock_reserve,
  1627.                 p.reference,
  1628.                 p.pump,
  1629.                 p.cpump,
  1630.                 p.arret_gamme,
  1631.                 MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation";
  1632.                     $sql .= $select;
  1633.                     $sql .= " FROM ".$selectTemp;
  1634.                     $sql .= "LEFT JOIN article__article_categorie as aac ON aac.categorie_id = tempAC.id LEFT JOIN article__article as p ON aac.article_id = p.id ";
  1635.                     $sql .= $jointureFromInventaire
  1636.                             ." WHERE ";
  1637.                     if ($jointureWhere != "") {
  1638.                         $sql .= $jointureWhere." and ";
  1639.                     }
  1640.                     $sql .= " p.temporaire is null
  1641.                 and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1642.                 and
  1643.                 (MATCH (".$match.")
  1644.                 AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)";
  1645.                     $sql .= "";
  1646.                     $sql .= $wherePrix.$whereStock;
  1647.                     $sql .= ")";
  1648.                     $sql .= $jointureWhereInventaire;
  1649.                     $sql .= " GROUP BY p.id
  1650.                             order by ".$order.", ponderation desc
  1651.                 LIMIT 0,100
  1652.                 ";
  1653.                     //echo $sql;
  1654.                 }
  1655.             }
  1656.         } else {
  1657.             //$order .= "p.reference asc";
  1658.             $sql "
  1659.             select
  1660.             p.arret_gamme,
  1661.             p.virtuel,
  1662.             p.article_remplacement_id,
  1663.             p.libelle,
  1664.             p.id,
  1665.             p.prix_vente as prixVente,
  1666.             p.prix_base as prixAchat,
  1667.             p.poids,
  1668.             p.stock,
  1669.             p.stock_reserve_fabrication,
  1670.             p.stock_reserve,
  1671.             p.pump,
  1672.             p.cpump,
  1673.             p.reference";
  1674.             $sql .= $select;
  1675.             $sql .= " FROM ".$selectTemp;
  1676.             $sql .= "LEFT JOIN article__article_categorie as aac ON aac.categorie_id = tempAC.id LEFT JOIN article__article as p ON aac.article_id = p.id ";
  1677.             $sql .= $jointureFromInventaire
  1678.                     ." WHERE ";
  1679.             if ($jointureWhere != "") {
  1680.                 $sql .= $jointureWhere." and ";
  1681.             }
  1682.             $sql .= " p.temporaire is null
  1683.             and
  1684.             (p.archive = 0 or p.archive is null)
  1685.                         ".$whereSuppl.$whereStock.$jointureWhereInventaire."
  1686.                             GROUP BY p.id
  1687.             order by ".$order."
  1688.             LIMIT 0,100
  1689.             ";
  1690.         }
  1691.         //where p.libelle like '%".$recherche."%' or p.reference like '%".$recherche."%'
  1692.         //echo $sql;
  1693.         $em   $this->getEntityManager();
  1694.         $stmt $em->getConnection()->prepare($sql);
  1695.         $resultats $stmt->executeQuery()->fetchAllAssociative();
  1696.         //$resultats = array();
  1697.         //echo $sql;
  1698.         //exit;
  1699.         //echo "<div>------(".$mouvemente.")</div>";
  1700.         //Si la recherche dans les articles mouvementés ne donne rien, on effectue la requete sur les articles non mouvementés
  1701.         if (count($resultats) == && $mouvemente == "1") {
  1702.             //$mouvemente =0;
  1703.             /*if(!$avecEtoile) {
  1704.                 $recherche = str_replace("*","",$recherche);
  1705.             }
  1706.             */
  1707.             //echo "<div>ototo ".$recherche."</div>";
  1708.             //echo "--".$mouvemente;
  1709.             //exit;
  1710.             $resultats $this->getRechercheArticleSelect3(
  1711.                 $recherche_tmp,
  1712.                 $produitInterne,
  1713.                 $fournisseur,
  1714.                 $articlesAExclure,
  1715.                 $stock,
  1716.                 $reference_search,
  1717.                 $libelle_search,
  1718.                 $prix_search,
  1719.                 $tri_reference,
  1720.                 $tri_libelle,
  1721.                 $tri_prix,
  1722.                 $tri_stock,
  1723.                 $categories,
  1724.                 "0",
  1725.                 $inventaire
  1726.             );
  1727.             //exit;
  1728.         }
  1729.         //exit;
  1730.         return $resultats;
  1731.     }
  1732.     public function findLibelleOrRefLike($query$articleId)
  1733.     {
  1734.         $tampon_recherche $query;
  1735.         $recherche        trim($query);
  1736.         $recherche        str_replace(" ""* "$recherche);
  1737.         $recherche        .= "*";
  1738.         $recherche        str_replace("-"""$recherche);
  1739.         $jointureFrom  "";
  1740.         $whereSuppl    "";
  1741.         $wherePrix     "";
  1742.         $whereStock    "";
  1743.         $jointureWhere "";
  1744.         $order         "";
  1745.         $sql "
  1746.             select
  1747.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle,
  1748.             p.id,
  1749.             p.virtuel,
  1750.             p.prix_vente as prixVente,
  1751.             p.prix_base as prixAchat,
  1752.             p.poids,
  1753.             p.stock,
  1754.             p.reference,
  1755.             MATCH (p.reference,p.libelle,p.reference_appel_1,p.reference_appel_2,p.reference_appel_3,p.reference_appel_4,p.reference_appel_5,p.reference_appel_6,p.reference_appel_7,p.reference_appel_8,p.reference_appel_9,p.reference_appel_10,p.reference_appel_11) AGAINST ('".$recherche."' IN BOOLEAN MODE) as ponderation
  1756.             FROM article__article as p ".$jointureFrom."
  1757.             WHERE
  1758.             p.temporaire is null
  1759.                     and
  1760.             p.consigne = 0
  1761.             and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1762.             and
  1763.             (MATCH (
  1764.                                    p.reference,
  1765.                                    p.libelle,
  1766.                                    reference_appel_1,
  1767.                                    reference_appel_2,
  1768.                                    reference_appel_3,
  1769.                                    reference_appel_4,
  1770.                                    reference_appel_5,
  1771.                                    reference_appel_6,
  1772.                                    reference_appel_7,
  1773.                                    reference_appel_8,
  1774.                                    reference_appel_9,
  1775.                                    reference_appel_10,
  1776.                                    reference_appel_11
  1777.                                )
  1778.             AGAINST ('".$recherche."' IN BOOLEAN MODE)
  1779.             ".$wherePrix.$whereStock.$jointureWhere."
  1780.             )
  1781.             order by ".$order." ponderation desc
  1782.             LIMIT 0,100
  1783.             ";
  1784.         /*
  1785.         return $this->getEntityManager()
  1786.             ->createQuery(
  1787.                 "select r.id, CONCAT(r.libelle, '[', COALESCE(r.reference, ''), ']') as libelle FROM App\Entity\Articles\Article r WHERE r.archive = 0 and r.reference LIKE '%".$query."%' or r.libelle LIKE '%".$query."%' and r.id NOT IN (:id) "
  1788.             )
  1789.                 ->setParameters(array('id' => $articleId))
  1790.             ->getResult();
  1791.         */
  1792.         $em   $this->getEntityManager();
  1793.         $stmt $em->getConnection()->prepare($sql);
  1794.         return $stmt->executeQuery()->fetchAllAssociative();
  1795.     }
  1796.     public function agetRechercheArticleSelect($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock ""$reference_search ""$libelle_search "",
  1797.                                                $prix_search ""$tri_reference ""$tri_prix ""$tri_stock ""
  1798.     ) {
  1799.         $where "";
  1800.         //$cond = "%".$recherche . "%";
  1801.         $cond $recherche."%";
  1802.         //$parameters = array('field' => $recherche,'archive'=>'0','cond' => $cond);
  1803.         $parameters = ['field' => $recherche'archive' => '0'];
  1804.         //if($recherche != "") $parameters = array('field' => $recherche,'archive'=>'0','cond' => $cond);
  1805.         if ($recherche != "") {
  1806.             $parameters = ['field' => $recherche'archive' => '0'];
  1807.         }
  1808.         $qb $this->getEntityManager()->createQueryBuilder();
  1809.         $qb
  1810.             ->select(
  1811.                 "p.libelle,p.id,p.prixVente,p.poids,p.stock,p.reference,(MATCH (p.reference,p.libelle,p.referenceAppel1,p.referenceAppel2,p.referenceAppel3,p.referenceAppel4,p.referenceAppel5,p.referenceAppel6,p.referenceAppel7,p.referenceAppel8,p.referenceAppel9,p.referenceAppel10) AGAINST (:field) as ponderation"
  1812.             )
  1813.             ->from(Article::class, 'p');
  1814.         //if($recherche != "") $where = '(MATCH (p.reference,p.libelle) AGAINST (:field) > 0 or p.prixVente like :cond) and ';
  1815.         if ($recherche != "") {
  1816.             $wherePrix "";
  1817.             if ($prix_search == 1) {
  1818.                 $parameters = ['field' => $recherche'archive' => '0''cond' => $cond];
  1819.                 $wherePrix  ' or p.prixVente like :cond ';
  1820.             }
  1821.             $where '(MATCH (
  1822.                                    p.reference,
  1823.                                    p.libelle,
  1824.                                    p.referenceAppel1,
  1825.                                    p.referenceAppel2,
  1826.                                    p.referenceAppel3,
  1827.                                    p.referenceAppel4,
  1828.                                    p.referenceAppel5,
  1829.                                    p.referenceAppel6,
  1830.                                    p.referenceAppel7,
  1831.                                    p.referenceAppel8,
  1832.                                    p.referenceAppel9,
  1833.                                    p.referenceAppel10
  1834.                                ) AGAINST (:field) > 0)'.$wherePrix.'and ';
  1835.         }
  1836.         $where .= 'p.temporaire is null and p.archive = :archive ';
  1837.         if ($fournisseur == true) {
  1838.             //ajout des tables conditions d'achat et fournisseur
  1839.             $select .= ', f.id as fournisseur';
  1840.             $query->join('p.conditionsAchat''ca');
  1841.             $query->innerJoin('ca.fournisseur''f''WITH''ca.fournisseur = f.id');
  1842.             $where                     .= ' and f.id = :fournisseur';
  1843.             $parameters['fournisseur'] = $fournisseur;
  1844.         }
  1845.         if ($produitInterne == true) {
  1846.             $where .= ' and p.produitInterne =1';
  1847.         }
  1848.         if (count($articlesAExclure) > 0) {
  1849.             $where .= " and p.id NOT IN ( '".implode($articlesAExclure"', '")."' )";// != :articleAExclure and p.id != :articleAExclure
  1850.         }
  1851.         if ($stock == 1) {
  1852.             $where .= " and p.stock > 0";
  1853.         }
  1854.         if ($where != "") {
  1855.             $qb->where($where)
  1856.                ->setParameters($parameters)
  1857.                ->setFirstResult(0)
  1858.                ->setMaxResults(1000)
  1859.                ->groupBy('p.id')//->orderBy('ponderation', 'desc')
  1860.             ;
  1861.         }
  1862.         if ($prix_search == 1) {
  1863.         } else {
  1864.             $qb->addOrderBy('ponderation''desc');
  1865.         }
  1866.         if ($tri_reference != "") {
  1867.         }
  1868.         if ($tri_prix != "") {
  1869.             $qb->addOrderBy('p.prixVente'$tri_prix);
  1870.         }
  1871.         if ($tri_stock != "") {
  1872.             $qb->addOrderBy('p.stock'$tri_stock);
  1873.         }
  1874.         $res $qb->getQuery()->getResult();
  1875.         //\Doctrine\Common\Util\Debug::dump($res);
  1876.         return $res;
  1877.     }
  1878.     public function ZgetRechercheArticleSelect($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock "")
  1879.     {
  1880.         //echo "V";
  1881.         $cond   "%".$recherche."%";
  1882.         $query  $this->createQueryBuilder('p')
  1883.             //->select("CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(p.reference, ' '), p.libelle),' (Stock : '),p.stock),') - '),p.prixVente),' &euro;') as libelle, p.id,p.prixBase,p.poids,p.stock,p.reference");
  1884.                        ->select("p.libelle, p.id,p.prixVente,p.poids,p.stock,p.reference");
  1885.         $select '';
  1886.         //$where = "MATCH (p.reference, p.libelle) AGAINST (:cond)";
  1887.         //$where = "MATCH_AGAINST(p.reference, p.libelle) > 0";
  1888.         $where      '(p.libelle LIKE :cond OR p.reference LIKE :cond OR p.prixVente LIKE :cond) and p.temporaire is null and p.archive = :archive';
  1889.         $parameters = ['cond' => $cond'archive' => '0'];
  1890.         if ($fournisseur == true) {
  1891.             //ajout des tables conditions d'achat et fournisseur
  1892.             $select .= ', f.id as fournisseur';
  1893.             $query->join('p.conditionsAchat''ca');
  1894.             $query->innerJoin('ca.fournisseur''f''WITH''ca.fournisseur = f.id');
  1895.             $where                     .= ' and f.id = :fournisseur';
  1896.             $parameters['fournisseur'] = $fournisseur;
  1897.         }
  1898.         if ($produitInterne == true) {
  1899.             $where .= ' and p.produitInterne =1';
  1900.         }
  1901.         if (count($articlesAExclure) > 0) {
  1902.             $where .= " and p.id NOT IN ( '".implode($articlesAExclure"', '")."' )";// != :articleAExclure and p.id != :articleAExclure
  1903.         }
  1904.         if ($stock == 1) {
  1905.             $where .= " and p.stock > 0";
  1906.         }
  1907.         //echo "AA ".$where;
  1908.         $query->where($where)
  1909.               ->setParameters($parameters)//array('cond' => $cond)
  1910.             //->orderBy('p.prenom', 'ASC');
  1911.               ->setFirstResult(0)
  1912.               ->setMaxResults(1000)
  1913.               ->orderBy('p.reference''ASC');
  1914.         $res $query->getQuery()->getResult();
  1915.         return $res;
  1916.     }
  1917.     public function getRechercheClient($recherche)
  1918.     {
  1919.         $cond  $recherche."%";
  1920.         $query $this->createQueryBuilder('p')
  1921.                       ->select("CONCAT(CONCAT(p.prenom, ' '), p.nom) as libelle, p.id,p.telephone")
  1922.                       ->where('p.prenom LIKE :cond or p.nom like :cond')
  1923.                       ->setParameters(['cond' => $cond])
  1924.                       ->orderBy('p.prenom''ASC');
  1925.         $res $query->getQuery()->getResult();
  1926.         return $res;
  1927.     }
  1928.     public function findByRefOrName($query)
  1929.     {
  1930.         return $this->getEntityManager()
  1931.                     ->createQuery(
  1932.                         "select a.id, a.reference, a.libelle, a.prixBase
  1933.                 FROM App\Entity\Articles\Article a
  1934.                 WHERE p.temporaire is null and p.archive = 0 and (a.reference LIKE '%".$query."%' OR a.libelle LIKE '%".$query."%' OR a.referenceFournisseur LIKE '%".$query."%)'
  1935.                 ORDER BY a.libelle ASC"
  1936.                     )
  1937.                     ->getResult();
  1938.     }
  1939.     public function genererCpumpArticle($id$date ''$quantiteTotal 0)
  1940.     {
  1941.         //$article = $this->getEntityManager()->getRepository(Article::class)->find($id);
  1942.         mail("thomas@noogaa.com""BB""B");
  1943.         $builder = new ProcessBuilder();
  1944.         $builder->setArguments(['php''../app/console''genererCpumpArticle'$id]);
  1945.         $builder->getProcess()->start();
  1946.         mail("thomas@noogaa.com""CC""C");
  1947.     }
  1948.     public function getPumpC($id$date ''$quantiteTotal 0)
  1949.     {
  1950.         return 0;
  1951.         $pump        "0";
  1952.         $repo_condha $this->getEntityManager()->getRepository(ConditionAchat::class);
  1953.         $article     $this->getEntityManager()->getRepository(Article::class)->find($id);
  1954.         if (is_object($article) && $article->getStock() > && $date != '') {
  1955.             $nbResultats floatval($article->getStock());
  1956.         } else {
  1957.             $nbResultats 30;
  1958.         }
  1959.         $where "WHERE a.id = :id and m.prixAchat is not null and r.action = 'augmenter' and m.quantite > 0 and c.id IS NULL and r.id NOT IN (19)";//and m.fournisseur is not null
  1960.         $where "WHERE a.id = :id and m.idImport is NULL";
  1961.         if ($date != '') {
  1962.             $where .= " and m.dateMvt <= '".$date->format("Y-m-d H:i:s")."'";
  1963.         }
  1964.         $nbResultats 2;
  1965.         $stocks $this->getEntityManager()
  1966.                        ->createQuery(
  1967.                            "select m.id,a.stock, m.quantite, m.prixAchat,m.cpump,r.action,m.stock as stockMvt,r.id as raison_id
  1968.                 FROM App\Entity\Articles\Article a
  1969.                 LEFT JOIN App\Entity\Articles\MouvementStock m with m.article = a.id
  1970.                 LEFT JOIN App\Entity\GestionComerciale\Commande c with m.commande = c.id
  1971.                 LEFT JOIN App\Entity\Articles\RaisonMouvementStock r with m.raisonMouvementStock = r.id
  1972.                 ".$where."
  1973.                 ORDER BY m.dateMvt DESC,m.id DESC"
  1974.                        )
  1975.                        ->setParameters(['id' => $id])
  1976.                        ->setMaxResults($nbResultats)
  1977.                        ->getResult();
  1978.         if (count($stocks) == "2") {
  1979.             //echo "<div>OKOKO</div>";
  1980.             $dernierMouvement $stocks[0];
  1981.             $mouvement        $stocks[1];
  1982.             if ($dernierMouvement["action"] == "augmenter" and ($dernierMouvement["raison_id"] == "3" or $dernierMouvement["raison_id"] == "15")) {
  1983.                 if ($dernierMouvement["stockMvt"] == 0) {
  1984.                     return 0;
  1985.                 } else {
  1986.                     if ($mouvement["cpump"] > 0) {
  1987.                         //echo "AZERTY";
  1988.                         $pump = (($mouvement["stockMvt"] * $mouvement["cpump"]) + ($dernierMouvement["quantite"] * $dernierMouvement["prixAchat"])) / $dernierMouvement["stockMvt"];
  1989.                         //return $pump;
  1990.                     } else {
  1991.                         //echo "QWERTY";
  1992.                         //print_r($dernierMouvement);
  1993.                         $pump = ($dernierMouvement["quantite"] * $dernierMouvement["prixAchat"]) / $dernierMouvement["quantite"];
  1994.                         //return $pump;
  1995.                     }
  1996.                 }
  1997.             } else {
  1998.                 $pump $mouvement["cpump"];
  1999.             }
  2000.         } elseif (count($stocks) == "1") {
  2001.             $dernierMouvement $stocks[0];
  2002.             if ($dernierMouvement["action"] == "augmenter" and ($dernierMouvement["raison_id"] == "3" or $dernierMouvement["raison_id"] == "15")) {
  2003.                 if ($dernierMouvement["stockMvt"] == 0) {
  2004.                     return 0;
  2005.                 } else {
  2006.                     $pump = ($dernierMouvement["quantite"] * $dernierMouvement["prixAchat"]) / $dernierMouvement["stockMvt"];
  2007.                 }
  2008.             } //else if($dernierMouvement["action"] == "diminuer") {
  2009.             else {
  2010.                 $pump 0;
  2011.             }
  2012.         } else {
  2013.             $pump 0;
  2014.         }
  2015.         if ($pump == "0" or $pump == "") {
  2016.             $condHa $repo_condha->findOneBy(["article" => $article->getId(), "defaut" => 1]);
  2017.             if (is_object($condHa)) {
  2018.                 //echo "<div>CONDHA OK</div>";
  2019.                 $prix $condHa->getPrixAchatNet();
  2020.                 if ($condHa->getCoefficientConditionnement() != 0) {
  2021.                     $prix $prix $condHa->getCoefficientConditionnement();
  2022.                 }
  2023.                 $tauxChange  1;
  2024.                 $fournisseur $condHa->getFournisseur();
  2025.                 if (is_object($fournisseur)) {
  2026.                     $devise $fournisseur->getDevise();
  2027.                 }
  2028.                 if (is_object($devise) && $devise->getTauxChange() != 0) {
  2029.                     $tauxChange $devise->getTauxChange();
  2030.                 }
  2031.                 $prix $prix $tauxChange;
  2032.                 $pump $prix;
  2033.             } else {
  2034.                 $condHa $repo_condha->findOneBy(["article" => $article->getId()]);
  2035.                 if (is_object($condHa)) {
  2036.                     //echo "<div>CONDHA OK</div>";
  2037.                     $prix $condHa->getPrixAchatNet();
  2038.                     if ($condHa->getCoefficientConditionnement() != 0) {
  2039.                         $prix $prix $condHa->getCoefficientConditionnement();
  2040.                     }
  2041.                     $tauxChange  1;
  2042.                     $fournisseur $condHa->getFournisseur();
  2043.                     if (is_object($fournisseur)) {
  2044.                         $devise $fournisseur->getDevise();
  2045.                     }
  2046.                     if (is_object($devise) && $devise->getTauxChange() != 0) {
  2047.                         $tauxChange $devise->getTauxChange();
  2048.                     }
  2049.                     $prix $prix $tauxChange;
  2050.                     $pump $prix;
  2051.                 } else {
  2052.                     $pump 0;
  2053.                 }
  2054.             }
  2055.         }
  2056.         if ($pump == "") {
  2057.             return 0;
  2058.         } else {
  2059.             return $pump;
  2060.         }
  2061.         $pump         0;
  2062.         $totalTmp     0;
  2063.         $totalPrixTmp 0;
  2064.         //echo $quantiteTotal;
  2065.         if ($quantiteTotal <= 0) {
  2066.             if (is_object($article)) {
  2067.                 $quantiteTotal $article->getStock();
  2068.             }
  2069.         }
  2070.         if (count($stocks) > && $quantiteTotal 0) {
  2071.             //$pump = $stocks[0]->getPrixAchat();
  2072.             //$quantiteTotal = $stocks[0]['stock'];
  2073.             $quantiteTmp $quantiteTotal;
  2074.             foreach ($stocks as $stock)//reverse
  2075.             {
  2076.                 $quantite $stock['quantite'];
  2077.                 $prix     $stock['prixAchat'];
  2078.                 //var_dump($quantite.' -> '.$prix);
  2079.                 /*
  2080.                 if (($quantiteTmp-$quantite) >= 0){
  2081.                     $pump += $quantite * $prix;
  2082.                     $totalTmp += $quantite;
  2083.                 }else{
  2084.                     $pump += $quantiteTmp * $prix;
  2085.                     $totalTmp += $quantiteTmp;
  2086.                 }
  2087.                 $quantiteTmp -= $quantite;
  2088.                 if($quantiteTmp <= 0) break;
  2089.                 */
  2090.                 $totalPrixTmp += $quantite $prix;
  2091.                 $totalTmp     += $quantite;
  2092.             }
  2093.             if ($totalTmp 0) {
  2094.                 $pump $totalPrixTmp $totalTmp;
  2095.             }
  2096.         }
  2097.         //echo "PUMP ".$pump;
  2098.         //exit;
  2099.         return $pump;
  2100.     }
  2101.     public function getPumpR($id$date ''$quantiteTotal 0$output '')
  2102.     {
  2103.         $article $this->getEntityManager()->getRepository(Article::class)->find($id);
  2104.         /*
  2105.         if(is_object($article) && $article->getStock() > 0 && $date != '')
  2106.             $nbResultats = floatval($article->getStock());
  2107.         else
  2108.         */
  2109.         $nbResultats 100;
  2110.         $where       "WHERE a.id = :id and m.prixAchat is not null and r.action = 'augmenter' and m.quantite > 0 and c.id IS NULL and r.id NOT IN (19)";//and m.fournisseur is not null
  2111.         if ($date != '') {
  2112.             $where .= " and m.dateMvt <= '".$date->format("Y-m-d H:i:s")."'";
  2113.         }
  2114.         $stocks   $this->getEntityManager()
  2115.                          ->createQuery(
  2116.                              "select a.stock, m.quantite, m.prixAchatUnitaireAvecFrais as prixAchat, m.prixAchat as prixAchat2
  2117.                 FROM App\Entity\Articles\Article a
  2118.                 LEFT JOIN App\Entity\Articles\MouvementStock m with m.article = a.id
  2119.                 LEFT JOIN App\Entity\GestionComerciale\Commande c with m.commande = c.id
  2120.                 LEFT JOIN App\Entity\Articles\RaisonMouvementStock r with m.raisonMouvementStock = r.id
  2121.                 ".$where."
  2122.                 ORDER BY m.date DESC"
  2123.                          )
  2124.                          ->setParameters(['id' => $id])
  2125.                          ->setMaxResults($nbResultats)
  2126.                          ->getResult();
  2127.         $pump     0;
  2128.         $totalTmp 0;
  2129.         //echo $quantiteTotal;
  2130.         if ($quantiteTotal <= 0) {
  2131.             if (is_object($article)) {
  2132.                 $quantiteTotal $article->getStock();
  2133.             }
  2134.         }
  2135.         if (count($stocks) > && $quantiteTotal 0) {
  2136.             //$pump = $stocks[0]->getPrixAchat();
  2137.             //$quantiteTotal = $stocks[0]['stock'];
  2138.             $quantiteTmp $quantiteTotal;
  2139.             foreach ($stocks as $stock)//reverse
  2140.             {
  2141.                 $quantite $stock['quantite'];
  2142.                 $prix     $stock['prixAchat'];
  2143.                 if (is_null($prix)) {
  2144.                     $prix $stock['prixAchat2'];
  2145.                 }
  2146.                 if (is_object($output)) {
  2147.                     $output->writeln('qte : '.$quantite.' / prix achat : '.$prix);
  2148.                 }
  2149.                 //var_dump($quantite.' -> '.$prix);
  2150.                 /*
  2151.                                     if (($quantiteTmp-$quantite) >= 0){
  2152.                                         $pump += $quantite * $prix;
  2153.                                         $totalTmp += $quantite;
  2154.                                     }else{
  2155.                                         $pump += $quantiteTmp * $prix;
  2156.                                         $totalTmp += $quantiteTmp;
  2157.                                     }
  2158.                                     */
  2159.                 if ($quantite >= $quantiteTmp) {
  2160.                     $pump     += $quantiteTmp $prix;
  2161.                     $totalTmp += $quantiteTmp;
  2162.                 } else {
  2163.                     $pump     += $quantite $prix;
  2164.                     $totalTmp += $quantite;
  2165.                 }
  2166.                 $quantiteTmp -= $quantite;
  2167.                 if ($quantiteTmp <= 0) {
  2168.                     break;
  2169.                 }
  2170.             }
  2171.             if ($totalTmp 0) {
  2172.                 $pump $pump $totalTmp;
  2173.             }
  2174.         }
  2175.         return $pump;
  2176.     }
  2177.     public function getPumpHorsFrais($id$date ''$quantiteTotal 0)
  2178.     {
  2179.         $article $this->getEntityManager()->getRepository(Article::class)->find($id);
  2180.         if (is_object($article) && $article->getStock() > && $date != '') {
  2181.             $nbResultats floatval($article->getStock());
  2182.         } else {
  2183.             $nbResultats 30;
  2184.         }
  2185.         $where "WHERE a.id = :id and m.prixAchat is not null and r.action = 'augmenter' and m.quantite > 0 and c.id IS NULL and r.id NOT IN (19)";//and m.fournisseur is not null
  2186.         if ($date != '') {
  2187.             $where .= " and m.dateMvt <= '".$date->format("Y-m-d H:i:s")."'";
  2188.         }
  2189.         $stocks   $this->getEntityManager()
  2190.                          ->createQuery(
  2191.                              "select a.stock, m.quantite, m.prixAchat
  2192.                 FROM App\Entity\Articles\Article a
  2193.                 LEFT JOIN App\Entity\Articles\MouvementStock m with m.article = a.id
  2194.                 LEFT JOIN App\Entity\GestionComerciale\Commande c with m.commande = c.id
  2195.                 LEFT JOIN App\Entity\Articles\RaisonMouvementStock r with m.raisonMouvementStock = r.id
  2196.                 ".$where."
  2197.                 ORDER BY m.date DESC"
  2198.                          )
  2199.                          ->setParameters(['id' => $id])
  2200.                          ->setMaxResults($nbResultats)
  2201.                          ->getResult();
  2202.         $pump     0;
  2203.         $totalTmp 0;
  2204.         //echo $quantiteTotal;
  2205.         if ($quantiteTotal <= 0) {
  2206.             if (is_object($article)) {
  2207.                 $quantiteTotal $article->getStock();
  2208.             }
  2209.         }
  2210.         if (count($stocks) > && $quantiteTotal 0) {
  2211.             //$pump = $stocks[0]->getPrixAchat();
  2212.             //$quantiteTotal = $stocks[0]['stock'];
  2213.             $quantiteTmp $quantiteTotal;
  2214.             foreach ($stocks as $stock)//reverse
  2215.             {
  2216.                 $quantite $stock['quantite'];
  2217.                 $prix     $stock['prixAchat'];
  2218.                 //var_dump($quantite.' -> '.$prix);
  2219.                 /*
  2220.                 if (($quantiteTmp-$quantite) >= 0){
  2221.                     $pump += $quantite * $prix;
  2222.                     $totalTmp += $quantite;
  2223.                 }else{
  2224.                     $pump += $quantiteTmp * $prix;
  2225.                     $totalTmp += $quantiteTmp;
  2226.                 }
  2227.                 */
  2228.                 if ($quantite >= $quantiteTmp) {
  2229.                     $pump     += $quantiteTmp $prix;
  2230.                     $totalTmp += $quantiteTmp;
  2231.                 } else {
  2232.                     $pump     += $quantite $prix;
  2233.                     $totalTmp += $quantite;
  2234.                 }
  2235.                 $quantiteTmp -= $quantite;
  2236.                 if ($quantiteTmp <= 0) {
  2237.                     break;
  2238.                 }
  2239.             }
  2240.             if ($totalTmp 0) {
  2241.                 $pump $pump $totalTmp;
  2242.             }
  2243.         }
  2244.         return $pump;
  2245.     }
  2246.     public function gethistoriqueVente($articleId)//ajouter filtre statut commande
  2247.     {
  2248.         $histo $this->getEntityManager()
  2249.                       ->createQuery(
  2250.                           "select c.reference, c.date, ac.quantite
  2251.                 FROM App\Entity\GestionComerciale\Commande c
  2252.                 JOIN App\Entity\GestionComerciale\ArticleCommande ac with ac.commande = c.id"
  2253.                 ." WHERE ac.article = :id and c.typeDocumentCommercial = ".TypeDocumentCommercial::COMMANDE."
  2254.                 ORDER BY c.date DESC"
  2255.                       )
  2256.                       ->setParameters(['id' => $articleId])
  2257.                       ->setMaxResults(5)
  2258.                       ->getResult();
  2259.         return $histo;
  2260.     }
  2261.     public function getNbArticlesVendus($articleId$periode)//ajouter filtre statut commande
  2262.     {
  2263.         $date = new DateTime();
  2264.         $date->sub(new DateInterval('P'.$periode.'D'));
  2265.         $nbArticlesVendus $this->getEntityManager()
  2266.                                  ->createQuery(
  2267.                                      "select SUM(ac.quantite) as quantite
  2268.                 FROM App\Entity\GestionComerciale\Commande c
  2269.                 JOIN App\Entity\GestionComerciale\ArticleCommande ac with ac.commande = c.id
  2270.                 WHERE ac.article = :id and c.date > :date and c.typeDocumentCommercial = ".TypeDocumentCommercial::COMMANDE."
  2271.                 ORDER BY c.date DESC"
  2272.                                  )
  2273.                                  ->setParameters(['id' => $articleId'date' => $date])
  2274.                                  ->getResult();
  2275.         return $nbArticlesVendus;
  2276.     }
  2277.     public function getNbArticlesVendusMois($articleId$m$y)//ajouter filtre statut commande
  2278.     {
  2279.         $dateDebut = new DateTime($y.'-'.$m.'-01 00:00:00');
  2280.         $date      = new DateTime($y.'-'.$m.'-01 00:00:00');
  2281.         //$date = new \DateTime('01/'.$m.'/'.$y);
  2282.         //$date = new \DateTime($m.'/01/'.$y);
  2283.         $periode $this->nbJoursMois($m$y);
  2284.         $dateFin $date->add(new DateInterval('P'.$periode.'D'));
  2285.         //$dateFin = $date->sub(new \DateInterval('P'.$periode.'D'));
  2286.         //return $dateDebut->format('d-m-Y').'/'.$dateFin->format('d-m-Y');
  2287.         $nbArticlesVendus $this->getEntityManager()
  2288.                                  ->createQuery(
  2289.                                      "select SUM(ac.quantite) as quantite
  2290.                 FROM App\Entity\GestionComerciale\Commande c
  2291.                 JOIN App\Entity\GestionComerciale\ArticleCommande ac with ac.commande = c.id
  2292.                 WHERE ac.article = :id and c.date >= :dateDebut and c.date < :dateFin and c.typeDocumentCommercial = ".TypeDocumentCommercial::COMMANDE."
  2293.                 and c.statutCommande != :statutCommande
  2294.                 "
  2295.                                  )
  2296.                                  ->setParameters(['id' => $articleId'dateDebut' => $dateDebut'dateFin' => $dateFin'statutCommande' => '7'])//
  2297.                                  ->getResult();
  2298.         return $nbArticlesVendus;
  2299.     }
  2300.     public function getArticlesAcommander()
  2301.     {
  2302.         $query $this->createQueryBuilder('p')
  2303.                       ->select("p.libelle, p.id, p.stock, p.seuilMiniCommandeFournisseur, p.seuilMaxiCommandeFournisseur")
  2304.             //->innerJoin('p.fournisseur', 'f', 'WITH', 'p.fournisseur = f.id')
  2305.             //->join("App\Entity\Fournisseurs\Four f")
  2306.                       ->where('p.stock < p.seuilMiniCommandeFournisseur and p.archive = 0')
  2307.             //->setParameters(array('cond' => $cond))
  2308.             //->orderBy('p.prenom', 'ASC');
  2309.             //->setFirstResult(0)
  2310.             //->setMaxResults(5)
  2311.             //->orderBy('p.id', 'DESC')
  2312.         ;
  2313.         $res $query->getQuery()->getResult();
  2314.         return $res;
  2315.     }
  2316.     public function getArticlesAcommanderGroupByFournisseur()
  2317.     {
  2318.         $query $this->createQueryBuilder('p')
  2319.                       ->select(
  2320.                           "COALESCE(SUM(DISTINCT(ac.ral)),0) as ralTotal, (COALESCE(SUM(DISTINCT(ac.ral)),0) + p.stock) as stockVirtuel, p.libelle, p.id, p.stock, p.seuilMiniCommandeFournisseur, p.seuilMaxiCommandeFournisseur, f.id as fournisseurId, f.libelle as fournisseur"
  2321.                       )
  2322.             //->leftJoin('p.fournisseurs', 'f')//, 'WITH', 'p.fournisseur = f.id'
  2323.                       ->leftJoin('p.mouvementStock''ms')
  2324.                       ->join('ms.fournisseur''f')
  2325.                       ->leftJoin(ArticleCommande::class, 'ac''with''ac.article = p.id')
  2326.                       ->leftJoin('ac.commandeFournisseur''c''with''c.statutCommande IN (1,2)')
  2327.             //->join("App\Entity\Fournisseurs\Four f")
  2328.                       ->where('p.archive = :archive')
  2329.                       ->setParameters(['archive' => "0"])
  2330.                       ->groupBy('f.id, p.id')
  2331.                       ->having('stockVirtuel < p.seuilMiniCommandeFournisseur')//p.stock + ral fournisseur < p.seuilMiniCommandeFournisseur
  2332.         ;
  2333.         $res $query->getQuery()->getResult();
  2334.         return $res;
  2335.     }
  2336.     public function getNbArticlesVendusParMois($articleId$nbMois)//ajouter filtre statut commande
  2337.     {
  2338.         $date  = new DateTime();
  2339.         $mois  = (int)$date->format('m');
  2340.         $annee = (int)$date->format('Y');
  2341.         //$date->sub(new \DateInterval('P'.$periode.'D'));
  2342.         $moisTmp $mois;
  2343.         for ($i 1$i <= $nbMois$i++) {
  2344.             if ($moisTmp 10) {
  2345.                 $id $annee.'-0'.$moisTmp;
  2346.             } else {
  2347.                 $id $annee.'-'.$moisTmp;
  2348.             }
  2349.             $nbArticlesVendusMois $this->getNbArticlesVendusMois($articleId$moisTmp$annee);
  2350.             if ($nbArticlesVendusMois[0]['quantite'] != '') {
  2351.                 $retour[$id] = $nbArticlesVendusMois[0]['quantite'];
  2352.             } else {
  2353.                 $retour[$id] = 0;
  2354.             }
  2355.             //$retour[$nbArticlesVendusMois] = 10;
  2356.             if ($moisTmp 0) {
  2357.                 $moisTmp -= 1;
  2358.             } else {
  2359.                 $moisTmp 12;
  2360.                 $annee   -= 1;
  2361.             }
  2362.         }
  2363.         return array_reverse($retour);
  2364.     }
  2365.     public function getQuantitePrixMoyenVendusMois($articleId$m$y)//ajouter filtre statut commande
  2366.     {
  2367.         $dateDebut = new DateTime($y.'-'.$m.'-01 00:00:00');
  2368.         $dateFin   = new DateTime($y.'-'.$m.'-01 00:00:00');
  2369.         //$date = new \DateTime('01/'.$m.'/'.$y);
  2370.         //$date = new \DateTime($m.'/01/'.$y);
  2371.         $periode $this->nbJoursMois($m$y);
  2372.         $dateFin->add(new DateInterval('P'.$periode.'D'));
  2373.         //$dateFin = $date->sub(new \DateInterval('P'.$periode.'D'));
  2374.         //return $dateDebut->format('d-m-Y').'/'.$dateFin->format('d-m-Y');
  2375.         /*
  2376.          * $nbArticlesVendus = $this->getEntityManager()
  2377.             ->createQuery(
  2378.                 "select SUM(ac.quantite) as quantite, SUM(ac.totalHt) as totalHt
  2379.                 FROM App\Entity\GestionComerciale\Commande c
  2380.                 JOIN App\Entity\GestionComerciale\ArticleCommande ac with ac.commande = c.id
  2381.                 WHERE ac.article = :id and c.date >= :dateDebut and c.date < :dateFin and c.typeDocumentCommercial = ".TypeDocumentCommercial::COMMANDE."
  2382.                 "
  2383.             )
  2384.          */
  2385.         $nbArticlesVendus $this->getEntityManager()
  2386.                                  ->createQuery(
  2387.                                      "select SUM(ac.quantite) as quantite, SUM(ac.totalHt) as totalHt
  2388.                 FROM App\Entity\GestionComerciale\ArticleCommande ac
  2389.                 JOIN App\Entity\GestionComerciale\Commande c with ac.commande = c.id
  2390.                 WHERE ac.article = :id and c.date >= :dateDebut and c.date < :dateFin and c.statutCommande = 22
  2391.                 "
  2392.                                  )
  2393.                                  ->setParameters(['id' => $articleId'dateDebut' => $dateDebut'dateFin' => $dateFin])//
  2394.                                  ->getResult();
  2395.         return $nbArticlesVendus;
  2396.     }
  2397.     public function getPrixMoyenAchatMois($articleId$m$y)
  2398.     {
  2399.         $dateDebut = new DateTime($y.'-'.$m.'-01 00:00:00');
  2400.         $date      = new DateTime($y.'-'.$m.'-01 00:00:00');
  2401.         //$date = new \DateTime('01/'.$m.'/'.$y);
  2402.         //$date = new \DateTime($m.'/01/'.$y);
  2403.         $periode $this->nbJoursMois($m$y);
  2404.         $dateFin $date->add(new DateInterval('P'.$periode.'D'));
  2405.         //$dateFin = $date->sub(new \DateInterval('P'.$periode.'D'));
  2406.         //return $dateDebut->format('d-m-Y').'/'.$dateFin->format('d-m-Y');
  2407.         $prixMoyenAchatResult $this->getEntityManager()
  2408.                                      ->createQuery(
  2409.                                          "select COUNT(ms.id) as total, SUM(ms.prixAchat) as prix
  2410.                 FROM App\Entity\Articles\MouvementStock ms
  2411.                 WHERE ms.article = :id and ms.dateMvt >= :dateDebut and ms.dateMvt < :dateFin and ms.raisonMouvementStock = 3
  2412.                 "
  2413.                                      )
  2414.                                      ->setParameters(['id' => $articleId'dateDebut' => $dateDebut'dateFin' => $dateFin])//
  2415.                                      ->getResult();
  2416.         $prixMoyenAchat       0;
  2417.         if ($prixMoyenAchatResult[0]['total'] != 0) {
  2418.             $prixMoyenAchat $prixMoyenAchatResult[0]['prix'] / $prixMoyenAchatResult[0]['total'];
  2419.         }
  2420.         return $prixMoyenAchat;
  2421.     }
  2422.     public function getPrixAchatVenteParMois($articleId$nbMois)//ajouter filtre statut commande
  2423.     {
  2424.         $date  = new DateTime();
  2425.         $mois  = (int)$date->format('m');
  2426.         $annee = (int)$date->format('Y');
  2427.         //$date->sub(new \DateInterval('P'.$periode.'D'));
  2428.         $moisTmp $mois;
  2429.         for ($i 1$i <= $nbMois$i++) {
  2430.             if ($moisTmp 10) {
  2431.                 $id $annee.'-0'.$moisTmp;
  2432.             } else {
  2433.                 $id $annee.'-'.$moisTmp;
  2434.             }
  2435.             $quantitePrixVenteMois $this->getQuantitePrixMoyenVendusMois($articleId$moisTmp$annee);
  2436.             if ($quantitePrixVenteMois[0]['totalHt'] != '' && $quantitePrixVenteMois[0]['quantite'] != '' && $quantitePrixVenteMois[0]['quantite'] != 0) {
  2437.                 $retour[$id]['vente']['prix'] = $quantitePrixVenteMois[0]['totalHt'] / $quantitePrixVenteMois[0]['quantite'];
  2438.             } else {
  2439.                 $retour[$id]['vente']['prix'] = 0;
  2440.             }
  2441.             $retour[$id]['achat']['prix'] = $this->getPrixMoyenAchatMois($articleId$moisTmp$annee);
  2442.             //$retour[$nbArticlesVendusMois] = 10;
  2443.             if ($moisTmp 0) {
  2444.                 $moisTmp -= 1;
  2445.             } else {
  2446.                 $moisTmp 12;
  2447.                 $annee   -= 1;
  2448.             }
  2449.         }
  2450.         return array_reverse($retour);
  2451.     }
  2452.     private function nbJoursMois($m$y)
  2453.     {
  2454.         $mois mktime(000$m1$y);
  2455.         //setlocale('LC_ALL', 'fr_FR');
  2456.         return date("t"$mois);
  2457.     }
  2458.     public function findLibelleOrRefLikeInConsigne($query$articleId)
  2459.     {
  2460.         return $this->getEntityManager()
  2461.                     ->createQuery(
  2462.                         "select r.id, CONCAT(r.libelle, '[', COALESCE(r.reference, ''), ']') as libelle FROM App\Entity\Articles\Article r WHERE r.archive = 0 and r.reference LIKE '%".$query."%' and r.consigne = 1"
  2463.                     )
  2464.                     ->getResult();
  2465.     }
  2466.     public function getArticlesInternesAFabriquer()
  2467.     {
  2468.         $query $this->createQueryBuilder('p')
  2469.                       ->select("p.id, p.reference, p.description, (p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) as quantiteAFabriquer")
  2470.                       ->leftJoin('p.articlesComposants''ac')
  2471.                       ->where(
  2472.                           '(p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) < p.seuilMiniCommandeFournisseur and (p.produitInterne = 1 OR p.virtuel = 1) and ac.id is not null and p.archive = 0'
  2473.                       );
  2474.         $res   $query->getQuery()->getResult();
  2475.         return $res;
  2476.     }
  2477.     public function getArticlesAfabriquerSeuilMini()
  2478.     {
  2479.         $query $this->createQueryBuilder('p')
  2480.             //->select("p.id, p.reference, p.description, (p.stock-COALESCE(p.stockReserveFabrication,0)+COALESCE(p.ralFournisseur,0)) as quantiteAFabriquer")
  2481.             //->leftJoin('p.articlesComposants', 'ac')
  2482.                       ->where(
  2483.                 '(p.stock-COALESCE(p.stockReserveFabrication,0)+COALESCE(p.ralFournisseur,0)) < p.seuilMiniCommandeFournisseur and (p.produitInterne = 1 OR p.virtuel = 1) and (p.archive = 0 OR p.archive IS NULL)'
  2484.             );
  2485.         $res   $query->getQuery()->getResult();
  2486.         return $res;
  2487.     }
  2488.     public function getQuantiteAFabriquer($article)
  2489.     {
  2490.         $query $this->createQueryBuilder('p')
  2491.                       ->select("(p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) as quantiteAFabriquer")
  2492.                       ->where('p.id = :id')
  2493.                       ->setParameters(['id' => $article->getId()]);
  2494.         $res   $query->getQuery()->getResult();
  2495.         if ($article->getVirtuel() == 1//Pas de stock
  2496.         {
  2497.             $seuilMini 0;
  2498.         } else {
  2499.             $seuilMini $article->getSeuilMiniCommandeFournisseur();
  2500.         }
  2501.         $quantiteMini $seuilMini $res[0]['quantiteAFabriquer'];
  2502.         $repo_condha $this->getEntityManager()->getRepository(ConditionAchat::class);
  2503.         $condHa $repo_condha->findOneBy(["defaut" => "1""fournisseur" => 132"article" => $article]);
  2504.         if (is_object($condHa)) {
  2505.             $qteCondHa $condHa->getQuantite();
  2506.             if ($qteCondHa $quantiteMini) {
  2507.                 $quantiteMini $qteCondHa;
  2508.             }
  2509.         }
  2510.         return $quantiteMini;
  2511.     }
  2512.     public function getClientsEnRal(Article $article)
  2513.     {
  2514.         $typeDocument = [TypeDocumentCommercial::COMMANDETypeDocumentCommercial::ORDREREPARATION];
  2515.         $query        $this->createQueryBuilder('p')
  2516.                              ->select("DISTINCT(cl.id) as id")
  2517.                              ->leftJoin(ArticleCommande::class, 'ac''with''ac.article = p.id')
  2518.                              ->join('ac.commande''c')
  2519.                              //->leftJoin(TypeDocumentCommercial::class, 'td', 'with', 'td.id = c.typeDocumentCommercial')
  2520.                              ->leftJoin(StatutCommande::class, 'sc''with''sc.id = c.statutCommande')
  2521.                              ->join('c.client''cl')
  2522.                              ->where('ac.ralClient > 0 and p.id = :id and c.typeDocumentCommercial IN (:typeDocument) and sc.ordre != 0')
  2523.                              ->setParameters(['id' => $article->getId(), 'typeDocument' => $typeDocument]);
  2524.         $res          $query->getQuery()->getResult();
  2525.         return $res;
  2526.     }
  2527.     public function getBpPourCommandeFournisseur($articles$commentaire 1209715)
  2528.     {
  2529.         $query $this->createQueryBuilder('p')
  2530.                       ->select("DISTINCT(c.id) as id, c.reference")
  2531.                       ->leftJoin(ArticleCommande::class, 'ac''with''ac.article = p.id')
  2532.                       ->join('ac.commande''c')
  2533.                       ->leftJoin(TypeDocumentCommercial::class, 'td''with''td.id = c.typeDocumentCommercial')
  2534.             //->join('c.client', 'cl')
  2535.                       ->where('ac.ralClient > 0 and p.id IN (:ids) and td.id IN (:typeDocument) and c.visible=1 and p.id != :com ')
  2536.                       ->setParameters(['ids' => $articles'typeDocument' => [4], 'com' => $commentaire]);
  2537.         $res   $query->getQuery()->getResult();
  2538.         //print_r($res);
  2539.         return $res;
  2540.         //return array(0=>array('id'=>1));
  2541.     }
  2542.     public function getCommandes($articles)
  2543.     {
  2544.         $query $this->createQueryBuilder('p')
  2545.                       ->select("DISTINCT(c.id) as id, c.reference")
  2546.                       ->leftJoin(ArticleCommande::class, 'ac''with''ac.article = p.id')
  2547.                       ->join('ac.commande''c')
  2548.                       //->leftJoin(TypeDocumentCommercial::class, 'td', 'with', 'td.id = c.typeDocumentCommercial')
  2549.                       ->join('c.client''cl')
  2550.                       ->where('ac.ralClient > 0 and p.id IN (:ids) and c.typeDocumentCommercial IN (:typeDocument)')
  2551.                       ->setParameters(['ids' => $articles'typeDocument' => [23]]);
  2552.         $res   $query->getQuery()->getResult();
  2553.         return $res;
  2554.         //return array(0=>array('id'=>1));
  2555.     }
  2556.     public function updateACommander()//ajouter filtre statut commande
  2557.     {
  2558.         $query $this->createQueryBuilder('p')
  2559.                       ->update(Article::class, 'p')
  2560.                       ->set('p.aCommander''0')
  2561.                       ->where('p.aCommander = 1 ')
  2562.                       ->getQuery()
  2563.                       ->execute();
  2564.         $query $this->createQueryBuilder('p')
  2565.                       ->update(Article::class, 'p')
  2566.                       ->set('p.aCommander''1')
  2567.                       ->join('p.conditionsAchat''cond')
  2568.             //->where('(p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) < COALESCE(p.seuilMiniCommandeFournisseur,0) and p.produitInterne != 1 and p.virtuel != 1 and p.consigne != 1')
  2569.                       ->where(
  2570.                 '
  2571.                             (p.virtuel != 1 or p.virtuel IS NULL)
  2572.                             and (p.consigne != 1 or p.consigne IS NULL)
  2573.                             and (p.nonStocke != 1 or p.nonStocke IS NULL)
  2574.                             and (p.nonCommandable != 1 or p.nonCommandable IS NULL)
  2575.                             and (p.arretGamme != 1 or p.arretGamme IS NULL)
  2576.                             and (p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) < COALESCE(p.seuilMiniCommandeFournisseur,0)
  2577.                             '
  2578.             //  nonStocke nonCommandable
  2579.             //and (p.produitInterne != 1 or p.produitInterne IS NULL)
  2580.             )
  2581.                       ->getQuery()
  2582.                       ->execute();
  2583.     }
  2584.     public function updateASuggerer()
  2585.     {
  2586.         $query $this->createQueryBuilder('p')
  2587.                       ->update(Article::class, 'p')
  2588.                       ->set('p.aSuggerer''0')
  2589.                       ->where('p.aSuggerer = 1 ')
  2590.                       ->getQuery()
  2591.                       ->execute();
  2592.         $query $this->createQueryBuilder('p')
  2593.                       ->update(Article::class, 'p')
  2594.                       ->set('p.aSuggerer''1')
  2595.                       ->join('p.conditionsAchat''cond')
  2596.             //->where('(p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) < COALESCE(p.seuilMiniCommandeFournisseur,0) and p.produitInterne != 1 and p.virtuel != 1 and p.consigne != 1')
  2597.                       ->where(
  2598.                 '
  2599.                             (p.virtuel != 1 or p.virtuel IS NULL)
  2600.                             and (p.consigne != 1 or p.consigne IS NULL)
  2601.                             and (p.nonStocke != 1 or p.nonStocke IS NULL)
  2602.                             and (p.nonCommandable != 1 or p.nonCommandable IS NULL)
  2603.                             and (p.arretGamme != 1 or p.arretGamme IS NULL)
  2604.                             and (p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) < (COALESCE(p.seuilMiniCommandeFournisseur,0) * 1.05)
  2605.                             and (p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) > COALESCE(p.seuilMiniCommandeFournisseur,0)
  2606.                             '
  2607.             //  nonStocke nonCommandable
  2608.             //and (p.produitInterne != 1 or p.produitInterne IS NULL)
  2609.             )
  2610.                       ->getQuery()
  2611.                       ->execute();
  2612.     }
  2613.     public function historiquePrix($article)
  2614.     {
  2615.         $sql "
  2616.             select
  2617.             p.date_maj,
  2618.             p.libelle,
  2619.             p.reference,
  2620.             p.utilisateur_id,
  2621.             p.prix_base,
  2622.             p.prix_vente,
  2623.             p.coefficient_px_base_px_vente,
  2624.             p.marge,
  2625.             p.taux_marge,
  2626.             p.pump,
  2627.             p.cpump,
  2628.             p.marge_reelle,
  2629.             u.prenom as equipier_prenom,
  2630.             u.nom as equipier_nom
  2631.             FROM article__article_audit as p
  2632.             LEFT JOIN utilisateur__utilisateurs as u on u.id = p.utilisateur_id
  2633.             WHERE
  2634.             p.id = ".$article->getId()."
  2635.             and p.date_maj_prix is NOT NULL
  2636.             GROUP BY p.date_maj_prix
  2637.             ORDER BY p.rev DESC
  2638.            ";
  2639.         //echo $sql;
  2640.         $em   $this->getEntityManager();
  2641.         $stmt $em->getConnection()->prepare($sql);
  2642.         $stmt->execute();
  2643.         $prix $stmt->fetchAll();
  2644.         return $prix;
  2645.         //print_r($prix);
  2646.     }
  2647.     public function setMiniMaxi($ids$donnees)
  2648.     {
  2649.         //print_r($donnees);
  2650.         if ( ! array_key_exists('stockAuto'$donnees)) {
  2651.             $donnees['stockAuto'] = 0;
  2652.         }
  2653.         $chaine "";
  2654.         for ($i 0$i count($ids); $i++) {
  2655.             $chaine .= $ids[$i].",";
  2656.         }
  2657.         $chaine .= ",";
  2658.         $chaine str_replace(",,"""$chaine);
  2659.         $sql  " UPDATE
  2660.                  article__article
  2661.                  SET
  2662.                  seuil_commande_f_mini = ".floatval($donnees['mini']).",
  2663.                  seuil_commande_f_maxi = ".floatval($donnees['maxi']).",
  2664.                  stock_auto = ".$donnees['stockAuto']."
  2665.                  WHERE
  2666.                  id IN (".$chaine.")
  2667.                 ";
  2668.         $em   $this->getEntityManager();
  2669.         $stmt $em->getConnection()->prepare($sql);
  2670.         $stmt->execute();
  2671.     }
  2672.     public function valorisation($parametres)
  2673.     {
  2674.         $where "";
  2675.         if (array_key_exists('date'$parametres) && $parametres['date'] != "") {
  2676.             //$dateDebut = new \Datetime($param['param']['date']);
  2677.             $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date']);
  2678.             $dateDebut->setTime(235959);
  2679.         } else {
  2680.             $dateDebut = new DateTime();
  2681.             $dateDebut->setTime(235959);
  2682.         }
  2683.         if (array_key_exists('article'$parametres) && $parametres['article'] != "") {
  2684.             if ($where != "") {
  2685.                 $where .= " and ";
  2686.             }
  2687.             $where .= "p.reference like '".$parametres['article']."%'";
  2688.         }
  2689.         if (array_key_exists('marque'$parametres) && $parametres['marque'] != "") {
  2690.             if ($where != "") {
  2691.                 $where .= " and ";
  2692.             }
  2693.             $where .= "p.marque_id = ".$parametres['marque']."";
  2694.         }
  2695.         /*
  2696.         if(array_key_exists('inventaire', $parametres) && $parametres['inventaire'] != "") {
  2697.             if($where != "") $where .= " and ";
  2698.             $where .= "p.inventaire_id = ".$parametres['inventaire']."";
  2699. }
  2700.          *
  2701.          */
  2702.         if (array_key_exists('stock'$parametres) && $parametres['stock'] == "") {
  2703.             $parametres['stock'] = 0;
  2704.         }
  2705.         if (array_key_exists('stock'$parametres) && $parametres['stock'] == 0) {
  2706.             if ($where != "") {
  2707.                 $where .= " and ";
  2708.             }
  2709.             //$where .= "p.stock > 0";
  2710.             $where .= "article__mouvement_stock.stock > 0";
  2711.         }
  2712.         $orderBy "";
  2713.         if (array_key_exists('tri'$parametres) && $parametres['tri'] != "") {
  2714.             $orderBy ' ORDER BY p.'.$parametres['tri'].' ASC';
  2715.         }
  2716.         //echo $parametres['stock']." Date debut ".$dateDebut->format("d-m-Y H:i:s");
  2717.         /*
  2718.                $sql = "
  2719.                    select
  2720.                    p.id,
  2721.                    p.libelle,
  2722.                    p.reference,
  2723.                    p.stock,
  2724.                    p.pump,
  2725.                    s.stock as stock_date,
  2726.                    s.id as mv_id,
  2727.                    s.date_mouvement
  2728.                    FROM article__article as p
  2729.                    LEFT JOIN (select article_id,date_mouvement,stock,id FROM `article__mouvement_stock`  ORDER BY `date_mouvement` DESC) as `s` ON s.article_id=p.id
  2730.                    WHERE (s.date_mouvement <= '".$dateDebut->format("Y-m-d H:i:s")."' or s.date_mouvement is null)
  2731.                    ".$where."
  2732.                    GROUP BY p.id
  2733.                    ORDER BY s.date_mouvement DESC
  2734.                    LIMIT 0,5000
  2735.                   ";
  2736.                   */
  2737.         /*
  2738.        $sql = "
  2739.        select  p.id as article_id, p.libelle, p.reference, p.stock, p.pump,p.cpump, lastMessage.id,lastMessage.date_mouvement,article__mouvement_stock.id,article__mouvement_stock.stock as stock_date
  2740.        FROM    article__article as p
  2741.            LEFT JOIN (
  2742.            select MAX(id) as id, MAX(date_mouvement) AS date_mouvement,article_id
  2743.            FROM article__mouvement_stock
  2744.            WHERE date_mouvement <= '".$dateDebut->format("Y-m-d H:i:s")."'
  2745.            GROUP BY article_id
  2746.            ) AS lastMessage
  2747.            ON p.id = lastMessage.article_id
  2748.            LEFT JOIN article__mouvement_stock
  2749.            ON article__mouvement_stock.id = lastMessage.id";
  2750.                  *
  2751.                  */
  2752.         if (array_key_exists('inventaire'$parametres) && $parametres['inventaire'] != "" && == 0) {
  2753.             if ($where != "") {
  2754.                 $where .= " and ";
  2755.             }
  2756.             $where .= "ai.inventaire_id = ".$parametres['inventaire'];
  2757.             $sql "select p.id as article_id, p.libelle, p.reference, p.stock, p.pump,p.cpump,p.pump_hors_frais
  2758.         FROM inventaire__article_inventaire ai
  2759.                 LEFT JOIN article__article as p ON p.id = ai.article_id
  2760.                     ";
  2761.             if ($where != "") {
  2762.                 $sql .= " WHERE ".$where;
  2763.             }
  2764.         } else {
  2765.             $sql "
  2766.         select  p.id as article_id, p.libelle, p.reference, p.stock, p.pump,p.cpump,p.pump_hors_frais
  2767.         FROM    article__article as p
  2768.         LEFT JOIN article__mouvement_stock ON article__mouvement_stock.article_id = p.id    ";
  2769.             if ($where != "") {
  2770.                 $where .= " and ";
  2771.             }
  2772.             //$where .= "p.marque_id = ".$parametres['marque']."";
  2773.             $where .= "
  2774.                 (
  2775.                 (p.est_commentaire is null or p.est_commentaire = 0)
  2776.                 and
  2777.                 (p.virtuel is null or p.virtuel = 0)
  2778.                 and
  2779.                 (p.main_oeuvre is null or p.main_oeuvre = 0)
  2780.                 and
  2781.                 (p.non_stocke is null or p.non_stocke = 0)
  2782.                 and
  2783.                 (p.divers is null or p.divers = 0)
  2784.                 and
  2785.                 (p.divers_composes is null or p.divers_composes = 0)
  2786.                 )";
  2787.             if ($where != "") {
  2788.                 $sql .= " WHERE ".$where;
  2789.             }
  2790.             $sql .= " GROUP BY p.id";
  2791.             if ($orderBy != "") {
  2792.                 $sql .= $orderBy;
  2793.             } else {
  2794.                 $sql .= " ORDER BY article__mouvement_stock.date_mouvement DESC";
  2795.             }
  2796.         }
  2797.         //$sql .= " LIMIT 0,500";
  2798.         /*
  2799.         $sql = "
  2800.         select p.id, p.libelle, p.reference, p.stock, p.pump, s.stock as stock_date, s.id as mv_id, s.date_mouvement FROM article__article as p LEFT JOIN (select article_id,date_mouvement,stock,id FROM `article__mouvement_stock` WHERE (date_mouvement <= '2016-09-09 23:59:59' or date_mouvement is null) ORDER BY `date_mouvement` DESC) as `s` ON s.article_id=p.id WHERE p.reference like 'TOY-04465-YZZAL%' GROUP BY p.id ORDER BY s.date_mouvement DESC LIMIT 0,500
  2801.         ";
  2802.         */
  2803.         $sql trim($sql);
  2804.         //var_dump($sql);die;
  2805.         //echo "<br/><br/>".$sql;
  2806.         $em   $this->getEntityManager();
  2807.         $stmt $em->getConnection()->prepare($sql);
  2808.         $articles $stmt->executeQuery()->fetchAllAssociative();
  2809.         return $articles;
  2810.     }
  2811.     public function getPrixAchat($idArticle$date)
  2812.     {
  2813.         //$idArticle = "2345678904567890";
  2814.         $date DateTime::createFromFormat('d/m/Y'$date);
  2815.         $date->setTime(235959);
  2816.         //echo "<div>".$idArticle." DATE : ".$date->format("d/m/Y H:i:s")."</div>";
  2817.         $sql "
  2818.         select  ca.prix_achat_net, d.taux_change as taux_change
  2819.         FROM    article__condition_achat_audit as ca
  2820.                 LEFT JOIN fournisseur__fournisseur f ON f.id = ca.fournisseur_id
  2821.                 LEFT JOIN article__devise d ON d.id = f.devise_id
  2822.         WHERE
  2823.         ca.date <= '".$date->format("Y-m-d H:i:s")."'
  2824.         and
  2825.         ca.statut = 1
  2826.         and
  2827.         ca.article_id = ".$idArticle."
  2828.         ORDER BY rev DESC
  2829.         LIMIT 0,1
  2830.         ";
  2831.         $em   $this->getEntityManager();
  2832.         $stmt $em->getConnection()->prepare($sql);
  2833.         $prix $stmt->executeQuery()->fetchAllAssociative();
  2834.         if (count($prix) > 0) {
  2835.             $prixAchat $prix[0]['prix_achat_net'];
  2836.             if ($prix[0]['taux_change'] != && $prix[0]['taux_change'] != '0' && $prix[0]['taux_change'] != '') {
  2837.                 $prixAchat $prixAchat floatval($prix[0]['taux_change']);
  2838.             }
  2839.             return $prixAchat;
  2840.         } else {
  2841.             $sql "
  2842.             select  ca.prix_achat_net, d.taux_change as taux_change
  2843.             FROM    article__condition_achat as ca
  2844.                         LEFT JOIN fournisseur__fournisseur f ON f.id = ca.fournisseur_id
  2845.                         LEFT JOIN article__devise d ON d.id = f.devise_id
  2846.             WHERE
  2847.             ca.statut = 1
  2848.             and
  2849.             ca.article_id = ".$idArticle."
  2850.             ORDER BY ca.id DESC
  2851.             LIMIT 0,1
  2852.             ";
  2853.             $em   $this->getEntityManager();
  2854.             $stmt $em->getConnection()->prepare($sql);
  2855.             $prix $stmt->executeQuery()->fetchAllAssociative();
  2856.             if (count($prix) > 0) {
  2857.                 $prixAchat $prix[0]['prix_achat_net'];
  2858.                 if ($prix[0]['taux_change'] != && $prix[0]['taux_change'] != '0' && $prix[0]['taux_change'] != '') {
  2859.                     $prixAchat $prixAchat floatval($prix[0]['taux_change']);
  2860.                 }
  2861.                 return $prixAchat;
  2862.             } else {
  2863.                 $sql  "
  2864.                 select  ca.prix_base
  2865.                 FROM   `article__article_audit` ca
  2866.                 WHERE
  2867.                 ca.date <= '".$date->format("Y-m-d H:i:s")."'
  2868.                 and
  2869.                 ca.id = ".$idArticle."
  2870.                 ORDER BY rev DESC
  2871.                 LIMIT 0,1
  2872.                 ";
  2873.                 $em   $this->getEntityManager();
  2874.                 $stmt $em->getConnection()->prepare($sql);
  2875.                 $prix $stmt->executeQuery()->fetchAllAssociative();
  2876.                 if (count($prix) > 0) {
  2877.                     return $prix[0]['prix_base'];
  2878.                 } else {
  2879.                     $sql  "
  2880.                     select  ca.prix_base
  2881.                     FROM   `article__article` ca
  2882.                     WHERE
  2883.                     ca.date <= '".$date->format("Y-m-d H:i:s")."'
  2884.                     and
  2885.                     ca.id = ".$idArticle."
  2886.                     ";
  2887.                     $em   $this->getEntityManager();
  2888.                     $stmt $em->getConnection()->prepare($sql);
  2889.                     $prix $stmt->executeQuery()->fetchAllAssociative();
  2890.                     if (count($prix) > 0) {
  2891.                         return $prix[0]['prix_base'];
  2892.                     } else {
  2893.                         return "0";
  2894.                     }
  2895.                 }
  2896.             }
  2897.         }
  2898.     }
  2899.     public function getDernierPrixAchat($idArticle$date)
  2900.     {
  2901.         //$idArticle = "2345678904567890";
  2902.         $date DateTime::createFromFormat('d/m/Y'$date);
  2903.         $date->setTime(235959);
  2904.         //echo "<div>".$idArticle." DATE : ".$date->format("d/m/Y H:i:s")."</div>";
  2905.         $sql "
  2906.                 select ac.prix_base
  2907.                 FROM `commerciale__article_commande` as ac
  2908.                 LEFT JOIN commerciale__commande_fournisseur as c on ac.commande_fournisseur_id = c.id
  2909.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  2910.                 WHERE
  2911.                 c.date <= '".$date->format("Y-m-d H:i:s")."'
  2912.                 and
  2913.                 c.statut_commande_id = 6
  2914.                 and
  2915.                 ac.article_id = ".$idArticle."
  2916.                 ORDER BY c.date DESC
  2917.                 LIMIT 0,1
  2918.             ";
  2919.         $em   $this->getEntityManager();
  2920.         $stmt $em->getConnection()->prepare($sql);
  2921.         $prix $stmt->executeQuery()->fetchAllAssociative();
  2922.         if (count($prix) > 0) {
  2923.             return $prix[0]['prix_base'];
  2924.         } else {
  2925.             return "0";
  2926.         }
  2927.     }
  2928.     public function getArticleEquivalent($idArticle)
  2929.     {
  2930.         $sql  "
  2931.             select
  2932.             a.id,
  2933.             a.libelle,
  2934.             a.reference,
  2935.             m.libelle as marque
  2936.             FROM article_article as aa
  2937.             LEFT JOIN article__article as a on a.id = aa.article_target
  2938.             LEFT JOIN article__marque as m on m.id = a.marque_id
  2939.             WHERE
  2940.             aa.article_source = ".$idArticle."
  2941.            ";
  2942.         $em   $this->getEntityManager();
  2943.         $stmt $em->getConnection()->prepare($sql);
  2944.         $equivalences $stmt->executeQuery()->fetchAllAssociative();
  2945.         return $equivalences;
  2946.     }
  2947.     public function estUneEquivalenceReciproque($article_equivalent$article_source)
  2948.     {
  2949.         $sql "
  2950.             select
  2951.             aa.article_source
  2952.             FROM article_article as aa
  2953.             WHERE
  2954.             aa.article_source = ".$article_source."
  2955.             and
  2956.             aa.article_target = ".$article_equivalent."
  2957.            ";
  2958.         //echo $sql;
  2959.         $em   $this->getEntityManager();
  2960.         $stmt $em->getConnection()->prepare($sql);
  2961.         $reciproque $stmt->executeQuery()->fetchAllAssociative();
  2962.         if (count($reciproque) == 0) {
  2963.             return false;
  2964.         } else {
  2965.             return true;
  2966.         }
  2967.     }
  2968.     public function getArticlesAinventorierSansEmplacement($inventaire)
  2969.     {
  2970.         $date "";
  2971.         if ( ! is_null($inventaire->getDateMouvement())) {
  2972.             $date $inventaire->getDateMouvement();
  2973.         }
  2974.         //$where = "(eplct.id is null OR eplct.visibilite IS NULL OR eplct.visibilite = 0) AND (p.virtuel = 0 or p.virtuel is null) AND (p.prestation = 0 or p.prestation is null) AND (p.mainOeuvre = 0 or p.mainOeuvre is null) AND (p.nonCommandable = 0 or p.nonCommandable is null)";
  2975.         /*
  2976.         $where = "(p.virtuel = 0 or p.virtuel is null) AND (p.prestation = 0 or p.prestation is null) AND (p.mainOeuvre = 0 or p.mainOeuvre is null) AND (p.nonCommandable = 0 or p.nonCommandable is null)";
  2977.         if($date != ''){
  2978.             $where .= " and ((p.stock is not null AND p.stock != 0) or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' AND mvt.quantite != 0))";
  2979.         } else {
  2980.             //$where .= " AND p.stock is not null AND p.stock != 0";
  2981.             $where .= " and ((p.stock is not null AND p.stock != 0) OR mvt.quantite != 0)";
  2982.         }
  2983.         */
  2984.         $where "(p.virtuel = 0 or p.virtuel is null) AND (p.prestation = 0 or p.prestation is null) AND (p.mainOeuvre = 0 or p.mainOeuvre is null) AND (p.nonCommandable = 0 or p.nonCommandable is null)";
  2985.         if ($inventaire->getInclureArticleDifferentZero()) {
  2986.             $where .= " and ((p.stock is not null and p.stock > 0)";
  2987.             if ($inventaire->getInclureArticleDateMouvement()) {
  2988.                 if ($date != "") {
  2989.                     $where .= " or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and (mvt.quantite != 0 and p.stock <= 0)) ";
  2990.                 } else {
  2991.                     $where .= " OR (mvt.quantite != 0 and p.stock <= 0)";
  2992.                 }
  2993.             }
  2994.             $where .= ")";
  2995.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  2996.             //$where = "((p.stock is not null and p.stock != 0) or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0))";
  2997.             $where .= "and ((mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and (mvt.quantite != 0 and p.stock <= 0)) ";
  2998.             if ($inventaire->getInclureArticleDifferentZero()) {
  2999.                 $where .= " OR (p.stock is not null and p.stock > 0) ";
  3000.             }
  3001.             $where .= ")";
  3002.         } else {
  3003.             $where .= "and 1=1 ";
  3004.         }
  3005.         $where .= ' and (p.nonStocke = 0 OR p.nonStocke IS NULL)';
  3006.         $where .= ' and r.id NOT IN (15)';
  3007.         $query $this->createQueryBuilder('p')
  3008.                       ->select(
  3009.                           'p.id, COUNT(mvt.id) as nbMvts, SUM(CASE WHEN eplct.visibilite = 1 AND eplct.dateSuppression IS NULL THEN 1 ELSE 0 END) AS nbEmplacements'
  3010.                       )//, COUNT(eplct.id) as nbeplcts
  3011.                       ->leftJoin('p.mouvementStock''mvt')
  3012.                       ->join('mvt.raisonMouvementStock''r')
  3013.                       ->leftJoin('p.emplacements''eplct')
  3014.                       ->where($where)
  3015.                       ->orderBy('p.reference''ASC')
  3016.                       ->groupBy('p.id');
  3017.         //->having('nbMvts > 0 AND nbEmplacements = 0')
  3018.         $having "nbEmplacements = 0";
  3019.         if ($inventaire->getInclureArticleDifferentZero()) {
  3020.             if ($inventaire->getInclureArticleDateMouvement()) {
  3021.                 $having .= " and nbMvts >0";
  3022.             }
  3023.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3024.             $having .= " and nbMvts >0";
  3025.             if ($inventaire->getInclureArticleDifferentZero()) {
  3026.             }
  3027.         } else {
  3028.         }
  3029.         if ($having != "") {
  3030.             $query->having($having);
  3031.         }
  3032.         //echo $query->getQuery()->getSql();
  3033.         //exit;
  3034.         //->setParameters(array('marque' => $marque,'archive'=>'0'))
  3035.         $res $query->getQuery()->getResult();
  3036.         return $res;
  3037.     }
  3038.     public function getArticlesAinventorierParEmplacement($emplacement$date '')
  3039.     {
  3040.         $where "p.stock is not null and p.stock != 0";
  3041.         if ($date != '') {
  3042.             $where "((p.stock is not null and p.stock != 0) or (mvt.date > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0))";
  3043.         }
  3044.         if (is_object($emplacement->getNiveauUn())) {
  3045.             $where .= ' and n1.id = '.$emplacement->getNiveauUn()->getId();
  3046.         }
  3047.         if (is_object($emplacement->getNiveauDeux())) {
  3048.             $where .= ' and n2.id = '.$emplacement->getNiveauDeux()->getId();
  3049.         }
  3050.         if (is_object($emplacement->getNiveauTrois())) {
  3051.             $where .= ' and n3.id = '.$emplacement->getNiveauTrois()->getId();
  3052.         }
  3053.         if (is_object($emplacement->getNiveauQuatre())) {
  3054.             $where .= ' and n4.id = '.$emplacement->getNiveauQuatre()->getId();
  3055.         }
  3056.         /*
  3057.                         if(is_object($emplacement->getNiveauDeux())) $recherche['niveauDeux'] = $emplacement->getNiveauDeux();
  3058.                         if(is_object($emplacement->getNiveauTrois())) $recherche['niveauTrois'] = $emplacement->getNiveauTrois();
  3059.                         if(is_object($emplacement->getNiveauQuatre())) $recherche['niveauQuatre'] = $emplacement->getNiveauQuatre();
  3060.          *
  3061.          */
  3062.         $query $this->createQueryBuilder('p')
  3063.                       ->select('p.id, COUNT(mvt.id) as total, COUNT(eplct.id) as nbArticleOuEmplacement')
  3064.                       ->leftJoin('p.mouvementStock''mvt')
  3065.                       ->leftJoin('p.emplacements''eplct')
  3066.                       ->leftJoin('eplct.niveauUn''n1')
  3067.                       ->leftJoin('eplct.niveauDeux''n2')
  3068.                       ->leftJoin('eplct.niveauTrois''n3')
  3069.                       ->leftJoin('eplct.niveauQuatre''n4')
  3070.                       ->where($where)
  3071.                       ->groupBy('p.id')
  3072.                       ->having('total > 0')//->setParameters(array('marque' => $marque,'archive'=>'0'))
  3073.         ;
  3074.         $res   $query->getQuery()->getResult();
  3075.         return $res;
  3076.     }
  3077.     public function getEmplacementsAinventorierParEmplacement($emplacement$inventaire)
  3078.     {
  3079.         $date "";
  3080.         if ( ! is_null($inventaire->getDateMouvement())) {
  3081.             $date $inventaire->getDateMouvement();
  3082.         }
  3083.         /*
  3084.           $where = "((p.stock is not null and p.stock != 0) OR mvt.quantite != 0)";
  3085.           if($date != ''){
  3086.               $where = "((p.stock is not null and p.stock != 0) or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0))";
  3087.           }
  3088.           */
  3089.         if ($inventaire->getInclureArticleDifferentZero()) {
  3090.             $where "((p.stock is not null and p.stock > 0)";
  3091.             if ($inventaire->getInclureArticleDateMouvement()) {
  3092.                 if ($date != "") {
  3093.                     $where .= " or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0 and p.stock <=0)";
  3094.                 } else {
  3095.                     $where .= " OR (mvt.quantite != 0 and p.stock <= 0) ";
  3096.                 }
  3097.             }
  3098.             $where .= ")";
  3099.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3100.             //$where = "((p.stock is not null and p.stock != 0) or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0))";
  3101.             $where "(( mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0 and p.stock <= 0)";
  3102.             if ($inventaire->getInclureArticleDifferentZero()) {
  3103.                 $where .= " OR (p.stock is not null and p.stock > 0) ";
  3104.             }
  3105.             $where .= ")";
  3106.         } else {
  3107.             $where "1=1";
  3108.         }
  3109.         $where .= ' and eplct.visibilite = 1';
  3110.         $where .= ' and (p.nonStocke = 0 OR p.nonStocke IS NULL)';
  3111.         $where .= ' and (r.id NOT IN (15) or r.id is null)';
  3112.         if (is_object($emplacement) and is_object($emplacement->getNiveauUn())) {
  3113.             $where .= ' and n1.id = '.$emplacement->getNiveauUn()->getId();
  3114.         }
  3115.         if (is_object($emplacement) and is_object($emplacement->getNiveauDeux())) {
  3116.             $where .= ' and n2.id = '.$emplacement->getNiveauDeux()->getId();
  3117.         }
  3118.         if (is_object($emplacement) and is_object($emplacement->getNiveauTrois())) {
  3119.             $where .= ' and n3.id = '.$emplacement->getNiveauTrois()->getId();
  3120.         }
  3121.         if (is_object($emplacement) and is_object($emplacement->getNiveauQuatre())) {
  3122.             $where .= ' and n4.id = '.$emplacement->getNiveauQuatre()->getId();
  3123.         }
  3124.         /*
  3125.                         if(is_object($emplacement->getNiveauDeux())) $recherche['niveauDeux'] = $emplacement->getNiveauDeux();
  3126.                         if(is_object($emplacement->getNiveauTrois())) $recherche['niveauTrois'] = $emplacement->getNiveauTrois();
  3127.                         if(is_object($emplacement->getNiveauQuatre())) $recherche['niveauQuatre'] = $emplacement->getNiveauQuatre();
  3128.          *
  3129.          */
  3130.         $query $this->createQueryBuilder('p')
  3131.                       ->select('eplct.id,p.stock, COUNT(mvt.id) as total, COUNT(eplct.id) as nbArticleOuEmplacement')
  3132.                       ->leftJoin('p.mouvementStock''mvt')
  3133.                       ->leftJoin('mvt.raisonMouvementStock''r')
  3134.                       ->leftJoin('p.emplacements''eplct')
  3135.                       ->leftJoin('eplct.niveauUn''n1')
  3136.                       ->leftJoin('eplct.niveauDeux''n2')
  3137.                       ->leftJoin('eplct.niveauTrois''n3')
  3138.                       ->leftJoin('eplct.niveauQuatre''n4')
  3139.                       ->where($where);
  3140.         if ($emplacement !== null && is_object($emplacement) or == 1) {
  3141.             //echo "YYYY";
  3142.             //exit;
  3143.             $query->addOrderBy('eplct.libelle''ASC');
  3144.         }
  3145.         $query->addOrderBy('p.reference''ASC')
  3146.             //->orderBy('p.reference', 'ASC')
  3147.               ->groupBy('eplct.id');
  3148.         $having "";
  3149.         //->having('(total > 0 or (p.stock is not null and p.stock != 0))')
  3150.         if ($inventaire->getInclureArticleDifferentZero()) {
  3151.             $having "((p.stock is null or p.stock > 0)";
  3152.             if ($inventaire->getInclureArticleDateMouvement()) {
  3153.                 $having .= " or (total > 0 and p.stock <=0)";
  3154.             }
  3155.             $having .= ")";
  3156.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3157.             $having "((total > 0 and p.stock <=0)";
  3158.             if ($inventaire->getInclureArticleDifferentZero()) {
  3159.                 $having .= " or (p.stock is not null or p.stock > 0)";
  3160.             }
  3161.             $having .= ")";
  3162.         } else {
  3163.         }
  3164.         if ($having != "") {
  3165.             $query->having($having);
  3166.         }//->setParameters(array('marque' => $marque,'archive'=>'0'))
  3167.         ;
  3168.         //echo "IIII";
  3169.         //exit;
  3170.         //echo $query->getQuery()->getSql();
  3171.         //exit;
  3172.         $res $query->getQuery()->getResult();
  3173.         return $res;
  3174.     }
  3175.     public function getArticlesAinventorierParMarque($marque$date '')
  3176.     {
  3177.         $where "p.marque = :marque and p.stock is not null and p.stock != 0";
  3178.         if ($date != '') {
  3179.             $where "p.marque = :marque and ((p.stock is not null and p.stock != 0) or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0))";
  3180.         }
  3181.         $query $this->createQueryBuilder('p')
  3182.                       ->select(
  3183.                           'p.id,p.stock, COUNT(mvt.id) as total, CASE
  3184.                     WHEN COUNT(eplct.id) > COUNT(p.id) THEN COUNT(eplct.id)
  3185.                     ELSE COUNT(p.id)
  3186.                     as nbArticleOuEmplacement'
  3187.                       )
  3188.                       ->leftJoin('p.mouvementStock''mvt')
  3189.                       ->leftJoin('p.emplacements''eplct')
  3190.                       ->where($where)
  3191.                       ->groupBy('p.id')
  3192.                       ->having('(total > 0 or (p.stock is not null and p.stock != 0))')
  3193.                       ->setParameters(['marque' => $marque]);
  3194.         $res   $query->getQuery()->getResult();
  3195.         return $res;
  3196.     }
  3197.     public function getArticlesAinventorierParMarqueEtEmplacement($marque$emplacement null$inventaire,$date '')
  3198.     {
  3199.         $selectId   'p';
  3200.         $parameters = ['marque' => $marque];
  3201.         /*
  3202.         $where = "p.marque = :marque and ((p.stock is not null and p.stock != 0) OR mvt.quantite != 0)";
  3203.         if($date != ''){
  3204.             $where = "p.marque = :marque and ((p.stock is not null and p.stock != 0) or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0))";
  3205.         }
  3206.         */
  3207.         $where "p.marque = :marque and ((p.stock is not null and p.stock != 0) OR mvt.quantite != 0)";
  3208.         if ($inventaire->getInclureArticleDifferentZero()) {
  3209.             $where .= " and ((p.stock is not null and p.stock > 0)";
  3210.             if ($inventaire->getInclureArticleDateMouvement()) {
  3211.                 if ($date != "") {
  3212.                     $where .= " OR (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0 and p.stock <= 0)";
  3213.                 } else {
  3214.                     $where .= " OR (mvt.quantite > 0 and p.stock <= 0) ";
  3215.                 }
  3216.             }
  3217.             $where .= ")";
  3218.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3219.             //$where = "((p.stock is not null and p.stock != 0) or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0))";
  3220.             $where .= "((mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0 and p.stock <= 0)";
  3221.             if ($inventaire->getInclureArticleDifferentZero()) {
  3222.                 $where .= " OR (p.stock is not null and p.stock > 0) ";
  3223.             }
  3224.             $where .= ")";
  3225.         }
  3226.         $where .= ' and (p.nonStocke = 0 OR p.nonStocke IS NULL)';
  3227.         $where .= ' and (r.id NOT IN (15) or r.id is null)';
  3228.         if ($emplacement !== null && is_object($emplacement)) {
  3229.             if (is_object($emplacement->getNiveauUn())) {
  3230.                 $where .= ' and n1.id = '.$emplacement->getNiveauUn()->getId();
  3231.             }
  3232.             if (is_object($emplacement->getNiveauDeux())) {
  3233.                 $where .= ' and n2.id = '.$emplacement->getNiveauDeux()->getId();
  3234.             }
  3235.             if (is_object($emplacement->getNiveauTrois())) {
  3236.                 $where .= ' and n3.id = '.$emplacement->getNiveauTrois()->getId();
  3237.             }
  3238.             if (is_object($emplacement->getNiveauQuatre())) {
  3239.                 $where .= ' and n4.id = '.$emplacement->getNiveauQuatre()->getId();
  3240.             }
  3241.             /*
  3242.                 $where .= " and eplct.id = :emplacement";
  3243.                 $parameters['emplacement'] = $emplacement->getId();
  3244.              *
  3245.              */
  3246.             $where    .= ' and eplct.visibilite = 1';
  3247.             $selectId 'eplct';
  3248.         }
  3249.         $query $this->createQueryBuilder('p')
  3250.                       ->select(
  3251.                           $selectId.'.id,p.stock, COUNT(mvt.id) as total, CASE
  3252.                                                         WHEN COUNT(eplct.id) > COUNT(p.id) THEN COUNT(eplct.id)
  3253.                                                         ELSE COUNT(p.id)
  3254.                                                         as nbArticleOuEmplacement'
  3255.                       )
  3256.                       ->leftJoin('p.mouvementStock''mvt')
  3257.                       ->leftJoin('mvt.raisonMouvementStock''r')
  3258.                       ->leftJoin('p.emplacements''eplct')
  3259.                       ->leftJoin('eplct.niveauUn''n1')
  3260.                       ->leftJoin('eplct.niveauDeux''n2')
  3261.                       ->leftJoin('eplct.niveauTrois''n3')
  3262.                       ->leftJoin('eplct.niveauQuatre''n4')
  3263.                       ->where($where);
  3264.         $query->addOrderBy('p.reference''ASC');
  3265.         if ($emplacement !== null && is_object($emplacement)) {
  3266.             $query->addOrderBy('eplct.libelle''ASC');
  3267.         }
  3268.         $query->groupBy($selectId.'.id')
  3269.               ->setParameters($parameters);
  3270.         //->having('(total > 0 or (p.stock is not null and p.stock != 0))')
  3271.         $having "";
  3272.         if ($inventaire->getInclureArticleDifferentZero()) {
  3273.             $having "((p.stock is null or p.stock > 0)";
  3274.             if ($inventaire->getInclureArticleDateMouvement()) {
  3275.                 $having .= " or (total > 0 and p.stock <=0)";
  3276.             }
  3277.             $having .= ")";
  3278.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3279.             $having "((total > 0 and p.stock <=0)";
  3280.             if ($inventaire->getInclureArticleDifferentZero()) {
  3281.                 $having .= " or (p.stock is not null or p.stock > 0)";
  3282.             }
  3283.             $having .= ")";
  3284.         } else {
  3285.         }
  3286.         if ($having != "") {
  3287.             $query->having($having);
  3288.         }
  3289.         $res $query->getQuery()->getResult();
  3290.         return $res;
  3291.     }
  3292.     public function getArticlesMouvementes($date '')
  3293.     {
  3294.         $where "(p.stock is not null and p.stock != 0) OR mvt.quantite != 0";
  3295.         if ($date != '') {
  3296.             $where "(p.stock is not null and p.stock != 0) or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0)";
  3297.             //exclure certains mvt
  3298.         }
  3299.         $query $this->createQueryBuilder('p')
  3300.                       ->select('p.id, COUNT(mvt.id) as nbMvts')//, COUNT(eplct.id) as nbeplcts
  3301.                       ->leftJoin('p.mouvementStock''mvt')
  3302.                       ->where($where)
  3303.                       ->groupBy('p.id')
  3304.                       ->having('nbMvts > 0')//->setParameters(array('marque' => $marque,'archive'=>'0'))
  3305.         ;
  3306.         $res   $query->getQuery()->getResult();
  3307.         return $res;
  3308.     }
  3309.     public function getArticlesMouvementesPump($date '')
  3310.     {
  3311.         $where "mvt.quantite > 0 and r.action = 'augmenter'";
  3312.         if ($date != '') {
  3313.             $where "mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' AND mvt.quantite > 0 AND r.action = 'augmenter'";
  3314.             //exclure certains mvt
  3315.         }
  3316.         $query $this->createQueryBuilder('p')
  3317.                       ->select('p.id, COUNT(mvt.id) as nbMvts')//, COUNT(eplct.id) as nbeplcts
  3318.                       ->leftJoin('p.mouvementStock''mvt')
  3319.                       ->leftJoin('mvt.raisonMouvementStock''r')
  3320.                       ->where($where)
  3321.                       ->groupBy('p.id')
  3322.                       ->having('nbMvts > 0')//->setParameters(array('marque' => $marque,'archive'=>'0'))
  3323.         ;
  3324.         $res   $query->getQuery()->getResult();
  3325.         return $res;
  3326.     }
  3327.     public function getArticlesPumpNull()
  3328.     {
  3329.         $query $this->createQueryBuilder('p')
  3330.                       ->select('p.id')//, COUNT(eplct.id) as nbeplcts
  3331.                       ->join('p.conditionsAchat''ca')
  3332.                       ->where('p.pump IS NULL OR p.pump = 0')
  3333.                       ->groupBy('p.id');
  3334.         $res   $query->getQuery()->getResult();
  3335.         return $res;
  3336.     }
  3337.     public function articleCommandeSupprimableCommande($ac)
  3338.     {
  3339.         $commande $ac->getCommande();
  3340.         $article  $ac->getArticle();
  3341.         $res $this->getEntityManager()
  3342.                     ->createQuery(
  3343.                         "select count(ac.id) as total
  3344.                 FROM App\Entity\GestionComerciale\ArticleCommande ac
  3345.                 LEFT JOIN App\Entity\GestionComerciale\Commande doc with ac.commande = doc.id
  3346.                 LEFT JOIN App\Entity\GestionComerciale\StatutCommande s with doc.statutCommande = s.id
  3347.                 WHERE doc.commande = :commande and ac.article = :article and doc.typeDocumentCommercial IN (:typeDocumentCommercial) and s.ordre NOT IN (:statut)
  3348.                 "
  3349.                     )
  3350.                     ->setParameters(['commande' => $commande->getId(), 'article' => $article->getId(), 'typeDocumentCommercial' => [TypeDocumentCommercial::BPTypeDocumentCommercial::BLTypeDocumentCommercial::FACTURE], 'statut' => [0]])
  3351.                     ->getOneOrNullResult();
  3352.         if ($res === null || $res['total'] == 0) {
  3353.             return true;
  3354.         }
  3355.         return false;
  3356.     }
  3357.     public function findByStockReserveNotNull()
  3358.     {
  3359.         $where "p.stockReserve is not null and p.stockReserve != 0 and (p.divers = 0 or p.divers is null)";// and (p.divers_composes = 0 or p.divers_composes is null)";
  3360.         $query $this->createQueryBuilder('p')
  3361.                       ->where($where)//->setMaxResults(10);
  3362.         ;
  3363.         //$res = $query->getQuery()->getResult();
  3364.         return $query;
  3365.     }
  3366.     public function findByStockReserveInferieur0()
  3367.     {
  3368.         $where "p.stockReserve < 0 and (p.divers = 0 or p.divers is null) and (p.diversComposes = 0 or p.diversComposes is null)";
  3369.         $query $this->createQueryBuilder('p')
  3370.                       ->where($where)//->setMaxResults(10);
  3371.         ;
  3372.         //$res = $query->getQuery()->getResult();
  3373.         return $query;
  3374.     }
  3375.     public function getArticleAvecCoeffCondit()
  3376.     {
  3377.         $where "p.majPrix = 1 and c.defaut = 1 and c.coefficientConditionnement > 1";
  3378.         $query $this->createQueryBuilder('p')
  3379.             //->select('p.id')
  3380.                       ->leftJoin('p.conditionsAchat''c')
  3381.             //->groupBy('p.id')
  3382.                       ->where($where);
  3383.         return $query;
  3384.     }
  3385.     public function findByCommande($commandeId)
  3386.     {
  3387.         $query $this->createQueryBuilder('p')
  3388.             //->select('p.id')
  3389.                       ->leftJoin('p.articleCommande''ac')
  3390.                       ->leftJoin('ac.commande''c')
  3391.                       ->groupBy('p.id')
  3392.                       ->where('c.id = :commande')
  3393.                       ->setParameters(['commande' => $commandeId]);
  3394.         return $query;
  3395.     }
  3396.     public function getArticleByRef($ref$categorie)
  3397.     {
  3398.         $query $this->createQueryBuilder('p')
  3399.             //->select('p.id')
  3400.                       ->leftJoin('p.articleCategorie''ac')
  3401.                       ->leftJoin('ac.categorie''c')
  3402.                       ->where("p.ref LIKE ':ref'")
  3403.                       ->setParameters(['ref' => '%|'.$ref]);
  3404.         return $query;
  3405.     }
  3406.     public function findRandomArticles(int $limit 3,$categorie_id,$client_id)
  3407.     {
  3408.         $conn $this->getEntityManager()->getConnection();
  3409.         $sql 'SELECT * 
  3410.                 FROM article__article 
  3411.                 WHERE 
  3412.                 dispo > :dispo and 
  3413.                 statut = :statut 
  3414.                 ORDER BY RAND() 
  3415.                 LIMIT :limit';
  3416.         $sql 'SELECT a.*
  3417.                 FROM article__article a
  3418.                 INNER JOIN article__article_categorie ac
  3419.                 ON a.id = ac.article_id
  3420.                 WHERE a.dispo > :dispo
  3421.                 AND a.statut = :statut
  3422.                 AND ac.categorie_id = :categorie_id
  3423.                 ORDER BY RAND()
  3424.                 LIMIT :limit
  3425.             ';
  3426.         $sql 'SELECT a.*
  3427.                 FROM article__article a
  3428.                 INNER JOIN article__article_categorie ac  ON a.id = ac.article_id
  3429.                 LEFT JOIN article__marque m  ON a.marque_id = m.id            
  3430.                 WHERE (a.dispo > :dispo or 1=1)
  3431.                 AND a.statut = :statut
  3432.                 AND ac.categorie_id = :categorie_id
  3433.                 AND (m.id is null or m.statut = 1)
  3434.                 ORDER BY RAND()
  3435.                 LIMIT :limit
  3436.             ';
  3437.         $sql 'SELECT a.*
  3438.                 FROM article__article a
  3439.                 INNER JOIN article__article_categorie ac  ON a.id = ac.article_id
  3440.                 LEFT JOIN article__marque m  ON a.marque_id = m.id  
  3441.                 LEFT JOIN client__client_marque ccm ON ccm.marque_id = a.marque_id AND ccm.client_id = :client_id
  3442.                 WHERE (a.dispo > :dispo or 1=1)
  3443.                 AND a.statut = :statut
  3444.                 AND ac.categorie_id = :categorie_id
  3445.                 AND (m.id is null or m.statut = 1)
  3446.                 AND ccm.marque_id IS NULL
  3447.                 ORDER BY RAND()
  3448.                 LIMIT :limit
  3449.             ';
  3450.         $em   $this->getEntityManager();
  3451.         $stmt $em->getConnection()->prepare($sql);
  3452.         $stmt->bindValue('client_id'$client_id);
  3453.         $stmt->bindValue('dispo'1);
  3454.         $stmt->bindValue('statut'1);
  3455.         $stmt->bindValue('categorie_id'$categorie_id);
  3456.         //$stmt->bindValue('public', true);
  3457.         $stmt->bindValue('limit'$limit\PDO::PARAM_INT);
  3458.         $hasard $stmt->executeQuery()->fetchAllAssociative();
  3459.         return $hasard;
  3460.     }
  3461.     public function getRechercheArticleSelectObi(
  3462.         $recherche,
  3463.         $produitInterne null,
  3464.         $fournisseur null,
  3465.         $articlesAExclure null,
  3466.         $stock "",
  3467.         $reference_search "",
  3468.         $libelle_search "",
  3469.         $prix_search "",
  3470.         $tri_reference "",
  3471.         $tri_libelle "",
  3472.         $tri_prix "",
  3473.         $tri_stock "",
  3474.         $categories "",
  3475.         $mouvemente 1,
  3476.         $inventaire "",
  3477.         $declinaisons false
  3478.     ) {
  3479.         $select "";
  3480.         $recherche str_replace("*""%"$recherche);
  3481.         $recherche str_replace("."""$recherche);
  3482.         $recherche str_replace("/""\/"$recherche);
  3483.         $recherche preg_replace('/\s+/'' '$recherche);
  3484.         $recherche trim($recherche);
  3485.         $whereStock "";
  3486.         if ($stock == 1) {
  3487.             $whereStock .= " AND p.stock > 0";
  3488.         }
  3489.         // Gestion du tri
  3490.         $order "";
  3491.         if ($tri_reference != "") { $order "p.reference " $tri_reference ", "; }
  3492.         if ($tri_prix != "") { $order "p.prix_vente " $tri_prix ", "; }
  3493.         if ($tri_stock != "") { $order "p.stock " $tri_stock ", "; }
  3494.         if ($tri_libelle != "") { $order "p.libelle " $tri_libelle ", "; }
  3495.         if ($order == "") { $order "p.reference ASC "; }
  3496.         $order str_replace(","""$order);
  3497.         // Construction SQL
  3498.         $sql "
  3499.         SELECT
  3500.             p.afficher_composant_pdf_externe, 
  3501.             p.virtuel, 
  3502.             p.dateProchaineDispo,                 
  3503.             p.libelle_secondaire,                                   
  3504.             CONCAT(p.reference, ' ', p.libelle) as libelle_construit,                             
  3505.             p.libelle,
  3506.             p.id,
  3507.             p.prix_vente as prixVente,
  3508.             p.prix_base as prixAchat,
  3509.             p.poids,
  3510.             p.stock,           
  3511.             p.dispo,           
  3512.             p.stock_reserve,
  3513.             p.reference,                  
  3514.             p.arret_gamme,
  3515.             marque.libelle as libelle_marque,
  3516.             marque.id as id_marque,
  3517.             marque.logo as logo_marque
  3518.             $select
  3519.         FROM article__article as p 
  3520.         LEFT JOIN article__marque as marque ON marque.id = p.marque_id
  3521.         WHERE
  3522.             (p.archive IS NULL OR p.archive = 0)
  3523.             AND (p.arret_gamme IS NULL OR p.arret_gamme = 0)
  3524.             AND (p.parent_id IS NULL)
  3525.             AND (p.statut IS NULL OR p.statut = 1)
  3526.     ";
  3527.         $params = [];
  3528.         if (!empty($recherche)) {
  3529.             // -----------------------------
  3530.             // 1️⃣ Recherche multi-mots sur libelle
  3531.             // -----------------------------
  3532.             $words preg_split('/\s+/'$recherche);
  3533.             $libelleConditions = [];
  3534.             $i 0;
  3535.             foreach ($words as $word) {
  3536.                 if ($word == '') continue;
  3537.                 $key "libelle_$i";
  3538.                 $libelleConditions[] = "LOWER(p.libelle) LIKE :$key";
  3539.                 $params[$key] = "%" strtolower($word) . "%";
  3540.                 $i++;
  3541.             }
  3542.             if (!empty($libelleConditions)) {
  3543.                 $sql .= " AND ((" implode(" AND "$libelleConditions) . ")";
  3544.             }
  3545.             // -----------------------------
  3546.             // 2️⃣ Recherche complète sur references
  3547.             // -----------------------------
  3548.             $sql .= " OR (
  3549.             p.reference LIKE :ref_full
  3550.             OR p.reference_appel_1 LIKE :ref_full
  3551.             OR p.reference_appel_2 LIKE :ref_full
  3552.             OR p.reference_appel_3 LIKE :ref_full
  3553.             OR p.reference_appel_4 LIKE :ref_full
  3554.             OR p.reference_appel_5 LIKE :ref_full
  3555.             OR p.reference_appel_6 LIKE :ref_full
  3556.             OR p.reference_appel_7 LIKE :ref_full
  3557.             OR p.reference_appel_8 LIKE :ref_full
  3558.             OR p.reference_appel_9 LIKE :ref_full
  3559.             OR p.reference_appel_10 LIKE :ref_full
  3560.             OR p.reference_appel_11 LIKE :ref_full
  3561.         ))";
  3562.             $params['ref_full'] = "%$recherche%";
  3563.         }
  3564.         $sql .= $whereStock;
  3565.         $sql .= "
  3566.         ORDER BY $order, p.libelle ASC
  3567.         LIMIT 0,100
  3568.     ";
  3569.         $em $this->getEntityManager();
  3570.         $stmt $em->getConnection()->prepare($sql);
  3571.         //echo $sql;
  3572. //        exit;
  3573.         $resultats $stmt->executeQuery($params)->fetchAllAssociative();
  3574.         return $resultats;
  3575.     }
  3576.     public function old___getRechercheArticleSelectObi($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock ""$reference_search ""$libelle_search "",
  3577.                                                  $prix_search ""$tri_reference ""$tri_libelle ""$tri_prix ""$tri_stock ""$categories ""$mouvemente 1$inventaire "",
  3578.                                                  $declinaisons false
  3579.     ) {
  3580.         $match                   'p.reference,p.libelle,p.reference_appel_1,p.reference_appel_2,p.reference_appel_3,p.reference_appel_4,p.reference_appel_5,p.reference_appel_6,p.reference_appel_7,p.reference_appel_8,p.reference_appel_9,p.reference_appel_10,p.reference_appel_11';
  3581.         $match2                  'ca.reference_fournisseur, ca.reference_fournisseur_appel';
  3582.         $select                  "";
  3583.         $jointureFromInventaire  '';
  3584.         $jointureWhereInventaire "";
  3585.         $recherche str_replace("*""%"$recherche);
  3586.         $recherche str_replace("."""$recherche);
  3587.         $recherche str_replace("/""\/"$recherche);
  3588.         $recherche str_replace("   "" "$recherche);
  3589.         $recherche str_replace("  "" "$recherche);
  3590.         $recherche_ref_fourn $recherche;
  3591.         //$recherche = str_replace("-"," ",$recherche);
  3592.         $recherche_tmp $recherche;
  3593.         $avecEtoile    false;
  3594.         $pos strpos($recherche"*");
  3595.         if ($pos !== false) {
  3596.             $avecEtoile true;
  3597.         }
  3598.         $wherePrix        "";
  3599.         $tampon_recherche $recherche;
  3600.         $recherche        trim($recherche);
  3601.         if (!$avecEtoile) {
  3602.            // $recherche = str_replace(" ", "* ", $recherche);
  3603.         }
  3604.         if (!$avecEtoile) {
  3605.             //   $recherche .= "*";
  3606.         }
  3607.         $whereSuppl " and p.parent_id is null and (p.statut is null or p.statut = 1)";
  3608.         $whereStock "";
  3609.         if ($stock == 1) {
  3610.             $whereStock .= " and p.stock > 0";
  3611.         }
  3612.         $order "";
  3613.         if ($tri_reference != "") {
  3614.             $order "p.reference ".$tri_reference.", ";
  3615.         }
  3616.         if ($tri_prix != "") {
  3617.             $order "p.prix_vente ".$tri_prix.", ";
  3618.         }
  3619.         if ($tri_stock != "") {
  3620.             $order "p.stock ".$tri_stock.", ";
  3621.         }
  3622.         if ($tri_libelle != "") {
  3623.             $order "p.libelle ".$tri_libelle.", ";
  3624.         }
  3625.         if ($order == "") {
  3626.             $order "p.reference ASC ";
  3627.         }
  3628.         $order str_replace(","""$order);
  3629.         $jointureFrom  "";
  3630.         $jointureWhere "";
  3631.         if ($recherche != "" and $recherche != "*" or 1==1) {
  3632.             if(1==1) {
  3633.                 if(1==1) {
  3634.                     $recherche_contruit "";
  3635.                     $recherche_tab      explode(" "$recherche);
  3636.                     for ($r 0$r count($recherche_tab); $r++)
  3637.                     {
  3638.                         $recherche_tab[$r]  = str_replace("+""#+"$recherche_tab[$r]);
  3639.                         $recherche_contruit .= "+".$recherche_tab[$r]." ";
  3640.                     }
  3641.                     $recherche trim($recherche"*");
  3642.                     $sql "
  3643.                     select
  3644.                     p.afficher_composant_pdf_externe, 
  3645.                     p.virtuel, 
  3646.                     p.dateProchaineDispo,                 
  3647.                     p.libelle_secondaire,                                   
  3648.                     CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,                             
  3649.                     p.libelle,
  3650.                     p.id,
  3651.                     p.prix_vente as prixVente,
  3652.                     p.prix_base as prixAchat,
  3653.                     p.poids,
  3654.                     p.stock,           
  3655.                     p.dispo,           
  3656.                     p.stock_reserve,
  3657.                     p.reference,                  
  3658.                     p.arret_gamme,
  3659.                     marque.libelle as libelle_marque,
  3660.                     marque.id as id_marque,
  3661.                     marque.logo as logo_marque
  3662.                     "
  3663.                     .$select
  3664.                     ." FROM article__article as p 
  3665.                     LEFT JOIN article__marque as marque ON marque.id = p.marque_id
  3666.                     WHERE ";
  3667.                     $sql .= "  ((p.reference LIKE '%".$recherche."%') 
  3668.                                 or 
  3669.                                 (p.reference_appel_1 LIKE '%".$recherche."%') 
  3670.                                 or 
  3671.                                 (p.reference_appel_2 LIKE '%".$recherche."%') 
  3672.                                 or 
  3673.                                 (p.reference_appel_3 LIKE '%".$recherche."%') 
  3674.                                 or 
  3675.                                 (p.reference_appel_4 LIKE '%".$recherche."%') 
  3676.                                 or 
  3677.                                 (p.reference_appel_5 LIKE '%".$recherche."%') 
  3678.                                 or 
  3679.                                 (p.reference_appel_6 LIKE '%".$recherche."%') 
  3680.                                 or 
  3681.                                 (p.reference_appel_7 LIKE '%".$recherche."%') 
  3682.                                 or 
  3683.                                 (p.reference_appel_8 LIKE '%".$recherche."%') 
  3684.                                 or 
  3685.                                 (p.reference_appel_9 LIKE '%".$recherche."%') 
  3686.                                 or 
  3687.                                 (p.reference_appel_10 LIKE '%".$recherche."%') 
  3688.                                 or 
  3689.                                 (p.libelle LIKE '%".$recherche."%')) 
  3690.                                 AND
  3691.                                 (p.archive is null or p.archive = 0)
  3692.                                 AND
  3693.                                 (p.arret_gamme is null or p.arret_gamme = 0)";
  3694.                     //$sql .= ")";
  3695.                     $sql .= $jointureWhereInventaire;
  3696.                    // $sql .= " GROUP BY p.id
  3697.                     $sql .= " order by ".$order.", libelle asc
  3698.                     LIMIT 0,100
  3699.                     ";
  3700.                 }
  3701.             }
  3702.         }
  3703.        // echo $sql;
  3704.         $em   $this->getEntityManager();
  3705.         $stmt $em->getConnection()->prepare($sql);
  3706.         $resultats $stmt->executeQuery()->fetchAllAssociative();
  3707.         return $resultats;
  3708.     }
  3709.     public function ___getRechercheArticleSelectObi($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock ""$reference_search ""$libelle_search "",
  3710.                                               $prix_search ""$tri_reference ""$tri_libelle ""$tri_prix ""$tri_stock ""$categories ""$mouvemente 1$inventaire "",
  3711.                                               $declinaisons false
  3712.     ) {
  3713.         $match                   'p.reference,p.libelle,p.reference_appel_1,p.reference_appel_2,p.reference_appel_3,p.reference_appel_4,p.reference_appel_5,p.reference_appel_6,p.reference_appel_7,p.reference_appel_8,p.reference_appel_9,p.reference_appel_10,p.reference_appel_11';
  3714.         $match2                  'ca.reference_fournisseur, ca.reference_fournisseur_appel';
  3715.         $select                  "";
  3716.         $jointureFromInventaire  '';
  3717.         $jointureWhereInventaire "";
  3718.         $recherche str_replace("*""%"$recherche);
  3719.         $recherche str_replace("."""$recherche);
  3720.         $recherche str_replace("/""\/"$recherche);
  3721.         $recherche str_replace("   "" "$recherche);
  3722.         $recherche str_replace("  "" "$recherche);
  3723.         $recherche_ref_fourn $recherche;
  3724.         //$recherche = str_replace("-"," ",$recherche);
  3725.         $recherche_tmp $recherche;
  3726.         $avecEtoile    false;
  3727.         $pos strpos($recherche"*");
  3728.         if ($pos !== false) {
  3729.             $avecEtoile true;
  3730.         }
  3731.         $wherePrix        "";
  3732.         $tampon_recherche $recherche;
  3733.         $recherche        trim($recherche);
  3734.         if (!$avecEtoile) {
  3735.             $recherche str_replace(" ""* "$recherche);
  3736.         }
  3737.         if (!$avecEtoile) {
  3738.             $recherche .= "*";
  3739.         }
  3740.         $whereSuppl " and p.parent_id is null and (p.statut is null or p.statut = 1)";
  3741.         $whereStock "";
  3742.         if ($stock == 1) {
  3743.             $whereStock .= " and p.stock > 0";
  3744.         }
  3745.         $order "";
  3746.         if ($tri_reference != "") {
  3747.             $order "p.reference ".$tri_reference.", ";
  3748.         }
  3749.         if ($tri_prix != "") {
  3750.             $order "p.prix_vente ".$tri_prix.", ";
  3751.         }
  3752.         if ($tri_stock != "") {
  3753.             $order "p.stock ".$tri_stock.", ";
  3754.         }
  3755.         if ($tri_libelle != "") {
  3756.             $order "p.libelle ".$tri_libelle.", ";
  3757.         }
  3758.         if ($order == "") {
  3759.             $order "p.reference ASC ";
  3760.         }
  3761.         $order str_replace(","""$order);
  3762.         $jointureFrom  "";
  3763.         $jointureWhere "";
  3764.         if ($recherche != "" and $recherche != "*" or 1==1) {
  3765.            if(1==1) {
  3766.                if(1==1) {
  3767.                     $recherche_contruit "";
  3768.                     $recherche_tab      explode(" "$recherche);
  3769.                     for ($r 0$r count($recherche_tab); $r++)
  3770.                     {
  3771.                         $recherche_tab[$r]  = str_replace("+""#+"$recherche_tab[$r]);
  3772.                         $recherche_contruit .= "+".$recherche_tab[$r]." ";
  3773.                     }
  3774.                     $recherche trim($recherche"*");
  3775.                     $sql "
  3776.                     select
  3777.                     p.dateProchaineDispo,
  3778.                   
  3779.                      p.libelle_secondaire,
  3780.                     p.reference_appel_2,
  3781.                     p.reference_appel_3,
  3782.                     p.reference_appel_4,
  3783.                     p.reference_appel_5,
  3784.                     p.reference_appel_6,
  3785.                     p.reference_appel_7,
  3786.                     p.reference_appel_8,
  3787.                     p.reference_appel_9,
  3788.                     p.reference_appel_10,
  3789.                     p.reference_appel_11,
  3790.                     p.stock_reserve_fabrication,
  3791.                     p.virtuel,
  3792.                     CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,           
  3793.                     p.article_remplacement_id,
  3794.                     p.libelle,
  3795.                     p.id,
  3796.                     p.prix_vente as prixVente,
  3797.                     p.prix_base as prixAchat,
  3798.                     p.poids,
  3799.                     p.stock,           
  3800.                     p.dispo,           
  3801.                     p.stock_reserve,
  3802.                     p.reference,
  3803.                     p.pump,
  3804.                     p.cpump,
  3805.                     p.arret_gamme,
  3806.                     marque.libelle as libelle_marque,
  3807.                     marque.id as id_marque,
  3808.                     marque.logo as logo_marque,
  3809.                     MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation"
  3810.                     .$select
  3811.                     ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  3812.                     LEFT JOIN article__marque as marque ON marque.id = p.marque_id
  3813.                     WHERE ";
  3814.                     if ($jointureWhere != "") {
  3815.                         $sql .= $jointureWhere." and ";
  3816.                     }
  3817.                     $sql .= " p.temporaire is null
  3818.                             and ( marque.statut = 1 or marque.id is null) and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  3819.                             and                            
  3820.                             (MATCH (".$match.")
  3821.                             AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)";
  3822.                     $sql .= "";
  3823.                     $sql .= $wherePrix.$whereStock;
  3824.                     $sql .= " OR (p.reference LIKE '%".$recherche."%') ";
  3825.                     $sql .= ")";
  3826.                     $sql .= $jointureWhereInventaire;
  3827.                     $sql .= " GROUP BY p.id
  3828.                     order by ".$order.", ponderation desc          
  3829.                     LIMIT 0,100
  3830.                     ";
  3831.                 }
  3832.             }
  3833.         }
  3834.         $em   $this->getEntityManager();
  3835.         $stmt $em->getConnection()->prepare($sql);
  3836.         $resultats $stmt->executeQuery()->fetchAllAssociative();
  3837.         return $resultats;
  3838.     }
  3839. }