 <?php
require_once ("../conexion.php");
session_start();
include '../include/sesiones.php';
	$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
	if($action == 'ajax'){
		// escaping, additionally removing everything that could be (html/javascript-) code
        $q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
        $tipo=mysqli_real_escape_string($con,(strip_tags($_REQUEST['tipo'], ENT_QUOTES)));
		include '../include/pagination.php';
		//Count the total number of row in your table*/
		$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
		$per_page = 6; //how much records you want to show
		$adjacents  = 4; //gap between pages after number of adjacents
		$offset = ($page - 1) * $per_page;

		?>

		<div class="table-responsive">
			  <table class="table">
				<tr  class="warning">
					<th width="18%">CODIGO</th>
					<th >NOMBRE</th>
					<?php if($texto_adicional=='si') { ?>
					<th >TEXTO ADIC.</th>
					<?php } else { ?>
					<th > </th>
					<?php } ?>
					<th width="3%">STOCK</th>
					<th width="12%"><span class="pull-left">CANT ETI</span></th>
					<th width="5%"></th>
				</tr>

		<?php
if($tipo=='articulos')
	{

		$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';
		//main query to fetch the data
		$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");
		//loop through fetched data
		if ($numrows>0){
			

				while ($row=mysqli_fetch_array($sql)){
					$art_id=$row['art_id'];
					$art_referencia=$row['art_referencia'];
					$art_referencia2=$row['art_referencia2'];
					$art_barras=$row['art_barras'];
					$art_nombre=utf8_encode($row['art_nombre']);			

					$sql_tar=mysqli_query($con, "SELECT 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" class="form-control" style="text-align:right" id="art_id<?php echo $art_id; ?>"  value="<?php echo $art_id; ?>" >
							<input type="hidden" class="form-control" style="text-align:right" id="barras<?php echo $art_id; ?>"  value="<?php echo $art_barras; ?>" ><?php echo $art_barras; ?>

						</td>

						<td>
							<input type="hidden" class="form-control" style="text-align:right" id="nombre_<?php echo $art_id; ?>"  value="<?php echo $art_nombre; ?>" ><a href="#"  data-id="<?php echo $art_id;?>" class="btn-art"  title="Modificar" ><?php echo $art_nombre; ?></a>
						</td>
						<td>
						<?php

						 	$ret=mysqli_query($con,"SELECT * FROM articulos where art_id='$art_id' and empresa='$empresa'") or die(mysqli_error($con)); 
							$rets=mysqli_fetch_array($ret);
							$art_precio_costo=$rets['art_precio_costo'];
							$art_pvp=$rets['art_pvp'];

							$letras = array();
							$numero = array();

							$sql2=mysqli_query($con, "SELECT * from etiquetas_texto_codificado ");
							while ($row2=mysqli_fetch_array($sql2))
							{
								$letras[]=$row2['tex_cod_letra'];
								$numero[]=$row2['tex_cod_numero'];
							}

							json_encode($letras);
							json_encode($numero);
							$venta_letras = str_replace($numero, $letras, $art_pvp);
							$costo_letras = str_replace($numero, $letras, $art_precio_costo);
						if($venta_letras<>null){ $espacio=' - ';} else { $espacio='';}
						?>
						<?php if($texto_adicional=='si') { ?>
							<input type="text" maxlength="15" title="15 LETRAS MAXIMO" class="form-control" style="text-align:left" id="texto_adicional<?php echo $art_id; ?>"  value="<?php echo $costo_letras; echo $espacio; echo $venta_letras ?>" >
						<?php } else { ?>
							<input type="hidden" id="texto_adicional<?php echo $art_id; ?>" value="">
						<?php  }  ?>
						</td>
						<td>
							<label><?php echo $art_cantidad ?></label>
						</td>
						<td>
							<input type="text" class="form-control" style="text-align:left" id="cantidad_<?php echo $art_id; ?>" value="" >
						</td>
						<td>
							<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>
						</td>
					</tr>
					<?php
				}
				?>
				<tr>
					<td colspan=6><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
		}
	}

if($tipo=='combos')
{
	$count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM combos");
	$row=mysqli_fetch_array($count_query);
	$numrows=$row['numrows'];
	$total_pages=ceil($numrows/$per_page);
	$reload='./index.php';
	$sql="SELECT * FROM combos where (com_referencia like '%$q%' OR com_nombre like '%$q%') and empresa='$empresa' LIMIT $offset,$per_page";
	$query = mysqli_query($con, $sql);
	if ($numrows>0)
	{
		while ($rowd=mysqli_fetch_array($query)){
		$com_id=$rowd['com_id'];
		$com_referencia=$rowd['com_referencia'];
		$com_barras=$rowd['com_barras'];
		$com_nombre=$rowd['com_nombre'];
		$sumador_total=0;
		$sumador_costo=0;
		$sql1=mysqli_query($con,"SELECT * FROM combos_articulos where combos_com_id='$com_id'");
		while($row1=mysqli_fetch_array($sql1))
		{
			$coma_art_id=$row1['coma_art_id'];
			$coma_art_cantidad=$row1['coma_art_cantidad'];
			$coma_art_precio=$row1['coma_art_precio'];
			$art_pvp=$coma_art_precio*$coma_art_cantidad;

			$sql2=mysqli_query($con,"SELECT * from articulos where  art_id='$coma_art_id' ");
			$art2=mysqli_fetch_array($sql2);
			$art_precio_costo=$art2["art_precio_costo"]*$coma_art_cantidad;
							
			$sumador_costo+=$art_precio_costo;//Sumador
			$sumador_total+=$art_pvp;//Sumador
		}
?>
		<tr>
				<td>
					<input type="hidden" class="form-control" style="text-align:right" id="com_id<?php echo $com_id; ?>"  value="<?php echo $com_id; ?>" >
					<input type="hidden" class="form-control" style="text-align:right" id="barras_combo<?php echo $com_id; ?>"  value="<?php echo $com_barras; ?>" ><?php echo $com_barras; ?>
				</td>
				<td>
					<input type="hidden" class="form-control input-sm" style="text-align:right" id="nombre_combo<?php echo $com_id; ?>"  value="<?php echo $com_nombre; ?>" ><?php echo $com_nombre; ?>
				</td>
				<td>
						<?php

							$letras = array();
							$numero = array();

							$sql2=mysqli_query($con, "SELECT * from etiquetas_texto_codificado ");
							while ($row2=mysqli_fetch_array($sql2))
							{
								$letras[]=$row2['tex_cod_letra'];
								$numero[]=$row2['tex_cod_numero'];
							}

							json_encode($letras);
							json_encode($numero);
							$venta_letras = str_replace($numero, $letras, $sumador_total);
							$costo_letras = str_replace($numero, $letras, $sumador_costo);
						if($venta_letras<>null){ $espacio=' - ';} else { $espacio='';}
						?>
						<?php if($texto_adicional=='si') { ?>
							<input type="text" maxlength="15" title="15 LETRAS MAXIMO" class="form-control" style="text-align:left" id="texto_adicional_combo<?php echo $com_id; ?>"  value="<?php echo $costo_letras; echo $espacio; echo $venta_letras ?>" >
						<?php } else { ?>
							<input type="hidden" id="texto_adicional_combo<?php echo $com_id; ?>" value="">
						<?php  }  ?>
						</td>
				<td>
					<label><?php echo $coma_art_cantidad ?></label>
				</td>
				<td>
					<input type="text" class="form-control" style="text-align:left" id="cantidad_combo<?php echo $com_id; ?>" value="" >
				</td>
				<td >
					<span class="pull-right"><a href="#" title="Agregar" class="btn btn-xs btn-info" onclick="agregarcombo('<?php echo $com_id ?>')"><i class="btn-icon-only icon-plus"></i></a></span>
				</td>
			</tr>
<?php }  } ?>
		<tr>
			<td colspan="7" ><span class="pull-right">
				<?php echo paginate($reload, $page, $total_pages, $adjacents);?></span>
			</td>
		</tr>
<?php  } 
 




	}
?>

<script type="text/javascript">

  	$(".btn-art").click(function(){
  		id = $(this).data("id");
  	var pagina = $("#pagina").val();
  		$.get("php_articulo/form_modificar_articulo.php","id="+id+"&pagina="+pagina,function(data){
  			$("#form-art").html(data);
  		});
  		$('#modificar_articulo').modal('show');
  	});
</script>