<?php

namespace OfficeBrain\Bundle\QuoteBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * QuoteMasterRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class QuoteMasterRepository extends EntityRepository
{
	
	protected $projectSetting;
	protected $instanceId;
	protected $instanceType;
	protected $cultureId;
	protected $cultureCode;
	protected $paginatorService;
	protected $projectSettingService;
	protected $userType;
	protected $userId;
	protected $userManager;
    protected $erpId;
	
	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;
	}
	
     function setErpId($erpId)
    {
        $this->erpId= $erpId;
    }
    
	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:QuoteMaster')->createQueryBuilder('qm');
	}
	
	public function createQuoteAdmin($erpId = null)
	{
		$userDetail = $this->userManager->getUserById($this->userId);
		$status =array();
		$quoteMaster = new QuoteMaster();
		
		$quoteMaster->setUserId($this->userId);
		$quoteMaster->setUserType($this->userType);
		$quoteMaster->setInstanceId($this->instanceId);
		$quoteMaster->setInstanceType($this->instanceType);
		$quoteMaster->setCulture($this->cultureCode);
        if($erpId != null){
            $quoteMaster->setErpId($erpId);
        }
        
		$em = $this->getEntityManager();
		$em->persist($quoteMaster);
		$em->flush();
		
 		if($this->instanceType=="supplier"){
 			$this->setSupplierMapping($quoteMaster, $userDetail);
 		}
		if($this->instanceType=="distributor"){
			$this->setDistributorMapping($quoteMaster, $userDetail);
		}
		
		if($this->instanceType=="association"){
			if($this->userObj->getUserType()=="supplier"){
				$this->setSupplierMapping($quoteMaster, $userDetail);
			}
			if($this->userObj->getUserType()=="distributor"){
				$this->setDistributorMapping($quoteMaster, $userDetail);
			}
		}
        $status['status'] = 'SUCCESS';
        $status['data'] = $quoteMaster;
		return $status;
	}
	
	public function setSupplierMapping($quoteMasterObj, $userDetail)
	{
		
		$quoteSupplierContactMapping = new QuoteSupplierContactMapping();
		$quoteSupplierContactMapping->setQuoteMasterId($quoteMasterObj);
		$quoteSupplierContactMapping->setUserId($userDetail->getId());
		$quoteSupplierContactMapping->setFirstName($userDetail->getFirstName());
		$quoteSupplierContactMapping->setLastName($userDetail->getLastName());
		$quoteSupplierContactMapping->setEmail($userDetail->getEmail());
		$quoteSupplierContactMapping->setPhone($userDetail->getPhone());
		$quoteSupplierContactMapping->setFax($userDetail->getFax());
		/* $quoteSupplierContactMapping->setCity($this->coreManager->getCityName($userDetail->getCityId()));
		$quoteSupplierContactMapping->setState($this->coreManager->getStateName($userDetail->getStateId()));
		$quoteSupplierContactMapping->setCountry($this->coreManager->getCountryName($userDetail->getCountryId())); */
		$quoteSupplierContactMapping->setCityName($userDetail->getCityName());
 		$quoteSupplierContactMapping->setStateName($userDetail->getStateName());
 		$quoteSupplierContactMapping->setCountryName($userDetail->getCountryName());
//		$quoteSupplierContactMapping->setStateId($userDetail->getStateId());
//		$quoteSupplierContactMapping->setCountryId($userDetail->getCountryId());
		$quoteSupplierContactMapping->setZipCode($userDetail->getZipcode());
		$quoteSupplierContactMapping->setAddress($userDetail->getStreet1());
		$quoteSupplierContactMapping->setStreet2($userDetail->getStreet2());
		$quoteSupplierContactMapping->setPhoto($userDetail->getPhoto());
		$quoteSupplierContactMapping->setCompanyName($userDetail->getCompanyName());
		$quoteSupplierContactMapping->setWebsite($userDetail->getWebsite());
		$quoteSupplierContactMapping->setIsImageUpload($userDetail->getPhoto() ? true : false);
		$em = $this->getEntityManager();
		$em->persist($quoteSupplierContactMapping);
		$em->flush();
        $quoteMasterObj->addQuoteSupplierContactMapping($quoteSupplierContactMapping);
	}
	
	public function setDistributorMapping($quoteMasterObj, $userDetail)
	{
		$quoteDistributorContactMapping = new QuoteDistributorContactMapping();
		$quoteDistributorContactMapping->setQuoteMasterId($quoteMasterObj);
        $quoteDistributorContactMapping->setContactbookId(0);
		$quoteDistributorContactMapping->setUserId($userDetail->getId());
		$quoteDistributorContactMapping->setFirstName($userDetail->getFirstName());
		$quoteDistributorContactMapping->setLastName($userDetail->getLastName());
		$quoteDistributorContactMapping->setEmail($userDetail->getEmail());
		$quoteDistributorContactMapping->setPhone($userDetail->getPhone());
		$quoteDistributorContactMapping->setFax($userDetail->getFax());
		$quoteDistributorContactMapping->setCityId($userDetail->getCityId());
		$quoteDistributorContactMapping->setStateId($userDetail->getStateId());
		$quoteDistributorContactMapping->setCountryId($userDetail->getCountryId());
		$quoteDistributorContactMapping->setZipCode($userDetail->getZipcode());
		$quoteDistributorContactMapping->setAddress($userDetail->getStreet1().' '.$userDetail->getStreet2());
		//$quoteDistributorContactMapping->setUserImage($userDetail->getPhoto());
		$quoteDistributorContactMapping->setCompanyName($userDetail->getCompanyName());
		$quoteDistributorContactMapping->setWebsite($userDetail->getWebsite());
		$quoteDistributorContactMapping->setIsImageUpload($userDetail->getPhoto() ? true : false);
		$em = $this->getEntityManager();
		$em->persist($quoteDistributorContactMapping);
		$em->flush();
        $quoteMasterObj->addQuoteDistributorContactMapping($quoteDistributorContactMapping);
		
	}
	
	public function isAdminQuoteExists($quoteId)
	{
		$queryBuilder = $this->getQueryBuilder();
		$queryBuilder->where('qm.id = :quoteId');
		$queryBuilder->setParameter('quoteId', $quoteId);
		return $queryBuilder->getQuery()->getOneOrNullResult();
	}
	
    public function isAdminERPQuoteExists($erpQuoteId)
    {
        $queryBuilder = $this->getQueryBuilder();
        $queryBuilder->where('qm.erpId = :erpQuoteId');
        $queryBuilder->setParameter('erpQuoteId', $erpQuoteId);
        return $queryBuilder->getQuery()->getOneOrNullResult();
    }
    
	public function isNegotiationPossible($quoteId,$secret)
	{
		$queryBuilder = $this->getQueryBuilder();
		$queryBuilder->where('qm.id = :quoteId');
		$queryBuilder->andWhere('qm.secretKey=:secret');
		$queryBuilder->setParameter('quoteId', $quoteId);
		$queryBuilder->setParameter('secret', $secret);
		return $queryBuilder->getQuery()->getOneOrNullResult();
	}
	
	public function getQuoteAdminList($page,$recordsPerPage,$requestStatus,$searchParameters,$sortingParameter,$searchKeyWordsArray, $flag)
	{
		$queryBuilder = $this->getQueryBuilder();
		if(!empty($searchParameters)){
			if(($searchParameters['start_date'] !='') && ($searchParameters['end_date'] !='')){
				$queryBuilder->add('where', "DATE_FORMAT(qm.createdAt,'%Y-%m-%d') between '".$searchParameters['start_date']."' and '".$searchParameters['end_date']."'");
			}
		}
		if ($flag == 'user') {
			$queryBuilder->andwhere('qm.deletedAt IS NULL AND qm.instanceId = :instanceId AND qm.userId = :userId')
				->setParameter('instanceId', $this->instanceId)
				->setParameter('userId', $this->userId);
		} else {
			$queryBuilder->where('qm.deletedAt IS NULL AND qm.instanceId = :instanceId')
				->setParameter('instanceId', $this->instanceId);
				//->setParameter('userId', $this->userId);
		}
			
		if (isset($searchKeyWordsArray) and $searchKeyWordsArray!='') {
			foreach ($searchKeyWordsArray as $key=>$val) {				
				if ($key != '') {
					$param = 'quote'.$key;
					$queryBuilder->andWhere('qm.'.$key.' LIKE :'.$param)->setParameter($param, '%'. $val .'%');
				}
			}
		}
		
		if($requestStatus !=''){
			$queryBuilder->andWhere('qm.quoteStatus =:quoteStatus')->setParameter('quoteStatus',$requestStatus );
		}
		if(!empty($sortingParameter) && $sortingParameter['sort_field'] !='' && $sortingParameter['sort_type'] !=''){			
			 $queryBuilder->orderBy('qm.'.$sortingParameter['sort_field'], $sortingParameter['sort_type']); 
		} else{
			$queryBuilder->orderBy('qm.id', 'Desc');
		}		

		return $this->paginatorService->paginate($queryBuilder, $page, $recordsPerPage);
	}
	
	public function addProductToQuote($quoteId, $productData, $erpId=null)
	{
	    if (isset($erpId)) {
	        $quoteMasterObj = $this->isAdminERPQuoteExists($erpId);
	    } else {
	        $quoteMasterObj = $this->isAdminQuoteExists($quoteId);
	    }
		if($quoteMasterObj!=NULL){
			$status =array();
			$quoteItemLine = new QuoteItemLine();
			$quoteItemLine->setQuoteMasterId($quoteMasterObj);
			$quoteItemLine->setProductId($productData['product_id']);
			$quoteItemLine->setSupplierId($productData['supplier']['supplier_id']);
			$quoteItemLine->setCurrencyId($productData['currency']['id']);
			$quoteItemLine->setCurrencyName($productData['currency']['name']);
			$quoteItemLine->setCurrencySymbol($productData['currency']['symbol']);
			$quoteItemLine->setProductSku($productData['basic_information']['sku_number']);
			$quoteItemLine->setProductSlug($productData['basic_information']['slug']);
			$quoteItemLine->setProductThumbImage($productData['basic_information']['default_image_url']);
			$quoteItemLine->setProductImage($productData['basic_information']['default_image_url']);
			$em = $this->getEntityManager();
			$em->persist($quoteItemLine);
			$em->flush();
			$status['status'] = 'SUCCESS';
			$status['data'] = $quoteItemLine;
			return  $status;
		}
	}	

	public function updateQuoteMasterInfo($data)
	{
		$quoteMaster = $this->findOneBy(array('id' => $data['quoteId']));
		if(isset($data['quoteExpire']) && $data['quoteExpire']!='') {
			$expiredDate = date("Y-m-d", strtotime($data['quoteExpire']));//date('Y-m-d', $data['quoteExpire'])
			$quoteMaster->setExpiredAt(new \DateTime($expiredDate));
		}
		$quoteMaster->setQuoteTitle($data['quoteTitle']);
		$quoteMaster->setHeaderTextField($data['quoteHeader']);
		$quoteMaster->setFooterTextField($data['quoteFooter']);
        if(isset($data['erpId']))
        {
            $quoteMaster->setErpId($data['erpId']);
        }
		$em = $this->getEntityManager();
		$em->persist($quoteMaster);
		$em->flush();
		$status['status'] = 'success';
		$status['success_message'] = 'Quote has been saved successfully.';
		$status['data'] = $quoteMaster;
		return $status;
	}
	
	
	/**
	 * To delete request info  product data of particular user
	 */
	public function deleteQuoteById($quoteId)
	{
		$em=$this->getEntityManager();
		$quote = $this->isAdminQuoteExists($quoteId);
		$quote->setDeletedAt(new \DateTime());
		$em->persist($quote);
		$em->flush();
		return array ('status' => 'SUCCESS' , 'data' => $quote);
	}
	
	
	public function setMethod($obj, $fieldName, $value) {
		$obj->setfieldMethod($fieldName, $value);
		$this->_em->persist($obj);
		$this->_em->flush();
	}
	
	public function generateDuplicateQuote($quoteMasterObj, $contactObj=""){
		if($contactObj==""){
			$isMailSent= NULL;
			$secretKey = NULL;
			$sent = $quoteMasterObj->getQuoteStatus();
		}else{
			$isMailSent= 1;
			$secretKey = md5 ( $contactObj->getId() . '::' . time () );
			$sent = 'sent';
		}
		$quoteMaster = new QuoteMaster();
		$quoteMaster->setInstanceId($quoteMasterObj->getInstanceId());
		$quoteMaster->setInstanceType($quoteMasterObj->getInstanceType());
		$quoteMaster->setUserId($quoteMasterObj->getUserId());
		$quoteMaster->setUserType($quoteMasterObj->getUserType());
		$quoteMaster->setNote($quoteMasterObj->getNote());
		$quoteMaster->setPrefferedState($quoteMasterObj->getPrefferedState());
		$quoteMaster->setPrefferedDistributorId($quoteMasterObj->getPrefferedDistributorId());
		$quoteMaster->setZipCode($quoteMasterObj->getZipCode());
		$quoteMaster->setOnHandDate($quoteMasterObj->getOnHandDate());
		$quoteMaster->setCouponId($quoteMasterObj->getCouponId());
		$quoteMaster->setIsMailSent($isMailSent);
		$quoteMaster->setQuoteStatus($sent);
		$quoteMaster->setSecretKey($secretKey);
		$quoteMaster->setSupplierId($quoteMasterObj->getSupplierId());
		$quoteMaster->setInquiryRefId($quoteMasterObj->getInquiryRefId());
		$quoteMaster->setInquiryRefType($quoteMasterObj->getInquiryRefType());
		$quoteMaster->setHeaderTextField($quoteMasterObj->getHeaderTextField());
		$quoteMaster->setFooterTextField($quoteMasterObj->getFooterTextField());
		$quoteMaster->setIsVisibleHeaderText($quoteMasterObj->getIsVisibleHeaderText());
		$quoteMaster->setIsVisibleFooterText($quoteMasterObj->getIsVisibleFooterText());
		$quoteMaster->setQuoteTitle($quoteMasterObj->getQuoteTitle());
		$quoteMaster->setCultureCode($quoteMasterObj->getCultureCode());
		$quoteMaster->setCultureId($quoteMasterObj->getCultureId());
		$quoteMaster->setOrderMasterId($quoteMasterObj->getOrderMasterId());
		$quoteMaster->setGrandTotal($quoteMasterObj->getGrandTotal());
		$quoteMaster->setSubTotal($quoteMasterObj->getSubTotal());
		$quoteMaster->setIsVisibleSubTotal($quoteMasterObj->getIsVisibleSubTotal());
		$quoteMaster->setDiscount($quoteMasterObj->getDiscount());
		$quoteMaster->setIsVisibleDiscount($quoteMasterObj->getIsVisibleDiscount());
		$quoteMaster->setAdditionalCharges($quoteMasterObj->getAdditionalCharges());
		$quoteMaster->setIsVisibleAdditionalCharges($quoteMasterObj->getIsVisibleAdditionalCharges());
		$quoteMaster->setIsVisibleGrandTotal($quoteMasterObj->getIsVisibleGrandTotal());
		$quoteMaster->setSubTotalText($quoteMasterObj->getSubTotalText());
		$quoteMaster->setAdditionalChargesText($quoteMasterObj->getAdditionalChargesText());
		$quoteMaster->setDiscountText($quoteMasterObj->getDiscountText());
		$quoteMaster->setGrandTotalText($quoteMasterObj->getGrandTotalText());
		$quoteMaster->setUserComment($quoteMasterObj->getUserComment());
		$quoteMaster->setCreatedUid($quoteMasterObj->getCreatedUid());
		$quoteMaster->setUpdatedUid($quoteMasterObj->getUpdatedUid());
		$quoteMaster->setDeletedUid($quoteMasterObj->getDeletedUid());
		$quoteMaster->setExpiredAt($quoteMasterObj->getExpiredAt());
		$quoteMaster->setNegotationComment($quoteMasterObj->getNegotationComment());
		
		$em = $this->getEntityManager();
		$em->persist($quoteMaster);
		$em->flush();
		if($this->instanceType=="supplier"){
			$supplier = $quoteMasterObj->getQuoteSupplierContactMapping();
			if(count($supplier)>0){
			$this->setDuplicateSupplierMapping($quoteMaster, $supplier[0]);
			}
		}
		if($this->instanceType=="distributor"){
			$distributor = $quoteMasterObj->getQuoteDistributorContactMapping();
			if(count($distributor)>0){
			$this->setDuplicateDistributorMapping($quoteMaster, $distributor[0]);
			}
		}
		
		if($this->instanceType=="association"){
			if($this->userObj->getUserType()=="supplier"){
				$supplier = $quoteMasterObj->getQuoteSupplierContactMapping();
				if(count($supplier)>0){
				$this->setDuplicateSupplierMapping($quoteMaster, $supplier[0]);
				}
			}
			if($this->userObj->getUserType()=="distributor"){
				$distributor = $quoteMasterObj->getQuoteDistributorContactMapping();
				if(count($distributor)>0){
				$this->setDuplicateDistributorMapping($quoteMaster, $distributor[0]);
				}
			}
		}
		
		if($contactObj==""){
			$this->setDuplicateCustomerContact($quoteMaster, $quoteMasterObj);
		}else{
			$contactObj->setQuoteMasterId($quoteMaster);
			$em->persist($contactObj);
			$em->flush();
		}
		
		$this->setDuplicateQuoteItemLine($quoteMaster, $quoteMasterObj);
		if($contactObj==""){
			return $quoteMaster;
		}else{
			return $quoteMaster->getId();
		}
	}
	
	function setDuplicateCustomerContact($quoteMasterObj, $quoteMasterData){
		if($this->instanceType=="supplier"){
			$distributor = $quoteMasterData->getQuoteDistributorContactMapping();
			if(count($distributor)>0){
				foreach($distributor as $dist){
					$this->setDuplicateDistributorMapping($quoteMasterObj, $dist);
				}
			}
		}
		if($this->instanceType=="distributor"){
			$user = $quoteMasterData->getQuoteDistributorContactMapping();
			if(count($user)>0){
				foreach($user as $userData){
					$this->setDuplicateUserMapping($quoteMasterObj, $userData);
				}
			}
		}
		
		if($this->instanceType=="association"){
			if($this->userObj->getUserType()=="supplier"){
				$distributor = $quoteMasterData->getQuoteDistributorContactMapping();
				if(count($distributor)>0){
					foreach($distributor as $dist){
						$this->setDuplicateDistributorMapping($quoteMasterObj, $dist);
					}
				}
			}
			if($this->userObj->getUserType()=="distributor"){
				$user = $quoteMasterData->getQuoteDistributorContactMapping();
				if(count($user)>0){
					foreach($user as $userData){
						$this->setDuplicateUserMapping($quoteMasterObj, $userData);
					}
				}
			}
		}
	}
	
	function setDuplicateSupplierMapping($quoteMasterObj, $supplier){
		$quoteSupplierContactMapping = new QuoteSupplierContactMapping();
		$quoteSupplierContactMapping->setQuoteMasterId($quoteMasterObj);
		$quoteSupplierContactMapping->setUserId($supplier->getUserId());
		$quoteSupplierContactMapping->setFirstName($supplier->getFirstName());
		$quoteSupplierContactMapping->setLastName($supplier->getLastName());
		$quoteSupplierContactMapping->setEmail($supplier->getEmail());
		$quoteSupplierContactMapping->setPhone($supplier->getPhone());
		$quoteSupplierContactMapping->setFax($supplier->getFax());
		$quoteSupplierContactMapping->setZipCode($supplier->getZipcode());
		$quoteSupplierContactMapping->setAddress($supplier->getAddress());
		$quoteSupplierContactMapping->setPhoto($supplier->getPhoto());
		$quoteSupplierContactMapping->setCompanyName($supplier->getCompanyName());
		$quoteSupplierContactMapping->setWebsite($supplier->getWebsite());
		$quoteSupplierContactMapping->setIsImageUpload($supplier->getIsImageUpload() ? true : false);
		$quoteSupplierContactMapping->setCityId($supplier->getCityId());
		$quoteSupplierContactMapping->setStateId($supplier->getStateId());
		$quoteSupplierContactMapping->setCountryId($supplier->getCountryId());
		$em = $this->getEntityManager();
		$em->persist($quoteSupplierContactMapping);
		$em->flush();
	}
	
	function setDuplicateDistributorMapping($quoteMasterObj, $distributor){
		$quoteDistributorContactMapping = new QuoteDistributorContactMapping();
		$quoteDistributorContactMapping->setQuoteMasterId($quoteMasterObj);
		$quoteDistributorContactMapping->setUserId($distributor->getUserId());
        $quoteDistributorContactMapping->setContactbookId($distributor->getContactbookId());
		$quoteDistributorContactMapping->setFirstName($distributor->getFirstName());
		$quoteDistributorContactMapping->setLastName($distributor->getLastName());
		$quoteDistributorContactMapping->setEmail($distributor->getEmail());
		$quoteDistributorContactMapping->setPhone($distributor->getPhone());
		$quoteDistributorContactMapping->setFax($distributor->getFax());
		$quoteDistributorContactMapping->setZipCode($distributor->getZipcode());
		$quoteDistributorContactMapping->setAddress($distributor->getAddress());
		$quoteDistributorContactMapping->setPhoto($distributor->getPhoto());
		$quoteDistributorContactMapping->setCompanyName($distributor->getCompanyName());
		$quoteDistributorContactMapping->setWebsite($distributor->getWebsite());
		$quoteDistributorContactMapping->setIsImageUpload($distributor->getIsImageUpload());
		$quoteDistributorContactMapping->setCityId($distributor->getCityId());
		$quoteDistributorContactMapping->setStateId($distributor->getStateId());
		$quoteDistributorContactMapping->setCountryId($distributor->getCountryId());
		$em = $this->getEntityManager();
		$em->persist($quoteDistributorContactMapping);
		$em->flush();
	}
	
	function setDuplicateUserMapping($quoteMasterObj, $user){
		$quoteUserContactMapping = new QuoteUserContactMapping();
		$quoteUserContactMapping->setQuoteMasterId($quoteMasterObj);
		$quoteUserContactMapping->setContactbookId($user->getContactbookId());
		$quoteUserContactMapping->setFirstName($user->getFirstName());
		$quoteUserContactMapping->setLastName($user->getLastName());
		$quoteUserContactMapping->setEmail($user->getEmail());
		$quoteUserContactMapping->setPhone($user->getPhone());
		$quoteUserContactMapping->setFax($user->getFax());
		$quoteUserContactMapping->setZipCode($user->getZipcode());
		$quoteUserContactMapping->setAddress($user->getAddress());
		$quoteUserContactMapping->setPhoto($user->getPhoto());
		$quoteUserContactMapping->setCompanyName($user->getCompanyName());
		$quoteUserContactMapping->setWebsite($user->getWebsite());
		$quoteUserContactMapping->setIsImageUpload($user->getIsImageUpload());
		$quoteUserContactMapping->setCityId($user->getCityId());
		$quoteUserContactMapping->setStateId($user->getStateId());
		$quoteUserContactMapping->setCountryId($user->getCountryId());
		$em = $this->getEntityManager();
		$em->persist($quoteUserContactMapping);
		$em->flush();
	}
	
	function setDuplicateQuoteItemLine($quoteMasterObj, $quoteMasterData){
		$itemLine=$quoteMasterData->getQuoteItemLine();
		if(count($itemLine)>0){
			foreach($itemLine as $itemLineData){

			$quoteItemLine = new QuoteItemLine();
			$quoteItemLine->setQuoteMasterId($quoteMasterObj);
			$quoteItemLine->setProductId($itemLineData->getProductId());
			$quoteItemLine->setIsPaperProof($itemLineData->getIsPaperProof());
			$quoteItemLine->setIsProductProof($itemLineData->getIsProductProof());
			$quoteItemLine->setEmailForPaperProof($itemLineData->getEmailForPaperProof());
			$quoteItemLine->setSupplierId($itemLineData->getSupplierId());
			$quoteItemLine->setCurrencyId($itemLineData->getCurrencyId());
			$quoteItemLine->setCurrencyName($itemLineData->getCurrencyName());
			$quoteItemLine->setCurrencySymbol($itemLineData->getCurrencySymbol());
			$quoteItemLine->setProductSku($itemLineData->getProductSku());
			$quoteItemLine->setProductSlug($itemLineData->getProductSlug());
			$quoteItemLine->setProductThumbImage($itemLineData->getProductThumbImage());
			$quoteItemLine->setProductImage($itemLineData->getProductImage());
			$quoteItemLine->setProductNote($itemLineData->getProductNote());
			$quoteItemLine->setIsSendReviewLink($itemLineData->getIsSendReviewLink());
			$quoteItemLine->setIsFeedbackPosted($itemLineData->getIsFeedbackPosted());
			$quoteItemLine->setIsComplete($itemLineData->getIsComplete());
			$quoteItemLine->setProductComments($itemLineData->getProductComments());
			$quoteItemLine->setOrderType($itemLineData->getOrderType());
			$quoteItemLine->setSequenceNum($itemLineData->getSequenceNum());
			$quoteItemLine->setDisplaySku($itemLineData->getDisplaySku());
			
			$quoteItemLine->setProductDetailConfiguration($itemLineData->getProductDetailConfiguration());
			$quoteItemLine->setItemLineTotal($itemLineData->getItemLineTotal());
			$quoteItemLine->setItemLineDiscount($itemLineData->getItemLineDiscount());
			$quoteItemLine->setItemLineAmount($itemLineData->getItemLineAmount());
			$quoteItemLine->setUnitPrice($itemLineData->getUnitPrice());
			$quoteItemLine->setExtraColor($itemLineData->getExtraColor());
			$em = $this->getEntityManager();
			$em->persist($quoteItemLine);
			$em->flush();
			$quoteMasterObj->addQuoteItemLine($quoteItemLine);
			$this->setDuplicateQuoteProduct($quoteItemLine, $itemLineData);
			$this->setDuplicateQuoteProductImprint($quoteItemLine, $itemLineData);
			$this->setDuplicateQuoteProductCharges($quoteItemLine, $itemLineData);
			$this->setDuplicateQuoteProductAdditionalCharges($quoteItemLine, $itemLineData);
			}
		}
	}
	
	public function setDuplicateQuoteProduct($quoteItemLineObj, $itemLineData){
		$product= $itemLineData->getQuoteProduct();
		if(count($product)>0){
			foreach($product as $productData){
				$quoteProduct = new QuoteProduct();
				$quoteProduct->setQuoteItemLineId($quoteItemLineObj);
				$quoteProduct->setQuoteMasterId($quoteItemLineObj->getQuoteMasterId()->getId());
				$quoteProduct->setProductId($productData->getProductId());
				$quoteProduct->setCultureProductId($productData->getCultureProductId());
				$em = $this->getEntityManager();
				$em->persist($quoteProduct);
				$em->flush();
				
				// One to One
				$this->setDuplicateQuoteShippingDetail($quoteProduct, $productData);
				// One to Many
				$this->setDuplicateQuoteVariationMaster($quoteProduct, $productData);
			}
		}
	}
	
	public function setDuplicateQuoteShippingDetail($quoteProductObj, $productData){
		$shipping =$productData->getQuoteShippingDetail();
		$inHandDt = array();
		if(count($productData->getQuoteItemLineId())>0 && $productData->getQuoteItemLineId()->getProductDetailConfiguration() != '') {
			$inHandDt = unserialize($productData->getQuoteItemLineId()->getProductDetailConfiguration());
		}
		if(count($shipping)>0){
		$quoteShippingDetail = new QuoteShippingDetail();
		$quoteShippingDetail->setQuoteProductId($quoteProductObj);
		$quoteShippingDetail->setShippingType($shipping->getShippingType());
		$quoteShippingDetail->setServiceType($shipping->getServiceType());
		$quoteShippingDetail->setShippingCharge($shipping->getShippingCharge());
		$quoteShippingDetail->setName($shipping->getName());
		$quoteShippingDetail->setEmail($shipping->getEmail());
		$quoteShippingDetail->setStreet1($shipping->getStreet1());
		$quoteShippingDetail->setStreet2($shipping->getStreet2());
		$quoteShippingDetail->setCountryId($shipping->getCountryId());
		$quoteShippingDetail->setStateId($shipping->getStateId());
		$quoteShippingDetail->setCityId($shipping->getCityId());
		$quoteShippingDetail->setZipCode($shipping->getZipCode());
		$quoteShippingDetail->setExtension($shipping->getExtension());
		$quoteShippingDetail->setPhone($shipping->getPhone());
		$quoteShippingDetail->setMobile($shipping->getMobile());
		$inHandDate = (isset($inHandDt['shipping_method']['shipping_detail'][0]['ship_handdate']))?$inHandDt['shipping_method']['shipping_detail'][0]['ship_handdate']:'';
		unset($inHandDt);
		$quoteShippingDetail->setInHandDate($inHandDate);
		$quoteShippingDetail->setFax($shipping->getFax());
		$quoteShippingDetail->setWebsite($shipping->getWebsite());
		$quoteShippingDetail->setCompanyName($shipping->getCompanyName());
		//$quoteShippingDetail->setCompanyNumber($shipping->getCompanyNumber());
		$em = $this->getEntityManager();
		$em->persist($quoteShippingDetail);
		$em->flush();
		}
	}
	
	public function setDuplicateQuoteVariationMaster($quoteProductObj, $productData){
		$variationMaster =$productData->getQuoteVariationMaster();
		if(count($variationMaster)>0){
			foreach($variationMaster as $variation){
			$quoteVariationMaster = new QuoteVariationMaster();
			$quoteVariationMaster->setQuoteProductId($quoteProductObj);
			$quoteVariationMaster->setAttributeId($variation->getAttributeId());
			$quoteVariationMaster->setAttributeValueId($variation->getAttributeValueId());
			$em = $this->getEntityManager();
			$em->persist($quoteVariationMaster);
			$em->flush();
			$this->setDuplicateQuoteVariationMapping($quoteVariationMaster, $variation);
			}
		}
	}
	
	public function setDuplicateQuoteVariationMapping($quoteVariationMasterObj, $varationMaster){
		$varationMapping= $varationMaster->getQuoteVariationMapping();
		if(count($varationMapping)>0){
		$quoteVariationMapping = new QuoteVariationMapping();
		$quoteVariationMapping->setVariationMasterId($quoteVariationMasterObj);
		$em = $this->getEntityManager();
		$em->persist($quoteVariationMapping);
		$em->flush();
		// One to Many
		$this->setDuplicateQuoteVariationDetail($quoteVariationMapping, $varationMapping);
		$this->setDuplicateQuoteVariationQuantity($quoteVariationMapping, $varationMapping);
		}
	}
	
	public function setDuplicateQuoteVariationDetail($quoteVariationMappingObj, $varationMapping){
		$vartionDetail= $varationMapping->getQuoteVariationDetail();
		if(count($vartionDetail)>0){
			foreach($vartionDetail as $detailData){
			$quoteVariationDetail = new QuoteVariationDetail();
			$quoteVariationDetail->setVariationMappingId($quoteVariationMappingObj);
			$quoteVariationDetail->setAttributeId($detailData->getAttributeId());
			$quoteVariationDetail->setAttributeValueId($detailData->getAttributeValueId());
			$em = $this->getEntityManager();
			$em->persist($quoteVariationDetail);
			$em->flush();
			}
		}
	}
	
	public function setDuplicateQuoteVariationQuantity($quoteVariationMappingObj, $varationMapping){
		$vartionQuantity= $varationMapping->getQuoteVariationQuantity();
		if(count($vartionQuantity)>0){
			foreach($vartionQuantity as $quantityData){
				$quoteVariationQuantity = new QuoteVariationQuantity();
				$quoteVariationQuantity->setVariationMappingId($quoteVariationMappingObj);
				$quoteVariationQuantity->setProductQuantity($quantityData->getProductQuantity());
				$quoteVariationQuantity->setUnitPrice($quantityData->getUnitPrice());
				$em = $this->getEntityManager();
				$em->persist($quoteVariationQuantity);
				$em->flush();
			}
		}
	}
	
	public function setDuplicateQuoteProductCharges($quoteItemLineObj, $itemLineData){
		$charges= $itemLineData->getQuoteProductCharges();
		if(count($charges)>0){
			foreach($charges as $chargesData){
				$quoteProductCharges = new QuoteProductCharges();
				$quoteProductCharges->setQuoteItemLineId($quoteItemLineObj);
				$quoteProductCharges->setChargesType($chargesData->getChargesType());
				$quoteProductCharges->setChargesAmount($chargesData->getChargesAmount());
				$em = $this->getEntityManager();
				$em->persist($quoteProductCharges);
				$em->flush();
			}
		}
	}
	
	public function setDuplicateQuoteProductAdditionalCharges($quoteItemLineObj, $itemLineData){
		$charges= $itemLineData->getQuoteProductAdditionalCharges();
		if(count($charges)>0){
			foreach($charges as $chargesData){
				$quoteProductAdditionalCharges = new QuoteProductAdditionalCharges();
				$quoteProductAdditionalCharges->setQuoteItemLineId($quoteItemLineObj);
				$quoteProductAdditionalCharges->setChargesType($chargesData->getChargesType());
				$quoteProductAdditionalCharges->setQuantity($chargesData->getQuantity());
				$quoteProductAdditionalCharges->setUnitCharge($chargesData->getUnitCharge());
				$quoteProductAdditionalCharges->setChargesAmount($chargesData->getChargesAmount());
				$em = $this->getEntityManager();
				$em->persist($quoteProductAdditionalCharges);
				$em->flush();
			}
		}
	}
	
	public function setDuplicateQuoteProductImprint($quoteItemLineObj, $itemLineData){
		$imprintArea= $itemLineData->getQuoteProductImprintArea();
		if(count($imprintArea)>0){
			foreach($imprintArea as $imprintAreaData){
				
				$quoteProductImprintArea = new QuoteProductImprintArea();
				$quoteProductImprintArea->setQuoteItemLineId($quoteItemLineObj);
				$quoteProductImprintArea->setImprintAreaId($imprintAreaData->getImprintAreaId());
				$quoteProductImprintArea->setImprintAreaMethodId($imprintAreaData->getImprintAreaMethodId());
				$quoteProductImprintArea->setImprintAreaNoofColor($imprintAreaData->getImprintAreaNoofColor());
				$em = $this->getEntityManager();
				$em->persist($quoteProductImprintArea);
				$em->flush();
				$this->setDuplicateQuoteProductImprintColor($quoteProductImprintArea,$imprintAreaData);
				$this->setDuplicateQuoteProductImprintTextArtwork($quoteProductImprintArea,$imprintAreaData);
			}
		}
	}
	
	public function setDuplicateQuoteProductImprintColor($quoteProductImprintAreaObj, $imprintAreaData){
		$imprintColor= $imprintAreaData->getQuoteProductImprintColor();
		if(count($imprintColor)>0){
			foreach($imprintColor as $imprintColorData){
				$quoteProductImprintColor = new QuoteProductImprintColor();
				$quoteProductImprintColor->setImprintAreaId($quoteProductImprintAreaObj);
				$quoteProductImprintColor->setIsPmsColor($imprintColorData->getIsPmsColor());
				$quoteProductImprintColor->setColorName($imprintColorData->getColorName());
				$quoteProductImprintColor->setColorId($imprintColorData->getColorId());
				$quoteProductImprintColor->setPrice($imprintColorData->getPrice());
				$em = $this->getEntityManager();
				$em->persist($quoteProductImprintColor);
				$em->flush();
			}
		}
	}
	
	public function setDuplicateQuoteProductImprintTextArtwork($quoteProductImprintAreaObj, $imprintAreaData){
		$imprintTextArtWork= $imprintAreaData->getQuoteProductImprintTextArtWork();
		if(count($imprintTextArtWork)>0){
			foreach($imprintTextArtWork as $imprintTextArtWorkData){
				$quoteProductImprintTextArtwork = new QuoteProductImprintTextArtWork();
				$quoteProductImprintTextArtwork->setImprintAreaId($quoteProductImprintAreaObj);
				$quoteProductImprintTextArtwork->setImprintText($imprintTextArtWorkData->getImprintText());
				$quoteProductImprintTextArtwork->setLogoName($imprintTextArtWorkData->getLogoName());
				$quoteProductImprintTextArtwork->setLogoExtension($imprintTextArtWorkData->getLogoExtension());
				$quoteProductImprintTextArtwork->setLogoPath($imprintTextArtWorkData->getLogoPath());
				$quoteProductImprintTextArtwork->setIsLogoSendAfterOrder($imprintTextArtWorkData->getIsLogoSendAfterOrder());
				$quoteProductImprintTextArtwork->setEmailForLogo($imprintTextArtWorkData->getEmailForLogo());
				$em = $this->getEntityManager();
				$em->persist($quoteProductImprintTextArtwork);
				$em->flush();
			}
		}
	}
	
	public function updateQuoteHash($contactObj){
			$isMailSent= 1;
			$secretKey = md5 ( $contactObj->getId() . '::' . time () );
			$quoteMasterObj=$contactObj->getQuoteMasterId();
			$sent = 'sent';
			$quoteMasterObj->setIsMailSent($isMailSent);
			$quoteMasterObj->setQuoteStatus($sent);
			$quoteMasterObj->setSecretKey($secretKey);
			$em = $this->getEntityManager();
			$em->persist($quoteMasterObj);
			$em->flush();
			return $quoteMasterObj->getId();
			
	}
	
	public function getQuoteCountList($status, $flag)
	{
		$queryBuilder = $this->getQueryBuilder();
		$queryBuilder->select('count(qm.id)');
		
		if ($flag == 'user') {
			$queryBuilder->where('qm.deletedAt IS NULL AND qm.instanceId = :instanceId AND qm.userId = :userId AND qm.quoteStatus=:quoteStatus ')
			->setParameter('instanceId', $this->instanceId)
			->setParameter('userId', $this->userId)
			->setParameter('quoteStatus', $status);
		} else {
			$queryBuilder->where('qm.deletedAt IS NULL AND qm.instanceId = :instanceId AND qm.quoteStatus=:quoteStatus ')
			->setParameter('instanceId', $this->instanceId)
			->setParameter('quoteStatus', $status);
		}
		return $queryBuilder->getQuery()->getSingleScalarResult();
	}
	
	public function getQuoteList($page,$recordsPerPage,$requestStatus,$searchParameters,$sortingParameter,$searchKeyWordsArray)
	{	
// 		echo $searchParameters['start_date']."===".$searchParameters['end_date']; die;
		$queryBuilder = $this->getQueryBuilder();
		//echo $queryBuilder->getQuery()->getSQL();exit;
		if(!empty($searchParameters)){
			if(($searchParameters['start_date'] !='') && ($searchParameters['end_date'] !='')){
				$queryBuilder->add('where', "DATE_FORMAT(qm.createdAt,'%Y-%m-%d') between '".$searchParameters['start_date']."' and '".$searchParameters['end_date']."'");
			}
		}
		$queryBuilder->andWhere('qm.deletedAt IS NULL and qm.userId=:userId and qm.instanceId=:instanceId');
		$queryBuilder->setParameter('userId',$this->userId);
		$queryBuilder->setParameter('instanceId',$this->instanceId);
		if (isset($searchKeyWordsArray) and $searchKeyWordsArray!='') {			
			foreach ($searchKeyWordsArray as $key=>$val) {
				if ($key != '') {
					$param = 'quote'.$key;
					$queryBuilder->andWhere('qm.'.$key.' LIKE :'.$param)->setParameter($param, '%'. $val .'%');
				}
			}
		}
		if($requestStatus !=''){
			$queryBuilder->andWhere('qm.quoteStatus =:quoteStatus')->setParameter('quoteStatus',$requestStatus );
		}
		if(!empty($sortingParameter) && $sortingParameter['sort_field'] !='' && $sortingParameter['sort_type'] !=''){
			$queryBuilder->orderBy('qm.'.$sortingParameter['sort_field'], $sortingParameter['sort_type']);
		} else{
			$queryBuilder->orderBy('qm.id', 'Desc');
		}
		//echo $queryBuilder->getQuery()->getSQL();exit;
		return $this->paginatorService->paginate($queryBuilder, $page, $recordsPerPage);
	}
	
	//============================ Inquiry Convert To Quote ===================================
	public function convertToQuoteAdmin($inquiryObj,$erpId = null )
	{
		$userDetail = $this->userManager->getUserById($this->userId);
		$status =array();
		$quoteMaster = new QuoteMaster();
	
		$quoteMaster->setUserId($this->userId);
		$quoteMaster->setUserType($this->userType);
		$quoteMaster->setInstanceId($this->instanceId);
		$quoteMaster->setInstanceType($this->instanceType);
		$quoteMaster->setCulture($this->cultureCode);
		if($erpId != null){
			$quoteMaster->setErpId($erpId);
		}
	
		$em = $this->getEntityManager();
		$em->persist($quoteMaster);
		$em->flush();
		$status['status'] = 'SUCCESS';
		$status['data'] = $quoteMaster;
		if($this->instanceType=="supplier"){
			$this->setSupplierMapping($quoteMaster, $userDetail);
		}
		if($this->instanceType=="distributor"){
			$this->setDistributorMapping($quoteMaster, $userDetail);
		}
	
		if($this->instanceType=="association"){
			if($this->userObj->getUserType()=="supplier"){
				$this->setSupplierMapping($quoteMaster, $userDetail);
			}
			if($this->userObj->getUserType()=="distributor"){
				$this->setDistributorMapping($quoteMaster, $userDetail);
			}
		}
		
		if($inquiryObj->getUserId()>0){
			$userDetailObj = $this->userManager->getUserById($inquiryObj->getUserId());
			$this->setConvertCustomerContact($quoteMaster, $userDetailObj);
		}
		
		$this->setConvertQuoteItemLine($quoteMaster, $inquiryObj);
		return $status;
	}
	
	function setConvertCustomerContact($quoteMasterObj, $userDetailObj){
		if($this->instanceType=="supplier"){
			if(count($userDetailObj)>0){
				$this->setConvertDistributorMapping($quoteMasterObj, $userDetailObj);
			}
		}
		if($this->instanceType=="distributor"){
			if(count($userDetailObj)>0){
					$this->setConvertUserMapping($quoteMasterObj, $userDetailObj);
			}
		}
	
		if($this->instanceType=="association"){
			if($this->userObj->getUserType()=="supplier"){
				if(count($userDetailObj)>0){
					$this->setConvertDistributorMapping($quoteMasterObj, $userDetailObj);
				}
			}
			if($this->userObj->getUserType()=="distributor"){
				if(count($userDetailObj)>0){
					$this->setConvertUserMapping($quoteMasterObj, $userDetailObj);
				}
			}
		}
	}
	
	function setConvertDistributorMapping($quoteMasterObj, $distributor){
		$quoteDistributorContactMapping = new QuoteDistributorContactMapping();
		$quoteDistributorContactMapping->setQuoteMasterId($quoteMasterObj);
		$quoteDistributorContactMapping->setContactbookId($distributor->getContactBook()[0]->getId());
		$quoteDistributorContactMapping->setFirstName($distributor->getFirstName());
		$quoteDistributorContactMapping->setLastName($distributor->getLastName());
		$quoteDistributorContactMapping->setEmail($distributor->getEmail());
		$quoteDistributorContactMapping->setPhone($distributor->getPhone());
		$quoteDistributorContactMapping->setFax($distributor->getFax());
		$quoteDistributorContactMapping->setZipCode($distributor->getZipcode());
		//$quoteDistributorContactMapping->setAddress($distributor->getAddress());
		//$quoteDistributorContactMapping->setPhoto($distributor->getPhoto());
		$quoteDistributorContactMapping->setCompanyName($distributor->getCompanyName());
		$quoteDistributorContactMapping->setWebsite($distributor->getWebsite());
		//$quoteDistributorContactMapping->setIsImageUpload($distributor->getIsImageUpload());
		$quoteDistributorContactMapping->setCityId($distributor->getCityId());
		$quoteDistributorContactMapping->setStateId($distributor->getStateId());
		$quoteDistributorContactMapping->setCountryId($distributor->getCountryId());
		$em = $this->getEntityManager();
		$em->persist($quoteDistributorContactMapping);
		$em->flush();
	}
	
	function setConvertUserMapping($quoteMasterObj, $user){
		$quoteUserContactMapping = new QuoteUserContactMapping();
		$quoteUserContactMapping->setQuoteMasterId($quoteMasterObj);
		//$quoteUserContactMapping->setContactbookId($user->getContactbookId());
		$quoteUserContactMapping->setContactbookId($user->getContactBook()[0]->getId());
		$quoteUserContactMapping->setFirstName($user->getFirstName());
		$quoteUserContactMapping->setLastName($user->getLastName());
		$quoteUserContactMapping->setEmail($user->getEmail());
		$quoteUserContactMapping->setPhone($user->getPhone());
		$quoteUserContactMapping->setFax($user->getFax());
		$quoteUserContactMapping->setZipCode($user->getZipcode());
		//$quoteUserContactMapping->setAddress($user->getAddress());
		$quoteUserContactMapping->setPhoto($user->getPhoto());
		$quoteUserContactMapping->setCompanyName($user->getCompanyName());
		$quoteUserContactMapping->setWebsite($user->getWebsite());
		$quoteUserContactMapping->setIsImageUpload($user->getPhoto() ? true : false);
		//$quoteUserContactMapping->setCityId($user->getCityId());
		$quoteUserContactMapping->setStateId($user->getStateId());
		$quoteUserContactMapping->setCountryId($user->getCountryId());
		$em = $this->getEntityManager();
		$em->persist($quoteUserContactMapping);
		$em->flush();
	}
	
	function setConvertQuoteItemLine($quoteMasterObj, $inquiryObj){
		if(get_class($inquiryObj)=="OfficeBrain\Bundle\RequestInfoBundle\Entity\RequestInfo"){
			$itemLine=$inquiryObj->getProductInfoRequest();
			if(count($itemLine)>0){
				foreach($itemLine as $itemLineData){
					$quoteItemLine = new QuoteItemLine();
					$quoteItemLine->setQuoteMasterId($quoteMasterObj);
					$quoteItemLine->setProductId($itemLineData->getProductId());
					$quoteItemLine->setSupplierId($itemLineData->getProductSupplierId());
					$quoteItemLine->setCurrencyId($itemLineData->getCurrencyId());
					$quoteItemLine->setCurrencyName($itemLineData->getCurrencyName());
					$quoteItemLine->setCurrencySymbol($itemLineData->getCurrencySymbol());
					$quoteItemLine->setProductSku($itemLineData->getProductSku());
					$quoteItemLine->setProductSlug($itemLineData->getProductSlug());
					$quoteItemLine->setProductThumbImage($itemLineData->getProductThumbImage());
					$quoteItemLine->setProductImage($itemLineData->getProductImage());
					$quoteItemLine->setProductNote($inquiryObj->getNote());
					$quoteItemLine->setIsComplete(0);
					$quoteItemLine->setDisplaySku(1);	
					$em = $this->getEntityManager();
					$em->persist($quoteItemLine);
					$em->flush();
					$quoteMasterObj->addQuoteItemLine($quoteItemLine);
				}
			}
		}else{
			$itemLine=$inquiryObj->getRequestQuoteItemLine();
			if(count($itemLine)>0){
				foreach($itemLine as $itemLineData){
			
					$quoteItemLine = new QuoteItemLine();
					$quoteItemLine->setQuoteMasterId($quoteMasterObj);
					$quoteItemLine->setProductId($itemLineData->getProductId());
					$quoteItemLine->setSupplierId($itemLineData->getSupplierId());
					$quoteItemLine->setCurrencyId($itemLineData->getCurrencyId());
					$quoteItemLine->setCurrencyName($itemLineData->getCurrencyName());
					$quoteItemLine->setCurrencySymbol($itemLineData->getCurrencySymbol());
					$quoteItemLine->setProductSku($itemLineData->getProductSku());
					$quoteItemLine->setProductSlug($itemLineData->getProductSlug());
					$quoteItemLine->setProductThumbImage($itemLineData->getProductThumbImage());
					$quoteItemLine->setProductImage($itemLineData->getProductImage());
					$quoteItemLine->setProductNote($itemLineData->getProductNote());
					$quoteItemLine->setIsSendReviewLink($itemLineData->getIsSendReviewLink());
					$quoteItemLine->setIsFeedbackPosted($itemLineData->getIsFeedbackPosted());
					$quoteItemLine->setIsComplete($itemLineData->getIsComplete());
					$quoteItemLine->setDisplaySku(1);
						
					$em = $this->getEntityManager();
					$em->persist($quoteItemLine);
					$em->flush();
					$quoteMasterObj->addQuoteItemLine($quoteItemLine);
					$this->setConvertQuoteProduct($quoteItemLine, $itemLineData);
					$this->setConvertQuoteProductImprint($quoteItemLine, $itemLineData);
					$this->setConvertQuoteProductCharges($quoteItemLine, $itemLineData);
				}
			}
		}
		
	}
	
	public function setConvertQuoteProduct($quoteItemLineObj, $itemLineData){
		$product= $itemLineData->getRequestQuoteProduct();
		if(count($product)>0){
			foreach($product as $productData){
				$quoteProduct = new QuoteProduct();
				$quoteProduct->setQuoteItemLineId($quoteItemLineObj);
				$quoteProduct->setQuoteMasterId($quoteItemLineObj->getQuoteMasterId()->getId());
				$quoteProduct->setProductId($productData->getProductId());
				$quoteProduct->setCultureProductId($productData->getCultureProductId());
				$em = $this->getEntityManager();
				$em->persist($quoteProduct);
				$em->flush();
	
				// One to One
				$this->setConvertQuoteShippingDetail($quoteProduct, $productData);
				// One to Many
				$this->setConvertQuoteVariationMaster($quoteProduct, $productData);
			}
		}
	}
	
	
	public function setConvertQuoteShippingDetail($quoteProductObj, $productData){
		$shipping =$productData->getRequestQuoteShippingDetail();
		if(count($shipping)>0){
			$quoteShippingDetail = new QuoteShippingDetail();
			$quoteShippingDetail->setQuoteProductId($quoteProductObj);
			$quoteShippingDetail->setShippingType($shipping->getShippingType());
			$quoteShippingDetail->setServiceType($shipping->getServiceType());
			$quoteShippingDetail->setShippingCharge($shipping->getShippingCharge());
			$quoteShippingDetail->setName($shipping->getName());
			$quoteShippingDetail->setEmail($shipping->getEmail());
			$quoteShippingDetail->setStreet1($shipping->getStreet1());
			$quoteShippingDetail->setStreet2($shipping->getStreet2());
			$quoteShippingDetail->setCountryId($shipping->getCountryId());
			$quoteShippingDetail->setStateId($shipping->getStateId());
			$quoteShippingDetail->setCityId($shipping->getCityId());
			$quoteShippingDetail->setZipCode($shipping->getZipCode());
			$quoteShippingDetail->setExtension($shipping->getExtension());
			$quoteShippingDetail->setPhone($shipping->getPhone());
			$quoteShippingDetail->setMobile($shipping->getMobile());
			$inHandDate = (isset($productData['ship_handdate']))?$productData['ship_handdate']:'';
			$quoteShippingDetail->setInHandDate($inHandDate);
			$quoteShippingDetail->setFax($shipping->getFax());
			$quoteShippingDetail->setWebsite($shipping->getWebsite());
			$quoteShippingDetail->setCompanyName($shipping->getCompanyName());
			//$quoteShippingDetail->setCompanyNumber($shipping->getCompanyNumber());
			$em = $this->getEntityManager();
			$em->persist($quoteShippingDetail);
			$em->flush();
		}
	}
	
	public function setConvertQuoteVariationMaster($quoteProductObj, $productData){
		$variationMaster =$productData->getRequestQuoteVariationMaster();
		if(count($variationMaster)>0){
			foreach($variationMaster as $variation){
				$quoteVariationMaster = new QuoteVariationMaster();
				$quoteVariationMaster->setQuoteProductId($quoteProductObj);
				$quoteVariationMaster->setAttributeId($variation->getAttributeId());
				$quoteVariationMaster->setAttributeValueId($variation->getAttributeValueId());
				$em = $this->getEntityManager();
				$em->persist($quoteVariationMaster);
				$em->flush();
				$this->setConvertQuoteVariationMapping($quoteVariationMaster, $variation);
			}
		}
	}
	
	public function setConvertQuoteVariationMapping($quoteVariationMasterObj, $varationMaster){
		$varationMapping= $varationMaster->getRequestQuoteVariationMapping();
		if(count($varationMapping)>0){
			$quoteVariationMapping = new QuoteVariationMapping();
			$quoteVariationMapping->setVariationMasterId($quoteVariationMasterObj);
			$em = $this->getEntityManager();
			$em->persist($quoteVariationMapping);
			$em->flush();
			// One to Many
			$this->setConvertQuoteVariationDetail($quoteVariationMapping, $varationMapping);
			$this->setConvertQuoteVariationQuantity($quoteVariationMapping, $varationMapping);
		}
	}
	
	public function setConvertQuoteVariationDetail($quoteVariationMappingObj, $varationMapping){
		$vartionDetail= $varationMapping->getRequestQuoteVariationDetail();
		if(count($vartionDetail)>0){
			foreach($vartionDetail as $detailData){
				$quoteVariationDetail = new QuoteVariationDetail();
				$quoteVariationDetail->setVariationMappingId($quoteVariationMappingObj);
				$quoteVariationDetail->setAttributeId($detailData->getAttributeId());
				$quoteVariationDetail->setAttributeValueId($detailData->getAttributeValueId());
				$em = $this->getEntityManager();
				$em->persist($quoteVariationDetail);
				$em->flush();
			}
		}
	}
	
	public function setConvertQuoteVariationQuantity($quoteVariationMappingObj, $varationMapping){
		$vartionQuantity= $varationMapping->getRequestQuoteVariationQuantity();
		if(count($vartionQuantity)>0){
			foreach($vartionQuantity as $quantityData){
				$quoteVariationQuantity = new QuoteVariationQuantity();
				$quoteVariationQuantity->setVariationMappingId($quoteVariationMappingObj);
				$quoteVariationQuantity->setProductQuantity($quantityData->getQuantity());
				$quoteVariationQuantity->setUnitPrice($quantityData->getUnitPrice());
				$em = $this->getEntityManager();
				$em->persist($quoteVariationQuantity);
				$em->flush();
			}
		}
	}
	
	public function setConvertQuoteProductCharges($quoteItemLineObj, $itemLineData){
		$charges= $itemLineData->getRequestQuoteProductCharges();
		if(count($charges)>0){
			foreach($charges as $chargesData){
				$quoteProductCharges = new QuoteProductCharges();
				$quoteProductCharges->setQuoteItemLineId($quoteItemLineObj);
				$quoteProductCharges->setChargesType($chargesData->getChargesType());
				$quoteProductCharges->setChargesAmount($chargesData->getChargesAmount());
				$em = $this->getEntityManager();
				$em->persist($quoteProductCharges);
				$em->flush();
			}
		}
	}
	
	public function setConvertQuoteProductImprint($quoteItemLineObj, $itemLineData){
		$imprintArea= $itemLineData->getRequestQuoteProductImprintArea();
		if(count($imprintArea)>0){
			foreach($imprintArea as $imprintAreaData){
	
				$quoteProductImprintArea = new QuoteProductImprintArea();
				$quoteProductImprintArea->setQuoteItemLineId($quoteItemLineObj);
				$quoteProductImprintArea->setImprintAreaId($imprintAreaData->getImprintAreaId());
				$quoteProductImprintArea->setImprintAreaMethodId($imprintAreaData->getImprintAreaMethodId());
				$quoteProductImprintArea->setImprintAreaNoofColor($imprintAreaData->getImprintAreaNoofColor());
				$em = $this->getEntityManager();
				$em->persist($quoteProductImprintArea);
				$em->flush();
				$this->setConvertQuoteProductImprintColor($quoteProductImprintArea,$imprintAreaData);
				$this->setConvertQuoteProductImprintTextArtwork($quoteProductImprintArea,$imprintAreaData);
			}
		}
	}
	
	public function setConvertQuoteProductImprintColor($quoteProductImprintAreaObj, $imprintAreaData){
		$imprintColor= $imprintAreaData->getRequestQuoteProductImprintColor();
		if(count($imprintColor)>0){
			foreach($imprintColor as $imprintColorData){
				$quoteProductImprintColor = new QuoteProductImprintColor();
				$quoteProductImprintColor->setImprintAreaId($quoteProductImprintAreaObj);
				$quoteProductImprintColor->setIsPmsColor($imprintColorData->getIsPmsColor());
				$quoteProductImprintColor->setColorName('');
				$quoteProductImprintColor->setColorId(is_integer($imprintColorData->getColorId())?$imprintColorData->getColorId():0);
				$quoteProductImprintColor->setPrice($imprintColorData->getPrice());
				$em = $this->getEntityManager();
				$em->persist($quoteProductImprintColor);
				$em->flush();
			}
		}
	}
	
	public function setConvertQuoteProductImprintTextArtwork($quoteProductImprintAreaObj, $imprintAreaData){
		$imprintTextArtWork= $imprintAreaData->getRequestQuoteProductImprintTextArtwork();
		if(count($imprintTextArtWork)>0){
			foreach($imprintTextArtWork as $imprintTextArtWorkData){
				$quoteProductImprintTextArtwork = new QuoteProductImprintTextArtWork();
				$quoteProductImprintTextArtwork->setImprintAreaId($quoteProductImprintAreaObj);
				$quoteProductImprintTextArtwork->setImprintText($imprintTextArtWorkData->getImprintText());
				$quoteProductImprintTextArtwork->setLogoName($imprintTextArtWorkData->getLogoName());
				$quoteProductImprintTextArtwork->setLogoExtension($imprintTextArtWorkData->getLogoExtension());
				$quoteProductImprintTextArtwork->setLogoPath($imprintTextArtWorkData->getLogoPath());
				$quoteProductImprintTextArtwork->setIsLogoSendAfterOrder($imprintTextArtWorkData->getIsLogoSendAfterOrder());
				$quoteProductImprintTextArtwork->setEmailForLogo($imprintTextArtWorkData->getEmailForLogo());
				$em = $this->getEntityManager();
				$em->persist($quoteProductImprintTextArtwork);
				$em->flush();
			}
		}
	}
}