<?php

namespace OfficeBrain\Bundle\QuoteBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * QuoteProductChargesRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class QuoteProductChargesRepository extends EntityRepository
{
	protected $projectSetting;
	protected $instanceId;
	protected $instanceType;
	protected $cultureId;
	protected $cultureCode;
	protected $paginatorService;
	protected $projectSettingService;
	protected $userType;
	protected $userId;
	protected $userManager;
	
	public function getPaginatorService()
	{
		return $this->paginatorService;
	}
	
	public function setPaginatorService($paginatorService)
	{
		$this->paginatorService = $paginatorService;
	}
	
	public function setUserManager($userManager)
	{
		$this->userManager = $userManager;
	}
	
	public function setCoreManager($coreManager){
		$this->coreManager = $coreManager;
	}
	
	public function getProjectSetting()
	{
		return $this->paginatorService;
	}
	
	public function setProjectSetting($projectSetting)
	{
		$this->projectSetting = $projectSetting;
		$this->prepareProjectSettingData();
	}
	
	public function setProjectUserData($userData)
	{
		$this->userObj = $userData;
	}
	
	public function prepareProjectSettingData()
	{
		$this->userId = $this->projectSetting['user_id'];
		$this->userType = $this->projectSetting['user_type'];
		$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'];
	}
	
	/**
	 * @author Employee Id: 4248
	 * OB eCommerce Product - Quotelist
	 * Function created for define a common querybuilder object
	 * @param integer $userId
	 * @return number
	 */
	public function getQueryBuilder()
	{
		return $this->getEntityManager()->getRepository('OfficeBrainQuoteBundle:QuoteProductCharges')->createQueryBuilder('qpc');
	}
	
	/**
	 * 4248
	 * To push all request quote product charges in database using TCL
	 */
	public function setQuoteProductCharges($productData,$itemLineObj){
		if(array_key_exists('charge',$productData)){
			if(count($productData['charge'])>0){
				foreach($productData['charge'] as $key=>$chargeDetail){
				$quoteProductCharges = new QuoteProductCharges();
				$quoteProductCharges->setQuoteItemLineId($itemLineObj);
				$quoteProductCharges->setChargesType($chargeDetail['charges_type']);
				$quoteProductCharges->setChargesAmount($chargeDetail['charges_amount']);
				$em = $this->getEntityManager();
				$itemLineObj->addQuoteProductCharge($quoteProductCharges);
				$em->persist($quoteProductCharges);
				$em->flush();
				
				}
			}
		}
	}
	/**
	 * 4203
	 * To get the additional charges
	 */
	public function getChargesById($chargeId) {
		$queryBuilder = $this->getQueryBuilder();
		$queryBuilder ->where('qpc.id  =:charge_id')
		->setParameter('charge_id', $chargeId);
		return $queryBuilder->getQuery()->getOneOrNullResult();
	}
	
	public function setMethod($obj, $fieldName, $value) {
		$obj->setfieldMethod($fieldName, $value);
		$this->_em->persist($obj);
		$this->_em->flush();
	}
	
	/*  To Remove this method after testing.
	 * public function getQuoteProductChargesByIdVal($productData)
	{
		$queryBuilder = $this->getQueryBuilder();
		$queryBuilder ->where('qpc.quoteItemLineId  =:quote_item_line_id ')
		->setParameter('quote_item_line_id', $productData['item_line_id']);
	
	
		return $queryBuilder->getQuery()->getOneOrNullResult();
	} */
	
	public function removeQuoteProductChargesArea($quoteProductCharges)
	{
		if(count($quoteProductCharges)>0){
			foreach($quoteProductCharges as $obj){
				$this->_em->remove($obj);
			}
			$this->_em->flush();
			return 'SUCCESS';
		}
	}
	
}
