<?php
include '../../session.php';
?>
<!DOCTYPE html>
<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">   
                    <div class="row">
                        <!--impresion del titulo de la pagina-->
                        <div class="col-lg-12">
                            <h3 class="page-header">Ajuste de Inventario 
                                <a data-toggle="modal" data-target="#registrar" 
                                   class="btn btn-primary btn-circle pull-right" 
                                   rel="tooltip" data-title="Registrar">
                                    <i class="fa fa-plus"></i> AGREGAR
                                </a>                       
                            </h3>
                        </div>     
                        <!--impresion del titulo de la pagina-->
                    </div>
                    <!-- /.row -->
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    Datos
                                </div>
                                <?php
                                $ajustes = consultas::get_datos("select * from v_compras_ajuste /*where aj_fecha  = (select current_date)*/ order by aj_fecha desc;");
                                if (!empty($ajustes)) {
                                    ?>                         
                                    <!-- /.panel-heading -->
                                    <div class="panel-body">
                                        <div>
                                            <table id="example1" width="100%" 
                                                   class="table table-striped table-bordered table-hover tablas">                                            
                                                <thead>
                                                    <tr>
                                                        <th class="text-center">#</th>
                                                        <th class="text-center">Fecha</th>
                                                        <th class="text-center">Motivo</th>
                                                        <th class="text-center">Deposito</th>
                                                        <th class="text-center">Articulo</th>
                                                        <th class="text-center">Cantidad</th>
                                                        <!--th class="text-center">Estado</th-->
                                                        <!--th class="text-center">Acciones</th-->
                                                    </tr>
                                                </thead>
                                                <tbody class="buscar">
                                                    <?php foreach ($ajustes as $ajuste) { ?> 
                                                        <tr>
                                                            <td class="text-center"><?php echo $ajuste['id_ajuste']; ?></td>
                                                            <td class="text-center"><?php echo date("d/m/Y", strtotime($ajuste['aj_fecha'])); ?></td>
                                                            <td class="text-center"><?php echo $ajuste['maj_descri']; ?></td> 
                                                            <td class="text-center"><?php echo $ajuste['dep_descrip']; ?></td> 
                                                            <td class="text-left"><?php echo $ajuste['art_descri']; ?></td> 
                                                            <td class="text-center"><?php echo $ajuste['cantidad']; ?></td> 
                                                            <!--td class="text-center"><?php echo $ajuste['estado']; ?></td--> 
                                                            <td class="text-center">
                                                                <!--a onclick="anular(<?php echo "'" . $ajuste['id_ajuste'] . "'"; ?>)"
                                                                   data-toggle="modal" data-target="#delete"
                                                                   class="btn btn-xs btn-danger" rel='tooltip' 
                                                                   data-title="Anular">
                                                                    <span class="glyphicon glyphicon-remove"></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>
                                            <strong>No se encontraron registro....!</strong>
                                        </div>
                                    <?php } ?>  
                                    <!-- /.panel-body -->
                                </div>
                            </div>
                            <!-- /.panel -->
                        </div>
                        <!-- /.col-lg-12 -->
                    </div>

                    <!--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"><strong><center>Registrar Ajuste</center></strong></h4>
                                </div>
                                <form action="ajuste_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="vusu" value="<?php echo $_SESSION['usu_cod']; ?>">
                                        <input type="hidden" name="vdescri" value="Ajuste"/> 
                                        <input type="hidden" name="pagina" value="ajuste_index.php" id="vcod">
                                        <input type="hidden" name="vcod" value="0"/> 

                                        <div class="form-group">
                                            <label class="col-lg-2 col-sm-2 col-md-2 control-label">Fecha:</label>
                                            <div class="col-md-4">
                                                <input type="date" required="" placeholder="Especifique fecha" class="form-control"
                                                  value="<?php echo date("Y-m-d") ?>" readonly="">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Motivo:</label>
                                                <div class="col-lg-6 col-md-6 col-sm-6">
                                                <?php $moti = consultas::get_datos("SELECT * from motivo_ajuste /*where maj_descri='ENTRADA'*/"); ?>                                 
                                                    <select name="vmotivo" class="select2 form-control" required="" style="width: 100%" >
                                                        <?php
                                                        if (!empty($moti)) {
                                                            foreach ($moti as $mot) {
                                                                ?>
                                                                <option value="<?php echo $mot['id_majuste']; ?>">
                                                                    <?php echo $mot['maj_descri']; ?></option>
                                                                <?php
                                                            }
                                                        } else {
                                                            ?>
                                                            <option value="0">Debe insertar un registro</option>
                                                        <?php } ?>
                                                    </select>
                                                </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-2 col-sm-2 col-md-2 control-label">Deposito:</label>
                                            <div class="col-lg-6 col-md-6 col-sm-6">
                                                <?php $depositos = consultas::get_datos(" select * from deposito where cod_depo<>3 and cod_depo<>0 and cod_suc=".$_SESSION['cod_suc'].""); ?>
                                                <select name="vdep" class="form-control select2" id="depo" onchange="articulo()" onkeyup="articulo()"
                                                        style="width : 100%;" required="">
                                                    <option value="">Seleccione</option>
                                                    <?php if (!empty($depositos)) {
                                                        foreach ($depositos as $deposito) { ?>
                                                            <option value="<?php echo $deposito['cod_depo']; ?>">
                                                                <?php echo $deposito['dep_descrip']; ?>
                                                            </option>
                                                   <?php
                                                        }
                                                    } else { ?>
                                                        <option value="0">Debe insertar un deposito</option>
                                                    <?php } ?>
                                                </select>
                                            </div>                                            
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-2 col-sm-2 col-md-2 control-label">Articulo:</label>
                                            <div class="col-lg-8 col-md-8 col-sm-8">
                                                <?php $depositos = consultas::get_datos(" select * from deposito where cod_depo<>3 and cod_depo<>0 and cod_suc=".$_SESSION['cod_suc'].""); ?>
                                                <select name="vart" class="form-control select2" id="detalles"
                                                        style="width:100%;" required="">
                                                    <option value="">Seleccione un articulo</option>
                                                </select>
                                            </div>                                            
                                        </div>                                        
                                        <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Cantidad</label>  
                                            <div class="col-md-4" id="cantidad">
                                                <input type="number" required="" class="form-control" require min="1" name="vcant"
                                                       value="0"  pattern="^[0-9]+">
                                            </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="reset" data-dismiss="modal" class="btn btn-danger pull-left">
                                                <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>
                                    </div>
                                </form>
                            </div>
                        </div> 
                    </div>  
                    <!--registrar-->
                    <!--borrar-->
                    <div class="modal fade" id="delete" 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 borrar-->
                </div> 
            </div>
        </div>

        <!--archivos js-->  
        <?php require '../../menu/js_lte.ctp'; ?>


        <script>
            function articulo() {
                if (parseInt($('#depo').val()) > 0) {
                    $.ajax({
                        type: "GET",
                        url: "/farmmedi/compras/ajuste/lista_articulo_ajuste.php?vdep=" + $('#depo').val(), cache: false,
                        beforeSend: function () {
                            $('#detalles').html('<img src="/farmmedi/img/ajax-loader.gif">\n\ <strong><i>Cargando...</i></strong>');
                        },
                        success: function (msg) {
                            $('#detalles').html(msg);
                            //obtenercantidad();
                        }
                    });
                }
            }

            function obtenercantidad() {
                var dat = $('#vart').val().split("_");
                if (parseInt($('#vart').val()) > 0) {
                    $.ajax({
                        type: "GET",
                        url: "/farmmedi/compras/ajuste/lista_cantidad_ajuste.php?vart=" + dat[0] + '&vdep=' + $('#depo').val(), cache: false,
                        beforeSend: function () {
                            $('#cantidad').html('<img src="/farmmedi/img/ajax-loader.gif">\n\ <strong><i>Cargando...</i></strong>');
                        },
                        success: function (msg) {
                            $('#cantidad').html(msg);
                        }

                    });
                }
            }

            function stock() {
                var cant = parseInt($('#cantstock').val());
                if (cant > 0) {
                    if (parseInt($('#cantidad').val()) > cant) {
                        alert('SOLO HAY ' + cant + ' EN STOCK ESTE PRODUCTO');
                        $('#cantidad').val(cant);
                    }
                } else {
                    $('#cantidad').val('0');
                }
            }

            function anular(datos) {
                var dat = datos.split("_");
                $('#si').attr('href', 'ajuste_control.php?\n\
                         vcod=' + dat[0] +
                        '&vdep=null' +
                        '&vmotivo=null' +
                        '&vusu=null' +
                        '&vdescri=null' +
                        '&vart=null' +
                        '&vcant=null' +
                        '&accion=2' +
                        '&pagina=ajuste_index.php');
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                 Desea anular el registro <i><strong>' + dat[0] + '</strong></i> ?');
            }

            /*MENSAJE*/
            $("#mensaje").delay(1000).slideUp(200, function () {
                $(this).alert('close');
            })
            /*MENSAJE*/
        </script>
    </body>
</html>
