<?php
/**
 * Clase Base DP para BaseCrPlanillaTempDP
 *
 * @package    
 * @subpackage BaseCrPlanillaTempDP
 * @author     Willy
 * @version    SVN:: BaseCrPlanillaTempDP.class.php,v 1.1 2022-07-23 14:30:20 william.taype Exp $
 */
abstract class BaseCrPlanillaTempDP extends CrPlanillaTempBN
{
  /* Número de registros afectados */
  private $nRowsAffected;

  public function __construct($bSetDefault = true)
  {
    if( $bSetDefault instanceof CrPlanillaTempBN )
    {
      $o_CrPlanillaTempBN = $bSetDefault;
      if($o_CrPlanillaTempBN->getIdPlanilla()) : $this->setIdPlanilla($o_CrPlanillaTempBN->getIdPlanilla()); endif;
      if($o_CrPlanillaTempBN->getPeriodo()) : $this->setPeriodo($o_CrPlanillaTempBN->getPeriodo()); endif;
      if($o_CrPlanillaTempBN->getIdEmpresa()) : $this->setIdEmpresa($o_CrPlanillaTempBN->getIdEmpresa()); endif;
      if($o_CrPlanillaTempBN->getIdColaborador()) : $this->setIdColaborador($o_CrPlanillaTempBN->getIdColaborador()); endif;
    }
  }

  /* Obtiene el número de registros afectados */
  public function getRowsAffected()
  {
    return $this->nRowsAffected;
  }

  public function save()
  {
    try
    {
      $con = Conexion::getConexion();
      $sql = "SELECT IF(MAX(IdPlanilla),(MAX(IdPlanilla)+1), 1) AS Maximo FROM cr_planilla_temp;";
      $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
      $result = mysql_query($sql, $oLink);
      $row = mysql_fetch_array($result);
      $this->setIdPlanilla($row[0]);

      if($this->getIdPlanilla()){
        $a_Fields = array();

        if($this->getIdPlanilla()) : $a_Fields['IdPlanilla'] = ($this->getIdPlanilla()); endif;
        if($this->getPeriodo()) : $a_Fields['Periodo'] = ($this->getPeriodo()); endif;
        if($this->getIdEmpresa()) : $a_Fields['IdEmpresa'] = ($this->getIdEmpresa()); endif;
        if($this->getIdColaborador()) : $a_Fields['IdColaborador'] = ($this->getIdColaborador()); endif;

        $sStringField = arrayClass::implode_keys(',', $a_Fields);
        $sStringValue = "'".implode("','", $a_Fields)."'";

        $sql = "INSERT INTO cr_planilla_temp (".$sStringField.") VALUES (".$sStringValue.");";
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
        $result = mysql_query($sql, $oLink);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function update()
  {
    try
    {
      if($this->getIdPlanilla())
      {
        $a_Fields = array();

        if($this->getIdPlanilla()) : $a_Fields['IdPlanilla'] = ($this->getIdPlanilla()); endif;
        if($this->getPeriodo()) : $a_Fields['Periodo'] = ($this->getPeriodo()); endif;
        if($this->getIdEmpresa()) : $a_Fields['IdEmpresa'] = ($this->getIdEmpresa()); endif;
        if($this->getIdColaborador()) : $a_Fields['IdColaborador'] = ($this->getIdColaborador()); endif;

        $sStringField = arrayClass::implode_key_value(',', $a_Fields);

        $con = Conexion::getConexion();
        $sql = "UPDATE cr_planilla_temp SET ".$sStringField." WHERE 1 AND IdPlanilla='".$this->getIdPlanilla()."';";
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
        $result = mysql_query($sql, $oLink);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function delete()
  {
    try
    {
      if($this->getIdPlanilla())
      {
        $a_Fields = array();

        $con = Conexion::getConexion();
        $sql = "DELETE FROM cr_planilla_temp WHERE 1 AND IdPlanilla='".$this->getIdPlanilla()."';"; 
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
        $result = mysql_query($sql, $oLink);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  /* Búsqueda del registro por la llave primaria */
  public static function selectByPK($vIdPlanilla)
  {
    try
    {
      $con = Conexion::getConexion();
      $sql = "SELECT IdPlanilla, Periodo, IdEmpresa, IdColaborador FROM cr_planilla_temp WHERE 1 AND IdPlanilla='".$vIdPlanilla."'; ";
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
        $result = mysql_query($sql, $oLink);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      
      return $o_Propel?self::insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con métodos de acceso */
  private function insertObjectBN($o_propel){
    $o_CrPlanillaTempDP = new CrPlanillaTempDP();
    $o_CrPlanillaTempDP->setIdPlanilla($o_propel->IdPlanilla);
    $o_CrPlanillaTempDP->setPeriodo($o_propel->Periodo);
    $o_CrPlanillaTempDP->setIdEmpresa($o_propel->IdEmpresa);
    $o_CrPlanillaTempDP->setIdColaborador($o_propel->IdColaborador);
    return $o_CrPlanillaTempDP;
  }
}