<?php

namespace OfficeBrain\Bundle\CoreBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * StateRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class StateRepository extends EntityRepository
{
    public function getState($countryId='')
    {
    	if ($countryId!='') {
	        $queryCommand=$this->createQueryBuilder('st');
	        $queryCommand->select('st.id', 'st.stateCode', 'st.stateName', 'st.countryCode');
	        $queryCommand->innerJoin("OfficeBrainCoreBundle:Country", "con", "WITH", "st.countryCode=con.countryCode");
	        $queryCommand->where($queryCommand->expr()->in('con.id', array($countryId)));
	        $queryCommand->orderBy('st.stateName',' ASC');
	        $stateData=$queryCommand->getQuery()->getArrayResult();
    	}else {
    		$stateData=array();
    	} 
        return $stateData;
    }
    
    public function getStateFromCountryCode($countryCode='')
    {
    	if ($countryCode!='') {
    		$queryCommand=$this->createQueryBuilder('st');
    		$queryCommand->select('st.id', 'st.stateCode', 'st.stateName', 'st.countryCode');
    		$queryCommand ->where('st.countryCode =:countryCode')
        				  ->setParameter('countryCode', array($countryCode));
    		$queryCommand->orderBy('st.stateName',' ASC');
    		$stateData=$queryCommand->getQuery()->getArrayResult();
    	}else {
    		$stateData=array();
    	}
    	return $stateData;
    }
    
    
    
    public function getLocateDistributorState($countryCode='',$countryConfiguration=array())
    {
    	
    	$queryCommand=$this->createQueryBuilder('st');
    	$queryCommand->select('st.id', 'st.stateCode', 'st.stateName', 'st.countryCode');
    	if ($countryCode!='') {
    		$queryCommand->innerJoin("OfficeBrainCoreBundle:Country", "con", "WITH", "st.countryCode=con.countryCode");
    		$queryCommand->where($queryCommand->expr()->in('con.id', array($countryCode)));
    	}else{
    		$queryCommand->innerJoin("OfficeBrainCoreBundle:Country", "con", "WITH", "st.countryCode=con.countryCode");
    		$queryCommand->where($queryCommand->expr()->in('con.countryCode', $countryConfiguration));    		
    	}
    	$queryCommand->orderBy('st.stateName',' ASC');
    	$stateData=$queryCommand->getQuery()->getArrayResult();    	
    	return $stateData;
    }

    public function getStateNameById($stateId)
    {
        $queryCommand=$this->createQueryBuilder('st');
        $queryCommand->select('st.stateName')
        ->where('st.id =:stateId')
        ->setParameter('stateId', $stateId);
        $stateData=$queryCommand->getQuery()->getArrayResult();
        return $stateData;
    }
    
    /**
     * @author Employee Id: 4846
     * OB eCommerce Product - Core
     * Function created to get state code by stateId
     */
    public function getStateCodeById($stateId)
    {
    	$queryCommand=$this->createQueryBuilder('st');
    	$queryCommand->select('st.stateCode')
    	->where('st.id =:stateId')
    	->setParameter('stateId', $stateId);
    	$stateData=$queryCommand->getQuery()->getArrayResult();
    	return $stateData;
    }
}
