<?php
namespace App\Repository;
use App\Entity\Pricing;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* @method Pricing|null find($id, $lockMode = null, $lockVersion = null)
* @method Pricing|null findOneBy(array $criteria, array $orderBy = null)
* @method Pricing[] findAll()
* @method Pricing[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PricingRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Pricing::class);
}
public function findAllDiscountsOfCompany($company)
{
return $this->createQueryBuilder('p')
->join('p.discountProduct', 'product')
->andWhere('product.company = :company')
->andWhere('p.isADiscount = true')
->setParameter('company', $company)
->orderBy('p.endDate', 'ASC')
->getQuery()
->getResult()
;
}
public function findOnSale($sorting = null, $order = null)
{
$now = new \DateTime();
$products = $this->createQueryBuilder('p')
// ->select('size(prod.inCarts)')
->andWhere('p.isADiscount = true')
->leftJoin('p.products', 'prod')
->andWhere('p.startDate <= :now')
->andWhere('p.endDate >= :now')
->setParameter('now', $now);
if ($sorting != null) {
if ($sorting == "popularity") {
if (strtolower($order) == "asc") {
$order = "ASC";
} else {
$order = "DESC";
}
$sorting = 'size(prod.inCarts)';
}
if ($sorting == "price") {
if (strtolower($order) == "asc") {
$order = "ASC";
} else {
$order = "DESC";
}
$sorting = "p.price";
}
if ($sorting == "date") {
if (strtolower($order) == "asc") {
$order = "ASC";
} else {
$order = "DESC";
}
$sorting ="p.startDate";
}
$products->orderBy( $sorting , $order);
}
// dd($products);
return $products->getQuery()
->getResult();
}
public function findOnSaleFrontpage($maxResults = 10)
{
$now = new \DateTime();
return $this->createQueryBuilder('p')
->andWhere('p.isADiscount = true')
->andWhere('p.startDate <= :now')
->andWhere('p.endDate >= :now')
->setParameter('now', $now)
->setMaxResults($maxResults)
->orderBy('RAND()')
->getQuery()
->getResult()
;
}
}