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

if (isset($_POST['art_id'])){ $art_id=$_POST['art_id'];}
if (isset($_POST['art_referencia'])){ $art_referencia=$_POST['art_referencia'];}
if (isset($_POST['cantidad'])){ $cantidad=$_POST['cantidad'];}
if (isset($_POST['precio_venta'])){ $precio_venta=$_POST['precio_venta'];}
if ($_POST['art_iva']>0){$art_iva=$_POST['art_iva'];} else { $art_iva='0';}
if ($_POST['descuento']>0){ $descuento=$_POST['descuento']; } else { $descuento='0';}
if (isset($_POST['nombre'])){$nombre=$_POST['nombre'];}
if (isset($_POST['vent_tipo'])){$vent_tipo=$_POST['vent_tipo'];}
  
/*    $sqll=mysqli_query($con,"SELECT vent_art_id FROM venta_detalle_temp WHERE vent_art_id='$art_id' and empresa='$empresa' and usuario='$usu_id' and detallado='no'");
    if($roww=mysqli_fetch_array($sqll))
    { 
        $can7=mysqli_query($con,"SELECT * FROM venta_detalle_temp WHERE vent_art_id='$art_id' and empresa='$empresa' and usuario='$usu_id' and detallado='no'") or die(mysqli_error($con));  
        while($dato=mysqli_fetch_array($can7))
        {
            $c=$dato['vent_cantidad'];
            $suma=$c+$cantidad;
        }
        $registros=mysqli_query($con,"UPDATE venta_detalle_temp Set vent_cantidad='$suma',vent_precio='$precio_venta' 
        Where vent_art_id='$art_id' and empresa='$empresa' and usuario='$usu_id' and detallado='no'") or die(mysqli_error($con));
    }
    else
    {   */
        $can=mysqli_query($con,"SELECT MAX(vent_id)as vent_id FROM venta_detalle_temp ") or die(mysqli_error($con)); 
        $dato=mysqli_fetch_array($can);
        $vent_id=$dato['vent_id']+1;

        $insert_tmp=mysqli_query($con, "INSERT INTO venta_detalle_temp (vent_id,art_art_id,vent_art_id,vent_referencia,vent_nombre,vent_cantidad,vent_iva,vent_desc,vent_precio,vent_tipo,usuario,empresa) 
        VALUES ('$vent_id','0','$art_id','$art_referencia','$nombre','$cantidad','$art_iva','$descuento','$precio_venta','$vent_tipo','$usu_id','$empresa')");
 //   }
$sab=mysqli_query($con,"SELECT * FROM sabores_productos WHERE articulo_art_id='$art_id' and empresa='$empresa'")or die(mysqli_error($con));
if ($res=mysqli_fetch_array($sab))
{
?>
<script type="text/javascript">
    $('#myModal').modal('hide');
    $('body').removeClass('modal-open');
    $('.modal-backdrop').remove();
    function redireccionar(){
    document.getElementById('id01').style.display='active';
    $("#id01").trigger("click");
} 
setTimeout ("redireccionar()", 100); 
</script>
<input id="id01" type="hidden" style="visibility: none;" data-toggle="modal" data-target="#myModal_sabor"></input>
<form name="form_producto" id="form_producto" >
    <div class="modal fade" id="myModal_sabor" 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">SABORES</h4>
                </div>
                <div class="modal-body">
                    <div id="resultado_sabor"></div>
                    <div class="">
                        <input type="hidden" name="vent_art_id" id="vent_art_id" value="<?php echo $art_id; ?>" >
                        <input type="hidden" name="vent_id" id="vent_id" value="<?php echo $vent_id; ?>" >                        
                        <?php
                           $sab=mysqli_query($con,"SELECT * FROM sabores_productos WHERE articulo_art_id='$art_id' and empresa='$empresa'")or die(mysqli_error($con));
                           while ($sab1=mysqli_fetch_array($sab))
                                {
                                    $sab_id=$sab1['sab_id'];
                        ?>
                        <input type="checkbox" id="<?php echo $sab1['sab_id']; ?>" name="sabor[]" value="<?php echo $sab1['sab_id']; ?>">
                        <label class="lab" for="<?php echo $sab1['sab_id']; ?>"><?php echo $sab1['sab_nombre']; ?></label>
                        <?php } ?>                            
                    </div> <!-- /.form-group -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                        <button type="submit" id="btn_producto" value="btn_producto" class="btn btn-success"><i class="icon-ok"></i> Registrar</button>        
                    </div>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
</form> 
<script type="text/javascript">
$("#form_producto").submit(function( event ) {
$('#btn_producto').attr("disabled", true);
var parametros = $(this).serialize();
    $.ajax({
      type: "POST",
    url: "php_facturar/registrar_sabor_producto_temp.php",
    data: parametros,
    beforeSend: function(objeto){
        $('#resultado_sabor').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
    },
    success: function(datos){
        $("#resultado_sabor").html(datos);
        $('#btn_producto').attr("disabled", false);
        document.getElementById("form_producto").reset();
        $('#myModal').modal('hide');
        $('body').removeClass('modal-open');
       $('.modal-backdrop').remove();
        loadTabla();
        loadtotal();
        }
    });
    event.preventDefault();
})
</script>
<?php } 
else
{ ?>
<script>
    loadtotal();
    loadTabla();
</script>
<?php } ?>