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

$cot_id=$_SESSION['cot_id'];
$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; //how much records you want to show
		$adjacents  = 4; //gap between pages after number of adjacents
		$offset = ($page - 1) * $per_page;

		$count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM articulos  where empresa='$empresa' and art_estado='activo' ");
		$row= mysqli_fetch_array($count_query);
		$numrows = $row['numrows'];
		$total_pages = ceil($numrows/$per_page);
		$reload = './index.php';
		$sql="SELECT * FROM  articulos where   empresa='$empresa' and art_estado='activo' and  (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%' OR art_referencia2 like '%$q%' )   LIMIT $offset,$per_page";
		$query = mysqli_query($con, $sql);
		if ($numrows>0){
			
			?>
			  <table class="table">
				<tr  class="warning">
					<th width="10%">REFERENCIA</th>
					<th width="">DESCRIPCIÓN</th>
					<th width="10%"><span class="pull-right">CANT.</span></th>
					<th width="5%"><span class="pull-right">EXT.</span></th>
					<th width="8%"><span class="pull-right">P_DISTRI</span></th>
					<th width="15%"><span class="pull-right">P_PUBLICO</span></th>
					<th width="5%"></th>
				</tr>
				<?php
				while ($row=mysqli_fetch_array($query)){
					$art_id=$row['art_id'];
					$art_referencia=$row['art_referencia'];
					$art_nombre=$row['art_nombre'];
					$proveedor_pro_id=$row['proveedor_pro_id'];
					$art_pvp=$row['art_pvp'];
					$art_iva='00';
					$art_precio_costo=$row['art_precio_costo'];
					

					$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'];
				    }
				   
					
					$tarifas_tar_id=2;


					$sql_pre=mysqli_query($con, "select p.pre_precio,pre_id from articulos a, precio p WHERE articulos_art_id='$art_id' and tarifas_tar_id='$tarifas_tar_id' and a.empresa='$empresa' ");
					$rw_pre= mysqli_fetch_array($sql_pre);
					$numrows = $rw_pre['pre_id'];
					if ($numrows>0){
					$pre_precio=$rw_pre['pre_precio'];}
					else{
						$pre_precio=$row['art_pvp'];
					}
					?>
					<tr>
						<td>
						<input type="hidden" class="form-control" style="text-align:right" id="fac<?php echo $art_referencia; ?>"  value="si" >
						<input type="hidden" class="form-control" style="text-align:right" id="art_id<?php echo $art_referencia; ?>"  value="<?php echo $art_id; ?>" ><?php echo $art_referencia; ?></td>
						<td><input type="hidden" class="form-control" style="text-align:right" id="nombre_<?php echo $art_referencia; ?>"  value="<?php echo $art_nombre; ?>" ><?php echo $art_nombre; ?></td>
						<td >
							<input type="hidden" class="form-control" style="text-align:right" id="cantidadexistente_<?php echo $art_referencia; ?>"  value="<?php echo $art_cantidad; ?>" >
							<input type="text" class="form-control input-sm" style="text-align:left" id="cantidad_<?php echo $art_referencia; ?>"  value="1" >
							<input type="hidden" class="form-control" style="text-align:right" id="art_iva<?php echo $art_referencia; ?>"  value="<?php echo $art_iva; ?>" >
						</td>
						<td >
							<p style="text-align:right"><?php echo $art_cantidad; ?></p>
						</td>
						<td>
				          <p style="text-align:right"><?php echo $pre_precio; ?></p>
				          	<input type="hidden" class="form-control input-sm" style="text-align:right" id="descuento<?php echo $art_referencia; ?>"  value="0" >
						<td>
							<input type="hidden" class="form-control" style="text-align:right" id="precio_costo_<?php echo $art_referencia; ?>"  value="<?php echo $art_precio_costo;?>" >
							<input type="text" class="form-control input-sm" style="text-align:right" id="precio_venta_<?php echo $art_referencia; ?>"  value="<?php echo $art_pvp;?>" >
						</td>
						<td >
						<span class="pull-right"><a href="#"  class="btn btn-xs  btn-info" onclick="agregar('<?php echo $art_referencia ?>')"><i class="btn-icon-only icon-plus"></i></a></span>
						</td>
					</tr>
					<?php
				}
		}
	}
