<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php'); 

$clie_id=$_SESSION['clie_id'];
$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 = 10; //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 Cliente_clie_id='$clie_id' and ven_estado='pagada' and  empresa='$emp_id' and ven_n_venta like '%$q%' ");
$row= mysqli_fetch_array($count_query);
$numrows = $row['numrows'];
$total_pages = ceil($numrows/$per_page);
$reload = './P_pos.php';


$sql=mysqli_query($con,"SELECT * FROM ventas where Cliente_clie_id='$clie_id' and ven_estado= 'pagada' and empresa='$emp_id' and ven_n_venta like '%$q%' order by ven_id desc LIMIT $offset,$per_page") or die(mysqli_error($con));
?>

 <div class="table-responsive">
    <table class="table">
        <thead>
            <tr>
                <th>N° FACTURA</th>
                <th>FECHA</th>
                <th>PAGO</th>
                <th>TOTAL</th>
                <th width="1%">DETALLE</th>
            </tr>           
        </thead>
        <tbody>
        <?php 
          
            while( $ven = mysqli_fetch_array($sql))
            { 
                $ven_id= $ven['ven_id'];
                $ven_prefijo=$ven['ven_prefijo'];
                $ven_n_venta=$ven['ven_n_venta'];
                $ven_fecha= $ven['ven_fecha'];
                 
                $ven_forma_de_pago=$ven['ven_forma_de_pago']; 
                $total=0;
                $sq2=mysqli_query($con,"SELECT * FROM venta_detalle where ventas_ven_id ='$ven_id' and empresa='$emp_id' ") or die(mysqli_error($con));
                while($vend=mysqli_fetch_array($sq2))                                               
                { 
                    $vend_cantidad=$vend['vend_cantidad'];
                    $vend_precio=$vend['vend_precio'];  
                     
                    $total_cantidades=$vend_cantidad*$vend_precio;
                    $total=$total+$total_cantidades;  
                }
                ?>
            <tr>
                <td><?php echo $ven_prefijo; echo $ven_n_venta; ?></td>
                <td><?php echo $ven_fecha; ?></td>
                <td><?php echo $ven_forma_de_pago; ?></td>
                <td>$<?php echo number_format($total,0,",","."); ?></td>
                <td><?php if($ven['ven_tipo'] =='electronica') { ?>
                    <a href="<?php echo $emp_url_sub ?>/libraries/tcpdf/documentos/factura_venta5.php?ven_id=<?php echo $ven_id;?>" target="_blank"><i class="zmdi zmdi-print"></i></a> <?php } else { ?>
                    <a href="<?php echo $emp_url_sub ?>/libraries/tcpdf/documentos/factura_venta5.php?ven_id=<?php echo $ven_id;?>" target="_blank"> <i class="zmdi zmdi-print"></i></a>
                <?php } ?>
                </td>
            </tr>
               <?php 
             } ?>
       
        </tbody>
        
    </table>
   
</div>

<div class="shop_toolbar t_bottom">
<div class="pagination">
    <ul>
        <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
    </ul>
</div>
</div>