#!/perl/bin/perl.exe
$|=1;
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# Information Software SA
# Date In: 2010-11-27
# Updates: 2010-11-27
# Author:Rodolfo Machon
# File Name: get_financiamientos.pl
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
#Perl Modules
use CGI;
use Win32::ODBC;
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
#HTML printing activation
print "Content-type: text/html\n\n";
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
#Getting parameters
$form	= new CGI;
$lan    = $form->param('lan');
$appid  = $form->param('appid');
$clase  = $form->param('clase');
$id	= $form->param('id');
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# 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 ($clase eq ""){print "Your are not passing CLASE parameter<br>";}
if ($id eq ""){print "Your are not passing ID parameter<br>";}
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
# MAIN-PROGRAM
@financiamiento = ();
%financiamiento_detalle = ();
plantillas();
$finan_activo = get_financiamiento();
if($finan_activo eq "NO"){print "NO";exit}
get_financiamiento_detalles();
construct_page();
exit;
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
#MAIN-SUBROUTINES
sub get_financiamiento {
	my $fa	     = "NO";
	my %finan    = ();
	my $query    = "SELECT i8_financiamiento_id,i8_".$clase."_id,i8_contribuyente_id,i4_periodos_mora,i6_desde,i6_hasta,c2_fiesta,c2_interes,
			       c2_multa,c2_icc,i2_cuotas,d82_mora,d82_cargo_corriente,v32_tipo_suscriptor,txt_financiamiento,c8_estado,c8_tipo_cuota,ts_update,ts_in
		        FROM sdd_".$clase."_financiamientos
		        WHERE i8_".$clase."_id='".$id."' and c8_estado='ACTIVO'
		        LIMIT 1";
	#print "$query<br>";
	if (!($dbh = new Win32::ODBC($appid))){connect_fail($appid)}
	if ( $dbh->Sql($query) ){SQL_fail($dbh,$query,'sub get_financiamientos')};
	if($dbh->FetchRow()){
		@financiamiento = $dbh->Data();
		$fa = "SI"
	}
	return $fa;
}
sub get_financiamiento_detalles {
	my %finan_d = ();
	my $query   = "SELECT dt_vencimiento,i4_periodos_mora,i6_desde,i6_hasta,d82_mora,d82_cargo_corriente,d82_cuota,c8_estado,dt_pago
		       FROM sdd_".$clase."_financiamientos_detalle
		       WHERE  i8_financiamiento_id='".$financiamiento[0]."' order by dt_vencimiento desc";
	if (!($dbh = new Win32::ODBC($appid))){connect_fail($appid)}
	if ( $dbh->Sql($query) ){SQL_fail($dbh,$query,'sub get_financiamiento_detalles')};
	while($dbh->FetchRow()){
		undef my @row;
		@row	= $dbh->Data();
		$financiamiento_detalle{$row[0]}=[@row]
	}
}
sub construct_page {
	my $count   = 0;
	my $detail1 = "";
	foreach $key(sort keys %financiamiento_detalle){
		$count++;
		my @detalle = @{$financiamiento_detalle{$key}};
		$detail1 .= $detail;
		for (my $i=0;$i<=$#detalle;$i++){
			$detail1 =~ s/{$campos_d[$i]}/$detalle[$i]/g;
			$detail1 =~ s/{count}/$count/g;	
		}
	}
	for (my $i=0;$i<=$#financiamiento;$i++){
		$master   =~ s/{$campos[$i]}/$financiamiento[$i]/g;
		$detail_h =~ s/{rowset}/$detail1/g;
	}
	print "$finan_activo~$master$detail_h";
}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#AUXILIAR-SUBROUTINES
sub plantillas {
	@campos   = split(/\,/,'i8_financiamiento_id,id,i8_contribuyente_id,i4_periodos_mora,i6_desde,i6_hasta,c2_fiesta,c2_interes,c2_multa,c2_icc,i2_cuotas,d82_mora,d82_cargo_corriente,v32_tipo_suscriptor,txt_financiamiento,c8_estado,c8_tipo_cuota,ts_update,ts_in');
	@campos_d = split(/\,/,'dt_vencimiento,i4_periodos_mora,i6_desde,i6_hasta,d82_mora,d82_cargo_corriente,d82_cuota,c8_estado,dt_pago');
	$master = "<TABLE id=plan_pago>
				    <TR>
						<TH>FINANCIAMIENTO</TH>				    
						<TH>PERIODOS</TH>
						<TH>DESDE</TH>
						<TH>HASTA</TH>
						<TH>MORA</TH>
						<TH>MENSUALIDAD</TH>
						<TH>CUOTAS</TH>
						<TH>FIESTAS</TH>	    
						<TH>INTERES</TH>
						<TH>MULTA</TH>
						<TH>MENSUALIDAD</TH>
						<TH>TIPO CUOTA</TH>
						<TH>SALIR A</TH>
				    </TR>
				    <TR>
						<TD>{i8_financiamiento_id}</TD>
						<TD>{i4_periodos_mora}</td>
						<TD>{i6_desde}</td>
						<TD>{i6_hasta}</td>
						<TD>{d82_mora}</td>		
						<TD>{d82_cargo_corriente}</td>
						<TD>{i2_cuotas}</TD>
						<TD>{c2_fiesta}</td>
						<TD>{c2_interes}</td>
						<TD>{c2_multa}</td>
						<TD>{c2_icc}</td>
						<TD>{c8_tipo_cuota}</td>
						<TD><input type=button name=IrEstado value='ESTADO CUENTA' onClick=GoEstado() class=button10></td>
				    </TR>
				</TABLE>
				";
	$detail_h = "<TABLE id=plan_pago_detalle>
			<TR>
			    <TH>No</TH>
			    <TH>VENCIMIENTO</TH>
			    <TH>PERIODOS</TH>
			    <TH>DESDE</TH>
			    <TH>HASTA</TH>	    
			    <TH>MORA</TH>
			    <TH>MENSUALIDAD</TH>
			    <TH>CUOTA</TH>
			    <TH>ESTADO</TH>
			    <TH>PAGADO EL</TH>
			    <TH><input type=button name=Cuota value='PAGAR' onClick=PagarCuota() class=button10></TH>			    
			</TR>{rowset}
		    </TABLE>";
	$detail = "<TR>
			    <TD>{count}</TD>
			    <TD>{dt_vencimiento}</TD>
			    <TD>{i4_periodos_mora}</TD>
			    <TD>{i6_desde}</TD>
			    <TD>{i6_hasta}</TD>	    
			    <TD>{d82_mora}</TD>
			    <TD>{d82_cargo_corriente}</TD>
			    <TD>{d82_cuota}</TD>
			    <TD>{c8_estado}</TD>
			    <TD>{dt_pago}</TD>
			    <TD></TD>			    
			</TR>";		    
}
sub SQL_fail {
    my ($dbh,$query,$section)= @_;
    print "SQL failed in Section $section<br>";
    print "Query: ".$query."<br>";    
    print "Error: ".$dbh->Error()."\n<br>";
    print "la clase: $clase<br>";
    $dbh->Close();
    exit;
}
sub connect_fail {
    my ($DSN)=@_;
    print "Error connecting to $DSN<br>";
    print "Error:".Win32::ODBC::Error()."<br>";
    exit;
}
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------