<?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"]);

$selClasi=$_GET["selClasi"];
$dtFec=$_GET["dtFec"];
$crTmone=$_GET["crTmone"];
$crTrepo=$_GET["crTrepo"];
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, $dtFec, $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("SALDOS DE ANTIGUEDAD POR DOCUMENTO CUENTAS POR COBRAR AL ".strftime("%d/%m/%Y", strtotime($dtFec))),0,1,"L");
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("CIFRAS EXPRESADAS EN ").$tipomon,0,1,"L");

        $this->Ln();
        $this->SetFont("","",7);
        $header = array("SERIE","NUMERO","FEC/DOC","FEC/VENCE","AL DIA","1-30 DIAS","31-60 DIAS","61-90 DIAS","MAS DE 90","TOTAL");
        $this->SetFont("","B");
        $this->SetWidths(array(2,1.6,1.5,1.8,2,2,2,2,2,2));
        $this->SetAligns(array("C","C","C","C","C","C","C","C","C","C"));
        $this->Row($header, $lineHeight);

        $this->SetFont("");
        $this->SetAligns(array("L","L","C","C","R","R","R","R","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 ($selClasi=='000') {
    $sDoctoscc = "SELECT *, TRIM(dc_clien) AS dc_clien, TRIM(dc_codp) AS dc_codp, TRIM(dc_nit) AS dc_nit, (dc_tot-(dc_abo+dc_ret+dc_reti)) AS dc_saldod, TRIM(cl.cli_clas) AS dc_clas, TRIM(cla.cla_nom) AS cla_nom FROM doctoscc dc 
        LEFT JOIN cliencc cl ON dc.dc_emp=cl.cli_emp and dc.dc_codp=cl.cli_cod 
        LEFT JOIN clasic cla ON cl.cli_clas=cla.cla_cod
        WHERE dc_emp='$empCod' and dc_fec<='$dtFec' ORDER BY cli_clas,cli_nom";

    $sAnticpc="SELECT * FROM anticpc ant
               LEFT JOIN cliencc cl ON ant.ant_emp=cl.cli_emp and ant.ant_codp=cl.cli_cod 
               LEFT JOIN clasic cla ON cl.cli_clas=cla.cla_cod
               WHERE ant_emp='$empCod' and ant_fec<='$dtFec' and ant_tot>ant_totapli ORDER BY cli_clas,cli_nom";

    function sAbonosc($codp,$tipod,$seried,$numd) { 
        global $empCod, $dtFec;
        return "SELECT * FROM movicc mc WHERE mov_emp='$empCod' and mov_fec>'$dtFec' and mov_codp='$codp' and mov_tipoc='$tipod' and mov_seriec='$seried' and mov_numc='$numd'";
    }    

} else {
    $sDoctoscc = "SELECT *, TRIM(dc_clien) AS dc_clien, TRIM(dc_codp) AS dc_codp, TRIM(dc_nit) AS dc_nit, (dc_tot-(dc_abo+dc_ret+dc_reti)) AS dc_saldod, TRIM(cl.cli_clas) AS dc_clas, TRIM(cla.cla_nom) AS cla_nom FROM doctoscc dc 
        LEFT JOIN cliencc cl ON dc.dc_emp=cl.cli_emp and dc.dc_codp=cl.cli_cod 
        LEFT JOIN clasic cla ON cl.cli_clas=cla.cla_cod
        WHERE dc_emp='$empCod' AND dc_fec<='$dtFec' and cli_clas='$selClasi' ORDER BY cli_clas,cli_nom";

    $sAnticpc="SELECT * FROM anticpc ant
               LEFT JOIN cliencc cl ON ant.ant_emp=cl.cli_emp and ant.ant_codp=cl.cli_cod 
               LEFT JOIN clasic cla ON cl.cli_clas=cla.cla_cod
               WHERE ant_emp='$empCod' and ant_fec<='$dtFec' and cli_clas='$selClasi' and ant_tot>ant_totapli ORDER BY cli_clas,cli_nom";

    function sAbonosc($codp,$tipod,$seried,$numd) { 
        global $empCod, $dtFec;
        return "SELECT * FROM movicc mc WHERE mov_emp='$empCod' and mov_fec>'$dtFec' and mov_codp='$codp' and mov_tipoc='$tipod' and mov_seriec='$seried' and mov_numc='$numd'";
    }
}
# EJECUCION DE QUERYS
$db = new dbExec();
$lineHeight=0.35;
$select["doctoscc"] = $db->query($sDoctoscc);
$select["anticpc"] = $db->query($sAnticpc);

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

$ti_clasi="XXXZZZ999";
$subcodal_dia=0;
$subcod1_30=0;
$subcod31_60=0;
$subcod61_90=0;
$subcodmas_90=0;
$subcodtot=0;
$subal_dia=0;
$sub1_30=0;
$sub31_60=0;
$sub61_90=0;
$submas_90=0;
$subtot=0;
$total_dia=0;
$tot1_30=0;
$tot31_60=0;
$tot61_90=0;
$totmas_90=0;
$tottot=0;
$t_lini=1;
$codp="";
$ti_codp="XXZZZ999";
$nomp="";
$clas="";
$clasnom="";

$arrayProv = array();

while ($rec = $db->fetchAssoc($select["doctoscc"])) {
    $codp=$rec["dc_codp"];
    $clas=TRIM($rec["cli_clas"]);
    $tipod=$rec["dc_tipo"];
    $seried=$rec["dc_serie"];
    $numd=$rec["dc_num"];
    $fec=$rec["dc_fec"];
    $diasc=$rec["cli_dcre"];
    $fecp=strtotime('+'.$diasc.' day', strtotime($rec["dc_fec"]));
    $fecp=date('d-m-Y', $fecp);
    $tcam=$rec["dc_tcam"];
    $saldod=$rec["dc_saldod"];
    if ($saldod>0) {
        if ($crTmone == 'L' && $tcam>1) {
            $saldod=round($saldod*$tcam,2);
        }
        if ($crTmone == 'E' && $tcam == 1) {
            $saldod=round($saldod/$empTcam,2);
        }
    }

    $sumabo=0;
    $select["movicc"] = $db->query(sAbonosc($codp,$tipod,$seried,$numd));
    //$fetchAssoc["movic"] = $db->sFetchAssoc(sAbonos($codp));
    while ($recabo = $db->fetchAssoc($select["movicc"])) {
        $vabo=$recabo["mov_abo"];
        $tcamabo=$recabo["mov_tcam"];
        if ($vabo>0) {
            if ($crTmone == 'L' && $tcamabo>1) {
                $vabo=round($vabo*$tcamabo,2);
            }
            if ($crTmone == 'E' && $tcamabo == 1) {
                $vabo=round($vabo/$empTcam,2);
            }
            $sumabo += $vabo;
        }
    }
    
    $saldod+=$sumabo;
    if ($saldod == 0) {
       continue;
    }

    if ($t_lini == 1) {
        $codp=$rec["dc_codp"];
        $nomp=$rec["cli_nom"];
        $clas=TRIM($rec["cli_clas"]);
        $clasnom=$rec["cla_nom"];
        //$t_lini=2;
    }
    if ($ti_codp!=$rec["dc_codp"]) {
        if ($t_lini > 1) {
            $subcodtot=($subcodal_dia+$subcod1_30+$subcod31_60+$subcod61_90+$subcodmas_90);
            $pdf->ln();
            $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
            $pdf->Cell(.04,.4,"          SUBTOTAL ESTE CLIENTE",0,0,"L");
            $body = array(
                "",
                "",
                "",
                "",
                number_format($subcodal_dia, 2, '.', ','),
                number_format($subcod1_30, 2, '.', ','),
                number_format($subcod31_60, 2, '.', ','),
                number_format($subcod61_90, 2, '.', ','),
                number_format($subcodmas_90, 2, '.', ','),
                number_format($subcodtot, 2, '.', ','),
            ); 
            $pdf->RowBorderHidden($body, $lineHeight);
            $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
        }             
        $subcodal_dia=0;
        $subcod1_30=0;
        $subcod31_60=0;
        $subcod61_90=0;
        $subcodmas_90=0;
        $codp=$rec["dc_codp"];
        $ti_codp=$rec["dc_codp"];
        $nomp=$rec["cli_nom"];
        if (TRIM($ti_clasi)!=TRIM($rec["cli_clas"])) {
            if ($t_lini > 1) {
              $subtot=($subal_dia+$sub1_30+$sub31_60+$sub61_90+$submas_90);
              $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
              $pdf->Cell(.04,.4,"          SUBTOTAL ESTE GRUPO",0,0,"L");
              $body = array(
                    "",
                    "",
                    "",
                    "",
                    number_format($subal_dia, 2, '.', ','),
                    number_format($sub1_30, 2, '.', ','),
                    number_format($sub31_60, 2, '.', ','),
                    number_format($sub61_90, 2, '.', ','),
                    number_format($submas_90, 2, '.', ','),
                    number_format($subtot, 2, '.', ','),
                ); 
                $pdf->RowBorderHidden($body, $lineHeight);
                $subal_dia=0;
                $sub1_30=0;
                $sub31_60=0;
                $sub61_90=0;
                $submas_90=0;
                $subtot=0;
                $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
           }
           $clas=TRIM($rec["cli_clas"]);
           $clasnom=$rec["cla_nom"];
           $pdf->SetFont("Arial","BU",9);
           $pdf->Cell($lineWidth,.7,$clasnom,0,1,"L");
           $ti_clasi=$rec["cli_clas"];
           //$pdf->ln(.4);
        }
        $pdf->SetFont("Arial","B",8);
        $pdf->Cell($lineWidth,.7,$codp."-".$nomp,0,1,"L");
        $pdf->SetFont("","",7);
        $t_lini=2;
    } 
    // Para saber cuantos dias
    $diff=$func->dateDiff($dtFec, $fecp);
    $dias=($diff->invert == 1) ? '-'.$diff->days : $diff->days;
    $AL_DIA=0;
    $D1_30=0;
    $D31_60=0;
    $D61_90=0;
    $DMAS_90=0;
    if ($dias<1) {
      $AL_DIA=$saldod;
    }
    if ($dias>0 && $dias<31) {
      $D1_30=$saldod;
    }
    if ($dias>30 && $dias<61) {
      $D31_60=$saldod;
    }
    if ($dias>60 && $dias<91) {
      $D61_90=$saldod;
    }
    if ($dias>90) {
      $DMAS_90=$saldod;
    }

    $subcodal_dia += $AL_DIA;
    $subcod1_30 += $D1_30;
    $subcod31_60 += $D31_60;
    $subcod61_90 += $D61_90;
    $subcodmas_90 += $DMAS_90;

    $subal_dia += $AL_DIA;
    $sub1_30 += $D1_30;
    $sub31_60 += $D31_60;
    $sub61_90 += $D61_90;
    $submas_90 += $DMAS_90;
    $TOTALL = ($AL_DIA+$D1_30+$D31_60+$D61_90+$DMAS_90);

    $total_dia += $AL_DIA;
    $tot1_30 += $D1_30;
    $tot31_60 += $D31_60;
    $tot61_90 += $D61_90;
    $totmas_90 += $DMAS_90;

    $body = array(
        $rec["dc_serie"],
        trim($rec["dc_num"]),
        strftime("%d/%m/%Y", strtotime($fec)),
        strftime("%d/%m/%Y", strtotime($fecp)),
        (round($AL_DIA, 2) != 0) ? number_format($AL_DIA, 2, '.', ',') : "",        
        (round($D1_30, 2) != 0) ? number_format($D1_30, 2, '.', ',') : "",        
        (round($D31_60, 2) != 0) ? number_format($D31_60, 2, '.', ',') : "",        
        (round($D61_90, 2) != 0) ? number_format($D61_90, 2, '.', ',') : "",        
        (round($DMAS_90, 2) != 0) ? number_format($DMAS_90, 2, '.', ',') : "",        
        (round($TOTALL, 2) != 0) ? number_format($TOTALL, 2, '.', ',') : "",        
    ); 
    $pdf->RowBorderHidden($body, $lineHeight);
}

$subcodtot=($subcodal_dia+$subcod1_30+$subcod31_60+$subcod61_90+$subcodmas_90);
$pdf->ln();
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
$pdf->Cell(.04,.4,"          SUBTOTAL ESTE CLIENTE",0,0,"L");
$body = array(
    "",
    "",
    "",
    "",
    number_format($subcodal_dia, 2, '.', ','),
    number_format($subcod1_30, 2, '.', ','),
    number_format($subcod31_60, 2, '.', ','),
    number_format($subcod61_90, 2, '.', ','),
    number_format($subcodmas_90, 2, '.', ','),
    number_format($subcodtot, 2, '.', ','),
); 
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());

