<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte de Retenciones</title>
<link href="../../maincss.css" rel="stylesheet" type="text/css" />
<link href="../../printcss.css" rel="stylesheet" type="text/css" media="print" />

</head>
<?php
include "../../conn.php";
include "../../general.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$mostrar=$_POST["mostrar"];
$tipo_ret=$_POST["tipo_ret"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
$otra_moneda=$_POST["otra_moneda"];
$usuario = $_POST["usuario"];
$cod_contexto=$_POST["contexto"];

?>
<body>
<?php


//Inicio reporte de Retenciones

if($rep == 9 && $tipo == 1) { // if 2

//nombre otra moneda
$resultotra = mysqli_query($link, "SELECT fldName as otra_moneda
from
actCurrency
where
pkyCurrency = '$otra_moneda' and
sysEnabled = 1 and sysAuthorized = 1;");
		
while ($row = mysqli_fetch_array($resultotra)) {
$nom_otra_moneda = $row['otra_moneda'];
}
mysqli_free_result($resultotra);		

//nombre concepto cxp para importaciones
$resultimp = mysqli_query($link, "select a.codImportConcept as cod_imp, b.fldName as import
from 
pchPurchasesConfig a, payAccountsPayableConcept b 
where 
a.codImportConcept = b.pkyAccountsPayableConcept and 
a.sysEnabled = 1 and a.sysAuthorized = 1;");
		
while ($row = mysqli_fetch_array($resultimp)) {
$nom_concepto_imp = $row['import'];
}
mysqli_free_result($resultimp);		

//nombre de retencion
if($tipo_ret == 0) {
	$nom_retencion = 'Todos';
	$valor_retencion = ' ';
} else {
	$resultret = mysqli_query($link, "select pkyRetentionType as cod_ret, fldName as retencion, fldPercent as porcentaje, txtDescription  as descripcion
	from 
	txsRetentionType 
	where 
	pkyRetentionType = '$tipo_ret' and 
	sysEnabled = 1 and sysAuthorized = 1
	order by fldName;");
	while ($row = mysqli_fetch_array($resultret)) {
	$nom_retencion = $row['retencion'];
	}
	mysqli_free_result($resultret);
	$valor_retencion = ' a.codType = '.$tipo_ret. ' and';
}

if($mostrar == 0) {
	$mostrar_detalle = 'Si';
} else {
	$mostrar_detalle = 'No';
}

//contexto
if($cod_contexto == -1){ 
	$nom_contexto = 'Todos';
	$valor_contexto2 = ' ';
} else {
	$resultcontx = mysqli_query($link, "select pkyContext as cod_contexto, fldName as contexto, codCostCenter as cod_cc 
	from srvContext
	where 
	pkyContext = '$cod_contexto';");
		while ($row = mysqli_fetch_array($resultcontx)) {   
			$nom_contexto = $row['cod_contexto'].' - '.$row['contexto'];
		}
		mysqli_free_result($resultcontx);
	$valor_contexto2 = ' b.sysContext = '.$cod_contexto.' and ';	
}

//listado de retenciones
$result = mysqli_query($link, "select fin.cod_ret as cod_ret, fin.cod_cxp as cod_cxp, fin.doc_cxp as doc_cxp, fin.cod_tipo_ret as cod_tipo_ret, fin.nom_ret as nom_ret, fin.fecha as fecha, fin.concepto_cxp as concepto_cxp, fin.concepto_gasto as concepto_gasto, fin.moneda as moneda, fin.monto as monto, fin.tot_ret as tot_ret, fin.tasa as tasa, fin.monto * fin.tasa as monto_om, fin.tot_ret * fin.tasa as tot_ret_om, fin.tipo_reten as tipo_reten 
from
(select t.cod_ret as cod_ret, t.cod_cxp as cod_cxp, 
t.doc_cxp as doc_cxp, t.cod_tipo_ret as cod_tipo_ret, t.nom_ret as nom_ret, 
t.fecha as fecha, t.concepto_cxp as concepto_cxp, 
t.cod_moneda as cod_moneda, t.moneda as moneda, t.otra_moneda as otra_moneda, 
t.cod_tc as cod_tc, t.concepto_gasto as concepto_gasto, 
t.monto as monto, t.tot_ret as tot_ret, r.fldRate as tasa, t.tipo_reten as tipo_reten
from 
(select a.pkyRetentionDetail as cod_ret, a.codAccountsPayable as cod_cxp, 
a.codType as cod_tipo_ret, f.fldName as nom_ret, b.fldDate as fecha, 
b.fldDocumentNumber as doc_cxp, c.fldName as concepto_cxp, 
b.codCurrency as cod_moneda, g.fldName as moneda, '$otra_moneda' as otra_moneda, 
b.codExchangeRate as cod_tc, a.codAccountsPayableDetail as cod_cxp_det, 
e.fldName as concepto_gasto, d.fldAmount as monto, a.fldTotal as tot_ret, 0 as tipo_reten
from 
txsRetentionDetail a, payAccountsPayable b, payAccountsPayableConcept c, payAccountsPayableDetail d, payExpensesConcept e, txsRetentionType f, actCurrency g 
where 
a.codAccountsPayable = b.pkyAccountsPayable and 
b.codConcept = c.pkyAccountsPayableConcept and 
b.fldDate >= '$fechaini' and 
b.fldDate <= '$fechafin' and 
$valor_retencion 
$valor_contexto2 
a.codAccountsPayableDetail = d.pkyAccountsPayableDetail and 
d.codExpensesConcept = e.pkyExpensesConcept and 
a.codType = f.pkyRetentionType and 
b.codCurrency = g.pkyCurrency and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
   union all 
select a.pkyPurchaseRetentionDetail as cod_ret, a.codPayrollExpenses as cod_cxp, 
a.codType as cod_tipo_ret, f.fldName as nom_ret, b.fldDate as fecha, 
b.fldNumber as doc_cxp, '$nom_concepto_imp' as concepto_cxp, 
b.codCurrency as cod_moneda, g.fldName as moneda, '$otra_moneda' as otra_moneda, 
b.codExchangeRate as cod_tc, a.codPayrollExpensesDetail as cod_cxp_det, 
d.fldName as concepto_gasto, c.fldAmount as monto, a.fldTotal as tot_ret, 1 as tipo_reten
from 
txsPurchaseRetentionDetail a, pchPayrollExpenses b, pchPayrollExpensesDetail c, pchPurchaseConcept d, txsRetentionType f, actCurrency g 
where 
a.codPayrollExpenses = b.pkyPayrollExpenses and 
a.codPayrollExpensesDetail = c.pkyPayrollExpensesDetail and 
b.fldDate >= '$fechaini' and 
b.fldDate <= '$fechafin' and 
$valor_retencion
$valor_contexto2 
c.codPurchaseConcept = d.pkyPurchaseConcept and 
a.codType = f.pkyRetentionType and 
b.codCurrency = g.pkyCurrency and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t, 
actExchangeRateDetail r
where
t.cod_tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency

union all

select t.cod_ret as cod_ret, t.cod_cxp as cod_cxp, t.doc_cxp as doc_cxp, t.cod_tipo_ret as cod_tipo_ret, t.nom_ret as nom_ret, t.fecha as fecha, t.concepto_cxp as concepto_cxp, t.cod_moneda as cod_moneda, t.moneda as moneda, t.otra_moneda as otra_moneda, t.cod_tc as cod_tc, t.concepto_gasto as concepto_gasto, t.monto as monto, t.tot_ret as tot_ret, 1 as tasa, t.tipo_reten as tipo_reten
from 
(select a.pkyRetentionDetail as cod_ret, a.codAccountsPayable as cod_cxp, 
a.codType as cod_tipo_ret, f.fldName as nom_ret, b.fldDate as fecha, 
b.fldDocumentNumber as doc_cxp, c.fldName as concepto_cxp, 
b.codCurrency as cod_moneda, g.fldName as moneda, '$otra_moneda' as otra_moneda, 
b.codExchangeRate as cod_tc, a.codAccountsPayableDetail as cod_cxp_det, 
e.fldName as concepto_gasto, d.fldAmount as monto, a.fldTotal as tot_ret, 0 as tipo_reten
from 
txsRetentionDetail a, payAccountsPayable b, payAccountsPayableConcept c, payAccountsPayableDetail d, payExpensesConcept e, txsRetentionType f, actCurrency g 
where 
a.codAccountsPayable = b.pkyAccountsPayable and 
b.codConcept = c.pkyAccountsPayableConcept and 
b.fldDate >= '$fechaini' and 
b.fldDate <= '$fechafin' and 
$valor_retencion  
$valor_contexto2 
a.codAccountsPayableDetail = d.pkyAccountsPayableDetail and 
d.codExpensesConcept = e.pkyExpensesConcept and 
a.codType = f.pkyRetentionType and 
b.codCurrency = g.pkyCurrency and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
   union all 
select a.pkyPurchaseRetentionDetail as cod_ret, a.codPayrollExpenses as cod_cxp, 
a.codType as cod_tipo_ret, f.fldName as nom_ret, b.fldDate as fecha, 
b.fldNumber as doc_cxp, '$nom_concepto_imp' as concepto_cxp, 
b.codCurrency as cod_moneda, g.fldName as moneda, '$otra_moneda' as otra_moneda, 
b.codExchangeRate as cod_tc, a.codPayrollExpensesDetail as cod_cxp_det, 
d.fldName as concepto_gasto, c.fldAmount as monto, a.fldTotal as tot_ret, 1 as tipo_reten
from 
txsPurchaseRetentionDetail a, pchPayrollExpenses b, pchPayrollExpensesDetail c, pchPurchaseConcept d, txsRetentionType f, actCurrency g 
where 
a.codPayrollExpenses = b.pkyPayrollExpenses and 
a.codPayrollExpensesDetail = c.pkyPayrollExpensesDetail and 
b.fldDate >= '$fechaini' and 
b.fldDate <= '$fechafin' and 
$valor_retencion 
$valor_contexto2 
c.codPurchaseConcept = d.pkyPurchaseConcept and 
a.codType = f.pkyRetentionType and 
b.codCurrency = g.pkyCurrency and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) fin
order by fin.cod_tipo_ret, fin.fecha;");
		

//contar numero de filas por cada retencion (mismo query de arriba, cambia el select fin superior y la agrupacion inferior
$resultcont = mysqli_query($link, "select fin.nom_ret as tipo, count(fin.cod_tipo_ret) as nro 
from
(select t.cod_ret as cod_ret, t.cod_cxp as cod_cxp, 
t.doc_cxp as doc_cxp, t.cod_tipo_ret as cod_tipo_ret, t.nom_ret as nom_ret, 
t.fecha as fecha, t.concepto_cxp as concepto_cxp, 
t.cod_moneda as cod_moneda, t.otra_moneda as otra_moneda, 
t.cod_tc as cod_tc, t.concepto_gasto as concepto_gasto, 
t.monto as monto, t.tot_ret as tot_ret, r.fldRate as tasa
from 
(select a.pkyRetentionDetail as cod_ret, a.codAccountsPayable as cod_cxp, 
a.codType as cod_tipo_ret, f.fldName as nom_ret, b.fldDate as fecha, 
b.fldDocumentNumber as doc_cxp, c.fldName as concepto_cxp, 
b.codCurrency as cod_moneda, '$otra_moneda' as otra_moneda, 
b.codExchangeRate as cod_tc, a.codAccountsPayableDetail as cod_cxp_det, 
e.fldName as concepto_gasto, d.fldAmount as monto, a.fldTotal as tot_ret
from 
txsRetentionDetail a, payAccountsPayable b, payAccountsPayableConcept c, payAccountsPayableDetail d, payExpensesConcept e, txsRetentionType f 
where 
a.codAccountsPayable = b.pkyAccountsPayable and 
b.codConcept = c.pkyAccountsPayableConcept and 
b.fldDate >= '$fechaini' and 
b.fldDate <= '$fechafin' and 
$valor_retencion 
$valor_contexto2 
a.codAccountsPayableDetail = d.pkyAccountsPayableDetail and 
d.codExpensesConcept = e.pkyExpensesConcept and 
a.codType = f.pkyRetentionType and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
    union all 
select a.pkyPurchaseRetentionDetail as cod_ret, a.codPayrollExpenses as cod_cxp, 
a.codType as cod_tipo_ret, f.fldName as nom_ret, b.fldDate as fecha, 
b.fldNumber as doc_cxp, '$nom_concepto_imp' as concepto_cxp, 
b.codCurrency as cod_moneda, '$otra_moneda' as otra_moneda, 
b.codExchangeRate as cod_tc, a.codPayrollExpensesDetail as cod_cxp_det, 
d.fldName as concepto_gasto, c.fldAmount as monto, a.fldTotal as tot_ret
from 
txsPurchaseRetentionDetail a, pchPayrollExpenses b, pchPayrollExpensesDetail c, pchPurchaseConcept d, txsRetentionType f
where 
a.codPayrollExpenses = b.pkyPayrollExpenses and 
a.codPayrollExpensesDetail = c.pkyPayrollExpensesDetail and 
b.fldDate >= '$fechaini' and 
b.fldDate <= '$fechafin' and 
$valor_retencion 
$valor_contexto2 
c.codPurchaseConcept = d.pkyPurchaseConcept and 
a.codType = f.pkyRetentionType and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t, 
actExchangeRateDetail r
where
t.cod_tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency

union all

select t.cod_ret as cod_ret, t.cod_cxp as cod_cxp, t.doc_cxp as doc_cxp, t.cod_tipo_ret as cod_tipo_ret, t.nom_ret as nom_ret, t.fecha as fecha, t.concepto_cxp as concepto_cxp, t.cod_moneda as cod_moneda, t.otra_moneda as otra_moneda, t.cod_tc as cod_tc, t.concepto_gasto as concepto_gasto, t.monto as monto, t.tot_ret as tot_ret, 1 as tasa
from 
(select a.pkyRetentionDetail as cod_ret, a.codAccountsPayable as cod_cxp, 
a.codType as cod_tipo_ret, f.fldName as nom_ret, b.fldDate as fecha, 
b.fldDocumentNumber as doc_cxp, c.fldName as concepto_cxp, 
b.codCurrency as cod_moneda, '$otra_moneda' as otra_moneda, 
b.codExchangeRate as cod_tc, a.codAccountsPayableDetail as cod_cxp_det, 
e.fldName as concepto_gasto, d.fldAmount as monto, a.fldTotal as tot_ret
from 
txsRetentionDetail a, payAccountsPayable b, payAccountsPayableConcept c, payAccountsPayableDetail d, payExpensesConcept e, txsRetentionType f 
where 
a.codAccountsPayable = b.pkyAccountsPayable and 
b.codConcept = c.pkyAccountsPayableConcept and 
b.fldDate >= '$fechaini' and 
b.fldDate <= '$fechafin' and 
$valor_retencion  
$valor_contexto2 
a.codAccountsPayableDetail = d.pkyAccountsPayableDetail and 
d.codExpensesConcept = e.pkyExpensesConcept and 
a.codType = f.pkyRetentionType and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
    union all 
select a.pkyPurchaseRetentionDetail as cod_ret, a.codPayrollExpenses as cod_cxp, 
a.codType as cod_tipo_ret, f.fldName as nom_ret, b.fldDate as fecha, 
b.fldNumber as doc_cxp, '$nom_concepto_imp' as concepto_cxp, 
b.codCurrency as cod_moneda, '$otra_moneda' as otra_moneda, 
b.codExchangeRate as cod_tc, a.codPayrollExpensesDetail as cod_cxp_det, 
d.fldName as concepto_gasto, c.fldAmount as monto, a.fldTotal as tot_ret
from 
txsPurchaseRetentionDetail a, pchPayrollExpenses b, pchPayrollExpensesDetail c, pchPurchaseConcept d, txsRetentionType f
where 
a.codPayrollExpenses = b.pkyPayrollExpenses and 
a.codPayrollExpensesDetail = c.pkyPayrollExpensesDetail and 
b.fldDate >= '$fechaini' and 
b.fldDate <= '$fechafin' and 
$valor_retencion 
$valor_contexto2 
c.codPurchaseConcept = d.pkyPurchaseConcept and 
a.codType = f.pkyRetentionType and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) fin
group by fin.cod_tipo_ret
order by fin.cod_tipo_ret;");

$matriz_cont = array();
$i = 1;
while ($row = mysqli_fetch_array($resultcont)) {
	$matriz_cont [$i][1] = $row['tipo'];
	$matriz_cont [$i][2] = $row['nro'];
	$i++;
}
$fin_cont = $i - 1;
mysqli_free_result($resultcont);

$matriz_ret = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_ret [$i][1] = $row['cod_ret'];
	$matriz_ret [$i][2] = $row['fecha'];
	$matriz_ret [$i][3] = $row['cod_cxp'];
	$matriz_ret [$i][4] = $row['doc_cxp'];
	$matriz_ret [$i][5] = $row['nom_ret'];
	$matriz_ret [$i][6] = $row['concepto_cxp'];
	$matriz_ret [$i][7] = $row['concepto_gasto'];
	$matriz_ret [$i][8] = $row['moneda'];
	$matriz_ret [$i][9] = round($row['monto'],$n_decimales);
	$matriz_ret [$i][10] = round($row['tot_ret'],$n_decimales);
	$matriz_ret [$i][11] = $row['tasa'];
	$matriz_ret [$i][12] = round($row['monto_om'],$n_decimales);
	$matriz_ret [$i][13] = round($row['tot_ret_om'],$n_decimales);
	$matriz_ret [$i][14] = $row['tipo_reten'];
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);

?>
<table border="0" align="left" width="100%" cellpadding="5">
<tr><td><?php echo $mostrar_logo_reportes; ?>
</td><td align="right"><b>Fecha: <?php echo date("Y-m-d"); ?><br/>Hora: <?php echo date("H:i:s"); ?><br/>Usuario: <?php echo $usuario; ?></b>
</td></tr></table>
<p><center><font color="#000099" size="+1"><b>Reporte de Retenciones</b></font></center></p>
<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>	
    <table border="1" align="center" cellpadding="2" width="100%">
        <TR> 
        <TD width="33%"><b>Tipo de Retenci&oacute;n: </b><?php echo $nom_retencion; ?></TD>
        <TD width="33%"><b>Mostrar Detalle: </b><?php echo $mostrar_detalle; ?></TD>
        <TD width="33%"><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        </TR>
        <TR>
        <TD colspan="2"><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD><b>Expresado en: </b><?php echo $nom_otra_moneda; ?></TD>
        </TR>
    </table>

</td>
<!--
<td valign="top" width="150">

    <table border="1" align="center" cellpadding="2" width="100%">
    <tr>
    <TD><b>Tipo de Cambio:</b></TD>
    </tr><tr>
    <TD align="center"><a href="../../ModuleAccounting/VoucherExchangeRate/index.php?pkyExchangeRate=<?php echo $ultimo_tc; ?>&user=<?php echo $usuario; ?>" title="Tipo de Cambio"><?php echo $ultimo_tc; ?></a> (<?php echo $tc_fecha; ?> - <?php echo $tc_hora; ?>)<br/ >
        <table border="0" cellpadding="2" align="center">
            <?php
            for($m = 1; $m <= $fin_m; $m++) {
            printf("<tr><td align='left'>1 %s</td>", $ma_tc [$m][1]);//moneda base
            printf("<td align='center'>%s</td>", ' = ');//letra a
            printf("<td align='left'>%s %s</td>", number_format($ma_tc [$m][3],$n_tipocambio), $ma_tc [$m][2]);//moneda target y tasa
            //printf("<td align='right'>%s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio));//tasa
            }
            ?>
        </table>
    </TD>
    </tr>
	</table>
    
</td>
-->
</tr>
</table>
<br />
<table border="1" align="center" cellpadding="2" width="100%">
    <thead>
    <TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center" width="65"><b>Fecha</b></TD>
    <TD align="center"><b>Cod. CxP / PdG</b></TD>
    <TD align="center"><b>Doc. CxP / PdG</b></TD>
    <TD align="center"><b>Retenci&oacute;n</b></TD>
    <TD align="center"><b>Concepto CxP</b></TD>
    <TD align="center"><b>Concepto Gasto</b></TD>
    <TD align="center"><b>Mon.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Monto Ret.</b></TD>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Monto en <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Monto Ret. en <?php echo $nom_otra_moneda; ?></b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">

  <?php
  $color = 2;
  $suma_ret_par = 0;
  $suma_ret = 0;
  $suma_monto_par = 0;
  $suma_monto = 0;
  $j = 1; 
  $i = 1;
  while ($fin_cont > 0) { //while
    while ($matriz_cont [$j][2] > 0) { //while result
	if($mostrar == 0) { //if mostrar
	if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_ret [$i][1]);//cod
	} else {
	printf("<tr><td align='center'>%s</td>", $matriz_ret [$i][1]);//cod
	}//fin if color
	printf("<td align='right'>%s</td>", $matriz_ret [$i][2]);//fecha
	$codigo = $matriz_ret [$i][3];
	if($matriz_ret [$i][14] == 0) {
	printf("<td align='center'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$codigo&user=$usuario' title='Nota de Cuenta por Pagar'>CxP - %s</a></td>", $matriz_ret [$i][3]);// cod cxp
	}
	if($matriz_ret [$i][14] == 1) {
	printf("<td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$codigo&user=$usuario' title='Planilla de Gastos'>PdG - %s</a></td>", $matriz_ret [$i][3]);// cod planilla
	}
	printf("<td align='center'>%s</td>", $matriz_ret [$i][4]);// doc cxp
	printf("<td align='left'>%s</td>", $matriz_ret [$i][5]);//retencion
	printf("<td align='left'>%s</td>", $matriz_ret [$i][6]);//concepto cxp
	printf("<td align='left'>%s</td>", $matriz_ret [$i][7]);//concepto gasto
	printf("<td align='left'>%s</td>", $matriz_ret [$i][8]);//moneda
	printf("<td align='right'>%s</td>", number_format($matriz_ret [$i][9],$n_decimales));//monto
	printf("<td align='right'>%s</td>", number_format($matriz_ret [$i][10],$n_decimales));//total ret
	printf("<td align='right'>%s</td>", number_format($matriz_ret [$i][11],$n_tipocambio));//tasa
	printf("<td align='right'>%s</td>", number_format($matriz_ret [$i][12],$n_decimales));//monto om
	printf("<td align='right'>%s</td></tr>", number_format($matriz_ret [$i][13],$n_decimales));//total ret om
	
	} //fin if mostrar
	$suma_ret_par = $suma_ret_par + $matriz_ret [$i][13];
	$suma_ret = $suma_ret + $matriz_ret [$i][13];
    $suma_monto_par = $suma_monto_par + $matriz_ret [$i][12];
	$suma_monto = $suma_monto + $matriz_ret [$i][12];
	$matriz_cont [$j][2] = $matriz_cont [$j][2] - 1;
	$i++;
	//echo $matriz_cont [$j][2]. '<br>';
	$color++;
  	}//fin while result
	printf("<tr BGCOLOR='#ffcccc'><td colspan='11'><b>Total %s</b></td>", $matriz_cont [$j][1]);
	printf("<td align='right'><b>%s</b></td>", number_format($suma_monto_par, $n_decimales));	
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_ret_par, $n_decimales));	
	$suma_monto_par = 0;
	$suma_ret_par = 0;
	$fin_cont--;
	//echo $fin_cont. '<br>';
	$j++;
  }//fin while
  printf("<tr BGCOLOR='#ffcccc'><td colspan='11'><b>Total Retenciones</b></td>");
  printf("<td align='right'><b>%s</b></td>", number_format($suma_monto, $n_decimales));	
  printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_ret, $n_decimales));	
?>
</tbody>
</table>
<?php

} //fin if 2

?>

</body>
</html>
