<?php session_start();?>
<?php //include('php_gene1.php'); ?>
<?php //include('php_session.php');?>
<?php //if(file_exists('../cfg/res.php')){include('../cfg/res.php');} ?>	
<?php if(file_exists('../_cfg/config.php')){include('../_cfg/config.php');}?>	
<?php include('php_gene1.php'); ?>	
<?php $pdocu=$_POST['docu'];?>
<?php $pcolu=$_POST['colu'];?>
<?php $pvalo=$_POST['valo'];?>
<?php $ptabla=$pdocu;?>
<?php $penti=$_POST['enti'];?>
<?php $pitem=$_POST['item'];?>


<?php ob_start(); ?>
<?php
$pvalo=trim($pvalo);



echo '<resul>';
// if ($pdocu!="" and $pcolu!="" and $pvalo!="")
// if ($ptabla!="" and strlen($pvalo)>=3)
// echo '*****';
// echo $ptabla;
// echo $pvalo;


if ($ptabla!="" and $pvalo!="")
{
// 	if($pcolu==''){$pcolu=$ptabla.'_descri,'.$ptabla.'_codigo';}

	echo '<res1>';
	$list_sql='select * from docu0 where docu_tabla!="" and docu_codigo like "'.$pdocu.'%"';
	$list_res=mysql_query($list_sql,$link_vcrx);
// 	echo '****'.$list_sql;
  
	if(!mysql_error($link_vcrx))
	{
		if(mysql_num_rows($list_res)!=0)
		{$ptabla=mysql_result($list_res,0,'docu_tabla').'';}


    
		$lyear=strtotime('-12 month');
		$lyear=date('Y-m-'.'01',$lyear);	
// 		$list_sql.=' and '.$ptabla.'_lused>="'.$lyear.'" ';
		$list_sql='select distinct ';
		$list_sql.= 'concat('.str_replace(',','," //",',$pcolu).') as datalist';
    if($ptabla=='item')
    {
//       $list_sql.=',tlla_descri';
//       $list_sql.=',clor_descri';
    }
		$list_sql.=' from '.$ptabla.'0';
      
		$list_sql.=' where '.$ptabla.'_status!="X"';
		$list_sql.=' and '.$ptabla.'_status!="O"';
		$list_sql.=' and '.$ptabla.'_status!="I"';

		if(strlen($pvalo)<=4 and ($ptabla=='enti' or $ptabla=='item'))
		{$list_sql.=' and concat('.$pcolu.') like "'.$pvalo.'%" ';}
		else
		{$list_sql.=' and concat('.$pcolu.') like "%'.$pvalo.'%" ';}
		
		if($cfg_tipo=='cong')
		{if($pdocu!='DPU2'){$list_sql.=' and cong="'.$_SESSION['cong_codigo'].'" ';}}

		if(substr($pdocu,0,2)=='FP' and $pcolu=='atrz')
		{ 
			$list_sql.=' and enti like "%'.$penti.'%" ';
		}

		$list_sql.=' order by '.$pcolu;
// 		$list_sql.=' limit 100';
// echo '****'.$list_sql;
// return $list_sql;
       
    
		if(substr($pdocu,0,2)=='FP' and $pcolu=='atrz')
		{$list_sql.=' desc ';}
		
		
		$list_sql1='';
		$list_sql1.=' and docu like "'.$pdocu.'%" ';
		$list_sql1.=' and fecha >= "'.$lyear.'" ';
		
// 		echo $list_sql1;
		$dlok=0;
		$list_res=mysql_query($list_sql,$link_vcrx);
		if(!mysql_error($link_vcrx)){$dlok=1;}
		else
		{
			$list_sql=str_replace($ptabla.'0',$ptabla.'00',$list_sql);
			$list_res=mysql_query($list_sql,$link_vcrx);

      if(!mysql_error($link_vcrx)){$dlok=1;}
			else
			{
				$list_sql=str_replace($ptabla.'00',$ptabla.'1',$list_sql);
				$list_sql=str_replace('_codigo','',$list_sql);
				$list_sql=str_replace($ptabla.'_status','status',$list_sql);
				$list_sql=str_replace('and status!="I"',$list_sql1,$list_sql);
				$list_res=mysql_query($list_sql,$link_vcrx);
				if(!mysql_error($link_vcrx)){$dlok=1;}
				else
				{
// 					$list_sql=str_replace($ptabla.'1',$ptabla.'11',$list_sql);
					$list_sql=str_replace($ptabla.'1',$ptabla.'12',$list_sql);
					$list_res=mysql_query($list_sql,$link_vcrx);
					if(!mysql_error($link_vcrx)){$dlok=1;}
					else
					{
						$list_sql=str_replace($ptabla.'11',$ptabla.'12',$list_sql);
						$list_res=mysql_query($list_sql,$link_vcrx);
						if(!mysql_error($link_vcrx)){$dlok=1;}
					}
				}
			}
		}
// 		echo $list_sql.'*****'.$dlok;
// 			echo '////'.explain_sql($list_sql);
		if($dlok==1)
		{
			if(mysql_num_rows($list_res)>50)
			{
// 				$list_sql=str_replace('order by','order by fecha desc,',$list_sql);
// 				$list_sql.=' limit 200 ';
// 				$list_res=mysql_query($list_sql,$link_vcrx);
				
// 				$list_sql=str_replace(') like "%',') like "',$list_sql);
				$list_sql.=' limit 50 ';
				$list_res=mysql_query($list_sql,$link_vcrx);
			}
			while($list_aso=mysql_fetch_assoc($list_res))
			{
        $optionvalue ='<option value="'.$list_aso['datalist'];
        if($ptabla=='item')
        {
//           $optionvalue.=' /'.$list_aso['tlla_descri'];
//           $optionvalue.=' /'.$list_aso['clor_descri'];
        }  
        $optionvalue.='">';
				echo $optionvalue;
// 				echo '<option value="'.$list_aso['datalist'].'">';
// 				echo $list_aso['datalist'];
// 				echo '</option>';
			}
		}
	}
	echo '</res1>';
}
echo '</resul>';
?>

<?php $resdatalist = ob_get_clean(); ?>
<?php $resdatalist = alltrim($resdatalist) ?>
<?php echo $resdatalist;	?>

