src/Repository/__Contact/contact/ContactRepository.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Repository\__Contact\contact;
  3. use App\Entity\__Contact\contact\Contact;
  4. use App\Entity\Booking\AdvBookingChange;
  5. use App\Entity\Email\Email;
  6. use App\Helper\ParticipantHelper;
  7. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  8. use Doctrine\DBAL\DBALException;
  9. use Doctrine\DBAL\Driver\Exception;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. /**
  12.  * @method Contact|null find($id, $lockMode = null, $lockVersion = null)
  13.  * @method Contact|null findOneBy(array $criteria, array $orderBy = null)
  14.  * @method Contact[]    findAll()
  15.  * @method Contact[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  16.  */
  17. class ContactRepository extends ServiceEntityRepository
  18. {
  19.     public const CONTACT_GENDER = [
  20.         => "H",
  21.         => "F",
  22.     ];
  23.     public function __construct(ManagerRegistry $registry)
  24.     {
  25.         parent::__construct($registryContact::class);
  26.     }
  27.     public function findByEmail(string $email): array
  28.     {
  29.         return $this->createQueryBuilder('c')
  30.             ->where('c.professionalEmail = :email OR c.personalEmail = :email')
  31.             ->setParameter('email'$email)
  32.             ->getQuery()
  33.             ->getResult();
  34.     }
  35.     public function findOneByEmail(string $email): ?Contact
  36.     {
  37.         $qb $this->createQueryBuilder('c');
  38.         return $qb
  39.             ->where($qb->expr()->orX(
  40.                 $qb->expr()->eq('c.professionalEmail'':email'),
  41.                 $qb->expr()->eq('c.personalEmail'':email')
  42.             ))
  43.             ->setParameter('email'$email)
  44.             ->getQuery()
  45.             ->setMaxResults(1)
  46.             ->getOneOrNullResult();
  47.     }
  48.     /**
  49.      * TODO : Retourne un user donc devrait plutôt se trouver dans le UserRepository + Régler les dépréciations.
  50.      *
  51.      * Retourne le user du dernier devis.
  52.      *
  53.      * @throws DBALException|Exception
  54.      */
  55.     public function getUserOfLastQuote(Contact $contact): array
  56.     {
  57.         $conn $this->getEntityManager()->getConnection();
  58.         /* @lang text */
  59.         $sql '
  60.             SELECT u.login
  61.             FROM contact c
  62.             JOIN booking_participant pp ON pp.contact_id = c.id
  63.             JOIN booking p ON p.id = pp.booking_id
  64.             JOIN quote q ON q.booking_id = p.id
  65.             JOIN security_users u ON u.id = q.seller_id
  66.             WHERE c.id = :contactId
  67.             ORDER BY q.created_at DESC
  68.             LIMIT 1';
  69.         $stmt $conn->prepare($sql);
  70.         $stmt->execute(['contactId' => $contact->getId()]);
  71.         return $stmt->fetchAll();
  72.     }
  73.     /**
  74.      * TODO : Régler les dépréciations.
  75.      *
  76.      * Retourne les lignes de factures et règlements pour un contact.
  77.      *
  78.      * @throws Exception
  79.      * @throws \Doctrine\DBAL\Exception
  80.      */
  81.     public function getAllLinesforContact(Contact $contact): array
  82.     {
  83.         $conn $this->getEntityManager()->getConnection();
  84.         /* @lang text */
  85.         $sql "
  86.         SELECT
  87.             i.id,
  88.             i.number AS number,
  89.             bp.contact_id,
  90.             i.updated_at AS status_line,
  91.             sum(il.amount*il.quantity) as amount,
  92.             il.description as name,
  93.             'invoice' as origin
  94.         FROM invoice_line il
  95.         JOIN invoice_invoice_line iil on iil.invoice_line_id = il.id
  96.         JOIN invoice i on i.id = iil.invoice_id
  97.         JOIN booking_participant bp on bp.id = i.payer_id
  98.         WHERE bp.contact_id = :contactId
  99.         AND i.is_current = 1
  100.         GROUP BY i.id
  101.         UNION 
  102.         SELECT
  103.             p.id,
  104.             p.status AS number,
  105.             p.contact_id,
  106.             p.status_at AS status_line,
  107.             p.amount AS amount,
  108.             p.mode as name,
  109.             'payment' as origin
  110.         FROM payment p
  111.         where p.contact_id = :contactId
  112.         ORDER BY status_line  DESC
  113.         LIMIT 30
  114.         ";
  115.         $stmt $conn->prepare($sql);
  116.         $stmt->execute(['contactId' => $contact->getId()]);
  117.         return $stmt->fetchAll();
  118.     }
  119.     /**
  120.      * Retourne le prénom et le nom de chaque filleul du contact, ainsi que la date du parrainage.
  121.      */
  122.     public function getSponsored(Contact $contact): array
  123.     {
  124.         return $this->createQueryBuilder('c')
  125.             ->select('f.firstName, f.lastName, s.sponsoredAt')
  126.             ->join('c.sponsors''s')
  127.             ->join('s.sponsoredParty''f')
  128.             ->andWhere('c.id = :contact')
  129.             ->setParameter('contact'$contact)
  130.             ->getQuery()
  131.             ->getResult();
  132.     }
  133.     /**
  134.      * Retourne le prénom et le nom de chaque parrain du contact, ainsi que la date du parrainage.
  135.      */
  136.     public function getSponsors(Contact $contact): array
  137.     {
  138.         return $this->createQueryBuilder('c')
  139.             ->select('p.firstName, p.lastName, s.sponsoredAt')
  140.             ->join('c.sponsoredParties''s')
  141.             ->join('s.sponsor''p')
  142.             ->andWhere('c.id = :contact')
  143.             ->setParameter('contact'$contact)
  144.             ->getQuery()
  145.             ->getResult();
  146.     }
  147.     /**
  148.      * @return Contact[]
  149.      */
  150.     public function findByAdvBookingChange(AdvBookingChange $advBookingChange): array
  151.     {
  152.         return $this->createQueryBuilder('c')
  153.             ->join('c.participants''bp')
  154.             ->join('bp.booking''b')
  155.             ->join('b.advBookingChangeAssociations''abca')
  156.             ->join('abca.bookingChange''abc')
  157.             ->where('abc.id = :advBookingChangeId')
  158.             ->andWhere('bp.roles LIKE :ownerRole')
  159.             ->setParameter('advBookingChangeId'$advBookingChange->getId())
  160.             ->setParameter('ownerRole''%'.ParticipantHelper::OWNER_ROLE.'%')
  161.             ->getQuery()
  162.             ->getResult();
  163.     }
  164.     /**
  165.      * @return Contact[]
  166.      */
  167.     public function findByEmailObject(Email $email): array
  168.     {
  169.         return $this->createQueryBuilder('c')
  170.             ->join('c.attributedSentEmails''ase')
  171.             ->join('ase.sentEmail''se')
  172.             ->join('se.email''e')
  173.             ->where('e.id = :emailId')
  174.             ->setParameter('emailId'$email->getId())
  175.             ->getQuery()
  176.             ->getResult();
  177.     }
  178.     /**
  179.      *  Update les catogry pour les customer
  180.      * @throws \Doctrine\DBAL\Exception|Exception
  181.      */
  182.     public function updateCategoryCustomer()
  183.     {
  184.         $conn $this->getEntityManager()
  185.             ->getConnection();
  186.         $sql "
  187.         UPDATE contact c 
  188.         LEFT JOIN booking_participant bp on bp.contact_id = c.id
  189.         RIGHT JOIN booking b on b.first_contact_id = bp.id 
  190.         SET c.category='customer' 
  191.         WHERE bp.contact_id is not null;
  192.             ";
  193.         $stmt $conn->prepare($sql);
  194.         return $stmt->executeStatement();
  195.     }
  196.     /**
  197.      * Update les
  198.      * @throws \Doctrine\DBAL\Exception|Exception
  199.      */
  200.     public function updateCategoryPassenger():int
  201.     {
  202.         $conn $this->getEntityManager()
  203.         ->getConnection();
  204.         $sql "
  205.         UPDATE contact c SET c.category='passenger' WHERE c.id IN 
  206.         (   
  207.             SELECT c.id FROM contact c 
  208.             LEFT JOIN booking_participant bp on bp.contact_id = c.id 
  209.             WHERE c.category= 'prospect' 
  210.             AND bp.contact_id is not null 
  211.             group BY c.id 
  212.         );
  213.             ";
  214.         $stmt $conn->prepare($sql);
  215.         return $stmt->executeStatement();
  216.     }
  217.     public function tryToFind(string $emailstring $firstNamestring $lastName): array
  218.     {
  219.         $qb $this->createQueryBuilder('c');
  220.         return $qb
  221.             ->where($qb->expr()->orX(
  222.                 $qb->expr()->like('c.professionalEmail'':email'),
  223.                 $qb->expr()->like('c.personalEmail'':email')
  224.             ))
  225.             ->andWhere($qb->expr()->like('c.firstName'':firstName'))
  226.             ->andWhere($qb->expr()->like('c.lastName'':lastName'))
  227.             ->andWhere($qb->expr()->eq('c.active'true))
  228.             ->setParameter('email'$email.'%')
  229.             ->setParameter('firstName'$firstName.'%')
  230.             ->setParameter('lastName'$lastName.'%')
  231.             ->getQuery()
  232.             ->getResult();
  233.     }
  234. }