src/Repository/GestionComerciale/CommandeRepository.php line 7308

Open in your IDE?
  1. <?php
  2. namespace App\Repository\GestionComerciale;
  3. use App\Entity\Articles\RaisonMouvementStock;
  4. use App\Entity\GestionComerciale\Acompte;
  5. use App\Entity\GestionComerciale\ArticleCommande;
  6. use App\Entity\GestionComerciale\Commande;
  7. use App\Entity\GestionComerciale\StatutCommande;
  8. use App\Entity\GestionComerciale\StatutPaiement;
  9. use App\Model\GestionCommerciale\TypeDocumentCommercial;
  10. use App\Entity\Rangements\ListePreparationCommande;
  11. use DateInterval;
  12. use DateTime;
  13. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  14. use Doctrine\DBAL\Connection;
  15. use Doctrine\Persistence\ManagerRegistry;
  16. use App\Entity\Clients\Client;
  17. use App\Entity\Vehicules\VehiculeClient;
  18. /**
  19.  * CommandeRepository
  20.  *
  21.  * This class was generated by the Doctrine ORM. Add your own custom
  22.  * repository methods below.
  23.  */
  24. class CommandeRepository extends ServiceEntityRepository
  25. {
  26.     public function __construct(ManagerRegistry $registry)
  27.     {
  28.         parent::__construct($registryCommande::class);
  29.     }
  30.     public function getAExporter($donnees = [])
  31.     {
  32.         $parameters = [];
  33.         $where      "";
  34.         //print_r($donnees);
  35.         //exit;
  36.         $where                                "x.typeDocumentCommercial = :typeDocumentCommercial ";
  37.         $parameters["typeDocumentCommercial"] = "2";
  38.         if (array_key_exists('statut'$donnees) && $donnees["statut"] != "") {
  39.             if ($where == "") {
  40.                 $where .= "sc.id = :statut ";
  41.             } else {
  42.                 $where .= "AND sc.id = :statut ";
  43.             }
  44.             $parameters["statut"] = $donnees["statut"];
  45.         } else {
  46.             //$where = "sc.id = :statut ";
  47.             //$parameters["statut"]=$donnees["statut"];
  48.         }
  49.         if (array_key_exists('client'$donnees) && $donnees["client"] != "") {
  50.             if ($where == "") {
  51.                 $where .= "c.id = :client ";
  52.             } else {
  53.                 $where .= "AND c.id = :client ";
  54.             }
  55.             $parameters["client"] = $donnees["client"];
  56.         }
  57.         if (array_key_exists('date_debut'$donnees) && $donnees["date_debut"] != "") {
  58.             $dateDebut DateTime::createFromFormat('d/m/Y'$donnees['date_debut']);
  59.             $dateDebut->setTime(235959);
  60.             if ($where == "") {
  61.                 $where .= "c.date >= :date_debut ";
  62.             } else {
  63.                 $where .= "AND c.date >= :date_debut ";
  64.             }
  65.             //$where .= "AND c.date >= :date_debut ";
  66.             $parameters["date_debut"] = $dateDebut;
  67.         }
  68.         if (array_key_exists('date_fin'$donnees) && $donnees["date_fin"] != "") {
  69.             $dateFin DateTime::createFromFormat('d/m/Y'$donnees['date_fin']);
  70.             $dateFin->setTime(235959);
  71.             if ($where == "") {
  72.                 $where .= "c.date <= :date_fin ";
  73.             } else {
  74.                 $where .= "AND c.date <= :date_fin ";
  75.             }
  76.             $parameters["date_fin"] = $dateFin;
  77.         }
  78.         if (array_key_exists('coche'$donnees) && $donnees["coche"] != "") {
  79.             $explode_coche             explode(","$donnees["coche"]);
  80.             $parameters["id_commande"] = $explode_coche;
  81.             if ($where == "") {
  82.                 $where .= "x.id IN (:id_commande) ";
  83.             } else {
  84.                 $where .= "AND x.id IN (:id_commande) ";
  85.             }
  86.         }
  87.         /*
  88.             {{commande.getDestinataireLivraisonTexte|raw}}
  89.             {{commande.numeroLivraison}} {{commande.rueLivraison}}
  90.             {{commande.complementLivraison}}
  91.             {{commande.complementLivraison2}}
  92.             {{commande.codePostalLivraisonTexte}}
  93.             {{commande.villeLivraisonTexte}}
  94.             {{commande.paysLivraisonTexte}}
  95.         */
  96.         //echo $where;
  97.         //print_r($parameters);
  98.         //exit;
  99.         $query $this->createQueryBuilder('x')
  100.                       ->select(
  101.                           ' x.referenceMarketPlace as refMP, '
  102.                           'c.nom, '
  103.                           'c.prenom, '
  104.                           'x.rueLivraison as adresse1, '
  105.                           'x.complementLivraison as adresse2, '
  106.                           'x.complementLivraison2 as adresse3, '
  107.                           'x.codePostalLivraisonTexte as code_postal,'
  108.                           'x.villeLivraisonTexte as ville, '
  109.                           'x.paysLivraisonTexte as pays, '
  110.                           'c.telephone, '
  111.                           'c.telephone2, '
  112.                           'c.email,'
  113.                           'x.destinataireLivraisonTexte as destinataireLivraisonTexte,'
  114.                           'x.numeroLivraison as numeroLivraison'
  115.                       );
  116.         if ($where != "") {
  117.             $query->where($where)->setParameters($parameters);
  118.         }
  119.         $query->leftJoin('x.client''c')
  120.               ->leftJoin('x.statutCommande''sc');
  121.         $query->orderBy('x.date''DESC');
  122.         $res $query->getQuery()->getResult();
  123.         return $res;
  124.     }
  125.     public function getCaMarketPlace($dateDebut ''$dateFin ''$compteMarketPlace '')
  126.     {
  127.         $where      'p.dateSuppression IS NULL AND p.typeDocumentCommercial IN (:typeDocumentCommercial) AND p.statutCommande NOT IN (:statut)';
  128.         $parameters = ['typeDocumentCommercial' => [TypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR], 'statut' => [0]];
  129.         if ($dateDebut != '') {
  130.             $where                   .= " AND p.date >= :dateDebut";
  131.             $parameters['dateDebut'] = $dateDebut;
  132.         }
  133.         if ($dateFin != '') {
  134.             $where                 .= " AND p.date < :dateFin";
  135.             $parameters['dateFin'] = $dateFin;
  136.         }
  137.         if ($compteMarketPlace != '') {
  138.             $where                           .= " AND p.compteMarketPlace = :compteMarketPlace";
  139.             $parameters['compteMarketPlace'] = $compteMarketPlace;
  140.         }
  141.         $query $this->createQueryBuilder('p')
  142.                       ->select("SUM(p.total) as total")
  143.                       ->where($where)
  144.                       ->setParameters($parameters);
  145.         $res   $query->getQuery()->getOneOrNullResult();
  146.         $sql $query->getQuery()->getSQL();
  147.         if ($compteMarketPlace == "1" or $compteMarketPlace == "2") {
  148.             //echo "<hr/><div style='margin-bottom:10px;margin-top:10px;'>".$compteMarketPlace." ".$sql."</div>";
  149.             //print_r($parameters);
  150.         }
  151.         return $res;
  152.     }
  153.     public function getCaNonFactureMarketPlace($dateDebut ''$dateFin ''$compteMarketPlace '')
  154.     {
  155.         $where      'p.dateSuppression IS NULL AND p.typeDocumentCommercial NOT IN (:typeDocumentCommercial) AND p.statutCommande NOT IN (:statut)';
  156.         $parameters = ['typeDocumentCommercial' => [TypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR], 'statut' => [0]];
  157.         if ($dateDebut != '') {
  158.             $where                   .= " AND p.date >= :dateDebut";
  159.             $parameters['dateDebut'] = $dateDebut;
  160.         }
  161.         if ($dateFin != '') {
  162.             $where                 .= " AND p.date < :dateFin";
  163.             $parameters['dateFin'] = $dateFin;
  164.         }
  165.         if ($compteMarketPlace != '') {
  166.             $where                           .= " AND p.compteMarketPlace < :compteMarketPlace";
  167.             $parameters['compteMarketPlace'] = $compteMarketPlace;
  168.         }
  169.         $query $this->createQueryBuilder('p')
  170.                       ->select("SUM(p.total) as total")
  171.                       ->where($where)
  172.                       ->setParameters($parameters);
  173.         $res   $query->getQuery()->getOneOrNullResult();
  174.         return $res;
  175.     }
  176.     public function getCommandesExpeditionsEnCoursTardive()
  177.     {
  178.         $date     = new Datetime();
  179.         $interval = new DateInterval('PT45M');
  180.         $date->sub($interval);
  181.         //echo "<div>date ".$date->format("d/m/y H:i")."</div>";
  182.         $where                                    "p.dateConfirmationExpedition < :dateConfirmationExpedition AND p.statutCommande = :statutCommande";
  183.         $parameters                               = [];
  184.         $parameters["statutCommande"]             = "36";
  185.         $parameters["dateConfirmationExpedition"] = $date;
  186.         $query $this->createQueryBuilder('p')
  187.             //->select("SUM(p.total) as total")
  188.                       ->where($where)
  189.                       ->setParameters($parameters);
  190.         $res $query->getQuery()->getResult();
  191.         return $res;
  192.     }
  193.     public function getTotalCmdeSifam()
  194.     {
  195.         $statut 9;
  196.         $query  $this->createQueryBuilder('p')
  197.                        ->select("COUNT(p.id) as total")
  198.                        ->where('p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.sifam = :sifam')
  199.                        ->setParameters(['statutCommande' => $statut'sifam' => "1"]);
  200.         $res    $query->getQuery()->getOneOrNullResult();
  201.         return $res;
  202.     }
  203.     public function getTotalCmdeVega()
  204.     {
  205.         $statut 9;
  206.         $query  $this->createQueryBuilder('p')
  207.                        ->select("COUNT(p.id) as total")
  208.                        ->where('p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.vega = :vega')
  209.                        ->setParameters(['statutCommande' => $statut'vega' => "1"]);
  210.         $res    $query->getQuery()->getOneOrNullResult();
  211.         return $res;
  212.     }
  213.     public function getBlsAfacturer()
  214.     {
  215.         $query $this->createQueryBuilder('p')
  216.                       ->select("COUNT(p.id) as total")
  217.                       ->leftJoin('p.statutCommande''sc')
  218.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND sc.ordre != 0 AND p.facture is null')
  219.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BL]);
  220.         $res   $query->getQuery()->getOneOrNullResult();
  221.         return $res;
  222.     }
  223.     public function getTotalCommandeStatut($statut$internet 0)
  224.     {
  225.         $where_internet " and p.referenceMarketPlace IS NULL";
  226.         if ($internet == 1) {
  227.             $where_internet " and p.referenceMarketPlace IS NOT NULL";
  228.         }
  229.         $query $this->createQueryBuilder('p')
  230.                       ->select("COUNT(p.id) as total")
  231.                       ->where('p.statutCommande = :statutCommande AND p.dateSuppression IS NULL ' $where_internet)
  232.                       ->setParameters(['statutCommande' => $statut]);
  233.         $res   $query->getQuery()->getOneOrNullResult();
  234.         return $res;
  235.     }
  236.     public function getDerniereCommandeV2Para($typeDocumentCommercial$parametres)
  237.     {
  238.         $nombre 10;
  239.         $tri    'p.date';
  240.         $ordre  'DESC';
  241.         if (isset($parametres)) {
  242.             foreach ($parametres as $para) {
  243.                 if ($para->getParametres()->getType() == 'nombre') {
  244.                     $nombre intval($para->getValeur());
  245.                 }
  246.                 if ($para->getParametres()->getType() == 'tri') {
  247.                     $tri $para->getValeur();
  248.                 }
  249.                 if ($para->getParametres()->getType() == 'ordre') {
  250.                     $ordre $para->getValeur();
  251.                 }
  252.             }
  253.         }
  254.         $query $this->createQueryBuilder('p')
  255.             //->select("CONCAT(CONCAT(p.prenom, ' '), p.nom) as libelle, p.id,p.telephone")
  256.                       ->where('p.typeDocumentCommercial = :type and p.visible=1 and p.origineSaisie IN (:origineSaisie)')
  257.                       ->setFirstResult(0)
  258.                       ->setMaxResults($nombre)
  259.                       ->orderBy($tri$ordre)
  260.                       ->setParameters(['type' => $typeDocumentCommercial'origineSaisie' => ["commande""devis""bp"]]);
  261.         $res   $query->getQuery()->getResult();
  262.         return $res;
  263.     }
  264.     public function getXmlCommandes($param = [])
  265.     {
  266.         $where            'x.typeDocumentCommercial IN (:typeDocumentCommercial)';
  267.         $parametres_query = ['typeDocumentCommercial' => [TypeDocumentCommercial::DEVISTypeDocumentCommercial::COMMANDETypeDocumentCommercial::BLTypeDocumentCommercial::FACTURE]];
  268.         $query $this->createQueryBuilder('x')
  269.             //->select("COUNT(p.id) as total")
  270.             //->leftJoin('p.marque', 'm')
  271.             //->where('(p.divers is null or p.divers = :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)')
  272.             //->setParameters(array('zero' =>'0'))
  273.             //->setMaxResults(100000)
  274.                       ->orderBy('x.id''DESC');;
  275.         if (array_key_exists('type'$param) && $param["type"] == "date") {
  276.             $date = (new DateTime())->modify('-3 hours');
  277.             //$date = (new \DateTime())->modify('-60 days');
  278.             $where                    .= " AND x.date >= :date";
  279.             $parametres_query["date"] = $date;
  280.         } elseif (array_key_exists('type'$param) && $param["type"] == "all") {
  281.         } else {
  282.             $date = (new DateTime())->modify('-6 months');
  283.             $where                    .= " AND x.date >= :date";
  284.             $parametres_query["date"] = $date;
  285.         }
  286.         if (count($parametres_query) > 0) {
  287.             $query->where($where);
  288.             $query->setParameters($parametres_query);
  289.         }
  290.         $res $query->getQuery()->getResult();
  291.         return $res;
  292.     }
  293.     public function getXmlCommandesAnnulees($param = [])
  294.     {
  295.         $where            'x.typeDocumentCommercial IN (:typeDocumentCommercial) AND x.dateAnnulation IS NOT NULL AND x.referencePrestashop IS NOT NULL';
  296.         $parametres_query = ['typeDocumentCommercial' => [TypeDocumentCommercial::COMMANDE]];
  297.         $query $this->createQueryBuilder('x')
  298.                       ->orderBy('x.id''DESC');;
  299.         if (array_key_exists('type'$param) && $param["type"] == "date") {
  300.             $date = (new DateTime())->modify('-3 hours');
  301.             //$date = (new \DateTime())->modify('-30 days');
  302.             $where                    .= " AND x.dateAnnulation >= :date";
  303.             $parametres_query["date"] = $date;
  304.         } elseif (array_key_exists('type'$param) && $param["type"] == "all") {
  305.         } else {
  306.             $date = (new DateTime())->modify('-6 months');
  307.             $where                    .= " AND x.dateAnnulation >= :date";
  308.             $parametres_query["date"] = $date;
  309.         }
  310.         if (count($parametres_query) > 0) {
  311.             $query->where($where);
  312.             $query->setParameters($parametres_query);
  313.         }
  314.         $res $query->getQuery()->getResult();
  315.         return $res;
  316.     }
  317.     public function getNombreCommandeInternet($where$parametres)
  318.     {
  319.         /*
  320.         ->addJoin('x.etatCommande', 'ec', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
  321.                      ->addJoin('x.statutCommande', 's', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
  322.                      ->addJoin('x.client', 'c', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
  323.                      //->addJoin('x.typeDocumentCommercial', 'td', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
  324.                      ->addJoin('x.utilisateur', 'u', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
  325.         */
  326.         $query $this->createQueryBuilder('x')
  327.                       ->leftjoin('x.statutCommande''s')
  328.                       ->leftjoin('x.client''c')
  329.                       ->leftjoin('x.typeDocumentCommercial''td')
  330.                       ->leftjoin('x.utilisateur''u')
  331.                       ->select("COUNT(x.id) as total")
  332.                       ->where($where)
  333.                       ->setParameters($parametres);
  334.         $res $query->getQuery()->getResult();
  335.         return $res[0]["total"];
  336.     }
  337.     public function getNombreCommandeAttenteValidation($where$parametres)
  338.     {
  339.         $query $this->createQueryBuilder('x')
  340.                       ->leftjoin('x.statutCommande''s')
  341.                       ->leftjoin('x.typeDocumentCommercial''td')
  342.                       ->select("COUNT(x.id) as total")
  343.                       ->where($where)
  344.                       ->setParameters($parametres);
  345.         $res $query->getQuery()->getResult();
  346.         return $res[0]["total"];
  347.     }
  348.     public function getFactureEtBl(Commande $commandeCommande $actuel)
  349.     {
  350.         $exclure 0;
  351.         if (is_object($actuel->getBl())) {
  352.             $exclure $actuel->getBl()->getId();
  353.         }
  354.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  355.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => [65], "ordre" => [0]]);
  356.         $query $this->createQueryBuilder('p')
  357.                       ->where(
  358.                           'p.typeDocumentCommercial IN (:typeDocumentCommercial)
  359.                         AND p.dateSuppression IS NULL
  360.                         AND p.statutCommande NOT IN (:statutCommande)
  361.                         AND p.commande = :commande
  362.                         AND p.id != :actuel
  363.                         AND p.id != :exclure
  364.                         AND p.bl is NULL
  365.                         '
  366.                       )
  367.                       ->setParameters(['typeDocumentCommercial' => [TypeDocumentCommercial::BLTypeDocumentCommercial::FACTURE], 'statutCommande' => $liste_statut"commande" => $commande"actuel" => $actuel"exclure" => $exclure]);
  368.         $res $query->getQuery()->getResult();
  369.         return $res;
  370.     }
  371.     public function getFactureEtAvoirEtBl(Commande $commandeCommande $actuel)
  372.     {
  373.         $exclure 0;
  374.         if (is_object($actuel->getBl())) {
  375.             $exclure $actuel->getBl()->getId();
  376.         }
  377.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  378.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => [65], "ordre" => [0]]);
  379.         $query $this->createQueryBuilder('p')
  380.                       ->where(
  381.                           'p.typeDocumentCommercial IN (:typeDocumentCommercial)
  382.                         AND p.dateSuppression IS NULL
  383.                         AND p.statutCommande NOT IN (:statutCommande)
  384.                         AND p.commande = :commande
  385.                         AND p.id != :actuel
  386.                         AND p.id != :exclure
  387.                         AND p.bl is NULL
  388.                         '
  389.                       )
  390.                       ->setParameters(
  391.                           ['typeDocumentCommercial' => [TypeDocumentCommercial::BLTypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR], 'statutCommande' => $liste_statut"commande" => $commande"actuel" => $actuel"exclure" => $exclure]
  392.                       );
  393.         $res $query->getQuery()->getResult();
  394.         return $res;
  395.     }
  396.     public function getTotalAvoir($moisRaz '')
  397.     {
  398.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  399.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 7"ordre" => [2]]);
  400.         if ($moisRaz != "") {
  401.             //Y-m-d H:i:s
  402.             $date = new Datetime();
  403.             if ($moisRaz == '99') {
  404.                 $date_mois = new Datetime($date->format('Y') . '-' $date->format('m') . '-01');
  405.             } else {
  406.                 $date_mois = new Datetime($date->format('Y') . '-' $moisRaz '-01');
  407.             }
  408.             $query $this->createQueryBuilder('p')
  409.                           ->select("COUNT(p.id) as total")
  410.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande) AND p.date >= :date')
  411.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::AVOIR'statutCommande' => $liste_statut'date' => $date_mois->format("Y-m-d H:i:s")]);
  412.         } else {
  413.             $query $this->createQueryBuilder('p')
  414.                           ->select("COUNT(p.id) as total")
  415.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)')
  416.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::AVOIR'statutCommande' => $liste_statut]);
  417.         }
  418.         $res $query->getQuery()->getOneOrNullResult();
  419.         return $res;
  420.     }
  421.     public function getTotalBp($moisRaz '')
  422.     {
  423.         if ($moisRaz != "") {
  424.             //Y-m-d H:i:s
  425.             $date = new Datetime();
  426.             if ($moisRaz == '99') {
  427.                 $date_mois = new Datetime($date->format('Y') . '-' $date->format('m') . '-01');
  428.             } else {
  429.                 $date_mois = new Datetime($date->format('Y') . '-' $moisRaz '-01');
  430.             }
  431.             $query $this->createQueryBuilder('p')
  432.                           ->select("COUNT(p.id) as total")
  433.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.date >= :date')
  434.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BP'date' => $date_mois->format("Y-m-d 00:00:00")]);
  435.         } else {
  436.             $query $this->createQueryBuilder('p')
  437.                           ->select("COUNT(p.id) as total")
  438.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL')
  439.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BP]);
  440.         }
  441.         $res $query->getQuery()->getOneOrNullResult();
  442.         return $res;
  443.     }
  444.     public function getTotalBpa($moisRaz '')
  445.     {
  446.         if ($moisRaz != "") {
  447.             //Y-m-d H:i:s
  448.             $date = new Datetime();
  449.             if ($moisRaz == '99') {
  450.                 $date_mois = new Datetime($date->format('Y') . '-' $date->format('m') . '-01');
  451.             } else {
  452.                 $date_mois = new Datetime($date->format('Y') . '-' $moisRaz '-01');
  453.             }
  454.             $query $this->createQueryBuilder('p')
  455.                           ->select("COUNT(p.id) as total")
  456.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.date >= :date')
  457.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BPATELIER'date' => $date_mois->format("Y-m-d H:i:s")]);
  458.         } else {
  459.             $query $this->createQueryBuilder('p')
  460.                           ->select("COUNT(p.id) as total")
  461.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL')
  462.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BPATELIER]);
  463.         }
  464.         $res $query->getQuery()->getOneOrNullResult();
  465.         return $res;
  466.     }
  467.     public function getTotalBl($moisRaz '')
  468.     {
  469.         if ($moisRaz != "") {
  470.             //Y-m-d H:i:s
  471.             $date = new Datetime();
  472.             if ($moisRaz == '99') {
  473.                 $date_mois = new Datetime($date->format('Y') . '-' $date->format('m') . '-01');
  474.             } else {
  475.                 $date_mois = new Datetime($date->format('Y') . '-' $moisRaz '-01');
  476.             }
  477.             $query $this->createQueryBuilder('p')
  478.                           ->select("COUNT(p.id) as total")
  479.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.date >= :date')
  480.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BL'date' => $date_mois->format("Y-m-d 00:00:00")]);
  481.         } else {
  482.             $query $this->createQueryBuilder('p')
  483.                           ->select("COUNT(p.id) as total")
  484.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL')
  485.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BL]);
  486.         }
  487.         $res $query->getQuery()->getOneOrNullResult();
  488.         return $res;
  489.     }
  490.     public function getTotalDevisOr($moisRaz '')
  491.     {
  492.         if ($moisRaz != "") {
  493.             //Y-m-d H:i:s
  494.             $date = new Datetime();
  495.             if ($moisRaz == '99') {
  496.                 $date_mois = new Datetime($date->format('Y') . '-' $date->format('m') . '-01');
  497.             } else {
  498.                 $date_mois = new Datetime($date->format('Y') . '-' $moisRaz '-01');
  499.             }
  500.             $query $this->createQueryBuilder('p')
  501.                           ->select("COUNT(p.id) as total")
  502.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.date >= :date')
  503.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::DEVISOR'date' => $date_mois->format("Y-m-d H:i:s")]);
  504.         } else {
  505.             $query $this->createQueryBuilder('p')
  506.                           ->select("COUNT(p.id) as total")
  507.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL')
  508.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::DEVISOR]);
  509.         }
  510.         $res $query->getQuery()->getOneOrNullResult();
  511.         return $res;
  512.     }
  513.     public function getTotalDevis($moisRaz '')
  514.     {
  515.         $total = [];
  516.         if ($moisRaz != '') {
  517.             // Y-m-d H:i:s
  518.             $date = new \DateTime();
  519.             if ($moisRaz == '99') {
  520.                 $date_mois = new \DateTime($date->format('Y') . '-' $date->format('m') . '-01');
  521.             } else {
  522.                 $date_mois = new \DateTime($date->format('Y') . '-' $moisRaz '-01');
  523.             }
  524.             /* $query = $this->createQueryBuilder('p')
  525.                 ->select("COUNT(p.id) as total")
  526.                 ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.date >= :date')
  527.                 ->setParameters(array('typeDocumentCommercial' => TypeDocumentCommercial::DEVIS, 'date' => $date_mois->format("Y-m-d 00:00:00")));
  528.             $res = $query->getQuery()->getOneOrNullResult(); */
  529.             $query $this->createQueryBuilder('p')
  530.                           ->select('COUNT(p.id) as total')
  531.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.date >= :date')
  532.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::DEVIS'date' => $date_mois->format('Y-m-d 00:00:00')])
  533.                           ->setMaxResults(1)
  534.                           ->orderBy('p.id''DESC');
  535.         } else {
  536.             /* $query = $this->createQueryBuilder('p')
  537.                 ->select("COUNT(p.id) as total")
  538.                 ->where('p.typeDocumentCommercial = :typeDocumentCommercial')
  539.                 ->setParameters(array('typeDocumentCommercial' => TypeDocumentCommercial::DEVIS))
  540.                 ; */
  541.             $query $this->createQueryBuilder('p')
  542.                           ->select('COUNT(p.id) as total')
  543.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial')
  544.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::DEVIS])
  545.                           ->setMaxResults(1)
  546.                           ->orderBy('p.id''DESC');
  547.         }
  548.         return $query->getQuery()->getOneOrNullResult();
  549.     }
  550.     public function getTotalCommande($moisRaz '')
  551.     {
  552.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  553.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 2"ordre" => [01234567910111213]]);
  554.         if ($moisRaz != "") {
  555.             //Y-m-d H:i:s
  556.             $date = new Datetime();
  557.             if ($moisRaz == '99') {
  558.                 $date_mois = new Datetime($date->format('Y') . '-' $date->format('m') . '-01');
  559.             } else {
  560.                 $date_mois = new Datetime($date->format('Y') . '-' $moisRaz '-01');
  561.             }
  562.             $query $this->createQueryBuilder('p')
  563.                           ->select("COUNT(p.id) as total")
  564.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande IN (:statutCommande) AND p.date >= :date')
  565.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE'statutCommande' => $liste_statut'date' => $date_mois->format("Y-m-d 00:00:00")]);
  566.         } else {
  567.             $query $this->createQueryBuilder('p')
  568.                           ->select("COUNT(p.id) as total")
  569.                           ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande IN (:statutCommande)')
  570.                           ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE'statutCommande' => $liste_statut]);
  571.         }
  572.         $res $query->getQuery()->getOneOrNullResult();
  573.         return $res;
  574.     }
  575.     public function getTotalAvoirFacture($facture)
  576.     {
  577.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  578.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 7"ordre" => [1]]);
  579.         $query $this->createQueryBuilder('p')
  580.                       ->select("COUNT(p.id) as total");
  581.         $where 'p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  582.         if (in_array($facture->getTypeDocumentCommercial(), [TypeDocumentCommercial::COMMANDE])) {
  583.             $query->join('p.statutCommande''s');
  584.             $where        'p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND s.ordre NOT IN (:statutCommande)';
  585.             $liste_statut = [0];
  586.         } elseif ($facture->getTypeDocumentCommercial() == TypeDocumentCommercial::BP) {
  587.             $where 'p.bp = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  588.         } elseif ($facture->getTypeDocumentCommercial() == TypeDocumentCommercial::BL) {
  589.             $query->join('p.bls''bl');
  590.             $where 'bl = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  591.         }
  592.         $query->where($where)
  593.               ->setParameters(['commande' => $facture'typeDocumentCommercial' => TypeDocumentCommercial::AVOIR'statutCommande' => $liste_statut]);
  594.         $res $query->getQuery()->getOneOrNullResult();
  595.         return $res;
  596.     }
  597.     public function getNbAvoirEtFacture($commande)
  598.     {
  599.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  600.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 7"ordre" => [1]]);
  601.         $query $this->createQueryBuilder('p')
  602.                       ->select("COUNT(p.id) as total");
  603.         $where 'p.commande = :commande AND p.typeDocumentCommercial IN (:typeDocumentCommercial) AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  604.         if (in_array($commande->getTypeDocumentCommercial(), [TypeDocumentCommercial::COMMANDETypeDocumentCommercial::ORDREREPARATION])) {
  605.             $query->join('p.statutCommande''s');
  606.             $where        'p.commande = :commande AND p.typeDocumentCommercial IN (:typeDocumentCommercial) AND p.dateSuppression IS NULL AND s.ordre NOT IN (:statutCommande)';
  607.             $liste_statut = [0];
  608.         } elseif ($commande->getTypeDocumentCommercial() == TypeDocumentCommercial::BP) {
  609.             $where 'p.bp = :commande AND p.typeDocumentCommercial IN (:typeDocumentCommercial) AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  610.         } elseif ($commande->getTypeDocumentCommercial() == TypeDocumentCommercial::BL) {
  611.             $query->join('p.bls''bl');
  612.             $where 'bl = :commande AND p.typeDocumentCommercial IN (:typeDocumentCommercial) AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  613.         }
  614.         $query->where($where)
  615.               ->setParameters(['commande' => $commande'typeDocumentCommercial' => [TypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR], 'statutCommande' => $liste_statut]);
  616.         $res $query->getQuery()->getOneOrNullResult();
  617.         return $res;
  618.     }
  619.     public function getTotaOrdreRepMecanicien($mecanicien)
  620.     {
  621.         $query $this->createQueryBuilder('p')
  622.                       ->select("COUNT(p.id) as total")
  623.                       ->where('p.mecanicien = :mecanicien AND p.dateSuppression IS NULL AND p.typeDocumentCommercial = :documentcommercial')
  624.                       ->setParameters(['mecanicien' => $mecanicien'documentcommercial' => 3]);
  625.         $res   $query->getQuery()->getOneOrNullResult();
  626.         return $res;
  627.     }
  628.     public function getTotalCommandeClient($client)
  629.     {
  630.         $query $this->createQueryBuilder('p')
  631.                       ->select("COUNT(p.id) as total")
  632.                       ->join('p.statutCommande''sc')
  633.                       ->where('p.client = :client AND p.dateSuppression IS NULL AND sc.ordre != 0')
  634.                       ->setParameters(['client' => $client]);
  635.         $res   $query->getQuery()->getOneOrNullResult();
  636.         return $res;
  637.     }
  638.     public function getTotalDocument($client,$type_document)
  639.     {
  640.         if($type_document=="COMMANDE"$type_document TypeDocumentCommercial::COMMANDE;
  641.         else if($type_document=="DEVIS"$type_document TypeDocumentCommercial::DEVIS;
  642.         else if($type_document=="BL"$type_document TypeDocumentCommercial::BL;
  643.         else if($type_document=="FACTURE"$type_document TypeDocumentCommercial::FACTURE;
  644.         $query $this->createQueryBuilder('p')
  645.             ->select("COUNT(p.id) as total")
  646.             ->leftjoin('p.statutCommande''sc')
  647.             ->where(
  648.                 'p.client = :client '
  649.                 'AND p.dateSuppression IS NULL '
  650.                 'AND p.visible = :visible '
  651.                 'AND ((sc.id is null or sc.ordre != 0) AND p.typeDocumentCommercial = :typeDocumentCommercial AND (p.statutCommande != :statutLivre or p.statutCommande is null))'
  652.             )
  653.             ->setParameter('client',$client)
  654.             ->setParameter('statutLivre',29)
  655.             ->setParameter('visible',1)
  656.             ->setParameter('typeDocumentCommercial',[$type_document])
  657.         ;
  658.         $res $query->getQuery()->getOneOrNullResult();
  659.         return $res;
  660.     }
  661.     public function getTotalCommandeClientFicheClient($client)
  662.     {
  663.         /*
  664.             $query = $this->createQueryBuilder('p')
  665.             ->select("COUNT(p.id) as total")
  666.             //->join('p.typeDocumentCommercial', 't')
  667.             ->leftjoin('p.statutCommande', 'sc')
  668.             ->where('p.client = :client '
  669.             . 'AND p.dateSuppression IS NULL '
  670.             . 'AND (sc.ordre != 0 OR p.typeDocumentCommercial = '.TypeDocumentCommercial::DEVIS.')'
  671.             . 'AND ('
  672.             . ' (p.typeDocumentCommercial = TypeDocumentCommercial::ORDREREPARATION AND p.statutCommande IN (14,15))'
  673.             . ' OR (p.typeDocumentCommercial = '.TypeDocumentCommercial::DEVIS.' AND p.visible = 1 AND p.commande IS NULL)'
  674.             . " OR (p.typeDocumentCommercial = ".TypeDocumentCommercial::COMMANDE." AND p.visible= 1 AND p.origineSaisie IN ('devis', 'commande', 'bp'))"
  675.             . ' OR (p.typeDocumentCommercial = '.TypeDocumentCommercial::BL.' AND p.visible= 1 AND p.facture IS NULL)'
  676.             . ' OR (p.typeDocumentCommercial IN (4, 6, 7) AND p.visible= 1)'
  677.             . ')')//(ded.id IS NULL AND dec.id IS NULL AND debp.id IS NULL AND debl.id IS NULL) OR
  678.             ->setParameters(array('client' => $client))
  679.             ;
  680.             */
  681.         $query $this->createQueryBuilder('p')
  682.                       ->select("COUNT(p.id) as total")
  683.             //->join('p.typeDocumentCommercial', 't')
  684.                       ->leftjoin('p.statutCommande''sc')
  685.                       ->where(
  686.                           'p.client = :client '
  687.                           'AND p.dateSuppression IS NULL '
  688.                           'AND (sc.ordre != 0 OR p.typeDocumentCommercial = ' TypeDocumentCommercial::DEVIS ')'
  689.                           'AND ('
  690.                           ' (p.typeDocumentCommercial = '.TypeDocumentCommercial::ORDREREPARATION.' AND p.statutCommande IN (14,15))'
  691.                           ' OR (p.typeDocumentCommercial = ' TypeDocumentCommercial::DEVIS ' AND p.visible = 1 AND p.commande IS NULL)'
  692.                           " OR (p.typeDocumentCommercial = " TypeDocumentCommercial::COMMANDE " AND p.visible= 1 )"
  693.                           ' OR (p.typeDocumentCommercial = ' TypeDocumentCommercial::BL ' AND p.visible= 1 AND p.facture IS NULL)'
  694.                           ' OR (p.typeDocumentCommercial IN (:typeDocumentCommercial) AND p.visible= 1)'
  695.                           ')'
  696.                       )//(ded.id IS NULL AND dec.id IS NULL AND debp.id IS NULL AND debl.id IS NULL) OR
  697.                       ->setParameter('client',$client)
  698.                     ->setParameter('typeDocumentCommercial',[TypeDocumentCommercial::BPTypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR])
  699.         ;
  700.         $res $query->getQuery()->getOneOrNullResult();
  701.         return $res;
  702.     }
  703.     public function getTotalCommandeClient2($client)
  704.     {
  705.         $query $this->createQueryBuilder('p')
  706.                       ->select("COUNT(p.id) as total")
  707.             //->join('p.typeDocumentCommercial', 't')
  708.                       ->join('p.statutCommande''sc')
  709.                       ->where(
  710.                           'p.client = :client '
  711.                           'AND p.dateSuppression IS NULL '
  712.                           'AND sc.ordre != 0 '
  713.                           'AND ('
  714.                           ' (p.typeDocumentCommercial = TypeDocumentCommercial::ORDREREPARATION AND p.statutCommande IN (14,15))'
  715.                           ' OR (p.typeDocumentCommercial = ' TypeDocumentCommercial::DEVIS ' AND p.visible = 1 AND p.commande IS NULL)'
  716.                           " OR (p.typeDocumentCommercial = " TypeDocumentCommercial::COMMANDE " AND p.visible= 1 AND p.origineSaisie IN ('devis', 'commande', 'bp'))"
  717.                           ' OR (p.typeDocumentCommercial = ' TypeDocumentCommercial::BL ' AND p.visible= 1 AND p.facture IS NULL)'
  718.                           ' OR (p.typeDocumentCommercial IN (4, 6, 7) AND p.visible= 1)'
  719.                           ')'
  720.                       )//(ded.id IS NULL AND dec.id IS NULL AND debp.id IS NULL AND debl.id IS NULL) OR
  721.                       ->setParameters(['client' => $client]);
  722.         $res $query->getQuery()->getOneOrNullResult();
  723.         return $res;
  724.     }
  725.     public function getRechercheDevis($recherche$maxResults 500)
  726.     {
  727.         $cond $recherche "%";
  728.         if (strpos($cond"*") !== false) {
  729.             //$cond = str_replace("*","%",$cond);
  730.         }
  731.         $query $this->createQueryBuilder('p')
  732.                       ->leftJoin(VehiculeClient::class, 'v''with''p.vehiculesClient = v.id')
  733.                       ->where('(p.reference LIKE :cond or v.immatriculation = :recherche) AND p.typeDocumentCommercial = :typeDocumentCommercial')
  734.                       ->setParameters(['cond' => $cond'recherche' => $recherche'typeDocumentCommercial' => TypeDocumentCommercial::DEVIS])
  735.                       ->setFirstResult(0)
  736.                       ->setMaxResults($maxResults)
  737.                       ->orderBy('p.date''DESC');
  738.         $res   $query->getQuery()->getResult();
  739.         return $res;
  740.     }
  741.     public function getRechercheFacture($recherche$maxResults 500)
  742.     {
  743.         $cond $recherche "%";
  744.         if (strpos($cond"*") !== false) {
  745.             //$cond = str_replace("*","%",$cond);
  746.         }
  747.         $query $this->createQueryBuilder('p')
  748.                       ->leftJoin(VehiculeClient::class, 'v''with''p.vehiculesClient = v.id')
  749.                       ->where('(p.reference LIKE :cond or v.immatriculation = :recherche) AND p.typeDocumentCommercial IN (:typeDocumentCommercial)')
  750.                       ->setParameters(['recherche' => $recherche'cond' => $cond'typeDocumentCommercial' => [TypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR]])
  751.                       ->setFirstResult(0)
  752.                       ->setMaxResults($maxResults)
  753.                       ->orderBy('p.date''DESC');
  754.         $res   $query->getQuery()->getResult();
  755.         return $res;
  756.     }
  757.     public function getRechercheBonPreparation($recherche$maxResults 500)
  758.     {
  759.         $cond $recherche "%";
  760.         if (strpos($cond"*") !== false) {
  761.             //$cond = str_replace("*","%",$cond);
  762.         }
  763.         $query $this->createQueryBuilder('p')
  764.                       ->leftJoin(ArticleCommande::class, 'ac''with''ac.commande = p.id')
  765.             //->leftJoin('DTCArticlesBundle:NumeroSerie', 'n', 'with', 'n.articleCommande = ac.id')
  766.                       ->leftJoin(VehiculeClient::class, 'v''with''p.vehiculesClient = v.id')
  767.                       ->where('(p.reference LIKE :cond or v.immatriculation = :recherche) AND p.typeDocumentCommercial = :typeDocumentCommercial')
  768.                       ->setParameters(['recherche' => $recherche'cond' => $cond'typeDocumentCommercial' => TypeDocumentCommercial::BP])
  769.                       ->setFirstResult(0)
  770.                       ->setMaxResults($maxResults)
  771.                       ->orderBy('p.date''DESC');
  772.         $res   $query->getQuery()->getResult();
  773.         return $res;
  774.     }
  775.     public function findBpAafficher()
  776.     {
  777.         $dateDebut = new Datetime();
  778.         $dateDebut->setTime(000);
  779.         $dateFin = clone $dateDebut;
  780.         $dateFin->add(new DateInterval('P1D'));
  781.         $query $this->createQueryBuilder('p')
  782.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial and p.dateBon >= :dateDebut and p.dateBon < :dateFin and (p.visible = 0 or p.visible is null)')
  783.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BP'dateDebut' => $dateDebut->format("Y-m-d H:i:s"), 'dateFin' => $dateFin->format("Y-m-d H:i:s")])
  784.                       ->orderBy('p.date''DESC');
  785.         return $query;
  786.     }
  787.     public function getRechercheBonLivraison($recherche$maxResults 500)
  788.     {
  789.         $cond $recherche "%";
  790.         if (strpos($cond"*") !== false) {
  791.             //$cond = str_replace("*","%",$cond);
  792.         }
  793.         $query $this->createQueryBuilder('p')
  794.                       ->leftJoin(VehiculeClient::class, 'v''with''p.vehiculesClient = v.id')
  795.                       ->where('(p.reference LIKE :cond or p.referenceMarketPlace LIKE :cond or v.immatriculation = :recherche) AND p.typeDocumentCommercial = :typeDocumentCommercial')
  796.                       ->setParameters(['recherche' => $recherche'cond' => $cond'typeDocumentCommercial' => TypeDocumentCommercial::BL])
  797.                       ->setFirstResult(0)
  798.                       ->setMaxResults($maxResults)
  799.                       ->orderBy('p.date''DESC');
  800.         $res   $query->getQuery()->getResult();
  801.         return $res;
  802.     }
  803.     public function getRechercheOrdreRep($recherche$maxResults 500)
  804.     {
  805.         $cond $recherche "%";
  806.         $cond "%" $recherche "%";
  807.         if (strpos($cond"*") !== false) {
  808.             //$cond = str_replace("*","%",$cond);
  809.         }
  810.         $query $this->createQueryBuilder('p')
  811.                       ->leftJoin(VehiculeClient::class, 'v''with''p.vehiculesClient = v.id')
  812.                       ->where(
  813.                           '(p.reference LIKE :cond or v.libelle LIKE :cond or v.immatriculation LIKE :cond or v.numSerie LIKE :cond) AND p.typeDocumentCommercial = :typeDocumentCommercial'
  814.                       )
  815.                       ->setParameters(['cond' => $cond'typeDocumentCommercial' => TypeDocumentCommercial::ORDREREPARATION])
  816.                       ->setFirstResult(0)
  817.                       ->setMaxResults($maxResults)
  818.                       ->orderBy('p.date''DESC');
  819.         $res   $query->getQuery()->getResult();
  820.         return $res;
  821.     }
  822.     public function getRechercheCommande($recherche$maxResults 25$parametres = [])
  823.     {
  824.         //print_r($parametres);
  825.         $cond $recherche "%";
  826.         if (strpos($cond"*") !== false) {
  827.             $cond str_replace("*""%"$cond);
  828.         }
  829.         $where      '(p.reference LIKE :cond or p.referenceMarketPlace LIKE :cond or v.immatriculation = :recherche) AND p.typeDocumentCommercial = :typeDocumentCommercial';
  830.         $parameters = ['recherche' => $recherche'cond' => $cond'typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE];
  831.         $query      $this->createQueryBuilder('p');
  832.         if (array_key_exists('articles'$parametres) && count($parametres['articles'])) {
  833.             $query
  834.                 ->leftJoin('p.articleCommande''ac')
  835.                 ->leftJoin('ac.article''a');
  836.             $where                  .= ' AND a.id IN (:articles) AND ac.ralClient > 0';
  837.             $parameters['articles'] = $parametres['articles'];
  838.         }
  839.         if (array_key_exists('archive_recherche'$parametres)) {
  840.             $query
  841.                 ->leftJoin(VehiculeClient::class, 'v''with''p.vehiculesClient = v.id')
  842.                 ->where('(p.reference LIKE :cond or p.referenceMarketPlace LIKE :cond or v.immatriculation = :recherche) AND p.typeDocumentCommercial = :typeDocumentCommercial')
  843.                 ->setParameters(['recherche' => $recherche'cond' => $cond'typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE])
  844.                 ->setFirstResult(0)
  845.                 ->setMaxResults($maxResults)
  846.                 ->orderBy('p.date''DESC');
  847.         } else {
  848.             $query $this->createQueryBuilder('p')
  849.                           ->leftJoin(VehiculeClient::class, 'v''with''p.vehiculesClient = v.id')
  850.                           ->where($where)
  851.                           ->setParameters($parameters)
  852.                           ->setFirstResult(0)
  853.                           ->setMaxResults($maxResults)
  854.                           ->orderBy('p.date''DESC');
  855.         }
  856.         $res $query->getQuery()->getResult();
  857.         return $res;
  858.     }
  859.     public function getDerniereCommandeV2($typeDocumentCommercial)
  860.     {
  861.         $query $this->createQueryBuilder('p')
  862.             //->select("CONCAT(CONCAT(p.prenom, ' '), p.nom) as libelle, p.id,p.telephone")
  863.                       ->where('p.typeDocumentCommercial = :type and p.visible=1 and p.origineSaisie IN (:origineSaisie)')
  864.                       ->setFirstResult(0)
  865.                       ->setMaxResults(5)
  866.                       ->orderBy('p.date''DESC')
  867.                       ->setParameters(['type' => $typeDocumentCommercial'origineSaisie' => ["commande""devis""bp"]]);
  868.         $res   $query->getQuery()->getResult();
  869.         return $res;
  870.     }
  871.     public function getDerniereCommande($typeDocumentCommercial)
  872.     {
  873.         $query $this->createQueryBuilder('p')
  874.             //->select("CONCAT(CONCAT(p.prenom, ' '), p.nom) as libelle, p.id,p.telephone")
  875.                       ->where('p.typeDocumentCommercial = :type and p.visible=1')
  876.                       ->setFirstResult(0)
  877.                       ->setMaxResults(5)
  878.                       ->orderBy('p.date''DESC')
  879.                       ->setParameters(['type' => $typeDocumentCommercial]);
  880.         $res   $query->getQuery()->getResult();
  881.         return $res;
  882.     }
  883.     public function getTotalVente($date)
  884.     {
  885.         $dateDebut $date->format("Y-m-d") . " 00:00:00";
  886.         $dateFin   $date->format("Y-m-d") . " 23:59:59";
  887.         $query $this->createQueryBuilder('p')
  888.             //->select("SUM(p.total+p.fraisPortSupplementaire+p.totalFraisPort) as total")
  889.                       ->select("SUM(COALESCE(p.total,0)+COALESCE(p.fraisPortSupplementaire,0)+COALESCE(p.totalFraisPort,0)) as total")
  890.                       ->where(
  891.                           'p.dateSuppression IS NULL and p.date >= :dateDebut and p.date <= :dateFin and p.statutCommande NOT IN (:status) AND p.typeDocumentCommercial = :typeDocumentCommercial'
  892.                       )
  893.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE'dateDebut' => $dateDebut'dateFin' => $dateFin'status' => [678]]);
  894.         $total $query->getQuery()->getSingleScalarResult();
  895.         //print_r($total);
  896.         //echo "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$";
  897.         $sql "
  898.             SELECT SUM(COALESCE(c.total,0)+COALESCE(c.frais_port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total
  899.             FROM `commerciale__commande` as c
  900.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  901.             LEFT JOIN client__client as u on u.id = c.client_id
  902.             WHERE
  903.             (
  904.             (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND c.multi_bl != 1 AND c.date >= '" $dateDebut "' AND c.date <= '" $dateFin "')
  905.             OR
  906.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND c.date >= '" $dateDebut "' AND c.date <= '" $dateFin "')
  907.             OR
  908.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  909.             AND f.statut_commande_id != 23
  910.             and
  911.             f.date >= '" $dateDebut "' AND f.date <= '" $dateFin "'
  912.             )
  913.             )
  914.         ";
  915.         //echo "<br/><br/><br/>";
  916.         $sql trim($sql);
  917.         $em     $this->getEntityManager();
  918.         $stmt   $em->getConnection()->prepare($sql);
  919.         $totaux $stmt->executeQuery()->fetchAllAssociative();
  920.         return $totaux["0"]["total"];
  921.         //print_r($totaux);
  922.         //echo "#############################";
  923.         //return $total;
  924.     }
  925.     public function getPremiereLivraison($commande)
  926.     {
  927.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  928.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 5"ordre" => [0]]);
  929.         $query $this->createQueryBuilder('p')
  930.                       ->select("p.id")
  931.                       ->where('p.bp = :bp AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL and p.statutCommande NOT IN (:statutCommande)')
  932.                       ->setFirstResult(0)
  933.                       ->setMaxResults(1)
  934.                       ->orderBy('p.id''ASC')
  935.                       ->setParameters(['bp' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::BL'statutCommande' => $liste_statut]);
  936.         $res   $query->getQuery()->getOneOrNullResult();
  937.         return $res;
  938.     }
  939.     public function getPremierBonPrepa($commande)
  940.     {
  941.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  942.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 4"ordre" => [0]]);
  943.         $query $this->createQueryBuilder('p')
  944.                       ->select("p.id")
  945.                       ->where('p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL and p.statutCommande NOT IN (:statutCommande)')
  946.                       ->setFirstResult(0)
  947.                       ->setMaxResults(1)
  948.             //->orderBy('p.date', 'DESC')
  949.                       ->orderBy('p.id''ASC')
  950.                       ->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::BP'statutCommande' => $liste_statut]);
  951.         $res   $query->getQuery()->getOneOrNullResult();
  952.         return $res;
  953.     }
  954.     public function getDernierBonPrepa($commande)
  955.     {
  956.         $query $this->createQueryBuilder('p')
  957.                       ->select("p.id")
  958.                       ->where('p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL')
  959.                       ->setFirstResult(0)
  960.                       ->setMaxResults(1)
  961.                       ->orderBy('p.id''DESC')
  962.                       ->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::BP]);
  963.         $res   $query->getQuery()->getOneOrNullResult();
  964.         return $res;
  965.     }
  966.     public function getDernierBonPrepaAtelier($commande)
  967.     {
  968.         $query $this->createQueryBuilder('p')
  969.                       ->select("p.id")
  970.                       ->where('p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL')
  971.                       ->setFirstResult(0)
  972.                       ->setMaxResults(1)
  973.                       ->orderBy('p.id''DESC')
  974.                       ->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::BPATELIER]);
  975.         $res   $query->getQuery()->getOneOrNullResult();
  976.         return $res;
  977.     }
  978.     public function getTotalBonPreparationAtelier($commande)
  979.     {
  980.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  981.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 11"ordre" => [0]]);
  982.         $query $this->createQueryBuilder('p')
  983.                       ->select("COUNT(p.id) as total")
  984.                       ->where('p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande NOT IN (:statutCommande)')
  985.                       ->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::BPATELIER'statutCommande' => $liste_statut]);
  986.         $res   $query->getQuery()->getOneOrNullResult();
  987.         return $res;
  988.     }
  989.     public function getTotalBonCommande($commande)
  990.     {
  991.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  992.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 4"ordre" => [0]]);
  993.         $query $this->createQueryBuilder('p')
  994.                       ->select("COUNT(p.id) as total")
  995.                       ->where('p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande NOT IN (:statutCommande)')
  996.                       ->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::BP'statutCommande' => $liste_statut]);
  997.         $res   $query->getQuery()->getOneOrNullResult();
  998.         return $res;
  999.     }
  1000.     public function getDocumentClientAmodifier(Commande $commande)
  1001.     {
  1002.         /*
  1003.         $repo_statut_document = $this->getEntityManager()->getRepository('DTCGestionComercialeBundle:StatutCommande');
  1004.         $liste_statut = $repo_statut_document->findBy(array("documentCommercial"=>4,"ordre"=>array(0)));
  1005.              *
  1006.              */
  1007.         $where 'p.commande = :commande AND p.dateSuppression IS NULL';
  1008.         if (is_object($commande->getCommande())) {
  1009.             $commande $commande->getCommande();
  1010.             $where    '(p.id= :commande OR p.commande = :commande) AND p.dateSuppression IS NULL';
  1011.         }
  1012.         $query $this->createQueryBuilder('p')
  1013.                       ->where($where)
  1014.                       ->setParameters(['commande' => $commande->getId()]);
  1015.         return $query;
  1016.     }
  1017.     public function getTotalBonPreparationByCommandes($commandes)
  1018.     {
  1019.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  1020.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 4"ordre" => [0]]);
  1021.         $query $this->createQueryBuilder('p')
  1022.                       ->select("COUNT(p.id) as total")
  1023.                       ->where('p.commande IN (:commandes) AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande NOT IN (:statutCommande)')
  1024.                       ->setParameters(['commandes' => $commandes'typeDocumentCommercial' => TypeDocumentCommercial::BP'statutCommande' => $liste_statut]);
  1025.         $res   $query->getQuery()->getOneOrNullResult();
  1026.         return $res;
  1027.     }
  1028.     public function getTotalBonLivraison($commande)
  1029.     {
  1030.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  1031.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 5"ordre" => [0]]);
  1032.         $query $this->createQueryBuilder('p')
  1033.                       ->select("COUNT(p.id) as total")
  1034.                       ->where('p.bp = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande NOT IN (:statutCommande)')
  1035.                       ->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::BL'statutCommande' => $liste_statut]);
  1036.         $res   $query->getQuery()->getOneOrNullResult();
  1037.         return $res;
  1038.     }
  1039.     public function getTotalBonLivraisonSurCommande($commande)
  1040.     {
  1041.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  1042.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 5"ordre" => [0]]);
  1043.         $query $this->createQueryBuilder('p')
  1044.                       ->select("COUNT(p.id) as total")
  1045.                       ->where('p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande NOT IN (:statutCommande)')
  1046.                       ->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::BL'statutCommande' => $liste_statut]);
  1047.         $res   $query->getQuery()->getOneOrNullResult();
  1048.         return $res;
  1049.     }
  1050.     public function getBonsLivraison($commande)
  1051.     {
  1052.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  1053.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 5"ordre" => [0]]);
  1054.         $query $this->createQueryBuilder('p')
  1055.             //->select("p.id")
  1056.                       ->where('p.bp = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande NOT IN (:statutCommande)')
  1057.                       ->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::BL'statutCommande' => $liste_statut]);
  1058.         $res   $query->getQuery()->getResult();
  1059.         return $res;
  1060.     }
  1061.     public function getTotalOrdreReparation($commande ""$moisRaz '')
  1062.     {
  1063.         if (!is_object($commande)) {
  1064.             $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  1065.             $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 3"ordre" => [01234]]);
  1066.             if ($moisRaz != "") {
  1067.                 //Y-m-d H:i:s
  1068.                 $date = new Datetime();
  1069.                 if ($moisRaz == '99') {
  1070.                     $date_mois = new Datetime($date->format('Y') . '-' $date->format('m') . '-01');
  1071.                 } else {
  1072.                     $date_mois = new Datetime($date->format('Y') . '-' $moisRaz '-01');
  1073.                 }
  1074.                 $query $this->createQueryBuilder('p')
  1075.                               ->select("COUNT(p.id) as total")
  1076.                               ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande IN (:statutCommande)  AND p.date >= :date')
  1077.                               ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::ORDREREPARATION'statutCommande' => $liste_statut'date' => $date_mois->format("Y-m-d 00:00:00")]);
  1078.             } else {
  1079.                 $query $this->createQueryBuilder('p')
  1080.                               ->select("COUNT(p.id) as total")
  1081.                               ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande IN (:statutCommande)')
  1082.                               ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::ORDREREPARATION'statutCommande' => $liste_statut]);
  1083.             }
  1084.         } else {
  1085.             $query $this->createQueryBuilder('p')
  1086.                           ->select("COUNT(p.id) as total")
  1087.                           ->where('p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial')
  1088.                           ->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::ORDREREPARATION]);
  1089.         }
  1090.         $res $query->getQuery()->getOneOrNullResult();
  1091.         return $res;
  1092.     }
  1093.     public function getProchainNumeroFacture($commande ""$moisRaz '')
  1094.     {
  1095.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  1096.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => $commande->getTypeDocumentCommercial(), "ordre" => [1]]);
  1097.         if ($moisRaz != "") {
  1098.             //Y-m-d H:i:s
  1099.             $date = new Datetime();
  1100.             if ($moisRaz == '99') {
  1101.                 $date_mois = new Datetime($date->format('Y') . '-' $date->format('m') . '-01');
  1102.             } else {
  1103.                 $date_mois = new Datetime($date->format('Y') . '-' $moisRaz '-01');
  1104.             }
  1105.             /*
  1106.                 $query = $this->createQueryBuilder('p')
  1107.                     ->select("p.numeroReference")
  1108.                     ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.date >= :date AND p.statutCommande IN (:statutCommande) AND p.numeroReference IS NOT NULL AND p.basculeComptabilite = :basculeComptabilite')
  1109.                     ->setParameters(array('typeDocumentCommercial'=>$commande->getTypeDocumentCommercial(),'date'=>$date_mois->format("Y-m-d H:i:s"),'statutCommande'=>$liste_statut,"basculeComptabilite"=>false))
  1110.                     ->orderBy('p.numeroReference', 'ASC')
  1111.                     ;
  1112.                 */
  1113.             $query $this->createQueryBuilder('p')
  1114.                 //->select("p.numeroReference")
  1115.                           ->select("MAX(p.numeroReference)")
  1116.                           ->where(
  1117.                               'p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.date >= :date AND p.statutCommande IN (:statutCommande) AND p.numeroReference IS NOT NULL'
  1118.                           )
  1119.                           ->setParameters(
  1120.                               [
  1121.                                   'typeDocumentCommercial' => $commande->getTypeDocumentCommercial(),
  1122.                                   'date'                   => $date_mois->format("Y-m-d H:i:s"),
  1123.                                   'statutCommande'         => $liste_statut,
  1124.                               ]
  1125.                           )//->orderBy('p.numeroReference', 'ASC')
  1126.             ;
  1127.         } else {
  1128.             /*
  1129.                 $query = $this->createQueryBuilder('p')
  1130.                     ->select("p.numeroReference")
  1131.                     ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande) AND p.numeroReference IS NOT NULL AND p.basculeComptabilite = :basculeComptabilite')
  1132.                     ->setParameters(array('typeDocumentCommercial'=>$commande->getTypeDocumentCommercial(),'statutCommande'=>$liste_statut,"basculeComptabilite"=>false))
  1133.                     ->orderBy('p.numeroReference', 'ASC')
  1134.                     ;
  1135.                 */
  1136.             $query $this->createQueryBuilder('p')
  1137.                 //->select("p.numeroReference")
  1138.                           ->select("MAX(p.numeroReference)")
  1139.                           ->where(
  1140.                               'p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande) AND p.numeroReference IS NOT NULL'
  1141.                           )
  1142.                           ->setParameters(['typeDocumentCommercial' => $commande->getTypeDocumentCommercial(), 'statutCommande' => $liste_statut]
  1143.                           )//->orderBy('p.numeroReference', 'ASC')
  1144.             ;
  1145.         }
  1146.         $temp_numero    "0";
  1147.         $numero         1;
  1148.         $dernier_numero 1;
  1149.         //$res = $query->getQuery()->getResult();
  1150.         $res $query->getQuery()->getSingleScalarResult();
  1151.         $res++;
  1152.         return $res;
  1153.         /*
  1154.         echo "<div>".$commande->getTypeDocumentCommercial()."</div>";
  1155.         echo count($res);
  1156.         exit;
  1157.         */
  1158.         if (count($res) > 0) {
  1159.             for ($i 0$i count($res); $i++) {
  1160.                 if ($i == 0) {
  1161.                     $dernier_numero $res[$i]['numeroReference'] + 1;
  1162.                     $temp_numero    $res[$i]['numeroReference'];
  1163.                 } else {
  1164.                     if (($temp_numero 1) < $res[$i]['numeroReference']) {
  1165.                         return ($temp_numero 1);
  1166.                     } else {
  1167.                         $temp_numero $res[$i]['numeroReference'];
  1168.                     }
  1169.                 }
  1170.             }
  1171.         }
  1172.         return $temp_numero 1;
  1173.     }
  1174.     public function getMontantTotalFacture($commande)
  1175.     {
  1176.         $total                0;
  1177.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  1178.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 6"ordre" => [1]]);
  1179.         if (is_object($commande)) {
  1180.             $query $this->createQueryBuilder('p')->select("SUM(COALESCE(p.totalTtc,0)) as total");
  1181.             $where 'p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  1182.             $query->where($where);
  1183.             $query->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::FACTURE'statutCommande' => $liste_statut]);
  1184.             $res   $query->getQuery()->getOneOrNullResult();
  1185.             $total $res["total"];
  1186.         }
  1187.         return $total;
  1188.     }
  1189.     public function getTotalFacture($commande ""$moisRaz '')
  1190.     {
  1191.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  1192.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 6"ordre" => [1]]);
  1193.         if (!is_object($commande)) {
  1194.             if ($moisRaz != "") {
  1195.                 //Y-m-d H:i:s
  1196.                 $date = new Datetime();
  1197.                 if ($moisRaz == '99') {
  1198.                     $date_mois = new Datetime($date->format('Y') . '-' $date->format('m') . '-01');
  1199.                 } else {
  1200.                     $date_mois = new Datetime($date->format('Y') . '-' $moisRaz '-01');
  1201.                 }
  1202.                 $query $this->createQueryBuilder('p')
  1203.                               ->select("COUNT(p.id) as total")
  1204.                               ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.date >= :date AND p.statutCommande IN (:statutCommande)')
  1205.                               ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::FACTURE'date' => $date_mois->format("Y-m-d H:i:s"), 'statutCommande' => $liste_statut]);
  1206.             } else {
  1207.                 $query $this->createQueryBuilder('p')
  1208.                               ->select("COUNT(p.id) as total")
  1209.                               ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)')
  1210.                               ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::FACTURE'statutCommande' => $liste_statut]);
  1211.             }
  1212.         } else {
  1213.             $query $this->createQueryBuilder('p')
  1214.                           ->select("COUNT(p.id) as total");
  1215.             $where 'p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  1216.             if ($commande->getTypeDocumentCommercial() == TypeDocumentCommercial::BP) {
  1217.                 $where 'p.bp = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  1218.             } elseif ($commande->getTypeDocumentCommercial() == TypeDocumentCommercial::BL) {
  1219.                 $query->join('p.bls''bl');
  1220.                 $where 'bl = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  1221.             }
  1222.             $query->where($where)
  1223.                   ->setParameters(['commande' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::FACTURE'statutCommande' => $liste_statut]);
  1224.         }
  1225.         $res $query->getQuery()->getOneOrNullResult();
  1226.         return $res;
  1227.     }
  1228.     public function getTotalOrEnCours()
  1229.     {
  1230.         $query $this->createQueryBuilder('p')
  1231.                       ->select("COUNT(p.id) as total")
  1232.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande IN (:statutCommande) AND p.dateSuppression IS NULL')
  1233.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::ORDREREPARATION'statutCommande' => [1415]]);
  1234.         $res   $query->getQuery()->getOneOrNullResult();
  1235.         return $res;
  1236.     }
  1237.     public function getTotalCommandeInternetSansTransporteur()
  1238.     {
  1239.         $query $this->createQueryBuilder('p')
  1240.             //->select("COUNT(p.id) as total")
  1241.                       ->where(
  1242.                 'p.compteMarketPlace is not null and p.transporteur is null AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.visible = :visible'
  1243.             )
  1244.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE'statutCommande' => 9'visible' => 1]);
  1245.         $res   $query->getQuery()->getResult();
  1246.         return $res;
  1247.     }
  1248.     public function getBpAPreparerAttente()
  1249.     {
  1250.         $query $this->createQueryBuilder('p')
  1251.             //->select("COUNT(p.id) as total")
  1252.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.visible = :visible')
  1253.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BP'statutCommande' => 32'visible' => 1]);
  1254.         $res   $query->getQuery()->getResult();
  1255.         return $res;
  1256.     }
  1257.     public function getBpAEmballerAttente()
  1258.     {
  1259.         $query $this->createQueryBuilder('p')
  1260.             //->select("COUNT(p.id) as total")
  1261.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.visible = :visible')
  1262.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BP'statutCommande' => 33'visible' => 1]);
  1263.         $res   $query->getQuery()->getResult();
  1264.         return $res;
  1265.     }
  1266.     public function totalBpAnomalies()
  1267.     {
  1268.         /*
  1269.                 $parameters["typeDocumentCommercial"]=4;
  1270.                 $parameters["statutCommande"]=array(32,33);
  1271.                 $where = '(x.typeDocumentCommercial = :typeDocumentCommercial and x.statutCommande IN (:statutCommande))';
  1272.                 $where .= ' OR (x.transporteur is null and x.statutCommande = :statutCommandeAccepte and x.compteMarketPlace is not null)';
  1273.                 $where .= ' OR (x.raisonAnomalie is not null)';
  1274.                 $parameters["statutCommandeAccepte"]=9;
  1275.                 */
  1276.         $parameters                           = [];
  1277.         $parameters["typeDocumentCommercial"] = 4;
  1278.         $parameters["statutEmballe"]         = 31;
  1279.         $parameters["statutCommande"]         = [3233];
  1280.         $where                                '(x.typeDocumentCommercial = :typeDocumentCommercial and x.statutCommande IN (:statutCommande))';
  1281.         $where                               .= ' OR (x.transporteur is null and x.statutCommande = :statutCommandeAccepte and x.compteMarketPlace is not null)';
  1282.         $where                               .= ' OR (x.statutCommande != :statutEmballe and x.raisonAnomalie is not null and x.typeDocumentCommercial = :typeDocumentCommercial)';
  1283.         $parameters["statutCommandeAccepte"] = 9;
  1284.         $query $this->createQueryBuilder('x')
  1285.                       ->select("COUNT(x.id) as total")
  1286.                       ->where($where)
  1287.                       ->setParameters($parameters);
  1288.         $res $query->getQuery()->getOneOrNullResult();
  1289.         return $res["total"];
  1290.     }
  1291.     public function getTotalBpAEmballer()
  1292.     {
  1293.         $query $this->createQueryBuilder('p')
  1294.                       ->select("COUNT(p.id) as total")
  1295.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.visible = :visible')
  1296.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BP'statutCommande' => 12'visible' => 1]);
  1297.         $res   $query->getQuery()->getOneOrNullResult();
  1298.         return $res;
  1299.     }
  1300.     public function getTotalBpEnCours()
  1301.     {
  1302.         $query $this->createQueryBuilder('p')
  1303.                       ->select("COUNT(p.id) as total")
  1304.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.visible = :visible')
  1305.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BP'statutCommande' => 10'visible' => 1]);
  1306.         $res   $query->getQuery()->getOneOrNullResult();
  1307.         return $res;
  1308.     }
  1309.     public function getProchainBpEmballerParEan($lp$article$quantite)
  1310.     {
  1311.         //echo "<div>LP:".$lp->getId()."</div>";
  1312.         //echo "<div>QTE".$quantite."</div>";
  1313.         //echo "<div>ART:".$article->getReference()."</div>";
  1314.         $query $this->createQueryBuilder('p')
  1315.             //->select("COUNT(p.id) as total")
  1316.                       ->join('p.articleCommande''ac')
  1317.             //->leftJoin('DTCGestionComercialeBundle:ArticleCommande', 'ac', 'with', 'ac.commande = p.id')
  1318.             //->leftJoin('DTCRangementsBundle:ListePreparation', 'lp', 'with', 'lp.id = p.id')
  1319.                       ->join(ListePreparationCommande::class, 'lpc''with''lpc.bp = p.id')
  1320.                       ->where(
  1321.                           'lpc.listePreparation = :lp
  1322.                             and
  1323.                             p.typeDocumentCommercial = :typeDocumentCommercial
  1324.                             AND
  1325.                             p.dateSuppression IS NULL
  1326.                             and
  1327.                             ac.article = :article
  1328.                             and
  1329.                             ac.quantite >= :quantite
  1330.                             and
  1331.                             p.statutCommande IN (:statutCommande)
  1332.                             '
  1333.                       )
  1334.                       ->setParameters(['statutCommande' => [1233], 'typeDocumentCommercial' => TypeDocumentCommercial::BP"article" => $article"quantite" => $quantite"lp" => $lp])
  1335.                       ->orderBy('p.dateBon''ASC')
  1336.                       ->setMaxResults(1);;
  1337.         $res $query->getQuery()->getOneOrNullResult();
  1338.         return $res;
  1339.     }
  1340.     public function getProchainBpAPreparer()
  1341.     {
  1342.         $query $this->createQueryBuilder('p')
  1343.             //->select("COUNT(p.id) as total")
  1344.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.visible = :visible')
  1345.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BP'statutCommande' => 10'visible' => 1])
  1346.                       ->orderBy('p.dateBon''ASC')
  1347.                       ->setMaxResults(1);;
  1348.         $res $query->getQuery()->getOneOrNullResult();
  1349.         return $res;
  1350.     }
  1351.     public function getProchainBpAEmballer($ean)
  1352.     {
  1353.         $query $this->createQueryBuilder('p')
  1354.             //->select("COUNT(p.id) as total")
  1355.                       ->where(
  1356.                 'p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.visible = :visible AND c.ean LIKE :ean'
  1357.             )
  1358.                       ->leftJoin('p.commandeCaisses''cc')
  1359.                       ->leftJoin('cc.caisse''c')
  1360.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BP'statutCommande' => 12'visible' => 1'ean' => $ean])//préparé
  1361.                       ->orderBy('p.dateBon''ASC')
  1362.                       ->setMaxResults(1);;
  1363.         $res $query->getQuery()->getOneOrNullResult();
  1364.         return $res;
  1365.     }
  1366.     public function getTotalDevisEnCours()
  1367.     {
  1368.         $repo_statut_commande $this->getEntityManager()->getRepository(StatutCommande::class);
  1369.         $liste_statut         $repo_statut_commande->findBy(["documentCommercial" => 1"ordre" => [23]]);
  1370.         $query $this->createQueryBuilder('p')
  1371.                       ->select("COUNT(p.id) as total")
  1372.                       ->where(
  1373.                           'p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.commande IS NULL and p.visible = :visible and (p.perdu = :perdu or p.perdu is null)'
  1374.                       )
  1375.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::DEVIS'visible' => 1"perdu" => "0",]);
  1376.         $res   $query->getQuery()->getOneOrNullResult();
  1377.         return $res;
  1378.     }
  1379.     public function getTotalDevisAPurger()
  1380.     {
  1381.         $repo_statut_commande $this->getEntityManager()->getRepository(StatutCommande::class);
  1382.         $date                 = (new DateTime())->modify('-3 months');
  1383.         $query $this->createQueryBuilder('p')
  1384.                       ->select("p.id ")
  1385.             //->leftjoin('p.commande', 'c')
  1386.             //->leftjoin('c.statutCommande', 'sc')
  1387.                       ->where('p.typeDocumentCommercial IN (:typeDocumentCommercial) AND p.commande IS NULL AND p.date <= :date')
  1388.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::DEVIS'date' => $date]);
  1389.         $res   $query->getQuery()->getResult();
  1390.         return $res;
  1391.     }
  1392.     public function getTotalCommandeEnCours($internet 0)
  1393.     {
  1394.         //x.typeDocumentCommercial = :typeDocumentCommercial AND (x.origine is null OR x.valideManuellement = 1) AND x.statutCommande IN (:statuts)
  1395.         /*
  1396.         [0] => 9
  1397.         [1] => 2
  1398.         [2] => 1
  1399.         [3] => 6
  1400.         [4] => 28
  1401.             */
  1402.         $where_internet " and x.referenceMarketPlace IS NULL";
  1403.         if ($internet == 1) {
  1404.             $where_internet " and x.referenceMarketPlace IS NOT NULL";
  1405.         }
  1406.         $statutCommande = [921628];
  1407.         $statutCommande = [9];
  1408.         $query          $this->createQueryBuilder('x')
  1409.                                ->select("COUNT(x.id) as total")
  1410.                                ->where('x.typeDocumentCommercial = :typeDocumentCommercial AND x.statutCommande IN (:statuts) ' $where_internet)
  1411.                                ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE'statuts' => $statutCommande]);
  1412.         $res            $query->getQuery()->getOneOrNullResult();
  1413.         return $res;
  1414.         /*
  1415.         $statutCommande = array(7,6);
  1416.          $query = $this->createQueryBuilder('p')
  1417.             ->select("COUNT(p.id) as total")
  1418.             ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande NOT IN (:statutCommande) AND p.dateSuppression IS NULL')
  1419.             ->setParameters(array('typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE,'statutCommande'=>$statutCommande))
  1420.             ;
  1421.         $res = $query->getQuery()->getOneOrNullResult();
  1422.         return $res;
  1423.         */
  1424.     }
  1425.     public function getCa($debut$fin$compteMarketPlace "")
  1426.     {
  1427.         $query $this->createQueryBuilder('p')
  1428.                       ->select("SUM(COALESCE(p.total,0)+COALESCE(p.fraisPortSupplementaire,0)+COALESCE(p.totalFraisPort,0)) as total")
  1429.             //->select("SUM(p.total+p.fraisPortSupplementaire+p.totalFraisPort) as total")
  1430.             /*
  1431.             ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.date >= :debut AND p.date <= :fin')
  1432.             ->setParameters(array('typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE,'debut'=>$debut,'fin'=>$fin))
  1433.                       *
  1434.                       */
  1435.                       ->where('p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.date >= :debut AND p.date <= :fin')
  1436.                       ->setParameters(['statutCommande' => 22'debut' => $debut'fin' => $fin])//->setMaxResults(1)
  1437.         ;
  1438.         $res   $query->getQuery()->getOneOrNullResult();
  1439.         $join  "";
  1440.         $where "";
  1441.         if ($compteMarketPlace == "") {
  1442.             $where .= " and u.compte_market_place_id is null";
  1443.         } else {
  1444.             $where .= " and u.compte_market_place_id = " $compteMarketPlace;
  1445.         }
  1446.         $sql "
  1447.             SELECT SUM(COALESCE(c.total,0)+COALESCE(c.frais_port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total,SUM(COALESCE(c.marge,0)) as total_marge
  1448.             FROM `commerciale__commande` as c
  1449.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  1450.             LEFT JOIN client__client as u on u.id = c.client_id
  1451.             WHERE
  1452.             (
  1453.             (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND (c.multi_bl != 1 or c.multi_bl is null) AND c.date >= '" $debut->format(
  1454.                 "Y-m-d H:i:s"
  1455.             ) . "' AND c.date <= '" $fin->format("Y-m-d H:i:s") . "')
  1456.             OR
  1457.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND (c.multi_bl != 1 or c.multi_bl is null) AND c.date >= '" $debut->format(
  1458.                 "Y-m-d H:i:s"
  1459.             ) . "' AND c.date <= '" $fin->format("Y-m-d H:i:s") . "')
  1460.             OR
  1461.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25 AND c.facture_id > 0 AND f.multi_bl = 1
  1462.             AND f.statut_commande_id != 23
  1463.             and
  1464.             f.date >= '" $debut->format("Y-m-d H:i:s") . "' AND f.date <= '" $fin->format("Y-m-d H:i:s") . "'
  1465.             )
  1466.             )
  1467.         ";
  1468.         /*
  1469.         $sql = "
  1470.             SELECT SUM(COALESCE(c.total,0)+COALESCE(c.frais_port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total,SUM(COALESCE(c.marge,0)) as total_marge
  1471.             FROM `commerciale__commande` as c
  1472.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  1473.             LEFT JOIN client__client as u on u.id = c.client_id
  1474.             WHERE
  1475.             (
  1476.             (c.type_document_commercial_id = ".TypeDocumentCommercial::AVOIR." AND c.statut_commande_id != 19  AND (c.multi_bl != 1 or c.multi_bl is null) AND c.date >= '".$debut->format("Y-m-d H:i:s")."' AND c.date <= '".$fin->format("Y-m-d H:i:s")."')
  1477.             OR
  1478.             (c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23  AND (c.multi_bl != 1 or c.multi_bl is null) AND c.date >= '".$debut->format("Y-m-d H:i:s")."' AND c.date <= '".$fin->format("Y-m-d H:i:s")."')
  1479.             OR
  1480.             (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 25 AND c.facture_id > 0 AND f.multi_bl = 1
  1481.             AND f.statut_commande_id != 23
  1482.             and
  1483.             f.date >= '".$debut->format("Y-m-d H:i:s")."' AND f.date <= '".$fin->format("Y-m-d H:i:s")."'
  1484.             )
  1485.             )
  1486.         ";*/
  1487.         $sql .= $where;
  1488.         $sql trim($sql);
  1489.         if ($compteMarketPlace == "") {
  1490.             //    echo $sql."<br/><br/><br/>";
  1491.         }
  1492.         $em     $this->getEntityManager();
  1493.         $stmt   $em->getConnection()->prepare($sql);
  1494.         $totaux $stmt->executeQuery()->fetchAllAssociative();
  1495.         return $totaux["0"];
  1496.         //return $res;
  1497.     }
  1498.     public function getCaComptoir($debut$fin)
  1499.     {
  1500.         $query $this->createQueryBuilder('p')
  1501.                       ->select("SUM(COALESCE(p.total,0)+COALESCE(p.fraisPortSupplementaire,0)+COALESCE(p.totalFraisPort,0)) as total")
  1502.             //->select("SUM(p.total+p.fraisPortSupplementaire+p.totalFraisPort) as total")
  1503.                       ->join('p.commande''c')
  1504.             /*
  1505.             ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.date >= :debut AND p.date <= :fin')
  1506.             ->setParameters(array('typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE,'debut'=>$debut,'fin'=>$fin))
  1507.                       *
  1508.                       */
  1509.                       ->where(
  1510.                 'p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.date >= :debut AND p.date <= :fin and c.typeDocumentCommercial = :typeDocumentCommercial'
  1511.             )
  1512.                       ->setParameters(['statutCommande' => 22'debut' => $debut'fin' => $fin'typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE])//->setMaxResults(1)
  1513.         ;
  1514.         $res   $query->getQuery()->getOneOrNullResult();
  1515.         return $res;
  1516.     }
  1517.     public function getCaOr($debut$fin)
  1518.     {
  1519.         $query $this->createQueryBuilder('p')
  1520.                       ->select("SUM(COALESCE(p.total,0)+COALESCE(p.fraisPortSupplementaire,0)+COALESCE(p.totalFraisPort,0)) as total")
  1521.             //->select("SUM(p.total+p.fraisPortSupplementaire+p.totalFraisPort) as total")
  1522.                       ->join('p.commande''c')
  1523.             /*
  1524.             ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.date >= :debut AND p.date <= :fin')
  1525.             ->setParameters(array('typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE,'debut'=>$debut,'fin'=>$fin))
  1526.                       *
  1527.                       */
  1528.                       ->where(
  1529.                 'p.statutCommande = :statutCommande AND p.dateSuppression IS NULL AND p.date >= :debut AND p.date <= :fin and c.typeDocumentCommercial = :typeDocumentCommercial'
  1530.             )
  1531.                       ->setParameters(['statutCommande' => 22'debut' => $debut'fin' => $fin'typeDocumentCommercial' => TypeDocumentCommercial::ORDREREPARATION])//->setMaxResults(1)
  1532.         ;
  1533.         $res   $query->getQuery()->getOneOrNullResult();
  1534.         return $res;
  1535.     }
  1536.     public function listeCommandeEnCours()
  1537.     {
  1538.         $repo_statut_commande $this->getEntityManager()->getRepository(StatutCommande::class);
  1539.         $liste_statut         $repo_statut_commande->findBy(["documentCommercial" => 2"ordre" => [01]]);
  1540.         $query $this->createQueryBuilder('p')
  1541.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande NOT IN (:statutCommande) AND p.dateSuppression IS NULL AND p.visible = :visible')
  1542.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE'statutCommande' => $liste_statut'visible' => 1])
  1543.                       ->orderBy('p.dateMaj''DESC')//->setMaxResults(100)
  1544.         ;
  1545.         $res   $query->getQuery()->getResult();
  1546.         return $res;
  1547.     }
  1548.     public function listeCommandeEnCoursPourReception($articles = [])
  1549.     {
  1550.         $repo_statut_commande $this->getEntityManager()->getRepository(StatutCommande::class);
  1551.         //$liste_statut = $repo_statut_commande->findBy(array("documentCommercial"=>2,"ordre"=>array(0,6)));
  1552.         $liste_statut $repo_statut_commande->findBy(["documentCommercial" => 2"id" => [729629]]);
  1553.         $query $this->createQueryBuilder('p')
  1554.                       ->join('p.articleCommande''ac')
  1555.                       ->join('ac.article''a')
  1556.                       ->where(
  1557.                           'a.id IN (:idsarticles) AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande NOT IN (:statutCommande) AND p.dateSuppression IS NULL AND p.visible = :visible'
  1558.                       )
  1559.                       ->setParameters(['idsarticles' => $articles'typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE'statutCommande' => $liste_statut'visible' => 1])
  1560.                       ->orderBy('p.dateMaj''DESC')//->setMaxResults(2)
  1561.         ;
  1562.         $res   $query->getQuery()->getResult();
  1563.         return $res;
  1564.     }
  1565.     public function listeCommandeEnCours2()
  1566.     {
  1567.         $repo_statut_commande $this->getEntityManager()->getRepository(StatutCommande::class);
  1568.         $liste_statut         $repo_statut_commande->findBy(["documentCommercial" => 2"ordre" => [0610]]);
  1569.         $query $this->createQueryBuilder('p')
  1570.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande NOT IN (:statutCommande) AND p.dateSuppression IS NULL AND p.visible = :visible')
  1571.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE'statutCommande' => $liste_statut'visible' => 1])
  1572.                       ->orderBy('p.dateMaj''DESC')//->setMaxResults(100)
  1573.         ;
  1574.         $res   $query->getQuery()->getResult();
  1575.         return $res;
  1576.     }
  1577.     public function listeDevisEnCoursV2()
  1578.     {
  1579.         $query $this->createQueryBuilder('p')
  1580.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.visible = :visible')
  1581.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::DEVIS'visible' => 1])
  1582.                       ->orderBy('p.dateMaj''DESC')//->setMaxResults(100)
  1583.         ;
  1584.         $res   $query->getQuery()->getResult();
  1585.         return $res;
  1586.     }
  1587.     public function listeOrEnCours()
  1588.     {
  1589.         $repo_statut_commande $this->getEntityManager()->getRepository(StatutCommande::class);
  1590.         $liste_statut         $repo_statut_commande->findBy(["documentCommercial" => 3"ordre" => [23]]);
  1591.         $query $this->createQueryBuilder('p')
  1592.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande IN (:statutCommande) AND p.dateSuppression IS NULL')
  1593.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::ORDREREPARATION'statutCommande' => $liste_statut])
  1594.                       ->orderBy('p.date''ASC');
  1595.         $res   $query->getQuery()->getResult();
  1596.         return $res;
  1597.     }
  1598.     public function listeDevisEnCours()
  1599.     {
  1600.         $repo_statut_commande $this->getEntityManager()->getRepository(StatutCommande::class);
  1601.         $liste_statut         $repo_statut_commande->findBy(["documentCommercial" => 1"ordre" => [23]]);
  1602.         $query $this->createQueryBuilder('p')
  1603.             //->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande IN (:statutCommande) AND p.dateSuppression IS NULL')
  1604.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.commande IS NULL')
  1605.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::DEVIS]);
  1606.         $res   $query->getQuery()->getResult();
  1607.         return $res;
  1608.     }
  1609.     public function getTotalHeuresFactureesDsPeriode($dateDebut$dateFin$mecanicien null)
  1610.     {
  1611.         if ($mecanicien == null) {
  1612.             $query $this->createQueryBuilder('p')
  1613.                           ->select("SUM(COALESCE(ac.quantite,0)) as total")
  1614.                           ->join('p.articleCommande''ac')
  1615.                           ->join('ac.article''a')
  1616.                           ->where('p.date > :dateDebut and p.date < :dateFin AND p.typeDocumentCommercial IN (:typeDocumentCommercial) AND a.mainOeuvre = :mainOeuvre')
  1617.                           ->setParameters(['dateDebut' => $dateDebut'dateFin' => $dateFin'typeDocumentCommercial' => [TypeDocumentCommercial::COMMANDETypeDocumentCommercial::ORDREREPARATION], 'mainOeuvre' => true])
  1618.                           ->orderBy('p.date''DESC');
  1619.             $res   $query->getQuery()->getOneOrNullResult();
  1620.             return $res['total'];
  1621.         } else {
  1622.             $query $this->createQueryBuilder('p')
  1623.                           ->select("p.id, SUM(COALESCE(ac.quantite,0)) as total")
  1624.                           ->join('p.articleCommande''ac')
  1625.                           ->join('ac.article''a')
  1626.                           ->where('p.date > :dateDebut and p.date < :dateFin AND p.typeDocumentCommercial IN (:typeDocumentCommercial) AND a.mainOeuvre = :mainOeuvre')
  1627.                           ->setParameters(['dateDebut' => $dateDebut'dateFin' => $dateFin'typeDocumentCommercial' => [TypeDocumentCommercial::COMMANDETypeDocumentCommercial::ORDREREPARATION], 'mainOeuvre' => true])
  1628.                           ->orderBy('p.date''DESC');
  1629.             $res   $query->getQuery()->getResult();
  1630.             $repo_commande $this->getEntityManager()->getRepository(Commande::class);
  1631.             $prorata       0;
  1632.             $tab_temp = [];
  1633.             $tab_or   = [];
  1634.             foreach ($res as $re) {
  1635.                 if (isset($re['id']) && $re['id'] != null) {
  1636.                     $facture $repo_commande->find($re['id']);
  1637.                     if (is_object($facture)) {
  1638.                         //$or = $facture->getCommande();
  1639.                         $or $facture;
  1640.                         if (is_object($or)) {
  1641.                             $interventions $or->getInterventions();
  1642.                             foreach ($interventions as $intervention) {
  1643.                                 if ($intervention->getMecanicien()->getId() == $mecanicien) {
  1644.                                     if (!in_array($intervention->getId(), $tab_temp)) {
  1645.                                         $tab_temp[] = $intervention->getId();
  1646.                                         $tab_or[]   = $or->getId();
  1647.                                     }
  1648.                                 }
  1649.                             }
  1650.                         }
  1651.                     }
  1652.                 }
  1653.             }
  1654.             $tab_intervention          = [];
  1655.             $total_intervention        0;
  1656.             $total_intervention_mecano 0;
  1657.             foreach ($res as $re) {
  1658.                 if (isset($re['id']) && $re['id'] != null) {
  1659.                     $facture $repo_commande->find($re['id']);
  1660.                     if (is_object($facture)) {
  1661.                         $or $facture;
  1662.                         if (is_object($or) and in_array($or->getId(), $tab_or)) {
  1663.                             $interventions $or->getInterventions();
  1664.                             foreach ($interventions as $intervention) {
  1665.                                 if (!array_key_exists($intervention->getArticleCommande()->getId(), $tab_intervention)) {
  1666.                                     $tab_intervention[$intervention->getArticleCommande()->getId()] = [
  1667.                                         "total_intervention"        => "0",
  1668.                                         "total_intervention_mecano" => "0",
  1669.                                         "totale_duree"              => $intervention->getArticleCommande()->getQuantite(),
  1670.                                     ];
  1671.                                 }
  1672.                                 $tab_intervention[$intervention->getArticleCommande()->getId()]["total_intervention"] += $intervention->getDuree();
  1673.                                 if ($intervention->getMecanicien()->getId() == $mecanicien) {
  1674.                                     //$total_intervention_mecano += $total_intervention_mecano->getDuree();
  1675.                                     $tab_intervention[$intervention->getArticleCommande()->getId()]["total_intervention_mecano"] += $intervention->getDuree();
  1676.                                 }
  1677.                             }
  1678.                         }
  1679.                     }
  1680.                 }
  1681.             }
  1682.             $prorata 0;
  1683.             //print_r($tab_intervention);
  1684.             foreach ($tab_intervention as $key => $value) {
  1685.                 $prorata += $value['total_intervention_mecano'] / $value['total_intervention'] * $value['totale_duree'];
  1686.             }
  1687.             //$tab_duree_temp = array();
  1688.             /*foreach ($res as $re){
  1689.                 if(isset($re['id']) && $re['id'] != null){
  1690.                     $facture = $repo_commande->find($re['id']);
  1691.                     if(is_object($facture)){
  1692.                         //$or = $facture->getCommande();
  1693.                         $or = $facture;
  1694.                         if(is_object($or)){
  1695.                             $interventions = $or->getInterventions();
  1696.                             foreach($interventions as $intervention){
  1697.                                 if($intervention->getMecanicien()->getId() == $mecanicien) {
  1698.                                     if (!in_array($intervention->getId(), $tab_temp)) {
  1699.                                         $tab_temp[] =  $intervention->getId();
  1700.                                     }
  1701.                                 }
  1702.                             }
  1703.                             $dureeTotale = 0;
  1704.                             $dureeTotaleMecanicien = 0;
  1705.                             foreach($interventions as $intervention){
  1706.                                 echo "<div>IA ".$intervention->getId()."</div>";
  1707.                                 if (in_array($intervention->getId(), $tab_temp) or 1==1) {
  1708.                                     echo "<div>IB ".$intervention->getId()."</div>";
  1709.                                     $dureeTotale += $intervention->getDuree();
  1710.                                     if($intervention->getMecanicien()->getId() == $mecanicien) {
  1711.                                         $dureeTotaleMecanicien += $intervention->getDuree();
  1712.                                     }
  1713.                                 }
  1714.                             }
  1715.                             echo "<div>dureeTotale ".$dureeTotale."</div>";
  1716.                             echo "<div>dureeTotaleMecanicien ".$dureeTotaleMecanicien."</div>";
  1717.                             echo "<div>total ".$re['total']."</div>";
  1718.                             if($dureeTotale != 0) {
  1719.                                 $prorata += $re['total'] / $dureeTotale * $dureeTotaleMecanicien;
  1720.                             }
  1721.                         }
  1722.                     }
  1723.                 }
  1724.             }
  1725.             */
  1726.             return $prorata;
  1727.         }
  1728.     }
  1729.     public function ____getTotalHeuresFactureesDsPeriode($dateDebut$dateFin$mecanicien null)
  1730.     {
  1731.         if ($mecanicien == null) {
  1732.             $query $this->createQueryBuilder('p')
  1733.                           ->select("SUM(COALESCE(ac.quantite,0)) as total")
  1734.                           ->join('p.articleCommande''ac')
  1735.                           ->join('ac.article''a')
  1736.                           ->where('p.date > :dateDebut and p.date < :dateFin AND p.typeDocumentCommercial = :typeDocumentCommercial AND a.mainOeuvre = :mainOeuvre')
  1737.                           ->setParameters(['dateDebut' => $dateDebut'dateFin' => $dateFin'typeDocumentCommercial' => TypeDocumentCommercial::FACTURE'mainOeuvre' => true])
  1738.                           ->orderBy('p.date''DESC');
  1739.             $res   $query->getQuery()->getOneOrNullResult();
  1740.             return $res['total'];
  1741.         } else {
  1742.             $query $this->createQueryBuilder('p')
  1743.                           ->select("p.id, SUM(COALESCE(ac.quantite,0)) as total")
  1744.                           ->join('p.articleCommande''ac')
  1745.                           ->join('ac.article''a')
  1746.                           ->where('p.date > :dateDebut and p.date < :dateFin AND p.typeDocumentCommercial = :typeDocumentCommercial AND a.mainOeuvre = :mainOeuvre')
  1747.                           ->setParameters(['dateDebut' => $dateDebut'dateFin' => $dateFin'typeDocumentCommercial' => TypeDocumentCommercial::FACTURE'mainOeuvre' => true])
  1748.                           ->orderBy('p.date''DESC');
  1749.             $res   $query->getQuery()->getResult();
  1750.             $repo_commande $this->getEntityManager()->getRepository(Commande::class);
  1751.             $prorata       0;
  1752.             foreach ($res as $re) {
  1753.                 if (isset($re['id']) && $re['id'] != null) {
  1754.                     $facture $repo_commande->find($re['id']);
  1755.                     if (is_object($facture)) {
  1756.                         $or $facture->getCommande();
  1757.                         if (is_object($or)) {
  1758.                             $interventions         $or->getInterventions();
  1759.                             $dureeTotale           0;
  1760.                             $dureeTotaleMecanicien 0;
  1761.                             foreach ($interventions as $intervention) {
  1762.                                 $dureeTotale += $intervention->getDuree();
  1763.                                 if ($intervention->getMecanicien()->getId() == $mecanicien) {
  1764.                                     $dureeTotaleMecanicien += $intervention->getDuree();
  1765.                                 }
  1766.                             }
  1767.                             if ($dureeTotale != 0) {
  1768.                                 $prorata += $re['total'] / $dureeTotale $dureeTotaleMecanicien;
  1769.                             }
  1770.                         }
  1771.                     }
  1772.                 }
  1773.             }
  1774.             return $prorata;
  1775.         }
  1776.     }
  1777.     public function getTotalHeuresMoFacture($commande)
  1778.     {
  1779.         /*$query = $this->createQueryBuilder('p')
  1780.                 ->select("SUM(ac.quantite) as total")
  1781.                 ->join('p.articleCommande', 'ac')
  1782.                 ->join('ac.article', 'a')
  1783.         ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND a.mainOeuvre = :mainOeuvre AND p.commande = :commande')
  1784.         ->setParameters(array('typeDocumentCommercial' => TypeDocumentCommercial::FACTURE, 'mainOeuvre'=>true, 'commande'=>$commande))
  1785.         ->orderBy('p.date', 'DESC')
  1786.         */
  1787.         $query $this->createQueryBuilder('p')
  1788.                       ->select("SUM(COALESCE(ac.quantite,0)) as total")
  1789.                       ->join('p.articleCommande''ac')
  1790.                       ->join('ac.article''a')
  1791.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND a.mainOeuvre = :mainOeuvre AND p.id = :commande')
  1792.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::ORDREREPARATION'mainOeuvre' => true'commande' => $commande])
  1793.                       ->orderBy('p.date''DESC');
  1794.         $res   $query->getQuery()->getOneOrNullResult();
  1795.         if (count($res) > 0) {
  1796.             return $res['total'];
  1797.         } else {
  1798.             return 0;
  1799.         }
  1800.     }
  1801.     public function getFacturesEtAvoirsAtransfererEnCompta($date ''$export '')
  1802.     {
  1803.         if ($date == '') {
  1804.             $date = new Datetime();
  1805.         }
  1806.         $where      'p.dateSuppression IS NULL AND p.date <= :date AND p.typeDocumentCommercial IN (:types) AND sc.ordre NOT IN (:statutCommande)';
  1807.         $parameters = [
  1808.             'date'           => $date->format("Y-m-d 23:59:59"),
  1809.             'types'          => [67],//Facture et avoirs
  1810.             'statutCommande' => [0],
  1811.         ];
  1812.         if (is_object($export)) {
  1813.             $where                      .= ' AND p.exportCompta = :exportCompta';
  1814.             $parameters['exportCompta'] = $export->getId();
  1815.         } else {
  1816.             //$where .= ' AND p.dateTransfertCompta IS NULL';
  1817.             $where .= ' AND p.exportCompta IS NULL';
  1818.         }
  1819.         $query $this->createQueryBuilder('p')
  1820.                       ->join('p.statutCommande''sc')
  1821.                       ->where($where)
  1822.                       ->setParameters($parameters);
  1823.         return $query->getQuery()->getResult();
  1824.     }
  1825.     public function getFacturesEtAvoirsByCommande(Commande $commande)
  1826.     {
  1827.         $where      'p.dateSuppression IS NULL AND p.typeDocumentCommercial IN (:types) AND sc.ordre NOT IN (:statutCommande) AND p.commande = :commande';
  1828.         $parameters = [
  1829.             'types'          => [67],//Facture et avoirs
  1830.             'statutCommande' => [0],
  1831.             'commande'       => $commande,
  1832.         ];
  1833.         $query $this->createQueryBuilder('p')
  1834.             //->select("p.id")
  1835.                       ->join('p.statutCommande''sc')
  1836.                       ->where($where)
  1837.                       ->setParameters($parameters);
  1838.         return $query->getQuery()->getResult();
  1839.     }
  1840.     public function topClients($parametres = [])
  1841.     {
  1842.         if (!array_key_exists('n1'$parametres)) {
  1843.             $parametres["n1"] = "0";
  1844.         }
  1845.         //if(intval($parametres['nbel'])>500) $parametres['nbel'] = 500;
  1846.         $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  1847.         $dateDebut->setTime(000000);
  1848.         $dateFin DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  1849.         $dateFin->setTime(235959);
  1850.         $where              "";
  1851.         $join               "";
  1852.         $join_table_client  false;
  1853.         $join_table_article false;
  1854.         $inclure_frais_de_port true;
  1855.         if (array_key_exists('ref_client'$parametres) and $parametres["ref_client"] > 0) {
  1856.             $join              .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  1857.             $where             .= " AND clt.reference like '" $parametres["ref_client"] . "%'";
  1858.             $join_table_client true;
  1859.         }
  1860.         if (array_key_exists('nom_client'$parametres) and $parametres["nom_client"] != "") {
  1861.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  1862.             $where             .= " AND u.nom like '%" $parametres["nom_client"] . "%'";
  1863.             $join_table_client true;
  1864.         }
  1865.         if (array_key_exists('client'$parametres) and $parametres["client"] != "") {
  1866.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  1867.             $where             .= " AND u.id = '" $parametres["client"] . "'";
  1868.             $join_table_client true;
  1869.         }
  1870.         if (array_key_exists('equipier'$parametres) and $parametres["equipier"] > 0) {
  1871.             $where .= " AND c.utilisateur_id = '" $parametres["equipier"] . "'";
  1872.         }
  1873.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  1874.             //echo "MARQUE";
  1875.             //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  1876.             $join               .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  1877.             $where              .= " AND ar.marque_id = '" $parametres["marque"] . "'";
  1878.             $join_table_article true;
  1879.             $inclure_frais_de_port false;
  1880.         }
  1881.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  1882.             if (!$join_table_article) {
  1883.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  1884.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  1885.             }
  1886.             $where              .= " AND ar.id = '" $parametres["article"] . "'";
  1887.             $join_table_article true;
  1888.             $inclure_frais_de_port false;
  1889.         }
  1890.         if (array_key_exists('categorie_client'$parametres) and $parametres["categorie_client"] > 0) {
  1891.             if (!$join_table_client) {
  1892.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  1893.             }
  1894.             //$join .= " LEFT JOIN client__client_categorie as cltcat on cltcat.client_id = u.id";
  1895.             if (array_key_exists('sous_categorie_client'$parametres) and $parametres["sous_categorie_client"] > 0) {
  1896.                 //$where .= " AND cltcat.categorie_id = '".$parametres["sous_categorie_client"]."'";
  1897.                 $where .= " AND clt.id IN  (select distinct(client_id) from client__client_categorie where dateSuppression is null and categorie_id = " $parametres["sous_categorie_client"] . ")";
  1898.             } else {
  1899.                 //$where .= " AND cltcat.categorie_id = '".$parametres["categorie_client"]."'";
  1900.                 $where .= " AND clt.id IN  (select distinct(client_id) from client__client_categorie where dateSuppression is null and categorie_id = " $parametres["categorie_client"] . ")";
  1901.             }
  1902.         }
  1903.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  1904.             if (!$join_table_article) {
  1905.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  1906.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  1907.             }
  1908.             //$join .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  1909.             //$where .= " AND artcat.categorie_id = '".$parametres["categorie_article"]."'";
  1910.             $join_table_article true;
  1911.             $inclure_frais_de_port false;
  1912.             $where .= " AND ( ar.id IN  (select distinct(article_id) from article__article_categorie where categorie_id = " $parametres["categorie_article"] . ")";
  1913.             if (array_key_exists('sous_categorie_article'$parametres) and $parametres["sous_categorie_article"] > 0) {
  1914.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  1915.                 //$join .= " LEFT JOIN article__article as ar2 on ar2.id = ac.article_id";
  1916.                 //$join .= " LEFT JOIN article__article_categorie as artcat2 on artcat2.article_id = ar2.id";
  1917.                 //$where .= " AND artcat2.categorie_id = '".$parametres["sous_categorie_article"]."'";
  1918.                 $join_table_article true;
  1919.                 $inclure_frais_de_port false;
  1920.                 $where .= " AND ar.id IN  (select distinct(article_id) from article__article_categorie where categorie_id = " $parametres["sous_categorie_article"] . ")";
  1921.                 if (array_key_exists('sous_sous_categorie_article'$parametres) and $parametres["sous_sous_categorie_article"] > 0) {
  1922.                     //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  1923.                     //$join .= " LEFT JOIN article__article as ar3 on ar3.id = ac.article_id";
  1924.                     //$join .= " LEFT JOIN article__article_categorie as artcat3 on artcat3.article_id = ar2.id";
  1925.                     //$where .= " AND artcat3.categorie_id = '".$parametres["sous_sous_categorie_article"]."'";
  1926.                     $join_table_article true;
  1927.                     $inclure_frais_de_port false;
  1928.                     $where .= " AND ar.id IN  (select distinct(article_id) from article__article_categorie where categorie_id = " $parametres["sous_sous_categorie_article"] . ")";
  1929.                 }
  1930.             }
  1931.             $where .= " )";
  1932.         }
  1933.         //SELECT SUM(ac.total_ht+ac.tva_vente) as total,u.prenom,u.nom,u.reference,u.id as client_id
  1934.         /*
  1935.         $sql = "
  1936.             SELECT SUM(ac.total_ht) as total,u.prenom,u.nom,u.reference,u.id as client_id
  1937.             FROM `commerciale__article_commande` as ac
  1938.             LEFT JOIN commerciale__commande as c on c.id = ac.commande_id
  1939.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  1940.             LEFT JOIN client__client as u on u.id = c.client_id
  1941.             ".$join."
  1942.             WHERE
  1943.             c.date >= '".$dateDebut->format('Y-m-d H:i:s')."' AND c.date <= '".$dateFin->format('Y-m-d H:i:s')."'
  1944.             AND
  1945.             (
  1946.             (c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23  AND c.multi_bl != 1)
  1947.             OR
  1948.             (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  1949.             AND f.statut_commande_id != 23
  1950.             )
  1951.             )
  1952.             ".$where."
  1953.             GROUP BY c.client_id
  1954.             ORDER BY total DESC
  1955.             LIMIT 0,".intval($parametres['nbel'])."
  1956.         ";
  1957.         */
  1958.         //SUM(p.total+p.fraisPortSupplementaire+p.totalFraisPort) as total
  1959.         if ($inclure_frais_de_port) {
  1960.             $sum "SUM(COALESCE(c.total,0)+COALESCE(c.frais_port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total";
  1961.             $sql "
  1962.                 SELECT " $sum ",u.prenom,u.nom,u.reference,u.id as client_id
  1963.                 FROM `commerciale__commande` as c
  1964.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  1965.                 LEFT JOIN client__client as u on u.id = c.client_id
  1966.                 " $join "
  1967.                 WHERE
  1968.                 (
  1969.                 (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format(
  1970.                     'Y-m-d H:i:s'
  1971.                 ) . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "')
  1972.                 OR
  1973.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format(
  1974.                     'Y-m-d H:i:s'
  1975.                 ) . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "')
  1976.                 OR
  1977.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  1978.                 AND f.statut_commande_id != 23
  1979.                 and
  1980.                 ((f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  1981.                 )
  1982.                 )
  1983.                 " $where "
  1984.                 GROUP BY c.client_id
  1985.                 ORDER BY total DESC
  1986.                 LIMIT 0," intval($parametres['nbel']) . "
  1987.             ";
  1988.             //echo $sql;
  1989.         } else {
  1990.             $sum "SUM(COALESCE(ac.total_ht,0)) as total";
  1991.             $sql "
  1992.                 SELECT " $sum ",u.prenom,u.nom,u.reference,u.id as client_id
  1993.                 FROM `commerciale__article_commande` as ac
  1994.                 LEFT JOIN commerciale__commande as c on c.id = ac.commande_id
  1995.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  1996.                 LEFT JOIN client__client as u on u.id = c.client_id
  1997.                 " $join "
  1998.                 WHERE
  1999.                 (
  2000.                 (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format(
  2001.                     'Y-m-d H:i:s'
  2002.                 ) . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "')
  2003.                 OR
  2004.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format(
  2005.                     'Y-m-d H:i:s'
  2006.                 ) . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "')
  2007.                 OR
  2008.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  2009.                 AND f.statut_commande_id != 23
  2010.                 and
  2011.                 ((f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  2012.                 )
  2013.                 )
  2014.                 " $where "
  2015.                 GROUP BY c.client_id
  2016.                 ORDER BY total DESC
  2017.                 LIMIT 0," intval($parametres['nbel']) . "
  2018.             ";
  2019.         }
  2020.         $sql trim($sql);
  2021.         //echo "<br/><br/>".$sql;
  2022.         $em      $this->getEntityManager();
  2023.         $stmt    $em->getConnection()->prepare($sql);
  2024.         $clients $stmt->executeQuery()->fetchAllAssociative();
  2025.         $cpt 0;
  2026.         for ($i 0$i count($clients); $i++) {
  2027.             $qte        "";
  2028.             $where_temp $where " AND c.client_id = " $clients[$i]["client_id"];
  2029.             $sql "
  2030.                 SELECT sum(COALESCE(quantite,0)) as total_qte
  2031.                 FROM `commerciale__article_commande` as ac
  2032.                 LEFT JOIN commerciale__commande as c on c.id = ac.commande_id
  2033.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  2034.                 LEFT JOIN client__client as u on u.id = c.client_id
  2035.                 " $join "
  2036.                 WHERE
  2037.                 (
  2038.                 (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format(
  2039.                     'Y-m-d H:i:s'
  2040.                 ) . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "')
  2041.                 OR
  2042.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format(
  2043.                     'Y-m-d H:i:s'
  2044.                 ) . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "')
  2045.                 OR
  2046.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  2047.                 AND f.statut_commande_id != 23
  2048.                 and
  2049.                 ((f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  2050.                 )
  2051.                 )
  2052.                 " $where_temp "
  2053.             ";
  2054.             $sql trim($sql);
  2055.             //echo "<br/><br/>".$sql;
  2056.             $em   $this->getEntityManager();
  2057.             $stmt $em->getConnection()->prepare($sql);
  2058.             $client_qte $stmt->executeQuery()->fetchAllAssociative();
  2059.             $clients[$i]["qte"] = $client_qte[0]["total_qte"];
  2060.             if ($inclure_frais_de_port) {
  2061.                 //////////
  2062.                 $where_temp $where " AND c.client_id = " $clients[$i]["client_id"];
  2063.                 $sql "
  2064.                 SELECT count(*) as qte ,t.id as transporteur_id
  2065.                 FROM `commerciale__commande` as c
  2066.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  2067.                 LEFT JOIN client__client as u on u.id = c.client_id
  2068.                 LEFT JOIN transporteur__transporteur as t on t.id = c.transporteur_id
  2069.                 " $join "
  2070.                 WHERE
  2071.                 c.transporteur_id is not null AND
  2072.                 (
  2073.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND
  2074.                 ((c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  2075.                 )
  2076.                 OR
  2077.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  2078.                 AND f.statut_commande_id != 23
  2079.                 and
  2080.                 ((f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  2081.                 )
  2082.                 )
  2083.                 " $where_temp "
  2084.                 ";
  2085.                 //echo $sql."<br/><br/>";
  2086.                 $sql trim($sql);
  2087.                 $em         $this->getEntityManager();
  2088.                 $stmt       $em->getConnection()->prepare($sql);
  2089.                 $transports $stmt->executeQuery()->fetchAllAssociative();
  2090.                 if (count($transports) > 0) {
  2091.                     for ($r 0$r count($transports); $r++) {
  2092.                         $clients[$i]["qte"] += $transports[$r]["qte"];
  2093.                     }
  2094.                 }
  2095.             }
  2096.         }
  2097.         if (array_key_exists('tri'$parametres) && $parametres["tri"] == "qte") {
  2098.             $qte = [];
  2099.             foreach ($clients as $key => $row) {
  2100.                 $qte[$key] = floatVal($row['qte']);
  2101.             }
  2102.             array_multisort($qteSORT_DESC$clients);
  2103.         }
  2104.         return $clients;
  2105.     }
  2106.     public function topClientsDetailsTransport($parametres = [])
  2107.     {
  2108.         if (!array_key_exists('n1'$parametres)) {
  2109.             $parametres["n1"] = "0";
  2110.         }
  2111.         //print_r($parametres);
  2112.         //if(intval($parametres['nbel'])>500) $parametres['nbel'] = 500;
  2113.         $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  2114.         $dateDebut->setTime(000000);
  2115.         $dateFin DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  2116.         $dateFin->setTime(235959);
  2117.         $where              "";
  2118.         $join               "";
  2119.         $join_table_client  false;
  2120.         $join_table_article false;
  2121.         $inclure_frais_de_port true;
  2122.         if (array_key_exists('ref_client'$parametres) and $parametres["ref_client"] > 0) {
  2123.             $join              .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2124.             $where             .= " AND clt.reference like '" $parametres["ref_client"] . "%'";
  2125.             $join_table_client true;
  2126.         }
  2127.         if (array_key_exists('nom_client'$parametres) and $parametres["nom_client"] != "") {
  2128.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2129.             $where             .= " AND u.nom like '%" $parametres["nom_client"] . "%'";
  2130.             $join_table_client true;
  2131.         }
  2132.         if (array_key_exists('client'$parametres) and $parametres["client"] != "") {
  2133.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2134.             $where             .= " AND u.id = '" $parametres["client"] . "'";
  2135.             $join_table_client true;
  2136.         }
  2137.         if (array_key_exists('equipier'$parametres) and $parametres["equipier"] > 0) {
  2138.             $where .= " AND c.utilisateur_id = '" $parametres["equipier"] . "'";
  2139.         }
  2140.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  2141.             //echo "MARQUE";
  2142.             //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2143.             $join               .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2144.             $where              .= " AND ar.marque_id = '" $parametres["marque"] . "'";
  2145.             $join_table_article true;
  2146.             $inclure_frais_de_port false;
  2147.         }
  2148.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  2149.             if (!$join_table_article) {
  2150.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2151.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2152.             }
  2153.             $where              .= " AND ar.id = '" $parametres["article"] . "'";
  2154.             $join_table_article true;
  2155.             $inclure_frais_de_port false;
  2156.         }
  2157.         if (array_key_exists('categorie_client'$parametres) and $parametres["categorie_client"] > 0) {
  2158.             if (!$join_table_client) {
  2159.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2160.             }
  2161.             $join .= " LEFT JOIN client__client_categorie as cltcat on cltcat.client_id = u.id";
  2162.             if (array_key_exists('sous_categorie_client'$parametres) and $parametres["sous_categorie_client"] > 0) {
  2163.                 $where .= " AND cltcat.categorie_id = '" $parametres["sous_categorie_client"] . "'";
  2164.             } else {
  2165.                 $where .= " AND cltcat.categorie_id = '" $parametres["categorie_client"] . "'";
  2166.             }
  2167.         }
  2168.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  2169.             if (!$join_table_article) {
  2170.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2171.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2172.             }
  2173.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  2174.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  2175.             $join_table_article true;
  2176.             $inclure_frais_de_port false;
  2177.             if (array_key_exists('sous_categorie_article'$parametres) and $parametres["sous_categorie_article"] > 0) {
  2178.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2179.                 $join .= " LEFT JOIN article__article as ar2 on ar2.id = ac.article_id";
  2180.                 $join               .= " LEFT JOIN article__article_categorie as artcat2 on artcat2.article_id = ar2.id";
  2181.                 $where              .= " AND artcat2.categorie_id = '" $parametres["sous_categorie_article"] . "'";
  2182.                 $join_table_article true;
  2183.                 $inclure_frais_de_port false;
  2184.                 if (array_key_exists('sous_sous_categorie_article'$parametres) and $parametres["sous_sous_categorie_article"] > 0) {
  2185.                     //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2186.                     $join .= " LEFT JOIN article__article as ar3 on ar3.id = ac.article_id";
  2187.                     $join               .= " LEFT JOIN article__article_categorie as artcat3 on artcat3.article_id = ar2.id";
  2188.                     $where              .= " AND artcat3.categorie_id = '" $parametres["sous_sous_categorie_article"] . "'";
  2189.                     $join_table_article true;
  2190.                     $inclure_frais_de_port false;
  2191.                 }
  2192.             }
  2193.         }
  2194.         $where .= " AND c.client_id = " $parametres["client_id"];
  2195.         $order_by "total DESC    ";
  2196.         if (array_key_exists('tri'$parametres) && $parametres["tri"] == "qte") {
  2197.             $order_by "total_qte DESC    ";
  2198.         }
  2199.         $articles = [];
  2200.         $where .= " AND c.transporteur_id = " $parametres["transporteur_id"];
  2201.         //echo "AAAAAA";
  2202.         $sql "
  2203.             SELECT count(*) as qte ,t.id as transporteur_id,t.reference,t.libelle,sum(COALESCE(c.frais_Port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total
  2204.             FROM `commerciale__commande` as c
  2205.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  2206.             LEFT JOIN client__client as u on u.id = c.client_id
  2207.             LEFT JOIN transporteur__transporteur as t on t.id = c.transporteur_id
  2208.             " $join "
  2209.             WHERE
  2210.             (
  2211.             (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format(
  2212.                 'Y-m-d H:i:s'
  2213.             ) . "')
  2214.             OR
  2215.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format(
  2216.                 'Y-m-d H:i:s'
  2217.             ) . "')
  2218.             OR
  2219.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  2220.             AND f.statut_commande_id != 23
  2221.             and
  2222.             f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  2223.             )
  2224.             )
  2225.             " $where "
  2226.         ";
  2227.         $sql trim($sql);
  2228.         $em         $this->getEntityManager();
  2229.         $stmt       $em->getConnection()->prepare($sql);
  2230.         $transports $stmt->executeQuery()->fetchAllAssociative();
  2231.         if (count($transports) > 0) {
  2232.             for ($r 0$r count($transports); $r++) {
  2233.                 $tab_transport = [];
  2234.                 //sum(ac.quantite) as total_qte,ac.libelle,sum(ac.total_ht) as total,a.reference,a.id as article_id
  2235.                 $tab_transport["total_qte"]       = $transports[$r]["qte"];
  2236.                 $tab_transport["libelle"]         = $transports[$r]["libelle"];
  2237.                 $tab_transport["total"]           = $transports[$r]["total"];
  2238.                 $tab_transport["reference"]       = $transports[$r]["reference"];
  2239.                 $tab_transport["article_id"]      = "";
  2240.                 $tab_transport["transporteur_id"] = $transports[$r]["transporteur_id"];
  2241.                 $articles[] = $tab_transport;
  2242.             }
  2243.         }
  2244.         //echo $sql;
  2245.         return $articles;
  2246.     }
  2247.     public function topClientsDetails($parametres = [])
  2248.     {
  2249.         if (!array_key_exists('n1'$parametres)) {
  2250.             $parametres["n1"] = "0";
  2251.         }
  2252.         if (!array_key_exists('details_ajax'$parametres)) {
  2253.             $parametres["details_ajax"] = "0";
  2254.         }
  2255.         //print_r($parametres);
  2256.         //if(intval($parametres['nbel'])>500) $parametres['nbel'] = 500;
  2257.         $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  2258.         $dateDebut->setTime(000000);
  2259.         $dateFin DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  2260.         $dateFin->setTime(235959);
  2261.         $where              "";
  2262.         $join               "";
  2263.         $join_table_client  false;
  2264.         $join_table_article false;
  2265.         $inclure_frais_de_port true;
  2266.         $inclure_transporteur  true;
  2267.         if (array_key_exists('ref_client'$parametres) and $parametres["ref_client"] > 0) {
  2268.             $join              .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2269.             $where             .= " AND clt.reference like '" $parametres["ref_client"] . "%'";
  2270.             $join_table_client true;
  2271.         }
  2272.         if (array_key_exists('nom_client'$parametres) and $parametres["nom_client"] != "") {
  2273.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2274.             $where             .= " AND u.nom like '%" $parametres["nom_client"] . "%'";
  2275.             $join_table_client true;
  2276.         }
  2277.         if (array_key_exists('client'$parametres) and $parametres["client"] != "") {
  2278.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2279.             $where             .= " AND u.id = '" $parametres["client"] . "'";
  2280.             $join_table_client true;
  2281.         }
  2282.         if (array_key_exists('equipier'$parametres) and $parametres["equipier"] > 0) {
  2283.             $where .= " AND c.utilisateur_id = '" $parametres["equipier"] . "'";
  2284.         }
  2285.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  2286.             //echo "MARQUE";
  2287.             //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2288.             $join               .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2289.             $where              .= " AND ar.marque_id = '" $parametres["marque"] . "'";
  2290.             $join_table_article true;
  2291.             $inclure_frais_de_port false;
  2292.         }
  2293.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  2294.             if (!$join_table_article) {
  2295.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2296.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2297.             }
  2298.             $where              .= " AND ar.id = '" $parametres["article"] . "'";
  2299.             $join_table_article true;
  2300.             $inclure_frais_de_port false;
  2301.         }
  2302.         if (array_key_exists('categorie_client'$parametres) and $parametres["categorie_client"] > 0) {
  2303.             if (!$join_table_client) {
  2304.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2305.             }
  2306.             $join .= " LEFT JOIN client__client_categorie as cltcat on cltcat.client_id = u.id";
  2307.             if (array_key_exists('sous_categorie_client'$parametres) and $parametres["sous_categorie_client"] > 0) {
  2308.                 $where .= " AND cltcat.categorie_id = '" $parametres["sous_categorie_client"] . "'";
  2309.             } else {
  2310.                 $where .= " AND cltcat.categorie_id = '" $parametres["categorie_client"] . "'";
  2311.             }
  2312.         }
  2313.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  2314.             if (!$join_table_article) {
  2315.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2316.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2317.             }
  2318.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  2319.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  2320.             $join_table_article true;
  2321.             $inclure_frais_de_port false;
  2322.             if (array_key_exists('sous_categorie_article'$parametres) and $parametres["sous_categorie_article"] > 0) {
  2323.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2324.                 $join .= " LEFT JOIN article__article as ar2 on ar2.id = ac.article_id";
  2325.                 $join               .= " LEFT JOIN article__article_categorie as artcat2 on artcat2.article_id = ar2.id";
  2326.                 $where              .= " AND artcat2.categorie_id = '" $parametres["sous_categorie_article"] . "'";
  2327.                 $join_table_article true;
  2328.                 $inclure_frais_de_port false;
  2329.                 if (array_key_exists('sous_sous_categorie_article'$parametres) and $parametres["sous_sous_categorie_article"] > 0) {
  2330.                     //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2331.                     $join .= " LEFT JOIN article__article as ar3 on ar3.id = ac.article_id";
  2332.                     $join               .= " LEFT JOIN article__article_categorie as artcat3 on artcat3.article_id = ar2.id";
  2333.                     $where              .= " AND artcat3.categorie_id = '" $parametres["sous_sous_categorie_article"] . "'";
  2334.                     $join_table_article true;
  2335.                     $inclure_frais_de_port false;
  2336.                 }
  2337.             }
  2338.         }
  2339.         $where .= " AND c.client_id = " $parametres["client_id"];
  2340.         $order_by "total DESC    ";
  2341.         if (array_key_exists('tri'$parametres) && $parametres["tri"] == "qte") {
  2342.             $order_by "total_qte DESC    ";
  2343.         }
  2344.         $date_n1_f "";
  2345.         $date_n1_c "";
  2346.         if ($parametres["n1"] == "1") {
  2347.             $dateDebutN1 DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  2348.             $dateDebutN1->setTime(000000);
  2349.             $dateDebutN1 $dateDebutN1->modify('-1 year');
  2350.             $dateFinN1 DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  2351.             $dateFinN1->setTime(235959);
  2352.             $dateFinN1 $dateFinN1->modify('-1 year');
  2353.             $date_n1_f " OR (f.date >= '" $dateDebutN1->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFinN1->format('Y-m-d H:i:s') . "')";
  2354.             $date_n1_c " OR (c.date >= '" $dateDebutN1->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFinN1->format('Y-m-d H:i:s') . "')";
  2355.         }
  2356.         $sql "
  2357.             SELECT sum(COALESCE(ac.quantite,0)) as total_qte,ac.libelle,sum(COALESCE(ac.total_ht,0)) as total,a.reference,a.id as article_id,c.date as date_c,f.date as date_f
  2358.             FROM `commerciale__article_commande` as ac
  2359.             LEFT JOIN commerciale__commande as c on c.id = ac.commande_id
  2360.             LEFT JOIN article__article as a on a.id = ac.article_id
  2361.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  2362.             LEFT JOIN client__client as u on u.id = c.client_id
  2363.             " $join "
  2364.             WHERE
  2365.             (
  2366.             (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format(
  2367.                 'Y-m-d H:i:s'
  2368.             ) . "')
  2369.             OR
  2370.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND
  2371.             ((c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  2372.             )
  2373.             OR
  2374.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  2375.             AND f.statut_commande_id != 23
  2376.             and
  2377.             ((f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  2378.             )
  2379.             )
  2380.             " $where "
  2381.             GROUP BY ac.article_id
  2382.             ORDER BY " $order_by "
  2383.         ";
  2384.         $sql trim($sql);
  2385.         $em       $this->getEntityManager();
  2386.         $stmt     $em->getConnection()->prepare($sql);
  2387.         $articles $stmt->executeQuery()->fetchAllAssociative();
  2388.         /*
  2389.         if(array_key_exists('article', $parametres) and $parametres["article"] == 213111 and 1!=1) {
  2390.             echo "<br></br/>";
  2391.             print_r($parametres);
  2392.             echo "<br></br/>";
  2393.             print_r($articles);
  2394.             //echo "<br><br>".$sql."<br>\n\n";
  2395.             exit;
  2396.         }
  2397.         */
  2398.         if ($inclure_frais_de_port) {
  2399.             $sql "
  2400.                 SELECT count(*) as qte ,t.id as transporteur_id,t.reference,t.libelle,sum(COALESCE(c.frais_Port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total
  2401.                 FROM `commerciale__article_commande` as ac
  2402.                 LEFT JOIN commerciale__commande as c on c.id = ac.commande_id
  2403.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  2404.                 LEFT JOIN client__client as u on u.id = c.client_id
  2405.                 LEFT JOIN transporteur__transporteur as t on t.id = c.transporteur_id
  2406.                 " $join "
  2407.                 WHERE
  2408.                 (
  2409.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND
  2410.                 ((c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  2411.                 )
  2412.                 OR
  2413.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  2414.                 AND f.statut_commande_id != 23
  2415.                 and
  2416.                 ((f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  2417.                 )
  2418.                 )
  2419.                 " $where "
  2420.                 GROUP BY c.transporteur_id
  2421.                 ORDER BY total DESC
  2422.             ";
  2423.             $sql trim($sql);
  2424.             $em         $this->getEntityManager();
  2425.             $stmt       $em->getConnection()->prepare($sql);
  2426.             $transports $stmt->executeQuery()->fetchAllAssociative();
  2427.             $tab_transporteur_inclus = [];
  2428.             if (count($transports) > && $parametres["details_ajax"] == 0) {
  2429.                 for ($r 0$r count($transports); $r++) {
  2430.                     $tab_transport = [];
  2431.                     $tab_transporteur_inclus[] = $transports[$r]["transporteur_id"];
  2432.                     //sum(ac.quantite) as total_qte,ac.libelle,sum(ac.total_ht) as total,a.reference,a.id as article_id
  2433.                     $tab_transport["total_qte"]       = $transports[$r]["qte"];
  2434.                     $tab_transport["libelle"]         = $transports[$r]["libelle"];
  2435.                     $tab_transport["total"]           = $transports[$r]["total"];
  2436.                     $tab_transport["reference"]       = $transports[$r]["reference"];
  2437.                     $tab_transport["article_id"]      = "";
  2438.                     $tab_transport["transporteur_id"] = $transports[$r]["transporteur_id"];
  2439.                     $articles[] = $tab_transport;
  2440.                 }
  2441.             }
  2442.             //echo $sql;
  2443.         }
  2444.         if (count($articles) > && $parametres["n1"] == "1") {
  2445.             $tab_article_inclus = [];
  2446.             foreach ($articles as $a) {
  2447.                 if ($a["article_id"] != "") {
  2448.                     $tab_article_inclus[] = $a["article_id"];
  2449.                 }
  2450.             }
  2451.             $sql "
  2452.                 SELECT sum(COALESCE(ac.quantite,0)) as total_qte,ac.libelle,sum(COALESCE(ac.total_ht,0)) as total,a.reference,a.id as article_id,c.date as date_c,f.date as date_f
  2453.                 FROM `commerciale__article_commande` as ac
  2454.                 LEFT JOIN commerciale__commande as c on c.id = ac.commande_id
  2455.                 LEFT JOIN article__article as a on a.id = ac.article_id
  2456.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  2457.                 LEFT JOIN client__client as u on u.id = c.client_id
  2458.                 " $join "
  2459.                 WHERE
  2460.                 (
  2461.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND
  2462.                 ((c.date >= '" $dateDebutN1->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFinN1->format('Y-m-d H:i:s') . "'))
  2463.                 )
  2464.                 OR
  2465.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  2466.                 AND f.statut_commande_id != 23
  2467.                 and
  2468.                 ((f.date >= '" $dateDebutN1->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFinN1->format('Y-m-d H:i:s') . "'))
  2469.                 )
  2470.                 )
  2471.                 " $where "
  2472.                 GROUP BY ac.article_id
  2473.                 ORDER BY " $order_by "
  2474.             ";
  2475.             $sql trim($sql);
  2476.             $em          $this->getEntityManager();
  2477.             $stmt        $em->getConnection()->prepare($sql);
  2478.             $articles_n1 $stmt->executeQuery()->fetchAllAssociative();
  2479.             if (count($articles_n1) > 0) {
  2480.                 foreach ($articles_n1 as $an1) {
  2481.                     if (!in_array($an1["article_id"], $tab_article_inclus)) {
  2482.                         $tab_article_n1                    = [];
  2483.                         $tab_article_n1["total_qte"]       = 0;
  2484.                         $tab_article_n1["libelle"]         = $an1["libelle"];
  2485.                         $tab_article_n1["total"]           = 0;
  2486.                         $tab_article_n1["reference"]       = $an1["reference"];
  2487.                         $tab_article_n1["article_id"]      = $an1["article_id"];
  2488.                         $tab_article_n1["transporteur_id"] = "";
  2489.                         $articles[]                        = $tab_article_n1;
  2490.                     }
  2491.                 }
  2492.             }
  2493.             if ($inclure_frais_de_port) {
  2494.                 $sql "
  2495.                 SELECT count(*) as qte ,t.id as transporteur_id,t.reference,t.libelle,sum(COALESCE(c.frais_Port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total
  2496.                 FROM `commerciale__article_commande` as ac
  2497.                 LEFT JOIN commerciale__commande as c on c.id = ac.commande_id
  2498.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  2499.                 LEFT JOIN client__client as u on u.id = c.client_id
  2500.                 LEFT JOIN transporteur__transporteur as t on t.id = c.transporteur_id
  2501.                 " $join "
  2502.                 WHERE
  2503.                 (
  2504.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND
  2505.                 ((c.date >= '" $dateDebutN1->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFinN1->format('Y-m-d H:i:s') . "'))
  2506.                 )
  2507.                 OR
  2508.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  2509.                 AND f.statut_commande_id != 23
  2510.                 and
  2511.                 ((f.date >= '" $dateDebutN1->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFinN1->format('Y-m-d H:i:s') . "'))
  2512.                 )
  2513.                 )
  2514.                 " $where "
  2515.                 GROUP BY c.transporteur_id
  2516.                 ORDER BY total DESC
  2517.                 ";
  2518.                 $sql trim($sql);
  2519.                 $em         $this->getEntityManager();
  2520.                 $stmt       $em->getConnection()->prepare($sql);
  2521.                 $transports $stmt->executeQuery()->fetchAllAssociative();
  2522.                 if (count($transports) > && $parametres["details_ajax"] == 0) {
  2523.                     for ($r 0$r count($transports); $r++) {
  2524.                         if (!in_array($transports[$r]["transporteur_id"], $tab_transporteur_inclus)) {
  2525.                             $tab_transport = [];
  2526.                             $tab_transporteur_inclus[] = $transports[$r]["transporteur_id"];
  2527.                             //sum(ac.quantite) as total_qte,ac.libelle,sum(ac.total_ht) as total,a.reference,a.id as article_id
  2528.                             $tab_transport["total_qte"]       = "0";
  2529.                             $tab_transport["libelle"]         = $transports[$r]["libelle"];
  2530.                             $tab_transport["total"]           = "0";
  2531.                             $tab_transport["reference"]       = $transports[$r]["reference"];
  2532.                             $tab_transport["article_id"]      = "";
  2533.                             $tab_transport["transporteur_id"] = $transports[$r]["transporteur_id"];
  2534.                             $articles[] = $tab_transport;
  2535.                         }
  2536.                     }
  2537.                 }
  2538.             }
  2539.         }
  2540.         if (array_key_exists('tri'$parametres) && $parametres["tri"] == "qte") {
  2541.             $qte = [];
  2542.             foreach ($articles as $key => $row) {
  2543.                 $qte[$key] = floatVal($row['total_qte']);
  2544.             }
  2545.             array_multisort($qteSORT_DESC$articles);
  2546.         } elseif (array_key_exists('tri'$parametres) && $parametres["tri"] == "ca") {
  2547.             $qte = [];
  2548.             foreach ($articles as $key => $row) {
  2549.                 $qte[$key] = floatVal($row['total']);
  2550.             }
  2551.             array_multisort($qteSORT_DESC$articles);
  2552.         }
  2553.         return $articles;
  2554.     }
  2555.     public function derniereEntreeStock($id_article$param)
  2556.     {
  2557.         $repo_raison_mvmt $this->getEntityManager()->getRepository(RaisonMouvementStock::class);
  2558.         $raisons_mvmt     $repo_raison_mvmt->findBy([], ['libelle' => 'ASC']);
  2559.         $where_origine "";
  2560.         $in_origine    "";
  2561.         foreach ($raisons_mvmt as $r) {
  2562.             if (array_key_exists('origine_' $r->getId(), $param)) {
  2563.                 $in_origine .= "'" $r->getId() . "',";
  2564.             }
  2565.         }
  2566.         if ($in_origine != "") {
  2567.             $in_origine    .= ",";
  2568.             $in_origine    str_replace(",,"""$in_origine);
  2569.             $where_origine .= " AND m.raison_mouvement_stock_id IN (" $in_origine ")";
  2570.         }
  2571.         $sql 'SELECT m.article_id,m.date_mouvement as dernier
  2572.                 FROM article__mouvement_stock as m
  2573.                 WHERE
  2574.                 m.raison_mouvement_stock_id IN (SELECT id FROM article__raison_mouvement_stock WHERE action = "augmenter")
  2575.                 AND
  2576.                 m.article_id = "' $id_article '"
  2577.                 ' $where_origine '
  2578.                 ORDER BY m.date_mouvement DESC
  2579.                 LIMIT 0,1';
  2580.         //echo $sql.'<br/><br/>';
  2581.         $em      $this->getEntityManager();
  2582.         $stmt    $em->getConnection()->prepare($sql);
  2583.         $dernier $stmt->executeQuery()->fetchAllAssociative();
  2584.         if (count($dernier) > 0) {
  2585.             return $dernier[0];
  2586.         }
  2587.         //return 9;
  2588.     }
  2589.     public function derniereSortieStock($id_article$param)
  2590.     {
  2591.         $repo_raison_mvmt $this->getEntityManager()->getRepository(RaisonMouvementStock::class);
  2592.         $raisons_mvmt     $repo_raison_mvmt->findBy([], ['libelle' => 'ASC']);
  2593.         $where_origine "";
  2594.         $in_origine    "";
  2595.         foreach ($raisons_mvmt as $r) {
  2596.             if (array_key_exists('origine_' $r->getId(), $param)) {
  2597.                 $in_origine .= "'" $r->getId() . "',";
  2598.             }
  2599.         }
  2600.         if ($in_origine != "") {
  2601.             $in_origine    .= ",";
  2602.             $in_origine    str_replace(",,"""$in_origine);
  2603.             $where_origine .= " AND m.raison_mouvement_stock_id IN (" $in_origine ")";
  2604.         }
  2605.         $sql 'SELECT m.article_id,m.date_mouvement as dernier
  2606.                 FROM article__mouvement_stock as m
  2607.                 WHERE
  2608.                 m.raison_mouvement_stock_id IN (SELECT id FROM article__raison_mouvement_stock WHERE action = "diminuer")
  2609.                 AND
  2610.                 m.article_id = "' $id_article '"
  2611.                 ' $where_origine '
  2612.                 ORDER BY m.date_mouvement DESC
  2613.                 LIMIT 0,1';
  2614.         //echo $sql.'<br/><br/>';
  2615.         $em      $this->getEntityManager();
  2616.         $stmt    $em->getConnection()->prepare($sql);
  2617.         $dernier $stmt->executeQuery()->fetchAllAssociative();
  2618.         if (count($dernier) > 0) {
  2619.             return $dernier[0];
  2620.         }
  2621.         //return 9;
  2622.     }
  2623.     public function vivantArticles($parametres = [])
  2624.     {
  2625.         if (intval($parametres['nbel']) > 5000) {
  2626.             $parametres['nbel'] = 5000;
  2627.         }
  2628.         $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  2629.         $dateDebut->setTime(000000);
  2630.         $where              "";
  2631.         $join               "";
  2632.         $join_table_client  false;
  2633.         $join_table_article false;
  2634.         $join_table_article true;
  2635.         $repo_raison_mvmt $this->getEntityManager()->getRepository(RaisonMouvementStock::class);
  2636.         $raisons_mvmt     $repo_raison_mvmt->findBy([], ['libelle' => 'ASC']);
  2637.         $where "(a.prestation != 1 or a.prestation IS NULL)";
  2638.         $where .= " and (a.non_stocke IS NULL OR a.non_stocke = 0) AND (a.divers IS NULL OR a.divers = 0) AND (a.divers_composes IS NULL OR a.divers_composes = 0)";
  2639.         $where_origine "";
  2640.         $in_origine    "";
  2641.         foreach ($raisons_mvmt as $r) {
  2642.             if (array_key_exists('origine_' $r->getId(), $parametres)) {
  2643.                 $in_origine .= "'" $r->getId() . "',";
  2644.             }
  2645.         }
  2646.         if ($in_origine != "") {
  2647.             $in_origine .= ",";
  2648.             $in_origine str_replace(",,"""$in_origine);
  2649.             $where      .= " and m.raison_mouvement_stock_id IN (" $in_origine ")";
  2650.         }
  2651.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  2652.             if ($where != "") {
  2653.                 $where .= " and";
  2654.             }
  2655.             $where              .= " a.marque_id = '" $parametres["marque"] . "'";
  2656.             $join_table_article true;
  2657.         }
  2658.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  2659.             $where              .= " and a.id = '" $parametres["article"] . "'";
  2660.             $join_table_article true;
  2661.         }
  2662.         if (array_key_exists('ref_article'$parametres) and $parametres["ref_article"] != "") {
  2663.             $parametres["ref_article"] = str_replace("*""%"$parametres["ref_article"]);
  2664.             if ($where != "") {
  2665.                 $where .= " and";
  2666.             }
  2667.             $where              .= " a.reference like '" $parametres["ref_article"] . "%'";
  2668.             $join_table_article true;
  2669.         }
  2670.         if (array_key_exists('art_stock'$parametres) and $parametres["art_stock"] == "1") {
  2671.             if ($where != "") {
  2672.                 $where .= " and";
  2673.             }
  2674.             $where              .= " a.stock > 0";
  2675.             $join_table_article true;
  2676.         }
  2677.         if (array_key_exists('type_article'$parametres) and $parametres["type_article"] != "") {
  2678.             if ($parametres["type_article"] != "tous") {
  2679.                 if ($where != "") {
  2680.                     $where .= " and";
  2681.                 }
  2682.                 if ($parametres["type_article"] == "non_reappro") {
  2683.                     $where .= " a.arret_gamme = 1";
  2684.                 } elseif ($parametres["type_article"] == "reappro") {
  2685.                     $where .= " a.arret_gamme != 1";
  2686.                 }
  2687.             }
  2688.         }
  2689.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  2690.             $join .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = a.id";
  2691.             if ($where != "") {
  2692.                 $where .= " and";
  2693.             }
  2694.             $where              .= " artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  2695.             $join_table_article true;
  2696.         }
  2697.         //echo "type_article ".$parametres["type_article"]."<br/>";
  2698.         //echo "WHERE ".$where."<br/>";
  2699.         //if($where != "") $where .= " and";
  2700.         $sql "
  2701.     SELECT a.reference,a.libelle,a.id as article_id,a.stock,a.pump,a.cpump
  2702.     FROM `article__article` as a
  2703.     " $join "
  2704.     WHERE
  2705.     " $where "
  2706.     a.id NOT IN
  2707.     (
  2708.         SELECT article_id
  2709.         FROM article__mouvement_stock as m
  2710.         WHERE
  2711.         m.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "'
  2712.         " $where_origine "
  2713.         GROUP BY article_id
  2714.         )
  2715.         GROUP BY article_id
  2716.         ORDER BY a.reference
  2717.         ";
  2718.         if (intval($parametres['nbel']) > 0) {
  2719.             $sql .= " LIMIT 0," intval($parametres['nbel']);
  2720.         }// else $sql .= " LIMIT 0,10";
  2721.         if ($where != "") {
  2722.             $where " and " $where;
  2723.         }
  2724.         $sql "
  2725.         SELECT a.reference,a.libelle,a.id as article_id,a.stock,a.pump,a.cpump,m.date_mouvement,m.raison_mouvement_stock_id,m.quantite,r.libelle as raison_libelle,CONCAT(CONCAT(u.prenom, ' '), u.nom) as utilisateur_libelle
  2726.         FROM `article__mouvement_stock` as m
  2727.         INNER JOIN article__article as a ON a.id = m.article_id
  2728.         INNER JOIN article__raison_mouvement_stock as r ON r.id = m.raison_mouvement_stock_id
  2729.         LEFT JOIN utilisateur__utilisateurs as u ON u.id = m.utilisateur_id
  2730.         " $join "
  2731.         WHERE
  2732.         m.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "'
  2733.         " $where "
  2734.         ORDER BY m.id DESC
  2735.         ";
  2736.         if (intval($parametres['nbel']) > 0) {
  2737.             $sql .= " LIMIT 0," intval($parametres['nbel']);
  2738.         }// else $sql .= " LIMIT 0,10";
  2739.         $sql trim($sql);
  2740.         //echo "<br/><br/>".$sql;
  2741.         //exit;
  2742.         $em       $this->getEntityManager();
  2743.         $stmt     $em->getConnection()->prepare($sql);
  2744.         $articles $stmt->executeQuery()->fetchAllAssociative();
  2745.         //print_r($articles);
  2746.         return $articles;
  2747.     }
  2748.     public function mortArticles($parametres = [])
  2749.     {
  2750.         if (intval($parametres['nbel']) > 5000) {
  2751.             $parametres['nbel'] = 5000;
  2752.         }
  2753.         $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  2754.         $dateDebut->setTime(000000);
  2755.         //$dateFin = \DateTime::createFromFormat('d/m/Y',$parametres['date_fin']);
  2756.         //$dateFin->setTime(23,59,59);
  2757.         $where              "";
  2758.         $join               "";
  2759.         $join_table_client  false;
  2760.         $join_table_article false;
  2761.         $join_table_article true;
  2762.         $repo_raison_mvmt $this->getEntityManager()->getRepository(RaisonMouvementStock::class);
  2763.         $raisons_mvmt     $repo_raison_mvmt->findBy([], ['libelle' => 'ASC']);
  2764.         $where "(a.prestation != 1 or a.prestation IS NULL)";
  2765.         $where .= " AND (a.non_stocke IS NULL OR a.non_stocke = 0) AND (a.divers IS NULL OR a.divers = 0) AND (a.divers_composes IS NULL OR a.divers_composes = 0)";
  2766.         $where_origine "";
  2767.         $in_origine    "";
  2768.         foreach ($raisons_mvmt as $r) {
  2769.             if (array_key_exists('origine_' $r->getId(), $parametres)) {
  2770.                 $in_origine .= "'" $r->getId() . "',";
  2771.             }
  2772.         }
  2773.         if ($in_origine != "") {
  2774.             $in_origine    .= ",";
  2775.             $in_origine    str_replace(",,"""$in_origine);
  2776.             $where_origine .= " AND m.raison_mouvement_stock_id IN (" $in_origine ")";
  2777.         }
  2778.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  2779.             if ($where != "") {
  2780.                 $where .= " AND";
  2781.             }
  2782.             $where              .= " a.marque_id = '" $parametres["marque"] . "'";
  2783.             $join_table_article true;
  2784.         }
  2785.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  2786.             $where              .= " AND a.id = '" $parametres["article"] . "'";
  2787.             $join_table_article true;
  2788.         }
  2789.         if (array_key_exists('ref_article'$parametres) and $parametres["ref_article"] != "") {
  2790.             $parametres["ref_article"] = str_replace("*""%"$parametres["ref_article"]);
  2791.             if ($where != "") {
  2792.                 $where .= " AND";
  2793.             }
  2794.             $where              .= " a.reference like '" $parametres["ref_article"] . "%'";
  2795.             $join_table_article true;
  2796.         }
  2797.         if (array_key_exists('art_stock'$parametres) and $parametres["art_stock"] == "1") {
  2798.             if ($where != "") {
  2799.                 $where .= " AND";
  2800.             }
  2801.             $where              .= " a.stock > 0";
  2802.             $join_table_article true;
  2803.         }
  2804.         if (array_key_exists('type_article'$parametres) and $parametres["type_article"] != "") {
  2805.             if ($parametres["type_article"] != "tous") {
  2806.                 if ($where != "") {
  2807.                     $where .= " AND";
  2808.                 }
  2809.                 if ($parametres["type_article"] == "non_reappro") {
  2810.                     $where .= " a.arret_gamme = 1";
  2811.                 } elseif ($parametres["type_article"] == "reappro") {
  2812.                     $where .= " a.arret_gamme != 1";
  2813.                 }
  2814.             }
  2815.         }
  2816.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  2817.             /*
  2818.             if(!$join_table_article) {
  2819.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2820.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2821.             }
  2822.             */
  2823.             $join .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = a.id";
  2824.             if ($where != "") {
  2825.                 $where .= " AND";
  2826.             }
  2827.             $where              .= " artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  2828.             $join_table_article true;
  2829.         }
  2830.         //echo "type_article ".$parametres["type_article"]."<br/>";
  2831.         //echo "WHERE ".$where."<br/>";
  2832.         if ($where != "") {
  2833.             $where .= " AND";
  2834.         }
  2835.         $sql "
  2836.             SELECT a.reference,a.libelle,a.id as article_id,a.stock,a.pump,a.cpump
  2837.             FROM `article__article` as a
  2838.             " $join "
  2839.             WHERE
  2840.             " $where "
  2841.             a.id NOT IN
  2842.             (
  2843.                 SELECT article_id
  2844.                 FROM article__mouvement_stock as m
  2845.                 WHERE
  2846.                 m.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "'
  2847.                 " $where_origine "
  2848.                 GROUP BY article_id
  2849.             )
  2850.             GROUP BY article_id
  2851.             ORDER BY a.reference
  2852.         ";
  2853.         if (intval($parametres['nbel']) > 0) {
  2854.             $sql .= " LIMIT 0," intval($parametres['nbel']);
  2855.         }// else $sql .= " LIMIT 0,10";
  2856.         /*
  2857.         $sql = "
  2858.             SELECT a.reference,a.libelle,a.id as article_id
  2859.             FROM `article__article` as a
  2860.             LEFT JOIN article__mouvement_stock as m on a.id = m.article_id
  2861.             ".$join."
  2862.             WHERE
  2863.             m.date_mouvement >= '".$dateDebut->format('Y-m-d H:i:s')."'
  2864.             ".$where."
  2865.             GROUP BY article_id
  2866.             LIMIT 0,".intval($parametres['nbel'])."
  2867.         ";
  2868.         */
  2869.         //echo $sql;
  2870.         /*$sql = "
  2871.             SELECT SUM(ac.total_ht) as total,ar.reference,ar.libelle,ar.id as article_id
  2872.             FROM `commerciale__article_commande` as ac
  2873.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  2874.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  2875.             LEFT JOIN client__client as u on u.id = c.client_id
  2876.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  2877.             ".$join."
  2878.             WHERE
  2879.             c.date >= '".$dateDebut->format('Y-m-d H:i:s')."'
  2880.             AND
  2881.             (
  2882.             (c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23 )
  2883.             OR
  2884.             (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  2885.             AND f.statut_commande_id != 23
  2886.             )
  2887.             )
  2888.             ".$where."
  2889.             GROUP BY ar.id
  2890.             ORDER BY total DESC
  2891.             LIMIT 0,".intval($parametres['nbel'])."
  2892.         ";
  2893.         */
  2894.         $sql trim($sql);
  2895.         //echo "<br/><br/>".$sql;
  2896.         $em       $this->getEntityManager();
  2897.         $stmt     $em->getConnection()->prepare($sql);
  2898.         $articles $stmt->executeQuery()->fetchAllAssociative();
  2899.         //print_r($articles);
  2900.         return $articles;
  2901.     }
  2902.     public function topArticles($parametres = [])
  2903.     {
  2904.         //if(intval($parametres['nbel'])>500) $parametres['nbel'] = 500;
  2905.         $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  2906.         $dateDebut->setTime(000000);
  2907.         $dateFin DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  2908.         $dateFin->setTime(235959);
  2909.         $where              "";
  2910.         $join               "";
  2911.         $join_table_client  false;
  2912.         $join_table_article false;
  2913.         $join_table_article true;
  2914.         if (array_key_exists('ref_client'$parametres) and $parametres["ref_client"] > 0) {
  2915.             $join              .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2916.             $where             .= " AND clt.reference = '" $parametres["ref_client"] . "'";
  2917.             $join_table_client true;
  2918.         }
  2919.         if (array_key_exists('client'$parametres) and $parametres["client"] != "") {
  2920.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2921.             $where             .= " AND u.id = '" $parametres["client"] . "'";
  2922.             $join_table_client true;
  2923.         }
  2924.         if (array_key_exists('nom_client'$parametres) and $parametres["nom_client"] != "") {
  2925.             if (!$join_table_client) {
  2926.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2927.             }
  2928.             $where             .= " AND clt.nom like '%" $parametres["nom_client"] . "%'";
  2929.             $join_table_client true;
  2930.         }
  2931.         if (array_key_exists('equipier'$parametres) and $parametres["equipier"] > 0) {
  2932.             $where .= " AND c.utilisateur_id = '" $parametres["equipier"] . "'";
  2933.         }
  2934.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  2935.             //echo "MARQUE";
  2936.             if (!$join_table_article) {
  2937.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2938.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2939.             }
  2940.             $where              .= " AND ar.marque_id = '" $parametres["marque"] . "'";
  2941.             $join_table_article true;
  2942.         }
  2943.         if (array_key_exists('ref_article'$parametres) and $parametres["ref_article"] != "") {
  2944.             if (!$join_table_article) {
  2945.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2946.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2947.             }
  2948.             $parametres["ref_article"] = str_replace("*""%"$parametres["ref_article"]);
  2949.             $where                     .= " AND ar.reference like '" $parametres["ref_article"] . "%'";
  2950.             $join_table_article        true;
  2951.         }
  2952.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  2953.             if (!$join_table_article) {
  2954.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2955.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2956.             }
  2957.             $where              .= " AND ar.id = '" $parametres["article"] . "'";
  2958.             $join_table_article true;
  2959.         }
  2960.         /*
  2961.         if(array_key_exists('article', $parametres) and $parametres["article"] > 0) {
  2962.             if(!$join_table_article) {
  2963.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2964.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2965.             }
  2966.             $where .= " AND ar.id = '".$parametres["article"]."'";
  2967.             $join_table_article = true;
  2968.         }
  2969.         */
  2970.         if (array_key_exists('categorie_client'$parametres) and $parametres["categorie_client"] > 0) {
  2971.             if (!$join_table_client) {
  2972.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  2973.             }
  2974.             //$join .= " LEFT JOIN client__client_categorie as cltcat on cltcat.client_id = u.id";
  2975.             if (array_key_exists('sous_categorie_client'$parametres) and $parametres["sous_categorie_client"] > 0) {
  2976.                 //$where .= " AND cltcat.categorie_id = '".$parametres["sous_categorie_client"]."'";
  2977.                 $where .= " AND clt.id IN  (select distinct(client_id) from client__client_categorie where dateSuppression is null and categorie_id = " $parametres["sous_categorie_client"] . ")";
  2978.             } else {
  2979.                 //$where .= " AND cltcat.categorie_id = '".$parametres["categorie_client"]."'";
  2980.                 $where .= " AND clt.id IN  (select distinct(client_id) from client__client_categorie where dateSuppression is null and categorie_id = " $parametres["categorie_client"] . ")";
  2981.             }
  2982.         }
  2983.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  2984.             if (!$join_table_article) {
  2985.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2986.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  2987.             }
  2988.             //$join .= " RIGHT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  2989.             //$where .= " AND artcat.categorie_id = '".$parametres["categorie_article"]."'";
  2990.             $join_table_article true;
  2991.             $where .= " AND ( ar.id IN  (select distinct(article_id) from article__article_categorie where categorie_id = " $parametres["categorie_article"] . ")";
  2992.             if (array_key_exists('sous_categorie_article'$parametres) and $parametres["sous_categorie_article"] > 0) {
  2993.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  2994.                 //$join .= " LEFT JOIN article__article as ar2 on ar2.id = ac.article_id";
  2995.                 //$join .= " LEFT JOIN article__article_categorie as artcat2 on artcat2.article_id = ar2.id";
  2996.                 //$where .= " AND artcat2.categorie_id = '".$parametres["sous_categorie_article"]."'";
  2997.                 $join_table_article true;
  2998.                 $inclure_frais_de_port false;
  2999.                 $where .= " AND ar.id IN  (select distinct(article_id) from article__article_categorie where categorie_id = " $parametres["sous_categorie_article"] . ")";
  3000.                 if (array_key_exists('sous_sous_categorie_article'$parametres) and $parametres["sous_sous_categorie_article"] > 0) {
  3001.                     //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3002.                     //$join .= " LEFT JOIN article__article as ar3 on ar3.id = ac.article_id";
  3003.                     //$join .= " LEFT JOIN article__article_categorie as artcat3 on artcat3.article_id = ar2.id";
  3004.                     //$where .= " AND artcat3.categorie_id = '".$parametres["sous_sous_categorie_article"]."'";
  3005.                     $join_table_article true;
  3006.                     $inclure_frais_de_port false;
  3007.                     $where .= " AND ar.id IN  (select distinct(article_id) from article__article_categorie where categorie_id = " $parametres["sous_sous_categorie_article"] . ")";
  3008.                 }
  3009.             }
  3010.             $where .= ")";
  3011.         }
  3012.         /*
  3013.         SELECT SUM(ac.total_ht) as total,ar.reference,ar.libelle,ar.id as article_id
  3014. FROM `commerciale__article_commande` as ac
  3015. LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  3016. LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3017. LEFT JOIN client__client as u on u.id = c.client_id
  3018. LEFT JOIN article__article as ar on ar.id = ac.article_id
  3019. WHERE c.date >= '2016-08-13 00:00:00' AND c.date <= '2017-09-13 23:59:59'
  3020. AND
  3021. (
  3022. (c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23 )
  3023. OR
  3024. (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 24 AND c.facture_id > 0
  3025. AND f.statut_commande_id != 23
  3026. )
  3027. )
  3028. GROUP BY ar.id
  3029. ORDER BY total DESC LIMIT 0,500
  3030. */
  3031.         //print_r($parametres);
  3032.         $order "total";
  3033.         if (array_key_exists('tri'$parametres) and $parametres["tri"] == "qte") {
  3034.             $order "total_quantite";
  3035.         }
  3036.         $sql "
  3037.             SELECT SUM(COALESCE(ac.total_ht,0)) as total,SUM(COALESCE(ac.quantite,0)) as total_quantite,ar.reference,ar.libelle,ar.id as article_id
  3038.             FROM `commerciale__article_commande` as ac
  3039.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  3040.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3041.             LEFT JOIN client__client as u on u.id = c.client_id
  3042.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  3043.             " $join "
  3044.             WHERE
  3045.             c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  3046.             AND
  3047.             (
  3048.             (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19)
  3049.             OR
  3050.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 AND c.multi_bl != 1)
  3051.             OR
  3052.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3053.             AND f.statut_commande_id != 23
  3054.             )
  3055.             )
  3056.             " $where "
  3057.             GROUP BY ar.id
  3058.             ORDER BY " $order " DESC
  3059.             LIMIT 0," intval($parametres['nbel']) . "
  3060.         ";
  3061.         /*$sql = "
  3062.             SELECT SUM(ac.total_ht) as total,ar.reference,ar.libelle,ar.id as article_id
  3063.             FROM `commerciale__article_commande` as ac
  3064.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  3065.             LEFT JOIN client__client as u on u.id = c.client_id
  3066.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  3067.             ".$join."
  3068.             WHERE
  3069.             c.date >= '".$dateDebut->format('Y-m-d H:i:s')."' AND c.date <= '".$dateFin->format('Y-m-d H:i:s')."'
  3070.             AND
  3071.             c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE."
  3072.             AND
  3073.             c.statut_commande_id != 23
  3074.             ".$where."
  3075.             GROUP BY ar.id
  3076.             ORDER BY total DESC
  3077.             LIMIT 0,".intval($parametres['nbel'])."
  3078.         ";
  3079.         */
  3080.         $sql trim($sql);
  3081.         //echo "<br/><br/>".$sql;
  3082.         $em       $this->getEntityManager();
  3083.         $stmt     $em->getConnection()->prepare($sql);
  3084.         $articles $stmt->executeQuery()->fetchAllAssociative();
  3085.         //print_r($articles);
  3086.         return $articles;
  3087.     }
  3088.     public function topMarquesDetails($parametres = [])
  3089.     {
  3090.         if (!array_key_exists('n1'$parametres)) {
  3091.             $parametres["n1"] = "0";
  3092.         }
  3093.         if (!array_key_exists('details_ajax'$parametres)) {
  3094.             $parametres["details_ajax"] = "0";
  3095.         }
  3096.         //print_r($parametres);
  3097.         //if(intval($parametres['nbel'])>500) $parametres['nbel'] = 500;
  3098.         $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  3099.         $dateDebut->setTime(000000);
  3100.         $dateFin DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  3101.         $dateFin->setTime(235959);
  3102.         $where              "";
  3103.         $join               "";
  3104.         $join_table_client  false;
  3105.         $join_table_article true;
  3106.         $inclure_frais_de_port true;
  3107.         $inclure_transporteur  true;
  3108.         if (array_key_exists('ref_client'$parametres) and $parametres["ref_client"] > 0) {
  3109.             $join              .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  3110.             $where             .= " AND clt.reference like '" $parametres["ref_client"] . "%'";
  3111.             $join_table_client true;
  3112.         }
  3113.         if (array_key_exists('nom_client'$parametres) and $parametres["nom_client"] != "") {
  3114.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  3115.             $where             .= " AND u.nom like '%" $parametres["nom_client"] . "%'";
  3116.             $join_table_client true;
  3117.         }
  3118.         if (array_key_exists('client'$parametres) and $parametres["client"] != "") {
  3119.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  3120.             $where             .= " AND u.id = '" $parametres["client"] . "'";
  3121.             $join_table_client true;
  3122.         }
  3123.         if (array_key_exists('equipier'$parametres) and $parametres["equipier"] > 0) {
  3124.             $where .= " AND c.utilisateur_id = '" $parametres["equipier"] . "'";
  3125.         }
  3126.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  3127.             //echo "MARQUE";
  3128.             //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3129.             //$join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  3130.             $where              .= " AND a.marque_id = '" $parametres["marque"] . "'";
  3131.             $join_table_article true;
  3132.             $inclure_frais_de_port false;
  3133.         }
  3134.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  3135.             if (!$join_table_article) {
  3136.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3137.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  3138.             }
  3139.             $where              .= " AND a.id = '" $parametres["article"] . "'";
  3140.             $join_table_article true;
  3141.             $inclure_frais_de_port false;
  3142.         }
  3143.         if (array_key_exists('categorie_client'$parametres) and $parametres["categorie_client"] > 0) {
  3144.             if (!$join_table_client) {
  3145.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  3146.             }
  3147.             $join .= " LEFT JOIN client__client_categorie as cltcat on cltcat.client_id = u.id";
  3148.             if (array_key_exists('sous_categorie_client'$parametres) and $parametres["sous_categorie_client"] > 0) {
  3149.                 $where .= " AND cltcat.categorie_id = '" $parametres["sous_categorie_client"] . "'";
  3150.             } else {
  3151.                 $where .= " AND cltcat.categorie_id = '" $parametres["categorie_client"] . "'";
  3152.             }
  3153.         }
  3154.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  3155.             if (!$join_table_article) {
  3156.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3157.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  3158.             }
  3159.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  3160.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  3161.             $join_table_article true;
  3162.             $inclure_frais_de_port false;
  3163.             if (array_key_exists('sous_categorie_article'$parametres) and $parametres["sous_categorie_article"] > 0) {
  3164.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3165.                 $join .= " LEFT JOIN article__article as ar2 on ar2.id = ac.article_id";
  3166.                 $join               .= " LEFT JOIN article__article_categorie as artcat2 on artcat2.article_id = ar2.id";
  3167.                 $where              .= " AND artcat2.categorie_id = '" $parametres["sous_categorie_article"] . "'";
  3168.                 $join_table_article true;
  3169.                 $inclure_frais_de_port false;
  3170.                 if (array_key_exists('sous_sous_categorie_article'$parametres) and $parametres["sous_sous_categorie_article"] > 0) {
  3171.                     //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3172.                     $join               .= " LEFT JOIN article__article as ar3 on ar3.id = ac.article_id";
  3173.                     $join               .= " LEFT JOIN article__article_categorie as artcat3 on artcat3.article_id = ar2.id";
  3174.                     $where              .= " AND artcat3.categorie_id = '" $parametres["sous_sous_categorie_article"] . "'";
  3175.                     $join_table_article true;
  3176.                     $inclure_frais_de_port false;
  3177.                 }
  3178.             }
  3179.         }
  3180.         $where .= " AND a.marque_id = " $parametres["marque_id"];
  3181.         $order_by "total DESC    ";
  3182.         if (array_key_exists('tri'$parametres) && $parametres["tri"] == "qte") {
  3183.             $order_by "total_qte DESC    ";
  3184.         }
  3185.         $date_n1_f "";
  3186.         $date_n1_c "";
  3187.         if ($parametres["n1"] == "1") {
  3188.             $dateDebutN1 DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  3189.             $dateDebutN1->setTime(000000);
  3190.             $dateDebutN1 $dateDebutN1->modify('-1 year');
  3191.             $dateFinN1 DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  3192.             $dateFinN1->setTime(235959);
  3193.             $dateFinN1 $dateFinN1->modify('-1 year');
  3194.             $date_n1_f " OR (f.date >= '" $dateDebutN1->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFinN1->format('Y-m-d H:i:s') . "')";
  3195.             $date_n1_c " OR (c.date >= '" $dateDebutN1->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFinN1->format('Y-m-d H:i:s') . "')";
  3196.         }
  3197.         $sql "
  3198.             SELECT sum(COALESCE(ac.quantite,0)) as total_qte,ac.libelle,sum(COALESCE(ac.total_ht,0)) as total,a.reference,a.id as article_id,c.date as date_c,f.date as date_f
  3199.             FROM `commerciale__article_commande` as ac
  3200.             LEFT JOIN commerciale__commande as c on c.id = ac.commande_id
  3201.             LEFT JOIN article__article as a on a.id = ac.article_id
  3202.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3203.             LEFT JOIN client__client as u on u.id = c.client_id
  3204.             LEFT JOIN article__marque as m on m.id = a.marque_id
  3205.             " $join "
  3206.             WHERE
  3207.             (
  3208.             (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19 AND
  3209.             ((c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  3210.             )
  3211.             OR
  3212.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND
  3213.             ((c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  3214.             )
  3215.             OR
  3216.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3217.             AND f.statut_commande_id != 23
  3218.             and
  3219.             ((f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  3220.             )
  3221.             )
  3222.             " $where "
  3223.             GROUP BY ac.article_id
  3224.             ORDER BY " $order_by "
  3225.         ";
  3226.         $sql trim($sql);
  3227.         if ($parametres["article"] == "218552" or == 1) {
  3228.             //echo $sql;
  3229.             //exit;
  3230.         }
  3231.         $em       $this->getEntityManager();
  3232.         $stmt     $em->getConnection()->prepare($sql);
  3233.         $articles $stmt->executeQuery()->fetchAllAssociative();
  3234.         if (count($articles) > && $parametres["n1"] == "1") {
  3235.             $tab_article_inclus = [];
  3236.             foreach ($articles as $a) {
  3237.                 if ($a["article_id"] != "") {
  3238.                     $tab_article_inclus[] = $a["article_id"];
  3239.                 }
  3240.             }
  3241.             $sql "
  3242.                 SELECT sum(COALESCE(ac.quantite,0)) as total_qte,ac.libelle,sum(COALESCE(ac.total_ht,0)) as total,a.reference,a.id as article_id,c.date as date_c,f.date as date_f
  3243.                 FROM `commerciale__article_commande` as ac
  3244.                 LEFT JOIN commerciale__commande as c on c.id = ac.commande_id
  3245.                 LEFT JOIN article__article as a on a.id = ac.article_id
  3246.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3247.                 LEFT JOIN client__client as u on u.id = c.client_id
  3248.                 LEFT JOIN article__marque as m on m.id = a.marque_id
  3249.                 " $join "
  3250.                 WHERE
  3251.                 (
  3252.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND
  3253.                 ((c.date >= '" $dateDebutN1->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFinN1->format('Y-m-d H:i:s') . "'))
  3254.                 )
  3255.                 OR
  3256.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3257.                 AND f.statut_commande_id != 23
  3258.                 and
  3259.                 ((f.date >= '" $dateDebutN1->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFinN1->format('Y-m-d H:i:s') . "'))
  3260.                 )
  3261.                 )
  3262.                 " $where "
  3263.                 GROUP BY ac.article_id
  3264.                 ORDER BY " $order_by "
  3265.             ";
  3266.             $sql trim($sql);
  3267.             $em          $this->getEntityManager();
  3268.             $stmt        $em->getConnection()->prepare($sql);
  3269.             $articles_n1 $stmt->executeQuery()->fetchAllAssociative();
  3270.             if (count($articles_n1) > 0) {
  3271.                 foreach ($articles_n1 as $an1) {
  3272.                     if (!in_array($an1["article_id"], $tab_article_inclus)) {
  3273.                         $tab_article_n1                    = [];
  3274.                         $tab_article_n1["total_qte"]       = 0;
  3275.                         $tab_article_n1["libelle"]         = $an1["libelle"];
  3276.                         $tab_article_n1["total"]           = 0;
  3277.                         $tab_article_n1["reference"]       = $an1["reference"];
  3278.                         $tab_article_n1["article_id"]      = $an1["article_id"];
  3279.                         $tab_article_n1["transporteur_id"] = "";
  3280.                         $articles[]                        = $tab_article_n1;
  3281.                     }
  3282.                 }
  3283.             }
  3284.         }
  3285.         if (array_key_exists('tri'$parametres) && $parametres["tri"] == "qte") {
  3286.             $qte = [];
  3287.             foreach ($articles as $key => $row) {
  3288.                 $qte[$key] = floatVal($row['total_qte']);
  3289.             }
  3290.             array_multisort($qteSORT_DESC$articles);
  3291.         } elseif (array_key_exists('tri'$parametres) && $parametres["tri"] == "ca") {
  3292.             $qte = [];
  3293.             foreach ($articles as $key => $row) {
  3294.                 $qte[$key] = floatVal($row['total']);
  3295.             }
  3296.             array_multisort($qteSORT_DESC$articles);
  3297.         }
  3298.         return $articles;
  3299.     }
  3300.     public function topMarques($parametres = [])
  3301.     {
  3302.         if (intval($parametres['nbel']) > 500) {
  3303.             $parametres['nbel'] = 500;
  3304.         }
  3305.         $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  3306.         $dateDebut->setTime(000000);
  3307.         $dateFin DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  3308.         $dateFin->setTime(235959);
  3309.         $where              "";
  3310.         $join               "";
  3311.         $join_table_client  false;
  3312.         $join_table_article false;
  3313.         $join_table_article true;
  3314.         if (array_key_exists('ref_client'$parametres) and $parametres["ref_client"] > 0) {
  3315.             $join              .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  3316.             $where             .= " AND clt.reference = '" $parametres["ref_client"] . "'";
  3317.             $join_table_client true;
  3318.         }
  3319.         if (array_key_exists('client'$parametres) and $parametres["client"] != "") {
  3320.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  3321.             $where             .= " AND u.id = '" $parametres["client"] . "'";
  3322.             $join_table_client true;
  3323.         }
  3324.         if (array_key_exists('nom_client'$parametres) and $parametres["nom_client"] != "") {
  3325.             if (!$join_table_client) {
  3326.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  3327.             }
  3328.             $where             .= " AND clt.nom like '%" $parametres["nom_client"] . "%'";
  3329.             $join_table_client true;
  3330.         }
  3331.         if (array_key_exists('equipier'$parametres) and $parametres["equipier"] > 0) {
  3332.             $where .= " AND c.utilisateur_id = '" $parametres["equipier"] . "'";
  3333.         }
  3334.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  3335.             //echo "MARQUE";
  3336.             if (!$join_table_article) {
  3337.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3338.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  3339.             }
  3340.             $where              .= " AND ar.marque_id = '" $parametres["marque"] . "'";
  3341.             $join_table_article true;
  3342.         }
  3343.         if (array_key_exists('ref_article'$parametres) and $parametres["ref_article"] != "") {
  3344.             if (!$join_table_article) {
  3345.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3346.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  3347.             }
  3348.             $parametres["ref_article"] = str_replace("*""%"$parametres["ref_article"]);
  3349.             $where                     .= " AND ar.reference like '" $parametres["ref_article"] . "%'";
  3350.             $join_table_article        true;
  3351.         }
  3352.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  3353.             if (!$join_table_article) {
  3354.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3355.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  3356.             }
  3357.             $where              .= " AND ar.id = '" $parametres["article"] . "'";
  3358.             $join_table_article true;
  3359.         }
  3360.         /*
  3361.         if(array_key_exists('article', $parametres) and $parametres["article"] > 0) {
  3362.             if(!$join_table_article) {
  3363.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3364.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  3365.             }
  3366.             $where .= " AND ar.id = '".$parametres["article"]."'";
  3367.             $join_table_article = true;
  3368.         }
  3369.         */
  3370.         if (array_key_exists('categorie_client'$parametres) and $parametres["categorie_client"] > 0) {
  3371.             if (!$join_table_client) {
  3372.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  3373.             }
  3374.             $join .= " LEFT JOIN client__client_categorie as cltcat on cltcat.client_id = u.id";
  3375.             if (array_key_exists('sous_categorie_client'$parametres) and $parametres["sous_categorie_client"] > 0) {
  3376.                 $where .= " AND cltcat.categorie_id = '" $parametres["sous_categorie_client"] . "'";
  3377.             } else {
  3378.                 $where .= " AND cltcat.categorie_id = '" $parametres["categorie_client"] . "'";
  3379.             }
  3380.         }
  3381.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  3382.             if (!$join_table_article) {
  3383.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3384.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  3385.             }
  3386.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  3387.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  3388.             $join_table_article true;
  3389.             if (array_key_exists('sous_categorie_article'$parametres) and $parametres["sous_categorie_article"] > 0) {
  3390.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3391.                 $join               .= " LEFT JOIN article__article as ar2 on ar2.id = ac.article_id";
  3392.                 $join               .= " LEFT JOIN article__article_categorie as artcat2 on artcat2.article_id = ar2.id";
  3393.                 $where              .= " AND artcat2.categorie_id = '" $parametres["sous_categorie_article"] . "'";
  3394.                 $join_table_article true;
  3395.                 $inclure_frais_de_port false;
  3396.                 if (array_key_exists('sous_sous_categorie_article'$parametres) and $parametres["sous_sous_categorie_article"] > 0) {
  3397.                     //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  3398.                     $join .= " LEFT JOIN article__article as ar3 on ar3.id = ac.article_id";
  3399.                     $join               .= " LEFT JOIN article__article_categorie as artcat3 on artcat3.article_id = ar2.id";
  3400.                     $where              .= " AND artcat3.categorie_id = '" $parametres["sous_sous_categorie_article"] . "'";
  3401.                     $join_table_article true;
  3402.                     $inclure_frais_de_port false;
  3403.                 }
  3404.             }
  3405.         }
  3406.         //print_r($parametres);
  3407.         $order "total";
  3408.         if (array_key_exists('tri'$parametres) and $parametres["tri"] == "qte") {
  3409.             $order "total_quantite";
  3410.         }
  3411.         $sql "
  3412.             SELECT SUM(COALESCE(ac.total_ht,0)) as total,SUM(COALESCE(ac.quantite,0)) as total_quantite,m.reference,m.libelle,m.id as marque_id
  3413.             FROM `commerciale__article_commande` as ac
  3414.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  3415.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  3416.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3417.             LEFT JOIN client__client as u on u.id = c.client_id
  3418.             LEFT JOIN article__marque as m on m.id = ar.marque_id
  3419.             " $join "
  3420.             WHERE
  3421.             m.id is not null AND
  3422.             c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  3423.             AND
  3424.             (
  3425.             (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19)
  3426.             OR
  3427.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 AND c.multi_bl != 1)
  3428.             OR
  3429.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3430.             AND f.statut_commande_id != 23
  3431.             )
  3432.             )
  3433.             " $where "
  3434.             GROUP BY m.id
  3435.             ORDER BY " $order " DESC
  3436.             LIMIT 0," intval($parametres['nbel']) . "
  3437.         ";
  3438.         $sql trim($sql);
  3439.         //echo "<br/><br/>".$sql;
  3440.         $em       $this->getEntityManager();
  3441.         $stmt     $em->getConnection()->prepare($sql);
  3442.         $articles $stmt->executeQuery()->fetchAllAssociative();
  3443.         //print_r($articles);
  3444.         return $articles;
  3445.     }
  3446.     public function panierMoyen($id_client)
  3447.     {
  3448.         $sql "
  3449.                 SELECT AVG(c.total) as total
  3450.                 FROM `commerciale__commande` as c
  3451.                 WHERE
  3452.                 c.client_id = " $id_client "
  3453.                 AND
  3454.                 c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE "
  3455.                 AND
  3456.                 c.statut_commande_id != 23
  3457.             ";
  3458.         $sql trim($sql);
  3459.         //echo "<br/><br/>".$sql;
  3460.         $em   $this->getEntityManager();
  3461.         $stmt $em->getConnection()->prepare($sql);
  3462.         $ca   $stmt->executeQuery()->fetchAllAssociative();
  3463.         return $ca[0]['total'];
  3464.     }
  3465.     public function caClient($parametres = [])
  3466.     {
  3467.         $parametres['client'] = intval($parametres['client']);
  3468.         $sql "
  3469.                 SELECT SUM(COALESCE(c.total,0)+COALESCE(c.frais_Port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total
  3470.                 FROM `commerciale__commande` as c
  3471.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3472.                 LEFT JOIN client__client as u on u.id = c.client_id
  3473.                 WHERE
  3474.                 (
  3475.                 (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND c.multi_bl != 1 AND c.date >= '" $parametres['dateDebut']->format(
  3476.                 'Y-m-d H:i:s'
  3477.             ) . "' AND c.date <= '" $parametres['dateFin']->format('Y-m-t H:i:s') . "')
  3478.                 OR
  3479.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND c.date >= '" $parametres['dateDebut']->format(
  3480.                 'Y-m-d H:i:s'
  3481.             ) . "' AND c.date <= '" $parametres['dateFin']->format('Y-m-t H:i:s') . "')
  3482.                 OR
  3483.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3484.                 AND f.statut_commande_id != 23
  3485.                 and
  3486.                 ((f.date >= '" $parametres['dateDebut']->format('Y-m-d H:i:s') . "' AND f.date <= '" $parametres['dateFin']->format('Y-m-t H:i:s') . "'))
  3487.                 )
  3488.                 )
  3489.                 AND
  3490.                 c.client_id = " $parametres['client'] . "
  3491.             ";
  3492.         //echo "<br/><br/>".$sql;
  3493.         $sql trim($sql);
  3494.         //echo "<br/><br/>".$sql;
  3495.         $em   $this->getEntityManager();
  3496.         $stmt $em->getConnection()->prepare($sql);
  3497.         $ca   $stmt->executeQuery()->fetchAllAssociative();
  3498.         $total $ca[0]['total'];
  3499.         return $total;
  3500.     }
  3501.     public function caAnnee($client)
  3502.     {
  3503.         $dateDebut = (new Datetime('first day of January this year'))->setTime(000);;
  3504.         $dateFin = new Datetime();
  3505.         $sql "
  3506.                 SELECT SUM(COALESCE(c.total,0)+COALESCE(c.frais_Port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total
  3507.                 FROM `commerciale__commande` as c
  3508.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3509.                 LEFT JOIN client__client as u on u.id = c.client_id
  3510.                 WHERE
  3511.                 (
  3512.                 (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format(
  3513.                 'Y-m-d H:i:s'
  3514.             ) . "' AND c.date <= '" $dateFin->format('Y-m-t H:i:s') . "')
  3515.                 OR
  3516.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format(
  3517.                 'Y-m-d H:i:s'
  3518.             ) . "' AND c.date <= '" $dateFin->format('Y-m-t H:i:s') . "')
  3519.                 OR
  3520.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3521.                 AND f.statut_commande_id != 23
  3522.                 and
  3523.                 ((f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-t H:i:s') . "'))
  3524.                 )
  3525.                 )
  3526.                 AND
  3527.                 c.client_id = " $client->getId() . "
  3528.             ";
  3529.         //echo "<br/><br/>".$sql;
  3530.         $sql trim($sql);
  3531.         //echo "<br/><br/>".$sql;
  3532.         $em   $this->getEntityManager();
  3533.         $stmt $em->getConnection()->prepare($sql);
  3534.         $ca   $stmt->executeQuery()->fetchAllAssociative();
  3535.         $total $ca[0]['total'];
  3536.         return $total;
  3537.     }
  3538.     public function caAnneeN1($client)
  3539.     {
  3540.         $dateDebut = (new Datetime('first day of January this year'))->modify('-1 year')->setTime(000);
  3541.         $dateFin   = (new Datetime('last day of December this year'))->modify('-1 year')->setTime(235959);
  3542.         $sql "
  3543.                 SELECT SUM(COALESCE(c.total,0)+COALESCE(c.frais_Port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total
  3544.                 FROM `commerciale__commande` as c
  3545.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3546.                 LEFT JOIN client__client as u on u.id = c.client_id
  3547.                 WHERE
  3548.                 (
  3549.                 (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format(
  3550.                 'Y-m-d H:i:s'
  3551.             ) . "' AND c.date <= '" $dateFin->format('Y-m-t H:i:s') . "')
  3552.                 OR
  3553.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND c.date >= '" $dateDebut->format(
  3554.                 'Y-m-d H:i:s'
  3555.             ) . "' AND c.date <= '" $dateFin->format('Y-m-t H:i:s') . "')
  3556.                 OR
  3557.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3558.                 AND f.statut_commande_id != 23
  3559.                 and
  3560.                 ((f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-t H:i:s') . "'))
  3561.                 )
  3562.                 )
  3563.                 AND
  3564.                 c.client_id = " $client->getId() . "
  3565.             ";
  3566.         //echo "<br/><br/>".$sql;
  3567.         $sql trim($sql);
  3568.         //echo "<br/><br/>".$sql;
  3569.         $em   $this->getEntityManager();
  3570.         $stmt $em->getConnection()->prepare($sql);
  3571.         $ca   $stmt->executeQuery()->fetchAllAssociative();
  3572.         $total $ca[0]['total'];
  3573.         return $total;
  3574.     }
  3575.     public function caMois($parametres = [])
  3576.     {
  3577.         $parametres['client'] = intval($parametres['client']);
  3578.         if (!array_key_exists('n1'$parametres)) {
  3579.             $parametres['n1'] = 0;
  3580.         }
  3581.         $limit     "10000000";
  3582.         $total     0;
  3583.         $totalTtc     0;
  3584.         $dateDebut DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  3585.         $dateDebut->setTime(000001);
  3586.         $dateFin DateTime::createFromFormat('m/Y'$parametres['date_fin']);
  3587.         $dateFin->setTime(235959);
  3588.         $dateDebutBoucle DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  3589.         $dateDebutBoucle->setTime(000000);
  3590.         $dateFinN1 DateTime::createFromFormat('m/Y'$parametres['date_fin']);
  3591.         $dateFinN1->setTime(235959);
  3592.         $dateFinN1 $dateFinN1->modify('-1 year');
  3593.         $dateDebutBoucleN1 DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  3594.         $dateDebutBoucleN1->setTime(000000);
  3595.         $dateDebutBoucleN1 $dateDebutBoucleN1->modify('-1 year');
  3596.         $totalN1 0;
  3597.         $totalN1Ttc 0;
  3598.         $retour  = [];
  3599.         $retour["totalN1"] = [];
  3600.         while ($dateDebutBoucle $dateFin)
  3601.         {
  3602.             $dateFinBoucle DateTime::createFromFormat('Y-m-d'$dateDebutBoucle->format('Y-m-d'));
  3603.             $dateFinBoucle->setTime(235959);
  3604.             $dateFinBoucleN1 DateTime::createFromFormat('Y-m-d'$dateDebutBoucleN1->format('Y-m-d'));
  3605.             $dateFinBoucleN1->setTime(235959);
  3606.             $mois[] = $dateDebutBoucle->format("m");
  3607.             $valeurs   = [];
  3608.             $valeursN1 = [];
  3609.             if ($dateDebutBoucle->format("m") == "01") {
  3610.                 $valeurs["mois"] = "Janvier";
  3611.             } elseif ($dateDebutBoucle->format("m") == "02") {
  3612.                 $valeurs["mois"] = "Fevrier";
  3613.             } elseif ($dateDebutBoucle->format("m") == "03") {
  3614.                 $valeurs["mois"] = "Mars";
  3615.             } elseif ($dateDebutBoucle->format("m") == "04") {
  3616.                 $valeurs["mois"] = "Avril";
  3617.             } elseif ($dateDebutBoucle->format("m") == "05") {
  3618.                 $valeurs["mois"] = "Mai";
  3619.             } elseif ($dateDebutBoucle->format("m") == "06") {
  3620.                 $valeurs["mois"] = "Juin";
  3621.             } elseif ($dateDebutBoucle->format("m") == "07") {
  3622.                 $valeurs["mois"] = "Juillet";
  3623.             } elseif ($dateDebutBoucle->format("m") == "08") {
  3624.                 $valeurs["mois"] = "Aout";
  3625.             } elseif ($dateDebutBoucle->format("m") == "09") {
  3626.                 $valeurs["mois"] = "Septembre";
  3627.             } elseif ($dateDebutBoucle->format("m") == "10") {
  3628.                 $valeurs["mois"] = "Octobre";
  3629.             } elseif ($dateDebutBoucle->format("m") == "11") {
  3630.                 $valeurs["mois"] = "Novembre";
  3631.             } elseif ($dateDebutBoucle->format("m") == "12") {
  3632.                 $valeurs["mois"] = "Décembre";
  3633.             }
  3634.             $valeurs["annee"]    = $dateDebutBoucle->format("Y");
  3635.             $valeurs["annee_n1"] = $dateDebutBoucleN1->format("Y");
  3636.             $sql "
  3637.                 SELECT SUM(COALESCE(c.total,0)+COALESCE(c.frais_Port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total,SUM(c.total_ttc) as total_ttc
  3638.                 FROM `commerciale__commande` as c
  3639.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3640.                 LEFT JOIN client__client as u on u.id = c.client_id
  3641.                 WHERE
  3642.                 (
  3643.                 (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19  AND c.multi_bl != 1 AND c.date >= '" $dateDebutBoucle->format(
  3644.                     'Y-m-d H:i:s'
  3645.                 ) . "' AND c.date <= '" $dateFinBoucle->format('Y-m-t H:i:s') . "')
  3646.                 OR
  3647.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1 AND c.date >= '" $dateDebutBoucle->format(
  3648.                     'Y-m-d H:i:s'
  3649.                 ) . "' AND c.date <= '" $dateFinBoucle->format('Y-m-t H:i:s') . "')
  3650.                 OR
  3651.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3652.                 AND f.statut_commande_id != 23
  3653.                 and
  3654.                 ((f.date >= '" $dateDebutBoucle->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFinBoucle->format('Y-m-t H:i:s') . "'))
  3655.                 )
  3656.                 )
  3657.                 AND
  3658.                 c.client_id = " $parametres['client'] . "
  3659.             ";
  3660.             $sql trim($sql);
  3661.             $em   $this->getEntityManager();
  3662.             $stmt $em->getConnection()->prepare($sql);
  3663.             $ca   $stmt->executeQuery()->fetchAllAssociative();
  3664.             $valeurs["total"] = $ca[0]['total'];
  3665.             $valeurs["total_ttc"] = $ca[0]['total_ttc'];
  3666.             $dateDebutBoucle->add(new DateInterval('P1M'));
  3667.             if($ca[0]['total']==""$ca[0]['total']=0;
  3668.             if($ca[0]['total_ttc']==""$ca[0]['total_ttc']=0;
  3669.             $total  $total $ca[0]['total'];
  3670.             $totalTtc  $totalTtc $ca[0]['total_ttc'];
  3671.             $retour["ca"][] = $valeurs;
  3672.             if ($parametres['n1'] == "1") {
  3673.                 $sql "
  3674.                     SELECT SUM(COALESCE(c.total,0)+COALESCE(c.frais_Port_supplementaire,0)+COALESCE(c.total_frais_port,0)) as total,SUM(c.total_ttc) as total_ttc
  3675.                     FROM `commerciale__commande` as c
  3676.                     WHERE
  3677.                     c.date >= '" $dateDebutBoucleN1->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFinBoucleN1->format('Y-m-t H:i:s') . "'
  3678.                     AND
  3679.                     c.client_id = " $parametres['client'] . "
  3680.                     AND
  3681.                     c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE "
  3682.                     AND
  3683.                     c.statut_commande_id != 23
  3684.                 ";
  3685.                 $sql trim($sql);
  3686.                 $em   $this->getEntityManager();
  3687.                 $stmt $em->getConnection()->prepare($sql);
  3688.                 $ca   $stmt->executeQuery()->fetchAllAssociative();
  3689.                 $valeursN1["totalN1"] = $ca[0]['total'];
  3690.                 $valeursN1["totalN1_ttc"] = $ca[0]['total_ttc'];
  3691.                 $dateDebutBoucleN1->add(new DateInterval('P1M'));
  3692.                 $totalN1          $totalN1 $ca[0]['total'];
  3693.                 $totalN1Ttc          $totalN1Ttc $ca[0]['total_ttc'];
  3694.                 $retour["caN1"][] = $valeursN1;
  3695.             }
  3696.         }
  3697.         $retour["total"]   = $total;
  3698.         $retour["total_ttc"]   = $totalTtc;
  3699.         $retour["totalN1"] = $totalN1;
  3700.         $retour["totalN1_ttc"] = $totalN1Ttc;
  3701.         $retour["panier"]  = $this->panierMoyen($parametres['client']);
  3702.         return $retour;
  3703.     }
  3704.     public function totalPortMois($idTransporteur$date)
  3705.     {
  3706.         $sql "
  3707.             SELECT SUM(COALESCE(c.total_frais_port_ttc,0)) as total
  3708.             FROM `commerciale__commande` as c
  3709.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3710.             WHERE
  3711.             c.date >= '" $date->format('Y-m-d ') . "00:00:00' AND c.date <= '" $date->format('Y-m-t H:i:s') . "'
  3712.             AND
  3713.             (
  3714.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 AND c.multi_bl != 1)
  3715.             OR
  3716.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3717.             AND f.statut_commande_id != 23
  3718.             )
  3719.             )
  3720.             AND
  3721.             c.transporteur_id = " $idTransporteur "
  3722.         ";
  3723.         $sql "
  3724.             SELECT SUM(COALESCE(c.total_frais_port,0)+COALESCE(c.frais_port_supplementaire,0)) as total
  3725.             FROM `commerciale__commande` as c
  3726.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3727.             WHERE
  3728.             c.date >= '" $date->format('Y-m-d ') . "00:00:00' AND c.date <= '" $date->format('Y-m-t ') . "23:59:59'
  3729.             AND
  3730.             (
  3731.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 AND c.multi_bl != 1)
  3732.             OR
  3733.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND (c.facture_id > 0) AND f.multi_bl = 1
  3734.             AND f.statut_commande_id != 23
  3735.             )
  3736.             )
  3737.             AND
  3738.             c.transporteur_id = " $idTransporteur "
  3739.         ";
  3740.         $sql trim($sql);
  3741.         //echo "<br/><br/>".$sql."<br/><br/>";
  3742.         //exit;
  3743.         $em    $this->getEntityManager();
  3744.         $stmt  $em->getConnection()->prepare($sql);
  3745.         $total $stmt->executeQuery()->fetchAllAssociative();
  3746.         return $total[0]['total'];
  3747.     }
  3748.     public function nbExpeditionsMois($idTransporteur$date)
  3749.     {
  3750.         /*$sql = "
  3751.             SELECT sum(ac.quantite) as total
  3752.             FROM `commerciale__article_commande` as ac
  3753.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  3754.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3755.             WHERE
  3756.             c.date >= '".$date->format('Y-m-d ')."00:00:00' AND c.date <= '".$date->format('Y-m-t H:i:s')."'
  3757.             AND
  3758.             (
  3759.             (c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23 )
  3760.             OR
  3761.             (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3762.             AND f.statut_commande_id != 23
  3763.             )
  3764.             )
  3765.             AND
  3766.             ac.article_id = ".$idArticle."
  3767.         ";
  3768.     */
  3769.         $sql "
  3770.             SELECT count(*) as total
  3771.             FROM `commerciale__commande` as c
  3772.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3773.             WHERE
  3774.             c.date >= '" $date->format('Y-m-d ') . "00:00:00' AND c.date <= '" $date->format('Y-m-t ') . "23:59:59'
  3775.             AND
  3776.             (
  3777.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23  AND c.multi_bl != 1)
  3778.             OR
  3779.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3780.             AND f.statut_commande_id != 23
  3781.             )
  3782.             )
  3783.             AND
  3784.             c.transporteur_id = " $idTransporteur "
  3785.         ";
  3786.         $sql trim($sql);
  3787.         //echo $sql;
  3788.         $em    $this->getEntityManager();
  3789.         $stmt  $em->getConnection()->prepare($sql);
  3790.         $total $stmt->executeQuery()->fetchAllAssociative();
  3791.         return $total[0]['total'];
  3792.     }
  3793.     public function suiviConsommationPort($parametres = [])
  3794.     {
  3795.         $limit     "10000000";
  3796.         $articles  = [];
  3797.         $dateDebut DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  3798.         $dateDebut->setTime(000000);
  3799.         $dateFin DateTime::createFromFormat('m/Y'$parametres['date_fin']);
  3800.         $dateFin->setTime(235959);
  3801.         $where "";
  3802.         $join  "";
  3803.         $sql "
  3804.             SELECT distinct t.id as id_transporteur,t.libelle,t.reference, SUM(COALESCE(c.total_frais_port_ttc,0)) as total
  3805.             FROM `commerciale__commande` as c
  3806.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3807.             LEFT JOIN transporteur__transporteur as t on t.id = c.transporteur_id
  3808.             " $join "
  3809.             WHERE
  3810.             c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  3811.             AND
  3812.             (
  3813.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 AND c.multi_bl != 1 AND c.multi_bl != 1)
  3814.             OR
  3815.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3816.             AND f.statut_commande_id != 23
  3817.             )
  3818.             )
  3819.             AND
  3820.             t.id is NOT NULL
  3821.             " $where "
  3822.             GROUP BY t.id
  3823.             ORDER BY c.total DESC
  3824.             LIMIT 0," $limit "
  3825.         ";
  3826.         $sql "
  3827.             SELECT distinct t.id as id_transporteur,t.libelle,t.reference, SUM(COALESCE(c.total_frais_port,0)+COALESCE(c.frais_port_supplementaire,0)) as total
  3828.             FROM `commerciale__commande` as c
  3829.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3830.             LEFT JOIN transporteur__transporteur as t on t.id = c.transporteur_id
  3831.             " $join "
  3832.             WHERE
  3833.             c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  3834.             AND
  3835.             (
  3836.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 AND c.multi_bl != 1 AND c.multi_bl != 1)
  3837.             OR
  3838.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3839.             AND f.statut_commande_id != 23
  3840.             )
  3841.             )
  3842.             AND
  3843.             t.id is NOT NULL
  3844.             " $where "
  3845.             GROUP BY t.id
  3846.             ORDER BY c.total DESC
  3847.             LIMIT 0," $limit "
  3848.         ";
  3849.         /*
  3850.         $sql = "
  3851.             SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  3852.             FROM `commerciale__article_commande` as ac
  3853.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  3854.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  3855.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  3856.             ".$join."
  3857.             WHERE
  3858.             c.date >= '".$dateDebut->format('Y-m-d H:i:s')."' AND c.date <= '".$dateFin->format('Y-m-d H:i:s')."'
  3859.             AND
  3860.             (
  3861.             (c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23 )
  3862.             OR
  3863.             (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  3864.             AND f.statut_commande_id != 23
  3865.             )
  3866.             )
  3867.             ".$where."
  3868.             GROUP BY ar.id
  3869.             ORDER BY c.total DESC
  3870.             LIMIT 0,".$limit."
  3871.         ";
  3872.         */
  3873.         //echo $sql;
  3874.         $sql      trim($sql);
  3875.         $em       $this->getEntityManager();
  3876.         $stmt     $em->getConnection()->prepare($sql);
  3877.         $articles $stmt->executeQuery()->fetchAllAssociative();
  3878.         $valeurs                = [];
  3879.         $mois                   = [];
  3880.         $qteTotal               "";
  3881.         $qteTotalStock          "";
  3882.         $qteTotalExpedition     "";
  3883.         $qteTotalExpeditionMois = [];
  3884.         $qteTotalMois           = [];
  3885.         for ($i 0$i count($articles); $i++) {
  3886.             $quantite         = [];
  3887.             $mois             = [];
  3888.             $total            0;
  3889.             $total_expedition 0;
  3890.             $dateFinBoucle    DateTime::createFromFormat('m/Y'$parametres['date_fin']);
  3891.             $dateFinBoucle->setTime(235959);
  3892.             //echo "<div>TEST 001 ".$articles[$i]["total"]."</div>";
  3893.             $dateDebutBoucle DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  3894.             $dateDebutBoucle->setTime(000000);
  3895.             //$qteTotalStock = $qteTotalStock+$articles[$i]['stock'];
  3896.             while ($dateDebutBoucle $dateFinBoucle) {
  3897.                 $mois[]          = $dateDebutBoucle->format("m/Y");
  3898.                 $valeurs         = [];
  3899.                 $valeurs["mois"] = $dateDebutBoucle->format("m/Y");
  3900.                 $valeurs["quantite"]   = $this->totalPortMois($articles[$i]['id_transporteur'], $dateDebutBoucle);
  3901.                 $valeurs["expedition"] = $this->nbExpeditionsMois($articles[$i]['id_transporteur'], $dateDebutBoucle);
  3902.                 $quantite[] = $valeurs;
  3903.                 if (array_key_exists($dateDebutBoucle->format("m/Y"), $qteTotalMois)) {
  3904.                     $qteTotalMois[$dateDebutBoucle->format("m/Y")] += $valeurs["quantite"];
  3905.                 } else {
  3906.                     $qteTotalMois[$dateDebutBoucle->format("m/Y")] = $valeurs["quantite"];
  3907.                 }
  3908.                 if (array_key_exists($dateDebutBoucle->format("m/Y"), $qteTotalExpeditionMois)) {
  3909.                     $qteTotalExpeditionMois[$dateDebutBoucle->format("m/Y")] += $valeurs["expedition"];
  3910.                 } else {
  3911.                     $qteTotalExpeditionMois[$dateDebutBoucle->format("m/Y")] = $valeurs["expedition"];
  3912.                 }
  3913.                 $dateDebutBoucle->add(new DateInterval('P1M'));
  3914.                 $total $total $valeurs["quantite"];
  3915.                 $total_expedition $total_expedition $valeurs["expedition"];
  3916.             }
  3917.             $articles[$i]["valeurs"]    = $quantite;
  3918.             $articles[$i]["total"]      = $total;
  3919.             $articles[$i]["expedition"] = $total_expedition;
  3920.             $qteTotal                   $qteTotal $total;
  3921.             $qteTotalExpedition         $qteTotalExpedition $total_expedition;
  3922.         }
  3923.         //print_r($articles);
  3924.         $retour                  = [];
  3925.         $retour['transporteurs'] = $articles;
  3926.         $retour['mois']          = $mois;
  3927.         $retour['total']         = [
  3928.             "valeur"         => $qteTotal,
  3929.             "expeditionMois" => $qteTotalExpeditionMois,
  3930.             "totalMois"      => $qteTotalMois,
  3931.             "stock"          => $qteTotalStock,
  3932.             "expedition"     => $qteTotalExpedition,
  3933.         ];
  3934.         //print_r($qteTotalMois);
  3935.         return $retour;
  3936.     }
  3937.     public function suiviConsommation($parametres = [])
  3938.     {
  3939.         $limit     "10000000";
  3940.         $articles  = [];
  3941.         $dateDebut DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  3942.         $dateDebut->setTime(000000);
  3943.         $dateFin DateTime::createFromFormat('m/Y'$parametres['date_fin']);
  3944.         $dateFin->setTime(235959);
  3945.         $where            "";
  3946.         $where_mouvemente "";
  3947.         $join "";
  3948.         //print_r($parametres);
  3949.         if (array_key_exists('non_mouv'$parametres) and $parametres["non_mouv"] == 1) {
  3950.             $where_mouvemente "OR ((ar.stock > 0) ";
  3951.         }
  3952.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  3953.             $where              .= " AND ar.marque_id = '" $parametres["marque"] . "'";
  3954.             $join_table_article true;
  3955.             if (array_key_exists('non_mouv'$parametres) and $parametres["non_mouv"] == 1) {
  3956.                 $where_mouvemente .= " AND ar.marque_id = '" $parametres["marque"] . "'";
  3957.             }
  3958.         }
  3959.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  3960.             $where              .= " AND ar.id = '" $parametres["article"] . "'";
  3961.             $join_table_article true;
  3962.             if (array_key_exists('non_mouv'$parametres) and $parametres["non_mouv"] == 1) {
  3963.                 $where_mouvemente .= " AND ar.id = '" $parametres["article"] . "'";
  3964.             }
  3965.         }
  3966.         if (array_key_exists('categorie_article_3'$parametres) and $parametres["categorie_article_3"] > 0) {
  3967.             //echo "<div>AA</div>";
  3968.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  3969.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article_3"] . "'";
  3970.             $join_table_article true;
  3971.             if (array_key_exists('non_mouv'$parametres) and $parametres["non_mouv"] == 1) {
  3972.                 $where_mouvemente .= " AND artcat.categorie_id = '" $parametres["categorie_article_3"] . "'";
  3973.             }
  3974.         } elseif (array_key_exists('categorie_article_2'$parametres) and $parametres["categorie_article_2"] > 0) {
  3975.             //echo "<div>BB</div>";
  3976.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  3977.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article_2"] . "'";
  3978.             $join_table_article true;
  3979.             if (array_key_exists('non_mouv'$parametres) and $parametres["non_mouv"] == 1) {
  3980.                 $where_mouvemente .= " AND artcat.categorie_id = '" $parametres["categorie_article_2"] . "'";
  3981.             }
  3982.         } elseif (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  3983.             //echo "<div>CC</div>";
  3984.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  3985.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  3986.             $join_table_article true;
  3987.             if (array_key_exists('non_mouv'$parametres) and $parametres["non_mouv"] == 1) {
  3988.                 $where_mouvemente .= " AND artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  3989.             }
  3990.         }
  3991.         if (array_key_exists('non_mouv'$parametres) and $parametres["non_mouv"] == 1) {
  3992.             $where_mouvemente .= ")";
  3993.         }
  3994.         //echo "<div><b>".$where_mouvemente."</b></div><br/><br/>";
  3995.         if (array_key_exists('consommation'$parametres) and $parametres["consommation"] == "achat") {
  3996.             /*
  3997.             $sql = "
  3998.                 SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  3999.                 FROM `commerciale__article_commande` as ac
  4000.                 LEFT JOIN commerciale__commande_fournisseur as c on ac.commande_fournisseur_id = c.id
  4001.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  4002.                 ".$join."
  4003.                 WHERE
  4004.                 c.date >= '".$dateDebut->format('Y-m-d H:i:s')."' AND c.date <= '".$dateFin->format('Y-m-d H:i:s')."'
  4005.                 AND
  4006.                 c.statut_commande_id = 6
  4007.                 ".$where."
  4008.                 GROUP BY ar.id
  4009.                 ORDER BY total DESC
  4010.                 LIMIT 0,".$limit."
  4011.             ";
  4012.             */
  4013.             $sql "
  4014.             SELECT
  4015.                     ar.id as id_article,
  4016.                     ar.libelle,
  4017.                     ar.reference,
  4018.                     ar.stock
  4019.             FROM `article__mouvement_stock` as ac
  4020.             LEFT JOIN commerciale__commande_fournisseur as c on ac.commande_id = c.id
  4021.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4022.             " $join "
  4023.             WHERE
  4024.             ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4025.             AND
  4026.             ac.raison_mouvement_stock_id IN ('3','32')
  4027.             " $where "
  4028.             " $where_mouvemente "
  4029.             GROUP BY ar.id
  4030.             ORDER BY reference ASC
  4031.             LIMIT 0," $limit "
  4032.         ";
  4033.         } elseif (array_key_exists('consommation'$parametres) and $parametres["consommation"] == "fabrication_plus") {
  4034.             /*
  4035.             $sql = "
  4036.                 SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  4037.                 FROM `commerciale__article_commande` as ac
  4038.                 LEFT JOIN commerciale__fabrication as c on ac.fabrication_id = c.id
  4039.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  4040.                 ".$join."
  4041.                 WHERE
  4042.                 c.date >= '".$dateDebut->format('Y-m-d H:i:s')."' AND c.date <= '".$dateFin->format('Y-m-d H:i:s')."'
  4043.                 AND
  4044.                 c.statut_fabrication_id IN (4)
  4045.                 ".$where."
  4046.                 GROUP BY ar.id
  4047.                 LIMIT 0,".$limit."
  4048.             ";
  4049.             */
  4050.             $sql "
  4051.             SELECT
  4052.                     ar.id as id_article,
  4053.                     ar.libelle,
  4054.                     ar.reference,
  4055.                     ar.stock
  4056.             FROM `article__mouvement_stock` as ac
  4057.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4058.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4059.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4060.             " $join "
  4061.             WHERE
  4062.             ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4063.             AND
  4064.             (ac.raison_mouvement_stock_id = '9')
  4065.             " $where "
  4066.             " $where_mouvemente "
  4067.             GROUP BY ar.id
  4068.             ORDER BY reference ASC
  4069.             LIMIT 0," $limit "
  4070.         ";
  4071.         } elseif (array_key_exists('consommation'$parametres) and $parametres["consommation"] == "fabrication_moins") {
  4072.             /*
  4073.             $sql = "
  4074.                 SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  4075.                 FROM `commerciale__article_commande` as ac
  4076.                 LEFT JOIN commerciale__fabrication as c on ac.fabrication_id = c.id
  4077.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  4078.                 ".$join."
  4079.                 WHERE
  4080.                 c.date >= '".$dateDebut->format('Y-m-d H:i:s')."' AND c.date <= '".$dateFin->format('Y-m-d H:i:s')."'
  4081.                 AND
  4082.                 c.statut_fabrication_id IN (4)
  4083.                 ".$where."
  4084.                 GROUP BY ar.id
  4085.                 LIMIT 0,".$limit."
  4086.             ";
  4087.             */
  4088.             $sql "
  4089.             SELECT
  4090.                     ar.id as id_article,
  4091.                     ar.libelle,
  4092.                     ar.reference,
  4093.                     ar.stock
  4094.             FROM `article__mouvement_stock` as ac
  4095.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4096.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4097.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4098.             " $join "
  4099.             WHERE
  4100.             ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4101.             AND
  4102.             (ac.raison_mouvement_stock_id = '6')
  4103.             " $where "
  4104.             " $where_mouvemente "
  4105.             GROUP BY ar.id
  4106.             ORDER BY reference ASC
  4107.             LIMIT 0," $limit "
  4108.         ";
  4109.         } else {
  4110.             /*
  4111.             $sql = "
  4112.                 SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  4113.                 FROM `commerciale__article_commande` as ac
  4114.                 LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4115.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4116.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  4117.                 ".$join."
  4118.                 WHERE
  4119.                 c.date >= '".$dateDebut->format('Y-m-d H:i:s')."' AND c.date <= '".$dateFin->format('Y-m-d H:i:s')."'
  4120.                 AND
  4121.                 (
  4122.                 ((c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23) OR (c.type_document_commercial_id = ".TypeDocumentCommercial::AVOIR." AND c.statut_commande_id != 20 AND c.statut_commande_id != 19) )
  4123.                 OR
  4124.                 (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  4125.                 AND f.statut_commande_id != 23
  4126.                 )
  4127.                 )
  4128.                 ".$where."
  4129.                 GROUP BY ar.id
  4130.                 ORDER BY c.total DESC
  4131.                 LIMIT 0,".$limit."
  4132.             ";
  4133.             */
  4134.             $sql "
  4135.             SELECT
  4136.                     ar.id as id_article,
  4137.                     ar.libelle,
  4138.                     ar.reference,
  4139.                     ar.stock
  4140.             FROM `article__mouvement_stock` as ac
  4141.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4142.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4143.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4144.             " $join "
  4145.             WHERE
  4146.             ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4147.             AND
  4148.             (
  4149.             (
  4150.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25)
  4151.                 OR
  4152.                 (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23)
  4153.                 OR
  4154.                 (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 20)
  4155.             )
  4156.             )
  4157.             " $where "
  4158.             " $where_mouvemente "
  4159.             GROUP BY ar.id
  4160.             ORDER BY reference ASC
  4161.             LIMIT 0," $limit "
  4162.         ";
  4163.             //echo $sql;
  4164.         }
  4165.         //echo "<div>".$sql."</div>";
  4166.         $sql      trim($sql);
  4167.         $em       $this->getEntityManager();
  4168.         $stmt     $em->getConnection()->prepare($sql);
  4169.         $articles $stmt->executeQuery()->fetchAllAssociative();
  4170.         $valeurs       = [];
  4171.         $mois          = [];
  4172.         $qteTotal      "";
  4173.         $qteTotalStock "";
  4174.         $qteTotalMois  = [];
  4175.         for ($i 0$i count($articles); $i++) {
  4176.             $quantite      = [];
  4177.             $mois          = [];
  4178.             $total         0;
  4179.             $dateFinBoucle DateTime::createFromFormat('m/Y'$parametres['date_fin']);
  4180.             $dateFinBoucle->setTime(235959);
  4181.             //echo "TEST 001";
  4182.             $dateDebutBoucle DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  4183.             $dateDebutBoucle->setTime(235959);
  4184.             $qteTotalStock $qteTotalStock $articles[$i]['stock'];
  4185.             while ($dateDebutBoucle $dateFinBoucle) {
  4186.                 $mois[]          = $dateDebutBoucle->format("m/Y");
  4187.                 $valeurs         = [];
  4188.                 $valeurs["mois"] = $dateDebutBoucle->format("m/Y");
  4189.                 if (array_key_exists('consommation'$parametres) and $parametres["consommation"] == "achat") {
  4190.                     $valeurs["quantite"] = $this->totalAchatMois($articles[$i]['id_article'], $dateDebutBoucle);
  4191.                 } elseif (array_key_exists('consommation'$parametres) and $parametres["consommation"] == "fabrication_plus") {
  4192.                     $valeurs["quantite"] = $this->totalUtiliseFabricationMois($articles[$i]['id_article'], $dateDebutBoucle"plus");
  4193.                 } elseif (array_key_exists('consommation'$parametres) and $parametres["consommation"] == "fabrication_moins") {
  4194.                     $valeurs["quantite"] = $this->totalUtiliseFabricationMois($articles[$i]['id_article'], $dateDebutBoucle"moins");
  4195.                 } else {
  4196.                     $valeurs["quantite"] = $this->totalVenduMois($articles[$i]['id_article'], $dateDebutBoucle);
  4197.                 }
  4198.                 $quantite[] = $valeurs;
  4199.                 if (array_key_exists($dateDebutBoucle->format("m/Y"), $qteTotalMois)) {
  4200.                     $qteTotalMois[$dateDebutBoucle->format("m/Y")] += $valeurs["quantite"];
  4201.                 } else {
  4202.                     $qteTotalMois[$dateDebutBoucle->format("m/Y")] = $valeurs["quantite"];
  4203.                 }
  4204.                 $dateDebutBoucle->add(new DateInterval('P1M'));
  4205.                 $total $total $valeurs["quantite"];
  4206.             }
  4207.             $articles[$i]["valeurs"] = $quantite;
  4208.             $articles[$i]["total"]   = $total;
  4209.             $qteTotal                $qteTotal $total;
  4210.         }
  4211.         //print_r($articles);
  4212.         $retour             = [];
  4213.         $retour['articles'] = $articles;
  4214.         $retour['mois']     = $mois;
  4215.         $retour['total']    = ["valeur" => $qteTotal"totalMois" => $qteTotalMois"stock" => $qteTotalStock];
  4216.         //print_r($qteTotalMois);
  4217.         return $retour;
  4218.     }
  4219.     public function ____suiviConsommation($parametres = [])
  4220.     {
  4221.         $limit     "10000000";
  4222.         $articles  = [];
  4223.         $dateDebut DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  4224.         $dateDebut->setTime(000000);
  4225.         $dateFin DateTime::createFromFormat('m/Y'$parametres['date_fin']);
  4226.         $dateFin->setTime(235959);
  4227.         $where "";
  4228.         $join  "";
  4229.         //print_r($parametres);
  4230.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  4231.             $where              .= " AND ar.marque_id = '" $parametres["marque"] . "'";
  4232.             $join_table_article true;
  4233.         }
  4234.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  4235.             $where              .= " AND ar.id = '" $parametres["article"] . "'";
  4236.             $join_table_article true;
  4237.         }
  4238.         if (array_key_exists('categorie_article_3'$parametres) and $parametres["categorie_article_3"] > 0) {
  4239.             //echo "<div>AA</div>";
  4240.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  4241.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article_3"] . "'";
  4242.             $join_table_article true;
  4243.         } elseif (array_key_exists('categorie_article_2'$parametres) and $parametres["categorie_article_2"] > 0) {
  4244.             //echo "<div>BB</div>";
  4245.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  4246.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article_2"] . "'";
  4247.             $join_table_article true;
  4248.         } elseif (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  4249.             //echo "<div>CC</div>";
  4250.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  4251.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  4252.             $join_table_article true;
  4253.         }
  4254.         if (array_key_exists('consommation'$parametres) and $parametres["consommation"] == "achat") {
  4255.             $sql "
  4256.                 SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  4257.                 FROM `commerciale__article_commande` as ac
  4258.                 LEFT JOIN commerciale__commande_fournisseur as c on ac.commande_fournisseur_id = c.id
  4259.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  4260.                 " $join "
  4261.                 WHERE
  4262.                 c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4263.                 AND
  4264.                 c.statut_commande_id = 6
  4265.                 " $where "
  4266.                 GROUP BY ar.id
  4267.                 ORDER BY total DESC
  4268.                 LIMIT 0," $limit "
  4269.             ";
  4270.         } elseif (array_key_exists('consommation'$parametres) and $parametres["consommation"] == "fabrication") {
  4271.             $sql "
  4272.                 SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  4273.                 FROM `commerciale__article_commande` as ac
  4274.                 LEFT JOIN commerciale__fabrication as c on ac.fabrication_id = c.id
  4275.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  4276.                 " $join "
  4277.                 WHERE
  4278.                 c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4279.                 AND
  4280.                 c.statut_fabrication_id IN (4)
  4281.                 " $where "
  4282.                 GROUP BY ar.id
  4283.                 LIMIT 0," $limit "
  4284.             ";
  4285.         } else {
  4286.             /*
  4287.             SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  4288.             FROM `commerciale__article_commande` as ac
  4289.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4290.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4291.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4292.             WHERE c.date >= '2015-09-01 23:59:59' AND c.date <= '2016-09-13 23:59:59'
  4293.             AND
  4294.             (
  4295.             (c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23 )
  4296.             OR
  4297.             (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 24 AND c.facture_id > 0
  4298.             AND f.statut_commande_id != 23
  4299.             )
  4300.             )
  4301.             GROUP BY ar.id ORDER BY c.total DESC LIMIT 0,10000000
  4302.             */
  4303.             $sql "
  4304.                 SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  4305.                 FROM `commerciale__article_commande` as ac
  4306.                 LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4307.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4308.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  4309.                 " $join "
  4310.                 WHERE
  4311.                 c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4312.                 AND
  4313.                 (
  4314.                 ((c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23) OR (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 20 AND c.statut_commande_id != 19) )
  4315.                 OR
  4316.                 (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  4317.                 AND f.statut_commande_id != 23
  4318.                 )
  4319.                 )
  4320.                 " $where "
  4321.                 GROUP BY ar.id
  4322.                 ORDER BY c.total DESC
  4323.                 LIMIT 0," $limit "
  4324.             ";
  4325.             /*$sql = "
  4326.                 SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  4327.                 FROM `commerciale__article_commande` as ac
  4328.                 LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4329.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  4330.                 ".$join."
  4331.                 WHERE
  4332.                 c.date >= '".$dateDebut->format('Y-m-d H:i:s')."' AND c.date <= '".$dateFin->format('Y-m-d H:i:s')."'
  4333.                 AND
  4334.                 c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE."
  4335.                 AND
  4336.                 c.statut_commande_id != 23
  4337.                 ".$where."
  4338.                 GROUP BY ar.id
  4339.                 ORDER BY total DESC
  4340.                 LIMIT 0,".$limit."
  4341.             ";
  4342.             */
  4343.         }
  4344.         //echo $sql;
  4345.         $sql      trim($sql);
  4346.         $em       $this->getEntityManager();
  4347.         $stmt     $em->getConnection()->prepare($sql);
  4348.         $articles $stmt->executeQuery()->fetchAllAssociative();
  4349.         $valeurs       = [];
  4350.         $mois          = [];
  4351.         $qteTotal      "";
  4352.         $qteTotalStock "";
  4353.         $qteTotalMois  = [];
  4354.         for ($i 0$i count($articles); $i++) {
  4355.             $quantite      = [];
  4356.             $mois          = [];
  4357.             $total         0;
  4358.             $dateFinBoucle DateTime::createFromFormat('m/Y'$parametres['date_fin']);
  4359.             $dateFinBoucle->setTime(235959);
  4360.             //echo "TEST 001";
  4361.             $dateDebutBoucle DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  4362.             $dateDebutBoucle->setTime(235959);
  4363.             $qteTotalStock $qteTotalStock $articles[$i]['stock'];
  4364.             while ($dateDebutBoucle $dateFinBoucle) {
  4365.                 $mois[]          = $dateDebutBoucle->format("m/Y");
  4366.                 $valeurs         = [];
  4367.                 $valeurs["mois"] = $dateDebutBoucle->format("m/Y");
  4368.                 if (array_key_exists('consommation'$parametres) and $parametres["consommation"] == "achat") {
  4369.                     $valeurs["quantite"] = $this->totalAchatMois($articles[$i]['id_article'], $dateDebutBoucle);
  4370.                 } else {
  4371.                     $valeurs["quantite"] = $this->totalVenduMois($articles[$i]['id_article'], $dateDebutBoucle);
  4372.                 }
  4373.                 $quantite[] = $valeurs;
  4374.                 if (array_key_exists($dateDebutBoucle->format("m/Y"), $qteTotalMois)) {
  4375.                     $qteTotalMois[$dateDebutBoucle->format("m/Y")] += $valeurs["quantite"];
  4376.                 } else {
  4377.                     $qteTotalMois[$dateDebutBoucle->format("m/Y")] = $valeurs["quantite"];
  4378.                 }
  4379.                 $dateDebutBoucle->add(new DateInterval('P1M'));
  4380.                 $total $total $valeurs["quantite"];
  4381.             }
  4382.             $articles[$i]["valeurs"] = $quantite;
  4383.             $articles[$i]["total"]   = $total;
  4384.             $qteTotal                $qteTotal $total;
  4385.         }
  4386.         //print_r($articles);
  4387.         $retour             = [];
  4388.         $retour['articles'] = $articles;
  4389.         $retour['mois']     = $mois;
  4390.         $retour['total']    = ["valeur" => $qteTotal"totalMois" => $qteTotalMois"stock" => $qteTotalStock];
  4391.         //print_r($qteTotalMois);
  4392.         return $retour;
  4393.     }
  4394.     public function totalAchatMois($idArticle$date)
  4395.     {
  4396.         $sql "
  4397.             SELECT sum(COALESCE(ac.quantite,0)) as total
  4398.             FROM `commerciale__article_commande` as ac
  4399.             LEFT JOIN commerciale__commande_fournisseur as c on ac.commande_fournisseur_id = c.id
  4400.             WHERE
  4401.             c.date >= '" $date->format('Y-m-d ') . "00:00:00' AND c.date <= '" $date->format('Y-m-t H:i:s') . "'
  4402.             AND
  4403.             c.statut_commande_id = 6
  4404.             AND
  4405.             ac.article_id = " $idArticle "
  4406.         ";
  4407.         $sql "
  4408.             SELECT sum(COALESCE(ac.quantite,0)) as total
  4409.             FROM `article__mouvement_stock` as ac
  4410.             LEFT JOIN commerciale__commande_fournisseur as c on ac.commande_id = c.id
  4411.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4412.             WHERE
  4413.             ac.date_mouvement >= '" $date->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $date->format('Y-m-t ') . "23:59:59'
  4414.             AND
  4415.             ac.raison_mouvement_stock_id IN ('3','32')
  4416.             AND
  4417.             ac.article_id = " $idArticle "
  4418.         ";
  4419.         $sql trim($sql);
  4420.         //echo "<br/><br/>".$sql;
  4421.         $em    $this->getEntityManager();
  4422.         $stmt  $em->getConnection()->prepare($sql);
  4423.         $total $stmt->executeQuery()->fetchAllAssociative();
  4424.         return $total[0]['total'];
  4425.     }
  4426.     public function totalUtiliseFabricationMois($idArticle$date$type "plus")
  4427.     {
  4428.         $total_global     "0";
  4429.         $total_fabrique   "0";
  4430.         $total_defabrique "0";
  4431.         if ($type == "plus") {
  4432.             $sql "
  4433.                 SELECT     sum(COALESCE(ac.quantite,0)) as total
  4434.                 FROM `article__mouvement_stock` as ac
  4435.                 LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4436.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4437.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  4438.                 WHERE
  4439.                 ac.date >= '" $date->format('Y-m-d ') . "00:00:00' AND ac.date <= '" $date->format('Y-m-t H:i:s') . "'
  4440.                 AND
  4441.                 (ac.raison_mouvement_stock_id = '9')
  4442.                 AND
  4443.                 ac.article_id = " $idArticle "
  4444.             ";
  4445.             //echo "<br/><br/>".$sql;
  4446.             $sql   trim($sql);
  4447.             $em    $this->getEntityManager();
  4448.             $stmt  $em->getConnection()->prepare($sql);
  4449.             $total $stmt->executeQuery()->fetchAllAssociative();
  4450.             $total_fabrique $total[0]['total'];
  4451.         } elseif ($type == "moins") {
  4452.             $sql "
  4453.                 SELECT     sum(COALESCE(ac.quantite,0)) as total
  4454.                 FROM `article__mouvement_stock` as ac
  4455.                 LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4456.                 LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4457.                 LEFT JOIN article__article as ar on ar.id = ac.article_id
  4458.                 WHERE
  4459.                 ac.date >= '" $date->format('Y-m-d ') . "00:00:00' AND ac.date <= '" $date->format('Y-m-t H:i:s') . "'
  4460.                 AND
  4461.                 (ac.raison_mouvement_stock_id = '6')
  4462.                 AND
  4463.                 ac.article_id = " $idArticle "
  4464.             ";
  4465.             //echo "<br/><br/>".$sql;
  4466.             $sql   trim($sql);
  4467.             $em    $this->getEntityManager();
  4468.             $stmt  $em->getConnection()->prepare($sql);
  4469.             $total $stmt->executeQuery()->fetchAllAssociative();
  4470.             $total_defabrique $total[0]['total'];
  4471.         }
  4472.         //echo "<div>FAB ".$total_fabrique."</div>";
  4473.         //echo "<div>DEFAB ".$total_defabrique."</div>";
  4474.         $total_global $total_fabrique $total_defabrique;
  4475.         return $total_global;
  4476.     }
  4477.     public function totalVenduMois($idArticle$date)
  4478.     {
  4479.         $sql "
  4480.             SELECT sum(COALESCE(ac.quantite,0)) as total
  4481.             FROM `commerciale__article_commande` as ac
  4482.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4483.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4484.             WHERE
  4485.             c.date >= '" $date->format('Y-m-d ') . "00:00:00' AND c.date <= '" $date->format('Y-m-t H:i:s') . "'
  4486.             AND
  4487.             (
  4488.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 )
  4489.             OR
  4490.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  4491.             AND f.statut_commande_id != 23
  4492.             )
  4493.             )
  4494.             AND
  4495.             ac.article_id = " $idArticle "
  4496.         ";
  4497.         //539354
  4498.         $sql "
  4499.             SELECT sum(COALESCE(ac.quantite,0)) as total
  4500.             FROM `article__mouvement_stock` as ac
  4501.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4502.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4503.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4504.             LEFT JOIN commerciale__commande as cv on ac.commande_virtuel_id = cv.id
  4505.             WHERE
  4506.             ac.date_mouvement >= '" $date->format('Y-m-d ') . "00:00:00' AND ac.date_mouvement <= '" $date->format('Y-m-t ') . "23:59:59'
  4507.             AND
  4508.             (
  4509.             (
  4510.             ((c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25) or (cv.type_document_commercial_id = " TypeDocumentCommercial::BL " AND cv.statut_commande_id != 25))
  4511.             OR
  4512.             ((c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23) OR (cv.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND cv.statut_commande_id != 23))
  4513.             OR
  4514.             ((c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 20) or (cv.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND cv.statut_commande_id != 20))
  4515.             )
  4516.             )
  4517.             AND
  4518.             ac.article_id = " $idArticle "
  4519.         ";
  4520.         //echo "<br/><br/>".$sql;
  4521.         $sql   trim($sql);
  4522.         $em    $this->getEntityManager();
  4523.         $stmt  $em->getConnection()->prepare($sql);
  4524.         $total $stmt->executeQuery()->fetchAllAssociative();
  4525.         return $total[0]['total'];
  4526.     }
  4527.     public function ___totalVenduMois($idArticle$date)
  4528.     {
  4529.         /*
  4530.         SELECT distinct ar.id as id_article,ar.libelle,ar.reference,ar.stock
  4531. FROM `commerciale__article_commande` as ac
  4532. LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4533. LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4534. LEFT JOIN article__article as ar on ar.id = ac.article_id
  4535. WHERE c.date >= '2015-09-01 23:59:59' AND c.date <= '2016-09-13 23:59:59'
  4536. AND
  4537. (
  4538. (c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23 )
  4539. OR
  4540. (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 24 AND c.facture_id > 0
  4541. AND f.statut_commande_id != 23
  4542. )
  4543. )
  4544. GROUP BY ar.id ORDER BY c.total DESC LIMIT 0,10000000
  4545. */
  4546.         $sql "
  4547.             SELECT sum(COALESCE(ac.quantite,0)) as total
  4548.             FROM `commerciale__article_commande` as ac
  4549.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4550.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4551.             WHERE
  4552.             c.date >= '" $date->format('Y-m-d ') . "00:00:00' AND c.date <= '" $date->format('Y-m-t H:i:s') . "'
  4553.             AND
  4554.             (
  4555.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 )
  4556.             OR
  4557.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  4558.             AND f.statut_commande_id != 23
  4559.             )
  4560.             )
  4561.             AND
  4562.             ac.article_id = " $idArticle "
  4563.         ";
  4564.         /*$sql = "
  4565.             SELECT sum(ac.quantite) as total
  4566.             FROM `commerciale__article_commande` as ac
  4567.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4568.             WHERE
  4569.             c.date >= '".$date->format('Y-m-d ')."00:00:00' AND c.date <= '".$date->format('Y-m-t H:i:s')."'
  4570.             AND
  4571.             c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE."
  4572.             AND
  4573.             c.statut_commande_id != 23
  4574.             AND
  4575.             ac.article_id = ".$idArticle."
  4576.         ";
  4577.         */
  4578.         //echo $sql;
  4579.         $sql   trim($sql);
  4580.         $em    $this->getEntityManager();
  4581.         $stmt  $em->getConnection()->prepare($sql);
  4582.         $total $stmt->executeQuery()->fetchAllAssociative();
  4583.         return $total[0]['total'];
  4584.     }
  4585.     public function ___statArticles($parametres = [])
  4586.     {
  4587.         $retour = [];
  4588.         //if(intval($parametres['nbel'])>500) $parametres['nbel'] = 500;
  4589.         $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  4590.         $dateDebut->setTime(000000);
  4591.         $dateFin DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  4592.         $dateFin->setTime(235959);
  4593.         $where              "";
  4594.         $join               "";
  4595.         $join_table_client  false;
  4596.         $join_table_article false;
  4597.         $join_table_article true;
  4598.         if (array_key_exists('ref_article'$parametres) and $parametres["ref_article"] != "") {
  4599.             if (!$join_table_article) {
  4600.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  4601.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  4602.             }
  4603.             $parametres["ref_article"] = str_replace("*""%"$parametres["ref_article"]);
  4604.             $where                     .= " AND ar.reference like '" $parametres["ref_article"] . "%'";
  4605.             $join_table_article        true;
  4606.         }
  4607.         if (array_key_exists('client'$parametres) and $parametres["client"] != "" and $parametres["client"] > 0) {
  4608.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  4609.             $where             .= " AND u.id = '" $parametres["client"] . "'";
  4610.             $join_table_client true;
  4611.         }
  4612.         if (array_key_exists('ref_client'$parametres) and $parametres["ref_client"] > 0) {
  4613.             $join              .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  4614.             $where             .= " AND clt.reference = '" $parametres["ref_client"] . "'";
  4615.             $join_table_client true;
  4616.         }
  4617.         if (array_key_exists('nom_client'$parametres) and $parametres["nom_client"] != "") {
  4618.             if (!$join_table_client) {
  4619.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  4620.             }
  4621.             $where             .= " AND clt.nom like '%" $parametres["nom_client"] . "%'";
  4622.             $join_table_client true;
  4623.         }
  4624.         if (array_key_exists('equipier'$parametres) and $parametres["equipier"] > 0) {
  4625.             $where .= " AND c.utilisateur_id = '" $parametres["equipier"] . "'";
  4626.         }
  4627.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  4628.             //echo "MARQUE";
  4629.             if (!$join_table_article) {
  4630.                 echo "TYTY";
  4631.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  4632.                 //$join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  4633.             }
  4634.             $where              .= " AND ar.marque_id = '" $parametres["marque"] . "'";
  4635.             $join_table_article true;
  4636.         }
  4637.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  4638.             if (!$join_table_article) {
  4639.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  4640.                 //$join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  4641.             }
  4642.             $where              .= " AND ar.id = '" $parametres["article"] . "'";
  4643.             $join_table_article true;
  4644.         }
  4645.         if (array_key_exists('categorie_client'$parametres) and $parametres["categorie_client"] > 0) {
  4646.             if (!$join_table_client) {
  4647.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  4648.             }
  4649.             $join .= " LEFT JOIN client__client_categorie as cltcat on cltcat.client_id = clt.id";
  4650.             if (array_key_exists('sous_categorie_client'$parametres) and $parametres["sous_categorie_client"] > 0) {
  4651.                 $where .= " AND cltcat.categorie_id = '" $parametres["sous_categorie_client"] . "'";
  4652.             } else {
  4653.                 $where .= " AND cltcat.categorie_id = '" $parametres["categorie_client"] . "'";
  4654.             }
  4655.         }
  4656.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  4657.             if (!$join_table_article) {
  4658.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  4659.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  4660.             }
  4661.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  4662.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  4663.             $join_table_article true;
  4664.         }
  4665.         /*
  4666.         $sql = "
  4667.             SELECT     SUM(ac.total_ht) as total,
  4668.                      SUM(ac.tva_vente) as total_tva,
  4669.                     SUM(ac.quantite) as total_quantite,
  4670.                     SUM(ac.total_ht) as total_ht,
  4671.                     (SUM(ac.total_ht)+SUM(ac.tva_vente)) as total_ca,
  4672.                     ar.reference,
  4673.                     ar.libelle,
  4674.                     ar.id as article_id,
  4675.                     ar.taux_marge,
  4676.                     ar.pump,
  4677.                     ar.marge,
  4678.                     ar.prix_base as prix_achat
  4679.             FROM `commerciale__article_commande` as ac
  4680.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4681.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4682.             LEFT JOIN client__client as u on u.id = c.client_id
  4683.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4684.             ".$join."
  4685.             WHERE
  4686.             c.date >= '".$dateDebut->format('Y-m-d H:i:s')."' AND c.date <= '".$dateFin->format('Y-m-d H:i:s')."'
  4687.             AND
  4688.             (
  4689.             (c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23 )
  4690.             OR
  4691.             (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  4692.             AND f.statut_commande_id != 23
  4693.             )
  4694.             )
  4695.             ".$where."
  4696.             GROUP BY ar.id
  4697.             ORDER BY reference ASC
  4698.             LIMIT 0,".intval($parametres['nbel'])."
  4699.         ";
  4700.         */
  4701.         $sql "
  4702.             SELECT     SUM(COALESCE(ac.prix_achat,0)*COALESCE(ac.quantite,0)) as total,
  4703.                      AVG(COALESCE(ac.cpump,0)) as pump_avg,
  4704.                     SUM(COALESCE(ac.quantite,0)) as total_quantite,"
  4705.                //."SUM(ac.total_ht) as total_ht,"
  4706.                //."(SUM(ac.total_ht)+SUM(ac.tva_vente)) as total_ca,"
  4707.                "ar.reference,
  4708.                     ar.libelle,
  4709.                     ar.id as article_id,
  4710.                     ar.taux_marge,
  4711.                     ar.pump,
  4712.                     ar.cpump,
  4713.                     ar.produit_interne as est_compose,
  4714.                     ar.virtuel as est_virtuel,
  4715.                     ar.marge,
  4716.                     ar.prix_base as prix_achat
  4717.             FROM `article__mouvement_stock` as ac
  4718.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4719.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4720.             LEFT JOIN client__client as u on u.id = c.client_id
  4721.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4722.             " $join "
  4723.             WHERE
  4724.             ac.article_commande_virtuel_id is null
  4725.             AND
  4726.             ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4727.             AND
  4728.             (
  4729.             (  (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25 AND (c.facture_id is NOT NULL and f.statut_commande_id != 23))
  4730.                 OR
  4731.                 ((c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23) )
  4732.                 OR
  4733.                 (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 20))
  4734.             )
  4735.             " $where "
  4736.             GROUP BY ar.id
  4737.             ORDER BY reference ASC
  4738.         ";
  4739. //LIMIT 0,".intval($parametres['nbel'])."
  4740.         $sql trim($sql);
  4741.         //echo "<br/><br/>".$sql;
  4742.         //exit;
  4743.         $em       $this->getEntityManager();
  4744.         $stmt     $em->getConnection()->prepare($sql);
  4745.         $articles $stmt->executeQuery()->fetchAllAssociative();
  4746.         //print_r($articles);
  4747.         $tableautemp = [];
  4748.         foreach ($articles as $art) {
  4749.             $tableautemp[$art["article_id"]]           = $art;
  4750.             $tableautemp[$art["article_id"]]['bl_obj'] = [""];
  4751.             $tableautemp[$art["article_id"]]['n1']     = [];
  4752.         }
  4753.         if (array_key_exists('bl'$parametres) and $parametres["bl"] > 0) {
  4754.             $sql "
  4755.             SELECT     SUM(COALESCE(ac.prix_achat,0)*COALESCE(ac.quantite,0)) as total,
  4756.                      AVG(COALESCE(ac.cpump,0)) as pump_avg,
  4757.                     SUM(COALESCE(ac.quantite,0)) as total_quantite,
  4758.                     ar.reference,
  4759.                     ar.libelle,
  4760.                     ar.id as article_id,
  4761.                     ar.taux_marge,
  4762.                     ar.pump,
  4763.                     ar.cpump,
  4764.                     ar.produit_interne as est_compose,
  4765.                     ar.virtuel as est_virtuel,
  4766.                     ar.marge,
  4767.                     ar.prix_base as prix_achat
  4768.             FROM `article__mouvement_stock` as ac
  4769.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4770.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4771.             LEFT JOIN client__client as u on u.id = c.client_id
  4772.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4773.             " $join "
  4774.             WHERE
  4775.             ac.article_commande_virtuel_id is null
  4776.             AND
  4777.             ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4778.             AND
  4779.             (
  4780.             ( (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25))
  4781.             )
  4782.             AND (c.facture_id is NULL or f.statut_commande_id = 23)
  4783.             " $where "
  4784.             GROUP BY ar.id
  4785.             ORDER BY reference ASC
  4786.             LIMIT 0," intval($parametres['nbel']) . "
  4787.             ";
  4788.             $em          $this->getEntityManager();
  4789.             $stmt        $em->getConnection()->prepare($sql);
  4790.             $articles_bl $stmt->executeQuery()->fetchAllAssociative();
  4791.             //echo "<div>".$sql."</div>";
  4792.             //echo "<div>BL BL BL BL BL BL </div>";
  4793.             if (count($articles_bl) > 0) {
  4794.                 foreach ($articles_bl as $abl) {
  4795.                     //echo "<div>TOTO </div>";
  4796.                     //print_r($abl);
  4797.                     if (array_key_exists($abl["article_id"], $tableautemp)) {
  4798.                         $tableautemp[$abl["article_id"]]["bl_obj"] = $abl;
  4799.                     } else {
  4800.                         $tableautemp[$abl["article_id"]]["reference"]  = $abl["reference"];
  4801.                         $tableautemp[$abl["article_id"]]["libelle"]    = $abl["libelle"];
  4802.                         $tableautemp[$abl["article_id"]]["article_id"] = $abl["article_id"];
  4803.                         $tableautemp[$abl["article_id"]]["bl_obj"]     = $abl;
  4804.                         $tableautemp[$abl["article_id"]]['n1']         = [];
  4805.                     }
  4806.                 }
  4807.             }
  4808.             //print_r($articles_bl);
  4809.         }
  4810.         //print_r($tableautemp);
  4811.         //echo "<div>-----------------</div>";
  4812.         if (array_key_exists('n1'$parametres) and $parametres["n1"] > 0) {
  4813.             $parametres_n1       $parametres;
  4814.             $parametres_n1['n1'] = 0;
  4815.             //echo "<div>N1</div>";
  4816.             $dateDebutN1 DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  4817.             $dateDebutN1->setTime(000000);
  4818.             $dateDebutN1->sub(new DateInterval('P1Y'));
  4819.             $dateFinN1 DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  4820.             $dateFinN1->setTime(235959);
  4821.             $dateFinN1->sub(new DateInterval('P1Y'));
  4822.             $parametres_n1['date_debut'] = $dateDebutN1->format('d/m/Y');
  4823.             $parametres_n1['date_fin']   = $dateFinN1->format('d/m/Y');
  4824.             //echo "<div>datedebut ".$parametres_n1['date_debut']."</div>";
  4825.             //echo "<div>datefin ".$parametres_n1['date_fin']."</div>";
  4826.             $tableau_temp_n1 $this->statArticles($parametres_n1);
  4827.             //echo "<div>---N1-------------</div>";
  4828.             //print_r($tableau_temp_n1);
  4829.             //echo "<div>---N1-------------</div>";
  4830.             if (array_key_exists('articles'$tableau_temp_n1) && is_array($tableau_temp_n1["articles"])) {
  4831.                 if (count($tableau_temp_n1["articles"]) > 0) {
  4832.                     foreach ($tableau_temp_n1["articles"] as $artN1) {
  4833.                         //echo "<div>XXXXXXX ".$artN1["article_id"]."</div>";
  4834.                         //print_r($artN1);
  4835.                         //echo "<div>YYYYYYYY ".$artN1["article_id"]."</div>";
  4836.                         $tableautemp[$artN1["article_id"]]["n1"]        = $artN1;
  4837.                         $tableautemp[$artN1["article_id"]]["reference"] = $artN1["reference"];
  4838.                         $tableautemp[$artN1["article_id"]]["libelle"]   = $artN1["libelle"];
  4839.                     }
  4840.                 }
  4841.             }
  4842.             //echo "<div>--------------------------------</div>";
  4843.         }
  4844.         //$retour['articles'] = $articles;
  4845.         //print_r($tableautemp);
  4846.         $retour['articles'] = $tableautemp;
  4847.         return $retour;
  4848.     }
  4849.     public function statArticles($parametres = [])
  4850.     {
  4851.         $retour = [];
  4852.         //if(intval($parametres['nbel'])>500) $parametres['nbel'] = 500;
  4853.         $dateDebut DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  4854.         $dateDebut->setTime(000000);
  4855.         $dateFin DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  4856.         $dateFin->setTime(235959);
  4857.         if (!array_key_exists('n1'$parametres)) {
  4858.             $parametres["n1"] = "0";
  4859.         }
  4860.         $where              "";
  4861.         $join               "";
  4862.         $join_table_client  false;
  4863.         $join_table_article false;
  4864.         $join_table_article true;
  4865.         if (array_key_exists('ref_article'$parametres) and $parametres["ref_article"] != "") {
  4866.             if (!$join_table_article) {
  4867.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  4868.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  4869.             }
  4870.             $parametres["ref_article"] = str_replace("*""%"$parametres["ref_article"]);
  4871.             $where                     .= " AND ar.reference like '" $parametres["ref_article"] . "%'";
  4872.             $join_table_article        true;
  4873.         }
  4874.         if (array_key_exists('client'$parametres) and $parametres["client"] != "" and $parametres["client"] > 0) {
  4875.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  4876.             $where             .= " AND u.id = '" $parametres["client"] . "'";
  4877.             $join_table_client true;
  4878.         }
  4879.         if (array_key_exists('ref_client'$parametres) and $parametres["ref_client"] > 0) {
  4880.             $join              .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  4881.             $where             .= " AND clt.reference = '" $parametres["ref_client"] . "'";
  4882.             $join_table_client true;
  4883.         }
  4884.         if (array_key_exists('nom_client'$parametres) and $parametres["nom_client"] != "") {
  4885.             if (!$join_table_client) {
  4886.                 $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  4887.             }
  4888.             $where             .= " AND clt.nom like '%" $parametres["nom_client"] . "%'";
  4889.             $join_table_client true;
  4890.         }
  4891.         if (array_key_exists('equipier'$parametres) and $parametres["equipier"] > 0) {
  4892.             $where .= " AND c.utilisateur_id = '" $parametres["equipier"] . "'";
  4893.         }
  4894.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  4895.             //echo "MARQUE";
  4896.             if (!$join_table_article) {
  4897.                 echo "TYTY";
  4898.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  4899.                 //$join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  4900.             }
  4901.             $where              .= " AND ar.marque_id = '" $parametres["marque"] . "'";
  4902.             $join_table_article true;
  4903.         }
  4904.         if (array_key_exists('article'$parametres) and $parametres["article"] > 0) {
  4905.             if (!$join_table_article) {
  4906.                 //$join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  4907.                 //$join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  4908.             }
  4909.             $where              .= " AND ar.id = '" $parametres["article"] . "'";
  4910.             $join_table_article true;
  4911.         }
  4912.         if (array_key_exists('categorie_client'$parametres) and $parametres["categorie_client"] > 0) {
  4913.             //if(!$join_table_client) $join .= " LEFT JOIN client__client as clt on c.client_id = clt.id";
  4914.             $join .= " LEFT JOIN client__client_categorie as cltcat on cltcat.client_id = u.id";
  4915.             if (array_key_exists('sous_categorie_client'$parametres) and $parametres["sous_categorie_client"] > 0) {
  4916.                 $where .= " AND cltcat.categorie_id = '" $parametres["sous_categorie_client"] . "'";
  4917.             } else {
  4918.                 $where .= " AND cltcat.categorie_id = '" $parametres["categorie_client"] . "'";
  4919.             }
  4920.         }
  4921.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  4922.             if (!$join_table_article) {
  4923.                 $join .= " LEFT JOIN commerciale__article_commande as ac on ac.commande_id = c.id";
  4924.                 $join .= " LEFT JOIN article__article as ar on ar.id = ac.article_id";
  4925.             }
  4926.             $join               .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = ar.id";
  4927.             $where              .= " AND artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  4928.             $join_table_article true;
  4929.         }
  4930.         $sql "
  4931.             SELECT     ar.id as article_id
  4932.             FROM `article__mouvement_stock` as ac
  4933.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4934.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4935.             LEFT JOIN client__client as u on u.id = c.client_id
  4936.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4937.             " $join "
  4938.             WHERE
  4939.             ac.article_commande_virtuel_id is null
  4940.             AND
  4941.             ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4942.             AND
  4943.             (
  4944.             (  (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25 AND (c.facture_id is NOT NULL and f.statut_commande_id != 23))
  4945.                 OR
  4946.                 ((c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23) )
  4947.                 OR
  4948.                 (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 20))
  4949.             )
  4950.             " $where "
  4951.             GROUP BY ar.id
  4952.             ORDER BY ar.reference ASC
  4953.         ";
  4954.         //LIMIT 0,".intval($parametres['nbel'])."
  4955.         $sql trim($sql);
  4956.         //echo "<br/><br/>".$sql;
  4957.         //exit;
  4958.         $em       $this->getEntityManager();
  4959.         $stmt     $em->getConnection()->prepare($sql);
  4960.         $articles $stmt->executeQuery()->fetchAllAssociative();
  4961.         //print_r($articles);
  4962.         $tableautemp = [];
  4963.         foreach ($articles as $art) {
  4964.             $tableautemp[$art["article_id"]] = $art["article_id"];
  4965.         }
  4966.         if (array_key_exists('bl'$parametres) and $parametres["bl"] > 0) {
  4967.             $sql "
  4968.             SELECT     ar.id as article_id
  4969.             FROM `article__mouvement_stock` as ac
  4970.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  4971.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  4972.             LEFT JOIN client__client as u on u.id = c.client_id
  4973.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  4974.             " $join "
  4975.             WHERE
  4976.             ac.article_commande_virtuel_id is null
  4977.             AND
  4978.             ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  4979.             AND
  4980.             (
  4981.             ( (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25))
  4982.             )
  4983.             AND (c.facture_id is NULL or f.statut_commande_id = 23)
  4984.             " $where "
  4985.             GROUP BY ar.id
  4986.             ORDER BY ar.reference ASC
  4987.             LIMIT 0," intval($parametres['nbel']) . "
  4988.             ";
  4989.             $em          $this->getEntityManager();
  4990.             $stmt        $em->getConnection()->prepare($sql);
  4991.             $articles_bl $stmt->executeQuery()->fetchAllAssociative();
  4992.             //echo "<div>".$sql."</div>";
  4993.             //echo "<div>BL BL BL BL BL BL </div>";
  4994.             if (count($articles_bl) > 0) {
  4995.                 foreach ($articles_bl as $abl) {
  4996.                     //echo "<div>TOTO </div>";
  4997.                     //print_r($abl);
  4998.                     if (!array_key_exists($abl["article_id"], $tableautemp)) {
  4999.                         $tableautemp[$abl["article_id"]] = $abl["article_id"];
  5000.                     }
  5001.                 }
  5002.             }
  5003.             //print_r($articles_bl);
  5004.         }
  5005.         //print_r($tableautemp);
  5006.         //echo "<div>-----------------</div>";
  5007.         if (array_key_exists('n1'$parametres) and $parametres["n1"] > 0) {
  5008.             $parametres_n1       $parametres;
  5009.             $parametres_n1['n1'] = 0;
  5010.             //echo "<div>N1</div>";
  5011.             $dateDebutN1 DateTime::createFromFormat('d/m/Y'$parametres['date_debut']);
  5012.             $dateDebutN1->setTime(000000);
  5013.             $dateDebutN1->sub(new DateInterval('P1Y'));
  5014.             $dateFinN1 DateTime::createFromFormat('d/m/Y'$parametres['date_fin']);
  5015.             $dateFinN1->setTime(235959);
  5016.             $dateFinN1->sub(new DateInterval('P1Y'));
  5017.             $parametres_n1['date_debut'] = $dateDebutN1->format('d/m/Y');
  5018.             $parametres_n1['date_fin']   = $dateFinN1->format('d/m/Y');
  5019.             //echo "<div>datedebut ".$parametres_n1['date_debut']."</div>";
  5020.             //echo "<div>datefin ".$parametres_n1['date_fin']."</div>";
  5021.             $tableau_temp_n1 $this->statArticles($parametres_n1);
  5022.             //echo "<div>COUNT ".count($tableau_temp_n1)."</div>";
  5023.             //echo "<div>---N1-------------</div>";
  5024.             //print_r($tableau_temp_n1);
  5025.             //echo "<div>---N1-------------</div>";
  5026.             //if(array_key_exists('articles', $tableau_temp_n1) && is_array($tableau_temp_n1["articles"])) {
  5027.             if (count($tableau_temp_n1) > 0) {
  5028.                 foreach ($tableau_temp_n1 as $artN1) {
  5029.                     if (!array_key_exists($artN1$tableautemp)) {
  5030.                         $tableautemp[$artN1] = $artN1;
  5031.                     }
  5032.                 }
  5033.             }
  5034.             //}
  5035.             //echo "<div>--------------------------------</div>";
  5036.         }
  5037.         //print_r($tableautemp);
  5038.         return $tableautemp;
  5039.     }
  5040.     public function statArticlesAnneePrec($idArticle ""$debut ""$fin "")
  5041.     {
  5042.         $join      "";
  5043.         $where     "";
  5044.         $dateDebut DateTime::createFromFormat('d/m/Y'$debut);
  5045.         $dateDebut->setTime(000000);
  5046.         $dateDebut->sub(new DateInterval('P1Y'));
  5047.         $dateFin DateTime::createFromFormat('d/m/Y'$fin);
  5048.         $dateFin->setTime(235959);
  5049.         $dateFin->sub(new DateInterval('P1Y'));
  5050.         //echo "<div>DU ".$dateDebut->format("d/m/Y H:i:s")." au ".$dateFin->format("d/m/Y H:i:s")."</div>";
  5051.         $sql "
  5052.             SELECT     SUM(COALESCE(ac.total_ht,0)) as total,
  5053.                      SUM(COALESCE(ac.tva_vente,0) as total_tva,
  5054.                     SUM(COALESCE(ac.quantite,0)) as total_quantite,
  5055.                     SUM(COALESCE(ac.total_ht,0)) as total_ht,
  5056.                     (SUM(COALESCE(ac.total_ht,0))+SUM(COALESCE(ac.tva_vente,0))) as total_ca,
  5057.                     ar.reference,
  5058.                     ar.libelle,
  5059.                     ar.id as article_id,
  5060.                     ar.taux_marge,
  5061.                     ar.marge,
  5062.                     ar.prix_base as prix_achat
  5063.             FROM `commerciale__article_commande` as ac
  5064.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5065.             LEFT JOIN client__client as u on u.id = c.client_id
  5066.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  5067.             " $join "
  5068.             WHERE
  5069.             c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  5070.             AND
  5071.             c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE "
  5072.             AND
  5073.             c.statut_commande_id != 23
  5074.             " $where "
  5075.             AND
  5076.             ar.id = " $idArticle "
  5077.         ";
  5078.         $em     $this->getEntityManager();
  5079.         $stmt   $em->getConnection()->prepare($sql);
  5080.         $retour $stmt->executeQuery()->fetchAllAssociative();
  5081.         return $retour[0];
  5082.     }
  5083.     public function getTotalBlNonFacturer($idArticle ""$debut ""$fin ""$anneePrec false)
  5084.     {
  5085.         $dateDebut DateTime::createFromFormat('d/m/Y'$debut);
  5086.         $dateDebut->setTime(000000);
  5087.         $dateFin DateTime::createFromFormat('d/m/Y'$fin);
  5088.         $dateFin->setTime(235959);
  5089.         if ($anneePrec) {
  5090.             $dateDebut->sub(new DateInterval('P1Y'));
  5091.             $dateFin->sub(new DateInterval('P1Y'));
  5092.         }
  5093.         //echo "<div>DU ".$dateDebut->format("d/m/Y H:i:s")." au ".$dateFin->format("d/m/Y H:i:s")."</div>";
  5094.         $sql "
  5095.             SELECT     SUM(COALESCE(ac.total_ht,0)) as total,
  5096.                      SUM(COALESCE(ac.tva_vente,0)) as total_tva,
  5097.                     SUM(COALESCE(ac.quantite,0)) as total_quantite,
  5098.                     SUM(COALESCE(ac.total_ht,0)) as total_ht,
  5099.                     (SUM(COALESCE(ac.total_ht,0))+SUM(COALESCE(ac.tva_vente,0))) as total_ca,
  5100.                     ar.reference,
  5101.                     ar.libelle,
  5102.                     ar.id as article_id,
  5103.                     ar.taux_marge,
  5104.                     ar.marge,
  5105.                     ar.prix_base as prix_achat,
  5106.                     c.id
  5107.             FROM `commerciale__article_commande` as ac
  5108.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5109.             LEFT JOIN client__client as u on u.id = c.client_id
  5110.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  5111.             WHERE
  5112.             c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  5113.             AND
  5114.             c.type_document_commercial_id = " TypeDocumentCommercial::BL "
  5115.             AND
  5116.             c.statut_commande_id != 25
  5117.             AND
  5118.             ar.id = " $idArticle "
  5119.             AND
  5120.             (c.facture_id IS NULL OR
  5121.                 c.facture_id IN (
  5122.                     SELECT facture_id
  5123.                     FROM commerciale__commande
  5124.                     WHERE
  5125.                     type_document_commercial_id = " TypeDocumentCommercial::FACTURE "
  5126.                     AND
  5127.                     statut_commande_id = 23
  5128.                 )
  5129.             )
  5130.             ";
  5131.         $sql "
  5132.             SELECT     SUM(COALESCE(ac.prix_achat,0)) as total,
  5133.                      AVG(COALESCE(ac.cpump,0)) as pump_avg,
  5134.                     SUM(COALESCE(ac.quantite,0)) as total_quantite,
  5135.                     ar.reference,
  5136.                     ar.libelle,
  5137.                     ar.id as article_id,
  5138.                     ar.taux_marge,
  5139.                     ar.pump,
  5140.                     ar.cpump,
  5141.                     ar.marge,
  5142.                     ar.prix_base as prix_achat
  5143.             FROM `article__mouvement_stock` as ac
  5144.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5145.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  5146.             LEFT JOIN client__client as u on u.id = c.client_id
  5147.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  5148.             WHERE
  5149.             ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  5150.             AND
  5151.             (
  5152.             ( (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25))
  5153.             )
  5154.             AND (c.facture_id is NULL or f.statut_commande_id = 23)
  5155.             AND ar.id = " $idArticle "
  5156.         ";
  5157.         //echo "<div>SQL ".$sql."</div>";
  5158.         $em     $this->getEntityManager();
  5159.         $stmt   $em->getConnection()->prepare($sql);
  5160.         $retour $stmt->executeQuery()->fetchAllAssociative();
  5161.         //print_r($retour);
  5162.         return $retour;
  5163.     }
  5164.     public function getTotalBlNonFacturerCa($idArticle ""$debut ""$fin ""$anneePrec false)
  5165.     {
  5166.         /*
  5167.         $dateDebut = \DateTime::createFromFormat('d/m/Y',$debut);
  5168.         $dateDebut->setTime(00,00,00);
  5169.         $dateFin = \DateTime::createFromFormat('d/m/Y',$fin);
  5170.         $dateFin->setTime(23,59,59);
  5171.         */
  5172.         $dateDebut DateTime::createFromFormat('d/m/Y''01/' $debut);
  5173.         $dateDebut->setTime(235959);
  5174.         $dateFin DateTime::createFromFormat('m/Y'$fin);
  5175.         $dateFin->setTime(235959);
  5176.         if ($anneePrec) {
  5177.             $dateDebut->sub(new DateInterval('P1Y'));
  5178.             $dateFin->sub(new DateInterval('P1Y'));
  5179.         }
  5180.         //echo "<div>DU ".$dateDebut->format("d/m/Y H:i:s")." au ".$dateFin->format("d/m/Y H:i:s")."</div>";
  5181.         $sql "
  5182.             SELECT     SUM(COALESCE(ac.total_ht,0)) as total,
  5183.                      SUM(COALESCE(ac.tva_vente,0)) as total_tva,
  5184.                     SUM(COALESCE(ac.quantite,0)) as total_quantite,
  5185.                     SUM(COALESCE(ac.total_ht,0)) as total_ht,
  5186.                     (SUM(COALESCE(ac.total_ht,0))+SUM(COALESCE(ac.tva_vente,0))) as total_ca,
  5187.                     ar.reference,
  5188.                     ar.libelle,
  5189.                     ar.id as article_id,
  5190.                     ar.taux_marge,
  5191.                     ar.marge,
  5192.                     ar.prix_base as prix_achat,
  5193.                     c.id
  5194.             FROM `commerciale__article_commande` as ac
  5195.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5196.             LEFT JOIN client__client as u on u.id = c.client_id
  5197.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  5198.             WHERE
  5199.             c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'
  5200.             AND
  5201.             c.type_document_commercial_id = " TypeDocumentCommercial::BL "
  5202.             AND
  5203.             c.statut_commande_id != 25
  5204.             AND
  5205.             ar.id = " $idArticle "
  5206.             AND
  5207.             (c.facture_id IS NULL OR
  5208.                 c.facture_id IN (
  5209.                     SELECT facture_id
  5210.                     FROM commerciale__commande
  5211.                     WHERE
  5212.                     type_document_commercial_id = " TypeDocumentCommercial::FACTURE "
  5213.                     AND
  5214.                     statut_commande_id = 23
  5215.                 )
  5216.             )
  5217.             ";
  5218.         //echo "<div>SQL ".$sql."</div>";
  5219.         $em     $this->getEntityManager();
  5220.         $stmt   $em->getConnection()->prepare($sql);
  5221.         $retour $stmt->executeQuery()->fetchAllAssociative();
  5222.         //print_r($retour);
  5223.         return $retour;
  5224.     }
  5225.     public function getArticlesEquivalences($parametres = [])
  5226.     {
  5227.         $retour = [];
  5228.         if (!array_key_exists('nbel'$parametres)) {
  5229.             $parametres['nbel'] = 200;
  5230.         }
  5231.         if (intval($parametres['nbel']) > 500) {
  5232.             $parametres['nbel'] = 500;
  5233.         }
  5234.         $where "";
  5235.         $join  "";
  5236.         if (array_key_exists('ref_article'$parametres) and $parametres["ref_article"] != "") {
  5237.             $where .= " AND a.reference like '" $parametres["ref_article"] . "%'";
  5238.         }
  5239.         if (array_key_exists('marque'$parametres) and $parametres["marque"] > 0) {
  5240.             $where .= " AND a.marque_id = '" $parametres["marque"] . "'";
  5241.         }
  5242.         if (array_key_exists('categorie_article'$parametres) and $parametres["categorie_article"] > 0) {
  5243.             $join  .= " LEFT JOIN article__article_categorie as artcat on artcat.article_id = a.id";
  5244.             $where .= " AND artcat.categorie_id = '" $parametres["categorie_article"] . "'";
  5245.         }
  5246.         if (array_key_exists('exclure'$parametres) and $parametres["exclure"] != "") {
  5247.             $where .= " AND a.id NOT IN (" $parametres["exclure"] . ")";
  5248.         }
  5249.         $sql "
  5250.             SELECT     a.libelle,a.id,a.reference,m.libelle as marque
  5251.             FROM `article__article` as a
  5252.             LEFT JOIN article__marque as m on m.id = a.marque_id
  5253.             " $join "
  5254.             WHERE
  5255.             a.temporaire is null
  5256.             and (a.archive = 0 or a.archive is null)
  5257.             " $where "
  5258.             LIMIT 0," intval($parametres['nbel']) . "
  5259.         ";
  5260.         $sql trim($sql);
  5261.         //echo $sql;
  5262.         $em       $this->getEntityManager();
  5263.         $stmt     $em->getConnection()->prepare($sql);
  5264.         $articles $stmt->executeQuery()->fetchAllAssociative();
  5265.         return $articles;
  5266.     }
  5267.     public function nbNewSurXjours($nbJours)
  5268.     {
  5269.         $date = new Datetime();
  5270.         $date->sub(new DateInterval('P' $nbJours 'D'));
  5271.         $query $this->createQueryBuilder('p')
  5272.                       ->select("COUNT(p.id) as total")
  5273.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.statutCommande NOT IN (:statutCommande) AND p.dateSuppression IS NULL AND p.date >= :date')
  5274.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE'statutCommande' => [7], 'date' => $date->format("Y-m-d H:i:s")]);
  5275.         $res   $query->getQuery()->getOneOrNullResult();
  5276.         if ($res !== null) {
  5277.             return $res['total'];
  5278.         }
  5279.         return $res;
  5280.     }
  5281.     public function getTotauxFactures($commande)
  5282.     {
  5283.         $query  $this->createQueryBuilder('p')
  5284.             //->select("SUM(p.total) as total")
  5285.                        ->select("SUM(COALESCE(p.total,0)+COALESCE(p.fraisPortSupplementaire,0)+COALESCE(p.totalFraisPort,0)) as total")
  5286.                        ->where('p.commande = :commande AND p.dateSuppression IS NULL AND p.typeDocumentCommercial IN (:typeDocumentCommercial) AND p.statutCommande NOT IN (:statut)')
  5287.                        ->setParameters(['commande' => $commande->getId(), 'typeDocumentCommercial' => [TypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR], 'statut' => [0]]);
  5288.         $res    $query->getQuery()->getOneOrNullResult();
  5289.         $retour 0;
  5290.         if ($res !== null) {
  5291.             $retour $res['total'];
  5292.         }
  5293.         return $retour;
  5294.     }
  5295.     public function getOrVehiculeClient(VehiculeClient $vehiculesClient)
  5296.     {
  5297.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  5298.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => [2], "ordre" => [0]]);
  5299.         $query $this->createQueryBuilder('p')
  5300.                       ->where(
  5301.                           'p.typeDocumentCommercial IN (:typeDocumentCommercial)
  5302.                         AND p.dateSuppression IS NULL
  5303.                         AND p.statutCommande NOT IN (:statutCommande)
  5304.                         AND p.vehiculesClient = :vehiculesClient
  5305.                         '
  5306.                       )
  5307.                       ->setParameters(['typeDocumentCommercial' => [TypeDocumentCommercial::COMMANDE], 'statutCommande' => $liste_statut"vehiculesClient" => $vehiculesClient])
  5308.                       ->orderBy('p.date''DESC');
  5309.         $res   $query->getQuery()->getResult();
  5310.         return $res;
  5311.     }
  5312.     public function getFacturesNonReglees(Client $client null)
  5313.     {
  5314.         /*
  5315.         $repo_statut_document = $this->getEntityManager()->getRepository('DTCGestionComercialeBundle:StatutCommande');
  5316.     $liste_statut = $repo_statut_document->findBy(array("documentCommercial"=>array(6),"ordre"=>array(0)));
  5317.          *
  5318.          */
  5319.         $repo_statut_paiement $this->getEntityManager()->getRepository(StatutPaiement::class);
  5320.         $statut_paiement      $repo_statut_paiement->findOneByOrdre(2);
  5321.         $where      'p.typeDocumentCommercial = :typeDocumentCommercial
  5322.                                     AND p.dateSuppression IS NULL
  5323.                         AND sc.ordre NOT IN (:statutCommande)
  5324.                                                 AND p.statutPaiement NOT IN (:statutPaiement)
  5325.                         ';
  5326.         $parameters = ['typeDocumentCommercial' => TypeDocumentCommercial::FACTURE'statutCommande' => [0], "statutPaiement" => $statut_paiement];
  5327.         if (is_object($client)) {
  5328.             $where                .= ' AND p.client = :client';
  5329.             $parameters['client'] = $client->getId();
  5330.         }
  5331.         $query $this->createQueryBuilder('p')
  5332.                       ->join('p.statutCommande''sc')
  5333.                       ->where($where)
  5334.                       ->setParameters($parameters);
  5335.         $res $query->getQuery()->getResult();
  5336.         return $res;
  5337.     }
  5338.     public function getFacturesEtAvoirsNonReglees(Client $client null)
  5339.     {
  5340.         $where      'p.typeDocumentCommercial IN (:typeDocumentCommercial)
  5341.                                     AND p.dateSuppression IS NULL
  5342.                         AND sc.ordre NOT IN (:statutCommande)
  5343.                                                 AND sp NOT IN (:statutPaiement)
  5344.                         ';
  5345.         $parameters = ['typeDocumentCommercial' => [TypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR], 'statutCommande' => [0], "statutPaiement" => [24]];
  5346.         if (is_object($client)) {
  5347.             $where                .= ' AND p.client = :client';
  5348.             $parameters['client'] = $client->getId();
  5349.         }
  5350.         $query $this->createQueryBuilder('p')
  5351.                       ->join('p.statutCommande''sc')
  5352.                       ->join('p.statutPaiement''sp')
  5353.                       ->where($where)
  5354.                       ->setParameters($parameters);
  5355.         $res $query->getQuery()->getResult();
  5356.         return $res;
  5357.     }
  5358.     public function getFacturesEtAvoirsNonReglesEtNonLettres(Client $client null)
  5359.     {
  5360.         $where      "p.typeDocumentCommercial IN (:typeDocumentCommercial)
  5361.                                     AND p.dateSuppression IS NULL
  5362.                         AND sc.ordre NOT IN (:statutCommande)
  5363.                                                 AND sp.ordre NOT IN (:statutPaiement)
  5364.                                                 AND (p.numeroLettrage IS NULL OR p.numeroLettrage = '')
  5365.                                                 AND (p.lettrage IS NULL OR p.lettrage = '')
  5366.                         ";
  5367.         $parameters = ['typeDocumentCommercial' => [TypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR], 'statutCommande' => [0], "statutPaiement" => [24]];
  5368.         if (is_object($client)) {
  5369.             $where                .= ' AND p.client = :client';
  5370.             $parameters['client'] = $client->getId();
  5371.         }
  5372.         $query $this->createQueryBuilder('p')
  5373.                       ->join('p.statutCommande''sc')
  5374.                       ->join('p.statutPaiement''sp')
  5375.                       ->where($where)
  5376.                       ->setParameters($parameters);
  5377.         $res $query->getQuery()->getResult();
  5378.         return $res;
  5379.     }
  5380.     public function getBlsAfacturerMultiBl($clientId)
  5381.     {
  5382.         $query $this->createQueryBuilder('p')
  5383.                       ->leftJoin('p.statutCommande''sc')
  5384.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.client = :clientId AND p.aFacturer = 1 AND p.attenteFacturation = 0 AND sc.ordre != 0')
  5385.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BL'clientId' => $clientId])
  5386.                       ->orderBy('p.date''DESC');
  5387.         return $query;
  5388.     }
  5389.     public function findBlsOfBp(Commande $bp)
  5390.     {
  5391.         $query $this->createQueryBuilder('p')
  5392.                       ->leftJoin('p.statutCommande''sc')
  5393.                       ->where('p.typeDocumentCommercial = :typeDocumentCommercial AND p.bp = :bp AND sc.ordre != 0')
  5394.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BL'bp' => $bp])
  5395.                       ->orderBy('p.date''DESC');
  5396.         return $query;
  5397.     }
  5398.     public function findBlsOfCommande(Commande $commande$facturesComptabilisees = [])
  5399.     {
  5400.         $where      'p.typeDocumentCommercial = :typeDocumentCommercial AND p.commande = :commande AND sc.ordre != 0';
  5401.         $parameters = ['typeDocumentCommercial' => TypeDocumentCommercial::BL'commande' => $commande];
  5402.         if (count($facturesComptabilisees) > 0) {
  5403.             $where                                .= ' AND f.id NOT IN (:facturesComptabilisees)';
  5404.             $parameters['facturesComptabilisees'] = $facturesComptabilisees;
  5405.         }
  5406.         $query $this->createQueryBuilder('p')
  5407.                       ->leftJoin('p.statutCommande''sc')
  5408.                       ->leftJoin('p.facture''f')
  5409.                       ->where($where)
  5410.                       ->setParameters($parameters)
  5411.                       ->orderBy('p.date''DESC');
  5412.         return $query;
  5413.     }
  5414.     public function getNumeroLettrage(Client $client)
  5415.     {
  5416.         $query $this->createQueryBuilder('p')
  5417.                       ->select('MAX(p.numeroLettrage)')
  5418.                       ->join('p.client''cl')
  5419.                       ->where('cl.id = :client')
  5420.                       ->setParameters(['client' => $client->getId()])//->orderBy('p.date', 'DESC')
  5421.         ;
  5422.         $res   $query->getQuery()->getSingleScalarResult();
  5423.         $res++;
  5424.         return $res;
  5425.     }
  5426.     public function getFacturesEtAvoirsClient(Client $client$start 0$length 25$sortColumn 'date'$sortDir 'DESC'$requete '')
  5427.     {
  5428.         $limit $start $length;
  5429.         $where 'p.typeDocumentCommercial IN (:typeDocumentCommercial)
  5430.                         AND p.dateSuppression IS NULL
  5431.                         AND s.ordre NOT IN (:ordre)
  5432.                         AND p.client = :client
  5433.                         ';
  5434.         if ($requete != '') {
  5435.             $where .= $requete;
  5436.         }
  5437.         $query $this->createQueryBuilder('p')
  5438.                       ->join('p.statutCommande''s')
  5439.                       ->where($where)
  5440.                       ->setParameters(['typeDocumentCommercial' => [TypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR], "ordre" => [0], "client" => $client])
  5441.                       ->add('orderBy''p.' $sortColumn ' ' $sortDir);
  5442.         if ($length 0) {
  5443.             $query->setMaxResults($limit);
  5444.         }
  5445.         return $query;
  5446.         /*
  5447.         $res = $query->getQuery()->getResult();
  5448.         return $res;
  5449.              *
  5450.              */
  5451.     }
  5452.     public function getNbFacturesEtAvoirsClient(Client $client)
  5453.     {
  5454.         $query $this->createQueryBuilder('p')
  5455.                       ->select('COUNT(p.id)')
  5456.                       ->join('p.statutCommande''s')
  5457.                       ->where(
  5458.                           'p.typeDocumentCommercial IN (:typeDocumentCommercial)
  5459.                         AND p.dateSuppression IS NULL
  5460.                         AND s.ordre NOT IN (:ordre)
  5461.                         AND p.client = :client
  5462.                         '
  5463.                       )
  5464.                       ->setParameters(['typeDocumentCommercial' => [TypeDocumentCommercial::FACTURETypeDocumentCommercial::AVOIR], "ordre" => [0], "client" => $client]);
  5465.         return $query->getQuery()->getSingleScalarResult();
  5466.     }
  5467.     public function statCaArticles($parametres = [])
  5468.     {
  5469.         $limit     "10000000";
  5470.         $articles  = [];
  5471.         $dateDebut DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  5472.         $dateDebut->setTime(000000);
  5473.         $dateFin DateTime::createFromFormat('m/Y'$parametres['date_fin']);
  5474.         $dateFin->setTime(235959);
  5475.         $where "";
  5476.         $join  "";
  5477.         //print_r($parametres);
  5478.         $calculN1 false;
  5479.         if (array_key_exists('n1'$parametres) and $parametres["n1"] == "1") {
  5480.             $calculN1 true;
  5481.         }
  5482.         $calculBl false;
  5483.         if (array_key_exists('bl'$parametres) and $parametres["bl"] == "1") {
  5484.             $calculBl true;
  5485.         }
  5486.         $articles   = [];
  5487.         $articles[] = ["libelle" => "VC""stock" => "0"];
  5488.         $articles[] = ["libelle" => "OR""stock" => "0"];
  5489.         $articles[] = ["libelle" => "Atelier""stock" => "0"];
  5490.         $articles[] = ["libelle" => "Mag Atelier""stock" => "0"];
  5491.         $articles[] = ["libelle" => "Magasin""stock" => "0"];
  5492.         $articles[] = ["libelle" => "VO""stock" => "0"];
  5493.         $articles[] = ["libelle" => "PO""stock" => "0"];
  5494.         $articles[] = ["libelle" => "PE""stock" => "0"];
  5495.         $valeurs       = [];
  5496.         $mois          = [];
  5497.         $qteTotal      "";
  5498.         $qteTotalStock "";
  5499.         $qteTotalMois  = [];
  5500.         for ($i 0$i count($articles); $i++) {
  5501.             $quantite      = [];
  5502.             $mois          = [];
  5503.             $total         0;
  5504.             $dateFinBoucle DateTime::createFromFormat('m/Y'$parametres['date_fin']);
  5505.             $dateFinBoucle->setTime(235959);
  5506.             //echo "TEST 001";
  5507.             $dateDebutBoucle DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  5508.             $dateDebutBoucle->setTime(000000);
  5509.             $dateDebutBoucle_n1 DateTime::createFromFormat('d/m/Y''01/' $parametres['date_debut']);
  5510.             $dateDebutBoucle_n1->setTime(235959);
  5511.             $dateDebutBouclen_1 $dateDebutBoucle_n1->modify('-1 year');
  5512.             $qteTotalStock $qteTotalStock $articles[$i]['stock'];
  5513.             while ($dateDebutBoucle $dateFinBoucle) {
  5514.                 if ($calculN1) {
  5515.                     $mois[] = [$dateDebutBoucle->format("m/Y"), $dateDebutBouclen_1->format("m/Y")];
  5516.                 } else {
  5517.                     $mois[] = [$dateDebutBoucle->format("m/Y")];
  5518.                 }
  5519.                 $valeurs         = [];
  5520.                 $valeurs["mois"] = $dateDebutBoucle->format("m/Y");
  5521.                 if ($calculN1) {
  5522.                     $valeurs["moisn_1"] = $dateDebutBouclen_1->format("m/Y");
  5523.                 }
  5524.                 if ($articles[$i]["libelle"] == "VC") {
  5525.                     //$valeurs["quantite"] = "123";
  5526.                     $valeurs["quantite"] = $this->totalVenduMoisType("VC"$dateDebutBoucle$calculBl);
  5527.                     if ($calculN1) {
  5528.                         $valeurs["quantite_n1"] = $this->totalVenduMoisType("VC"$dateDebutBouclen_1$calculBl);
  5529.                     }
  5530.                 } elseif ($articles[$i]["libelle"] == "OR") {
  5531.                     $valeurs["quantite"] = $this->totalVenduMoisType("OR"$dateDebutBoucle$calculBl);
  5532.                     if ($calculN1) {
  5533.                         $valeurs["quantite_n1"] = $this->totalVenduMoisType("OR"$dateDebutBouclen_1$calculBl);
  5534.                     }
  5535.                 } elseif ($articles[$i]["libelle"] == "Atelier") {
  5536.                     $valeurs["quantite"] = $this->totalVenduMoisType("Atelier"$dateDebutBoucle$calculBl);
  5537.                     if ($calculN1) {
  5538.                         $valeurs["quantite_n1"] = $this->totalVenduMoisType("OR"$dateDebutBouclen_1$calculBl);
  5539.                     }
  5540.                 } elseif ($articles[$i]["libelle"] == "Mag Atelier") {
  5541.                     $valeurs["quantite"] = $this->totalVenduMoisType("Mag Atelier"$dateDebutBoucle$calculBl);
  5542.                     if ($calculN1) {
  5543.                         $valeurs["quantite_n1"] = $this->totalVenduMoisType("OR"$dateDebutBouclen_1$calculBl);
  5544.                     }
  5545.                 } elseif ($articles[$i]["libelle"] == "Magasin") {
  5546.                     $valeurs["quantite"] = $this->totalVenduMoisType("Magasin"$dateDebutBoucle$calculBl);
  5547.                     if ($calculN1) {
  5548.                         $valeurs["quantite_n1"] = $this->totalVenduMoisType("OR"$dateDebutBouclen_1$calculBl);
  5549.                     }
  5550.                 } elseif ($articles[$i]["libelle"] == "VO") {
  5551.                     $valeurs["quantite"] = $this->totalVenduMoisType("VO"$dateDebutBoucle$calculBl);
  5552.                     if ($calculN1) {
  5553.                         $valeurs["quantite_n1"] = $this->totalVenduMoisType("OR"$dateDebutBouclen_1$calculBl);
  5554.                     }
  5555.                 } elseif ($articles[$i]["libelle"] == "PO") {
  5556.                     $valeurs["quantite"] = $this->totalVenduMoisType("PO"$dateDebutBoucle$calculBl);
  5557.                     if ($calculN1) {
  5558.                         $valeurs["quantite_n1"] = $this->totalVenduMoisType("OR"$dateDebutBouclen_1$calculBl);
  5559.                     }
  5560.                 } elseif ($articles[$i]["libelle"] == "PE") {
  5561.                     $valeurs["quantite"] = $this->totalVenduMoisType("PE"$dateDebutBoucle$calculBl);
  5562.                     if ($calculN1) {
  5563.                         $valeurs["quantite_n1"] = $this->totalVenduMoisType("OR"$dateDebutBouclen_1$calculBl);
  5564.                     }
  5565.                 }
  5566.                 $quantite[] = $valeurs;
  5567.                 if (array_key_exists($dateDebutBoucle->format("m/Y"), $qteTotalMois)) {
  5568.                     $qteTotalMois[$dateDebutBoucle->format("m/Y")] += $valeurs["quantite"];
  5569.                 } else {
  5570.                     $qteTotalMois[$dateDebutBoucle->format("m/Y")] = $valeurs["quantite"];
  5571.                 }
  5572.                 if ($calculN1) {
  5573.                     if (array_key_exists($dateDebutBouclen_1->format("m/Y"), $qteTotalMois)) {
  5574.                         $qteTotalMois[$dateDebutBouclen_1->format("m/Y")] += $valeurs["quantite_n1"];
  5575.                     } else {
  5576.                         $qteTotalMois[$dateDebutBouclen_1->format("m/Y")] = $valeurs["quantite_n1"];
  5577.                     }
  5578.                 }
  5579.                 $dateDebutBoucle->add(new DateInterval('P1M'));
  5580.                 $dateDebutBouclen_1->add(new DateInterval('P1M'));
  5581.                 $total $total $valeurs["quantite"];
  5582.             }
  5583.             $articles[$i]["valeurs"] = $quantite;
  5584.             $articles[$i]["total"]   = $total;
  5585.             $qteTotal                $qteTotal $total;
  5586.         }
  5587.         //print_r($articles);
  5588.         $retour             = [];
  5589.         $retour['articles'] = $articles;
  5590.         $retour['mois']     = $mois;
  5591.         $retour['total']    = ["valeur" => $qteTotal"totalMois" => $qteTotalMois"stock" => $qteTotalStock];
  5592.         //print_r($retour);
  5593.         return $retour;
  5594.     }
  5595.     public function getCommandeAmaj()
  5596.     {
  5597.         $query $this->createQueryBuilder('p')
  5598.                       ->select("p.id, p.dateSuppression")
  5599.                       ->join("p.typeDocumentCommercial""t")
  5600.                       ->where('p.maj = 1 AND p.dateSuppression IS NULL AND t.id IN (2,4)')
  5601.                       ->orderBy('p.id''DESC');
  5602.         $res $query->getQuery()->getResult();
  5603.         return $res;
  5604.     }
  5605.     public function getCommandeIdsStatutAmodifier()
  5606.     {
  5607.         $query $this->createQueryBuilder('p')
  5608.                       ->select("p.id, p.dateSuppression")
  5609.                       //->join("p.typeDocumentCommercial", "t")
  5610.                       ->where('p.statutAmodifier = 1 AND p.dateSuppression IS NULL AND p.typeDocumentCommercial IN (2,3)')
  5611.                       ->orderBy('p.id''DESC');
  5612.         $res $query->getQuery()->getResult();
  5613.         return $res;
  5614.     }
  5615.     public function _getCommandeIdsDateLivraisonTheoriqueAmodifier()
  5616.     {
  5617.         $date = new Datetime();
  5618.         $date->modify('-60 days');
  5619.         $query $this->createQueryBuilder('p')
  5620.                       ->select("p.id, p.dateSuppression")
  5621.                       ->join("p.typeDocumentCommercial""t")
  5622.                       ->where("p.date > '" $date->format('Y-m-d h:i:s') . "' AND p.dateSuppression IS NULL AND t.id IN (1)")//devis
  5623.                       ->orderBy('p.id''DESC');
  5624.         $res $query->getQuery()->getResult();
  5625.         return $res;
  5626.     }
  5627.     public function getCommandeIdsDateLivraisonTheoriqueAmodifier($articlesId = [])
  5628.     {
  5629.         $date = new Datetime();
  5630.         $date->modify('-60 days');
  5631.         $where      "p.date > '" $date->format('Y-m-d h:i:s') . "' AND p.dateSuppression IS NULL AND p.id IN (1)";
  5632.         $parameters = [];
  5633.         $query $this->createQueryBuilder('p')
  5634.                       ->select("p.id, p.dateSuppression")
  5635.                       //->join("p.typeDocumentCommercial", "t");
  5636.         ;
  5637.         if (count($articlesId)) {
  5638.             $query->leftJoin('p.articleCommande''ac')
  5639.                   ->leftJoin('ac.article''a');
  5640.             $where                    .= ' AND a.id IN (:articlesId)';
  5641.             $parameters['articlesId'] = $articlesId;
  5642.         }
  5643.         $query
  5644.             ->where($where)//devis
  5645.             ->orderBy('p.id''DESC');
  5646.         if (count($parameters)) {
  5647.             $query->setParameters($parameters);
  5648.         }
  5649.         $res $query->getQuery()->getResult();
  5650.         return $res;
  5651.     }
  5652.     public function getCommandeIdsDateExpeditionTheoriqueAmodifier($articlesId = [])
  5653.     {
  5654.         $date = new Datetime();
  5655.         $date->modify('-60 days');
  5656.         $where      "p.date > '" $date->format('Y-m-d h:i:s') . "' AND p.dateSuppression IS NULL AND t.id IN (1, 2)";//devis et commande
  5657.         $parameters = [];
  5658.         $query $this->createQueryBuilder('p')
  5659.                       ->select("p.id, p.dateSuppression")
  5660.                       ->join("p.typeDocumentCommercial""t");
  5661.         if (count($articlesId)) {
  5662.             $query->leftJoin('p.articleCommande''ac')
  5663.                   ->leftJoin('ac.article''a');
  5664.             $where                    .= ' AND a.id IN (:articlesId)';
  5665.             $parameters['articlesId'] = $articlesId;
  5666.         }
  5667.         $query
  5668.             ->where($where)//devis
  5669.             ->orderBy('p.id''DESC');
  5670.         if (count($parameters)) {
  5671.             $query->setParameters($parameters);
  5672.         }
  5673.         $res $query->getQuery()->getResult();
  5674.         return $res;
  5675.     }
  5676.     public function totalVenduMoisType($type "VC"$date$integreBl false)
  5677.     {
  5678.         //echo "<div>TYPE:".$type."</div>";
  5679.         if ($type == "VC") {
  5680.             $join  "";
  5681.             $where "";
  5682.             $blocBl "";
  5683.             if ($integreBl) {
  5684.                 $blocBl "OR
  5685.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  5686.             AND f.statut_commande_id != 23 and cc.type_document_commercial_id = " TypeDocumentCommercial::COMMANDE " and cc.type_document_commercial_id = " TypeDocumentCommercial::COMMANDE "
  5687.             )";
  5688.             }
  5689.             $sql "
  5690.             SELECT     SUM(COALESCE(ac.total_ht,0)) as total,
  5691.                      SUM(COALESCE(ac.tva_vente,0)) as total_tva,
  5692.                     SUM(COALESCE(ac.quantite,0)) as total_quantite,
  5693.                     SUM(COALESCE(ac.total_ht,0)) as total_ht,
  5694.                     (SUM(COALESCE(ac.total_ht,0))+SUM(COALESCE(ac.tva_vente,0))) as total_ca,
  5695.                     c.id as commande_id,
  5696.                     cc.id as commande__commande_id
  5697.             FROM `commerciale__article_commande` as ac
  5698.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5699.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  5700.             LEFT JOIN commerciale__commande as cc on c.commande_id = cc.id
  5701.             LEFT JOIN client__client as u on u.id = c.client_id
  5702.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  5703.             " $join "
  5704.             WHERE
  5705.             c.date >= '" $date->format('Y-m-d ') . "00:00:00' AND c.date <= '" $date->format('Y-m-t H:i:s') . "'
  5706.             AND
  5707.             (
  5708.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 and cc.type_document_commercial_id = " TypeDocumentCommercial::COMMANDE ")
  5709.             " $blocBl "
  5710.             )
  5711.             " $where "
  5712.     ";
  5713.             /*
  5714.     */
  5715.         } elseif ($type == "OR") {
  5716.             $join  "";
  5717.             $where "";
  5718.             $blocBl "";
  5719.             if ($integreBl) {
  5720.                 $blocBl "OR
  5721.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  5722.             AND f.statut_commande_id != 23 and cc.type_document_commercial_id = " TypeDocumentCommercial::COMMANDE " and cc.type_document_commercial_id = " TypeDocumentCommercial::ORDREREPARATION "
  5723.             )";
  5724.             }
  5725.             $sql "
  5726.             SELECT     SUM(COALESCE(ac.total_ht,0)) as total,
  5727.                      SUM(COALESCE(ac.tva_vente,0)) as total_tva,
  5728.                     SUM(COALESCE(ac.quantite,0)) as total_quantite,
  5729.                     SUM(COALESCE(ac.total_ht,0)) as total_ht,
  5730.                     (SUM(COALESCE(ac.total_ht,0))+SUM(COALESCE(ac.tva_vente,0))) as total_ca,
  5731.                     c.id as commande_id,
  5732.                     cc.id as commande__commande_id
  5733.             FROM `commerciale__article_commande` as ac
  5734.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5735.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  5736.             LEFT JOIN commerciale__commande as cc on c.commande_id = cc.id
  5737.             LEFT JOIN client__client as u on u.id = c.client_id
  5738.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  5739.             " $join "
  5740.             WHERE
  5741.             c.date >= '" $date->format('Y-m-d ') . "00:00:00' AND c.date <= '" $date->format('Y-m-t H:i:s') . "'
  5742.             AND
  5743.             (
  5744.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 and cc.type_document_commercial_id = " TypeDocumentCommercial::ORDREREPARATION ")
  5745.             " $blocBl "
  5746.             )
  5747.             " $where "
  5748.     ";
  5749.         } //else if($type == "Atelier") {
  5750.         else {
  5751.             $join  "";
  5752.             $where "";
  5753.             $blocBl "";
  5754.             if ($integreBl) {
  5755.                 $blocBl "OR
  5756.             (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 24 AND c.facture_id > 0 AND f.multi_bl = 1
  5757.             AND f.statut_commande_id != 23 and cc.type_document_commercial_id = " TypeDocumentCommercial::COMMANDE " and cc.type_document_commercial_id = " TypeDocumentCommercial::ORDREREPARATION "
  5758.             )";
  5759.             }
  5760.             $sql "
  5761.             SELECT     SUM(COALESCE(ac.total_ht,0)) as total,
  5762.                      SUM(COALESCE(ac.tva_vente,0)) as total_tva,
  5763.                     SUM(COALESCE(ac.quantite,0)) as total_quantite,
  5764.                     SUM(COALESCE(ac.total_ht,0)) as total_ht,
  5765.                     (SUM(COALESCE(ac.total_ht,0))+SUM(COALESCE(ac.tva_vente,0))) as total_ca,
  5766.                     c.id as commande_id,
  5767.                     cc.id as commande__commande_id
  5768.             FROM `commerciale__article_commande` as ac
  5769.             LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5770.             LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  5771.             LEFT JOIN commerciale__commande as cc on c.commande_id = cc.id
  5772.             LEFT JOIN client__client as u on u.id = c.client_id
  5773.             LEFT JOIN article__article as ar on ar.id = ac.article_id
  5774.             " $join "
  5775.             WHERE
  5776.             c.date >= '" $date->format('Y-m-d ') . "00:00:00' AND c.date <= '" $date->format('Y-m-t H:i:s') . "'
  5777.             AND
  5778.             (
  5779.             (c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23 and cc.type_document_commercial_id = " TypeDocumentCommercial::ORDREREPARATION ")
  5780.             " $blocBl "
  5781.             )
  5782.             " $where "
  5783.     ";
  5784.         }
  5785.         //echo "<div>".$type." ".$sql."<br/><br/></div>";
  5786.         $sql   trim($sql);
  5787.         $em    $this->getEntityManager();
  5788.         $stmt  $em->getConnection()->prepare($sql);
  5789.         $total $stmt->executeQuery()->fetchAllAssociative();
  5790.         //print_r($total);
  5791.         if (is_array($total) && array_key_exists('0'$total)) {
  5792.             //echo $sql."<br/>(".$total[0]['total'].")<br/>";
  5793.             return $total[0]['total'];
  5794.         } else {
  5795.             return "0";
  5796.         }
  5797.     }
  5798.     public function calculCaPeriode($type "VC"$date$integreBl false$n1 false)
  5799.     {
  5800.         $retour = [];
  5801.         //echo "TYPE ".$type;
  5802.         //echo "DATE ".$date;
  5803.         $dateDebut DateTime::createFromFormat('m/Y'$date);
  5804.         $dateDebut->modify('first day of this month')->setTime(000000);
  5805.         //echo "DATE DEBUT FORMAT ".$dateDebut->format("Y-m-d H:i:s");
  5806.         $dateFin DateTime::createFromFormat('m/Y'$date);
  5807.         $dateFin->modify('last day of this month')->setTime(235959);
  5808.         //echo $dateFin->format("Y-m-d H:i:s");
  5809.         if ($n1) {
  5810.             $dateDebut->sub(new DateInterval('P1Y'));
  5811.             $dateFin->sub(new DateInterval('P1Y'));
  5812.             //echo "DATE DEBUT FORMAT ".$dateDebut->format("Y-m-d H:i:s");
  5813.             //echo "DATE FIN FORMAT ".$dateFin->format("Y-m-d H:i:s");
  5814.         }
  5815.         //$dateDebut = \DateTime::createFromFormat('d/m/Y',$parametres['date_debut']);
  5816.         //$dateDebut->setTime(00,00,00);
  5817.         //$dateFin = \DateTime::createFromFormat('d/m/Y',$parametres['date_fin']);
  5818.         //$dateFin->setTime(23,59,59);
  5819.         $where              "";
  5820.         $join               "";
  5821.         $join_table_client  false;
  5822.         $join_table_article false;
  5823.         $join_table_article true;
  5824.         //echo "type:".$type;
  5825.         $total 0;
  5826.         if (== 1) {
  5827.             if ($type == "Atelier MO") {
  5828.                 //if($where != "") $where .= " AND";
  5829.                 $where .= " AND ar.main_oeuvre = '1'";
  5830.                 $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  5831.                 //$where .= " AND cor.type_document_commercial_id = '3'";
  5832.                 $where .= " AND (cor.type_document_commercial_id = '3' OR (cor.type_document_commercial_id = '7' and corigine.type_document_commercial_id='3'))";
  5833.             } elseif ($type == "Mag Atelier") {
  5834.                 //if($where != "") $where .= " AND";
  5835.                 $where .= " AND (ar.main_oeuvre = '0' or ar.main_oeuvre = NULL)";
  5836.                 $where .= " AND ar.reference NOT LIKE '%OCCAS%' AND ar.reference NOT LIKE '%DTC-VO%'";
  5837.                 $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  5838.                 //$where .= " AND cor.type_document_commercial_id = '3'";
  5839.                 $where .= " AND (cor.type_document_commercial_id = '3' OR (cor.type_document_commercial_id = '7' and corigine.type_document_commercial_id='3'))";
  5840.             } elseif ($type == "Magasin") {
  5841.                 //if($where != "") $where .= " AND";
  5842.                 //$where .= " AND (ar.main_oeuvre = '0' or ar.main_oeuvre = NULL)";
  5843.                 $where .= " AND ar.reference NOT LIKE '%OCCAS%' AND ar.reference NOT LIKE '%DTC-VO%'";
  5844.                 $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  5845.                 //$where .= " AND cor.type_document_commercial_id = '2'";
  5846.                 $where .= " AND (cor.type_document_commercial_id = '2' OR (cor.type_document_commercial_id = '7' and corigine.type_document_commercial_id='2'))";
  5847.                 //370095
  5848.                 $where .= " AND (c.pays_livraison_id = '370095' or c.pays_livraison_id is null)";
  5849.             } elseif ($type == "VO") {
  5850.                 //if($where != "") $where .= " AND";
  5851.                 $where .= " AND (ar.main_oeuvre = '0' or ar.main_oeuvre = NULL)";
  5852.                 $where .= " AND ar.reference LIKE '%DTC-VO%'";
  5853.                 $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  5854.                 //$where .= " AND cor.type_document_commercial_id = '2'";
  5855.             } elseif ($type == "PO") {
  5856.                 //if($where != "") $where .= " AND";
  5857.                 $where .= " AND (ar.main_oeuvre = '0' or ar.main_oeuvre = NULL)";
  5858.                 $where .= " AND ar.reference LIKE '%OCCAS%'";
  5859.                 $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  5860.                 //$where .= " AND cor.type_document_commercial_id = '2'";
  5861.             } elseif ($type == "PE") {
  5862.                 //if($where != "") $where .= " AND";
  5863.                 $where .= " AND (c.pays_livraison_id != '370095' and c.pays_livraison_id is not null)";
  5864.                 $where .= " AND ar.reference NOT LIKE '%DTC-VO%'";
  5865.                 $where .= " AND ar.reference NOT LIKE '%OCCAS%'";
  5866.                 $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  5867.                 $where .= " AND ((cor.type_document_commercial_id != '3' OR (cor.type_document_commercial_id = '7' and corigine.type_document_commercial_id !='3')))";
  5868.                 //$where .= " AND cor.type_document_commercial_id = '2'";
  5869.             }
  5870.         }
  5871.         $sql "
  5872.         SELECT     SUM(COALESCE(ac.prix_achat,0)*COALESCE(ac.quantite,0)) as total
  5873.         FROM `article__mouvement_stock` as ac
  5874.         LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5875.         LEFT JOIN commerciale__commande as corigine on c.commande_id = corigine.id
  5876.         LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  5877.         LEFT JOIN client__client as u on u.id = c.client_id
  5878.         LEFT JOIN article__article as ar on ar.id = ac.article_id
  5879.         " $join "
  5880.         WHERE
  5881.         ac.article_commande_virtuel_id is null
  5882.         AND
  5883.         (
  5884.         ( (ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "') and (c.date >= '" $dateDebut->format(
  5885.                 'Y-m-d H:i:s'
  5886.             ) . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  5887.         or
  5888.         (c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "')
  5889.         )
  5890.         AND
  5891.         (
  5892.         (  (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25 AND (c.facture_id is NOT NULL and f.statut_commande_id != 23))
  5893.             OR
  5894.             ((c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23) )
  5895.             OR
  5896.             (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19))
  5897.         )
  5898.         " $where "
  5899.     ";
  5900.         $sql "
  5901.         SELECT     SUM(COALESCE(ac.prix_achat,0)*COALESCE(ac.quantite,0)) as total
  5902.         FROM `article__mouvement_stock` as ac
  5903.         LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5904.         LEFT JOIN commerciale__commande as corigine on c.commande_id = corigine.id
  5905.         LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  5906.         LEFT JOIN client__client as u on u.id = c.client_id
  5907.         LEFT JOIN article__article as ar on ar.id = ac.article_id
  5908.         " $join "
  5909.         WHERE
  5910.         ac.article_commande_virtuel_id is null
  5911.         AND
  5912.         (
  5913.         ( (c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  5914.         or
  5915.         (f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-d H:i:s') . "')
  5916.         )
  5917.         AND
  5918.         (
  5919.         (   (
  5920.                 c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25
  5921.                 AND (c.facture_id is NOT NULL and f.statut_commande_id != 23)
  5922.                 and (f.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND f.date <= '" $dateFin->format('Y-m-d H:i:s') . "')
  5923.             )
  5924.             OR
  5925.             ((c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23) )
  5926.             OR
  5927.             (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19))
  5928.         )
  5929.         " $where "
  5930.     ";
  5931.         /*
  5932.     $sql = "
  5933.         SELECT     SUM(COALESCE(ac.prix_achat,0)*COALESCE(ac.quantite,0)) as total
  5934.         FROM `article__mouvement_stock` as ac
  5935.         LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5936.         LEFT JOIN commerciale__commande as corigine on c.commande_id = corigine.id
  5937.         LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  5938.         LEFT JOIN client__client as u on u.id = c.client_id
  5939.         LEFT JOIN article__article as ar on ar.id = ac.article_id
  5940.         ".$join."
  5941.         WHERE
  5942.         ac.article_commande_virtuel_id is null
  5943.         AND
  5944.         ac.date_mouvement >= '".$dateDebut->format('Y-m-d H:i:s')."' AND ac.date_mouvement <= '".$dateFin->format('Y-m-d H:i:s')."'
  5945.         AND
  5946.         (
  5947.         (  (c.type_document_commercial_id = ".TypeDocumentCommercial::BL." AND c.statut_commande_id != 25 AND (c.facture_id is NOT NULL and f.statut_commande_id != 23))
  5948.             OR
  5949.             ((c.type_document_commercial_id = ".TypeDocumentCommercial::FACTURE." AND c.statut_commande_id != 23) )
  5950.             OR
  5951.             (c.type_document_commercial_id = ".TypeDocumentCommercial::AVOIR." AND c.statut_commande_id != 19))
  5952.         )
  5953.         ".$where."
  5954.     ";
  5955.     */
  5956.         $sql trim($sql);
  5957.         if ($type == "Magasin") {
  5958.             //echo $sql;
  5959.         }
  5960.         $em      $this->getEntityManager();
  5961.         $stmt    $em->getConnection()->prepare($sql);
  5962.         $totalCa $stmt->executeQuery()->fetchAllAssociative();
  5963.         //print_r($totalCa);
  5964.         $total $totalCa[0]["total"];
  5965.         //echo "AAA==>".$total;
  5966.         //$tableautemp = array();
  5967.         if ($integreBl) {
  5968.             //echo "INTEG BL";
  5969.             $sql "
  5970.         SELECT     SUM(COALESCE(ac.prix_achat,0)*COALESCE(ac.quantite,0)) as total,
  5971.                  AVG(COALESCE(ac.cpump,0)) as pump_avg,
  5972.                 SUM(COALESCE(ac.quantite,0)) as total_quantite,
  5973.                 ar.reference,
  5974.                 ar.libelle,
  5975.                 ar.id as article_id,
  5976.                 ar.taux_marge,
  5977.                 ar.pump,
  5978.                 ar.cpump,
  5979.                 ar.produit_interne as est_compose,
  5980.                 ar.virtuel as est_virtuel,
  5981.                 ar.marge,
  5982.                 ar.prix_base as prix_achat
  5983.         FROM `article__mouvement_stock` as ac
  5984.         LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  5985.         LEFT JOIN commerciale__commande as corigine on c.commande_id = corigine.id
  5986.         LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  5987.         LEFT JOIN client__client as u on u.id = c.client_id
  5988.         LEFT JOIN article__article as ar on ar.id = ac.article_id
  5989.         " $join "
  5990.         WHERE
  5991.         ac.article_commande_virtuel_id is null
  5992.         AND
  5993.         (
  5994.         ( (ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "') and (c.date >= '" $dateDebut->format(
  5995.                     'Y-m-d H:i:s'
  5996.                 ) . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "'))
  5997.         or
  5998.         (c.date >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND c.date <= '" $dateFin->format('Y-m-d H:i:s') . "')
  5999.         )
  6000.         AND
  6001.         (
  6002.         ( (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25))
  6003.         )
  6004.         AND (c.facture_id is NULL or f.statut_commande_id = 23)
  6005.         " $where "
  6006.         ";
  6007.             $em      $this->getEntityManager();
  6008.             $stmt    $em->getConnection()->prepare($sql);
  6009.             $totalCa $stmt->executeQuery()->fetchAllAssociative();
  6010.             $total $total $totalCa[0]["total"];
  6011.             if ($type == "VO") {
  6012.                 //echo "<br/><br/>".$sql;
  6013.             }
  6014.             //print_r($articles_bl);
  6015.         }
  6016.         //$retour['articles'] = $tableautemp;
  6017.         return $total;
  6018.     }
  6019.     public function ___calculCaPeriode($type "VC"$date$integreBl false$n1 false)
  6020.     {
  6021.         $retour = [];
  6022.         //echo "TYPE ".$type;
  6023.         //echo "DATE ".$date;
  6024.         $dateDebut DateTime::createFromFormat('m/Y'$date);
  6025.         $dateDebut->modify('first day of this month')->setTime(000000);
  6026.         $dateFin DateTime::createFromFormat('m/Y'$date);
  6027.         $dateFin->modify('last day of this month')->setTime(235959);
  6028.         if ($n1) {
  6029.             $dateDebut->sub(new DateInterval('P1Y'));
  6030.             $dateFin->sub(new DateInterval('P1Y'));
  6031.             //echo "DATE DEBUT FORMAT ".$dateDebut->format("Y-m-d H:i:s");
  6032.             //echo "DATE FIN FORMAT ".$dateFin->format("Y-m-d H:i:s");
  6033.         }
  6034.         //$dateDebut = \DateTime::createFromFormat('d/m/Y',$parametres['date_debut']);
  6035.         //$dateDebut->setTime(00,00,00);
  6036.         //$dateFin = \DateTime::createFromFormat('d/m/Y',$parametres['date_fin']);
  6037.         //$dateFin->setTime(23,59,59);
  6038.         $where              "";
  6039.         $join               "";
  6040.         $join_table_client  false;
  6041.         $join_table_article false;
  6042.         $join_table_article true;
  6043.         $total 0;
  6044.         if ($type == "Atelier MO") {
  6045.             //if($where != "") $where .= " AND";
  6046.             $where .= " AND ar.main_oeuvre = '1'";
  6047.             $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  6048.             //$where .= " AND cor.type_document_commercial_id = '3'";
  6049.             $where .= " AND (cor.type_document_commercial_id = '3' OR (cor.type_document_commercial_id = '7' and corigine.type_document_commercial_id='3'))";
  6050.         } elseif ($type == "Mag Atelier") {
  6051.             //if($where != "") $where .= " AND";
  6052.             $where .= " AND (ar.main_oeuvre = '0' or ar.main_oeuvre = NULL)";
  6053.             $where .= " AND ar.reference NOT LIKE '%OCCAS%' AND ar.reference NOT LIKE '%DTC-VO%'";
  6054.             $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  6055.             //$where .= " AND cor.type_document_commercial_id = '3'";
  6056.             $where .= " AND (cor.type_document_commercial_id = '3' OR (cor.type_document_commercial_id = '7' and corigine.type_document_commercial_id='3'))";
  6057.         } elseif ($type == "Magasin") {
  6058.             //if($where != "") $where .= " AND";
  6059.             //$where .= " AND (ar.main_oeuvre = '0' or ar.main_oeuvre = NULL)";
  6060.             $where .= " AND ar.reference NOT LIKE '%OCCAS%' AND ar.reference NOT LIKE '%DTC-VO%'";
  6061.             $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  6062.             //$where .= " AND cor.type_document_commercial_id = '2'";
  6063.             $where .= " AND (cor.type_document_commercial_id = '2' OR (cor.type_document_commercial_id = '7' and corigine.type_document_commercial_id='2'))";
  6064.             //370095
  6065.             $where .= " AND c.pays_livraison_id = '370095'";
  6066.         } elseif ($type == "VO") {
  6067.             //if($where != "") $where .= " AND";
  6068.             $where .= " AND (ar.main_oeuvre = '0' or ar.main_oeuvre = NULL)";
  6069.             $where .= " AND ar.reference LIKE '%DTC-VO%'";
  6070.             $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  6071.             //$where .= " AND cor.type_document_commercial_id = '2'";
  6072.         } elseif ($type == "PO") {
  6073.             //if($where != "") $where .= " AND";
  6074.             $where .= " AND (ar.main_oeuvre = '0' or ar.main_oeuvre = NULL)";
  6075.             $where .= " AND ar.reference LIKE '%OCCAS%'";
  6076.             $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  6077.             //$where .= " AND cor.type_document_commercial_id = '2'";
  6078.         } elseif ($type == "PE") {
  6079.             //if($where != "") $where .= " AND";
  6080.             $where .= " AND c.pays_livraison_id != '370095'";
  6081.             $where .= " AND ar.reference NOT LIKE '%DTC-VO%'";
  6082.             $where .= " AND ar.reference NOT LIKE '%OCCAS%'";
  6083.             $join  .= "LEFT JOIN commerciale__commande as cor on c.commande_id = cor.id";
  6084.             //$where .= " AND cor.type_document_commercial_id = '2'";
  6085.         }
  6086.         $sql "
  6087.         SELECT     SUM(COALESCE(ac.prix_achat,0)*COALESCE(ac.quantite,0)) as total
  6088.         FROM `article__mouvement_stock` as ac
  6089.         LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  6090.         LEFT JOIN commerciale__commande as corigine on c.commande_id = corigine.id
  6091.         LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  6092.         LEFT JOIN client__client as u on u.id = c.client_id
  6093.         LEFT JOIN article__article as ar on ar.id = ac.article_id
  6094.         " $join "
  6095.         WHERE
  6096.         ac.article_commande_virtuel_id is null
  6097.         AND
  6098.         ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  6099.         AND
  6100.         (
  6101.         (  (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25 AND (c.facture_id is NOT NULL and f.statut_commande_id != 23))
  6102.             OR
  6103.             ((c.type_document_commercial_id = " TypeDocumentCommercial::FACTURE " AND c.statut_commande_id != 23) )
  6104.             OR
  6105.             (c.type_document_commercial_id = " TypeDocumentCommercial::AVOIR " AND c.statut_commande_id != 19))
  6106.         )
  6107.         " $where "
  6108.     ";
  6109.         $sql trim($sql);
  6110.         $em      $this->getEntityManager();
  6111.         $stmt    $em->getConnection()->prepare($sql);
  6112.         $totalCa $stmt->executeQuery()->fetchAllAssociative();
  6113.         //print_r($totalCa);
  6114.         $total $totalCa[0]["total"];
  6115.         //echo "AAA==>".$total;
  6116.         //$tableautemp = array();
  6117.         if ($integreBl) {
  6118.             //echo "INTEG BL";
  6119.             $sql "
  6120.         SELECT     SUM(COALESCE(ac.prix_achat,0)*COALESCE(ac.quantite,0)) as total,
  6121.                  AVG(COALESCE(ac.cpump,0)) as pump_avg,
  6122.                 SUM(COALESCE(ac.quantite,0)) as total_quantite,
  6123.                 ar.reference,
  6124.                 ar.libelle,
  6125.                 ar.id as article_id,
  6126.                 ar.taux_marge,
  6127.                 ar.pump,
  6128.                 ar.cpump,
  6129.                 ar.produit_interne as est_compose,
  6130.                 ar.virtuel as est_virtuel,
  6131.                 ar.marge,
  6132.                 ar.prix_base as prix_achat
  6133.         FROM `article__mouvement_stock` as ac
  6134.         LEFT JOIN commerciale__commande as c on ac.commande_id = c.id
  6135.         LEFT JOIN commerciale__commande as f on c.facture_id = f.id
  6136.         LEFT JOIN client__client as u on u.id = c.client_id
  6137.         LEFT JOIN article__article as ar on ar.id = ac.article_id
  6138.         " $join "
  6139.         WHERE
  6140.         ac.article_commande_virtuel_id is null
  6141.         AND
  6142.         ac.date_mouvement >= '" $dateDebut->format('Y-m-d H:i:s') . "' AND ac.date_mouvement <= '" $dateFin->format('Y-m-d H:i:s') . "'
  6143.         AND
  6144.         (
  6145.         ( (c.type_document_commercial_id = " TypeDocumentCommercial::BL " AND c.statut_commande_id != 25))
  6146.         )
  6147.         AND (c.facture_id is NULL or f.statut_commande_id = 23)
  6148.         " $where "
  6149.         ";
  6150.             $em      $this->getEntityManager();
  6151.             $stmt    $em->getConnection()->prepare($sql);
  6152.             $totalCa $stmt->executeQuery()->fetchAllAssociative();
  6153.             $total $total $totalCa[0]["total"];
  6154.             if ($type == "VO") {
  6155.                 //echo "<br/><br/>".$sql;
  6156.             }
  6157.             //print_r($articles_bl);
  6158.         }
  6159.         //$retour['articles'] = $tableautemp;
  6160.         return $total;
  6161.     }
  6162.     public function getNbCommandesInternetPayeesNonValidees()
  6163.     {
  6164.         $query $this->createQueryBuilder('x')
  6165.                       ->leftjoin('x.reglements''r')
  6166.                       ->leftjoin('x.acomptes''a')
  6167.                       ->select("x.id, COUNT(a.id) as totalAcomptes, COUNT(r.id) as totalReglements")
  6168.                       ->where("x.origine LIKE 'prestashop' AND (x.valideManuellement = 0 OR x.valideManuellement IS NULL)")
  6169.                       ->groupBy('x.id')
  6170.                       ->having('totalAcomptes > 0 OR totalReglements > 0');
  6171.         $res $query->getQuery()->getResult();
  6172.         return $res;
  6173.     }
  6174.     public function getDate1ereCommandeClient($client)
  6175.     {
  6176.         $query $this->createQueryBuilder('p')
  6177.                       ->select("p.date")
  6178.                       ->join("p.client""c")
  6179.                       ->where('p.dateSuppression IS NULL AND p.typeDocumentCommercial IN (:typeDocumentCommercial) AND c.id = :client')
  6180.                       ->setParameter('client'$client->getId())
  6181.                       ->setParameter('typeDocumentCommercial', [TypeDocumentCommercial::COMMANDETypeDocumentCommercial::ORDREREPARATION] )
  6182.                       ->orderBy('p.id''ASC')
  6183.                       ->setMaxResults(1);
  6184.         $res $query->getQuery()->getResult();
  6185.         if (empty($res[0])) {
  6186.             return '';
  6187.         } else {
  6188.             return $res[0]['date'];
  6189.         }
  6190.     }
  6191.     public function getCommandes21jours()
  6192.     {
  6193.         $parameters = [];
  6194.         $where      "";
  6195.         $where                                "x.typeDocumentCommercial = :typeDocumentCommercial ";
  6196.         $parameters["typeDocumentCommercial"] = "2";
  6197.         $where                .= "AND sc.id IN(:statut) ";
  6198.         $parameters["statut"] = ["6""9"];
  6199.         $dateDebut = new DateTime();
  6200.         $dateDebut->setTime(000000);
  6201.         $dateDebut->sub(new DateInterval('P21D'));
  6202.         //$dateDebut->sub(new \DateInterval('P31D'));
  6203.         //$dateDebut->sub(new \DateInterval('P91D'));
  6204.         //echo $dateDebut->format("Y-m-d H:i:s");
  6205.         if ($where == "") {
  6206.             $where .= "x.date >= :date_debut ";
  6207.         } else {
  6208.             $where .= "AND x.date >= :date_debut ";
  6209.         }
  6210.         //$where .= "AND c.date >= :date_debut ";
  6211.         $parameters["date_debut"] = $dateDebut;
  6212.         $query $this->createQueryBuilder('x')
  6213.                       ->select(
  6214.                           ' x.referenceMarketPlace as refMP, '
  6215.                           'c.nom, '
  6216.                           'c.prenom, '
  6217.                           'x.rueLivraison as adresse1, '
  6218.                           'x.complementLivraison as adresse2, '
  6219.                           'x.complementLivraison2 as adresse3, '
  6220.                           'x.codePostalLivraisonTexte as code_postal,'
  6221.                           'x.villeLivraisonTexte as ville, '
  6222.                           'x.paysLivraisonTexte as pays, '
  6223.                           'c.telephone, '
  6224.                           'c.telephone2, '
  6225.                           'c.email,'
  6226.                           'x.destinataireLivraisonTexte as destinataireLivraisonTexte,'
  6227.                           'x.numeroLivraison as numeroLivraison'
  6228.                       );
  6229.         if ($where != "") {
  6230.             $query->where($where)->setParameters($parameters);
  6231.         }
  6232.         $query->leftJoin('x.client''c')
  6233.               ->leftJoin('x.statutCommande''sc');
  6234.         $query->orderBy('x.date''DESC');
  6235.         $res $query->getQuery()->getResult();
  6236.         //print_r($res);
  6237.         //exit;
  6238.         return $res;
  6239.     }
  6240.     public function getBpFreres($commande)
  6241.     {
  6242.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  6243.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 4"ordre" => [0]]);
  6244.         $query $this->createQueryBuilder('p')
  6245.             //->select("p.id")
  6246.                       ->where(
  6247.                 'p.id != :bp AND p.commande = :commande AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL and p.statutCommande NOT IN (:statutCommande)'
  6248.             )
  6249.                       ->orderBy('p.id''ASC')
  6250.                       ->setParameters(['commande' => $commande->getCommande(), 'bp' => $commande'typeDocumentCommercial' => TypeDocumentCommercial::BP'statutCommande' => $liste_statut]);
  6251.         $res   $query->getQuery()->getResult();
  6252.         return $res;
  6253.     }
  6254.     public function getNbCommandesDisponiblesPourLpPourTransporteur($transporteur)
  6255.     {
  6256.         //AND c.compte_market_place_id is not null
  6257.         //
  6258.         $where_retrait_mag "";
  6259.         if ($transporteur->getId() == 13) {
  6260.             $where_retrait_mag " and c.compte_market_place_id = 19 ";
  6261.         }
  6262.         $total_commande_valide 0;
  6263.         $sql "select c.*
  6264.                 FROM commerciale__commande as c
  6265.                 LEFT JOIN transporteur__transporteur as t ON c.transporteur_id = t.id
  6266.                 LEFT JOIN client__client as cl ON c.client_id = cl.id
  6267.                 LEFT JOIN commerciale__statut_paiement as sp ON c.statut_paiement_id = sp.id
  6268.                 WHERE c.statut_commande_id = 9
  6269.                 AND c.client_id is not null
  6270.                 AND c.dateSuppression is NULL
  6271.                 AND c.type_document_commercial_id = " TypeDocumentCommercial::COMMANDE "
  6272.                 AND c.transporteur_id = " $transporteur->getId() .
  6273.                $where_retrait_mag
  6274.                " and ((c.compte_market_place_id is NOT NULL AND c.reference_market_place is NOT NULL) OR cl.facturation_multi_bl = 1 OR sp.id IN (2) OR c.total_ttc = 0)"
  6275.                " ORDER BY c.date_insertion ASC
  6276.                 LIMIT 0,5000000
  6277.                 ";
  6278.         $sql trim($sql);
  6279.         //if($transporteur->getId() == 13) echo "<div>".$sql."</div>";
  6280.         $em        $this->getEntityManager();
  6281.         $stmt      $em->getConnection()->prepare($sql);
  6282.         $commandes $stmt->executeQuery()->fetchAllAssociative();
  6283.         //echo "<div>count:".count($commandes)."</div>";
  6284.         if (count($commandes) == 0) {
  6285.             //echo "<hr/>";
  6286.             return 0;
  6287.         }
  6288.         foreach ($commandes as $cmde) {
  6289.             //Avant d'ajouter la commande à une LP on vérifie qu'elle est mono ligne et que le stock est dispo.
  6290.             $sql_ac "select *
  6291.                     FROM commerciale__article_commande
  6292.                     WHERE quantite >= 0 AND commande_id = " $cmde["id"] . "
  6293.                     ";
  6294.             //echo "<div>".$sql_ac."</div>";
  6295.             $stmt              $em->getConnection()->prepare($sql_ac);
  6296.             $article_commandes $stmt->executeQuery()->fetchAllAssociative();
  6297.             //echo "<br/><br/><div>ref_commande ".$cmde["reference"]."</div>";
  6298.             //echo "<div>count(article_commandes) ".count($article_commandes)."</div>";
  6299.             if (count($article_commandes) > 0) {
  6300.                 //On regarde si il existe un bp déja dans une LP pour cette commande
  6301.                 $sql_bp "select id,reference
  6302.             FROM commerciale__commande
  6303.             WHERE type_document_commercial_id = " TypeDocumentCommercial::BP "
  6304.             AND commande_id = " $cmde["id"] . "
  6305.             AND statut_commande_id != 24
  6306.             ";
  6307.                 $stmt        $em->getConnection()->prepare($sql_bp);
  6308.                 $bp_commande $stmt->executeQuery()->fetchAllAssociative();
  6309.                 //echo "<div>count bp ".count($bp_commande)."</div>";
  6310.                 if (count($bp_commande) > 0) {
  6311.                 } else {
  6312.                     $total_commande_valide++;
  6313.                 }
  6314.             }
  6315.         }
  6316.         //echo "<hr/>";
  6317.         return $total_commande_valide;
  6318.     }
  6319.     public function getAvoirsClientEtFacture($facture)
  6320.     {
  6321.         $repo_statut_document $this->getEntityManager()->getRepository(StatutCommande::class);
  6322.         $liste_statut         $repo_statut_document->findBy(["documentCommercial" => 7"ordre" => [1]]);
  6323.         $query $this->createQueryBuilder('p')//->select("COUNT(p.id) as total")
  6324.         ;
  6325.         $where 'p.client = :client AND (p.factureAvoir IS NULL OR p.factureAvoir = :facture) AND p.typeDocumentCommercial = :typeDocumentCommercial AND p.dateSuppression IS NULL AND p.statutCommande IN (:statutCommande)';
  6326.         $query->where($where)
  6327.               ->setParameters(['facture' => $facture'client' => $facture->getClient(), 'typeDocumentCommercial' => TypeDocumentCommercial::AVOIR'statutCommande' => $liste_statut]);
  6328.         $res $query->getQuery()->getResult();
  6329.         return $res;
  6330.     }
  6331.     public function getBlsSansRapportCloture($transporteur)
  6332.     {
  6333.         $query $this->createQueryBuilder('p')
  6334.                       ->leftJoin('p.statutCommande''sc')
  6335.                       ->where(
  6336.                           'p.typeDocumentCommercial = :typeDocumentCommercial AND sc.ordre != 0 AND p.rapportCloture is null AND p.dateSuppression IS NULL AND p.transporteur = :transporteur'
  6337.                       )
  6338.                       ->setParameters(['typeDocumentCommercial' => TypeDocumentCommercial::BL'transporteur' => $transporteur]);
  6339.         $res   $query->getQuery()->getResult();
  6340.         return $res;
  6341.     }
  6342.     public function findCommandesMPSansAcompte()
  6343.     {
  6344.         $query $this->createQueryBuilder('p')
  6345.                       ->select('p.id, COUNT(a.id) as total')
  6346.                       ->leftJoin('p.compteMarketPlace''cmp')
  6347.                       ->leftJoin('p.statutCommande''sc')
  6348.                       ->leftJoin(Acompte::class, 'a''with''a.commande = p.id')
  6349.                       ->where(
  6350.                           'p.date > :dateDebut AND p.typeDocumentCommercial = ' TypeDocumentCommercial::COMMANDE ' AND sc.ordre NOT IN (0) AND p.dateSuppression IS NULL and p.referenceMarketPlace IS NOT NULL AND cmp.id IN (:comptesMarketPlace)'
  6351.                       )
  6352.                       ->setParameters(['comptesMarketPlace' => [11121315], 'dateDebut' => '2021-07-01 00:00:00'])
  6353.                       ->groupBy('p.id')
  6354.                       ->having('total = 0')
  6355.                       ->orderBy('p.date''ASC');
  6356.         $res   $query->getQuery()->getResult();
  6357.         return $res;
  6358.     }
  6359.     public function getFacturesEtAvoirs($parametres)
  6360.     {
  6361.         $where      'p.dateSuppression IS NULL AND p.typeDocumentCommercial IN (:types) AND sc.ordre NOT IN (:statutCommande)';
  6362.         $parameters = [
  6363.             'types'          => [67],//Facture et avoirs
  6364.             'statutCommande' => [0],
  6365.             //'commande' => $commande
  6366.         ];
  6367.         if (!empty($parametres["date_debut_ca_pfc"])) {
  6368.             $dateDebut DateTime::createFromFormat('d/m/Y'$parametres["date_debut_ca_pfc"]);
  6369.             $where     .= " AND p.date >= '" $dateDebut->format('Y-m-d 00:00:00') . "'";
  6370.         }
  6371.         if (!empty($parametres["date_fin_ca_pfc"])) {
  6372.             $dateFin DateTime::createFromFormat('d/m/Y'$parametres["date_fin_ca_pfc"]);
  6373.             $where   .= " AND p.date <= '" $dateFin->format('Y-m-d 23:59:59') . "'";
  6374.         }
  6375.         $query $this->createQueryBuilder('p')
  6376.             //->select("p.id")
  6377.                       ->join('p.statutCommande''sc')
  6378.                       ->where($where)
  6379.                       ->setParameters($parameters)//->setMaxResults(100)
  6380.         ;
  6381.         //dump($query->getQuery()->getSql());
  6382.         return $query->getQuery()->getResult();
  6383.     }
  6384.     public function getcommandeEnRAlPourCF($commandeCF$recherche$maxResults 25$parametres = [])
  6385.     {
  6386.         $query $this->createQueryBuilder('p')
  6387.                       ->select("DISTINCT(c.id) as id, c.reference")
  6388.                       ->leftJoin(ArticleCommande::class, 'ac''with''ac.article = p.id')
  6389.                       ->join('ac.commande''c')
  6390.                       ->leftJoin(TypeDocumentCommercial::class, 'td''with''td.id = c.typeDocumentCommercial')
  6391.                       ->join('c.client''cl')
  6392.                       ->where('ac.ralClient > 0 and p.id IN (:ids) and td.id IN (:typeDocument)')
  6393.                       ->setParameters(['ids' => $articles'typeDocument' => [23]]);
  6394.         $res   $query->getQuery()->getResult();
  6395.         return $res;
  6396.         //print_r($parametres);
  6397.         $cond $recherche "%";
  6398.         if (strpos($cond"*") !== false) {
  6399.             $cond str_replace("*""%"$cond);
  6400.         }
  6401.         if (array_key_exists('archive_recherche'$parametres)) {
  6402.             $query $this->createQueryBuilder('p')
  6403.                           ->leftJoin(VehiculeClient::class, 'v''with''p.vehiculesClient = v.id')
  6404.                           ->where('(p.reference LIKE :cond or p.referenceMarketPlace LIKE :cond or v.immatriculation = :recherche) AND p.typeDocumentCommercial = :typeDocumentCommercial')
  6405.                           ->setParameters(['recherche' => $recherche'cond' => $cond'typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE])
  6406.                           ->setFirstResult(0)
  6407.                           ->setMaxResults($maxResults)
  6408.                           ->orderBy('p.date''DESC');
  6409.         } else {
  6410.             $query $this->createQueryBuilder('p')
  6411.                           ->leftJoin(VehiculeClient::class, 'v''with''p.vehiculesClient = v.id')
  6412.                           ->where('(p.reference LIKE :cond or p.referenceMarketPlace LIKE :cond or v.immatriculation = :recherche) AND p.typeDocumentCommercial = :typeDocumentCommercial')
  6413.                           ->setParameters(['recherche' => $recherche'cond' => $cond'typeDocumentCommercial' => TypeDocumentCommercial::COMMANDE])
  6414.                           ->setFirstResult(0)
  6415.                           ->setMaxResults($maxResults)
  6416.                           ->orderBy('p.date''DESC');
  6417.         }
  6418.         $res $query->getQuery()->getResult();
  6419.         return $res;
  6420.         //return array(0=>array('id'=>1));
  6421.     }
  6422.     public function getTotalFabrications($commande "")
  6423.     {
  6424.         $sql 'select count(*) as total
  6425.                     from commerciale__fabrication f
  6426.                     LEFT JOIN commerciale__article_commande ac ON f.article_commande_commande_client_id = ac.id                        
  6427.                     where
  6428.                     ac.commande_id = '.$commande->getid().'
  6429.                                                                    
  6430.                     ';
  6431.         $em $this->getEntityManager();
  6432.         $stmt $em->getConnection()->prepare($sql);
  6433.         $res =  $stmt->executeQuery()->fetchAllAssociative();
  6434.         return $res[0]["total"];
  6435.     }
  6436. }