<!DOCTYPE html>
<html lang="es">

<head>
   <!-- Google Tag Manager -->
        <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
        new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
        j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
        'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
        })(window,document,'script','dataLayer','GTM-N4ZZ9MM');</script>
        <!-- End Google Tag Manager -->
    <meta charset="iso-8859-1" />
    <title>Performance Paris Internet</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="../../../bootstrap-3.3.6-dist/css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../../../bootstrap-select-1.9.4/dist/css/bootstrap-select.css" />
    <link rel="stylesheet" type="text/css" href="../../../estilo.css" />
    <link rel="shortcut icon" type="image/png" href="../../../paris.png" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <!-- analytics -->
    <script>
        /*(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                                                                    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                                                                m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                                                            })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

                                                            ga('create', 'UA-88784345-1', 'auto');
                                                            ga('send', 'pageview');*/

        function change_date() {
            var month = document.getElementById("month").value;

            var year = document.getElementById("year").value;

            var historicos = document.getElementsByClassName("tipo_venta");
            
            var neg_div = document.getElementById("neg_div").value;

            var cantidad_historicos = historicos.length;

            document.getElementById("plan").href = "../../../ingresos/plan/index.php?month=" + month + "&year=" + year;

            document.getElementById("filtro_depto").href = "../por_departamento/index.php?month=" + month + "&year=" + year + "&depto=601";

            document.getElementById("filtro_negocio_division").href = "index.php?month=" + month + "&year=" + year + "&neg_div="+neg_div;
            
            document.getElementById("historico").href = "../../../ingresos/historico/index.php?month=" + month + "&year=" + year;
            
            document.getElementById("tipo_pago").href = "../../tipo_pago/index.php?month=" + month + "&year=" + year;

            for (var i = 0; i < cantidad_historicos; i++)
                historicos[i].href = "../index.php?month=" + month + "&year=" + year;
        }
    </script>

</head>

