<!DOCTYPE html>
<?php
include '../../session.php';
?>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <?php
        require '../../conexion.php';
        require '../../menu/css_lte.ctp';
        ?>
    </head>
    <body class="hold-transition skin-blue sidebar-mini">
        <div class="wrapper">
            <?php require '../../menu/cabecera.ctp'; ?><!--BARRA DE HERRAMIENTAS-->
            <?php require '../../menu/izquierda.ctp'; ?><!--BARRA DE HERRAMIENTAS-->

            <div class="content-wrapper">
                <div class="content">
                    <!--consultas-->
                    <div class="row">
                        <?php
                        $aperturas = consultas::get_datos("select * from v_apertura where usu_cod = " . $_SESSION['usu_cod'] . " and estado != 'CERRADO' ");
                        if ($aperturas) {
                            $idapertura  = $aperturas[0]['id_apecie'];
                        } else {
                            $idapertura  = null;
                        }
                        ?>
                        <!--impresion del titulo de la pagina-->
                        <div class="col-lg-12">
                            <h3 class="page-header">Apertura Cierre de Cajas - <?php echo $_SESSION['nombres'] ?> 
                                <a <?= isset($idapertura) ? "disabled" : "data-toggle='modal' data-target='#registrar' " ?>
                                    class="btn btn-primary btn-circle pull-right" 
                                    rel="tooltip" data-title="Registrar Apertura">
                                    <i class="fa fa-plus"></i> AGREGAR
                                </a>                      
                            </h3>
                        </div>     
                    </div>
                    <!--consultas-->

                    <!--datos-->
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    Datos
                                </div>                     
                                <div class="panel-body">
                                    <?php
                                    if (!empty($aperturas)) {
                                        ?>    
                                        <div>
                                            <table id="example1" width="100%" class="table table-striped table-bordered table-hover">
                                                <thead>
                                                    <tr>
                                                        <th class="text-center">#</th>
                                                        <th class="text-center">Fecha Apertura</th> 
                                                        <th class="text-center">Monto Apertura</th> 
                                                        <th class="text-center">Caja</th> 
                                                        <th class="text-center">Monto Efectivo</th>
                                                        <th class="text-center">Monto Cheque</th>
                                                        <th class="text-center">Monto Tarjeta</th>
                                                        <th class="text-center">Estado</th>
                                                        <th class="text-center">Acciones</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="buscar">
                                                    <?php foreach ($aperturas as $apertura) { ?> 
                                                        <tr>
                                                            <td class="text-center"><?= $apertura['id_apecie']; ?></td>
                                                            <td class="text-center"><?php echo date('d/m/Y', strtotime($apertura['apertura_fecha'])); ?></td>
                                                            <td class="text-center"><?php echo number_format($apertura['apertura_monto'], 0, ',', '.'); ?></td>
                                                            <td class="text-center"><?php echo $apertura['caja_descrip']; ?></td>
                                                            <td class="text-center"><?php echo number_format($apertura['monto_efectivo'], 0, ',', '.'); ?></td>
                                                            <td class="text-center"><?php echo number_format($apertura['monto_cheque'], 0, ',', '.'); ?></td>
                                                            <td class="text-center"><?php echo number_format($apertura['monto_tarjeta'], 0, ',', '.'); ?></td>
                                                            <td class="text-center"><?php echo $apertura['estado']; ?></td>
                                                            <td class="text-center">

                                                                <?php if ($apertura['estado'] != "CERRADO") { ?>
                                                                    <a onclick="cerrar(<?php
                                                                    echo "'" . ($apertura['monto_efectivo'] + $apertura['monto_cheque'] + $apertura['monto_tarjeta']) . "_" .
                                                                    $apertura['id_apecie'] . "_" . $apertura['id_caja'] . "_" .
                                                                    $apertura['usu_cod'] . "_" .
                                                                    $apertura['apertura_monto'] . "'";
                                                                    ?>)"
                                                                       class="btn btn-sm btn-danger" rel='tooltip' data-title="Cerrar Caja" 
                                                                       data-toggle="modal" data-target="#cerrar">
                                                                        <span class="glyphicon glyphicon-log-in"></span>
                                                                    </a>

                                                                <?php } ?>

                                                                <a href="arqueo_caja.php?vcod=<?= $apertura['id_apecie'] ?>" target="_blank" 
                                                                   class="btn btn-sm btn-success" rel='tooltip' data-title="Imprimir Arqueo">
                                                                    <span class="glyphicon glyphicon-print"></span>
                                                                </a>

                                                            </td>
                                                        </tr>
                                                    <?php } ?>
                                                </tbody>
                                            </table>                         
                                        </div>
                                    <?php } else { ?>
                                        <div class="alert alert-info alert-dismissable">
                                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">
                                                &times;</button>
                                            <span class="glyphicon glyphicon-info-sign"></span><strong> No posee ninguna apertura hecha a la fecha....!</strong>
                                        </div>
                                    <?php } ?>  
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--datos-->

                    <!--registrar-->
                    <div id="registrar" class="modal fade" role="dialog">
                        <div class="modal-dialog">
                            <!-- Modal content-->
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" 
                                            data-dismiss="modal" arial-label="Close">x</button>
                                    <h4 class="modal-title"><i class="fa fa-plus"></i> <strong>Registrar Apertura</strong></h4>
                                </div>
                                <form action="apertura_control.php" method="post" accept-charset="utf-8" class="form-horizontal">
                                    <div class="panel-body se">
                                        <input type="hidden" name="accion"  value="1">
                                        <input type="hidden" name="vcod" value="0"/> 
                                        <input type="hidden" name="moncierre" value="0"/> 
                                        <input type="hidden" name="pagina" value="apertura_cierre.php">

                                        <div class="row">

                                            <div class="col-md-6" style="left: 100px">
                                                <label class="control-label">Monto Inicial:</label>
                                                <input type="number" required="" placeholder="Ingrese monto" min="0" class="form-control" 
                                                       value="0" name="apemonto" autofocus="" style="width: 50%">
                                            </div>


                                            <div class="col-md-6">
                                                <label class="control-label">Caja:</label>
                                                <?php
                                                $cajas = consultas::get_datos("select * from cajas where cod_suc = " . $_SESSION['cod_suc'] . " and id_caja not in(select id_caja from apertura_cierre where cod_suc = " . $_SESSION['cod_suc'] . " and cierre_fecha is null)");
                                                ?> 
                                                <br>
                                                <select name="vcaja" class="form-control select2" style="width: 200px">
                                                    <?php
                                                    if (!empty($cajas)) {
                                                        foreach ($cajas as $caja) {
                                                            ?>
                                                            <option value="<?php echo $caja['id_caja']; ?>">
                                                                <?php echo $caja['caja_descrip']; ?></option>
                                                            <?php
                                                        }
                                                    } else {
                                                        ?>
                                                        <option value="0">Debe insertar una caja</option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="reset" data-dismiss="modal" class="btn btn-default">
                                            <i class="fa fa-close"></i> Cerrar</button>
                                        <button type="submit" class="btn btn-primary pull-right">
                                            <i class="fa fa-floppy-o"></i> Registrar</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <!--registrar-->

                    <!--borrar-->
                    <div id="cerrar" class="modal fade" tabindex="-1" role="dialog">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                    <h4 class="modal-title custom_align" id="Heading">Atenci&oacute;n!!!</h4>
                                </div>
                                <div class="modal-body">
                                    <div class="alert alert-warning" id="confirmacion"></div>
                                </div>
                                <div class="modal-footer">
                                    <a id="si" role="button" class="btn btn-primary" ><span class="glyphicon glyphicon-ok-sign"></span> Si</a>
                                    <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button>
                                </div>
                            </div>
                        </div>
                    </div> 
                    <!--fin-->
                </div>
            </div>
        </div> 
        <!--archivos js-->  
        <?php require '../../menu/js_lte.ctp'; ?>
        <!--archivos js-->  
        <script>
            function cerrar(datos) {
                var dat = datos.split("_");
                $('#si').attr('href', 'apertura_control.php?vcod=' + dat[1] +
                        '&vcaja=' + dat[2] +
                        '&usu_cod=' + dat[3] +
                        '&apemonto=' +dat[4] +
                        '&moncierre=' + dat[0] +
                        '&accion=2&pagina=cierre_caja.php?vcod=' + dat[1]);
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                Desea Cerrar la Caja <i><strong>' + dat[2] + '</strong></i>?');
   
            }
        </script>
    </body>
</html>
