<?
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();
$rN = configNodos($_REQUEST['tipodocto']);
if(mysqli_num_rows($rN) > 0){
	$tmpN = mysqli_fetch_assoc($rN);
}
?>
	<script type="text/javascript" src="../libs/jqueryEasy_1.2.5/jquery.easyui.min.js"></script>
	<ul id="tt1" class="easyui-tree" animate="true" dnd="false">
			<li iconCls="icon-folder" state="open">
				<span title=""><span onClick="AdjuntoSeleccionado('');NodoSeleccionado('','');documentoSeleccionado('','')"><?=$tmpN['nodo']?></span></span>
				<?
					getRecibeDoce($_REQUEST['uuid']);
					nodos($_REQUEST['tipodocto'],$_REQUEST['uuid']);				
				?>
			</li>
	</ul>
<?

function nodos($idTipoDocto,$uuid=""){
	global $conexion;
	
	$query = "select id,nodo,nodo_padre from nodos where nodo_padre='$idTipoDocto'";
	$result = mysqli_query($conexion->CONEXION,$query);
	if(mysqli_num_rows($result) > 0){
		?>	<ul><?
		while($tmpNodo = mysqli_fetch_array($result,MYSQLI_ASSOC)){
				$rConfigNodo = configNodos($tmpNodo["id"]);					
				if(mysqli_num_rows($rConfigNodo) > 0){
					$tmpConfigNodo = mysqli_fetch_assoc($rConfigNodo);
				}
		?>
				<li iconCls="icon-folder" state="closed">
					<span title=""><span onClick="<?if($tmpConfigNodo["adjuntar"] == "1"){?>NodoSeleccionado(<?=$tmpNodo["id"]?>,'<?=$uuid?>');<?}else{?>NodoSeleccionado('','');<?}?>documentoSeleccionado('','');AdjuntoSeleccionado('');"><?=$tmpNodo["nodo"]?></span></span>
						<?
							if(mysqli_num_rows($rConfigNodo) > 0){
								if($rNodoAdjuntos = mysqli_query($conexion->CONEXION,"select id,nodo_id,uuid_padre,nombre_archivo from adjuntos_exp_pago where nodo_id=".$tmpNodo["id"]." and uuid_padre='".$uuid."'")){
									if(mysqli_num_rows($rNodoAdjuntos) > 0){
										?><ul><?
											while($tmpAdj = mysqli_fetch_array($rNodoAdjuntos,MYSQLI_ASSOC)){
											?>	<li iconCls="icon-document">
													<span title=""><span onClick="NodoSeleccionado('','');AdjuntoSeleccionado(<?=$tmpAdj["id"]?>);documentoSeleccionado('<?=$tmpAdj["id"]?>','adjuntos_exp_pago')"><?=$tmpAdj["nombre_archivo"]?></span></span>
												</li>
											<?
											}
										?></ul>
								
									<?}
								}
								
								if($tmpConfigNodo["tabla"] == "pago_documentos_ref"){
									$rPagoDocRef = pago_documentos_ref($uuid);
									if(mysqli_num_rows($rPagoDocRef) > 0){
										?><ul><?
										while($tmpDoc = mysqli_fetch_array($rPagoDocRef,MYSQLI_ASSOC)){
										?>	<li iconCls="icon-document">
												<span title=""><span onClick="AdjuntoSeleccionado('');NodoSeleccionado('','');documentoSeleccionado('<?=$tmpDoc["uuid"]?>','doce')"><?=$tmpDoc["tipodocto"]." ".$tmpDoc["serie"].$tmpDoc["folio"]?></span></span>
											</li>
										<?
										}
										?></ul><?
									}
								}
								if($tmpConfigNodo["tabla"] == "recibedoce"){
									
									$rComplementoPago = getComplementoPago($uuid);
									
									if(mysqli_num_rows($rComplementoPago) > 0){
										?><ul><?
										while($tmpComplementoPago = mysqli_fetch_array($rComplementoPago,MYSQLI_ASSOC)){
										?>	<li iconCls="icon-document">
												<span title=""><span onClick="AdjuntoSeleccionado('');NodoSeleccionado('','');documentoSeleccionado('<?=$tmpComplementoPago["uuid"]?>','recibedoce')"><?=$tmpComplementoPago["tipodocto"]?> <?=$tmpComplementoPago["serie"].$tmpComplementoPago["folio"]?></span></span>
										<?
											
										?>
											</li>
										<?
										}
										?></ul><?
									}
								}
								if($tmpConfigNodo["tabla"] == "recibedoce_pagos"){
									
									$rPagos = getPagos($uuid);
									
									if(mysqli_num_rows($rPagos) > 0){
										?><ul><?
										while($tmpPagos = mysqli_fetch_array($rPagos,MYSQLI_ASSOC)){
										?>	<li iconCls="icon-folder" state="closed">
												<span title=""><span onClick="AdjuntoSeleccionado('');NodoSeleccionado('','');documentoSeleccionado('','')">PAGO DE <?=$tmpPagos["Monto"]?></span></span>
										<?
											$rCFDIRel = recibedoce_pagos_docto_relacionado($tmpPagos['recibedoce_pagos_id']);
											if(mysqli_num_rows($rCFDIRel) > 0){
												?><ul><?
												while($tmpCFDIRel = mysqli_fetch_array($rCFDIRel,MYSQLI_ASSOC)){
												?>	<li iconCls="icon-document">
														<span title=""><span onClick="AdjuntoSeleccionado('');NodoSeleccionado('','');documentoSeleccionado('<?=$tmpCFDIRel["uuid"]?>','recibedoce')"><?=$tmpCFDIRel["tipodocto"]." ".$tmpCFDIRel["serie"].$tmpCFDIRel["folio"]?></span></span>
												<?
												?>
													</li>
												<?
													$uuidNP = $tmpCFDIRel["doce_UUID"];
												}
												if(trim($uuidNP) != ""){
													$rNP = getNP($uuidNP);
													if(mysqli_num_rows($rNP) > 0){
														$tNP = mysqli_fetch_assoc($rNP);
														?><li iconCls="icon-document">
															<span title=""><span onClick="AdjuntoSeleccionado('');NodoSeleccionado('','');documentoSeleccionado('<?=$uuidNP?>','doce')"><?=$tNP["tipodocto"]." ".$tNP["serie"].$tNP["folio"]?></span></span>
														</li>
													<?
													}
												}
												?></ul><?
											}
											?>
											</li>
										<?
										}
										?></ul><?
									}
								}
							}
							
						?>
						<?nodos($tmpNodo["id"],$uuid);?>
				</li>
		<?}?>
			</ul><?
	}
	
}

