<?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');

$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']);
$pCaja = $_SESSION['pCaja'];

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

$arrayError=array();

# QUERYS
function sFacturas($IDInterno) {
    global $empCod, $pCaja;
    return "SELECT *, 
    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 caja ca ON ca.caj_emp='$empCod' AND ca.caj_cod='$pCaja'
    LEFT JOIN resolu res ON res.res_emp='$empCod' AND res.res_num=ca.caj_resolu
    LEFT JOIN vendedor vend ON fac.fact_vend=vend.vend_cod 
    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;
    $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"];
    include('../felPrint/FEL.php');
}
$pdf->Output('','I');

$db->close();
?>
