<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php'); 
$art_barras=$_POST['art_barras'];
$pen_id=$_POST['pen_id'];
$can=mysqli_query($con,"SELECT * FROM articulos where art_barras='$art_barras'  and empresa='$empresa'  AND art_barras <> '' 
      AND art_barras IS NOT NULL") or die(mysqli_error($con)); 
$row=mysqli_fetch_array($can);
$count_row2=mysqli_num_rows($can);
if ($count_row2<=0) 
{
?>
    <script type='text/javascript'>
        alert('No hay articulo registrado con ese codigo');
        loadTabla();
        loadtotal();
        $("#art_barras").focus();
        document.getElementById("barras").reset();
    </script>
<?php 
}
else
{
    if($modal_cantidad=='si')
    { 
        $art_id=$row['art_id'];
        $art_atributo_estado=$row['art_atributo_estado'];
        $art_atributo1=$row['art_atributo1'];
        $art_atributo2=$row['art_atributo2'];
        $art_lote=$row['art_lote'];
?>
        <script type="text/javascript">
        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_Barras"></input>
        <div id="myModal_Barras" class="modal fade" role="dialog">
            <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">Cantidad</h4>
                    </div>
                    <div id="resultado"></div>
                    <form method="post" action="" name="pesar" id="pesar">
                        <div class="modal-body"> 
                            <div class="alert alert-danger alert-dismissable">
                                <strong><?php echo $row['art_nombre']; ?></strong> 
                            </div>

                            <?php
                            if($art_atributo_estado=='si') 
                            {       
                                $datoss=mysqli_query($con,"SELECT * FROM articulos_has_bodegas where articulos_art_id='$art_id' AND sucursal='$sucursal'") ;
                                while( $filas=mysqli_fetch_array($datoss))
                                {   ?>
                                    <input type="radio" id="<?php echo $filas['art_id']; ?>" name="art_art_id" value="<?php echo $filas['art_id']; ?>" >
                                   
                                <?php   if ($art_atributo1<>null) 
                                    {
                                        $atributo1=$filas['atributo1'];
                                        if ($atributo1=='0')
                                        {
                                            echo '&nbsp;&nbsp;SIN '; echo $art_atributo1; echo' ';
                                            echo '<i style="color:red;">';
                                           // echo $atri1['atri_nombre'];
                                            echo '</i>';
                                            echo '/';
                                        } 
                                        else
                                        {
                                            $atri1=mysqli_query($con,"SELECT * FROM atributos1 where atri_art_id='$art_id' and atri_id='$atributo1'");
                                            $atri1 = mysqli_fetch_array($atri1);
                                            if (isset($atri1['atri_nombre'])) 
                                            {
                                                echo '&nbsp;&nbsp;';echo $art_atributo1; echo ' ';
                                                echo '<i style="color:red;">';
                                                echo $atri1['atri_nombre'];
                                                echo '</i>';
                                                echo ' ';
                                            }
                                        }
                                    }


                                    if ($art_atributo2<>null) 
                                    {
                                        $atributo2=$filas['atributo2'];
                                        if ($atributo2=='0')
                                        {
                                            echo 'SIN '; echo $art_atributo2; echo' ';
                                            echo '<i style="color:red;">';
                                           // echo $atri1['atri_nombre'];
                                            echo '</i>';
                                            echo ' ';
                                        } 
                                        else
                                        {
                                            $atri2=mysqli_query($con,"SELECT * FROM atributos2 where atri_art_id2='$art_id' and atri_id2='$atributo2'");
                                            $atri2 = mysqli_fetch_array($atri2);
                                            if (isset($atri2['atri_nombre2'])) 
                                            {
                                                echo $art_atributo2;echo ' ';
                                                echo '<i style="color:red;">';
                                                echo $atri2['atri_nombre2'];
                                                echo '</i>';
                                                echo ' ';
                                            }
                                        }
                                    }

                                    echo ': ';
                                    echo $cantidad=$filas['art_cantidad']; 
                                ?>
                                </br>
                                    <?php
                                } 
                            }
                            else 
                            {
                                $can7=mysqli_query($con,"SELECT *, sum(art_cantidad) as art_cantidad FROM articulos_has_bodegas WHERE articulos_art_id='$art_id' and sucursal='$sucursal' ") or die(mysqli_error($con));
                                $row7=mysqli_fetch_array($can7);
                                $cantidad=$row7['art_cantidad'];
                            ?>
                                <input type="hidden" id="art_art_id" name="art_art_id" value="<?php echo $row7['art_id']; ?>" class="form-control name_list" autocomplete="off" required >
                               
                        <?php  }  ?>

                            <div class="form-group">
                                <input type="hidden" id="pen_id" name="pen_id" value="<?php echo $pen_id; ?>" required >
                                <input type="hidden" id="art_id" name="art_id" value="<?php echo $art_id; ?>" required >
                                <label>Cantidad</label>
                                <input type="text" id="peso" name="peso" value="" class="form-control" autocomplete="off" required autofocus >
                            </div>
                            <div class="modal-footer">
                               <button type="submit" class="btn btn-info" name="cargar" value="cargar" id="cargar">Agregar</button>
                            </div>  
                        </div>
                    </form>  
                </div>
            </div>
        </div>
        <script>
            $('#myModal_Barras').on('shown.bs.modal', function() {
                $("#peso").focus();
            });
            $("#pesar").submit(function( event ) {
            $('#cargar').attr("disabled", true);
            document.getElementById("barras").reset();

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

                $('#myModal_Barras').modal('hide');
                $('body').removeClass('modal-open');
                $('.modal-backdrop').remove();
                loadTabla();
                loadtotal();
                $("#art_barras").focus();
                }
              });
              event.preventDefault();
            })
        </script> 
