<?
session_name('sesiones_expoleofinos');
session_start();

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

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

$result = mysql_query($SQL_1) or die(mysql_error());

$row = mysql_fetch_array($result,MYSQL_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_doce']." order by $sidx $sord LIMIT $start , $limit";
//$SQL = "select rfcemisor,tipodocto,serie,folio from doce order by $sidx $sord LIMIT $start , $limit";

$result = mysql_query( $SQL ) or die(mysql_error());



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

$arrRutas = configDoctos(getIDEmisor());



desconectar();


while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
				
				$usarRutaDistribuida=$arrRutas[$row['rfcemisor']][$row['tipodocto']]['usarRutaDistribuida'];
				$rutaXML=$arrRutas[$row['rfcemisor']][$row['tipodocto']]['rutaXML_P'];
				$rutaPDF=$arrRutas[$row['rfcemisor']][$row['tipodocto']]['rutaPDF_P'];
				
                
                
				if($usarRutaDistribuida == "SI"){
					$year = substr($row['fecha'],0,4);
					$mes = substr($row['fecha'],5,2);
					$rutaXML.=$year."/".$mes."/".$row['archXML'];
					$rutaPDF.=$year."/".$mes."/".$row['archPDF'];
				}else{
					$rutaXML.=$row['archXML'];
					$rutaPDF.=$row['archPDF'];
				}
				
				$rutaXML = trim(str_replace('\\','/',$rutaXML));
				$rutaPDF = trim(str_replace('\\','/',$rutaPDF));
				
				$ArchXML=$row['archXML'];
				$ArchPDF=$row['archPDF'];
				
				$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)."\",\"".str_replace("0","C",$row['status'])."\")'/>";
				
				//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/Help.png' width='11' height='15' title='XML no disponible'/>";
				}
				
				if(is_file($rutaPDF)){
					$icoPDF="<img src='grids/Imagenes/pdf.png' width='11' height='15' style='cursor:pointer' title='Descargar PDF' 
				onClick='javascript: descargar(\"$ArchPDF\",\"".str_replace('../','',$rutaPDF)."\",\"".str_replace("0","C",$row['status'])."\")'/>";
				
				//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)."\")'/>";*/
				
					$ban++;
				}else{
					$icoPDF="<img src='Imagenes/Help.png' 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,550,200]\',\'\',\''.str_replace('../','',$rutaPDF).'\',\''.str_replace('../','',$rutaXML).'\',\''.$ArchXML.'\')" src="Imagenes/E-Mail.png" width="16" height="14" />';
				}

				if(trim($row['status']) == "1")
					$img="accept.png";
				else
					$img="cancel.png";
				
				$status = "<center><img src='grids/Imagenes/$img'/></center>";
				
				$iconoExp='<img style="cursor:pointer" src="Imagenes/Docs.png" width="16" height="16" onClick="buscaExpedientes(\''.$row['rfcemisor'].'\',\''.$row['tipodocto'].'\',\''.$row['serie'].'\',\''.$row['folio'].'\',\''.$row['uuid'].'\')"/>';
				
			
	$responce->rows[$i]['cell']=array("<center><pre> $icoXML $icoPDF $icoMail $iconoExp $icoShow</pre></center>",$row['rfcemisor'],$row['tipodocto'],$row['serie'],$row['folio'],$status,$row['fecha'],$row['rfcreceptor'],$row['nombrerec'],$row['uuid'],$row['iva'],$row['total']);
	$i++;
}
echo json_encode($responce);


function configDoctos($arr){
/*$arrR=array();

	foreach($arr as $rfc => $valor){*/
        $query="";
        if(trim($_SESSION['RFC_RECEPTOR_PORTAL']) != "" && trim($_SESSION['todo_RFC_p_prov_absa']) != "SI"){
            $query="select url_local_pdf as rutaPDF_P,url_local_xml as rutaXML_P,rfc,usarRutaDistribuida,tipodocto from configtdoctosexp 
            where rfc='".$_SESSION['RFC_RECEPTOR_PORTAL']."'";
            $result = mysql_query($query);
        }else{
            $query="select url_local_pdf as rutaPDF_P,url_local_xml as rutaXML_P,rfc,usarRutaDistribuida,tipodocto from configtdoctosexp";
            $result = mysql_query($query);
        }
		if(mysql_num_rows($result) > 0){
			while($tmp = mysql_fetch_array($result)){
				$arrR[$tmp['rfc']][$tmp['tipodocto']]['rutaXML_P']=$tmp['rutaXML_P'];
				$arrR[$tmp['rfc']][$tmp['tipodocto']]['rutaPDF_P']=$tmp['rutaPDF_P'];
				$arrR[$tmp['rfc']][$tmp['tipodocto']]['usarRutaDistribuida']=$tmp['usarRutaDistribuida'];
                
                //$arrR[$rfc][$tmp['tipodocto']]['usarRutaDistribuida']="SI";
			}
		}
	//}
    
    
	
	return $arrR;
	
}

function getIDEmisor(){
$arr=array();
if($_SESSION['esAdmin_p_prov_absa'] == "SI"){
	$result = mysql_query("select rfc,idemisor from emisor");
}else{
	$result = mysql_query("select rfc,idemisor from emisor where rfc='".$_SESSION['RFC_RECEPTOR_PORTAL']."'");
}
if(mysql_num_rows($result) > 0){
	while($tmp = mysql_fetch_array($result)){
		$arr[$tmp['rfc']]=$tmp['idemisor'];
	}
}	
	return $arr;

}

?>