<?php
$usuario = $_COOKIE["usuario"];
$nivel = $_COOKIE["nivel"];
if($nivel=="Master"){$usuario="";}

if($nivel=="Vendedor"){
	echo "<script>window.location.href='../api/main.php'</script>";
}

if($nivel!="Master"){
	echo "<script>window.location.href='menu2.php'</script>";
}

if($nivel=="Supervisor"){
	echo "<script>window.location.href='../creditos_cobrador.php'</script>";
}


if($nivel=="Asistente"){
	echo "<script>window.location.href='../api/main.php'</script>";
}


 include("../header.php");

 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());
 
 $recargas = @mysql_query("select sum(monto) as total from recargas where fecha='$hoy' and estado='Realizada' AND tipo!='remesa'") or die(mysql_error());
   $creditos = @mysql_query("select sum(total) as total from ventas where fecha='$hoy' and estado='Cerrada' and balance>0") or die(mysql_error());

  $remesas = @mysql_query("select sum(recibe) as total from recargas where fecha='$hoy'") or die(mysql_error());
  $contado = @mysql_query("select sum(total) as total from ventas where fecha='$hoy' and termino='Efectivo' and estado!='Anulada'") or die(mysql_error());
 $credito = @mysql_query("select sum(total) as total from ventas where fecha='$hoy' and termino='Credito' 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());
  $ingresos = @mysql_query("select sum(monto_pesos) as total from ingresos where fecha='$hoy'") or die(mysql_error());
 $vencidas = @mysql_query("select sum(total) as total from ventas where vence<='$hoy' and vence!='0000-00-00' and estado!='Anulada' and estado='Cerrada' and termino='Credito'") or die(mysql_error());
$cvencidas = @mysql_query("select count(*) as c from ventas where vence<='$hoy' and vence!='0000-00-00' and estado!='Anulada' and estado='Cerrada' and termino='Credito'") or die(mysql_error());

 $devoluciones = @mysql_query("select sum(importe) as total from devoluciones where fecha='$hoy'") or die(mysql_error());
 $gastos = @mysql_query("select sum(monto) as total from egresos where fecha='$hoy'") or die(mysql_error());
 $pedidos = @mysql_query("select sum(total) as total from ventas where fecha='$hoy' and estado='Pedido'") or die(mysql_error());
 $inventario = @mysql_query("select * from productos") or die(mysql_error());
 
  $ccobros = @mysql_query("select sum(monto) as total from pagos_credito where fecha='$hoy'") or die(mysql_error());
  $abonopesos = @mysql_query("select sum(monto) as total from pagos_credito where fecha='$hoy' and moneda='PESOS'") or die(mysql_error());
  $abonodolar = @mysql_query("select sum(monto) as total from pagos_credito where fecha='$hoy' and moneda='USD'") or die(mysql_error());
  $abonogourde = @mysql_query("select sum(monto) as total from pagos_credito where fecha='$hoy' and moneda='GOURDE'") or die(mysql_error());







 while($r000=mysql_fetch_array($inventario)){
            		if($r000["stock"] and $r000["descripcion"]=="MONCASH"){
						          $costo_moncash+=$r000["stock"];

					}

          if($r000["stock"] and $r000["descripcion"]=="NATCASH"){
						          $costo_natcash+=$r000["stock"];

					}


 }
 
 //$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");
 
 
  
  $params = mysql_query("select * from parametros") or die(mysql_error());
  $default_menu_pos = @mysql_result($params, 0, "default_menu_pos");

?>  
<style>

body{
    background: #f5f5f5;
}
  
    .text-verde{
	color:#4ABC06;

}

#lista-entrada::-webkit-scrollbar{
	width:7px;
}

#kardex th, td{
	font-size:9px;
}
</style> 



 <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>

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

                <i class="bi bi-bar-chart-line-fill" style="font-size: 50px; color:#424983"></i>
        <span id="clientes" style="font-size: 25px;"><?php echo @decimal(mysql_result($remesas, 0, "total"))?></span>
        <br><a href="../api/lista_recargas_hoy.php" style="color:black; font-size:20px; font-weight: lighter;">REMESAS</a><br>
        <span id="clientes" style="font-size: 14px;"><b>Contado HTG:</b>  <?php echo @decimal(mysql_result($contado, 0, "total"))?>| <b>Credito HTG:</b> <?php echo @decimal(mysql_result($credito, 0, "total"))?></span>
 <hr>
    
   <a href="../api/main.php"  style="color:#424983; font-size:16px"><b>Facturar remesa</b></a>
  </div>



                </div>

