#!/perl/bin/perl.exe
$|=1;
#-------------------------------------------------------------------
#-------------------------------------------------------------------
use Win32::ODBC;
use CGI;
#-------------------------------------------------------------------
#-------------------------------------------------------------------
print "Content-type: text/html\n\n";
#-------------------------------------------------------------------
#-------------------------------------------------------------------
$form 		= new CGI;
$lan		= $form->param('lan');
$appid		= $form->param('appid');
$expediente	= $form->param('i8_expediente_id');
$tipo		= $form->param('tipo');
#---------------------------------------------------------------
#-------------------------------------------------------------------
if (!($db = new Win32::ODBC($appid) ) ) {print "Error connecting to $appid <br>";print "Error: " . Win32::ODBC::Error() . "<br>";exit;}
#---------------------------------------------------------------
#---------------------------------------------------------------
$is_ok=get_nuc_expediente();
if($is_ok>0){print "true";exit;}
else{asociar_expediente();print "true";exit;}
$db->Close();
exit;
#-------------------------------------------------------
#SUB-ROUTINES
sub get_nuc_expediente {
	$cnt=0;	
	$sql="select count(*) from sdd_nuc_expedientes where i8_expediente_id='$expediente'";
	if($db->Sql($sql)){print "SQL failed1.\n<br>";print "Error: ".$db->Error()."\n";$db->Close();exit;}
	while($db->FetchRow()){
		undef @row;
		@row	= $db->Data();
		$cnt	= $row[0];				
	}
	return $cnt;
}
sub asociar_expediente {
	$generador="";
	$clase="";
	$i8_contribuyente_id="";
	if($tipo==1){$generador="INM";$clase="inmueble";}else{$generador="EMP";$clase="empresa";}
	##vamos a crear la nuc para el titular de ese contribuyente
	$sql="select i8_contribuyente_id from sdd_".$clase."_contribuyentes where i8_".$clase."_id=$expediente";
	
	if($db->Sql($sql)){print "SQL failed1.\n<br>";print "$query<br>";print "Error: ".$db_cc->Error()."\n";$db_cc->Close();exit;}
	while($db->FetchRow()){
		undef @row;
		@row	= $db->Data();
		$i8_contribuyente_id	= $row[0];				
		
	}
	$sql="select v128_contribuyente from sdm_contribuyentes where i8_contribuyente_id='$i8_contribuyente_id'";
	
	if($db->Sql($sql)){print "SQL failed1.\n<br>";print "$query<br>";print "Error: ".$db_cc->Error()."\n";$db_cc->Close();exit;}
	while($db->FetchRow()){
		undef @row;
		@row			= $db->Data();
		$v128_contribuyente	= $row[0];						
	}
	$sql="insert into sdm_nucs (v128_nuc,ts_in,ts_update) values(\"$v128_contribuyente\",now(),now())";
	
	if($db->Sql($sql)){print "SQL failed1.\n<br>";print "$query<br>";print "Error: ".$db_cc->Error()."\n";$db_cc->Close();exit;}
	
	##getting last nuc inserted
	$i8_nuc_id=0;
	$sql="select i8_nuc_id from sdm_nucs order by ts_in desc limit 1";
	
	if($db->Sql($sql)){print "SQL failed1.\n<br>";print "$query<br>";print "Error: ".$db_cc->Error()."\n";$db_cc->Close();exit;}
	while($db->FetchRow()){
		undef @row;
		@row	   = $db->Data();
		$i8_nuc_id = $row[0];						
	}
	##association of cibtrubtebte
	$sql="insert into sdd_nuc_contribuyentes (i8_nuc_id,i8_contribuyente_id,i2_tipo_contribuyente_id,ts_in,ts_update)
	values($i8_nuc_id,$i8_contribuyente_id,1,now(),now())";
	
	if($db->Sql($sql)){print "SQL failed1.\n<br>";print "$query<br>";print "Error: ".$db_cc->Error()."\n";$db_cc->Close();exit;}
	
	##association of the file to the nuc,
	if($generador eq "INM"){
		$i8_nic_id=0;
		$sql="select i8_nic_id from sdd_".$clase."_suministro_nics where i8_".$clase."_id='$expediente'";
		
		if($db->Sql($sql)){print "SQL failed1.\n<br>";print "$query<br>";print "Error: ".$db_cc->Error()."\n";$db_cc->Close();exit;}
		while($db->FetchRow()){
			undef @row;
			@row	   = $db->Data();
			$i8_nic_id = $row[0];						
		}
		$sql="insert into sdd_nuc_expedientes (i8_nuc_id,i8_expediente_id,c3_tipo_generador_id,i8_nis_id,ts_in,ts_update)
		values($i8_nuc_id,$expediente,'INM',$i8_nic_id,now(),now())";
		
		if($db->Sql($sql)){print "SQL failed1.\n<br>";print "$query<br>";print "Error: ".$db->Error()."\n";$db->Close();exit;}
		
	}
	if($generador eq "EMP"){
		$sql="insert into sdd_nuc_expedientes (i8_nuc_id,i8_expediente_id,c3_tipo_generador_id,i8_nis_id,ts_in,ts_update)
		values($i8_nuc_id,$expediente,'EMP',0,now(),now())";
		
		if($db->Sql($sql)){print "SQL failed1.\n<br>";print "$query<br>";print "Error: ".$db->Error()."\n";$db->Close();exit;}
	}
}