<?php
session_start();
require_once ("../conexion.php");
include '../include/sesiones.php';
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax'){
    $q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
	include '../include/pagination.php';
	$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
	$per_page = 5; 
	$adjacents  = 2; 
	$offset = ($page - 1) * $per_page;

	$count_query = mysqli_query($con, "SELECT count(*) AS numrows FROM articulos where empresa='$empresa' and art_tipo='articulo' and art_estado='activo' and (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%' OR art_referencia2 like '%$q%' )");
	$row= mysqli_fetch_array($count_query);
	$numrows = $row['numrows'];
	$total_pages = ceil($numrows/$per_page);
	$reload = './index.php';
	$sql = mysqli_query($con,"SELECT * FROM articulos where empresa='$empresa' and art_estado='activo' and art_tipo='articulo' and (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%' OR art_referencia2 like '%$q%' ) LIMIT $offset,$per_page");
	if ($numrows>0){
?>
<div class="table-responsive">
	<table class="table">
		<tr class="success">
			<th width="10%">CODIGO</th>
			<th >NOMBRE</th>
			<th width="6%"><span class="pull-right">STOCK</span></th>
			<th width="15%"><span class="pull-left">ATRIBUTOS</span></th>
			<th width="8%"><span class="pull-left">CANTIDAD</span></th>
			<th width="8%"><span class="pull-left">IMP</span></th>
			<th width="12%"><span class="pull-left">PRECIO</span></th>
			<?php if($facturador=='si') { ?>
			<th width="10%"><span class="pull-left">C. ADICIONAL</span></th>
			<?php } ?>
			<!--<th width="15%"><span class="pull-center">FECHA DE VCTO</span></th>-->
			<th width="5%"></th>
		</tr>
<?php
while ($row=mysqli_fetch_array($sql)){
	$id=$row['art_id'];
	$art_id=$row['art_id'];
	$art_referencia=$row['art_referencia'];
	$art_nombre=$row['art_nombre'];			
	$precio_costo=$row["art_precio_costo"];
	$art_iva=$row["art_iva"];
	$art_tipo_imp=$row["art_tipo_imp"];
	$art_atributo1=$row["art_atributo1"];
	$art_atributo2=$row["art_atributo2"];
	$iva0=$art_iva/100; //0.19
	$iva1=1+$iva0; //1.1
	if($art_tipo_imp=='4')
	{
		$art_precio_costo=$precio_costo;  //840.336
		$art_iva=0;
	}
	else
	{
		$art_precio_costo=$precio_costo/$iva1;  //840.336
		$art_iva=$row["art_iva"];
	}
	$art_lote=$row["art_lote"];


	$sql_tar=mysqli_query($con, "SELECT sum(art_cantidad) as art_cantidad from articulos_has_bodegas where articulos_art_id='$art_id' and empresa='$empresa' and bodegas='$bod_id' and sucursal='$sucursal'");
	$rw_tar=mysqli_fetch_array($sql_tar);
	if($rw_tar==null){
	$art_cantidad=0;
	}else{
	$art_cantidad=$rw_tar['art_cantidad'];
    }
	?>
		<tr>
			<td>
				<input type="hidden" id="art_referencia<?php echo $art_id; ?>" value="<?php echo $art_referencia; ?>" ><?php echo $art_referencia; ?>
			</td>
			<td>
				<input type="hidden" id="atributo_estado<?php echo $art_id; ?>" value="<?php echo $row["art_atributo_estado"]; ?>" >
				<input type="hidden" id="nombre_<?php echo $art_id; ?>" value="<?php echo $art_nombre; ?>" >
				<a href="#"  data-id="<?php echo $id;?>" class="btn-art"  title="Modificar" ><?php echo $art_nombre; ?></a>
			</td> 
			<td><?php echo $art_cantidad; ?></td>
			<td>
				<?php if($row["art_atributo_estado"]=='si') { ?>
					<?php if($art_atributo1<>null) { ?>
						<select id="atributo1_<?php echo $art_id; ?>" class="form-control input-sm">
	                        <option value="0">SIN <?php echo $art_atributo1; ?></option>
	                        <?php
	                            $datos = mysqli_query($con,"SELECT * FROM atributos1 where atri_art_id='$art_id'") ;
	                            while( $fila = mysqli_fetch_array( $datos ) )
	                            {
	                                echo'<option value="'.$fila['atri_id'].'">'.$fila['atri_nombre'].'</option>';
	                            }
	                        ?>
	                    </select>
	                <?php }  else { ?>
	                	<input type="hidden" id="atributo1_<?php echo $art_id; ?>" value="0" >
	                <?php } ?>
	                <?php if($art_atributo2<>null) { ?>
	                    <select id="atributo2_<?php echo $art_id; ?>" class="form-control input-sm">
	                       <option value="0">SIN <?php echo $art_atributo2; ?></option>
	                        <?php
	                            $datos = mysqli_query($con,"SELECT * FROM atributos2 where atri_art_id2='$art_id'") ;
	                            while( $fila = mysqli_fetch_array( $datos ) )
	                            {
	                                echo' <option value="'.$fila['atri_id2'].'">'.$fila['atri_nombre2'].'</option>';
	                            }
	                        ?>
	                    </select>
	                <?php }  else { ?>
	                	<input type="hidden" id="atributo2_<?php echo $art_id; ?>" value="0" >
	                <?php }  ?>
                <?php } else { ?>
            			<input type="hidden" id="atributo1_<?php echo $art_id; ?>" value="0" >
            			<input type="hidden" id="atributo2_<?php echo $art_id; ?>" value="0" >
                <?php } ?>
			</td>
			<td>
                <input type="text" class="form-control input-sm" style="text-align:left" id="cantidad_<?php echo $art_id; ?>"  value="1" target >
			</td>
			<td>
				<select id="iva_<?php echo $art_id; ?>" class="form-control input-sm" >
                    <option value="<?php echo $art_iva; ?>"><?php echo $art_iva; ?> %</option>
                    <?php
                        $datos=mysqli_query($con,"SELECT * FROM iva where empresa='$empresa' and iva_porcentaje<>'$art_iva' order by iva_porcentaje asc");
                        while($fila=mysqli_fetch_array($datos ))
                        {
                        echo' <option value="'.$fila['iva_porcentaje'].'" select>'.$fila['iva_nombre'].'</option>';
                        }
                    ?>
                </select> 
			</td>
			<td>
				<input type="text" class="form-control input-sm" style="text-align:right" id="precio_venta_<?php echo $art_id; ?>" value="<?php echo round($art_precio_costo,2);?>" >
			</td>
			<?php if($facturador=='si') { ?>
			<td><input type="text" class="form-control input-sm" style="text-align:right;" id="flete<?php echo $art_id; ?>" value="0">
			</td>
			<?php } else { ?>
				<input type="hidden" id="flete<?php echo $art_id; ?>" value="0">
			<?php } ?>
			
				
			<input type="hidden" class="form-control" style="text-align:right" id="fecha_vencimiento_<?php echo $art_id; ?>" value="1999-01-01" >

			<td>
				<?php if($art_lote =='si') { ?>
					<span class="pull-right"><a href="#" data-id="<?php echo $id;?>" class="btn btn-lote btn-xs btn-danger" title="Modificar" ><i class="btn-icon-only icon-plus"></i></a></span>
				<?php } else { ?>
					<span class="pull-right"><a href="#" class="btn btn-xs btn-info" onclick="agregar('<?php echo $art_id ?>')"><i class="btn-icon-only icon-plus"></i></a></span>
				<?php } ?>
			</td>
		</tr>
<?php } ?>
		<tr>
			<td colspan=9><span class="pull-right">
			<?php
			 echo paginate($reload, $page, $total_pages, $adjacents);
			?></span>
		 	<input type="hidden" name="pagina" id="pagina" value="<?php echo $page; ?>"></td>
		</tr>
	</table>
</div>
<?php } } ?>
<script>
  	$(".btn-art").click(function(){
  		id = $(this).data("id");
  		$.get("php_articulo/form_modificar_articulo.php","id="+id+"&pagina="+pagina,function(data){
  			$("#form-art").html(data);
  		});
  		$('#modificar_articulo').modal('show');
  	});
</script>

<script>
  	$(".btn-lote").click(function(){
  		id = $(this).data("id");
  		var pagina = $("#pagina").val();
  		var cantidad = $("#cantidad_"+id).val();
  		var precio_venta = $("#precio_venta_"+id).val();
  		var flete = $("#flete"+id).val();
  		$.get("php_buscador/form_lote_compras.php","id="+id+"&pagina="+pagina+"&cantidad="+cantidad+"&precio_venta="+precio_venta+"&flete="+flete,function(data){
  			$("#form-lote").html(data);
  		});
  		$('#lote').modal('show');
  	});
</script>