#!/perl/bin/perl.exe
$|=1;
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# Web Software SA
# Date In: 2016-03-18
# Updates: 2016-03-18,2022-11-16
# Author:Rodolfo Machon
# File Name: capturar_usuario.pl
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
# PERL MODULES
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use XML::Simple;
use Data::Dumper;
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
# HTML PRINTING ACTIVATION
print "Content-type: text/html; charset=utf-8\n\n";
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#GET PARAMETERS
$form 		= new CGI;
$clase		= $form->param('clase');
$id         = $form->param('id');
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# GLBAL VARIABLES
get_app_data();
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# GET CLASE SCHEMA AND VARIABLES
$dbh = DBI->connect("dbi:ODBC:$appid",'','',{PrintError => 1, RaiseError => 1});
if (!$dbh) {print "error: connection: $DBI::err\n$DBI::errstr\n$DBI::state\n";}
$dbh->{LongReadLen} = 512 * 2048;
$dbh->{LongTruncOk} = 1;
#-------------------------------------------------------
#MAIN PROGRAM
#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";}

$user = getUser();
buscar_usuario();

exit;
#-------------------------------------------------------------------------
# LEVEL I
sub getUser {
    local($username) = $ENV{"REMOTE_USER"};
    local($identify) = $ENV{"REMOTE_IDENT"};
    local($user) = $username || $identify;
    return($user);
}
sub buscar_usuario {
    my $fn  = 0;
    my $nombre    = "";
    my $apellido  = "";
    my $res = "";
    my $dt_ultima_modificacion  = "";
    my $sql ="select a.`v32_fname`,a.`v32_lname` from sdm_users a where a.`v16_login`='$user'";
    my $sth = $dbh->prepare($sql);
    $sth->execute or die SQL_fail($sql,$dbh,'sub buscar_usuario');
    while (my @row = $sth->fetchrow_array) {
        $nombre=$row[0];
        $apellido=$row[1];
    }
    
    #my $sql1="select count(*) from $table where $field_id='$id'";
    #my $sth1 = $dbh->prepare($sql1);
    #$sth1->execute or die SQL_fail($sql1,$dbh,'sub get_recibos');
    #while (my @row = $sth1->fetchrow_array) {
    #    $fn=$row[0];
    #}
    #if($fn>0){$res="SI"}else{$res="NO"}
    
    #if($empresa eq "SI"){
    #    $sql2 = "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
    #         ";
    #}
    #if($inmueble eq "SI"){
    #    $sql2 = "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";
    #}
    #my $sth2 = $dbh->prepare($sql2);
    #$sth2->execute or die SQL_fail($sql2,$dbh,'sub buscar_usuario');
    #while (my @row = $sth2->fetchrow_array) {
    #      $dt_ultima_modificacion=$row[0];
    #}
    
     print "$nombre $apellido|$res|$dt_ultima_modificacion";
}
#-------------------------------------------------------------------------
#AUXILIAR-SUBROUTINES
sub numeros {$a<=>$b}
sub get_app_data {
    my $schema  = "app.data.xml";
    my $xml     = new XML::Simple;
    my $data    = $xml->XMLin("../xml/".$schema);
    $appid      = $data->{portal}->{app};
    $lan        = $data->{portal}->{lang};
}
sub SQL_fail {
    my ($query,$dbh,$section)= @_;
    print "SQL failed in Section $section<br>";
    print "Query: ".$query."<br>";    
    exit;
}