?>
	<?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)));

		//Count the total number of row in your table*/
		$paged = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
		$per_page =2; //how much records you want to show
		$adjacentsd  = 4; //gap between pages after number of adjacents
		$offset = ($paged - 1) * $per_page;

		$count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM articulos_detallado  ");
		$row= mysqli_fetch_array($count_query);
		$numrows = $row['numrows'];
		$total_pagesd = ceil($numrows/$per_page);
		$reloadd = './index.php';
		//main query to fetch the data
		$sql="SELECT * FROM  articulos_detallado where   (artd_referencia like '%$q%' OR artd_nombre like '%$q%'  ) and empresa='$empresa'  LIMIT $offset,$per_page";
		$query = mysqli_query($con, $sql);
		//loop through fetched data
		if ($numrows>0){
			
			?>
				
				<?php
					while ($rowd=mysqli_fetch_array($query)){
					$artd_id=$rowd['artd_id'];
					$artd_referencia=$rowd['artd_referencia'];
					$artd_nombre=$rowd['artd_nombre'];
					$artd_pvp=$rowd['artd_pvp'];
					$artd_precio_costo=$rowd['artd_precio_costo'];
					$artd_cantidad=$rowd['artd_cantidad'];
					$artd_unidad=$rowd['artd_unidad'];

					
					?>
					<tr>
						<td><?php
						$sql_pre=mysqli_query($con, "select * FROM factura WHERE empresa='$empresa'  ");
						$fa= mysqli_fetch_array($sql_pre); ?>
						<input type="hidden" class="form-control input-sm" style="text-align:right" id="art_id<?php echo $artd_referencia; ?>"  value="<?php echo $artd_id; ?>" >
						<?php echo $artd_referencia; ?></td>
						<td><input type="hidden" class="form-control input-sm" style="text-align:right" id="nombre<?php echo $artd_referencia; ?>"  value="<?php echo $artd_nombre; ?>" ><?php echo $artd_nombre; ?></td>

						<td >
							<input type="text" class="form-control input-sm" style="text-align:left	" id="cantidad_<?php echo $artd_referencia; ?>"  value="1" >
						</td>
						<td ><p style="text-align:right"><?php echo $artd_cantidad; ?> (<?php echo $artd_unidad; ?>)</p></td>
						<td>
				          <p style="text-align:right"><?php echo $artd_pvp; ?></p>
				          	<input type="hidden" class="form-control input-sm" style="text-align:right" id="descuento<?php echo $art_referencia; ?>"  value="0" >
						<td>
						<input type="hidden" class="form-control" style="text-align:right" id="fac<?php echo $artd_referencia; ?>"  value="si" >
							<input type="hidden" class="form-control input-sm" style="text-align:right" id="precio_costo_<?php echo $artd_referencia; ?>"  value="<?php echo $artd_precio_costo;?>" >
							<input type="text" class="form-control input-sm" style="text-align:right" id="precio_venta_<?php echo $artd_referencia; ?>"  value="<?php echo $artd_pvp;?>" >
						</td>
						<td >
						<span class="pull-right"><a href="#"  title="Agregar" class="btn btn-xs  btn-info" onclick="agregard('<?php echo $artd_referencia ?>')"><i class="btn-icon-only icon-plus"></i></a></span>
						</td>
					</tr>
					<?php
				}
				?>
			 
			<?php
		}
	}
?>

			<tr  class="danger">
						<td><input type="text" class="form-control input-sm" style="text-align:right" id="art_id"  value="" ></td>
						<td><input type="text" class="form-control input-sm" style="text-align:right" id="nombre_"  value="" ></td>
						<td >
							<input type="hidden" class="form-control" style="text-align:right" id="cantidadexistente_"  value="1" >
							<input type="text" class="form-control input-sm" style="text-align:left" id="cantidad_"  value="1" >
							<input type="hidden" class="form-control" style="text-align:right" id="art_iva"  value="00" >
						</td>
						<td >
							<p style="text-align:right"></p>
						</td>
						<td>
				         
							<input type="hidden" class="form-control input-sm" style="text-align:right" id="descuento"  value="0" ></td>
						<td>
							<input type="hidden" class="form-control" style="text-align:right" id="precio_costo_"  value="0" >
							<input type="text" class="form-control input-sm" style="text-align:right" id="precio_venta"  value="0" >
     						<input type="hidden" class="form-control" style="text-align:right" id="fac"  value="no" >
						</td>
						<td >
						<span class="pull-right"><a href="#"  class="btn btn-xs  btn-info" onclick="agregars()"><i class="btn-icon-only icon-plus"></i></a></span>
						</td>
					</tr>
				<tr>
					<td colspan=7><span class="pull-right"><?php
					 echo paginate($reload, $page, $total_pages, $adjacents);
					?></span></td>
				</tr>
			  </table>
			  </div>
			
	