<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php'); 
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax')
{
    $tipo_doc=mysqli_real_escape_string($con,(strip_tags($_REQUEST['tipo_doc'], ENT_QUOTES)));
  //  $q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
    include '../include/pagination.php'; 
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 20;
    $adjacents  = 4; 
    $offset = ($page - 1) * $per_page;
    $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM dian_documento_soporte WHERE docs_tipo_doc='$tipo_doc' ");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './P_pos.php';
    $sql="SELECT * FROM dian_documento_soporte where empresa='$empresa' AND docs_tipo_doc='$tipo_doc' order by docs_id desc";
    $query = mysqli_query($con, $sql);
    if ($numrows>0)
    {
?>
<div class="table-responsive">
    <table class="table table-bordered table-hover table-striped" id="datos" style="font-size: 9pt;">
        <thead>
            <tr>
                <th width="10%">N° DOC</th>
                <th width="10%">FECHA</th>
                <th>PROVEEDOR</th>
                <th>TOTAL</th>
                <th>ESTADO</th>
                <th width="15%"></th>
            </tr>
        </thead>
        <tbody  class="buscar">
        <?php 
        while ($reg=mysqli_fetch_array($query))
        {
            $docs_id=$reg["docs_id"];
            $docs_tipo_doc=$reg["docs_tipo_doc"];
            $docs_numero=$reg['docs_numero'];
            $docs_fecha=$reg['docs_fecha'];
            $docs_proveedor=$reg['docs_proveedor'];


            $sumador_total=0;
            $can7=mysqli_query($con,"SELECT * FROM dian_documento_detalle where documento_id='$docs_id'") or die(mysqli_error($con));  
            while($row=mysqli_fetch_array($can7))
            {
                $sopd_nombre=$row['sopd_nombre'];
                $sopd_cantidad=$row['sopd_cantidad'];
                $sopd_precio=$row['sopd_precio'];
                $total=$sopd_precio*$sopd_cantidad;
                $sumador_total+= $total;
            }

            $total_retencion=0;
            $ret=mysqli_query($con,"SELECT SUM(ret_importe) as ret_importe FROM retencion_doc where retencion_ret_id='$docs_id'") or die(mysqli_error($con)); 
            $rets=mysqli_fetch_array($ret);
            $total_retencion=$rets['ret_importe'];

            $can=mysqli_query($con,"SELECT * FROM proveedor where pro_id='$docs_proveedor'") or die(mysqli_error($con)); 
            $dato1=mysqli_fetch_array($can);
           
          ?>
            <tr>
                <td><?php echo $reg['docs_prefijo']; echo $docs_numero;?></td>
                <td><?php echo $docs_fecha;?></td>
                <td><?php echo $dato1['pro_nombre'];?></td>
                <td><span class="pull-right"><?php echo number_format($sumador_total-$total_retencion,0);?></span></td>
                <td><?php if ($reg['estado_dian']=="Aprobada") { ?>
                    <p class="pull-right"><span class="badgeazul"> <i class="btn-icon-only icon-ok"></i></span></p>
                    <?php } else { ?>
                    <p class="pull-right"><span class="badgerojo"> <i class="btn-icon-only icon-remove"></i></span></p>
                    <?php } ?>
                </td>
                <td>
                    
                    <a href="libraries/tcpdf/documentos/documento_soporte.php?docs_id=<?php echo $docs_id;?>" target="_blank" class="btn btn-success btn-xs" ><span class="btn-icon-only icon-print"></span></a>

                    <?php if ($reg['estado_dian']=="Aprobada" ) { ?>
                        <a data-id="<?php echo $reg['docs_id'];?>" class="btn btn-xs btn-default btn-correo" title='Enviar por email' ><i class="btn-icon-only icon-envelope"></i></a>
                        <?php if($docs_tipo_doc=='11'){?>
                        <a href="V_documento_nota_registrar.php?docs_id=<?php echo $docs_id;?>" class="btn btn-xs btn-warning" title='Nota de Ajuste' ><i class="btn-icon-only fa fa-reply-all"></i></a>
                         <?php } ?>
                    <?php } else { ?>
                    <a href="#" id="del-<?php echo $reg["docs_id"];?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>


                    <?php if($docs_tipo_doc=='11'){?>
                    <a data-id="<?php echo $reg['docs_id'];?>" title="Renviar" class="btn btn-xs btn-warning btn-dian" data-toggle="modal"><i class="btn-icon-only fa fa-upload"></i></a> 
                    <?php } else { ?>
                    <a data-id="<?php echo $reg['docs_id'];?>" title="Renviar" class="btn btn-xs btn-warning btn-dian_nota" data-toggle="modal"><i class="btn-icon-only fa fa-upload"></i></a> 
                    <?php } ?>

                    <script>
                    $("#del-"+<?php echo $reg["docs_id"];?>).click(function(e){
                        e.preventDefault();
                        p = confirm("Estas seguro?");
                        if(p){
                            $.get("./php_doc_soporte/eliminar.php","docs_id="+<?php echo $reg["docs_id"];?>,function(data){
                                documentosoporte(1);
                            });
                        }
                    });
                    </script>
                    <?php } ?>
                </td>
            </tr>
           <?php }?>

            <tr>
                <td colspan=12>
                    <span class="pull-right">
                        <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
                    </span>
                </td>
            </tr>
        </table>
    </div>
<?php } } ?>

<script>
    $(".btn-dian").click(function(){
        id = $(this).data("id");
        $.get("./php_doc_soporte/forms_dian_renviar.php","id="+id,function(data){
            $("#form-dian").html(data);
        });
        $('#dian').modal('show');
    });
</script>

<script>
    $(".btn-dian_nota").click(function(){
        id = $(this).data("id");
        $.get("./php_doc_soporte/forms_dian_renviar_nota.php","id="+id,function(data){
            $("#form-dian").html(data);
        });
        $('#dian').modal('show');
    });
</script>
