<?php

namespace OfficeBrain\Bundle\AttributeBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * AttributeMappingRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class AttributeMappingRepository extends EntityRepository
{
	public function getAttributeMapping()
	{
		$query=$this->createQueryBuilder('am')
		->select('am.id as local_id, am.pmsId as pms_id')
		->addOrderBy('am.pmsId','ASC');
		//echo $query->getQuery()->getSQL(); die;
		return $query->getQuery()->getResult();
	}
	public function addAttributeMapping($pms_id)
	{
		$em = $this->getEntityManager();
		$attributeMapping = new AttributeMapping();
		$attributeMapping->setPmsId($pms_id);
		$em->persist($attributeMapping);
		$em->flush();
		
		return $attributeMapping;
	}
	public function addAttributeValueMapping($pms_id,$attribute_id)
	{
		$em = $this->getEntityManager();
		$attributeValueMapping = new AttributeValueMapping();
		$attributeValueMapping->setPmsId($pms_id);
		$attributeValueMapping->setAttributeId($attribute_id);
		$em->persist($attributeValueMapping);
		$em->flush();
	
		return $attributeValueMapping;
	}
	public function checkDuplicateAttribute($pms_id)
	{
		$data = $this->findOneBy(array('pmsId'=>$pms_id));
		return $data;
	}
	/**
	 * @author: Employee Id : 4808
	 * @description: This Function used to check duplciate attribute value
	 * @param unknown $pms_id
	 * @param unknown $attribute_id
	 */
	public function checkDuplicateAttributeValue($pms_id,$attribute_id){
		$em = $this->getEntityManager();
		$attributeValueMappingRepo=  $em->getRepository('OfficeBrainAttributeBundle:AttributeValueMapping');
		
		$query=$attributeValueMappingRepo->createQueryBuilder('avm')
		->select('avm.id')
		->andWhere('avm.attributeId= :attribute_id')->setParameter('attribute_id', $attribute_id)
		->andWhere('avm.pmsId= :pmsId')->setParameter('pmsId', $pms_id);
		return $query->getQuery()->getResult();	
	}
	public function getBrandMapping()
	{
		$query=$this->createQueryBuilder('bm')
		->select('bm.id as local_id, bm.pmsId as pms_id');
		//echo $query->getQuery()->getSQL(); die;
		return $query->getQuery()->getResult();
	}
}
