#!/perl/bin/perl.exe
$|=1;
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# Information Software SA
# Date In: 2013-03-31
# Updates: 2013-03-31
# Author:Rodolfo Machon
# File Name: recibo_detalles_p.pl
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
# PERL MODULES
use CGI;
use Win32::ODBC;
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
# HTML PRINTING ACTIVATION
print "Content-type: text/html\n\n";
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#GET PARAMETERS
$cgiparms   = new CGI;
$lan	    = $cgiparms->param('lan');
$appid	    = $cgiparms->param('appid');
$id 	    = $cgiparms->param('id');
$dt_emision = $cgiparms->param('dt_emision');
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# CHECK PARAMETERS
if ($lan eq ""){print "Your are not passing LAN parameter<br>";}
if ($appid eq ""){print "Your are not passing APPID parameter<br>";}
if ($id eq ""){print "Your are not passing ID parameter<br>";}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# GLBAL VARIABLES
$PATH 	    = "../".$lan."/";
$file       = $lan."_recibo_detalles_p.html";
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# GET CLASE SCHEMA AND VARIABLES
if (!($dbh  = new Win32::ODBC($appid))){connect_fail($appid)}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# PROGRAM DESCRIPTION
# PROCUREMENT
#$periodo_actual    = get_periodo_actual();
%recibos 	   = get_recibos();
#%tarifas_cuentas   = get_tarifas_cuentas();
# INTEGRATION
#%partida	   = get_debe_haber();
# PRESENTATION
$table		   = construct_table();
construct_page();
exit;
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#SUB-ROUTINES -> LEVEL 1
sub get_recibos {
    my $count = 1;
    my %hash  = ();
    my $sql   = "SELECT b.c16_tarifa_id,
			b.c16_cuenta_ingreso_id,
			b.v128_tarifa,
			b.d82_cantidad,
			b.d82_tarifa,
			(b.`d82_cantidad`*b.`d82_tarifa`) as TOTAL
		 FROM sdm_recibos a
		 INNER JOIN sdd_recibos b ON a.c16_recibo_id=b.c16_recibo_id
		 WHERE a.c16_recibo_id= '$id'
		 ORDER BY b.c16_tarifa_id";
    if ( $dbh->Sql($sql) ){SQL_fail($sql,$dbh,'sub get_recibos')};
    while($dbh->FetchRow()){
	my @row = $dbh->Data();
	$hash{$row[0]."_".$count}   = [@row];
	$count++;
    }
    return %hash;
}
sub construct_table {
    my $count   = 1;
    my $total   = 0;
    my $table   = "";
    my %thead_h = ();
    my %tfoot_h = ();    
    my $thead_s = "";
    my $tfoot_s = "";    
    my $rowset  = "";
    my $thead   = "<thead><tr><TD>No</TD><TD>TARIFA ID</TD><TD>CUENTA</TD><TD>DESCRIPCION</TD><TD>CANTIDAD</TD><TD>VALOR UNIDAD</TD><TD>SUB-TOTAL</TD></tr></thead>";
    my $tfoot   = "<tfoot><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>TOTAL</td><td>{total}</td></tr></tfoot>";    
    foreach my $k (sort keys %recibos){
	my @arr  = @{$recibos{$k}};
	$arr[5]  = sprintf("%.2f",$arr[5]);
	my $row  = join("</td><td>",@arr);
	$row     = "<tr><td>$count</td><td>".$row."</td></tr>";
	$rowset .= $row;
	$total  += $arr[5];
	$count++;	    }
    $total  = sprintf("%.2f",$total);
    $thead  =~ s/\{thead_s}/$thead_s/g;
    $tfoot  =~ s/\{total}/$total/g;
    $rowset = "<tbody>".$rowset."</tbody>";
    $table  = "<table id=grid>".$thead.$rowset.$tfoot."</table>";
    $table  = convertir_tildes($table);
    return $table;
}
sub construct_page {
	open (DATA, "$PATH$file");
	while ($line = <DATA>) {	
		$line =~ s/\{lan}/$lan/g;
		$line =~ s/\{appid}/$appid/g;
		$line =~ s/\{id}/$id/g;	 
		$line =~ s/\{table}/$table/g;
		$line =~ s/\{dt_emision}/$dt_emision/g;
		print "$line";
	}
}
#-------------------------------------------------------------------------
#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};
    $agencia    = $data->{portal}->{agencia};
    $sucursal   = $data->{portal}->{sucursal};     
}
sub SQL_fail {
    my ($query,$dbh,$section)= @_;
    print "SQL failed in Section $section<br>";
    print "Error: ".$dbh->Error()."\n<br>";
    print "Query: ".$query."<br>";    
    $dbh->Close();
    exit;
}
sub connect_fail {
    my ($DSN)=@_;
    print "Error connecting to $DSN<br>";
    print "Error:".Win32::ODBC::Error()."<br>";
    exit;
}
sub convertir_tildes{
    my ($str) = @_;
    $str =~ s/\Ã±/ñ/g;
    $str =~ s/\Ã/Ñ/g;
    $str =~ s/\Ã‘/Ñ/g;      
    $str =~ s/\Ã¡/á/g;
    $str =~ s/\Ã©/é/g;
    $str =~ s/\Ã­/í/g;
    $str =~ s/\Ã³/ó/g;
    $str =~ s/\Ãº/ú/g;
    return $str;
}
sub get_periodo_actual {
    my @de = split("-",$dt_emision);
    return $de[0].$de[1];
}
#-------------------------------------------------------------------------
#sub get_tarifas_cuentas {
#    my %hash  = ();	
#    foreach my $k(sort keys %recibos){
#	my @tid = split(/\_/,$k);
#	my $sql = "SELECT c16_cuenta_recurso_id,
#			  c16_cuenta_obligacion_id,
#			  c16_cuenta_recurso_pasado_id,
#			  c16_cuenta_obligacion_pasado_id
#			  c16_cuenta_recurso_futuro_id,
#			  c16_cuenta_obligacion_futuro_id
#		   FROM sdm_tarifas
#		   WHERE c16_tarifa_id='$tid[0]'";
#	if ($dbh->Sql($sql) ){SQL_fail($sql,$dbh,'sub get_tarifa_cuentas')};
#	while ($dbh->FetchRow()){
#		my @row = $dbh->Data();
#		$hash{$tid[0]}= join('|',@row);;
#	}		
#    }
#    return %hash;
#}
#sub get_debe_haber{
#    my %debe_haber = ();
#    foreach my $k(sort keys %recibos){
#	my @tid = split(/\_/,$k);			
#	my @tcc = split(/\|/,$tarifas_cuentas{$tid[0]});			
#	$recibos{$k}[3] =~ s/\ //g;
#	if($recibos{$k}[3] ne ""){
#		my @periodos = split(/\~/,$recibos{$k}[3]);
#		for (my $i=0;$i<=$#periodos;$i++){
#			my @periodo_data  = split(/\,/,$periodos[$i]);
#			my $ano_periodo   = substr($periodo_data[0],0,4); 
#			my $ano_actual    = substr($periodo_actual,0,4);
#			if($ano_periodo == $ano_actual){
#				$debe_haber{$tid[0]}{'recurso'}{$tcc[0]} += $periodo_data[3];
#				$debe_haber{$tid[0]}{'obligacion'}{$tcc[1]} += $periodo_data[3];
#			}
#			if($ano_periodo < $ano_actual){
#				if($tcc[2] ne "" && $tcc[3] ne ""){
#					$debe_haber{$tid[0]}{'recurso_pasado'}{$tcc[2]} += $periodo_data[3];
#					$debe_haber{$tid[0]}{'obligacion_pasado'}{$tcc[3]} += $periodo_data[3];
#				}else{
#					$debe_haber{$tid[0]}{'recurso'}{$tcc[0]} += $periodo_data[3];
#					$debe_haber{$tid[0]}{'obligacion'}{$tcc[1]} += $periodo_data[3];
#				}
#			}
#			if($ano_periodo > $ano_actual){
#				if($tcc[4] ne "" && $tcc[5] ne ""){
#					$debe_haber{$tid[0]}{'recurso_futuro'}{$tcc[4]} += $periodo_data[3];
#					$debe_haber{$tid[0]}{'obligacion_futuro'}{$tcc[5]} += $periodo_data[3];
#				}else{
#					$debe_haber{$tid[0]}{'recurso'}{$tcc[0]} += $periodo_data[3];
#					$debe_haber{$tid[0]}{'obligacion'}{$tcc[1]} += $periodo_data[3];
#				}
#			}		
#		}
#	}else{
#		$debe_haber{$tid[0]}{'recurso'}{$tcc[0]} += $recibos{$k}[2];
#		$debe_haber{$tid[0]}{'obligacion'}{$tcc[1]} += $recibos{$k}[2];
#	}
#    }
#    foreach my $k (sort keys %debe_haber){
#	foreach my $kk (sort keys %{$debe_haber{$k}}){
#	    foreach my $kkk (sort keys %{$debe_haber{$k}{$kk}}){
#		$debe_haber{$k}{$kk} = $kkk."|".$debe_haber{$k}{$kk}{$kkk};
#	    }
#	}
#    }   
#    return %debe_haber;
#}
