<?php	
	session_start();
	require_once('tcpdf\tcpdf.php');
	include "php/config.inc";
	$conexion = mysqli_connect($serv,$dbu,$dbp,$db);
	mysqli_set_charset($conexion, "utf8");
	ob_start();
		function hex2rgb($hex) {
		$hex = str_replace("#", "", $hex);
		if(strlen($hex) == 3) {
			$r = hexdec(substr($hex,0,1).substr($hex,0,1));
			$g = hexdec(substr($hex,1,1).substr($hex,1,1));
			$b = hexdec(substr($hex,2,1).substr($hex,2,1));
			} else {
			$r = hexdec(substr($hex,0,2));
			$g = hexdec(substr($hex,2,2));
			$b = hexdec(substr($hex,4,2));
		}
		$rgb = array($r, $g, $b);
	
	return $rgb;
	//return implode(",", $rgb);
	}
	
	
	$visual = mysqli_query($conexion,"SELECT * FROM ecomm_visual WHERE CVE_CUENTA=".$_GET['IDS']);
	$st = mysqli_fetch_array($visual);
	
	global $empresa;
	$empresa = $st['EMPRESA'];
	global $color;
	$color= hex2rgb($st['C-ENF1']);
	global $IMGPDF;
	$IMGPDF= 'img/'.$st['LOGO'];
	if($st['LOGO']=="" OR $st['LOGO']=null){$IMGPDF= 'img/Logo_Gris.jpg';}
	class MYPDF extends TCPDF {
		public function Header() {
			global $IMGPDF;
			global $color;
			global $empresa;
			$this->SetTextColor($color); 
			$this->Image($IMGPDF, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
			$this->SetFont('helvetica', 'B', 20);
			$this->Cell(0, 15, $empresa.'@LogSys WMS', 0, false, 'C', 0, '', 0, false, 'M', 'M');
		}
		public function Footer() {
			$this->SetY(-15);
			$this->SetFont('helvetica', 'I', 8);
			$this->Cell(0, 10, 'Página '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
		}
	}
	
	$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	
	$pdf->SetCreator(PDF_CREATOR);
	$pdf->SetAuthor('Nicola Asuni');
	$pdf->SetTitle('TCPDF Example 003');
	$pdf->SetSubject('TCPDF Tutorial');
	$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
	
	$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
	
	$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
	$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
	
	$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
	
	$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
	$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
	$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
	
	$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
	$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
	
	if (@file_exists(dirname(__FILE__).'/lang/spa.php')) {
		require_once(dirname(__FILE__).'/lang/spa.php');
		$pdf->setLanguageArray($l);
	}
	
	$pdf->AddPage();

	$suma=0;
	$STY = "STYLE='color:".$st['C-T-TEXT']."; background-color: ".$st['C-T-CELL'].";' ";
	$res = '<TABLE STYLE="WIDTH:100%;COLOR:BLACK;padding:4px;border: 1px solid '.$st['C-T-BACK'].';"><THEAD><TR><TD>Nombre</TD><TD>Piezas</TD><TD>Precio unitario</TD><TD>Subtotal</TD></TR></THEAD><TBODY>';
	$REC = mysqli_query($conexion,"SELECT *,COUNT(ID) AS ITERA FROM  tabla_detalledocsur WHERE NO_DOCUMENTO='$_GET[ndoc]'");
	$ST = mysqli_fetch_array($REC);
	for($i = 0;$i< $ST['ITERA'];$i++){
		$peticion = "SELECT * FROM tabla_altaproducto WHERE SKU='$ST[SKU]' AND CUENTA='$_GET[IDS]'";
		$resultado = mysqli_query($conexion, $peticion);
		while($fila = mysqli_fetch_array($resultado)){
			$total = $fila['COSTOECOM']*$ST['CANT_X_SURTIR'];
			$res = $res."<TR $STY><TD $STY>$fila[DESCRIPCION]</TD><TD $STY>$ST[CANT_X_SURTIR]</TD><TD $STY>$ $fila[COSTOECOM]</TD><TD $STY>$ $total</TD></TR>";
			$suma += $total;
		}}
	
	$res = $res.'<TR><TD colspan="2" $STY ></TD><TD $STY>Total:</TD><TD $STY>$'.$suma.'</TD></TR></TBODY></TABLE></CENTER><BR><BR>';	
		
$html = <<<EOD
	<BR><BR>
	<CENTER>
	$res
	</CENTER>
EOD;
$txt1 = <<<EOD
	Recibo de compra online
	Clave Paypal: $_GET[paymentId]. Documento: $_GET[ndoc]
EOD;
$txt2 = <<<EOD
	
	Datos del comprador
	
EOD;
$txt3 = <<<EOD
	Nombre: $_GET[nom] 
	Teléfono: $_GET[tel] 
	Email: $_GET[ema]
EOD;
$txt4 = <<<EOD
	
	Dirección de entrega
	
EOD;
$txt5 = <<<EOD
	
	Colonia: $_GET[col]  Calle: $_GET[cal]  Número exterior: $_GET[nex]  Código Postal: $_GET[cp]  Entre calles (referencia): $_GET[ref]  Municipio: $_GET[mun]  Estado: $_GET[est]
	
EOD;
$txt6= <<<EOD
	

	Detalles de la compra
	
EOD;

	$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => hex2rgb($st['C-ENF1']));
	$pdf->Line(0, 25, 300, 25, $style);
	$pdf->SetFont('times', 'u', 11);
	$pdf->Write(0, $txt1, '', 0, 'C', true, 0, false, false, 0);
	$pdf->Line(0, 40, 300, 40, $style);
	$pdf->SetFont('times', '', 11);
	$pdf->Write(0, $txt2, '', 0, 'L', true, 0, false, false, 0);
	$pdf->SetFont('times', 'u', 11);
	$pdf->Write(0, $txt3, '', 0, 'L', true, 0, false, false, 0);
	$pdf->SetFont('times', '', 11);
	$pdf->Write(0, $txt6, '', 0, 'C', true, 0, false, false, 0);
	$pdf->SetFont('times', '', 10);
	$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
	$pdf->SetFont('times', '', 11);
	$pdf->Write(0, $txt4, '', 0, 'C', true, 0, false, false, 0);
	$pdf->Write(0, $txt5, '', 0, 'C', true, 0, false, false, 0);
	
	ob_end_clean();
	
	$pdf->Output('Comprobante_'.$_GET['paymentId'].'.pdf', 'I');
	
	ob_end_flush();
?>