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

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

$selTipoa=$_GET["selTipoa"];
$dtFeci=$_GET["dtFeci"];
$dtFecf=$_GET["dtFecf"];
$crTmone=$_GET["crTmone"];
if ($crTmone == 'L') {
    $tipomon=$empMoneda;
} else {
    $tipomon='DOLARES';
}   

//$func = new functions();

$lineWidth=17;
$lineHeight=0.5;

class generatePDF extends PDF_MC_Table {
    function Header() {
        global $empNom, $lineHeight, $lineWidth, $dtFeci, $dtFecf, $tipomon;
        $this->SetMargins(1.5, 1, 2);
        $this->Cell(0, 0, "", 0, 1);

        $this->SetFont("Arial","B",12);
        $this->Cell($lineWidth,.7,$empNom,0,0,"L");

        $this->SetFont("Arial","I",8);
        $this->SetY(.7);
        $this->Cell(0,1,utf8_decode("PÁGINA ").$this->PageNo(),0,1,"R");

        $this->SetFont("","",9);
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("LISTADO DE OTROS DOCUMENTOS DE ABONO CxC DEL ".strftime("%d/%m/%Y", strtotime($dtFeci))." AL ".strftime("%d/%m/%Y", strtotime($dtFecf))),0,1,"L");
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("DOCUMENTOS REGISTRADOS EN ").$tipomon,0,1,"L");

        $this->Ln();
        $this->SetFont("","",8);
        $this->SetFont("","B");
        $this->SetWidths(array(1.7,2.3,1.8,2,6,7,1.6,2.2));
        $this->SetAligns(array("C","C","C","C","C","C","C","C"));

        $this->SetFont("");
        $this->SetAligns(array("L","L","C","C","L","L","R","R"));
    }
    
    function Footer() {
        // Posición: a 1,5 cm del final
        //$this->SetY(-2.5);
        //$this->SetFont("Arial","I",8);
        //$this->Cell(0,1,"PAGINA ".$this->PageNo(),0,0,"C");
    }
}

