<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php'); 
include ('totales_caja.php'); 
?>
<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th width="1%">N°</th>
            <th width="8%">FECHA</th>
            <th width="">USUARIO</th>
            <th width="1%">BASE</th>
            <th width="1%">EFECTIVO</th>
            <th width="1%">CREDITO</th>
            <th width="1%">TARJETA</th>
            <th width="1%">TRANSF</th>
            <!--<th width="1%">CRUZADAS</th>-->
            <th width="1%">COMBINADO</th>
            <th width="1%">TOTAL_V</th>
            <!--<th width="6%">E_CRUCES</th> -->
            <th width="1%">ABONOS</th>
            <th width="1%">EGRESOS</th>
            <th width="1%">CIERRE</th>
            <th width="8%"></th>
        </tr>
    </thead>
    <?php 
        $registros=mysqli_query($con,"SELECT *, DATE_FORMAT(`mca_inicio`,'%d-%m-%Y %H:%i:%s') as mca_inicio FROM movimiento_caja where empresa='$empresa' and sucursal='$sucursal' and apertura='$apertura' ") or die(mysqli_error($con));
        $reg=mysqli_fetch_array($registros);
    ?>
        <tr style=" border: orange 3px solid; ">
            <td><?php echo $apertura; ?></td>
            <td><?php echo $reg['mca_inicio']; ?></td>
            <td><?php
                $mca_usuario=$reg['mca_usuario']; 
                $datos1 = mysqli_query($con,"SELECT * FROM usuario WHERE usu_id='$mca_usuario'") ;
                $fila1 = mysqli_fetch_array( $datos1 );
                echo $fila1['usu_nombre']; ?></strong></td>
            <td>
                <a href="" class="base" data-toggle="modal"><p style="color:#07650F"align="right"><i class="fas fa-eye"><?php echo number_format($mca_base,0); ?></i></p></a>
            </td>
            <td>
                <a href="" class="contado" data-toggle="modal"><p style="color:#07650F" align="right"><i class="fas fa-eye"><?php echo number_format($ventas_contado,0);?></i></p></a>
            </td>
            <td>
                <a href="" class="credito" data-toggle="modal"><p style="color:#FF7514" align="right"><i class="fas fa-eye"><?php echo number_format($ventas_credito,0);?></i></p></a>
            </td>
            <td>
                <a href="" class="tarjeta" data-toggle="modal"><p style="color:#FF7514" align="right"><i class="fas fa-eye"><?php echo number_format($ventas_targeta,0);?></i></p></a></td>
            </td> 
            <td>
                <a href="" class="transferencia" data-toggle="modal"><p style="color:#FF7514" align="right"><i class="fas fa-eye"><?php echo number_format($ventas_transferencia,0);?></i></p></a></td>
            <!--<td>
                <a href="" class="cruces" data-toggle="modal"><p style="color:#FF7514" align="right"><i class="fas fa-eye"><?php echo number_format($ventas_cruce,0);?></i></p></a>
            </td>-->
            <td>
                <a href="" class="combinado" data-toggle="modal"><p style="color:#FF7514" align="right"><i class="fas fa-eye"><?php echo number_format($ventas_combinado);?></i></p></a>
            </td>
            <td><p style="color:#000" align="right"><?php echo number_format($ventas_total,0); ?></p></td>
            <!-- <td>
                <a href="#detalles_cruces" data-toggle="modal"><p style="color:#07650F" align="right"><i class="fas fa-eye"></i> <?php echo number_format($excedente_nota,0); ?></p></a>
            </td> -->
            <td>
                <a href="" class="abonos" data-toggle="modal"><p style="color:#FF7514" align="right"><i class="fas fa-eye"><?php echo number_format($total_abonos,0); ?></i></p>
            </td>
            <td>
                <a href="" class="egresos" data-toggle="modal"><p style="color:#FF0000" align="right"><i class="fas fa-eye"><?php echo number_format($egresos,0); ?></i></p></a>
            </td>
            <td><p style="color:#000" align="right"><?php echo number_format($saldoencaja,0); ?></p></td>
            <td>
            <?php if($reg['mca_estado']==1)
            { 
            ?>
                <a href="#modificar<?php echo $reg['mca_id'];?>" title="Modificar base de apertura" class="btn btn-xs btn-info"  data-toggle="modal"><i class="btn-icon-only icon-edit"></i></a>

                <a href="#cerrar<?php echo $reg['mca_id'];?>" title="Cerrar Caja" class="btn btn-xs btn-danger"  data-toggle="modal"><i class="btn-icon-only icon-unlock-alt"></i></a>
            <?php include ('modificar_apertura.php'); ?>
            <?php } ?>
            </td>
        </tr>

 <?php 

