<?
session_name('portal_absa');
session_start();
require "../conexion/conectar.php";

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

$rCN = configNodos($_REQUEST['tipodocto']);
if(mysqli_num_rows($rCN) > 0){
	$tCN = mysqli_fetch_assoc($rCN);
}
?>
	<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('','')"><?=$tCN['nodo']?></span></span>
				<?
					getDoce($_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('','');documentoICFDISeleccionado('<?=$tmpDoc["uuid"]?>');documentoSeleccionado('<?=$tmpDoc["uuid"]?>','recibedoce')"><?=$tmpDoc["tipodocto"]." ".$tmpDoc["serie"].$tmpDoc["folio"]?></span></span>
												<?nodos($tmpConfigNodo["id"],$tmpDoc["uuid"]);?>
											</li>
										<?
										}
										?></ul><?
									}
								}
								if($tmpConfigNodo["tabla"] == "doce"){
									
									$rPagoDocRef = getExpedientePagos($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($uuid);
											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('','');documentoICFDISeleccionado('<?=$tmpCFDIRel["uuid"]?>');documentoSeleccionado('<?=$tmpCFDIRel["uuid"]?>','recibedoce')"><?=$tmpCFDIRel["tipodocto"]." ".$tmpCFDIRel["serie"].$tmpCFDIRel["folio"]?></span></span>
												<?
												?>
													</li>
												<?
												}
												?></ul><?
											}
											?>
											</li>
										<?
										}
										?></ul><?
									}
								}
							}
							
						?>
						<?nodos($tmpNodo["id"],$uuid);?>
				</li>
		<?}?>
			</ul><?
	}
	
}

function getExpedientePagos($uuid){
	global $conexion;
	$queryDocs = "select d.tipodocto,d.serie,d.folio,d.adicionalA as uuid from pago_documentos_ref p inner join doce d on p.doce_UUID = d.adicionalA where p.uuidCFDIRelacionado = '$uuid' group by d.adicionalA,p.uuidCFDIRelacionado";
	
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
	return $rDocs;
}

function getDoce($uuid){
	global $conexion;
	$queryDocs = "SELECT tipodocto,rfcemisor,serie,folio,adicionalA as uuid FROM doce
	WHERE adicionalA='$uuid'";
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
		
	if(mysqli_num_rows($rDocs) > 0){
		?><ul><?
		while($tmpDocDoce = mysqli_fetch_assoc($rDocs)){
			?><li iconCls="icon-document">
				<span title=""><span onClick="AdjuntoSeleccionado('');NodoSeleccionado('','');documentoSeleccionado('<?=$tmpDocDoce["uuid"]?>','doce')"><?=$tmpDocDoce["tipodocto"]." ".$tmpDocDoce["serie"].$tmpDocDoce["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($uuid){
	global $conexion;
	$queryDocs = "SELECT rd.serie,rd.tipodocto,rd.folio,rd.rfcemisor,rd.uuid 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 rdpdr.uuidCFDIRelacionado=rd.uuid WHERE rdpdr.doce_UUID='$uuid'";
	
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
	return $rDocs;
}

function getComplementoPago($uuid){
	global $conexion;
	$queryDocs = "SELECT rd.tipodocto,rd.serie,rd.folio,rd.uuid 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 rdpdr.doce_UUID='$uuid' group by rdp.recibedoce_pagos_id";
	/*?><script>$j_7.messager.alert("info","<?=mysqli_real_escape_string($conexion->CONEXION,$queryDocs)?>","info");</script><?*/
	$rDocs = mysqli_query($conexion->CONEXION,$queryDocs);
	return $rDocs;
}

function getRecibeDoce($tipodocto,$serie="",$folio="",$rfcemisor="",$uuid=""){
	global $conexion;
	$queryDocs = "SELECT rfcemisor,tipodocto,serie,folio,uuid FROM recibedoce 
	WHERE tipoDocto='$tipodocto' and rfcEmisor='$rfcemisor' and serie='$serie' and folio='$folio' and uuid='$uuid'";
	
	$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;							
							
}

?>