<?
/*
Autor: Lic. Carlos Alberto Vaca Coronado
Version: 3.2.0
*/

session_name('portal_absa');session_start();if(!isset($_SESSION['usuario_p_prov_absa']) || trim($_SESSION['usuario_p_prov_absa']) == "" || trim(strtoupper($_SESSION["URL_PORTAL"]."admin.php")) != trim(strtoupper($_SERVER['HTTP_REFERER']))){?><script>window.location.href="<?=$_SESSION["URL_PORTAL"]."index.php"?>"</script><?exit;}

$headerNames = explode(",",$_POST['headerNames']);
$optionsCol = explode(";",$_POST['optionsCol']);

for($i = 0; $i < count($optionsCol); $i++){
	$optionCol = explode("|",$optionsCol[$i]);
	$optNameCol[$i]["name"] = trim($optionCol[0]);
	$optNameCol[$i]["options"] = trim($optionCol[1]);
}
$key_grid_datos="";
$optionsForm = explode(";;",$_POST['optionsForm']);
$fieldKeys="";
if($_POST['optionsForm'] != ""){
	for($i = 0; $i < count($optionsForm); $i++){
		$optForm = explode("|",$optionsForm[$i]);
		if($optForm[1] == "true"){
			if($fieldKeys == ""){
				$fieldKeys=$optForm[0];
				$key_grid_datos=$optForm[0];
			}else{
				$fieldKeys.="|".$optForm[0];
			}
		}
	}	
}
 
$hiddenCol = explode(",",$_POST['hiddenCol']);
?>					  
<style>
	th,td{font-size:12px}
		
