<?php

namespace OfficeBrain\Bundle\ProductBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * BrandMappingRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class BrandMappingRepository extends EntityRepository
{
	public function addBrandMapping($masterID)
	{
		$em = $this->getEntityManager();
		$brandMappingObj= new BrandMapping();
		$brandMappingObj->setPmsId($masterID);
		$brandMappingObj->setStatus(1);
		$em->persist($brandMappingObj);
		$em->flush();
		
		return $brandMappingObj;
		
	}
	
	public function getBrandMapping()
	{
		$query=$this->createQueryBuilder('bm')
		->select('bm.id as local_id1,bm.status as status, bm.pmsId as pms_id');
		//echo $query->getQuery()->getSQL(); die;
		return $query->getQuery()->getResult();
	}

	public function deleteBrand($pmsId){
		$obj = $this->findOneBy(array('pmsId' => $pmsId,'deletedAt' => NULL));
		$em = $this->getEntityManager();
		$em->remove($obj);
		$em->flush();
		return $obj;
	}

	public function updateBrand($brandMappingObj,$pmsId,$imageName)
	{   $em = $this->getEntityManager();
		//$brandMappingObj->setImage($imageName);
		$brandMappingObj->setPmsId($pmsId);
		$em->persist($brandMappingObj);
		$em->flush();		
		return $brandMappingObj;
		
	}

	 public function changeBrandStatus($id)
    {
    	$brandMappingObj = $this->findOneBy(array('pmsId' => $id));
       	if ($brandMappingObj) {
    		$em = $this->getEntityManager();
       		if ($brandMappingObj->getStatus() == 1) {
    			$brandMappingObj->setStatus(0);
    		} else {
    			$brandMappingObj->setStatus(1);
    		}
    		$em->persist($brandMappingObj);
    		$em->flush();
       		return true;
    	}
    
    	return false;
    }

}
