<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$ord_id=$_GET['ord_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)));
    $tipo=mysqli_real_escape_string($con,(strip_tags($_REQUEST['tipo'], ENT_QUOTES)));
    $buscar = mb_strtoupper($q);
	include '../include/pagination.php';
	$page=(isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
	$per_page=6; 
	$adjacents=4; 
	$offset=($page-1)*$per_page;

?>

<div class="table-responsive">
	<table class="table">
		<tr class="warning">
			<th width="10%">CODIGO</th>
			<th width="">NOMBRE</th>
			<th width="10%"><span class="pull-left">CANTIDAD</span></th>
			<th width="8%"><span class="pull-center">STOCK</span></th>
			<th width="13%"><span class="pull-right">DESCUENTO</span></th>
			<th width="13%"><span class="pull-right">PRECIO</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_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='../I_articulos.php';
    $sql=mysqli_query($con,"SELECT *, a.art_id as art_id ,b.art_id as art_idd FROM articulos a, articulos_has_bodegas b
    where a.art_id=b.articulos_art_id and b.sucursal='$sucursal' and a.empresa='$empresa' and a.art_estado='activo' and a.art_tipo='articulo' and (a.art_referencia like '%$q%' OR a.art_nombre like '%$q%' OR a.art_barras like '%$q%' OR a.art_referencia2 like '%$q%') order by b.art_cantidad desc LIMIT $offset,$per_page");

	if ($numrows>0)
	{
		$sql_tar=mysqli_query($con, "SELECT ord_clie_id from orden_servicios where ord_id='$ord_id' and empresa='$empresa'");
		$rw_tar=mysqli_fetch_array($sql_tar);
		$cliente_id=$rw_tar['ord_clie_id'];

		$sql_tar=mysqli_query($con, "SELECT tarifas_tar_id from cliente  where clie_id='$cliente_id' and empresa='$empresa'");
		$rw_tar=mysqli_fetch_array($sql_tar);
		$tarifas_tar_id=$rw_tar['tarifas_tar_id'];
		while ($row=mysqli_fetch_array($sql))
		{
			$art_id=$row['art_id'];
			$art_art_id=$row['art_idd'];
			$art_tipo=$row['art_tipo'];
			$art_referencia=$row['art_referencia'];
			$art_nombre=mb_strtoupper($row['art_nombre']);
			$art_pvp=$row['art_pvp'];
			$art_iva=$row['art_iva'];
			$art_precio_costo=$row['art_precio_costo'];
			$art_cantidad=$row['art_cantidad'];

			$sql_pre=mysqli_query($con, "SELECT * from  precio WHERE articulos_art_id='$art_id' and tarifas_tar_id='$tarifas_tar_id'");
			if ($rw_pre= mysqli_fetch_array($sql_pre))
			{
				$pre_precio=$rw_pre['pre_precio'];}
			else 
			{
				$pre_precio=$row['art_pvp'];
			}
?>
		<tr>
				<td>
					<input type="hidden" id="art_tipo<?php echo $art_art_id; ?>" value="<?php echo $art_tipo; ?>">
					<input type="hidden" id="art_id<?php echo $art_art_id; ?>" value="<?php echo $art_id; ?>">
					<input type="hidden" id="art_referencia<?php echo $art_art_id;?>" value="<?php echo $art_referencia; ?>" >
					<?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $art_referencia ));?>
				</td>
				<td>
					<input type="hidden" id="nombre_<?php echo $art_art_id; ?>" value="<?php echo $art_nombre; ?>">
					<?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $art_nombre ));?>
					<span style="color: red;"><?php if($row["art_atributo_estado"]=='si') 
					{  
						$atributo1=$row['atributo1'];

                        $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 $atri1['atri_nombre']; }

                    	$atributo2=$row['atributo2'];
                        $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 ' / '; echo $atri2['atri_nombre2']; }
                    }
                	?></span>
                </td>
				<td>
					<p style="text-align:right"><?php echo $art_cantidad; ?></p>
				</td>
				<td>
					<input type="hidden" class="form-control" style="text-align:right" id="cantidadexistente_<?php echo $art_art_id; ?>" value="<?php echo $art_cantidad; ?>">
					<input type="text" class="form-control input-sm" style="text-align:left" id="cantidad_<?php echo $art_art_id; ?>"  value="1" >
					<input type="hidden" class="form-control" style="text-align:right" id="art_iva<?php echo $art_art_id; ?>" value="<?php echo $art_iva; ?>" >
				</td>
				<td>
					<input type="text" class="form-control input-sm" style="text-align:right" id="descuento<?php echo $art_art_id;?>"  value="0">
				</td>
				<td>
					<input type="hidden" class="form-control" style="text-align:right" id="fac<?php echo $art_art_id; ?>" value="si" >
					<input type="hidden" class="form-control" style="text-align:right" id="precio_costo_<?php echo $art_art_id; ?>"  value="<?php echo $art_precio_costo;?>">
					<input type="text" class="form-control input-sm" style="text-align:right" id="precio_venta_<?php echo $art_art_id; ?>" value="<?php echo $pre_precio;?>" >
				</td>
				<td>
					<span class="pull-right"><a href="#" class="btn btn-xs btn-info" onclick="agregar('<?php echo $art_art_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  } 



