<?php
namespace App\Repository;
use App\Entity\Category;
use App\Entity\CategoryMaster;
use App\Entity\Company;
use App\Entity\Product;
use App\Entity\SubCategory;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @method Product|null find($id, $lockMode = null, $lockVersion = null)
* @method Product|null findOneBy(array $criteria, array $orderBy = null)
* @method Product[] findAll()
* @method Product[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ProductRepository extends ServiceEntityRepository
{
/**
* @var SubCategoryRepository
*/
private $subCategoryRepository;
public function __construct(ManagerRegistry $registry, SubCategoryRepository $subCategoryRepository)
{
parent::__construct($registry, Product::class);
$this->subCategoryRepository = $subCategoryRepository;
}
public function findAllProducts()
{
return $this->createQueryBuilder('p')
->leftJoin('p.departments', 'departments')
->leftJoin('p.company', 'company')
->join('p.subCategories', 'subCategories')
->addSelect('departments')
->where('company.isConfirmed = 1')
->andWhere('company.isValidDocument = 1')
->andWhere('p.isActivated = 1')
->andWhere('subCategories.isService is NULL or subCategories.isService = 0')
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function findAllServices()
{
return $this->createQueryBuilder('p')
->leftJoin('p.departments', 'departments')
->leftJoin('p.company', 'company')
->join('p.subCategories', 'subCategories')
->addSelect('departments')
->where('company.isConfirmed = 1')
->andWhere('company.isValidDocument = 1')
->andWhere('p.isActivated = 1')
->andWhere('p.isDesactivatedByAdmin = 0')
->andWhere('subCategories.isService = 1')
// ->leftJoin('p.avis', 'avis')
// ->addselect('avis')
// ->orderBy('avis.globalNote', 'DESC')
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function searchProduct(array $parameters)
{
$qb = $this
->createQueryBuilder('p')
->leftJoin('p.subCategories', 'subCategory')
->leftJoin('p.productAgendas', 'productAgendas')
->where('p.isActivated = 1')
->leftJoin('p.company', 'company')
->andWhere('company.isConfirmed = 1')
->andWhere('company.isValidDocument = 1')
->andWhere('p.isDesactivatedByAdmin = 0');
if ($parameters['department']) {
$qb
->leftJoin('p.departments', 'departments')
->addSelect('departments')
->andWhere('departments = :department OR departments IS NULL')
->setParameter('department', $parameters['department']);
}
if ($parameters['city']) {
$qb
->andWhere('p.cityLocalisation = :city')
->setParameter('city', $parameters['city']);
}
if ($parameters['min_price']) {
$qb
->andWhere('p.price > :price')
->setParameter('price', $parameters['min_price']);
}
if ($parameters['category']) {
$qb
->andWhere($qb->expr()->in('subCategory.id', $parameters['category']));
} /*elseif ($parameters['categoryMaster']) {
$subCategories = $this->subCategoryRepository->getSubCategoriesByCategory($parameters['categoryMaster']);
$arrayId = [];
foreach ($subCategories as $subCategory) {
$arrayId[] = $subCategory['id'];
}
$qb
->andWhere('subCategory.id IN (:idMaster)')
->setParameter('idMaster', $arrayId);
}*/
if ($parameters['type']) {
if ($parameters['type'] === 'service') {
$qb
->andWhere('subCategory.isService = 1');
} else {
$qb
->andWhere('subCategory.isService = 0');
}
}
if ($parameters['nbGuest']) {
$qb
->andWhere('p.nbGuest > :nbGuest')
->orWhere('p.nbGuest IS NULL')
->setParameter('nbGuest', $parameters['nbGuest']);
}
if ($parameters['max_price']) {
$qb
->andWhere('p.price < :maxprice')
->setParameter('maxprice', $parameters['max_price']);
}
if ($parameters['keyword']) {
$qb
->andWhere('p.name LIKE :name')
->setParameter('name', '%'.$parameters['keyword'].'%');
}
if ($parameters['weddingdate_start'] && $parameters['weddingdate_end']) {
$weddingDateStart = \DateTime::createFromFormat('d/m/Y', $parameters['weddingdate_start'])->format('Y-m-d');
$weddingDateEnd = \DateTime::createFromFormat('d/m/Y', $parameters['weddingdate_end'])->format('Y-m-d');
$qb
->andWhere('(productAgendas.startDayNotAvailable IS NULL OR :weddingdate_end < productAgendas.startDayNotAvailable OR :weddingdate_start > productAgendas.endDayNotAvailable)')
->setParameter('weddingdate_start', $weddingDateStart)
->setParameter('weddingdate_end', $weddingDateEnd);
}
if ($parameters['weddingdate']) {
$weddingdate = \DateTime::createFromFormat('d/m/Y', $parameters['weddingdate'])->format('Y-m-d');
$qb
->andWhere('(productAgendas.startDayNotAvailable IS NULL OR :weddingdate < productAgendas.startDayNotAvailable OR :weddingdate > productAgendas.endDayNotAvailable)')
->setParameter('weddingdate', $weddingdate);
}
/*
if ($parameters['category']) {
if ($parameters['category'] !== 'Service') {
$qb
->andWhere('subCategory IN (:category)')
->setParameter('category', $parameters['category']);
}
}*/
return $qb->orderBy('p.id', 'DESC')->getQuery()->getResult();
}
public function searchProductCompany(array $parameters, Company $company)
{
$qb = $this
->createQueryBuilder('p')
->leftJoin('p.company', 'company')
->where('company = :company')
->setParameter('company', $company);
if ($parameters['minQuantity']) {
$qb
->andWhere('p.quantity > :quantitymin')
->setParameter('quantitymin', $parameters['minQuantity']);
}
if ($parameters['maxQuantity']) {
$qb
->andWhere('p.quantity < :quantitymax')
->setParameter('quantitymax', $parameters['maxQuantity']);
}
if ($parameters['minPrice']) {
$qb
->andWhere('p.price > :minPrice')
->setParameter('minPrice', $parameters['minPrice']);
}
if ($parameters['maxPrice']) {
$qb
->andWhere('p.price < :maxPrice')
->setParameter('maxPrice', $parameters['maxPrice']);
}
if ($parameters['keyword']) {
$qb
->andWhere('p.name LIKE :name')
->setParameter('name', '%'.$parameters['keyword'].'%');
}
return $qb->getQuery()->getResult();
}
public function productsByCategory(Category $category)
{
return $this->createQueryBuilder('p')
->leftJoin('p.subCategories', 'subCategory')
->addSelect('subCategory')
->leftJoin('subCategory.categories', 'category')
->addSelect('category')
->where('category = :category')
->andWhere('p.isActivated = 1')
->setParameter('category', $category)
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function productsByCategoryMaster(SubCategory $subCategory)
{
return $this->createQueryBuilder('p')
->leftJoin('p.subCategories', 'subCategory')
->addSelect('subCategory')
->leftJoin('subCategory.categories', 'category')
->addSelect('category')
->leftJoin('p.company', 'company')
->addSelect('company')
->where('subCategory = :subCategory')
->andWhere('company.isConfirmed = 1')
->andWhere('company.isValidDocument = 1')
->andWhere('p.isActivated = 1')
->setParameter('subCategory', $subCategory)
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function productsByCategoryEvent(Category $category)
{
return $this->createQueryBuilder('p')
->leftJoin('p.subCategory', 'subCategory')
->addSelect('subCategory')
->leftJoin('subCategory.category', 'category')
->addSelect('category')
->where('category = :category')
->andWhere('p.isActivated = 1')
->setParameter('category', $category)
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function productsBySubCategory(SubCategory $subCategory)
{
return $this->createQueryBuilder('p')
->leftJoin('p.subCategories', 'subCategory')
->addSelect('subCategory')
->leftJoin('p.company', 'company')
->addSelect('company')
->where('subCategory = :subCategory')
->setParameter('subCategory', $subCategory)
->andWhere('company.isConfirmed = 1')
->andWhere('company.isValidDocument = 1')
->andWhere('p.isActivated = 1')
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function getBySubcategories(SubCategory $subCategory)
{
return $this->createQueryBuilder('p')
->leftJoin('p.subCategories', 'subCategory')
->where(':subCategory = subCategory')
->setParameter('subCategory', $subCategory)
->getQuery()
->getResult();
}
public function getProduct(int $id)
{
return $this->createQueryBuilder('p')
->leftJoin('p.departments', 'departments')
->addSelect('departments')
->leftJoin('p.photos', 'photos')
->addSelect('photos')
->where('p.id = :id')
->setParameter('id', $id)
->getQuery()
->getOneOrNullResult();
}
public function getOtherProductsCompany(Product $product)
{
return $this->createQueryBuilder('p')
->leftJoin('p.photos', 'photos')
->leftJoin('p.avis', 'avis')
->leftJoin('p.departments', 'departments')
->addSelect('photos')
->addSelect('avis')
->addSelect('departments')
->where('p.id != :id')
->setParameter('id', $product->getId())
->andWhere('p.isActivated = 1')
->setMaxResults(3)
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function getOtherProductsByCompany(Company $company)
{
return $this->createQueryBuilder('p')
->where('p.company = :company')
->setParameter('company', $company)
->leftJoin('p.company', 'company')
->addSelect('company')
->andWhere('company.isConfirmed = 1')
->andWhere('company.isValidDocument = 1')
->andWhere('p.isActivated = :true')
->setParameter('true', 1)
->setMaxResults(3)
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function getPhotoProductsByCompany(Company $company)
{
return $this->createQueryBuilder('p')
->where('p.company = :company')
->setParameter('company', $company)
->leftJoin('p.company', 'company')
->addSelect('company')
->andWhere('company.isConfirmed = 1')
->andWhere('company.isValidDocument = 1')
->andWhere('p.isActivated = :true')
->setParameter('true', 1)
->setMaxResults(1)
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function getAllProductsByCompany(Company $company)
{
return $this->createQueryBuilder('p')
->where('p.company = :company')
->setParameter('company', $company)
->leftJoin('p.company', 'company')
->addSelect('company')
->andWhere('company.isConfirmed = 1')
->andWhere('company.isValidDocument = 1')
->andWhere('p.isActivated = :true')
->setParameter('true', 1)
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function getOtherProductsCompanyNotSingle(Company $company, Product $product)
{
return $this->createQueryBuilder('p')
->where('p.company = :company')
->setParameter('company', $company)
->andWhere('p.isActivated = :true')
->setParameter('true', 1)
->andWhere('p.id != :idProduct')
->setParameter('idProduct', $product->getId())
->setMaxResults(3)
->orderBy('p.id', 'DESC')
->getQuery()
->getResult();
}
public function findProductByNote()
{
return $this->createQueryBuilder('p')
->leftJoin('p.avis', 'avis')
->addSelect('SUM(avis.globalNote) as countNote')
->orderBy('countNote', 'DESC')
->groupBy('p')
->setMaxResults(4)
->getQuery()
->getResult();
}
public function orderByNotes()
{
return $this->createQueryBuilder('p')
->leftJoin('p.avis', 'avis')
->where('p.isActivated = 1')
->orderBy('avis.globalNote', 'DESC')
->getQuery()
->getResult();
}
public function orderByCommands()
{
return $this->createQueryBuilder('p')
->leftJoin('p.commandProducts', 'commandProducts')
->where('p.isActivated = 1')
->orderBy('commandProducts.quantity', 'DESC')
->setMaxResults(4)
->getQuery()
->getResult();
}
public function findByCritere($critere=null){
$qb = $this->createQueryBuilder('p');
$qb->innerjoin('App\Entity\OptionHebergement', 'o', 'WITH', 'o.products = p.id');
if($critere != null && isset($critere["nbrParticipant"]) && $critere["nbrParticipant"] != '' ){
$qb->andWhere('o.numberOfParticipants = :nbrParticipant');
$qb->setParameter('nbrParticipant', $critere["nbrParticipant"]);
}
if($critere != null && isset($critere["nbrAdulte"]) && $critere["nbrAdulte"] != '' ){
$qb->andWhere('o.numberOfAdults = :nbrAdulte');
$qb->setParameter('nbrAdulte', $critere["nbrAdulte"]);
}
if($critere != null && isset($critere["nbrEnfant"]) && $critere["nbrEnfant"] != '' ){
$qb->andWhere('o.numberOfChild = :nbrEnfant');
$qb->setParameter('nbrEnfant', $critere["nbrEnfant"]);
}
if($critere != null && isset($critere["nbrLits"]) && $critere["nbrLits"] != '' ){
$qb->andWhere('o.bedNumber = :nbrLits');
$qb->setParameter('nbrLits', $critere["nbrLits"]);
}
if($critere != null && isset($critere["nbrBebe"]) && $critere["nbrBebe"] != '' ){
$qb->andWhere('o.numberOfBaby = :nbrBebe');
$qb->setParameter('nbrEnfant', $critere["nbrBebe"]);
}
$result = $qb->getQuery()->getResult();
return $result;
// $qb->leftJoin('App\Entity\Caracteristic', 'c');
// $qb->leftJoin('o.caracteristics', 'c');
// $qb->addSelect('c');
// $qb->addIsPublishedQueryBuilder('App\Entity\Caracteristic', 'c', 'WITH', 'o.products = p.id');
// if($critere != null && isset($critere["caracteristique"]) && $critere["caracteristique"] != null ){
// $caracteristic = '';
// foreach ($critere["caracteristique"] as $key => $value) {
// # code...
// $caracteristic .= $key == 0 ? 'o.caracteristics = :caracteristique'.$key : ' OR o.caracteristics = :caracteristique'.$key;
// }
// $qb->andWhere($caracteristic);
// foreach ($critere["caracteristique"] as $key => $value) {
// # code...
// $qb->setParameter('caracteristique'.$key, $value);
// }
// }
}
/*
public function findOneBySomeField($value): ?Product
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
public function filtreHebergement($equipements)
{
$qb = $this
->createQueryBuilder('p')
->leftJoin('p.subCategories', 'subCategory')
->leftJoin('p.equipementHebergs', 'equipementHebergs')
// ->addSelect('hebergementOptions.equipement')
// ->andWhere('p.id =:idProduct')
// ->setParameter('idProduct', 2)
;
// dd($qb);
// if ($equipements) {
// $qb
// ->andWhere($qb->expr()->in('equipementHebergs.nomEquipement', $equipements));
// }
return $qb->getQuery()->getResult();
}
public function Hebergement(array $parameters)
{
$qb = $this
->createQueryBuilder('p')
->leftJoin('p.subCategories', 'subCategory')
->addSelect('subCategory')
->leftJoin('p.chambres', 'chambres')
->where('p.isActivated = 1')
->leftJoin('p.company', 'company')
->andWhere('company.isConfirmed = 1')
->andWhere('company.isValidDocument = 1')
->leftJoin('p.equipment', 'equipment')
->leftJoin('p.caracteristics', 'caracteristics')
->leftJoin('p.typeLocalisations', 'typeLocalisations')
->leftJoin('p.securities', 'securities')
->leftJoin('p.langueHotes', 'langueHotes')
;
if ($parameters['prix'] == "plusCher") {
$qb
->orderBy('chambres.tarifJour', 'ASC');
} else{
$qb
->orderBy('chambres.tarifJour', 'DESC');
}
if (!empty($parameters['equipements'])) {
$qb
->andWhere('equipment.labels IN (:equipements)')
->setParameter('equipements', $parameters['equipements']);
}
if (!empty($parameters['caracteristics'])) {
$qb
->andWhere('caracteristics.labels IN (:caracteristics)')
->setParameter('caracteristics', $parameters['caracteristics']);
}
if (!empty($parameters['typeLocalisations'])) {
$qb
->andWhere('typeLocalisations.slugTypeLocal IN (:typeLocalisation)')
->setParameter('typeLocalisation', $parameters['typeLocalisations']);
}
if (!empty($parameters['securities'])) {
$qb
->andWhere('securities.slugSecurity IN (:securitie)')
->setParameter('securitie', $parameters['securities']);
}
if (!empty($parameters['langueHotes'])) {
$qb
->andWhere('langueHotes.labels IN (:langueHotes)')
->setParameter('langueHotes', $parameters['langueHotes']);
}
return $qb->getQuery()->getResult();
}
public function findprestation(float $lat = null, float $lng = null, ?int $radius = 1000, $idprod)
{
$query = $this->findVisibleQuery();
$query ->andWhere('p.id != :id')
->andWhere('p.typeActivity IS NOT NULL')
->leftJoin('p.subCategories', 'subCategory')
->addSelect('subCategory')
->leftJoin('subCategory.categories', 'category')
->addSelect('category')
->andWhere('category IS NOT NULL')
->setParameter('id', $idprod);
$query->andWhere('(6371 * acos(cos(radians(' . $lat . ')) * cos(radians(p.latitude)) * cos(radians(p.longitude) - radians(' . $lng . ')) + sin(radians(' . $lat . ')) * sin(radians(p.latitude)))) < :distance');
$query->setParameter('distance', $radius);
return $query->getQuery()
->getResult();
}
private function findVisibleQuery()
{
return $this->createQueryBuilder('p');
}
}