<?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 CUENTAS POR PAGAR 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("CODIGO","NOMBRE PROVEEDOR","AL DIA","1-30 DIAS","31-60 DIAS","61-90 DIAS","MAS DE 90","TOTAL");
        $this->SetFont("","B");
        $this->SetWidths(array(2,5,2,2,2,2,2,2));
        $this->SetAligns(array("C","C","C","C","C","C","C","C"));
        $this->Row($header, $lineHeight);

        $this->SetFont("");
        $this->SetAligns(array("L","L","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') {
    $sDoctosc = "SELECT *, TRIM(dc_prov) AS dc_prov, TRIM(dc_codp) AS dc_codp, TRIM(dc_nit) AS dc_nit, (dc_tot-(dc_abo+dc_ret+dc_reti)) AS dc_saldod, TRIM(pv.pro_clas) AS dc_clas, TRIM(cla.cla_nom) AS cla_nom FROM doctosc dc 
        LEFT JOIN provcp pv ON dc.dc_emp=pv.pro_emp and dc.dc_codp=pv.pro_cod 
        LEFT JOIN clasi cla ON pv.pro_clas=cla.cla_cod
        WHERE dc_emp='$empCod' and dc_fec<='$dtFec' ORDER BY pro_clas,pro_nom";

    $sAnticp="SELECT * FROM anticp ant
        LEFT JOIN provcp pv ON ant.ant_emp=pv.pro_emp and ant.ant_codp=pv.pro_cod 
        LEFT JOIN clasi cla ON pv.pro_clas=cla.cla_cod
        WHERE ant_emp='$empCod' and ant_fec<='$dtFec' and ant_stat='P' ORDER BY pro_clas,pro_nom";

    function sAbonos($codp,$seried,$numd) { 
        global $empCod, $dtFec;
        return "SELECT * FROM movic mc WHERE mov_emp='$empCod' and mov_fec>'$dtFec' and mov_codp='$codp' and mov_tipoc='$seried' and mov_numc='$numd'";
    }    
} else {
    $sDoctosc = "SELECT *, TRIM(dc_prov) AS dc_prov, TRIM(dc_codp) AS dc_codp, TRIM(dc_nit) AS dc_nit, (dc_tot-(dc_abo+dc_ret+dc_reti)) AS dc_saldod, TRIM(pv.pro_clas) AS dc_clas, TRIM(cla.cla_nom) AS cla_nom FROM doctosc dc 
        LEFT JOIN provcp pv ON dc.dc_emp=pv.pro_emp and dc.dc_codp=pv.pro_cod 
        LEFT JOIN clasi cla ON pv.pro_clas=cla.cla_cod
        WHERE dc_emp='$empCod' and dc_fec<='$dtFec' and pro_clas='$selClasi' ORDER BY pro_clas,pro_nom";

    $sAnticp="SELECT * FROM anticp ant
        LEFT JOIN provcp pv ON ant.ant_emp=pv.pro_emp and ant.ant_codp=pv.pro_cod 
        LEFT JOIN clasi cla ON pv.pro_clas=cla.cla_cod
        WHERE ant_emp='$empCod' and ant_fec<='$dtFec' and pro_clas='$selClasi' and ant_stat='P' ORDER BY pro_clas,pro_nom";

    function sAbonos($codp,$seried,$numd) { 
        global $empCod, $dtFec;
        return "SELECT * FROM movic mc WHERE mov_emp='$empCod' and mov_fec>'$dtFec' and mov_codp='$codp' and mov_tipoc='$seried' and mov_numc='$numd'";
    }
}    
# EJECUCION DE QUERYS
$db = new dbExec();
$lineHeight=0.35;
$select["doctosc"] = $db->query($sDoctosc);
$select["anticp"] = $db->query($sAnticp);

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

$ti_clasi="XXX";
$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="";
$nomp="";
$clas="";
$clasnom="";
$arrayProv = array();
while ($rec = $db->fetchAssoc($select["doctosc"])) {
    if ($t_lini == 1) {
        $codp=$rec["dc_codp"];
        $ti_codp=$rec["dc_codp"];
        $nomp=$rec["pro_nom"];
        $clas=$rec["pro_clas"];
        $clasnom=$rec["cla_nom"];
        $t_lini=2;
    }   
    if ($ti_codp!=$rec["dc_codp"]) {
        array_push($arrayProv, array('codp'=>$codp, 'nomp'=>$nomp, 'clas'=>$clas, 'clasnom'=>$clasnom, 'dal_dia'=>$subal_dia, 'd1_30'=>$sub1_30, 'd31_60'=>$sub31_60, 'd61_90'=>$sub61_90, 'dmas_90'=>$submas_90)); 
        $subal_dia=0;
        $sub1_30=0;
        $sub31_60=0;
        $sub61_90=0;
        $submas_90=0;
        $codp=$rec["dc_codp"];
        $ti_codp=$rec["dc_codp"];
        $nomp=$rec["pro_nom"];
        $clas=$rec["pro_clas"];
        $clasnom=$rec["cla_nom"];
    }   
    $seried=$rec["dc_serie"];
    $numd=$rec["dc_num"];
    $fecp=$rec["dc_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["movic"] = $db->query(sAbonos($codp,$seried,$numd));
    //$fetchAssoc["movic"] = $db->sFetchAssoc(sAbonos($codp));
    while ($recabo = $db->fetchAssoc($select["movic"])) {
        $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;
    // 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;
    }
    $subal_dia += $AL_DIA;
    $sub1_30 += $D1_30;
    $sub31_60 += $D31_60;
    $sub61_90 += $D61_90;
    $submas_90 += $DMAS_90;
}

array_push($arrayProv, array('codp'=>$codp, 'nomp'=>$nomp, 'clas'=>$clas, 'clasnom'=>$clasnom, 'dal_dia'=>$subal_dia, 'd1_30'=>$sub1_30, 'd31_60'=>$sub31_60, 'd61_90'=>$sub61_90, 'dmas_90'=>$submas_90)); 

// Para definir si incluye anticipos
if ($crTrepo == 'A') {
    $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="";
    $nomp="";
    $clas="";
    $clasnom="";

    while ($reca = $db->fetchAssoc($select["anticp"])) {
        if ($t_lini == 1) {
            $codp=$reca["ant_codp"];
            $ti_codp=$reca["ant_codp"];
            $nomp=$reca["pro_nom"];
            $clas="ZZZ999";
            $clasnom="ANTICIPOS";
            $t_lini=2;
        }   
        if ($ti_codp!=$reca["ant_codp"]) {
            array_push($arrayProv, array('codp'=>$codp, 'nomp'=>$nomp, 'clas'=>$clas, 'clasnom'=>$clasnom, 'dal_dia'=>$subal_dia, 'd1_30'=>$sub1_30, 'd31_60'=>$sub31_60, 'd61_90'=>$sub61_90, 'dmas_90'=>$submas_90)); 
            $subal_dia=0;
            $sub1_30=0;
            $sub31_60=0;
            $sub61_90=0;
            $submas_90=0;
            $codp=$reca["ant_codp"];
            $ti_codp=$reca["ant_codp"];
            $nomp=$reca["pro_nom"];
            $clas="ZZZ999";
            $clasnom="ANTICIPOS";
        }   
        $tipod="ANT";
        $seried="ANTICIPO";
        $numd=$reca["ant_num"];
        $fecp=$reca["ant_fec"];
        $tcam=$reca["ant_tcam"];
        $saldod=$reca["ant_tot"];
        if ($saldod>0) {
            if ($crTmone == 'L' && $tcam>1) {
                $saldod=round($saldod*$tcam,2);
            }
            if ($crTmone == 'E' && $tcam == 1) {
                $saldod=round($saldod/$empTcam,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<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;
        }
        $subal_dia -= $AL_DIA;
        $sub1_30 -= $D1_30;
        $sub31_60 -= $D31_60;
        $sub61_90 -= $D61_90;
        $submas_90 -= $DMAS_90;
    }
    array_push($arrayProv, array('codp'=>$codp, 'nomp'=>$nomp, 'clas'=>$clas, 'clasnom'=>$clasnom, 'dal_dia'=>$subal_dia, 'd1_30'=>$sub1_30, 'd31_60'=>$sub31_60, 'd61_90'=>$sub61_90, 'dmas_90'=>$submas_90)); 
}
//

$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;
$ti_clasi="XXX";

foreach ($arrayProv as $recp) {
//while ($recp = $arrayProv) {
    if (TRIM($ti_clasi)!=TRIM($recp["clas"])) {
        if ($t_lini > 1) {
          $pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
          $body = array(
                "",
                "      SUBTOTAL DE ESTE GRUPO ",
                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());
        }   
       $pdf->Cell($lineWidth,.7,$recp["clasnom"],0,0,"L");
       $ti_clasi=$recp["clas"];
       $pdf->ln();
       $t_lini=2;
    }
    $codp=$recp["codp"];
    $nomp=$recp["nomp"];

    $subal_dia += $recp["dal_dia"];
    $sub1_30 += $recp["d1_30"];
    $sub31_60 += $recp["d31_60"];
    $sub61_90 += $recp["d61_90"];
    $submas_90 += $recp["dmas_90"];
    $TOTALL = ($recp["dal_dia"]+$recp["d1_30"]+$recp["d31_60"]+$recp["d61_90"]+$recp["dmas_90"]);
    $subtot += $TOTALL;
    $total_dia += $recp["dal_dia"];
    $tot1_30 += $recp["d1_30"];
    $tot31_60 += $recp["d31_60"];
    $tot61_90 += $recp["d61_90"];
    $totmas_90 += $recp["dmas_90"];
    $tottot += $TOTALL;
    if ($TOTALL==0) {
       continue;
    }
    $body = array(
        $recp["codp"],
        trim($recp["nomp"]),
        (round($recp["dal_dia"], 2) != 0) ? number_format($recp["dal_dia"], 2, '.', ',') : "",        
        (round($recp["d1_30"], 2) != 0) ? number_format($recp["d1_30"], 2, '.', ',') : "",        
        (round($recp["d31_60"], 2) != 0) ? number_format($recp["d31_60"], 2, '.', ',') : "",        
        (round($recp["d61_90"], 2) != 0) ? number_format($recp["d61_90"], 2, '.', ',') : "",        
        (round($recp["dmas_90"], 2) != 0) ? number_format($recp["dmas_90"], 2, '.', ',') : "",        
        (round($TOTALL, 2) != 0) ? number_format($TOTALL, 2, '.', ',') : "",        
    ); 
    $pdf->RowBorderHidden($body, $lineHeight);
}
$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
$body = array(
    "",
    "      SUBTOTAL DE ESTE GRUPO ",
    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());

$pdf->line($pdf->GetX()+0, $pdf->GetY(), 20.5, $pdf->GetY());
$body = array(
    "",
    "      TOTAL GENERAL ",
    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").".pdf","I");

$db->close();

?>