<?php

namespace OfficeBrain\Bundle\CoreBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * CountryRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CountryRepository extends EntityRepository
{
	
	public function getQueryBudler()
    {
        return $this->getEntityManager()->getRepository('OfficeBrainCoreBundle:Country')->createQueryBuilder('co');
    }
	
    public function getCountry($countryConfiguration)
    {
        $queryCommand=$this->createQueryBuilder('cmp');
        $queryCommand->select('cmp.id', 'cmp.countryName','cmp.countryCode');
        $queryCommand->where($queryCommand->expr()->in('cmp.countryCode', $countryConfiguration));
        $queryCommand->orderBy('cmp.countryName',' ASC');
        $countryData=$queryCommand->getQuery()->getArrayResult();
        return $countryData;
    }


    public function getCountryNameById($countryId)
    {
        $queryCommand=$this->createQueryBuilder('cm');
        $queryCommand->select('cm.countryName')
                    ->where('cm.id =:countryId')
                    ->setParameter('countryId', $countryId);
        $countryData=$queryCommand->getQuery()->getArrayResult();
        return $countryData;
    }
	
    public function getCountryCodeById($countryId)
    {
    	$queryCommand=$this->createQueryBuilder('cm');
    	$queryCommand->select('cm.countryCode')
    	->where('cm.id =:countryId')
    	->setParameter('countryId', $countryId);
    	$countryData=$queryCommand->getQuery()->getArrayResult();
    	return $countryData;
    }
    
	/**
     * to get currency code list
     * @param null
     * @return \OfficeBrain\Bundle\CoreBundleBundle\Entity\country
     */
     
	public function getCurrencyCodeList()
	{
		$queryBuilder = $this->getQueryBudler();
        $queryBuilder->select('co.currencyCode');
        $queryBuilder->where('co.currencyCode IS NOT NULL')
					 ->distinct()
					 ->orderBy('co.currencyCode');

        return $queryBuilder->getQuery()->getResult();
	}
	
	public function getCountryIdFromCountryCode($country_code){
		$country_info = $this->findOneBy(array('countryCode'=>$country_code));
		if($country_info){
			return $country_info->getId();
		}
		else {
			return false;
		}
	}
}
