src/Repository/SubscriptionRepository.php line 108

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Subscription;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\DBAL\Connection;
  6. use Doctrine\ORM\OptimisticLockException;
  7. use Doctrine\ORM\ORMException;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. /**
  10.  * @method Subscription|null find($id, $lockMode = null, $lockVersion = null)
  11.  * @method Subscription|null findOneBy(array $criteria, array $orderBy = null)
  12.  * @method Subscription[]    findAll()
  13.  * @method Subscription[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14.  */
  15. class SubscriptionRepository extends ServiceEntityRepository
  16. {
  17.     private $connection;
  18.     public function __construct(ManagerRegistry $registryConnection $connection)
  19.     {
  20.         parent::__construct($registrySubscription::class);
  21.         $this->connection $connection;
  22.     }
  23.     public function insert(Subscription $subscription)
  24.     {
  25.         $sql "INSERT INTO subscription (is_subscribed, end_date, month, payment_type, reference, user_want_stop, end_date_card, created_at, professional_id, mandate_status, deleted) ";
  26.         $sql .= "VALUES (:is_subscribed, :end_date, :month, :payment_type, :reference, :user_want_stop, :end_date_card, :created_at, :professional_id, :mandate_status, :deleted)";
  27.         $stmt $this->connection->prepare($sql);
  28.         $stmt->bindValue("is_subscribed"$subscription->getIsSubscribed());
  29.         $stmt->bindValue("end_date"$subscription->getEndDate()->format("Y-m-d"));
  30.         $stmt->bindValue("month"$subscription->getMonth());
  31.         $stmt->bindValue("payment_type"$subscription->getPaymentType());
  32.         $stmt->bindValue("reference"$subscription->getReference());
  33.         $stmt->bindValue("user_want_stop"$subscription->getUserWantStop() == true 0);
  34.         $stmt->bindValue("end_date_card"$subscription->getEndDateCard() == null $subscription->getEndDateCard() : $subscription->getEndDateCard()->format("Y-m-01"));
  35.         $stmt->bindValue("created_at"$subscription->getCreatedAt()->format("Y-m-d H:i:s"));
  36.         $stmt->bindValue("professional_id"$subscription->getProfessional()->getId()->toBinary());
  37.         $stmt->bindValue("mandate_status"$subscription->getMandateStatus());
  38.         $stmt->bindValue("deleted"$subscription->isDeleted());
  39.         $stmt->executeQuery();
  40.     }
  41.     public function findEndDate($date)
  42.     {
  43.         $sql "SELECT *
  44. FROM subscription
  45. WHERE user_want_stop = 0
  46. AND end_date BETWEEN :date1 AND :date2
  47. ";
  48.         $stmt $this->connection->prepare($sql);
  49.         $stmt->bindValue("date1"$date->format("Y-m-d 00:00:00"));
  50.         $stmt->bindValue("date2"$date->format("Y-m-d 23:59:59"));
  51.         $subscriptions $stmt->executeQuery();
  52.         $subscriptions $subscriptions->fetchAllAssociative();
  53.         return $subscriptions;
  54.     }
  55.     public function updateAferPayment(Subscription $subscription)
  56.     {
  57.         $sql "UPDATE subscription
  58.         SET professional_id = :professional_id,
  59.             is_subscribed = :is_subscribed,
  60.             end_date = :end_date,
  61.             month = :month
  62.         WHERE reference = :reference;
  63.         ";
  64.         $stmt $this->connection->prepare($sql);
  65.         $stmt->bindValue("professional_id"$subscription->getProfessional()->getId()->toBinary());
  66.         $stmt->bindValue("is_subscribed"$subscription->getIsSubscribed());
  67.         $stmt->bindValue("end_date"$subscription->getEndDate()->format("Y-m-d"));
  68.         $stmt->bindValue("month"$subscription->getMonth());
  69.         $stmt->bindValue("reference"$subscription->getReference());
  70.         $stmt->executeQuery();
  71.     }
  72.     /**
  73.      * @throws ORMException
  74.      * @throws OptimisticLockException
  75.      */
  76.     public function add(Subscription $entitybool $flush true): void
  77.     {
  78.         $this->_em->persist($entity);
  79.         if ($flush) {
  80.             $this->_em->flush();
  81.         }
  82.     }
  83.     /**
  84.      * @throws ORMException
  85.      * @throws OptimisticLockException
  86.      */
  87.     public function remove(Subscription $entitybool $flush true): void
  88.     {
  89.         $this->_em->remove($entity);
  90.         if ($flush) {
  91.             $this->_em->flush();
  92.         }
  93.     }
  94.     public function getSubscriptionByMonthAndDay($todayDate$isTakenEndMonth false$dayNumber)
  95.     {
  96.         $sql "SELECT *
  97. FROM subscription
  98. WHERE is_subscribed = 1
  99. AND deleted = 0          
  100. AND DAY(created_at) = :day1
  101. ";
  102.         if ($isTakenEndMonth) {
  103.             $sql .= "
  104. OR DAY(created_at) = :day2
  105.             ";
  106.         }
  107.         $stmt $this->connection->prepare($sql);
  108.         $stmt->bindValue("day1"$todayDate->format("d"));
  109.         if ($isTakenEndMonth) {
  110.             $stmt->bindValue("day2"$dayNumber);
  111.         }
  112.         $subscriptions $stmt->executeQuery();
  113.         $subscriptions $subscriptions->fetchAllAssociative();
  114.         return $subscriptions;
  115.     }
  116. }