<?php

namespace OfficeBrain\CustomBundle\QuoteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use OfficeBrain\Bundle\QuoteBundle\Form\AddCustomerType;
use OfficeBrain\Bundle\QuoteBundle\Form\AddCustomerContactType;
use OfficeBrain\Bundle\QuoteBundle\Form\QuoteNegotiationType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Form\FormError;
use OfficeBrain\Bundle\QuoteBundle\Controller\QuoteController as BaseQuoteController;

/**
 *
 *
 * @Route("", defaults={"rule_service"="rule_charges"})
 */
class QuoteController extends BaseQuoteController {
	/**
	 *
	 * @author Employee Id: 4203
	 *         To set member variables default value
	 * @return NULL
	 */
	public function __construct() {
		parent::__construct();
		$this->twigManager = null;
	}
	
	/**
	 *
	 * @author Employee Id: 4203
	 *         To unset member variables
	 * @return NULL
	 */
	public function __destruct() {
		parent::__destruct();
		unset ( $this->twigManager );
	}
	
	/**
	 *
	 * @author Employee Id: 4203
	 *         Create various objects required action in this class
	 * @return NULL
	 */
	public function initAction() {
		parent::initAction();
		$this->categoryApiManager = $this->get("officebrain_category_api_manager");
		$this->freeFreightAllowCategoryList = $this->getParameter('free_freight_allow_category_list');
		$this->imageLibraryManager = $this->get ( 'office_brain_cms_media_library_manager' );
        $this->twigManager = $this->get('twig');
	}
	
	/**
	 *
	 * @author Employee Id: 4217
	 *         OB eCommerce Product - preapre search parameter
	 */
	private function prepareSearchParameterArray() {
		$this->searchParameterArray ['start_date'] = $this->request->get ( 'start_date', null );
		$this->searchParameterArray ['end_date'] = $this->request->get ( 'end_date', null );
		$jsDateFormat = $this->coreManager->getDateJsFormat();
		if ($this->searchParameterArray ['start_date'] != null) {
			$startDate = \DateTime::createFromFormat ( $jsDateFormat, $this->searchParameterArray ['start_date'] );
				
			$this->searchParameterArray ['start_date'] = $startDate->format ( 'Y-m-d' );
		}
		if ($this->searchParameterArray ['end_date'] != null) {
			$endDate = \DateTime::createFromFormat ( $jsDateFormat, $this->searchParameterArray ['end_date'] );
			$this->searchParameterArray ['end_date'] = $endDate->format ( 'Y-m-d' );
		}
		$this->option = $this->request->get ( 'option', null );
		$this->keywords = $this->request->get ( 'keyword', null );
		$this->searchKeyWordsArray [$this->option] = trim ( $this->keywords );
	}
	
	private function prepareSortingParameterArray() {
		$this->sortingParameterArray ['sort_type'] = $this->request->get ( 'sort_type', null );
		$this->sortingParameterArray ['sort_field'] = $this->request->get ( 'sort_field', null );
	}
	
