<?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="004";
$selBanCod=$_GET["selBanCod"];
$dtFec=$_GET["dtFec"];
$numTot=$_GET["numTot"];
$numTcam=$_GET["numTcam"];
$txtBene=$_GET["txtBene"];
$txtCon=$_GET["txtCon"];
$cbNego=$_GET["cbNego"];
$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() {
    }
    
    function Footer() {
        //global $btn, $dtGrabado;
        // Posición: a 1,5 cm del final
        $this->SetY(-2.5);
    }
}

# QUERYS
$sCheques = "SELECT *, 
             TRIM(ch.ch_bene) AS ch_bene,
             TRIM(ch.ch_con) AS ch_con,
             TRIM(ban.ban_nom) AS ban_nom,
             TRIM(ban.ban_ctab) AS ban_ctab 
             FROM cheques ch INNER JOIN bancos ban ON ch.ch_emp=ban.ban_emp AND ch.ch_ban=ban.ban_num 
             WHERE ch_emp='$empCod' AND ch_ban='$selBanCod' AND ch_tipo='$selTipoCod' AND ch_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["cheques"] = $db->query($sCheques);
$rech = $db->fetchAssoc($select['cheques']);
$db->close();

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

    $lineWidth=17;
    $lineHeight=0.6;

    $pdf->SetFont("","",9);
    $pdf->setx(10);
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight, $numNum);
    $pdf->Ln($lineHeight*4);

    $pdf->SetFont("","");
    $pdf->SetX(4.5);
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("Guatemala, "));
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, strftime("%d/%m/%Y", strtotime($dtFec)));
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, "***".number_format($numTot,2),0,1,"R");
    $pdf->SetX(4.5);
    $pdf->MultiAlignCell($lineWidth/1.1, $lineHeight,utf8_decode($txtBene),0,1);
    $pdf->SetX(4.5);
    $pdf->MultiAlignCell($lineWidth/1.1, $lineHeight,utf8_decode($func->numLetras((float)$numTot)));
    $pdf->Ln($lineHeight*2);
    if ($cbNego == true) {
        $pdf->SetFont("","B");
        $pdf->MultiAlignCell($lineWidth/1, $lineHeight, "                                          NO NEGOCIABLE");
    } else {
        $pdf->SetFont("","B");
        $pdf->MultiAlignCell($lineWidth/4, $lineHeight, " ");
    }

    $lineHeight=0.4;

    $pdf->Ln($lineHeight*6);

    $header = array("Cuenta","Nombre de Cuenta","Debe","Haber");
    $pdf->SetFont("","B");
    $pdf->SetWidths(array(3,8,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;
    foreach ($jsonPol as $obj) {
        $debe += $obj->debe;
        $haber += $obj->haber;
        $body = array(
            $obj->cta,
            trim($obj->ctaNom),
            ($obj->debe != 0) ? $obj->debe : "",
            ($obj->haber != 0) ? $obj->haber : "",
        );
        $pdf->RowBorderLR($body, $lineHeight);
    }
    $pdf->line($pdf->GetX(), $pdf->GetY(), 19.5, $pdf->GetY());

    $pdf->SetAligns(array("","R","R","R"));
    $body = array(
        "",
        "Totales: ",
        number_format($debe,2),
        number_format($haber,2),
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    $pdf->line($pdf->GetX(), $pdf->GetY(), 19.5, $pdf->GetY());
    $pdf->SetFont("","");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight, "Concepto: ".$txtCon);
    $pdf->Ln($lineHeight*3);
    $pdf->MultiAlignCell($lineWidth/1.2, $lineHeight, "Banco: ".$rech["ban_nom"]);
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/1.2, $lineHeight, "Cuenta: ".$rech["ban_ctab"]."  Cheque: ".$numNum);

    $pdf->Ln($lineHeight*2);
    $pdf->Cell($lineWidth/6);
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "Hecho por", "T", 0, "C");
    $pdf->Cell($lineWidth/6);
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, "Revisado por", "T", 0, "C");
    $pdf->Output(utf8_decode("Cheque ").$selBanCod."-".$numNum.".pdf","I");
}

?>