<?php
include "conn.php";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // Make MySQLi throw exceptions.

        try {
            // Try Connect to the DB with new MySqli object - Params {hostname, userid, password, dbname}
            $mysqli = new mysqli($host, $user, $pass, $base);


              $tipo1 = " AND c.suc='".$_GET["tienda"]."' AND c.genero='".$_GET["gen"]."'";

              $tipo2 = '';
              if($_GET["suc"]>0) {
                $tipo2 = " AND suc='".$_GET["suc"]."'";
              } else{
                $tienda = "Por Categoría";
              }
              
              $titulo = $_GET["tienda"];
              $subtitulo = "Artículos entallados que debería tener cada tienda en base a su venta por Categoría";


            // Select all records from the db. Keep inside try block incase there an issue with sql query
            $query = "select a.periodo,c.suc as tienda,c.genero,c.linea
            ,concat('$',format(round(a.mntovta),0)) as venta_pesos,concat(round(100*porc_vta,2),'%') as porc_vta
            ,round(a.porc_vta*b.`total`) as entallados_venta
            ,concat(c.`articulos_entallados`,' --> ',round(100*c.`articulos_entallados`/(c.`articulos_entallados`+c.`articulos_picos`)),'%') as articulos_entallados
            ,concat(c.`articulos_picos`,' --> ',round(100*c.`articulos_picos`/(c.`articulos_entallados`+c.`articulos_picos`)),'%') as articulos_picos
            ,round(a.porc_vta*b.`total`*0.10) as bodega
            ,round(a.porc_vta*b.`total`*0.00) as bodegar
            ,b.`total` as entallados_tot
            ,m.meta
            FROM tienda_entallado c LEFT JOIN ventas_gen a
            ON a.`tienda`=c.suc AND a.`genero`=c.genero AND a.linea=c.linea 
            AND c.fecha=CURDATE() AND periodo = DATE_FORMAT(CURDATE(),'%Y%m')
            left join tot_entallado b on a.tienda=b.tienda
            and a.genero = b.genero
            left join metas m on m.tienda=a.tienda and m.linea=a.linea and m.genero=a.genero and m.almacen='PISO' 
            where c.`fecha`=CURDATE() ".$tipo1."
            UNION
            select a.periodo,c.suc as tienda,c.genero,'TOTAL' as linea,concat('$',format(round(sum(a.mntovta)),0)) as venta_pesos
            ,concat(round(100*sum(porc_vta),2),'%') as porc_vta
            ,round(sum(a.porc_vta*b.`total`)) as entallados_venta
            ,concat(sum(c.`articulos_entallados`),' --> ',round(100*sum(c.`articulos_entallados`)/sum(c.`articulos_entallados`+c.`articulos_picos`)),'%') as articulos_entallados
            ,concat(sum(c.`articulos_picos`),' --> ',round(100*sum(c.`articulos_picos`)/sum(c.`articulos_entallados`+c.`articulos_picos`)),'%') as articulos_picos
            ,round(sum(a.porc_vta*b.`total`)*0.10) as bodega
            ,round(sum(a.porc_vta*b.`total`)*0.00) as bodegar
            ,b.`total` as entallados_tot
            ,sum(m.meta) as meta
            FROM tienda_entallado c LEFT JOIN ventas_gen a
            ON a.`tienda`=c.suc AND a.`genero`=c.genero AND a.linea=c.linea 
            AND c.fecha=CURDATE() AND periodo = DATE_FORMAT(CURDATE(),'%Y%m')
            left join tot_entallado b on a.tienda=b.tienda
            and a.genero = b.genero
            left join metas m on m.tienda=a.tienda and m.linea=a.linea and m.genero=a.genero and m.almacen='PISO' 
            where c.`fecha`=CURDATE() ".$tipo1."
            ";

            //echo $query;
            
            if ($result = $mysqli->query($query)) {

                /* fetch associative array */
                while ($row = $result->fetch_assoc()) {
                    
                    if($tienda==''){
                      $tienda = $row["sucursal"];
                    }
                    
                    $tabla = $tabla . '<tr>
                    <td>'.$row["tienda"].'</td>
                    <td>'.$row["genero"].'</td>
                    <td><a href="sucursal_detalle.php?suc='.$row["tienda"].'&linea='.$row["linea"].'">'.$row["linea"].'</a></td>
                    <td>'.$row["venta_pesos"].'</td>
                    <td>'.$row["porc_vta"].'</td>
                    <td>'.$row["entallados_venta"].'</td>
                    <td>'.$row["meta"].'</td>
                    <td>'.$row["articulos_entallados"].'</td>
                    <td>'.$row["articulos_picos"].'</td>
                    <td>'.$row["entallados_tot"].'</td>
                    <td>'.$row["bodega"].'</td>
                    <td>'.$row["bodegar"].'</td>
                  </tr>';
                }

                /* free the memory associated with this result set */
                $result->free();
            }
        } catch (mysqli_sql_exception $e) { // Failed to connect? Lets see the exception details..
            echo "MySQLi Error Code: " . $e->getCode() . "<br />";
            echo "Exception Msg: " . $e->getMessage();
            exit; // exit and close connection.
        }

        $mysqli->close(); // finally, close the connection
        
 ?>  
 
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="Start your development with a Dashboard for Bootstrap 4.">
  <meta name="author" content="Creative Tim">
  <title><?php echo $marca;?></title>

  <!-- Fonts -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700">
  <!-- Icons -->
  <link rel="stylesheet" href="../../assets/vendor/nucleo/css/nucleo.css" type="text/css">
  <link rel="stylesheet" href="../../assets/vendor/@fortawesome/fontawesome-free/css/all.min.css" type="text/css">
  <!-- Page plugins -->
  <link rel="stylesheet" href="../../assets/vendor/datatables.net-bs4/css/dataTables.bootstrap4.min.css">
  <link rel="stylesheet" href="../../assets/vendor/datatables.net-buttons-bs4/css/buttons.bootstrap4.min.css">
  <link rel="stylesheet" href="../../assets/vendor/datatables.net-select-bs4/css/select.bootstrap4.min.css">
  <!-- Argon CSS -->
  <link rel="stylesheet" href="../../assets/css/argon.css?v=1.1.0" type="text/css">