if($tipo=='detallados')
{
	$count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM articulos_detallado  ");
	$row= mysqli_fetch_array($count_query);
	$numrows=$row['numrows'];
	$total_pages=ceil($numrows/$per_page);
	$reload='./index.php';
	$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);
	if ($numrows>0)
	{
		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>
					<p style="text-align:right"><?php echo $artd_cantidad; ?> (<?php echo $artd_unidad; ?>)</p>
				</td>
				<td>
					<input type="text" class="form-control input-sm" style="text-align:left" id="cantidad_<?php echo $artd_referencia; ?>" value="1" >
				</td>
				<td>
					<input type="text" class="form-control input-sm" style="text-align:right" id="descuento<?php echo $artd_referencia; ?>"  value="0" >
				</td>
				<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="agregardetallado('<?php echo $artd_referencia ?>')"><i class="btn-icon-only icon-plus"></i></a></span>
				</td>
		</tr>
<?php }  } ?>
		<tr>
			<td colspan="7" style="padding-top:0px; padding-bottom:0px;"><span class="pull-right">
				<?php echo paginate($reload, $page, $total_pages, $adjacents);?></span>
			</td>
		</tr>
<?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_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>
					<?php echo $com_referencia; ?></td>
				<td>
					<input type="hidden" class="form-control input-sm" style="text-align:right" id="nombre<?php echo $com_id; ?>" value="<?php echo $com_nombre; ?>" ><?php echo $com_nombre; ?></td>
				<td></td>
				<td>
					<input type="text" class="form-control input-sm" style="text-align:left" id="cantidad_<?php echo $com_id; ?>" value="1" >
				</td>
				<td>
					<input type="hidden" class="form-control input-sm" style="text-align:right" id="descuento<?php echo $com_id; ?>" value="0"></td>
				<td>
					<input type="text" class="form-control input-sm" style="text-align:right" id="precio_venta_<?php echo $com_id; ?>" value="<?php echo $sumador_total;?>" readonly title="ff">
				</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" style="padding-top:0px; padding-bottom:0px;"><span class="pull-right">
				<?php echo paginate($reload, $page, $total_pages, $adjacents);?></span>
			</td>
		</tr>
<?php  } 




if($tipo=='servicio')
{
	$count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM articulos 
    where empresa='$empresa' and art_tipo='servicio' and (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%') ");
    $row= mysqli_fetch_array($count_query);
    $numrows=$row['numrows'];
    $total_pages=ceil($numrows/$per_page);
    $reload='../I_articulos.php';
    $sql=mysqli_query($con,"SELECT * FROM articulos 
    where empresa='$empresa' and art_tipo='servicio' and (art_referencia like '%$q%' OR art_nombre like '%$q%' OR art_barras like '%$q%') order by art_nombre desc LIMIT $offset,$per_page");

	if ($numrows>0)
	{
		
		while ($row=mysqli_fetch_array($sql))
		{
			$art_id=$row['art_id'];
			$art_tipo=$row['art_tipo'];
			$art_referencia=$row['art_referencia'];
			$art_nombre=mb_strtoupper($row['art_nombre']);
			$pre_precio=$row['art_pvp'];
			$art_iva=$row['art_iva'];
			$art_precio_costo=$row['art_precio_costo'];
			$art_cantidad=0;

			
?>
		<tr>
				<td>
					<input type="hidden" id="art_tipo<?php echo $art_id; ?>" value="<?php echo $art_tipo; ?>">
					<input type="hidden" id="art_id<?php echo $art_id; ?>" value="<?php echo $art_id; ?>">
					<input type="hidden" id="art_referencia<?php echo $art_id;?>" value="<?php echo $art_referencia; ?>" >
					<?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $art_referencia ));?>
				</td>
				<td>
					<input type="hidden" id="nombre_<?php echo $art_id; ?>" value="<?php echo $art_nombre; ?>">
					<?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $art_nombre ));?>.
				</td>
				<td>
					<p style="text-align:right"><?php echo $art_cantidad; ?></p>
				</td>
				<td>
					<input type="hidden" class="form-control" style="text-align:right" id="cantidadexistente_<?php echo $art_id; ?>" value="<?php echo $art_cantidad; ?>">
					<input type="text" class="form-control input-sm" style="text-align:left" id="cantidad_<?php echo $art_id; ?>"  value="1" >
					<input type="hidden" class="form-control" style="text-align:right" id="art_iva<?php echo $art_id; ?>" value="<?php echo $art_iva; ?>" >
				</td>
				<td>
					<input type="text" class="form-control input-sm" style="text-align:right" id="descuento<?php echo $art_id;?>"  value="0">
				</td>
				<td>
					<input type="hidden" class="form-control" style="text-align:right" id="fac<?php echo $art_id; ?>" value="si" >
					<input type="hidden" class="form-control" style="text-align:right" id="precio_costo_<?php echo $art_id; ?>"  value="<?php echo $art_precio_costo;?>">
					<input type="text" class="form-control input-sm" style="text-align:right" id="precio_venta_<?php echo $art_id; ?>" value="<?php echo $pre_precio;?>" >
				</td>
				<td>
					<span class="pull-right"><a href="#" class="btn btn-xs btn-info" onclick="agregarservicio('<?php echo $art_id; ?>')"><i class="btn-icon-only icon-plus"></i></a></span>
				</td>
			</tr>
<?php }  } ?>
		<tr>
			<td colspan="7" style="padding-top:0px; padding-bottom:0px;"><span class="pull-right">
				<?php echo paginate($reload, $page, $total_pages, $adjacents);?></span>
			</td>
		</tr>
<?php  } 

 } ?>
		
</table>
</div>