<?php
require('../fpdf.php');
include ("../../Conexion.php");

$UsuEn=$_POST['USUkp'] or $UsuEn=$_GET['USUkp'];
$UsuAImp=strtoupper($_POST['USUkp']) or $UsuAImp=strtoupper($_GET['USUkp']);
$NoDocSEn=$_POST['txt_NODOC'] or $NoDocSEn=$_GET['txt_NODOC'];

if ($UsuEn=="")
		{
		echo "<META HTTP-EQUIV=Refresh CONTENT='0; URL=RedireccionDeCierre.php' target='_parent'>";
		}

/////////IDENTIFICAR DATOS DE NIVEL///////
		$sql = mysql_query("select * from tabla_usuarios where USUARION='$UsuEn'");
						
	if (mysql_numrows($sql)==0) echo "";
		else{
		$registro=mysql_fetch_array($sql);
		}
		if (mysql_numrows($sql)>0)
		{
		$CNiv=$registro["NIVEL"];
		}
			
	if ($CNiv<>"01" and $CNiv<>"02" and $CNiv<>"04" and $CNiv<>"05")
		{
		echo "<script>alert('No estas autorizado para acceder a este módulo del WMS');</script>";
		echo "<META HTTP-EQUIV=Refresh CONTENT='0; URL=RedireccionDeCierre.php'>";
		}

class MiPDF extends FPDF {

		public function Header(){
		
		
			$this -> Image ("../../../WMS_Resources/Imagenes/Logo_Rectangu_Transp.png" , 10 , 10 , 30 ,15);
			$this -> SetFont ( 'Arial' , 'B', '12');
			$this -> Cell ( 180, 10 , '|| Guia de Embarque ||' , 0 , 0 , 'C');
			$this -> Ln ( 2 );
			$this -> SetFont ( 'Arial' , 'I', '10');
			$this -> Cell ( 180, 20 , 'www.logsyswms.com' , 0 , 0 , 'C');
			$this -> Ln ( 4 );
			$this -> SetTextColor (0 , 136 , 29);
			$this -> SetFont ( 'Arial' , '', '10');
			$this -> Cell ( 180, 20 , '_______________________________________________________________________________________________' , 0 , 0 , 'C');
			$this -> Ln ( 3 );
			$this -> SetFont ( 'Arial' , '', '10');
			$this -> Cell ( 180, 20 , '_____________________________________________' , 0 , 0 , 'C');
			$this -> Ln ( 20 );
		}
		
	    function Footer(){
    		// Posición: a 2,0 cm del final
    		$this->SetY(-20);
    		// Arial italic 8
    		$this->SetFont('Arial','I',8);
    		// Número de página
    		$this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'C');
		}
}

	$mipdf = new MiPDF();

	$mipdf -> AliasNbPages();
	$mipdf -> addPage();
	
//////////////////////////////////CONSULTAR LA TABLA DEL CONTRATANTE////////////////////////////////////////////
		$sql = mysql_query("select * from tabla_contratante");
						
	if (mysql_numrows($sql)==0) echo "";
		else{
		$registro=mysql_fetch_array($sql);
		}
		if (mysql_numrows($sql)>0)
		{
		$CNomContra=$registro["NOMBRE_FISCAL"];
		$CNomCuenta=$registro["CUENTA"];
		}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////CONSULTAR LA TABLA DE DOCUMENTOS////////////////////////////////////////////
		$sql = mysql_query("select * from tabla_docsurtido WHERE NO_DOCUMENTO='$NoDocSEn'");
						
		if (mysql_numrows($sql)==0) 
		{
		echo "<script>alert('El documento de surtido y/o embarque que ingresaste no existe.\\n\\nFavor de corroborarlo.');</script>";
		echo "<META HTTP-EQUIV=Refresh CONTENT='0; URL=RedireccionDeCierre.php'>";
		}
		else
		{
		$registro=mysql_fetch_array($sql);
		}
		if (mysql_numrows($sql)>0)
		{
		$CDocu=$registro["NO_DOCUMENTO"];
		$CDesti=$registro["DESTINO"];
		$CFeCrea=$registro["FECHA_CREADO"];

		}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////CONSULTAR EL NOMBRE DEL DESTINO////////////////////////////////////////////
		$sql = mysql_query("select * from tabla_destinosclientes WHERE CVE_DESTINO='$CDesti'");
						
	if (mysql_numrows($sql)==0) echo "";
		else{
		$registro=mysql_fetch_array($sql);
		}
		if (mysql_numrows($sql)>0)
		{
		$CDestiNom=$registro["NOMBRE"];
		}

