<?php
class vrinventar
{
var $idreturn;

	//function vrinventar($docu,$idnum)
	//{echo $docu.'-'.$idnum.'*';}
	function vrinventar($docu,$idnum)
	{
		require('../cfg/res.php');
		include('../_cfg/config.php');
		$xfechanow=date('Y-m-d');
		
		$xmdfrom='X';		
		$xbode='B010';
		$xbode2='';
		$xptoe='001001';
	        	
		if(mb_stristr('.FC.',substr($docu,0,2)))
		{
	    $xmdfrom='VTAS';
			$xdocu='EB10';
		}

		if(mb_stristr('.NC.',substr($docu,0,2)))
		{
	    $xmdfrom='VTAS';
			$xdocu='IB12';
		}

    //if(mb_stristr('EB13.IB13',substr($docu,0,4)))
		if(mb_stristr('EB13.',substr($docu,0,4)))
		{
			$xmdfrom='INVE';
			$xdocu='IB13';
		}


		if(mb_stristr('IB11.',substr($docu,0,4)))
		{
			$xmdfrom='INVE';
			$xdocu='EB11';
		}


		if(mb_stristr('.FP.',substr($docu,0,2)))
		{
	    $xmdfrom='COMP';
			$xdocu='IB10';
		}

	if($xmdfrom=='X'){die('Documento origen No VALIDO');}

		$sql_odocu1='select * from '.strtolower($xmdfrom).'1 where  idnum1="'.$idnum.'"';
		$res_odocu1=mysql_query($sql_odocu1,$link_vcrx);

		// validar que solo sean bienes
		$sql_odocu2='select * from '.strtolower($xmdfrom).'2 where  idnum2="'.$idnum.'" and item in (select item_codigo from bien0)';
		$res_odocu2=mysql_query($sql_odocu2,$link_vcrx);

// 		echo $sql_odocu1;
		$xbode2='';
    
//     echo '*/*/*/*/*/*/*'.$cfg_empr;

		if($xmdfrom=='VTAS' and mb_stristr('imcp.tgck',$cfg_empr)){return;}
    
    
		if($xmdfrom=='VTAS')
		{
			$xptov=mysql_result($res_odocu1,0,'ptov');
			$sql_ptov='select * from ptov0 where ptov_codigo="'.$xptov.'"';
			$res_ptov=mysql_query($sql_ptov,$link_vcrx);
			$xbode=mysql_result($res_ptov,0,'ptov_bode');
			if(empty($xbode)){$xbode='B010';}
		}

		if ($xmdfrom=='INVE')
		{
			$xbode=mysql_result($res_odocu1,0,'bode');
			if(mb_stristr('EB13.',substr($docu,0,4)))
			{
			$xbode=mysql_result($res_odocu1,0,'tobode');
			$xbode2=mysql_result($res_odocu1,0,'bode');
			}
		}


		if ($xmdfrom=='COMP')
		{
			$xbode='B010';
		}

    
		$sql_odocu2='select * from '.strtolower($xmdfrom).'2 where  idnum2="'.$idnum.'" and item in (select item_codigo from item0 where item_bser="1")';
		$res_odocu2=mysql_query($sql_odocu2,$link_vcrx);
    if(mysql_num_rows($res_odocu2)==0){return;}
    

		$sql_bode='select * from bode0 where bode_codigo="'.$xbode.'"';
		$res_bode=mysql_query($sql_bode,$link_vcrx);
		$xptoe=mysql_result($res_bode,0,'bode_ptoe');
		if(empty($xptoe)){$xptoe='001001';}

		$sql_docu1='select * from inve1 where mdfrom="'.$xmdfrom.'" and idfrom="'.$idnum.'" ';
		$res_docu1=mysql_query($sql_docu1,$link_vcrx);


		$xnumero1=1;
		if (mysql_num_rows($res_docu1)==0)
		{

// 			$sql_ndocu='select * from numdocu where docu="'.$xdocu.'" and ptoe="'.$xptoe.'" ';
// 			$res_ndocu=mysql_query($sql_ndocu,$link_vcrx);
			
// 			if (mysql_num_rows($res_ndocu)!=0)
// 			{$xnumero1=mysql_result($res_ndocu,0,'numero')+1;}
			
      
      $xnumero1=nume_docu($xdocu,$xptoe);
			$xnumero1=str_pad($xnumero1, 9, "0", STR_PAD_LEFT);

			$sql_docu1='insert into inve1(numero,mdfrom,idfrom,iuser,idate) values("'.$xnumero1.'","'.$xmdfrom.'","'.$idnum.'","'.$_SESSION['user_vcrx_id'].'",now() )';
			$res_docu1=mysql_query($sql_docu1,$link_vcrx);

// 			echo '+++'.$sql_docu1;
		
			$sql_docu1='select * from inve1 where mdfrom="'.$xmdfrom.'" and idfrom="'.$idnum.'" ';
			$res_docu1=mysql_query($sql_docu1,$link_vcrx);
		}

		$xidnum1=mysql_result($res_docu1,0,'idnum1');
		$this->idreturn=$xidnum1;
		$xnumero1=mysql_result($res_docu1,0,'numero');
		$xstatus=mysql_result($res_docu1,0,'status');
		$xconcepto=mysql_result($res_odocu1,0,'concepto').'/'.$docu.'-'.mysql_result($res_odocu1,0,'numero');

		$sql_uinve='update inve1 set ';
		$sql_uinve.='docu="'.$xdocu.'",';
		$sql_uinve.='bode="'.$xbode.'",';
		$sql_uinve.='tobode="'.$xbode2.'",';
		$sql_uinve.='ptoe="'.$xptoe.'",';
		$sql_uinve.='numero="'.$xnumero1.'",';
		$sql_uinve.='fecha="'.mysql_result($res_odocu1,0,'fecha').'",';
		$sql_uinve.='enti="'.mysql_result($res_odocu1,0,'enti').'",';
// 		$sql_uinve.='iuser="'.mysql_result($res_odocu1,0,'iuser').'",';
// 		$sql_uinve.='idate="'.mysql_result($res_odocu1,0,'idate').'",';
		$sql_uinve.='status="'.$xstatus.'",';
		$sql_uinve.='concepto="'.$xconcepto.'" ';
		$sql_uinve.='where idnum1="'.$xidnum1.'"';

		$res_uinve=mysql_query($sql_uinve,$link_vcrx);


		
		if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
		{
		//	$sql_uvta1='update inve1 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 inve1 set contrato="'.mysql_result($res_tran1,0,'contrato').'" where idnum1='.$xidnum1;
		//	$res_uvta1=mysql_query($sql_uvta1,$link_vcrx);
		}
		
		if ($xstatus!="X" and $xstatus!="O")
		{		
		$sql_dinve2='delete from inve2 where idnum2="'.$xidnum1.'"';
		$res_dinve2=mysql_query($sql_dinve2,$link_vcrx);

		$xiddet = 0;
		// validar que solo sean bienes
// 		$sql_odocu2='select * from '.strtolower($xmdfrom).'2 where  idnum2="'.$idnum.'" and item in (select item_codigo from bien0)';

		$sql_odocu2='select * from '.strtolower($xmdfrom).'2 where  idnum2="'.$idnum.'" and item in (select item_codigo from item0 where item_bser="1")';
		$res_odocu2=mysql_query($sql_odocu2,$link_vcrx);
		//echo '++'.$sql_odocu2.'#####';

		while($r_odocu2=mysql_fetch_assoc($res_odocu2))
		{
		$xiddet++;
		$sql_iinve2='insert into inve2 (idnum2,iddet) value ("'.$xidnum1.'","'.$xiddet.'")';
		$res_iinve2=mysql_query($sql_iinve2,$link_vcrx);
		
		$xdetalle=$r_odocu2['detalle'];
		
		$sql_uinve='update inve2 set ';

			if(mb_stristr('IB11.',substr($docu,0,4)))
			{
			$sql_uinve.='item="'.$r_odocu2['cambio'].'",';
			$sql_uinve.='cambio="'.$r_odocu2['item'].'",';
			$xdetalle='CAMBIO : '.$r_odocu2['item'].' X '.$r_odocu2['cambio'];
			}
			else
			{
			$sql_uinve.='item="'.$r_odocu2['item'].'",';
			}
		
		$sql_uinve.='cant="'.$r_odocu2['cant'].'",';
		$sql_uinve.='puni="'.$r_odocu2['puni'].'",';
		$sql_uinve.='detalle="'.$xdetalle.'" ';
		$sql_uinve.='where idnum2="'.$xidnum1.'" and iddet="'.$xiddet.'"';
		//echo $sql_uinve;
		$res_uinve=mysql_query($sql_uinve,$link_vcrx);
		}
		}

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

?>