<?php
namespace App\Repository\__Accounting;
use App\Entity\__Accounting\Supplier;
use App\Entity\Cruise\Company\Company;
use App\Entity\ParameterCodes;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Supplier|null find($id, $lockMode = null, $lockVersion = null)
* @method Supplier|null findOneBy(array $criteria, array $orderBy = null)
* @method Supplier[] findAll()
* @method Supplier[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class SupplierRepository extends ServiceEntityRepository
{
public const CROISILAND_SUPPLIER = 'Croisiland';
public const ASSUREVER_SUPPLIER = 'Assurever';
public const MAPFRE_SUPPLIER = 'Mapfre';
public const MIGRATION_SUPPLIER = 'Migration';
public const COSTA_CODE_SUPPLIER = "COSTA";
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Supplier::class);
}
public function getFormQueryBuilder(): QueryBuilder
{
return $this->createQueryBuilder('s')
->orderBy('s.name');
}
/**
* @throws NonUniqueResultException
*/
public function getCompanySupplier(Company $company): ?Supplier
{
return $this->createQueryBuilder('s')
->andWhere('s.company = :company')
->setParameters(['company' => $company])
->getQuery()
->getOneOrNullResult();
}
/**
* @return array|Supplier[]
*/
public function getSuppliers(): array
{
$qb = $this->createQueryBuilder('s');
return $qb
->andWhere($qb->expr()->eq('s.active', true))
->orderBy('s.name')
->getQuery()
->getResult();
}
/**
* used in offer/discount type
*
* @return array|Supplier[]
*/
public function getCompaniesSuppliers(): array
{
$qb = $this->createQueryBuilder('s');
return $qb
->andWhere($qb->expr()->isNotNull('s.company'))
->andWhere($qb->expr()->eq('s.active', true))
->orderBy('s.name')
->getQuery()
->getResult();
}
/**
* used in offer/discount grid
*
* @return QueryBuilder
*/
public function getCompaniesSuppliersQB(): QueryBuilder
{
$qb = $this->createQueryBuilder('s');
return $qb
->andWhere($qb->expr()->isNotNull('s.company'))
->andWhere($qb->expr()->eq('s.active', true))
->orderBy('s.name');
}
/**
* @throws NonUniqueResultException
*/
public function getCroisilandSupplier(): ?Supplier
{
return $this->createQueryBuilder('s')
->andWhere('s.name = :name')
->setParameters(['name' => self::CROISILAND_SUPPLIER])
->getQuery()
->getOneOrNullResult();
}
/**
* @return array|Supplier[]
*/
public function getSupplierWithSameName(string $name, Supplier $supplier): array
{
$qb = $this->createQueryBuilder('s');
return $this->createQueryBuilder('s')
->where($qb->expr()->eq('s.name', ':name'))
->andWhere($qb->expr()->neq('s.id', $supplier->getId()))
->setParameter('name', $name)
->getQuery()
->getResult();
}
/**
* @return array|Supplier[]
*/
public function getSupplierWithSameCode(string $code, Supplier $supplier): array
{
$qb = $this->createQueryBuilder('s');
return $qb
->where($qb->expr()->eq('s.code', ':code'))
->andWhere($qb->expr()->neq('s.id', $supplier->getId()))
->setParameter('code', $code)
->getQuery()
->getResult();
}
/****************** Migration *************************************************************************************/
/**
* @throws NonUniqueResultException
*/
public function getMigrationSupplier(): ?Supplier
{
return $this->createQueryBuilder('s')
->andWhere('s.name = :name')
->setParameters(['name' => self::MIGRATION_SUPPLIER])
->getQuery()
->getOneOrNullResult();
}
/****************** Insurance suppliers ***************************************************************************/
/**
* @return array|Supplier[]
*/
public function getInsuranceSuppliers(?bool $active = true): array
{
$qb = $this->createQueryBuilder('s')
->innerJoin('s.type', 't');
$qb->andWhere($qb->expr()->in('t.code', ':typeCode'))->setParameter('typeCode', [ParameterCodes::SUPPLIER_TYPE_INSURANCE, ParameterCodes::SUPPLIER_TYPE_COMPANY]);
if ($active !== null) {
$qb->andWhere($qb->expr()->in('s.active', ':active'))->setParameter('active', $active);
}
return $qb->orderBy("s.name", "ASC")
->getQuery()
->getResult();
}
/**
* @throws NonUniqueResultException
*/
public function getAssureverSupplier(): ?Supplier
{
return $this->createQueryBuilder('s')
->andWhere('s.name = :name')
->setParameters(['name' => self::ASSUREVER_SUPPLIER])
->getQuery()
->getOneOrNullResult();
}
/**
* @throws NonUniqueResultException
*/
public function getMapfreSupplier(): ?Supplier
{
return $this->createQueryBuilder('s')
->andWhere('s.name = :name')
->setParameters(['name' => self::MAPFRE_SUPPLIER])
->getQuery()
->getOneOrNullResult();
}
}