<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php'); 
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax'){
    $q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
    $ser = mysqli_real_escape_string($con,(strip_tags($_REQUEST['ser'], ENT_QUOTES)));
    $buscar = mb_strtoupper($q);
    $tipo_doc=mysqli_real_escape_string($con,(strip_tags($_REQUEST['tipo_doc'], ENT_QUOTES)));

    include '../include/pagination.php'; //include pagination file
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 20; //how much records you want to show
    $adjacents  = 4; //gap between pages after number of adjacents
    $offset = ($page - 1) * $per_page;
    if ($tipo_doc=='1'){
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM ventas where ven_tipo='pos electronico' and empresa='$empresa' and sucursal='$sucursal' and  (ven_n_venta like '%$q%' OR ven_nombre like '%$q%' OR ven_cc like '%$q%' OR ven_prefijo like '%$q%') ");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './P_pos.php';
        if ($ser<>null) 
        {
            $sql="SELECT *, DATE_FORMAT(`ven_fecha`,'%d-%m-%Y') as ven_fecha FROM seriales s, ventas v where v.ven_tipo='pos electronico' and v.empresa='$empresa' and s.ser_codigo like '%$ser%' and v.ven_id=s.venta_detalle_ventas_ven_id  order by v.ven_id desc LIMIT $offset,$per_page";
            $query = mysqli_query($con, $sql);
        }
        else
        {
            $sql="SELECT *, DATE_FORMAT(`ven_fecha`,'%d-%m-%Y') as ven_fecha FROM ventas where ven_tipo='pos electronico' and empresa='$empresa' and sucursal='$sucursal'  and (ven_n_venta like '%$q%' OR ven_nombre like '%$q%' OR ven_cc like '%$q%' OR ven_prefijo like '%$q%') order by ven_id desc LIMIT $offset,$per_page";
            $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>
                <td width="3%"></td>
                <th width="9%">N° FACTURA</th>
                <th width="11%">FECHA</th>
                <th>CLIENTE</th>
                <th>F.PAGO</th>
                <th width="5%">TOTAL</th>
                <th width="5%">ABONO</th>
                <th width="5%">DEVOLUCIÓN</th>
                <th width="5%">SALDO</th>
                <th width="5%">ESTADO</th>
                <th width="15%" ></th>
            </tr>
        </thead>
        <tbody  class="buscar">
        <?php 
        while ($reg=mysqli_fetch_array($query))
            {
                $id=$reg["ven_id"];
                $ven_id=$reg['ven_id'];
                $apertura=$reg['apertura'];
                $ven_n_venta=$reg['ven_n_venta'];
                $ven_fecha=$reg['ven_fecha'];
                $ven_hora=$reg['ven_hora'];
                $ven_estado=$reg['ven_estado'];
                $Cliente_clie_id=$reg['Cliente_clie_id'];
                $clie_id=$reg['Cliente_clie_id'];
                $clie_nombre=$reg['ven_nombre'];
                $ven_forma_de_pago=$reg['ven_forma_de_pago'];

                include '../php_pos/saldodeventas.php';
              ?>
            <tr>
                <td><?php  echo $estados;?></td>
                <td><a href="P_pos_consulta.php?ven_id=<?php echo $reg['ven_id']; ?>"><?php echo $reg['ven_prefijo']; ?><?php echo $ven_n_venta; ?></a></td>
                <td><?php echo $ven_fecha;?> - <?php  echo $ven_hora; ?></td>
                <td><?php echo $clie_nombre; ?></td>
                <td><?php echo $ven_forma_de_pago; ?></td>
                <td><p class="pull-right"><?php echo number_format($sumador_total,0); ?></p></td>
                <td><p class="pull-right"><?php //echo number_format($sumador_total,0); ?></p></td>
                <td><p class="pull-right"><?php echo number_format($devo_total,0); ?></p></td>
                <td><p class="pull-right"><?php echo number_format($saldo,0); ?></p></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></td>
                    <?php } else { ?>
                    <p class="pull-right"><span class="badgerojo" > <i class="btn-icon-only icon-remove"></i></span></p>
                </td>
                    <?php } ?>
                <td>
                    <a href="#" data-id="<?php echo $id;?>" title="Ver Detalle" class="btn btn-xs btn-info btn_detalle" data-toggle="modal" > 
                    <i class="btn-icon-only icon-eye-open"></i></a>
                   
                    <?php if( $reg['ven_estado']<>"anulada" ){ ?>
                    <div class="btn-group" >
                        <button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown"><i class="fa-solid fa-plus"></i></i> <span class="caret"></span></button>
                        <ul class="dropdown-menu" role="menu">
                            <?php if($reg['estado_dian']=="Aprobada"){ ?>
                            <li><a href="V_nota_credito_registrar_pos.php?ven_id=<?php echo $reg['ven_id'];?>"><i class="fa-solid fa-file-invoice-dollar"></i> Nota Credito</a></li>
                            <li><a href="V_nota_debito_registrar_pos.php?ven_id=<?php echo $reg['ven_id'];?>"><i class="fa-solid fa-file-invoice-dollar"></i> Nota Debito</a></li>
                            <li><a href="#" data-id="<?php echo $reg['ven_id'];?>" class="btn-correo"><i class="fa-solid fa-envelope"></i> Enviar Correo</a></li>
                        <?php } ?>
                            <?php if($reg['estado_dian']<>"Aprobada" && $reg['ven_estado']<>"anulada" ){ ?>
                            <li><a href="#" data-id="<?php echo $reg['ven_id'];?>" class="btn-editar" data-toggle="modal"><i class="fa-solid fa-edit"></i> Editar</a></li>
                            <?php  } ?>
                            <?php if($negocio_neg_id=='1') { ?>
                            <li><a href="V_ventas_serial.php?ven_id=<?php echo $reg['ven_id']; ?>" class=""><i class="fa-solid fa-barcode"></i> Seriales</a></li>
                        <?php } ?>
                        </ul>
                    </div>
                    <?php } ?>
                    <?php include ('../include/boton_venta2.php'); ?>

                    <?php if($reg['estado_dian']<>"Aprobada") { if ($reg['ven_estado']<>"anulada") { ?>
                        <a data-id="<?php echo $reg['ven_id'];?>" title="Enviar" class="btn btn-xs btn-warning btn-dian" data-toggle="modal"><i class="btn-icon-only fa fa-upload"></i></a> 
                    <?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>
<script>
    $(".btn-editar").click(function(){
        id = $(this).data("id");
        $.get("./php_doc_factura/forms_editar_factura.php","id="+id,function(data){
            $("#form-editar").html(data);
        });
        $('#factura').modal('show');
    });
</script>
<script>
    $(".btn-dian").click(function(){
        id = $(this).data("id");
        $.get("./php_doc_equivalente/forms_dian_renviar.php","id="+id,function(data){
            $("#form-dian").html(data);
        });
        $('#dian').modal('show');
    });
</script>
<script>
    $(".btn-correo").click(function(){
        id = $(this).data("id");
        $.get("./php_doc_factura/forms_envio_correo.php","id="+id,function(data){
            $("#form-correo").html(data);
        });
        $('#correo').modal('show');
    });
</script>
<script>
    $(".btn_detalle").click(function(){
        id = $(this).data("id");
        $.get("php_pos/forms_detalle.php","id="+id,function(data){
            $("#form-detalle").html(data);
        });
        $('#detalle').modal('show');
    });
</script>
  
 <div class="modal fade" id="correo" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Enviar Correo </h4>
            </div>
            <div class="modal-body">
                <div id="form-correo"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->




<?php } } 
if ($tipo_doc=='4'){
    $count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM dian_nota_credito where empresa='$empresa' and not_tipo_doc='ncp' and (not_numero like '%$q%')");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './P_pos.php';
    $sql="SELECT *, DATE_FORMAT(`not_fecha`,'%d-%m-%Y') as not_fecha FROM dian_nota_credito where empresa='$empresa' and  not_tipo_doc='ncp' and (not_numero like '%$q%') order by not_id desc LIMIT $offset,$per_page";
    $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="8%">N° NOTA</th>
                <th width="10%">N° FACTURA</th>
                <th width="15%">FECHA</th>
                <th>CLIENTE</th>
                <th width="10%">TOTAL</th>
                <th width="5%">ESTADO</th>
                <th width="12%"></th>
            </tr>
        </thead>
        <tbody  class="buscar">
        <?php 
        while ($reg=mysqli_fetch_array($query))
            {
                $id=$reg["not_id"];
                $ventas_ven_id=$reg["ventas_ven_id"];

                $reg1=mysqli_query($con, "SELECT * from ventas where ven_id='$ventas_ven_id'");
                $reg1=mysqli_fetch_array($reg1);

                $Cliente_clie_id=$reg['cliente_id'];
                $sql2=mysqli_query($con, "SELECT * from cliente where clie_id='$Cliente_clie_id'");
                $sql2=mysqli_fetch_array($sql2);
              
                $sumador_total=0;
                $sql=mysqli_query($con, "SELECT * from dian_nota_credito_detalle where dian_not_id='$id'");
                while ($row=mysqli_fetch_array($sql))
                {
                    $id=$row["not_id"];
                    $not_art_cantidad=$row['not_art_cantidad'];
                    $not_precio=$row['not_precio'];
                    $not_iva=$row['not_iva'];
                    $not_desc=$row['not_descuento'];
                    $descuento=($not_desc/100)*$not_precio;

                    $precio_total=$not_precio*$not_art_cantidad;
                    $sumador_total+=$precio_total;//Sumador
                }
                ?>
            <tr>
                <td><?php echo $reg['not_prefijo']; ?><?php echo $reg['not_numero']; ?></td>
                <td><?php echo $reg1['ven_prefijo']; ?><?php echo $reg1['ven_n_venta']; ?></td>
                <td><?php echo $reg['not_fecha'];?> - <?php echo $reg['not_hora']; ?></td>
                <td><?php echo $sql2['clie_nombre']; ?></td>
                <td><p class="pull-right"><?php echo number_format($sumador_total,0); ?></p></td>
                <td> <?php if ($reg['not_estado']=="Aprobada") { ?>
                    <p class="pull-right"><span class="badgeazul"> <i class="btn-icon-only icon-ok"></i></span></p></td>
                    <?php } else { ?>
                    <p class="pull-right"><span class="badgerojo"> <i class="btn-icon-only icon-remove"></i></span></p></td>
                    <?php } ?>
                <td>
                    <a href="#verdetalle<?php echo $reg['not_id'];?>" title="Ver Detalle" class="btn btn-xs btn-info" data-toggle="modal"> 
                    <i class="btn-icon-only icon-eye-open"></i></a>
                    <a href="libraries/tcpdf/documentos/nota_credito5.php?not_id=<?php echo $reg['not_id'];?>" target="_blank" class="btn btn-success btn-xs"><span class="btn-icon-only icon-print"></span></a>
                    <?php  if($reg['not_estado']<>"Aprobada") { ?>
                    <a data-id="<?php echo $reg['not_id'];?>" title="Renviar" class="btn btn-xs btn-warning btn-nota_credito" data-toggle="modal"><i class="btn-icon-only fa fa-upload"></i></a> 
                    <?php } ?>
                    <?php  if($reg['not_estado']=="Aprobada") { ?>
                    <a data-id="<?php echo $reg['not_id'];?>" class="btn btn-xs btn-default btn-correo" title='Enviar por email' ><i class="btn-icon-only icon-envelope"></i></a>
                      <?php } ?>
                    <?php include ('../php_doc_nota_credito/forms_detalle.php'); ?>
                </td>
            </tr>
           <?php }?>
            <tr>
                <td colspan=7>
                    <span class="pull-right">
                        <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
                    </span>
                </td>
            </tr>
        </table>
    </div>
<script>
    $(".btn-nota_credito").click(function(){
        id = $(this).data("id");
        $.get("./php_doc_nota_credito/forms_reenviar_notacredito.php","id="+id,function(data){
            $("#form-notacredito").html(data);
        });
        $('#notacredito').modal('show');
    });
</script>
<div class="modal fade" id="notacredito" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Renviar NotaCredito</h4>
            </div>
            <div class="modal-body">
                <div id="form-notacredito"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
    $(".btn-correo").click(function(){
        id = $(this).data("id");
        $.get("./php_doc_nota_credito/forms_envio_correo.php","id="+id,function(data){
            $("#form-correo").html(data);
        });
        $('#correo').modal('show');
    });
</script>
  
 <div class="modal fade" id="correo" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Enviar Correo </h4>
            </div>
            <div class="modal-body">
                <div id="form-correo"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<?php } }
if ($tipo_doc=='5'){

    $count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM dian_nota_credito where empresa='$empresa' and not_tipo_doc='ndp' and (not_numero like '%$q%') ");
    $row= mysqli_fetch_array($count_query);
    $numrows= $row['numrows'];
    $total_pages=ceil($numrows/$per_page);
    $reload='./P_pos.php';
    $sql="SELECT *, DATE_FORMAT(`not_fecha`,'%d-%m-%Y') as not_fecha FROM dian_nota_credito where empresa='$empresa' and not_tipo_doc='ndp' and (not_numero like '%$q%') order by not_id desc LIMIT $offset,$per_page";
    $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="8%">N° NOTA</th>
                <th width="10%">N° FACTURA</th>
                <th width="15%">FECHA</th>
                <th>CLIENTE</th>
                <th width="10%">TOTAL</th>
                <th width="5%">ESTADO</th>
                <th width="12%"></th>
            </tr>
        </thead>
        <tbody  class="buscar">
        <?php 
        while ($reg=mysqli_fetch_array($query))
            {
                $id=$reg["not_id"];
                $ventas_ven_id=$reg["ventas_ven_id"];

                $reg1=mysqli_query($con, "SELECT * from ventas where ven_id='$ventas_ven_id'");
                $reg1=mysqli_fetch_array($reg1);

                $Cliente_clie_id=$reg['cliente_id'];
                $sql2=mysqli_query($con, "SELECT * from cliente where clie_id='$Cliente_clie_id'");
                $sql2=mysqli_fetch_array($sql2);
              
                $sumador_total=0;
                $sql=mysqli_query($con, "SELECT * from dian_nota_credito_detalle where dian_not_id='$id'");
                while ($row=mysqli_fetch_array($sql))
                {
                    $id=$row["not_id"];
                    $not_art_cantidad=$row['not_art_cantidad'];
                    $not_precio=$row['not_precio'];
                    $not_iva=$row['not_iva'];
                    $not_desc=$row['not_descuento'];
                    $descuento=($not_desc/100)*$not_precio;

                    $precio_total=$not_precio*$not_art_cantidad;
                    $sumador_total+=$precio_total;//Sumador
                }
                ?>
            <tr>
                <td><?php echo $reg['not_prefijo']; ?><?php echo $reg['not_numero']; ?></td>
                <td><?php echo $reg1['ven_prefijo']; ?><?php echo $reg1['ven_n_venta']; ?></td>
                <td><?php echo $reg['not_fecha'];?> - <?php echo $reg['not_hora']; ?></td>
                <td><?php echo $sql2['clie_nombre']; ?></td>
                <td><p class="pull-right"><?php echo number_format($sumador_total,0); ?></p></td>
                <td> <?php if ($reg['not_estado']=="Aprobada") { ?>
                    <p class="pull-right"><span class="badgeazul"> <i class="btn-icon-only icon-ok"></i></span></p></td>
                    <?php } else { ?>
                    <p class="pull-right"><span class="badgerojo"> <i class="btn-icon-only icon-remove"></i></span></p></td>
                    <?php } ?>
                <td>
                    <a href="#verdetalle<?php echo $reg['not_id'];?>" title="Ver Detalle" class="btn btn-xs btn-info" data-toggle="modal"> 
                    <i class="btn-icon-only icon-eye-open"></i></a>
                    <a href="libraries/tcpdf/documentos/nota_debito5.php?not_id=<?php echo $reg['not_id'];?>" target="_blank" class="btn btn-success btn-xs"><span class="btn-icon-only icon-print"></span></a>
                    <?php  if($reg['not_estado']<>"Aprobada") { ?>
                    <a data-id="<?php echo $reg['not_id'];?>" title="Renviar" class="btn btn-xs btn-warning btn-nota_credito" data-toggle="modal"><i class="btn-icon-only fa fa-upload"></i></a> 
                    <?php } ?>
                    <?php  if($reg['not_estado']=="Aprobada") { ?>
                    <a data-id="<?php echo $reg['not_id'];?>" class="btn btn-xs btn-default btn-correo" title='Enviar por email' ><i class="btn-icon-only icon-envelope"></i></a>
                      <?php } ?>
                    <?php include ('../php_doc_nota_credito/forms_detalle.php'); ?>
                </td>
            </tr>
           <?php }?>
            <tr>
                <td colspan=7>
                    <span class="pull-right">
                        <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
                    </span>
                </td>
            </tr>
        </table>
    </div>
<script>
    $(".btn-nota_credito").click(function(){
        id = $(this).data("id");
        $.get("./php_doc_nota_debito/forms_reenviar_notadebito.php","id="+id,function(data){
            $("#form-notadebito").html(data);
        });
        $('#notadebito').modal('show');
    });
</script>
 <div class="modal fade" id="notadebito" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Renviar NotaDebito</h4>
            </div>
            <div class="modal-body">
                <div id="form-notadebito"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



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

 <div class="modal fade" id="correo" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Enviar Correo </h4>
            </div>
            <div class="modal-body">
                <div id="form-correo"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<?php }  }  } ?>

