<?php
require('../cfg/res.php'); 
	// Actualizado 2016-02-17
session_start();

$xvalor = str_replace('  ',' ',$_POST['codigo']);
$xvalor = str_replace('  ',' ',$xvalor);
$xvalor = str_replace('  ',' ',$xvalor);

$tabla=$_POST['tabla'];
$tabla0=$_POST['tabla'].'0';
$codig=$_POST['tabla'].'_codigo';
$descr=$_POST['tabla'].'_descri';
$busca=$_POST['tabla'].'_buscar';

if($_POST['tabla']=='drtr')
{
	$codig='enti_codigo';
	$descr='enti_descri';
}

$sort;
$tipo;

$list_sql='select *from '.$tabla0 .' where '.$codig.' not like "-%"';

$xorder = ' order by '.$codig;
if(mb_stristr('enti.drtr',$_POST['tabla']))
{$xorder = ' order by '.$descr;}

$xwhered= '(1=1)';	
$xacceso=0;

if($tabla=='docu')
{
if (!mb_stristr('sist.supe',$_SESSION['user_vcrx_tipo']))
	{
		$xwhered='(docu_codigo in(select docu from usrdoc1 where user="'.	$_SESSION['user_vcrx_id'].'" and (nuev=1 or modi=1 or proc=1 or anul=1 or cons=1 or info=1)))';
	}
}

	if(mb_stristr('enti.drtr',$_POST['tabla']))
	{
// 		echo '123123';
		$list_sql2='select *from '.$tabla0;
		$list_sql2.=' where adddate('.$tabla.'_lused,365)>=now()';
		$list_sql2.=' and length('.$tabla.'_codigo)>=6';
		$list_sql2.=' and '.$tabla.'_codigo not like "-%"';
		$list_sql2.=' and '.$tabla.'_codigo not like "*%"';
		$list_sql2.=' order by '.$tabla.'_descri,'.$tabla.'_codigo';
		$list_sql2.=' limit 1000';
		$list_sql=$list_sql2;
	}
	if(mb_stristr('item',$_POST['tabla']))
	{
		$list_sql2='select *from '.$tabla0;
		$list_sql2.=' where adddate('.$tabla.'_lused,365)>=now()';
		$list_sql2.=' and length('.$tabla.'_codigo)>=6';
		$list_sql2.=' and '.$tabla.'_codigo not like "-%"';
		$list_sql2.=' and '.$tabla.'_codigo not like "*%"';
		$list_sql2.=' order by '.$tabla.'_descri,'.$tabla.'_codigo';
		$list_sql2.=' limit 1000';
		$list_sql=$list_sql2;
// 		echo $list_sql;
	}

	
if($_POST['codigo']!="")
{
	$list_sql='select *from '.$tabla0.' where '.$codig.' like "'.$_POST['codigo'].'%" and' .$xwhered;
	$list_res=mysql_query($list_sql,$link_vcrx);
	if (mysql_num_rows($list_res)==0)
	{
		$list_sql='select *from '.$tabla0.' where '.$descr.' like "'.$_POST['codigo'].'%"';
		$list_res=mysql_query($list_sql,$link_vcrx);
		if (mysql_num_rows($list_res)!=0)
		{	$xorder = ' order by '.$descr.','.$codig;}
		else
		{
			$xwhere = $_POST['tabla'].'_buscar like "%'.str_replace(' ', '%" and '.$_POST['tabla'].'_buscar like "%', $xvalor).'%"';
			$list_sql='select *from '.$tabla0.'b where '.$xwhere.' and '.$_POST['tabla'].'_status!="X"';
			$list_res=mysql_query($list_sql,$link_vcrx);
			if(!mysql_error($link_vcrx))
			{if (mysql_num_rows($list_res)==0){}
			else
			{
// 	if(mb_stristr('FC.NC.NV.OP.',substr($_POST['docu'],0,2)))
				$list_sql='select *from '.$tabla0;
				if(mb_stristr('enti.drtr.item',$_POST['tabla']))
				{	$list_sql=$list_sql2;}
			}

		}
	}
	}
}

if($_POST['tabla']=='enti' || $_POST['tabla']=='drtr'){}
	
// $list_sql.=$xorder;
$list_sql=str_replace('order by',' '.$xorder.' , ',$list_sql);
$list_sql=str_replace('where','where '.$xwhered.' and ',$list_sql);
// echo $list_sql;
$ind_list=0;
?>
<div style="display:none;">
	<?php $pcaption=''; $pidtxt='idseleccion'; $plong=10; include('vrgettxt.php');?>
</div>
<div style="max-height:100px; margin-top:5px;">
	<table width="100%" >
		<tr>
			<th align="left" width="20%">
				<?php $pcaption='Aceptar'; $ponclick="list_agrega('','".$_POST['list_cod']."','".$_POST['list_desc']."','');";  include('vrbutton.php');?>
			</th>
			<th width ="50%"></th>
			<th align="right" width="20%">
				<?php $pcaption='Salir'; $ponclick="document.getElementById('lista_temporal_contenedor').style.display='none';document.getElementById('lista_temporal').style.display='none';"; include('vrbutton.php');?>
<!-- 			<div> -->
<!-- 				<input  type="image" style="width:20px;" src="http://vcrxol.com/_img/ico_eliminar.png" onclick="document.getElementById('lista_temporal_contenedor').style.display='none';document.getElementById('lista_temporal').style.display='none';" /> -->
<!-- 			</div> -->
			</th>
		</tr>
	</table>
