<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');

$ven_id=$_GET['id']; 
$sql=mysqli_query($con, "SELECT * from ventas where ven_id='$ven_id' and empresa='$empresa'");
$reg=mysqli_fetch_array($sql);
$Cliente_clie_id=$reg['Cliente_clie_id'];
$cli=mysqli_query($con, "SELECT * from cliente where clie_id='$Cliente_clie_id' and empresa='$empresa'");
$cli=mysqli_fetch_array($cli);
$clie_id=$cli['clie_id'];
$clie_nombre=$cli['clie_nombre'];

    //total factura
$sumador_total=0;
$total_art=0;
$total_combos=0;
$descuento_total=0; 
$sql1=mysqli_query($con, "SELECT * from venta_detalle where ventas_ven_id='$ven_id'");
while ($row1=mysqli_fetch_array($sql1))
{
    $vend_precio=$row1['vend_precio_real'];
    $vend_cantidad=$row1['vend_cantidad'];
    $vend_descuento=$row1['vend_desc'];
    $precio_total=$vend_precio*$vend_cantidad;
    $descuento=($vend_descuento/100)*$precio_total;
    $descuento_total+=$descuento;
    $total_art+=$precio_total;//Sumador
}

$descuento_combo_total=0;
$neto_combo=0;
$total_iva_combo=0;
$sumador_combo=0;
$sql1=mysqli_query($con, "SELECT * from venta_detalle_combo where ven_id='$ven_id'");
while ($row1=mysqli_fetch_array($sql1))
{
    $combo_cantidad=$row1["combo_cantidad"];
    $combo_precio=$row1['combo_precio'];
    if ($row1['combo_iva']>0){ $combo_iva=$row1['combo_iva']; } else { $combo_iva='0';}
    if ($row1['combo_desc']>0){ $combo_desc=$row1['combo_desc']; } else { $combo_desc='0';}
    $com_iva0=$combo_iva/100; //0.19
    $com_iva1=1+$com_iva0; //1.19

    $precio_total_item_com=$combo_precio*$combo_cantidad;//precio normal 1.000.000
    $precio_total_sin_iva_item_com=($combo_precio/$com_iva1)*$combo_cantidad; //precio sin iva  840.336
    $descuento_item_com=($combo_desc/100)*$precio_total_sin_iva_item_com; //descuento_item 84.033
    $total_iva_item_com=($precio_total_sin_iva_item_com-$descuento_item_com)*$com_iva0; // total iva  159.336

    $neto_combo+=$precio_total_sin_iva_item_com;
    $descuento_combo_total+=$descuento_item_com;
    $total_iva_combo+=$total_iva_item_com;

    $sumador_combo=($neto_combo+$total_iva_combo)-$descuento_combo_total;
}

$sumador_total=$total_art+$sumador_combo;

?>                
<form method="post" action="" id="anular_fac" name="anular_fac">
    <div class="form-group">
        <label>Factura N</label>
        <input type="hidden" name="sumador_total" value="<?php echo $sumador_total;?>" class="form-control name_list" autocomplete="off" required readonly>
        <input type="hidden" name="ven_estado" value="<?php echo $reg['ven_estado'];?>" class="form-control name_list" autocomplete="off" required readonly>
        <input type="hidden" id="ven_id" name="ven_id" value="<?php echo $reg['ven_id'];?>" class="form-control name_list" readonly>
        <input type="text" name="ven_n_venta" value="<?php echo $reg['ven_n_venta'];?>" class="form-control input-sm" autocomplete="off" required readonly>
        <label>Cliente</label>
        <input type="hidden" name="clie_id" value="<?php echo $clie_id;?>" class="form-control name_list" autocomplete="off" required readonly>
        <input type="text" name="clie_nombre" value="<?php echo $clie_nombre;?>" class="form-control input-sm" autocomplete="off" required readonly>
        <input type="hidden" name="clie_nota_credito" value="<?php echo $cli['clie_nota_credito'];?>" class="form-control name_list" autocomplete="off" required readonly >
        <label>Detalle</label>
        <textarea name="detalle" class="form-control input-sm" rows="3" required>Anulacion de la venta <?php echo $reg['ven_n_venta'];?></textarea>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
        <button type="submit" name="btn_anular" class="btn btn-success" value="btn_anular" id="btn_anular">Anular</button>
    </div>
</form>

<script>
$( "#anular_fac" ).submit(function( event ) {
$('#btn_anular').attr("disabled", true);

var parametros = $(this).serialize();
   $.ajax({
      type: "POST",
      url: "php_pos/anular.php",
      data: parametros,
        beforeSend: function(objeto){
         $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        $("#resultados").html(datos);
        $('#btn_anular').attr("disabled", false);

        $('#anular').modal('hide');
        $('body').removeClass('modal-open');
        $('.modal-backdrop').remove();
       load(1);
      }
  });
  event.preventDefault();
})
</script>