//////////////////////////INFORMACIÓN PRINCIPAL////////////////////////////////////////////////////////////////
	
	$CInfPrin_Contra = "P. LOGISTICO:";
	$CInfPrin_Cuen = "CUENTA:";
	$CInfPrin_Doc = "DOCUMENTO:";
	$CInfPrin_Desti = "DESTINO:";
	$CInfPrin_NDesti = "";
	$CInfPrin_Fecha = "FECHA:";
	$CInfPrin_Usu = "USUARIO:";
	
	
	$mipdf -> SetFont ( 'Courier' , '', '8');
	$mipdf -> SetTextColor (255 , 255 , 255);
	$mipdf -> SetFillColor ( 76 , 100 , 191 );
	$mipdf -> Cell ( 30 , 3 , $CInfPrin_Contra, 1 , 0 , 'L' , true );
	$mipdf -> SetTextColor (0 , 0 , 0);
	$mipdf -> SetFillColor ( 255 , 255 , 255 );
	$mipdf -> Cell ( 90 , 3 , $CNomContra, 1 , 0 , 'L' , true );
	$mipdf -> Ln ( 3 );////////////////////////////////////////////////////////
	$mipdf -> SetTextColor (255 , 255 , 255);
	$mipdf -> SetFillColor ( 76 , 100 , 191 );
	$mipdf -> Cell ( 30 , 3 , $CInfPrin_Cuen, 1 , 0 , 'L' , true );
	$mipdf -> SetTextColor (0 , 0 , 0);
	$mipdf -> SetFillColor ( 255 , 255 , 255 );
	$mipdf -> Cell ( 90 , 3 , $CNomCuenta, 1 , 0 , 'L' , true );
	$mipdf -> Ln ( 3 );////////////////////////////////////////////////////////
	$mipdf -> SetTextColor (255 , 255 , 255);
	$mipdf -> SetFillColor ( 76 , 100 , 191 );
	$mipdf -> Cell ( 30 , 3 , $CInfPrin_Doc, 1 , 0 , 'L' , true );
	$mipdf -> SetTextColor (0 , 0 , 0);
	$mipdf -> SetFillColor ( 255 , 255 , 255 );
	$mipdf -> Cell ( 90 , 3 , $CDocu, 1 , 0 , 'L' , true );
	$mipdf -> Ln ( 3 );////////////////////////////////////////////////////////
	$mipdf -> SetTextColor (255 , 255 , 255);
	$mipdf -> SetFillColor ( 76 , 100 , 191 );
	$mipdf -> Cell ( 30 , 3 , $CInfPrin_Desti, 1 , 0 , 'L' , true );
	$mipdf -> SetTextColor (0 , 0 , 0);
	$mipdf -> SetFillColor ( 255 , 255 , 255 );
	$mipdf -> Cell ( 90 , 3 , $CDesti, 1 , 0 , 'L' , true );
	$mipdf -> Ln ( 3 );////////////////////////////////////////////////////////
	$mipdf -> SetTextColor (255 , 255 , 255);
	$mipdf -> SetFillColor ( 76 , 100 , 191 );
	$mipdf -> Cell ( 30 , 3 , $CInfPrin_NDesti, 1 , 0 , 'L' , true );
	$mipdf -> SetTextColor (0 , 0 , 0);
	$mipdf -> SetFillColor ( 255 , 255 , 255 );
	$mipdf -> Cell ( 90 , 3 , $CDestiNom, 1 , 0 , 'L' , true );
	$mipdf -> Ln ( 3 );////////////////////////////////////////////////////////
	$mipdf -> SetTextColor (255 , 255 , 255);
	$mipdf -> SetFillColor ( 76 , 100 , 191 );
	$mipdf -> Cell ( 30 , 3 ,$CInfPrin_Fecha, 1 , 0 , 'L' , true );
	$mipdf -> SetTextColor (0 , 0 , 0);
	$mipdf -> SetFillColor ( 255 , 255 , 255 );
	$mipdf -> Cell ( 90 , 3 , $CFeCrea, 1 , 0 , 'L' , true );
	$mipdf -> Ln ( 3 );////////////////////////////////////////////////////////
	$mipdf -> SetTextColor (255 , 255 , 255);
	$mipdf -> SetFillColor ( 76 , 100 , 191 );
	$mipdf -> Cell ( 30 , 3 ,$CInfPrin_Usu, 1 , 0 , 'L' , true );
	$mipdf -> SetTextColor (0 , 0 , 0);
	$mipdf -> SetFillColor ( 255 , 255 , 255 );
	$mipdf -> Cell ( 90 , 3 , $UsuAImp, 1 , 0 , 'L' , true );

	