</div>

<div style=" max-height:400px; overflow-y:scroll; margin-top:5px;">	
<table border="0" id="tablita" align="center" width="90%" cellpadding="2" cellspacing="0" style="border-collapse:collapse; font-size:12px;">
<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
<th></th>
<th>CODIGO</th>
<th>DESCRIPCION</th>

<?php	if(mb_stristr('enti.drtr',$_POST['tabla']))	{	?>
	<th>STAT.RUC</th>
	<th>DIRECCION</th>
	<th>TELEFONO</th>
<?php }?> 

<?php if($_POST['tabla']=='item'){?>
	<?php	if(!empty($_POST['docu'])){ ?>
		<?php if(mb_stristr('FC.NC.NV.OP',substr($_POST['docu'],0,2))){?>
		<th>P.UNIT.</th>
		<th>%ICE</th>
		<th>%DCTO</th>
		<th>%IVA</th>
		<th>IRBP</th>
		<th>PVP</th>
		<?php }else{ ?>
		<!--
		<th>P.UNIT</th>
		<th>P.V.P.</th
		-->	
		<?php } ?>
	<?php }	?>
  <th>LINEA</th>
<?php } ?>
	
</tr>

<?php 
// echo $list_sql;
$list_res=mysql_query($list_sql,$link_vcrx);
$filax=0;

while($r_list=mysql_fetch_assoc($list_res))
{ 
$ind_list++;?>

<tr id="list_fila<?php echo $ind_list;?>" 
		class="list_filax" style=" <?php if($filax>=3){echo 'border-top:dotted 1px #000;';$filax=1;}else{$filax++;} ?>" 
		onkeypress="if(event.keyCode==13){list_agrega('<?php echo $ind_list;?>','<?php echo $_POST['list_cod'];?>','<?php echo $_POST['list_desc'];?>','');}" onkeyup="if(event.keyCode==27){ document.getElementById('lista_temporal_contenedor').style.display='none';document.getElementById('lista_temporal').style.display='none';};"
		ondblclick="list_agrega('<?php echo $ind_list;?>');">

<td <?php if($ind_list==1){echo ' tabIndex="1"';}?> >
<!-- 	<input id="radio_<?php echo $ind_list;?>" <?php if($ind_list==1){echo 'checked';}?> type="radio"  name="lista" class="opt"
				 onfocus="list_filabg('<?php echo $ind_list;?>');" 
				 ondblclick="list_agrega('<?php echo $ind_list;?>','<?php echo $_POST['list_cod'];?>','<?php echo $_POST['list_desc'];?>','');" /> -->
	<?php $pcaption=''; $pidtxt='radio_'.$ind_list; $pname='lista'; if($ind_list==1){$pdefa='checked';};
				$ponfocus="list_filabg('".$ind_list."');"; 
// 				$ponclick="list_filabg('".$ind_list."');"; 
				$pdblclick="list_agrega('".$ind_list."','".$_POST['list_cod']."','".$_POST['list_desc']."','');"; 
				include('vrradio.php');?>
</td>
<!-- <td><input type="button" name="lista" class="opt" onfocus="list_filabg('<?php echo $ind_list;?>');" onclick="list_agrega('<?php echo $ind_list;?>','<?php echo $_POST['list_cod'];?>','<?php echo $_POST['list_desc'];?>','');" /></td> -->

<td><?php echo $r_list[$codig];?></td>
<td><?php echo $r_list[$descr];?></td>

<?php if($_POST['tabla']=='enti' || $_POST['tabla']=='drtr'){?>
	<td align="center"><?php echo $r_list['enti_struc'];?></td>
	<td><?php echo $r_list['enti_direccion'];?></td>
	<td><?php echo $r_list['enti_telefono']; ?></td>
<?php }?>

<?php 
// 	echo '****'.$_POST['tabla'].'//////////'.$_POST['docu'];
	?>	
<?php if($_POST['tabla']=='item'){?>
<!--	
 -->   
	<?php 
	if(isset($_POST['docu']) && !empty($_POST['docu']))
	{
		?>
	<?php 
	if(mb_stristr('FC.NC.NV.OP.',substr($_POST['docu'],0,2)))
	{?>
	<td align="right" style="border-left:solid 1px"><?php echo number_format($r_list['item_puni'],4); ?></td>
	<td align="center" style="border-left:solid 1px"><?php if($r_list['item_ice']>0){echo $r_list['item_ice'];} ?></td>
	<td align="center" style="border-left:dotted 1px"><?php if($r_list['item_dcto']>0){echo $r_list['item_dcto'];} ?></td>
  <td align="center" style="border-left:solid 1px"><?php if($r_list['item_iva']>0){echo $r_list['item_iva'];} ?></td>
  <td align="right" style="border-left:dotted 1px"><?php if($r_list['item_irbp']>0){echo $r_list['item_irbp'];} ?></td>
	<td align="right" style="border-left:solid 1px;font-weight:bold;"><?php echo number_format($r_list['item_pvp1'],2); ?></td>

    <?php } ?>
	<?php } ?>

    <td align="left" style="border-left:solid 1px"><?php echo $r_list['lsub_descri']; ?></td>

	
<?php }?>

</tr>

<?php } ?>
</table>
</div>