function getRecibeDoce($uuid){
	global $conexion;
	$queryDocs = "SELECT rfcemisor,tipodocto,serie,folio,uuid FROM recibedoce 
	WHERE uuid='$uuid'";
	$rDoc = mysqli_query($conexion->CONEXION,$queryDocs);
	if(mysqli_num_rows($rDoc) > 0){
		?><ul><?
		$tmp = mysqli_fetch_assoc($rDoc);
			?><li iconCls="icon-document">
				<span title=""><span onClick="AdjuntoSeleccionado('');NodoSeleccionado('','');documentoSeleccionado('<?=$uuid?>','recibedoce')"><?=$tmp["tipodocto"]." ".$tmp["serie"].$tmp["folio"]?></span></span>
			</li>
		</ul><?
	}
}

function pago_documentos_ref($uuid){
	global $conexion;
	$queryDocs = "SELECT r.tipoDocto as tipodocto,r.rfcEmisor as rfcemisor
,r.serie as serie,r.folio as folio,r.uuid as uuid FROM doce d inner join pago_documentos_ref p ON d.adicionalA=p.doce_UUID inner join recibedoce r ON p.uuidCFDIRelacionado = r.uuid WHERE doce_UUID='$uuid'";
	
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
	return $rDocs;
}

function recibedoce_pagos_docto_relacionado($id){
	global $conexion;
	$queryDocs = "SELECT rd.serie,rd.tipodocto,rd.folio,rd.rfcemisor,rd.uuid ,rdpdr.doce_UUID FROM recibedoce_pagos_docto_relacionado rdpdr inner join recibedoce rd ON rd.uuid = rdpdr.uuidCFDIRelacionado WHERE rdpdr.recibedoce_pagos_id='$id'";
	
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
	return $rDocs;
}

function getComplementoPago($uuid){
	global $conexion;
	$queryDocs = "SELECT rd.serie,rd.folio,rd.uuid,rd.tipodocto FROM recibedoce_pagos_docto_relacionado rdpdr inner join recibedoce_pagos rdp
	ON rdp.recibedoce_pagos_id = rdpdr.recibedoce_pagos_id inner join recibedoce rd ON rd.uuid=rdp.recibedoce_UUID WHERE rdpdr.doce_UUID='$uuid' limit 1";
	
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
	return $rDocs;
}

function getPagos($uuid){
	global $conexion;
	
	$queryDocs = "SELECT rdp.recibedoce_pagos_id,rdp.Monto FROM recibedoce_pagos_docto_relacionado rdpdr inner join recibedoce_pagos rdp
	ON rdp.recibedoce_pagos_id = rdpdr.recibedoce_pagos_id WHERE rdp.recibedoce_UUID='$uuid' group by rdp.recibedoce_pagos_id";
	
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
	return $rDocs;
}

function getDoce($tipodocto,$serie="",$folio="",$rfcemisor="",$uuid=""){
	global $conexion;
	$queryDocs = "SELECT tipodocto,rfcemisor,serie,folio,adicionalA as uuid FROM doce 
	WHERE tipoDocto='$tipodocto' and rfcEmisor='$rfcemisor' and serie='$serie' and folio='$folio' and adicionalA='$uuid'";
	
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
	return $rDocs;
}

function getNP($uuid=""){
	global $conexion;
	$queryDocs = "SELECT tipodocto,rfcemisor,serie,folio,adicionalA as uuid FROM doce 
	WHERE adicionalA='$uuid' and status=1";
	
	$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.adicionalA 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($idnodo){
	global $conexion;
$queryNodos="select adjuntar,ver,descargar,rspo,detalle_manual,esadjunto,tabla,agrega_doc_ref,vobo,cancelar,eliminar,quitar,rechazar,nodo,nodo_padre,id from nodos where id='$idnodo' limit 1";
$resultadoNodos = mysqli_query($conexion->CONEXION,$queryNodos);
return $resultadoNodos;							
							
}

?>