# QUERYS
if ($crTmone=='L') {
    if ($selTipoa=='000') {
        $sAbonosc = "SELECT *, TRIM(cl.cli_nom) AS cli_nom, TRIM(abo.abo_codp) AS abo_codp, TRIM(abo.abo_con) AS abo_con, TRIM(ta.ta_nom) AS ta_nom
           FROM abo_detc abd LEFT JOIN cliencc cl ON abd.abd_emp=cl.cli_emp and abd.abd_codp=cl.cli_cod 
           LEFT JOIN abonosc abo ON abd.abd_emp=abo.abo_emp and abd.abd_tipo=abo.abo_tipo and abd.abd_num=abo.abo_num 
           LEFT JOIN tipoaboc ta ON  abd.abd_tipo=ta.ta_cod 
           WHERE abd_emp='$empCod' and abd_fec>='$dtFeci' and abd_fec<='$dtFecf' and abo_tcam=1 and abd_abop>0 ORDER BY abd_fec,abd_tipo,abd_num";
    } else {
        $sAbonosc = "SELECT *, TRIM(cl.cli_nom) AS cli_nom, TRIM(abo.abo_codp) AS abo_codp, TRIM(abo.abo_con) AS abo_con, TRIM(ta.ta_nom) AS ta_nom
           FROM abo_detc abd LEFT JOIN cliencc cl ON abd.abd_emp=cl.cli_emp and abd.abd_codp=cl.cli_cod 
           LEFT JOIN abonosc abo ON abd.abd_emp=abo.abo_emp and abd.abd_tipo=abo.abo_tipo and abd.abd_num=abo.abo_num 
           LEFT JOIN tipoaboc ta ON  abd.abd_tipo=ta.ta_cod 
           WHERE abd_emp='$empCod' and abd_tipo='$selTipoa' and abd_fec>='$dtFeci' and abd_fec<='$dtFecf' and abo_tcam=1 and abd_abop>0 ORDER BY abd_fec,abd_tipo,abd_num";
    }           
} else {
    if ($selTipoa=='000') {
        $sAbonosc = "SELECT *, TRIM(cl.cli_nom) AS cli_nom, TRIM(abo.abo_codp) AS abo_codp, TRIM(abo.abo_con) AS abo_con, TRIM(ta.ta_nom) AS ta_nom
           FROM abo_detc abd LEFT JOIN cliencc cl ON abd.abd_emp=cl.cli_emp and abd.abd_codp=cl.cli_cod 
           LEFT JOIN abonosc abo ON abd.abd_emp=abo.abo_emp and abd.abd_tipo=abo.abo_tipo and abd.abd_num=abo.abo_num 
           LEFT JOIN tipoaboc ta ON  abd.abd_tipo=ta.ta_cod 
           WHERE abd_emp='$empCod' and abd_fec>='$dtFeci' and abd_fec<='$dtFecf' and abo_tcam>1 and abd_abop>0 ORDER BY abd_fec,abd_tipo,abd_num";
    } else {
        $sAbonosc = "SELECT *, TRIM(cl.cli_nom) AS cli_nom, TRIM(abo.abo_codp) AS abo_codp, TRIM(abo.abo_con) AS abo_con, TRIM(ta.ta_nom) AS ta_nom
           FROM abo_detc abd LEFT JOIN cliencc cl ON abd.abd_emp=cl.cli_emp and abd.abd_codp=cl.cli_cod 
           LEFT JOIN abonosc abo ON abd.abd_emp=abo.abo_emp and abd.abd_tipo=abo.abo_tipo and abd.abd_num=abo.abo_num 
           LEFT JOIN tipoaboc ta ON  abd.abd_tipo=ta.ta_cod 
           WHERE abd_emp='$empCod' and abd_tipo='$selTipoa' and abd_fec>='$dtFeci' and abd_fec<='$dtFecf' and abo_tcam>1 and abd_abop>0 ORDER BY abd_fec,abd_tipo,abd_num";
    }           
}    
# EJECUCION DE QUERYS
$db = new dbExec();
$lineHeight=0.40;
$select["abo_detc"] = $db->query($sAbonosc);

# GENERAR PDF
$pdf = new generatePDF("P","cm", array(22, 28));
$pdf->AddPage();
$func = new functions();

$ti_tipo="XXXZZZ999";
$ti_num=9999;
$subtot=0;
$tottot=0;
$t_lini=1;
$codp="";
$nomp="";
$clas="";
$clasnom="";

$arrayProv = array();