<?php  
    }  
    else
    {
        $art_id=$row['art_id'];
        $art_atributo_estado=$row['art_atributo_estado'];
        $art_atributo1=$row['art_atributo1'];
        $art_atributo2=$row['art_atributo2'];
        $art_lote=$row['art_lote'];
        if($art_atributo_estado=='si') 
        { 
?>
            <script type="text/javascript">
            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_Barras"></input>
            <div id="myModal_Barras" class="modal fade" role="dialog">
                <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">Cantidad</h4>
                        </div>
                        <div id="resultado"></div>
                        <form method="post" action="" name="pesar" id="pesar">
                            <div class="modal-body"> 
                                <div class="alert alert-danger alert-dismissable">
                                    <strong><?php echo $row['art_nombre']; ?></strong> 
                                </div>
                                <?php
                                if($art_atributo_estado=='si') 
                                {       
                                    $datoss=mysqli_query($con,"SELECT * FROM articulos_has_bodegas where articulos_art_id='$art_id' AND sucursal='$sucursal'") ;
                                    while( $filas=mysqli_fetch_array($datoss))
                                    {   ?>
                                        <input type="radio" id="<?php echo $filas['art_id']; ?>" name="art_art_id" value="<?php echo $filas['art_id']; ?>" >
                                       
                                    <?php   if ($art_atributo1<>null) 
                                        {
                                            $atributo1=$filas['atributo1'];
                                            if ($atributo1=='0')
                                            {
                                                echo '&nbsp;&nbsp;SIN '; echo $art_atributo1; echo' ';
                                                echo '<i style="color:red;">';
                                               // echo $atri1['atri_nombre'];
                                                echo '</i>';
                                                echo '/';
                                            } 
                                            else
                                            {
                                                $atri1=mysqli_query($con,"SELECT * FROM atributos1 where atri_art_id='$art_id' and atri_id='$atributo1'");
                                                $atri1 = mysqli_fetch_array($atri1);
                                                if (isset($atri1['atri_nombre'])) 
                                                {
                                                    echo '&nbsp;&nbsp;';echo $art_atributo1; echo ' ';
                                                    echo '<i style="color:red;">';
                                                    echo $atri1['atri_nombre'];
                                                    echo '</i>';
                                                    echo ' ';
                                                }
                                            }
                                        }


                                        if ($art_atributo2<>null) 
                                        {
                                            $atributo2=$filas['atributo2'];
                                            if ($atributo2=='0')
                                            {
                                                echo 'SIN '; echo $art_atributo2; echo' ';
                                                echo '<i style="color:red;">';
                                               // echo $atri1['atri_nombre'];
                                                echo '</i>';
                                                echo ' ';
                                            } 
                                            else
                                            {
                                                $atri2=mysqli_query($con,"SELECT * FROM atributos2 where atri_art_id2='$art_id' and atri_id2='$atributo2'");
                                                $atri2 = mysqli_fetch_array($atri2);
                                                if (isset($atri2['atri_nombre2'])) 
                                                {
                                                    echo $art_atributo2;echo ' ';
                                                    echo '<i style="color:red;">';
                                                    echo $atri2['atri_nombre2'];
                                                    echo '</i>';
                                                    echo ' ';
                                                }
                                            }
                                        }
                                        echo ': ';
                                        echo $cantidad=$filas['art_cantidad']; 
                                    ?>
                                    </br>
                        <?php       }   
                                }
                                else 
                                {
                                    $can7=mysqli_query($con,"SELECT *, sum(art_cantidad) as art_cantidad FROM articulos_has_bodegas WHERE articulos_art_id='$art_id' and sucursal='$sucursal' ") or die(mysqli_error($con));
                                    $row7=mysqli_fetch_array($can7);
                                    $cantidad=$row7['art_cantidad'];
                                ?>
                                    <input type="hidden" id="art_art_id" name="art_art_id" value="<?php echo $row7['art_id']; ?>" class="form-control name_list" autocomplete="off" required>
                        <?php   }   ?>

                                <div class="form-group">
                                    <input type="hidden" id="pen_id" name="pen_id" value="<?php echo $pen_id; ?>" required >
                                    <input type="hidden" id="art_id" name="art_id" value="<?php echo $art_id; ?>" required >
                                    <label>Cantidad</label>
                                    <input type="text" id="peso" name="peso" value="" class="form-control name_list" autocomplete="off" required autofocus="">
                                </div>
                                <div class="modal-footer">
                                   <button type="submit" class="btn btn-info" name="cargar" value="cargar" id="cargar">Si</button>
                                </div>  
                            </div>
                        </form>  
                    </div>
                </div>
            </div>
            <script>
                $('#myModal_Barras').on('shown.bs.modal', function() {
                    $("#peso").focus();
                });
                $("#pesar").submit(function( event ) {
                $('#cargar').attr("disabled", true);
                document.getElementById("barras").reset();

                var parametros = $(this).serialize();
                $.ajax({
                    type: "POST",
                    url: "php_pedidos/agregar_articulo_barras1.php",
                    data: parametros,
                        beforeSend: function(objeto){
                         $('#tabla').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
                        },
                    success: function(datos){
                    $("#tabla").html(datos);
                      
                    $('#cargar').attr("disabled", false);
                    $('#myModal_Barras').modal('hide');
                    $('body').removeClass('modal-open');
                    $('.modal-backdrop').remove();
                    loadTabla();
                    loadtotal();
                    $("#art_barras").focus();
                }
                });
                event.preventDefault();
                })
            </script> 

<?php   }  
        else   
        { 
            $cantidad=1;
            $art_id=$row['art_id'];
            $sql_pre=mysqli_query($con, "SELECT * from articulos WHERE art_id='$art_id' and empresa='$empresa' ");
            $rw_pre= mysqli_fetch_array($sql_pre);
            $art_referencia=$rw_pre['art_referencia'];
            $nombre=$rw_pre['art_nombre'];
            if ($rw_pre['art_iva']>0){$art_iva=$rw_pre['art_iva'];} else { $art_iva='0';}

            $art_atributo_estado=$rw_pre['art_atributo_estado'];

            if($art_atributo_estado=='si')
            { 
                $atributo1=0;
                $atributo2=0;

                $can2=mysqli_query($con,"SELECT * from articulos_has_bodegas where articulos_art_id='$art_id' and atributo1='$atributo1' and atributo2='$atributo2'") or die(mysqli_error($con)); 
                $dato2=mysqli_fetch_array($can2);
                $art_art_id=$dato2['art_id'];
            }
            else 
            {
                $atributo1=0;
                $atributo2=0;

                $can2=mysqli_query($con,"SELECT * from articulos_has_bodegas where articulos_art_id='$art_id' and atributo1='$atributo1' and atributo2='$atributo2'") or die(mysqli_error($con)); 
                $dato2=mysqli_fetch_array($can2);
                $art_art_id=$dato2['art_id'];
            }


            $sql_tar=mysqli_query($con, "SELECT * from cliente c,pedido cc where c.clie_id=cc.clie_id and cc.usuario='$usu_id' and cc.empresa='$empresa'");
            $rw_tar=mysqli_fetch_array($sql_tar);
            $tarifas_tar_id=$rw_tar['tarifas_tar_id'];

            $pre=mysqli_query($con,"SELECT * from precio WHERE articulos_art_id='$art_id' and tarifas_tar_id='$tarifas_tar_id' and empresa='$empresa'");
            $pre= mysqli_fetch_array($pre);
            $numrows = $pre['pre_id'];
            if ($numrows>0)
            {
                $pre_precio=$pre['pre_precio'];
            }
            else
            {
                $pre_precio=$rw_pre['art_pvp'];
            }

            $sqll=mysqli_query($con,"SELECT pend_art_id FROM pedido_detalle WHERE pend_art_id='$art_id' and pedido_pen_id='$pen_id' and pend_estado='facturado'");
            if($roww=mysqli_fetch_array($sqll))
            { 
                $can7=mysqli_query($con,"SELECT * FROM pedido_detalle WHERE pend_art_id='$art_id' and pedido_pen_id='$pen_id' and pend_estado='facturado'") or die(mysqli_error($con));  
                while($dato=mysqli_fetch_array($can7))
                {
                    $c=$dato['pend_cantidad'];
                    $suma=$c+$cantidad;
                }
                $registros=mysqli_query($con,"UPDATE pedido_detalle Set pend_cantidad='$suma' 
                    Where pend_art_id='$art_id' and pedido_pen_id='$pen_id' and pend_estado='facturado'") or die(mysqli_error($con));
            }
            else
            {   
                $insert_tmp=mysqli_query($con, "INSERT INTO pedido_detalle (art_id_bod,pend_art_id,pend_cantidad,pend_nombre,pend_precio,pend_iva,pend_descuento,pend_tipo,pend_estado,pend_archivado,pedido_pen_id) 
                    VALUES ('$art_art_id','$art_id','$cantidad','$nombre','$pre_precio','$art_iva','0','articulo','no','facturado','$pen_id')") or die(mysqli_error($con));  
            } 
     ?>
        <script >
            loadTabla();
            loadtotal();
            $("#art_barras").focus();
            document.getElementById("barras").reset();
        </script>

<?php   }  }  } ?>