<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php'); 
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax')
{
    $hoy = date("Y-m-d");
    $clie_id = mysqli_real_escape_string($con,(strip_tags($_REQUEST['clie_id'], ENT_QUOTES)));
    $desde = mysqli_real_escape_string($con,(strip_tags($_REQUEST['desde'], ENT_QUOTES)));
    $hasta = mysqli_real_escape_string($con,(strip_tags($_REQUEST['hasta'], ENT_QUOTES)));
    $factura = mysqli_real_escape_string($con,(strip_tags($_REQUEST['factura'], ENT_QUOTES)));
    $serial = mysqli_real_escape_string($con,(strip_tags($_REQUEST['serial'], ENT_QUOTES)));
    $vent_id = mysqli_real_escape_string($con,(strip_tags($_REQUEST['ven_id'], ENT_QUOTES)));
    $Cliente_clie_id = mysqli_real_escape_string($con,(strip_tags($_REQUEST['Cliente_clie_id'], ENT_QUOTES)));

    include '../include/pagination.php'; 
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 20;
    $adjacents  = 4; 
    $offset = ($page - 1) * $per_page;

    


    if ($clie_id<>null)
    {
        $count_query=mysqli_query($con, "SELECT count(*) AS numrows FROM ventas where Cliente_clie_id='$clie_id'");
        $row= mysqli_fetch_array($count_query);
        $numrows=$row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './index.php';
        $query = mysqli_query($con,"SELECT * FROM  ventas where Cliente_clie_id='$clie_id' and empresa='$empresa' order by ven_id desc  LIMIT $offset,$per_page");
    }
    if ($factura<>null)
    {
        $count_query= mysqli_query($con, "SELECT count(*) AS numrows FROM ventas where ven_n_venta='$factura'");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './index.php';
        $query = mysqli_query($con,"SELECT * FROM  ventas where ven_n_venta='$factura' and empresa='$empresa' order by ven_id desc  LIMIT $offset,$per_page");
    }
    if ($serial<>null)
    {
        $count_query= mysqli_query($con, "SELECT count(*) AS numrows FROM seriales where ser_codigo='$serial'");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './index.php';
        $query = mysqli_query($con,"SELECT * FROM  seriales s, ventas v where v.empresa='$empresa' and s.ser_codigo='$serial' and v.ven_id=s.venta_detalle_ventas_ven_id  order by v.ven_id desc  LIMIT $offset,$per_page");

    }
    if ($vent_id<>null)
    {
        $count_query= mysqli_query($con, "SELECT count(*) AS numrows FROM ventas where ven_id='$vent_id'");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './index.php';
        $query = mysqli_query($con,"SELECT * FROM  ventas where ven_id='$vent_id' and  empresa='$empresa' order by ven_id desc  LIMIT $offset,$per_page");
    }


    if ($Cliente_clie_id<>null)
    {
        $count_query= mysqli_query($con, "SELECT count(*) AS numrows FROM ventas where Cliente_clie_id='$Cliente_clie_id'");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './index.php';
        $query = mysqli_query($con,"SELECT * FROM  ventas where Cliente_clie_id='$Cliente_clie_id' and ven_estado='sin pagar' and  empresa='$empresa' order by ven_id desc  LIMIT $offset,$per_page");
    }    
    if ($desde<>null && $hasta<>null)
    {
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM ventas where ven_fecha BETWEEN '$desde' AND '$hasta'  and  empresa='$empresa'");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './index.php';
        $query = mysqli_query($con,"SELECT * FROM  ventas where ven_fecha BETWEEN '$desde' AND '$hasta'  and  empresa='$empresa' order by ven_id desc LIMIT $offset,$per_page");
    }
    if ($desde<>null && $hasta==null)
    {
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM ventas where ven_fecha BETWEEN '$desde' AND '$hoy'  and  empresa='$empresa'");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './index.php';
        $query = mysqli_query($con,"SELECT * FROM  ventas where ven_fecha BETWEEN '$desde' AND '$hoy'  and  empresa='$empresa' order by ven_id desc LIMIT $offset,$per_page");
    }
    if ($desde==null && $hasta<>null)
    {
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM ventas where ven_fecha BETWEEN '$desde' AND '$hoy'  and  empresa='$empresa'");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './index.php';
        $query = mysqli_query($con,"SELECT * FROM  ventas where ven_fecha BETWEEN '$desde' AND '$hasta'  and  empresa='$empresa' order by ven_id desc LIMIT $offset,$per_page");
    } 
    if ($desde==null && $hasta==null && $factura==null && $clie_id==null && $serial==null && $vent_id==null && $Cliente_clie_id==null)
    {
        $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM ventas where empresa='$empresa'");
        $row= mysqli_fetch_array($count_query);
        $numrows = $row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload = './index.php';
        $query = mysqli_query($con,"SELECT * FROM ventas where empresa='$empresa' order by ven_id desc LIMIT $offset,$per_page");
    }              
    
    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></td>
                <th>N°</th>
                <th width="10%">FECHA</th>
                <th>CLIENTE</th>
                <th>F.PAGO</th>
                <th>TOTAL</th>
                <th>ABONOS</th>
                <th>NOTAS CREDITO</th>
                <th>SALDO</th>
                <th width="15%" ></th>
            </tr>
        </thead>
        <?php 
        while ($reg=mysqli_fetch_array($query))
            {
                $id=$reg["ven_id"];
                $ven_id=$reg['ven_id'];
                $apertura_fa=$reg['apertura'];
                $ven_estado=$reg['ven_estado'];
                $estado_dian=$reg['estado_dian'];
                $clie_id_ven=$reg['Cliente_clie_id'];
                $clie_nombre=$reg['ven_nombre'];
                $ven_forma_de_pago=$reg['ven_forma_de_pago'];

                include('saldodeventas.php');
              ?>
              <tr>
               <td><?php  echo $estados;?></td>
                <td><?php echo $reg['ven_prefijo']; ?><?php echo $reg['ven_n_venta']; ?></td>
                <td><?php echo $reg['ven_fecha'];?> - <?php  echo $reg['ven_hora']; ?></td>
                <td><?php echo $reg['ven_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($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>
                    <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="fas fa-plus"></i> <span class="caret"></span></button>
                        <ul class="dropdown-menu" role="menu">
                        <?php
                        $per = mysqli_query($con,"SELECT * FROM  modulo_permiso where usuario='$usu_id' and mod_id='24' and modp_nombre='Anular Ventas' ") or die(mysqli_error($con));
                        $permi = mysqli_fetch_array($per);
                        $estado=$permi['estado'];
                        if ($estado=='Activo') {
                        if ($devo_total<=0 && $ven_estado<>'anulada' && $estado_dian<>'Aprobada' ){?>
                            <li><a  href="#" data-id="<?php echo $id;?>" class="btn-anular"><i class="fas fa-ban"></i> Anular</a></li>
                        <?php } } ?>
                        
                        <?php 
                        $per = mysqli_query($con,"SELECT * FROM  modulo_permiso where usuario='$usu_id' and mod_id='24' and modp_nombre='Realizar Abonos' ") or die(mysqli_error($con));
                        $permi = mysqli_fetch_array($per);
                        $estado_abonar=$permi['estado'];
                        if ($estado_abonar=='Activo') {
                            if ($ven_estado=="sin pagar") { ?>
                            <li><a href="#" data-id="<?php echo $id;?>" class="btn-abonar"><i class="fas fa-hand-holding-usd"></i> Abonar</a></li>
                        <?php } } ?>

                        <?php 
                        $per=mysqli_query($con,"SELECT * FROM modulo_permiso where usuario='$usu_id' and mod_id='24' and modp_nombre='Nota Credito'") or die(mysqli_error($con));
                        $permi = mysqli_fetch_array($per);
                        $estado=$permi['estado'];
                        if ($estado=='Activo') { ?>

                        <?php  
                            if($clie_nombre<>"CLIENTE MOSTRADOR" && $ven_estado<>'anulada'){ ?>
                            <li><a href="P_devolucion.php?ven_id=<?php echo $ven_id ;?>" title="Devoluciones Y Descuentos"><i class="fas fa-undo-alt"></i> Nota Credito</a></li>
                        <?php } } ?>


                        <?php  
                            if ($ven_estado=="pagada"){ ?>
                            <li><a href="#" data-id="<?php echo $id;?>" class="btn-cliente"><i class="fas fa-user-edit"></i> Modificar Cliente</a></li>
                        <?php } ?>

                        
                        <?php if($ven_forma_de_pago=="Credito"){?>
                            <li class="divider"></li>
                            <li><a href="#abonos<?php echo $reg['ven_id']; ?>" data-toggle="modal"><i class="fas fa-credit-card"></i> Historial de Abonos</a></li>
                        <?php }?>
                        </ul>
                        <?php include ('../php_ventas/historial_abono.php'); ?> 
                    </div>
                    <?php include ('../include/boton_venta2.php'); } ?>
            </td>
        </tr>
        <?php }?>
        <tr>
            <td colspan=11>
                <span class="pull-right">
                    <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
                </span>
            </td>
        </tr>
    </table>
</div>
<?php 
}
else
    {
        echo "<div class='alert alert-danger alert-dismissable'>
                    <strong>No hay resultados !</strong> 
             </div>";
    }
}
?>
<script>
    $(".btn-anular").click(function(){
        id = $(this).data("id");
        $.get("./php_pos/forms_anular.php","id="+id,function(data){
            $("#form-anular").html(data);
        });
        $('#anular').modal('show');
    });
</script>
<script>
    $(".btn-abonar").click(function(){
        id = $(this).data("id");
        $.get("./php_pos/forms_abonar.php","id="+id,function(data){
            $("#form-abonar").html(data);
        });
        $('#abonar').modal('show');
    });
</script>
<script>
    $(".btn-cliente").click(function(){
        id = $(this).data("id");
        $.get("./php_pos/forms_cliente.php","id="+id,function(data){
            $("#form-cliente").html(data);
        });
        $('#cliente_modal').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>

<script>
   function descargar(ven_n_venta){
     VentanaCentrada('./pdf/documentos/imprimir_abonos_pdf.php?ven_n_venta='+ven_n_venta,'Abonos','','1024','768','true');
    }
    
</script>