<?php
$lineWidth=19.5;
$lineHeight=0.4;

if ($rec["fact_tcam"]>1) {
   $tcam="$";
} else {
   $tcam="Q";
}  

$pdf->AddPage();

// Para anular factura
if ($rec["fact_tot"] == 0) {
    $pdf->SetFont('Arial','B',100);
    $pdf->SetTextColor(255, 144, 144);
    $pdf->SetY(12);
    $pdf->Cell($lineWidth, 1, "ANULADO",0,0,"C");
}

$pdf->SetFont("Arial","B",7);
$pdf->SetTextColor(0,0,0);

$lineHeight7 = .3;
$img = '../img/Galavista01.jpg';
$pdf->Image($img,.9,-1,6.8,7.3);
$pdf->SetMargins(1, 1, 1);
$pdf->SetY(1.4);
$pdf->Cell(0, 0, "", 0, 1);
$pdf->Cell($lineWidth/3.5, $lineHeight7, "");
$pdf->SetFont("Arial","B",9);
$pdf->Cell($lineWidth/2.5, $lineHeight7+.5, TRIM(SUBSTR($empNom,0,30)),0,1,"C");
//$pdf->Cell($lineWidth/5, $lineHeight7, "");
//$pdf->SetFont("Times","BI",15);
//$pdf->Cell($lineWidth/3, $lineHeight7+.5, TRIM(SUBSTR($empNom,31,30)),0,1,"C");
$pdf->SetFont("Arial","B",10);
$pdf->Cell($lineWidth/3.5, $lineHeight7, "");
$pdf->Cell($lineWidth/2.5, $lineHeight7, 'NIT: '.$empNit,0,1,"C");
$pdf->Cell($lineWidth/3.1, $lineHeight7, "");
$pdf->Cell($lineWidth/3, $lineHeight7, $rec["caj_nomt"],0,1,"C");
$pdf->Cell($lineWidth/2.9, $lineHeight7, "");
$pdf->MultiAlignCell($lineWidth/3.5, .3, $rec["caj_dire"],0,1,"C");
//$pdf->Ln();
//$pdf->Cell($lineWidth/5, $lineHeight7, "");
//$pdf->Cell($lineWidth/3, $lineHeight7, utf8_decode($_SESSION["empNompat"]),0,1,"C");

$pdf->SetFont("Arial","B",9);
// Derecha
$y = 1.3;
$x = 14;
$pdf->SetY($y);
$pdf->SetX($x);
$pdf->Cell($lineWidth/2.9, 1, "DOCUMENTO TRIBUTARIO ELECTRONICO",0,1,"C");
$pdf->SetY($y+=$lineHeight+.07);
$pdf->SetX($x);
$pdf->Cell($lineWidth/2.9,1, utf8_decode($TdocNom),0,1,"C");

if ($rec["fact_documentguid"]!="") {
    $pdf->SetY($y+=$lineHeight);
    $pdf->SetX($x);
    $pdf->Cell($lineWidth/3,1, "NUMERO DE AUTORIZACION:",0,1,"C");
    $pdf->SetY($y+=$lineHeight);
    $pdf->SetX($x);
    $pdf->Cell($lineWidth/3,1, $rec["fact_documentguid"],0,1,"C");
    $pdf->SetY($y+=$lineHeight);
    $pdf->SetX($x);
    $pdf->Cell($lineWidth/3,1, "SERIE: ".$rec["fact_serie"],0,1,"C");
    $pdf->SetY($y+=$lineHeight);
    $pdf->SetX($x);
    $pdf->Cell($lineWidth/3,1, "NUMERO: ".$rec["fact_num"],0,1,"C");
    $pdf->SetY($y+=$lineHeight);
    $pdf->SetX($x);
    $pdf->Cell($lineWidth/3,1, "Id interno: ".$idinterno,0,1,"C");
} else {
    $pdf->SetY($y+=$lineHeight*2);
    $pdf->SetX($x);
    $pdf->SetFont("Arial","B",28);
    $pdf->MultiAlignCell($lineWidth/3,.8, "VISTA PREVIA",0,1,"C");
    $pdf->SetFont("Arial","B",9);
}
$pdf->SetY(4.5);

$pdf->line($pdf->GetX(), $pdf->GetY(), $lineWidth+1, $pdf->GetY());

$pdf->SetY($pdf->GetY()+.25);

$pdf->SetFont("");
$pdf->MultiAlignCell($lineWidth/9.50, $lineHeight, utf8_decode("NOMBRE:"),0,0,"L");
$pdf->MultiAlignCell($lineWidth/1.5, $lineHeight, $rec["fact_nom"],0,0,"L");
$pdf->MultiAlignCell($lineWidth/10, $lineHeight, utf8_decode("FECHA:"),0,0,"L");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, date('d-m-Y', strtotime($rec["fact_fec"])),0,1,"L");

$pdf->MultiAlignCell($lineWidth/9.40, $lineHeight, utf8_decode("DIRECCIÓN:"),0,0,"L");
$pdf->MultiAlignCell($lineWidth/1.5, .3, $rec["fact_dir"],0,0,"L");
$pdf->MultiAlignCell($lineWidth/10, $lineHeight, utf8_decode("NIT:"),0,0,"L");
$pdf->MultiAlignCell($lineWidth/8, $lineHeight, utf8_decode(str_replace('-','',$rec["fact_nit"])),0,1,"L");

