<?php
/**
 * _             _       _
 * | |____      _| | __ _| |__
 * | '_ \ \ /\ / / |/ _` | '_ \
 * | |_) \ V  V /| | (_| | |_) |
 * |_.__/ \_/\_/ |_|\__,_|_.__/.
 *
 * @author    Bwlab di Luigi Massa <info@bwlab.it>
 * @copyright since 2014 Bwlab
 * @license   proprietary
 */

declare(strict_types=1);

namespace Bwlab\Withdrawal\Repository;

if (!defined('_PS_VERSION_')) {
    exit;
}

use Bwlab\Withdrawal\Entity\WithdrawalRequest;
use Doctrine\ORM\EntityRepository;

class WithdrawalRequestRepository extends EntityRepository
{
    public function findOneByOrderAndCustomer(int $orderId, int $customerId): ?WithdrawalRequest
    {
        return $this->createQueryBuilder('wr')
            ->where('wr.idOrder = :orderId')
            ->andWhere('wr.idCustomer = :customerId')
            ->setParameter('orderId', $orderId)
            ->setParameter('customerId', $customerId)
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();
    }

    /** @return WithdrawalRequest[] */
    public function findByCustomer(int $customerId): array
    {
        return $this->createQueryBuilder('wr')
            ->where('wr.idCustomer = :customerId')
            ->setParameter('customerId', $customerId)
            ->orderBy('wr.dateAdd', 'DESC')
            ->getQuery()
            ->getResult();
    }
}
