<?php 
session_start();
include ("coneccionfe.php");
$id=isset($_REQUEST['id'])?$_REQUEST['id']:0;

require('../lib/fpdf16/fpdf.php');
include("phpqrcode/qrlib.php");

header("Content-type: text/html; charset=utf-8");
 $link = pg_connect("host=$dbhost port=$dbport user=$dbuser password=$dbpwd dbname=$dbname")or die(pg_last_error($link));
 
$record_emp = "";

	
	$sqlca = " set search_path to 'SHFE'; SELECT b.id_febinario_tc, 
	a.rucemisor, 
	a.tipodoc, 
	a.serie, 
	a.correlativo,
	a.fechemision, 
	a.rucreceptor, 
	a.rznsocreceptor,
    a.".chr(34)."datoadicional002".chr(34).",
	a.tipomoneda, 
	a.montodescuentos, 
	a.montogravadas, 
	a.montoinafectos,
    a.montoexonerado, 
	a.montoigv,
	a.montoisc, 
	a.".chr(34)."datoadicional009".chr(34)." as cnumeroletra,
	a.nombre_txt, 
	a.stat_anulado,
    a.rucreceptor,
	".chr(34)."datoadicional001".chr(34)." as fpago,	
	a.montototal, a.rznsocemisor,
	".chr(34)."datoadicional004".chr(34)." as detra, a.numdocafectado,
	a.datoadicional003 as creferencia_guias, a.dirreceptor as cdireccion, a.tasaimpuesto, cast(a.codigomotivo||'-'||a.dscmotivo as text) as dscmotivo, a.fechvcto  as fechvcto, a.referencia as vreferencia,
	a.".chr(34)."datoadicional015".chr(34)." as cexp, a.".chr(34)."datoadicional007".chr(34)." as cdsc_vend,  a.".chr(34)."datoadicional005".chr(34)." as ndias_vcto,
	f_fecha_referencia_cd(a.tipdocafectado, a.numdocafectado) as  dfch_ref
	FROM  fa_faele_tc a
   	inner join fa_febinario_tc b on a.tipodoc = b.tipodoc and a.serie = b.serie and a.correlativo = b.correlativo
    where b.id_febinario_tc = $id";
	
	//echo $sqlca;
	
   $content = pg_query($link,"set search_path to 'SHFE';SELECT a.rucemisor || '|' || a.tipodoc || '|' || a.serie || '|' || a.correlativo || '|' || a.montoigv || '|' || 
	a.montototal || '|' || a.fechemision || '|' || a.tipodocreceptor || '|' || a.rucreceptor || '|' as qr, a.rucemisor || '-' ||a.tipodoc || '-' || a.serie || '-' || a.correlativo as nombre FROM fa_faele_tc a
	inner join fa_febinario_tc b on a.serie = b.serie and a.correlativo = b.correlativo where id_febinario_tc = $id;");
	
	$objAd = pg_fetch_object($content);
 
	QRcode::png($objAd->qr,"$objAd->nombre.png",QR_ECLEVEL_L,3,1.2);
	
	$qr = "<img src='$objAd->nombre.png'/>";
	$vp2 = "$objAd->nombre.png";
	
    $result=pg_query($link, $sqlca);
	

    while($row=pg_fetch_array($result)){
        $record_emp = $row;
    }    
		
 
    
	
	
	$TipoMoneda = " ";
	$Simbolo = "";
	if ($record_emp["tipomoneda"]=="USD") {
		$TipoMoneda = "DOLARES";
		$Simbolo = "US$";
	} else {
		$TipoMoneda = "SOLES";
		$Simbolo = "S/";
	}
	$detra = $record_emp["detra"];
	
	
	
    $nomarchivo = str_replace('txt', 'pdf', $record_emp['nombre_txt'].".pdf");

   
class PDF extends FPDF
{
// Page header
function Header()
{ 

	global $record_emp;
	global $TipoMoneda;
	global $Simbolo;
	
	$y_axis_initial = 25;
	
		//$this->Cell(0,10,utf8_decode($politicas),0,0,'C');
		
		
		$this->Image("../img/logofe10400339088.png",9,11,33,32,"png");
		
		$ruc = $record_emp["rucemisor"];
		
		
		$FechaEmision= date("d-m-Y",strtotime($record_emp["fechemision"]));
		$dfch_vcto = date("d-m-Y",strtotime($record_emp["fechvcto"]));
		
		$refnumdocafectado = "";
		$solotboleta = "";
		$tipodoc = "";
		$visible_lb_guia = true;
		$GUIAS_label = "GUIAS";
		$creferencia_guias = $record_emp["creferencia_guias"];
		$nlon= strpos($creferencia_guias,"/");
		
		if ($record_emp['tipodoc']=='01')
		{
			$tipodoc = "FACTURA ELECTRÓNICA";
			$doccliente = "R.U.C.";
			$visible_lb_guia = true;
			$refnumdocafectado =  $record_emp["vreferencia"];
			
			if (trim($record_emp['cexp'])=="02")
			{
				$doccliente = "DOCUMENTO";
			}
			
		}
		elseif ($record_emp['tipodoc']=='03')
		{
			$tipodoc = "BOLETA DE VENTA ELECTRÓNICA";
			//$tipodoc = "ELECTRÓNICA";
			$doccliente = "D.N.I.";
			$visible_lb_guia = true;
			$refnumdocafectado =  $record_emp["vreferencia"];
		}
		elseif ($record_emp['tipodoc']=='07')
		{
			$tipodoc = "NOTA DE CREDITO ELECTRÓNICA";
			//$tipodoc = "ELECTRÓNICA";
			$doccliente = "R.U.C.";
			$visible_lb_guia = true;
			
			$refnumdocafectado = $record_emp["numdocafectado"];
			$GUIAS_label = "MOTIVO";
			$creferencia_guias = $record_emp["cdsc_motivo"];
		}
		elseif ($record_emp['tipodoc']=='08')
		{
			$tipodoc = "NOTA DE DEBITO ELECTRÓNICA";
			//$tipodoc = "ELECTRÓNICA";
			$doccliente = "R.U.C.";
			$visible_lb_guia = true;
			$refnumdocafectado = $record_emp["numdocafectado"];
			$GUIAS_label = "MOTIVO";
			$creferencia_guias = $record_emp["cdsc_motivo"];
			
		}
		
		
		
		$this->SetFont('Arial','B',11);
		$this->SetTextColor(60);
		$this->ln(5);
		$this->SetTextColor(88,95,98);
		$this->SetLineWidth(0.4);
		$this->SetFillColor(192);
		
		
		$this->Rect(130,15,71,27,'D');
		
		$ruc = "R.U.C. ".$ruc ;
		$this->SetFillColor(255,255,255);
		$this->SetX(130);
		$this->Cell(0,14,utf8_decode($ruc),0,0,'C');
		$this->ln(7);
		$this->SetX(130);
		$this->Cell(0,14,utf8_decode($tipodoc),0,0,'C');
		$this->ln(7);
		$this->SetX(147);
		$this->Cell(0,14,utf8_decode($record_emp["serie"]."-".$record_emp["correlativo"]),0,0,'C');
	
		
		$this->ln(8);
		$this->SetX(18);
		$this->SetFont('Arial','B',11);
		$this->SetFillColor(243,243,243);
		$this->SetTextColor(169, 53, 32);
		$this->SetX(40);
		
		$this->Cell(40,5,"",0);
		$this->Cell(2,-28,utf8_decode("NIÑO HUAYLLURINO"),0,0,'C');
		
		////
		$this->SetTextColor(0,0,0);
		$this->SetX(80);
		$this->SetFont('Arial','B',8);
		$this->Cell(2,-20,"De: Yeny Asuncion Llamoca Medina",0,0,'C');
		
		$this->SetFont('Arial','B',7);
		$this->Cell(2,-10,"VENTA DE CARNES ROJA, AVES Y MENUDENCIAS ",0,0,'C');
		$this->Cell(2,-4,"AL POR MAYOR Y MENOR",0,0,'C');
		
		/////
		
		
		$this->ln(8);
		$this->SetFont('Arial','',7);
		$this->Cell(70,4,"",0);
		$this->ln();
		$this->SetLineWidth(0.08);
		$this->SetTextColor(0,0,0);
		
		$direccion1 = "Dirección Fiscal:";
		$direccion2 = "JR. BILLINGHURST 297 P.J. EL PROGRESO 3ER SECTOR LIMA LIMA-CARABAYLLO";
		
		$direccion3 = "Teléfono: 547 3667 / Celular: 938 904 530 ";
		$direccion4 = "E-mail: yenyllamoca15@gmail.com";
		
		
		$this->SetFont('Arial','',7);
		$y = $this->GetY();
		$this->MultiCell(150,-2,utf8_decode("$direccion1"),0);
		$this->SetXY(140,$y);
		$this->Ln(1);
		
		$this->SetX(54);
		$this->SetFillColor(255,255,255);
		$this->SetTextColor(0,0,0);
		$this->Cell(10,6,utf8_decode(" ".$direccion2 ),0,0,'C');
		
		$this->Ln(3);
		$this->SetFont('Arial','',7);
		$this->Cell(120,8,utf8_decode("$direccion3"),0);
		$this->Ln(2);
		$this->Cell(2,12,utf8_decode("$direccion4"),0);
		
		
		  
		$this->SetFont('Arial','',7);
		$this->SetX(150);
		$this->SetFillColor(243,243,243);
		$this->SetTextColor(0,0,0);
		$this->Cell(18,6,utf8_decode("F.EMISIÓN"),1,0,'L',1);
		$this->SetX(168);
		$this->SetFillColor(255,255,255);
		$this->Cell(33,6,utf8_decode("  " .$FechaEmision),1,0,'B',1);
		
		 
		$this->ln(6);
		
		$this->SetFont('Arial','',7);
		$this->SetX(150);
		$this->SetFillColor(243,243,243);
		$this->SetTextColor(0,0,0);
		$this->Cell(18,6,utf8_decode("F.VMCTO"),1,0,'L',1);
		$this->SetX(168);
		$this->SetFillColor(255,255,255);
		$this->Cell(33,6,utf8_decode("  " .$dfch_vcto),1,0,'B',1);
	     
		
		$this->SetX(7);
		
		$this->SetFont('Arial','',7);
		$this->SetTextColor(0,0,0);
		
		$this->SetFillColor(243,243,243);
		$this->SetLineWidth(0.01);
		
		$y = $this->GetY();
		$x = $this->GetX();
		
		$this->ln(5);
		$this->Cell(29,6,utf8_decode("SEÑOR(ES)"),1,0,'L',1);
		$this->SetX(39);
		$this->SetFillColor(255,255,255);
		$this->SetFont('Arial','',7);
		$this->SetFillColor(255,255,255);
		$this->Cell(162,6,utf8_decode("  ".$record_emp["rznsocreceptor"]),1,0,'B',1);
		
		
		$this->SetX(150);
		$this->SetFillColor(243,243,243);
		$this->SetTextColor(0,0,0);
		$this->Cell(22,6,utf8_decode("RUC N°"),1,0,'L',1);
		$this->SetX(168);
		$this->SetFont('Arial','',7);
		$this->SetFillColor(255,255,255);
		//$this->SetTextColor(88,95,98);
		$this->Cell(33,6,utf8_decode("    " .$record_emp["rucreceptor"]),1,0,'B',1);

		$this->SetX(9);
		$this->SetFont('Arial','',7);
		$this->SetTextColor(0,0,0);		
		$this->SetFillColor(243,243,243);
		$this->SetLineWidth(0.01);
		
		
		//--------------------
		
		$this->ln(6);
		
		
		$this->SetFont('Arial','',7);
		$this->SetTextColor(0,0,0);		
		$this->SetFillColor(243,243,243);
		$this->SetLineWidth(0.01);
		$this->SetX(10);		
		//$this->Cell(29,6,utf8_decode("DIRECCIÓN"),1,0,'L',1);
		$vlen =  strlen(trim($record_emp["cdireccion"]));
		$height = 4;
		if ($vlen>110)
		{
			$height = 8;
		}
		$this->Cell(29,$height,utf8_decode("DIRECCIÓN"),1,0,'L',1);
		
		
		$this->SetX(39);
		
		$this->SetFillColor(243,243,243);
		$this->SetTextColor(0,0,0);
		$this->SetFont('Arial','',7);
		//$this->MultiCell(162,12,utf8_decode("  ".$record_emp["cdireccion"]),1,0,'B',1);
		$this->MultiCell(162,4,utf8_decode("  ".$record_emp["cdireccion"]),1,'L',false);
		 //$this->MultiCell(112,5,utf8_decode($descri),0,'L',false);
		
		$this->ln(0);
		$this->SetX(10);
		$this->SetFont('Arial','',7);
		$this->SetTextColor(0,0,0);		
		$this->SetFillColor(243,243,243);
		$this->SetLineWidth(0.01);
		$this->SetX(10);		
		$this->Cell(29,6,utf8_decode("REFERENCIA"),1,0,'L',1);
		$this->SetX(39);
		$this->SetFillColor(255,255,255);
		$this->SetFillColor(255,255,255);
		$this->Cell(162,6,utf8_decode("  ".$refnumdocafectado),1,0,'B',1);
		
		$this->SetX(150);
		$this->SetFillColor(243,243,243);
		$this->SetTextColor(0,0,0);
		$this->Cell(25,6,utf8_decode("VENDEDOR"),1,0,'L',1);
		$this->SetX(168);
		$this->SetFont('Arial','',7);
		$this->SetFillColor(255,255,255);
		//$this->SetTextColor(88,95,98);
		$this->Cell(33,6,utf8_decode("    " . substr($record_emp["cdsc_vend"],0,18)),1,0,'B',1);
		
		//$TipoMoneda = $record_emp["cdsc_mon"];
		//$Simbolo = $record_emp["csigno_mon"];
		
		
		
		$this->ln(6);
		$this->SetX(9);
		$this->SetFont('Arial','',7);
		$this->SetTextColor(0,0,0);		
		$this->SetFillColor(243,243,243);
		$this->SetLineWidth(0.01);
		$this->SetX(10);		
		$this->Cell(29,6,utf8_decode("FORMA DE PAGO"),1,0,'L',1);
		$this->SetX(39);
		$this->SetFillColor(255,255,255);
		//$this->SetTextColor(88,95,98);
		 $this->SetFont('Arial','',7);
		$fpago = $record_emp["fpago"]." ".$record_emp["ndias_vcto"]." DIAS" ;
		$this->Cell(162,6,utf8_decode("  ".$fpago),1,0,'B',1);
		
		$this->SetX(150);
		$this->SetFillColor(243,243,243);
		$this->SetTextColor(0,0,0);
		$this->SetFont('Arial','',7);
		$this->Cell(25,6,utf8_decode("MONEDA"),1,0,'L',1);
		$this->SetX(168);
		$this->SetFillColor(255,255,255);
		//$this->SetTextColor(88,95,98);
		$this->SetFont('Arial','',7);
		$this->Cell(33,6,utf8_decode("    " .$TipoMoneda),1,0,'B',1);
		
		
		$this->ln(6);
	  
	   
	   
}

	// Page footer
	function Footer()
	{
		// Position at 1.5 cm from bottom
		
		$this->SetY(-15);
		/*
		$this->Cell(180,3,utf8_decode("Jr. Los Mirtos Nº 590 Int. 1406, Urb. San Eugenio - Lince – Lima 14 – Perú "),0,0,'C');
		$this->ln(3);
		$this->Cell(180,3,utf8_decode("Tel.: 511 699-8066 / Cel. +51 989 047 635 "),0,0,'C');
		$this->ln(3);
		$this->Cell(180,3,utf8_decode("E-mail: grotperu@grotperu.com "),0,0,'C');
	*/
		// Arial italic 8
		$this->SetFont('Arial','I',6);
		// Page number
		//$this->Cell(0,10,'Pag: '.$this->PageNo().'/{nb}',0,0,'C');
	}
}

$pdf = new PDF(); 
//7.6 cm lrgo/ 5 cm de ancho
//$pdf = new FPDF('P', 'mm', array(50,76));
//$pdf = new FPDF($orientation='P',$unit='mm', array(45,350));

$pdf->AliasNbPages();
$pdf->AddPage();


    $pdf->SetFont('Arial','',9);

	$sqldet = " set search_path to 'SHFE'; select a.codigoitem, cast(trim(a.nombreitem)||chr(10) ||a.vobs as text) as nombreitem, a.qtyitem,a.montoprecio,a.montoventa,a.unmditem, 
	a.vobs, a.montoprecioimpto, a.montototal, a.montoigv from 
    fa_faele_td a 
	inner join fa_faele_tc b on a.tipodoc = b.tipodoc and a.serie = b.serie 
	and a.correlativo = b.correlativo where  
	a.correlativo = '$record_emp[correlativo]' and a.serie='$record_emp[serie]' and a.tipodoc='$record_emp[tipodoc]'  order by a.id_faele_td ";

    
	$result=pg_query($link, $sqldet);
	 
    $numregs=pg_num_rows($result);
	
	   $pdf->SetLineWidth(0.01);
       $pdf->SetFillColor(  241, 250, 227 );
	   $pdf->SetTextColor(0,0,0);
	   $pdf->SetFont('Arial','B',7);
	   $pdf->Ln(2);
	   
	   $pdf->Cell(15,5,utf8_decode("CÓDIGO"),1,0,'C');
	   $pdf->Cell(112,5,utf8_decode("DESCRIPCIÓN  "),1,0,'C');
	   $pdf->Cell(10,5,"UM",1,0,'C');	   
	   $pdf->Cell(18,5,"CANTIDAD",1,0,'C');
	   $pdf->Cell(18,5,"PRECIO UNIT",1,0,'C');
	   $pdf->Cell(18,5,"TOTAL",1,0,'C');
	   $pdf->Ln(6);
	    $pdf->SetTextColor(0,0,0);
   $pdf->SetFont('Arial','',6.5);
    $i=0;
    $count =0;
	$filaspaginas = 0;
	$ifillRecord = 0;
	
	
    while($i<$numregs){
		  
         $codigo=pg_result($result,$i,'codigoitem');
         $descri=pg_result($result,$i,'nombreitem');
         $unimet=pg_result($result,$i,'unmditem');
         $cantid=pg_result($result,$i,'qtyitem');
		
	    //$valoru = pg_result($result,$i,'nprecio');
         $valoru = pg_result($result,$i,'montoprecioimpto');
         $preven = pg_result($result,$i,'montoventa')+ pg_result($result,$i,'montoigv');
         //$preven=pg_result($result,$i,'montototal');
         $pdf->SetFont('Arial','',7);
		 $y = $pdf->GetY();  
		 $abe= strlen($descri);
		
		 
		 $pdf->Cell(15,5,$codigo,0,0,'L');
		 $pdf->MultiCell(112,5,utf8_decode($descri),0,'L',false);
		 $pdf->SetXY(138,$y);
         $pdf->Cell(10,5,$unimet,0,0);
		 $pdf->SetXY(145,$y);
		 
         $pdf->Cell(18,5,number_format($cantid,2),0,0,'R');
		 $pdf->SetXY(165,$y);
         $pdf->Cell(18,5,number_format($valoru,2),0,0,'R');
         $pdf->Cell(18,5,number_format($preven,2),0,0,'R');
		 
         if ($abe > 200){
			 $pdf->Ln(25);
			 $count = $count + 25;	 
		}elseif ($abe > 120){
			 $pdf->Ln(20);
			 $count = $count + 20;
		}elseif ($abe > 70){
			 $pdf->Ln(9);
			 $count = $count + 15;
		}elseif ($abe > 40){
			 $pdf->Ln(5);
			 $count = $count + 10;
		}else{
			 $pdf->Ln(4);
			 $count = $count + 5;
		}
		
         $i++;
		 $ifillRecord++;
    }
	
  
		$espacio = 20;
		$pdf->Ln($espacio);
   
		$pdf->ln(5);

		 
	    $pdf->Cell(190,5,"SON: ".$record_emp["cnumeroletra"],1,0,'L');
	   
	
		 $pdf->ln(7); 
				
//
$y = $pdf->GetY();
	$pdf->SetXY(10,$y);
	
	$pdf->Image($vp2);
//	
	    $y = $y -1.8;
		$pdf->SetXY(135,$y);
		
		$pdf->SetLineWidth(0.08);
		$pdf->SetFont('Arial','',7.5);
		$pdf->SetX(140);
		$pdf->SetFillColor(243,243,243);
		$pdf->SetTextColor(0,0,0);
		$pdf->Cell(34,6,utf8_decode("SUB TOTAL  ".$Simbolo),1,0,'L',1);
		$pdf->SetX(173);
		$pdf->SetFillColor(255,255,255);
		$pdf->Cell(27,6,utf8_decode("  " .number_format($record_emp["montogravadas"],2)),1,0,'R',1);
		$pdf->ln(5);
		
		$pdf->SetTextColor(0,0,255);
		
		$pdf->SetFont('Arial','',7.5);
		$pdf->SetX(140);
		$pdf->SetFillColor(243,243,243);
		$pdf->SetTextColor(0,0,0);
		$pdf->Cell(34,6,utf8_decode("I.G.V. ".$record_emp["tasaimpuesto"] . " %"."   ".$Simbolo),1,0,'L',1);
		$pdf->SetX(173);
		$pdf->SetFillColor(255,255,255);
		$pdf->Cell(27,6,utf8_decode("  " .number_format($record_emp["montoigv"],2)),1,0,'R',1);
		
		$pdf->ln(6);
		
		
	    $pdf->SetLineWidth(0.08);
		$pdf->SetFont('Arial','',7.5);
		$pdf->SetX(140);
		$pdf->SetFillColor(243,243,243);
		$pdf->SetTextColor(0,0,0);
		$pdf->Cell(34,6,utf8_decode("TOTAL  ".$Simbolo),1,0,'L',1);
		$pdf->SetX(173);
		$pdf->SetFillColor(255,255,255);
		$pdf->Cell(27,6,utf8_decode("  " .number_format($record_emp["montototal"],2)),1,0,'R',1);
		$pdf->ln(3);

	
	
	
	$y = $pdf->GetY();
	$x = $pdf->GetX();
	$pdf->SetXY(9,$y);
    

	
	 
	 
/*	
	$pdf = new PDF(); 
	$pdf->AliasNbPages();
	$pdf->AddPage();
*/
	
	$pdf->ln(20); 
     $pdf->SetTextColor(0,0,0);	
	 $pdf->SetFont('Arial','B',7);
	 $pdf->Cell(60,5,"CUENTA CORRIENTE BBVA SOLES",1,0,'C');
	 
	 $pdf->ln(5);
	 $pdf->SetFont('Arial','B',7);
	 $pdf->Cell(60,5,utf8_decode("0011 0105 0100043611"),1,0,'C');
	
	
	 $pdf->ln(5); 
	 $pdf->SetFont('Arial','',7);
	 $pdf->Cell(60,5,utf8_decode("CUENTA INTERBANCARIA BBVA (CCI) SOLES"),1,0,'C');
	 $pdf->ln(5);
	 $pdf->SetFont('Arial','B',7);
	 $pdf->Cell(60,5,utf8_decode("01110500010004361141"),1,0,'C');
	 
	$spot= "../../imagenes/spotd20544736486.png";
	$detra = $record_emp["detra"];
	
	if ($detra=="DETRA")
	{
		
		 $pdf->SetLineWidth(0.5);
		 $pdf->SetFillColor(192);
		
		
		
		
         $y = $pdf->GetY();
		 $pdf->SetXY(79,$y-10);
		 $pdf->SetTextColor(0,0,0);	
		 
		 $pdf->Rect(87,$y-15,46,20,'D');
		 $pdf->SetLineWidth(0.08);
		 
		 $pdf->SetFont('Arial','B',9);
		 $pdf->Cell(60,2,"SUJETO A DETRACCION",0,0,'C');
		 $pdf->ln(5); 
		 $y = $pdf->GetY();
		 $pdf->SetXY(80,$y);
		 $pdf->SetFont('Arial','B',9);
		 $pdf->Cell(60,2,utf8_decode("N° 00-030-079663"),0,0,'C');
		 
		 $pdf->ln(5);
		 $y = $pdf->GetY();
		 $pdf->SetXY(80,$y);
		 $pdf->SetFont('Arial','B',9);
		 $pdf->Cell(60,2,utf8_decode("BANCO DE LA NACION"),0,0,'C');
		 
	}
	
	 $pdf->SetFont('Arial','',7);

	$pdf->Output('I',$nomarchivo,true);
	unlink($vp2);
?>
