<?
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();
if(!isset($_REQUEST["uuidIngreso"])){
	?><script>$j_6.messager.alert("Error","Faltan parametros.","error");</script><?exit;
}
$uuidIngreso=$_REQUEST["uuidIngreso"];
$uuidEgreso=$_REQUEST["uuidEgreso"];

if($_SESSION['esAdmin_p_prov_absa'] != "SI"){
	?><script>$j_6.messager.alert("Error","No tiene permisos de eliminar la compensaci&oacute;n.","error");</script><?exit;
}
$tC = validaCompensacionGenerada($uuidEgreso,$uuidIngreso);

if($tC == 0){
	?><script>$j_6.messager.alert("Aviso","A&uacute;n no se ha compensado.","error");</script><?exit;
}
if($tC["automatico"] == "1"){
	?><script>$j_6.messager.alert("Error","No es posible deshacer una compensaci&oacute;n generada automaticamente.","error");</script><?exit;
}

$qDelete="delete from recibedoce_compensacion where uuid_padre='$uuidIngreso' and uuid_hijo='$uuidEgreso' and automatico='0'";
if(mysqli_query($conexion->CONEXION,$qDelete)){
	setBitacora($conexion, $_SESSION['usuario_p_prov_absa'], $_SESSION['rfc_cliente_p_prov_absa'], "DELETE", "tabla recibedoce_compensacion: quita compensacion.", $qDelete);
	?><script>$j_6.messager.alert("Informacion.","Se ha logrado quitar la compensaci&oacute;n.","info");</script><?exit;
}else{
	?><script>$j_6.messager.alert("Error.","Ocurrio un error al quitar la compensaci&oacute;n.","error");</script><?exit;
}


function validaCompensacionGenerada($uuidEgreso,$uuidIngreso){
	global $conexion;
	$q="select automatico from recibedoce_compensacion where uuid_padre='$uuidIngreso' and uuid_hijo='$uuidEgreso'";
	if(!$r = mysqli_query($conexion->CONEXION,$q)){
		?><script>$j_6.messager.alert("Error.","Ocurrio un error al consultar las compensaciones del egreso. Intente nuevamente.","error");</script><?exit;
	}
	if(mysqli_num_rows($r) > 0){
		$tmp = mysqli_fetch_assoc($r);
		return $tmp;
	}
	return 0;
}

?>