<?php
include 'is_logged.php';
require_once "../db.php";
require_once "../php_conexion.php";
require_once "../funciones.php";

$user_id = $_SESSION['id_users'];
$id_moneda = get_row('perfil', 'moneda', 'id_perfil', 1);
$fecha_actual = date('Y-m-d');
$hora_actual = date('H:i');

$totalEfectivo = 0;
$totalCheque = 0;
$totalTrans = 0;
$totalCredito = 0;
$totalVenta = 0;

/* Control de Caja */
$caja = mysqli_query($conexion, "select LAST_INSERT_ID(caja_id) as last_id, s_inicial, estado_caja from detalle_caja where estado_caja = 0");
$rw = mysqli_fetch_array($caja);
$id_caja = $rw['last_id'];
$s_inicial = $rw['s_inicial'];
/*fin */
$orderSql = "SELECT * FROM facturas_ventas WHERE control_caja = $id_caja ;
";
$orderQuery = $conexion->query($orderSql);

while ($row = $orderQuery->fetch_assoc()) {
    switch ($row['condiciones']) {
        case 2:
            $totalCheque += $row['monto_factura'];
            break;
        case 3:
            $totalTrans += $row['monto_factura'];
            break;
        case 4:
            $totalCredito += $row['monto_factura'];
            break;
        case 1:
            $totalEfectivo += $row['monto_factura'];
            break;
    }
    $totalVenta += $row['monto_factura'];
}

$orderSql = "SELECT * FROM facturas_compras WHERE control_caja = $id_caja and id_users_factura='$user_id' and condiciones=1";
$orderQuery = $conexion->query($orderSql);

$totalCompra = 0;

while ($orderResult = $orderQuery->fetch_assoc()) {
    $totalCompra += $orderResult['monto_factura'];
}
?>
<div class="row">
	<div class="col-md-6">
		<div class="form-group">
			<label for="fecha" class="control-label">FECHA:</label>
			<input type="date" class="form-control UpperCase" id="fecha" name="fecha"  autocomplete="off" value="<?php echo $fecha_actual; ?>" required>
			<input id="id_caja" name="id_caja" value="<?php echo $id_caja; ?>" type='hidden'>
		</div>
	</div>
	<div class="col-md-6">
		<div class="form-group">
			<label for="hora" class="control-label">HORA:</label>
			<input type="time" class="form-control UpperCase" id="hora" name="hora"  autocomplete="off" value="<?php echo $hora_actual; ?>" required>
		</div>
	</div>
</div>
<div class="row">
	<div class="col-md-6">
		<div class="form-group">
			<label for="inicial" class="control-label">CAJA INICIAL:</label>
			<input type="text" style=" background-color:#E5E8E8; border-radius: 5px; border: 1px solid #39c; text-align: center; font-size: 25px;" class="form-control UpperCase number-only" id="inicial" name="inicial"  autocomplete="off" title="Ingresa sólo números con 0 ó 2 decimales" value="<?php echo formato($s_inicial); ?>" readonly>
		</div>
	</div>
	<div class="col-md-6">
		<div class="form-group">
			<label for="efectivo" class="control-label">EFECTIVO:</label>
			<input type="text" style=" border-radius: 5px; border: 1px solid #39c; text-align: center; font-size: 30px;" class="form-control UpperCase number-only" id="efectivo" name="efectivo"  autocomplete="off" title="Ingresa sólo números con 0 ó 2 decimales" placeholder="0.00" required>
		</div>
	</div>

</div>
<div class="row">
	<div class="col-md-6">
		<div class="form-group">
			<label for="venta" class="control-label">VENTAS:</label>
			<input type="text" style=" background-color:#D1F2EB; border-radius: 5px; border: 1px solid #39c; text-align: center; font-size: 18px;" class="form-control UpperCase number-only" id="ventas" name="ventas"  autocomplete="off" value="<?php echo $id_moneda . ' ' . formato($totalEfectivo); ?>" readonly>
		</div>
	</div>
	<div class="col-md-6">
		<div class="form-group">
			<label for="compras" class="control-label">COMPRAS:</label>
			<input type="text" style=" background-color:#F9E79F; border-radius: 5px; border: 1px solid #39c; text-align: center; font-size: 18px;" class="form-control UpperCase number-only" id="compras" name="compras"  autocomplete="off" value="<?php echo formato($totalCompra); ?>" readonly>
		</div>
	</div>

</div>
<div class="row">
	<div class="col-md-6">
		<div class="form-group">
			<label for="sub" class="control-label">SUB-TOTAL EFECTIVO:</label>
			<input type="text" style=" background-color:#E5E8E8; border-radius: 5px; border: 1px solid #39c; text-align: center; font-size: 25px;" class="form-control UpperCase number-only" id="sub" name="sub"  value="<?php echo formato($totalVenta); ?>" readonly>
		</div>
	</div>
	<div class="col-md-6">
		<div class="form-group">
			<label for="tot" class="control-label">TOTAL:</label>
			<input type="text" style=" background-color:#FADBD8; border-radius: 5px; border: 1px solid #39c; text-align: center; font-size: 25px;" class="form-control UpperCase number-only" id="tot" name="tot"  autocomplete="off"   value="<?php echo formato($totalVenta + $s_inicial); ?>" readonly>
		</div>
	</div>

</div>
<script type="text/javascript">
    $(function(){

  $('.number-only').keyup(function(e) {
        if(this.value!='-')
          while(isNaN(this.value))
            this.value = this.value.split('').reverse().join('').replace(/[\D]/i,'')
                                   .split('').reverse().join('');
    })
    .on("cut copy paste",function(e){
        e.preventDefault();
    });

});
  </script>
