<?php

namespace OfficeBrain\CustomBundle\ServiceGroupBundle\Entity;

/**
 * PromoCouponRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class _PromoCouponRepository extends \Doctrine\ORM\EntityRepository
{
	protected $projectSetting;
	protected $instanceId;
	protected $cultureId;
	protected $cultureCode;
	protected $userId;
	/**
	 * @author Employee Id: 5497
	 * Description : Set project settings
	 */
	public function setProjectSetting($projetSetting)
	{
		$this->projectSetting = $projetSetting;
		$this->prepareProjectSettingData();
		$this->currentDate = new \DateTime();
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Get project settings
	 */
	public function getProjectSetting()
	{
		return $this->projetSetting;
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Prepare data from project setting
	 */
	public function prepareProjectSettingData()
	{
		$this->instanceId = $this->projectSetting['instance_id'];
		$this->cultureId = $this->projectSetting['culture_id'];
		$this->cultureCode = $this->projectSetting['culture_code'];
		$this->userId = $this->projectSetting['user_id'];
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Get coupon list
	 */
	public function getList($searchQuery = '')
	{
		$query = $this->createQueryBuilder('pc')
		->select('pc');
		if ($searchQuery != '') {
			$query->andWhere('pc.couponName like :searchQuery');
			$query->setParameter(':searchQuery', '%'.$searchQuery.'%');
		}
		$query->orderBy('pc.id', 'DESC');
	
		return $query->getQuery()->getResult();
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Get page by coupon
	 */
	public function findByCouponName($couponName)
	{
		return $this->createQueryBuilder('c')
		->select('c')
		->innerJoin('OfficeBrainCustomBundleServiceGroupBundle:CouponUsers', 'cu', 'WITH', 'cu.promoCouponId = c.id AND cu.userId = :userId')
		->where('c.couponName = :couponName')
		->andWhere('c.status = 1')
		->setParameter(':couponName', $couponName)
		->setParameter(':userId', $this->userId)
		->getQuery()
		->getOneOrNullResult();
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Get page by coupon name
	 */
	public function getSiteSearchList($search)
	{
		return $this->createQueryBuilder('p')
		->select('p')
		->where('l.couponName LIKE :search OR l.couponType LIKE :search OR l.startDate LIKE :search OR l.endDate LIKE :search')
		->setParameter(':search',"%".$search."%")
		->getQuery()
		->getResult();
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Get coupon name of specific Id
	 */
	public function getPromoCouponById($id)
	{
		return $this->createQueryBuilder('cp')
		->select('cp')
		->where('cp.id = :id')
		->setParameter(':id', $id)
		->getQuery()
		->getOneOrNullResult();
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Find all data of coupon name
	 */
	public function findAll()
	{
		return $this->createQueryBuilder('s')
		->select('s')
		->orderBy('s.id', 'DESC')
		->getQuery()
		->getResult();
	}
	
	
	/**
	 * @author Employee Id: 5497
	 * Description : Return count requested coupon name
	 */
	public function getCountCouponNameExist($couponName)
	{
		$result = $this->createQueryBuilder('s')
		->select('COUNT(s)')
		->where('s.couponName = :couponName')
		->setParameter(':couponName', $couponName)
		->getQuery()->getResult();
		if ($result[0][1] > 0) {
			return $result[0][1];
		}
	
		return;
	}
	
	public function findByOption($option, $keyword)
	{
		$return = $this->createQueryBuilder('cp')
		->select(['cp']);
	
		if ($option == 'coupon_name') {
			$return->where('cp.couponName LIKE :keyword');
		}
		elseif ($option == 'coupon_type') {
			$return->where('cp.couponType LIKE :keyword');
		}
		elseif ($option == 'start_date') {
			$return->where('cp.startDate LIKE :keyword');
		}
		elseif ($option == 'end_date') {
			$return->where('cp.endDate LIKE :keyword');
		}
	
		$return->setParameter(':keyword', '%'.$keyword.'%')
		->orderBy('cp.id', 'DESC')
		->getQuery()
		->getResult();
	
		return $return;
	}
	
	/**
	 * @author Employee Id: 5497
	 *
	 * Description : Change Status of coupon
	 *
	 * @param $id
	 *
	 * @return : boolean
	 *
	 * @throws Exception: Null
	 */
	public function changeStatus($id)
	{
		$priceMatrixColumn = $this->findOneBy(array('id' => $id));
		if ($priceMatrixColumn) {
			if ($priceMatrixColumn->getStatus() == 1) {
				$priceMatrixColumn->setStatus(0);
			} else {
				$priceMatrixColumn->setStatus(1);
			}
			$this->commit($priceMatrixColumn);
			return true;
		}
		return false;
	}
	
	/**
	 * @author Employee Id: 5497
	 *
	 * Description : Find single result  based on ID
	 *
	 * @param $id
	 *
	 * @return : Result
	 *
	 * @throws Exception: Null
	 */
	public function findCountByCouponName($couponName)
	{
		return $this->createQueryBuilder('cp')
		->select('COUNT(cp.id)')
		->where('cp.couponName = :couponName')
		->setParameter(':couponName', $couponName)
		->getQuery()
		->getSingleScalarResult();
	}
	
	/**
	 * @author Employee Id: 5497
	 *
	 * Description : Find single result  based on ID
	 *
	 * @param $id
	 *
	 * @return : Result
	 *
	 * @throws Exception: Null
	 */
	public function findCount()
	{
		return $this->createQueryBuilder('cp')
		->select('COUNT(cp.id)')
		->getQuery()
		->getSingleScalarResult();
	}
	
	public function set($entity)
	{
		return $this->commit($entity);
	}
	
	private function commit($entity)
	{
		$this->_em->persist($entity);
		$this->_em->flush();
	
		return $entity;
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Get coupon by expiry date
	 */
	public function findByExpiryDate($couponName)
	{
		return $this->createQueryBuilder('p')
		->select('p.id')
		->where('p.couponName = :couponName')
		->andWhere('p.startDate <= :today AND p.endDate >= :today')
		->setParameter(':couponName', $couponName)
		->setParameter(':today', $this->currentDate->format('Y-m-d'))
		->getQuery()
		->getResult();
	}
}
