<?php 



//include('functions.php');  
require('fpdf.php');

if (isset($_REQUEST['codigoid'])){$codigoid=$_REQUEST['codigoid'];}else{$codigoid="";}
if (isset($_REQUEST['empresa'])){$empresa=$_REQUEST['empresa'];}else{$empresa="";}
if (isset($_REQUEST['nomemp'])){$nomemp=$_REQUEST['nomemp'];}else{$nomemp="";}
if (isset($_REQUEST['nit'])){$nit=$_REQUEST['nit'];}else{$nit="";}
if (isset($_REQUEST['email'])){$email=$_REQUEST['email'];}else{$email="";}


if (isset($_REQUEST['level'])){$level=$_REQUEST['level'];}else{$level="L";}
if (isset($_REQUEST['data'])){$data=$_REQUEST['data'];}else{$data="";}
//$anticipo=0;
//$vlrletras="";
//			 $copago=0;
//			 $moderacion=0;
//			 $cpgefe=0;
//			 $crdeps=0;

//			 $ingreso=0;
//			 $egreso=0;
require_once("JSON.php");
$oJSON = new JSON();

date_default_timezone_set('America/Bogota');
$time = time();
$fecha=date("Y-m-d H:i:s", $time);


    $Sqlempre="select * from empafiliado where codigo='".$empresa."'";
	//$resultsetempre=getSQLResultSet($Sqlempre);
	//    while ($row = $resultsetempre->fetch_array(MYSQLI_ASSOC)){
	//        extract($row);
    //        $nomempresa=$row['nombre'];
	//		$nit=$row['nit']."-".$row['digito'];
	//		$email=$row['email'];
	//     }
	
     $conng = mysqli_connect('66.70.255.14','netempire_factu','J4,kcPnHQG?v','netempire_fac_electro');
	   		$conng-> set_charset("utf8");
	   		if($result = $conng->query($Sqlempre)) {
				$nr= $result->num_rows;
				if($nr>=1){
					//Asigno estructura Json a variable vJson
					while($row=mysqli_fetch_assoc($result)) {
						$nomempresa=$row['nombre'];
						$nit=$row['nit']."-".$row['digito'];
						$email=$row['email'];
					}
				}
			}
	
	
	$consecutive="";
    $Sqlactsec="select * from documentos where id='".$codigoid."'";
	//echo $Sqlactsec;
	 //$resultsetSec=getSQLResultSet($Sqlactsec);
	 $conng = mysqli_connect('66.70.255.14','netempire_factu','J4,kcPnHQG?v','netempire_fac_electro');
	   		$conng-> set_charset("utf8");
	   		if($result = $conng->query($Sqlactsec)) {
				$nr= $result->num_rows;
				if($nr>=1){
					//Asigno estructura Json a variable vJson
					while($row=mysqli_fetch_assoc($result)) {
			       		$consecutive=$row['consecutive'];
						$codigo=$row['codigo'];
					$cufe=$row['cufe'];
					$estructura=$row['estructura'];
					$qrgenerete=$row['qr'];
			
					 $fecgeneracion=$row['fechgenerado'];
 			 		$fecexpedicion=$row['fechaexped'];
			 
             		$observaciongeneral=$row['obsgen'];
             		$emailgen=$row['emailgen'];
					
					$vlrletras=$row['vlrletras'];
			 $copago=$row['copago'];
			 $moderacion=$row['moderacion'];
			 $cpgefe=$row['cpgefe'];
			 $crdeps=$row['crdeps'];
			 $anticipo=$row['anticipo'];
			 $ingreso=$row['ingreso'];
			 $egreso=$row['egreso'];
						
           	   		} mysqli_free_result($result);
					
				}
				//echo $codigo;
				
			}
	   // while ($row = $resultsetSec->fetch_array(MYSQLI_ASSOC)){
	   //   extract($row);
       //     $consecutive=$row['consecutive'];
		//	$codigo=$row['codigo'];
		//	$cufe=$row['cufe'];
		//	$estructura=$row['estructura'];
		//	$qrgenerete=$row['qr'];
			
		//	 $fecgeneracion=$row['fechgenerado'];
 		//	 $fecexpedicion=$row['fechaexped'];
			 
        //     $observaciongeneral=$row['obsgen'];
        //     $emailgen=$row['emailgen'];
 			
	    // }
	 
	$data=htmlspecialchars($qrgenerete);
    //set it to writable location, a place for temp generated PNG files
    $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
    //html PNG location prefix
    $PNG_WEB_DIR = 'temp/';
    include "phpqrcode/qrlib.php";    
    //ofcourse we need rights to create temp dir
    if (!file_exists($PNG_TEMP_DIR))
        mkdir($PNG_TEMP_DIR);
    $filename = $PNG_TEMP_DIR.$codigoid.'.png';
    //processing form input
    //remember to sanitize user input in real-life solution !!!
    $errorCorrectionLevel = 'L';
    if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H')))
        $errorCorrectionLevel = $_REQUEST['level'];    
    $matrixPointSize = 4;
    if (isset($_REQUEST['size']))
        $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10);
    if (isset($data)) { 
        //it's very important!
        if (trim($data) == '')
          //  die('data cannot be empty! <a href="?">back</a>');
        // user data
        $filename = $PNG_TEMP_DIR.$codigoid.'.png';
        QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);    
    } else {    
        //default data
       // echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>';    
        QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);    
    }    

     $sSalidaJson = $oJSON->decode($estructura);
	 

	 
	  if($observaciongeneral!=""){
            $observation1=$observaciongeneral;
			}else{  
	 		$observation1=$sSalidaJson->observation;
	 	}
      if($emailgen!=""){
            $emailgenclien=$emailgen;
			}else{  
	 		$emailgenclien=$sSalidaJson->customer->email;
	 	}
	 $observation2=$sSalidaJson->terms;
	 $observation3=$sSalidaJson->remark;

 $empresaL1 =  substr($nomempresa, 0, 31);
 $empresaL2 =  substr($nomempresa, 31, 31);
 
 $direccion=$sSalidaJson->branch->address;
 $direccionL1 =  substr($direccion, 0, 30);
 $direccionL2 =  $sSalidaJson->branch->cityName." ".$sSalidaJson->branch->countryName;
 $direccionL2=utf8_decode($direccionL2);
 $telefono=$sSalidaJson->branch->phone;
 $telefono =  substr($telefono, 0, 30);
 
 $ordencom=$sSalidaJson->externalNumber;
 $incoterms=$sSalidaJson->incoterms;

 $fecha=$sSalidaJson->date;
 $fecvencimient=$sSalidaJson->dateEnd;
       if($fecha==$fecvencimient){
            $formapago="CONTADO";
			}else{  
	 		 $formapago="CREDITO";
	 	}



