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

$empCod=utf8_decode($_SESSION["empCod"]);
$empNom=$_SESSION["empNom"];
$pCaja=$_SESSION["pCaja"];

$dtFecIni = $_GET["dtFecIni"];
$dtFecFin = $_GET["dtFecFin"];


# QUERYS
$sFacturas = "SELECT * 
              FROM facturas 
              WHERE fact_emp='$empCod' AND fact_caja='$pCaja' AND fact_fec>='$dtFecIni' AND fact_fec<='$dtFecFin' ORDER BY fact_emp,fact_fec,fact_serie,fact_num";


# EJECUCION DE QUERYS
$db = new dbExec();

//$s = $sFacturas;
/*$cr["documentos"] = "";
$cr["formaPago"] = "";
switch ($selTipoCod[0]) {
    case 'F':
        $s = $sFacturas;
        $cr["documentos"] = utf8_decode("DOCUMENTOS: {$_GET["crAnuNom"]}");
        $cr["formaPago"] = utf8_decode("FORMA DE PAGO: {$_GET["crFpagNom"]}");
        break;
    
    default:
        $s = $sNotas;
        break;
}*/
$select["documentos"] = $db->query($sFacturas);

$lineWidth=17.5;
$lineHeight=0.35;
class generatePDF extends PDF_MC_Table {
    function Header() {
        global $empNom, $lineWidth, $lineHeight, $cr, $fa;
        $dtFecIni = date('d/m/Y', strtotime($_GET["dtFecIni"]));
        $dtFecFin = date('d/m/Y', strtotime($_GET["dtFecFin"]));
        $this->SetMargins(2, 1, 2);
        $this->SetFont("Arial","",9);
        $this->Cell(0, 0, "", 0, 1);
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode($empNom),0,1,"L");
        $this->MultiAlignCell($lineWidth/1.36, $lineHeight, "BITACORA DE TRANSACCIONES DE FACTURAS",0,1,"L");
        $this->MultiAlignCell($lineWidth/1.36, $lineHeight, "CORRESPONDIENTES DEL $dtFecIni AL $dtFecFin",0,1,"L");

        // Derecha
        $this->SetY(.7);
        $this->Cell(0,1, utf8_decode("PÁGINA: ".((string)($this->PageNo()))),0,1,"R");
        $this->SetY(2.2);

        $header = array("Serie",utf8_decode("Número"),"Fecha","Caja","Cliente","Monto","Estado");
        $this->SetFont("","B",7);
        $this->SetWidths(array(1.75,2,1.5,1.75,7.3,2,2));
        $this->SetAligns(array("C","C","C","C","C","C","C"));
        $this->RowLHColor($header, $lineWidth, $lineHeight, array(192,192,192), array(0,0,0));
        $this->SetFont("");
        $this->SetAligns(array("L","L","L","L","L","R","C"));
    }
    
    function Footer() {
        $this->SetY(-1.5);
        $this->SetFont("Arial","I",6);
        $this->Cell(0,1,strftime("%d/%m/%Y %T"),0,0,"C");
    }
}

# GENERAR PDF
$pdf = new generatePDF("P", "cm", "Letter");
//$pdf = new FPDF('P','mm',array(500,150));
$pdf->AddPage();

$totDocumentos=0;
while ($rec = $db->fetchAssoc($select["documentos"])) {
    $totDocumentos += round($rec["fact_tot"], 2);
    $body = array(
        trim($rec["fact_serie"]),
        $rec["fact_num"],
        date('d/m/Y', strtotime($rec["fact_fec"])),
        trim($rec["fact_caja"]),
        trim($rec["fact_nom"]),
        number_format($rec["fact_tot"], 2),
        ($rec["fact_stat"]=='A') ? "ANULADA" : "EMITIDA",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
}
$pdf->line($pdf->GetX(), $pdf->GetY(), 20, $pdf->GetY());
$body = array(
    "",
    "",
    "",
    "",
    "                                  TOTAL",
    number_format($totDocumentos, 2),
    "",
);
$pdf->RowBorderHidden($body, $lineHeight);

$pdf->Output(utf8_decode("Bitacora de Documentos").".pdf","I");
?>