<?
/*
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;}

require "../conexion/conectar.php";

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

$page = $_GET['page']; // get the requested page
$limit = $_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;
$_SESSION['query'] = str_replace("\\","",$_SESSION['query']);
$SQL_1="select COUNT(*) as count from (".$_SESSION['query'].") 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);

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

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

$SQL = $_SESSION['query']." 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 );

$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;


$ocultos = explode(",",$_GET['ocultos']);
for($j = 0; $j < count($ocultos); $j++){
	$ocultos[$j] = trim($ocultos[$j]);
}
while ($property = mysqli_fetch_field($result)) {
	$columnas[$j]=$property->name;
}

/*$responce->rows[0]['id']='rfcemisor';
$responce->rows[0]['cell']=array("",$SQL,"");*/

while($row = mysqli_fetch_array($result,MYSQL_ASSOC)){

    //$responce->rows[$i]['id']=$row[$_GET['key']];
			
			$nombre=str_replace('.xml','',$row['archXML']);
			
			if($row['usarRutaDistribuida'] == "SI"){
								$year = substr($row[5],0,4);
								$mes = substr($row[5],5,2);
								
				$ArchPDF = str_replace('\\','/',$row['rutaPDF_P']).$year."/".$mes."/".$row['archPDF'];
				$ArchXML = str_replace('\\','/',$row['rutaXML_P']).$year."/".$mes."/".$row['archXML'];
			}else{
				$ArchPDF = str_replace('\\','/',$row['rutaPDF_P']).$row['archPDF'];
				$ArchXML = str_replace('\\','/',$row['rutaXML_P']).$row['archXML'];
			}
			
			$icono='<center><img src="Imagenes/Eraser.png" width="11" height="15" onclick="if(confirm(\'Esta seguro de eliminar fisicamente el documento: '.$nombre.'\')){borrar(\'borra.php\',\''.$ArchPDF.'\',\''.$ArchXML.'\',\''.$nombre.'\'); return false;}" /></center>';

				if(trim($row['status']) == "1")
					$img="accept.png";
				else
					$img="cancel.png";
				
				$status = "<center><img src='grids/Imagenes/$img'/></center>";
			
	$responce->rows[$i]['cell']=array($icono,$row['rfcemisor'],$row['tipodocto'],$row['serie'],$row['folio'],$row['fecha'],$row['rfcreceptor'],$row['nombrerec'],$row['iva'],$row['total'],$status);
	$i++;
}
echo json_encode($responce);

?>