	/**
	 *
	 * @author Employee Id: 4217
	 *         Description : Use to display quote list.
	 * @param
	 *        	1 : No paramater
	 * @return type : Form view and data.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/quote-list",
	 *         name="office_brain_quotebundle_quote_quotelist",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainCustomBundleQuoteBundle:Quote:quoteList.html.twig")
	 *
	 */
	public function quoteListAction() {
		$this->initAction ();
		if ($this->userId == 0) {
			$this->get('session')->set('_security.main.target_path', $this->generateUrl($this->request->get('_route')));
			return $this->redirect ( $this->generateUrl ( 'office_brain_userbundle_login_login' ) );
		}
		$this->responseArray ['records_per_page'] = $this->recordsPerPage;
		$this->responseArray ['page'] = $this->page;
		$this->prepareSearchParameterArray ();
		$this->prepareSortingParameterArray ();
		$this->responseArray ['tab_status'] = $this->requestStatus;
		$requestStatus = ($this->requestStatus == "All-Quotes") ? "" : $this->requestStatus;
		$this->responseArray ['data'] = $this->quoteManager->getQuoteList ( $this->page, $this->recordsPerPage, $requestStatus, $this->searchParameterArray, $this->sortingParameterArray, $this->searchKeyWordsArray );
		$this->responseArray ['tab_count'] = $this->quoteManager->getAllTabCount ();
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4203
	 *         Description : Use to configure this quote.
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/quote/{quote_id}/{mode}",
	 *         name="office_brain_quotebundle_quote_quote",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainCustomBundleQuoteBundle:Quote:quote.html.twig")
	 */
	public function quoteAction($mode = "create") {
		$this->initAction ();
		if ($this->userId == 0) {
			if ($this->request->isXmlHttpRequest ()) {
				$this->responseArray ['error_type'] = 'loginError';
				$this->responseArray ['page_url'] = $this->generateUrl ( 'office_brain_userbundle_login_login' );
				return new JsonResponse ( $this->responseArray );
			} else {
				$this->get('session')->set('_security.main.target_path', $this->generateUrl($this->request->get('_route'),array('quote_id'=>$this->request->get('quote_id'),'mode'=>$this->request->get('mode'))));
				return $this->redirect ( $this->generateUrl ( 'office_brain_userbundle_login_login' ) );
			}
		}
		$quoteDetail = $this->quoteManager->isQuoteExists ();
		if ($mode == "edit") {
			$this->responseArray ['mode'] = 'edit';
		} else {
			$this->responseArray ['mode'] = 'create';
		}
		$this->quoteManager->updateQuoteStatus ( $quoteDetail );
		$userObj = $this->getCustomerDetailFromQuote ( $quoteDetail->getId () );
	
		if ($quoteDetail != NULL && ($this->userId == $quoteDetail->getUserId())) {
			$this->responseArray ['data'] = $quoteDetail;
			$this->responseArray ['userData'] = $userObj;
			unset ( $quoteDetail );
			return $this->responseArray;
		} else {
			return $this->redirect ( $this->generateUrl ( 'office_brain_quotebundle_quote_quotelist' ) );
		}
	}
	
	/**
	 *
	 * @author Employee Id: 5633
	 *         Description : Use to get all product list added in this quote.
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/get-all-quote-product/{quote_id}",
	 *         name="office_brain_quotebundle_quote_getproductlist",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:getProductList.html.twig")
	 */
	public function getProductListAction() { //echo '<pre>';
		$this->initAction ();
		$quoteDetail = $this->quoteManager->isQuoteExists ();
		$culture = $quoteDetail->getCulture();
		$this->responseArray ['quote'] = $quoteDetail;
		
		$this->responseArray ['data'] = $this->quoteManager->prepareQuoteProductList ($culture);		
		
		foreach($this->responseArray ['data'] as $dataIndex => $dataValues)
		{
			if(isset($this->responseArray ['data'][$dataIndex]['price_type']) && $this->responseArray ['data'][$dataIndex]['price_type']== 'variations')
			{
				if(isset($this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country']))
				{
					foreach($this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'] as $countryIndex=> $countryCode)
					{
						$firstVariationDataWithSpecial = array();
						$firstVariationDataWithoutSpecial= array();
						$count = 0;
						foreach($this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex] as $variationId => $variationData)
						{
							if($count == 0)
							{
								$firstVariationDataWithSpecial = $variationData['pricing']['decorative'];
								$firstVariationDataWithoutSpecial= $variationData['pricing']['decorative'];
							}
							$count = $count + 1;
						}
						// Now update the rate and other things if provided
						if(!(empty($firstVariationDataWithSpecial)))
						{
							foreach($firstVariationDataWithSpecial as $firstVariationRangeIndex => $firstVariationRange)
							{
								if(isset($firstVariationDataWithSpecial[$firstVariationRangeIndex]['rate']))
								{
									$firstVariationDataWithSpecial[$firstVariationRangeIndex]['rate'] = 'N/A';
									$firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['rate'] = 'N/A';
								}
								if(isset($firstVariationDataWithSpecial[$firstVariationRangeIndex]['rate_orig']))
								{
									$firstVariationDataWithSpecial[$firstVariationRangeIndex]['rate_orig'] = 'N/A';
									$firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['rate_orig'] = 'N/A';
								}
								if(isset($firstVariationDataWithSpecial[$firstVariationRangeIndex]['code']))
								{
									$firstVariationDataWithSpecial[$firstVariationRangeIndex]['code'] = '';
									$firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['code'] = '';
								}
								if(isset($firstVariationDataWithSpecial[$firstVariationRangeIndex]['code_orig']))
								{
									$firstVariationDataWithSpecial[$firstVariationRangeIndex]['code_orig'] = '';
									$firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['code_orig'] = '';
								}
								
								// Remove special data from without special array
								unset($firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['rate_orig']);
								unset($firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['expired_at']);
							}
						}
						if(!(empty($firstVariationDataWithSpecial)))
						{
							foreach($this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex] as $variationId => $variationData)
							{
								//echo "<br/><br/><br/><br/>Variation Changed";
								$originalData = $variationData['pricing']['decorative'];
								
								if(isset($this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex][$variationId]['pricing']['decorative'][0]['rate_orig']))
								{
									$this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex][$variationId]['pricing']['decorative'] = $firstVariationDataWithSpecial;
								}
								else
								{
									$this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex][$variationId]['pricing']['decorative'] = $firstVariationDataWithoutSpecial;
								}
								foreach($firstVariationDataWithSpecial as $firstVariationRangeIndex => $firstVariationRange)
								{
									//echo "<br/><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Variation Range Changed";
									foreach($originalData as $originalDataRangeIndex => $originalDataRange)
									{
										//echo "<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;originalDataRange: ".$originalDataRange['from_qty'];
										//echo "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firstVariationRange: ".$firstVariationRange['from_qty'];
										if($originalDataRange['from_qty'] == $firstVariationRange['from_qty'])
										{
											//echo "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We are updating original data";
											$this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex][$variationId]['pricing']['decorative'][$firstVariationRangeIndex] = $originalDataRange;
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		/*
		\Doctrine\Common\Util\Debug::dump($this->responseArray );
		die;*/
		
		$this->responseArray ['instance_type'] = $this->instanceType;
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4532
	 *         Description : Use to search product from solr.
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/search-product-for-quote",
	 *         name="office_brain_quotebundle_quote_searchproductforquote",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:searchProductForQuote.html.twig")
	 */
	public function searchProductForQuoteAction() {
		$this->initAction ();
		return array (
				'ob_id' => substr ( $this->user->getObCustomerId (), 2 )
		);
	}
	
	/**
	 *
	 * @author Employee Id: 4532
	 *         Description : Use to get product info in this quote.
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/get-product-info-to-quote",
	 *         name="office_brain_quotebundle_quote_getproductinfotoquote",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:getProductInfotoQuote.html.twig")
	 */
	public function getProductInfotoQuoteAction() {
		$this->initAction ();

		list($defaultCulture,$defaultCountry) = explode("_",$this->request->get("_locale"));
		$culture = $this->request->get('language',$defaultCulture);
		$activeCulture = $culture;
		$cultureCategory =  $activeCulture . "_" . $country;
		$categoryAndSubCategoryData = $this->categoryApiManager->getList(array($cultureCategory),array(),true);
		$itemLineId = $this->request->get ( 'item_line_id', null );
		$data ['product_detail'] = $this->quoteManager->getProductDetailByItemLine ( $itemLineId );
		
		$data['color_info'] = $this->quoteManager->checkForAttribute($data ['product_detail']['attributes']);
		if($data['color_info']['is_trim_color'] == 1 or $data['color_info']['is_ink_color'] == 1 or $data['color_info']['is_barrel_color'] == 1){
			$data['color_info_val'] = $this->quoteManager->mergeAttributeArray($data['color_info'],$data['product_detail']['attributes']);
		}		
		$categoryAndSubCategoryList = array();
		$categoryAndSubCategoryList = $this->categoryApiManager->getParentChildCategoryList($categoryAndSubCategoryData,$data ['product_detail']['category'],$activeCulture);
		$existValue = 0;
        /*foreach($this->freeFreightAllowCategoryList as $key => $value){ 
         	if(array_key_exists($value,$categoryAndSubCategoryList['parent_id'])){ 
         		$existValue = 1;
         		break;
         	}else{
         		if(count($categoryAndSubCategoryList['parent_id'][$value]['child'])>0){
         			if(array_key_exists($value,$categoryAndSubCategoryList[$value]['child'])){
		   				$existValue = 1;
		   				break;
		    		}
         		}else{
         				$existValue = 0;
         				break;
         		}
         	}	
         }*/	
         $data['dispalyText'] = $existValue;
		$data ['item_line_id'] = $itemLineId;
		


		$firstImprintLocationId = $this->getFirstImprintLocationId($data);
		$firstImprintMethodId = $this->getFirstImprintMethodId($data,$firstImprintLocationId);
		$firstImprintMethodName = $this->getFirstImprintMethodName($data,$firstImprintLocationId);
		$firstMaximumSelectableColor = ($data ['product_detail']['imprint_information']['imprint_method_information'][$firstImprintMethodId]['maximum_selectable_color'] >= 1)?1:0;
		
		$data['first_imprint_location_id'] = $firstImprintLocationId;
		$data['first_imprint_method_id'] = $firstImprintMethodId;
		$data['first_imprint_method_name'] = $firstImprintMethodName;
		$data['first_maximum_selectable_color'] = $firstMaximumSelectableColor;
		
		$data['my_quote']['zero_controller']['html'] = $this->twigManager->render ( 'OfficeBrainQuoteBundle:Quote:getProductInfotoQuote.html.twig', $data);
		
		// render the controller of first ajax
		$data['first_controller']['html']['data'] = array(
				'imprint_methods' => $data ['product_detail']['imprint_methods'],
				'imprint_information' => $data ['product_detail']['imprint_information'],
				'imprint_position_and_methods' => $data ['product_detail']['imprint_position_and_methods'],
				'product_id' => $data ['product_detail']['product_id'],
				'imprint_location_id' => $firstImprintLocationId,
		);
		$data['first_controller']['html']['status'] = 'success';
		
		// render the controller of second ajax
		$data['second_controller']['imprintLocation'] = array(
				'imprint_methods' => $data ['product_detail']['imprint_methods'],
				'imprint_information' => $data ['product_detail']['imprint_information'],
				'imprint_position_and_methods' => $data ['product_detail']['imprint_position_and_methods'],
				'product_id' => $data ['product_detail']['product_id'],
				'imprint_location_id' => $firstImprintLocationId,
		);
		$data['second_controller']['confiSetting'] = $this->productDetailconfigurationRule;
		$data['second_controller']['first_imprint_method_id'] = $firstImprintMethodId;
		$data['second_controller']['first_imprint_method_name'] = $firstImprintMethodName;
		$data['second_controller']['html'] = $this->twigManager->render ( 'OfficeBrainQuoteBundle:Quote:addImprintLocation.html.twig', $data['second_controller']);
		
		
		// render the controller of third ajax
		$standardImprintColor = $this->requestQuoteManager->getStandardImprintColorInformation($data ['product_detail']['product_id']);
		$pmsColor = $this->container->getParameter('pms_color');
		$data['third_controller']['maximum_selectable_color'] = $data ['product_detail']['imprint_information']['imprint_method_information'][$firstImprintMethodId]['maximum_selectable_color'];
		$data['third_controller']['method_name'] = $firstImprintMethodName;
		$data['third_controller']['product_id'] = $data ['product_detail']['product_id'];
		$data['third_controller']['imprint_location_id'] = $firstImprintLocationId;
		$data['third_controller']['first_maximum_selectable_color'] = $firstMaximumSelectableColor;
		$data['third_controller']['standard_imprint_color'] = $standardImprintColor;
		if($data ['product_detail']['imprint_information']['imprint_method_information'][$firstImprintMethodId]['is_pms_color_allow'] == 1) {
			$data['third_controller']['pms_color'] = $pmsColor;
		}
		$data['third_controller']['html'] = $this->twigManager->render ( 'OfficeBrainQuoteBundle:Quote:selectMethod.html.twig', $data['third_controller']);
		
		// render the controller of fourth ajax
		
		$data['fourth_controller'] = array('color_no'=>$firstMaximumSelectableColor
				,'imprint_id'=>$firstImprintLocationId
				,'product_id'=>$data ['product_detail']['product_id']
				,'standard_imprint_color'=> $standardImprintColor
// 				, 'pms_color'=>$pmsColor
		);
		if($data ['product_detail']['imprint_information']['imprint_method_information'][$firstImprintMethodId]['is_pms_color_allow'] == 1) {
			$data['fourth_controller']['pms_color'] = $pmsColor;
		}
		
		$data['fourth_controller']['html'] = $this->twigManager->render ( 'OfficeBrainQuoteBundle:Quote:selectColor.html.twig', $data['fourth_controller']);
		/*echo "<pre>";
		 print_r($data);exit;*/

		return new JsonResponse($data);
	}
	
	public function getFirstImprintLocationId($data)
	{
		$firstImprintLocationId = 0;
		if(isset($data ['product_detail']['imprint_position_and_methods']) && (!(empty($data ['product_detail']['imprint_position_and_methods']))))
		{
			foreach($data ['product_detail']['imprint_position_and_methods'] as $key => $imprint_method)
			{
				//{%  if imprint_method.position_area is defined and imprint_method.position_area|length>0 and ob_project_transaction_configuration_rule.shopping_cart.start_from_imprint_method.status==1 %}
				if(isset($imprint_method['position_area']) && strlen($imprint_method['position_area']) > 0
						&& isset($this->transactionconfigurationRule['shopping_cart']['start_from_imprint_method']['status'])
						&& $this->transactionconfigurationRule['shopping_cart']['start_from_imprint_method']['status'] == 1)
				{
					if($firstImprintLocationId == 0)
					{
						$firstImprintLocationId = $key;
						break;
					}
				}
				else
				{
					if($firstImprintLocationId == 0)
					{
						$firstImprintLocationId = $imprint_method['id'];
						break;
					}
				}
			}
		}
		return $firstImprintLocationId;
	}
	public function getFirstImprintMethodId($data,$firstImprintLocationId)
	{
		$firstImprintMethodId = 0;
		if(isset($data ['product_detail']['imprint_position_and_methods']) && (!(empty($data ['product_detail']['imprint_position_and_methods']))))
		{
			foreach($data ['product_detail']['imprint_position_and_methods'] as $location_id => $imprint_position)
			{
				if($location_id == $firstImprintLocationId)
				{
					if(isset($imprint_position['imprint_method']))
					{
						foreach($imprint_position['imprint_method'] as $id => $method)
						{
							if($firstImprintMethodId == 0)
							{
								$firstImprintMethodId = $id;
								break;
							}
						}
					}
				}
			}
		}
		
		return $firstImprintMethodId;
	}
	public function getFirstImprintMethodName($data,$firstImprintLocationId)
	{
		$firstImprintMethodName = '';
		if(isset($data ['product_detail']['imprint_position_and_methods']) && (!(empty($data ['product_detail']['imprint_position_and_methods']))))
		{
			foreach($data ['product_detail']['imprint_position_and_methods'] as $location_id => $imprint_position)
			{
				if($location_id == $firstImprintLocationId)
				{
					if(isset($imprint_position['imprint_method']))
					{
						foreach($imprint_position['imprint_method'] as $id => $method)
						{
							if($firstImprintMethodName == '')
							{
								$firstImprintMethodName = $method;
								break;
							}
						}
					}
				}
			}
		}
		
		return $firstImprintMethodName;
	}
	
	/**
	 *
	 * @author Employee Id: 4203
	 *         Description : Use to add addtional charges
	 * @param
	 *        	1 : No paramater
	 * @return type : Ajax response.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/add-addtional-charges",
	 *         name="office_brain_quotebundle_quote_addadditionalcharges",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:addAdditionalCharges.html.twig")
	 */
	public function addAdditionalChargesAction() {
		$this->initAction ();
		$status = $this->quoteManager->addAdditionalCharges ();
		if ($status ['status'] == "SUCCESS") {
			$this->responseArray ['data'] = $status ['data'];
		}
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4217
	 *         Description : Use to render customer info form.
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/add-new-customer-to-quote/{quote_id}",
	 *         name="office_brain_quotebundle_quote_addnewcustomertoquote",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:addNewCustomer.html.twig")
	 */
	public function addNewCustomerAction() {
		$this->initAction ();
		$this->form = $this->createForm ( new AddCustomerContactType ( $this->coreManager ) );
		$this->form->handleRequest ( $this->request );
		$this->responseArray ['quote_customer'] = $this->form->createView ();
		$this->responseArray ['quote_id'] = $this->request->get ( 'quote_id', null );
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4493
	 *         Description : Use to render customer info form.
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/edit-customer-to-quote/{user_id}/{quote_id}",
	 *         name="office_brain_quotebundle_quote_editcustomertoquote",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:editCustomerToQuote.html.twig")
	 */
	public function editCustomerToQuoteAction() {
		$this->initAction ();
	
		$quoteId = $this->request->get ( 'quote_id', null );
		$userId = $this->request->get ( 'user_id', null );
		$userObj = $this->getCustomerDetailFromQuote ( $quoteId );
		$photo = $userObj->getPhoto ();
		$this->form = $this->createForm ( new AddCustomerType ( $this->coreManager, 'edit' ), $userObj );
		$this->form->handleRequest ( $this->request );
		$this->responseArray ['edit_quote_customer'] = $this->form->createView ();
		$this->responseArray ['quote_id'] = $quoteId;
		$this->responseArray ['contact_id'] = $userObj->getId ();
		$this->responseArray ['user_id'] = $userId;
		$this->responseArray ['photo'] = $photo;
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4532
	 *         Description : Use to render view to add imprint location box.
	 * @param
	 *        	1 : No paramater
	 * @return type : Ajax response in view.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/quote-add-imprint-location",
	 *         name="office_brain_quotebundle_quote_addimprintlocation",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:addImprintLocation.html.twig")
	 *         @Method({"POST","GET"})
	 */
	public function addImprintLocationAction() {
		$this->initAction ();
		$data = array ();
		$data ['imprintLocation'] = $this->quoteManager->addImprintLocation ();
		$data ['confiSetting'] = $this->productDetailconfigurationRule;
		return $data;
	}
	
	/**
	 *
	 * @author Employee Id: 4532
	 *
	 *         Description : Use to submit guest user quote form.
	 *
	 * @param
	 *        	1 : No paramater
	 *
	 * @return type : Form view and data.
	 *
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/select-method-quote-front",
	 *         name="office_brain_quotebundle_quote_selectMethod",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:selectMethod.html.twig")
	 *         @Method({"POST","GET"})
	 */
	public function selectMethodAction() {
		$this->initAction ();
		return $selectImprintMethod = $this->quoteManager->selectImprintMethod ();
	}
	
	/**
	 *
	 * @author Employee Id: 4532
	 *
	 *         Description : Use to show the list of product in this request quote.
	 *
	 * @param
	 *        	1 : No paramater
	 *
	 * @return type : json
	 *
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/select-color-quote",
	 *         name="office_brain_quotebundle_quote_selectColor",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:selectColor.html.twig")
	 *         @Method({"POST","GET"})
	 */
	public function selectColorAction() {
		$this->initAction ();
		$standardImprintColor = $this->quoteManager->getStandardImprintColorInformation ();
		$pmsColor = $this->container->getParameter ( 'pms_color' );
		$selectColorArray = array (
				'color_no' => $this->request->get ( 'color_no' ),
				'imprint_id' => $this->request->get ( 'imprint_id' ),
				'product_id' => $this->request->get ( 'product_id' ),
				'standard_imprint_color' => $standardImprintColor,
// 				'pms_color' => $pmsColor
		);
		
		$data ['product_detail']=$this->quoteManager->getProductDetailById($this->request->get('product_id'));
		if(isset($data ['product_detail']) && !empty($data ['product_detail'])) {
			$imprintMethodId = $this->getFirstImprintMethodId($data,$this->request->get('imprint_id'));
			if(isset($imprintMethodId) && $imprintMethodId!='') {
				if($data ['product_detail']['imprint_information']['imprint_method_information'][$imprintMethodId]['is_pms_color_allow'] == 1) {
					$selectColorArray['pms_color'] = $pmsColor;
				}
			}
		}
		
		return $selectColorArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4532
	 *         Description : Use to get pms colors as per given hashkey
	 * @param
	 *        	1 : hashkey color code
	 * @return type : Ajax response in view.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/quote-pms-colors",
	 *         name="office_brain_quotebundle_quote_pmscolor",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:quotePmsColors.html.twig")
	 */
	public function quotePmsColorsAction() {
		$this->initAction ();
		$pmsColorValue = $this->quoteManager->getPMSColorInfo ();
		$this->responseArray ['counter'] = $this->request->get ( 'counter', 0 );
		$this->responseArray ['count'] = $pmsColorValue ['data'] ['count'];
		$this->responseArray ['pms_colors_value'] = $pmsColorValue ['data'] ['pmshexcolor'];
		$this->responseArray ['status'] = $pmsColorValue ['status'];
	
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4532
	 *
	 *         Description : Use to set session data for Quote
	 *
	 * @param
	 *        	1 : No paramater
	 *
	 * @return type : json
	 *
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/set-product",
	 *         name="office_brain_quotebundle_quote_setproduct",
	 *         options={"expose"=true}
	 *         )
	 *         @Method({"POST"})
	 */
	public function setProductAction() {
		$this->initAction ();
		$this->validationManager->setMaxNoOfColor ( $this->request->get ( 'max_selectable_color' ) );
		$this->validationManager->setProjectConfiguration ( $this->productDetailconfigurationRule );
		$this->validationManager->setRules ( 'color', 'color', 'color' );
		$this->validationManager->setRules ( 'extra_color', 'extraColor', 'extraColor' );
		$this->validationManager->setRules ( 'imprint', 'printPosition', 'printPosition' );
		$this->validationManager->setRules ( 'fax_proof', 'faxProof', 'printPositionProof' );
		$this->validationManager->setRules ( 'paper_proof', 'paperProof', 'printPositionProof' );
		$this->validationManager->setRules ( 'shipping_method', 'shippingMethod', 'shippingMethod' );
		// $this->validationManager->setRules('special_instruction', 'required','specialInstruction');
		if ($this->validationManager->isValid ()) {
			$status = $this->quoteManager->setProductToQuote ();
			switch ($status ['success']) {
				case 'SUCCESS' :
					if ($this->erpConfigrationField && $this->erpConfigrationField ['orderManagement'] == 1 && ! empty ( $this->queueApiManager )) {
						$this->request->attributes->set ( 'object', 'QuoteItemLine' );
						if ($status ['data']->getQuoteMasterId ()->getQuoteStatus () == 'sent') {
							/*
							 * if($status['data']->getErpId() > 0) { $this->queueApiManager->mapQueue('update', $status['data']); }else{
							 */
							$this->queueApiManager->mapQueue ( 'create', $status ['data'] );
							// }
						}
					}
					$this->responseArray ['status'] = 'success';
					$this->responseArray ['page_url'] = $this->generateUrl ( 'office_brain_quotebundle_quote_quote', array (
							'quote_id' => $status ['quote_id']
					) );
					break;
			}
		} else {
			$this->responseArray ['status'] = 'error';
			$this->responseArray ['error_data'] = $this->validationManager->isError ();
			return new JsonResponse ( $this->responseArray ); // / return validation error array
		}
		return new JsonResponse ( $this->responseArray );
	}
	
	/**
	 *
	 * @author Employee Id: 4532
	 *         Description : Use to edit product in this quote.
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/edit-product-to-quote",
	 *         name="office_brain_quotebundle_quote_editquoteproduct",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:editQuoteProduct.html.twig")
	 */
	public function editQuoteProductAction() {
		$this->initAction ();
		list($defaultCulture,$defaultCountry) = explode("_",$this->request->get("_locale"));
		$culture = $this->request->get('language',$defaultCulture);
		$activeCulture = $culture;
		$cultureCategory =  $activeCulture . "_" . $country;
		$categoryAndSubCategoryData = $this->categoryApiManager->getList(array($cultureCategory),array(),true);
		$itemLineId = $this->request->get ( 'item_line_id', null );
		$this->responseArray ['product_detail'] = $this->quoteManager->getProductDetailByItemLine ( $itemLineId );
		$this->responseArray ['item_line_obj'] = $this->quoteManager->getQuoteInfo ( $itemLineId );
		$this->responseArray ['item_line_id'] = $itemLineId;
		$this->responseArray ['size_allow'] =($this->responseArray ['product_detail']['is_size']) ? 1 : 0 ;
		$this->responseArray ['request_quote_session'] = $this->quoteManager->getInputDataForEdit ( $itemLineId );
		
		$imprint = $this->responseArray ['request_quote_session']['imprint'];
		foreach($imprint as $key=>$imp){
			$impPosition = explode(':',$key);
			$imprintMethodPosition[$imp['imprint_method_id']] = $impPosition[1];
		}
		$this->responseArray ['imprint_method_position'] = $imprintMethodPosition;
		$categoryAndSubCategoryList = array();
		$categoryAndSubCategoryList = $this->categoryApiManager->getParentChildCategoryList($categoryAndSubCategoryData,$this->responseArray ['product_detail']['category'],$activeCulture);
		
 		$existValue = 0;
 		
 		$this->responseArray['color_info'] = $this->quoteManager->checkForAttribute($this->responseArray ['product_detail']['attributes']);
		if($this->responseArray['color_info']['is_trim_color'] == 1 or $this->responseArray['color_info']['is_ink_color'] == 1 or $this->responseArray['color_info']['is_barrel_color'] == 1){
			$this->responseArray['color_info_val'] = $this->quoteManager->mergeAttributeArray($this->responseArray['color_info'],$this->responseArray ['product_detail']['attributes']);
		}
 		
        /*foreach($this->freeFreightAllowCategoryList as $key => $value){ 
         	if(array_key_exists($value,$categoryAndSubCategoryList['parent_id'])){ 
         		$existValue = 1;
         		break;
         	}else{
         		if(count($categoryAndSubCategoryList['parent_id'][$value]['child'])>0){
         			if(array_key_exists($value,$categoryAndSubCategoryList[$value]['child'])){
		   				$existValue = 1;
		   				break;
		    		}
         		}
         	}	
         }*/	
         $this->responseArray['dispalyText'] = $existValue;
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4217
	 *         Description : Use to render Billing Address By Id
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *         @Route("/{_locale}/get-billing-address-by-id/{quote_id}",
	 *         name="office_brain_quotebundle_quote_getbillingaddressbyid",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:getBillingAddressById.html.twig")
	 *         @Method({"GET"})
	 */
	public function getBillingAddressByIdAction() {
		$this->initAction ();
		$quote_id = $this->request->get ( 'quote_id', null );
		$this->responseArray ['billing_address'] = $this->quoteManager->getUserBillingAddresById ( $quote_id );
		$this->responseArray ['quote_id'] = $quote_id;
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4217
	 *         Description : Use to render customer billing info form.
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/edit-customer-billing-to-quote",
	 *         name="office_brain_quotebundle_quote_editcustomerbillingtoquote",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:editCustomerbillingToQuote.html.twig")
	 */
	public function editCustomerbillingToQuoteAction() {
		$this->initAction ();
		$userId = $this->request->get ( 'user_id', null );
		$userObj = $this->getCustomerBillingDetailFromQuote ( $userId );
		$photo = $userObj->getPhoto ();
		$this->form = $this->createForm ( new AddCustomerContactType ( $this->coreManager, 'edit' ), $userObj );
		$this->form->handleRequest ( $this->request );
		$this->responseArray ['edit_quote_billing_customer'] = $this->form->createView ();
		$this->responseArray ['user_id'] = $userId;
		$this->responseArray ['photo'] = $photo;
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4203
	 *         Description : Use to send quote in email and generate duplicate quote for all customer added
	 * @param
	 *        	1 : No paramater
	 * @return type : Ajax response.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/send-quote",
	 *         name="office_brain_quotebundle_quote_send_quote",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:sendQuote.html.twig")
	 */
	public function sendQuoteAction() {
		$this->initAction ();
		$status = $this->quoteManager->sendQuote ();
		switch ($status ['status']) {
			case 'QUOTE_NOT_FOUND' :
				$this->responseArray ['error_message'] = $this->get ( 'translator' )->trans ( 'admin.quote_not_found' );
				$this->responseArray ['error'] = 'QUOTE_NOT_FOUND';
				break;
			case 'PRODUCT_NOT_ADDED' :
				$this->responseArray ['error_message'] = $this->get ( 'translator' )->trans ( 'admin.add_atleast_one_product' );
				$this->responseArray ['error'] = 'PRODUCT_NOT_ADDED';
				break;
			case 'SELECT_CUSTOMER' :
				$this->responseArray ['error_message'] = $this->get ( 'translator' )->trans ( 'admin.select_atleast_one_contact' );
				$this->responseArray ['error'] = 'SELECT_CUSTOMER';
				break;
			case 'SUCCESS' :
				// $status['quoteId']; here you get duplicate quote created object for ERP.
				if ($this->erpConfigrationField && $this->erpConfigrationField ['orderManagement'] == 1 && ! empty ( $this->queueApiManager )) {
					$this->request->attributes->set ( 'object', 'Quote' );
					$quoteObj = $this->quoteManager->isQuoteExists ();
					if ($quoteObj->getQuoteStatus () == 'sent') {
						if ($quoteObj->getErpId () > 0) {
							$this->queueApiManager->mapQueue ( 'update', $quoteObj );
						} else {
							$this->queueApiManager->mapQueue ( 'create', $quoteObj );
						}
						if (isset ( $status ['new_quote_id'] )) {
							foreach ( $status ['new_quote_id'] as $newQuoteId ) {
								$newQuoteObj = $this->quoteManager->isQuoteExists ( $newQuoteId );
								$this->queueApiManager->mapQueue ( 'create', $newQuoteObj );
							}
						}
					}
				}
				$this->responseArray ['status'] = 'success';
				$this->responseArray ['success_message'] = $this->get ( 'translator' )->trans ( 'admin.quote_sent_successfully' );
				$this->responseArray ['goto_url'] = $this->generateUrl ( 'office_brain_quotebundle_quote_quotelist' );
				break;
		}
		return new JsonResponse ( $this->responseArray );
	}
	
	/**
	 *
	 * @author Employee Id:4217
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/preview-quote/{quote_id}/",
	 *         name="office_brain_quotebundle_quote_previewquote",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:previewQuote.html.twig")
	 */
	public function previewQuoteAction() {
		$this->initAction ();
		$quoteDetail = $this->quoteManager->isQuoteExists ();
		$userObj = $this->getCustomerDetailFromQuote ( $quoteDetail->getId () );
		if ($quoteDetail != NULL) {
			$this->responseArray ['data'] = $quoteDetail;
			$this->responseArray ['userData'] = $userObj;
			$this->responseArray ['is_quote_preview_page'] = $this->request->get ( 'is_quote_preview_page', 1 );
			return $this->responseArray;
		} else {
			return $this->redirect ( $this->generateUrl ( 'office_brain_quotebundle_quote_quotelist' ) );
		}
	}
	
	/**
	 *
	 * @author Employee Id: 4248
	 *         Description : Use to get all product list added in this quote.
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/get-all-quote-product-preview/{quote_id}/{is_quote_preview_page}",
	 *         name="office_brain_quotebundle_quote_getproductlistpreview",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:getProductListPreview.html.twig")
	 */
	public function getProductListPreviewAction() {
		$this->initAction ();
		$quoteDetail = $this->quoteManager->isQuoteExists ();
		$this->responseArray ['quote'] = $quoteDetail;
		$this->responseArray ['data'] = $this->quoteManager->prepareQuoteProductList ();
		$this->responseArray ['is_quote_preview_page'] = $this->request->get ( 'is_quote_preview_page', 0 );
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id:
	 *         Description : Use to get all product list added in this quote.
	 * @param
	 *        	1 : No paramater
	 * @return type : Null.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/get-all-quote-product-preview/{quote_id}",
	 *         name="office_brain_quotebundle_quote_getpreviewquoteproductList",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:getPreviewQuoteProductList.html.twig")
	 */
	public function getPreviewQuoteProductListAction() {
		$this->initAction ();
		$quoteDetail = $this->quoteManager->isQuoteExists ();
		$this->responseArray ['quote'] = $quoteDetail;
		$this->responseArray ['data'] = $this->quoteManager->prepareQuoteProductList ();		
		
		foreach($this->responseArray ['data'] as $dataIndex => $dataValues)
		{
			if(isset($this->responseArray ['data'][$dataIndex]['price_type']) && $this->responseArray ['data'][$dataIndex]['price_type']== 'variations')
			{
				if(isset($this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country']))
				{
					foreach($this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'] as $countryIndex=> $countryCode)
					{
						$firstVariationDataWithSpecial = array();
						$firstVariationDataWithoutSpecial= array();
						$count = 0;
						foreach($this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex] as $variationId => $variationData)
						{
							if($count == 0)
							{
								$firstVariationDataWithSpecial = $variationData['pricing']['decorative'];
								$firstVariationDataWithoutSpecial= $variationData['pricing']['decorative'];
							}
							$count = $count + 1;
						}
						// Now update the rate and other things if provided
						if(!(empty($firstVariationDataWithSpecial)))
						{
							foreach($firstVariationDataWithSpecial as $firstVariationRangeIndex => $firstVariationRange)
							{
								if(isset($firstVariationDataWithSpecial[$firstVariationRangeIndex]['rate']))
								{
									$firstVariationDataWithSpecial[$firstVariationRangeIndex]['rate'] = 'N/A';
									$firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['rate'] = 'N/A';
								}
								if(isset($firstVariationDataWithSpecial[$firstVariationRangeIndex]['rate_orig']))
								{
									$firstVariationDataWithSpecial[$firstVariationRangeIndex]['rate_orig'] = 'N/A';
									$firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['rate_orig'] = 'N/A';
								}
								if(isset($firstVariationDataWithSpecial[$firstVariationRangeIndex]['code']))
								{
									$firstVariationDataWithSpecial[$firstVariationRangeIndex]['code'] = '';
									$firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['code'] = '';
								}
								if(isset($firstVariationDataWithSpecial[$firstVariationRangeIndex]['code_orig']))
								{
									$firstVariationDataWithSpecial[$firstVariationRangeIndex]['code_orig'] = '';
									$firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['code_orig'] = '';
								}
								
								// Remove special data from without special array
								unset($firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['rate_orig']);
								unset($firstVariationDataWithoutSpecial[$firstVariationRangeIndex]['expired_at']);
							}
						}
						if(!(empty($firstVariationDataWithSpecial)))
						{
							foreach($this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex] as $variationId => $variationData)
							{
								//echo "<br/><br/><br/><br/>Variation Changed";
								$originalData = $variationData['pricing']['decorative'];
								
								if(isset($this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex][$variationId]['pricing']['decorative'][0]['rate_orig']))
								{
									$this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex][$variationId]['pricing']['decorative'] = $firstVariationDataWithSpecial;
								}
								else
								{
									$this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex][$variationId]['pricing']['decorative'] = $firstVariationDataWithoutSpecial;
								}
								foreach($firstVariationDataWithSpecial as $firstVariationRangeIndex => $firstVariationRange)
								{
									//echo "<br/><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Variation Range Changed";
									foreach($originalData as $originalDataRangeIndex => $originalDataRange)
									{
										//echo "<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;originalDataRange: ".$originalDataRange['from_qty'];
										//echo "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firstVariationRange: ".$firstVariationRange['from_qty'];
										if($originalDataRange['from_qty'] == $firstVariationRange['from_qty'])
										{
											//echo "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We are updating original data";
											$this->responseArray ['data'][$dataIndex]['variation_pricing_for_other_country'][$countryIndex][$variationId]['pricing']['decorative'][$firstVariationRangeIndex] = $originalDataRange;
										}
									}
								}
							}
						}
					}
				}
			}			
		}
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4532
	 *         Desciption: This function allow user to download quote preview.
	 * @param
	 *        	integer quoteId
	 * @return download file on requested formate
	 * @throws NULL @Route("/{_locale}/quote-product-download/{quote_id}",
	 *         name="office_brain_quotebundle_quote_downloadquoteproduct",
	 *         options={"expose"=true}
	 *         )
	 *
	 *         @Method({"GET"})
	 *
	 *         @Template()
	 *
	 */
	public function downloadQuoteProductAction() {
		$this->initAction ();
	
		$quoteDetail = $this->quoteManager->isQuoteExists ();
		$downloadType = $this->request->get ( 'downloadType' );
		$downloadExt = ($downloadType == 'image') ? 'jpg' : $downloadType;
		$userObj = $this->getCustomerDetailFromQuote ( $quoteDetail->getId () );
		if ($quoteDetail != NULL) {
			$this->responseArray ['data'] = $quoteDetail;
			$this->responseArray ['userData'] = $userObj;
			$response = $this->render ( 'OfficeBrainQuoteBundle:Quote:downloadQuoteProduct.html.twig', $this->responseArray );
			 // echo $response->getContent(); exit;
			if ($quoteDetail->getQuoteTitle () != '') {
				$quoteName = str_replace ( ' ', '_', $quoteDetail->getQuoteTitle () );
			} else {
				$quoteName = $quoteDetail->getId ();
			}
				
			// emp_id: 5460
			// remove the following comment and run quote pdf url in following format for debugging
			// http://172.16.120.75/5460/ob-product-web-client/web/app_dev.php/en_no/quote-product-download/42
			// return new Response ( $response->getContent ());
			$quoteFile = $quoteName . '.' . $downloadExt;
			if ($downloadType == 'html') {
				return new Response ( $response->getContent (), 200, array (
						'Content-Type' => 'application/' . $downloadType,
						'Content-Disposition' => 'attachment; filename="' . $quoteFile . '"',
						'Pragma' => '0',
						'Expires' => '0'
				) );
			} else {			
				
				$content = $response->getContent();
				$baseURL = $this->container->getParameter('base_url');
				$basePath = $this->container->getParameter('base_path');
				$content = str_replace($baseURL,$basePath,$content);
				
				$this->get( 'knp_snappy.' . $downloadType)->getInternalGenerator()->setTimeout(300);
				return new Response ( $this->get ( 'knp_snappy.' . $downloadType )->getOutputFromHtml ( $content), 200, array (
						'Content-Type' => 'application/' . $downloadType,
						'Content-Disposition' => 'attachment; filename="' . $quoteFile . '"'
				) );
			}
		} else {
			$quoteId = $this->request->get ( 'quote_id' );
			return $this->redirect ( $this->generateUrl ( 'office_brain_quotebundle_quote_createquote', array (
					'quote_id' => $quoteId
			) ) );
		}
	}
	
	/**
	 *
	 * @author Employee Id: 4532
	 *         Description : Use to render Shipping Address By Id
	 * @param
	 *        	1 : No paramater
	 * @return type : Ajax response in view.
	 * @throws Exception: Null
	 *
	 *         @Route("/{_locale}/get-shipping-address-by-id",
	 *         name="office_brain_quotebundle_quote_getshippingaddressbyid",
	 *         options={"expose"=true}
	 *         )
	 *         @Template("OfficeBrainQuoteBundle:Quote:getShippingAddressById.html.twig")
	 *         @Method({"GET"})
	 */
	public function getShippingAddressByIdAction() {
		$this->initAction ();
		$this->response ['result'] = $this->quoteManager->getUserShippingAddresById ();
		$this->response ['addressId'] = $this->request->get ( 'contact_id', null );
		return $this->response;
	}
	
	public function getErrorsAsArray($form)
	{
		$errors = array();
		foreach ($form->getErrors() as $error) {
			$errors[] = $error->getMessage();
		}
	
		foreach ($form->all() as $key => $child) {
			if ($err = $this->getErrorsAsArray($child)) {
				$errors[$key] = $err;
			}
		}
	
		return $errors;
	}
	
	/**
	 * @author Employee Id: 410175
	 * Description : use to render Select Logo Block
	 * @param 1 : No paramater
	 * @return type : Ajax response in view.
	 * @throws Exception: Null
	 *
	 * @Route("/{_locale}/quote-select-logo-block",
	 * name="office_brain_quotebundle_quote_selectlogoblock",
	 * options={"expose"=true}
	 * )
	 * @Template("OfficeBrainQuoteBundle:Quote:selectLogoBlock.html.twig")
	 * @Method({"GET"})
	 */
	public function selectLogoBlockAction()
	{
		$this->initAction();
		if( $this->userId==0){
			if ($this->request->isXmlHttpRequest()) {
				$this->responseArray['error_type'] = 'loginError';
				$this->responseArray['page_url'] = $this->generateUrl('office_brain_userbundle_login_login');
				return new JsonResponse($this->responseArray);
			}else{
				$this->get('session')->set('_security.main.target_path', $this->generateUrl($this->request->get('_route')));
				return $this->redirect($this->generateUrl('office_brain_userbundle_login_login'));
			}
		}
		else
		{
			$page=$this->request->get('page',1);
			$recordPerPage = $this->request->get('recordperpage',12);
			$this->response ['logo_id'] = $this->request->get ( 'productsku' );
			$this->response ['callFrom'] = $this->request->get ( 'productName' );
			//$this->response['record_set']=$this->imageLibraryManager->getMediaLibraryRecords();
			$this->response['record_set']=$this->imageLibraryManager->getMediaLibraryRecordsWithPagination(null,$page,$recordPerPage=9);
	
			return $this->response;
		}
	}
	
	/**
	 * @author Employee Id: 4471
	 * Description : use to copy image for art work
	 * @param 1 : No paramater
	 * @return type : Ajax response in image successfully uploaded
	 * @throws Exception: Null
	 *
	 * @Route("/{_locale}/order-upload-artwork-from-gallery",
	 * name="office_brain_quotebundle_quote_uploadartworkfromgallery",
	 * options={"expose"=true}
	 * )
	 * @Template("OfficeBrainQuoteBundle:Quote:uploadArtWorkFromGallery.html.twig")
	 * @Method({"POST"})
	 */
	public function uploadArtWorkFromGalleryAction()
	{
		$this->initAction();
		$logoId= $this->request->get('logoId');
		$originalImageName= $this->request->get('imageName');
		$imageUpload=$this->imageLibraryManager->copyMedia($this->mediaFilePath, $this->artworkFilePath ,$originalImageName,$originalImageName);
		if($imageUpload != '')
		{
			$this->response['logoId']=$logoId;
			$this->response['uploadedImageName']= $this->artworkFileURL.'/'.$imageUpload;
			$this->response['status']='success';
			$this->response['success_message']= $this->get('translator')->trans('order.print_position.artwork.upload_image_sucess');
		}
		else
		{
			$this->response['logoId']=$logoId;
			$this->response['sucess']='error';
		}
		return new JsonResponse($this->response);
	}
	
	
}
