<?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);

// guarda datos
if( $_POST["porcentaje_entallado_global"] > 0 
&& $_POST["dias_venta"] > 0
&& $_POST["porcentaje_entallado_cedis"] > 0
&& $_POST["porcentaje_entallado_bodega"] > 0
 ) {
    
  $query = "UPDATE globales
  SET `porcentaje_entallado_cedis` = '".$_POST["porcentaje_entallado_cedis"]."',
    `dias_venta` = '".$_POST["dias_venta"]."',
    `porcentaje_entallado_bodega` = '".$_POST["porcentaje_entallado_bodega"]."',
    `dias_venta_movs` = '".$_POST["dias_venta_movs"]."',
    `porcentaje_entallado_global` = '".$_POST["porcentaje_entallado_global"]."'";
  $mysqli->query($query);

  # calcula el % de la meta del cedis
  $query = "REPLACE INTO metas_cedis_calc(tienda,genero,linea,meta_tot,meta_calc)
  SELECT a.tienda,a.genero,a.linea,SUM(a.meta) AS meta_tot
  ,ROUND(SUM(a.meta)*(porcentaje_entallado_cedis/100)) AS meta_calc
  FROM metas a INNER JOIN globales
  WHERE a.almacen='piso' and tienda<>'00 CDS'
  GROUP BY 1,2,3";
  $mysqli->query($query);

  # actualiza la meta del cedis calculada
  $query = "UPDATE metas2 a INNER JOIN metas_cedis_calc b ON a.tienda=b.tienda AND a.genero=b.genero AND a.linea=b.linea
  SET a.meta_cedis_calc=b.meta_calc";
  $mysqli->query($query);

  # borra cedis calculada
  $query = "UPDATE metas2 SET meta_cedis_calc=0 WHERE tienda='00 CDS'";
  $mysqli->query($query);

  $date = date('m/d/Y h:i:s a', time());
  $mensaje = "se guardaron los globales ".$date; 

  $mysqli->query("insert into log(tipo,log) values('glo','$mensaje')"); 

} 
            // Select all records from the db. Keep inside try block incase there an issue with sql query
            $query = "select * from globales
            ";

            //echo $query;
            /*
            `porcentaje_entallado_cedis`,
            `dias_venta`,
            `porcentaje_entallado_bodega`,
            `porcentaje_entallado_global`
            */
            $porcentaje_entallado_cedis = "";
            $dias_venta = "";
            $porcentaje_entallado_bodega = "";
            $porcentaje_entallado_global = "";


            if ($result = $mysqli->query($query)) {

                /* fetch associative array */
                while ($row = $result->fetch_assoc()) {
                    if($porcentaje_entallado_global==""){

                    $porcentaje_entallado_cedis = $row["porcentaje_entallado_cedis"];
                    $dias_venta = $row["dias_venta"];
                    $porcentaje_entallado_bodega = $row["porcentaje_entallado_bodega"];
                    $porcentaje_entallado_global = $row["porcentaje_entallado_global"];
                    $dias_venta_movs = $row["dias_venta_movs"];

                    }
                }

                /* 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">

  <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">Globales</h6>
</div>

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

</div>
        </div>
      </div>
    </div>
    <!-- Page content -->
    <div class="container-fluid mt--6">
      <div class="row">

        <div class="col-lg-7">
          <!-- card-wrapper -->
          <div class="card-wrapper">
            
            <div class="card">
              <!-- Card header -->
              <div class="card-header">
                <h3 class="mb-0">Ajustes generales</h3>
              </div>
              <!-- Card body -->
              <div class="card-body">
                <form method="post">
                  <!-- porcentaje_entallado_global -->
                  <div class="row">
                    <div class="col-md-7">
                      <div class="form-group">
                        <div class="input-group input-group-merge">
                        Porcentaje de entallado global<input name="porcentaje_entallado_global" value="<?php echo $porcentaje_entallado_global;?>" class="form-control" size="3" maxlength="3" type="text">%
                        </div>
                      </div>
                    </div>
                  </div>

                  <!-- dias_venta -->
                  <div class="row">
                    <div class="col-md-7">
                      <div class="form-group">
                        <div class="input-group input-group-merge">
                        Días de venta<input name="dias_venta" value="<?php echo $dias_venta;?>" class="form-control" size="3" maxlength="3" type="text">
                        </div>
                      </div>
                    </div>
                  </div>

                  <!-- porcentaje_entallado_cedis -->
                  <div class="row">
                    <div class="col-md-7">
                      <div class="form-group">
                        <div class="input-group input-group-merge">
                        Porcentaje de entallado Cedis<input name="porcentaje_entallado_cedis" value="<?php echo $porcentaje_entallado_cedis;?>" class="form-control" size="3" maxlength="3" type="text">%
                        </div>
                      </div>
                    </div>
                  </div>

                  <!-- porcentaje_entallado_bodega -->
                  <div class="row">
                    <div class="col-md-7">
                      <div class="form-group">
                        <div class="input-group input-group-merge">
                        Porcentaje de entallado Bodega<input name="porcentaje_entallado_bodega" value="<?php echo $porcentaje_entallado_bodega;?>" class="form-control" size="6" maxlength="6" type="text">%
                        </div>
                      </div>
                    </div>
                  </div>
                  
                  <!-- dias_venta_movs -->
                  <div class="row">
                    <div class="col-md-7">
                      <div class="form-group">
                        <div class="input-group input-group-merge">
                        Días de venta movs<input name="dias_venta_movs" value="<?php echo $dias_venta_movs;?>" class="form-control" size="3" maxlength="3" type="text">
                        </div>
                      </div>
                    </div>
                  </div>
                  
                  <?php echo $mensaje;?>
                  <button class="btn btn-primary" type="submit">Guardar</button>
                  
                </form>
              </div>

            </div>

          </div>
          <!-- card-wrapper -->
        </div>
        
      </div>
      <!-- row -->
    </div>
    <!-- container -->
       <!-- 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>