<?php 
class vrfacturar
{
var $idreturn;

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

    $xfechanow=date('Y-m-d');
		
		$xmdfrom='X';		

    if(mb_stristr('PF.PC',substr($docu,0,2)))
		{$xmdfrom='CLIE';}

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

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


    $xdocu='FC10';
    $xstruc='';

		$sql_ofact1='select * from '.strtolower($xmdfrom).'1 where  idnum1="'.$idnum.'"';
		$res_ofact1=mysql_query($sql_ofact1,$link_vcrx);
//     echo '*/*/*/';
//     echo $sql_ofact1;
//     echo mysql_error();

		//$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" and status!="O"';
    
		if (mysql_num_rows($res_ofact1)!=0)
		{
      if (mysql_result($res_ofact1,0,'status')=='F')
		  {$sql_fact1='select * from vtas1 where mdfrom="'.$xmdfrom.'" and idfrom="'.$idnum.'" and status!="X"';}
// 		  {$sql_fact1='select * from vtas11 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);

      $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.'1 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 idnum1="'.$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 idnum1="'.$idnum.'" and item_descri like "%TRANS%"';
      $res_tran3=mysql_query($sql_tran3,$link_vcrx);
      $xenvioa=1;

      $xenti = mysql_result($res_tran1,0,'enti');
      $xconcepto = trim(mysql_result($res_tran1,0,'concepto')).' /'.$docu.'-'.mysql_result($res_tran1,0,'numero');

      $xptoe= mysql_result($res_tran1,0,'ptoe');
      $xptov = 'V101';
      if($cfg_empr='gnya'){$xptov='005001';}
      if($cfg_empr='gmny'){$xptov='003101';}
      if($cfg_empr='mzlj'){$xptov='001101';}


      $xvndd = mysql_result($res_tran1,0,'vndd');
      if ($xvndd=="" or $xvndd="-")	{$xvndd=$_SESSION['user_vcrx_id'];}

        // comentariado el 4 de mayo
  // 		$sql_tabl2='select *from docu0 where docu_codigo like "'.$xdocu.'"';
  // 		$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 "'.$xdocu.'"';
  // 		$res_docnum=mysql_query($sql_docnum,$link_vcrx);

  // aqui numdoc

      $sql_numdoc='select * from numdocu where docu="'.$xdocu.'" and ptoe="'.$xptoe.'"';
      $res_numdoc=mysql_query($sql_numdoc,$link_vcrx);
      $xnumdoc=mysql_result($res_numdoc,0,'numero');
      $xnumdoc=$xnumdoc+1;
      $xnumero=$xnumdoc;

      if($_SESSION['user_vcrx_empr']=='gmny')
      {
        $xptoe="003001";
        $sql_numdoc='select * from numdocu where docu="'.$xdocu.'" and ptoe="'.$xptoe.'"';
        $res_numdoc=mysql_query($sql_numdoc,$link_vcrx);
        $xnumdoc=mysql_result($res_numdoc,0,'numero');
        $xnumdoc=$xnumdoc+1;
        $xnumero=$xnumdoc;
      }


      if($_SESSION['user_vcrx_empr']=='gnya')
      {
        $xptoe="005001";
        $sql_numdoc='select * from numdocu where docu="'.$xdocu.'" and ptoe="'.$xptoe.'"';
        $res_numdoc=mysql_query($sql_numdoc,$link_vcrx);
        $xnumdoc=mysql_result($res_numdoc,0,'numero');
        $xnumdoc=$xnumdoc+1;
        $xnumero=$xnumdoc;
      }
      
      if($_SESSION['user_vcrx_empr']=='mzlj')
      {
        $xptoe="001101";
        $sql_numdoc='select * from numdocu where docu="'.$xdocu.'" and ptoe="'.$xptoe.'"';
        $res_numdoc=mysql_query($sql_numdoc,$link_vcrx);
        $xnumdoc=mysql_result($res_numdoc,0,'numero');
        $xnumdoc=$xnumdoc+1;
        $xnumero=$xnumdoc;
      }
      
      if($cfg_empr=='gnya'){$xptoe='005001';}
      if($cfg_empr=='gmny'){$xptoe='003101';}
      if($cfg_empr=='mzlj'){$xptoe='001101';}

//       $xnumero=str_pad($xnumero, 9, "0", STR_PAD_LEFT);
      $xnumero=nume_docu($xdocu,$xptoe);
//       echo '*/*/*/*'.$xnumero;
//       return $xnumero;
      
      $sql_ivta1='insert into 
      vtas1 (
      docu,
      ptoe,
      numero,
      fecha,
      enti,
      drtr,
      concepto,
      vndd,
      ptov,
      status,
      idfrom,
      mdfrom,
      iuser,
      idate)
      value ('
      .'"'.$xdocu.'",'
      .'"'.$xptoe.'",'
      .'"'.$xnumero.'"'
      .',"'.$xfechanow.'",'
      .'"'.mysql_result($res_tran1,0,'enti').$xstruc.'",'
      .'"'.mysql_result($res_tran1,0,'drtr').'",'
      .'"'.$xconcepto.'",'
      .'"'.$xvndd.'",'
      .'"'.$xptov.'",'
      .'"-",'
      .''.mysql_result($res_tran1,0,'idnum1').','
      .'"'.$xmdfrom.'",'
      .'"'.$_SESSION['user_vcrx_id'].'",'
      .'NOW())';

      //$res_ivta1=mysql_query($sql_ivta1,$link_vcrx);
      mysql_query($sql_ivta1,$link_vcrx);

      $sql_idnum1='select max(idnum1) as idnum1 from vtas1 where docu="'.$xdocu.'" and ptoe="'.$xptoe.'" 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');

      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);
      }

      if (mb_stristr('tcor',$_SESSION['user_vcrx_empr']))
      {
        $sql_uvta1='update vtas1 set contrato="'.mysql_result($res_tran1,0,'contrato').'" 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);

        if($_SESSION['user_vcrx_empr']=='ecgl')
        {
          $sql_updt ='update vtas2 set ';
          $sql_updt.='itnum="'.$r_tran2['itnum'].'",';
          $sql_updt.='lmns="'.$r_tran2['lmns'].'",';
          $sql_updt.='mm_l="'.$r_tran2['mm_l'].'",';
          $sql_updt.='mm_a="'.$r_tran2['mm_a'].'",';
          $sql_updt.='mm_e="'.$r_tran2['mm_e'].'",';
          $sql_updt.='tmpl="'.$r_tran2['tmpl'].'",';
          $sql_updt.='puli="'.$r_tran2['puli'].'",';
          $sql_updt.='perfor1="'.$r_tran2['perfor1'].'",';
          $sql_updt.='saques1="'.$r_tran2['saques1'].'",';
          $sql_updt.='detalle="'.$r_tran2['detalle'].'"';
          $sql_updt.='where idnum2="'.$xidnum1.'" and iddet="'.$xiddet.'"';
          $res_updt=mysql_query($sql_updt,$link_vcrx);
        }

      }

      $sql_tran2='update vtas2 set prop=0 where prop is null and idnum2='.$xidnum1;
      $res_tran2=@mysql_query($sql_tran2,$link_vcrx);

      $sql_tran2='update vtas2 set ssid=0 where ssid is null and idnum2='.$xidnum1;
      $res_tran2=@mysql_query($sql_tran2,$link_vcrx);

      
      
      $sql_tran1='update '.$docu_tabla.'1 set status="F" where idnum1='.$idnum;
      $res_tran1=mysql_query($sql_tran1,$link_vcrx);

      $this->idreturn=$xidnum1;
    }
	}
}

?>