<?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="012";
$selTabo=$_GET["selTabo"];
$selTaboNom=$_GET["selTaboNom"];
$totLetras=$_GET["totLetras"];
$jsonDocp=json_decode($_GET['tblDocp']);


# QUERYS

$sAbonosc = "SELECT *,
               TRIM(ab.abo_nom) AS abo_nom,
               TRIM(ab.abo_con) AS abo_con,
               TRIM(cl.cli_nom) AS pro_nom 
             FROM abonosc ab
             LEFT JOIN cliencc cl ON cl.cli_emp=ab.abo_emp AND cl.cli_cod=ab.abo_codp 
             WHERE abo_emp='$empCod' AND abo_tipo='$selTabo' AND abo_num='$numNum'";

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


# EJECUCION DE QUERYS
$db = new dbExec();
$fa["abonosc"] = $db->sFetchAssoc($sAbonosc);
$select["abo_detc"] = $db->query($sAbo_detc);

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


# GENERAR PDF
    $pdf = new generatePDF("P","cm", "Letter");
    $pdf->AddPage();
    $pdf->SetMargins(1, 0, 1.5);
    $pdf->SetFont("Arial","B",20);

    $lineWidth=17;
    $lineHeight=0.6;
    $lineHeight2=0.2;
    $lineHeight3=0.3;
    $lineHeight4=0.4;
    $lineHeight5=0.45;
    $lineHeight8=0.8;

    $concep="";
    $tlin=1;
    $tcancela="S";
    $salant=0;
    $totabo=0;
    // Para ver el detalle de los documentos pagados
    while ($recd = $db->fetchAssoc($select["abo_detc"])) {
        $seried = trim($recd["abd_seriec"]);
        $numerod = $recd["abd_numc"];
        $saldod = $recd["abd_sal"];
        $abonod = $recd["abd_abop"];
        $tcamd = $recd["abd_tcam"];
        $salant+=$saldod;
        $totabo+=$abonod;
        if ($saldod!=$abonod) {
            $tcandela="N";
        }
        if ($tlin==1) {
            if ($seried!="") {
                $concep=$concep.$seried."-".TRIM($numerod);
            } else {
                $concep=$concep.TRIM($numerod);
            }
        } else {
            if ($seried!="") {
                $concep=$concep.", ".$seried."-".TRIM($numerod);
            } else {
                $concep=$concep.", ".TRIM($numerod);
            }
        }   
        $tlin=2;
    }
    $salact=($salant-$totabo);
    $pdf->SetFont("","",9);
    //$pdf->SetX(4.5);
    //$pdf->SetY(1);
    //$pdf->Ln();
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight4, "");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight4, utf8_decode(trim($selTaboNom))."-".trim($numNum));
    $pdf->Ln(.7);
    //$pdf->SetX(4.5);

    $pdf->MultiAlignCell($lineWidth/1.3, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/9, $lineHeight, date("d", strtotime($fa["abonosc"]["abo_fec"])),0,0,"C");
    $pdf->MultiAlignCell($lineWidth/8.5, $lineHeight, date("m", strtotime($fa["abonosc"]["abo_fec"])),0,0,"C");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, date("Y", strtotime($fa["abonosc"]["abo_fec"])),0,1,"C");
    $pdf->Ln(.2);
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight, utf8_decode((trim($fa["abonosc"]["abo_codp"]))."-".trim($fa["abonosc"]["abo_nom"])),0,1,"L");
    $pdf->MultiAlignCell($lineWidth/9.5, $lineHeight8, "");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight8, utf8_decode((trim($fa["abonosc"]["cli_dir"]))),0,1,"L");
    $pdf->MultiAlignCell($lineWidth/12, $lineHeight8, "");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight8, utf8_decode((trim($totLetras))),0,1,"L");
    $pdf->Ln(.4);
    if ($tcancela!="S") {
        $pdf->MultiAlignCell($lineWidth/13, $lineHeight5, "");
        $pdf->MultiAlignCell($lineWidth/12, $lineHeight5, "X",0,1,"L");
    } else {
        $pdf->MultiAlignCell($lineWidth/4.20, $lineHeight5, "");
        $pdf->MultiAlignCell($lineWidth/12, $lineHeight5, "X",0,1,"L");
    }        
    $pdf->Ln(.1);
    $pdf->MultiAlignCell($lineWidth/13, $lineHeight5, "");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight5, $concep,0,1,"L");
    $pdf->Ln(.8);
    $pdf->MultiAlignCell($lineWidth/10, $lineHeight5, "");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight5, number_format($salant,2),0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight5, "");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight5, ($fa["abonosc"]["abo_efec"]>0) ? $fa["abonosc"]["abo_efec"] : "",0,1,"R");

    $pdf->MultiAlignCell($lineWidth/10, $lineHeight5, "");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight5, number_format($totabo, 2),0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight5, "");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight5, ($fa["abonosc"]["abo_nch"]>0) ? $fa["abonosc"]["abo_nch"] : "",0,1,"R");

    $pdf->MultiAlignCell($lineWidth/10, $lineHeight5, "");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight5, number_format($salact, 2),0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight5, "");
    //$pdf->Ln(.1);
    if ($fa["abonosc"]["abo_tcam"]>1.000000) {
        $pdf->MultiAlignCell($lineWidth/8, $lineHeight8, $fa["abonosc"]["abo_banch"],0,0,"R");
        $pdf->MultiAlignCell($lineWidth/12, $lineHeight8, "");
        $pdf->MultiAlignCell($lineWidth/8, $lineHeight8, number_format($totabo, 2),0,1,"R");
    } else {
        $pdf->MultiAlignCell($lineWidth/8, $lineHeight8, $fa["abonosc"]["abo_banch"],0,1,"R");
    }
    $pdf->MultiAlignCell($lineWidth/10, $lineHeight3, "");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight3, "",0,0,"R");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight3, "");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight3, ($fa["abonosc"]["abo_vtarj"]>0) ? number_format($fa["abonosc"]["abo_vtarj"],2) : "",0,1,"R");
    $pdf->Ln(.1);
    $pdf->MultiAlignCell($lineWidth/10, $lineHeight2, "");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight2, "",0,0,"R");
    $pdf->MultiAlignCell($lineWidth/2.5, $lineHeight2, "");
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight2, $fa["abonosc"]["abo_tcam"],0,1,"R");

    $pdf->Ln($lineHeight*2);
    $pdf->Output(utf8_decode("Documento ").$selTabo."-".$numNum.".pdf","I");

$db->close();

?>