<?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)));
    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 ven_n_venta like '%$q%' and Cliente_clie_id='$distribuidor'");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './P_pos.php';
   
    $sql="SELECT * , DATE_FORMAT(`ven_fecha`,'%d-%m-%Y') as ven_fecha FROM ventas where empresa='$empresa' and ven_n_venta like '%$q%' and Cliente_clie_id='$distribuidor' order by ven_id desc LIMIT $offset,$per_page";
    $query = mysqli_query($con, $sql);
    if ($numrows>0){
?>
<table class="table table-bordered table-hover table-striped" style="font-size: 9pt;">
    <thead>
        <tr>
            <td></td>
            <th>N°</th>
            <th width="10%">FECHA</th>
            <th width="9%">F.DE PAGO</th>
            <th>TOTAL</th>
            <th>ABONOS</th>
            <th>DEVOLUCION</th>
            <th>SALDO</th>
            <th width="15%"></th>
        </tr>
    </thead>
    <tbody class="buscar">
    <?php 
        while ($reg=mysqli_fetch_array($query))
            {
                $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'];
                $clie_nombre=$reg['ven_nombre'];
                $ven_estado=$reg['ven_estado'];
                $ven_forma_de_pago=$reg['ven_forma_de_pago'];
                $total_descuento=0;
                $total_iva=0;

                include 'saldodeventas.php';
                ?>
        <tr>
            <td><?php echo $estados;?></td>
            <td><?php echo $reg['ven_prefijo']; ?><?php echo $ven_n_venta; ?></td>
            <td><?php echo $ven_fecha;?> - <?php echo $ven_hora; ?></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($total_abono,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>
                <div class="btn-group pull-right">
                    <a href="#verdetalle<?php echo $reg['ven_id']; ?>" title="Ver Detalle" class="btn btn-xs btn-default" data-toggle="modal"><i class="btn-icon-only icon-eye-open"></i></a>  
                    <?php include ('forms_detalle.php'); ?>
                    <?php include ('../include/boton_venta2.php'); ?>
                </div> 
            </td>
        </tr>
            <?php } ?>
        <tr>
            <td colspan=10><span class="pull-right">
                <?php echo paginate($reload, $page, $total_pages, $adjacents);?></span>
            </td>
        </tr>
    </table>
<?php } } ?>