<?
session_name('portal_absa');
session_start();
$arrHTTP = explode("?",trim(strtoupper($_SERVER['HTTP_REFERER'])));
$http = trim(strtoupper($arrHTTP[0]));

if(!isset($_SESSION['usuario_p_prov_absa']) || trim($_SESSION['usuario_p_prov_absa']) == "" || (trim(strtoupper($_SESSION["URL_PORTAL"]."FORMULARIOS/FORM_EXPEDIENTE_COMPLEMENTOPAGO.PHP")) != $http && trim(strtoupper($_SESSION["URL_PORTAL"]."FORMULARIOS/FORM_EXPEDIENTE_ICFDI.PHP")) != $http && trim(strtoupper($_SESSION["URL_PORTAL"]."FORMULARIOS/FORM_EXPEDIENTE_PAGO.PHP")) != $http)){
	?><script>window.close();</script><?
	exit;
}
require "../conexion/conectar.php";

$conexion = new CONDB();
$conexion->conectar();


?>
	<script type="text/javascript" src="../libs/jqueryEasy_1.2.5/jquery.easyui.min.js"></script>
	<ul id="tt1" class="easyui-tree" animate="true" dnd="true">
			<li iconCls="icon-document">
				<span title=""><span onClick="clicDNodo(false,true,true,false,false,'<?=$_REQUEST['tbl']?>',false,true,0,true,true,false,false,'<?=$_REQUEST['rfcemisor_exp']?>','<?=$_REQUEST['tipodocto_exp']?>','<?=$_REQUEST['serie_exp']?>','<?=$_REQUEST['folio_exp']?>','<?=$_REQUEST['uuid_exp']?>')"><?=$_REQUEST['tipodocto_exp']." - ".$_REQUEST['serie_exp']." ".$_REQUEST['folio_exp']?></span></span>
				<?								
				$queryGroup="select nodo as tipodocto from nodos 
				where nodo not in('OTROS','FACTURAS ADJUNTAS') and (nodo_padre='".$_REQUEST['tipodocto_exp']."' or nodo_padre2='".$_REQUEST['tipodocto_exp']."' or nodo_padre3='".$_REQUEST['tipodocto_exp']."')";
				
				$resultGroup = mysqli_query($conexion->CONEXION,$queryGroup);
				if(mysqli_num_rows($resultGroup) > 0){
				?>
				<ul>
					<?
						while($tmpGroup = mysqli_fetch_array($resultGroup,MYSQLI_ASSOC)){
							nodoGroup($tmpGroup['tipodocto'],$_REQUEST['tipodocto_exp'],$_REQUEST['serie_exp'],$_REQUEST['folio_exp'],$_REQUEST['rfcemisor_exp'],$_REQUEST['uuid_exp'],$_REQUEST['rfcreceptor_exp']);
						}
						
						nodoGroup("OTROS",$_REQUEST['tipodocto_exp'],$_REQUEST['serie_exp'],$_REQUEST['folio_exp'],$_REQUEST['rfcemisor_exp'],$_REQUEST['uuid_exp'],$_REQUEST['rfcreceptor_exp']);
					?>
				</ul>
				<?}?>
			</li>
			<?//}?>
	</ul>
<?

function nodoGroup($tipodocto,$tdocto_exp,$serie_exp,$folio_exp,$rfcemisor_exp,$uuid_exp){
	global $conexion;
	
	$rNodos=mysqli_fetch_assoc(configNodos($tipodocto));
	
	?><li state="closed"><span><span style="cursor:pointer" onClick="clicDNodo(<?=$rNodos['adjuntar']?>,false,false,false,false,'',false,true,false,false,false,false,false,'<?=$rfcemisor_exp?>','<?=$tdocto_exp?>','<?=$serie_exp?>','<?=$folio_exp?>','<?=$uuid_exp?>','<?=$rfcemisor_exp?>','<?=$tdocto_exp?>','<?=$serie_exp?>','<?=$folio_exp?>','<?=$uuid_exp?>','<?=$tipodocto?>','<?=$rfcreceptor_exp?>','<?=$rNodos['tabla']?>','<?=$tipodocto?>')"><?=$tipodocto?></span></span><?
	
	if($tipodocto == "OTROS"){
		$rNodoDocs = nodoAdjuntosDocs($uuid_exp);
	}else{
		$rNodoDocs=nodoDocs($uuid_exp);
	}
	
	if(mysqli_num_rows($rNodoDocs) > 0){
		while($tmpDocs = mysqli_fetch_array($rNodoDocs,MYSQLI_ASSOC)){
			if($tipodocto == "OTROS"){
				nodoRaiz($tmpDocs['tipodocto'],$tmpDocs['serie'],$tmpDocs['folio'],$tmpDocs['rfcemisor'],$tmpDocs['uuid'],$rNodos,$tmpDocs['titulo']);
			}else{
				nodoRaiz($tmpDocs['DoctoCFDI'],$tmpDocs['SerieCFDI'],$tmpDocs['FolioCFDI'],$tmpDocs['RFCCFDI'],$tmpDocs['UUIDCFDI'],$rNodos,$tipodocto." ".$tmpDocs['SerieCFDI'].$tmpDocs['FolioCFDI']);
			}
			
		}
	}
	?></li><?
}

