<?
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;
}
include "../../conexion/conectar.php";
require "../../libs/funciones.php";
require "../../MandaCorreos/FuncionSendMail.php";

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

$rfc=$_REQUEST['id'];
$tipodocto=$_REQUEST['id2'];
$serie=$_REQUEST['id3'];
$folio=$_REQUEST['id4'];
$uuid=$_REQUEST['id5'];
$correo=$_REQUEST['correo'];

$completaQuery="";

if(trim($_REQUEST['id3']) != "")
	$completaQuery.=" and serie='$serie'";
if(trim($_REQUEST['id4']) != "")
	$completaQuery.=" and folio='$folio'";
if(trim($_REQUEST['id5']) != "")
	$completaQuery.=" and uuid='$uuid'";

$query="select id,concepto_pago,fecha_pago,referencia,monto from pago_doc_detalle where rfcemisor='$rfc' and tipodocto='$tipodocto' $completaQuery";
$result = mysqli_query($conexion->CONEXION,$query);

$query="select id,total from recibedoce where rfcemisor='$rfc' and tipodocto='$tipodocto' and serie='$serie' and folio='$folio' and uuid='$uuid' limit 1";
$rDoc = mysqli_query($conexion->CONEXION,$query);
if(mysqli_num_rows($rDoc) > 0){
	$tDoc = mysqli_fetch_array($rDoc,MYSQLI_ASSOC);
}


$body='
<table>
	<tr>
		<th align="left">RFC:</th>
		<td>'.$rfc.'</td>
		<th>Serie:</th>
		<td>'.$serie.'</td>
		<th>Folio:</th>
		<td>'.$folio.'</td>
	</tr>
	<tr>
		<th align="left">UUID:</th>
		<td colspan="3">'.$uuid.'</td>
		<th>No. Interno:</th>
		<td>'.$tDoc['id'].'</td>
	</tr>
	<tr>
		<th align="left">Total factura:</th>
		<td colspan="5">'.$tDoc['total'].'</td>
	</tr>
</table>';


$body.='<table class="td_general">
	<tr style="background-color: #999999">
		<th width="150">Referencia</th>
		<th width="200">Concepto</th>
		<th width="150">Fecha de pago</th>
		<th width="150">Monto</th>
	</tr>';


if(mysqli_num_rows($result) > 0){
	$total=0;
	while($temp=mysqli_fetch_array($result,MYSQLI_ASSOC)){
		$total+=$temp['monto'];
		$body.='
	<tr>
		<td>'.$temp['referencia'].'</td>
		<td>'.$temp['concepto_pago'].'</td>
		<td>'.$temp['fecha_pago'].'</td>
		<td align="right">'.$temp['monto'].'</td>
	</tr>';
	}
}


	$body.='<tr>
		<td style="border-top: 1px solid; padding: 10px 10px 10px 10px" align="right" colspan="3">Total</td>
		<th style="border-top: 1px solid; padding: 10px 10px 10px 10px" align="right" >'.$total.'</th>
	</tr>
</table>';
$TIPO_CORREO = getINI("FUNCION_ENVIA_CORREO=","../../config.ini");
$FROM = getINI("FROM_CORREO=","../../config.ini");
if(strtoupper(trim($TIPO_CORREO)) == "MAIL"){
	if(mail($correo,"Acuse de pago, factura: ".$rfc." - ".$serie." - ".$folio." - ".$uuid." - ".$tDoc['id'],$body,'From: '.$FROM)){
		?><script>$j_6.messager.alert("Notificacion.","Correo enviado.","info");</script><?
	}
}else{
	if(SendMail($correo,$correo,"Acuse de pago, factura: ".$rfc." - ".$serie." - ".$folio." - ".$uuid." - ".$tDoc['id'],$body,'','','')){
		?><script>$j_6.messager.alert("Notificacion.","Correo enviado.","info");</script><?
	}
}


?>