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

#---------------------------------------------------------------
#---------------------------------------------------------------
if ( !( $db_tes = new Win32::ODBC($appid) ) ) {print "Error conecting to $appid";exit;}
if ( !( $db_tes1 = new Win32::ODBC($appid) ) ) {print "Error conecting to $appid";exit;}
#-------------------------------------------------------
$PATH		= "../".$lan."/";
$file    	= $lan."_desecho_historico.html";

#################llamado a la funcion que organiza la busqueda de los inmuebles a los que se les sacará la notificacion
get_cargos();
construct_page();
#################
#construct_page();

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

exit;
#-------------------------------------------------------------------------
# AUXILIAR-SUBROUTINES

sub get_cargos {
    ##para el calculo
    ##getting current date for calculation of interese
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);
    $year=$year+1900;
    
    $mon=$mon+1;
    if($mon<10){$mon="0".$mon}
    if($mday<10){$mday="0".$mday}
    $dt_actual=$year."-".$mon."-".$mday;
    
   
    
    ##gettin datos de la cuenta
    $sql="select v128_nombre, txt_direccion,d82_dfds,c8_cuenta_id from sdd_desecho_tdatosgen where i8_nic_id like '$i8_nic_id%' and c8_cuenta_id like '$c8_cuenta_id%'";
    
    if($db_tes1->Sql($sql)){print "Error:$db_test->Error()";exit;}
    while( $db_tes1->FetchRow() )
    {
        undef my @row;
        @row = $db_tes1->Data();
        $v128_nombre=$row[0];
        $txt_direccion=$row[1];
        $d82_dfds=$row[2];
        $cuenta=$row[3];
        
    }
    
    	
        $sql="select i8_periodo,(d82_valor-d82_pagado),c2_procesado from sdd_desecho_tcargos where (i8_nic_id = '$i8_nic_id' and c8_cuenta_id ='$c8_cuenta_id') and d82_valor!=d82_pagado order by i8_periodo asc";
        
        $contador=1;
        $diferencia=0;#variable para guardar el d82_valor que se pago en exceso
        if($db_tes->Sql($sql)){print "Error:$db_tes->Error();<br>$sql";exit;}
        $table= "<table border=0 width='100%'>";
        $table.= "<tr><td class=my_style2>PERIODO</td><td class=my_style2>VALOR</td><td class=my_style2>FIESTA</td><td class=my_style2>INTERES<input type='checkbox' name='interes' checked onClick=CleanColum()></td><td class=my_style2>SUBTOTAL</td></TR>";
        while( $db_tes->FetchRow() )
        {
            undef my @row1;
            @row1 = $db_tes->Data();
            $i8_periodo=$row1[0];
            $dt_inicio=substr($i8_periodo,0,4)."-".substr($i8_periodo,4,2)."-"."01";
            $dias_mora=get_dias_mora($dt_inicio,$dt_actual);
            $d82_valor=$row1[1];            
            $c2_procesado=$row1[2];
            if($d82_valor>0)
            {
            	$fiesta=$d82_valor*0.05;
            	$interes=$d82_valor*$dias_mora*0.07/365;
            }
            else
            {
            	$fiesta=0;
            	$interes=0;
            }
            $d82_valor=sprintf("%.2f",$d82_valor);
            $fiesta=sprintf("%.2f",$fiesta);
            $interes=sprintf("%.2f",$interes);
            if($d82_valor>0){$subtotal=$d82_valor+$fiesta+$interes;}
            else{$subtotal=0}
            $total_d82_valor+=$d82_valor;
            $total_fiesta+=$fiesta;
            $total_interes+=$interes;
            $total_sub_total+=$subtotal;
            
            
                $table.= "<tr><td class=my_style2><input type=checkbox name=per_$contador checked onClick='CleanRow($contador)'><input type=text class='my_style' name=i8_periodo_$contador value=$i8_periodo readonly ondblClick=OpenForm($i8_periodo)></td><td class=my_style2><input type=text class='my_style' name=d82_valor_$contador value=$d82_valor readonly></td><td class=my_style2><input type=text class='my_style' name=fiesta_$contador value=$fiesta readonly></td><td class=my_style2><input type=text class='my_style' name=interes_$contador value=$interes readonly></td><td class=my_style2><input type=text class='my_style' name=sub_total_$contador value=$subtotal readonly></td></tr>";
                $total_cargo+=$d82_valor;
            
            $contador++;
        }
        $total_d82_valor=sprintf("%.2f",$total_d82_valor);
        $total_fiesta=sprintf("%.2f",$total_fiesta);
        $total_interes=sprintf("%.2f",$total_interes);
        $total_sub_total=sprintf("%.2f",$total_sub_total);
        $total=$total_d82_valor+$total_fiesta+$total_interes;
        $total=sprintf("%.2f",$total);
            
        $table.="<tr><td class=my_style2>&nbsp;</td><td class=my_style3><input type=text class='my_style' name=total_d82_valor value= $total_d82_valor></td><td class=my_style3><input type=text class='my_style' name=total_fiesta value=$total_fiesta></td><td class=my_style3><input type=text class='my_style' name=total_interes value=$total_interes></td><td class=my_style3><input type=text class='my_style' name=total_sub_total value=$total_sub_total></td></tr>";
        $table.= "</table>";
    
}
sub get_dias_mora {
	my($dt_desde,$dt_hasta)=@_;
        
	$dt_temp=$dt_desde;
	$dias=0;
	while($dt_temp ne $dt_actual) {
		@dt=split(/\-/,$dt_temp);
		$y1=$dt[0];
		$m1=$dt[1];
		$d1=$dt[2];
		($y2,$m2,$d2) = Add_Delta_YMD($y1,$m1,$d1, 0, 0, 1);
		if($m2<10){$m2="0".$m2}
		if($d2<10){$d2="0".$d2}
		$dt_temp=$y2."-".$m2."-".$d2;
                
		$dias++;
	}
	$dias=$dias-60;
	#print "<br>Dias mora:$dias<br>";
	return $dias;
}
sub construct_page {  
    open (DATA, "$PATH$file");
    	while ($line = <DATA>)	{
      	$line =~ s/{appid}/$appid/g;
        $line =~ s/{lan}/$lan/g;
        $line =~ s/{i8_nic_id}/$i8_nic_id/g;
        $line =~ s/{cuenta}/$cuenta/g;
        $line =~ s/{d82_dfds}/$d82_dfds/g;
        $line =~ s/{v128_nombre}/$v128_nombre/g;
        $line =~ s/{txt_direccion}/$txt_direccion/g;
        $line =~ s/{total}/$total/g;
        $line =~ s/{contador}/$contador/g;
        $line =~ s/{i8_inmueble_id}/$i8_inmueble_id/g;
        
        $line =~ s/{table}/$table/g;
        $line =~ s/\Ã±/ñ/g;
		$line=~ s/\Ã±/ñ/g;
		$line=~ s/\Ã‘/Ñ/g;
		$line=~ s/\Ã±/ñ/g;
		$line=~ s/\Ã‘/Ñ/g;

       	print $line;
	}
}