function nodoRaiz($doc_ref,$serie_ref,$folio_ref,$rfcemisor_ref,$uuid_ref,$rNodos,$titulo){
	global $conexion;
	
	$rDocsRef = nodoDocs($uuid_ref);
	$icono="";
	if(mysqli_num_rows($rDocsRef) > 0){
		$numRows=" (".mysqli_num_rows($rDocsRef).")";
		$icono='iconCls="icon-document" state="closed"';
	}
	$documento=$doc_ref." ";
	if(trim($serie_ref=="ADJUNTO")){
		$documento=$titulo." ";
	}
		
	$rTabla=mysqli_fetch_assoc(configNodos($doc_ref));
	
	$esadjunto="false";
	
	if($rTabla['tabla']=="adjuntos")
		$esadjunto="true";
	
	$documento=$doc_ref." ";
		if(trim($serie_ref=="ADJUNTO")){
			$documento=$titulo." ";
			$rTabla['tabla']="adjuntos";
			$esadjunto="true";
		}	
	
	?><ul><li <?=$icono?>><span><span style="font-size: 14px;<?=$styleColor?>" title="<?=$descripcion?>" style="cursor:pointer" 
	onClick="clicDNodo(<?=$rNodos['adjuntar']?>,<?=$rNodos['ver']?>,<?=$rNodos['descargar']?>,<?=$rNodos['rspo']?>,<?=$esadjunto?>,'<?=$rNodos['tabla']?>',<?=$rNodos['detalle_manual']?>,<?=$rNodos['agrega_doc_ref']?>,<?=$rNodos['vobo']?>,<?=$rNodos['cancelar']?>,<?=$rNodos['eliminar']?>,<?=$rNodos['quitar']?>,<?=$rNodos['rechazar']?>,'<?=$rfcemisor_ref?>','<?=$doc_ref?>','<?=$serie_ref?>','<?=$folio_ref?>','<?=$uuid_ref?>','<?=$rNodos['tabla']?>','OTROS')"><?=$documento.$serie_ref." ".$folio_ref?></span></span><?

	if(mysqli_num_rows($rDocsRef) > 0){
		while($tmp=mysqli_fetch_array($rDocsRef,MYSQLI_ASSOC)){
			$rNodosHijos=mysqli_fetch_assoc(configNodos($tmp['tipodocto']));
			nodoHijo($tmp['tipodocto'],$tmp['serie'],$tmp['folio'],$tmp['rfcemisor'],$tmp['uuid'],$doc_ref,$serie_ref,$folio_ref,$rfcemisor_ref,$uuid_ref,$rNodosHijos,$tmp['tipodocto']." ".$tmp['serie'].$tmp['folio']);
		}
	}
	?></li></ul><?

}

