src/Repository/Articles/ArticleRepository.php line 3832

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";
  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.reference_appel_1,
  424.           p.reference_appel_2,
  425.           p.reference_appel_3,
  426.           p.reference_appel_4,
  427.           p.reference_appel_5,
  428.           p.reference_appel_6,
  429.           p.reference_appel_7,
  430.           p.reference_appel_8,
  431.           p.reference_appel_9,
  432.           p.reference_appel_10,
  433.           p.reference_appel_11,
  434.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  435.             p.virtuel,
  436.             p.arret_gamme,
  437.             p.article_remplacement_id,
  438.             p.libelle,
  439.             p.id,
  440.             p.prix_vente as prixVente,
  441.             p.prix_base as prixAchat,
  442.             p.poids,
  443.             p.stock,
  444.             p.stock_reserve_fabrication,
  445.             p.stock_reserve,
  446.             p.reference,
  447.             p.pump,
  448.             p.cpump,
  449.                 MATCH (".$match.") AGAINST ('".$recherche."' IN BOOLEAN MODE) as ponderation"
  450.                        .$select
  451.                        ." FROM article__article as p"
  452.                        .$jointureFromInventaire
  453.                        ." WHERE
  454.             p.temporaire is null
  455.             and
  456.             (p.archive = 0 or p.archive is null)
  457.             and
  458.             p.prix_vente like '".$tampon_recherche."%'
  459.             ".$whereStock."
  460.             ".$whereSuppl.$jointureWhereInventaire."
  461.             GROUP BY p.id
  462.                     order by ".$order.", p.id desc
  463.             LIMIT 0,100
  464.             ";
  465.                 //print_r($sql);
  466.             } else {
  467.                 /*
  468.                  p.reference LIKE '".$recherche."'
  469.                         OR
  470.                         p.libelle LIKE '".$recherche."'
  471.                         OR
  472.                         reference_appel_1 LIKE '".$recherche."'
  473.                         OR
  474.                         reference_appel_2 LIKE '".$recherche."'
  475.                         OR
  476.                         reference_appel_3 LIKE '".$recherche."'
  477.                         OR
  478.                         reference_appel_4 LIKE '".$recherche."'
  479.                         OR
  480.                         reference_appel_5 LIKE '".$recherche."'
  481.                         OR
  482.                         reference_appel_6 LIKE '".$recherche."'
  483.                         OR
  484.                         reference_appel_7 LIKE '".$recherche."'
  485.                         OR
  486.                         reference_appel_8 LIKE '".$recherche."'
  487.                         OR
  488.                         reference_appel_9 LIKE '".$recherche."'
  489.                         OR
  490.                         reference_appel_10 LIKE '".$recherche."'
  491.                         OR
  492.                         reference_appel_11 LIKE '".$recherche."'
  493.                 */
  494.                 if ($avecEtoile) {
  495.                     if ($order == "") {
  496.                         $order "p.reference";
  497.                     }
  498.                     $order     str_replace(","""$order);
  499.                     $recherche .= "%";
  500.                     $sql       "
  501.                 select
  502.             p.reference_appel_1,
  503.             p.reference_appel_2,
  504.             p.reference_appel_3,
  505.             p.reference_appel_4,
  506.             p.reference_appel_5,
  507.             p.reference_appel_6,
  508.             p.reference_appel_7,
  509.             p.reference_appel_8,
  510.             p.reference_appel_9,
  511.             p.reference_appel_10,
  512.             p.reference_appel_11,
  513.                 CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  514.                 p.arret_gamme,
  515.                 p.virtuel,
  516.                 p.article_remplacement_id,
  517.                 p.libelle,
  518.                 p.id,
  519.                 p.prix_vente as prixVente,
  520.                 p.prix_base as prixAchat,
  521.                 p.poids,
  522.                 p.stock,
  523.                 p.stock_reserve_fabrication,
  524.                 p.stock_reserve,
  525.                 p.pump,
  526.                 p.cpump,
  527.                 p.reference"
  528.                                  .$select
  529.                                  ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  530.                 WHERE
  531.                 p.temporaire is null
  532.                 and
  533.                 (p.archive = 0 or p.archive is null) ".$whereSuppl."
  534.                 and
  535.                 (
  536.                     p.reference LIKE '".$recherche."'
  537.                     OR
  538.                     p.libelle LIKE '".$recherche."'
  539.                     OR
  540.                     p.reference_appel_1 LIKE '".$recherche."'
  541.                     OR
  542.                     p.reference_appel_2 LIKE '".$recherche."'
  543.                     OR
  544.                     p.reference_appel_3 LIKE '".$recherche."'
  545.                     OR
  546.                     p.reference_appel_4 LIKE '".$recherche."'
  547.                     OR
  548.                     p.reference_appel_5 LIKE '".$recherche."'
  549.                     OR
  550.                     p.reference_appel_6 LIKE '".$recherche."'
  551.                     OR
  552.                     p.reference_appel_7 LIKE '".$recherche."'
  553.                     OR
  554.                     p.reference_appel_8 LIKE '".$recherche."'
  555.                     OR
  556.                     p.reference_appel_9 LIKE '".$recherche."'
  557.                     OR
  558.                     p.reference_appel_10 LIKE '".$recherche."'
  559.                     OR
  560.                     p.reference_appel_11 LIKE '".$recherche."'";
  561.                     if ($match2 != "") {
  562.                         $sql .= "OR ca.reference_fournisseur LIKE '".$recherche."'";
  563.                         $sql .= "OR ca.reference_fournisseur_appel LIKE '".$recherche."'";
  564.                     }
  565.                     $sql .= "   )
  566.                 ".$wherePrix.$whereStock.$jointureWhere.$jointureWhereInventaire."
  567.                 GROUP BY p.id
  568.                             order by ".$order."
  569.                 LIMIT 0,100
  570.                 ";
  571.                     //echo $sql;
  572.                     //exit;
  573.                 } else {
  574.                     //echo "OK";
  575.                     //exit;
  576.                     $recherche_contruit "";
  577.                     $recherche_tab      explode(" "$recherche);
  578.                     //print_r($recherche_tab);
  579.                     for ($r 0$r count($recherche_tab); $r++) {
  580.                         //$recherche_contruit .="+".$recherche_tab[$r]." ";
  581.                         $recherche_tab[$r]  = str_replace("+""#+"$recherche_tab[$r]);
  582.                         $recherche_contruit .= "+".$recherche_tab[$r]." ";
  583.                     }
  584.                     //echo "<div>SUPPL ".$whereSuppl."</div>";
  585.                     $recherche trim($recherche"*");
  586.                     $sql "
  587.         select
  588.         p.reference_appel_1,
  589.         p.reference_appel_2,
  590.         p.reference_appel_3,
  591.         p.reference_appel_4,
  592.         p.reference_appel_5,
  593.         p.reference_appel_6,
  594.         p.reference_appel_7,
  595.         p.reference_appel_8,
  596.         p.reference_appel_9,
  597.         p.reference_appel_10,
  598.         p.reference_appel_11,
  599.         p.stock_reserve_fabrication,
  600.         p.virtuel,
  601.         CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  602.         p.article_remplacement_id,
  603.         p.libelle,
  604.         p.id,
  605.         p.prix_vente as prixVente,
  606.         p.prix_base as prixAchat,
  607.         p.poids,
  608.         p.stock,
  609.         p.stock_reserve,
  610.         p.reference,
  611.         p.pump,
  612.         p.cpump,
  613.         p.arret_gamme,
  614.         MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation"
  615.                            .$select
  616.                            ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  617.         WHERE ";
  618.                     if ($jointureWhere != "") {
  619.                         $sql .= $jointureWhere." and ";
  620.                     }
  621.                     $sql .= " p.temporaire is null
  622.         and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  623.         and
  624.         (MATCH (".$match.")
  625.         AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)";
  626.                     if ($match2 != "" and != 1) {
  627.                         $sql .= "OR ca.reference_fournisseur LIKE '".$recherche_ref_fourn."%'";
  628.                         $sql .= "OR ca.reference_fournisseur_appel LIKE '".$recherche_ref_fourn."%'";
  629.                     }
  630.                     // if($match2 != "") $sql .= " OR MATCH (".$match2.") AGAINST ('".$recherche_ref_fourn."' IN BOOLEAN MODE) as ponderation2";
  631.                     $sql .= "";
  632.                     $sql .= $wherePrix.$whereStock;
  633.                     $sql .= ")";
  634.                     $sql .= $jointureWhereInventaire;
  635.                     $sql .= " GROUP BY p.id
  636.         order by ".$order.", ponderation desc
  637.         LIMIT 0,100
  638.         ";
  639.                     /*
  640.                     $jointureFrom = "";
  641.                     $sql = "
  642.                     select
  643.                     p.stock_reserve_fabrication,
  644.                     p.virtuel,
  645.                     CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  646.                     p.article_remplacement_id,
  647.                     p.libelle,
  648.                     p.id,
  649.                     p.prix_vente as prixVente,
  650.                     p.prix_base as prixAchat,
  651.                     p.poids,
  652.                     p.stock,
  653.                     p.stock_reserve,
  654.                     p.reference,
  655.                     p.pump,
  656.                     p.cpump,
  657.                     p.arret_gamme
  658.                     "
  659.                     .$select
  660.                     ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  661.                     WHERE ";
  662.                     if($jointureWhere != "") $sql .= $jointureWhere." and ";
  663.                     $sql .= " p.temporaire is null
  664.                     and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  665.                     and
  666.                     p.reference LIKE '".$recherche_tmp."%'
  667.                     ";
  668.                     if($match2 != "" and 1!=1) {
  669.                       $sql .= "OR ca.reference_fournisseur LIKE '".$recherche_ref_fourn."%'";
  670.                       $sql .= "OR ca.reference_fournisseur_appel LIKE '".$recherche_ref_fourn."%'";
  671.                     }
  672.                     // if($match2 != "") $sql .= " OR MATCH (".$match2.") AGAINST ('".$recherche_ref_fourn."' IN BOOLEAN MODE) ";
  673.                     $sql .= "";
  674.                     $sql .= $wherePrix.$whereStock;
  675.                     //$sql .= ")";
  676.                     $sql .= $jointureWhereInventaire;
  677.                     $sql .= " GROUP BY p.id
  678.                     order by ".$order."
  679.                     LIMIT 0,100
  680.                     ";
  681.                     */
  682.                     //echo $sql;
  683.                     //exit;
  684.                 }
  685.             }
  686.             //echo "ORDER(".$order.")";
  687.         } else {
  688.             //$order .= "p.reference asc";
  689.             $sql "
  690.             select
  691.           p.reference_appel_1,
  692.           p.reference_appel_2,
  693.           p.reference_appel_3,
  694.           p.reference_appel_4,
  695.           p.reference_appel_5,
  696.           p.reference_appel_6,
  697.           p.reference_appel_7,
  698.           p.reference_appel_8,
  699.           p.reference_appel_9,
  700.           p.reference_appel_10,
  701.           p.reference_appel_11,
  702.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  703.             p.arret_gamme,
  704.             p.virtuel,
  705.             p.article_remplacement_id,
  706.             p.virtuel,
  707.             p.libelle,
  708.             p.id,
  709.             p.prix_vente as prixVente,
  710.             p.prix_base as prixAchat,
  711.             p.poids,
  712.             p.stock,
  713.             p.stock_reserve_fabrication,
  714.             p.stock_reserve,
  715.             p.cpump,
  716.             p.reference"
  717.                    .$select
  718.                    ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  719.             WHERE ";
  720.             if ($jointureWhere != "") {
  721.                 $sql .= $jointureWhere." and ";
  722.             }
  723.             $sql .= " p.temporaire is null
  724.             and
  725.             (p.archive = 0 or p.archive is null)
  726.                         ".$whereSuppl.$whereStock.$jointureWhereInventaire."
  727.                             GROUP BY p.id
  728.             order by ".$order."
  729.             LIMIT 0,100
  730.             ";
  731.         }
  732.         //where p.libelle like '%".$recherche."%' or p.reference like '%".$recherche."%'
  733.         //echo $sql;
  734.         //exit;
  735.         $em   $this->getEntityManager();
  736.         $stmt $em->getConnection()->prepare($sql);
  737.         $resultats $stmt->executeQuery()->fetchAllAssociative();
  738.         //$resultats = array();
  739.         //echo $sql;
  740.         //exit;
  741.         //echo "<div>------(".$mouvemente.")</div>";
  742.         //Si la recherche dans les articles mouvementés ne donne rien, on effectue la requete sur les articles non mouvementés
  743.         if (count($resultats) == && $mouvemente == "1") {
  744.             //$mouvemente =0;
  745.             /*if(!$avecEtoile) {
  746.                 $recherche = str_replace("*","",$recherche);
  747.             }
  748.             */
  749.             //echo "<div>ototo ".$recherche."</div>";
  750.             //echo "--".$mouvemente;
  751.             //exit;
  752.             $resultats $this->getRechercheArticleSelect(
  753.                 $recherche_tmp,
  754.                 $produitInterne,
  755.                 $fournisseur,
  756.                 $articlesAExclure,
  757.                 $stock,
  758.                 $reference_search,
  759.                 $libelle_search,
  760.                 $prix_search,
  761.                 $tri_reference,
  762.                 $tri_libelle,
  763.                 $tri_prix,
  764.                 $tri_stock,
  765.                 $categories,
  766.                 "0",
  767.                 $inventaire
  768.             );
  769.             //exit;
  770.         }
  771.         //exit;
  772.         return $resultats;
  773.     }
  774.     public function getRechercheArticleSelect2($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock ""$reference_search ""$libelle_search "",
  775.                                                $prix_search ""$tri_reference ""$tri_libelle ""$tri_prix ""$tri_stock ""$categories ""$mouvemente 1$inventaire ''
  776.     ) {
  777.         $sql2                    "";
  778.         $sql3                    "";
  779.         $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';
  780.         $match2                  "";
  781.         $select                  '';
  782.         $jointureFromInventaire  "";
  783.         $jointureWhereInventaire "";
  784.         $recherche str_replace("*""%"$recherche);
  785.         //$recherche = str_replace("-"," ",$recherche);
  786.         $recherche str_replace("."""$recherche);
  787.         $recherche str_replace("/""\/"$recherche);
  788.         $recherche str_replace("   "" "$recherche);
  789.         $recherche_tmp $recherche;
  790.         $avecEtoile    false;
  791.         $pos strpos($recherche"*");
  792.         if ($pos !== false) {
  793.             $avecEtoile true;
  794.         }
  795.         $recherche str_replace("*""%"$recherche);
  796.         $wherePrix        "";
  797.         $tampon_recherche $recherche;
  798.         $recherche        trim($recherche);
  799.         if ( ! $avecEtoile) {
  800.             $recherche str_replace(" ""* "$recherche);
  801.         }
  802.         if ( ! $avecEtoile) {
  803.             $recherche .= "*";
  804.         }
  805.         $whereSuppl " and p.parent_id is null";
  806.         $whereStock "";
  807.         if ($stock == 1) {
  808.             $whereStock .= " and p.stock > 0";
  809.         }
  810.         $order "";
  811.         if ($tri_reference != "") {
  812.             $order "p.reference ".$tri_reference.", ";
  813.         }
  814.         if ($tri_prix != "") {
  815.             $order "p.prix_vente ".$tri_prix.", ";
  816.         }
  817.         if ($tri_stock != "") {
  818.             $order "p.stock ".$tri_stock.", ";
  819.         }
  820.         if ($tri_libelle != "") {
  821.             $order "p.libelle ".$tri_libelle.", ";
  822.         }
  823.         if ($order == "") {
  824.             $order "p.reference ASC, ";
  825.         }
  826.         //echo "<div>order : ".$order."</div>";
  827.         $jointureFrom   "";
  828.         $jointureWhere  "";
  829.         $jointureWhere2 "";
  830.         if ( ! ($fournisseur == null || $fournisseur == '')) {
  831.             $match2 'ca.reference_fournisseur, ca.reference_fournisseur_appel';
  832.             //$jointureFrom .= " INNER JOIN article__condition_achat as ca on ca.article_id = p.id";
  833.             //$jointureWhere .= " and (p.non_commandable = 0 or p.non_commandable = 1 or p.non_commandable is null)";
  834.             /*
  835.             $jointureWhere .= "(p.id IN (select
  836.         ca.article_id
  837.         FROM article__condition_achat as ca
  838.         WHERE
  839.         ca.fournisseur_id = ".$fournisseur." )
  840.         OR p.divers=1 ";
  841.             */
  842.             if ($categories != '') {
  843.                 /*
  844.                 $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';
  845.                 foreach($categories as $categorie){
  846.                     $jointureWhere .= " OR cat.libelle LIKE '".$categorie."'";
  847.                 }
  848.                      *
  849.                      */
  850.                 $selectTemp "(select id FROM article__categorie cat WHERE ";
  851.                 foreach ($categories as $key => $categorie) {
  852.                     if ($key == 0) {
  853.                         $selectTemp .= " ";
  854.                     } else {
  855.                         $selectTemp .= " OR";
  856.                     }
  857.                     $selectTemp .= " cat.reference LIKE '".$categorie."'";
  858.                 }
  859.                 $selectTemp .= ") as tempAC ";
  860.                 $jointureFrom .= "LEFT JOIN ".$selectTemp." LEFT JOIN article__article_categorie as aac ON aac.categorie_id = tempAC.id";
  861.             }
  862.             //$jointureWhere .= ")";
  863.             $whereSuppl .= " and (p.arret_gamme != 1 or p.arret_gamme IS NULL)";
  864.         }
  865.         if ($produitInterne == true || $produitInterne == 'true') {
  866.             $whereSuppl .= ' and p.produit_interne =1';
  867.         }
  868.         if (count($articlesAExclure) > 0) {
  869.             $whereSuppl .= " and p.id NOT IN ( '".implode($articlesAExclure"', '")."' )";// != :articleAExclure and p.id != :articleAExclure
  870.         }
  871.         if ($mouvemente != "0" && $mouvemente !== null or ($mouvemente == "")) {
  872.             $whereSuppl .= " and (p.mouvemente = 1 or p.divers = 1)";
  873.         } else {
  874.             //$whereSuppl .= " and p.divers = 1";
  875.         }
  876.         if ( ! $avecEtoile) {
  877.             $recherche str_replace("-"""$recherche);
  878.         }
  879.         if ($inventaire != '') {
  880.             //$select .= ', count(ai.id) as nbArticleInventaire';
  881.             $select                 .= ', COUNT(CASE WHEN ai.inventaire_id = '.$inventaire.' then 1 ELSE NULL END) as "nbArticleInventaire"';
  882.             $jointureFromInventaire .= ' LEFT JOIN inventaire__article_inventaire ai ON ai.article_id = p.id';
  883.             //$jointureWhereInventaire .= ' and (ai.inventaire_id = '.$inventaire.' OR ai.inventaire_id IS NULL)';
  884.         }
  885.         if ($recherche != "" and $recherche != "*") {
  886.             if ($prix_search == 1) {
  887.                 $sql "
  888.             select
  889.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  890.           p.reference_appel_1,
  891.           p.reference_appel_2,
  892.           p.reference_appel_3,
  893.           p.reference_appel_4,
  894.           p.reference_appel_5,
  895.           p.reference_appel_6,
  896.           p.reference_appel_7,
  897.           p.reference_appel_8,
  898.           p.reference_appel_9,
  899.           p.reference_appel_10,
  900.           p.reference_appel_11,
  901.             p.arret_gamme,
  902.             p.virtuel,
  903.                 p.divers,
  904.             p.article_remplacement_id,
  905.             p.libelle,
  906.             p.id,
  907.             p.reference_fournisseur,
  908.             p.prix_vente as prixVente,
  909.             p.prix_base as prixAchat,
  910.             p.poids,
  911.             p.stock,
  912.             p.stock_reserve_fabrication,
  913.             p.stock_reserve,
  914.             p.reference,
  915.             p.pump,
  916.             p.cpump,
  917.             MATCH (".$match.") AGAINST ('".$recherche."' IN BOOLEAN MODE) as ponderation"
  918.                        .$select
  919.                        ." FROM article__article as p"
  920.                        .$jointureFromInventaire
  921.                        ." WHERE
  922.             p.temporaire is null
  923.             and
  924.             (p.archive = 0 or p.archive is null)
  925.             and
  926.             p.prix_vente like '".$tampon_recherche."%'
  927.             ".$whereStock."
  928.             ".$whereSuppl.$jointureWhereInventaire."
  929.             GROUP BY p.id
  930.                     order by ".$order." p.id desc
  931.             LIMIT 0,100
  932.             ";
  933.             } else {
  934.                 /*
  935.                  p.reference LIKE '".$recherche."'
  936.                         OR
  937.                         p.libelle LIKE '".$recherche."'
  938.                         OR
  939.                         reference_appel_1 LIKE '".$recherche."'
  940.                         OR
  941.                         reference_appel_2 LIKE '".$recherche."'
  942.                         OR
  943.                         reference_appel_3 LIKE '".$recherche."'
  944.                         OR
  945.                         reference_appel_4 LIKE '".$recherche."'
  946.                         OR
  947.                         reference_appel_5 LIKE '".$recherche."'
  948.                         OR
  949.                         reference_appel_6 LIKE '".$recherche."'
  950.                         OR
  951.                         reference_appel_7 LIKE '".$recherche."'
  952.                         OR
  953.                         reference_appel_8 LIKE '".$recherche."'
  954.                         OR
  955.                         reference_appel_9 LIKE '".$recherche."'
  956.                         OR
  957.                         reference_appel_10 LIKE '".$recherche."'
  958.                         OR
  959.                         reference_appel_11 LIKE '".$recherche."'
  960.                 */
  961.                 if ($avecEtoile) {
  962.                     if ($order == "") {
  963.                         $order "p.reference";
  964.                     }
  965.                     $order     str_replace(","""$order);
  966.                     $recherche .= "%";
  967.                     $sql       "
  968.                 select
  969.                 CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  970.             p.reference_appel_1,
  971.             p.reference_appel_2,
  972.             p.reference_appel_3,
  973.             p.reference_appel_4,
  974.             p.reference_appel_5,
  975.             p.reference_appel_6,
  976.             p.reference_appel_7,
  977.             p.reference_appel_8,
  978.             p.reference_appel_9,
  979.             p.reference_appel_10,
  980.             p.reference_appel_11,
  981.                 p.arret_gamme,
  982.                 p.virtuel,
  983.                         p.divers,
  984.                 p.article_remplacement_id,
  985.                 p.libelle,
  986.                 p.id,
  987.                 p.reference_fournisseur,
  988.                 p.prix_vente as prixVente,
  989.                 p.prix_base as prixAchat,
  990.                 p.poids,
  991.                 p.stock,
  992.                 p.stock_reserve_fabrication,
  993.                 p.stock_reserve,
  994.                 p.pump,
  995.                 p.cpump,
  996.                 p.reference"
  997.                                  .$select
  998.                                  ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  999.                 WHERE
  1000.                 p.temporaire is null
  1001.                 and
  1002.                 (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1003.                 and
  1004.                 (
  1005.                     p.reference LIKE '".$recherche."'
  1006.                     OR
  1007.                     p.libelle LIKE '".$recherche."'
  1008.                     OR
  1009.                     p.reference_appel_1 LIKE '".$recherche."'
  1010.                     OR
  1011.                     p.reference_appel_2 LIKE '".$recherche."'
  1012.                     OR
  1013.                     p.reference_appel_3 LIKE '".$recherche."'
  1014.                     OR
  1015.                     p.reference_appel_4 LIKE '".$recherche."'
  1016.                     OR
  1017.                     p.reference_appel_5 LIKE '".$recherche."'
  1018.                     OR
  1019.                     p.reference_appel_6 LIKE '".$recherche."'
  1020.                     OR
  1021.                     p.reference_appel_7 LIKE '".$recherche."'
  1022.                     OR
  1023.                     p.reference_appel_8 LIKE '".$recherche."'
  1024.                     OR
  1025.                     p.reference_appel_9 LIKE '".$recherche."'
  1026.                     OR
  1027.                     p.reference_appel_10 LIKE '".$recherche."'
  1028.                     OR
  1029.                     p.reference_appel_11 LIKE '".$recherche."'";
  1030.                     if ($match2 != "") {
  1031.                         $sql .= "OR ca.reference_fournisseur LIKE '".$recherche."'";
  1032.                         $sql .= "OR ca.reference_fournisseur_appel LIKE '".$recherche."'";
  1033.                     }
  1034.                     $sql .= "   )
  1035.                 ".$wherePrix.$whereStock.$jointureWhere.$jointureWhereInventaire."
  1036.                 GROUP BY p.id
  1037.                             order by ".$order."
  1038.                 LIMIT 0,100
  1039.                 ";
  1040.                     //echo $sql;
  1041.                     //exit;
  1042.                 } else {
  1043.                     //echo "OK";
  1044.                     //exit;
  1045.                     $recherche_contruit "";
  1046.                     $recherche_tab      explode(" "$recherche);
  1047.                     for ($r 0$r count($recherche_tab); $r++) {
  1048.                         //$recherche_contruit .="+".$recherche_tab[$r]." ";
  1049.                         $recherche_tab[$r]  = str_replace("+""#+"$recherche_tab[$r]);
  1050.                         $recherche_contruit .= "+".$recherche_tab[$r]." ";
  1051.                     }
  1052.                     //echo "<div>SUPPL ".$whereSuppl."</div>";
  1053.                     /*
  1054.             $sql = "
  1055.             select
  1056.             p.stock_reserve_fabrication,
  1057.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1058.             p.article_remplacement_id,
  1059.             p.libelle,
  1060.             p.id,
  1061.             p.prix_vente as prixVente,
  1062.             p.prix_base as prixAchat,
  1063.             p.poids,
  1064.             p.stock,
  1065.             p.stock_reserve,
  1066.             p.reference,
  1067.             p.pump,
  1068.             p.arret_gamme,
  1069.             MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation
  1070.             FROM article__article as p ".$jointureFrom."
  1071.             WHERE ";
  1072.                         if($jointureWhere != "") $sql .= $jointureWhere." and ";
  1073.             $sql .= " p.temporaire is null
  1074.             and p.archive = 0".$whereSuppl."
  1075.             and
  1076.             (MATCH (".$match.")
  1077.             AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)";
  1078.              if($match2 != "") $sql .= " OR MATCH (".$match2.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) ";
  1079.             $sql .= "";
  1080.             $sql .= $wherePrix.$whereStock;
  1081.                     $sql .= ")
  1082.             GROUP BY p.id
  1083.                         order by ".$order.", ponderation desc
  1084.             LIMIT 0,100
  1085.             ";
  1086.             //echo $sql;
  1087.         }
  1088.         */
  1089.                     //pre select on condition achat before match
  1090.                     if ($match2 != '') {
  1091.                         $sql "select
  1092.                             p.reference_appel_1,
  1093.                             p.reference_appel_2,
  1094.                             p.reference_appel_3,
  1095.                             p.reference_appel_4,
  1096.                             p.reference_appel_5,
  1097.                             p.reference_appel_6,
  1098.                             p.reference_appel_7,
  1099.                             p.reference_appel_8,
  1100.                             p.reference_appel_9,
  1101.                             p.reference_appel_10,
  1102.                             p.reference_appel_11,
  1103.                                           temp.reference_fournisseur as reference_fournisseur,
  1104.                             p.stock_reserve_fabrication,
  1105.                             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1106.                             p.article_remplacement_id,
  1107.                             p.libelle,
  1108.                             p.virtuel,
  1109.                             p.divers,
  1110.                             p.id,
  1111.                             p.reference_fournisseur as old,
  1112.                             p.prix_vente as prixVente,
  1113.                             p.prix_base as prixAchat,
  1114.                             p.poids,
  1115.                             p.stock,
  1116.                             p.stock_reserve,
  1117.                             p.reference,
  1118.                             p.pump,
  1119.                             p.cpump,
  1120.                             p.arret_gamme,
  1121.                             MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation";
  1122.                         $sql .= $select;
  1123.                         $sql .= " FROM (select
  1124.                     *
  1125.                     FROM article__condition_achat
  1126.                     WHERE
  1127.                     date_supression is null and
  1128.                     fournisseur_id = ".$fournisseur." ) AS temp ";
  1129.                         $sql .= " LEFT JOIN article__article as p ON temp.article_id = p.id";
  1130.                         $sql .= $jointureFromInventaire
  1131.                                 //. " LEFT JOIN article__condition_achat ca ON ca.id = p.id"
  1132.                                 ." WHERE ";
  1133.                         if ($jointureWhere2 != "") {
  1134.                             $sql .= $jointureWhere2." and ";
  1135.                         }
  1136.                         $sql .= " p.temporaire is null
  1137.                 and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1138.                 and
  1139.                 (MATCH (".$match.")
  1140.                 AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)"
  1141.                                 ." OR MATCH (temp.reference_fournisseur, temp.reference_fournisseur_appel) AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) ";
  1142.                         $sql .= $wherePrix.$whereStock;
  1143.                         $sql .= ")";
  1144.                         $sql .= $jointureWhereInventaire;
  1145.                         $sql .= " GROUP BY p.id
  1146.                             order by ".$order." ponderation desc
  1147.                 LIMIT 0,100
  1148.                 ";
  1149.                         $sql2 "select
  1150.                             p.stock_reserve_fabrication,
  1151.                             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1152.                             p.reference_appel_1,
  1153.                             p.reference_appel_2,
  1154.                             p.reference_appel_3,
  1155.                             p.reference_appel_4,
  1156.                             p.reference_appel_5,
  1157.                             p.reference_appel_6,
  1158.                             p.reference_appel_7,
  1159.                             p.reference_appel_8,
  1160.                             p.reference_appel_9,
  1161.                             p.reference_appel_10,
  1162.                             p.reference_appel_11,
  1163.                             p.article_remplacement_id,
  1164.                             p.libelle,
  1165.                             p.virtuel,
  1166.                             p.divers,
  1167.                             p.id,
  1168.                                           p.reference_fournisseur,
  1169.                             p.prix_vente as prixVente,
  1170.                             p.prix_base as prixAchat,
  1171.                             p.poids,
  1172.                             p.stock,
  1173.                             p.stock_reserve,
  1174.                             p.reference,
  1175.                             p.pump,
  1176.                             p.cpump,
  1177.                             p.arret_gamme,
  1178.                             MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation ";
  1179.                         $sql2 .= "FROM ".$selectTemp;
  1180.                         $sql2 .= $jointureFromInventaire;
  1181.                         $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"
  1182.                                  //. " LEFT JOIN article__condition_achat ca ON ca.id = p.id"
  1183.                                  ." WHERE ";
  1184.                         if ($jointureWhere2 != "") {
  1185.                             $sql .= $jointureWhere2." and ";
  1186.                         }
  1187.                         $sql2 .= " p.temporaire is null
  1188.                 and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1189.                 and
  1190.                 (MATCH (".$match.")
  1191.                 AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)"//. " OR MATCH (ca.reference_fournisseur, ca.reference_fournisseur_appel) AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) ";
  1192.                         ;
  1193.                         $sql2 .= $wherePrix.$whereStock;
  1194.                         $sql2 .= ")";
  1195.                         $sql2 .= $jointureWhereInventaire;
  1196.                         $sql2 .= " GROUP BY p.id
  1197.                             order by ".$order." ponderation desc
  1198.                 LIMIT 0,100
  1199.                 ";
  1200.                         //sql3
  1201.                         $sql3 "select
  1202.                             p.stock_reserve_fabrication,
  1203.                             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1204.                             p.reference_appel_1,
  1205.                             p.reference_appel_2,
  1206.                             p.reference_appel_3,
  1207.                             p.reference_appel_4,
  1208.                             p.reference_appel_5,
  1209.                             p.reference_appel_6,
  1210.                             p.reference_appel_7,
  1211.                             p.reference_appel_8,
  1212.                             p.reference_appel_9,
  1213.                             p.reference_appel_10,
  1214.                             p.reference_appel_11,
  1215.                             p.article_remplacement_id,
  1216.                             p.libelle,
  1217.                             p.virtuel,
  1218.                             p.divers,
  1219.                             p.id,
  1220.                             p.reference_fournisseur,
  1221.                             p.prix_vente as prixVente,
  1222.                             p.prix_base as prixAchat,
  1223.                             p.poids,
  1224.                             p.stock,
  1225.                             p.stock_reserve,
  1226.                             p.reference,
  1227.                             p.pump,
  1228.                             p.cpump,
  1229.                             p.arret_gamme,
  1230.                             MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation ";
  1231.                         $preselectWhere "divers = 1 and parent_id IS NULL and temporaire is null ";
  1232.                         $sql3 .= "FROM (select * FROM article__article WHERE ".$preselectWhere." and (archive = 0 or archive is null)) AS p";
  1233.                         $sql3 .= $jointureFromInventaire;
  1234.                         $sql3 .= " WHERE (MATCH (".$match.")
  1235.                             AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)";
  1236.                         $sql3 .= $wherePrix.$whereStock;
  1237.                         $sql3 .= ")";
  1238.                         $sql3 .= $jointureWhereInventaire;
  1239.                         $sql3 .= " GROUP BY p.id
  1240.                                 order by ".$order." ponderation desc
  1241.                                 LIMIT 0,100
  1242.                                 ";
  1243.                     }
  1244.                 }
  1245.             }
  1246.         } else {
  1247.             $order .= "p.reference asc";
  1248.             $sql "
  1249.             select
  1250.           p.reference_appel_1,
  1251.           p.reference_appel_2,
  1252.           p.reference_appel_3,
  1253.           p.reference_appel_4,
  1254.           p.reference_appel_5,
  1255.           p.reference_appel_6,
  1256.           p.reference_appel_7,
  1257.           p.reference_appel_8,
  1258.           p.reference_appel_9,
  1259.           p.reference_appel_10,
  1260.           p.reference_appel_11,
  1261.             p.arret_gamme,
  1262.             p.virtuel,
  1263.                 p.divers,
  1264.             p.article_remplacement_id,
  1265.             p.libelle,
  1266.             p.id,
  1267.             p.reference_fournisseur,
  1268.             p.prix_vente as prixVente,
  1269.             p.prix_base as prixAchat,
  1270.             p.poids,
  1271.             p.stock,
  1272.             p.stock_reserve_fabrication,
  1273.         p.stock_reserve,
  1274.         p.pump,
  1275.         p.cpump,
  1276.             p.reference"
  1277.                    .$select
  1278.                    //." 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"
  1279.                    ." FROM (select
  1280.                     *
  1281.                     FROM article__condition_achat
  1282.                     WHERE
  1283.                     fournisseur_id = ".$fournisseur." ) AS temp "
  1284.                    ." LEFT JOIN article__article as p ON temp.article_id = p.id"
  1285.                    .$jointureFromInventaire
  1286.                    ." WHERE ";
  1287.             if ($jointureWhere != '') {
  1288.                 $sql .= $jointureWhere." and";
  1289.             }
  1290.             $sql .= " p.temporaire is null
  1291.             and
  1292.             (p.archive = 0 or p.archive is null)
  1293.                         "
  1294.                     //.$whereSuppl
  1295.                     .$whereStock.$jointureWhereInventaire.
  1296.                     "
  1297.                             GROUP BY p.id
  1298.             order by ".$order."
  1299.             LIMIT 0,100
  1300.             ";
  1301.         }
  1302.         //where p.libelle like '%".$recherche."%' or p.reference like '%".$recherche."%'
  1303.         //echo $sql;
  1304.         $em   $this->getEntityManager();
  1305.         $stmt $em->getConnection()->prepare($sql);
  1306.         $resultats1 $stmt->executeQuery()->fetchAllAssociative();
  1307.         $resultats  $resultats1;
  1308.         //var_dump($resultats);
  1309.         if ($sql3 != "") {
  1310.             $stmt $em->getConnection()->prepare($sql3);
  1311.             $resultats3 $stmt->executeQuery()->fetchAllAssociative();
  1312.             //var_dump($resultats2);
  1313.             $resultats array_merge($resultats$resultats3);
  1314.             //var_dump($resultats);
  1315.         }
  1316.         if ($sql2 != "" && count($resultats) == 0) {
  1317.             $stmt $em->getConnection()->prepare($sql2);
  1318.             $resultats2 $stmt->executeQuery()->fetchAllAssociative();
  1319.             //var_dump($resultats2);
  1320.             $resultats array_merge($resultats$resultats2);
  1321.             //var_dump($resultats);
  1322.         }
  1323.         //echo $sql;
  1324.         //exit;
  1325.         //echo "<div>------(".$mouvemente.")</div>";
  1326.         //Si la recherche dans les articles mouvementés ne donne rien, on effectue la requete sur les articles non mouvementés
  1327.         if (count($resultats1) == && count($resultats) == && $mouvemente == "1") {
  1328.             //$mouvemente =0;
  1329.             /*if(!$avecEtoile) {
  1330.                 $recherche = str_replace("*","",$recherche);
  1331.             }
  1332.             */
  1333.             //echo "<div>ototo ".$recherche."</div>";
  1334.             //echo "--".$mouvemente;
  1335.             //exit;
  1336.             $resultats $this->getRechercheArticleSelect2(
  1337.                 $recherche_tmp,
  1338.                 $produitInterne,
  1339.                 $fournisseur,
  1340.                 $articlesAExclure,
  1341.                 $stock,
  1342.                 $reference_search,
  1343.                 $libelle_search,
  1344.                 $prix_search,
  1345.                 $tri_reference,
  1346.                 $tri_libelle,
  1347.                 $tri_prix,
  1348.                 $tri_stock,
  1349.                 $categories,
  1350.                 "0",
  1351.                 $inventaire
  1352.             );
  1353.             //exit;
  1354.         }
  1355.         //exit;
  1356.         return $resultats;
  1357.     }
  1358.     public function getRechercheArticleSelect3($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock ""$reference_search ""$libelle_search "",
  1359.                                                $prix_search ""$tri_reference ""$tri_libelle ""$tri_prix ""$tri_stock ""$categories ""$mouvemente 1$inventaire ''
  1360.     ) {
  1361.         $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';
  1362.         $select                  '';
  1363.         $jointureFromInventaire  "";
  1364.         $jointureWhereInventaire "";
  1365.         $recherche str_replace("*""%"$recherche);
  1366.         $recherche str_replace("-"" "$recherche);
  1367.         $recherche str_replace("."""$recherche);
  1368.         $recherche str_replace("/""\/"$recherche);
  1369.         $recherche str_replace("   "" "$recherche);
  1370.         $recherche str_replace("  "" "$recherche);
  1371.         $recherche_tmp $recherche;
  1372.         $avecEtoile    false;
  1373.         $pos strpos($recherche"*");
  1374.         if ($pos !== false) {
  1375.             $avecEtoile true;
  1376.         }
  1377.         $recherche str_replace("*""%"$recherche);
  1378.         $wherePrix        "";
  1379.         $tampon_recherche $recherche;
  1380.         $recherche        trim($recherche);
  1381.         if ( ! $avecEtoile) {
  1382.             $recherche str_replace(" ""* "$recherche);
  1383.         }
  1384.         if ( ! $avecEtoile) {
  1385.             $recherche .= "*";
  1386.         }
  1387.         $whereSuppl " and p.parent_id is null";
  1388.         $whereStock "";
  1389.         if ($stock == 1) {
  1390.             $whereStock .= " and p.stock > 0";
  1391.         }
  1392.         $order "";
  1393.         if ($tri_reference != "") {
  1394.             $order "p.reference ".$tri_reference.", ";
  1395.         }
  1396.         if ($tri_prix != "") {
  1397.             $order "p.prix_vente ".$tri_prix.", ";
  1398.         }
  1399.         if ($tri_stock != "") {
  1400.             $order "p.stock ".$tri_stock.", ";
  1401.         }
  1402.         if ($tri_libelle != "") {
  1403.             $order "p.libelle ".$tri_libelle.", ";
  1404.         }
  1405.         if ($order == "") {
  1406.             $order "p.reference ASC ";
  1407.         }
  1408.         //echo "<div>order : ".$order."</div>";
  1409.         $jointureFrom  "";
  1410.         $jointureWhere "";
  1411.         /*
  1412.      if($categories != ''){
  1413.          $jointureFrom .= ' LEFT JOIN article__article_categorie acat on acat.article_id = p.id JOIN article__categorie as cat on cat.id = acat.categorie_id';
  1414.          //$jointureWhere .= " and cat.libelle LIKE '".$categorie."%'";
  1415.          foreach($categories as $key=>$categorie){
  1416.              if($key == 0 )
  1417.                  $jointureWhere .= " and (";
  1418.              else
  1419.                  $jointureWhere .= " OR";
  1420.              $jointureWhere .= " cat.libelle LIKE '".$categorie."'";
  1421.          }
  1422.          $jointureWhere .= ")";
  1423.      }
  1424.          *
  1425.          */
  1426.         if ($categories != '') {
  1427.             /*
  1428.             $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';
  1429.             foreach($categories as $categorie){
  1430.                 $jointureWhere .= " OR cat.libelle LIKE '".$categorie."'";
  1431.             }
  1432.                  *
  1433.                  */
  1434.             $selectTemp "(select id FROM article__categorie cat WHERE ";
  1435.             foreach ($categories as $key => $categorie) {
  1436.                 if ($key == 0) {
  1437.                     $selectTemp .= " ";
  1438.                 } else {
  1439.                     $selectTemp .= " OR";
  1440.                 }
  1441.                 $selectTemp .= " cat.reference LIKE '".$categorie."'";
  1442.             }
  1443.             $selectTemp .= ") as tempAC ";
  1444.             $jointureFrom .= "LEFT JOIN ".$selectTemp." LEFT JOIN article__article_categorie as aac ON aac.categorie_id = tempAC.id";
  1445.         }
  1446.         if ($produitInterne == true || $produitInterne == 'true') {
  1447.             $whereSuppl .= ' and p.produit_interne =1';
  1448.         }
  1449.         if (count($articlesAExclure) > 0) {
  1450.             $whereSuppl .= " and p.id NOT IN ( '".implode($articlesAExclure"', '")."' )";// != :articleAExclure and p.id != :articleAExclure
  1451.         }
  1452.         /*
  1453.         if ($mouvemente == "")
  1454.             $whereSuppl .= " and p.divers = 1";
  1455.         else
  1456.             $whereSuppl .= " and (p.mouvemente = ".$mouvemente." or p.divers = 1)";
  1457.          *
  1458.          */
  1459.         if ($mouvemente != "0" && $mouvemente !== null or ($mouvemente == "")) {
  1460.             $mouvemente 1;
  1461.             $whereSuppl .= " and (p.mouvemente = ".$mouvemente." or p.divers = 1)";
  1462.         } else {
  1463.             //echo "titit (".$mouvemente.")";
  1464.             $whereSuppl .= " and (p.mouvemente = ".$mouvemente." or p.mouvemente =1 or p.mouvemente is null or p.divers = 1)";
  1465.         }
  1466.         //echo 'mouvemente : '.$mouvemente;
  1467.         if ( ! $avecEtoile) {
  1468.             $recherche str_replace("-"""$recherche);
  1469.         }
  1470.         if ($inventaire != '') {
  1471.             //$select .= ', count(ai.id) as nbArticleInventaire';
  1472.             $select                 .= ', COUNT(CASE WHEN ai.inventaire_id = '.$inventaire.' then 1 ELSE NULL END) as "nbArticleInventaire"';
  1473.             $jointureFromInventaire .= ' LEFT JOIN inventaire__article_inventaire ai ON ai.article_id = p.id';
  1474.             //$jointureWhereInventaire .= ' and (ai.inventaire_id = '.$inventaire.' OR ai.inventaire_id IS NULL)';
  1475.         }
  1476.         //echo "<div>S : ".$recherche."</div>";
  1477.         if ($recherche != "" and $recherche != "*") {
  1478.             if ($prix_search == 1) {
  1479.                 $sql "
  1480.             select
  1481.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1482.             p.arret_gamme,
  1483.             p.virtuel,
  1484.             p.article_remplacement_id,
  1485.             p.libelle,
  1486.             p.id,
  1487.             p.prix_vente as prixVente,
  1488.             p.prix_base as prixAchat,
  1489.             p.poids,
  1490.             p.stock,
  1491.             p.stock_reserve_fabrication,
  1492.             p.stock_reserve,
  1493.             p.reference,
  1494.             p.pump,
  1495.             p.cpump,
  1496.             MATCH (".$match.") AGAINST ('".$recherche."' IN BOOLEAN MODE) as ponderation"
  1497.                        .$select
  1498.                        ." FROM article__article as p"
  1499.                        .$jointureFromInventaire
  1500.                        ." WHERE
  1501.             p.temporaire is null
  1502.             and
  1503.             (p.archive = 0 or p.archive is null)
  1504.             and
  1505.             p.prix_vente like '".$tampon_recherche."%'
  1506.             ".$whereStock."
  1507.             ".$whereSuppl.$jointureWhereInventaire."
  1508.             GROUP BY p.id
  1509.                     order by ".$order." p.id desc
  1510.             LIMIT 0,100
  1511.             ";
  1512.             } else {
  1513.                 /*
  1514.                  p.reference LIKE '".$recherche."'
  1515.                         OR
  1516.                         p.libelle LIKE '".$recherche."'
  1517.                         OR
  1518.                         reference_appel_1 LIKE '".$recherche."'
  1519.                         OR
  1520.                         reference_appel_2 LIKE '".$recherche."'
  1521.                         OR
  1522.                         reference_appel_3 LIKE '".$recherche."'
  1523.                         OR
  1524.                         reference_appel_4 LIKE '".$recherche."'
  1525.                         OR
  1526.                         reference_appel_5 LIKE '".$recherche."'
  1527.                         OR
  1528.                         reference_appel_6 LIKE '".$recherche."'
  1529.                         OR
  1530.                         reference_appel_7 LIKE '".$recherche."'
  1531.                         OR
  1532.                         reference_appel_8 LIKE '".$recherche."'
  1533.                         OR
  1534.                         reference_appel_9 LIKE '".$recherche."'
  1535.                         OR
  1536.                         reference_appel_10 LIKE '".$recherche."'
  1537.                         OR
  1538.                         reference_appel_11 LIKE '".$recherche."'
  1539.                 */
  1540.                 if ($avecEtoile) {
  1541.                     if ($order == "") {
  1542.                         $order "p.reference";
  1543.                     }
  1544.                     $order     str_replace(","""$order);
  1545.                     $recherche .= "%";
  1546.                     $sql       "
  1547.                 select
  1548.                 CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1549.                 p.arret_gamme,
  1550.                 p.virtuel,
  1551.                 p.article_remplacement_id,
  1552.                 p.libelle,
  1553.                 p.id,
  1554.                 p.prix_vente as prixVente,
  1555.                 p.prix_base as prixAchat,
  1556.                 p.poids,
  1557.                 p.stock,
  1558.                 p.stock_reserve_fabrication,
  1559.                 p.stock_reserve,
  1560.                 p.pump,
  1561.                 p.cpump,
  1562.                 p.reference"
  1563.                                  .$select
  1564.                                  ." FROM article__article as p ".$jointureFrom.$jointureFromInventaire."
  1565.                 WHERE
  1566.                 p.temporaire is null
  1567.                 and
  1568.                 (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1569.                 and
  1570.                 (
  1571.                     p.reference LIKE '".$recherche."'
  1572.                     OR
  1573.                     p.libelle LIKE '".$recherche."'
  1574.                     OR
  1575.                     p.reference_appel_1 LIKE '".$recherche."'
  1576.                     OR
  1577.                     p.reference_appel_2 LIKE '".$recherche."'
  1578.                     OR
  1579.                     p.reference_appel_3 LIKE '".$recherche."'
  1580.                     OR
  1581.                     p.reference_appel_4 LIKE '".$recherche."'
  1582.                     OR
  1583.                     p.reference_appel_5 LIKE '".$recherche."'
  1584.                     OR
  1585.                     p.reference_appel_6 LIKE '".$recherche."'
  1586.                     OR
  1587.                     p.reference_appel_7 LIKE '".$recherche."'
  1588.                     OR
  1589.                     p.reference_appel_8 LIKE '".$recherche."'
  1590.                     OR
  1591.                     p.reference_appel_9 LIKE '".$recherche."'
  1592.                     OR
  1593.                     p.reference_appel_10 LIKE '".$recherche."'
  1594.                     OR
  1595.                     p.reference_appel_11 LIKE '".$recherche."'";
  1596.                     if ($match2 != "") {
  1597.                         $sql .= "OR ca.reference_fournisseur LIKE '".$recherche."'";
  1598.                         $sql .= "OR ca.reference_fournisseur_appel LIKE '".$recherche."'";
  1599.                     }
  1600.                     $sql .= "   )
  1601.                 ".$wherePrix.$whereStock.$jointureWhere.$jointureWhereInventaire."
  1602.                 GROUP BY p.id
  1603.                             order by ".$order."
  1604.                 LIMIT 0,100
  1605.                 ";
  1606.                     //echo $sql;
  1607.                     //exit;
  1608.                 } else {
  1609.                     //echo "OK";
  1610.                     //exit;
  1611.                     $recherche_contruit "";
  1612.                     $recherche_tab      explode(" "$recherche);
  1613.                     for ($r 0$r count($recherche_tab); $r++) {
  1614.                         //$recherche_contruit .="+".$recherche_tab[$r]." ";
  1615.                         $recherche_tab[$r]  = str_replace("+""#+"$recherche_tab[$r]);
  1616.                         $recherche_contruit .= "+".$recherche_tab[$r]." ";
  1617.                     }
  1618.                     //echo "<div>SUPPL ".$whereSuppl."</div>";
  1619.                     $sql "
  1620.                 select
  1621.                 p.stock_reserve_fabrication,
  1622.                 CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle_construit,
  1623.                 p.article_remplacement_id,
  1624.                 p.virtuel,
  1625.                 p.libelle,
  1626.                 p.id,
  1627.                 p.prix_vente as prixVente,
  1628.                 p.prix_base as prixAchat,
  1629.                 p.poids,
  1630.                 p.stock,
  1631.                 p.stock_reserve,
  1632.                 p.reference,
  1633.                 p.pump,
  1634.                 p.cpump,
  1635.                 p.arret_gamme,
  1636.                 MATCH (".$match.") AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE) as ponderation";
  1637.                     $sql .= $select;
  1638.                     $sql .= " FROM ".$selectTemp;
  1639.                     $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 ";
  1640.                     $sql .= $jointureFromInventaire
  1641.                             ." WHERE ";
  1642.                     if ($jointureWhere != "") {
  1643.                         $sql .= $jointureWhere." and ";
  1644.                     }
  1645.                     $sql .= " p.temporaire is null
  1646.                 and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1647.                 and
  1648.                 (MATCH (".$match.")
  1649.                 AGAINST ('".$recherche_contruit."' IN BOOLEAN MODE)";
  1650.                     $sql .= "";
  1651.                     $sql .= $wherePrix.$whereStock;
  1652.                     $sql .= ")";
  1653.                     $sql .= $jointureWhereInventaire;
  1654.                     $sql .= " GROUP BY p.id
  1655.                             order by ".$order.", ponderation desc
  1656.                 LIMIT 0,100
  1657.                 ";
  1658.                     //echo $sql;
  1659.                 }
  1660.             }
  1661.         } else {
  1662.             //$order .= "p.reference asc";
  1663.             $sql "
  1664.             select
  1665.             p.arret_gamme,
  1666.             p.virtuel,
  1667.             p.article_remplacement_id,
  1668.             p.libelle,
  1669.             p.id,
  1670.             p.prix_vente as prixVente,
  1671.             p.prix_base as prixAchat,
  1672.             p.poids,
  1673.             p.stock,
  1674.             p.stock_reserve_fabrication,
  1675.             p.stock_reserve,
  1676.             p.pump,
  1677.             p.cpump,
  1678.             p.reference";
  1679.             $sql .= $select;
  1680.             $sql .= " FROM ".$selectTemp;
  1681.             $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 ";
  1682.             $sql .= $jointureFromInventaire
  1683.                     ." WHERE ";
  1684.             if ($jointureWhere != "") {
  1685.                 $sql .= $jointureWhere." and ";
  1686.             }
  1687.             $sql .= " p.temporaire is null
  1688.             and
  1689.             (p.archive = 0 or p.archive is null)
  1690.                         ".$whereSuppl.$whereStock.$jointureWhereInventaire."
  1691.                             GROUP BY p.id
  1692.             order by ".$order."
  1693.             LIMIT 0,100
  1694.             ";
  1695.         }
  1696.         //where p.libelle like '%".$recherche."%' or p.reference like '%".$recherche."%'
  1697.         //echo $sql;
  1698.         $em   $this->getEntityManager();
  1699.         $stmt $em->getConnection()->prepare($sql);
  1700.         $resultats $stmt->executeQuery()->fetchAllAssociative();
  1701.         //$resultats = array();
  1702.         //echo $sql;
  1703.         //exit;
  1704.         //echo "<div>------(".$mouvemente.")</div>";
  1705.         //Si la recherche dans les articles mouvementés ne donne rien, on effectue la requete sur les articles non mouvementés
  1706.         if (count($resultats) == && $mouvemente == "1") {
  1707.             //$mouvemente =0;
  1708.             /*if(!$avecEtoile) {
  1709.                 $recherche = str_replace("*","",$recherche);
  1710.             }
  1711.             */
  1712.             //echo "<div>ototo ".$recherche."</div>";
  1713.             //echo "--".$mouvemente;
  1714.             //exit;
  1715.             $resultats $this->getRechercheArticleSelect3(
  1716.                 $recherche_tmp,
  1717.                 $produitInterne,
  1718.                 $fournisseur,
  1719.                 $articlesAExclure,
  1720.                 $stock,
  1721.                 $reference_search,
  1722.                 $libelle_search,
  1723.                 $prix_search,
  1724.                 $tri_reference,
  1725.                 $tri_libelle,
  1726.                 $tri_prix,
  1727.                 $tri_stock,
  1728.                 $categories,
  1729.                 "0",
  1730.                 $inventaire
  1731.             );
  1732.             //exit;
  1733.         }
  1734.         //exit;
  1735.         return $resultats;
  1736.     }
  1737.     public function findLibelleOrRefLike($query$articleId)
  1738.     {
  1739.         $tampon_recherche $query;
  1740.         $recherche        trim($query);
  1741.         $recherche        str_replace(" ""* "$recherche);
  1742.         $recherche        .= "*";
  1743.         $recherche        str_replace("-"""$recherche);
  1744.         $jointureFrom  "";
  1745.         $whereSuppl    "";
  1746.         $wherePrix     "";
  1747.         $whereStock    "";
  1748.         $jointureWhere "";
  1749.         $order         "";
  1750.         $sql "
  1751.             select
  1752.             CONCAT(CONCAT(p.reference, ' '), p.libelle) as libelle,
  1753.             p.id,
  1754.             p.virtuel,
  1755.             p.prix_vente as prixVente,
  1756.             p.prix_base as prixAchat,
  1757.             p.poids,
  1758.             p.stock,
  1759.             p.reference,
  1760.             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
  1761.             FROM article__article as p ".$jointureFrom."
  1762.             WHERE
  1763.             p.temporaire is null
  1764.                     and
  1765.             p.consigne = 0
  1766.             and (p.archive = 0 or p.archive is null) ".$whereSuppl."
  1767.             and
  1768.             (MATCH (
  1769.                                    p.reference,
  1770.                                    p.libelle,
  1771.                                    reference_appel_1,
  1772.                                    reference_appel_2,
  1773.                                    reference_appel_3,
  1774.                                    reference_appel_4,
  1775.                                    reference_appel_5,
  1776.                                    reference_appel_6,
  1777.                                    reference_appel_7,
  1778.                                    reference_appel_8,
  1779.                                    reference_appel_9,
  1780.                                    reference_appel_10,
  1781.                                    reference_appel_11
  1782.                                )
  1783.             AGAINST ('".$recherche."' IN BOOLEAN MODE)
  1784.             ".$wherePrix.$whereStock.$jointureWhere."
  1785.             )
  1786.             order by ".$order." ponderation desc
  1787.             LIMIT 0,100
  1788.             ";
  1789.         /*
  1790.         return $this->getEntityManager()
  1791.             ->createQuery(
  1792.                 "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) "
  1793.             )
  1794.                 ->setParameters(array('id' => $articleId))
  1795.             ->getResult();
  1796.         */
  1797.         $em   $this->getEntityManager();
  1798.         $stmt $em->getConnection()->prepare($sql);
  1799.         return $stmt->executeQuery()->fetchAllAssociative();
  1800.     }
  1801.     public function agetRechercheArticleSelect($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock ""$reference_search ""$libelle_search "",
  1802.                                                $prix_search ""$tri_reference ""$tri_prix ""$tri_stock ""
  1803.     ) {
  1804.         $where "";
  1805.         //$cond = "%".$recherche . "%";
  1806.         $cond $recherche."%";
  1807.         //$parameters = array('field' => $recherche,'archive'=>'0','cond' => $cond);
  1808.         $parameters = ['field' => $recherche'archive' => '0'];
  1809.         //if($recherche != "") $parameters = array('field' => $recherche,'archive'=>'0','cond' => $cond);
  1810.         if ($recherche != "") {
  1811.             $parameters = ['field' => $recherche'archive' => '0'];
  1812.         }
  1813.         $qb $this->getEntityManager()->createQueryBuilder();
  1814.         $qb
  1815.             ->select(
  1816.                 "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"
  1817.             )
  1818.             ->from(Article::class, 'p');
  1819.         //if($recherche != "") $where = '(MATCH (p.reference,p.libelle) AGAINST (:field) > 0 or p.prixVente like :cond) and ';
  1820.         if ($recherche != "") {
  1821.             $wherePrix "";
  1822.             if ($prix_search == 1) {
  1823.                 $parameters = ['field' => $recherche'archive' => '0''cond' => $cond];
  1824.                 $wherePrix  ' or p.prixVente like :cond ';
  1825.             }
  1826.             $where '(MATCH (
  1827.                                    p.reference,
  1828.                                    p.libelle,
  1829.                                    p.referenceAppel1,
  1830.                                    p.referenceAppel2,
  1831.                                    p.referenceAppel3,
  1832.                                    p.referenceAppel4,
  1833.                                    p.referenceAppel5,
  1834.                                    p.referenceAppel6,
  1835.                                    p.referenceAppel7,
  1836.                                    p.referenceAppel8,
  1837.                                    p.referenceAppel9,
  1838.                                    p.referenceAppel10
  1839.                                ) AGAINST (:field) > 0)'.$wherePrix.'and ';
  1840.         }
  1841.         $where .= 'p.temporaire is null and p.archive = :archive ';
  1842.         if ($fournisseur == true) {
  1843.             //ajout des tables conditions d'achat et fournisseur
  1844.             $select .= ', f.id as fournisseur';
  1845.             $query->join('p.conditionsAchat''ca');
  1846.             $query->innerJoin('ca.fournisseur''f''WITH''ca.fournisseur = f.id');
  1847.             $where                     .= ' and f.id = :fournisseur';
  1848.             $parameters['fournisseur'] = $fournisseur;
  1849.         }
  1850.         if ($produitInterne == true) {
  1851.             $where .= ' and p.produitInterne =1';
  1852.         }
  1853.         if (count($articlesAExclure) > 0) {
  1854.             $where .= " and p.id NOT IN ( '".implode($articlesAExclure"', '")."' )";// != :articleAExclure and p.id != :articleAExclure
  1855.         }
  1856.         if ($stock == 1) {
  1857.             $where .= " and p.stock > 0";
  1858.         }
  1859.         if ($where != "") {
  1860.             $qb->where($where)
  1861.                ->setParameters($parameters)
  1862.                ->setFirstResult(0)
  1863.                ->setMaxResults(1000)
  1864.                ->groupBy('p.id')//->orderBy('ponderation', 'desc')
  1865.             ;
  1866.         }
  1867.         if ($prix_search == 1) {
  1868.         } else {
  1869.             $qb->addOrderBy('ponderation''desc');
  1870.         }
  1871.         if ($tri_reference != "") {
  1872.         }
  1873.         if ($tri_prix != "") {
  1874.             $qb->addOrderBy('p.prixVente'$tri_prix);
  1875.         }
  1876.         if ($tri_stock != "") {
  1877.             $qb->addOrderBy('p.stock'$tri_stock);
  1878.         }
  1879.         $res $qb->getQuery()->getResult();
  1880.         //\Doctrine\Common\Util\Debug::dump($res);
  1881.         return $res;
  1882.     }
  1883.     public function ZgetRechercheArticleSelect($recherche$produitInterne null$fournisseur null$articlesAExclure null$stock "")
  1884.     {
  1885.         //echo "V";
  1886.         $cond   "%".$recherche."%";
  1887.         $query  $this->createQueryBuilder('p')
  1888.             //->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");
  1889.                        ->select("p.libelle, p.id,p.prixVente,p.poids,p.stock,p.reference");
  1890.         $select '';
  1891.         //$where = "MATCH (p.reference, p.libelle) AGAINST (:cond)";
  1892.         //$where = "MATCH_AGAINST(p.reference, p.libelle) > 0";
  1893.         $where      '(p.libelle LIKE :cond OR p.reference LIKE :cond OR p.prixVente LIKE :cond) and p.temporaire is null and p.archive = :archive';
  1894.         $parameters = ['cond' => $cond'archive' => '0'];
  1895.         if ($fournisseur == true) {
  1896.             //ajout des tables conditions d'achat et fournisseur
  1897.             $select .= ', f.id as fournisseur';
  1898.             $query->join('p.conditionsAchat''ca');
  1899.             $query->innerJoin('ca.fournisseur''f''WITH''ca.fournisseur = f.id');
  1900.             $where                     .= ' and f.id = :fournisseur';
  1901.             $parameters['fournisseur'] = $fournisseur;
  1902.         }
  1903.         if ($produitInterne == true) {
  1904.             $where .= ' and p.produitInterne =1';
  1905.         }
  1906.         if (count($articlesAExclure) > 0) {
  1907.             $where .= " and p.id NOT IN ( '".implode($articlesAExclure"', '")."' )";// != :articleAExclure and p.id != :articleAExclure
  1908.         }
  1909.         if ($stock == 1) {
  1910.             $where .= " and p.stock > 0";
  1911.         }
  1912.         //echo "AA ".$where;
  1913.         $query->where($where)
  1914.               ->setParameters($parameters)//array('cond' => $cond)
  1915.             //->orderBy('p.prenom', 'ASC');
  1916.               ->setFirstResult(0)
  1917.               ->setMaxResults(1000)
  1918.               ->orderBy('p.reference''ASC');
  1919.         $res $query->getQuery()->getResult();
  1920.         return $res;
  1921.     }
  1922.     public function getRechercheClient($recherche)
  1923.     {
  1924.         $cond  $recherche."%";
  1925.         $query $this->createQueryBuilder('p')
  1926.                       ->select("CONCAT(CONCAT(p.prenom, ' '), p.nom) as libelle, p.id,p.telephone")
  1927.                       ->where('p.prenom LIKE :cond or p.nom like :cond')
  1928.                       ->setParameters(['cond' => $cond])
  1929.                       ->orderBy('p.prenom''ASC');
  1930.         $res $query->getQuery()->getResult();
  1931.         return $res;
  1932.     }
  1933.     public function findByRefOrName($query)
  1934.     {
  1935.         return $this->getEntityManager()
  1936.                     ->createQuery(
  1937.                         "select a.id, a.reference, a.libelle, a.prixBase
  1938.                 FROM App\Entity\Articles\Article a
  1939.                 WHERE p.temporaire is null and p.archive = 0 and (a.reference LIKE '%".$query."%' OR a.libelle LIKE '%".$query."%' OR a.referenceFournisseur LIKE '%".$query."%)'
  1940.                 ORDER BY a.libelle ASC"
  1941.                     )
  1942.                     ->getResult();
  1943.     }
  1944.     public function genererCpumpArticle($id$date ''$quantiteTotal 0)
  1945.     {
  1946.         //$article = $this->getEntityManager()->getRepository(Article::class)->find($id);
  1947.         mail("thomas@noogaa.com""BB""B");
  1948.         $builder = new ProcessBuilder();
  1949.         $builder->setArguments(['php''../app/console''genererCpumpArticle'$id]);
  1950.         $builder->getProcess()->start();
  1951.         mail("thomas@noogaa.com""CC""C");
  1952.     }
  1953.     public function getPumpC($id$date ''$quantiteTotal 0)
  1954.     {
  1955.         return 0;
  1956.         $pump        "0";
  1957.         $repo_condha $this->getEntityManager()->getRepository(ConditionAchat::class);
  1958.         $article     $this->getEntityManager()->getRepository(Article::class)->find($id);
  1959.         if (is_object($article) && $article->getStock() > && $date != '') {
  1960.             $nbResultats floatval($article->getStock());
  1961.         } else {
  1962.             $nbResultats 30;
  1963.         }
  1964.         $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
  1965.         $where "WHERE a.id = :id and m.idImport is NULL";
  1966.         if ($date != '') {
  1967.             $where .= " and m.dateMvt <= '".$date->format("Y-m-d H:i:s")."'";
  1968.         }
  1969.         $nbResultats 2;
  1970.         $stocks $this->getEntityManager()
  1971.                        ->createQuery(
  1972.                            "select m.id,a.stock, m.quantite, m.prixAchat,m.cpump,r.action,m.stock as stockMvt,r.id as raison_id
  1973.                 FROM App\Entity\Articles\Article a
  1974.                 LEFT JOIN App\Entity\Articles\MouvementStock m with m.article = a.id
  1975.                 LEFT JOIN App\Entity\GestionComerciale\Commande c with m.commande = c.id
  1976.                 LEFT JOIN App\Entity\Articles\RaisonMouvementStock r with m.raisonMouvementStock = r.id
  1977.                 ".$where."
  1978.                 ORDER BY m.dateMvt DESC,m.id DESC"
  1979.                        )
  1980.                        ->setParameters(['id' => $id])
  1981.                        ->setMaxResults($nbResultats)
  1982.                        ->getResult();
  1983.         if (count($stocks) == "2") {
  1984.             //echo "<div>OKOKO</div>";
  1985.             $dernierMouvement $stocks[0];
  1986.             $mouvement        $stocks[1];
  1987.             if ($dernierMouvement["action"] == "augmenter" and ($dernierMouvement["raison_id"] == "3" or $dernierMouvement["raison_id"] == "15")) {
  1988.                 if ($dernierMouvement["stockMvt"] == 0) {
  1989.                     return 0;
  1990.                 } else {
  1991.                     if ($mouvement["cpump"] > 0) {
  1992.                         //echo "AZERTY";
  1993.                         $pump = (($mouvement["stockMvt"] * $mouvement["cpump"]) + ($dernierMouvement["quantite"] * $dernierMouvement["prixAchat"])) / $dernierMouvement["stockMvt"];
  1994.                         //return $pump;
  1995.                     } else {
  1996.                         //echo "QWERTY";
  1997.                         //print_r($dernierMouvement);
  1998.                         $pump = ($dernierMouvement["quantite"] * $dernierMouvement["prixAchat"]) / $dernierMouvement["quantite"];
  1999.                         //return $pump;
  2000.                     }
  2001.                 }
  2002.             } else {
  2003.                 $pump $mouvement["cpump"];
  2004.             }
  2005.         } elseif (count($stocks) == "1") {
  2006.             $dernierMouvement $stocks[0];
  2007.             if ($dernierMouvement["action"] == "augmenter" and ($dernierMouvement["raison_id"] == "3" or $dernierMouvement["raison_id"] == "15")) {
  2008.                 if ($dernierMouvement["stockMvt"] == 0) {
  2009.                     return 0;
  2010.                 } else {
  2011.                     $pump = ($dernierMouvement["quantite"] * $dernierMouvement["prixAchat"]) / $dernierMouvement["stockMvt"];
  2012.                 }
  2013.             } //else if($dernierMouvement["action"] == "diminuer") {
  2014.             else {
  2015.                 $pump 0;
  2016.             }
  2017.         } else {
  2018.             $pump 0;
  2019.         }
  2020.         if ($pump == "0" or $pump == "") {
  2021.             $condHa $repo_condha->findOneBy(["article" => $article->getId(), "defaut" => 1]);
  2022.             if (is_object($condHa)) {
  2023.                 //echo "<div>CONDHA OK</div>";
  2024.                 $prix $condHa->getPrixAchatNet();
  2025.                 if ($condHa->getCoefficientConditionnement() != 0) {
  2026.                     $prix $prix $condHa->getCoefficientConditionnement();
  2027.                 }
  2028.                 $tauxChange  1;
  2029.                 $fournisseur $condHa->getFournisseur();
  2030.                 if (is_object($fournisseur)) {
  2031.                     $devise $fournisseur->getDevise();
  2032.                 }
  2033.                 if (is_object($devise) && $devise->getTauxChange() != 0) {
  2034.                     $tauxChange $devise->getTauxChange();
  2035.                 }
  2036.                 $prix $prix $tauxChange;
  2037.                 $pump $prix;
  2038.             } else {
  2039.                 $condHa $repo_condha->findOneBy(["article" => $article->getId()]);
  2040.                 if (is_object($condHa)) {
  2041.                     //echo "<div>CONDHA OK</div>";
  2042.                     $prix $condHa->getPrixAchatNet();
  2043.                     if ($condHa->getCoefficientConditionnement() != 0) {
  2044.                         $prix $prix $condHa->getCoefficientConditionnement();
  2045.                     }
  2046.                     $tauxChange  1;
  2047.                     $fournisseur $condHa->getFournisseur();
  2048.                     if (is_object($fournisseur)) {
  2049.                         $devise $fournisseur->getDevise();
  2050.                     }
  2051.                     if (is_object($devise) && $devise->getTauxChange() != 0) {
  2052.                         $tauxChange $devise->getTauxChange();
  2053.                     }
  2054.                     $prix $prix $tauxChange;
  2055.                     $pump $prix;
  2056.                 } else {
  2057.                     $pump 0;
  2058.                 }
  2059.             }
  2060.         }
  2061.         if ($pump == "") {
  2062.             return 0;
  2063.         } else {
  2064.             return $pump;
  2065.         }
  2066.         $pump         0;
  2067.         $totalTmp     0;
  2068.         $totalPrixTmp 0;
  2069.         //echo $quantiteTotal;
  2070.         if ($quantiteTotal <= 0) {
  2071.             if (is_object($article)) {
  2072.                 $quantiteTotal $article->getStock();
  2073.             }
  2074.         }
  2075.         if (count($stocks) > && $quantiteTotal 0) {
  2076.             //$pump = $stocks[0]->getPrixAchat();
  2077.             //$quantiteTotal = $stocks[0]['stock'];
  2078.             $quantiteTmp $quantiteTotal;
  2079.             foreach ($stocks as $stock)//reverse
  2080.             {
  2081.                 $quantite $stock['quantite'];
  2082.                 $prix     $stock['prixAchat'];
  2083.                 //var_dump($quantite.' -> '.$prix);
  2084.                 /*
  2085.                 if (($quantiteTmp-$quantite) >= 0){
  2086.                     $pump += $quantite * $prix;
  2087.                     $totalTmp += $quantite;
  2088.                 }else{
  2089.                     $pump += $quantiteTmp * $prix;
  2090.                     $totalTmp += $quantiteTmp;
  2091.                 }
  2092.                 $quantiteTmp -= $quantite;
  2093.                 if($quantiteTmp <= 0) break;
  2094.                 */
  2095.                 $totalPrixTmp += $quantite $prix;
  2096.                 $totalTmp     += $quantite;
  2097.             }
  2098.             if ($totalTmp 0) {
  2099.                 $pump $totalPrixTmp $totalTmp;
  2100.             }
  2101.         }
  2102.         //echo "PUMP ".$pump;
  2103.         //exit;
  2104.         return $pump;
  2105.     }
  2106.     public function getPumpR($id$date ''$quantiteTotal 0$output '')
  2107.     {
  2108.         $article $this->getEntityManager()->getRepository(Article::class)->find($id);
  2109.         /*
  2110.         if(is_object($article) && $article->getStock() > 0 && $date != '')
  2111.             $nbResultats = floatval($article->getStock());
  2112.         else
  2113.         */
  2114.         $nbResultats 100;
  2115.         $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
  2116.         if ($date != '') {
  2117.             $where .= " and m.dateMvt <= '".$date->format("Y-m-d H:i:s")."'";
  2118.         }
  2119.         $stocks   $this->getEntityManager()
  2120.                          ->createQuery(
  2121.                              "select a.stock, m.quantite, m.prixAchatUnitaireAvecFrais as prixAchat, m.prixAchat as prixAchat2
  2122.                 FROM App\Entity\Articles\Article a
  2123.                 LEFT JOIN App\Entity\Articles\MouvementStock m with m.article = a.id
  2124.                 LEFT JOIN App\Entity\GestionComerciale\Commande c with m.commande = c.id
  2125.                 LEFT JOIN App\Entity\Articles\RaisonMouvementStock r with m.raisonMouvementStock = r.id
  2126.                 ".$where."
  2127.                 ORDER BY m.date DESC"
  2128.                          )
  2129.                          ->setParameters(['id' => $id])
  2130.                          ->setMaxResults($nbResultats)
  2131.                          ->getResult();
  2132.         $pump     0;
  2133.         $totalTmp 0;
  2134.         //echo $quantiteTotal;
  2135.         if ($quantiteTotal <= 0) {
  2136.             if (is_object($article)) {
  2137.                 $quantiteTotal $article->getStock();
  2138.             }
  2139.         }
  2140.         if (count($stocks) > && $quantiteTotal 0) {
  2141.             //$pump = $stocks[0]->getPrixAchat();
  2142.             //$quantiteTotal = $stocks[0]['stock'];
  2143.             $quantiteTmp $quantiteTotal;
  2144.             foreach ($stocks as $stock)//reverse
  2145.             {
  2146.                 $quantite $stock['quantite'];
  2147.                 $prix     $stock['prixAchat'];
  2148.                 if (is_null($prix)) {
  2149.                     $prix $stock['prixAchat2'];
  2150.                 }
  2151.                 if (is_object($output)) {
  2152.                     $output->writeln('qte : '.$quantite.' / prix achat : '.$prix);
  2153.                 }
  2154.                 //var_dump($quantite.' -> '.$prix);
  2155.                 /*
  2156.                                     if (($quantiteTmp-$quantite) >= 0){
  2157.                                         $pump += $quantite * $prix;
  2158.                                         $totalTmp += $quantite;
  2159.                                     }else{
  2160.                                         $pump += $quantiteTmp * $prix;
  2161.                                         $totalTmp += $quantiteTmp;
  2162.                                     }
  2163.                                     */
  2164.                 if ($quantite >= $quantiteTmp) {
  2165.                     $pump     += $quantiteTmp $prix;
  2166.                     $totalTmp += $quantiteTmp;
  2167.                 } else {
  2168.                     $pump     += $quantite $prix;
  2169.                     $totalTmp += $quantite;
  2170.                 }
  2171.                 $quantiteTmp -= $quantite;
  2172.                 if ($quantiteTmp <= 0) {
  2173.                     break;
  2174.                 }
  2175.             }
  2176.             if ($totalTmp 0) {
  2177.                 $pump $pump $totalTmp;
  2178.             }
  2179.         }
  2180.         return $pump;
  2181.     }
  2182.     public function getPumpHorsFrais($id$date ''$quantiteTotal 0)
  2183.     {
  2184.         $article $this->getEntityManager()->getRepository(Article::class)->find($id);
  2185.         if (is_object($article) && $article->getStock() > && $date != '') {
  2186.             $nbResultats floatval($article->getStock());
  2187.         } else {
  2188.             $nbResultats 30;
  2189.         }
  2190.         $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
  2191.         if ($date != '') {
  2192.             $where .= " and m.dateMvt <= '".$date->format("Y-m-d H:i:s")."'";
  2193.         }
  2194.         $stocks   $this->getEntityManager()
  2195.                          ->createQuery(
  2196.                              "select a.stock, m.quantite, m.prixAchat
  2197.                 FROM App\Entity\Articles\Article a
  2198.                 LEFT JOIN App\Entity\Articles\MouvementStock m with m.article = a.id
  2199.                 LEFT JOIN App\Entity\GestionComerciale\Commande c with m.commande = c.id
  2200.                 LEFT JOIN App\Entity\Articles\RaisonMouvementStock r with m.raisonMouvementStock = r.id
  2201.                 ".$where."
  2202.                 ORDER BY m.date DESC"
  2203.                          )
  2204.                          ->setParameters(['id' => $id])
  2205.                          ->setMaxResults($nbResultats)
  2206.                          ->getResult();
  2207.         $pump     0;
  2208.         $totalTmp 0;
  2209.         //echo $quantiteTotal;
  2210.         if ($quantiteTotal <= 0) {
  2211.             if (is_object($article)) {
  2212.                 $quantiteTotal $article->getStock();
  2213.             }
  2214.         }
  2215.         if (count($stocks) > && $quantiteTotal 0) {
  2216.             //$pump = $stocks[0]->getPrixAchat();
  2217.             //$quantiteTotal = $stocks[0]['stock'];
  2218.             $quantiteTmp $quantiteTotal;
  2219.             foreach ($stocks as $stock)//reverse
  2220.             {
  2221.                 $quantite $stock['quantite'];
  2222.                 $prix     $stock['prixAchat'];
  2223.                 //var_dump($quantite.' -> '.$prix);
  2224.                 /*
  2225.                 if (($quantiteTmp-$quantite) >= 0){
  2226.                     $pump += $quantite * $prix;
  2227.                     $totalTmp += $quantite;
  2228.                 }else{
  2229.                     $pump += $quantiteTmp * $prix;
  2230.                     $totalTmp += $quantiteTmp;
  2231.                 }
  2232.                 */
  2233.                 if ($quantite >= $quantiteTmp) {
  2234.                     $pump     += $quantiteTmp $prix;
  2235.                     $totalTmp += $quantiteTmp;
  2236.                 } else {
  2237.                     $pump     += $quantite $prix;
  2238.                     $totalTmp += $quantite;
  2239.                 }
  2240.                 $quantiteTmp -= $quantite;
  2241.                 if ($quantiteTmp <= 0) {
  2242.                     break;
  2243.                 }
  2244.             }
  2245.             if ($totalTmp 0) {
  2246.                 $pump $pump $totalTmp;
  2247.             }
  2248.         }
  2249.         return $pump;
  2250.     }
  2251.     public function gethistoriqueVente($articleId)//ajouter filtre statut commande
  2252.     {
  2253.         $histo $this->getEntityManager()
  2254.                       ->createQuery(
  2255.                           "select c.reference, c.date, ac.quantite
  2256.                 FROM App\Entity\GestionComerciale\Commande c
  2257.                 JOIN App\Entity\GestionComerciale\ArticleCommande ac with ac.commande = c.id"
  2258.                 ." WHERE ac.article = :id and c.typeDocumentCommercial = ".TypeDocumentCommercial::COMMANDE."
  2259.                 ORDER BY c.date DESC"
  2260.                       )
  2261.                       ->setParameters(['id' => $articleId])
  2262.                       ->setMaxResults(5)
  2263.                       ->getResult();
  2264.         return $histo;
  2265.     }
  2266.     public function getNbArticlesVendus($articleId$periode)//ajouter filtre statut commande
  2267.     {
  2268.         $date = new DateTime();
  2269.         $date->sub(new DateInterval('P'.$periode.'D'));
  2270.         $nbArticlesVendus $this->getEntityManager()
  2271.                                  ->createQuery(
  2272.                                      "select SUM(ac.quantite) as quantite
  2273.                 FROM App\Entity\GestionComerciale\Commande c
  2274.                 JOIN App\Entity\GestionComerciale\ArticleCommande ac with ac.commande = c.id
  2275.                 WHERE ac.article = :id and c.date > :date and c.typeDocumentCommercial = ".TypeDocumentCommercial::COMMANDE."
  2276.                 ORDER BY c.date DESC"
  2277.                                  )
  2278.                                  ->setParameters(['id' => $articleId'date' => $date])
  2279.                                  ->getResult();
  2280.         return $nbArticlesVendus;
  2281.     }
  2282.     public function getNbArticlesVendusMois($articleId$m$y)//ajouter filtre statut commande
  2283.     {
  2284.         $dateDebut = new DateTime($y.'-'.$m.'-01 00:00:00');
  2285.         $date      = new DateTime($y.'-'.$m.'-01 00:00:00');
  2286.         //$date = new \DateTime('01/'.$m.'/'.$y);
  2287.         //$date = new \DateTime($m.'/01/'.$y);
  2288.         $periode $this->nbJoursMois($m$y);
  2289.         $dateFin $date->add(new DateInterval('P'.$periode.'D'));
  2290.         //$dateFin = $date->sub(new \DateInterval('P'.$periode.'D'));
  2291.         //return $dateDebut->format('d-m-Y').'/'.$dateFin->format('d-m-Y');
  2292.         $nbArticlesVendus $this->getEntityManager()
  2293.                                  ->createQuery(
  2294.                                      "select SUM(ac.quantite) as quantite
  2295.                 FROM App\Entity\GestionComerciale\Commande c
  2296.                 JOIN App\Entity\GestionComerciale\ArticleCommande ac with ac.commande = c.id
  2297.                 WHERE ac.article = :id and c.date >= :dateDebut and c.date < :dateFin and c.typeDocumentCommercial = ".TypeDocumentCommercial::COMMANDE."
  2298.                 and c.statutCommande != :statutCommande
  2299.                 "
  2300.                                  )
  2301.                                  ->setParameters(['id' => $articleId'dateDebut' => $dateDebut'dateFin' => $dateFin'statutCommande' => '7'])//
  2302.                                  ->getResult();
  2303.         return $nbArticlesVendus;
  2304.     }
  2305.     public function getArticlesAcommander()
  2306.     {
  2307.         $query $this->createQueryBuilder('p')
  2308.                       ->select("p.libelle, p.id, p.stock, p.seuilMiniCommandeFournisseur, p.seuilMaxiCommandeFournisseur")
  2309.             //->innerJoin('p.fournisseur', 'f', 'WITH', 'p.fournisseur = f.id')
  2310.             //->join("App\Entity\Fournisseurs\Four f")
  2311.                       ->where('p.stock < p.seuilMiniCommandeFournisseur and p.archive = 0')
  2312.             //->setParameters(array('cond' => $cond))
  2313.             //->orderBy('p.prenom', 'ASC');
  2314.             //->setFirstResult(0)
  2315.             //->setMaxResults(5)
  2316.             //->orderBy('p.id', 'DESC')
  2317.         ;
  2318.         $res $query->getQuery()->getResult();
  2319.         return $res;
  2320.     }
  2321.     public function getArticlesAcommanderGroupByFournisseur()
  2322.     {
  2323.         $query $this->createQueryBuilder('p')
  2324.                       ->select(
  2325.                           "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"
  2326.                       )
  2327.             //->leftJoin('p.fournisseurs', 'f')//, 'WITH', 'p.fournisseur = f.id'
  2328.                       ->leftJoin('p.mouvementStock''ms')
  2329.                       ->join('ms.fournisseur''f')
  2330.                       ->leftJoin(ArticleCommande::class, 'ac''with''ac.article = p.id')
  2331.                       ->leftJoin('ac.commandeFournisseur''c''with''c.statutCommande IN (1,2)')
  2332.             //->join("App\Entity\Fournisseurs\Four f")
  2333.                       ->where('p.archive = :archive')
  2334.                       ->setParameters(['archive' => "0"])
  2335.                       ->groupBy('f.id, p.id')
  2336.                       ->having('stockVirtuel < p.seuilMiniCommandeFournisseur')//p.stock + ral fournisseur < p.seuilMiniCommandeFournisseur
  2337.         ;
  2338.         $res $query->getQuery()->getResult();
  2339.         return $res;
  2340.     }
  2341.     public function getNbArticlesVendusParMois($articleId$nbMois)//ajouter filtre statut commande
  2342.     {
  2343.         $date  = new DateTime();
  2344.         $mois  = (int)$date->format('m');
  2345.         $annee = (int)$date->format('Y');
  2346.         //$date->sub(new \DateInterval('P'.$periode.'D'));
  2347.         $moisTmp $mois;
  2348.         for ($i 1$i <= $nbMois$i++) {
  2349.             if ($moisTmp 10) {
  2350.                 $id $annee.'-0'.$moisTmp;
  2351.             } else {
  2352.                 $id $annee.'-'.$moisTmp;
  2353.             }
  2354.             $nbArticlesVendusMois $this->getNbArticlesVendusMois($articleId$moisTmp$annee);
  2355.             if ($nbArticlesVendusMois[0]['quantite'] != '') {
  2356.                 $retour[$id] = $nbArticlesVendusMois[0]['quantite'];
  2357.             } else {
  2358.                 $retour[$id] = 0;
  2359.             }
  2360.             //$retour[$nbArticlesVendusMois] = 10;
  2361.             if ($moisTmp 0) {
  2362.                 $moisTmp -= 1;
  2363.             } else {
  2364.                 $moisTmp 12;
  2365.                 $annee   -= 1;
  2366.             }
  2367.         }
  2368.         return array_reverse($retour);
  2369.     }
  2370.     public function getQuantitePrixMoyenVendusMois($articleId$m$y)//ajouter filtre statut commande
  2371.     {
  2372.         $dateDebut = new DateTime($y.'-'.$m.'-01 00:00:00');
  2373.         $dateFin   = new DateTime($y.'-'.$m.'-01 00:00:00');
  2374.         //$date = new \DateTime('01/'.$m.'/'.$y);
  2375.         //$date = new \DateTime($m.'/01/'.$y);
  2376.         $periode $this->nbJoursMois($m$y);
  2377.         $dateFin->add(new DateInterval('P'.$periode.'D'));
  2378.         //$dateFin = $date->sub(new \DateInterval('P'.$periode.'D'));
  2379.         //return $dateDebut->format('d-m-Y').'/'.$dateFin->format('d-m-Y');
  2380.         /*
  2381.          * $nbArticlesVendus = $this->getEntityManager()
  2382.             ->createQuery(
  2383.                 "select SUM(ac.quantite) as quantite, SUM(ac.totalHt) as totalHt
  2384.                 FROM App\Entity\GestionComerciale\Commande c
  2385.                 JOIN App\Entity\GestionComerciale\ArticleCommande ac with ac.commande = c.id
  2386.                 WHERE ac.article = :id and c.date >= :dateDebut and c.date < :dateFin and c.typeDocumentCommercial = ".TypeDocumentCommercial::COMMANDE."
  2387.                 "
  2388.             )
  2389.          */
  2390.         $nbArticlesVendus $this->getEntityManager()
  2391.                                  ->createQuery(
  2392.                                      "select SUM(ac.quantite) as quantite, SUM(ac.totalHt) as totalHt
  2393.                 FROM App\Entity\GestionComerciale\ArticleCommande ac
  2394.                 JOIN App\Entity\GestionComerciale\Commande c with ac.commande = c.id
  2395.                 WHERE ac.article = :id and c.date >= :dateDebut and c.date < :dateFin and c.statutCommande = 22
  2396.                 "
  2397.                                  )
  2398.                                  ->setParameters(['id' => $articleId'dateDebut' => $dateDebut'dateFin' => $dateFin])//
  2399.                                  ->getResult();
  2400.         return $nbArticlesVendus;
  2401.     }
  2402.     public function getPrixMoyenAchatMois($articleId$m$y)
  2403.     {
  2404.         $dateDebut = new DateTime($y.'-'.$m.'-01 00:00:00');
  2405.         $date      = new DateTime($y.'-'.$m.'-01 00:00:00');
  2406.         //$date = new \DateTime('01/'.$m.'/'.$y);
  2407.         //$date = new \DateTime($m.'/01/'.$y);
  2408.         $periode $this->nbJoursMois($m$y);
  2409.         $dateFin $date->add(new DateInterval('P'.$periode.'D'));
  2410.         //$dateFin = $date->sub(new \DateInterval('P'.$periode.'D'));
  2411.         //return $dateDebut->format('d-m-Y').'/'.$dateFin->format('d-m-Y');
  2412.         $prixMoyenAchatResult $this->getEntityManager()
  2413.                                      ->createQuery(
  2414.                                          "select COUNT(ms.id) as total, SUM(ms.prixAchat) as prix
  2415.                 FROM App\Entity\Articles\MouvementStock ms
  2416.                 WHERE ms.article = :id and ms.dateMvt >= :dateDebut and ms.dateMvt < :dateFin and ms.raisonMouvementStock = 3
  2417.                 "
  2418.                                      )
  2419.                                      ->setParameters(['id' => $articleId'dateDebut' => $dateDebut'dateFin' => $dateFin])//
  2420.                                      ->getResult();
  2421.         $prixMoyenAchat       0;
  2422.         if ($prixMoyenAchatResult[0]['total'] != 0) {
  2423.             $prixMoyenAchat $prixMoyenAchatResult[0]['prix'] / $prixMoyenAchatResult[0]['total'];
  2424.         }
  2425.         return $prixMoyenAchat;
  2426.     }
  2427.     public function getPrixAchatVenteParMois($articleId$nbMois)//ajouter filtre statut commande
  2428.     {
  2429.         $date  = new DateTime();
  2430.         $mois  = (int)$date->format('m');
  2431.         $annee = (int)$date->format('Y');
  2432.         //$date->sub(new \DateInterval('P'.$periode.'D'));
  2433.         $moisTmp $mois;
  2434.         for ($i 1$i <= $nbMois$i++) {
  2435.             if ($moisTmp 10) {
  2436.                 $id $annee.'-0'.$moisTmp;
  2437.             } else {
  2438.                 $id $annee.'-'.$moisTmp;
  2439.             }
  2440.             $quantitePrixVenteMois $this->getQuantitePrixMoyenVendusMois($articleId$moisTmp$annee);
  2441.             if ($quantitePrixVenteMois[0]['totalHt'] != '' && $quantitePrixVenteMois[0]['quantite'] != '' && $quantitePrixVenteMois[0]['quantite'] != 0) {
  2442.                 $retour[$id]['vente']['prix'] = $quantitePrixVenteMois[0]['totalHt'] / $quantitePrixVenteMois[0]['quantite'];
  2443.             } else {
  2444.                 $retour[$id]['vente']['prix'] = 0;
  2445.             }
  2446.             $retour[$id]['achat']['prix'] = $this->getPrixMoyenAchatMois($articleId$moisTmp$annee);
  2447.             //$retour[$nbArticlesVendusMois] = 10;
  2448.             if ($moisTmp 0) {
  2449.                 $moisTmp -= 1;
  2450.             } else {
  2451.                 $moisTmp 12;
  2452.                 $annee   -= 1;
  2453.             }
  2454.         }
  2455.         return array_reverse($retour);
  2456.     }
  2457.     private function nbJoursMois($m$y)
  2458.     {
  2459.         $mois mktime(000$m1$y);
  2460.         //setlocale('LC_ALL', 'fr_FR');
  2461.         return date("t"$mois);
  2462.     }
  2463.     public function findLibelleOrRefLikeInConsigne($query$articleId)
  2464.     {
  2465.         return $this->getEntityManager()
  2466.                     ->createQuery(
  2467.                         "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"
  2468.                     )
  2469.                     ->getResult();
  2470.     }
  2471.     public function getArticlesInternesAFabriquer()
  2472.     {
  2473.         $query $this->createQueryBuilder('p')
  2474.                       ->select("p.id, p.reference, p.description, (p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) as quantiteAFabriquer")
  2475.                       ->leftJoin('p.articlesComposants''ac')
  2476.                       ->where(
  2477.                           '(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'
  2478.                       );
  2479.         $res   $query->getQuery()->getResult();
  2480.         return $res;
  2481.     }
  2482.     public function getArticlesAfabriquerSeuilMini()
  2483.     {
  2484.         $query $this->createQueryBuilder('p')
  2485.             //->select("p.id, p.reference, p.description, (p.stock-COALESCE(p.stockReserveFabrication,0)+COALESCE(p.ralFournisseur,0)) as quantiteAFabriquer")
  2486.             //->leftJoin('p.articlesComposants', 'ac')
  2487.                       ->where(
  2488.                 '(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)'
  2489.             );
  2490.         $res   $query->getQuery()->getResult();
  2491.         return $res;
  2492.     }
  2493.     public function getQuantiteAFabriquer($article)
  2494.     {
  2495.         $query $this->createQueryBuilder('p')
  2496.                       ->select("(p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) as quantiteAFabriquer")
  2497.                       ->where('p.id = :id')
  2498.                       ->setParameters(['id' => $article->getId()]);
  2499.         $res   $query->getQuery()->getResult();
  2500.         if ($article->getVirtuel() == 1//Pas de stock
  2501.         {
  2502.             $seuilMini 0;
  2503.         } else {
  2504.             $seuilMini $article->getSeuilMiniCommandeFournisseur();
  2505.         }
  2506.         $quantiteMini $seuilMini $res[0]['quantiteAFabriquer'];
  2507.         $repo_condha $this->getEntityManager()->getRepository(ConditionAchat::class);
  2508.         $condHa $repo_condha->findOneBy(["defaut" => "1""fournisseur" => 132"article" => $article]);
  2509.         if (is_object($condHa)) {
  2510.             $qteCondHa $condHa->getQuantite();
  2511.             if ($qteCondHa $quantiteMini) {
  2512.                 $quantiteMini $qteCondHa;
  2513.             }
  2514.         }
  2515.         return $quantiteMini;
  2516.     }
  2517.     public function getClientsEnRal(Article $article)
  2518.     {
  2519.         $typeDocument = [TypeDocumentCommercial::COMMANDETypeDocumentCommercial::ORDREREPARATION];
  2520.         $query        $this->createQueryBuilder('p')
  2521.                              ->select("DISTINCT(cl.id) as id")
  2522.                              ->leftJoin(ArticleCommande::class, 'ac''with''ac.article = p.id')
  2523.                              ->join('ac.commande''c')
  2524.                              //->leftJoin(TypeDocumentCommercial::class, 'td', 'with', 'td.id = c.typeDocumentCommercial')
  2525.                              ->leftJoin(StatutCommande::class, 'sc''with''sc.id = c.statutCommande')
  2526.                              ->join('c.client''cl')
  2527.                              ->where('ac.ralClient > 0 and p.id = :id and c.typeDocumentCommercial IN (:typeDocument) and sc.ordre != 0')
  2528.                              ->setParameters(['id' => $article->getId(), 'typeDocument' => $typeDocument]);
  2529.         $res          $query->getQuery()->getResult();
  2530.         return $res;
  2531.     }
  2532.     public function getBpPourCommandeFournisseur($articles$commentaire 1209715)
  2533.     {
  2534.         $query $this->createQueryBuilder('p')
  2535.                       ->select("DISTINCT(c.id) as id, c.reference")
  2536.                       ->leftJoin(ArticleCommande::class, 'ac''with''ac.article = p.id')
  2537.                       ->join('ac.commande''c')
  2538.                       ->leftJoin(TypeDocumentCommercial::class, 'td''with''td.id = c.typeDocumentCommercial')
  2539.             //->join('c.client', 'cl')
  2540.                       ->where('ac.ralClient > 0 and p.id IN (:ids) and td.id IN (:typeDocument) and c.visible=1 and p.id != :com ')
  2541.                       ->setParameters(['ids' => $articles'typeDocument' => [4], 'com' => $commentaire]);
  2542.         $res   $query->getQuery()->getResult();
  2543.         //print_r($res);
  2544.         return $res;
  2545.         //return array(0=>array('id'=>1));
  2546.     }
  2547.     public function getCommandes($articles)
  2548.     {
  2549.         $query $this->createQueryBuilder('p')
  2550.                       ->select("DISTINCT(c.id) as id, c.reference")
  2551.                       ->leftJoin(ArticleCommande::class, 'ac''with''ac.article = p.id')
  2552.                       ->join('ac.commande''c')
  2553.                       //->leftJoin(TypeDocumentCommercial::class, 'td', 'with', 'td.id = c.typeDocumentCommercial')
  2554.                       ->join('c.client''cl')
  2555.                       ->where('ac.ralClient > 0 and p.id IN (:ids) and c.typeDocumentCommercial IN (:typeDocument)')
  2556.                       ->setParameters(['ids' => $articles'typeDocument' => [23]]);
  2557.         $res   $query->getQuery()->getResult();
  2558.         return $res;
  2559.         //return array(0=>array('id'=>1));
  2560.     }
  2561.     public function updateACommander()//ajouter filtre statut commande
  2562.     {
  2563.         $query $this->createQueryBuilder('p')
  2564.                       ->update(Article::class, 'p')
  2565.                       ->set('p.aCommander''0')
  2566.                       ->where('p.aCommander = 1 ')
  2567.                       ->getQuery()
  2568.                       ->execute();
  2569.         $query $this->createQueryBuilder('p')
  2570.                       ->update(Article::class, 'p')
  2571.                       ->set('p.aCommander''1')
  2572.                       ->join('p.conditionsAchat''cond')
  2573.             //->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')
  2574.                       ->where(
  2575.                 '
  2576.                             (p.virtuel != 1 or p.virtuel IS NULL)
  2577.                             and (p.consigne != 1 or p.consigne IS NULL)
  2578.                             and (p.nonStocke != 1 or p.nonStocke IS NULL)
  2579.                             and (p.nonCommandable != 1 or p.nonCommandable IS NULL)
  2580.                             and (p.arretGamme != 1 or p.arretGamme IS NULL)
  2581.                             and (p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) < COALESCE(p.seuilMiniCommandeFournisseur,0)
  2582.                             '
  2583.             //  nonStocke nonCommandable
  2584.             //and (p.produitInterne != 1 or p.produitInterne IS NULL)
  2585.             )
  2586.                       ->getQuery()
  2587.                       ->execute();
  2588.     }
  2589.     public function updateASuggerer()
  2590.     {
  2591.         $query $this->createQueryBuilder('p')
  2592.                       ->update(Article::class, 'p')
  2593.                       ->set('p.aSuggerer''0')
  2594.                       ->where('p.aSuggerer = 1 ')
  2595.                       ->getQuery()
  2596.                       ->execute();
  2597.         $query $this->createQueryBuilder('p')
  2598.                       ->update(Article::class, 'p')
  2599.                       ->set('p.aSuggerer''1')
  2600.                       ->join('p.conditionsAchat''cond')
  2601.             //->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')
  2602.                       ->where(
  2603.                 '
  2604.                             (p.virtuel != 1 or p.virtuel IS NULL)
  2605.                             and (p.consigne != 1 or p.consigne IS NULL)
  2606.                             and (p.nonStocke != 1 or p.nonStocke IS NULL)
  2607.                             and (p.nonCommandable != 1 or p.nonCommandable IS NULL)
  2608.                             and (p.arretGamme != 1 or p.arretGamme IS NULL)
  2609.                             and (p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) < (COALESCE(p.seuilMiniCommandeFournisseur,0) * 1.05)
  2610.                             and (p.stock-COALESCE(p.stockReserve,0)+COALESCE(p.ralFournisseur,0)) > COALESCE(p.seuilMiniCommandeFournisseur,0)
  2611.                             '
  2612.             //  nonStocke nonCommandable
  2613.             //and (p.produitInterne != 1 or p.produitInterne IS NULL)
  2614.             )
  2615.                       ->getQuery()
  2616.                       ->execute();
  2617.     }
  2618.     public function historiquePrix($article)
  2619.     {
  2620.         $sql "
  2621.             select
  2622.             p.date_maj,
  2623.             p.libelle,
  2624.             p.reference,
  2625.             p.utilisateur_id,
  2626.             p.prix_base,
  2627.             p.prix_vente,
  2628.             p.coefficient_px_base_px_vente,
  2629.             p.marge,
  2630.             p.taux_marge,
  2631.             p.pump,
  2632.             p.cpump,
  2633.             p.marge_reelle,
  2634.             u.prenom as equipier_prenom,
  2635.             u.nom as equipier_nom
  2636.             FROM article__article_audit as p
  2637.             LEFT JOIN utilisateur__utilisateurs as u on u.id = p.utilisateur_id
  2638.             WHERE
  2639.             p.id = ".$article->getId()."
  2640.             and p.date_maj_prix is NOT NULL
  2641.             GROUP BY p.date_maj_prix
  2642.             ORDER BY p.rev DESC
  2643.            ";
  2644.         //echo $sql;
  2645.         $em   $this->getEntityManager();
  2646.         $stmt $em->getConnection()->prepare($sql);
  2647.         $stmt->execute();
  2648.         $prix $stmt->fetchAll();
  2649.         return $prix;
  2650.         //print_r($prix);
  2651.     }
  2652.     public function setMiniMaxi($ids$donnees)
  2653.     {
  2654.         //print_r($donnees);
  2655.         if ( ! array_key_exists('stockAuto'$donnees)) {
  2656.             $donnees['stockAuto'] = 0;
  2657.         }
  2658.         $chaine "";
  2659.         for ($i 0$i count($ids); $i++) {
  2660.             $chaine .= $ids[$i].",";
  2661.         }
  2662.         $chaine .= ",";
  2663.         $chaine str_replace(",,"""$chaine);
  2664.         $sql  " UPDATE
  2665.                  article__article
  2666.                  SET
  2667.                  seuil_commande_f_mini = ".floatval($donnees['mini']).",
  2668.                  seuil_commande_f_maxi = ".floatval($donnees['maxi']).",
  2669.                  stock_auto = ".$donnees['stockAuto']."
  2670.                  WHERE
  2671.                  id IN (".$chaine.")
  2672.                 ";
  2673.         $em   $this->getEntityManager();
  2674.         $stmt $em->getConnection()->prepare($sql);
  2675.         $stmt->execute();
  2676.     }
  2677.     public function valorisation($parametres)
  2678.     {
  2679.         $where "";
  2680.         if (array_key_exists('date'$parametres) && $parametres['date'] != "") {
  2681.             //$dateDebut = new \Datetime($param['param']['date']);
  2682.             $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date']);
  2683.             $dateDebut->setTime(235959);
  2684.         } else {
  2685.             $dateDebut = new DateTime();
  2686.             $dateDebut->setTime(235959);
  2687.         }
  2688.         if (array_key_exists('article'$parametres) && $parametres['article'] != "") {
  2689.             if ($where != "") {
  2690.                 $where .= " and ";
  2691.             }
  2692.             $where .= "p.reference like '".$parametres['article']."%'";
  2693.         }
  2694.         if (array_key_exists('marque'$parametres) && $parametres['marque'] != "") {
  2695.             if ($where != "") {
  2696.                 $where .= " and ";
  2697.             }
  2698.             $where .= "p.marque_id = ".$parametres['marque']."";
  2699.         }
  2700.         /*
  2701.         if(array_key_exists('inventaire', $parametres) && $parametres['inventaire'] != "") {
  2702.             if($where != "") $where .= " and ";
  2703.             $where .= "p.inventaire_id = ".$parametres['inventaire']."";
  2704. }
  2705.          *
  2706.          */
  2707.         if (array_key_exists('stock'$parametres) && $parametres['stock'] == "") {
  2708.             $parametres['stock'] = 0;
  2709.         }
  2710.         if (array_key_exists('stock'$parametres) && $parametres['stock'] == 0) {
  2711.             if ($where != "") {
  2712.                 $where .= " and ";
  2713.             }
  2714.             //$where .= "p.stock > 0";
  2715.             $where .= "article__mouvement_stock.stock > 0";
  2716.         }
  2717.         $orderBy "";
  2718.         if (array_key_exists('tri'$parametres) && $parametres['tri'] != "") {
  2719.             $orderBy ' ORDER BY p.'.$parametres['tri'].' ASC';
  2720.         }
  2721.         //echo $parametres['stock']." Date debut ".$dateDebut->format("d-m-Y H:i:s");
  2722.         /*
  2723.                $sql = "
  2724.                    select
  2725.                    p.id,
  2726.                    p.libelle,
  2727.                    p.reference,
  2728.                    p.stock,
  2729.                    p.pump,
  2730.                    s.stock as stock_date,
  2731.                    s.id as mv_id,
  2732.                    s.date_mouvement
  2733.                    FROM article__article as p
  2734.                    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
  2735.                    WHERE (s.date_mouvement <= '".$dateDebut->format("Y-m-d H:i:s")."' or s.date_mouvement is null)
  2736.                    ".$where."
  2737.                    GROUP BY p.id
  2738.                    ORDER BY s.date_mouvement DESC
  2739.                    LIMIT 0,5000
  2740.                   ";
  2741.                   */
  2742.         /*
  2743.        $sql = "
  2744.        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
  2745.        FROM    article__article as p
  2746.            LEFT JOIN (
  2747.            select MAX(id) as id, MAX(date_mouvement) AS date_mouvement,article_id
  2748.            FROM article__mouvement_stock
  2749.            WHERE date_mouvement <= '".$dateDebut->format("Y-m-d H:i:s")."'
  2750.            GROUP BY article_id
  2751.            ) AS lastMessage
  2752.            ON p.id = lastMessage.article_id
  2753.            LEFT JOIN article__mouvement_stock
  2754.            ON article__mouvement_stock.id = lastMessage.id";
  2755.                  *
  2756.                  */
  2757.         if (array_key_exists('inventaire'$parametres) && $parametres['inventaire'] != "" && == 0) {
  2758.             if ($where != "") {
  2759.                 $where .= " and ";
  2760.             }
  2761.             $where .= "ai.inventaire_id = ".$parametres['inventaire'];
  2762.             $sql "select p.id as article_id, p.libelle, p.reference, p.stock, p.pump,p.cpump,p.pump_hors_frais
  2763.         FROM inventaire__article_inventaire ai
  2764.                 LEFT JOIN article__article as p ON p.id = ai.article_id
  2765.                     ";
  2766.             if ($where != "") {
  2767.                 $sql .= " WHERE ".$where;
  2768.             }
  2769.         } else {
  2770.             $sql "
  2771.         select  p.id as article_id, p.libelle, p.reference, p.stock, p.pump,p.cpump,p.pump_hors_frais
  2772.         FROM    article__article as p
  2773.         LEFT JOIN article__mouvement_stock ON article__mouvement_stock.article_id = p.id    ";
  2774.             if ($where != "") {
  2775.                 $where .= " and ";
  2776.             }
  2777.             //$where .= "p.marque_id = ".$parametres['marque']."";
  2778.             $where .= "
  2779.                 (
  2780.                 (p.est_commentaire is null or p.est_commentaire = 0)
  2781.                 and
  2782.                 (p.virtuel is null or p.virtuel = 0)
  2783.                 and
  2784.                 (p.main_oeuvre is null or p.main_oeuvre = 0)
  2785.                 and
  2786.                 (p.non_stocke is null or p.non_stocke = 0)
  2787.                 and
  2788.                 (p.divers is null or p.divers = 0)
  2789.                 and
  2790.                 (p.divers_composes is null or p.divers_composes = 0)
  2791.                 )";
  2792.             if ($where != "") {
  2793.                 $sql .= " WHERE ".$where;
  2794.             }
  2795.             $sql .= " GROUP BY p.id";
  2796.             if ($orderBy != "") {
  2797.                 $sql .= $orderBy;
  2798.             } else {
  2799.                 $sql .= " ORDER BY article__mouvement_stock.date_mouvement DESC";
  2800.             }
  2801.         }
  2802.         //$sql .= " LIMIT 0,500";
  2803.         /*
  2804.         $sql = "
  2805.         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
  2806.         ";
  2807.         */
  2808.         $sql trim($sql);
  2809.         //var_dump($sql);die;
  2810.         //echo "<br/><br/>".$sql;
  2811.         $em   $this->getEntityManager();
  2812.         $stmt $em->getConnection()->prepare($sql);
  2813.         $articles $stmt->executeQuery()->fetchAllAssociative();
  2814.         return $articles;
  2815.     }
  2816.     public function getPrixAchat($idArticle$date)
  2817.     {
  2818.         //$idArticle = "2345678904567890";
  2819.         $date DateTime::createFromFormat('d/m/Y'$date);
  2820.         $date->setTime(235959);
  2821.         //echo "<div>".$idArticle." DATE : ".$date->format("d/m/Y H:i:s")."</div>";
  2822.         $sql "
  2823.         select  ca.prix_achat_net, d.taux_change as taux_change
  2824.         FROM    article__condition_achat_audit as ca
  2825.                 LEFT JOIN fournisseur__fournisseur f ON f.id = ca.fournisseur_id
  2826.                 LEFT JOIN article__devise d ON d.id = f.devise_id
  2827.         WHERE
  2828.         ca.date <= '".$date->format("Y-m-d H:i:s")."'
  2829.         and
  2830.         ca.statut = 1
  2831.         and
  2832.         ca.article_id = ".$idArticle."
  2833.         ORDER BY rev DESC
  2834.         LIMIT 0,1
  2835.         ";
  2836.         $em   $this->getEntityManager();
  2837.         $stmt $em->getConnection()->prepare($sql);
  2838.         $prix $stmt->executeQuery()->fetchAllAssociative();
  2839.         if (count($prix) > 0) {
  2840.             $prixAchat $prix[0]['prix_achat_net'];
  2841.             if ($prix[0]['taux_change'] != && $prix[0]['taux_change'] != '0' && $prix[0]['taux_change'] != '') {
  2842.                 $prixAchat $prixAchat floatval($prix[0]['taux_change']);
  2843.             }
  2844.             return $prixAchat;
  2845.         } else {
  2846.             $sql "
  2847.             select  ca.prix_achat_net, d.taux_change as taux_change
  2848.             FROM    article__condition_achat as ca
  2849.                         LEFT JOIN fournisseur__fournisseur f ON f.id = ca.fournisseur_id
  2850.                         LEFT JOIN article__devise d ON d.id = f.devise_id
  2851.             WHERE
  2852.             ca.statut = 1
  2853.             and
  2854.             ca.article_id = ".$idArticle."
  2855.             ORDER BY ca.id DESC
  2856.             LIMIT 0,1
  2857.             ";
  2858.             $em   $this->getEntityManager();
  2859.             $stmt $em->getConnection()->prepare($sql);
  2860.             $prix $stmt->executeQuery()->fetchAllAssociative();
  2861.             if (count($prix) > 0) {
  2862.                 $prixAchat $prix[0]['prix_achat_net'];
  2863.                 if ($prix[0]['taux_change'] != && $prix[0]['taux_change'] != '0' && $prix[0]['taux_change'] != '') {
  2864.                     $prixAchat $prixAchat floatval($prix[0]['taux_change']);
  2865.                 }
  2866.                 return $prixAchat;
  2867.             } else {
  2868.                 $sql  "
  2869.                 select  ca.prix_base
  2870.                 FROM   `article__article_audit` ca
  2871.                 WHERE
  2872.                 ca.date <= '".$date->format("Y-m-d H:i:s")."'
  2873.                 and
  2874.                 ca.id = ".$idArticle."
  2875.                 ORDER BY rev DESC
  2876.                 LIMIT 0,1
  2877.                 ";
  2878.                 $em   $this->getEntityManager();
  2879.                 $stmt $em->getConnection()->prepare($sql);
  2880.                 $prix $stmt->executeQuery()->fetchAllAssociative();
  2881.                 if (count($prix) > 0) {
  2882.                     return $prix[0]['prix_base'];
  2883.                 } else {
  2884.                     $sql  "
  2885.                     select  ca.prix_base
  2886.                     FROM   `article__article` ca
  2887.                     WHERE
  2888.                     ca.date <= '".$date->format("Y-m-d H:i:s")."'
  2889.                     and
  2890.                     ca.id = ".$idArticle."
  2891.                     ";
  2892.                     $em   $this->getEntityManager();
  2893.                     $stmt $em->getConnection()->prepare($sql);
  2894.                     $prix $stmt->executeQuery()->fetchAllAssociative();
  2895.                     if (count($prix) > 0) {
  2896.                         return $prix[0]['prix_base'];
  2897.                     } else {
  2898.                         return "0";
  2899.                     }
  2900.                 }
  2901.             }
  2902.         }
  2903.     }
  2904.     public function getDernierPrixAchat($idArticle$date)
  2905.     {
  2906.         //$idArticle = "2345678904567890";
  2907.         $date DateTime::createFromFormat('d/m/Y'$date);
  2908.         $date->setTime(235959);
  2909.         //echo "<div>".$idArticle." DATE : ".$date->format("d/m/Y H:i:s")."</div>";
  2910.         $sql "
  2911.                 select ac.prix_base
  2912.                 FROM `commerciale__article_commande` as ac
  2913.                 LEFT JOIN commerciale__commande_fournisseur as c on ac.commande_fournisseur_id = c.id
  2914.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  2915.                 WHERE
  2916.                 c.date <= '".$date->format("Y-m-d H:i:s")."'
  2917.                 and
  2918.                 c.statut_commande_id = 6
  2919.                 and
  2920.                 ac.article_id = ".$idArticle."
  2921.                 ORDER BY c.date DESC
  2922.                 LIMIT 0,1
  2923.             ";
  2924.         $em   $this->getEntityManager();
  2925.         $stmt $em->getConnection()->prepare($sql);
  2926.         $prix $stmt->executeQuery()->fetchAllAssociative();
  2927.         if (count($prix) > 0) {
  2928.             return $prix[0]['prix_base'];
  2929.         } else {
  2930.             return "0";
  2931.         }
  2932.     }
  2933.     public function getArticleEquivalent($idArticle)
  2934.     {
  2935.         $sql  "
  2936.             select
  2937.             a.id,
  2938.             a.libelle,
  2939.             a.reference,
  2940.             m.libelle as marque
  2941.             FROM article_article as aa
  2942.             LEFT JOIN article__article as a on a.id = aa.article_target
  2943.             LEFT JOIN article__marque as m on m.id = a.marque_id
  2944.             WHERE
  2945.             aa.article_source = ".$idArticle."
  2946.            ";
  2947.         $em   $this->getEntityManager();
  2948.         $stmt $em->getConnection()->prepare($sql);
  2949.         $equivalences $stmt->executeQuery()->fetchAllAssociative();
  2950.         return $equivalences;
  2951.     }
  2952.     public function estUneEquivalenceReciproque($article_equivalent$article_source)
  2953.     {
  2954.         $sql "
  2955.             select
  2956.             aa.article_source
  2957.             FROM article_article as aa
  2958.             WHERE
  2959.             aa.article_source = ".$article_source."
  2960.             and
  2961.             aa.article_target = ".$article_equivalent."
  2962.            ";
  2963.         //echo $sql;
  2964.         $em   $this->getEntityManager();
  2965.         $stmt $em->getConnection()->prepare($sql);
  2966.         $reciproque $stmt->executeQuery()->fetchAllAssociative();
  2967.         if (count($reciproque) == 0) {
  2968.             return false;
  2969.         } else {
  2970.             return true;
  2971.         }
  2972.     }
  2973.     public function getArticlesAinventorierSansEmplacement($inventaire)
  2974.     {
  2975.         $date "";
  2976.         if ( ! is_null($inventaire->getDateMouvement())) {
  2977.             $date $inventaire->getDateMouvement();
  2978.         }
  2979.         //$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)";
  2980.         /*
  2981.         $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)";
  2982.         if($date != ''){
  2983.             $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))";
  2984.         } else {
  2985.             //$where .= " AND p.stock is not null AND p.stock != 0";
  2986.             $where .= " and ((p.stock is not null AND p.stock != 0) OR mvt.quantite != 0)";
  2987.         }
  2988.         */
  2989.         $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)";
  2990.         if ($inventaire->getInclureArticleDifferentZero()) {
  2991.             $where .= " and ((p.stock is not null and p.stock > 0)";
  2992.             if ($inventaire->getInclureArticleDateMouvement()) {
  2993.                 if ($date != "") {
  2994.                     $where .= " or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and (mvt.quantite != 0 and p.stock <= 0)) ";
  2995.                 } else {
  2996.                     $where .= " OR (mvt.quantite != 0 and p.stock <= 0)";
  2997.                 }
  2998.             }
  2999.             $where .= ")";
  3000.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3001.             //$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))";
  3002.             $where .= "and ((mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and (mvt.quantite != 0 and p.stock <= 0)) ";
  3003.             if ($inventaire->getInclureArticleDifferentZero()) {
  3004.                 $where .= " OR (p.stock is not null and p.stock > 0) ";
  3005.             }
  3006.             $where .= ")";
  3007.         } else {
  3008.             $where .= "and 1=1 ";
  3009.         }
  3010.         $where .= ' and (p.nonStocke = 0 OR p.nonStocke IS NULL)';
  3011.         $where .= ' and r.id NOT IN (15)';
  3012.         $query $this->createQueryBuilder('p')
  3013.                       ->select(
  3014.                           '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'
  3015.                       )//, COUNT(eplct.id) as nbeplcts
  3016.                       ->leftJoin('p.mouvementStock''mvt')
  3017.                       ->join('mvt.raisonMouvementStock''r')
  3018.                       ->leftJoin('p.emplacements''eplct')
  3019.                       ->where($where)
  3020.                       ->orderBy('p.reference''ASC')
  3021.                       ->groupBy('p.id');
  3022.         //->having('nbMvts > 0 AND nbEmplacements = 0')
  3023.         $having "nbEmplacements = 0";
  3024.         if ($inventaire->getInclureArticleDifferentZero()) {
  3025.             if ($inventaire->getInclureArticleDateMouvement()) {
  3026.                 $having .= " and nbMvts >0";
  3027.             }
  3028.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3029.             $having .= " and nbMvts >0";
  3030.             if ($inventaire->getInclureArticleDifferentZero()) {
  3031.             }
  3032.         } else {
  3033.         }
  3034.         if ($having != "") {
  3035.             $query->having($having);
  3036.         }
  3037.         //echo $query->getQuery()->getSql();
  3038.         //exit;
  3039.         //->setParameters(array('marque' => $marque,'archive'=>'0'))
  3040.         $res $query->getQuery()->getResult();
  3041.         return $res;
  3042.     }
  3043.     public function getArticlesAinventorierParEmplacement($emplacement$date '')
  3044.     {
  3045.         $where "p.stock is not null and p.stock != 0";
  3046.         if ($date != '') {
  3047.             $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))";
  3048.         }
  3049.         if (is_object($emplacement->getNiveauUn())) {
  3050.             $where .= ' and n1.id = '.$emplacement->getNiveauUn()->getId();
  3051.         }
  3052.         if (is_object($emplacement->getNiveauDeux())) {
  3053.             $where .= ' and n2.id = '.$emplacement->getNiveauDeux()->getId();
  3054.         }
  3055.         if (is_object($emplacement->getNiveauTrois())) {
  3056.             $where .= ' and n3.id = '.$emplacement->getNiveauTrois()->getId();
  3057.         }
  3058.         if (is_object($emplacement->getNiveauQuatre())) {
  3059.             $where .= ' and n4.id = '.$emplacement->getNiveauQuatre()->getId();
  3060.         }
  3061.         /*
  3062.                         if(is_object($emplacement->getNiveauDeux())) $recherche['niveauDeux'] = $emplacement->getNiveauDeux();
  3063.                         if(is_object($emplacement->getNiveauTrois())) $recherche['niveauTrois'] = $emplacement->getNiveauTrois();
  3064.                         if(is_object($emplacement->getNiveauQuatre())) $recherche['niveauQuatre'] = $emplacement->getNiveauQuatre();
  3065.          *
  3066.          */
  3067.         $query $this->createQueryBuilder('p')
  3068.                       ->select('p.id, COUNT(mvt.id) as total, COUNT(eplct.id) as nbArticleOuEmplacement')
  3069.                       ->leftJoin('p.mouvementStock''mvt')
  3070.                       ->leftJoin('p.emplacements''eplct')
  3071.                       ->leftJoin('eplct.niveauUn''n1')
  3072.                       ->leftJoin('eplct.niveauDeux''n2')
  3073.                       ->leftJoin('eplct.niveauTrois''n3')
  3074.                       ->leftJoin('eplct.niveauQuatre''n4')
  3075.                       ->where($where)
  3076.                       ->groupBy('p.id')
  3077.                       ->having('total > 0')//->setParameters(array('marque' => $marque,'archive'=>'0'))
  3078.         ;
  3079.         $res   $query->getQuery()->getResult();
  3080.         return $res;
  3081.     }
  3082.     public function getEmplacementsAinventorierParEmplacement($emplacement$inventaire)
  3083.     {
  3084.         $date "";
  3085.         if ( ! is_null($inventaire->getDateMouvement())) {
  3086.             $date $inventaire->getDateMouvement();
  3087.         }
  3088.         /*
  3089.           $where = "((p.stock is not null and p.stock != 0) OR mvt.quantite != 0)";
  3090.           if($date != ''){
  3091.               $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))";
  3092.           }
  3093.           */
  3094.         if ($inventaire->getInclureArticleDifferentZero()) {
  3095.             $where "((p.stock is not null and p.stock > 0)";
  3096.             if ($inventaire->getInclureArticleDateMouvement()) {
  3097.                 if ($date != "") {
  3098.                     $where .= " or (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0 and p.stock <=0)";
  3099.                 } else {
  3100.                     $where .= " OR (mvt.quantite != 0 and p.stock <= 0) ";
  3101.                 }
  3102.             }
  3103.             $where .= ")";
  3104.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3105.             //$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))";
  3106.             $where "(( mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0 and p.stock <= 0)";
  3107.             if ($inventaire->getInclureArticleDifferentZero()) {
  3108.                 $where .= " OR (p.stock is not null and p.stock > 0) ";
  3109.             }
  3110.             $where .= ")";
  3111.         } else {
  3112.             $where "1=1";
  3113.         }
  3114.         $where .= ' and eplct.visibilite = 1';
  3115.         $where .= ' and (p.nonStocke = 0 OR p.nonStocke IS NULL)';
  3116.         $where .= ' and (r.id NOT IN (15) or r.id is null)';
  3117.         if (is_object($emplacement) and is_object($emplacement->getNiveauUn())) {
  3118.             $where .= ' and n1.id = '.$emplacement->getNiveauUn()->getId();
  3119.         }
  3120.         if (is_object($emplacement) and is_object($emplacement->getNiveauDeux())) {
  3121.             $where .= ' and n2.id = '.$emplacement->getNiveauDeux()->getId();
  3122.         }
  3123.         if (is_object($emplacement) and is_object($emplacement->getNiveauTrois())) {
  3124.             $where .= ' and n3.id = '.$emplacement->getNiveauTrois()->getId();
  3125.         }
  3126.         if (is_object($emplacement) and is_object($emplacement->getNiveauQuatre())) {
  3127.             $where .= ' and n4.id = '.$emplacement->getNiveauQuatre()->getId();
  3128.         }
  3129.         /*
  3130.                         if(is_object($emplacement->getNiveauDeux())) $recherche['niveauDeux'] = $emplacement->getNiveauDeux();
  3131.                         if(is_object($emplacement->getNiveauTrois())) $recherche['niveauTrois'] = $emplacement->getNiveauTrois();
  3132.                         if(is_object($emplacement->getNiveauQuatre())) $recherche['niveauQuatre'] = $emplacement->getNiveauQuatre();
  3133.          *
  3134.          */
  3135.         $query $this->createQueryBuilder('p')
  3136.                       ->select('eplct.id,p.stock, COUNT(mvt.id) as total, COUNT(eplct.id) as nbArticleOuEmplacement')
  3137.                       ->leftJoin('p.mouvementStock''mvt')
  3138.                       ->leftJoin('mvt.raisonMouvementStock''r')
  3139.                       ->leftJoin('p.emplacements''eplct')
  3140.                       ->leftJoin('eplct.niveauUn''n1')
  3141.                       ->leftJoin('eplct.niveauDeux''n2')
  3142.                       ->leftJoin('eplct.niveauTrois''n3')
  3143.                       ->leftJoin('eplct.niveauQuatre''n4')
  3144.                       ->where($where);
  3145.         if ($emplacement !== null && is_object($emplacement) or == 1) {
  3146.             //echo "YYYY";
  3147.             //exit;
  3148.             $query->addOrderBy('eplct.libelle''ASC');
  3149.         }
  3150.         $query->addOrderBy('p.reference''ASC')
  3151.             //->orderBy('p.reference', 'ASC')
  3152.               ->groupBy('eplct.id');
  3153.         $having "";
  3154.         //->having('(total > 0 or (p.stock is not null and p.stock != 0))')
  3155.         if ($inventaire->getInclureArticleDifferentZero()) {
  3156.             $having "((p.stock is null or p.stock > 0)";
  3157.             if ($inventaire->getInclureArticleDateMouvement()) {
  3158.                 $having .= " or (total > 0 and p.stock <=0)";
  3159.             }
  3160.             $having .= ")";
  3161.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3162.             $having "((total > 0 and p.stock <=0)";
  3163.             if ($inventaire->getInclureArticleDifferentZero()) {
  3164.                 $having .= " or (p.stock is not null or p.stock > 0)";
  3165.             }
  3166.             $having .= ")";
  3167.         } else {
  3168.         }
  3169.         if ($having != "") {
  3170.             $query->having($having);
  3171.         }//->setParameters(array('marque' => $marque,'archive'=>'0'))
  3172.         ;
  3173.         //echo "IIII";
  3174.         //exit;
  3175.         //echo $query->getQuery()->getSql();
  3176.         //exit;
  3177.         $res $query->getQuery()->getResult();
  3178.         return $res;
  3179.     }
  3180.     public function getArticlesAinventorierParMarque($marque$date '')
  3181.     {
  3182.         $where "p.marque = :marque and p.stock is not null and p.stock != 0";
  3183.         if ($date != '') {
  3184.             $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))";
  3185.         }
  3186.         $query $this->createQueryBuilder('p')
  3187.                       ->select(
  3188.                           'p.id,p.stock, COUNT(mvt.id) as total, CASE
  3189.                     WHEN COUNT(eplct.id) > COUNT(p.id) THEN COUNT(eplct.id)
  3190.                     ELSE COUNT(p.id)
  3191.                     as nbArticleOuEmplacement'
  3192.                       )
  3193.                       ->leftJoin('p.mouvementStock''mvt')
  3194.                       ->leftJoin('p.emplacements''eplct')
  3195.                       ->where($where)
  3196.                       ->groupBy('p.id')
  3197.                       ->having('(total > 0 or (p.stock is not null and p.stock != 0))')
  3198.                       ->setParameters(['marque' => $marque]);
  3199.         $res   $query->getQuery()->getResult();
  3200.         return $res;
  3201.     }
  3202.     public function getArticlesAinventorierParMarqueEtEmplacement($marque$date ''$emplacement null$inventaire)
  3203.     {
  3204.         $selectId   'p';
  3205.         $parameters = ['marque' => $marque];
  3206.         /*
  3207.         $where = "p.marque = :marque and ((p.stock is not null and p.stock != 0) OR mvt.quantite != 0)";
  3208.         if($date != ''){
  3209.             $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))";
  3210.         }
  3211.         */
  3212.         $where "p.marque = :marque and ((p.stock is not null and p.stock != 0) OR mvt.quantite != 0)";
  3213.         if ($inventaire->getInclureArticleDifferentZero()) {
  3214.             $where .= " and ((p.stock is not null and p.stock > 0)";
  3215.             if ($inventaire->getInclureArticleDateMouvement()) {
  3216.                 if ($date != "") {
  3217.                     $where .= " OR (mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0 and p.stock <= 0)";
  3218.                 } else {
  3219.                     $where .= " OR (mvt.quantite > 0 and p.stock <= 0) ";
  3220.                 }
  3221.             }
  3222.             $where .= ")";
  3223.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3224.             //$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))";
  3225.             $where .= "((mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' and mvt.quantite != 0 and p.stock <= 0)";
  3226.             if ($inventaire->getInclureArticleDifferentZero()) {
  3227.                 $where .= " OR (p.stock is not null and p.stock > 0) ";
  3228.             }
  3229.             $where .= ")";
  3230.         }
  3231.         $where .= ' and (p.nonStocke = 0 OR p.nonStocke IS NULL)';
  3232.         $where .= ' and (r.id NOT IN (15) or r.id is null)';
  3233.         if ($emplacement !== null && is_object($emplacement)) {
  3234.             if (is_object($emplacement->getNiveauUn())) {
  3235.                 $where .= ' and n1.id = '.$emplacement->getNiveauUn()->getId();
  3236.             }
  3237.             if (is_object($emplacement->getNiveauDeux())) {
  3238.                 $where .= ' and n2.id = '.$emplacement->getNiveauDeux()->getId();
  3239.             }
  3240.             if (is_object($emplacement->getNiveauTrois())) {
  3241.                 $where .= ' and n3.id = '.$emplacement->getNiveauTrois()->getId();
  3242.             }
  3243.             if (is_object($emplacement->getNiveauQuatre())) {
  3244.                 $where .= ' and n4.id = '.$emplacement->getNiveauQuatre()->getId();
  3245.             }
  3246.             /*
  3247.                 $where .= " and eplct.id = :emplacement";
  3248.                 $parameters['emplacement'] = $emplacement->getId();
  3249.              *
  3250.              */
  3251.             $where    .= ' and eplct.visibilite = 1';
  3252.             $selectId 'eplct';
  3253.         }
  3254.         $query $this->createQueryBuilder('p')
  3255.                       ->select(
  3256.                           $selectId.'.id,p.stock, COUNT(mvt.id) as total, CASE
  3257.                                                         WHEN COUNT(eplct.id) > COUNT(p.id) THEN COUNT(eplct.id)
  3258.                                                         ELSE COUNT(p.id)
  3259.                                                         as nbArticleOuEmplacement'
  3260.                       )
  3261.                       ->leftJoin('p.mouvementStock''mvt')
  3262.                       ->leftJoin('mvt.raisonMouvementStock''r')
  3263.                       ->leftJoin('p.emplacements''eplct')
  3264.                       ->leftJoin('eplct.niveauUn''n1')
  3265.                       ->leftJoin('eplct.niveauDeux''n2')
  3266.                       ->leftJoin('eplct.niveauTrois''n3')
  3267.                       ->leftJoin('eplct.niveauQuatre''n4')
  3268.                       ->where($where);
  3269.         $query->addOrderBy('p.reference''ASC');
  3270.         if ($emplacement !== null && is_object($emplacement)) {
  3271.             $query->addOrderBy('eplct.libelle''ASC');
  3272.         }
  3273.         $query->groupBy($selectId.'.id')
  3274.               ->setParameters($parameters);
  3275.         //->having('(total > 0 or (p.stock is not null and p.stock != 0))')
  3276.         $having "";
  3277.         if ($inventaire->getInclureArticleDifferentZero()) {
  3278.             $having "((p.stock is null or p.stock > 0)";
  3279.             if ($inventaire->getInclureArticleDateMouvement()) {
  3280.                 $having .= " or (total > 0 and p.stock <=0)";
  3281.             }
  3282.             $having .= ")";
  3283.         } elseif ($inventaire->getInclureArticleDateMouvement() && $date != "") {
  3284.             $having "((total > 0 and p.stock <=0)";
  3285.             if ($inventaire->getInclureArticleDifferentZero()) {
  3286.                 $having .= " or (p.stock is not null or p.stock > 0)";
  3287.             }
  3288.             $having .= ")";
  3289.         } else {
  3290.         }
  3291.         if ($having != "") {
  3292.             $query->having($having);
  3293.         }
  3294.         $res $query->getQuery()->getResult();
  3295.         return $res;
  3296.     }
  3297.     public function getArticlesMouvementes($date '')
  3298.     {
  3299.         $where "(p.stock is not null and p.stock != 0) OR mvt.quantite != 0";
  3300.         if ($date != '') {
  3301.             $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)";
  3302.             //exclure certains mvt
  3303.         }
  3304.         $query $this->createQueryBuilder('p')
  3305.                       ->select('p.id, COUNT(mvt.id) as nbMvts')//, COUNT(eplct.id) as nbeplcts
  3306.                       ->leftJoin('p.mouvementStock''mvt')
  3307.                       ->where($where)
  3308.                       ->groupBy('p.id')
  3309.                       ->having('nbMvts > 0')//->setParameters(array('marque' => $marque,'archive'=>'0'))
  3310.         ;
  3311.         $res   $query->getQuery()->getResult();
  3312.         return $res;
  3313.     }
  3314.     public function getArticlesMouvementesPump($date '')
  3315.     {
  3316.         $where "mvt.quantite > 0 and r.action = 'augmenter'";
  3317.         if ($date != '') {
  3318.             $where "mvt.dateMvt > '".$date->format("Y-m-d H:i:s")."' AND mvt.quantite > 0 AND r.action = 'augmenter'";
  3319.             //exclure certains mvt
  3320.         }
  3321.         $query $this->createQueryBuilder('p')
  3322.                       ->select('p.id, COUNT(mvt.id) as nbMvts')//, COUNT(eplct.id) as nbeplcts
  3323.                       ->leftJoin('p.mouvementStock''mvt')
  3324.                       ->leftJoin('mvt.raisonMouvementStock''r')
  3325.                       ->where($where)
  3326.                       ->groupBy('p.id')
  3327.                       ->having('nbMvts > 0')//->setParameters(array('marque' => $marque,'archive'=>'0'))
  3328.         ;
  3329.         $res   $query->getQuery()->getResult();
  3330.         return $res;
  3331.     }
  3332.     public function getArticlesPumpNull()
  3333.     {
  3334.         $query $this->createQueryBuilder('p')
  3335.                       ->select('p.id')//, COUNT(eplct.id) as nbeplcts
  3336.                       ->join('p.conditionsAchat''ca')
  3337.                       ->where('p.pump IS NULL OR p.pump = 0')
  3338.                       ->groupBy('p.id');
  3339.         $res   $query->getQuery()->getResult();
  3340.         return $res;
  3341.     }
  3342.     public function articleCommandeSupprimableCommande($ac)
  3343.     {
  3344.         $commande $ac->getCommande();
  3345.         $article  $ac->getArticle();
  3346.         $res $this->getEntityManager()
  3347.                     ->createQuery(
  3348.                         "select count(ac.id) as total
  3349.                 FROM App\Entity\GestionComerciale\ArticleCommande ac
  3350.                 LEFT JOIN App\Entity\GestionComerciale\Commande doc with ac.commande = doc.id
  3351.                 LEFT JOIN App\Entity\GestionComerciale\StatutCommande s with doc.statutCommande = s.id
  3352.                 WHERE doc.commande = :commande and ac.article = :article and doc.typeDocumentCommercial IN (:typeDocumentCommercial) and s.ordre NOT IN (:statut)
  3353.                 "
  3354.                     )
  3355.                     ->setParameters(['commande' => $commande->getId(), 'article' => $article->getId(), 'typeDocumentCommercial' => [TypeDocumentCommercial::BPTypeDocumentCommercial::BLTypeDocumentCommercial::FACTURE], 'statut' => [0]])
  3356.                     ->getOneOrNullResult();
  3357.         if ($res === null || $res['total'] == 0) {
  3358.             return true;
  3359.         }
  3360.         return false;
  3361.     }
  3362.     public function findByStockReserveNotNull()
  3363.     {
  3364.         $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)";
  3365.         $query $this->createQueryBuilder('p')
  3366.                       ->where($where)//->setMaxResults(10);
  3367.         ;
  3368.         //$res = $query->getQuery()->getResult();
  3369.         return $query;
  3370.     }
  3371.     public function findByStockReserveInferieur0()
  3372.     {
  3373.         $where "p.stockReserve < 0 and (p.divers = 0 or p.divers is null) and (p.diversComposes = 0 or p.diversComposes is null)";
  3374.         $query $this->createQueryBuilder('p')
  3375.                       ->where($where)//->setMaxResults(10);
  3376.         ;
  3377.         //$res = $query->getQuery()->getResult();
  3378.         return $query;
  3379.     }
  3380.     public function getArticleAvecCoeffCondit()
  3381.     {
  3382.         $where "p.majPrix = 1 and c.defaut = 1 and c.coefficientConditionnement > 1";
  3383.         $query $this->createQueryBuilder('p')
  3384.             //->select('p.id')
  3385.                       ->leftJoin('p.conditionsAchat''c')
  3386.             //->groupBy('p.id')
  3387.                       ->where($where);
  3388.         return $query;
  3389.     }
  3390.     public function findByCommande($commandeId)
  3391.     {
  3392.         $query $this->createQueryBuilder('p')
  3393.             //->select('p.id')
  3394.                       ->leftJoin('p.articleCommande''ac')
  3395.                       ->leftJoin('ac.commande''c')
  3396.                       ->groupBy('p.id')
  3397.                       ->where('c.id = :commande')
  3398.                       ->setParameters(['commande' => $commandeId]);
  3399.         return $query;
  3400.     }
  3401.     public function getArticleByRef($ref$categorie)
  3402.     {
  3403.         $query $this->createQueryBuilder('p')
  3404.             //->select('p.id')
  3405.                       ->leftJoin('p.articleCategorie''ac')
  3406.                       ->leftJoin('ac.categorie''c')
  3407.                       ->where("p.ref LIKE ':ref'")
  3408.                       ->setParameters(['ref' => '%|'.$ref]);
  3409.         return $query;
  3410.     }
  3411. }