<?php

namespace Anuncios\AnuncioBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * CampaignRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CampaignRepository extends EntityRepository
{
	public function getCampaignActive()
	{
		return $this->getEntityManager()
		->createQuery(
				'SELECT c
				FROM AnunciosAnuncioBundle:Campaign c
				WHERE c.isActive = 1 AND 
				CURRENT_DATE() BETWEEN c.dateBegin AND c.dateEnd'
		)->setMaxResults(1)
		->getOneOrNullResult();
	}
	
	public function getLastActiveCampaignClosed()
	{
		return $this->getEntityManager()
		->createQuery(
				'SELECT c
				FROM AnunciosAnuncioBundle:Campaign c
				WHERE c.isActive = 1 AND
				CURRENT_DATE() > c.dateEnd'
		)->setMaxResults(1)
		->getOneOrNullResult();
	}
	
	public function getCampaignWithMonthAndYear($month, $year)
	{
		return $this->getEntityManager()
		->createQuery(
				'SELECT c
				FROM AnunciosAnuncioBundle:Campaign c
				WHERE c.month = :month AND c.year = :year'
		)
		->setParameters(array(
				'month' => $month,
				'year'  => $year
		))
		->setMaxResults(1)
		->getOneOrNullResult();
	}
	
	public function getFinalCampaignOfYear($year)
	{
		return $this->getEntityManager()
		->createQuery(
				'SELECT c
				FROM AnunciosAnuncioBundle:Campaign c
				WHERE c.month IS NULL AND c.year = :year'
		)
		->setParameters(array(
				'year'  => $year
		))
		->setMaxResults(1)
		->getOneOrNullResult();
	}
}
