<?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 botones cabecera-->
                    <div class="row">
                        <!--impresion del titulo de la pagina-->
                        <div class="col-lg-12">
                            <h3 class="page-header">Listado de Compras 
                                <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>   

                        <!--Buscador-->
                        <div class="col-lg-12">
                            <div class="panel-heading">
                                <div class="input-group custom-search-form">
                                    <input id="filtrar" type="text" class="form-control" placeholder="Buscar...">
                                    <span class="input-group-btn">
                                        <button class="btn btn-primary" type="button" rel="tooltip" title="Buscar">
                                            <i class="fa fa-search"></i>
                                        </button>
                                    </span>
                                </div>
                            </div>                      
                        </div>
                        <!--fin Buscador-->
                    </div>
                    <!-- /.div botones cabecera-->

                    <!-- /.cabecera -->
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    Datos
                                </div>
                                <?php
                                $compras = consultas::get_datos("select * from v_compras /*where com_fecha=(select current_date)*/ order by com_fecha desc,com_cod desc ");
                                if (!empty($compras)) {
                                    ?>                         
                                    <!-- /.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">Factura N°</th>
                                                        <th class="text-center">Orden Nº </th>
                                                        <th class="text-center">Fecha</th>
                                                        <th class="text-center">Proveedor</th>
                                                        <th class="text-center">Total</th>
                                                        <th class="text-center">Estado</th>
                                                        <th class="text-center">Acciones</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="buscar">
                                                    <?php foreach ($compras as $compra) { ?> 
                                                        <tr>
                                                            <td class="text-center"><?php echo $compra['com_nro_factura']; ?></td>   
                                                            <td class="text-center"><?php echo (!empty($compra['orden_cod'])?$compra['orden_cod']:"SIN ORDEN"); ?></td>
                                                            <td class="text-center"><?php echo date("d/m/Y", strtotime($compra['com_fecha'])); ?></td>
                                                            <td class="text-left"><?php echo $compra['prv']; ?></td>
                                                            <td class="text-center"><?php echo number_format($compra['com_total'], 0, ',', '.'); ?></td>
                                                            <td class="text-center"><?php echo $compra['com_estado']; ?></td>
                                                            <td class="text-center">                                                               
                                                                    <?php if ($compra['com_estado'] == 'PENDIENTE') { ?> 
                                                                        <?php if ($compra['com_total'] != '0') { ?> 
                                                                            <a onclick="estado(<?php echo "'" . $compra['com_cod'] . "_" . $compra['orden_cod'] . "'"; ?>)"
                                                                               data-toggle="modal" data-target="#delete"
                                                                               class="btn btn-sm btn-success" rel='tooltip' 
                                                                               data-title="Confirmar">
                                                                                <span class="glyphicon glyphicon-ok-sign"></span>
                                                                            </a>

                                                                        <?php } ?>
                                                                    <?php } ?>
                                                                    <a href="detcompra_agregar.php?vcod=<?php echo $compra['com_cod']; ?>" class="btn btn-sm btn-primary" rel='tooltip' data-title="Detalles">
                                                                        <i class="fa fa-list"></i>
                                                                    </a>
                                                                    <?php if ($compra['com_estado'] !== 'ANULADO') { ?>
                                                                        <a onclick="anular(<?php echo "'" . $compra['com_cod'] . "_" . $compra['orden_cod'] . "'"; ?>)"
                                                                           data-toggle="modal" data-target="#delete"
                                                                           class="btn btn-sm btn-danger" rel='tooltip' 
                                                                           data-title="Anular">
                                                                            <span class="glyphicon glyphicon-remove"></span>
                                                                        </a>

                                                                        <a  
                                                                            href="/farmmedi/compras/compras/imprimir_facturacompras.php?vcod=<?php echo $compra['com_cod']; ?>"
                                                                            target="blank"
                                                                            class="btn btn-sm btn-default" rel="tooltip" data-title="Imprimir Factura" >
                                                                            <span class="glyphicon glyphicon-print"></span>
                                                                        </a>
                                                                    <?php } ?>
                                                            </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 registros....!</strong>
                                        </div>
                                    <?php } ?>  
                                    <!-- /.panel-body -->
                                </div>
                            </div>

                        </div>
                    </div>
                    <!-- /.cabecera -->

                    <!--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 Compras</strong></h4>
                                </div>
                                <form action="compras_control.php" method="post" accept-charset="utf-8" class="form-horizontal">
                                    <?php $fecha = consultas::get_datos("select current_date as fecha")?>
                                    <div class="panel-body">
                                        <input type="hidden" name="accion"  value="1">
                                        <input type="hidden" name="vtotal" value="0">
                                        <input type="hidden" name="viva" value="0">
                                        <input type="hidden" name="vintervalo" value="30">
                                        <input type="hidden" name="vusu" value="<?php echo $_SESSION['usu_cod']; ?>">
                                        <input type="hidden" name="vsuc" value="<?php echo $_SESSION['cod_suc']; ?>">
                                        <input type="hidden" name="pagina" value="compras_index.php" id="pagina">
                                        <input type="hidden" name="vcod" value="0"/> 
                                        <div class="row">
                                            <div class="col-md-4">
                                                <label class="control-label">Fecha</label>
                                                <input type="date" name="vcom_fecha" required="" class="form-control" value="<?php echo $fecha[0]['fecha'];?>" max="<?php echo $fecha[0]['fecha']; ?>" name="vfecha">
                                            </div>
                                            <div class="col-md-8" >
                                                <label class="control-label">Nro Factura</label>
                                                <input type="text" required="" placeholder="Ingrese el N° factura" class="form-control" name="vnrofac" 
                                                       id="vnrofac" maxlength="15" pattern="[0-9-]+">
                                            </div>                                            
                                        </div>

                                        <div class="row">
                                            <div class="col-md-4">
                                                <label class="control-label">Condicion de Venta</label>
                                                <select name="vcondicion" class="form-control" style="width : 150px"
                                                        id="vcondicion" onchange="tiposelect()">
                                                    <option value="CONTADO">CONTADO</option>
                                                    <option value="CREDITO">CREDITO</option>
                                                </select>
                                            </div>

                                            <div class="col-md-8">
                                                <label class="control-label">Nro Timbrado</label>
                                                <input type="text" required=""
                                                       placeholder="Digite timbrado" class="form-control" name="vnrotim" id="vnrotim"
                                                       maxlength="8" pattern="[0-9-]+">
                                            </div>
                                        </div>

                                        <div class="row">
                                            <div class="col-md-4">
                                                <label class="control-label">Venc. Timbrado</label>
                                                <input type="date" required="" 
                                                       placeholder="Especifique fecha" class="form-control"
                                                       value="<?php echo date("Y-m-d") ?>"
                                                       name="vfechavenc" style="width: 145px " >
                                            </div>                                            
                                            <div class="col-md-4">
                                                <label class="control-label">Cantidad Cuota</label>
                                                <input type="number" class="form-control" name="vcantcuo" readonly="" pattern="^[0-9]+" min="0" id="vcancuo">
                                            </div>

                                            <div class="col-md-4">
                                                <label class="control-label">Intervalo</label>
                                                <input type="number" class="form-control" name="vintervalo" readonly="" pattern="^[0-9]+" min="0" id="vintervalo" value="0">
                                            </div>
                                        </div>

                                        <!--Orden por proveedores-->
                                        <div class="row">
                                            <div class="col-md-8 col-lg-8">
                                                <?php $prv = consultas::get_datos("SELECT * from proveedores"); ?>                                 
                                                <label class="control-label">Proveedor</label>
                                                <select name="vprv" id="prv_cod" class="select2" required="" style="width:100%" onchange="div_orden();">
                                                    <?php
                                                    if (!empty($prv)) {
                                                        foreach ($prv as $prv) {
                                                            ?>
                                                            <option value="<?php echo $prv['prv_cod']; ?>">
                                                                <?php echo $prv['prv_razonsoc']; ?></option>
                                                            <?php
                                                        }
                                                    } else {
                                                        ?>
                                                        <option value=""></option>
                                                    <?php } ?>

                                                </select>
                                            </div>

                                            <div class="form-group">
                                                <label class="col-md-2 control-label">Orden</label>
                                                <br>
                                                <div class="col-md-2 control-label" style="right:115px">
                                                    <input type="checkbox" style="" id="myCheck" onclick="myFunction(); div_orden();">
                                                    <p id="text" style="display:none"></p>
                                                </div>
                                            </div>
                                        </div>
                                            <div class="form-group" id="div_orden1" style="display:none">

                                            </div>                                        
                                        <!--Orden por proveedores-->

                                        <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> 
                    <!--borrar-->
                </div>
            </div>
        </div>
        <!--archivos js-->  
        <?php require '../../menu/js_lte.ctp'; ?>


        <script>
            function estado(datos) {
                var dat = datos.split("_");
                $('#si').attr('href', 'compras_control.php?vcod=' + dat[0] +
                        '&vorden=' + dat[1] +
                        '&vusu=null' +
                        '&vsuc=null' +
                        '&vusu=null' +
                        '&vprv=null' +
                        '&vnrofac=null' +
                        '&vnrotim=null' +
                        '&vcondicion=null' +
                        '&vcantcuo=null' +
                        '&vintervalo=null' +
                        '&viva=null' +
                        '&vtotal=null' +
                        '&vfechavenc=1900-01-01' +
                        '&accion=2' +
                        '&pagina=compras_index.php');
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                Desea confirmar la Compra N° <i><strong>' + dat[0] + '</strong></i> ?');
            }
            function anular(datos) {
                var dat = datos.split("_");
                $('#si').attr('href', 'compras_control.php?vcod=' + dat[0] +
                        '&vorden=' + dat[1] +
                        '&vusu=null' +
                        '&vsuc=null' +
                        '&vusu=null' +
                        '&vprv=null' +
                        '&vnrofac=null' +
                        '&vnrotim=null' +
                        '&vcondicion=null' +
                        '&vcantcuo=null' +
                        '&vintervalo=null' +
                        '&viva=null' +
                        '&vtotal=null' +
                        '&vfechavenc=1900-01-01' +
                        '&accion=3' +
                        '&pagina=compras_index.php');
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                Desea anular la Compra N° <i><strong>' + dat[0] + '</strong></i> ?');
            }
            /*PARA TIEMPO DE MENSAJE*/
            $("#mensaje").delay(1000).slideUp(200, function () {
                $(this).alert('close');
            })

            function tiposelect() {
                if (document.getElementById('vcondicion').
                        value === "CONTADO") {
                    document.getElementById('vcancuo').setAttribute('readonly', 'true');
                    document.getElementById('vintervalo').setAttribute('readonly', 'true');
                    document.getElementById('vcancuo').value = '1';
                    document.getElementById('vintervalo').value = '0';
                } else {
                    document.getElementById('vcancuo').removeAttribute('readonly');
                    document.getElementById('vintervalo').removeAttribute('readonly');
                    document.getElementById('vcancuo').value = '1';
                }
            }
            window.onload = tiposelect();

            function myFunction() {
                var checkBox = document.getElementById("myCheck");
                var text = document.getElementById("text");

                if (checkBox.checked == true) {
                    text.style.display = "block";
                    $('#myCheck').val('1');
                    $('#div_orden1').prop('style', 'display:block');
                    //$('#div_pedi').prop('style', 'display:none');
                } else {
                    text.style.display = "none";
                    $('#myCheck').val('0');
                    $('#div_orden1').prop('style', 'display:none');
                    //$orden_cod = "null";
                    //$('#div_pedi').prop('style', 'display:block');

                }
            }
        </script>

        <script type="text/javascript">
            function div_orden() {
                var prv_cod = $("#prv_cod").val();
                $.ajax({
                    type: "POST", url: "div_orden.php", data: {prv_cod: prv_cod}
                }).done(function (datos) {
                    $("#div_orden1").html(datos);
                });
            }
            function div_orden_detalle() {
                var prv_cod = $("#prv_cod").val();
                $.ajax({
                    type: "POST", url: "div_orden.php", data: {prv_cod: prv_cod}
                }).done(function (datos) {
                    $("#div_orden1").html(datos);
                });
            }
            function div_orden_detalle() {
                var orden_cod = $("#orden_cod").val();
                $.ajax({
                    type: "POST", url: "div_orden_detalle.php", data: {orden_cod: orden_cod}
                }).done(function (datos) {
                    $("#div_orden_detalle").html(datos);
                });
            }
        </script>
    </body>
</html>