.btnCSS {
	-moz-box-shadow: 0px 10px 14px -7px #3e7327;
	-webkit-box-shadow: 0px 10px 14px -7px #3e7327;
	box-shadow: 0px 10px 14px -7px #3e7327;
	background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #77b55a), color-stop(1, #72b352));
	background:-moz-linear-gradient(top, #77b55a 5%, #72b352 100%);
	background:-webkit-linear-gradient(top, #77b55a 5%, #72b352 100%);
	background:-o-linear-gradient(top, #77b55a 5%, #72b352 100%);
	background:-ms-linear-gradient(top, #77b55a 5%, #72b352 100%);
	background:linear-gradient(to bottom, #77b55a 5%, #72b352 100%);
	filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#77b55a', endColorstr='#72b352',GradientType=0);
	background-color:#77b55a;
	-moz-border-radius:4px;
	-webkit-border-radius:4px;
	border-radius:4px;
	border:1px solid #4b8f29;
	display:inline-block;
	cursor:pointer;
	color:#ffffff;
	font-family:Arial;
	font-size:13px;
	font-weight:bold;
	padding:6px 12px;
	text-decoration:none;
	text-shadow:0px 1px 0px #5b8a3c;
}
.btnCSS:hover {
	background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #72b352), color-stop(1, #77b55a));
	background:-moz-linear-gradient(top, #72b352 5%, #77b55a 100%);
	background:-webkit-linear-gradient(top, #72b352 5%, #77b55a 100%);
	background:-o-linear-gradient(top, #72b352 5%, #77b55a 100%);
	background:-ms-linear-gradient(top, #72b352 5%, #77b55a 100%);
	background:linear-gradient(to bottom, #72b352 5%, #77b55a 100%);
	filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#72b352', endColorstr='#77b55a',GradientType=0);
	background-color:#72b352;
}
.btnCSS:active {
	position:relative;
	top:1px;
}

	</style>
	<br/>
	
	
        <script>
	$j_6(document).ready(function() {
	
		$j_6("#<?=$_POST['idGrid']?>").jqGrid({
				url:"<?=$_POST['urlDatos']?>?key=<?=$key_grid_datos?>&ocultos=<?=$_POST['hiddenCol']?>",
				datatype: "json",
				colNames:[
				<?for($i = 0; $i < count($headerNames); $i++){?>
						<?if($i==count($headerNames)-1){?>
							'<?=$headerNames[$i]?>'
						<?}else{?>
							'<?=$headerNames[$i]?>',
						<?}?>
					<?}?>
				
				],
				colModel:[
					<?for($i = 0; $i < count($optionsCol); $i++){
						$options="";
						$options=$optNameCol[$i]["options"];
						?>
							<?if($i==count($optionsCol)-1){?>
								{name:'<?=$optNameCol[$i]['name']?>',index:'<?=$optNameCol[$i]['name']?>'<?if($options != ""){echo ", ".str_replace("\\","",$options);}?>}
							<?}else{?>
								{name:'<?=$optNameCol[$i]['name']?>',index:'<?=$optNameCol[$i]['name']?>'<?if($options != ""){echo ", ".str_replace("\\","",$options);}?>},
							<?}?>
					<?	
					  }?>
				],
				rowNum:<?=$_POST['rowNum']?>,
				rowTotal: 6000,
				width:<?=$_POST['width']?>,
				rowList:[10,20,30],
				pager: '#<?=$_POST['idPagina']?>',
				sortname: '<?=$_POST['sortname']?>',
				viewrecords: false,
				sortorder: "desc",
				loadonce: false,
				caption: "<?=$_POST['sheetName']?>",
				shrinkToFit: false,
				rownumbers: false,
				gridview: true,
				height:'<?=$_POST['height']?>',
				editurl: 'grids/editurl.php',
				loadError : function(xhr, st, str){
					// alert("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);
					alert("Type: "+st+"; Response: "+ xhr.status + " "+xhr.responseText);
				},
				 loadBeforeSend: function(jqXHR)  {

					// jqXHR.setRequestHeader("Authorization", basicAuthCredentials);
					 $j_6(this).jqGrid('hideCol',["totalPagoDocumentosRef"]); 
					 $j_6(this).jqGrid('hideCol',["totalIngresoCompensado"]); 
					 $j_6(this).jqGrid('hideCol',["ImpPagadoComplemento"]); 
					 $j_6(this).jqGrid('hideCol',["totalEgresoCompensado"]); 
					 $j_6(this).jqGrid('hideCol',["status_pagada"]); 
					 $j_6(this).jqGrid('hideCol',["status_compensada"]);
				},
				loadComplete: function(data){
						/*<?if($_SESSION["MODULO_PAGOS"] != "1"){?>
							 $j_6(this).jqGrid('hideCol',["totalPagoDocumentosRef"]); 
							 $j_6(this).jqGrid('hideCol',["totalIngresoCompensado"]); 
							 $j_6(this).jqGrid('hideCol',["ImpPagadoComplemento"]); 
							 $j_6(this).jqGrid('hideCol',["totalEgresoCompensado"]); 
							 $j_6(this).jqGrid('hideCol',["status_pagada"]); 
							 $j_6(this).jqGrid('hideCol',["status_compensada"]);
						<?}else{?>
							$j_6(this).jqGrid('hideCol',["status_pago"]);
						<?}?>*/
						/*var total = data.records;
						for(var i=0; i < total; i++){
							if(data.rows[i].cell[2] == "Administrador"){
							$j_6("#" + data.rows[i].id).find("td").css("background-color", "#D6D6D6");
							}
						}*/
								
								
				}
			});
			$j_6("#<?=$_POST['idGrid']?>").jqGrid('navGrid','#<?=$_POST['idPagina']?>',
			{edit:<?=$_POST['edit']?>,add:<?=$_POST['add']?>,del:<?=$_POST['del']?>,excel:true,exceltext:'Excel'},
			{},
			{},
			{},
			{multipleSearch:true, multipleGroup:true, showQuery: true}
			);
			
			/*$j_6("#<?=$_POST['idGrid']?>").jqGrid('navGrid','#<?=$_POST['idPagina']?>',{edit:false,add:false,del:false});
			$j_6("#<?=$_POST['idGrid']?>").jqGrid('gridResize',{minWidth:350,maxWidth:1024,minHeight:80, maxHeight:768});
			*/
			$j_6('#<?=$_POST['idGrid']?>').jqGrid('navButtonAdd','#<?=$_POST['idPagina']?>',{caption:'',title:'Exportar a Excel',onClickButton : function(e){
			exportExcel('<?=$_POST['idGrid']?>','<?=$_POST['headerNames']?>','<?=$_POST['ignoreColNamesExcel']?>','<?=$_POST['sheetName']?>','<?=$_POST['formName']?>','1');
			}});
			
			$j_6('#<?=$_POST['idGrid']?>').jqGrid('filterToolbar',{defaultSearch:'cn',stringResult:true});
			$j_6("#<?=$_POST['idGrid']?>").jqGrid('setFrozenColumns');
			<?if(strtoupper($_POST['add']) == "TRUE"){?>
				$j_6("#add_<?=$_POST['idGrid']?>").click(function(){
					ventanaModal('[<?=$_POST['fileForm']?>,<?=$_POST['widthForm']?>,<?=$_POST['heightForm']?>]','<?=$_POST['optionsForm']?>','','<?=$_POST['captionForm']?>','add','<?=$_POST['tabla']?>','','','<?=$_POST['reloadGridAfterSave']?>');
				});
			<?}?>
			<?if(strtoupper($_POST['edit']) == "TRUE"){?>
				$j_6("#edit_<?=$_POST['idGrid']?>").click(function(){
					var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
					var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);
					
					var fieldKeys='<?=$fieldKeys?>';
					var valKeys="";
					
					<?
						$keys = explode("|",$fieldKeys);
						for($i = 0; $i < count($keys); $i++){
					?>
					if(valKeys == "")
						valKeys=ret.<?=$keys[$i]?>;
					else
						valKeys=valKeys+"|"+ret.<?=$keys[$i]?>;
					<?}?>
					ventanaModal('[<?=$_POST['fileForm']?>,<?=$_POST['widthForm']?>,<?=$_POST['heightForm']?>]','<?=$_REQUEST['optionsForm']?>','','<?=$_REQUEST['captionForm']?>','edit','<?=$_POST['tabla']?>',valKeys,fieldKeys,'<?=$_POST['reloadGridAfterSave']?>');
				});
			<?}?>
			<?if(strtoupper($_POST['del']) == "TRUE"){?>
				$j_6("#del_<?=$_POST['idGrid']?>").click(function(){
					var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
					var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);
					
					var fieldKeys='<?=$fieldKeys?>';
					var valKeys="";
					<?
						$keys = explode("|",$fieldKeys);
						for($i = 0; $i < count($keys); $i++){
					?>
					if(valKeys == "")
						valKeys=ret.<?=$keys[$i]?>;
					else
						valKeys=valKeys+"|"+ret.<?=$keys[$i]?>;
					<?}?>
					ventanaModal('[<?=$_POST['delForm']?>,<?=$_POST['widthConfirm']?>,<?=$_POST['heightConfirm']?>]','','','<table><tr><td valign="center"><img src="Imagenes/Help.png" width="35" height="32"/></td><td width="95%" valign="center"><?=$_REQUEST['captionConfirm']?></td></tr></table>','del','<?=$_POST['tabla']?>',valKeys,fieldKeys,'<?=$_POST['reloadGridAfterSave']?>');
				});
			<?}?>
			$j_6('#<?=$_POST['idGrid']?>').jqGrid('navButtonAdd','#<?=$_POST['idPagina']?>',{caption:'<img src=\'Imagenes/relacion.png\' width=\'16\' height=\'16\'>',buttonicon: 'ui-icon-custom',title:'Ver Documentos Relacionados',onClickButton : function(e){
			
				var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
				var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);
				if(ret.uuid == null || ret.uuid == "undefined"){
					$j_6.messager.alert('Error.','Debe de seleccionar un documento.','error');
					return;
				}
				loadDiv("extras/cfdirelacionados.php","cuerpoPopup",ret.uuid);
				
				open_w();

			}});
			$j_6('#<?=$_POST['idGrid']?>').jqGrid('navButtonAdd','#<?=$_POST['idPagina']?>',{caption:'<img src=\'Imagenes/relacionXLS.png\' width=\'16\' height=\'16\'>',buttonicon: 'ui-icon-custom',title:'Exportar documentos relacionados',onClickButton : function(e){
			
				var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
				var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);
				
				var ultimaPagina = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','lastpage');
				var registrosXPagina = jQuery('.ui-pg-selbox').val();
				var pagina1 = 0;
				var pagina2 = 0;
				if(ultimaPagina > 50){
					$j_6.messager.prompt("Indique la pagina inicial para la exportación","Página",function(a){
						if(isNaN(a) == true || a <= 0){
							$j_6.messager.alert("Error.","El número de pagina debe de ser numérico y mayor a cero.","error");
							return false;
						}else{
							pagina1 = a;
							$j_6.messager.prompt("Indique la pagina final para la exportación","Página",function(n){
								if(isNaN(n) == true || n <= 0){
									$j_6.messager.alert("Error.","El número de pagina debe de ser numérico y mayor a cero.","error");
									return false;
								}else{
									if(pagina1 > n){
										$j_6.messager.alert("Error.","La página inicial debe de ser menor o igual a la página final.","error");
										return false;
									}
									if((pagina2-pagina1) >= 50){
										$j_6.messager.alert("Error.","Solo se pueden exportar de 50 en 50 paginas.","error");
										return false;
									}
									pagina2 = n;
									alert(registrosXPagina);
									window.open("excel/excelPagosREP.php?registrosXPagina="+registrosXPagina+"&pagina1="+pagina1+"&pagina2="+pagina2);
								}
							});
						}
					});
				}else{
					window.open("excel/excelPagosREP.php?registrosXPagina="+registrosXPagina+"&pagina1=1&pagina2="+ultimaPagina);
				}
			}});
			<?if($_SESSION["esAdmin_p_prov_absa"] == "SI" || (strtoupper($_SESSION["PROVISION_DE_PAGOS"]) == "SI" && $_SESSION['sesion_mod_desbloquea_provision_pago'] == "1" && $_SESSION['todo_RFC_p_prov_absa'] == "SI")){?>
				$j_6('#<?=$_POST['idGrid']?>').jqGrid('navButtonAdd','#<?=$_POST['idPagina']?>',{caption:'<img src=\'Imagenes/unlock128.png\' width=\'16\' height=\'16\'>',buttonicon: 'ui-icon-custom',title:'Desbloquear para cancelar.',onClickButton : function(e){
				
					var idRow = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getGridParam','selrow');
					var ret = jQuery("#<?=$_POST['idGrid']?>").jqGrid('getRowData',idRow);
					
					if(ret.uuid == "" || ret.uuid == undefined || ret.uuid == "undefined"){
						$j_6.messager.alert("Error","Debe de seleccionar un documento.","error");
						return false;
					}
					if(ret.tipodocto != "I CFDI 3.3"){
						$j_6.messager.alert("Error","Debe de seleccionar un documento de ingreso.","error");
						return false;
					}
					desbloquiar(ret.id,ret.uuid);
				}});
			<?}?>
			/*$j_6(window).bind('resize', function(){
                                
				//set to 0 so grid does not continually grow
				$j_6('#<?=$_POST['idGrid']?>').setGridWidth(0);
				//resize to our container's width
				$j_6('#<?=$_POST['idGrid']?>').setGridWidth($j_6('#cuerpo').width());
			}).trigger('resize');*/
	
			$j_6('#loading').hide();
			//$j_6("body select").msDropDown();
			
	});
	function getNumPages(){
		return $j_6('#<?=$_POST['idGrid']?>').getGridParam('lastpage');
	}
	function getRowNum(){
		return $j_6('#<?=$_POST['idGrid']?>').getGridParam('rowNum');
	}
	function getTotalRecords(){
		return $j_6('#<?=$_POST['idGrid']?>').jqGrid('getGridParam', 'records');
	}
	function exportaZip(){
		$j_6('#form_progressbar').load("formularios/form_exportaXLS.php",{});
	}
	
	function ver_validaciones(rfcemisor,serie,folio,uuid,tipodocto,fecharecibido){
		loadDiv("extras/preview_validaciones.php","cuerpoPopup",rfcemisor,serie,folio,uuid,tipodocto,fecharecibido);
		open_w();
	}
	
	function ver_comentario_validacion(rfcemisor,tipodocto,serie,folio,uuid,numeroEvento){
		loadDiv("extras/trae_comentario_evento.php","cuerpoPopup",rfcemisor,tipodocto,serie,folio,uuid,numeroEvento);
		open_w();
	}
	
	function desbloquiar(id,uuid){
	
	$j_6.messager.confirm("Desbloqueo de cancelación de documento.","¿Esta seguro de desbloquear el documento para su cancelación?",function(r){
		if(r){
			$j_6.post("formularios/extras/desbloquiarDocumentoCancelacion.php",{id: id, uuid: uuid},function(data){
				$j_6("#result").html(data);
			});
		}else{
			return false;
		}
	});
	
}
	
	function iniciaDescargaXLS(formName){
		$j_6.messager.prompt('Exportar a Excel', 'Indique el nombre del archivo', function(file_name){
					if(!file_name){
						return false;
					}
				
		if(file_name == null || file_name == ""){
			file_name="Reporte";
		}
		
		document.forms[formName].file_name.value=file_name;
		document.forms[formName].method='POST';
		
		/*if(navigator.appVersion.toLowerCase().indexOf("safari") != -1 && navigator.appVersion.toLowerCase().indexOf("chrome") == -1)
			document.forms[formName].action='excel/excelSafari.php';
		else*/

			document.forms[formName].action='excel/excelRecibeDoce.php';
		
			document.forms[formName].target='_blank';
			
		document.forms[formName].submit();
		});
}

function imprSelec(muestra)
{
	var libs = '<ht'+'ml><he'+'ad><scr'+'ipt language="javascript" src="libs/js/jquery.min1.6.2.js"></scr'+'ipt>'+
				'<scr'+'ipt>'+
				"var $j_6 = $.noConflict();"+
				'</scr'+'ipt>'+
				'<link rel="stylesheet" type="text/css" href="libs/jqueryEasy_1.2.5/themes/default/easyui.css">'+
				'<link rel="stylesheet" type="text/css" href="libs/jqueryEasy_1.2.5/themes/icon.css">'+
				'<scr'+'ipt type="text/javascript" src="libs/jqueryEasy_1.2.5/jquery.easyui.min.js"></scr'+'ipt></he'+'ad>'+
				'<sty'+'le>@media print {.imprimir {transform: scale(.80);margin-top:-50px;}}</sty'+'le>'+
				'<bo'+'dy class="imprimir">';
	
				
	var ficha=document.getElementById(muestra);var ventimp=window.open(' ','popimpr');ventimp.document.write(libs+ficha.innerHTML+"<scr"+"ipt>window.print();</scr"+"ipt></bo"+"dy></ht"+"ml>");ventimp.document.close();
}




	</script>
	<script src="grids/funcionesJQGrid.js" type="text/javascript"></script>
	<form name="<?=$_POST['formName']?>" id="<?=$_POST['formName']?>">
		<input type="hidden" name="csvBuffer" id="csvBuffer" value="" />
		<input type="hidden" name="file_name" id="file_name" value="" />
		<input type="hidden" name="header" id="header" value="" />
		<input type="hidden" name="sheetName" id="sheetName" value="" />
		<input type="hidden" name="ignorarCol" id="ignorarCol" value="" />
                <input type="hidden" name="noSesion" id="noSesion" value="" />
		</form>
		<!--<div id="myjqgridwrapper" style="width:100%;">-->
			<table id="<?=$_POST['idGrid']?>"></table>
			<div id="<?=$_POST['idPagina']?>"></div>
		<!--</div>-->
	<form name="form_exportaXLS" id="form_exportaXLS" target="_blank">
		<input type="hidden" name="file_name" />
		<input type="hidden" name="pagos" value="NO" />
		<input type="hidden" name="ignorarCol" value="<?=$_POST['ignoreColNamesExcel']?>"/>
	</form>
	
	<script type="text/javascript" src="libs/jqueryEasy_1.2.5/jquery.easyui.min.js"></script>
		
	<div id="windowVal" class="easyui-window" closed="true" title="Validaciones" iconCls="icon-save" style="width:800px;height:600px;padding:5px;">
		<div class="easyui-layout" fit="true">
			<div region="center" border="false" style="padding:10px;background:#fff;border:1px solid #ccc;">
				<div id="cuerpoPopup"></div>
			</div>
			<div region="south" border="false" style="text-align:right;padding:5px 0;">
				<a class="easyui-linkbutton" iconCls="icon-print" href="javascript:void(0)" onclick="imprSelec('cuerpoPopup')">Imprimir</a>
				<a class="easyui-linkbutton" iconCls="icon-cancel" href="javascript:void(0)" onclick="close_w()">Cancelar</a>
			</div>
		</div>
	</div>
	<div id="mm" class="easyui-menu" style="width:120px;">
		<div onclick="getFile('XML')"><font style="color: black">XML</font></div>
		<div onclick="getFile('PDF')"><font style="color: black">PDF</font></div>
		<div onclick="getFile('STD')"><font style="color: black">PDF Estandar</font></div>
		
	</div>