<?php include "../index/conexion.inc" ?>
<?php include "../index/validacion.php" ?>
<?php
	// 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
	{
		// VARIABLES
		$cV00 = $_GET['id'];	/* DESDE */
		// PARAMETROS PARA EXPORTAR
		header("Pragma: public");
		header("Expires: 0");
		$filename = "InventarioFisico.xls";
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=$filename");
		header("Pragma: no-cache");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		echo "
			<table>
				<thead>
					<tr>
						<th>LINEA</th>
						<th>DOCUMENTO</th>
						<th>GONDOLA</th>
						<th>COD.ARTICULO</th>
						<th>PROVEEDOR</th>
						<th>TALLA</th>
						<th>COLOR</th>
						<th>COD.BARRAS</th>
						<th>BODEGA</th>
						<th>ALMACEN</th>
						<th>C1</th>
						<th>C2</th>
						<th>C1-C2</th>
						<th>FECHA</th>
						<th>COD.VENDEDOR</th>
						<th>VENDEDOR</th>
						<th>CAJA MASTER</th>
						<th>UBICACION</th>
						<th>ZONA</th>
						<th>PASILLO</th>
						<th>LADO</th>
					</tr>
				</thead>
				<tbody>";
					// CONSULTA SQL
					$sql = "SELECT A.IDLINEA, AR.CODARTICULO, AR.REFPROVEEDOR, ISNULL(AL1.TALLA, ISNULL(AL2.TALLA, AL3.TALLA)) AS TALLA, \n"
						  ."ISNULL(AL1.COLOR, ISNULL(AL2.COLOR, AL3.COLOR)) AS COLOR, A.CODBARRAS, A.BODEGA, B.NOMBREALMACEN, A.UNIDADES C1, ISNULL(dbo.JOIN_INV_REV (A.CODBARRAS , GONDOLA , UBICACION, 1),0) C2, A.UNIDADES-ISNULL(dbo.JOIN_INV_REV (A.CODBARRAS , GONDOLA , UBICACION, 1),0) 'C1-C2', A.FECHAALTA, \n"
						  ."ISNULL(V.CODVENDEDOR, 0) AS CODVENDEDOR, V.NOMVENDEDOR AS VENDEDOR, A.CAJAMASTER, A.UBICACION, UB.IDZONA, UB.PASILLO, UB.LADO, \n"
						  ."P.CODPROVEEDOR, A.GONDOLA, A.DOCUMENTO, 1 AS ORD \n"
						  ."FROM JOIN_INVENTARIO_FISICO AS A \n"
						  ."LEFT JOIN ALMACEN AS B ON (A.BODEGA = B.CODALMACEN COLLATE Latin1_General_CS_AI) \n"
						  ."LEFT JOIN ARTICULOSLIN AS AL1 ON (A.CODBARRAS = AL1.CODBARRAS COLLATE Latin1_General_CS_AI) \n"
						  ."LEFT JOIN ARTICULOSLIN AS AL2 ON (A.CODBARRAS = AL2.CODBARRAS2 COLLATE Latin1_General_CS_AI) \n"
						  ."LEFT JOIN ARTICULOSLIN AS AL3 ON (A.CODBARRAS = AL3.CODBARRAS3 COLLATE Latin1_General_CS_AI) \n"
						  ."LEFT JOIN ARTICULOS AS AR ON (AR.CODARTICULO = AL1.CODARTICULO OR AR.CODARTICULO = AL2.CODARTICULO OR AR.CODARTICULO = AL3.CODARTICULO) \n"
						  ."LEFT JOIN JOIN_UBICACIONES AS UB ON (A.UBICACION = IDUBICACION) \n"
						  ."LEFT JOIN VENDEDORES AS V ON (A.USUARIO = V.CODVENDEDOR) \n"
						  ."LEFT JOIN REFERENCIASPROV AS P ON (AR.REFPROVEEDOR = P.REFPROVEEDOR) \n"
						  ."WHERE (A.DOCUMENTO = ".$cV00.")  \n"
						  ."ORDER BY A.FECHAALTA DESC";
					$pa1 = array();
					$op1 = array("scrollable" => SQLSRV_CURSOR_KEYSET);
					// Lanzar la consulta
					$re1 = sqlsrv_query($conexion, $sql, $pa1, $op1);
					while($fila = sqlsrv_fetch_array($re1))
					{
						echo "
							<tr>
								<td>".$fila['IDLINEA']."</td>
								<td>".$fila['DOCUMENTO']."</td>
								<td>".$fila['GONDOLA']."</td>
								<td>".$fila['CODARTICULO']."</td>
								<td>".$fila['REFPROVEEDOR']."</td>
								<td>".$fila['TALLA']."</td>
								<td>".$fila['COLOR']."</td>
								<td>".$fila['CODBARRAS']."</td>
								<td>".$fila['BODEGA']."</td>
								<td>".$fila['NOMBREALMACEN']."</td>
								<td>".$fila['C1']."</td>
								<td>".$fila['C2']."</td>
								<td>".$fila['C1-C2']."</td>
								<td>".$fila['FECHAALTA']->format('Y-m-d h:m:s')."</td>
								<td>".$fila['CODVENDEDOR']."</td>
								<td>".$fila['VENDEDOR']."</td>
								<td>".$fila['CAJAMASTER']."</td>
								<td>".$fila['UBICACION']."</td>
								<td>".$fila['IDZONA']."</td>
								<td>".$fila['PASILLO']."</td>
								<td>".$fila['LADO']."</td>
							</tr>
						";
					}
				echo "
				</tbody>
			</table>
		";
	}
	// Desconexion
	sqlsrv_close($conexion);
?>