<%@ Language=VBScript %>
<!--#INCLUDE file="ADOVBS.inc"-->
<% 

function makeCSVFile(solicitudeFolio)
	dim str_password, str_empresa
	dim sqlQuery, dsn
	Dim STR_REGISTRO 
	Dim STR_DIVISION 
	Dim STR_DEPARTAMENTO 
	Dim STR_PUESTO 
	Dim STR_CATEGORIA
	Dim BOL_ENCONTRO        
	Dim STR_NOM_PRUEBA(30) 
	Dim STR_NOM_ARCHIVO(30)
	dim fileName

	'solicitudeFolio = Request.QueryString("folio")
	
	dsn = application("bd_sieps")	
	SET rs = server.CreateObject("ADODB.Recordset")
	SET connection = SERVER.CreateObject("ADODB.CONNECTION")
	connection.Open dsn
	
	'-- Nombre del archivo a descargar
	fileName = "UpdateFile.sie"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.CreateTextFile(fileName, True)
	
	sqlQuery = "SELECT * FROM solicitud WHERE soli_Folio = " & folio
    rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
        
	If rs.EOF = False Then
		BOL_ENCONTRO = True
		STR_NOMBRE = RES_DATOS.Fields("soli_nombre")
		STR_EMPRESA = RES_DATOS.Fields("empr_clave")
		STR_DIVISION = RES_DATOS.Fields("divi_clave")
		STR_DEPARTAMENTO = RES_DATOS.Fields("depa_clave")
		STR_PUESTO = RES_DATOS.Fields("pues_clave")
		STR_CATEGORIA = RES_DATOS.Fields("cate_clave")
		STR_REGISTRO = "Solicitud¦" & RES_DATOS.GetString(, , "¦", "¦ø")
	End If
	rs.Close
	
	objFile.WriteLine (STR_REGISTRO)
				
	' empresa
	sqlQuery = "SELECT * FROM empresa WHERE empr_clave = '" & STR_EMPRESA & "'"
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	STR_REGISTRO = Empty
	If rs.EOF = False Then
		STR_REGISTRO = "Empresa¦" & rs.GetString(, , "¦", "¦ø")
		objFile.WriteLine (STR_REGISTRO)
	End If
	rs.Close

	' division
	sqlQuery = "SELECT * FROM division WHERE divi_clave = '" & STR_DIVISION & "' AND empr_clave='" & STR_EMPRESA & "'"
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	STR_REGISTRO = Empty
	If rs.EOF = False Then
		STR_REGISTRO = "Division¦" & rs.GetString(, , "¦", "¦ø")
		objFile.WriteLine (STR_REGISTRO)
	End If
	rs.Close

	' departamento
	sqlQuery = "SELECT * FROM departamento WHERE depa_clave = '" & STR_DEPARTAMENTO & "' AND empr_clave='" & STR_EMPRESA & "' AND divi_clave='" & STR_DIVISION & "'"
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	STR_REGISTRO = Empty
	If rs.EOF = False Then
		STR_REGISTRO = "Departamento¦" & rs.GetString(, , "¦", "¦ø")
		objFile.WriteLine (STR_REGISTRO)
	End If
	rs.Close

	' puesto
	sqlQuery = "SELECT * FROM puestos WHERE pues_clave = '" & STR_PUESTO & "' AND empr_clave = '" & STR_EMPRESA & "' AND divi_clave='" & STR_DIVISION & "'"
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	STR_REGISTRO = Empty
	If rs.EOF = False Then
		STR_REGISTRO = "Puestos¦" & rs.GetString(, , "¦", "¦ø")
		objFile.WriteLine (STR_REGISTRO)
	End If
	rs.Close

	' categoria
	sqlQuery = "SELECT * FROM categoria WHERE cate_clave = '" & STR_CATEGORIA & "'"
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	STR_REGISTRO = Empty
	If rs.EOF = False Then
		STR_REGISTRO = "Categoria¦" & rs.GetString(, , "¦", "¦ø")
		objFile.WriteLine (STR_REGISTRO)
	End If
	rs.Close

	' pruebas seleccionadas
	sqlQuery = "SELECT * FROM pruebas_seleccionadas WHERE soli_Folio = " & solicitudeFolio
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	STR_REGISTRO = Empty
	If rs.EOF = False Then
		STR_REGISTRO = "Pruebas_Seleccionadas¦" & rs.GetString(, , "¦", "¦ø")
		objFile.WriteLine (STR_REGISTRO)
	End If
	rs.Close

	' pruebas
	sqlQuery = "SELECT * FROM pruebas"
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	i = 0
	If rs.EOF = False Then
		Do Until rs.EOF = True
			i = i + 1
			STR_NOM_PRUEBA(i) = rs.Fields("PRUE_CLAVE")
			STR_NOM_ARCHIVO(i) = rs.Fields("prue_archivo")
			rs.MoveNext
		Loop
	End If
	INT_CANT_ARCHIVOS = i
	rs.Close
		
	For i = 1 To INT_CANT_ARCHIVOS
		sqlQuery = "SELECT * FROM " & STR_NOM_ARCHIVO(i) & " WHERE soli_Folio = " & solicitudeFolio
		rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
		STR_REGISTRO = Empty
		If rs.EOF = False Then
			STR_REGISTRO = STR_NOM_ARCHIVO(i) & "¦" & rs.GetString(, , "¦", "¦ø")
			objFile.WriteLine (STR_REGISTRO)
		End If
		rs.Close
	Next
		
	' Conclusión y Sintesis
	sqlQuery = "SELECT * FROM conclusion_sintesis WHERE soli_Folio = " & solicitudeFolio
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	STR_REGISTRO = Empty
	If rs.EOF = False Then
		STR_REGISTRO = "CONCLUSION_SINTESIS¦" & rs.GetString(, , "¦", "¦ø")
		objFile.WriteLine (STR_REGISTRO)
	End If
	rs.Close
	connection.close
	
	makeCSVFile = objFile
end function
		
sub handleError(connectionRef)
	dim objErr
	dim str_c
	dim strrr, flag
	flag = 0

	if connectionRef.Errors.Count <> 0 Then
		str_c = str_c & "Count: " 
		str_c = str_c & connectionRef.Errors.Count
		for each objErr in connectionRef.Errors
			if objErr.Number <> 0 then ' Number = 0 el error es meramente informativo
				str_c = str_c & " Number: "
				str_c = str_c & objErr.Number			
				
				str_c = str_c & " Description: "
				str_c = str_c & objErr.Description  
			  
				str_c=str_c&" Native error: "
				str_c=str_c&objErr.NativeError 
				  
				str_c=str_c&" SQL state: "
				str_c=str_c&objErr.SQLState
				flag = 1
			end if
		next		
		if flag <> 0 then	
			' clean up		
			if connection.State = adStateOpen then
				connection.Close
			end if
			
			set rs = nothing
			set connection = nothing			
			
			strrr = "uti_console.asp?mensaje=" & "Error: " & str_c 
			Response.Redirect(strrr)
		end if		
	end if	
end sub

sub sendMensajeToConsole(mensaje)
	dim strrr
	strrr="uti_console.asp?mensaje=" & mensaje
	Response.Redirect(strrr)
end sub

%>

	