$est=mysqli_query($con,"SELECT * FROM modulo_permiso WHERE mod_id='24' and modp_nombre='Historiales de caja' and usuario='$usu_id' ") or die(mysqli_error($con));
$est1=mysqli_fetch_array($est);

$estado=$est1['estado'];

if ($estado=='Activo') 
{
           
    $action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
    if($action == 'ajax')
    {
        include '../include/pagination.php'; 
        $page=(isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
        $per_page=10;
        $adjacents=4; 
        $offset=($page - 1) * $per_page;
        $count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM movimiento_caja where empresa='$empresa' and sucursal='$sucursal' and mca_estado='0' order by apertura desc ");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './P_pos.php';
        $sql="SELECT *, DATE_FORMAT(`mca_inicio`,'%d-%m-%Y %H:%i:%s') as mca_inicio FROM movimiento_caja where empresa='$empresa' and sucursal='$sucursal' and mca_estado='0' order by apertura desc  LIMIT $offset,$per_page";
        $query = mysqli_query($con, $sql);
        if ($numrows>0)
           {
             while ($reg=mysqli_fetch_array($query))
                {
    ?>
        <tr>
            <td><?php echo $reg['apertura']; ?></td>
            <td><?php echo $reg['mca_inicio']; ?></td>
            <td><?php
                $mca_usuario=$reg['mca_usuario']; 
                $datos1 = mysqli_query($con,"SELECT * FROM usuario WHERE usu_id='$mca_usuario'") ;
                $fila1 = mysqli_fetch_array( $datos1 );
                echo $fila1['usu_nombre']; ?></strong></td>
            <td><p style="color:#07650F" align="right"><?php echo number_format($reg['mca_base']); ?></p></td>
            <td><p style="color:#07650F" align="right"><?php echo number_format($reg['mca_ventas'],0); ?></p></td>
            <td><p style="color:#FF7514" align="right"><?php echo number_format($reg['mca_credito'],0);?></p></td>
            <td><p style="color:#FF7514" align="right"><?php echo number_format($reg['mca_tarjeta'],0); ?></p></td>
            <td><p style="color:#FF7514" align="right"><?php echo number_format($reg['mca_transferencia'],0); ?></p></td>
           <!-- <td><p style="color:#FF7514" align="right"><?php echo number_format($reg['mca_nota'],0); ?></p></td>-->
            <td><p style="color:#000" align="right"><?php echo number_format($reg['mca_combinado'],0); ?></p></td>
            <td><p style="color:#000" align="right"><?php echo number_format($reg['mca_ventas']+$reg['mca_credito']+$reg['mca_tarjeta']+$reg['mca_transferencia']+$reg['mca_nota']+$reg['mca_combinado'],0); ?></p></td>
            <!--<td><p style="color:#07650F" align="right"><?php echo number_format($reg['mca_efectivo_nota'],0); ?></p></td>-->
            <td><p style="color:#07650F" align="right"><?php echo number_format($reg['mca_abonos'],0); ?><p style="color:#07650F";></td>
            <td><p style="color:#FF0000" align="right"><?php echo number_format($reg['mca_otros_movimientos'],0); ?></p></td>
            <td><p style="color:#000" align="right"><?php echo number_format($reg['mca_valor_cierre'],0); ?></p></td>
          <!--  <td><p style="" align="right"><?php echo number_format($reg['mca_diferencia'],0); ?></p></td>-->
            <td>
               <!--  <a data-id="<?php echo $id=$reg['mca_id'];?>" title="Modificar Valor de Cierre" class="btn btn-xs btn-info cierre" data-toggle="modal" >
                <i class="btn-icon-only icon-edit"></i></a>-->
              
                <a href="tirillas/cierre.php?mca_id=<?php echo $reg['mca_id']; ?>" target="_blank" class="btn btn-xs btn-success"> <i class="btn-icon-only icon-print"></i></a>
            </td>
        </tr>
<?php } } }?>
        <tr>
            <td colspan=15>
              <span class="pull-right">
                <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
              </span>
            </td>
        </tr>
    <?php }  ?>
</table>


<script>
    $(".base").click(function(){
        $.get("./php_caja/form_base.php",function(data){
            $("#form-bas").html(data);
        });
        $('#baseModal').modal('show');
    });
</script>
<div class="modal fade" id="baseModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Base Apertura</h4>
            </div>
            <div class="modal-body">
                <div id="form-bas"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
    $(".contado").click(function(){
        $.get("./php_caja/form_contado.php",function(data){
            $("#form-con").html(data);
        });
        $('#contadoModal').modal('show');
    });
</script>
<div class="modal fade" id="contadoModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Ventas En Efectivo</h4>
            </div>
            <div class="modal-body">
                <div id="form-con"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
    $(".credito").click(function(){
        $.get("./php_caja/form_credito.php",function(data){
            $("#form-cre").html(data);
        });
        $('#creditoModal').modal('show');
    });
</script>
<div class="modal fade" id="creditoModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Ventas a Credito</h4>
            </div>
            <div class="modal-body">
                <div id="form-cre"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
    $(".tarjeta").click(function(){
        $.get("./php_caja/form_tarjeta.php",function(data){
            $("#form-tar").html(data);
        });
        $('#tarjetaModal').modal('show');
    });
</script>
<div class="modal fade" id="tarjetaModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Ventas Con tarjeta</h4>
            </div>
            <div class="modal-body">
                <div id="form-tar"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script>
    $(".transferencia").click(function(){
        $.get("./php_caja/form_transferencia.php",function(data){
            $("#form-trans").html(data);
        });
        $('#transferenciaModal').modal('show');
    });
</script>
<div class="modal fade" id="transferenciaModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Ventas Con Transferencia</h4>
            </div>
            <div class="modal-body">
                <div id="form-trans"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!--
<script>
    $(".cruces").click(function(){
        $.get("./php_caja/form_cruces.php",function(data){
            $("#form-cru").html(data);
        });
        $('#crucesModal').modal('show');
    });
</script>
<div class="modal fade" id="crucesModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Ventas con Cruces de Nota Credito</h4>
            </div>
            <div class="modal-body">
                <div id="form-cru"></div>
            </div>
        </div><!-- /.modal-content -->
  <!--   </div><!-- /.modal-dialog -->
<!-- </div><!-- /.modal -->

<script>
    $(".combinado").click(function(){
        $.get("./php_caja/form_combinado.php",function(data){
            $("#form-com").html(data);
        });
        $('#combinadoModal').modal('show');
    });
</script>
<div class="modal fade bs-example-modal-lg" id="combinadoModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Ventas Combinadas</h4>
            </div>
            <div class="modal-body">
                <div id="form-com"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

 
<script>
    $(".egresos").click(function(){
        $.get("./php_caja/form_egresos.php",function(data){
            $("#form-egre").html(data);
        });
        $('#egresosModal').modal('show');
    });
</script>
<div class="modal fade" id="egresosModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Egresos</h4>
            </div>
            <div class="modal-body">
                <div id="form-egre"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script>
    $(".abonos").click(function(){
        $.get("./php_caja/form_abonos.php",function(data){
            $("#form-abo").html(data);
        });
        $('#abonosModal').modal('show');
    });
</script>
<div class="modal fade" id="abonosModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Abonos</h4>
            </div>
            <div class="modal-body">
                <div id="form-abo"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script>
    $(".cierre").click(function(){
        id = $(this).data("id");
        $.get("./php_caja/form_cierre.php","id="+id,function(data){
        $("#form-cierre").html(data);
        });
        $('#cierreModal').modal('show');
    });
</script>
<div class="modal fade" id="cierreModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Modificar Cierre de Caja</h4>
            </div>
            <div class="modal-body">
                <div id="form-cierre"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


  <div class="modal fade" id="tabla_detalle-base" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">DETALLE ABONOS</h4>
                </div>
                <div class="modal-body">
                    <div id="detalle_bas"></div>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->