<?php

namespace OfficeBrain\Bundle\CmsBundle\Entity;

use Doctrine\ORM\EntityRepository;

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

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to set project settings
     *
     * @param 1: $projectSetting
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function setProjectSetting($projetSetting)
    {
        $this->projectSetting = $projetSetting;
        $this->prepareProjectSettingData();
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to get project settings
     *
     * @param : None
     *
     * @return: project setting array
     *
     * @throws Exception: Null
     */
    public function getProjectSetting()
    {
        return $this->projetSetting;
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to set member variables default value
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function prepareProjectSettingData()
    {
        $this->projectId = $this->projectSetting['project_id'];
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->userId = $this->projectSetting['user_id'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
        $this->userId = $this->projectSetting['user_id'];
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to find form values wrt id for payment methods
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function find($id, $lockMode = NULL, $lockVersion = NULL)
    {
        return $this->createQueryBuilder('tps')
        ->select()
        ->where('tps.id = :id')
        ->setParameter('id', $id)
        ->andWhere('tps.status=1')
        ->getQuery()->getOneOrNullResult();
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to find parent payment methods
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function findParentPaymentMethods()
    {
        return $this->createQueryBuilder('tps')
        ->select()
        //->select(['tps'])
        //->leftJoin('tps.thirdPartyServiceKey', 'tpsk')
        //->where('tps.id = :id')
        ->andWhere('tps.status=1')
        ->andWhere('tps.type = :type')
        ->setParameter('type', 'payment')
        ->orderBy('tps.position', 'ASC')
        ->getQuery()->getResult();
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to find parent shipping methods
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function findParentShipmentMethods()
    {
        return $this->createQueryBuilder('tps')
        ->select()
        ->andWhere('tps.status=1')
        ->andWhere('tps.type = :type')
        ->setParameter('type', 'shipment')
        ->orderBy('tps.position', 'ASC')
        ->getQuery()->getResult();
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : Find Id based on Form
     *
     * @param $formId
     *
     * @return : Result
     *
     * @throws Exception: Null
     */
    public function findFormId($type)
    {
        return $this->createQueryBuilder('tps')
        ->select(['tps,tpsk'])
        ->leftJoin('tps.thirdPartyServiceKey', 'tpsk')
        ->where('tps.type = :type')
        ->andWhere('tps.status= 1 ')
        ->setParameter(':type', $type)
        ->getQuery()
        ->getResult();
    }
    
    /**
     * @author Employee Id: 4571
     *
     * Description : Find Id based on Form
     *
     * @param $formId
     *
     * @return : Result
     *
     * @throws Exception: Null
     */
    public function findFormIdByTpsKey($type)
    {
    	return $this->createQueryBuilder('tps')
        ->select(['tps.id'])
        ->where('tps.tpsKey = :tpsKey')
        ->setParameter('tpsKey', $type)
        ->andWhere('tps.status=1')
        ->getQuery()->getSingleScalarResult();
    }
}
