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

$empCod=utf8_decode($_SESSION["empCod"]);
$empNom=utf8_decode($_SESSION["empNom"]);
$empNomc=utf8_decode($_SESSION["empNomc"]);
$pTcam=$_SESSION["pTcam"];
$empNit=utf8_decode($_SESSION['empNit']);

$selMes=$_GET["selMes"];
$selMesNom=$_GET["selMesNom"];
$numAno=$_GET["numAno"];
$selMesf=$_GET["selMesf"];
$selMesNomf=$_GET["selMesNomf"];
$numAnof=$_GET["numAnof"];
$numHoja=$_GET["numHoja"];
$cbFecsn=$_GET["cbFecsn"];
$cbNoFol=$_GET["cbNoFol"];
$Uldia=$_GET["Uldia"];
$Fecfin = (strval($numAnof)."-".strval($selMesf)."-".strval($Uldia));
$numDiai = 1;
$Fecini = (strval($numAno)."-".strval($selMes)."-".strval($numDiai));

$lineWidth=20;
$lineHeight=0.35;

class generatePDF extends PDF_MC_Table {
    function Header() {
        global $empNom, $empNomc, $empNit, $selMes, $selMesNom, $numAno, $selMesf, $selMesNomf, $numAnof, $Fecini, $Fecfin, $numHoja, $cbFecsn, $cbNoFol, $lineWidth, $lineHeight;
        $this->SetMargins(1, 1, 2);

        $func = new functions();
        $this->SetFont("Arial","",9);
        $this->Cell(0, 0, "", 0, 1);
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode($empNom),0,1,"L");
        //$this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode($empNomc),0,1,"C");

        if ($func->stringToBool($cbNoFol)) {
            $this->SetFont("Arial","I",8);
            $this->SetY(.7);
            $this->Cell(0,1, utf8_decode("Folio: ".((string)($this->PageNo())+$numHoja)),0,1,"R");
        }

        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("LIBRO DE COMPRAS Y SERVICIOS"),0,1,"L");
        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("Mes Inicial  ".trim($selMesNom)."  Año ".$numAno."  Mes Final  ".trim($selMesNomf)."  Año ".$numAnof."                    Nit: ".$empNit),0,1,"L");
        //$this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("Cifras Expresadas en ".$textoMoneda),0,1,"C");
        $this->Ln();

        $this->SetFont("","",7);
        $header = array("FECHA","SERIE","NUMERO","NIT", "PROVEEDOR", "COMBUST.",utf8_decode("PEQUEÑO CON."), "COMPRAS", "SERVICIOS", "IMPORTAC.", "IVA", "IMP/COMBUS. Y OTROS", "TOTAL");
        $this->SetWidths(array(1.5,2.2,2.2,2.2,3,1.8,1.8,1.6,1.8,1.8,1.8,1.8,2));
        $this->SetAligns(array("C","C","C","C","C","C","C","C","C","C","C","C","C"));
        $this->Row($header, $lineHeight);
        $this->SetAligns(array("C","L","L","L","L","R","R","R","R","R","R","R","R"));
    }
    
    function Footer() {
        global $cbFecsn;
        // Posición: a 1,5 cm del final
        $func = new functions();
        $this->SetY(-2);
        $this->SetFont("Arial","I",7);
        if ($func->stringToBool($cbFecsn)) {
            $this->Cell(0,1,strftime("%d/%m/%Y %T"),0,0,"L");
        }
    }
}
# QUERYS
$sIva_comp = "SELECT *,
                TRIM(ic.iva_serie) AS iva_serie,
                TRIM(ic.iva_nit) AS iva_nit,
                TRIM(ic.iva_prov) AS iva_prov
            FROM iva_comp ic 
            WHERE ic.iva_emp='$empCod' AND ic.iva_rfec>='$Fecini' AND ic.iva_rfec<='$Fecfin' AND ic.iva_afe='S' 
            ORDER BY ic.iva_emp,ic.iva_fec";

$sIvacResu = "SELECT *, 
            (SUM(iva_subb)+SUM(iva_subs)+SUM(iva_subco)) AS iva_svalor, 
            SUM(1) AS iva_sumdoc 
            FROM iva_comp ic WHERE ic.iva_emp='$empCod' AND ic.iva_rfec>='$Fecini' AND ic.iva_rfec<='$Fecfin' AND ic.iva_afe='S'  GROUP BY ic.iva_emp,ic.iva_nit ORDER BY iva_svalor DESC";

# EJECUCION DE QUERYS
$db = new dbExec();
# GENERAR PDF
$pdf = new generatePDF("L","cm", array(22, 28));
$pdf->AddPage();

