<?php
class vrfacturar
{
var $idreturn;

	function vrfacturar($docu,$idnum)
	{
		require('../cfg/res.php');

		$xmdfrom='X';		
		if(substr($docu,0,2)=='OP')
		{
		$xmdfrom='CLIE';
		}

		if(substr($docu,0,2)=='EB')
		{
		$xmdfrom='INVE';
		}

		if(substr($docu,0,2)=='FC')
		{
		$xmdfrom='VTAS';
		}

// 		$sql_fact1='select * from vtas11 where mdfrom="'.$xmdfrom.'" and idfrom="'.$idnum.'" and status!="X"';
		$sql_fact1='select * from vtas1 where mdfrom="'.$xmdfrom.'" and idfrom="'.$idnum.'" and status!="X"';
		$res_fact1=mysql_query($sql_fact1,$link_vcrx);

		if (mysql_num_rows($res_fact1)!=0)
		{
		$xidnum1=mysql_result($res_fact1,0,'idnum1');
		$this->idreturn=$xidnum1;
		}
		else
		{
		$sql_tabla='select *from docu0 where docu_codigo="'.$docu.'"';
		$res_tabla=mysql_query($sql_tabla,$link_vcrx);
		
		$sql_empr='select *from empr1';
		$res_empr=mysql_query($sql_empr,$link_vcrx);
		
		$docu_tabla=mysql_result($res_tabla,0,'docu_tabla');
		$docu_tabla=strtolower($docu_tabla);

		if (mb_stristr('clie.prov',substr($_SESSION['user_vcrx_tipo'],0,4)))
		{$docu_tabla='';}

		$sql_tran1='select *from '.$docu_tabla.'11 where idnum1="'.$idnum.'" and status!="O" and status!="X" and status!="F"';
		$res_tran1=mysql_query($sql_tran1,$link_vcrx);

		$sql_tran2='select *from '.$docu_tabla.'12 where idnum2="'.$idnum.'" order by item_nctlg desc, item_pctlg, item ';
		$res_tran2=mysql_query($sql_tran2,$link_vcrx);
		
// 		$sql_tran3='select * from '.$docu_tabla.'12 where idnum2="'.$idnum.'" and item_descri like "%TRANS%"';
// 		$res_tran3=mysql_query($sql_tran3,$link_vcrx);
		$xenvioa=1;
		
		$xenti = mysql_result($res_tran1,0,'drtr');
		$xconcepto = trim(mysql_result($res_tran1,0,'concepto')).' /'.$docu.'-'.mysql_result($res_tran1,0,'numero').' /'.mysql_result($res_tran1,0,'enti_descri');

		$sql_tabl2='select *from docu0 where docu_codigo like "NV13"';
		$res_tabl2=mysql_query($sql_tabl2,$link_vcrx);
		$xnumero=mysql_result($res_tabl2,0,'docu_numero');
		$xnumero=$xnumero+1;
		
		$sql_docnum='update docu0 set docu_numero='.$xnumero.' where docu_codigo like "NV13"';
		$res_docnum=mysql_query($sql_docnum,$link_vcrx);

		$xnumero=str_pad($xnumero, 9, "0", STR_PAD_LEFT);
		

		$sql_ivta1='insert into 
		vtas1 (
		docu,
		ptoe,
		numero,
		fecha,
		enti,
		concepto,
		status,
		idfrom,
		mdfrom,
		iuser,
		idate)
		value ('
		.'"NV13",'
		.'"001001",'
		.'"'.$xnumero.'"'
		.',NOW(),'
		.'"'.mysql_result($res_tran1,0,'drtr').'",'
		.'"'.$xconcepto.'",'
		.'"-",'
		.''.mysql_result($res_tran1,0,'idnum1').','
		.'"'.$xmdfrom.'",'
		.'"'.$_SESSION['user_vcrx_id'].'",'
		.'NOW())';
		$res_ivta1=mysql_query($sql_ivta1,$link_vcrx);
				
		$sql_idnum1='select max(idnum1) as idnum1 from vtas1 where docu="NV13" and ptoe="001001" and numero="'.$xnumero.'"';
		$sql_idnum1.=' and enti="'.$xenti.'" and iuser="'.$_SESSION['user_vcrx_id'].'"';	
		$res_idnum1=mysql_query($sql_idnum1,$link_vcrx);
		
		$xidnum1=mysql_result($res_idnum1,0,'idnum1');

		if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
		{
			$sql_uvta1='update vtas1 set drtr="'.mysql_result($res_tran1,0,'drtr').'" where idnum1='.$xidnum1;
			$res_uvta1=mysql_query($sql_uvta1,$link_vcrx);
		}

		
		$xiddet = 0;
		$res_tran2=mysql_query($sql_tran2,$link_vcrx);
		while($r_tran2=mysql_fetch_assoc($res_tran2))
		{
		$xiddet++;
		$sql_ivta2='insert into vtas2 (idnum2,iddet,item,cant,puni,dcto,ice,iva,irbp,detalle) 
		value ('
		.'"'.$xidnum1.'",'
		.'"'.$xiddet.'",'
		.'"'.$r_tran2['item'].'",'
		.$r_tran2['cant'].','
		.$r_tran2['puni'].','
		.$r_tran2['dcto'].','
		.$r_tran2['ice'].','
		.$r_tran2['iva'].','
		.$r_tran2['irbp'].','
		.'"'.$r_tran2['detalle'].'"'
		.')';
		$res_ivta2=mysql_query($sql_ivta2,$link_vcrx);
		}

		$sql_stat1='update '.$docu_tabla.'1 set status="C" where idnum1='.$idnum;
		$res_stat1=mysql_query($sql_stat1,$link_vcrx);

	$this->idreturn=$xidnum1;
	
	
		//$res_tran1=mysql_query($sql_tran1,$link_vcrx);
	
		$sql_tabl2='select * from docu0 where docu_codigo like "CC13"';
		$res_tabl2=mysql_query($sql_tabl2,$link_vcrx);
		$xnumero=mysql_result($res_tabl2,0,'docu_numero');
		$xnumero=$xnumero+1;
		$sql_docnum='update docu0 set docu_numero='.$xnumero.' where docu_codigo like "CC13"';
		$res_docnum=mysql_query($sql_docnum,$link_vcrx);
		$xnumero=str_pad($xnumero, 9, "0", STR_PAD_LEFT);

		$xenti = mysql_result($res_tran1,0,'enti');
		
		$sql_icaja1='insert into 
		caja1(
		docu,
		ptoe,
		numero,
		fecha,
		enti,
		concepto,
		status,
		idfrom,
		mdfrom,
		iuser,
		idate)
		value ('
		.'"CC13",'
		.'"001001",'
		.'"'.$xnumero.'"'
		.',NOW(),'
		.'"'.$xenti.'",'
		.'"'.$xconcepto.'",'
		.'"-",'
		.''.mysql_result($res_tran1,0,'idnum1').','
		.'"'.$xmdfrom.'",'
		.'"'.$_SESSION['user_vcrx_id'].'",'
		.'NOW())';
		$res_icaja1=mysql_query($sql_icaja1,$link_vcrx);


		$sql_idnum1='select max(idnum1) as idnum1 from caja1 where docu="CC13" and ptoe="001001" and numero="'.$xnumero.'" and enti="'.$xenti.'" and iuser="'.$_SESSION['user_vcrx_id'].'"';
		$res_idnum1=mysql_query($sql_idnum1,$link_vcrx);
		
		$xidnum1=mysql_result($res_idnum1,0,'idnum1');


		$sql_icaja2='insert into caja2 (idnum2,iddet,docum,ptoem,numer,cant,detalle) 
		value ('
		.'"'.$xidnum1.'",'
		.'"1",'
		.'"'.mysql_result($res_tran1,0,'docu').'",'
		.'"'.mysql_result($res_tran1,0,'ptoe').'",'
		.'"'.mysql_result($res_tran1,0,'numero').'",'
		.'"'.mysql_result($res_tran1,0,'total').'",'
		.'"'.mysql_result($res_tran1,0,'concepto').'"'
		.')';
		$res_icaja2=mysql_query($sql_icaja2,$link_vcrx);
		}
	}
}
?>