<?php
$usuario = $_COOKIE["usuario"];
$nivel = $_COOKIE["nivel"];
if($nivel=="Master"){$usuario="";}
if($nivel!="Master"){
	echo "<script>window.location.href='../drink/vender.php'</script>";
}


 include_once("../php/config.php");
 include("graficos.php");
 if($anivel=="administrador"){$usuario="";}

 //@mysql_query("update cuotas set mora=0 where mora<0") or die(mysql_error());

//@mysql_query("update cuotas set balance_capital=0 where balance_capital<0") or die(mysql_error());

//@mysql_query("update cuotas set balance_interes=0 where balance_interes<0") or die(mysql_error());

//@mysql_query("update cuotas set balance=mora+balance_capital+balance_interes") or die(@mysql_error());



 $sql = @mysql_query("select * from empresa") or die(mysql_error());

 $ventas = @mysql_query("select sum(total) as total from ventas where fecha='$hoy' and estado!='Anulada'") or die(mysql_error());
 $compras = @mysql_query("select sum(total) as total from compras where fecha='$hoy'") or die(mysql_error());
 $cobros = @mysql_query("select sum(monto) as total from pagos_credito where fecha='$hoy'") or die(mysql_error());
 $devoluciones = @mysql_query("select sum(importe) as total from devoluciones where fecha='$hoy'") or die(mysql_error());
 $ventas_pendiente = @mysql_query("select sum(total) as total from mesas where estado='Ocupada'") or die(mysql_error());
  $todos_cuadres = @mysql_query("select sum(total_ventas) as total from cuadres where estado='Abierta'") or die(mysql_error());
   $sqlInventario = @mysql_query("select * from productos") or die(mysql_error());
   while($r = mysql_fetch_array($sqlInventario)){
	   $inventario+=$r["costo"] * $r["stock"];
   }
   
   $sql_cancelaciones = mysql_query("select sum(monto) as monto from cancelaciones where fecha=CURDATE() and hora<'24:01:01' order by fecha asc") or die(mysql_error());
$cancelaciones = @mysql_result($sql_cancelaciones, 0, "monto");



 //$sql_prestamos = @mysql_query("select count(*) as prestamos from prestamos where estado_prestamo!='Anulado' and usuario like '%".$usuario."%'") or die(mysql_error());

 //$sql_vencidas = @mysql_query("select count(*) as vencidas from cuotas where vence='$hoy' and balance>0 and usuario like '%".$usuario."%'") or die(mysql_error());
 //$sql_atrasadas =@mysql_query("select  count(distinct(id_prestamo)) as atrasadas from cuotas where vence<'$hoy' and balance>0 and usuario like '%".$usuario."%'") or die(@mysql_error());

 //$sql_cobros = @mysql_query("select sum(total) as total from cobros where fecha='$hoy' and usuario like '%".$usuario."%'") or die(@mysql_error());

 $empresa = @mysql_result($sql,0,"nombre");
 $telefono = @mysql_result($sql,0,"telefono");
 $direccion = @mysql_result($sql,0,"direccion");
 $rnc = @mysql_result($sql,0,"rnc");
?>

<?php
 include("../header.php");
?>
 <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<style>


 .container-fluid{
@background: white;
	 @padding: 15px;
 }

 .panel{
    height: 200px;
     border:none;
 }

.panel-heading{
 box-shadow: rgba(0, 0, 0, 0.05) 0px 0px 0px, rgba(0, 0, 0, 0.05) 0px 0px 0px -20px;
 height: 200px;
 border:none;
}

body{
    background: #f5f5f5;
}
</style>



<div class='container' style="width:90%">
<div class="row">
<div class="col-sm-2">
	<div class="panel">
		    <div class="panel-heading" style="border-top:solid #0096eb 20px;border-bottom:solid #c3c3c3 1px; border-left:solid #c3c3c3 1px;border-right:solid #c3c3c3 1px;color:black">

				<i class="bi bi-bar-chart-line-fill" style="font-size: 50px; color:#0096eb"></i>
        <span id="clientes" style="font-size: 25px;"><?php echo @decimal(mysql_result($ventas, 0, "total"))?></span>
        <br><a href="../pos/ventas.php" style="color:black; font-size:20px; font-weight: lighter;">VENTAS</a>
 <hr>
    
   <a href="../drink/vender.php"  style="color:#0096eb; font-size:16px"><b>Facturar</b></a>
  </div>



				</div>

