<?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 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(3,3,3,3));
        $this->SetAligns(array("C","C","C","C"));
        $this->SetFont("");
        $this->SetAligns(array("L","L","C","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') {
        $sAbonos = "SELECT *, TRIM(pv.pro_nom) AS pro_nom, TRIM(abo.abo_codp) AS abo_codp, TRIM(abo.abo_con) AS abo_con, TRIM(ta.ta_nom) AS ta_nom
           FROM movic mc
           LEFT JOIN provcp pv ON mc.mov_emp=pv.pro_emp and mc.mov_codp=pv.pro_cod 
           LEFT JOIN abonos abo ON mc.mov_emp=abo.abo_emp and mc.mov_tipo=abo.abo_tipo and mc.mov_num=abo.abo_num 
           LEFT JOIN doctosc dc ON mc.mov_emp=dc.dc_emp and mc.mov_tipoc=dc.dc_serie and mc.mov_numc=dc.dc_num 
           LEFT JOIN tipoabo ta ON  mc.mov_tipo=ta.ta_cod 
           WHERE mov_emp='$empCod' and mov_fec>='$dtFeci' and mov_fec<='$dtFecf' and mov_tcam=1 and mov_abo>0 ORDER BY mov_fec,mov_tipo,mov_num";
    } else {
        $sAbonos = "SELECT *, TRIM(pv.pro_nom) AS pro_nom, TRIM(abo.abo_codp) AS abo_codp, TRIM(abo.abo_con) AS abo_con, TRIM(ta.ta_nom) AS ta_nom
           FROM movic mc
           LEFT JOIN provcp pv ON mc.mov_emp=pv.pro_emp and mc.mov_codp=pv.pro_cod 
           LEFT JOIN abonos abo ON mc.mov_emp=abo.abo_emp and mc.mov_tipo=abo.abo_tipo and mc.mov_num=abo.abo_num 
           LEFT JOIN doctosc dc ON mc.mov_emp=dc.dc_emp and mc.mov_tipoc=dc.dc_serie and mc.mov_numc=dc.dc_num 
           LEFT JOIN tipoabo ta ON  mc.mov_tipo=ta.ta_cod 
           WHERE mov_emp='$empCod' and mov_tipo='$selTipoa' and mov_fec>='$dtFeci' and mov_fec<='$dtFecf' and mov_tcam=1 and mov_abo>0 ORDER BY mov_fec,mov_tipo,mov_num";
    }           
} else {
    if ($selTipoa=='000') {
        $sAbonos = "SELECT *, TRIM(pv.pro_nom) AS pro_nom, TRIM(abo.abo_codp) AS abo_codp, TRIM(abo.abo_con) AS abo_con, TRIM(ta.ta_nom) AS ta_nom
           FROM movic mc
           LEFT JOIN provcp pv ON mc.mov_emp=pv.pro_emp and mc.mov_codp=pv.pro_cod 
           LEFT JOIN abonos abo ON mc.mov_emp=abo.abo_emp and mc.mov_tipo=abo.abo_tipo and mc.mov_num=abo.abo_num 
           LEFT JOIN doctosc dc ON mc.mov_emp=dc.dc_emp and mc.mov_tipoc=dc.dc_serie and mc.mov_numc=dc.dc_num 
           LEFT JOIN tipoabo ta ON  mc.mov_tipo=ta.ta_cod 
           WHERE mov_emp='$empCod' and mov_fec>='$dtFeci' and mov_fec<='$dtFecf' and mov_tcam>1 and mov_abo>0 ORDER BY mov_fec,mov_tipo,mov_num";
    } else {
        $sAbonos = "SELECT *, TRIM(pv.pro_nom) AS pro_nom, TRIM(abo.abo_codp) AS abo_codp, TRIM(abo.abo_con) AS abo_con, TRIM(ta.ta_nom) AS ta_nom
           FROM movic mc
           LEFT JOIN provcp pv ON mc.mov_emp=pv.pro_emp and mc.mov_codp=pv.pro_cod 
           LEFT JOIN abonos abo ON mc.mov_emp=abo.abo_emp and mc.mov_tipo=abo.abo_tipo and mc.mov_num=abo.abo_num 
           LEFT JOIN doctosc dc ON mc.mov_emp=dc.dc_emp and mc.mov_tipoc=dc.dc_serie and mc.mov_numc=dc.dc_num 
           LEFT JOIN tipoabo ta ON  mc.mov_tipo=ta.ta_cod 
           WHERE mov_emp='$empCod' and mov_tipo='$selTipoa' and mov_fec>='$dtFeci' and mov_fec<='$dtFecf' and mov_tcam>1 and mov_abo>0 ORDER BY mov_fec,mov_tipo,mov_num";
    }           
}    
# EJECUCION DE QUERYS
$db = new dbExec();
$lineHeight=0.40;
$select["movic"] = $db->query($sAbonos);

# 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["movic"])) {
    if ($t_lini == 1) {
        //$pdf->SetFont("","B",8);        
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "TIPO DE DOCUMENTO: ".$rec["ta_nom"]."         NUMERO:  ".$rec["mov_num"]."         FECHA:  ".strftime("%d/%m/%Y", strtotime($rec["mov_fec"])),0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "PROVEEDOR:   ".$rec["mov_codp"]." - ".$rec["pro_nom"],0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "TASA DE CAMBIO : ".$rec["mov_tcam"],0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "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["mov_tipo"]  || $ti_num!=$rec["mov_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["mov_num"]."         FECHA:  ".strftime("%d/%m/%Y", strtotime($rec["mov_fec"])),0,1,"L");
            $pdf->MultiAlignCell($lineWidth, $lineHeight, "PROVEEDOR:   ".$rec["mov_codp"]." - ".$rec["pro_nom"],0,1,"L");
            $pdf->MultiAlignCell($lineWidth, $lineHeight, "TASA DE CAMBIO : ".$rec["mov_tcam"],0,1,"L");
            $pdf->MultiAlignCell($lineWidth, $lineHeight, "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["mov_tipo"];
        $ti_num=$rec["mov_num"];

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

    $body = array(
        $rec["mov_tipoc"],
        trim($rec["mov_numc"]),
        strftime("%d/%m/%Y", strtotime($rec["dc_fec"])),
        (round($rec["mov_abo"], 2) != 0) ? number_format($rec["mov_abo"], 2, '.', ',') : "",        
    ); 
    $pdf->RowBorderHidden($body, $lineHeight);
    if (($pdf->GetY()+$lineHeight) > 25) {
        $pdf->AddPage();
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "TIPO DE DOCUMENTO: ".$rec["ta_nom"]."         NUMERO:  ".$rec["mov_num"]."         FECHA:  ".strftime("%d/%m/%Y", strtotime($rec["mov_fec"])),0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "PROVEEDOR:   ".$rec["mov_codp"]." - ".$rec["pro_nom"],0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "TASA DE CAMBIO : ".$rec["mov_tcam"],0,1,"L");
        $pdf->MultiAlignCell($lineWidth, $lineHeight, "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" ).".pdf","I");

$db->close();

?>