<?php

namespace OfficeBrain\Bundle\ProductBundle\Entity;

use Doctrine\ORM\EntityRepository;
/**
 * CountryProductRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CountryProductRepository extends \Doctrine\ORM\EntityRepository{
	
	/**
	 * Emp Id : 5190
	 * Description : This Function get country list for product added which is Deleted
	 *
	 * @param unknown $productId
	 */
	public function getProductCountryForAdd($productId){
		 
		$em = $this->getEntityManager();
		$em->getFilters()->disable('softdeleteable');
		 
		$queryBuilder = $em->createQueryBuilder();
		$query = $queryBuilder->select('c.id,c.countryCode as code,cp.status as active')
		->from('OfficeBrainProductBundle:CountryProduct','cp')
		->innerjoin('OfficeBrainCoreBundle:Country','c')
		->andWhere('c.id = cp.countryId')
		->andWhere(' cp.productId = :bproductId')
		//     	->andWhere('cp.deletedAt IS NOT NULL')
		->setParameter('bproductId',$productId);
	
		$data = $query->getQuery()->getArrayResult();
		$em->getFilters()->enable('softdeleteable');
		return $data;
	}
	
	
	/**
	 * Emp Id : 5190
	 * Description : For Revert Deleted Product of Set Deleted at To Null
	 * @param unknown $productId
	 * @param unknown $countryId
	 */
	public function setDeletedAtNull($productId,$countryId){
		$em = $this->getEntityManager();
		$em->getFilters()->disable('softdeleteable');
		$qb = $this->createQueryBuilder('cp');
		$qb->update()
		->set('cp.deletedAt', 'NULL')
		->where('cp.productId = :cpProductId')
		->setParameter('cpProductId', $productId)
		->andWhere('cp.countryId = :cpCountryId')
		->setParameter('cpCountryId', $countryId)
		->getQuery()
		->execute();
		$em->getFilters()->enable('softdeleteable');
	}
	
	/**
	 * Emp Id : 5190
	 * @param integer $productId
	 * @param integer $countryId
	 * @param integer $status
	 *
	 * 
	 */
	public function pMEdit($productId, $countryId, $status, $currencyId) {
		$countryProductObj = $this->findOneBy(array('productId' => $productId, 'countryId' => $countryId));
	
		$countryProductObj = ($countryProductObj) ? $countryProductObj : new CountryProduct();
	
		//SMART PATCH: For edit time not update status
		$status = ($countryProductObj->getStatus()!="") ? $countryProductObj->getStatus() : $status;
		 
		$countryProductObj->setProductId($productId);
		$countryProductObj->setCountryId($countryId);
		$countryProductObj->setStatus($status);
		$countryProductObj->setCurrency($currencyId);
	
		$this->_em->persist($countryProductObj);
		$this->_em->flush();
	
		return $countryProductObj;
	}
	
	public function delete($productId,$countryId){
    	$em = $this->getEntityManager();
    	$countryProductObj = $this->findOneBy(array('productId' => $productId,'countryId' => $countryId));
    	if(is_object($countryProductObj)){
    		$em->remove($countryProductObj);
    	}
    	$em->flush();
    	return $countryProductObj;
    }
	
	public function findProductById($productId, $countryId){
		$em = $this->getEntityManager();
		$em->getFilters()->disable('softdeleteable');
	
		$queryBuilder = $em->createQueryBuilder();
		$queryBuilder->select('cp')
		->from('OfficeBrainProductBundle:CountryProduct','cp')
		->where('cp.productId=:productId')
		->setParameter('productId',$productId)
		->andWhere('cp.countryId = :country')
		->setParameter('country', $countryId);
	
		$countryProductObject = $queryBuilder->getQuery()->getResult();
	
		$em->getFilters()->enable('softdeleteable');
		return $countryProductObject;
		 
	}
	
	/**
	 * Emp Id : 5190
	 * Description : This Function get country list for product added
	 *
	 * @param unknown $productId
	 * @param unknown $countryId
	 * @param unknown $setStatus
	 */
	public function getProductCountry($productId){
		$queryBuilder = $this->getEntityManager()->createQueryBuilder();
		$query = $queryBuilder->select('c.id, c.countryCode as code,cp.status as active')
		->from('OfficeBrainCoreBundle:Country','c')
		->innerjoin('OfficeBrainProductBundle:CountryProduct','cp','with','c.id=cp.countryId')
		->Where(' cp.productId = :productId')
		->setParameter('productId',$productId);
		
		return $query->getQuery()->getArrayResult();
	}
	public function addCountryDataFromSolr($productMasterId,$countryId,$currencyId){
		$countryProductObj = new CountryProduct();
		$countryProductObj->setProductId($productMasterId);
		$countryProductObj->setCountryId($countryId);
		$countryProductObj->setCurrency($currencyId);
		$countryProductObj->setStatus(true);
		$this->_em->persist($countryProductObj);
		$this->_em->flush();
	
		return $countryProductObj;
	}
	
	/**
	 * Emp Id : 5190
	 * Description : This Function Set Status According To Status Field
	 *
	 * @param unknown $productId
	 * @param unknown $countryId
	 * @param unknown $setStatus
	 */
	public function changeProductStatus($productId,$countryId,$setStatus){
		$query = $this->createQueryBuilder('cp')->update()
		->set('cp.status', $setStatus)
		->where('cp.countryId = :countryId')
		->andWhere("cp.productId IN (:productId)")
		->setParameter('countryId', $countryId)
		->setParameter('productId', array_values($productId))
		->getQuery();
	
		$countryProductObj = $query->execute();
		$this->_em->flush();
	
		$query = $this->createQueryBuilder('c')
		->select('c.id, c.status as active, c.countryId')
		->where('c.countryId = :countryId')
		->andWhere("c.productId IN (:productId)")
		->setParameter('countryId', $countryId)
		->setParameter('productId', array_values($productId));
	
		return $query->getQuery()->getArrayResult();
	}
}
