<?php
/**
 * Clase FN para CrPlanillaFN
 *
 * @package    
 * @subpackage CrPlanillaFN
 * @author     Willy
 * @version    SVN:: CrPlanillaFN.class.php,v 1.1 2014-03-11 18:51:07 william.taype Exp $
 */
class CrPlanillaFN extends BaseCrPlanillaFN
{
	 public function select()
  {
    try
    {
      $tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;

      $where = $this->buildConsult();
      $orden = $this->buildOrderByField();

      $con = Conexion::getConexion();
      $sql = "	SELECT p.IdColaborador, p.Periodo, p.IdConcepto, p.Valor, p.FechaCreacion, p.UsuarioCreacion 
      			FROM cr_planilla p
      			LEFT JOIN cr_colaborador c on c.IdColaborador = p.IdColaborador 
      			LEFT JOIN cr_sucursal_per s on s.Periodo = p.Periodo and c.IdColaborador = s.IdAdministrador 
      			WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
     //echo $sql;
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
        $result = mysql_query($sql, $oLink);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      $sql = "	SELECT p.IdColaborador, p.Periodo, p.IdConcepto, p.Valor, p.FechaCreacion, p.UsuarioCreacion 
      			FROM cr_planilla p
      			LEFT JOIN cr_colaborador_per c on c.IdColaborador = p.IdColaborador and c.Periodo = p.Periodo
      			LEFT JOIN cr_sucursal_per s on s.Periodo = p.Periodo and c.IdPadre = s.IdAdministrador 
      			WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
     //echo $sql;
      $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
      $num_rows = mysql_num_rows($result);
      
      
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }
  
  function getColaboradores($periodo=''){
  	  $tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;

      $where = $this->buildConsult();
      $orden = $this->buildOrderByField();

      $con = Conexion::getConexion();
      $tabla = " cr_colaborador";
      if($periodo) {
      	$tabla_col =  " cr_colaborador_per";
      	$join_col =  " and c.Periodo = p.Periodo ";
      	$where.=" and p.periodo = '".$periodo."'";
      	$left_join_padre ="LEFT JOIN cr_sucursal_per s on s.Periodo = p.Periodo and c.IdColaborador= s.IdAdministrador ";
      	$left_join="LEFT JOIN cr_sucursal_per s on s.Periodo = p.Periodo and c.IdPadre = s.IdAdministrador ";
      }
      
      $sql = "	SELECT 	c.IdColaborador,concat(' ',ApellidoPaterno,' ',	ApellidoMaterno,', ',Nombres) as NombreCompleto,Email, idRol,c.dni,FechaIngreso
      			FROM 	cr_planilla p
      			LEFT JOIN $tabla_col c on c.IdColaborador = p.IdColaborador $join_col
      			$left_join_padre
      			WHERE 1 ".$where." 
      			GROUP BY p.IdColaborador ";
     
     
     
     
     $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
     
     $num_rows = mysql_num_rows($result);
     
     try{
	      $a_tmp = array();
	      if($num_rows>0){
	      	$i=1;
	        while ($o_valor = mysql_fetch_object($result)) {
	        	
	          $a_tmp[$i]['IdColaborador'] 	= $o_valor->IdColaborador;
	          $a_tmp[$i]['NombreCompleto'] 	= $o_valor->NombreCompleto;
	          $a_tmp[$i]['DNI'] 			= $o_valor->dni;
	          $a_tmp[$i]['idRol'] 			= $o_valor->idRol;
	          $a_tmp[$i]['Email'] 			= $o_valor->Email;
	          $a_tmp[$i]['FechaIngreso'] 		= $o_valor->FechaIngreso;
	          $i++;
	          
	        }
	      }
	     
	      if($this->getIdSucursal()){
    
			      //Los hijos
			      $sql = "	SELECT 	c.IdColaborador,concat(' ',ApellidoPaterno,' ',	ApellidoMaterno,', ',Nombres) as NombreCompleto,Email, idRol,c.dni,FechaIngreso
			      			FROM 	cr_planilla p
			      			LEFT JOIN $tabla_col c on c.IdColaborador = p.IdColaborador $join_col
			      			$left_join
			      			WHERE 1 ".$where." 
			      			GROUP BY p.IdColaborador 
			      			
			      			";
		
			   
				 $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
			     $num_rows = mysql_num_rows($result);
			       
				      
			      if($num_rows>0){
			        while ($o_valor = mysql_fetch_object($result)) {
			          $a_tmp[$i]['IdColaborador'] 	= $o_valor->IdColaborador;
			          $a_tmp[$i]['NombreCompleto'] 	= $o_valor->NombreCompleto;
			          $a_tmp[$i]['DNI'] 			= $o_valor->dni;
			          $a_tmp[$i]['idRol'] 			= $o_valor->idRol;
			          $a_tmp[$i]['Email'] 			= $o_valor->Email;
			          $a_tmp[$i]['FechaIngreso'] 		= $o_valor->FechaIngreso;
			          $i++;
			        }
			      }
			      
	  	   }
	  	   
	      
	      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }
}