<?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"];

$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_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_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",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","Deposito","Tarjeta","Credito","Total");
        $this->SetFont("","B",8);
        $this->SetWidths(array(2,2,2,8,2,2,2,2,2));
        $this->SetAligns(array("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"));
    }
    
    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;
$sdepo = 0;
$starj = 0;
$scred = 0;
$stotal=0;
while ($rec = $db->fetchAssoc($select["documentos"])) {
    $sefec += round($rec["fact_efec"], 2);
    $sdepo += round($rec["fact_depo"], 2);
    $starj += round($rec["fact_tarj"], 2);
    $scred += round($rec["fact_cred"], 2);
    $total = round($rec["fact_efec"]+$rec["fact_depo"]+$rec["fact_tarj"]+$rec["fact_cred"], 2);
    $stotal += $total;
    $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_depo"] != 0) ? number_format($rec["fact_depo"], 2, '.', ',') : "",
        ($rec["fact_tarj"] != 0) ? number_format($rec["fact_tarj"], 2, '.', ',') : "",
        ($rec["fact_cred"] != 0) ? number_format($rec["fact_cred"], 2, '.', ',') : "",
        ($total != 0) ? number_format($total, 2, '.', ',') : "",
    );
    $pdf->RowBorderHidden($body, $lineHeight);
}
$pdf->line($pdf->GetX(), $pdf->GetY(), 26, $pdf->GetY());
$body = array(
    "",
    "",
    "",
    "                                  TOTAL",
    number_format($sefec, 2),
    number_format($sdepo, 2),
    number_format($starj, 2),
    number_format($scred, 2),
    number_format($stotal, 2),
);
$pdf->RowBorderHidden($body, $lineHeight);

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