<?php
	require 'conexion.php';
	$sql_id = mysql_query("SELECT MAX(idegreso) AS id FROM tb_egreso");
	if ($row = mysql_fetch_row($sql_id)) 
	{
		$idegreso = trim($row[0]);

	}
	if ($idegreso>=1)
	{
		
	}
	else
	{
		$idegreso=1;
	}
	//$idegreso--;
	$consulta="SELECT cl.nombre, cl.telefono1, au.fecha, eg.subtotal, eg.iva, eg.total, eg.tipopago, eg.pago  
				FROM tb_egreso  as eg
				INNER JOIN tb_cliente as cl on cl.idcliente = eg.id_cliente
				INNER JOIN tb_auditoria as au on au.id_egreso = eg.idegreso
				WHERE eg.idegreso = $idegreso and eg.condicion = 1 ";
	$soli = mysql_query($consulta,$dbx);
	if($row = mysql_fetch_array($soli) )
	{
		$razon= $row['nombre'];
		$fecha= $row['fecha'];
		$telefono= $row['telefono1'];		
		$total= $row['total'];		
		$tipopago= $row['tipopago'];		
		$pago= $row['pago'];		
	}
		
	require 'fpdf/fpdf.php';	
	class PDF extends FPDF
	{
		function Header()
		{
			//$this->Image('img/logo1.png',85,5,40);
			$this->SetFont('Arial','B',15);
			//$this->Cell(30); 
			$y = $this->GetY();
			$this->SetXY(20,$y+10);
			$this->Cell(0,0,'LUBRICADORA "SAN ANTONIO"',0,1,'C');
			$this->SetFont('Arial','I',10);
			$this->SetXY(20,$y+10);
			//$this->Ln(13);
			$this->Cell(0,$y,'Chone - San Antonio',0,1,'C');
			$this->SetXY(20,$y+15);
			$this->Image('img/wasapp.png',53,28,4);
			$this->Cell(0,$y,'0989834881 - 0979961257',0,1,'C');			
			$this->SetX(20);			
			$this->Ln(5);
		}
	}
	//include 'plantilla.php';
	
	$pdf = new PDF();
	$pdf->AliasNbPages();;
	$pdf->AddPage();
	//$pdf = nuevo FPDF ('P', 'pt', 'Carta');
	
	$concat="Orden Nro.: ".$idegreso;
	$pdf->SetFont('Arial','B',10);
	$pdf->SetX(10);
	$pdf->Cell(0,6,$concat,0,1,'L');
	
	$concatl="Ciliente: ".$razon;
	$pdf->SetFont('Arial','B',10);
	$pdf->SetX(10);
	$pdf->Cell(0,6,$concatl,0,1,'L');

	$concat2="Telefono: ".$telefono;
	$pdf->SetFont('Arial','B',10);
	$pdf->SetX(10);
	$pdf->Cell(170,7,utf8_decode($concat2)."                Fecha: ".$fecha,0,1,'L');
	/*
	$concat3="R.U.C.: ".$ruc;
	$pdf->SetFont('Arial','B',10);
	$pdf->SetX(10);
	$pdf->Cell(0,6,$concat3."       Ciudad: ".$ciudad."        Telefono:".$telefono,0,1,'L');
	*/
	//$concat3="R.U.C.: ".$ruc;
	$pdf->SetFont('Arial','B',10);
	$pdf->SetX(10);
	$pdf->Cell(0,6,"Cant.               Descripcion                                                        V. Unit   V. Total",1,1,'L');
	
	$pdf->SetY(50);
	$pdf->SetFont('Arial','',8);	
	//detalle de proforma
	
	$detalle="SELECT de.iddetallee, pr.idproducto, de.cantidad, ca.detallec,  ma.detallem, pr.serie, pr.volumen, pr.precio, de.valor 
								FROM tb_detallee as de
								INNER JOIN tb_producto as pr on pr.idproducto = de.id_producto
								INNER JOIN tb_marca as ma on ma.idmarca = pr.id_marca
								INNER JOIN tb_categoria as ca on ca.idcategoria = pr.id_categoria
								WHERE de.id_egreso = '$idegreso' and de.condicion = 1 ";
	$deta = mysql_query($detalle,$dbx);
	while($row = mysql_fetch_array($deta))
	{	
		$pdf->SetX(10);
		$pdf->Cell(15,35,$row['cantidad'],0,0,'C',0);
		$pdf->SetFont('Arial','',6);
		$pdf->Cell(90,35,"  ".utf8_encode($row['detallec'])." ".utf8_encode($row['detallem'])." ".utf8_encode($row['serie'])." ".($row['volumen']),0,0,'L',0);
		$pdf->SetFont('Arial','',8);
		$pdf->Cell(5,35,"$".number_format($row['precio'],2),0,0,'R',0);
		$pdf->Cell(15,35,"$".number_format(($row['cantidad']*$row['precio']),2),0,0,'R',0);
		
		$y = $pdf->GetY();
		$pdf->SetY($y+4);
	}
	
	$pdf->Sety($y+8);
	$pdf->Cell(110,35,"Total",0,0,'R',0);
	$pdf->Cell(15,35,"$".number_format($total,2),0,0,'R',0);
	
	$pdf->Sety($y+12);
	$pdf->Cell(110,35,"Abono",0,0,'R',0);
	$pdf->Cell(15,35,"$".number_format($pago,2),0,0,'R',0);
	
	$pdf->Sety($y+16);
	$pdf->Cell(110,35,"Saldo",0,0,'R',0);
	$pdf->Cell(15,35,"$".number_format($total-$pago,2),0,0,'R',0);
	
	if(($pago) > 0)
	{
		$fpago="Credio";
	}
	else
	{
		$fpago="Efectivo";
	}
	
	$pdf->Sety($y+18);
	$pdf->Cell(40,35,"Forma de pago",0,0,'R',0);
	$pdf->Cell(15,35,$fpago,0,0,'R',0);
	
	$pdf->Output();
	
?>
