#!/perl/bin/perl.exe
$|=1;
#-------------------------------------------------------------------
#-------------------------------------------------------------------
use Win32::ODBC;
use CGI;
#-------------------------------------------------------------------
#-------------------------------------------------------------------
print "Content-type: text/html\n\n";
#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------------------------------------------------
#-------------------------------------------------------------------
$form 		= new CGI;
$lan		= $form->param('lan');
$appid		= $form->param('appid');
$clase		= $form->param('clase');
$id             = $form->param('id');


#---------------------------------------------------------------
#---------------------------------------------------------------
if ( !( $db_tes = new Win32::ODBC($appid) ) ) {print "Error connecting to $dsn_tes <br>";print "Error: " . Win32::ODBC::Error() . "<br>";exit;}
if ( !( $db_tes1 = new Win32::ODBC($appid) ) ) {print "Error connecting to $dsn_tes1 <br>";print "Error: " . Win32::ODBC::Error() . "<br>";exit;}
#-------------------------------------------------------
#MAIN PROGRAM
#print "<br>".$clase;
#print "<br>".$appid;
#print "<br>".$i8_nuc_id;

#################llamado a la funcion que organiza la busqueda de los inmuebles a los que se les sacará la notificacion
if(substr($id,0,1) eq "1"){$table="sdd_inmueble_financiamientos_sgm";$field_id="i8_inmueble_id";}
else{$table="sdd_empresa_financiamientos_sgm";$field_id="i8_empresa_id";}

buscar_usuario();
#################


#construct_page();

$db_tes->Close();
$db_tes1->Close();

exit;

#-------------------------------------------------------------------------
# AUXILIAR-SUBROUTINES

sub buscar_usuario {
    # my ($id)=@_;
    #print "llega<br>";
    my $nombre    = "";
    my $apellido  = "";
    my $dt_ultima_modificacion  = "";
    ####PARA OBTENER EL USUARIO DE LA SESION
    $user=GetUser();
    #######
    #print "Eres ".GetUser()." y estas ";
    
      $sql="select a.`v32_fname`,a.`v32_lname` from sdm_users a where a.`v16_login`='$user'";
      #print "$sql";
      if($db_tes->Sql($sql)){print "false";exit;}
      
      while( $db_tes->FetchRow() ){
            undef my @row1;
            @row1 = $db_tes->Data();
            $nombre=$row1[0];
            $apellido=$row1[1];
            #print "true";
      }
      $sql="select count(*) from $table where $field_id='$id'";
      #print "$sql";
      $fn=0;
      if($db_tes->Sql($sql)){print "false";exit;}
      while( $db_tes->FetchRow() ){
            undef my @row1;
            @row1 = $db_tes->Data();
            $fn=$row1[0];
            
      }
      if($fn>0){$res="SI"}else{$res="NO"}
      
    if($es_empresa eq "SI"){
        $sql="select C1.empresa from (select DATE(a.`ts_update`) as empresa from sdm_empresas a WHERE a.`i8_empresa_id`='$id' union select DATE(b.`ts_update`) as empresa from `sdd_empresa_cargo_inicios_transacciones` b WHERE b.`i8_empresa_id`='$id' union select DATE(c.`ts_update`) as empresa from `sdd_empresa_bases_imponibles_transacciones` c WHERE c.`i8_empresa_id`='$id' union select DATE(d.`ts_update`) as empresa from `sdd_empresa_servicios_transacciones` d WHERE d.`i8_empresa_id`='$id' union select DATE(e.`ts_update`) as empresa from `sdd_empresa_contribuyentes` e WHERE e.`i8_empresa_id`='$id') as C1 order by C1.empresa DESC limit 1;";
    }
    else{
        $sql="select C1.inmueble from (select DATE(a.`ts_update`) as inmueble from sdm_inmuebles a WHERE a.`i8_inmueble_id`='$id' union select DATE(b.`ts_update`) as inmueble from `sdd_inmueble_cargo_inicios_transacciones` b WHERE b.`i8_inmueble_id`='$id' union select DATE(c.`ts_update`) as inmueble from `sdd_inmueble_bases_imponibles_transacciones` c WHERE c.`i8_inmueble_id`='$id' union select DATE(d.`ts_update`) as inmueble from `sdd_inmueble_servicios_transacciones` d WHERE d.`i8_inmueble_id`='$id' union select DATE(e.`ts_update`) as inmueble from `sdd_inmueble_contribuyentes` e WHERE e.`i8_inmueble_id`='$id') as C1 order by C1.inmueble DESC limit 1;";
    }
      
      
      if($db_tes->Sql($sql)){print "false";exit;}
      while( $db_tes->FetchRow() ){
            undef my @row1;
            @row1 = $db_tes->Data();
            $dt_ultima_modificacion=$row1[0];
      }

     print "$nombre $apellido|$res|$dt_ultima_modificacion";
}

sub GetUser {
        local($username) = $ENV{"REMOTE_USER"};
        local($identify) = $ENV{"REMOTE_IDENT"};
        local($user) = $username || $identify;
        return($user);
}