<?
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;}
require "../conexion/conectar.php";
require "../libs/funciones.php";

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

$page = $_GET['page']; // get the requested page
$limit = (integer) $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction

if(!$sidx) $sidx =1;

$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
if($totalrows) {
	//$limit = (integer) $totalrows;
}

$filtros="";
$busqueda="";
if(isset($_REQUEST['filters'])){
	$filtros = json_decode($_REQUEST['filters']);
	foreach($filtros->rules as $rule){ 
		if(trim($rule->data) != ""){
			$busqueda.=" AND ".$rule->field." like '%".$rule->data."%'";
		}
	}
}

$_SESSION['query'] = str_replace("\\","",$_SESSION['query']);
$SQL_1="select COUNT(*) as count from (".$_SESSION['query']." $busqueda) as temp";
//$SQL_1="select COUNT(*) as count from (select rfcemisor,tipodocto,serie,folio from doce) as temp";

$result = mysqli_query($conexion->CONEXION,$SQL_1) or die(mysqli_error($conexion->CONEXION));

$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$count = (integer) $row['count'];

if( $count >0 ) {
	$total_pages = (integer) ceil($count/$limit);
} else {
	$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
if ($limit<0) $limit = 0;
$start = (integer) $limit*$page - $limit;
if ($start<0) $start = 0;

$SQL = $_SESSION['query']." $busqueda order by $sidx $sord LIMIT $start , $limit";
//$SQL = "select rfcemisor,tipodocto,serie,folio from doce order by $sidx $sord LIMIT $start , $limit";

$result = mysqli_query($conexion->CONEXION, $SQL ) or die(mysqli_error($conexion->CONEXION));

$responce=new stdclass();
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;

/*$responce->rows[0]['cell']=array($SQL);
echo json_encode($responce);exit;*/

$valorIni = getINI("CAMPOSD_ORDEN=","../config.ini");

$arrConf = explode(",",$valorIni);

$notificar_fecha_inicio="";
//Extraigo Configuracion General de notificaciones
$qConfig = "select notificar_dias,notificar_despuesde,notificar_repetirdias,notificar_correos,notificar_asunto,notificar_correos_cc,notificar_fecha_inicio from configpagina where rfc_receptor='".$_SESSION['RFC_RECEPTOR_PORTAL']."' limit 1";
if($rConfig = mysqli_query($conexion->CONEXION,$qConfig)){	
	if(mysqli_num_rows($rConfig) > 0){
		$tConfig = mysqli_fetch_assoc($rConfig);
		$notificar_fecha_inicio = $tConfig["notificar_fecha_inicio"];
	}
}

while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
	$arrTemp = array();		
	if($row['usarRutaDistribuida'] == "SI"){
		$year = substr($row['fecha'],0,4);
		$mes = substr($row['fecha'],5,2);
		$row['xml'].=$year."/".$mes."/".$row['archXML'];
		$row['pdf'].=$year."/".$mes."/".$row['archPDF'];
	}else{
		$row['xml'].=$row['archXML'];
		$row['pdf'].=$row['archPDF'];
	}
	
	$ArchXML = trim($row['archXML']);
	$ArchPDF = trim($row['archPDF']);

	$rutaXML = trim(str_replace('\\','/',$row['xml']));
	$rutaPDF = trim(str_replace('\\','/',$row['pdf']));
	
	$ban=0;
	
	if(is_file($rutaXML)){
		$icoXML="<img src='grids/Imagenes/xml.png' width='11' height='15' style='cursor:pointer' title='Descargar XML' 
	onClick='javascript: descargar(\"$ArchXML\",\"".str_replace('../','',$rutaXML)."\")'/>";
	
	//VISTA ARCHIVOS BELL FLAVORS
	/*$icoXML="<img src='grids/Imagenes/xml.png' width='11' height='15' style='cursor:pointer' title='Descargar XML' 
	onClick='window.open(\"".str_replace('../','',$rutaXML)."\")'/>";*/
	
		$ban++;
	}else{
		$icoXML="<img src='Imagenes/nodisponible.jpg' width='11' height='15' title='XML no disponible'/>";
	}
	
	if(is_file($rutaPDF)){
		if($row["alta_manual"] == 0){
				$icoPDF="<img src='grids/Imagenes/pdf.png' width='11' height='15' style='cursor:pointer' title='Descargar PDF' 
			onClick='javascript: descargar(\"$ArchPDF\",\"".str_replace('../','',$rutaPDF)."\")'/>";
			
			//VISTA ARCHIVOS BELL FLAVORS
			/*$icoPDF="<img src='grids/Imagenes/pdf.png' width='11' height='15' style='cursor:pointer' title='Descargar PDF' 
			onClick='window.open(\"".str_replace('../','',$rutaPDF)."\")'/>";*/
		}else{
					$icoPDF='<img src="Imagenes/attach.png" width="11" height="15" style="cursor:pointer" title="Descargar PDF"
			onClick="javascript: descargar(\''.$ArchPDF.'\',\''.str_replace('../','',$rutaPDF).'\')"/>';
		}
	
		$ban++;
	}else{
		$icoPDF="<img src='Imagenes/nodisponible.jpg' width='11' height='15' title='PDF no disponible'/>";
	}
	
	$row["statusNP"] = "<center><img src='Imagenes/circulo_".$row["statusNP"].".png' width='16' height='16'></center>";
	
	if($_SESSION['esAdmin'] == "SI"){
		if($row['mostrarweb'] == "SI"){
			$iconoShowHide="table_row_insert.png";
			$titulo="Ocultar";
			$mostrar="NO";
		}else{
			$iconoShowHide="table_row_delete.png";
			$titulo="Mostrar";
			$mostrar="SI";
		}
		$icoShow="<img style='cursor: pointer' id='".$titulo."_".$row['rfcemisor'].$row['tipodocto'].$row['serie'].$row['folio']."' title='$titulo' onClick='showHide(\"".$row['rfcemisor']."\",\"".$row['tipodocto']."\",\"".$row['serie']."\",\"".$row['folio']."\",\"".$mostrar."\",this.id);' src='Imagenes/$iconoShowHide' width='11' height='15'/>";
	}
	$icoMail="";
	if($ban==2){
		$icoMail='<img style="cursor: pointer" id="mail" title="Enviar por correo" onClick="ventanaModal2(\'[formularios/form_correo.php,450,150]\',\'\',\''.str_replace('../','',$rutaPDF).'\',\''.str_replace('../','',$rutaXML).'\',\''.$ArchXML.'\')" src="Imagenes/E-Mail.png" width="16" height="14" />';
	}
	
	$row["xml"]= "<center><pre> $icoXML $icoPDF ".$icoMail." $icoShow</pre></center>";
	if($notificar_fecha_inicio != ""){
		if( str_replace(":","",str_replace(" ","",str_replace("-","",$row["fecha"]))) < str_replace(":","",str_replace(" ","",str_replace("-","",$notificar_fecha_inicio))) ){
			$row["statusNP"]="";
		}
	}
	array_push($arrTemp,$row["xml"]);
	
		if(trim($row["status"]) == "1")
			$img="accept.png";
		else
			$img="cancel.png";
		
		$row["status"] = "<center><img src='grids/Imagenes/$img'/></center>";
	
	for($j = 0; $j < count($arrConf); $j++){
		array_push($arrTemp,$row[$arrConf[$j]]);
	}
	
	$responce->rows[$i]['cell']=$arrTemp;
	$i++;
}
echo json_encode($responce);

?>