#!/perl/bin/perl.exe
$|=1;
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# Web Software SA
# Date In: 2022-11-30
# Updates: 2022-11-30
# Author:Rodolfo Machon
# File Name: agua_notificacion_rept.pl
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# PERL MODULES
use CGI;
use DBI;
use XML::Simple;
use Data::Dumper;
use CGI::Carp qw(fatalsToBrowser);
binmode STDOUT, ":encoding(utf8)";
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# HTML PRINTING ACTIVATION
print "Content-type: text/html; charset=utf-8\n\n";
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# PERL MODULES
$form 	= new CGI;
$id     = $form->param('id');
$periodo= $form->param('period');
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# GET CLASE SCHEMA AND VARIABLES
get_app_data();
db_connect();
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
$clase = "agua";
$PATH  = "../".$lan."/";
$file  = $lan."_agua_notificacion_rept.html";
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#MAIN PROGRAMM
($year,$month,$day) = Today();
if(length($month)<2){$month = "0".$month}
$i8_periodo_actual = $year.$month;
procesar_expedientes();

exit;
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# AUXILIAR-SUBROUTINES
sub procesar_expedientes {
    @expedientes = split(/\|/,$str_expedientes);
    for($i=0;$i<=$#expedientes;$i++) {
        my $id		      = $expedientes[$i];
        my $nombre	    = get_nombre('sdd_'.$clase.'_contribuyentes a,sdm_contribuyentes b','b.v128_contribuyente',$id);
        my $direccion	  = get_direccion($id);
        my $nivel	      = get_nivel($id);
        
        my ($total_mor,$desde_mor,$hasta_mor) = get_mora($id);
        my ($total_cor,$desde_cor,$hasta_cor) = get_corriente($id);
        
        my $total = $total_mor+$total_cor;
        my $table = construct_table($total_mor,$total_cor,$total,$desde_mor,$hasta_mor,$desde_cor,$hasta_cor,$id);
        
        construct_page($id,$nombre,$direccion,$total,($nivel*1+1),$table,$desde_mor,$hasta_mor);
    }
}
sub get_nombre{
    my ($tables,$fields,$id)= @_;
    my $r     = "";
    my $rs    = "";
    my $sql   = "SELECT $fields FROM $tables WHERE a.i8_".$clase."_id='$id' and a.i8_contribuyente_id=b.i8_contribuyente_id  and (i2_tipo_contribuyente_id='1' or i2_tipo_contribuyente_id='3')";    
    #print "$sql<br>";
    my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_nombre');
	while (my @row = $sth->fetchrow_array) {
	    $r	 = $row[0];
    }
    return $r;
}
sub get_direccion {
	my($expediente_id)=@_;
	$direccion = "";
	my $sql ="select c11_sector_id,c4_calle_id,c4_avenida_id,c2_pasaje_id,c2_senda_id,c4_casa,c2_block_id,c2_poligono_id from sdm_".$clase."s where i8_".$clase."_id='$expediente_id'";
    if($clase eq "empresa"){$sql="select c11_sector_id,c4_calle_id,c4_avenida_id,c2_pasaje_id,c2_senda_id,c4_casa,c2_block_id,c2_poligono_id from sdd_".$clase."_ubicacion where i8_".$clase."_id='$expediente_id'";}
	#print "<br>get_direccion: $sql<br>";
    my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_direccion');
	while (my @row = $sth->fetchrow_array) {
		$sector_id	= $row[0];
		$calle_id	= $row[1];
		$avenida_id	= $row[2];
		$pasaje_id	= $row[3];
		$senda_id	= $row[4];
		$casa		= $row[5];
                $block_id	= $row[6];
                $poligono_id	= $row[7];
		$sql="select a.i2_tipo_sector_id, a.v64_sector, b.v64_tipo_sector from sdm_sectores a left join sdm_tipo_sectores b
                on a.i2_tipo_sector_id=b.i2_tipo_sector_id where a.c11_sector_id='$sector_id'";
		if($db1->Sql($sql)){print "SQL failed2.\n";print "Error: " . $db1->Error() . "\n";$db1->Close();exit;}
		if($db1->FetchRow()) {
			undef @row1;
			@row1       = $db1->Data();
			$sector     = $row1[1];
                        $tipo_sector= $row1[2];
			$direccion  = $tipo_sector." ".$sector;
		}
		$sql="select v64_calle from sdd_sector_calles where c4_calle_id='$calle_id' and c11_sector_id='$sector_id'";
		if($db1->Sql($sql)){print "SQL failed2.\n";print "Error: " . $db1->Error() . "\n";$db1->Close();exit;}
		if($db1->FetchRow()){undef @row2;@row2=$db1->Data();$calle=$row2[0];$direccion=$direccion.", Calle ".$calle;}
		$sql="select v64_avenida from sdd_sector_avenidas where c4_avenida_id='$avenida_id' and c11_sector_id='$sector_id'";
		if($db1->Sql($sql)){print "SQL failed2.\n";print "Error: " . $db1->Error() . "\n";$db1->Close();exit;}
		if($db1->FetchRow()){undef @row2;@row2=$db1->Data();$avenida=$row2[0];$direccion=$direccion.", Avenida ".$avenida;}
		$sql="select v64_pasaje from sdd_sector_pasajes where c2_pasaje_id='$pasaje_id' and c11_sector_id='$sector_id'";
		if($db1->Sql($sql)){print "SQL failed2.\n";print "Error: " . $db1->Error() . "\n";$db1->Close();exit;}
		if($db1->FetchRow()){undef @row2;@row2=$db1->Data();$pasaje=$row2[0];$direccion=$direccion.", Pasaje ".$pasaje;}
		$sql="select v64_senda from sdd_sector_sendas where c2_senda_id='$senda_id' and c11_sector_id='$sector_id'";
		if($db1->Sql($sql)){print "SQL failed2.\n";print "Error: " . $db1->Error() . "\n";$db1->Close();exit;}
		if($db1->FetchRow()){undef @row2;@row2=$db1->Data();$senda=$row2[0];$direccion=$direccion.", Senda ".$senda;}
                $sql="select v64_block from sdd_sector_blocks where c2_block_id='$block_id' and c11_sector_id='$sector_id'";
		if($db1->Sql($sql)){print "SQL failed2.\n";print "Error: " . $db1->Error() . "\n";$db1->Close();exit;}
		if($db1->FetchRow()){undef @row2;@row2=$db1->Data();$block=$row2[0];$direccion=$direccion.",".$block;}
                $sql="select v64_poligono from sdd_sector_poligonos where c2_poligono_id='$poligono_id' and c11_sector_id='$sector_id'";
		if($db1->Sql($sql)){print "SQL failed2.\n";print "Error: " . $db1->Error() . "\n";$db1->Close();exit;}
		if($db1->FetchRow()){undef @row2;@row2=$db1->Data();$poligono=$row2[0];$direccion=$direccion.",".$poligono;}
                if($direccion ne ""){
                    $direccion=$direccion." ".$casa;
                }
	}
	return $direccion;
}
sub get_nivel {
    my($id)=@_;
    my $nivel = 0;
    my $sql = "SELECT a.i2_tipo_notificacion_id FROM sdd_".$clase."_administrativo a WHERE a.i8_".$clase."_id='$id' and c8_gestion_estado='ACTIVO' order by a.dt_administrativo desc limit 1";    
    #print "$query<br>";
    my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_nivel');
	while (my @row = $sth->fetchrow_array) {
	    $nivel = $row[0];
    }
    if($nivel>=5){$nivel=0}
    return $nivel;
}
sub get_mora {
    my($id) = @_;
    my $des = 0;    
    my $has = 0;
    my $tm  = 0;
    my $sql = "select sum(d82_val)+ sum(d82_fie)+ sum(d82_int)+ sum(d82_mul),MIN(i8_periodo),MAX(i8_periodo) from sdd_".$clase."_estados_mensuales where i8_".$clase."_id='$id' and i4_dias_mora>0 and d82_val>0";
    my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_mora');
	while (my @row = $sth->fetchrow_array) {
        $tm   = $row[0];
        $des  = $row[1];
        $has  = $row[2];	
    }
    my $mes = substr($des,4,2);
    my $ano = substr($des,0,4);
    $des    = "01/".$mes."/".$ano;
    my $ano1 = substr($has,0,4);
    my $mes1 = substr($has,4,2);
    my $dia  = Days_in_Month($ano1,$mes1);
    $has     = $dia."/".$mes1."/".$ano1;
    return $tm,$des,$has;
}
sub get_corriente {
    my($id) = @_;
    my $des = 0;    
    my $has = 0;
    my $tm  = 0;
    my $sql = "select (d82_val)+ (d82_fie)+ (d82_int)+ (d82_mul),MIN(i8_periodo),MAX(i8_periodo) from sdd_".$clase."_estados_mensuales where i8_".$clase."_id='$id' and i4_dias_mora=0 and d82_val>0 and i8_periodo<='$i8_periodo_actual'";
   #print "$sql";
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_corriente');
	while (my @row = $sth->fetchrow_array) {
        $tm   = $row[0];
        $des  = $row[1];
        $has  = $row[2];	
    }
    my $mes = substr($des,4,2);
    my $ano = substr($des,0,4);
    $des    = "01/".$mes."/".$ano;

    my $ano1 = substr($has,0,4);
    my $mes1 = substr($has,4,2);
    my $dia  = Days_in_Month($ano1,$mes1);
    $has     = $dia."/".$mes1."/".$ano1;
    #print "$tm,$des,$has<BR>"; 
    return $tm,$des,$has;
}
sub construct_table {
    my ($total_mor,$total_cor,$total,$desde_mor,$hasta_mor,$desde_cor,$hasta_cor,$id)= @_;
    my $query         = "";
    my $r	          = "";
    my $rs	          = "";
    my $suma_fila     = "";
    my $suma_monto    = "";
    my $suma_fiestas  = "";
    my $suma_intereses= "";
    my $suma_multas   = "";
    my $dias_mora     = "";
    my $suma_mora     = "";
    my $suma_total       = "";
    my $contador      = "";
    my $template_table = "<TABLE>{body}</TABLE>";
    my $fields        = "b.v64_tributo,MIN(a.i8_periodo),MAX(a.i8_periodo),sum(a.d82_val),sum(a.d82_fie),sum(a.d82_int),sum(a.d82_mul),MAX(a.i4_dias_mora)";
    my $tables        = "sdd_".$clase."_estados_mensuales a,sdm_tributos b";
    if($clase eq "inmueble"){$query = "SELECT $fields FROM $tables WHERE i8_inmueble_id='$id' and substring(a.c16_tarifa_id,1,8)=b.c8_tributo_id and a.i4_dias_mora>0 GROUP BY substring(c16_tarifa_id,1,8) order by a.i8_periodo desc";}
    if($clase eq "empresa"){$query = "SELECT $fields FROM $tables WHERE i8_empresa_id='$id' and substring(a.c16_tarifa_id,1,8)=b.c8_tributo_id GROUP BY substring(c16_tarifa_id,1,8) order by a.i8_periodo desc";}
      #print "$query<br>";
          
          #Aqui se le pone la primera fila de Título
          $template_rows="";
          $contador = 0;
          $template_rows=$template_rows."<TR><TD width=175px><b>DETALLE</b></TD><TD align=center><b>MORA</b></TD></TR>";
          $vv="\$";
      #print "$query";
    if ($db->Sql($query)){SQL_fail($db,$query,'sub get_records')};
    while($db->FetchRow()){				
        $contador++;
        undef my @row;
        @row	= $db->Data();
        #print "$contador <br>";
        $suma_fila="";
        $row[3] 	        = sprintf("%.2f",$row[3]);
        $row[4] 	        = sprintf("%.2f",$row[4]);
        $row[5] 	        = sprintf("%.2f",$row[5]);
        $row[6] 	        = sprintf("%.2f",$row[6]);
        $suma_fila	        = $row[3]+$row[4];##sumando valor y fiesta
        $suma_fila	        = sprintf("%.2f",$suma_fila);
        $valor_tributo	    = $row[3];
        $suma_monto        += $row[3];
        $suma_monto	        = sprintf("%.2f",$suma_monto);
        $suma_fiestas      += $row[4];
        $suma_fiesta	    = sprintf("%.2f",$suma_fiesta);
        $suma_intereses    += $row[5];
        $suma_multas       += $row[6];
        
        $suma_total        +=$suma_fila;
        
        $suma_total        +=$row[5];
        $suma_total        +=$row[6];
        
        $r	= join('|',@row);
        $rs	.= $r."~~";
        $i8_periodo_desde   =$row[1];
        $i8_periodo_hasta   =$row[2];
        $dias_mora          =$row[7];
        $template_rows      =$template_rows."<TR><TD>$row[0]</TD><TD>$vv$valor_tributo</TD></TR>"; 
    }
    #print "$template_rows";
            $contador++;
    if($suma_multas>0 && $suma_multas<2.86){$suma_total=$suma_total-$suma_multas+2.86;$suma_multas=2.86;}
    $template_rows	    = $template_rows."<TR><TD>FIESTAS</TD><TD>$vv$suma_fiestas</TD></TR>";
    $template_rows	    = $template_rows."<TR><TD>INTERESES</TD><TD>$vv$suma_intereses</TD></TR>";
    $template_rows	    = $template_rows."<TR><TD>MULTAS</TD><TD>$vv$suma_multas</TD></TR>";
    $template_rows      =$template_rows."<TR><TD><b>TOTAL MORA</b></TD><TD>$vv$suma_total</TD></TR>";
    $template_rows      =$template_rows."<TR><TD><b>COBRO CORRIENTE</b></TD><TD>".big_money($total_cor)."</TD></TR>";
    #$template_rows      =$template_rows."<TR><TD><b>TOTAL A PAGAR</b></TD><TD>".big_money($suma_total+$total_cor)."</TD></TR>";
        
    $table = $template_table;
    $table =~ s/{body}/$template_rows/g;
        #print "yo $table";
    return $table;
}
sub construct_table1 {
    my ($total_mor,$total_cor,$total,$desde_mor,$hasta_mor,$desde_cor,$hasta_cor)= @_;
    my $mora	  = big_money($total_mor);
    my $corriente = big_money($total_cor);
    my $deuda	  = big_money($total);
    my $tab = "<table class=Grid>
		    <tr><td>MORA DESDE $desde_mor HASTA $hasta_mor</td><td align=right>$mora</td></tr>
		    <tr><td>MESES CORRIENTES DESDE $desde_cor HASTA $hasta_cor</td><td align=right>$corriente</td></tr>
		    <tr><td><b>TOTAL</b></td><td align=right><b>$deuda</b></td></tr>
		</table>";
    return $tab;
}


