<?php
namespace App\Repository\__Sale;
use App\Entity\__Accounting\Supplier;
use App\Entity\__Sale\LegalText;
use App\Entity\ParameterCodes;
use Crea\ParameterBundle\Entity\Parameter;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method LegalText|null find($id, $lockMode = null, $lockVersion = null)
* @method LegalText|null findOneBy(array $criteria, array $orderBy = null)
* @method LegalText[] findAll()
* @method LegalText[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class LegalTextRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, LegalText::class);
}
/**
* @return array|LegalText[]
*/
public function findActives(): array
{
$qb = $this->createQueryBuilder('lt');
return $qb
->where($qb->expr()->eq('lt.active', 1))
->orderBy('lt.name', 'asc')
->getQuery()
->getResult();
}
/**
* @param string $supplierName
* @param string $categoryCode
* @param LegalText|null $legalTextToExclude
* @return LegalText|null
* @throws NonUniqueResultException
*/
public function findLegalTextBySupplierNameAndCategoryCode(string $supplierName, string $categoryCode, ?LegalText $legalTextToExclude = null): ?LegalText
{
$qb = $this->createQueryBuilder('lt');
$qb
->join('lt.category', 'ltc')
->join('lt.supplier', 'lts')
->where($qb->expr()->eq('ltc.code', ':categoryCode'))
->andWhere($qb->expr()->eq('lts.name', ':supplierName'))
->andWhere($qb->expr()->eq('lt.active', 1))
->setParameter('categoryCode', $categoryCode)
->setParameter('supplierName', $supplierName);
if ($legalTextToExclude !== null) {
$qb->andWhere($qb->expr()->neq('lt.id', $legalTextToExclude->getId()));
}
return $qb
->getQuery()
->getOneOrNullResult();
}
/**
* @param Parameter $category
* @param Supplier $supplier
* @param LegalText|null $legalTextToExclude
* @return LegalText|null
* @throws NonUniqueResultException
*/
public function findCurrentLegalTextWithSameAttributes(Parameter $category, Supplier $supplier, ?LegalText $legalTextToExclude = null): ?LegalText
{
return $this->findLegalTextBySupplierNameAndCategoryCode($supplier->getName(), $category->getCode(), $legalTextToExclude);
}
}