<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php'); 
$art_barras=$_POST['art_barras'];

$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) 
{
    $codigo=substr( $art_barras, 2,5 );       
    $peso=substr( $art_barras, 7,5 ); 
    $cantidad=$peso/1000;

    $can2=mysqli_query($con,"SELECT * FROM articulos where art_barras='$codigo'  and empresa='$empresa'  AND art_barras <> '' 
      AND art_barras IS NOT NULL") or die(mysqli_error($con)); 

    $row2= mysqli_fetch_array($can2);
    $count_row2=mysqli_num_rows($can2);
    if($count_row2>0)
    {
        $art_id=$row2['art_id'];
        $art_referencia=$row2['art_referencia'];
        $nombre=$row2['art_nombre'];
        if ($row2['art_iva']>0){$art_iva=$row2['art_iva'];} else { $art_iva='0';}
        $art_tipo=$row2['art_tipo'];

        $sql_tar=mysqli_query($con, "SELECT * from cliente c,cliente_venta_temp cc where c.clie_id=cc.cvt_clie_id and cc.usuario='$usu_id' and cc.empresa='$empresa'");
        if ($rw_tar= mysqli_fetch_array($sql_tar))
        {
            $tarifas_tar_id=$rw_tar['tarifas_tar_id'];
        }
        else 
        {
            $clim=mysqli_query($con,"SELECT * FROM cliente where clie_nombre='CLIENTE MOSTRADOR'") or die(mysqli_error($con)); 
            $clim=mysqli_fetch_array($clim);
            $tarifas_tar_id=$clim['tarifas_tar_id'];
        }


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

        $art_bod=mysqli_query($con,"SELECT * FROM articulos_has_bodegas where articulos_art_id='$art_id' and atributo1='0' and atributo2='0' ");
        $art_bod1 = mysqli_fetch_array($art_bod);
        $art_art_id=$art_bod1['art_id'];

       



        $can5=mysqli_query($con,"SELECT vent_art_id FROM venta_detalle_temp WHERE vent_art_id='$art_id' and empresa='$empresa' and usuario='$usu_id'");
        if($row5=mysqli_fetch_array($can5))
        { 
            $can6=mysqli_query($con,"SELECT * FROM venta_detalle_temp WHERE vent_art_id='$art_id' and empresa='$empresa' and usuario='$usu_id'") or die(mysqli_error($con));  
            while($row6=mysqli_fetch_array($can6))
            {
                $cantidad_anterior=$row6['vent_cantidad'];
                $suma=$cantidad_anterior+$cantidad;
            }
            $registros=mysqli_query($con,"UPDATE venta_detalle_temp Set vent_cantidad='$suma',vent_precio='$pre_precio' 
                Where vent_art_id='$art_id' and empresa='$empresa' and usuario='$usu_id' ") or die(mysqli_error($con));
        }
        else
        {   
            $can2=mysqli_query($con,"SELECT * FROM articulos_presentacion where pres_art_id='$art_id' and pres_defecto='si' ") or die(mysqli_error($con)); 
            $dato2=mysqli_fetch_array($can2);
            $pres_unidad=$dato2['pres_unidad'];

            $insert_tmp=mysqli_query($con, "INSERT INTO venta_detalle_temp (art_art_id,vent_art_id,vent_referencia,vent_nombre,vent_unidad,vent_x_cantidad,vent_cantidad,vent_iva,vent_desc,vent_precio,vent_tipo,usuario,empresa) 
            VALUES ('$art_art_id','$art_id','$art_referencia','$nombre','$pres_unidad','1','$cantidad','$art_iva','0','$pre_precio','$art_tipo','$usu_id','$empresa')") or die(mysqli_error($con));  
        }
    }
    else
    { ?>
        <script type='text/javascript'>
            alert('No hay articulo registrado con ese codigo');
            loadTabla();
            loadcliente();
            loadtotal();
            loadbotones();
            $("#art_barras").focus();
            document.getElementById("barras").reset();
        </script>
<?php    }
?>
<script>
    loadTabla();
    loadcliente();
    loadtotal();
    loadbotones();
    $("#art_barras").focus();
    document.getElementById("barras").reset();
</script>
<?php 
}
else
{
    $ventana=$modal_cantidad;
    if($ventana=='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); 
         $('#myModal_Barras').on('shown.bs.modal', function() {
            $("#peso").focus();
            });
        </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 autofocus="">
                               
                        <?php  }  ?>

                            <div class="form-group">
                                <input type="hidden" id="art_id" name="art_id" value="<?php echo $art_id; ?>" class="form-control name_list" autocomplete="off" required autofocus="">
                                <label>Cantidad</label>
                                <input type="number" id="peso" name="peso" value="" step="0.0001" min="0.0001" 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>
           
            $("#pesar").submit(function( event ) {
            $('#cargar').attr("disabled", true);
            document.getElementById("barras").reset();

            var parametros = $(this).serialize();
            $.ajax({
                type: "POST",
                url: "php_facturar/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').modal('hide');
                $('body').removeClass('modal-open');
                $('.modal-backdrop').remove();
                loadTabla();
                loadcliente();
                loadtotal();
                loadbotones();
                $("#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 autofocus="">
                               
                        <?php  }  ?>


                            <div class="form-group">
                                <input type="hidden" id="art_id" name="art_id" value="<?php echo $art_id; ?>" class="form-control name_list" autocomplete="off" required autofocus="">
                                <label>Cantidad</label>
                               <input type="number" id="peso" name="peso" value="" step="0.0001" min="0.0001" 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').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_facturar/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').modal('hide');
                $('body').removeClass('modal-open');
                $('.modal-backdrop').remove();
                loadTabla();
                loadcliente();
                loadtotal();
                loadbotones();
                $("#art_barras").focus();
                }
              });
              event.preventDefault();
            })
        </script> 

        <?php   }  else   { 

        $art_id=$row['art_id'];
        $art_referencia=$row['art_referencia'];
        $nombre=$row['art_nombre'];
        if ($row['art_iva']>0){$art_iva=$row['art_iva'];} else { $art_iva='0';}
        $vent_tipo=$row['art_tipo'];

        $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);
        $art_art_id=$row7['art_id'];
        
       
        $can3=mysqli_query($con, "SELECT * from cliente c,cliente_venta_temp cc where c.clie_id=cc.cvt_clie_id and cc.usuario='$usu_id' and cc.empresa='$empresa'");
        if ($row3= mysqli_fetch_array($can3))
        {
            $tarifas_tar_id=$row3['tarifas_tar_id'];
        }
        else 
        {
            $can4=mysqli_query($con,"SELECT * FROM cliente where clie_nombre='CLIENTE MOSTRADOR'") or die(mysqli_error($con)); 
            $row4=mysqli_fetch_array($can4);
            $tarifas_tar_id=$row4['tarifas_tar_id'];
        }

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



        $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' ") ;
        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' ") or die(mysqli_error($con));  
            while($dato=mysqli_fetch_array($can7))
            {
                $cantidad_disponible=$dato['vent_cantidad'];
                $suma=$cantidad_disponible+1;
            }
            $registros=mysqli_query($con,"UPDATE venta_detalle_temp Set vent_cantidad='$suma',vent_precio='$pre_precio' 
                Where vent_art_id='$art_id' and empresa='$empresa' and usuario='$usu_id'") or die(mysqli_error($con));
        }
        else
        {   
            $can2=mysqli_query($con,"SELECT * FROM articulos_presentacion where pres_art_id='$art_id' and pres_defecto='si' ") or die(mysqli_error($con)); 
            $dato2=mysqli_fetch_array($can2);
            $pres_unidad=$dato2['pres_unidad'];

            $insert_tmp=mysqli_query($con, "INSERT INTO venta_detalle_temp (art_art_id,vent_art_id,vent_referencia,vent_nombre,vent_unidad,vent_x_cantidad,vent_cantidad,vent_iva,vent_desc,vent_precio,vent_tipo,usuario,empresa) 
                VALUES ('$art_art_id','$art_id','$art_referencia','$nombre','$pres_unidad','1','1','$art_iva','0','$pre_precio','$vent_tipo','$usu_id','$empresa')") or die(mysqli_error($con));  
        } ?>
        <script >
            loadTabla();
            loadcliente();
            loadtotal();
            loadbotones();
            $("#art_barras").focus();
            document.getElementById("barras").reset();
        </script>

<?php   }  }  } ?>