<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$ven_id=$_GET['ven_id'];
?>
<table class="table table-bordered table-hover table-striped">
    <tr>
        <th width="5%">CODIGO</th>
        <th Width="5%">CANTIDAD</th>
        <th Width="5%">DEVOLUCIONES</th>
        <th>DESCRIPCION</th>
        <th Width="5%"><span class="pull-right">PRECIO_UNITARIO</span></th>
        <th Width="5%"><span class="pull-right">DESCUENTO</span></th>
        <th Width="10%"><span class="pull-right">TOTAL</span></th>
        <th width="5%"></th>
    </tr>
    <?php
        $sql=mysqli_query($con,"SELECT sum(veab_abono) as total from venta_abonos where ventas_ven_id='$ven_id'");
        $sql=mysqli_fetch_array($sql);
        $total_abono=$sql['total'];

        $total_real=0;
        $sql=mysqli_query($con,"SELECT * from venta_detalle where ventas_ven_id='$ven_id'");
        while ($row=mysqli_fetch_array($sql))
        {
            $vend_cantidad_dev=$row['vend_cantidad_dev'];
            $vend_precio=$row['vend_precio'];
            $vend_iva=$row['vend_iva'];
            $vend_desc=$row['vend_desc'];
            $precio_total_dev=$vend_precio*$vend_cantidad_dev;
            $total_real+=$precio_total_dev;//Sumador
        }

      $sumador_total=0;
      $sql=mysqli_query($con, "SELECT * from venta_detalle where ventas_ven_id='$ven_id'");
      while ($row=mysqli_fetch_array($sql))
      {
        $id=$row["vend_id"];
        $vend_id=$row["vend_id"];
        $vend_art_id=$row["vend_art_id"];
        $vend_referencia=$row["vend_referencia"];
        $vend_nombre=$row['vend_nombre'];
        $vend_cantidad=$row['vend_cantidad'];
        $vend_cantidad_dev=$row['vend_cantidad_dev'];
        $vend_precio=$row['vend_precio'];
        $vend_iva=$row['vend_iva'];
        $vend_desc=$row['vend_desc'];
        $descuento=($vend_desc/100)*$vend_precio;

        $precio_total=$vend_precio*$vend_cantidad;
        $sumador_total+=$precio_total;//Sumador
        ?>
    <tr>
        <td><?php echo $vend_referencia;?></td>
        <td><?php echo $vend_cantidad;?></td>
        <td><p style="color: red"><?php echo $resta=$vend_cantidad-$vend_cantidad_dev;?></p></td>
        <td><?php echo $vend_nombre;?></td>
        <td><span class="pull-right"><?php echo number_format($vend_precio,0);?></span></td>
        <td><span class="pull-right"><?php echo number_format($descuento,0);?></span></td>
        <td><span class="pull-right"><?php echo number_format($precio_total,0);?></span></td>
        <td>
            <a data-id="<?php echo $id;?>" title="Devolucion" class="btn btn-info btn-devo btn-xs"> <i class="btn-icon-only fas fa-reply-all"></i></a>
        </td>  
    </tr>  
    <?php } ?>
</table>
    
<script>
    $(".btn-devo").click(function(){
        id = $(this).data("id");
        $.get("./php_pos/forms_devolucion.php","id="+id,function(data){
            $("#form-devolucion").html(data);
        });
        $('#devolucion').modal('show');
    });
</script>
  

<script>
function eliminar (dev_id)
{
    if (confirm("Realmente deseas eliminar la devolucion")){ 
    $.ajax({
        type: "POST",
        url: "php_pos/eliminar_devolucion.php",
        data: "dev_id="+dev_id,

        beforeSend: function(objeto){
         $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        $("#resultados").html(datos);
        load(1);
        loadtabla();
        }
        });
    }
} 
</script>