$subtot=($subal_dia+$sub1_30+$sub31_60+$sub61_90+$submas_90);
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
$pdf->Cell(.04,.4,"          SUBTOTAL ESTE GRUPO",0,0,"L");
$body = array(
    "",
    "",
    "",
    "",
    number_format($subal_dia, 2, '.', ','),
    number_format($sub1_30, 2, '.', ','),
    number_format($sub31_60, 2, '.', ','),
    number_format($sub61_90, 2, '.', ','),
    number_format($submas_90, 2, '.', ','),
    number_format($subtot, 2, '.', ','),
); 
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());

$sipaso="N";

// Solo si se necesita incluir anticipos
if ($crTrepo == 'A') {
    $select["anticpc"] = $db->query($sAnticpc);
    $ti_clasi="XXXZZZ999";
    $subcodal_dia=0;
    $subcod1_30=0;
    $subcod31_60=0;
    $subcod61_90=0;
    $subcodmas_90=0;
    $subcodtot=0;
    $subal_dia=0;
    $sub1_30=0;
    $sub31_60=0;
    $sub61_90=0;
    $submas_90=0;
    $subtot=0;
    $t_lini=1;
    $codp="";
    $ti_codp="XXZZZ999";
    $nomp="";
    $clas="";
    $clasnom="";
    while ($recanti = $db->fetchAssoc($select["anticpc"])) {
        $codp=$recanti["ant_codp"];
        $clas="ZZZ999";
        $tipod="ANTICIPO";
        $seried="ANTICIPO";
        $numd=$recanti["ant_num"];
        $fec=$recanti["ant_fec"];
        $fecp=$recanti["ant_fec"];
        $tcam=$recanti["ant_tcam"];
        $saldod=$recanti["ant_tot"]-$recanti["ant_totapli"];
        if ($saldod>0) {
            if ($crTmone == 'L' && $tcam>1) {
                $saldod=round($saldod*$tcam,2);
            }
            if ($crTmone == 'E' && $tcam == 1) {
                $saldod=round($saldod/$empTcam,2);
            }
        }
        if ($saldod == 0) {
           continue;
        }
        if ($t_lini == 1) {
            $codp=$recanti["ant_codp"];
            $nomp=$rec["cli_nom"];
            $clas="ZZZ999";
            $clasnom="ANTICIPOS";
            //$t_lini=2;
        }
        if ($ti_codp!=$recanti["ant_codp"]) {
            if ($t_lini > 1) {
                $subcodtot=($subcodal_dia+$subcod1_30+$subcod31_60+$subcod61_90+$subcodmas_90);
                $pdf->ln();
                $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
                $pdf->Cell(.04,.4,"          SUBTOTAL ESTE CLIENTE",0,0,"L");
                $body = array(
                    "",
                    "",
                    "",
                    "",
                    number_format($subcodal_dia, 2, '.', ','),
                    number_format($subcod1_30, 2, '.', ','),
                    number_format($subcod31_60, 2, '.', ','),
                    number_format($subcod61_90, 2, '.', ','),
                    number_format($subcodmas_90, 2, '.', ','),
                    number_format($subcodtot, 2, '.', ','),
                ); 
                $pdf->RowBorderHidden($body, $lineHeight);
                $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
            }             
            $subcodal_dia=0;
            $subcod1_30=0;
            $subcod31_60=0;
            $subcod61_90=0;
            $subcodmas_90=0;
            $codp=$recanti["ant_codp"];
            $ti_codp=$recanti["ant_codp"];
            $nomp=$recanti["cli_nom"];
            if ($ti_clasi!=$rec["cli_clas"]) {
                if ($t_lini > 1) {
                  $subtot=($subal_dia+$sub1_30+$sub31_60+$sub61_90+$submas_90);
                  $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
                  $pdf->Cell(.04,.4,"          SUBTOTAL ESTE GRUPO",0,0,"L");
                  $body = array(
                        "",
                        "",
                        "",
                        "",
                        number_format($subal_dia, 2, '.', ','),
                        number_format($sub1_30, 2, '.', ','),
                        number_format($sub31_60, 2, '.', ','),
                        number_format($sub61_90, 2, '.', ','),
                        number_format($submas_90, 2, '.', ','),
                        number_format($subtot, 2, '.', ','),
                    ); 
                    $pdf->RowBorderHidden($body, $lineHeight);
                    $subal_dia=0;
                    $sub1_30=0;
                    $sub31_60=0;
                    $sub61_90=0;
                    $submas_90=0;
                    $subtot=0;
                    $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
               }
               $clas="ZZZ999";
               $clasnom="ANTICIPOS";
               $pdf->SetFont("Arial","BU",9);
               $pdf->Cell($lineWidth,.7,$clas."-".$clasnom,0,1,"L");
               $ti_clasi=$rec["cli_clas"];
               //$pdf->ln(.4);
            }
            $pdf->SetFont("Arial","B",8);
            $pdf->Cell($lineWidth,.7,$codp."-".$nomp,0,1,"L");
            $pdf->SetFont("","",7);
            $t_lini=2;
        } 
        $vanti=$recanti["ant_tot"]-$recanti["ant_totapli"];
        $vantiapli=$recanti["ant_totapli"];
        $fecanti=$recanti["ant_fec"];
        $tcamanti=$recanti["ant_tcam"];
        if ($vanti>0) {
            if ($crTmone == 'L' && $tcamanti>1) {
                $vanti=round(($vanti)*$tcamanti,2);
            }
            if ($crTmone == 'E' && $tcamanti == 1) {
                $vanti=round(($vanti)/$empTcam,2);
            }
            $diff=$func->dateDiff($dtFec, $fecanti);
            $dias=($diff->invert == 1) ? '-'.$diff->days : $diff->days;
            $AL_DIA=0;
            $D1_30=0;
            $D31_60=0;
            $D61_90=0;
            $DMAS_90=0;
            if ($dias<1) {
              $total_dia=$total_dia-$vanti;
              $subal_dia=$subal_dia-$vanti;
              $subcodal_dia=$subcodal_dia-$vanti;
              $AL_DIA=$vanti*-1;
            }
            if ($dias>0 && $dias<31) {
              $tot1_30=$tot1_30-$vanti;
              $sub1_30=$sub1_30-$vanti;
              $subcod1_30=$subcod1_30-$vanti;
              $D1_30=$vanti*-1;
            }
            if ($dias>30 && $dias<61) {
              $tot31_60=$tot31_60-$vanti;
              $sub31_60=$sub31_60-$vanti;
              $subcod31_60=$subcod31_60-$vanti;
              $D31_60=$vanti*-1;
            }
            if ($dias>60 && $dias<91) {
              $tot61_90=$tot61_90-$vanti;
              $sub61_90=$sub61_90-$vanti;
              $subcod61_90=$subcod61_90-$vanti;
              $D61_90=$vanti*-1;
            }
            if ($dias>90) {
              $totmas_90=$totmas_90-$vanti;
              $submas_90=$submas_90-$vanti;
              $subcodmas_90=$subcodmas_90-$vanti;
              $DMAS_90=$vanti*-1;
            }
        }
        $TOTALL = ($AL_DIA+$D1_30+$D31_60+$D61_90+$DMAS_90);
        if ($AL_DIA==0 && $D1_30==0 && $D31_60==0 && $D61_90==0 && $DMAS_90==0) {
            continue;
        }    
        $body = array(
            "ANTICIPO",
            trim($recanti["ant_num"]),
            strftime("%d/%m/%Y", strtotime($recanti["ant_fec"])),
            strftime("%d/%m/%Y", strtotime($recanti["ant_fec"])),
            (round($AL_DIA, 2) != 0) ? number_format($AL_DIA, 2, '.', ',') : "",        
            (round($D1_30, 2) != 0) ? number_format($D1_30, 2, '.', ',') : "",        
            (round($D31_60, 2) != 0) ? number_format($D31_60, 2, '.', ',') : "",        
            (round($D61_90, 2) != 0) ? number_format($D61_90, 2, '.', ',') : "",        
            (round($DMAS_90, 2) != 0) ? number_format($DMAS_90, 2, '.', ',') : "",        
            (round($TOTALL, 2) != 0) ? number_format($TOTALL, 2, '.', ',') : "",        
        ); 
        $pdf->RowBorderHidden($body, $lineHeight);
        $sipaso="S";
    }
}
if ($sipaso=='S') {

    $subcodtot=($subcodal_dia+$subcod1_30+$subcod31_60+$subcod61_90+$subcodmas_90);
    $pdf->ln();
    $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
    $pdf->Cell(.04,.4,"          SUBTOTAL ESTE CLIENTE",0,0,"L");
    $body = array(
        "",
        "",
        "",
        "",
        number_format($subcodal_dia, 2, '.', ','),
        number_format($subcod1_30, 2, '.', ','),
        number_format($subcod31_60, 2, '.', ','),
        number_format($subcod61_90, 2, '.', ','),
        number_format($subcodmas_90, 2, '.', ','),
        number_format($subcodtot, 2, '.', ','),
    ); 
    $pdf->RowBorderHidden($body, $lineHeight);
    $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());

    $subtot=($subal_dia+$sub1_30+$sub31_60+$sub61_90+$submas_90);
    $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
    $pdf->Cell(.04,.4,"          SUBTOTAL ESTE GRUPO",0,0,"L");
    $body = array(
        "",
        "",
        "",
        "",
        number_format($subal_dia, 2, '.', ','),
        number_format($sub1_30, 2, '.', ','),
        number_format($sub31_60, 2, '.', ','),
        number_format($sub61_90, 2, '.', ','),
        number_format($submas_90, 2, '.', ','),
        number_format($subtot, 2, '.', ','),
    ); 
    $pdf->RowBorderHidden($body, $lineHeight);
    $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
}
$tottot=($total_dia+$tot1_30+$tot31_60+$tot61_90+$totmas_90);
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
$pdf->Cell(.04,.4,"          TOTAL GENERAL",0,0,"L");
$body = array(
    "",
    "",
    "",
    "",
    number_format($total_dia, 2, '.', ','),
    number_format($tot1_30, 2, '.', ','),
    number_format($tot31_60, 2, '.', ','),
    number_format($tot61_90, 2, '.', ','),
    number_format($totmas_90, 2, '.', ','),
    number_format($tottot, 2, '.', ','),
); 
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());


$pdf->Output(utf8_decode("Saldos Antiguedad Detallado cxc" ).".pdf","I");

$db->close();

?>