<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$ven_id=$_GET['ven_id'];
?>
<div class="table-responsive">
    <table class="table table-bordered table-hover table-striped">    
        <tr>
        <th width="5%">CODIGO</th>
        <th Width="5%">CANTIDAD</th>
        <th>DESCRIPCION</th>
        <th Width="5%"><span class="pull-right">PRECIO_UNITARIO</span></th>
        <th width="10%"></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;
    $sql2=mysqli_query($con, "SELECT * from venta_detalle where ventas_ven_id='$ven_id'");
    while ($row2=mysqli_fetch_array($sql2))
    {
        $id=$row2["vend_id"];
        $vend_id=$row2["vend_id"];
        $vend_art_id=$row2["vend_art_id"];
        $vend_referencia=$row2["vend_referencia"];
        $vend_nombre=$row2['vend_nombre'];
        $vend_cantidad=$row2['vend_cantidad'];
        $vend_cantidad_dev=$row2['vend_cantidad_dev'];
        $vend_precio=$row2['vend_precio'];
        $vend_iva=$row2['vend_iva'];
        $vend_desc=$row2['vend_desc'];
        $descuento=($vend_desc/100)*$vend_precio;

        $precio_total=$vend_precio*$vend_cantidad;
        $sumador_total+=$precio_total;//Sumador
?>
<input type="hidden" id="ven_id" value="<?php echo $ven_id;?>">
<input type="hidden" id="vend_art_id<?php echo $vend_id;?>" value="<?php echo $vend_art_id;?>">
<input type="hidden" id="vend_referencia<?php echo $vend_id;?>" value="<?php echo $vend_referencia;?>">
<input type="hidden" id="vend_nombre<?php echo $vend_id;?>" value="<?php echo $vend_nombre;?>">
<input type="hidden" id="vend_precio<?php echo $vend_id;?>" value="<?php echo $vend_precio-$descuento;?>">
<input type="hidden" id="vend_iva<?php echo $vend_id;?>" value="<?php echo $vend_iva;?>">
<input type="hidden" id="vend_desc<?php echo $vend_id;?>" value="0">
    <tr>
        <td><?php echo $vend_referencia;?></td>
        <td><?php echo $vend_cantidad;?></td>
        <td><?php echo $vend_nombre;?></td>
        <td><span class="pull-right"><?php echo number_format($vend_precio-$descuento,0);?></span></td>
        <td><input type="text" class="form-control input-sm" id="cantidad<?php echo $vend_id;?>" value="1" max="<?php echo $vend_cantidad;?>" ></td>
        <td><a onclick="agregar_nota('<?php echo $vend_id ?>')" class="btn btn-info btn-devo btn-xs"><i class="btn-icon-only icon-plus"></i></a></a></td>
    </tr>   
    <?php } ?>
    </table>
</div>
<script>
function agregar_nota (vend_id)
{
    var ven_id=document.getElementById('ven_id').value;
    var vend_art_id=document.getElementById('vend_art_id'+vend_id).value;
    var vend_referencia=document.getElementById('vend_referencia'+vend_id).value;
    var vend_nombre=document.getElementById('vend_nombre'+vend_id).value;
    var vend_precio=document.getElementById('vend_precio'+vend_id).value;
    var vend_iva=document.getElementById('vend_iva'+vend_id).value;
    var vend_desc=document.getElementById('vend_desc'+vend_id).value;
    var cantidad=document.getElementById('cantidad'+vend_id).value;

    if (isNaN(cantidad))
    {
    alert('Esto no es un numero');
    document.getElementById('cantidad'+vend_id).focus();
    return false;
    }

    $.ajax({
    type: "POST",
    url: "./php_doc_nota_credito/nota_credito_agregar_temp.php",
    data: "vend_art_id="+vend_art_id+"&vend_referencia="+vend_referencia+"&vend_nombre="+vend_nombre+"&vend_precio="+vend_precio+"&vend_iva="+vend_iva+"&vend_desc="+vend_desc+"&cantidad="+cantidad+"&ven_id="+ven_id,

    beforeSend: function(objeto){
    $("#resultados_temp").html("Mensaje: Cargando...");
    },
    success: function(datos){
    $("#resultados_temp").html(datos);
    loadtablanota();
    }
    });
}
</script>