<?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">
                    <!--impresion del titulo de la pagina-->
                    <div class="row">
                        <div class="col-lg-12">
                            <h3 class="page-header" style="left: 100px">Compras
                                <a href="compras_index.php" 
                                   class="btn btn-primary btn-circle pull-right" 
                                   rel='tooltip' title="Atras">
                                    <i class="glyphicon glyphicon-arrow-left"></i> VOLVER
                                </a> 

                                <?php $compras = consultas::get_datos("select * from v_compras where com_cod=" . $_REQUEST['vcod'] . "order by com_cod asc"); ?>
                                <?php foreach ($compras as $compra) { ?> 
                                    <?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="#confirmar"
                                                 class="btn btn-success btn-success pull-right" rel='tooltip' 
                                                 data-title="Confirmar">
                                                <span class="glyphicon glyphicon-ok-sign"></span> CONFIRMAR
                                            </a>
                                        <?php } ?>
                                    <?php } ?>
                                <?php } ?>
                            </h3>
                        </div>     
                    </div>
                    <!--impresion del titulo de la pagina-->
                    <!-- /.cabecera -->
                    <div class="row">
                        <div class="col-lg-12">
                            <!-- CABECERA -->
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    Datos Cabecera
                                </div>
                                <?php
                                /* if (isset($_SESSION['vcod'])) {
                                  if (($_SESSION['vcod'] > 0)) {
                                  $_REQUEST['vcod'] = $_SESSION['vcod'];
                                  }
                                  } */
                                $compras = consultas::get_datos("select * from v_compras where com_cod=" . $_REQUEST['vcod'] . "order by com_cod asc");
                                ?>                         
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table width="100%"
                                               class="table table-bordered">
                                            <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 IVA</th>
                                                    <th class="text-center">Total a Pagar</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?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_totiva'], 0, ',', '.'); ?></td>
                                                        <td class="text-center" style="font-size:40px" ><?php echo number_format($compra['com_total'], 0, ',', '.'); ?></td>
                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>                         
                                    </div>
                                </div>
                            </div>
                            <!-- CABECERA -->
                    <!--carga par el detalle-->
                    <?php if($compra['com_estado'] === 'PENDIENTE'){ ?>
                    <div class="container-fluid">
                        <div class="row">
                            <div class="box box-default">                                                           
                                <div class="box-body">
                                    <div class="row">
                                        <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12">
                                            <!-- FORMULARIO DE PRODUCTOS -->
                                            <form class="" action="detcompra_control.php" method="post" id="frm_add_det">
                                                <input type="hidden" name="accion" value="1">
                                                <input type="hidden" name="vcod" value="<?php echo $_REQUEST['vcod']; ?>" >  
                                                <input type="hidden" name="vtotal" value="0">
                                                <input type="hidden" name="pagina" value="detcompra_agregar.php" >
                                                <div class="row">
                                                    <div class="col-md-3 col-sm-3 col-lg-3">
                                                        <div class="form-group">
                                                            <label >Deposito: </label>
                                                            <?php $depositos = consultas::get_datos("select * from deposito where cod_suc=".$_SESSION['cod_suc']); ?>
                                                            <select name="vdep" class="form-control select2" id="depo" required="">
                                                                    <option value="">Seleccione Deposito</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="col-md-3 col-sm-3 col-lg-3">
                                                        <div class="form-group">
                                                            <label >Tipo: </label>
                                                            <?php $tipos = consultas::get_datos("select * from tipo_articulo where stock = 1"); ?>
                                                            <select class="form-control select2" id="tipo" required="" onchange="selarticulos()">
                                                                    <option value="">Seleccione un Tipo</option>
                                                                    <?php
                                                                    if (!empty($tipos)) {
                                                                        foreach ($tipos as $tipo) { ?>
                                                                            <option value="<?php echo $tipo['tarti_cod']; ?>">
                                                                                <?php echo $tipo['descripcion']; ?>
                                                                            </option>
                                                                            <?php
                                                                        }
                                                                    } else { ?>
                                                                        <option value="0">Debe insertar un tipo</option>
                                                                    <?php } ?>
                                                                </select>                                                            
                                                        </div>                                                    
                                                    </div>                                                    
                                                    <div class="col-md-6 col-sm-6 col-lg-6">
                                                        <div class="form-group" id="div_articulos">
                                                            <label class="control-label">Producto: </label>                                                            
                                                            <select name="vart" class="form-control select2" id="vart" required="">
                                                                   <option value="">Seleccione un tipo</option>
                                                            </select>
                                                        </div>                                                    
                                                    </div>                                                    
                                                </div>                                                 
                                                <div class="row">                                               
                                                    <div class="col-md-3 col-sm-3 col-lg-3">
                                                        <div class="form-group">
                                                            <label>Precio:</label>
                                                            <input type="number" id="vprecio"class="form-control" name="vprecio" min="1" value="1" required=""/>
                                                        </div>                                                        
                                                    </div>                                                     
                                                    <div class="col-md-2 col-sm-2 col-lg-2 col-xs-5">
                                                        <div class="form-group">
                                                            <label>Cantidad:</label>
                                                            <input type="number" id="cant"class="form-control" name="vcant" min="1" value="1" required=""/>
                                                        </div>                                                        
                                                    </div>                                                                                                        
                                                    <div class="col-md-1 col-sm-1 col-lg-1">
                                                        <div class="form-group">
                                                            <label class="control-label hidden-xs">&nbsp;</label>
                                                            <button type="submit" class="btn btn-primary btn-sm pull-right"><i class="fa fa-plus"></i> AGREGAR</button>
                                                        </div>
                                                    </div>                                                     
                                                </div>                                                
                                           </form>
                                                <!-- FIN FORMULARIO DE PRODUCTOS -->                                             
                                        </div>                                        
                                    </div>                                    
                                </div>
                            </div>                                                
                        </div>
                    </div>   
                    <?php } ?> 
                            <?php
                            $detcompra1 = consultas::get_datos("select * from v_compras_detalle "
                                            . " where com_cod = " . $_REQUEST['vcod'] . " order by com_cod asc");
                            ?>
                            <!-- /.cabecera -->

                            <!--GRILLA DETALLE-->
                            <div class="row">
                                <div class="col-lg-12">
                                    <div class="panel panel-primary">
                                        <div class="panel-heading">
                                            Detalles
                                        </div>
                                        <?php if (!empty($detcompra1)) { ?>
                                            <div class="table-responsive">
                                                <div class="panel-body">
                                                <table class="table table-bordered table-striped tablas">
                                                    <thead> 
                                                        <tr>
                                                            <th class="text-center">Descripción</th>
                                                            <th class="text-center">Precio</th>
                                                            <th class="text-center">Cantidad</th>
                                                            <th class="text-center">Subtotal</th>
                                                            <th class="text-center">IVA 5</th>
                                                            <th class="text-center">IVA 10</th>
                                                            <th class="text-center">Exentas</th>
                                                            <th class="text-center">Accion</th>
                                                        </tr>   
                                                    </thead>
                                                    <tbody>
                                                        <?php foreach ($detcompra1 as $detcompra) { ?>
                                                            <tr>
                                                                <td class="text-left"><?php echo $detcompra['art_descri']; ?></td>
                                                                <td class="text-center"><?php echo number_format($detcompra['precio_unit'], 0, ',', '.'); ?></td>
                                                                <td class="text-center"><?php echo $detcompra['cantidad']; ?></td>
                                                                <td class="text-center"><?php echo number_format($detcompra['subtotal'], 0, ',', '.'); ?></td>
                                                                <td class="text-center"><?php echo number_format($detcompra['iva5'], 0, ',', '.'); ?></td>
                                                                <td class="text-center"><?php echo number_format($detcompra['iva10'], 0, ',', '.'); ?></td>
                                                                <td class="text-center"><?php echo number_format($detcompra['exentas'], 0, ',', '.'); ?></td>
                                                                <td class="text-center">
                                                                    <?php if ($compra['com_estado'] === 'PENDIENTE') { ?> 
                                                                        <a onclick="borrar(<?php
                                                                        echo"'" . $detcompra['id_articulo'] . "_" . $_REQUEST['vcod'] . "_" .
                                                                        $detcompra['cod_depo'] . "_" .
                                                                        $detcompra['cantidad'] . "_" .
                                                                        $detcompra['art_descri'] . "'";
                                                                        ?>)"
                                                                           class="btn btn-sm btn-danger" rel="tooltip" data-title="Borrar"
                                                                           data-toggle="modal" data-target="#delete">
                                                                            <span class="glyphicon glyphicon-trash"></span>
                                                                        </a>
                                                                    <?php } ?>
                                                                </td>
                                                            </tr>
                                                        <?php } ?>
                                                    </tbody>
                                                </table>
                                            </div>
                                            <?php } else { ?>
                                                    <div class="panel-body">
                                                        <div class='alert alert-info'>
                                                            <span class='glyphicon glyphicon-info-sign'>
                                                            </span> A&uacute;n no se han registrado detalles...
                                                        </div>                                            
                                                    </div>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            </div> 
                        </div>
                    </div>
                    <!--borrar-->
                    <div class="modal fade" id="confirmar" 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-success" id="cconfirmacion"></div>

                                </div>
                                <div class="modal-footer">
                                    <a id="sic" 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-->                    
                    <!--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-danger" 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 selarticulos(){
                var vtipo = $("#tipo").val();
                if(vtipo !== null){
                $.ajax({
                    type: "POST", url: "compras_articulos.php", data: {vtipo: vtipo}
                }).done(function (datos) {
                    $("#div_articulos").html(datos);
                });  
                }
            }
            function obtenerprecio() {
                var dat = $('#vart').val().split("_");
                if (parseInt($('#vart').val()) > 0) {
                    $("#vprecio").val(dat[1]);
                }
            }




            function borrar(datos) {
                var dat = datos.split("_");
                $('#si').attr('href',
                        'detcompra_control.php?vcod=' + dat[1] +
                        '&vart=' + dat[0] +
                        '&vdep=' + dat[2] +
                        '&vprecio=0' +
                        '&vcant=' + dat[3]
                        + '&accion=3'
                        + '&pagina=detcompra_agregar.php');
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                Desea Borrar el Articulo <i><strong>' + dat[4] + '</strong></i> del detalle?');
            }


            /*PARA TIEMPO DE MENSAJE*/
            $("#mensaje").delay(1000).slideUp(200, function () {
                $(this).alert('close');
            })
            
            function estado(datos) {
                var dat = datos.split("_");
                $('#sic').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');
                $('#cconfirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                Desea confirmar la Compra N° <i><strong>' + dat[0] + '</strong></i> ?');
            }
        </script>

    </body>
</html>
