<?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);
        $this->SetY(1.7);

        $func = new functions();
        $this->SetFont("Arial","",11);
        $this->Cell(0, 0, "", 0, 1);
        $this->Ln(.4);
        $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",11);
            $this->SetY(1.8);
            $this->Cell(0,1, utf8_decode("Folio: ".((string)($this->PageNo())+$numHoja)),0,1,"R");
        }
        $this->SetFont("Arial","",9);
        $this->SetY(2.5);

        $this->MultiAlignCell($lineWidth, $lineHeight, utf8_decode("LIBRO DE VENTAS Y SERVICIOS VARIOS MESES"),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", "CLIENTE", "VENTAS", "EXENTO", "SERVICIOS", "EXPORTACION", "IVA", "TOTAL");
        $this->SetWidths(array(1.5,2,1.5,1.5,3,1.7,1.7,1.7,1.7,1.7,1.7));
        $this->SetAligns(array("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"));
    }
    
    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_ven = "SELECT *,
                TRIM(iv.ivav_serie) AS ivav_serie,
                TRIM(iv.ivav_nit) AS ivav_nit,
                TRIM(iv.ivav_clin) AS ivav_clin
            FROM iva_ven iv LEFT JOIN tipov tv ON tv.tv_cod=iv.ivav_tipo
            WHERE iv.ivav_emp='$empCod' AND iv.ivav_fec>='$Fecini' AND iv.ivav_fec<='$Fecfin' AND iv.ivav_afe='S' 
            ORDER BY iv.ivav_emp,iv.ivav_fec,iv.ivav_tipo,iv.ivav_num";

# EJECUCION DE QUERYS
$db = new dbExec();
# GENERAR PDF
$pdf = new generatePDF("P","cm", "Legal");
$pdf->AddPage();

$func = new functions();
$linea=1;
$cantdoc=0;
$select["iva_ven"] = $db->query($sIva_ven);
while ($rec = $db->fetchAssoc($select['iva_ven'])) {
    // TASA DE CAMBIO
    $cantdoc += 1;
    $tsubna = $tsubna+($rec["ivav_subna"]+$rec["ivav_exen"]);
    $tsubb = ($tsubb+$rec["ivav_subb"]+$rec["ivav_subme"]+$rec["ivav_subve"]+$rec["ivav_subco"]);
    $tsubs += $rec["ivav_subs"];
    $tsube = $tsube+$rec["ivav_sube"]+$rec["ivav_suber"];
    $tiva += $rec["ivav_iva"];
    $ttot += $rec["ivav_tot"];

    $rsubna += ($rec["ivav_subna"]+$rec["ivav_exen"]);
    $rsubb  += ($rec["ivav_subb"]+$rec["ivav_subme"]+$rec["ivav_subve"]+$rec["ivav_subco"]);
    $rsubs  +=  $rec["ivav_subs"];
    $rsube +=  ($rec["ivav_sube"]+$rec["ivav_suber"]);
    $riva += $rec["ivav_iva"];
    $rtot += $rec["ivav_tot"];

    $body = array(
        strftime("%d/%m/%Y", strtotime($rec["ivav_fec"])),
        $rec["tv_nom"],
        $rec["ivav_num"],
        $rec["ivav_nit"],
        $rec["ivav_clin"],
        (round($rec["ivav_subb"]+$rec["ivav_subme"]+$rec["ivav_subve"]+$rec["ivav_subco"], 2) != 0) ? number_format($rec["ivav_subb"]+$rec["ivav_subme"]+$rec["ivav_subve"]+$rec["ivav_subco"], 2, '.', ',') : "",
        (round($rec["ivav_subna"]+$rec["ivav_exen"], 2) != 0) ? number_format($rec["ivav_subna"]+$rec["ivav_exen"], 2, '.', ',') : "",
        (round($rec["ivav_subs"], 2) != 0) ? number_format($rec["ivav_subs"], 2, '.', ',') : "",
        (round($rec["ivav_sube"]+$rec["ivav_suber"], 2) != 0) ? number_format($rec["ivav_sube"]+$rec["ivav_suber"], 2, '.', ',') : "",
        (round($rec["ivav_iva"], 2) != 0) ? number_format($rec["ivav_iva"], 2, '.', ',') : "",
        number_format($rec["ivav_tot"], 2, '.', ','),
    );
    $pdf->RowBorderHidden($body, $lineHeight);
    //  (round($totSalIni, 2) != 0) ? number_format($totSalIni, 2, '.', ',') : "",

    $linea=2;
    if (($pdf->GetY()+$lineHeight) > 32) {
        $pdf->line($pdf->GetX()+7, $pdf->GetY(), 20.6, $pdf->GetY());
        $body = array(
            "",
            "",
            "",
            "",
            "      VAN ................",
            number_format($tsubb, 2, '.', ','),
            number_format($tsubna, 2, '.', ','),
            number_format($tsubs, 2, '.', ','),
            number_format($tsube, 2, '.', ','),
            number_format($tiva, 2, '.', ','),
            number_format($ttot, 2, '.', ','),
        );
        $pdf->RowBorderHidden($body, $lineHeight);
        $pdf->line($pdf->GetX()+7, $pdf->GetY(), 20.6, $pdf->GetY());
        $pdf->AddPage();
        $pdf->line($pdf->GetX()+7, $pdf->GetY(), 20.6, $pdf->GetY());
        $body = array(
            "",
            "",
            "",
            "",
            "      VIENEN ............",
            number_format($tsubb, 2, '.', ','),
            number_format($tsubna, 2, '.', ','),
            number_format($tsubs, 2, '.', ','),
            number_format($tsube, 2, '.', ','),
            number_format($tiva, 2, '.', ','),
            number_format($ttot, 2, '.', ','),
        );
        $pdf->RowBorderHidden($body, $lineHeight);
        $pdf->line($pdf->GetX()+7, $pdf->GetY(), 20.6, $pdf->GetY());

    }
}

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

if (($pdf->GetY()+$lineHeight) > 30) {
        $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("VENTAS",number_format($rsubb, 2, '.', ','),"SERVICIOS",number_format($rsubs, 2, '.', ','));
$pdf->Row($body, $lineHeight);
$body = array(utf8_decode("EXENTAS"),number_format($rsubna, 2, '.', ','),"EXPORTACIONES",number_format($rsube, 2, '.', ','));
$pdf->Row($body, $lineHeight);
$body = array("IVA",number_format($riva, 2, '.', ','),"TOTAL",number_format($rtot, 2, '.', ','));
$pdf->Row($body, $lineHeight);

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

?>