<?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=utf8_decode($_SESSION["pCaja"]);
$cajNombre=utf8_decode($_SESSION["cajNombre"]);

$dtFecIni = $_GET["dtFecIni"];
$dtFecFin = $_GET["dtFecFin"];
$selVend = $_GET["selVend"];
$selVendNom = TRIM($_GET["selVendNom"]);

# QUERYS
if ($selVend=="000"){
    $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";
} else {
    $sFacturas = "SELECT *
              FROM facturas 
              WHERE fact_emp='$empCod' AND fact_caja='$pCaja' AND fact_fec>='$dtFecIni' AND fact_fec<='$dtFecFin' AND fact_vend='$selVend' 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, $selVendNom,$cajNombre;
        $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, $lineHeight, "CORTE DE CAJA DETALLADO DEL $dtFecIni AL $dtFecFin",0,1,"L");
        $this->MultiAlignCell($lineWidth/1, $lineHeight, "CORRESPONDIENTES A $selVendNom Caja: $cajNombre",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","Cliente","Efectivo","Exencion","Cheques","Tarjeta","Vales","Credito","Total","Dolares");
        $this->SetFont("","B",7);
        $this->SetWidths(array(1.75,2,1.5,6.3,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5));
        $this->SetAligns(array("C","C","C","C","C","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","R","R","R","R","R","R","R","R"));
    }
    
    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("L", "cm", "Letter");
//$pdf = new FPDF('P','mm',array(500,150));
$pdf->AddPage();
$sefec = 0;
$sexenval = 0;
$schval = 0;
$starj = 0;
$svale = 0;
$scred = 0;
$stotal=0;
$sdolares=0;
while ($rec = $db->fetchAssoc($select["documentos"])) {
    $sefec += round($rec["fact_efec"], 2);
    $sexenval += round($rec["fact_exenval"], 2);
    $schval += round($rec["fact_chval"], 2);
    $starj += round($rec["fact_tarj"], 2);
    $svale += round($rec["fact_vale"], 2);
    $scred += round($rec["fact_cred"], 2);
    $total = round($rec["fact_efec"]+$rec["fact_exenval"]+$rec["fact_chval"]+$rec["fact_tarj"]+$rec["fact_vale"]+$rec["fact_cred"], 2);
    $stotal += $total;
    $sdolares += round($rec["fact_dolares"], 2);
    $body = array(
        trim($rec["fact_serie"]),
        $rec["fact_num"],
        date('d/m/Y', strtotime($rec["fact_fec"])),
        trim($rec["fact_nom"]),
        ($rec["fact_efec"] != 0) ? number_format($rec["fact_efec"], 2, '.', ',') : "",
        ($rec["fact_exenval"] != 0) ? number_format($rec["fact_exenval"], 2, '.', ',') : "",
        ($rec["fact_chval"] != 0) ? number_format($rec["fact_chval"], 2, '.', ',') : "",
        ($rec["fact_tarj"] != 0) ? number_format($rec["fact_tarj"], 2, '.', ',') : "",
        ($rec["fact_vale"] != 0) ? number_format($rec["fact_vale"], 2, '.', ',') : "",
        ($rec["fact_cred"] != 0) ? number_format($rec["fact_cred"], 2, '.', ',') : "",
        ($total != 0) ? number_format($total, 2, '.', ',') : "",
        ($rec["fact_dolares"] != 0) ? number_format($rec["fact_dolares"], 2, '.', ',') : "",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
}
$pdf->line($pdf->GetX(), $pdf->GetY(), 25.6, $pdf->GetY());
$body = array(
    "",
    "",
    "",
    "                                  TOTAL",
    number_format($sefec, 2),
    number_format($sexenval, 2),
    number_format($schval, 2),
    number_format($starj, 2),
    number_format($svale, 2),
    number_format($scred, 2),
    number_format($stotal, 2),
    number_format($sdolares, 2),
);
$pdf->RowBorderHidden($body, $lineHeight);

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