$pdf->SetY($pdf->GetY()+.25);

$header = array("CODIGO",utf8_decode("DESCRIPCIÓN"),"CANTIDAD","P/UNIT.","IMPORTE ".$tcam);
$widths = array(3.3,9.2,2,2.25,2.75);
$pdf->SetWidths($widths);
$pdf->SetAligns(array("C","C","C","C","C"));
$pdf->Row($header, $lineHeight);

$pdf->SetAligns(array("L","L","C","R","R"));

// Lineas separadoras del detalle
$detX = 0;
$detY = $pdf->GetY()+4.9;
$pdf->line($pdf->GetX(), $pdf->GetY(), $pdf->GetX(), $detY);
foreach ($widths as $x) {
    $detX+=$x;
    $pdf->line($pdf->GetX()+$detX, $pdf->GetY(), $pdf->GetX()+$detX, $detY);
}
$descuv=0;
$sub=0;
$sumExen=0;
$sumGrav=0;
while ($recDet = $db->fetchAssoc($select["fact_det"])) {
    $body = array(
        $recDet["factd_cod"],
        ($recDet["factd_exensn"]=='S') ? "E-".$recDet["factd_desc"] : "  ".$recDet["factd_desc"],
        ($recDet["factd_cant"] > 0) ? number_format($recDet["factd_cant"],0) : "",
        ($recDet["factd_tot"] > 0) ? number_format(round(($recDet["factd_tot"]/$recDet["factd_cant"]),4),4) : "",
        ($recDet["factd_tot"] > 0) ? number_format($recDet["factd_tot"],2) : ""
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $sub+=$recDet["factd_sub"];
    $descuv+=$recDet["factd_descuv"];
    if ($recDet["factd_exensn"]=='S') {
        $sumExen+=$recDet["factd_tot"];
    } else {
        $sumGrav+=$recDet["factd_tot"];
    }  
}
$pdf->SetY(9.9);
$pdf->Cell($lineWidth/5.5, $lineHeight, " ",0,0,"L");
$pdf->Cell($lineWidth/1, $lineHeight, utf8_decode("(E) EXENTO DE IVA Según Art. 7 numeral 15 Ley del IVA              "),0,1,"L");
$pdf->Cell($lineWidth/5.5, $lineHeight, " ",0,0,"L");
$pdf->Cell($lineWidth/1, $lineHeight, "Total Exento: ".number_format($sumExen, 2)." Total Gravado: ".number_format($sumGrav, 2)."   ",0,1,"L");
$pdf->Cell($lineWidth-2, $lineHeight, "** SUJETO A PAGOS TRIMESTRALES **          ",0,1,"C");

$moneda = ($tcam=='Q') ? "QUETZALES" : utf8_decode("DÓLARES");
$pdf->SetFont("","",6);
//$pdf->MultiAlignCell(15.7, $lineHeight, "",'TLB',0,"L");
//$pdf->SetFont("","",8);
//$pdf->Cell(1.05, $lineHeight, "SUBTOTAL:",'TBR',0,"R");
//$pdf->Cell(2.75, $lineHeight, $tcam.number_format($sub, 2),1,1,"R");
//$pdf->SetFont("","",6);
//$pdf->MultiAlignCell(15.7, $lineHeight, "",'TLB',0,"L");
//$pdf->SetFont("","",8);
//$pdf->Cell(1.05, $lineHeight, "(-) DESCUENTO:",'TBR',0,"R");
//$pdf->Cell(2.75, $lineHeight, $tcam.number_format($descuv, 2),1,1,"R");
//$pdf->SetFont("","",6);
$pdf->MultiAlignCell(15.7, $lineHeight, "EN LETRAS: $totLetras $moneda",'TLB',0,"L");
$pdf->SetFont("","",8);
$pdf->Cell(1.05, $lineHeight, "TOTAL:",'TBR',0,"R");
$pdf->SetFont("","B",10);
$pdf->Cell(2.75, $lineHeight, $tcam.number_format($rec["fact_tot"], 2),1,1,"R");

$pdf->Ln();

//$pdf->MultiAlignCell($lineWidth/2, $lineHeight, "AUTORIZADO\n\n________________________\n".utf8_decode($empNomc),1,0,"C");
//$pdf->MultiAlignCell($lineWidth/2, $lineHeight, "CLIENTE\n\n________________________\nRECIBI CONFORME",1,0,"C");

//$pdf->Ln(.2);

$FechaHoraCertificacion = $rec["fact_guardado"];
$NombreCertificador = "INFILE, S.A. - NIT:12521337";

$pdf->Cell($lineWidth, .1, "",1,1);
$pdf->Cell($lineWidth, $lineHeight, "DATOS FISCALES",1,1,"C");
$pdf->SetFont("Arial","B");
$pdf->Cell($lineWidth, $lineHeight, utf8_decode("Fecha de certificación: ").$FechaHoraCertificacion,1,1);
$pdf->Cell($lineWidth, $lineHeight, utf8_decode("CERTIFICADOR: ").$NombreCertificador,1,1);
?>