</div>


<div class="col-sm-2">
	<div class="panel">
		    <div class="panel-heading" style="border-top:solid #ffb335 20px;border-bottom:solid #c3c3c3 1px; border-left:solid #c3c3c3 1px;border-right:solid #c3c3c3 1px;color:black">

				<i class="bi bi-calendar2" style="font-size: 50px; color:#ffb335"></i>
        <span id="ventashoy" style="font-size: 25px;"><?php echo @decimal(mysql_result($ventas_pendiente, 0, "total"))?></span>
        <br><strong>CUENTAS ABIERTAS</strong>
 <hr>
     <a href="../pos/ventas.php" style="color:#0096eb; font-size:20px;"><b><i class="bi bi-clipboard2-data"></i> Ver ventas</b></a>

  </div>



				</div>

</div>


<div class="col-sm-3">
  <div class="panel">
         <div class="panel-heading" style="border-top:solid #ffb335 20px;border-bottom:solid #c3c3c3 1px; border-left:solid #c3c3c3 1px;border-right:solid #c3c3c3 1px;color:black">

        <i class="fa fa-box" style="font-size: 50px; color:#ffb335"></i>
        <span id="prestamos" style="font-size: 16px;"><?php echo @decimal($inventario)?>

        <br> <strong>COSTO INVENTARIO</strong> | 
				<a href='#' onClick="openNuevoProducto()" style="color:black">Crear nuevo</a>
        <br><a href='#' onClick="openInventario()" style="color:black">Imprimir</a>
  </span>

 <hr>
     <a href="../productos/productos.php" style="color: #0096eb; font-size: 20px;"><b><i class="bi bi-clipboard2-data"></i> Ver inventario</b>
     <i class="glyphicon glyphicon-circle-arrow-right pull-right" style="font-size: 20px;"></i></a>

 </div>


        </div>
</div>

<div class="col-sm-2">
	<div class="panel">
        <div class="panel-heading" style="border-top:solid #ffb335 20px;border-bottom:solid #c3c3c3 1px; border-left:solid #c3c3c3 1px;border-right:solid #c3c3c3 1px;color:black">



        <i class="bi bi-cash-coin" style="font-size: 50px; color:#ffb335"></i>
        <span id="cobros" style="font-size: 25px;"><?php echo @decimal(mysql_result($cobros, 0, "total"))?></span>

        <br><strong>PAGOS DE CREDITO</strong>
<hr>
     <a href="../pos/ingresos.php" style="color: #0096eb;; font-size:20px;"><b><i class="bi bi-clipboard2-data"></i> Ver pagos</b>
     <i class="glyphicon glyphicon-circle-arrow-right pull-right" style="font-size: 20px;"></i></a>
  </div>



        </div>
</div>

<div class="col-sm-2">
	<div class="panel">
          <div class="panel-heading" style="border-top:solid #ffb335 20px;border-bottom:solid #c3c3c3 1px; border-left:solid #c3c3c3 1px;border-right:solid #c3c3c3 1px;color:black">


        <i class="bi bi-clipboard2-x" style="font-size: 50px; color:#ffb335"></i>
        <span id="cuotas" style="font-size: 25px;"><?php echo @decimal($cancelaciones)?></span>

        <br><strong>CANCELACIONES</strong>
<hr>
     <a href="../drink/cancelaciones.php" style="color:#0096eb; font-size: 20px"><b><i class="bi bi-clipboard2-data"></i> Ver</b>
     <i class="glyphicon glyphicon-circle-arrow-right pull-right" style="font-size: 20px;"></i></a>
  </div>



        </div>
</div>
</div>

