<?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"]);
$empDir=utf8_decode($_SESSION["empDir"]);
$empTelpat=utf8_decode($_SESSION["empTelpat"]);
$empNit=utf8_decode($_SESSION["empNit"]);
$empEmail=utf8_decode($_SESSION["empEmail"]);
$usuNom=utf8_decode($_SESSION["usuNom"]);
$usuCod=utf8_decode($_SESSION["usuCod"]);

$numNum=$_GET["numNum"];
$selTipoCod="012";
$selTabo=$_GET["selTabo"];
$selTaboNom=$_GET["selTaboNom"];
$totLetras=TRIM($_GET["totLetras"]);
$jsonDocp=json_decode($_GET['tblDocp']);


# QUERYS

$sApliant = "SELECT *,
               TRIM(ant.ant_con) AS ant_con,
               TRIM(cl.cli_nom) AS cli_nom
             FROM apliantcpc ant
             LEFT JOIN cliencc cl ON cl.cli_emp=ant.ant_emp AND cl.cli_cod=ant.ant_codp 
             WHERE ant_emp='$empCod' AND ant_tipoabo='$selTabo' AND ant_num='$numNum'";

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


# EJECUCION DE QUERYS
$db = new dbExec();
$fa["apliantcpc"] = $db->sFetchAssoc($sApliant);
$select["apliantcpc_det"] = $db->query($sApliant_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() {
    }
    
    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="Factura(s): ";
    $formapago="Forma de Pago: ";
    if ($fa["apliantcpc"]["ant_tot"]>0) {
        $formapago=$formapago." Aplica Anticipo: ".number_format($fa["apliantcpc"]["ant_tot"], 2);
    }
    $formapago=$formapago.".";
    if ($fa["apliantcpc"]["ant_tmone"]=="E") {
       $totLetras=$totLetras." DOLARES.";
    } else {
       $totLetras=$totLetras." QUETZALES.";
   }

    $tlin=1;
    $tcancela="S";
    $salant=0;
    $totabo=0;
    // Para ver el detalle de los documentos pagados
    while ($recd = $db->fetchAssoc($select["apliantcpc_det"])) {
        $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) {
            $concep=$concep.TRIM($numerod);
        } else {
                $concep=$concep.", ".TRIM($numerod);
        }   
        $tlin=2;
    }
    $salact=($salant-$totabo);
    $pdf->SetFont("","",9);
    $img = '../img/LOGO_AMTOURS.jpg';
    $pdf->Image($img,1.5,.5,4.3);
    $pdf->SetAutoPageBreak(true, .1);
    $pdf->SetMargins(1, .5, .5);
    $pdf->SetLineWidth(.05);
    $pdf->SetY(.8);
    $pdf->SetTextColor(0,0,0);
    $pdf->SetFont("","",9);
    $pdf->MultiAlignCell($lineWidth/3, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/3, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight4, TRIM($selTaboNom)." Nro.",0,0,"L");
    $numeroConCeros = str_pad($numNum, 7, "0", STR_PAD_LEFT);
    $pdf->SetFont("","",9);
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight4, trim($numeroConCeros),0,1,"L");

    $pdf->MultiAlignCell($lineWidth/3, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/3, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight4, "Emitido por",0,0,"L");
    $pdf->SetFont("","",9);
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight4, TRIM($usuCod),0,1,"L");
    $pdf->Ln(.8);
    $pdf->SetFont("","",7);
    $pdf->MultiAlignCell($lineWidth/30, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/1.55, $lineHeight4, TRIM($empDir),0,0,"L");
    //$pdf->Rect(12.3, 2.15, 7.7, .9);
    $pdf->RoundedRect(12.3, 2.15, 7.7, .9, .2, '1234', 'D');
    $pdf->SetFont("","",9);
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight4, "FECHA: ",0,0,"L");
    $pdf->SetFont("","B",11);
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight4, strftime("%d/%m/%Y", strtotime($fa["apliantcpc"]["ant_fec"])),0,1,"L");
    $pdf->SetFont("","",7);
    $pdf->MultiAlignCell($lineWidth/30, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/2.2, $lineHeight4, "Tel: ".TRIM($empTelpat),0,1,"C");
    $pdf->MultiAlignCell($lineWidth/30, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/2.2, $lineHeight4, TRIM($empEmail),0,1,"C");
    $pdf->Ln(.8);
