<?php

namespace OfficeBrain\Bundle\AttributeBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * AttributeColorMasterColorMappingRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class AttributeColorMasterColorMappingRepository extends EntityRepository
{
	public function getAttributeColorMasterColorMapping($attribute_id="",$attributeValueAttributeColorId = "",$startValue=0,$attrValueReturn = '',$isFromAttributePage = 0)
	{
		$query=$this->createQueryBuilder('avm')
		      ->select('avm.id as local_id, avm.attributeColorId as pms_id');
		if (trim($attribute_id) != '') {
    	    $query->where('avm.masterColorId = :masterColorId');
    		$query->setParameter('masterColorId', $attribute_id);
		}
		if (trim($attributeValueAttributeColorId) != '') {
			$query->andWhere('avm.attributeColorId = :attributeAttributeColorId');
			$query->setParameter('attributeAttributeColorId', $attributeValueAttributeColorId);
		}
		$query->addOrderBy('avm.attributeColorId','ASC');
		
		//TODO : Need To Improve :  When from Attribute Value
		if($isFromAttributePage == 1){
			$countQuery = $query;
			$count = $countQuery->select('count(avm.id)')->getQuery()->getSingleScalarResult();
			$returnData['count'] = $count;
			
			if ($attrValueReturn == '-1') {
				$query->select('avm.id as local_id, avm.attributeColorId as pms_id')->setFirstResult( $startValue );
			} else {
				$query->select('avm.id as local_id, avm.attributeColorId as pms_id')->setFirstResult( $startValue )->setMaxResults( $attrValueReturn );
			}
			$returnData['data'] = $query->getQuery()->getResult();
		}else{
			if($attrValueReturn != ''){
				if ($attrValueReturn == '-1') {
					$query->setFirstResult( $startValue );
				} else {
					$query->setFirstResult( $startValue )->setMaxResults( $attrValueReturn );
				}
				
			}
			$returnData= $query->getQuery()->getResult();
		}
		return $returnData;
	}
	public function addAttributeColorMasterColorMapping($pms_id,$attribute_id)
	{
		$em = $this->getEntityManager();
		$attributeValueMapping = $this->findOneBy(array('attributeColorId'=>$pms_id,'masterColorId'=>$attribute_id));
		if(!(is_object($attributeValueMapping) && $attributeValueMapping instanceof AttributeColorMasterColorMapping))
		{
			$attributeValueMapping = new AttributeColorMasterColorMapping();
			$attributeValueMapping->setAttributeColorId($pms_id);
			$attributeValueMapping->setMasterColorId($attribute_id);
			$em->persist($attributeValueMapping);
			$em->flush();
		}
		return $attributeValueMapping;
	}

	public function removeAttributeColorMasterColorMappingByAttributeColorId($pms_id)
	{
		$em = $this->getEntityManager();
		$attributeColorMasterColorMappingObjArray = $this->findByAttributeColorId($pms_id);
		foreach($attributeColorMasterColorMappingObjArray as $attributeColorMasterColorMappingObj)
		{
			$em->remove($attributeColorMasterColorMappingObj);
		}
		$em->flush();
		return $attributeValueMapping;
	}
	public function checkDuplicateAttribute($pms_id)
	{
		$data = $this->findOneBy(array('attributeColorId'=>$pms_id));
		return $data;
	}
	public function getBrandMapping()
	{
		$query=$this->createQueryBuilder('bm')
		->select('bm.id as local_id, bm.attributeColorId as pms_id');
		//echo $query->getQuery()->getSQL(); die;
		return $query->getQuery()->getResult();
	}
	public function removeAttributeColorMasterColorMapping($pms_id,$attribute_id)
	{
		$objIMs = $this->createQueryBuilder('avm');
		$objIMs->delete('OfficeBrainAttributeBundle:AttributeColorMasterColorMapping', 'avm')
		->where($objIMs->expr()->eq('avm.attributeColorId', ':objAttributeColorId'))
		->setParameter('objAttributeColorId', $pms_id)
		->andWhere($objIMs->expr()->eq('avm.masterColorId', ':objMasterColorId'))
		->setParameter('objMasterColorId', $attribute_id)
		->getQuery() //-> getSQL();exit;
		->execute();
		return $objIMs;
	}
}