<div class="row">
  <div class="col-md-6">

            <span class="h5 text-info font-weight-light text-center"><center>Historico de ventas por dia </center></span>

    <canvas id="myChart2" height="210">


    </canvas>
 </div>

  <div class="col-md-6">
             <span class="h5 text-info font-weight-light text-center"><center>Operaciones de caja</center></span>

  <canvas id="myChart1" height="250" width="360" class="chartjs-render-monitor" style="display: block;">

  </canvas>
  </div>

    </div>
<script type="text/javascript">
function openNuevoProducto() {
		window.open('../productos/productos2.php?from_pos=1','_blank', 'toolbar=no,scrollbars=no,resizable=no,top=500,left=500,width=500,height=850');
		opener.location.reload()
}

function openInventario() {
		window.open('../productos/inventario.php?from_pos=1','_blank', 'toolbar=no,scrollbars=no,resizable=no,top=1000,left=500,width=500,height=850');
		opener.location.reload()
}



dias()
cobro(100,200,300)
function dias(){
var ctx = document.getElementById('myChart2').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado','Domingo'],
        datasets: [{
            label: 'Distribucion ventas diarias en esta semana',
            data: [<?php echo $datos["lunes"]?>, <?php echo $datos["martes"]?>, <?php echo $datos["miercoles"]?>, <?php echo $datos["jueves"]?>, <?php echo $datos["viernes"]?>, <?php echo $datos["sabado"]?>,<?php echo $datos["domingo"]?>],
            backgroundColor: [
                "#0096eb",
                "#0096eb",
               "#0096eb",
                "#0096eb",
                "#0096eb",
                "#0096eb",
                "#0096eb"
            ],
            borderColor: [
                 "#0096eb",
                "#0096eb",
               "#0096eb",
                "#0096eb",
                "#0096eb",
                "#0096eb",
                "#0096eb"
            ],
            borderWidth: 0
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
});
}

function cobro(abono,redito,mora){
var ctx = document.getElementById('myChart1').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'pie',
    data: {
        labels: ['Ingresos', 'Egresos', 'Ventas'],
        datasets: [{
            label: '# of Votes',
            data: [abono, redito, mora],
            backgroundColor: [
                "#5EB246",
                '#535b63',
                '#f6cc47'

            ],
            borderColor: [
                 "#5EB246",
                '#535b63',
                '#f6cc47'

            ],
            borderWidth: 0
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
});
}

</script>
  </div>

</div>

<!--Modal productos-->

<div class="modal" id="modalLista"  tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg"  role="document" >
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
       <label for="buscar"> Buscar </label>
        <input type="text" id="buscar" onKeyUp="mostrar()" class="form-control form-control-sm mb-2" placeholder="buscar producto...">
       <div class="table-responsive" id="lista">


       </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
<!--/Modal productos-->






<!--Modal load-->

<div class="modal" id="loading"  tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm"  role="document" >
    <div class="modal-content">

      <div class="modal-body">
       <div id="ld">

       </div>
      </div>

    </div>
  </div>
</div>
<!--/Modal load-->


<?php
   include("../footer.php");
  ?>
</body>

</html>

<script type="text/javascript">


    $("#lb_usuario_activo").html(myUser);
     $("#lb_fecha").html(fecha_actual_normal());
     localStorage.setItem("empresa","<?php echo $empresa?>");
     localStorage.setItem("direccion","<?php echo $direccion?>");
     localStorage.setItem("telefono","<?php echo $telefono?>");
     localStorage.setItem("rnc","<?php echo $rnc?>");
	 get_compra()
     function get_compra() {
	 var hoy = moment().format('YYYY-MM-DD')

    var sql = "select * from compra_factoria where fecha_entrega<='" + hoy + "'"
    _enviar("../php/traer_datos.php", {
        "sql": sql
      })
      .done(function(data) {
        var rs = JSON.parse(data);

        for (var i in rs.data) {

		   noti("info",rs.data[i].productor,"Produccion pendientes de pago",);

        }

      })
  }

</script>
