<?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
	{
		require('fpdf.php');
		class PDF extends FPDF
		{
			// Cabecera de página
			function Header()
			{
				global $serie;
				global $pedido;
				global $supedido;
				global $bodega;
				$destino = $_GET['id6'];
				global $unidadest;
				global $fecha;
				$cvV01 = date("F j, Y, g:i a");
				$this->SetFont('Arial','',18);
				$this->Text(111,10,'SALIDA DE MERCADERIA');
				$this->SetFont('Arial','',12);
				//$this->Cell(280,5,'Concepto "A" INVENTARIO FISICO CD',0,0,'C');
				$this->Text(10,16,'SERIE: '.$serie);
				$this->Text(10,22,'PEDIDO: '.$pedido);
				$this->Text(10,28,'TOTAL UNIDADES: '.$unidadest);
				
				$this->Text(230,16,'FECHA: '.$fecha);
				$this->Text(230,22,'SUPEDIDO: '.$supedido);
				$this->Text(230,28,'BODEGA: '.$bodega);

				$this->Text(10,34,'DESTINO: '.$destino);
				
				$this->Line(10, 37, 290, 37);
				$this->Ln(27);
				
				$w = array(20, 20, 20, 50, 10, 80, 30, 30, 30);
				$this->Cell($w[0],10,'UBIC.',0,0,'C');
				$this->Cell($w[1],10,'CAJA MT',0,0,'C');
				$this->Cell($w[2],10,'CARRITO',0,0,'C');
				$this->Cell($w[3],10,'COD.BARRAS',0,0,'C');
				$this->Cell($w[4],10,'REF.',0,0,'C');
				$this->Cell($w[5],10,'DESCRIPCION',0,0,'C');
				$this->Cell($w[6],10,'TALLA',0,0,'C');
				$this->Cell($w[7],10,'COLOR',0,0,'C');
				$this->Cell($w[8],10,'UND.',0,0,'C');
				$this->Line(10, 47, 290, 47);
				$this->Ln();
			}
			// Pie de página
			function Footer()
			{
				$this->SetFont('Arial','',8);
				// Posición: a 1,5 cm del final
				$this->SetY(-15);
				// Número de página
				$this->Line(10, 195, 290, 195);
				$this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'R');
			}
		}
		$cV01 = $_GET['id'];
		$cV02 = $_GET['id2'];
		$cV03 = $_GET['id3'];
		$cV04 = $_GET['id4'];
		$cV05 = $_GET['id5'];
		
		$pdf = new PDF('L');
		$sql = "DECLARE @SERIE NVARCHAR(10) = '".$cV01."' \n"
			  ."DECLARE @NUMERO INT = ".$cV02." \n"
			  ."DECLARE @SUPEDIDO NVARCHAR(30) = '".$cV03."' \n"
			  ."DECLARE @BODEGA NVARCHAR(5) = '".$cV04."' \n"
			  ."DECLARE @CARRITO NVARCHAR(10) = '".$cV05."' \n"
			  ."SELECT C.NUMSERIE, C.NUMPEDIDO, C.SUPEDIDO, D.FECHA, D.CODIGOARTICULO, A.DESCRIPCION, D.TALLA, D.COLOR, D.CANTIDADDEVUELTA, D.UBICACION, D.CAJAMASTER, D.ICG AS CARRITO, \n"
			  ."@BODEGA AS CODALMACEN, N.CODBARRAS, A.REFPROVEEDOR  \n"
			  ."FROM JOIN_DEVOLUCIONES AS D   \n"
			  ."LEFT JOIN PEDVENTACAB AS C ON (C.TIPODOC = CASE WHEN   D.MODULO = 220 THEN 22 ELSE D.MODULO END) AND (C.SUPEDIDO = D.DOCUMENTO COLLATE Modern_Spanish_CI_AS)   \n"
			  ."LEFT JOIN ARTICULOS AS A ON (D.CODIGOARTICULO = A.CODARTICULO)   \n"
			  ."LEFT JOIN ARTICULOSLIN AS N ON (D.CODIGOARTICULO = N.CODARTICULO) AND (D.TALLA = N.TALLA COLLATE Modern_Spanish_CI_AS) AND (D.COLOR = N.COLOR COLLATE Modern_Spanish_CI_AS )  \n"
			  ."WHERE (MODULO IN(22,220)) AND (CONVERT(VARCHAR, C.SUPEDIDO) = CONVERT(VARCHAR, @SUPEDIDO)) AND (D.ICG = @CARRITO)  \n"
			  ."ORDER BY D.FECHA DESC";
		$pa1 = array();
		$op1 = array("scrollable" => SQLSRV_CURSOR_KEYSET);
		$re1 = sqlsrv_query($conexion, $sql, $pa1, $op1);
		$unidadest = 0;
		while($fila = sqlsrv_fetch_array($re1))
		{
			$unidadest = $unidadest + $fila['CANTIDADDEVUELTA'];
		}
		$serie = $cV01;
		$pedido = $cV02;
		$supedido = $cV03;
		$bodega = $cV04;
		$fecha = date("Y-m-d H:i:s");
		// DISEÑO PDF
		$pdf->AliasNbPages();
		$pdf->AddPage();
		// DETALLE Anchuras de las columnas GRID
		$w = array(20, 20, 20, 50, 10, 80, 30, 30, 30);
		$pdf->SetFont('Arial','',14);
		$re1 = sqlsrv_query($conexion, $sql, $pa1, $op1);
		while($fila = sqlsrv_fetch_array($re1))
		{
			$pdf->SetFont('Arial','',10); 
			$pdf->Cell($w[0],10,''.$fila['UBICACION'].'',0,0,'C');
			$pdf->Cell($w[1],10,''.$fila['CAJAMASTER'].'',0,0,'C');
			$pdf->Cell($w[2],10,''.$fila['CARRITO'].'',0,0,'C');
			$pdf->Cell($w[3],10,''.$fila['CODBARRAS'].'',0,0,'C');
			$pdf->Cell($w[4],10,''.$fila['REFPROVEEDOR'].'',0,0,'C');
			$pdf->Cell($w[5],10,''.utf8_decode($fila['DESCRIPCION']).'',0,0,'C');
			$pdf->Cell($w[6],10,''.$fila['TALLA'].'',0,0,'C');
			$pdf->Cell($w[7],10,''.$fila['COLOR'].'',0,0,'C');
			$pdf->Cell($w[8],10,''.$fila['CANTIDADDEVUELTA'].'',0,0,'C');
			$pdf->Ln();
		}
		$pdf->Output();
	}
?>