<?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);

    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;
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM ventas where empresa='$empresa' and sucursal='$sucursal' and  (ven_tipo='remision' or ven_tipo='pos') 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.empresa='$empresa' and s.ser_codigo like '%$ser%' and v.ven_id=s.venta_detalle_ventas_ven_id and (v.ven_tipo='remision' or v.ven_tipo='pos') 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 empresa='$empresa' and sucursal='$sucursal' and (ven_tipo='remision' or ven_tipo='pos') 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°</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="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>
                    <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 include ('../include/boton_venta2.php'); ?>
                 
                    <?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['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 if($facturador=='si') { ?>
                        <?php if($reg['ven_estado']<>"anulada") { ?>
                                <a href="#" class="btn btn-xs btn-warning btn-dian" title="Enviar Dian" data-id="<?php echo $reg['ven_id'];?>"><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>

<?php } }  ?>
<script>
    $(".btn-dian").click(function(){
        id = $(this).data("id");
        $.get("./php_electronica/forms_dian.php","id="+id,function(data){
            $("#form-dian").html(data);
        });
        $('#dian').modal('show');
    });
</script>
<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_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>