<%@ Language=VBScript %>
<!--#INCLUDE file="ADOVBS.inc"-->
<%Response.Buffer = True%>
<% 
	dim solicitudeFolio 
	dim objFile
	Dim objStream 
	Dim objFSO
	dim strAbsPath
	
	solicitudeFolio = CStr(Request.Form("folio"))
	'-- do some basic error checking for the QueryString
 	if solicitudeFolio = "" Then
 	    Response.Clear
 	    Response.Write("No file specified.")
 	    Response.End
 	ElseIf Len(solicitudeFolio) > 64 Then
 	    Response.Clear
 	    Response.Write("Folio too long.")
 	    Response.End
 	Else
 	    call makeCSVFile(solicitudeFolio)
 	End If 
			
	strAbsPath = server.mappath(".") & "\filesCSV\UpdateFile.sie"
	
	'-- create FSO object to check if file exists and get properties
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 	
	
	if objFSO.FileExists(strAbsPath) then
		Set objFile = objFSO.GetFile(strAbsPath)
'sendMensajeToConsole("el nombre del archivo: "&objFile.Name)
		'-- first clear the response, and then set the appropriate headers		
		Response.Clear
		'-- the filename you give it will be the one that is shown
		' to the users by default when they save		
		Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
		Response.AddHeader "Content-Length", objFile.Size
		Response.ContentType = "application/octet-stream"			
		Response.CharSet = "UTF-8"
		
		Set objStream = Server.CreateObject("ADODB.Stream")
		objStream.Open
		'-- set as binary
		objStream.Type = 1
'sendMensajeToConsole("ya se configuro la response: "&objFile.Name)

		'-- load into the stream the file
		objStream.LoadFromFile(strAbsPath)
'sendMensajeToConsole("ya lo baja "&objStream)		
		'-- send the stream in the response
		Response.BinaryWrite(objStream.Read)
		objStream.Close
'		objFile.close
'		objFSO.close
		Set objStream = Nothing
		Set objFile = Nothing 
'		set objFSO = nothing		
	else
		sendMensajeToConsole("no existe el archivo")
	end if
	
	set objFSO = nothing
	

sub 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
	dim strAbsPath
	dim fso
	dim objFile
	
	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 = "\filesCSV\UpdateFile.sie"
	strAbsPath = server.mappath(".") & fileName 'Server.mappath(fileName)
'sendMensajeToConsole("abs path: "+strAbsPath)	
	Set fso = server.CreateObject("Scripting.FileSystemObject") 'Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'sendMensajeToConsole("El nombre: ")	
	Set objFile = fso.CreateTextFile(strAbsPath, True) ' objFSO.CreateTextFile(strAbsPath)		
'sendMensajeToConsole("Creado: ")	

	sqlQuery = "SELECT * FROM solicitud WHERE soli_Folio = " & solicitudeFolio
	On Error Resume Next	
    rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	handleError(connection)
        
	If rs.EOF = False Then
		BOL_ENCONTRO = True
		STR_NOMBRE = rs.Fields("soli_nombre")
		STR_EMPRESA = rs.Fields("empr_clave")
		STR_DIVISION = rs.Fields("divi_clave")
		STR_DEPARTAMENTO = rs.Fields("depa_clave")
		STR_PUESTO = rs.Fields("pues_clave")
		STR_CATEGORIA = rs.Fields("cate_clave")
		STR_REGISTRO = "Solicitud¦" & rs.GetString(, , "¦", "¦ø")
	End If
	rs.Close
	
	objFile.WriteLine (STR_REGISTRO)
'sendMensajeToConsole("Escrito: "&STR_REGISTRO)

	' empresa
	sqlQuery = "SELECT * FROM empresa WHERE empr_clave = '" & STR_EMPRESA & "'"
	On Error Resume Next
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	handleError(connection)
	
	STR_REGISTRO = Empty
	If rs.EOF = False Then
		STR_REGISTRO = "Empresa¦" & rs.GetString(, , "¦", "¦ø")
		objFile.WriteLine (STR_REGISTRO)
	End If
	rs.Close
'sendMensajeToConsole("Escrito: "&STR_REGISTRO)

	' division
	sqlQuery = "SELECT * FROM division WHERE divi_clave = '" & STR_DIVISION & "' AND empr_clave='" & STR_EMPRESA & "'"
	On Error Resume Next
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	handleError(connection)
	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 & "'"
	On Error Resume Next
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	handleError(connection)
	
	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 & "'"
	On Error Resume Next
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	handleError(connection)
	
	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 & "'"
	On Error Resume Next
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	handleError(connection)
	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
	On Error Resume Next
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	handleError(connection)
	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"
	On Error Resume Next
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	handleError(connection)
	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
		On Error Resume Next
		rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
		handleError(connection)
		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
	On Error Resume Next
	rs.Open sqlQuery, connection, adOpenForwardOnly, adLockReadOnly, 1
	handleError(connection)
	STR_REGISTRO = Empty
	If rs.EOF = False Then
		STR_REGISTRO = "CONCLUSION_SINTESIS¦" & rs.GetString(, , "¦", "¦ø")
		objFile.WriteLine (STR_REGISTRO)
	End If
	rs.Close
	connection.close
	
	fso.close
	objFile.close
	set fso = nothing
	set objFile = nothing
end sub

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

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


%>

	