</head>

<body>

  <!-- menu lateral -->
  <?php include "menu.php"?>
  
  <!-- Main content -->
  <div class="main-content" id="panel">
    <!-- Topnav -->
    
    <!-- Header -->
    <!-- Header -->
    <div class="header bg-primary pb-6">
      <div class="container-fluid">
        <div class="header-body">
        <div class="row align-items-center py-4">

<div class="col-lg-6 col-7">
  <h6 class="h2 text-white d-inline-block mb-0"><?php echo $tienda;?></h6>
</div>

  <!-- Sidenav toggler -->
  <?php include "toggler.php"?>


</div>
        </div>
      </div>
    </div>
    <!-- Page content -->
    <div class="container-fluid mt--6">
      <!-- Table -->
      <div class="row">
        <div class="col">
          <div class="card">
            <!-- Card header -->
            <div class="card-header">
              <h3 class="mb-0"><?php echo $titulo;?></h3>
              <p class="text-sm mb-0">
              <?php echo $subtitulo;?>
              </p>
            </div>
            <div class="table-responsive py-4">
              <table class="table table-flush" id="datatable-basic" data-order='[[ 4, "desc" ]]' data-page-length='100'>
                <thead class="thead-light">
                  <tr>
                    <th>Tienda</th>
                    <th>Genero</th>
                    <th>Linea</th>
                    <th>Venta$</th>
                    <th>Porc Vta</th>
                    <th>Sugeridos</th>
                    <th>Meta</th>
                    <th>Entallados actual</th>
                    <th>Picos actual</th>
                    <th>Entallados meta</th>
                    <th>Bodega meta</th>
                    <th>Bodega actual</th>
                  </tr>
                </thead>
                <tfoot>
                  <tr>
                    <th>Tienda</th>
                    <th>Genero</th>
                    <th>Linea</th>
                    <th>Venta$</th>
                    <th>Porc Vta</th>
                    <th>Sugeridos</th>
                    <th>Meta</th>
                    <th>Entallados actual</th>
                    <th>Picos actual</th>
                    <th>Entallados meta</th>
                    <th>Bodega meta</th>
                    <th>Bodega actual</th>
                  </tr>
                </tfoot>
                <tbody>
                  <?php echo $tabla;?>
                </tbody>
              </table>
            </div>
          </div>
          
        </div>
      </div>
       <!-- Footer -->
       <footer class="footer pt-0">
         <div class="row align-items-center justify-content-lg-between">
           <div class="col-lg-6">
             <div class="copyright text-center text-lg-left text-muted">
               &copy; <?php echo $anio;?> <a href="#" class="font-weight-bold ml-1" ><?php echo $marca;?></a>
             </div>
           </div>

         </div>
       </footer>
    </div>
  </div>
  <!-- Argon Scripts -->
  <!-- Core -->
  <script src="../../assets/vendor/jquery/dist/jquery.min.js"></script>
  <script src="../../assets/vendor/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
  <script src="../../assets/vendor/js-cookie/js.cookie.js"></script>
  <script src="../../assets/vendor/jquery.scrollbar/jquery.scrollbar.min.js"></script>
  <script src="../../assets/vendor/jquery-scroll-lock/dist/jquery-scrollLock.min.js"></script>
  <!-- Optional JS -->
  <script src="../../assets/vendor/datatables.net/js/jquery.dataTables.min.js"></script>
  <script src="../../assets/vendor/datatables.net-bs4/js/dataTables.bootstrap4.min.js"></script>
  <script src="../../assets/vendor/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
  <script src="../../assets/vendor/datatables.net-buttons-bs4/js/buttons.bootstrap4.min.js"></script>
  <script src="../../assets/vendor/datatables.net-buttons/js/buttons.html5.min.js"></script>
  <script src="../../assets/vendor/datatables.net-buttons/js/buttons.flash.min.js"></script>
  <script src="../../assets/vendor/datatables.net-buttons/js/buttons.print.min.js"></script>
  <script src="../../assets/vendor/datatables.net-select/js/dataTables.select.min.js"></script>
  <!-- Argon JS -->
  <script src="../../assets/js/argon.js?v=1.1.0"></script>

</body>

</html>