<?php
namespace App\Repository;
use App\Entity\Subscription;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Subscription|null find($id, $lockMode = null, $lockVersion = null)
* @method Subscription|null findOneBy(array $criteria, array $orderBy = null)
* @method Subscription[] findAll()
* @method Subscription[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class SubscriptionRepository extends ServiceEntityRepository
{
private $connection;
public function __construct(ManagerRegistry $registry, Connection $connection)
{
parent::__construct($registry, Subscription::class);
$this->connection = $connection;
}
public function insert(Subscription $subscription)
{
$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) ";
$sql .= "VALUES (:is_subscribed, :end_date, :month, :payment_type, :reference, :user_want_stop, :end_date_card, :created_at, :professional_id, :mandate_status, :deleted)";
$stmt = $this->connection->prepare($sql);
$stmt->bindValue("is_subscribed", $subscription->getIsSubscribed());
$stmt->bindValue("end_date", $subscription->getEndDate()->format("Y-m-d"));
$stmt->bindValue("month", $subscription->getMonth());
$stmt->bindValue("payment_type", $subscription->getPaymentType());
$stmt->bindValue("reference", $subscription->getReference());
$stmt->bindValue("user_want_stop", $subscription->getUserWantStop() == true ? 1 : 0);
$stmt->bindValue("end_date_card", $subscription->getEndDateCard() == null ? $subscription->getEndDateCard() : $subscription->getEndDateCard()->format("Y-m-01"));
$stmt->bindValue("created_at", $subscription->getCreatedAt()->format("Y-m-d H:i:s"));
$stmt->bindValue("professional_id", $subscription->getProfessional()->getId()->toBinary());
$stmt->bindValue("mandate_status", $subscription->getMandateStatus());
$stmt->bindValue("deleted", $subscription->isDeleted());
$stmt->executeQuery();
}
public function findEndDate($date)
{
$sql = "SELECT *
FROM subscription
WHERE user_want_stop = 0
AND end_date BETWEEN :date1 AND :date2
";
$stmt = $this->connection->prepare($sql);
$stmt->bindValue("date1", $date->format("Y-m-d 00:00:00"));
$stmt->bindValue("date2", $date->format("Y-m-d 23:59:59"));
$subscriptions = $stmt->executeQuery();
$subscriptions = $subscriptions->fetchAllAssociative();
return $subscriptions;
}
public function updateAferPayment(Subscription $subscription)
{
$sql = "UPDATE subscription
SET professional_id = :professional_id,
is_subscribed = :is_subscribed,
end_date = :end_date,
month = :month
WHERE reference = :reference;
";
$stmt = $this->connection->prepare($sql);
$stmt->bindValue("professional_id", $subscription->getProfessional()->getId()->toBinary());
$stmt->bindValue("is_subscribed", $subscription->getIsSubscribed());
$stmt->bindValue("end_date", $subscription->getEndDate()->format("Y-m-d"));
$stmt->bindValue("month", $subscription->getMonth());
$stmt->bindValue("reference", $subscription->getReference());
$stmt->executeQuery();
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(Subscription $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(Subscription $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
public function getSubscriptionByMonthAndDay($todayDate, $isTakenEndMonth = false, $dayNumber)
{
$sql = "SELECT *
FROM subscription
WHERE is_subscribed = 1
AND deleted = 0
AND DAY(created_at) = :day1
";
if ($isTakenEndMonth) {
$sql .= "
OR DAY(created_at) = :day2
";
}
$stmt = $this->connection->prepare($sql);
$stmt->bindValue("day1", $todayDate->format("d"));
if ($isTakenEndMonth) {
$stmt->bindValue("day2", $dayNumber);
}
$subscriptions = $stmt->executeQuery();
$subscriptions = $subscriptions->fetchAllAssociative();
return $subscriptions;
}
}