class PDF extends FPDF{
function Header(){
if (isset($_REQUEST['codigoid'])){$codigoid=$_REQUEST['codigoid'];}else{$codigoid="";}
if (isset($_REQUEST['empresa'])){$empresa=$_REQUEST['empresa'];}else{$empresa="";}
if (isset($_REQUEST['nomemp'])){$nomemp=$_REQUEST['nomemp'];}else{$nomemp="";}
if (isset($_REQUEST['nit'])){$nit=$_REQUEST['nit'];}else{$nit="";}
if (isset($_REQUEST['email'])){$email=$_REQUEST['email'];}else{$email="";}

if (isset($_REQUEST['level'])){$level=$_REQUEST['level'];}else{$level="L";}
if (isset($_REQUEST['data'])){$data=$_REQUEST['data'];}else{$data="";}

require_once("JSON.php");
$oJSON = new JSON();

date_default_timezone_set('America/Bogota');
$time = time();
$fecha=date("Y-m-d H:i:s", $time);

    $Sqlempre="select * from empafiliado where codigo='".$empresa."'";
	 $conng = mysqli_connect('66.70.255.14','netempire_factu','J4,kcPnHQG?v','netempire_fac_electro');
	   		$conng-> set_charset("utf8");
	   		if($result = $conng->query($Sqlempre)) {
				$nr= $result->num_rows;
				if($nr>=1){
					//Asigno estructura Json a variable vJson
					while($row=mysqli_fetch_assoc($result)) {
						$nomempresa=$row['nombre'];
						$nit=$row['nit']."-".$row['digito'];
						$email=$row['email'];
					}
				}
			}

    $consecutive="";
    $Sqlactsec="select * from documentos where id='".$codigoid."'";
	//$resultsetSec=getSQLResultSet($Sqlactsec);
	//    while ($row = $resultsetSec->fetch_array(MYSQLI_ASSOC)){
	//      extract($row);
    $conng = mysqli_connect('66.70.255.14','netempire_factu','J4,kcPnHQG?v','netempire_fac_electro');
	   		$conng-> set_charset("utf8");
	   		if($result = $conng->query($Sqlactsec)) {
				$nr= $result->num_rows;
				if($nr>=1){
					//Asigno estructura Json a variable vJson
					while($row=mysqli_fetch_assoc($result)) {
	        $consecutive=$row['consecutive'];
			$codigo=$row['codigo'];
			$cufe=$row['cufe'];
			$estructura=$row['estructura'];
			$qrgenerete=$row['qr'];
			
			 $fecgeneracion=$row['fechgenerado'];
 			 $fecexpedicion=$row['fechaexped'];
			 
             $observaciongeneral=$row['obsgen'];
             $emailgen=$row['emailgen'];


			 $autorizacion=$row['autorizacion'];
			 $nripac=$row['nripac'];
			 $nompac=$row['nompac'];
			 $dirpac=$row['dirpac'];
			 $telpac=$row['telpac'];
			 $ciupac=$row['ciupac'];
			 $vlrletras=$row['vlrletras'];
			 $copago=$row['copago'];
			 $moderacion=$row['moderacion'];
			 $cpgefe=$row['cpgefe'];
			 $crdeps=$row['crdeps'];
			 $anticipo=$row['anticipo'];
			 $ingreso=$row['ingreso'];
			 $egreso=$row['egreso'];
					}mysqli_free_result($result);
				}
	     }
	 
     $sSalidaJson = $oJSON->decode($estructura);
	 
	  if($observaciongeneral!=""){
            $observation1=$observaciongeneral;
			}else{  
	 		$observation1=$sSalidaJson->observation;
	 	}
      if($emailgen!=""){
            $emailgenclien=$emailgen;
			}else{  
	 		$emailgenclien=$sSalidaJson->customer->email;
	 	}
	 $observation2=$sSalidaJson->terms;
	 $observation3=$sSalidaJson->remark;

 $empresaL1 =  substr($nomempresa, 0, 31);
 $empresaL2 =  substr($nomempresa, 31, 31);
 
 $direccion=$sSalidaJson->branch->address;
 $direccionL1 =  substr($direccion, 0, 30);
 $direccionL2 =  $sSalidaJson->branch->cityName." ".$sSalidaJson->branch->countryName;
 $direccionL2=utf8_decode($direccionL2);
 $telefono=$sSalidaJson->branch->phone;
 $telefono =  substr($telefono, 0, 30);
 
 $ordencom=$sSalidaJson->externalNumber;
 $incoterms=$sSalidaJson->incoterms;

 $fecha=$sSalidaJson->date;
 $fecvencimient=$sSalidaJson->dateEnd;
       if($fecha==$fecvencimient){
            $formapago="CONTADO";
			}else{  
	 		 $formapago="CREDITO";
	 	}

	
     $this->Image('temp/'.$codigoid.'.png',175,55,28);
     $this->Ln(5);
	 $this->Image('afiliado/'.$empresa.'.png',10,10,40);
	 
	 $this->Ln(1);
	 $this->Cell(50,1);
	 $this->SetFont('Arial','B',9);
	 $this->Cell(10,1,$empresaL1,0,0,'0');
	 
	 $this->Cell(70,1);
	 $this->SetFont('Arial','B',9);
	 $this->Cell(10,1,'FACTURA ELECTRONICA DE VENTA',0,0,'0');
	 
	 $this->Ln(3);
	 $this->Cell(50,1);
	 $this->SetFont('Arial','B',9);
	 $this->Cell(10,1,$empresaL2,0,0,'0');
	 
     $this->Cell(110,1);
	 $this->SetFont('Arial','B',9);
	 $this->Cell(10,1,$codigo,0,0,'0');
	 
     $this->Ln(4);
	 $this->Cell(50,1);
	 $this->SetFont('Arial','B',9);
	 $this->Cell(10,1,"NIT: ".$nit,0,0,'0');
	 
     $this->Cell(85,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,"Forma de pago: ".$formapago,0,0,'0');
	 
     $this->Ln(4);
	 $this->Cell(50,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,$direccionL1,0,0,'0');
	 
     $this->Cell(85,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,"Fecha: ".$fecha,0,0,'0');
	 
     $this->Ln(4);
	 $this->Cell(50,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,$direccionL2,0,0,'0');
	 
     $this->Cell(85,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,"Fecha de Vencimiento: ".$fecvencimient,0,0,'0');
	 
     $this->Ln(4);
	 $this->Cell(50,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,$telefono,0,0,'0');
	 
     $this->Cell(85,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,"Medio de Pago: Instrumento no definido",0,0,'0');
	 
     $this->Ln(4);
	 $this->Cell(50,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,$email,0,0,'0');
	 
     $this->Cell(85,1);
	 $this->SetFont('Arial','',8);
	 
	 $this->Cell(10,1,"MONEDA / TRM: COP / $0",0,0,'0');
	 
     $this->Ln(4);
	 $this->Cell(50,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,$fecgeneracion,0,0,'0');
	 
     $this->Cell(85,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,"Orden de Compra: ".$ordencom,0,0,'0');
	 
     $this->Ln(4);
	 $this->Cell(50,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,$fecexpedicion,0,0,'0');
	 
     $this->Cell(85,1);
	 $this->SetFont('Arial','',8);
	 $this->Cell(10,1,"INCOTERMS:".$incoterms,0,0,'0');
	 
     $this->Ln(4);
	 $this->Cell(45,1);
	 $this->SetFont('Arial','',7.2);
	 $this->Cell(10,1,"CUFE:".$cufe,0,0,'0');
     
	 $this->Ln(1);
	 $this->Cell(10,1,"_______________________________________________________________________________________________________________________________________________________________",0,0,'0');
	 
     $this->Ln(4);
	 $this->Cell(1,1);
	 $this->SetFont('Arial','B',10);
	 $this->Cell(1,1,"DATOS DEL CONVENIO",0,0,'0');
     $this->Cell(90,1);
	 $this->Cell(1,1,"DATOS DEL PACIENTE",0,0,'0');
   
	 $this->Ln(4);
     $this->Cell(1,1);
	 $this->SetFont('Arial','B',8);
	 $this->Cell(1,1,utf8_decode("Razón Social: "));
	 $this->SetFont('Arial','',8);
	 $this->Cell(1,1,"                         ".utf8_decode($sSalidaJson->customer->companyName),0,0,'0');
	 $this->SetFont('Arial','B',8);
	 $this->Cell(1,1,"                                                                                 
	                              Nombre: ");
     $this->SetFont('Arial','',8);
	 $this->Cell(1,1,"                                                                                 
	                                             ".utf8_decode($nompac),0,0,'0');


     $this->Ln(4);
     $this->Cell(1,1);
	 $this->SetFont('Arial','B',8);
	 $this->Cell(1,1,"Nit: ");
	 $this->SetFont('Arial','',8);
	 $this->Cell(1,1,"      ".$sSalidaJson->customer->identification."-".$sSalidaJson->customer->digitCheck,0,0,'0');
	 $this->SetFont('Arial','B',8);
	 $this->Cell(1,1,"                                                                                 
	                              Documento: ");
	 $this->SetFont('Arial','',8);
	 $this->Cell(1,1,"                                                                                 
	                                                  ".utf8_decode($nripac),0,0,'0');

	 $this->Ln(1);
	 $this->Cell(10,1,"________________________________________________________________________________________________________",0,0,'0');
	 

	 $this->Ln(4);
     $this->Cell(1,1);
	 $this->SetFont('Arial','B',8);
	 $this->Cell(1,1,"Direccion: ");
	 $this->SetFont('Arial','',8);
	 $this->Cell(1,1,"                  ".utf8_decode($sSalidaJson->customer->billingAddress),0,0,'0');
	 $this->SetFont('Arial','B',8);
	 $this->Cell(1,1,"                                                                                 
	                              Direccion: ");
	 $this->SetFont('Arial','',8);
	 $this->Cell(1,1,"                                                                                 
	                                                  ".utf8_decode($dirpac),0,0,'0');

	 $this->Ln(4);
     $this->Cell(1,1);
	 $this->SetFont('Arial','B',8);
	 $this->Cell(1,1,"Telefono: ");
	 $this->SetFont('Arial','',8);
	 $this->Cell(1,1,"                  ".utf8_decode($sSalidaJson->customer->phone),0,0,'0');
	 $this->SetFont('Arial','B',8);
	 $this->Cell(1,1,"                                                                                 
	                              Telefono: ");
	 $this->SetFont('Arial','',8);
	 $this->Cell(1,1,"                                                                                 
	                                                  ".utf8_decode($telpac),0,0,'0');

	 $this->Ln(4);
     $this->Cell(1,1);
	 $this->SetFont('Arial','B',8);
	 $this->Cell(1,1,"Ciudad: ");
	 $this->SetFont('Arial','',8);
	 $this->Cell(1,1,"                  ".utf8_decode($sSalidaJson->branch->cityName),0,0,'0');
	 $this->SetFont('Arial','B',8);
	 $this->Cell(1,1,"                                                                                 
	                              Ciudad: ");
	 $this->SetFont('Arial','',8);
	 $this->Cell(1,1,"                                                                                 
						                                             ".utf8_decode($ciupac),0,0,'0');


    $this->Ln(4);
	$this->Cell(1,1);
	$this->SetFont('Arial','',7);
	$this->Cell(1,1,$emailgenclien,0,0,'0');


	$this->SetFont('Arial','B',8);
	$this->Cell(1,1,"                                                                                 
	                                Ingreso: ");
	$this->SetFont('Arial','',8);
	$this->Cell(1,1,"                                                                                 
						                                          ".utf8_decode($ingreso),0,0,'0');
	$this->SetFont('Arial','B',8);
	$this->Cell(1,1,"                                                                                 
	                                                                  Egreso: ");
	$this->SetFont('Arial','',8);
	$this->Cell(1,1,"                                                                                 
	                                                                               ".utf8_decode($egreso),0,0,'0');

    $this->Ln(4);
    $this->SetFont('Arial','B',8);
    $this->Cell(1,1,"                                                         
	                               No Autorizacion ");
    $this->SetFont('Arial','',8);
    $this->Cell(1,1,"                              
	                                                                                       ".utf8_decode($autorizacion),0,0,'0');	

     
	 $this->Image('linedes.png',10,88,200);
     $this->Ln(5);
	 $this->SetFont('Arial','B',9);
	 $this->Cell(1,1);
	 $this->Cell(1,1,"",0,0,'0');
	 $this->Cell(20,1);
	 $this->Cell(1,1,utf8_decode("DESCRIPCIÓN"),0,0,'0');
	 $this->Cell(65,1);
     $this->Cell(1,1,"CANT",0,0,'0');
	 $this->Cell(10,1);
     $this->Cell(1,1,"U/M",0,0,'0');
     $this->Cell(18,1);
     $this->Cell(1,1,"VR. UNIT.",0,0,'0');
     $this->Cell(18,1);
     $this->Cell(1,1,"IMP.",0,0,'0');
     $this->Cell(18,1);
     $this->Cell(1,1,"DCTO.",0,0,'0');
     $this->Cell(18,1);
     $this->Cell(1,1,"TOTAL.",0,0,'0');
	 $this->Ln(3);
	 
    // Line break
    
}
function Footer(){
    if (isset($_REQUEST['codigoid'])){$codigoid=$_REQUEST['codigoid'];}else{$codigoid="";}
	require_once("JSON.php");
    $oJSON = new JSON();

    $consecutive="";
    $Sqlactsec="select * from documentos where id='".$codigoid."'";
	//$resultsetSec=getSQLResultSet($Sqlactsec);
	//    while ($row = $resultsetSec->fetch_array(MYSQLI_ASSOC)){
	//        extract($row);
	$conng = mysqli_connect('66.70.255.14','netempire_factu','J4,kcPnHQG?v','netempire_fac_electro');
	   		$conng-> set_charset("utf8");
	   		if($result = $conng->query($Sqlactsec)) {
				$nr= $result->num_rows;
				if($nr>=1){
					//Asigno estructura Json a variable vJson
					while($row=mysqli_fetch_assoc($result)) {
			$estructura=$row['estructura'];
	     }
				}
			}
     $sSalidaJson = $oJSON->decode($estructura);
     $prefije=$sSalidaJson->resolution->resolutionPrefix;
     $resolutionNumber=$sSalidaJson->resolution->resolutionNumber;
	 $resolutionValidFrom=$sSalidaJson->resolution->resolutionValidFrom;
	 $resolutionValidUntil=$sSalidaJson->resolution->resolutionValidUntil;
	 $resolutionRangeInitial=$sSalidaJson->resolution->resolutionRangeInitial;
	 $resolutionRangeFinal=$sSalidaJson->resolution->resolutionRangeFinal;

$piepagine="Autorización según resolución número ".$resolutionNumber." vigencia de ".$resolutionValidFrom." al ".$resolutionValidUntil
." rango del ".$resolutionRangeInitial." al ".$resolutionRangeFinal
." prefijo ".$prefije;

	 $piepagine2="Elaborado por Software: EM-EMPIRE - Proveedor Tecnológico:  ALIADDO SAS NIT:830099008-5";
    // Posición: a 1,5 cm del final
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','B',8);
    // Número de página
    $this->Cell(0,10,utf8_decode($piepagine).'/{nb}',0,0,'');
	$this->Ln(4);
	$this->Cell(0,10,utf8_decode($piepagine2).'/{nb}',0,0,'');
	$this->Ln(4);
	$this->SetFont('Arial','I',8);
    $this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'C');
}
}

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();

$obj = $oJSON->encode($sSalidaJson->invoiceDetails);
$arr = json_decode($obj, true);
$sumatotal=0;
$subtotal=0;
$formateador = new NumberFormatter( 'es_ES', NumberFormatter::DECIMAL );
$cantidaddet=0;
$cantidadlinea=0;
foreach($arr as $key => $value) {
	$cantidaddet=$cantidaddet+1;
	$descritionr=$value["itemCode"]." ".$value["itemName"];
	 $pdf->Ln(5);
     $pdf->SetFont('Arial','',8);
	 $pdf->Cell(1,1);
	 $pdf->Cell(1,1,$key+1,0,0,'0');
	 $pdf->Cell(20,1);
	 $pdf->Cell(1,1,substr(utf8_decode($descritionr), 0, 35),0,0,'0');
	 $pdf->Cell(65,1);
     $pdf->Cell(1,1,"   ".$value["quantity"],0,0,'0');
	 $pdf->Cell(10,1);
     $pdf->Cell(1,1,"NIU",0,0,'0');
     $pdf->Cell(18,1);
     
     $precie=$formateador->format($value["price"]) . PHP_EOL;
	 
     $pdf->Cell(1,1,"   $".$precie,0,0,'0');
     $pdf->Cell(18,1);
     $pdf->Cell(1,1,"  0",0,0,'0');
     $pdf->Cell(18,1);
     $pdf->Cell(1,1,"  0",0,0,'0');
     $pdf->Cell(18,1);
	 $total=$value["price"]*$value["quantity"];
     $sumatotal=$sumatotal+$total;
	 $subtotal=$subtotal+$total;
     $total=$formateador->format($total) . PHP_EOL;

     $pdf->Cell(1,1,"    $".$total,0,0,'0');
	 $pdf->Cell(18,1);
		  
$cDet=substr(utf8_decode($descritionr), 35, 2000);
$tTam=strlen($cDet);
$cadena="";
$m=35; 
$cantidadlinea=$cantidadlinea+1;
 for($n=0; $n<$tTam; $n++): 
 	if($n+35>$tTam){
		$m=$tTam-$n;
	}
	$cadena=substr($cDet,$n,$m);
    $n = $n + $m ;
	--$n;
        $pdf->Ln(4);
        $pdf->SetFont('Arial','',7);
	    $pdf->Cell(1,1);
	    $pdf->Cell(1,1," ",0,0,'0');
        $pdf->Cell(20,1);
        $pdf->Cell(1,1,utf8_decode($cadena),0,0,'0');
		$cantidadlinea=$cantidadlinea+1;
		
 endfor;
       /*
	    $pdf->Ln(4);
        $pdf->SetFont('Arial','',7);
	    $pdf->Cell(1,1);
	    $pdf->Cell(1,1," ",0,0,'0');
        $pdf->Cell(20,1);
        $pdf->Cell(1,1," Modelo: ".$value["itemModel"],0,0,'0');
		*/
}

if($cantidaddet<=2){
  for($n=0; $n<5; $n++){
        //$pdf->Ln(2);
  }
}

if($cantidadlinea>=30){
	$pdf->Ln(25);
}
$totalpie=$sumatotal-$anticipo;
$totalpie=$formateador->format($totalpie) . PHP_EOL;
$pdf->Ln(1);
$pdf->Cell(1,1,"_________________________________________________________________________________________________________________________",0,0,'0');
     $pdf->Ln(6);
	 $pdf->SetFont('Arial','B',10);
	 $pdf->Cell(1,1,"Copago:",0,0,'0');
	 $pdf->SetFont('Arial','',10);
	 $copago=$formateador->format($copago) . PHP_EOL;
	 $pdf->Cell(1,1,"                $".$copago,0,0,'0');
	 $pdf->SetFont('Arial','B',10);
	 $pdf->Cell(1,1,utf8_decode("                                           Moderación:"),0,0,'0');
	 $pdf->SetFont('Arial','',10);
	 $moderacion=$formateador->format($moderacion) . PHP_EOL;
	 $pdf->Cell(1,1,"                                                                   $".$moderacion,0,0,'0');
	 $pdf->SetFont('Arial','B',10);
	 $pdf->Cell(1,1,utf8_decode("                                                
	                                        SUBTOTAL DETALLE DE LOS SERVICIOS:"),0,0,'0');
	 $pdf->SetFont('Arial','',10);
	 $sumatotal=$formateador->format($sumatotal) . PHP_EOL;
	 $pdf->Cell(1,1,"                                                                       
	                                                                                            $".$sumatotal,0,0,'0');
	
     $pdf->Ln(8);
	 $pdf->SetFont('Arial','B',9);
	 $pdf->Cell(1,1,"VALOR EN LETRAS:".$vlrletras,0,0,'0');


	 $pdf->Ln(4);
	 $pdf->SetFont('Arial','B',9);
	 $pdf->Cell(1,1,"Forma de Pago",0,0,'0');
	 
	 
	 $pdf->SetFont('Arial','B',10);
	 $pdf->Cell(1,1,"                                                                 
	                                                                            SUBTOTAL:",0,0,'0');
	 $pdf->SetFont('Arial','',9);
	 $pdf->Cell(1,1,"                                                                 
	                                                                                                     
	                  $".$sumatotal,0,0,'0');

    $pdf->Ln(4);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(1,1,"Credito Eps:",0,0,'0');
    $pdf->SetFont('Arial','',9);
	$crdeps=$formateador->format($crdeps) . PHP_EOL;
    $pdf->Cell(1,1,"                                          $".$crdeps,0,0,'0');
    $pdf->SetFont('Arial','B',10);
    $pdf->Cell(1,1,"                                                               
	                                                VALOR PAGO POR USUARIO:",0,0,'0');
    $pdf->SetFont('Arial','',9);
    $anticipo=$formateador->format($anticipo) . PHP_EOL;
	$pdf->Cell(1,1,"                                                            
	                                                                                                                              $".$anticipo,0,0,'0');
    $pdf->Ln(4);
    $pdf->SetFont('Arial','',9);
    $pdf->Cell(1,1,"Efectivo:",0,0,'0');
    $pdf->SetFont('Arial','',9);
	
	
    $pdf->Cell(1,1,"                                          $".$anticipo,0,0,'0');
    $pdf->SetFont('Arial','B',10);
    $pdf->Cell(1,1,"                                                               
	                                                                                       TOTAL:",0,0,'0');
    $pdf->SetFont('Arial','',9);
	$pdf->Cell(1,1,"                                                            
	                                                                                                                              $".$totalpie,0,0,'0');

//ante pie pagine 
$pdf->Ln(10);
$pdf->SetFont('Arial','',8);
$pdf->Cell(1,1);
$pdf->Cell(1,1,substr($observation1, 0, 40),0,0,'0');
$pdf->SetFont('Arial','',8);
$pdf->Cell(70,1);
$pdf->Cell(1,1,substr($observation2, 0, 40),0,0,'0');
$pdf->Cell(70,1);

$pdf->Ln(3);
$pdf->SetFont('Arial','',8);
$pdf->Cell(1,1);
$pdf->Cell(1,1,substr($observation1, 40, 40),0,0,'0');
$pdf->SetFont('Arial','',8);
$pdf->Cell(70,1);
$pdf->Cell(1,1,substr($observation2, 40, 40),0,0,'0');
$pdf->Cell(70,1);

$pdf->Ln(3);
$pdf->SetFont('Arial','',8);
$pdf->Cell(1,1);
$pdf->Cell(1,1,substr($observation1, 80, 40),0,0,'0');
$pdf->SetFont('Arial','',8);
$pdf->Cell(70,1);
$pdf->Cell(1,1,substr($observation2, 80, 40),0,0,'0');

$pdf->Ln(3);
$pdf->SetFont('Arial','',8);
$pdf->Cell(1,1);
$pdf->Cell(1,1,substr($observation1, 120, 40),0,0,'0');
$pdf->SetFont('Arial','',8);
$pdf->Cell(70,1);
$pdf->Cell(1,1,substr($observation2, 120, 40),0,0,'0');

$pdf->Ln(3);
$pdf->SetFont('Arial','',8);
$pdf->Cell(1,1);
$pdf->Cell(1,1,substr($observation1, 160, 40),0,0,'0');
$pdf->SetFont('Arial','',8);
$pdf->Cell(70,1);
$pdf->Cell(1,1,substr($observation2, 160, 40),0,0,'0');
//$pdf->Cell(70,1);
//$subtotal=$formateador->format($subtotal) . PHP_EOL;
//$pdf->Cell(1,1,"                 TOTAL     $".$sumatotal,0,0,'0');

//fin ante pie pagine 	 	 

$pdf->Ln(10);
$pdf->SetFont('Arial','',9);
$pdf->Cell(1,1);
$pdf->Cell(1,1,substr(utf8_decode($observation3), 0, 135),0,0,'0');
$cDet=substr(utf8_decode($observation3), 135, 135);
$tTam=strlen($cDet);
$cadena="";
$m=135; 
 for($n=0; $n<$tTam; $n++): 
 	if($n+135>$tTam){
		$m=$tTam-$n;
	}
	$cadena=substr($cDet,$n,$m);
    $n = $n + $m ;
	--$n;
     $pdf->Ln(3);
	 $pdf->SetFont('Arial','',9);
     $pdf->Cell(1,1);
	 $pdf->Cell(1,1,utf8_decode($cadena),0,0,'0');
 endfor;
	 
	 //$pdf->Image('pedido.png',20,10,0);
$pdf->Output();
?>