<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$vena_id=$_GET['vena_id'];
?>
    <table class="table table-bordered table-hover table-striped">
        <tr>
            <th width="10%">CODIGO</th>
            <th width="5%">CANTIDAD</th>
            <th width="5%">UM</th>
            <th ><span class="pull-center">DESCRIPCIÓN</span></th>
            <th width="10%"><span class="pull-right">PRECIO U</span></th>
            <th width="5%"><span class="pull-right">DESCUENTO</span></th>
            <th width="10%"><span class="pull-right">PRECIO T</span></th>
            <th  width="10%"></th>
            <?php
            $descuento=0;
            $sumador_total=0;
            $neto=0;
            $descuentof=0;
            $iva_total=0;
            $tt=0;
            $to=0;
            $sqls=mysqli_query($con,"SELECT * from venta_apartadas_detalle where ventas_apartadas_vena_id='$vena_id'");
            while ($rows=mysqli_fetch_array($sqls))
            {
                $id=$rows["venad_id"];
                $venad_id=$rows["venad_id"];
                $svenad_referencia=$rows['venad_referencia'];
                $venad_cantidad=$rows['venad_cantidad'];
                $venad_nombre=$rows['venad_nombre'];
                $venad_unidad=$rows['venad_unidad'];
                $venad_iva=$rows['venad_iva'];
                $venad_desc=$rows['venad_desc'];
                $venad_precio=$rows['venad_precio'];

                $ivaf=$venad_iva/100;
                $precio_total=$venad_precio*$venad_cantidad;
                $descuento=($venad_desc/100)*$precio_total;
                $pt=$precio_total-$descuento;
                $iva=$venad_precio*$ivaf;
                $sin_iva=$venad_precio-$iva;
                $sumador_total+=$precio_total;//Sumador
                $tt=$sin_iva*$venad_cantidad;

                $descuentof+=$descuento;
                $ivat=($venad_precio*$ivaf)*$venad_cantidad;
                $sin_ivat=($venad_precio-$iva)*$venad_cantidad;
                $iva_total+=$ivat;
                $neto+=$sin_ivat;
        ?>
        <tr>
            <td ><?php echo $svenad_referencia;?></td>
            <td ><?php echo $venad_cantidad;?></td>
            <td ><?php echo $venad_unidad;?></td>
            <td ><?php echo $venad_nombre;?></td>
            <td ><span class="pull-right"><?php echo number_format($venad_precio,0);?></span></td>
            <td ><span class="pull-right"><?php echo $venad_desc;?></span></td>
            <td style="text-align: right; "><span class="pull-right"><?php echo number_format($precio_total,0);?></span></td>
            <td>  
                <a data-id="<?php echo $id;?>" class="btn btn-edit btn-xs btn-warning"> <i class="btn-icon-only icon-edit"></i></a>
                <!-- <a href="#" data-id="<?php //echo $id;?>" class="btn btn-xs btn-success btn-serial"><i class="btn-icon-only icon-barcode"></i></a>-->
                <?php
                if($negocio_neg_id=='1')
                {
                    $can1=mysqli_query($con,"SELECT * FROM seriales_apartados where venad_id='$venad_id'") or die(mysqli_error($con));
                    $dat=mysqli_fetch_array($can1);
                    $result=mysqli_num_rows($can1);
                    if($result==0)
                    {
                ?>
                    <a data-id="<?php echo $id;?>" class="btn btn-ser btn-xs btn-info" title="Serial"><i class="btn-icon-only icon-barcode"></i></a>
                <?php } else { ?>
                    <a data-id="<?php echo $id;?>" data-toggle="modal" class="btn btn-ser_tabla btn-xs btn-info" title="Serial"> <i class="btn-icon-only icon-barcode"></i></a>
                <?php } }?>
                <a href="#" id="del-<?php echo $rows["venad_id"];?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
                <script>
                $("#del-"+<?php echo $rows["venad_id"];?>).click(function(e){
                    e.preventDefault();
                    p = confirm("Estas seguro?");
                    if(p){
                        $.get("./php_apartados/eliminar_articulo.php","venad_id="+<?php echo $rows["venad_id"];?>,function(data){
                             loadtabla();
                        });
                    }

                });
                </script>

            </td>
        </tr>   
        <?php } $to+=$sumador_total; 

        $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_total-$abono;

        ?>
        <tr>
            <td colspan=6><span class="pull-right"><strong>TOTAL $</strong></span></td>
            <td><span class="pull-right"><strong><?php echo number_format($to,0);?></strong></span></td>
            <td></td>
        </tr>
         <tr>
            <td colspan=6><span class="pull-right"><strong>DESCUENTO $</strong></span></td>
            <td><span class="pull-right"><strong><?php echo number_format($descuentof,0);?></strong></span></td>
            <td></td>
        </tr>
         <tr>
            <td colspan=6><span class="pull-right"><strong>TOTAL $</strong></span></td>
            <td><span class="pull-right"><strong><?php echo number_format($to-$descuentof,0);?></strong></span></td>
            <td></td>
        </tr>
         <tr>
            <td colspan=6><span class="pull-right"><strong><p style="color: green;">ABONO $</p></strong></span></td>
            <td><span class="pull-right"><strong><?php echo number_format($abono,0);?></strong></span></td>
            <td></td>
        </tr>
         <tr>
            <td colspan=6><span class="pull-right"><strong><p style="color: red;">SALDO $</p></strong></span></td>
            <td><span class="pull-right"><strong><?php echo number_format($saldo,0);?></strong></span></td>
            <td></td>
        </tr>
    </table>
<script>
    $(".btn-edit").click(function(){
        id = $(this).data("id");
        $.get("./php_apartados/form_modificar.php","id="+id,function(data){
            $("#form-edit").html(data);
        });
        $('#editModal').modal('show');
    });
</script>
<div class="modal fade" id="editModal" 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">ACTUALIZAR</h4>
            </div>
            <div class="modal-body">
                <div id="form-edit"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script>
$(".btn-ser").click(function(){
  id = $(this).data("id");
  $.get("./php_apartados/form_seriales.php","id="+id,function(data){
    $("#form-ser").html(data);
  });
  $('#serModal').modal('show');
});
</script>
<div class="modal fade" id="serModal" 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">Agregar Seriales nu</h4>
            </div>
            <div class="modal-body">
                <div id="form-ser"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal --> 


<script>
$(".btn-ser_tabla").click(function(){
  id = $(this).data("id");
  $.get("./php_apartados/form_tabla_serial.php","id="+id,function(data){
    $("#form-tabla_serial").html(data);
  });
  $('#reg_ser_tabla').modal('show');
});
</script>
<div class="modal fade" id="reg_ser_tabla" 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">Seriales</h4>
            </div>
            <div class="modal-body">
                <div id="form-tabla_serial"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



<div class="modal fade" id="serModal_n" 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">Agregar Seriales nuevo</h4>
            </div>
            <div class="modal-body">
                <div id="form-ser_n"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal --> 