//    $pdf->Rect(1.5, 3.8, 18.5, 3.7);
    $pdf->RoundedRect(1.5, 3.8, 18.5, 6.7, .2, '1234', 'D');
    $pdf->SetFont("","",9);
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, "RECIBIMOS DE: ");
    $pdf->MultiAlignCell($lineWidth/1.2, $lineHeight, utf8_decode(trim($fa["apliantcpc"]["cli_nom"])),0,1,"L");
    $pdf->SetY(5.3);
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/5, $lineHeight, "LA CANTIDAD DE: ");
    $pdf->MultiAlignCell($lineWidth/1.2, $lineHeight, utf8_decode($totLetras),0,1,"L");

    $pdf->SetY(6.3);
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/2.3, $lineHeight-.3, "POR EL CONCEPTO INDICADO COMO SIGUE: ",0,1,"L");
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/1.7, $lineHeight, "Se aplico anticipo No. ".$fa["apliantcpc"]["ant_numanti"]." a ".$concep,0,1,"L");
//    $pdf->Rect(1.5, 7.6, 18.5, 1.3);
/*    
    $pdf->RoundedRect(1.5, 7.6, 18.5, 1.3, .2, '1234', 'D');
    $pdf->SetY(7.67);
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight-.2, utf8_decode($formapago),0,1,"L"); // Segundo concepto
//    $pdf->Rect(1.5, 9, 18.5, 2.3);
    $pdf->RoundedRect(1.5, 9, 18.5, 2.3, .2, '1234', 'D');
    $pdf->SetY(9.2);
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight-.3, utf8_decode("Cuenta por Cobrar:"),0,1,"L");
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight, utf8_decode($fa["apliantcpc"]["ant_con"]),0,1,"L");
*/
    $pdf->Image($img,2.2,11.7,3.1);
    $pdf->line(2.3, 13.1, 8.2, 13.1);
//    $pdf->Rect(14.5, 11.4, 5.5, .9);
    $pdf->RoundedRect(14.5, 11.4, 5.5, .9, .2, '1234', 'D');
    $pdf->SetY(11.4);
    $pdf->SetFont("","B",11);
    if ($fa["apliantcpc"]["ant_tmone"]=='L') {
       $pdf->MultiAlignCell($lineWidth/1.25, $lineHeight, "");
       $pdf->MultiAlignCell($lineWidth/3.5, $lineHeight8, "GTQ     ".number_format($fa["apliantcpc"]["ant_tot"], 2),0,1,"R");
    } else {
       $pdf->MultiAlignCell($lineWidth/1.25, $lineHeight, "");
       $pdf->MultiAlignCell($lineWidth/3.5, $lineHeight8, "USD     ".number_format($fa["apliantcpc"]["ant_tot"], 2),0,1,"R");
    }  
    $pdf->SetFont("","",9);
    $pdf->Ln(1);
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, utf8_decode("Firma del"),0,1,"C");

    $pdf->line(1.2, 14, 20.1, 14);

    $pdf->Image($img,1.5,14.5,4.3);
    $pdf->SetY(14.8);
    $pdf->SetTextColor(0,0,0);
    $pdf->SetFont("","",9);
    $pdf->MultiAlignCell($lineWidth/3, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/3, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight4, TRIM($selTaboNom)."  Nro.",0,0,"L");
    $numeroConCeros = str_pad($numNum, 7, "0", STR_PAD_LEFT);
    $pdf->SetFont("","",9);
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight4, trim($numeroConCeros),0,1,"L");



    $pdf->MultiAlignCell($lineWidth/3, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/3, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight4, "Emitido por",0,0,"L");
    $pdf->SetFont("","",9);
    $pdf->MultiAlignCell($lineWidth/4, $lineHeight4, TRIM($usuCod),0,1,"L");
    $pdf->Ln(.8);
    $pdf->SetFont("","",7);
    $pdf->MultiAlignCell($lineWidth/30, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/1.55, $lineHeight4, TRIM($empDir),0,0,"L");
    //$pdf->Rect(12.3, 2.15, 7.7, .9);
    $pdf->RoundedRect(12.3, 16.15, 7.7, .9, .2, '1234', 'D');
    $pdf->SetFont("","",9);
    $pdf->MultiAlignCell($lineWidth/8, $lineHeight4, "FECHA: ",0,0,"L");
    $pdf->SetFont("","B",11);
    $pdf->MultiAlignCell($lineWidth/7, $lineHeight4, strftime("%d/%m/%Y", strtotime($fa["apliantcpc"]["ant_fec"])),0,1,"L");
    $pdf->SetFont("","",7);
    $pdf->MultiAlignCell($lineWidth/30, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/2.2, $lineHeight4, "Tel: ".TRIM($empTelpat),0,1,"C");
    $pdf->MultiAlignCell($lineWidth/30, $lineHeight4, "",0,0,"L");
    $pdf->MultiAlignCell($lineWidth/2.2, $lineHeight4, TRIM($empEmail),0,1,"C");
    $pdf->Ln(.8);
