<?php

namespace OfficeBrain\Bundle\QuoteBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * QuoteDistributorContactMappingRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class QuoteDistributorContactMappingRepository extends EntityRepository
{
	protected $projectSetting;
	protected $instanceId;
	protected $instanceType;
	protected $cultureId;
	protected $cultureCode;
	protected $projectSettingService;
	protected $projectUserSetting;
	
	
	public function getProjectSetting()
	{
		return $this->paginatorService;
	}
	
	/**
	 * @author Employee Id: 4493
	 * OB eCommerce Product - setProjectSetting
	 * Function created to set Project Setting
	 */
	public function setProjectSetting($projectSetting)
	{
		$this->projectSetting = $projectSetting;
		$this->prepareProjectSettingData();
	}
	
	public function setProjectUserSetting($projectUserSetting)
	{
		$this->projectUserSetting = $projectUserSetting;
		$this->prepareProjectUserSettingData();
	}
	
	public function getProjectUserSetting()
	{
		return $this->projectUserSetting;
	}
	
	public function prepareProjectSettingData()
	{
		$this->userId = $this->projectSetting['user_id'];
		$this->createdUid = $this->upatedUid = $this->userId;
		$this->instanceId = $this->projectSetting['instance_id'];
		$this->instanceType = $this->projectSetting['instance_type'];
		$this->cultureId = $this->projectSetting['culture_id'];
		$this->cultureCode = $this->projectSetting['culture_code'];
	}
	public function prepareProjectUserSettingData()
	{
		$this->user = $this->projectUserSetting['user_data'];
	}
	
	
	/**
	 * @author Employee Id: 4493
	 * OB eCommerce Product - Quote User Contact Mapping
	 * Function created for Quote User Contact Mapping
	 */
	
	public function addQuoteDistributorContact($addQuoteUserContact)
	{
		$this->getEntityManager()->persist($addQuoteUserContact);
		$this->getEntityManager()->flush();
		return $addQuoteUserContact;
	
	}
	
	/**
	 * @author Employee Id: 4493
	 * OB eCommerce Product - Quote Distributor Contact Info Exist Check
	 * Function created for Quote Distributor Contact Info
	 */
	
	public function isExistQuoteDistributorContact($quoteId,$contactId)
	{
		$query=$this->createQueryBuilder('qu');
		$query->select('count(qu.id)');		
		$query->where("qu.quoteMasterId =:quoteId")->setParameter('quoteId',$quoteId);
		$query->andWhere("qu.contactbookId =:contactId")->setParameter('contactId',$contactId);
		return $query->getQuery()->getSingleScalarResult();
	
	}
	
	/**
	 * @author Employee Id: 4493
	 * OB eCommerce Product - Quote Distributor Contact Info
	 * Function created for Quote Distributor Contact Info
	 */
	
	public function getQuoteDistributorContact($quoteId,$contactFlag='')
	{
		$query=$this->createQueryBuilder('qu');
		$query->where("qu.quoteMasterId =:quoteId")->setParameter('quoteId',$quoteId);
		if($contactFlag ==''){
			$query->andWhere("qu.contactbookId =0");
		}
		$result = $query->getQuery()->getResult();
		//echo count($result); die;
		//echo $query->getQuery()->getSql(); echo $this->instanceType; echo "=".$contactFlag; die; 
		
		if($this->instanceType == 'distributor'){
			return  $result[0];
		}
		if($this->instanceType == 'association'){
			if($this->projectUserSetting['user_data']->getUserType()=="supplier"){
				return $result;
			}
			if($this->projectUserSetting['user_data']->getUserType()=="distributor"){
				return  $result[0];
			}
		}
		if($this->instanceType == 'supplier'){
			return  $result;
		}
	}
	
	public function getQuoteDistributorContactNego($quoteId,$contactFlag='')
	{
		$query=$this->createQueryBuilder('qu');
		$query->where("qu.quoteMasterId =:quoteId")->setParameter('quoteId',$quoteId);
		if($contactFlag ==''){
			$query->andWhere("qu.contactbookId =0");
		}
		$result = $query->getQuery()->getResult();
		//echo count($result); die;
		//echo $query->getQuery()->getSql(); echo $this->instanceType; echo "=".$contactFlag; die;
	
		if($this->instanceType == 'distributor'){
			return  $result[0];
		}
		if($this->instanceType == 'association'){
	
			return  $result[0];
		}
		if($this->instanceType == 'supplier'){
			return  $result;
		}
	}
	
	/**
	 * @author Employee Id: 4493
	 * OB eCommerce Product - Quote User edit 
	 * Function created for Quote User edit
	 */
	
	public function editQuoteDistributor($quoteUserObj,$userPhoto)
	{
		$quoteUserObj->setPhoto($userPhoto);
		$this->getEntityManager()->persist($quoteUserObj);
		$this->getEntityManager()->flush();
		return $quoteUserObj;
	
	}
	public function editQuoteDistributorContact($quoteUserObj,$userPhoto)
	{
		$quoteUserObj->setPhoto($userPhoto);
		$this->getEntityManager()->persist($quoteUserObj);
		$this->getEntityManager()->flush();
		return $quoteUserObj;
	
	}
	
	/**
	 * @author Employee Id: 4203
	 * OB eCommerce Product - Quote Distributor Contact Info Exist Check
	 * Function created for Quote Distributor Contact Info
	 */
	
	public function getQuoteDistributorByQuoteMaster($quoteId)
	{
		$query=$this->createQueryBuilder('qu');
		$query->where("qu.quoteMasterId =:quoteId")->setParameter('quoteId',$quoteId);
		return $query->getQuery()->getOneOrNullResult();
	
	}
	
	/**
	 * @author Employee Id: 4493
	 * OB eCommerce Product - Quote Distributor Contact Info Exist Check
	 * Function created for Quote Distributor Contact Info
	 */
	
	public function isExistQuoteDistributorContactId($contactId)
	{
		$query=$this->createQueryBuilder('qu');
		$query->where("qu.id =:contact_id")->setParameter('contact_id',$contactId);
		return $query->getQuery()->getOneOrNullResult();
	
	}
	
	public function deleteQuoteUserContactAdmin($contactObj){
		$this->_em->remove($contactObj);
		$this->_em->flush();
		return 'SUCCESS';
		
		
	}
}
