FluxRepository.php 5.87 KB
Newer Older
1 2 3 4
<?php

namespace App\Repository;

5
use App\Entity\Adherent;
Julien Jorry committed
6
use App\Entity\Comptoir;
7
use App\Entity\Flux;
Julien Jorry committed
8
use App\Entity\Groupe;
9 10
use App\Entity\Prestataire;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
11
use Doctrine\Common\Persistence\ManagerRegistry;
12 13 14 15 16 17 18 19 20

/**
 * @method Flux|null find($id, $lockMode = null, $lockVersion = null)
 * @method Flux|null findOneBy(array $criteria, array $orderBy = null)
 * @method Flux[]    findAll()
 * @method Flux[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class FluxRepository extends ServiceEntityRepository
{
21
    public function __construct(ManagerRegistry $registry)
22 23
    {
        parent::__construct($registry, Flux::class);
24 25 26
        $em = $this->getEntityManager();
        $this->connection = $em->getConnection();
        $this->tableName =  $em->getMetadataFactory()->getMetadataFor(Flux::class)->getTableName();
27 28
    }

Julien Jorry committed
29 30 31 32 33 34
    /**
     * @param  Prestataire $presta [description]
     * @param  string   $parenttype Parent type of flux (cotisation, transfert, transaction, vente...)
     * @return Query Returns a query fo finding an array of Flux
     */
    public function getQueryByPrestataire(Prestataire $presta, $parenttype = null)
35
    {
Julien Jorry committed
36 37 38 39 40 41 42 43 44
        $sqlQuery = "SELECT f.id FROM {$this->tableName} f WHERE (f.prestataire_id = :id OR f.prestataire_dest_id = :id)";
        if ($parenttype != null) {
            $sqlQuery .= ' AND f.parenttype = :type';
        }
        $statement = $this->connection->prepare($sqlQuery);
        if ($parenttype != null) {
            $statement->bindValue(':type', $parenttype);
        }
        $statement->bindValue(':id', $presta->getId());
45 46 47 48 49 50
        $statement->execute();
        $results = $statement->fetchAll();
        $qb = $this->createQueryBuilder('f');
        return $qb
            ->where($qb->expr()->in('f.id', ':ids'))
            ->setParameter('ids', $results)
Julien Jorry committed
51
            ->orderBy('f.createdAt', 'DESC')
52 53 54 55
            ->getQuery()
        ;
    }

Julien Jorry committed
56 57 58 59 60 61
    /**
     * @param  Adherent $adherent [description]
     * @param  string   $parenttype Parent type of flux (cotisation, transfert, transaction, vente...)
     * @return Query Returns a query fo finding an array of Flux
     */
    public function getQueryByAdherent(Adherent $adherent, $parenttype = null)
62
    {
Julien Jorry committed
63 64 65 66 67 68 69 70 71
        $sqlQuery = "SELECT f.id FROM {$this->tableName} f WHERE (f.adherent_id = :id OR f.adherent_dest_id = :id)";
        if ($parenttype != null) {
            $sqlQuery .= ' AND f.parenttype = :type';
        }
        $statement = $this->connection->prepare($sqlQuery);
        if ($parenttype != null) {
            $statement->bindValue(':type', $parenttype);
        }
        $statement->bindValue(':id', $adherent->getId());
72 73 74 75 76 77
        $statement->execute();
        $results = $statement->fetchAll();
        $qb = $this->createQueryBuilder('f');
        return $qb
            ->where($qb->expr()->in('f.id', ':ids'))
            ->setParameter('ids', $results)
Julien Jorry committed
78 79 80 81 82
            ->orderBy('f.createdAt', 'DESC')
            ->getQuery()
        ;
    }

Julien Jorry committed
83 84 85 86
    /**
     * @param  Comptoir $comptoir [description]
     * @return Query Returns a query fo finding an array of Flux
     */
Julien Jorry committed
87 88
    public function getQueryByComptoir(Comptoir $comptoir)
    {
89
        $statement = $this->connection->prepare("SELECT f.id FROM {$this->tableName} f WHERE f.comptoir_id = :id");
Julien Jorry committed
90 91 92 93 94 95 96 97 98 99 100 101
        $statement->bindValue('id', $comptoir->getId());
        $statement->execute();
        $results = $statement->fetchAll();
        $qb = $this->createQueryBuilder('f');
        return $qb
            ->where($qb->expr()->in('f.id', ':ids'))
            ->setParameter('ids', $results)
            ->orderBy('f.createdAt', 'DESC')
            ->getQuery()
        ;
    }

Julien Jorry committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    /**
     * @param  Comptoir $comptoir [description]
     * @return Query Returns a query fo finding an array of Flux
     */
    public function getQueryByComptoirParams(Comptoir $comptoir, $params)
    {
        $sql = "SELECT f.id FROM {$this->tableName} f WHERE f.comptoir_id = :id";
        if (isset($param['parenttype'])) {
            $sql.= ' AND f.parenttype = :parenttype';
        }
        if (isset($param['type'])) {
            $sql.= ' AND f.type = :type';
        }
        if (isset($param['start'])) {
            $sql.= ' AND f.created_at > :start';
        }
        if (isset($param['end'])) {
            $sql.= ' AND f.created_at < :end';
        }
        $statement = $this->connection->prepare($sql);
        $statement->bindValue('id', $comptoir->getId());
        if (isset($param['parenttype'])) {
            $statement->bindValue('parenttype', $param['parenttype']);
        }
        if (isset($param['type'])) {
            $statement->bindValue('type', $param['type']);
        }
        if (isset($param['start'])) {
            $statement->bindValue('start', $param['start']);
        }
        if (isset($param['end'])) {
            $statement->bindValue('end', $param['end']);
        }
        $statement->execute();
        $results = $statement->fetchAll();
        $qb = $this->createQueryBuilder('f');
        return $qb
            ->where($qb->expr()->in('f.id', ':ids'))
            ->setParameter('ids', $results)
            ->orderBy('f.createdAt', 'DESC')
            ->getQuery()
        ;
    }

Julien Jorry committed
146 147 148 149
    /**
     * @param  Groupe $groupe [description]
     * @return Query Returns a query fo finding an array of Flux
     */
Julien Jorry committed
150 151
    public function getQueryByGroupe(Groupe $groupe)
    {
152
        $statement = $this->connection->prepare("SELECT f.id FROM {$this->tableName} f WHERE f.groupe_id = :id");
Julien Jorry committed
153 154 155 156 157 158 159 160
        $statement->bindValue('id', $groupe->getId());
        $statement->execute();
        $results = $statement->fetchAll();
        $qb = $this->createQueryBuilder('f');
        return $qb
            ->where($qb->expr()->in('f.id', ':ids'))
            ->setParameter('ids', $results)
            ->orderBy('f.createdAt', 'DESC')
161 162 163 164
            ->getQuery()
        ;
    }
}