<?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)));
    $cliente = mysqli_real_escape_string($con,(strip_tags($_REQUEST['cliente'], 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($cliente==0){
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM dis_cotizacion  ");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './V_cotizaciones.php';
        $sql="SELECT * FROM  dis_cotizacion  order by cot_id desc LIMIT $offset,$per_page";
        $query = mysqli_query($con, $sql);
    }
    else
    {
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM dis_cotizacion where  distribuidor='$cliente' ");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './V_cotizaciones.php';
        $sql="SELECT * FROM  dis_cotizacion where distribuidor='$cliente'  order by cot_id desc LIMIT $offset,$per_page";
        $query = mysqli_query($con, $sql);
    }
if ($numrows>0){
?>
<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th width="5%">Nº</th>
            <th width="15%">FECHA</th>
            <th>DISTRIBUIDOR</th>
            <th>CLIENTE</th>
            <th >TOTAL</th>
            <th width="10%"></th>
        </tr>
    </thead>
    <tbody  class="buscar">
<?php 
while ($reg=mysqli_fetch_array($query))
{
    $cot_id=$reg['cot_id'];
    $dis_cliente_clie_id= $reg['dis_cliente_clie_id'];
    $distribuidor=$reg['distribuidor'];
    $result_1=mysqli_query($con,"SELECT * FROM cliente WHERE clie_id='$distribuidor'");  
    $row_1=mysqli_fetch_array($result_1);
    $descuentof=0;
    $descuento=0;
    $neto=0;
    $sumador_total=0;
    $iva_total=0;
    $sumador_total=0;
    $sql=mysqli_query($con, "SELECT * from  dis_cotizacion_detalle where dis_cotizacion_cot_id='$cot_id'  ");
    while ($row=mysqli_fetch_array($sql))
    {
        $id=$row["cotd_id"];
        $cotd_id=$row["cotd_id"];
        $cotd_referencia=$row['cotd_referencia'];
        $cotd_nombre=$row['cotd_nombre'];
        $cotd_cantidad=$row['cotd_cantidad'];
        $cotd_precio=$row['cotd_precio'];
        $cotd_iva=$row['cotd_iva'];
        $cotd_desc=$row['cotd_desc'];
     
        $ivaf=$cotd_iva/100;
        $precio_total=$cotd_precio*$cotd_cantidad;
        $sumador_total+=$precio_total;//Sumador
        $descuento=($cotd_desc/100)*$precio_total;
        $descuentof+=$descuento;

        $iva=$cotd_precio*$ivaf;
        $ivat=$iva*$cotd_cantidad;
        $sin_iva=($cotd_precio-$iva);
        $sin_ivat=($cotd_precio-$iva)*$cotd_cantidad;
        $iva_total+=$ivat;
        $neto+=$precio_total;
        $total=$sumador_total-$descuentof;
  }

$can=mysqli_query($con,"SELECT * FROM dis_cliente where clie_id='$dis_cliente_clie_id' ") or die(mysqli_error($con)); 
$dato=mysqli_fetch_array($can);
?>
    <tr>
        <td><?php  echo $reg['cot_n_cotizacion']; ?></td>
        <td><?php  echo $reg['cot_fecha']; ?> / <?php echo $reg['cot_hora'];?></td>
        <td><?php echo $row_1['clie_nombre']; ?></td>
        <td><?php echo $dato['clie_nombre']; ?></td>
        <td><p  class="pull-right"> <?php echo number_format($total,0);?> </P></td>
        <td>
            <a href="#verdetalle<?php echo $reg['cot_id']; ?>" data-toggle="modal" class='btn btn-xs btn-info' title='Detalles'><i class="btn-icon-only icon-eye-open"></i></a> 
            <a href="#facturar<?php echo $reg['cot_id']; ?>" data-toggle="modal" class='btn btn-xs btn-success' title='Facturar'><i class="btn-icon-only icon-usd"></i></a>
            <?php 
                include ('asesores_forms_detalle.php');
                include ('asesores_modal_facturar.php'); 
            ?>
        </td>
    </tr>
    <?php }?>
    <tr>
        <td colspan=7>
            <span class="pull-right"> 
                <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
            </span>
        </td>
    </tr>
</table>
<?php } } ?>