function nodoHijo($doc_ref,$serie_ref,$folio_ref,$rfcemisor_ref,$uuid_ref,$doc_ref_padre,$serie_ref_padre,$folio_ref_padre,$rfcemisor_ref_padre,$uuid_ref_padre,$rNodos,$titulo){
	global $conexion;
	
	$rDocsRef = nodoDocs($uuid_ref);
	$icono="";
	if(mysqli_num_rows($rDocsRef) > 0){
		$numRows=" (".mysqli_num_rows($rDocsRef).")";
		$icono='iconCls="icon-document" state="closed"';
	}
		
	$rTabla=mysqli_fetch_assoc(configNodos($doc_ref));
	
	$documento=$doc_ref." ";
		if(trim($serie_ref=="ADJUNTO")){
			$documento=$titulo." ";
		}
		
	$esadjunto="false";
	
	if($rTabla['tabla']=="adjuntos")
		$esadjunto="true";
	
	$documento=$doc_ref." ";
	if(trim($serie_ref=="ADJUNTO")){
		$documento=$titulo." ";
		$rTabla['tabla']="adjuntos";
		$esadjunto="true";
		//$rNodos['tabla']="adjuntos";
	}
		
	?><ul><li <?=$icono?>><span><span style="font-size: 14px;" title="<?=$descripcion?>" style="cursor:pointer" onClick="clicDNodo(<?=$rNodos['adjuntar']?>,<?=$rNodos['ver']?>,<?=$rNodos['descargar']?>,<?=$rNodos['rspo']?>,<?=$esadjunto?>,'<?=$rNodos['tabla']?>',<?=$rNodos['detalle_manual']?>,<?=$rNodos['agrega_doc_ref']?>,<?=$rNodos['vobo']?>,<?=$rNodos['cancelar']?>,<?=$rNodos['eliminar']?>,<?=$rNodos['quitar']?>,<?=$rNodos['rechazar']?>,'<?=$rfcemisor_ref?>','<?=$doc_ref?>','<?=$serie_ref?>','<?=$folio_ref?>','<?=$uuid_ref?>','<?=$rfcemisor_ref_padre?>','<?=$doc_ref_padre?>','<?=$serie_ref_padre?>','<?=$folio_ref_padre?>','<?=$uuid_ref_padre?>','<?=$doc_ref?>','<?=$rfcreceptor?>','<?=$rNodos['tabla']?>','OTROS')"><?=$documento.$serie_ref." ".$folio_ref.$numRows." ".$icoVobo?></span></span><?

	if(mysqli_num_rows($rDocsRef) > 0){
		while($tmp=mysqli_fetch_array($rDocsRef,MYSQLI_ASSOC)){
			$rNodosHijos=mysqli_fetch_assoc(configNodos($tmp['tipodocto']));
			nodoHijo($tmp['tipodocto'],$tmp['serie'],$tmp['folio'],$tmp['rfcemisor'],$tmp['uuid'],$doc_ref,$serie_ref,$folio_ref,$rfcemisor_ref,$uuid_ref,$rNodosHijos,$tmp['tipodocto']." ".$tmp['serie'].$tmp['folio']);
		}
	}
	?></li></ul><?

}

function nodoDocs($uuid_exp){
	global $conexion;
	$queryDocs = "SELECT d.rfcemisor,d.tipodocto,d.serie,d.folio,d.uuid,r.tipoDocto as DoctoCFDI,r.rfcEmisor as RFCCFDI
,r.serie as SerieCFDI,r.folio as FolioCFDI,r.uuid as UUIDCFDI	FROM doce d inner join pago_documentos_ref p ON d.uuid=p.doce_UUID inner join recibedoce r ON p.uuidCFDIRelacionado = r.uuid WHERE doce_UUID='$uuid_exp'";
	
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
	return $rDocs;
}
function nodoAdjuntosDocs($uuid_exp){
	global $conexion;
	$queryDocs = "SELECT dr.rfcemisor, dr.tipodocto, dr.serie, dr.folio, dr.uuid, dr.descripcion, dr.titulo	FROM doce d inner join documentosref dr 
	ON dr.uuid_ref=d.UUID and dr.rfcemisor_ref=d.RFCEmisor and dr.doc_ref=d.tipoDocto and dr.serie_ref=d.serie and dr.folio_ref=d.folio WHERE dr.uuid_ref='$uuid_exp'";
	
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
	return $rDocs;
}

function configNodos($tipodocto){
	global $conexion;
$queryNodos="select adjuntar,ver,descargar,rspo,detalle_manual,esadjunto,tabla,agrega_doc_ref,vobo,cancelar,eliminar,quitar,rechazar from nodos where nodo='$tipodocto' limit 1";
$resultadoNodos = mysqli_query($conexion->CONEXION,$queryNodos);
return $resultadoNodos;							
							
}

?>