<?
/*
Autor: Lic. Carlos Alberto Vaca Coronado
Version: 3.2.2
Cambio: Se agregan 3 status extras para la verificacion SAT
*/

session_name('portal_absa');
session_start();

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;

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

$_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) or die(mysqli_num_rows($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']." 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_num_rows($conexion->CONEXION));

$responce = new stdclass();
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
//$responce->rows[0]['cell']=array($SQL);

if(file_exists("../config.ini")){
	$vlineas = file("../config.ini"); 

	foreach ($vlineas as $sLinea) {
		if(trim(strtoupper(substr($sLinea,0,15))) == "RUTA_RECIBIDOS=")
			$rutaArchivos=trim(substr($sLinea,15));
	}
}

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

				$year = substr($row['fecharecibido'],0,4);
				$mes = substr($row['fecharecibido'],5,2);
	
				 /*if(trim($row['status']) == "A"){
					$img="acceptHand.png";
					$title="Aceptado";
				}else if(trim($row['status']) == "W"){
					$title="Advertencia";
					$img="Attention.png";
				}else if(trim($row['status']) == "P"){
					$title="En Proceso";
					$img="watch.png";
				}else{
					$img="rejectHand.png";
					$title="Rechazado";
				}*/
				
				$queryStatus="select count(status) as no_status from detallerecibedoce where fecharecibido='".$row['fecharecibido']."' and 
				rfcemisor='".$row['rfcemisor']."' and
				tipodocto='".$row['tipodocto']."' and
				serie='".$row['serie']."' and
				folio='".$row['folio']."' and
				uuid='".$row['uuid']."' and status='R'";
				
				
				$rqueryStatus = mysqli_query($conexion->CONEXION,$queryStatus);
				
				
				$temp=mysqli_fetch_array($rqueryStatus,MYSQLI_ASSOC);
				
				if($temp['no_status'] > 0){
					$img="rejectHand.png";
					$title="Rechazado";
				}else{
					$queryStatus="select count(status) as no_status from detallerecibedoce where fecharecibido='".$row['fecharecibido']."' and 
					rfcemisor='".$row['rfcemisor']."' and
					tipodocto='".$row['tipodocto']."' and
					serie='".$row['serie']."' and
					folio='".$row['folio']."' and
					uuid='".$row['uuid']."' and status='V'";
					
					mysqli_free_result($rqueryStatus);
					$rqueryStatus = mysqli_query($conexion->CONEXION,$queryStatus);
					$temp=mysqli_fetch_array($rqueryStatus,MYSQLI_ASSOC);
					if($temp['no_status'] > 0){
						$title="Verificado en el SAT correctamente";
						$img="verificadoSAT.png";
					}else{
						$queryStatus="select count(status) as no_status from detallerecibedoce where fecharecibido='".$row['fecharecibido']."' and 
						rfcemisor='".$row['rfcemisor']."' and
						tipodocto='".$row['tipodocto']."' and
						serie='".$row['serie']."' and
						folio='".$row['folio']."' and
						uuid='".$row['uuid']."' and status='?'";
						
						mysqli_free_result($rqueryStatus);
						$rqueryStatus = mysqli_query($conexion->CONEXION,$queryStatus);
						$temp=mysqli_fetch_array($rqueryStatus,MYSQLI_ASSOC);
						if($temp['no_status'] > 0){
							$title="Verificado en el SAT incorrectamente";
							$img="about.png";
						}else{
							$queryStatus="select count(status) as no_status from detallerecibedoce where fecharecibido='".$row['fecharecibido']."' and 
							rfcemisor='".$row['rfcemisor']."' and
							tipodocto='".$row['tipodocto']."' and
							serie='".$row['serie']."' and
							folio='".$row['folio']."' and
							uuid='".$row['uuid']."' and status='W'";
							
							mysqli_free_result($rqueryStatus);
							$rqueryStatus = mysqli_query($conexion->CONEXION,$queryStatus);
							$temp=mysqli_fetch_array($rqueryStatus,MYSQLI_ASSOC);
							if($temp['no_status'] > 0){
								$title="Advertencia";
								$img="Attention.png";
							}else{
								$img="acceptHand.png";
								$title="Aceptado";
							}
						}
					}
				}
				
				
				
				$rutaXML=str_replace("\\","/",str_replace("<<RFCRECEPTOR>>",$_GET['rfcreceptor'],$rutaArchivos)).$year."/".$mes."/".$row['xml'];
				$rutaPDF=str_replace("\\","/",str_replace("<<RFCRECEPTOR>>",$_GET['rfcreceptor'],$rutaArchivos)).$year."/".$mes."/".$row['pdf'];
				
/*		$responce->rows[$i]['cell']=array($rutaXML);
				echo json_encode($responce);
				exit;*/
				
				if(is_file($rutaXML)){
					$iconoXML='<img src="grids/Imagenes/xml.png" width="11" height="15" style="cursor:pointer" title="Descargar XML" 
				onClick="descargar(\''.str_replace("&",";AMP;",$row['xml']).'\',\''.str_replace("&",";AMP;",str_replace('../','',$rutaXML)).'\',\''.$row['status'].'\'); return false;"/>';
				}else{
					$iconoXML='<img src="grids/Imagenes/nodisponible.jpg" width="11" height="15" title="XML no disponible"/>';
				}
				if(is_file($rutaPDF)){
					$iconoPDF='<img src="grids/Imagenes/pdf.png" width="11" height="15" style="cursor:pointer" title="Descargar PDF" 
				onClick="descargar(\''.str_replace("&",";AMP;",$row['pdf']).'\',\''.str_replace("&",";AMP;",str_replace('../','',$rutaPDF)).'\',\''.$row['status'].'\'); return false;"/>';
				}else{
					$iconoPDF='<img src="grids/Imagenes/nodisponible.jpg" width="11" height="15" style="cursor:pointer" title="PDF no disponible"/>';
				}
				
				if($_SESSION["archivos_recibidos"] == "SI"){
					$status = "<center>$iconoXML $iconoPDF <img title='$title' src='grids/Imagenes/$img' width='17' height='16'/></center>";
				}else{
					$status = "<center><img title='$title' src='grids/Imagenes/$img' width='17' height='16'/></center>";
				}
				
				$fecha=substr($row['fecharecibido'],0,10)."--".substr(str_replace(":","A",$row['fecharecibido']),11);
				
	$responce->rows[$i]['cell']=array($status,$fecha,$row['fecharecibido'],$row['rfcemisor'],$row['tipodocto'],$row['serie'],$row['folio'],$row['uuid']);
	
	$i++;
}

echo json_encode($responce);

?>
