<?php
include_once("../../../php/sessionStart.php"); 
include_once("../../../php/db/dbExec.php");
include_once('../../../php/functions.php');
include_once("../../../php/fpdf/functions.php");
include_once('../../../php/timezone.php');
include_once('../../../php/maxExecTime.php');
include_once('../ws/COFIDI/wsExec.php');

$empCod=utf8_decode($_SESSION["empCod"]);
$empNom=utf8_decode($_SESSION["empNom"]);
$empNomc=utf8_decode($_SESSION["empNomc"]);
$empDir=utf8_decode($_SESSION["empDir"]);
$empNit=str_replace('-','',$_SESSION["empNit"]);
$usuNom = utf8_decode($_SESSION['usuNom']);

$jsonFac = (isset($_GET['tblFac'])) ? json_decode($_GET['tblFac']) : json_decode($_SESSION['tblFac']);

$arrayError=array();

# QUERYS
function sFacturas($IDInterno) {
    global $empCod;
    return "SELECT *, iv.ivav_iva AS ivav_iva, 
    TRIM(fac.fact_nom) AS fact_nom,
    TRIM(fac.fact_dir) AS fact_dir,
    TRIM(fac.fact_tel) AS fact_tel,
    TRIM(fac.fact_nit) AS fact_nit,
    TRIM(tv.tv_nom) AS tv_nom
    FROM facturas fac
    LEFT JOIN tipov tv ON fac.fact_tipo=tv.tv_cod
    LEFT JOIN iva_ven iv ON fac.fact_emp=iv.ivav_emp AND fac.fact_serie=iv.ivav_serie AND fac.fact_num=iv.ivav_num 
    WHERE fact_emp='$empCod' AND fact_transac='$IDInterno'";
}

function sFact_det($IDInterno) {
    global $empCod, $pCaja;
    return "SELECT *,
            TRIM(factd_cod) AS factd_cod,
            TRIM(factd_desc) AS factd_desc
            FROM fact_det WHERE factd_emp='$empCod' AND factd_transac='$IDInterno'";
}

class generatePDF extends PDF_MC_Table {
    function Header() {
    }
    
    function Footer() { 
    }
}

$db = new dbExec();
$func = new functions();

# GENERAR PDF
$pdf = new generatePDF("P", "cm", "Letter");
/*$pdf->SetFont("Arial","",10);
$pdf->SetTextColor(0,0,0);
$pdf->AddPage();  */

$lineWidth=19.5;
$lineHeight=0.4;
$lineHeight7 = .3;
//$pdf->SetMargins(2, 1, 1);

foreach ($jsonFac as $obj) {
    $IDInterno =  $obj->IDInterno;
    $select["facturas"] = $db->query(sFacturas($IDInterno));
    $select["fact_det"] = $db->query(sFact_det($IDInterno));
    $rec = $db->fetchAssoc($select["facturas"]);
    $totLetras = $obj->totLetras;
    $TdocNom = $rec["tv_nom"];
    IF ($rec["fact_tipoform"]=='B') {
      include('../felPrint/FEL.php');
    } else {
      include('../felPrint/FELSERV.php');
    }
}
$pdf->Output('','I');

$db->close();
?>