</div>



<div class="col-sm-3">
  <div class="panel">
            <div class="panel-heading" style="border-top:solid #c3c3c3 1px;border-bottom:solid #c3c3c3 1px; border-left:solid #ffb335 20px;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;">MONCASH: <?php echo @decimal($costo_moncash)?> | NATCASH: <?php echo @decimal($costo_natcash)?>

        <br> <b class="text-ubuntu">BALANCE EN CHIP</b> 
              


<script>
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()
}
 
</script>

 <hr>
     <a href="../productos/productos.php" style="color: #47b942; 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 #c3c3c3 1px;border-bottom:solid #c3c3c3 1px; border-left:solid #fa4000 20px;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($gastos, 0, "total"))?></span>
        <br><strong>GASTOS</strong>
 <hr>
     <a href="../pos/egresos.php" style="color:#47b942; font-size:20px;"><b><i class="bi bi-clipboard2-data"></i> Ver gastos</b></a>

  </div>



                </div>

</div>




<div class="col-sm-2">
    <div class="panel">
            <div class="panel-heading" style="border-top:solid #c3c3c3 1px;border-bottom:solid #c3c3c3 1px; border-left:solid #47b942 20px;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 @(mysql_result($ccobros, 0, "c"))?></span><br>
        <span id="clientes" style="font-size: 14px;"><b>RD$:</b>  <?php echo @decimal(mysql_result($abonopesos, 0, "total"))?>| <b>USD$:</b> <?php echo @decimal(mysql_result($abonodolar, 0, "total"))?></span><br>
		 <span id="clientes" style="font-size: 14px;"><b>HTG:</b>  <?php echo @decimal(mysql_result($abonogourde, 0, "total"))?>
		 
        <br><strong>PAGOS DE CREDITO</strong>
		
		
		
<hr>
     <a href="../pos/ingresos_creditos.php" style="color: #47b942;; 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 #c3c3c3 1px;border-bottom:solid #c3c3c3 1px; border-left:solid #424983 20px;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(mysql_result($devoluciones, 0, "total"))?></span>

        <br><strong>ANULACIONES</strong>
<hr>
     <a href="../clientes/anuladas.php" style="color:#47b942; 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>

<?php if($default_menu_pos=="complete"){?>
<div class="row">

<div class="col-md-3">
 <div class="panel panel-default" style="height:250px;">
   <div class="panel-body">
   <center>
     <span class="text-verde h4">PRODUCTOS AGOTADOS</span>
	 <hr>
	<div id="detalle_agotados">
	  <?php 
	  include_once 'productos_agotados.php';
	  echo "<span class='text-rojo h1'>".decimal($agotados)."</span><br>";
	  /////echo "<span class='text-primary h4'><b>RD$".decimal($precio_)."</b></span>";
	 ?>
	</div>
	 <br><br>
	 <a class="text-primary" href="../inventario/agotados.php">Verificar</a>
	 
	 
   </center>
   
   </div>
 </div>

</div>




<div class="col-md-3">
 <div class="panel panel-default" style="height:250px;">
   <div class="panel-body">
   <center>
     <span class="text-verde h4">VERIFICADOR DE PRECIOS</span>
	 <hr>
	 <img src="../img/barcode.jpg" width="100" >
	 
	 <br><br>
	 <form action="" method="POST">
	 <input  type="text" id="txt_codigo" name="txt_codigo" class="form-control" placeholder="Codigo de barras..">
	
	
	 </form>
	<div id="detalle_precio">
	  <?php 
	  include_once 'check_precio.php';
	  echo "<span class='text-primary h5'>".($descripcion)."</span><br>";
	  echo "<span class='text-primary h4'><b>RD$".decimal($precio_)."</b></span>";
	 ?>
	</div>
  </center>
   
   </div>
 </div>

</div>



<div class="col-md-3">
 <div class="panel panel-default" style="height:250px;">
   <div class="panel-body">
   <center>
   <img class="icon3" src="../img/stock.jpg">
     <span class="text-verde h5">MERCANCIAS RECIBIDAS HOY</span>
	 
	 <div id="lista-entrada" class="table-responsive" style="overflow-y:scroll; height:120px">
	 
	  <?php 
	  include_once 'mercancias_recibidas.php';
	  
	 ?>
	
	 
	 
	 <br>
	
  
   </div>
   </center>
   
 </div>

</div>
</div>




<div class="col-md-3">
 <div class="panel panel-default" style="height:250px;">
   <div class="panel-body">
   <center>
   
     <span class="text-verde h5">FACTURAS VENCIDAS</span>
	 <hr>
	 <img src="../img/factura_vencida.jpg" width="150" >
	 
	 <br>
	 	<div id="detalle_precio">
	  <?php 
	  ////include_once 'ventas_vencidas.php';
	  
	  echo "<span class='text-rojo'><b>(".@mysql_result($cvencidas, 0, 'c').") RD$: ".decimal(@mysql_result($vencidas, 0, 'total'))."</b></span><br>";
	  echo "<a class='text-primary' href='../pos/vencidas.php'> Ver facturas vencidas</a></span>";
	 ?>
	</div>
	
   </center>
   
   </div>
 </div>

</div>
</div>

<?php }?>





