<?php

namespace OfficeBrain\CustomBundle\OrderBundle\Entity;

use Doctrine\ORM\EntityRepository;
use OfficeBrain\Bundle\OrderBundle\Entity\OrderMasterRepository as BaseOrderMasterRepository;

/**
 * OrderMasterRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class OrderMasterRepository extends BaseOrderMasterRepository
{
	protected $preCartIdsWithOrderItemLineIds;
	public function getQueryBuilder()
	{
		return $this->getEntityManager()->getRepository('OfficeBrainCustomBundleOrderBundle:OrderMaster')->createQueryBuilder('om');
	}
	
	/**
	 * 4248
	 * To push all request quote data in database using TCL
	 */
	public function setOrderData($productDetail,$productInput)
	{   
		$orderMaster = new OrderMaster();
		$orderMaster->setUserId($this->userId);
		$orderMaster->setUserType($this->userType);
		$orderMaster->setInstanceId($this->instanceId);
		$orderMaster->setInstanceType($this->instanceType);
		$orderMaster->setPrice($productInput['paymentAmount']);
		$orderMaster->setTaxTotal($productInput['tax_total']);
		$orderMaster->setCustomerNumber(isset($productInput['customerNumber'])?$productInput['customerNumber']:NULL);
		$orderMaster->setPoNumber(isset($productInput['poNumber'])?$productInput['poNumber']:NULL);
		$orderMaster->setTransactionId($productInput['transactionId']);
		$orderMaster->setPaymentMethod($productInput['paymentmethod']);
		$orderMaster->setOrderNo($productInput['order_no']);
		$orderMaster->setPrefferedDistributorId($productInput['pref_dist']);
        $orderMaster->setCulture($this->cultureCode);
        $orderMaster->setErpId($this->erpId);
		$this->em->getConnection()->beginTransaction();
		try
		{
			$this->em->persist($orderMaster);
			$this->em->flush();
			// One to One
			$this->setOrderBillingDetail($orderMaster);
			// One to Many
			$this->setOrderItemLine($orderMaster, $productDetail,$productInput);
			$this->em->getConnection()->commit();
		}
		catch (\Exception $e)
		{
			$this->em->getConnection()->rollback();
			$this->em->close();
	
			throw $e;
		}
		return array("status"=>"success","data"=>$orderMaster, "pre_cart_ids_with_order_item_line_ids" => $this->preCartIdsWithOrderItemLineIds); 
	}
	
	public function setOrderBillingDetail($orderMaster){
		$ob_city_dropdown = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['city']['option']['dropdown']['status'];
		$ob_city_textbox = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['city']['option']['textbox']['status'];
		$ob_state_dropdown = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['state']['option']['dropdown']['status'];
		$ob_state_textbox = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['state']['option']['textbox']['status'];
		$ob_country_dropdown = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['country']['option']['dropdown']['status'];
		$ob_country_textbox = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['country']['option']['textbox']['status'];
		
		$orderBilling = new OrderBillingDetail();
		$orderBilling->setOrderMasterId($orderMaster);
		$orderBilling->setContactbookId($this->billingAddress->getId());
		$orderBilling->setName($this->billingAddress->getName());
		$orderBilling->setEmail($this->billingAddress->getEmail());
		$orderBilling->setStreet1($this->billingAddress->getStreet1());
		$orderBilling->setStreet2($this->billingAddress->getStreet2());
		
		if($ob_country_dropdown){
			$orderBilling->setCountryId(((int) $this->billingAddress->getCountryId()));
		}
		if($ob_country_textbox){
			$orderBilling->setCountryName($this->billingAddress->getCountryName());
		}
		
		if($ob_state_dropdown){
			$orderBilling->setStateId(((int) $this->billingAddress->getStateId()));
		}
		if($ob_state_textbox){
			$orderBilling->setStateName($this->billingAddress->getStateName());
		}

		if($ob_city_dropdown){
			$orderBilling->setCityId(((int) $this->billingAddress->getCityId()));
		}
		if($ob_city_textbox){
			$orderBilling->setCityName($this->billingAddress->getCityName());
		}
		
		$orderBilling->setZipCode($this->billingAddress->getZipCode());
		$orderBilling->setExtension($this->billingAddress->getExtension());
		$orderBilling->setPhone($this->billingAddress->getPhone());
		$orderBilling->setMobile($this->billingAddress->getMobile());
		$orderBilling->setFax($this->billingAddress->getFax());
		$orderBilling->setWebsite($this->billingAddress->getWebsite());
		$orderBilling->setCompanyName($this->billingAddress->getCompanyName());
		$orderBilling->setCompanyNumber($this->billingAddress->getCompanyNumber());
		$this->em->persist($orderBilling);
		$this->em->flush();
		$orderMaster->setOrderBillingDetail($orderBilling);
	}
	
	private function getExtraColorAttr($extraColor){
		$result = array();
		foreach($extraColor as $key=>$val){
			if($val['color'] != ''){
				$colorId = str_replace('id:','',$key);
				$result[$colorId] = $val['color']; 
			}
		}
		$serRes = '';
		if(!empty($result)){
			$serRes = serialize($result);
		}
		return $serRes;
	}
	
	public function setOrderItemLine($orderMaster, $productDetail, $productInput){
		if(count($productInput)>0){
			foreach($productInput['productData'] as $key=> $data){
				$orderItemLine = new OrderItemLine();
				$orderItemLine->setOrderMasterId($orderMaster);
				$orderItemLine->setProductId($data['product_id']);
				if($productInput[$data['cart_id']]['paper_proof'] !='null' && count($productInput[$data['cart_id']]['paper_proof'])>0 && $productInput[$data['cart_id']]['paper_proof']['status']==1) {
					$orderItemLine->setIsPaperProof(true);
					$orderItemLine->setEmailForPaperProof($productInput[$data['cart_id']]['paper_proof']['email']);
				}else{
					$orderItemLine->setIsPaperProof(false);
				}
	
				if($productInput[$data['cart_id']]['fax_proof']!='null' && count($productInput[$data['cart_id']]['fax_proof'])>0 && $productInput[$data['cart_id']]['fax_proof']['status']==1) {
					$orderItemLine->setIsFaxProof(true);
					$orderItemLine->setFaxnumberForPaperProof($productInput[$data['cart_id']]['fax_proof']['fax_no']);
				}else{
					$orderItemLine->setIsFaxProof(false);
				}
	
				if($productInput[$data['cart_id']]['product_proof']!='null' && count($productInput[$data['cart_id']]['product_proof'] && $productInput[$data['cart_id']]['product_proof']['status']==1)>0) {
					$orderItemLine->setIsProductProof(true);
				}else{
					$orderItemLine->setIsProductProof(false);
				}
				$orderItemLine->setSupplierId($productDetail[$data['cart_id']]['supplier']['supplier_id']);
				$orderItemLine->setCurrencyId($productDetail[$data['cart_id']]['currency']['id']);
				$orderItemLine->setCurrencyName($productDetail[$data['cart_id']]['currency']['name']);
				$orderItemLine->setCurrency($productDetail[$data['cart_id']]['currency']['currency']);
				$orderItemLine->setCurrencySymbol($productDetail[$data['cart_id']]['currency']['symbol']);
				$orderItemLine->setProductSku($productDetail[$data['cart_id']]['basic_information']['sku_number']);
				$orderItemLine->setProductSlug($productDetail[$data['cart_id']]['basic_information']['slug']);
				$orderItemLine->setProductThumbImage($productDetail[$data['cart_id']]['basic_information']['default_image_thumb_url']);
				$orderItemLine->setProductImage($productDetail[$data['cart_id']]['basic_information']['default_image_url']);
				$orderItemLine->setProductNote($data['special_instrution']);
				$orderItemLine->setIsComplete(true);
				$orderItemLine->setOrderType($data['order_type']);
				$orderItemLine->setItemLineAmount($data['amount']);
				$orderItemLine->setItemQtyTotal($data['total_qty']);
				$orderItemLine->setUnitPrice($data['unit_price']);
				$orderItemLine->setPriceUnit($productDetail[$data['cart_id']]['price_unit']);
				$orderItemLine->setIsFeedbackPosted(false);
				if(isset($data['tax']))
				{
					$orderItemLine->setVatTax($data['tax']['vat']);
					$orderItemLine->setVatType($data['tax']['vat_unit']);
				}
				$orderItemLine->setProductDetail(base64_encode(json_encode($productDetail[$data['cart_id']])));
				$orderItemLine->setproductDetailConfiguration(base64_encode(serialize($data['productDataArray'])));
				$extraColor = $this->getExtraColorAttr($productInput[$data['cart_id']]['extra_color']);
				$orderItemLine->setExtraColor($extraColor);
				$this->em->persist($orderItemLine);
				$this->em->flush();
				$orderMaster->addOrderItemLine($orderItemLine);
				$this->preCartIdsWithOrderItemLineIds[$data['cart_id']] = $orderItemLine->getId();

				//OneToMany
	
				$this->setOrderProduct($orderItemLine,$productDetail, $data);
				//OneToMany
				if(count($data['charges'])>0){
					$this->setOrderProductCharges($orderItemLine, $productDetail, $data['charges']);
				}
				//OneToMany
				if(count($data['imprint'])>0 ){
					$this->setOrderProductImprintArea($orderItemLine, $productDetail, $data['imprint']);
				}
			}
		}
	}
	
	public function setOrderProductCharges($orderItemLine, $productDetail, $productInput)
	{
		if(count($productInput)>0){
			foreach($productInput as $data)
			{
				$orderProductCharges = new OrderProductCharges();
				$orderProductCharges->setOrderItemLineId($orderItemLine);
				$orderProductCharges->setChargesType($data['charges_type']);
				$orderProductCharges->setChargesAmount($data['charges_amount']);
				$this->em->persist($orderProductCharges);
				$this->em->flush();
				$orderItemLine->addOrderProductCharge($orderProductCharges);
			}
		}
	}
	
	public function setOrderProductImprintArea($orderItemLine, $productDetail, $productInput)
	{
		if(count($productInput)>0){
			foreach($productInput as $data)
			{
				$orderProductImprintArea = new OrderProductImprintArea();
				$orderProductImprintArea->setOrderItemLineId($orderItemLine);
				$orderProductImprintArea->setImprintAreaId($data['imprint_area_id']);
				$orderProductImprintArea->setImprintAreaMethodId($data['imprint_method_id']);
				$orderProductImprintArea->setImprintAreaNoofColor($data['no_of_color']);
				$this->em->persist($orderProductImprintArea);
				$this->em->flush();
				$orderItemLine->addOrderProductImprintArea($orderProductImprintArea);
	
				$this->setOrderProductImprintColor($orderProductImprintArea, $productDetail, $data['imprint_color']);
				$this->setOrderProductImprintTextArtWork($orderProductImprintArea, $productDetail, $data['imprint_art_work_detail']);
			}
		}
	
	}
	
	public function setOrderProductImprintColor($orderProductImprintArea, $productDetail, $saveData)
	{
	
		if(count($saveData)>0){
			foreach($saveData as $rqpica)
			{
				$orderProductImprintColor = new OrderProductImprintColor();
				$orderProductImprintColor->setImprintAreaId($orderProductImprintArea);
				$orderProductImprintColor->setIsPmsColor($rqpica['is_pms']);
				if($rqpica['is_pms']==1 && $this->pms==1){
					$orderProductImprintColor->setColorName($rqpica['pms_color']['pms_color_code']);
					$orderProductImprintColor->setColorId($rqpica['pms_color']['pms_color_hashkey']);
				}else{
					$orderProductImprintColor->setColorName(isset($rqpica['color_code'])?$rqpica['color_code']:'');
					$orderProductImprintColor->setColorId($rqpica['color_id']);
				}
				$orderProductImprintColor->setPrice(isset($rqpica['price'])?$rqpica['price']:'0.00');
				$this->em->persist($orderProductImprintColor);
				$this->em->flush();
				$orderProductImprintArea->addOrderProductImprintColor($orderProductImprintColor);
			}
		}
	}
	
	public function setOrderProductImprintTextArtWork($orderProductImprintArea, $productDetail, $saveData)
	{
		if(count($saveData)>0){
			foreach($saveData as $rqpita)
			{
				if($rqpita['mail_sent']==0)
				{
					if($rqpita['active_tab_id'] == 1 || $rqpita['active_tab_id'] == 3)
					{
						if(isset($rqpita['logo']) && count($rqpita['logo'])>0)
						{
							foreach ($rqpita['logo'] as $artworklogo)
							{
								$logoName=substr(strrchr($artworklogo,'/'),strpos(strrchr($artworklogo,'/'),'/')+1);
								$logoExtension=substr($logoName,strpos($logoName,'.')+1);
								$OrderProductImprintTextArtWork = new OrderProductImprintTextArtWork();
								$OrderProductImprintTextArtWork->setImprintAreaId($orderProductImprintArea);
								$OrderProductImprintTextArtWork->setImprintText('');
								$OrderProductImprintTextArtWork->setLogoName(isset($logoName)?$logoName:'');
								$OrderProductImprintTextArtWork->setLogoExtension(isset($logoExtension)?$logoExtension:'');
								$OrderProductImprintTextArtWork->setLogoPath(isset($artworklogo)?$artworklogo:'');
								$OrderProductImprintTextArtWork->setIsLogoSendAfterOrder('0');
								$OrderProductImprintTextArtWork->setEmailForLogo('');
								$this->em->persist($OrderProductImprintTextArtWork);
								$this->em->flush();
								$orderProductImprintArea->addOrderProductImprintTextArtWork($OrderProductImprintTextArtWork);
							}
						}
					}
					if ($rqpita['active_tab_id'] == 2 || $rqpita['active_tab_id'] == 3)
					{
						if(count($rqpita['text'])>0)
						{
							foreach ($rqpita['text'] as $artworktext)
							{
								$OrderProductImprintTextArtWork = new OrderProductImprintTextArtWork();
								$OrderProductImprintTextArtWork->setImprintAreaId($orderProductImprintArea);
								$OrderProductImprintTextArtWork->setImprintText(isset($artworktext)?$artworktext:'');
								$OrderProductImprintTextArtWork->setLogoName('');
								$OrderProductImprintTextArtWork->setLogoExtension('');
								$OrderProductImprintTextArtWork->setLogoPath('');
								$OrderProductImprintTextArtWork->setIsLogoSendAfterOrder('0');
								$OrderProductImprintTextArtWork->setEmailForLogo('');
								$this->em->persist($OrderProductImprintTextArtWork);
								$this->em->flush();
								$orderProductImprintArea->addOrderProductImprintTextArtWork($OrderProductImprintTextArtWork);
							}
						}
					}
				}
				else
				{
	
					$OrderProductImprintTextArtWork = new OrderProductImprintTextArtWork();
					$OrderProductImprintTextArtWork->setImprintAreaId($orderProductImprintArea);
					$OrderProductImprintTextArtWork->setImprintText('');
					$OrderProductImprintTextArtWork->setLogoName('');
					$OrderProductImprintTextArtWork->setLogoExtension('');
					$OrderProductImprintTextArtWork->setLogoPath('');
					$OrderProductImprintTextArtWork->setIsLogoSendAfterOrder($rqpita['mail_sent']);
					$OrderProductImprintTextArtWork->setEmailForLogo(isset($rqpita['email'])?$rqpita['email']:'');
					$this->em->persist($OrderProductImprintTextArtWork);
					$this->em->flush();
					$orderProductImprintArea->addOrderProductImprintTextArtWork($OrderProductImprintTextArtWork);
				}
	
			}
		}
	}
	
	public function setOrderProduct($orderItemLine, $productDetail, $productInput)
	{
		if(array_key_exists('split',$productInput)){
			foreach($productInput['split'] as $key=>$data){
				$data['shipping_detail']['carrier_type']=($data['shipping_detail']['shipping_type']!='') ? $data['shipping_detail']['shipping_type']:NULL;
				$data['shipping_detail']['shipping_type']=$productInput['shipping_type'];
				$orderProduct = new OrderProduct();
				$orderProduct->setOrderItemLineId($orderItemLine);
				$orderProduct->setOrderId($orderItemLine->getOrderMasterId()->getId());
				$orderProduct->setProductId($data['product_id']);
				$orderProduct->setCultureProductId($data['culture_product_id']);
				$this->em->persist($orderProduct);
				$this->em->flush();
				$orderItemLine->addOrderProduct($orderProduct);
					
				//OneToOne
				$this->setOrderShippingDetail($orderProduct, $data);
				//OneToMany
				$this->setOrderVariationMaster($orderProduct, $data['variation_master']);
			}
		}
	}
	
	public function setOrderVariationMaster($orderProduct, $variationData)
	{
		if(count($variationData)>0){
			foreach($variationData as $key=>$data){
				$orderVariationMaster = new OrderVariationMaster();
				$orderVariationMaster->setOrderProductId($orderProduct);
				$orderVariationMaster->setAttributeId($data['attribute_id']);
				$orderVariationMaster->setAttributeValueId($data['attribute_value_id']);
				if (isset($data['erp_id'])) {
					$orderVariationMaster->setErpId($data['erp_id']);
				}
				$this->em->persist($orderVariationMaster);
				$this->em->flush();
				$orderProduct->addOrderVariationMaster($orderVariationMaster);
	
				$this->setOrderVariationMapping($orderVariationMaster,$data);
	
			}
		}
	}
	
	public function setOrderVariationMapping($orderVariationMaster,$variationData)
	{
		$orderVariationMapping = new OrderVariationMapping();
		$orderVariationMapping->setVariationMasterId($orderVariationMaster);
		$this->em->persist($orderVariationMapping);
		$this->em->flush();
		$orderVariationMaster->setOrderVariationMapping($orderVariationMapping);
		if(array_key_exists('variation_detail',$variationData)){
			$this->setOrderVariationDetail($orderVariationMapping,$variationData['variation_detail']);
		}
		if(array_key_exists('variation_quantity',$variationData)){
			$this->setOrderVariationQuantity($orderVariationMapping,$variationData['variation_quantity']);
		}
	}
	
	public function setOrderVariationDetail($orderVariationMapping,$variationData)
	{
		if(count($variationData)>0){
			foreach($variationData as $varData){
				foreach($varData as $key=>$data){
					$orderVariationDetail = new OrderVariationDetail();
					$orderVariationDetail->setVariationMappingId($orderVariationMapping);
					$orderVariationDetail->setAttributeId($data['attribute_id']);
					$orderVariationDetail->setAttributeValueId($data['attribute_value_id']);
					if (isset($data['erp_id'])) {
						$orderVariationDetail->setErpId($data['erp_id']);
					}
					$this->em->persist($orderVariationDetail);
					$this->em->flush();
					$orderVariationMapping->addOrderVariationDetail($orderVariationDetail);
				}
			}
		}
	}
	
	public function setOrderVariationQuantity($orderVariationMapping,$variationData)
	{
		if(count($variationData)>0){
			foreach($variationData as $data){
				if($data['quantity']!="" and $data['quantity']!=NULL){
					$orderVariationQuantity = new OrderVariationQuantity();
					$orderVariationQuantity->setVariationMappingId($orderVariationMapping);
					$orderVariationQuantity->setQuantity($data['quantity']);
					$orderVariationQuantity->setUnitPrice($data['unit_price']);
					$this->em->persist($orderVariationQuantity);
					$this->em->flush();
					$orderVariationMapping->addOrderVariationQuantity($orderVariationQuantity);
				}
			}
		}
	}
	
	public function setOrderShippingDetail($orderProduct, $data)
	{
		$ob_city_dropdown = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['city']['option']['dropdown']['status'];
		$ob_city_textbox = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['city']['option']['textbox']['status'];
		$ob_state_dropdown = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['state']['option']['dropdown']['status'];
		$ob_state_textbox = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['state']['option']['textbox']['status'];
		$ob_country_dropdown = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['country']['option']['dropdown']['status'];
		$ob_country_textbox = $this->projectSetting['configuration_rule']['site_management']['option']['address']['option']['country']['option']['textbox']['status'];
		
		$orderShippingDetail = new OrderShippingDetail();
		$orderShippingDetail->setOrderProductId($orderProduct);
		$orderShippingDetail->setCarrierType($data['shipping_detail']['carrier_type']);
		$orderShippingDetail->setSelectedAddressId($data['shipping_detail']['selected_address_id']);
		$orderShippingDetail->setShippingType($data['shipping_detail']['shipping_type']);
		$orderShippingDetail->setShipperAccount($data['shipping_detail']['shipper_account']);
		$orderShippingDetail->setServiceType($data['shipping_detail']['service_type']);
		$orderShippingDetail->setShippingCharge($data['shipping_detail']['shipping_charge']);
		$orderShippingDetail->setName($data['shipping_detail']['name']);
		$orderShippingDetail->setEmail($data['shipping_detail']['email']);
		$orderShippingDetail->setStreet1($data['shipping_detail']['street1']);
		$orderShippingDetail->setStreet2($data['shipping_detail']['street2']);
		
		if($ob_country_dropdown){
			$orderShippingDetail->setCountryName($data['shipping_detail']['country_id']);
		}
		if($ob_country_textbox){
			$orderShippingDetail->setCountryName($data['shipping_detail']['countryname']);
		}
		
		if($ob_state_dropdown){
			$orderShippingDetail->setStateName($data['shipping_detail']['state_id']);
		}
		if($ob_state_textbox){
			$orderShippingDetail->setStateName($data['shipping_detail']['statename']);
		}
		
		if($ob_city_dropdown){
			$orderShippingDetail->setCityName($data['shipping_detail']['city_id']);
		}
		if($ob_city_textbox){
			$orderShippingDetail->setCityName($data['shipping_detail']['cityname']);
		}
		
		$orderShippingDetail->setCountryId(((int)$data['shipping_detail']['countryid']));
		$orderShippingDetail->setStateId(((int)$data['shipping_detail']['stateid']));
		$orderShippingDetail->setCityId(((int)$data['shipping_detail']['cityid']));
		$orderShippingDetail->setZipCode($data['shipping_detail']['zip_code']);
		$orderShippingDetail->setExtension($data['shipping_detail']['extension']);
		$orderShippingDetail->setPhone($data['shipping_detail']['phone']);
		$orderShippingDetail->setMobile($data['shipping_detail']['mobile']);
		$orderShippingDetail->setFax($data['shipping_detail']['fax']);
		$orderShippingDetail->setWebsite($data['shipping_detail']['website']);
		$orderShippingDetail->setCompanyName($data['shipping_detail']['company_name']);
		$orderShippingDetail->setCompanyNumber($data['shipping_detail']['company_number']);
	
	
		if ($data['shipping_detail']['on_hand_date'] != null && $data['shipping_detail']['on_hand_date'] != 'null' && $data['shipping_detail']['on_hand_date'] != '') {
			$orderShippingDetail->setOnHandDate(new \DateTime($data['shipping_detail']['on_hand_date']));
		}
		if ($data['shipping_detail']['ship_date'] != null && $data['shipping_detail']['ship_date'] != 'null' && $data['shipping_detail']['ship_date'] != '') {
			$orderShippingDetail->setShipDate(new \DateTime($data['shipping_detail']['ship_date']));
		}
		if(isset($data['tax_shipping_wise']) && $data['tax_shipping_wise'] != '') {
			$orderShippingDetail->setTax($data['tax_shipping_wise']);
		}
	
		$this->em->persist($orderShippingDetail);
		$this->em->flush();
		$orderProduct->setOrderShippingDetail($orderShippingDetail);
	}
	
	public function isAdminOrderExists($orderId)
	{
		$queryBuilder = $this->getQueryBuilder();
		$queryBuilder->where('om.id = :orderId');
		$queryBuilder->setParameter('orderId', $orderId);
		return $queryBuilder->getQuery()->getOneOrNullResult();
	}
	
	public function getOrderList($userId,$searchParameters,$searchKeyWordsArray,$page,$recordsPerPage)
	{
		$queryBuilder = $this->createQueryBuilder('om')
		->where('om.deletedAt IS NULL AND om.instanceId = :instanceId AND om.userId = :userId')
		->setParameter('instanceId', $this->instanceId)
		->setParameter('userId', $this->userId);
		
		if (isset($searchKeyWordsArray) && !empty($searchKeyWordsArray))
		{
			foreach ($searchKeyWordsArray as $key=>$val) {
				if ($key != '') {
					$param = 'order'.$key;
					$queryBuilder->andWhere('om.'.$key.' LIKE :'.$param)->setParameter($param, '%'. $val .'%');
				}
			}
		}
		
		if (isset($searchParameters) && !empty($searchParameters))
		{
			if ($searchParameters['start_date'] != '' && $searchParameters['end_date'] != '')
			{
				$start_date_obj = date('Y-m-d', strtotime($searchParameters['start_date']));
				$end_date_obj = date('Y-m-d', strtotime($searchParameters['end_date']));
				
				$queryBuilder->andWhere("DATE_FORMAT(om.createdAt, '%Y-%m-%d') BETWEEN :startDate AND :endDate")
				->setParameter('startDate', $start_date_obj)
				->setParameter('endDate', $end_date_obj);
			}
		}
		
		$queryBuilder->orderBy('om.id', 'DESC');
		return $this->paginatorService->paginate($queryBuilder, $page, $recordsPerPage);
		//return $queryBuilder->getQuery()->getResult();
	}
	
}
