<?php

namespace AppBundle\Entity\Product\Repository;
use AppBundle\Entity\Organization\Organization;
use AppBundle\Entity\Person\Person;
use AppBundle\Entity\Product\Equipment;
use AppBundle\Entity\Product\EquipmentList;
use Doctrine\ORM\EntityRepository;

/**
 * Description of EquipmentRepository
 *
 */
class EquipmentRepository extends EntityRepository
{

    public function findDuplicateProduct(Equipment $equipment, Organization $organization)
    {
        $qb = $this->createQueryBuilder('a')
            ->innerJoin(EquipmentList::class, 'el', 'WITH', 'el.id <> 1')
            ->andWhere('a.sku = :sku')
            ->andWhere('a.name = :name')
            ->andWhere('a.organization=:organizationId')
            ->setParameter(':sku', $equipment->getSku())
            ->setParameter(':name', $equipment->getName())
            ->setParameter(':organizationId', $organization);

        return $qb->getQuery()->getResult();
    }

    public function findDuplicateMusicScore(Equipment $equipment, Organization $organization)
    {
        $qb = $this->createQueryBuilder('a');
        $qb->innerJoin(EquipmentList::class, 'el','WITH', 'el.id = 1')
            ->andWhere('a.sku = :sku')
            ->andWhere('a.name = :name')
            ->andWhere('a.organization=:organizationId')
            ->setParameter(':sku', $equipment->getSku())
            ->setParameter(':name', $equipment->getName())
            ->setParameter(':organizationId', $organization);

        return $qb->getQuery()->getResult();
    }

    public function findEquipmentWithoutAccess(Organization $organization)
    {

        $qb = $this->createQueryBuilder('o');
        $qb->where('o.organization=:org AND o.access IS NULL')
            ->setParameter(':org', $organization);

        return $qb->getQuery()->getResult();
    }

    public function findEquipmentAccessOwner(Organization $organization)
    {

        $qb = $this->createQueryBuilder('o');
        $qb->innerJoin('o.access','owner', 'WITH')
            ->innerJoin('owner.person','p', 'WITH')
            ->andWhere('p.isPhysical=:physical')
            ->andWhere('owner.organization=:org')
            ->setParameter(':physical', 0)
            ->setParameter(':org', $organization);
        $resultPersonNotPhysical = $qb->getQuery()->getResult();

        $qb = $this->createQueryBuilder('o');
        $qb = $this->createQueryBuilder('o');
            $qb->innerJoin('o.access','owner', 'WITH')
            ->innerJoin('owner.organizationFunction', 'organizationFunction')
            ->andWhere('owner.organization=:org')
            ->setParameter(':org', $organization);
        $resultPersonIsPhysical = $qb->getQuery()->getResult();

        return array_merge($resultPersonNotPhysical, $resultPersonIsPhysical);
    }

    public function findEquipmentNoAvailableForRent(Organization $organization)
    {
        $today = new \DateTime();

        $qb = $this->createQueryBuilder('o');
        $qb
            ->innerJoin('o.equipmentLoan', 'equipmentLoan')
            ->where('equipmentLoan.endDate > :today or (equipmentLoan.startDate < :today and equipmentLoan.endDate is null) ')
            ->andWhere('o.organization=:org')
            ->setParameter(':org', $organization)
            ->setParameter(':today', $today);

        return $qb->getQuery()->getResult();
    }

}

?>
