<?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="009";
$selTabo=$_GET["selTabo"];
$selTaboNom=$_GET["selTaboNom"];


# QUERYS

$sAbonos = "SELECT *,
               TRIM(ab.abo_con) AS abo_con,
               TRIM(pc.pro_nom) AS pro_nom 
             FROM abonos ab
             LEFT JOIN provcp pc ON pc.pro_emp=ab.abo_emp AND pc.pro_cod=ab.abo_codp 
             WHERE abo_emp='$empCod' AND abo_tipo='$selTabo' AND abo_num='$numNum'";

$sAbo_det = "SELECT * FROM abo_det WHERE abd_emp='$empCod' AND abd_tipo='$selTabo' AND abd_num='$numNum'";

$sPoli_det = "SELECT * FROM poli_det WHERE pold_emp='$empCod' AND pold_ban=0 AND pold_tipo='$selTipoCod' AND pold_num='$numNum' AND pold_tipoi='$selTabo'";

# EJECUCION DE QUERYS
$db = new dbExec();
$fa["abonos"] = $db->sFetchAssoc($sAbonos);
$select["abo_det"] = $db->query($sAbo_det);
$select["poli_det"] = $db->query($sPoli_det);


$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, $selTabo, $selTaboNom;

        $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() {
        $this->SetY(-2.5);
    }
}


# 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/5, $lineHeight, utf8_decode("Tipo de Documento: "));
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight, utf8_decode(trim($selTaboNom)));
    //$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($fa["abonos"]["abo_fec"])));
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, "Proveedor: ");
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, utf8_decode((trim($fa["abonos"]["abo_codp"]))."-".trim($fa["abonos"]["pro_nom"])));
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/10.5, $lineHeight, "Bienes: ");
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, $fa["abonos"]["abo_bien"]);
    $pdf->MultiAlignCell($lineWidth/8.5, $lineHeight, "Servicios: ");
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, $fa["abonos"]["abo_serv"]);
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, utf8_decode("Pequeño Cont.: "));
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight, $fa["abonos"]["abo_peque"]);
    $pdf->MultiAlignCell($lineWidth/10, $lineHeight, "T.C.: ");
    $pdf->MultiAlignCell($lineWidth/5, $lineHeight, $fa["abonos"]["abo_tcam"]);
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/5.5, $lineHeight, "Tipo de Moneda: ");
    if ($fa["abonos"]["abo_tmone"] == "L") {
       $pdf->MultiAlignCell($lineWidth/5, $lineHeight, "LOCAL");
    } else {
       $pdf->MultiAlignCell($lineWidth/5, $lineHeight, "EXTRANJERA");
    }
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight, "Concepto: ");
    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight, $fa["abonos"]["abo_con"]);
    $pdf->Ln($lineHeight*2);
    $lineHeight=0.5;
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, "DOCUMENTOS QUE ABONA: ");
    $pdf->Ln();
    $header = array("Serie",utf8_decode("Número"),"Fecha","T/Cambio","Saldo","Abono");
    $pdf->SetFont("","B");
    $pdf->SetWidths(array(3,3,2.5,2.5,3,3));
    $pdf->SetAligns(array("C","C","C","C","C","C"));
    $pdf->Row($header, $lineHeight);

    $pdf->SetFont("");
    $pdf->SetAligns(array("L","L","C","R","R","R"));
    $tsal = 0;
    $tabo = 0;
    while ($rec = $db->fetchAssoc($select['abo_det'])) {
        $tsal += $rec["abd_sal"];
        $tabo += $rec["abd_abop"];
        $body = array(
            $rec["abd_tipoc"],
            $rec["abd_numc"],
            strftime("%d/%m/%Y", strtotime($rec["abd_feccd"])),
            $rec["abd_tcam"],
            ($rec["abd_sal"] != 0) ? $rec["abd_sal"] : "",
            ($rec["abd_abop"] != 0) ? $rec["abd_abop"] : "",
        );
        $pdf->RowBorderLR($body, $lineHeight);
    }
    $pdf->line($pdf->GetX(), $pdf->GetY(), 19, $pdf->GetY());

    $pdf->SetAligns(array("","","","R","R","R"));
    $body = array(
        "",
        "",
        "",
        "Total abonos: ",
        number_format($tsal,2),
        number_format($tabo,2),
    );
    $pdf->RowBorderHidden($body, $lineHeight);

    $pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, "PARTIDA CONTABLE: ");
    $pdf->Ln();
    $header = array("Cuenta","Nombre de Cuenta","Debe","Haber");
    $pdf->SetFont("","B");
    $pdf->SetWidths(array(3,8.5,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 ($rec = $db->fetchAssoc($select['poli_det'])) {
        $debe += $rec["pold_debe"];
        $haber += $rec["pold_haber"];
        $body = array(
            $rec["pold_cta"],
            $rec["pold_ctan"],
            (round($rec["pold_debe"], 2) != 0) ? $rec["pold_debe"] :"",
            (round($rec["pold_haber"], 2) != 0) ? $rec["pold_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->Output(utf8_decode("Documento ").$selTabo."-".$numNum.".pdf","I");

$db->close();

?>