<?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)));
    $estado=mysqli_real_escape_string($con,(strip_tags($_REQUEST['estado'], 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;
    $count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM ventas_apartadas where empresa='$empresa' and sucursal='$sucursal' and (vena_n_venta like '%$q%' OR vena_nombre like '%$q%' OR vena_cc like '%$q%') and vena_estado='$estado'");
    $row= mysqli_fetch_array($count_query);
    $numrows=$row['numrows'];
    $total_pages=ceil($numrows/$per_page);
    $reload='./P_pos.php';
    
    $sql="SELECT * FROM ventas_apartadas where empresa='$empresa' and sucursal='$sucursal' and (vena_n_venta like '%$q%' OR vena_nombre like '%$q%' OR vena_cc like '%$q%') and vena_estado='$estado' order by vena_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">
        <thead>
            <tr>
                <th width="8%">APART N°</th>
                <th width="8%">FECHA</th>
                <th>CLIENTE</th>
                <th width="8%">TOTAL</th>
                <th width="8%">ABONOS</th>
                <th width="8%">SALDO</th>
                <th width="15%"></th>
            </tr>
        </thead>
        <tbody class="buscar">
            <?php 

            while ($reg=mysqli_fetch_array($query))
                {
                    $vena_id=$reg['vena_id'];
                    $observaciones=$reg['observaciones'];
                    $id=$reg['vena_id'];
                    $vena_n_venta=$reg['vena_n_venta'];
                    
            $sumador_combo=0;
            $sumador_total=0;

            $neto=0;
            $total_descuento=0;
            $total_iva=0;
            $total_retencion=0;
            $sumador_articulos=0;
            
                    $sql=mysqli_query($con,"SELECT * from venta_apartadas_detalle where ventas_apartadas_vena_id='$vena_id' and empresa='$empresa'");
                    while ($row=mysqli_fetch_array($sql))
                    {
                        $vend_precio=$row['venad_precio'];
                        $vend_cantidad=$row['venad_cantidad'];
                        $vend_descuento=$row['venad_desc'];
                        $vend_iva=$row['venad_iva'];

                        $iva0=$vend_iva/100; //0.19
                        $iva1=1+$iva0; //1.19

                        $precio_total_item=$vend_precio*$vend_cantidad;//precio normal 1.000.000
                        $precio_total_sin_iva_item=($vend_precio/$iva1)*$vend_cantidad; //precio sin iva  840.336
                        $descuento_item=($vend_descuento/100)*$precio_total_sin_iva_item; //descuento_item 84.033
                        $total_iva_item=$precio_total_sin_iva_item*$iva0; // total iva  159.336

                        $neto+=$precio_total_sin_iva_item;
                        $total_descuento+=$descuento_item;
                        $total_iva+=$total_iva_item;

                        $sumador_articulos=($neto+$total_iva)-$total_descuento;
                    }


                    $sql=mysqli_query($con, "SELECT sum(veaa_abono) as abono from  venta_apartada_abonos va where ventas_apartadas_vena_id='$vena_id' and empresa='$empresa' ");
                    $sql=mysqli_fetch_array($sql);
                    $abono=$sql['abono']+0;
                    $saldo=$sumador_articulos-$abono;
                ?>
            <tr>
                <td><?php echo $reg['vena_n_venta']; ?></td>
                <td><?php echo $reg['vena_fecha']; ?></td>
                <td><?php echo $reg['vena_nombre'];if ($observaciones=='') { } else { echo '</br> (  '; echo $observaciones; echo  ' ) '; } ?></td>
                <td><p class="pull-right"><?php echo number_format($sumador_articulos,0); ?></p></td>
                <td><p class="pull-right"><?php echo number_format($abono,0); ?></p></td>
                <td><p class="pull-right"><?php echo number_format($saldo,0); ?></p> </td>
                <td >
                <?php
                if($reg['vena_estado']=='anulada')
                {?>
                    <div class="btn-group" ><span class="badgerojo" title="Anulada"> Anulada</span></div>
                    <a href="#verdetalle<?php echo $reg['vena_id'];?>" class="btn btn-xs btn-default" data-toggle="modal"><i class="btn-icon-only icon-eye-open"></i></a>
                <?php }
                else if ($reg['vena_estado']=='pagada')
                { ?>
                    <div class="btn-group" ><span class="badgeazul" title="Pagada"> Pagada</span></div>
                    <a href="#verdetalle<?php echo $reg['vena_id'];?>" class="btn btn-xs btn-default" data-toggle="modal"><i class="btn-icon-only icon-eye-open"></i></a>
                <?php }
                else
                { ?>
                <div class="btn-group">
                    <button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">Accion <span class="caret"></span></button>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#" data-id="<?php echo $id;?>" class="btn-anular">Anular</a></a>
                        <li><a href="#" data-id="<?php echo $id;?>" class=" btn-fact" title="Facturar Factura"  >Facturar </a></li>
                        <li><a href="#" data-id="<?php echo $id;?>" class="btn-abonar">Abonar</a></li>
                        <li class="divider"></li>
                        <li><a href="#verdetalle<?php echo $reg['vena_id'];?>" data-toggle="modal" >Detalles</a></a>
                        <li><a href="#historial<?php echo $reg['vena_id']; ?>" data-toggle="modal" >Historial de Abonos</a></li>
                    </ul> 
                    <?php //include ('forms_facturar.php');?>
                    <?php include ('historial_abonos.php');?> 
                </div><!-- /btn-group -->
                <a href="P_apartados_modificar.php?vena_id=<?php echo $reg['vena_id']; ?>" title="Editar" class="btn btn-xs btn-info" data-toggle="modal" ><i class="btn-icon-only icon-edit"></i></a>
                <?php include ('../include/boton_apartado.php'); ?>        
                <?php } ?>
                <?php include ('forms_detalles.php');?> 

                    
                    
            </td>
        </tr>
    <?php }?>
    <tr>
        <td colspan=7><span class="pull-right">
        <?php
            echo paginate($reload, $page, $total_pages, $adjacents);
        ?></span></td>
    </tr>
</table>
</div>
<?php } } ?>
<script>
$(".btn-anular").click(function(){
    id = $(this).data("id");
    $.get("./php_apartados/forms_anular.php","id="+id,function(data){
        $("#form-anular").html(data);
    });
    $('#anular').modal('show');
});
</script>
<div class="modal fade" id="anular" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Anular</h4>
            </div>
            <div class="modal-body">
                <div id="form-anular"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script>
$(".btn-abonar").click(function(){
    id = $(this).data("id");
    $.get("./php_apartados/forms_abonar.php","id="+id,function(data){
        $("#form-abonar").html(data);
    });
    $('#abonar').modal('show');
});
</script>
  
<div class="modal fade" id="abonar" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Abonar</h4>
            </div>
            <div class="modal-body">
                <div id="form-abonar"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



<script>
$(".btn-fact").click(function(){
  id = $(this).data("id");
  $.get("./php_apartados/forms_facturar.php","id="+id,function(data){
    $("#form-fact").html(data);
  });
  $('#cobrar').modal('show');
});
</script>