<div class="row">


  <div class="col-md-6">
        <div class="panel panel-default">
		<div class="panel-heading">
            <span class="h5 text-info font-weight-light text-center"><center>Historico de ventas por dia </center></span>
		</div>
<div class="panel-body">
    <canvas id="myChart2" height="210">
        
        
    </canvas>
 </div> 
</div>
</div>
  <div class="col-md-6">
  <div class="panel panel-default">
		<div class="panel-heading">
             <span class="h5 text-info font-weight-light text-center"><center>Operaciones de caja</center></span>
       </div>
<div class="panel-body">  
  <canvas id="myChart1" height="250" width="360" class="chartjs-render-monitor" style="display: block;">
  
  </canvas>
  
  </div>
  
    </div>
	    </div>
		
 </div>
  </div>
<script type="text/javascript">





dias()
cobro(<?php echo @mysql_result($abonopesos,0,"total")?>,<?php echo @mysql_result($gastos,0,"total")?>)
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: [
                "#00a65a",
                "#00a65a",
               "#00a65a",
                "#00a65a1",
                "#00a65a",
                "#00a65a",
                "#00a65a"
            ],
            borderColor: [
                 "#0f4c81",
                "#0f4c81",
               "#0f4c81",
                "#0f4c81",
                "#0f4c81",
                "#0f4c81",
                "#5EB246"
            ],
            borderWidth: 0
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
});
}

function cobro(ingresos, gastos){
var ctx = document.getElementById('myChart1').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'pie',
    data: {
			labels: ['Ingresos', 'Gastos'],
			datasets: [{
            label: '# Operaciones',
            data: [ingresos, gastos],
            backgroundColor: [
                "#5EB246",
                '#535b63',
                
               
            ],
            borderColor: [
                 "#5EB246",
                '#535b63',
                
                
            ],
            borderWidth: 0
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
});
}

</script>
  </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-->


<!--/Modal aviso pago-->
<div class="modal" id="modalPago" style="opacity:3px"  tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop="">

  <div class="modal-dialog modal-lg"  role="document" >

    <div class="modal-content">



      <div class="modal-body">

       <div id="ld">

         <center>

         <img src="../aviso_pago.png"><br>

        

         </center>

       </div>
 <hr>
 <center>
 <a href="#" data-dismiss="modal" class="text-primary"><i class="bi bi-check"></i> De acuerdo</a>
</center>  
  </div>

      

    </div>

  </div>

</div>  

<!--/Modal aviso pago-->



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

        }

      })
  }
//$("#txt_codigo").focus();
///$("#txt_codigo").select();
setTimeout(function(){
	$("#txt_codigo").val("");
	$("#detalle_precio").html("");
},5000)


estado();
 function estado(){
	 var db = localStorage.getItem("db");
   //$("#msg").html("<p>Conectando con el servidor de datos, espere unos segundos...</p>")
   $.get("http://217.71.203.122/App_planes/estado.php?db="+db,function(data,st){

     if(st=="success"){
      var datos = JSON.parse(data); 
      //alert(datos.estado);
      var estado = datos.estado;
	  ///alert(estado);
      if(estado=="inactivo"){
		   $("#modalPago").modal("show");
      }
	  
     }
   })
 }	
</script>