<?php
class numdocu
{
	var $ruta;
	var $error;
	var $sql;
	var $numero;
	function numdocu($r)
	{
		$this->ruta=$r;
	}
	
  
  function get_numero($docu,$ptoe)
  {
    include ('php_gene1.php');
		$this->numero=nume_docu($docu,$ptoe);
  }
  
	function Xget_numero($docu,$ptoe)
	{
    include ('php_gene1.php');
    $tabd=r_fiel('docu',$docu,'tabla').'1';
    
		require($this->ruta);
		if(empty($ptoe))
		{
			$ptoe='001001';
		}
		else
		{
			$ptoe_sql='select "'.$ptoe.'" as ptoe';
			
			if (mb_stristr(".FC.NC.PC.PF.FR",substr($docu,0,2)))
			{$ptoe_sql='select *,ptov_ptoe as ptoe from ptov0 where ptov_descri like "%'.$ptoe.'%" or ptov_codigo like "%'.$ptoe.'%"';}
		
			if (mb_stristr("IB.EB.OP.QP",substr($docu,0,2)))
			{$ptoe_sql='select *,bode_ptoe as ptoe from bode0 where bode_descri like "%'.$ptoe.'%" or bode_codigo like "%'.$ptoe.'%"';}

			if (mb_stristr("IE.EE",substr($docu,0,2)))
			{$ptoe_sql='select *,caja_ptoe as ptoe from caja0 where caja_descri like "%'.$ptoe.'%" or caja_codigo like "%'.$ptoe.'%"';}

			if (mb_stristr("CH",substr($docu,0,2)))
			{$ptoe_sql='select *,ctab_ptoe as ptoe from ctab0 where ctab_descri like "%'.$ptoe.'%" or ctab_codigo like "%'.$ptoe.'%"';}

			if (mb_stristr("RP",substr($docu,0,2)))
 			{$ptoe_sql='select docu,ptoe as ptoe from retp1 where docu like "RP%" order by fecha desc limit 1';}

 			if (mb_stristr("RO.NM",substr($docu,0,2)))
 			{$ptoe_sql='select docu,ptoe as ptoe from rolp1 where docu like "RO%" order by fecha desc limit 1';}


// 			echo $ptoe_sql;
			$ptoe_res=mysql_query($ptoe_sql,$link_vcrx);
			if(mysql_num_rows($ptoe_res)!=0)
			{
				if (mb_stristr("FC.NC.PF.PC",substr($docu,0,2))){$ptoe=mysql_result($ptoe_res,0,'ptoe');}
				if (mb_stristr("IB.EB",substr($docu,0,2))){$ptoe=mysql_result($ptoe_res,0,'ptoe');}
        if (mb_stristr("OP.QP",substr($docu,0,2))){$ptoe=mysql_result($ptoe_res,0,'ptoe');}
				if (mb_stristr("IE.EE",substr($docu,0,2))){$ptoe=mysql_result($ptoe_res,0,'ptoe');}
				if (mb_stristr("RP.",substr($docu,0,2))){$ptoe=mysql_result($ptoe_res,0,'ptoe');}
				if (mb_stristr("RO.NM",substr($docu,0,2))){$ptoe=mysql_result($ptoe_res,0,'ptoe');}
				if (mb_stristr("CH.",substr($docu,0,2))){$ptoe=mysql_result($ptoe_res,0,'ptoe');}
				if (mb_stristr("AP.",substr($docu,0,2))){$ptoe=mysql_result($ptoe_res,0,'ptoe');}

			}
		}

  $fecha1=date('Y-m-01', strtotime('-12 month'));

	$sql_ndoc ='select docu, ptoe, max(numero*1) as numero, max(fecha) as fecha, status';
	$sql_ndoc.=' from '.$tabd;
	$sql_ndoc.=' where docu like "'.$docu.'%" ';
	$sql_ndoc.=' and ptoe="'.$ptoe.'"';
	$sql_ndoc.=' and fecha is not null';
// 	$sql_ndoc.=' and fecha>="'.$fecha1.'"';
	$sql_ndoc.=' and status<>"X" ';
// 	$sql_ndoc.=' and fecha<=curdate()  ';
	$sql_ndoc.=' group by docu,ptoe ';
	$sql_ndoc.=' limit 1 ';
	$numd_res=mysql_query($sql_ndoc,$link_vcrx);
    
    
    
		$this->error=mysql_error($link_vcrx);
		$this->numero=0;
		if(mysql_num_rows($numd_res)!=0)
		{$this->numero=mysql_result($numd_res,0,'numero');}
		$this->sql=$numd_sql;
    unset($numd_res);
		unset($numd_sql);
		unset($ptoe);
		unset($ptoe_res);
		unset($ptoe_sql);
    

    
    
    
    
    
	}
}
?>
