src/Repository/GestionComerciale/CommandeRepository.php line 6818

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