<?php

namespace OfficeBrain\CustomBundle\ServiceGroupBundle\Entity;

/**
 * CouponUsersRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class _CouponUsersRepository 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();
	}
	
	/**
	 * @author Employee Id: 5497
	 */
	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 name of specific Id
	 */
	public function getCouponUsersById($id)
	{
		return $this->createQueryBuilder('cp')
		->select('cp')
		->where('cp.id = :id')
		->setParameter(':id', $id)
		->getQuery()
		->getOneOrNullResult();
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Get coupon name of specific Id
	 */
	public function getCouponUsersByCouponId($couponId)
	{
		return $this->createQueryBuilder('cp')
		->select('cp')
		->where('cp.promoCouponId = :couponId')
		->setParameter(':couponId', $couponId)
		->getQuery()
		->getResult();
	}

	/**
	 * @author Employee Id: 5497
	 * Description : Get used coupon name of specific Id
	 */
	public function getUsedCouponUsersByCouponId($couponId)
	{
		return $this->createQueryBuilder('cp')
		->select('cp')
		->where('cp.promoCouponId = :couponId')
		->andwhere('cp.isUsed = 1')
		->setParameter(':couponId', $couponId)
		->getQuery()
		->getResult();
	}

	/**
	 * @author Employee Id: 5497
	 * Description : Get redeem coupon name of specific Id
	 */
	public function getRedeemCouponUsersByCouponId($couponId)
	{
		return $this->createQueryBuilder('cp')
		->select('cp')
		->where('cp.promoCouponId = :couponId')
		->andwhere('cp.isRedeem = 1 and cp.isUsed = 0')
		->setParameter(':couponId', $couponId)
		->getQuery()
		->getResult();
	}

	/**
	 * @author Employee Id: 5497
	 * Description : Get aplied coupon name of specific Id
	 */
	public function getActiveCouponUsersByCouponId($couponId)
	{
		return $this->createQueryBuilder('cp')
		->select('cp')
		->where('cp.promoCouponId = :couponId')
		->andwhere('cp.isRedeem = 0 and cp.isUsed = 0')
		->setParameter(':couponId', $couponId)
		->getQuery()
		->getResult();
	}

	/**
	 * @author Employee Id: 5497
	 * Description : Get redeem or used coupon name of specific Id
	 */
	public function getUsedRedeemCouponUsersByCouponId($couponId)
	{
		return $this->createQueryBuilder('cp')
		->select('cp')
		->where('cp.promoCouponId = :couponId')
		->andwhere('cp.isRedeem = 1 or cp.isUsed = 1')
		->setParameter(':couponId', $couponId)
		->getQuery()
		->getResult();
	}
	
	/**
	 * @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 : 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;
	}
	
	public function remove($entity)
	{
		$this->_em->remove($entity);
		$this->_em->flush();
		return true;
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Get id from promoCouponId and userId
	 */
	public function findByCouponIdAndUserId($couponId, $userId = 0)
	{
		if ($userId == 0) {
			$userId = $this->userId;
		}

		return $this->createQueryBuilder('cu')
					->select('cu')
					->where('cu.promoCouponId = :couponId')
					->andwhere('cu.userId = :userId')
					->setParameter(':couponId', $couponId)
					->setParameter(':userId', $userId)
					->getQuery()
					->getOneOrNullResult();
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Get id from promoCouponId and userId
	 */
	public function findActiveUserRedeemCoupon($userId = 0)
	{
		if ($userId == 0) {
			$userId = $this->userId;
		}

		return $this->createQueryBuilder('cu')
		->select('cu')
		->where('cu.isRedeem = 1 AND cu.isUsed = 0')
		->andwhere('cu.userId = :userId')
		->setParameter(':userId', $userId)
		->getQuery()
		->getOneOrNullResult();
	}
	
	/**
	 * @author Employee Id: 5497
	 * Description : Get coupon status from promoCouponId and userId
	 */
	public function getUserCouponRedeemStatus($couponId)
	{
		if ($userId == 0) {
			$userId = $this->userId;
		}
	
		return $this->createQueryBuilder('cu')
		->select('cu')
		->where('cu.isRedeem = 1 AND cu.isUsed = 1')
		->andwhere('cu.userId = :userId')
		->setParameter(':userId', $userId)
		->andwhere('cu.promoCouponId = :couponId')
		->setParameter(':couponId', $couponId)
		->getQuery()
		->getOneOrNullResult();
	}
}