<body>
   <!-- Google Tag Manager (noscript) -->
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-N4ZZ9MM"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <!-- End Google Tag Manager (noscript) -->
    <header class="container">
        <nav class="navbar navbar-default">
            <div class="btn-group-sm">
                <div class="row">
                    <div class="col-md-12">
                        <h3 class="text-center"><a href="http://10.95.17.114/paneles"><img src="../../../paris.png" width="140px" height="100px"></a> Ingresos por Tipo Venta, <?php
                            if(isset($_GET['neg_div']))
                                echo " " . $_GET['neg_div'];
                            else
                                echo " VESTUARIO";
                            ?></h3></div>
                </div>

                <div class="row">
                    <div class="col-lg-6 col-sm-6">
                        <h5 class="text-center text-success" style="margin-left: 200px;">Última actualización a las <?php
                                date_default_timezone_set("America/Santiago");
                                $con = new mysqli('localhost', 'root', '', 'ventas');
                                $query = "select hora from actualizar";
                                $res = $con->query($query);
                                $hour = 0;
                                while($row = mysqli_fetch_assoc($res)){
                                    $h = $row['hora'];

                                    if(strlen($row['hora']) == 1)
                                        $h = "00000" . $h;

                                    if(strlen($row['hora']) == 2)
                                        $h = "0000" . $h;

                                    if(strlen($row['hora']) == 3)
                                        $h = "000" . $h;

                                    if(strlen($row['hora']) == 4)
                                        $h = "00" . $h;

                                    if(strlen($row['hora']) == 5)
                                        $h = "0" . $row['hora'];

                                    $h = new DateTime($h);
                                }
                                echo $h->format("H:i") . " horas.";

                                ?></h5></div>
                    <div class="col-lg-6 col-sm-6">
                        <a class="btn btn-default btn-sm" href="../../../query.php" style="margin-left: 001px;">Query Ventas<img id="txt" src="../../../images.png"></a>
                    </div>
                </div>
                <br>

                <form action="index.php" method="get" class="row">
                    <div class="col-lg-2">
                        <div class="dropdown">
                            <button class="btn btn-default btn-sm dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                                Seleccione Reporte
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                                <li class="dropdown-header">Reporte de Ingresos</li>

                                <!-- Link a Panel Plan -->
                                <li><a id="plan" href="../../../ingresos/plan/index.php?month=<?php 
                                        if(isset($_GET['month'])) 
                                            echo $_GET['month'];
                                        else
                                            echo date(" m ");?>&year=<?php 
                                        if(isset($_GET['year']))
                                            echo $_GET['year'];
                                        else
                                            echo date("Y ");?>">Reporte por Plan</a></li>

                                <!-- Link a Panel Historico -->
                                <li><a id="historico" href="../../../ingresos/historico/index.php?month=<?php 
                                        if(isset($_GET['month'])) 
                                            echo $_GET['month'];
                                        else
                                            echo date(" m ");?>&year=<?php 
                                        if(isset($_GET['year']))
                                            echo $_GET['year'];
                                        else
                                            echo date("Y ");?>">Reporte Histórico</a></li>

                                <li role="separator" class="divider"></li>

                                <li class="dropdown-header">Reporte de Ingresos por Canal</li>
                                
                                <li><a class="tipo_venta" href="../index.php?month=<?php 
                                        if(isset($_GET['month'])) 
                                            echo $_GET['month'];
                                        else
                                            echo date(" m ");?>&year=<?php 
                                        if(isset($_GET['year']))
                                            echo $_GET['year'];
                                        else
                                            echo date("Y ");?>">Reporte Ingresos Tipo Venta</a></li>
                                
                                <li><a id="tipo_pago" href="../../tipo_pago/index.php?month=<?php 
                                        if(isset($_GET['month'])) 
                                            echo $_GET['month'];
                                        else
                                            echo date(" m ");?>&year=<?php 
                                        if(isset($_GET['year']))
                                            echo $_GET['year'];
                                        else
                                            echo date("Y ");?>">Reporte Ingresos Tipo Pago</a></li>

                                <li role="separator" class="divider"></li>

                                <li><a href="#">Reporte de Indicadores</a></li>
                            </ul>
                        </div>
                    </div>

                    <div class="col-lg-1" style="width: 20%;">
                        <select name="month" id="month" title="Mes" class="selectpicker" data-style="btn btn-default btn-sm" data-width="100px" onchange="change_date();">
                            <?php
                            $meses = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");

                            $cant_meses = count($meses);

                            if(isset($_GET['month'])){
                                $mes = $_GET['month'] - 1;

                                for ($i = 0; $i < $cant_meses; $i++) {
                                    $month_number = $i + 1;

                                    if (strlen($month_number) < 2)
                                        $month_number = "0" . $month_number;

                                    if ($mes == $i)
                                        echo "<option value='$month_number' selected='selected'>" . $meses[$i] . "</option>";
                                    else
                                        echo "<option value='$month_number'>" . $meses[$i] . "</option>";
                                }
                            }else{
                                $mes_actual = date("m") - 1;

                                for ($i = 0; $i < $cant_meses; $i++) {
                                    $month_number = $i + 1;

                                    if (strlen($month_number) < 2)
                                        $month_number = "0" . $month_number;

                                    if ($mes_actual == $i)
                                        echo "<option value='$month_number' selected='selected'>" . $meses[$i] . "</option>";
                                    else
                                        echo "<option value='$month_number'>" . $meses[$i] . "</option>";
                                }
                            }
                            ?>
                        </select>

                        <select name="year" id="year" title="Año" class="selectpicker" data-style="btn btn-default btn-sm" data-width="100px" onchange="change_date();">
                            <?php
                                $first_year = 2015;

                                $last_year = date("Y");

                                if(isset($_GET['year'])){
                                    $this_year = $_GET['year'];

                                    for ($i = $first_year; $i <= $last_year; $i++) {
                                        if ($i == $this_year)
                                            echo "<option value='$i' selected='selected'>$i</option>";
                                        else
                                            echo "<option value='$i'>$i</option>";
                                    }
                                }else {
                                    for ($i = $first_year; $i <= $last_year; $i++) {
                                        if ($i == $last_year)
                                            echo "<option value='$i' selected='selected'>$i</option>";
                                        else
                                            echo "<option value='$i'>$i</option>";
                                    }
                                }
                                ?>
                        </select>
                    </div>

                    <!-- Div para selección de filtro -->
                    <div class="col-lg-2">
                        <div class="dropdown">
                            <button class="btn btn-default btn-sm dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                                Seleccione Filtro
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu" aria-labelledby="dropdownMenu2">

                                <!-- Link a Panel Historico (Sin Filtro) -->
                                <li><a class="tipo_venta" href="../index.php?month=<?php 
                                        if(isset($_GET['month'])) 
                                            echo $_GET['month'];
                                        else
                                            echo date(" m ");?>&year=<?php 
                                        if(isset($_GET['year']))
                                            echo $_GET['year'];
                                        else
                                            echo date("Y ");?>">Sin Filtro</a></li>

                                <!-- Link a Panel Historico -->
                                <li><a id="filtro_depto" href="../por_departamento/index.php?month=<?php 
                                        if(isset($_GET['month'])) 
                                            echo $_GET['month'];
                                        else
                                            echo date(" m ");?>&year=<?php 
                                        if(isset($_GET['year']))
                                            echo $_GET['year'];
                                        else
                                            echo date("Y ");?>&depto=601">Por Departamento</a></li>

                                <li><a id="filtro_negocio_division" href="index.php?month=<?php 
                                        if(isset($_GET['month'])) 
                                            echo $_GET['month'];
                                        else
                                            echo date(" m ");?>&year=<?php 
                                        if(isset($_GET['year']))
                                            echo $_GET['year'];
                                        else
                                            echo date("Y ");?>&neg_div=<?php 
                                        if(isset($_GET['neg_div']))
                                            echo $_GET['neg_div'];
                                        else
                                            echo "VESTUARIO";?>">Por Negocio / División</a></li>
                            </ul>
                        </div>
                    </div>

                    <div class="col-lg-2" style="width: 20%;">
                            <select name="neg_div" id="neg_div" class="selectpicker" data-style="btn btn-default btn-sm" onchange="change_date();">
                                <?php
                                if(isset($_GET['neg_div'])){
                                    $neg_div_selected = $_GET['neg_div'];
                                    
                                    $query = "select distinct negocio from depto where negocio <> ''";
                                    
                                    $res   = $con->query($query);
                                    
                                    echo "<optgroup label='Negocios'>";
                                    
                                    while($row = mysqli_fetch_assoc($res)){
                                        $neg_div    = $row['negocio'];
                                        if($neg_div_selected == $neg_div)
                                            echo "<option value='$neg_div' selected='selected'>$neg_div</option>";
                                        else
                                            echo "<option value='$neg_div'>$neg_div</option>";
                                    }
                                    
                                    echo "</optgroup>";
                                    
                                    $query = "select distinct division from depto where division <> ''";
                                    
                                    $res   = $con->query($query);
                                    
                                    echo "<optgroup label='Divisiones'>";
                                    
                                    while($row = mysqli_fetch_assoc($res)){
                                        $neg_div    = $row['division'];
                                        if($neg_div_selected == $neg_div)
                                            echo "<option value='$neg_div' selected='selected'>$neg_div</option>";
                                        else
                                            echo "<option value='$neg_div'>$neg_div</option>";
                                    }
                                    
                                    echo "</optgroup>";
                                }else{
                                    $query = "select distinct negocio from depto where negocio <> ''";
                                    
                                    $res   = $con->query($query);
                                    
                                    $i = 0;
                                    
                                    echo "<optgroup label='Negocios'>";
                                    
                                    while($row = mysqli_fetch_assoc($res)){
                                        $neg_div    = $row['negocio'];
                                        if($i == 0)
                                            echo "<option value='$neg_div' selected='selected'>$neg_div</option>";
                                        else
                                            echo "<option value='$neg_div'>$neg_div</option>";
                                        
                                        $i++;
                                    }
                                    
                                    echo "</optgroup>";
                                    
                                    $query = "select distinct division from depto where division <> ''";
                                    
                                    $res   = $con->query($query);
                                    
                                    echo "<optgroup label='Divisiones'>";

                                    
                                    while($row = mysqli_fetch_assoc($res)){
                                        $neg_div    = $row['division'];
                                        if($neg_div_selected == $neg_div)
                                            echo "<option value='$neg_div' selected='selected'>$neg_div</option>";
                                        else
                                            echo "<option value='$neg_div'>$neg_div</option>";
                                    }
                                    echo "</optgroup>";
                                    
                                }
                                ?>
                            </select>
                        </div>

                    <div class="col-lg-1 col-md-1 col-sm-1" style="width: 140px;" id="act">
                        <button class="btn btn-primary btn-sm" style="width: 100px;">Actualizar</button>
                    </div>
                </form>
            </div>
        </nav>
    </header>

    <table class='table table-bordered table-condensed table-hover'>
        <thead>
            <tr>
                <th rowspan="3" style="background: white;">
                    <h6 class="text-center"><b>Fecha</b></h6></th>
                <th colspan="15" rowspan="1" style="background-color: #35388E; color: white;">
                    <h6 class="text-center"><b>Ingresos Tipo de Venta</b></h6></th>
            </tr>

            <tr>
                <th rowspan="1" colspan="3" style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>Ingresos Sitio</b></h6></th>
                <th rowspan="1" colspan="3" style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>Ingresos Fonocompras</b></h6></th>
                <th rowspan="1" colspan="3" style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>Empresa</b></h6></th>
                <th rowspan="1" colspan="3" style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>Ingresos Puntos Cencosud</b></h6></th>
                <th rowspan="1" colspan="3" style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>Total</b></h6></th>
            </tr>

            <tr>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>$ Actual</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>$ Anterior</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>% R/Past</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>$ Actual</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>$ Anterior</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>% R/Past</b></h6></th>
                <th style='background-color: #E0E1EE;'>
                    <h6 class="text-center"><b>$ Actual</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>$ Anterior</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>% R/Past</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>$ Actual</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>$ Anterior</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>% R/Past</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>$ Actual</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>$ Anterior</b></h6></th>
                <th style="background-color: #E0E1EE;">
                    <h6 class="text-center"><b>% R/Past</b></h6></th>
            </tr>
        </thead>

        <?php
        require_once '../../paneles.php';
        
        if(isset($_GET['month']) && isset($_GET['year']) && isset($_GET['neg_div'])){
            $month = $_GET['month'];
            
            $year  = $_GET['year'];
            
            $neg_div = $_GET['neg_div'];
            
            tipo_venta_por_negocio_division($month, $year, $neg_div, $con);
        }else{
            $month = date("m");
            
            $year  = date("Y");
            
            $neg_div = 'VESTUARIO';
            
            tipo_venta_por_negocio_division($month, $year, $neg_div, $con);
        }
        ?>
    </table>


    <script src="../../../bootstrap-3.3.6-dist/js/bootstrap.min.js"></script>
    <script src="../../../bootstrap-select-1.9.4/js/bootstrap-select.js"></script>
</body>

</html>