$func = new functions();
$linea=1;
$cantdoc=0;
$select["iva_comp"] = $db->query($sIva_comp);
while ($rec = $db->fetchAssoc($select['iva_comp'])) {
    // TASA DE CAMBIO
    $cantdoc += 1;
    $tsubco += $rec["iva_subco"];
    $tsubpe += $rec["iva_peque"]+$rec["iva_peque2"];
    $tsubb = ($tsubb+$rec["iva_subb"]+$rec["iva_subme"]+$rec["iva_subve"]+$rec["iva_subna"]+$rec["iva_subac"]);
    $tsubs += $rec["iva_subs"];
    $tsubi = $tsubi+$rec["iva_subi"]+$rec["iva_subir"];
    $tiva += $rec["iva_iva"];
    $tdedu += ($rec["iva_dedu"]+$rec["iva_tasam"]);
    $ttot = $ttot+$rec["iva_tot"]+$rec["iva_dedu"];

    $rsubco += $rec["iva_subco"];
    $rsubb += $rec["iva_subb"];
    $rsubme += $rec["iva_subme"];
    $rsubve += $rec["iva_subve"];
    $rsubna += $rec["iva_subna"];
    $rsubac += $rec["iva_subac"];
    $rsubpe += $rec["iva_subpe"];
    $rsubs +=  $rec["iva_subs"];
    $rsubi +=  $rec["iva_subi"];
    $rsubir += $rec["iva_subir"];
    $riva += $rec["iva_iva"];
    $rdedu += $rec["iva_dedu"]+$rec["iva_tasam"];
    $rtot += $rec["iva_tot"];
    $rtott = $rtott+($rec["iva_tot"]+$rec["iva_dedu"]); 

    $body = array(
        strftime("%d/%m/%Y", strtotime($rec["iva_fec"])),
        $rec["iva_serie"],
        $rec["iva_num"],
        $rec["iva_nit"],
        $rec["iva_prov"],
        (round($rec["iva_subco"], 2) != 0) ? number_format($rec["iva_subco"], 2, '.', ',') : "",
        (round($rec["iva_peque"]+$rec["iva_peque2"], 2) != 0) ? number_format($rec["iva_peque"]+$rec["iva_peque2"], 2, '.', ',') : "",
        (round($rec["iva_subb"]+$rec["iva_subme"]+$rec["iva_subve"]+$rec["iva_subna"]+$rec["iva_subac"]+$rec["iva_peque"], 2) != 0) ? number_format($rec["iva_subb"]+$rec["iva_subme"]+$rec["iva_subve"]+$rec["iva_subna"]+$rec["iva_subac"], 2, '.', ',') : "",
        (round($rec["iva_subs"]+$rec["iva_peque2"], 2) != 0) ? number_format($rec["iva_subs"], 2, '.', ',') : "",
        (round($rec["iva_subi"]+$rec["iva_subir"], 2) != 0) ? number_format($rec["iva_subi"]+$rec["iva_subir"], 2, '.', ',') : "",
        (round($rec["iva_iva"], 2) != 0) ? number_format($rec["iva_iva"], 2, '.', ',') : "",
        (round($rec["iva_dedu"]+$rec["iva_tasam"], 2) != 0) ? number_format($rec["iva_dedu"]+$rec["iva_tasam"], 2, '.', ',') : "",
        number_format($rec["iva_tot"]+$rec["iva_dedu"], 2, '.', ','),
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    //  (round($totSalIni, 2) != 0) ? number_format($totSalIni, 2, '.', ',') : "",

    $linea=2;
    if (($pdf->GetY()+$lineHeight) > 19) {
        $pdf->line($pdf->GetX()+7, $pdf->GetY(), 26.5, $pdf->GetY());
        $body = array(
            "",
            "",
            "",
            "",
            "      VAN ................",
            number_format($tsubco, 2, '.', ','),
            number_format($tsubpe, 2, '.', ','),
            number_format($tsubb, 2, '.', ','),
            number_format($tsubs, 2, '.', ','),
            number_format($tsubi, 2, '.', ','),
            number_format($tiva, 2, '.', ','),
            number_format($tdedu, 2, '.', ','),
            number_format($ttot, 2, '.', ','),
        );
        $pdf->RowBorderHidden($body, $lineHeight);
        $pdf->line($pdf->GetX()+7, $pdf->GetY(), 26.5, $pdf->GetY());
        $pdf->AddPage();
        $pdf->line($pdf->GetX()+7, $pdf->GetY(), 26.5, $pdf->GetY());
        $body = array(
            "",
            "",
            "",
            "",
            "      VIENEN ............",
            number_format($tsubco, 2, '.', ','),
            number_format($tsubpe, 2, '.', ','),
            number_format($tsubb, 2, '.', ','),
            number_format($tsubs, 2, '.', ','),
            number_format($tsubi, 2, '.', ','),
            number_format($tiva, 2, '.', ','),
            number_format($tdedu, 2, '.', ','),
            number_format($ttot, 2, '.', ','),
        );
        $pdf->RowBorderHidden($body, $lineHeight);
        $pdf->line($pdf->GetX()+7, $pdf->GetY(), 26.5, $pdf->GetY());

    }
}

// TOTALES DEL MOVIMIENTO
//$pdf->Ln();
$pdf->line($pdf->GetX()+7, $pdf->GetY(), 26.5, $pdf->GetY());
$body = array(
    "",
    "",
    "",
    "",
    "TOTALES",
    number_format($tsubco, 2, '.', ','),
    number_format($tsubpe, 2, '.', ','),
    number_format($tsubb, 2, '.', ','),
    number_format($tsubs, 2, '.', ','),
    number_format($tsubi, 2, '.', ','),
    number_format($tiva, 2, '.', ','),
    number_format($tdedu, 2, '.', ','),
    number_format($ttot, 2, '.', ','),
);
$pdf->RowBorderHidden($body, $lineHeight);
$pdf->line($pdf->GetX()+7, $pdf->GetY(), 26.5, $pdf->GetY());

if (($pdf->GetY()+$lineHeight) > 16) {
        $pdf->AddPage();
}
// RESUMEN
$pdf->Ln();
$pdf->SetFont("","",7);
$header = array("RESUMEN:","","No. Documentos",$cantdoc);
$pdf->SetWidths(array(3,2,3,2));
$pdf->SetAligns(array("L","R","L","R"));
$pdf->Row($header, $lineHeight);
$body = array("COMPRAS",number_format($rsubb, 2, '.', ','),"SERVICIOS",number_format($rsubs, 2, '.', ','));
$pdf->Row($body, $lineHeight);
$body = array(utf8_decode("PEQUEÑO CON."),number_format($rsubpe, 2, '.', ','),"MEDICINA",number_format($rsubme, 2, '.', ','));
$pdf->Row($body, $lineHeight);
$body = array("VEHICULOS",number_format($rsubve, 2, '.', ','),"IMPORT. C.A.",number_format($rsubi, 2, '.', ','));
$pdf->Row($body, $lineHeight);
$body = array("EXENTO",number_format($rsubna, 2, '.', ','),"IMPORT. RESTO",number_format($rsubir, 2, '.', ','));
$pdf->Row($body, $lineHeight);
$body = array("COMBUSTIBLE",number_format($rsubco, 2, '.', ','),"IVA",number_format($riva, 2, '.', ','));
$pdf->Row($body, $lineHeight);
$body = array("ACTIVOS",number_format($rsubac, 2, '.', ','),"TOTAL",number_format($rtot, 2, '.', ','));
$pdf->Row($body, $lineHeight);
$body = array("IMP/COMBUS. Y OTROS",number_format($rdedu, 2, '.', ','),"TOTAL GENERAL",number_format($rtott, 2, '.', ','));
$pdf->Row($body, $lineHeight);

// RESUMEN DE LOS 10 PROVEEDORES
$lineHeight=0.65;
$pdf->AddPage();
$pdf->SetWidths(array(4,12,2.5,3.5));
$pdf->SetAligns(array("C","C","C","C"));
$pdf->Ln();
$pdf->SetFont("","B",9);
$titulo = array("","RESUMEN DE LOS 10 MAYORES PROVEEDORES","","");
$pdf->RowBorderHidden($titulo, $lineHeight);
$pdf->SetFont("","",9);
$header = array("NIT","NOMBRE DEL PROVEEDOR","No. DOCUMENTOS","VALOR AFECTO");
$pdf->Row($header, $lineHeight);
$pdf->SetAligns(array("L","L","R","R"));
$nlinea=1;
$totrp=0;
$select["iva_compr"] = $db->query($sIvacResu);
while ($recr = $db->fetchAssoc($select['iva_compr'])) {
    if ($recr["iva_svalor"] <= 0) {
        continue;
    }    
    
    if ($nlinea <= 10) {
        $totrp += $recr["iva_svalor"];
        $body = array(
            trim($recr["iva_nit"]),
            utf8_encode(trim(trim($recr["iva_prov"]))),
            $recr["iva_sumdoc"],
            number_format($recr["iva_svalor"], 2, '.', ','),
        );
        $pdf->Row($body, $lineHeight);
    }
    $nlinea += 1;
}
$body = array(
    "",
    "         TOTAL ..............",
    "",
    number_format($totrp, 2, '.', ','),
);
$pdf->Row($body, $lineHeight);

$db->close();
ob_end_clean();
ob_start();
$pdf->Output(utf8_decode("LIBRO DE COMPRAS").".pdf","I"); 

?>