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

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

$numNum=$_GET["numNum"];
$selTipoCod=$_GET["selTipoCod"];
$selTipoNom=$_GET["selTipoNom"];
$selBanCod=$_GET["selBanCod"];
$selBanNom=$_GET["selBanNom"];
$selBanaCod=$_GET["selBanaCod"];
$selBanaNom=$_GET["selBanaNom"];
$dtFec=$_GET["dtFec"];
$numTot=$_GET["numTot"];
$numTcam=$_GET["numTcam"];
$txtCon=$_GET["txtCon"];
$crDH=$_GET["crDH"];
//$jsonPol=json_decode($_GET['tblPol']);

$func = new functions();
//$dtGrabado=(empty($_GET["dtGrabado"])) ? strftime("%d/%m/%Y %T") : $_GET["dtGrabado"];
//$btn=utf8_decode($_GET["btn"]);

class generatePDF extends PDF_MC_Table {
    function Header() {
        global $empNom, $lineHeight, $lineWidth,$dtFecI, $dtFecF, $selBan,$salIni,$Saldo,$banNom,$banCtab;
        $func = new functions();
        $this->SetMargins(2, 1, 1.35);
        $this->Cell(0, 0, "", 0, 1);

        $this->SetFont("Arial","B",12);
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode($empNom),0,1,"L");
        $this->SetFont("","I",8);
        $this->SetY(.6);
        $this->Cell(0,1,utf8_decode("PÁGINA ").$this->PageNo(),0,1,"R");
    }
    
    function Footer() {
        //global $btn, $dtGrabado;
        // Posición: a 1,5 cm del final
        $this->SetY(-2.5);
    }
}

# QUERYS
$sMovi = "SELECT *, 
             TRIM(m.mov_con) AS ch_con
             FROM movi m 
             WHERE mov_emp='$empCod' AND mov_ban='$selBanCod' AND mov_tipo='$selTipoCod' AND mov_num=$numNum";

$sPoli_det = "SELECT * FROM poli_det WHERE pold_emp='$empCod' AND pold_fec='$dtFec' AND pold_ban='$selBanCod' AND pold_tipo='$selTipoCod' AND pold_num='$numNum'";

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

$select["movi"] = $db->query($sMovi);
$rech = $db->fetchAssoc($select['movi']);
$select["poli_det"] = $db->query($sPoli_det);
$db->close();

# GENERAR PDF
    $pdf = new generatePDF("P","cm", array(22, 28));
    $pdf->AddPage();
    $pdf->SetMargins(2, 1, 2.5);
    $pdf->SetFont("Arial","B",20);

    $lineWidth=17;
    $lineHeight=0.6;

    $pdf->SetFont("","",10);
    //$pdf->SetX(4.5);
    //$pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, utf8_decode("Tipo: "));
    $pdf->MultiAlignCell($lineWidth/3, $lineHeight, trim($selTipoNom));
    //$pdf->SetX(4.5);
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("No. Documento: "));
    $pdf->MultiAlignCell($lineWidth/5, $lineHeight, trim($numNum));
    $pdf->MultiAlignCell($lineWidth/10, $lineHeight, utf8_decode("Fecha: "));
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, strftime("%d/%m/%Y", strtotime($dtFec)));
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/10, $lineHeight, "Cuenta: ");
    $pdf->MultiAlignCell($lineWidth/1.5, $lineHeight, trim($selBanNom));
    if ($crDH == "D") {
        $pdf->MultiAlignCell($lineWidth/1, $lineHeight, "SUMA AL BANCO");
    } else {
        $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "RESTA AL BANCO");
    }
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/10, $lineHeight, "Valor: ");
    $pdf->MultiAlignCell($lineWidth/5, $lineHeight, $numTot);
    $pdf->MultiAlignCell($lineWidth/10, $lineHeight, "T.C.: ");
    $pdf->MultiAlignCell($lineWidth/5, $lineHeight, $numTcam);
    if($selTipoCod == "003") {
        $pdf->Ln();
        $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode("Cuenta a la que sumara: "));
        $pdf->MultiAlignCell($lineWidth/2, $lineHeight, trim($selBanaNom));
    }    
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight, "Concepto: ");
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight, $txtCon);
    $pdf->Ln($lineHeight*4);
    $lineHeight=0.4;

    $header = array("Cuenta","Nombre de Cuenta","Debe","Haber");
    $pdf->SetFont("","B");
    $pdf->SetWidths(array(3,10,3,3));
    $pdf->SetAligns(array("C","C","C","C"));
    $pdf->Row($header, $lineHeight);

    $pdf->SetFont("");
    $pdf->SetAligns(array("L","L","R","R"));
    $debe = 0;
    $haber = 0;
//
    while ($recDet = $db->fetchAssoc($select["poli_det"])) {
//    foreach ($jsonPol as $obj) {
        $debe += $recDet["pold_debe"];
        $haber += $recDet["pold_haber"];
        $body = array(
            $recDet["pold_cta"],
            trim($recDet["pold_ctan"]),
            ($recDet["pold_debe"] != 0) ? $recDet["pold_debe"] : "",
            ($recDet["pold_haber"] != 0) ? $recDet["pold_haber"] : "",
        );
        $pdf->RowBorderLR($body, $lineHeight);
    }
    $pdf->line($pdf->GetX(), $pdf->GetY(), 21, $pdf->GetY());

    $pdf->SetAligns(array("","R","R","R"));
    $body = array(
        "",
        "Totales: ",
        number_format($debe,2),
        number_format($haber,2),
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $pdf->Output(utf8_decode("Documento ").$selBanCod."-".$numNum.".pdf","I");
?>