while ($rec = $db->fetchAssoc($select["abo_detc"])) {
    if ($t_lini == 1) {
        //$pdf->SetFont("","B",8);        
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "TIPO DE DOCUMENTO: ".$rec["ta_nom"]."         NUMERO:  ".$rec["abd_num"]."         FECHA:  ".strftime("%d/%m/%Y", strtotime($rec["abd_fec"])),0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("CLIENTE:   ".$rec["abd_codp"]." - ".$rec["cli_nom"]),0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "TASA DE CAMBIO : ".$rec["abo_tcam"],0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("CONCEPTO: ".$rec["abo_con"]),0,1,"L");
        $pdf->SetFont("","",8);        
        $header = array("SERIE CARGO","NUMERO CARGO","FECHA/CARGO","ABONO APLICADO");
        $pdf->SetWidths(array(3,3,3,3));
        $pdf->SetAligns(array("C","C","C","C"));
        $pdf->Row($header, $lineHeight); 
        $pdf->SetFont("");
        $pdf->SetAligns(array("L","L","C","R"));
    }
    if ($ti_tipo!=$rec["abd_tipo"]  || $ti_num!=$rec["abd_num"]) {
        if ($t_lini > 1) {
            $pdf->line($pdf->GetX()+0, $pdf->GetY(), 13.5, $pdf->GetY());
            $pdf->Cell(.04,.4,"          TOTAL ESTE DOCUMENTO",0,0,"L");
            $body = array(
                "",
                "",
                "",
                number_format($subtot, 2, '.', ','),
            ); 
            $pdf->RowBorderHidden($body, $lineHeight);
            $pdf->line($pdf->GetX()+0, $pdf->GetY(), 13.5, $pdf->GetY());
            $pdf->ln();
            $pdf->MultiAlignCell($lineWidth, $lineHeight, "TIPO DE DOCUMENTO: ".$rec["ta_nom"]."         NUMERO:  ".$rec["abd_num"]."         FECHA:  ".strftime("%d/%m/%Y", strtotime($rec["abd_fec"])),0,1,"L");
            $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("CLIENTE:   ".$rec["abd_codp"]." - ".$rec["cli_nom"]),0,1,"L");
            $pdf->MultiAlignCell($lineWidth, $lineHeight, "TASA DE CAMBIO : ".$rec["abo_tcam"],0,1,"L");
            $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("CONCEPTO: ".$rec["abo_con"]),0,1,"L");
            $pdf->SetFont("","",8);        
            $header = array("SERIE CARGO","NUMERO CARGO","FECHA/CARGO","ABONO APLICADO");
            $pdf->SetWidths(array(3,3,3,3));
            $pdf->SetAligns(array("C","C","C","C"));
            $pdf->Row($header, $lineHeight); 
            $pdf->SetFont("");
            $pdf->SetAligns(array("L","L","C","R"));
        }             
        $subtot=0;
        $ti_tipo=$rec["abd_tipo"];
        $ti_num=$rec["abd_num"];

        $t_lini=2;
    } 
    $subtot += $rec["abd_abop"];
    $tottot += $rec["abd_abop"];

    $body = array(
        $rec["abd_seriec"],
        trim($rec["abd_numc"]),
        strftime("%d/%m/%Y", strtotime($rec["abd_feccd"])),
        (round($rec["abd_abop"], 2) != 0) ? number_format($rec["abd_abop"], 2, '.', ',') : "",        
    ); 
    $pdf->RowBorderHidden($body, $lineHeight);
    if (($pdf->GetY()+$lineHeight) > 25) {
        $pdf->AddPage();
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "TIPO DE DOCUMENTO: ".$rec["ta_nom"]."         NUMERO:  ".$rec["abd_num"]."         FECHA:  ".strftime("%d/%m/%Y", strtotime($rec["abd_fec"])),0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("CLIENTE:   ".$rec["abd_codp"]." - ".$rec["cli_nom"]),0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "TASA DE CAMBIO : ".$rec["abo_tcam"],0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("CONCEPTO: ".$rec["abo_con"]),0,1,"L");
        $pdf->SetFont("","",8);        
        $header = array("SERIE CARGO","NUMERO CARGO","FECHA/CARGO","ABONO APLICADO");
        $pdf->SetWidths(array(3,3,3,3));
        $pdf->SetAligns(array("C","C","C","C"));
        $pdf->Row($header, $lineHeight); 
        $pdf->SetFont("");
        $pdf->SetAligns(array("L","L","C","R"));
    }    
}

$pdf->line($pdf->GetX()+0, $pdf->GetY(), 13.5, $pdf->GetY());
$pdf->Cell(.04,.4,"          TOTAL ESTE DOCUMENTO",0,0,"L");
$body = array(
    "",
    "",
    "",
    number_format($subtot, 2, '.', ','),
); 
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 13.5, $pdf->GetY());
$pdf->ln();

$pdf->line($pdf->GetX()+0, $pdf->GetY(), 13.5, $pdf->GetY());
$pdf->Cell(.04,.4,"          TOTAL GENERAL",0,0,"L");
$body = array(
    "",
    "",
    "",
    number_format($tottot, 2, '.', ','),
); 
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 13.5, $pdf->GetY());

$pdf->Output(utf8_decode("Lista Abono CxC" ).".pdf","I");

$db->close();

?>