 <%@ Language=VBScript %>
<!--#INCLUDE file="ADOVBS.inc"-->
<% 
	Response.Buffer=true
	'Response.Clear

dim clave, name, action, status, contact, passwd,str_fecha_actual, auxStatus
dim userId, userName, userPasswd
dim str_bd, sqlQuery, rs, conn 

str_bd = application("bd_sieps")

' En teoria ya debe estar todo validado
action = Request.form("action")
clave = Request.form("clave")
name = Request.form("name") 'replace(replace(Request.Form("TXT_NOMBRE"),"'"," "),chr(34)," ")
contact = Request.form("contact")
passwd = Request.form("passwd")
auxStatus = Request.form("statusEna")

' Los datos del usuario maestro. Esto debe de ser unicamente para wichPage=empresa y action=save
userId = Request.form("userId")
userName = Request.form("userName")
userPasswd = Request.form("userPasswd")

if auxStatus = 1 then
	status="Activo"
else
	status="Desactivo"
end if

str_fecha_actual = month(now)&"/"&day(now)&"/"& year(now)

set conn = server.CreateObject ("ADODB.Connection")
set rs = server.CreateObject ("ADODB.Recordset")
'conn.Mode = adModeReadWrite	
conn.IsolationLevel = adXactIsolated
conn.Attributes = adXactCommitAbort
On Error Resume Next
conn.Open str_bd
handleError(conn)
'sendMensajeToConsole("ya abri la connection")

SELECT CASE action
	CASE "save"	
	
	'sendMensajeToConsole("La action: "&action)
	
		conn.BeginTrans
		sqlQuery = "INSERT INTO empresa (empr_clave, empr_idx, empr_nombre, empr_contacto, empr_estatus, empr_password) "
		sqlQuery = sqlQuery & "VALUES('" & clave & "',1 , '" & name & "', '" & contact & "','" & status & "','" & passwd & "')"
		On Error Resume Next
		conn.Execute sqlQuery,,adCmdText
		handleError(conn)
		'sendMensajeToConsole("la action: "&action&". El error: "&conn.Errors.Count)
		
		'Inserto el usuario maestro de la empresa
		sqlQuery = getQueryUser(userId, userName, userPasswd, clave)
		
		On Error Resume Next		
		conn.Execute sqlQuery,,adCmdText
		handleError(conn)
		
		'sendMensajeToConsole("la action: "&action&". El error: "&conn.Errors.Count)
		conn.CommitTrans		
	case "update"
		conn.BeginTrans		
		sqlQuery = "UPDATE empresa SET empr_nombre='" & name & "', empr_estatus ='"& status &"' WHERE empr_clave ='" & clave & "';"
		On Error Resume Next		
		conn.Execute sqlQuery,,1
		handleError(conn)
				 
		sqlQuery = "UPDATE usuario SET usua_nombre='" & userName & "', usua_password ='"& userPasswd &"' WHERE usua_clave ='" & userId & "';"
		On Error Resume Next		
		conn.Execute sqlQuery,,1
		handleError(conn)
		conn.CommitTrans		
	CASE "del"		
		conn.BeginTrans
'		sqlQuery = "DELETE FROM departamentos WHERE divi_clave ='" & clave  & "' AND empr_clave ='" & str_empresa & "'"
'		sqlQuery = "DELETE FROM division WHERE divi_clave ='" & clave  & "' AND empr_clave ='" & str_empresa & "'"

		status = "Desactivo"
		sqlQuery = "UPDATE empresa SET empr_estatus ='"& status &"' WHERE empr_clave ='" & clave & "';"
		
		'On Error Resume Next
		conn.Execute sqlQuery,,adCmdText
		'sendMensajeToConsole("ya lo ejecuto")
		'handleError(conn)
		conn.CommitTrans
END SELECT

' clean up
If rs.State = adStateOpen then
	rs.Close
End If
If conn.State = adStateOpen then
	conn.Close
End If
Set rs = Nothing
Set conn = Nothing

Response.clear
Response.Redirect("inbox.asp?inbox=empresa")

function getQueryUser(clave, name, password, enterpriseId)
	dim sql 	
		sql = "INSERT INTO usuario(usua_clave, usua_nombre, usua_password, empr_clave, tipo_usuario, "
		sql = sql & "usua_g_administrador, usua_g_aplicacion, usua_g_actualizacion, usua_g_divisiones, usua_g_departamentos, usua_g_puestos, usua_g_categorias, usua_g_turnos, usua_g_usuarios, usua_g_bajar, usua_g_password,usua_s_solicitud, usua_s_departamentos, usua_s_puestos) "
		sql = sql & "VALUES('" & clave & "', '" & name & "', '" & password & "', '" & enterpriseId & "', 255, "
		sql = sql & "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)"		
	getQueryUser = sql
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
'strrr = "uti_console.asp?mensaje=" & "Entro en handleError la flag: "&flag&". en el if dentro del if. " & str_c
'Response.Redirect(strrr)
			
			'conn.RollbackTrans
		
			' clean up		
			if conn.State = adStateOpen then
				conn.Close
			end if
			
			set rs = Nothing
			set conn = Nothing
			set connectionRef = 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
%>
