<?php

namespace OfficeBrain\Bundle\UserBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\FormError;

/**
 * EmployeeInfoRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class EmployeeInfoRepository extends \Doctrine\ORM\EntityRepository
{
	protected $projectSetting;
	protected $instanceId;
	protected $projectId;
	
	/**
	 * @author Employee Id: 4493
	 * OB eCommerce Product - addEmployeeInfo
	 * Function created for add Employee Info
	 */
	public function addEmployeeInfo($employeeInfoObj)
	{
		$em = $this->getEntityManager();
		$em->persist($employeeInfoObj);
		$em->flush();
		return $employeeInfoObj;
	}
	
	/**
	 * @author Employee Id: 4273
	 * OB eCommerce Product - editEmployeeInfo
	 * Function created for edit employee info
	 */
	public function editEmployeeInfo($employeeInfo)
	{
		$em = $this->getEntityManager();
		$em->persist($employeeInfo);
		$em->flush();
		return $employeeInfo;
	}
	
	
	/**
	 * @author Employee Id: 4273
	 * OB eCommerce Product - getEmplyeeInfoById
	 * Function created to get Emplyee Info By UserId
	 */
	public function getEmplyeeInfoById($userId)
	{
		return $query = $this->createQueryBuilder('e')
		->where("e.user =:userId")->setParameter('userId', $userId)
		->getQuery()->getOneorNullResult();
	
	}
	
	/**
	 * @author Employee Id: 5051
	 * Function created to get Employee Details
	 */
	
	public function getEmployeeInfo($companyId, $sortPosition = false)
	{
		$query=$this->createQueryBuilder('s')
		->select('std.firstName,std.middleName,std.lastName,std.extension,std.email,std.photo,std.phone,s.position,s.primaryJob,s.favoriteQuote,s.fact,s.favoriteFood')
		->innerJoin('OfficeBrainUserBundle:User', 'std', 'WITH', 'std.id = s.user')
		->andWhere('std.userCompany = :companyid')
		->andWhere('std.status=:status')
		->andWhere('std.userType=:type')
		->andWhere('std.deletedAt IS NULL')		
		->setParameter(':companyid', $companyId)
		->setParameter(':status', 'active')
		->setParameter(':type','employee');
		if($sortPosition){
		    $query->orderBy('s.sortPosition', 'ASC');
		}
		$query = $query->getQuery()
		->getResult();
		return $query;
	}
}
