src/Repository/__Accounting/SupplierRepository.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Repository\__Accounting;
  3. use App\Entity\__Accounting\Supplier;
  4. use App\Entity\Cruise\Company\Company;
  5. use App\Entity\ParameterCodes;
  6. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  7. use Doctrine\ORM\NonUniqueResultException;
  8. use Doctrine\ORM\QueryBuilder;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. /**
  11.  * @method Supplier|null find($id, $lockMode = null, $lockVersion = null)
  12.  * @method Supplier|null findOneBy(array $criteria, array $orderBy = null)
  13.  * @method Supplier[]    findAll()
  14.  * @method Supplier[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15.  */
  16. class SupplierRepository extends ServiceEntityRepository
  17. {
  18.     public const CROISILAND_SUPPLIER 'Croisiland';
  19.     public const ASSUREVER_SUPPLIER 'Assurever';
  20.     public const MAPFRE_SUPPLIER 'Mapfre';
  21.     public const MIGRATION_SUPPLIER 'Migration';
  22.     public const COSTA_CODE_SUPPLIER "COSTA";
  23.     public function __construct(ManagerRegistry $registry)
  24.     {
  25.         parent::__construct($registrySupplier::class);
  26.     }
  27.     public function getFormQueryBuilder(): QueryBuilder
  28.     {
  29.         return $this->createQueryBuilder('s')
  30.             ->orderBy('s.name');
  31.     }
  32.     /**
  33.      * @throws NonUniqueResultException
  34.      */
  35.     public function getCompanySupplier(Company $company): ?Supplier
  36.     {
  37.         return $this->createQueryBuilder('s')
  38.             ->andWhere('s.company = :company')
  39.             ->setParameters(['company' => $company])
  40.             ->getQuery()
  41.             ->getOneOrNullResult();
  42.     }
  43.     /**
  44.      * @return array|Supplier[]
  45.      */
  46.     public function getSuppliers(): array
  47.     {
  48.         $qb $this->createQueryBuilder('s');
  49.         return $qb
  50.             ->andWhere($qb->expr()->eq('s.active'true))
  51.             ->orderBy('s.name')
  52.             ->getQuery()
  53.             ->getResult();
  54.     }
  55.     /**
  56.      * used in offer/discount type
  57.      *
  58.      * @return array|Supplier[]
  59.      */
  60.     public function getCompaniesSuppliers(): array
  61.     {
  62.         $qb $this->createQueryBuilder('s');
  63.         return $qb
  64.             ->andWhere($qb->expr()->isNotNull('s.company'))
  65.             ->andWhere($qb->expr()->eq('s.active'true))
  66.             ->orderBy('s.name')
  67.             ->getQuery()
  68.             ->getResult();
  69.     }
  70.     /**
  71.      * used in offer/discount grid
  72.      *
  73.      * @return QueryBuilder
  74.      */
  75.     public function getCompaniesSuppliersQB(): QueryBuilder
  76.     {
  77.         $qb $this->createQueryBuilder('s');
  78.         return $qb
  79.             ->andWhere($qb->expr()->isNotNull('s.company'))
  80.             ->andWhere($qb->expr()->eq('s.active'true))
  81.             ->orderBy('s.name');
  82.     }
  83.     /**
  84.      * @throws NonUniqueResultException
  85.      */
  86.     public function getCroisilandSupplier(): ?Supplier
  87.     {
  88.         return $this->createQueryBuilder('s')
  89.             ->andWhere('s.name = :name')
  90.             ->setParameters(['name' => self::CROISILAND_SUPPLIER])
  91.             ->getQuery()
  92.             ->getOneOrNullResult();
  93.     }
  94.     /**
  95.      * @return array|Supplier[]
  96.      */
  97.     public function getSupplierWithSameName(string $nameSupplier $supplier): array
  98.     {
  99.         $qb $this->createQueryBuilder('s');
  100.         return $this->createQueryBuilder('s')
  101.             ->where($qb->expr()->eq('s.name'':name'))
  102.             ->andWhere($qb->expr()->neq('s.id'$supplier->getId()))
  103.             ->setParameter('name'$name)
  104.             ->getQuery()
  105.             ->getResult();
  106.     }
  107.     /**
  108.      * @return array|Supplier[]
  109.      */
  110.     public function getSupplierWithSameCode(string $codeSupplier $supplier): array
  111.     {
  112.         $qb $this->createQueryBuilder('s');
  113.         return $qb
  114.             ->where($qb->expr()->eq('s.code'':code'))
  115.             ->andWhere($qb->expr()->neq('s.id'$supplier->getId()))
  116.             ->setParameter('code'$code)
  117.             ->getQuery()
  118.             ->getResult();
  119.     }
  120.     /****************** Migration *************************************************************************************/
  121.     /**
  122.      * @throws NonUniqueResultException
  123.      */
  124.     public function getMigrationSupplier(): ?Supplier
  125.     {
  126.         return $this->createQueryBuilder('s')
  127.             ->andWhere('s.name = :name')
  128.             ->setParameters(['name' => self::MIGRATION_SUPPLIER])
  129.             ->getQuery()
  130.             ->getOneOrNullResult();
  131.     }
  132.     /****************** Insurance suppliers ***************************************************************************/
  133.     /**
  134.      * @return array|Supplier[]
  135.      */
  136.     public function getInsuranceSuppliers(?bool $active true): array
  137.     {
  138.         $qb $this->createQueryBuilder('s')
  139.             ->innerJoin('s.type''t');
  140.         $qb->andWhere($qb->expr()->in('t.code'':typeCode'))->setParameter('typeCode', [ParameterCodes::SUPPLIER_TYPE_INSURANCEParameterCodes::SUPPLIER_TYPE_COMPANY]);
  141.         if ($active !== null) {
  142.             $qb->andWhere($qb->expr()->in('s.active'':active'))->setParameter('active'$active);
  143.         }
  144.         return $qb->orderBy("s.name""ASC")
  145.             ->getQuery()
  146.             ->getResult();
  147.     }
  148.     /**
  149.      * @throws NonUniqueResultException
  150.      */
  151.     public function getAssureverSupplier(): ?Supplier
  152.     {
  153.         return $this->createQueryBuilder('s')
  154.             ->andWhere('s.name = :name')
  155.             ->setParameters(['name' => self::ASSUREVER_SUPPLIER])
  156.             ->getQuery()
  157.             ->getOneOrNullResult();
  158.     }
  159.     /**
  160.      * @throws NonUniqueResultException
  161.      */
  162.     public function getMapfreSupplier(): ?Supplier
  163.     {
  164.         return $this->createQueryBuilder('s')
  165.             ->andWhere('s.name = :name')
  166.             ->setParameters(['name' => self::MAPFRE_SUPPLIER])
  167.             ->getQuery()
  168.             ->getOneOrNullResult();
  169.     }
  170. }