sub construct_page {
    my ($id,$nombre,$direccion,$total,$nivel,$table,$desde_mor,$hasta_mor)= @_;
    my $arc	= $file;
    my $pagina	= "";
    my $todate	= "<script language='javascript'>document.write(dateString)</script>";
    my $deuda	= big_money($total);        
    $arc 	=~ s/\{nivel}/$nivel/g;
    $arc 	=~ s/\{clase}/$clase/g;
    open (DATA, "$PATH$arc");        
    #print "<br>$PATH$arc<br>";
    while ($line = <DATA>) {	
	$line =~ s/\{lan}/$lan/g;
	$line =~ s/\{appid}/$appid/g;
	$line =~ s/\{id}/$id/g;
	$line =~ s/\{nombre}/$nombre/g;
	$line =~ s/\{direccion}/$direccion/g;
	$line =~ s/\{generador}/$clase/g;
	$line =~ s/\{table}/$table/g;
	$line =~ s/\{deuda}/$deuda/g;
	$line =~ s/\{recibio}//g;
  $line =~ s/\{i8_periodo_desde}/$desde_mor/g;
  $line =~ s/\{i8_periodo_hasta}/$hasta_mor/g;         
	$line =~ s/\{dui}//g;
	$line =~ s/\{telefono}//g;
	$line =~ s/\{relacion}//g;
	$line =~ s/\{todate}/$todate/g;
	$line =~ s/\{entregado}//g;
	$pagina .= $line;
    }
    print "$pagina";
    return 0;
}
sub SQL_fail {
    ($dbh,$query,$section)= @_;
    print "SQL failed in Section $section<br>";
    print "Error: ".$dbh->Error()."\n<br>";
    print "Query: ".$query."<br>";    
    $dbh->Close();
    exit;
}
sub big_money {
  my $number = sprintf "%.2f", shift @_;
  # Add one comma each time through the do-nothing loop
  1 while $number =~ s/^(-?\d+)(\d\d\d)/$1,$2/;
  # Put the dollar sign in the right place
  $number =~ s/^(-?)/$1\$/;
  $number;
}
sub imprimir_notificacion {
        my($i,$id,$dir,$periodo,$periodo1)=@_;
	
       # $i8_periodo = get_ultimo_periodo($id);
	#$i8_periodo1 = get_min_periodo($id);
	#print "$i8_periodo<br>";
       # $total_mora = get_total_mora($id);
	#print "$total_mora<br>";
        #$tabla      = get_records($i8_periodo,'sdd_'.$clase.'_estados_mensuales a,sdm_tributos b','b.v64_tributo,MIN(a.i8_periodo),MAX(a.i8_periodo),sum(a.d82_val),sum(a.d82_fie),sum(a.d82_int),sum(a.d82_mul)');        	
	#print "$tabla<br>";
          
	#print "$i";
        construct_page($tabla,$nombre,$dir,$id,$periodo,$periodo1);
}
sub get_records{
	my ($i8_periodo,$tables,$fields)= @_;
	my $r	  	   = "";
	my $rs	  	   = "";
    my $suma_monto 	   = "";
    my $suma_fiestas   = "";
    my $suma_intereses = "";
    my $suma_multas    = "";
    my $contador 	   = 0;
    my $sql 	   = "SELECT $fields FROM $tables WHERE i8_".$clase."_id='$id' and substring(a.c16_tarifa_id,1,8)=b.c8_tributo_id GROUP BY substring(c16_tarifa_id,1,8)";
    #print "$query";
	$vv="\$";
	$template_rows = "";
	$template_rows  = $template_rows."<TR class=my_style3><TD width=175px>TARIFAS</TD><TD align=center>MONTO</TD></TR>";
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_records');
	while (my @row = $sth->fetchrow_array) {
		$row[3] 	 = sprintf("%.2f",$row[3]);
		$row[4] 	 = sprintf("%.2f",$row[4]);
		$row[5] 	 = sprintf("%.2f",$row[5]);
		$row[6] 	 = sprintf("%.2f",$row[6]);
        $valor_tributo   = $row[3];
        $suma_monto     += $row[3];
        $suma_fiestas   += $row[4];
        $suma_intereses += $row[5];
        $suma_multas    += $row[6];
        $r		 = join('|',@row);
		$rs		.= $r."~~";
		$template_rows   = $template_rows."<TR class=my_style3><TD>$row[0]</TD><TD align=center>$vv$valor_tributo</TD></TR>"; 
	}
    if($suma_multas<2.86){$suma_multas=2.86}
    $template_rows	= $template_rows."<TR class=my_style3><TD>Fiestas</TD><TD align=center>$vv$suma_fiestas</TD></TR>";
    $template_rows	= $template_rows."<TR class=my_style3><TD>Intereses</TD><TD align=center>$vv$suma_intereses</TD></TR>";
    $template_rows	= $template_rows."<TR class=my_style3><TD>Multas</TD><TD align=center>$vv$suma_multas</TD></TR>";
    $suma_total	= $suma_monto+$suma_fiestas+$suma_intereses+$suma_multas;
    $template_rows	= $template_rows."<TR class=my_style3><TD>TOTAL MORAS</TD><TD align=center>$vv$suma_total</TD></TR>";
    $table 		= $template_table;
    $table 		=~ s/{body}/$template_rows/g;
	if(substr($id,0,1) eq "1"){$clase="inmueble"}
	if(substr($id,0,1) eq "2"){$clase="empresa"}
	return $table;	
}
#----------------
sub get_min_periodo {
    my ($id)=@_;
    my $dt  = "";
    my $sql = "select min(i8_periodo) from sdd_".$clase."_estados_mensuales where i8_".$clase."_id='$id'";
    #print "$sql<br>";
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_min_periodo');
	while (my @row = $sth->fetchrow_array) {
        $dt    = $row[0];
    }
    my $mes = substr($dt,4,2);
    my $ano = substr($dt,0,4);

    return "01/".$mes."/".$ano; 
}
sub get_ultimo_periodo {
    my($id)=@_;
    my $dt  = "";
    my $sql = "select max(i8_periodo) from sdd_".$clase."_estados_mensuales where i8_".$clase."_id='$id'";
    #print "$sql<br>";
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_ultimo_periodo');
	while (my @row = $sth->fetchrow_array) {
        $dt   = $row[0];
    }
    my $ano = substr($dt,0,4);
    my $mes = substr($dt,4,2);
    my $dia = Days_in_Month($ano,$mes);

    return $dia."/".$mes."/".$ano;
}
sub get_total_mora {
    my($id) = @_;
    my $tm  = 0;
    my $sql = "select sum(d82_val)+ sum(d82_fie)+ sum(d82_int)+ sum(d82_mul) from sdd_".$clase."_estados_mensuales where i8_".$clase."_id='$id' and i4_dias_mora>0 and d82_val>0";
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_total_mora');
	while (my @row = $sth->fetchrow_array) {
        $tm   = $row[0];
    }
    $tm = big_money($tm);	    
    return $tm;
}
sub get_notificacion {
    my($id) = @_;
    my $sql = "select a.v64_persona,a.c11_dui_id,b.v64_tipo_contribuyente,a.c16_telefono,a.dt_emision,a.dt_entrega,a.i2_tipo_contribuyente_id from sdd_".$clase."_administrativo a left join sdm_tipo_contribuyentes b on a.i2_tipo_contribuyente_id=b.i2_tipo_contribuyente_id where a.i8_".$clase."_id='$id' and a.i4_gestion_id='$gid' and a.i2_tipo_notificacion_id='$nivel'";
    #print "$sql<br>";
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_nombre');
	if (@row = $sth->fetchrow_array) {

    }
    if($row[6] eq "10"){$row[2]="PADRE"}
    if($row[6] eq "11"){$row[2]="MADRE"}
    if($row[6] eq "12"){$row[2]="ESPOSA"}
    if($row[6] eq "14"){$row[2]="HERMANO"}
    if($row[6] eq "15"){$row[2]="TIO(A)"}
    if($row[6] eq "16"){$row[2]="PRIMO(A)"}
    if($row[6] eq "17"){$row[2]="VECINO(A)"}
    if($row[6] eq "13"){$row[2]="NO APLICA"}
    return $row[0],$row[1],$row[2],$row[3],$row[4],$row[5];
}
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# AUXILIAR-SUBROUTINES
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};
	$city	    = $data->{portal}->{city};
	$state      = $data->{portal}->{state};
    $country    = $data->{portal}->{country};
}
sub SQL_fail {
    my ($query,$dbh,$section)= @_;
    print "SQL failed in Section $section<br>";
    print "Query: ".$query."<br>";    
    exit;
}
sub db_connect {
	$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 * 1024;	
}
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------