//    $pdf->Rect(1.5, 3.8, 18.5, 3.7);
    $pdf->RoundedRect(1.5, 17.8, 18.5, 6.7, .2, '1234', 'D');
    $pdf->SetFont("","",9);
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/6, $lineHeight, "RECIBIMOS DE: ");
    $pdf->MultiAlignCell($lineWidth/1.2, $lineHeight, utf8_decode(trim($fa["apliantcpc"]["cli_nom"])),0,1,"L");
    $pdf->SetY(19.3);
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/5, $lineHeight, "LA CANTIDAD DE: ");
    $pdf->MultiAlignCell($lineWidth/1.2, $lineHeight, utf8_decode($totLetras),0,1,"L");

    $pdf->SetY(20.3);
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/2.3, $lineHeight-.3, "POR EL CONCEPTO INDICADO COMO SIGUE: ",0,1,"L");
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/1.7, $lineHeight, "Se aplico anticipo No. ".$fa["apliantcpc"]["ant_numanti"]." a ".$concep,0,1,"L");
//    $pdf->Rect(1.5, 7.6, 18.5, 1.3);
/*    
    $pdf->RoundedRect(1.5, 21.6, 18.5, 1.3, .2, '1234', 'D');
    $pdf->SetY(21.67);
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight-.2, utf8_decode($formapago),0,1,"L");   // Segundo concepto
//    $pdf->Rect(1.5, 9, 18.5, 2.3);
    $pdf->RoundedRect(1.5, 23, 18.5, 2.3, .2, '1234', 'D');
    $pdf->SetY(23.2);
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight-.3, utf8_decode("Cuenta por Cobrar:"),0,1,"L");
    $pdf->MultiAlignCell($lineWidth/15, $lineHeight, "");
    $pdf->MultiAlignCell($lineWidth/1, $lineHeight, utf8_decode($fa["apliantcpc"]["ant_con"]),0,1,"L");
*/
    $pdf->Image($img,2.2,25.7,3.1);
    $pdf->line(2.3, 27.1, 8.2, 27.1);
//    $pdf->Rect(14.5, 11.4, 5.5, .9);
    $pdf->RoundedRect(14.5, 25.4, 5.5, .9, .2, '1234', 'D');
    $pdf->SetY(25.4);
    $pdf->SetFont("","B",11);
    if ($fa["apliantcpc"]["ant_tmone"]=='L') {
       $pdf->MultiAlignCell($lineWidth/1.25, $lineHeight, "");
       $pdf->MultiAlignCell($lineWidth/3.5, $lineHeight8, "GTQ     ".number_format($fa["apliantcpc"]["ant_tot"], 2),0,1,"R");
    } else {
       $pdf->MultiAlignCell($lineWidth/1.25, $lineHeight, "");
       $pdf->MultiAlignCell($lineWidth/3.5, $lineHeight8, "USD     ".number_format($fa["apliantcpc"]["ant_tot"], 2),0,1,"R");
    }  
    $pdf->SetFont("","",9);
    $pdf->Ln(1);
    $pdf->MultiAlignCell($lineWidth/2, $lineHeight, utf8_decode("Firma del"),0,1,"C");

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

$db->close();

?>