src/Repository/GestionComerciale/AcompteRepository.php line 98

Open in your IDE?
  1. <?php
  2. namespace App\Repository\GestionComerciale;
  3. use App\Entity\GestionComerciale\Acompte;
  4. use App\Entity\GestionComerciale\Commande;
  5. use App\Entity\GestionComerciale\ModeReglement;
  6. use App\Entity\GestionComerciale\NumerotationDocument;
  7. use DateInterval;
  8. use Datetime;
  9. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. /**
  12.  * AcompteRepository
  13.  *
  14.  * This class was generated by the Doctrine ORM. Add your own custom
  15.  * repository methods below.
  16.  */
  17. class AcompteRepository extends ServiceEntityRepository
  18. {
  19.     public function __construct(ManagerRegistry $registry)
  20.     {
  21.         parent::__construct($registryAcompte::class);
  22.     }
  23.     public function getTotalAcompte(Commande $commande)
  24.     {
  25.         $query $this->createQueryBuilder('p')
  26.                       ->select("COUNT(p.id) as total")
  27.                       ->where('p.commande = :commande AND p.typeReglement = :typeReglement AND p.dateSuppression IS NULL')
  28.                       ->setParameters(['commande' => $commande'typeReglement' => 1]);
  29.         $res $query->getQuery()->getOneOrNullResult();
  30.         return $res;
  31.     }
  32.     public function getTotalReglementCommande(Commande $commande)
  33.     {
  34.         $query $this->createQueryBuilder('p')
  35.                       ->select("COUNT(p.id) as total")
  36.                       ->join('p.commande''c')
  37.                       ->join('c.statutCommande''sc')
  38.                       ->where('p.parent = :commande AND p.typeReglement = :typeReglement AND sc.ordre != 0')
  39.                       ->setParameters(['commande' => $commande'typeReglement' => 2]);
  40.         $res $query->getQuery()->getOneOrNullResult();
  41.         return $res;
  42.     }
  43.     public function getTotalReglement(Commande $commande)
  44.     {
  45.         $query $this->createQueryBuilder('p')
  46.                       ->select("COUNT(p.id) as total")
  47.                       ->where('p.commande = :commande AND p.typeReglement = :typeReglement')
  48.                       ->setParameters(['commande' => $commande'typeReglement' => 2]);
  49.         $res $query->getQuery()->getOneOrNullResult();
  50.         return $res;
  51.     }
  52.     public function getTotalReglementParTraite($moisRaz '')
  53.     {
  54.         if ($moisRaz != "") {
  55.             //Y-m-d H:i:s
  56.             $date = new Datetime();
  57.             if ($moisRaz == '99') {
  58.                 $date_mois = new Datetime($date->format('Y').'-'.$date->format('m').'-01');
  59.             } else {
  60.                 $date_mois = new Datetime($date->format('Y').'-'.$moisRaz.'-01');
  61.             }
  62.             $query $this->createQueryBuilder('p')
  63.                           ->select("COUNT(p.id) as total")
  64.                           ->join('p.modeReglement''mr')
  65.                           ->where('p.dateSuppression IS NULL AND p.date >= :date AND p.modeReglement = 6')
  66.                           ->setParameters(['date' => $date_mois->format("Y-m-d H:i:s")]);
  67.         } else {
  68.             $query $this->createQueryBuilder('p')
  69.                           ->select("COUNT(p.id) as total")
  70.                           ->where('p.dateSuppression IS NULL AND p.modeReglement = 6');
  71.         }
  72.         $res $query->getQuery()->getOneOrNullResult();
  73.         return $res;
  74.     }
  75.     public function getTotalReglementParModeReglement($moisRaz ''$modeReglement)
  76.     {
  77.         $where      '';
  78.         $parameters = [];
  79.         $repo_mode_reglement $this->getEntityManager()->getRepository(ModeReglement::class);
  80.         //Verifier si il existe un masque de numérotation pour ce mode de reglement
  81.         $repo_numerotation $this->getEntityManager()->getRepository(NumerotationDocument::class);
  82.         $numerotation      $repo_numerotation->findOneBy(['modeReglement' => $modeReglement]);
  83.         if (is_object($numerotation)) {
  84.             if ($where != '') {
  85.                 $where .= ' AND ';
  86.             }
  87.             $where                       .= 'mr.id = :modeReglement';
  88.             $parameters['modeReglement'] = $modeReglement;
  89.         } else {
  90.             //Trouver les modes de réglement sans masque
  91.             $modesReglementSsMasque = [];
  92.             $modesReglement         $repo_mode_reglement->findAll();
  93.             foreach ($modesReglement as $modeReglement) {
  94.                 $num $repo_numerotation->findOneBy(['modeReglement' => $modeReglement]);
  95.                 if ( ! is_object($num)) {
  96.                     $modesReglementSsMasque[] = $modeReglement->getId();
  97.                 }
  98.             }
  99.             if ($where != '') {
  100.                 $where .= ' AND ';
  101.             }
  102.             $where                       .= 'mr.id IN (:modeReglement)';
  103.             $parameters['modeReglement'] = $modesReglementSsMasque;
  104.         }
  105.         if ($moisRaz != "") {
  106.             if ($where != '') {
  107.                 $where .= ' AND ';
  108.             }
  109.             $where              .= 'p.date >= :date';
  110.             $parameters['date'] = $date_mois->format("Y-m-d H:i:s");
  111.             //Y-m-d H:i:s
  112.             $date = new Datetime();
  113.             if ($moisRaz == '99') {
  114.                 $date_mois = new Datetime($date->format('Y').'-'.$date->format('m').'-01');
  115.             } else {
  116.                 $date_mois = new Datetime($date->format('Y').'-'.$moisRaz.'-01');
  117.             }
  118.             $query $this->createQueryBuilder('p')
  119.                           ->select("COUNT(p.id) as total")
  120.                           ->join('p.modeReglement''mr')
  121.                           ->where($where)
  122.                           ->setParameters($parameters);
  123.         } else {
  124.             $query $this->createQueryBuilder('p')
  125.                           ->select("COUNT(p.id) as total")
  126.                           ->join('p.modeReglement''mr')
  127.                           ->where($where)
  128.                           ->setParameters($parameters);
  129.         }
  130.         $res $query->getQuery()->getOneOrNullResult();
  131.         return $res;
  132.     }
  133.     public function nbNewSurXjours($nbJours)
  134.     {
  135.         $date = new Datetime();
  136.         $date->sub(new DateInterval('P'.$nbJours.'D'));
  137.         $query $this->createQueryBuilder('p')
  138.                       ->select("COUNT(p.id) as total")
  139.                       ->where('p.dateSuppression IS NULL AND p.date >= :date')
  140.                       ->setParameters(['date' => $date->format("Y-m-d H:i:s")]);
  141.         $res   $query->getQuery()->getOneOrNullResult();
  142.         if ($res !== null) {
  143.             return $res['total'];
  144.         }
  145.         return $res;
  146.     }
  147.     public function getMontantRembousement($client)
  148.     {
  149.         $query $this->createQueryBuilder('p')
  150.                       ->select("SUM(p.montant) as total")
  151.                       ->where(
  152.                           'p.dateSuppression IS NULL AND p.client = :client AND p.commande IS NULL AND p.remboursement IS NULL AND (p.montant > :montant) AND (p.estParent = 0 or p.estParent is NULL) AND (p.horsErp = 0 OR p.horsErp IS NULL) AND (p.mouvementEquilibrage = 0 OR p.mouvementEquilibrage IS NULL)'
  153.                       )
  154.                       ->setParameters(['client' => $client'montant' => '0']);
  155.         $res    $query->getQuery()->getOneOrNullResult();
  156.         $retour 0;
  157.         if ($res["total"] > 0) {
  158.             $retour '-'.$res["total"];
  159.         }
  160.         return $retour;
  161.     }
  162.     public function getMontantRembousement2($client)
  163.     {
  164.         $query $this->createQueryBuilder('p')
  165.                       ->select("SUM(p.montant) as total")
  166.                       ->where(
  167.                           'p.dateSuppression IS NULL AND p.client = :client  AND p.remboursement IS NULL AND (p.montant > :montant) AND (p.estParent = 0 or p.estParent is NULL) AND (p.horsErp = 0 OR p.horsErp IS NULL) AND (p.mouvementEquilibrage = 0 OR p.mouvementEquilibrage IS NULL)'
  168.                       )
  169.                       ->setParameters(['client' => $client'montant' => '0']);
  170.         $res    $query->getQuery()->getOneOrNullResult();
  171.         $retour 0;
  172.         if ($res["total"] > 0) {
  173.             $retour '-'.$res["total"];
  174.         }
  175.         return $retour;
  176.     }
  177.     public function getMontantRembourser($client)
  178.     {
  179.         $query $this->createQueryBuilder('p')
  180.                       ->select("SUM(p.montant) as total")
  181.                       ->where(
  182.                           'p.dateSuppression IS NULL AND p.client = :client AND p.commande IS NOT NULL AND p.remboursement IS NULL AND (p.montant < :montant) AND (p.estParent = 0 or p.estParent is NULL) AND (p.horsErp = 0 OR p.horsErp IS NULL) AND (p.mouvementEquilibrage = 0 OR p.mouvementEquilibrage IS NULL)'
  183.                       )
  184.                       ->setParameters(['client' => $client'montant' => '0']);
  185.         $res    $query->getQuery()->getOneOrNullResult();
  186.         $retour 0;
  187.         /*
  188.         if($res["total"] < 0) {
  189.             $retour = '-'.$res["total"];
  190.         }
  191.                  * 
  192.                  */
  193.         if ($res !== null) {
  194.             $retour $res["total"];
  195.         }
  196.         return $retour;
  197.     }
  198.     public function getMontantAccompte($client)
  199.     {
  200.         $query $this->createQueryBuilder('p')
  201.                       ->select("SUM(p.montant) as total")
  202.                       ->where(
  203.                           'p.dateSuppression IS NULL AND p.client = :client AND p.commande IS NOT NULL AND p.remboursement IS NULL and (p.estParent = 0 or p.estParent is NULL) AND (p.horsErp = 0 OR p.horsErp IS NULL) AND (p.mouvementEquilibrage = 0 OR p.mouvementEquilibrage IS NULL)'
  204.                       )
  205.                       ->setParameters(['client' => $client]);
  206.         $res    $query->getQuery()->getOneOrNullResult();
  207.         $retour 0;
  208.         /*
  209.         if($res["total"] < 0) {
  210.             $retour = '-'.$res["total"];
  211.         }
  212.                  * 
  213.                  */
  214.         if ($res !== null) {
  215.             $retour $res["total"];
  216.         }
  217.         return $retour;
  218.     }
  219.     public function getMontantaPayerPourClient($client)
  220.     {
  221.         $query $this->createQueryBuilder('p')
  222.                       ->select("SUM(p.montant) as total")
  223.                       ->where(
  224.                           'p.dateSuppression IS NULL AND p.client = :client AND p.commande IS NULL AND p.remboursement IS NULL AND (p.montant < :montant) AND (p.estParent = 0 or p.estParent is NULL) AND (p.horsErp = 0 OR p.horsErp IS NULL) AND (p.mouvementEquilibrage = 0 OR p.mouvementEquilibrage IS NULL)'
  225.                       )
  226.                       ->setParameters(['client' => $client'montant' => '0']);
  227.         $retour 0;
  228.         $res $query->getQuery()->getOneOrNullResult();
  229.         if ($res !== null) {
  230.             $retour "".$res["total"];
  231.         }
  232.         return $retour;
  233.     }
  234.     public function getMontantEquilibrage($client)
  235.     {
  236.         $query $this->createQueryBuilder('p')
  237.                       ->select("SUM(p.montant) as total")
  238.                       ->where('p.dateSuppression IS NULL AND p.client = :client AND p.commande IS NULL AND p.mouvementEquilibrage = 1 ')
  239.                       ->setParameters(['client' => $client]);
  240.         $res    $query->getQuery()->getOneOrNullResult();
  241.         $retour 0;
  242.         if ($res !== null) {
  243.             $retour $res["total"];
  244.         }
  245.         return $retour;
  246.     }
  247.     public function getMontantHorsErp($client)
  248.     {
  249.         $query $this->createQueryBuilder('p')
  250.                       ->select("SUM(p.montant) as total")
  251.                       ->where('p.dateSuppression IS NULL AND p.client = :client AND p.commande IS NULL AND p.horsErp = 1 ')
  252.                       ->setParameters(['client' => $client]);
  253.         $res    $query->getQuery()->getOneOrNullResult();
  254.         $retour 0;
  255.         if ($res !== null) {
  256.             $retour $res["total"];
  257.         }
  258.         return $retour;
  259.     }
  260.     public function proposerRemboursement($client)
  261.     {
  262.         return true;
  263.     }
  264.     public function getTotauxReglements($facture)
  265.     {
  266.         $client $facture->getClient();
  267.         $query $this->createQueryBuilder('p')
  268.                       ->select("SUM(p.montant) as total")
  269.                       ->where('p.dateSuppression IS NULL AND (p.commande IN (:commande) OR (p.client = :client AND p.commande is null AND p.remboursement is null AND p.estParent is null))')
  270.                       ->setParameters(['commande' => [$facture->getId(), $facture->getCommande()->getId()], 'client' => $client->getId()]);
  271.         $res    $query->getQuery()->getOneOrNullResult();
  272.         $retour 0;
  273.         if ($res !== null) {
  274.             $retour $res["total"];
  275.         }
  276.         return $retour;
  277.     }
  278.     public function getAcompteDisponible($client)
  279.     {
  280.         $query $this->createQueryBuilder('p')
  281.             //->select("SUM(p.montant) as total")
  282.                       ->where(
  283.                 'p.dateSuppression IS NULL AND p.client = :client AND p.commande IS NULL AND p.remboursement IS NULL AND p.montant > :montant AND (p.estParent = 0 or p.estParent is NULL) AND (p.mouvementEquilibrage = 0 OR p.mouvementEquilibrage IS NULL) AND (p.horsErp = 0 OR p.horsErp IS NULL)'
  284.             )
  285.                       ->setParameters(['client' => $client'montant' => '0'])
  286.                       ->orderBy('p.date''ASC');
  287.         $res $query->getQuery()->getResult();
  288.         return $res;
  289.     }
  290.     public function getTotalAcomptesEtReglements($commande)
  291.     {
  292.         $query $this->createQueryBuilder('p')
  293.                       ->select("SUM(p.montant) as total")
  294.                       ->where('p.dateSuppression IS NULL and (p.commande = :commande or p.parent= :commande)')
  295.                       ->setParameters(['commande' => $commande]);
  296.         $res $query->getQuery()->getOneOrNullResult();
  297.         return $res["total"];
  298.     }
  299.     public function getAcomptesNonUtilisésTotalement($client)
  300.     {
  301.         $query $this->createQueryBuilder('p')
  302.             //->select("SUM(p.montant) as total")
  303.                       ->where(
  304.                 'p.dateSuppression IS NULL AND p.client = :client AND p.commande IS NULL AND p.remboursement IS NULL AND p.montant > :montant AND (p.estParent = 0 or p.estParent is NULL) AND (p.horsErp = 0 OR p.horsErp IS NULL) '
  305.                 .'AND p.lettrage IS NULL '
  306.                 .'AND (rp.id IS NULL or rp.lettrage IS NULL) AND (ap.id IS NULL or ap.lettrage IS NULL)'
  307.             )//AND (p.mouvementEquilibrage = 0 OR p.mouvementEquilibrage IS NULL) AND (p.horsErp = 0 OR p.horsErp IS NULL)
  308.                       ->leftJoin('p.reglementParent''rp')
  309.                       ->leftJoin('p.acompteParent''ap')
  310.                       ->setParameters(['client' => $client'montant' => '0'])
  311.                       ->orderBy('p.date''ASC');
  312.         $res $query->getQuery()->getResult();
  313.         return $res;
  314.     }
  315.     public function getTotalAcomptesEtReglementsClient($client)
  316.     {
  317.         $query $this->createQueryBuilder('p')
  318.                       ->select("SUM(COALESCE(p.montant, 0)) + SUM(COALESCE(p.operationDiverse, 0)) as total")
  319.                       ->where(
  320.                           'p.dateSuppression IS NULL AND p.client = :client AND (p.acompteParent = 0 or p.acompteParent is NULL) AND (p.reglementParent = 0 or p.reglementParent is NULL)'
  321.                       )
  322.                       ->setParameters(['client' => $client]);
  323.         $res $query->getQuery()->getSingleScalarResult();
  324.         return $res;
  325.     }
  326.     public function getAcomptesEtReglementsClientDebug($client)
  327.     {
  328.         $query $this->createQueryBuilder('p')
  329.                       ->select("p.montant")
  330.                       ->where(
  331.                           'p.dateSuppression IS NULL AND p.client = :client AND (p.acompteParent = 0 or p.acompteParent is NULL) AND (p.reglementParent = 0 or p.reglementParent is NULL)'
  332.                       )
  333.                       ->setParameters(['client' => $client]);
  334.         $res $query->getQuery()->getResult();
  335.         return $res;
  336.     }
  337.     public function getNbAcomptesEtReglementsClient($client)
  338.     {
  339.         $query $this->createQueryBuilder('p')
  340.                       ->select("COUNT(p.id) as total")
  341.                       ->where(
  342.                           'p.dateSuppression IS NULL AND p.client = :client AND (p.acompteParent = 0 or p.acompteParent is NULL) AND (p.reglementParent = 0 or p.reglementParent is NULL)'
  343.                       )
  344.                       ->setParameters(['client' => $client]);
  345.         $res $query->getQuery()->getSingleScalarResult();
  346.         return $res;
  347.     }
  348.     public function getAcomptesEtReglementsClient($client$start 1$length 25$sortColumn 'dateReglement'$sortDir 'DESC'$requete '')
  349.     {
  350.         $limit $start $length;
  351.         $where 'p.dateSuppression IS NULL AND p.client = :client AND (p.acompteParent = 0 or p.acompteParent is NULL) AND (p.reglementParent = 0 or p.reglementParent is NULL)';
  352.         if ($requete != '') {
  353.             $where .= $requete;
  354.         }
  355.         $query $this->createQueryBuilder('p')
  356.             //->select("SUM(p.montant) as total")
  357.                       ->where($where)
  358.                       ->setParameters(['client' => $client])
  359.             //->orderBy(array('p.'.$sortColumn => $sortDir))
  360.                       ->add('orderBy''p.'.$sortColumn.' '.$sortDir)
  361.                       ->setMaxResults($limit);
  362.         return $query;
  363.     }
  364.     public function getReglementsBordereau($donnees = [], $output '')
  365.     {
  366.         $where      'p.dateSuppression IS NULL AND (p.acompteParent = 0 or p.acompteParent is NULL) AND (p.reglementParent = 0 or p.reglementParent is NULL)';
  367.         $where      .= ' AND p.bordereau IS NULL';
  368.         $where      .= " AND p.dateReglement > '2021-07-01 00:00:00'";
  369.         $parameters = [];
  370.         if ( ! empty($donnees['date'])) {
  371.             /*
  372.             $where .= ' AND p.date >= :dateDebut';
  373.             $parameters['dateDebut'] = $donnees['date']->format('Y-m-d 00:00:00');
  374.             */
  375.             $where                 .= ' AND p.date <= :dateFin';
  376.             $parameters['dateFin'] = $donnees['date']->format('Y-m-d 23:59:59');
  377.         }
  378.         if ( ! empty($donnees['modeReglement'])) {
  379.             $where                       .= ' AND p.modeReglement = :modeReglement';
  380.             $parameters['modeReglement'] = $donnees['modeReglement'];
  381.         }
  382.         $query $this->createQueryBuilder('p')
  383.                       ->where($where)
  384.                       ->setParameters($parameters);
  385.         if (is_object($output)) {
  386.             $output->writeln($query->getQuery()->getSql());
  387.             foreach ($donnees as $key => $donnee) {
  388.                 $output->writeln($key);
  389.             }
  390.         }
  391.         return $query->getQuery()->getResult();
  392.     }
  393. }