<?
session_name('portal_absa');
session_start();
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 d.".$rule->field." like '%".$rule->data."%'";
		}
	}
}

$auxQuery = str_replace("<<FILTROENGRID>>",$busqueda,$_SESSION["query_doce"]);

// $responce->rows[0]['cell']=array('','','',$auxQuery);
// echo json_encode($responce);
// exit;
$auxCountQuery = str_replace("<<FILTROENGRID>>",$busqueda,$_SESSION["query_count_doce"]);

$result = mysqli_query($conexion->CONEXION,$auxCountQuery) 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;

$auxQuery = $auxQuery." order by $sidx $sord LIMIT $start , $limit";

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

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

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

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

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'/>";
	}
	
	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($etiqueta = getINI("MOD_NP_STATUS_".$row['statusNP']."=","../config.ini")){
		if($etiqueta != ""){
			$row['statusNP']='<img src="Imagenes/circulo_'.$row['statusNP'].'.png" width="16" height="16"/>';
			$statusPagada = $etiqueta;
		}
	}

	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);

?>