<?php include "../index/conexion.inc" ?>
<?php include "../index/validacion.php" ?>
<?php
	//Crear variables
	$cR01 = $_GET['id'];			// Error
	$cR02 = $_GET['id2'];			// Justificacion
	// conexión
	$servername = $svr_servidor;
	$conectioninfo = array("Database"=>$svr_base, "UID"=>$svr_usuario, "PWD"=>$svr_contrasena, "CharacterSet"=>"UTF-8");
	$conexion = sqlsrv_connect($servername, $conectioninfo);
	if ($conexion == false)
	{
		$mensaje = "Existe un error al intentar conectarse con el servidor...";
	}
	else
	{
		/* ACTUALIZO RUTA */
		$sql = "DECLARE @EXISTE INT = (SELECT COUNT(*) FROM JOIN_CAJAMASTER_UBICACION WHERE (CODIGOBARRAS = '".$cR01."')) \n"
			  ."IF (@EXISTE = 0) BEGIN \n"
			  ."	INSERT INTO JOIN_CAJAMASTER_UBICACION (CODIGOBARRAS, UBICACION, FECHAALTA) \n"
			  ."	VALUES ('$cR01', '$cR02', GETDATE()) \n"
			  ."END ELSE BEGIN \n"
			  ."	UPDATE JOIN_CAJAMASTER_UBICACION SET \n"
			  ."	UBICACION = '$cR02' \n"
			  ."	WHERE (CODIGOBARRAS = '".$cR01."') \n"
			  ."END";
		$params = array();
		$options = array("scrollable" => SQLSRV_CURSOR_KEYSET);
		// Lanzar la consulta
		$resultado = sqlsrv_query($conexion, $sql);
		if ( $resultado )
		{
			 $something = "Submission successful.";
		}
		else
		{
			 $something = "Submission unsuccessful.";
			 die( print_r( sqlsrv_errors(), true));
		}
		sqlsrv_free_stmt($resultado);
	}
	sqlsrv_close($conexion);
?>