<?php

namespace OfficeBrain\Bundle\RewardReferralBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * RewardEarnHistoryRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class RewardEarnHistoryRepository extends EntityRepository
{
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $cultureId;
    protected $cultureCode;
    protected $paginatorService;
    protected $projectSettingService;

    public function getPaginatorService()
    {
        return $this->paginatorService;
    }

    public function setPaginatorService($paginatorService)
    {
        $this->paginatorService = $paginatorService;
    }

    public function getProjectSetting()
    {
        return $this->paginatorService;
    }

    public function setProjectSetting($projectSetting)
    {
        $this->projectSetting = $projectSetting;
        $this->prepareProjectSettingData();
    }

    public function prepareProjectSettingData()
    {
        $this->userId = $this->projectSetting['user_id'];
        $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: 4589
     * Description : Function created for define a common querybuilder object
     *
     * @return object
     */
    public function getQueryBuilder()
    {
        return $this->getEntityManager()->getRepository('OfficeBrainRewardReferralBundle:RewardEarnHistory')->createQueryBuilder('reh');
    }
    
    /**
     * @Author Employee Id: 4589
     * Description : Function created for check user already sent request to same email
     *
     * @return object
     */
    public function setRewardEarnHistory($rewardEarnHistory)
    {
        $em = $this->getEntityManager();
        
        $rewardEarnHistory->setInstanceId($this->instanceId);
        $rewardEarnHistory->setInstanceType($this->instanceType);
        $rewardEarnHistory->setCreatedAt(new \DateTime());
        $rewardEarnHistory->setUpdatedUid($this->userId);
        $rewardEarnHistory->setUpdatedAt(new \DateTime());
        
        $em->persist($rewardEarnHistory);
    }
    
    /**
     * @Author Employee Id: 4589
     * Description : Function created for get earn history by user
     *
     * @param 1 : $userId
     *
     * @return object
     */
    public function getEarnHisotry($userId)
    {
        $qb = $this->getQueryBuilder();
        $qb->where('reh.userId = :userId')
                ->setParameter('userId', $userId);
        $qb->orderBy('reh.createdAt', 'DESC');
        $result = $qb->getQuery()->getResult();
        
        return $result;
    }
    
    public function commit()
    {
        $em = $this->getEntityManager();
        $em->flush();
    }

    public function clear()
    {
        $em = $this->getEntityManager();
        $em->clear();
    }
}