//////////////////////////FIN INFORMACIÓN PRINCIPAL///////////////////////////////////////////////////////////	

	$cabeceraID = "ID";
	$cabeceraSKU = "SKU";
	$cabeceraDESC = "DESCRIPCIÓN";
	$cabeceraLOT = "LOTE";
	$cabeceraREG = "REGISTRO";
	$cabeceraCIE = "CIERRE";
	$cabeceraCSURTI = "C.SURTIDA";

	$mipdf -> Ln ( 10 );
	$mipdf -> SetFont ( 'Courier' , '', '6');
	$mipdf -> SetTextColor (255 , 255 , 255);
	$mipdf -> SetFillColor ( 76 , 100 , 191 );

	$mipdf -> Cell ( 6 , 3 , $cabeceraID, 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 24 , 3 , $cabeceraSKU, 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 60 , 3 , $cabeceraDESC, 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 23 , 3 , $cabeceraLOT, 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 16 , 3 , $cabeceraREG, 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 16 , 3 , $cabeceraCIE, 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 16 , 3 , $cabeceraCSURTI, 1 , 0 , 'C' , true );
	
	$mipdf -> Ln ( 3 );

$consulta = mysql_query ("SELECT  * from temporal_guiaembarque WHERE NO_DOCUMENTO='$NoDocSEn' ORDER BY ID  ASC");

$ID = "0";
while ($datos = mysql_fetch_array($consulta))
{
	$ID = $ID + 1;
	$SKU = $datos [ 'SKU' ];
	$DESC = $datos [ 'DESCRIPCION' ];
	$LOT = $datos [ 'LOTE' ];
	$FRegistro = $datos [ 'REGISTRO' ];
	$FCierre = $datos [ 'CIERRE' ];
	$CSURTIDA = $datos [ 'CANT_SURTIDA' ];

	
	$mipdf -> SetFont ( 'Courier' , '', '7');
	$mipdf -> SetTextColor (0 , 0 , 0 );
	$mipdf -> SetFillColor ( 255 , 255 , 255 );
	
	$mipdf -> Cell ( 6 , 3 , $ID , 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 24 , 3 , $SKU , 1 , 0 , 'L' , true );
	$mipdf -> Cell ( 60 , 3 , $DESC , 1 , 0 , 'L' , true );
	$mipdf -> Cell ( 23 , 3 , $LOT , 1 , 0 , 'L' , true );
	$mipdf -> Cell ( 16 , 3 , $FRegistro , 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 16 , 3 , $FCierre , 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 16 , 3 , $CSURTIDA , 1 , 0 , 'C' , true );
	$mipdf -> Ln ( 3 );
}
		/////////////////////CONSULTA PARA SUMAR EL TOTAL DE PIEZAS POR RECIBIR/////////////////////////////////////////////////////
		$sqlSUMA = "SELECT SUM(CANT_SURTIDA) as ObtCant FROM tabla_tareas where NO_DOCUMENTO='$NoDocSEn'";
		$resSUMA = mysql_query($sqlSUMA); 
		$PrePiezas = mysql_fetch_assoc($resSUMA);
		$TotPiezas = "T:". $PrePiezas ['ObtCant']; 


	$mipdf -> Ln ( 0 );
	$mipdf -> SetFont ( 'Courier' , 'B', '8');
	$mipdf -> SetFillColor ( 208 , 72 , 69 );
	$mipdf -> SetTextColor (255 , 255 , 255);
	$TDC= "Total: ".$ID;
	$mipdf -> Cell ( 6 , 3 , '' , 0 , 0 , 'C' , true );
	$mipdf -> Cell ( 24 , 3 , $TDC, 0 , 0 , 'C' , true );
	$mipdf -> Cell ( 60 , 3 , '' , 0 , 0 , 'L' , true );
	$mipdf -> Cell ( 23 , 3 , '' , 0 , 0 , 'L' , true );
	$mipdf -> Cell ( 16 , 3 , '' , 0 , 0 , 'L' , true );
	$mipdf -> Cell ( 16 , 3 , '' , 0 , 0 , 'L' , true );
	$mipdf -> Cell ( 16 , 3 , $TotPiezas , 0 , 0 , 'C' , true );


//////////////////////IMPRIME EL AREA DE FIRMAS///////////////////////////////////////////////////////////////////////////
	$mipdf -> Ln ( 20 );
	$mipdf -> SetFont ( 'Courier' , 'B', '8');
	$mipdf -> SetFillColor ( 255 , 255 , 255 );
	$mipdf -> SetTextColor (0 , 0 , 0);
	$mipdf -> Cell ( 47 , 3 , 'EMBARCA' , 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 47 , 3 , 'AUTORIZA', 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 47 , 3 , 'TRANSPORTISTA' , 1 , 0 , 'C' , true );
	$mipdf -> Cell ( 47 , 3 , 'VIGILANCIA' , 1 , 0 , 'C' , true );
	
	$mipdf -> SetTextColor (0 , 0 , 0);
	
$mipdf -> Output();

/////CONSULTA QUE ELIMINA LA TABLA TEMPORAL DE GUIA DE EMBARQUE///////////////
		$sqlE= "DELETE FROM temporal_guiaembarque WHERE NO_DOCUMENTO='$NoDocSEn'";
		$resultadoE=mysql_query($sqlE,$conexion) or die(mysql_error());
?>
