<?php
/* Busqueda de registros de la tabla TR_Vehiculo redefinidos por el usuario. */
class TR_VehiculoFN extends BaseTR_VehiculoFN
{
    public static function recuperarId()
    {
        $s_Sql = "SELECT IF( MAX( IdVehiculo ) IS NULL, 1, MAX( IdVehiculo ) + 1 ) i_Id FROM TR_Vehiculo;";
        $o_ResultSet = Conexion::getConexion()->prepareStatement( $s_Sql )->executeQuery();
        $o_ResultSet->next();
        return $o_ResultSet->getInt( 'i_Id' );
    }
    
    public static function selectActivos()
    {
        $o_TR_VehiculoFN = new TR_VehiculoFN();
        $o_TR_VehiculoFN->setEstado( "A" );
        return $o_TR_VehiculoFN->select();
    }
    
    public static function selectActivosProg( $s_FechaProgramada, $i_Turno )
    {
        $s_Sql = "
                    SELECT
                        ( 
                            SELECT IdRuta 
                            FROM TR_Ruta 
                            WHERE 1=1
                                AND RutaFechaProgramada='$s_FechaProgramada' 
                                AND IdVehiculo=V.IdVehiculo  
                                AND IdTurno='$i_Turno' 
                                AND IdCompania='".myUser::getUser()->getIdBusiness()."' 
                                AND IdSucursal='".myUser::getUser()->getIdSucursal()."'
                            GROUP BY IdVehiculo 
                        ) IdRuta,
                        V.*
                    FROM TR_Vehiculo V
                    WHERE 1=1
                    AND V.Estado='A'
                    ORDER BY V.VehiculoPlaca;";
        //pr( $s_Sql );
        $o_ResultSet = Conexion::getConexion()->prepareStatement( $s_Sql )->executeQuery();
        $a_ObjectArrayBN = array();
        $o_ObjectFN = new TR_VehiculoFN();
        while( $o_ResultSet->next() ) 
        {
            $o_SQLRow = $o_ResultSet->getRow();
            $o_DefaultBN = $o_ObjectFN->insertObjectBN( $o_SQLRow );
            if( $o_SQLRow['IdRuta'] )
            {
                $o_DefaultBN->setVehiculoPlaca( $o_DefaultBN->getVehiculoPlaca()." (PROG)" );
            }
            $a_ObjectArrayBN[] = $o_DefaultBN;
        }
        
        return $a_ObjectArrayBN;
    }
}