<?php include "../index/conexion.inc" ?>
<?php include "../index/validacion.php" ?>
<?php
	if (isset($_POST['Agregar']))
	{
		// VARIABLES FORMULARIO
		$cR01 = $_POST['cBZT01'];		// BODEGA
		$cR02 = $_POST['cBZT02'];		// UBICACION
		$cR03 = $_POST['cBZT03'];		// CAJAMASTER
		$cR04 = $_POST['cBZT04'];		// CODBARRAS
		$cR05 = $_POST['cBZT05'];		// UNIDADES
		$cR06 = $_POST['cBZT06'];		// GONDOLA
		$cR07 = $_POST['cBZT07'];		// DOCUMENTO
		// 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
		{
			/* INSERTO NUEVAS CAJAS MASTER */
			$sql = "DECLARE @VARDOCU INT = ".$cR07." \n"
				  ."DECLARE @VARTRAN INT = (SELECT TOP(1) IDTRANSACCION FROM JOIN_INVENTARIO_FISICO WHERE (DOCUMENTO = @VARDOCU) ORDER BY IDTRANSACCION DESC) \n"
				  ."DECLARE @VARLINE INT = (SELECT TOP(1) IDLINEAT FROM JOIN_INVENTARIO_FISICO WHERE (DOCUMENTO = @VARDOCU) ORDER BY IDLINEAT DESC) + 1 \n"
				  ."DECLARE @VARGONDOLA INT = (SELECT TOP(1) GONDOLA FROM JOIN_INVENTARIO_FISICO WHERE (DOCUMENTO = @VARDOCU) ORDER BY GONDOLA DESC)  \n"
				  ."DECLARE @VARUSUA INT = 0 \n"
				  ."INSERT INTO JOIN_INVENTARIO_FISICO (BODEGA, UBICACION, CAJAMASTER, CODBARRAS, UNIDADES, FECHAALTA, USUARIO, GONDOLA, DOCUMENTO, IDTRANSACCION, IDLINEAT) VALUES \n"
				  ."('$cR01', '$cR02', '$cR03', '$cR04', '$cR05', GETDATE(), @VARUSUA, @VARGONDOLA , '$cR07', @VARTRAN, @VARLINE)";
			$pa1 = array();
			$op1 = array("scrollable" => SQLSRV_CURSOR_KEYSET);
			$re1 = sqlsrv_query($conexion, $sql, $pa1, $op1);
			if ( $re1 )
			{
				$something = "Submission successful.";
			}
			else
			{
				$something = "Submission unsuccessful.";
				die( print_r( sqlsrv_errors(), true));
			}
			sqlsrv_free_stmt($re1);
		}
		sqlsrv_close($conexion);
		echo "
		<script languaje='javascript' type='text/javascript'>
			self.close();
		</script>
		";
	}
?>