<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Cuentas por Cobrar</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"];
$deudor=$_POST["deudor"];
$orden_cobro=$_POST["orden_cobro"];
$concepto_cobro=$_POST["concepto_cobro"];
$canal_venta=$_POST["canal_venta"];
$cod_contexto=$_POST["contexto"];
$muestra_cero=$_POST["muestra_cero"];
$otra_moneda=$_POST["otra_moneda"];
$fechaini_ven=$_POST["fechaini_ven"];
$fechafin_ven=$_POST["fechafin_ven"];
$usuario = $_POST["usuario"];

$cod_cxc=$_GET["cod_cxc"];
$empresa=$_GET["empresa"];
$otra_moneda_det=$_GET["otra_moneda_det"];
$cod_contexto_det=$_GET["contexto_det"];
$usuario_det=$_GET["usuario_det"];
//$n_decimales = 6;

if($orden_cobro == 0 || $orden_cobro == 1) {
	$fechaini_ven = '1900-01-01';
	$fechafin_ven = '2999-12-31';
}

if($fechaini_ven == '0000-00-00') { $fechaini_ven = '1900-01-01'; }
if($fechafin_ven == '0000-00-00') { $fechafin_ven = '2999-12-31'; }

function resta_dias($fecha_i,$fecha_f)
{
	$dias	= (strtotime($fecha_i)-strtotime($fecha_f))/86400;
	$dias 	= abs($dias); $dias = floor($dias);		
	return $dias;
}
?>
<body>

<?php

$empresaoempleado = substr($deudor, -2);
$nombreemp = substr($deudor, 0, -3);

//Inicio reporte cuentas por cobrar


if($rep == 6 && $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);

//valor del codigo del ultimo tipo de cambio
$fecha_hoy=date("Y-m-d");
$resultom = mysqli_query($link, "SELECT T2.pkyExchangeRate as tc FROM 
(SELECT fldDate, MAX(fldTime) AS fldTime FROM actExchangeRate WHERE fldExclusive = '0' GROUP BY fldDate) as T1,
actExchangeRate T2 
WHERE 
T1.fldDate = (SELECT MAX(fldDate) FROM actExchangeRate WHERE fldDate <= '$fecha_hoy' AND fldExclusive = '0') AND 
T2.fldDate = T1.fldDate AND 
T2.fldTime = T1.fldTime AND 
sysEnabled = 1 AND sysAuthorized = 1;");
while ($row = mysqli_fetch_array($resultom)) {
	$ultimo_tc = $row['tc'];
	}
	mysqli_free_result($resultom);

//valores de conversion tipo de cambio
$resulttc = mysqli_query($link, "SELECT pkyExchangeRateDetail as codigotc, codBaseCurrency as monedabase, codTargetCurrency as monedacambio, fldRate as tc
from
actExchangeRateDetail
where
codExchangeRate = '$ultimo_tc' and
codBaseCurrency = '$otra_moneda' and
sysEnabled = 1 AND sysAuthorized = 1;");
$matriz_tc = array();
$i = 1;
while ($row = mysqli_fetch_array($resulttc)) {
	$matriz_tc [$i][1] = $row['codigotc'];//codigo tc
	$matriz_tc [$i][2] = $row['monedabase'];//codigo moneda base
	$matriz_tc [$i][3] = $row['monedacambio'];//codigo moneda cambio
	$matriz_tc [$i][4] = $row['tc'];//valor a multiplicar del tc 
	$i++;
	}
	mysqli_free_result($resulttc);

$fin_om = $i - 1;

 if ($muestra_cero == 0) { //if muestra cero
    $valor_muestra_cero = ' and a.fldBalance > 0 ';
	$nom_muestra_cero = 'No';
  } else { 
    $valor_muestra_cero = ' ';
	$nom_muestra_cero = 'Si';
  } //fin if muestra cero
  
if($otra_moneda != 0){ 
$mostrar_txt_moneda = $nom_otra_moneda;
} else {
$mostrar_txt_moneda = 'Moneda original';	
}

if($cod_contexto == -1){ 
	$nom_contexto = 'Todos';
	$valor_contexto = ' ';
} 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_contexto = ' a.sysContext = '.$cod_contexto.' and ';	
}

	if ($empresaoempleado == -3) { // if 2.2
		$nom_deudor = 'Todos';
	
	if ($orden_cobro == 0) { // if 2.2.1
		$nom_orden = 'Fecha generaci&oacute;n CxC y deudor';
		$valor_orden = 'fecha, empresa, empleado';
	}//fin if 2.2.1  
	
	if ($orden_cobro == 1) { // if 2.2.2
		$nom_orden = 'Deudor y fecha generaci&oacute;n CxC';
		$valor_orden = 'empresa, empleado, fecha';
	}//fin if 2.2.2
	
	if ($orden_cobro == 2) { // if 2.2.3
		$nom_orden = 'Fecha vencimiento CxC y deudor';
		$valor_orden = 'fecha_cobro, empresa, empleado';
	}//fin if 2.2.3  
	
	if ($orden_cobro == 3) { // if 2.2.4
		$nom_orden = 'Deudor y fecha vencimiento CxC';
		$valor_orden = 'empresa, empleado, fecha_cobro';
	}//fin if 2.2.4
	
	if ($orden_cobro == 4) { // if 2.2.4.1
		$nom_orden = 'C&oacute;digo';
		$valor_orden = 'codigo';
	}//fin if 2.2.4.1
	
	if($concepto_cobro == 0) { // if 2.2.5
		$nom_concepto_cobro = 'Todos';
		$valor_concepto_cobro = ' ';
	} // fin if 2.2.5
		else { //else 2.2.5
		
		$result3 = mysqli_query($link, "select fldName as nombre
							from colAccountsReceivableConcept
							where
							pkyAccountsReceivableConcept = '$concepto_cobro';");
		while ($row = mysqli_fetch_array($result3)) {   
		$nom_concepto_cobro = $row['nombre'];
		}
		mysqli_free_result($result3);
		$valor_concepto_cobro = ' a.codConcept = '.$concepto_cobro.' and';
		} // fin else 2.2.5
		
	if($canal_venta == 0) { // if 2.2.6
		$nom_canal_venta = 'Todos';
		$valor_canal_venta1 = ' ';
		$valor_canal_venta2 = ' ';
	} // fin if 2.2.6
		else { //else 2.2.6
		
		$result5 = mysqli_query($link, "select pkyChannel as codigo, fldName as canal
							from agnChannel
							where
							pkyChannel = '$canal_venta';");
		while ($row = mysqli_fetch_array($result5)) {   
		$nom_canal_venta = $row['canal'];
		}
		mysqli_free_result($result5);
		$valor_canal_venta1 = ' d.codChannel = '.$canal_venta.' and ';
		$valor_canal_venta2 = ' e.pkyPerson = 0 and ';
		} // fin else 2.2.6
		
		
$result = mysqli_query($link, "select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldCollectionDate as fecha_cobro, b.fldName as concepto, 
c.fldName as concepto_ingreso, d.fldName as empresa, a.codEmployee as empleado, a.codCurrency as cod_moneda, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as deuda, a.fldBalance as saldo, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c,
agnCompany d, actCurrency f
where
$valor_concepto_cobro 
$valor_canal_venta1 
$valor_contexto 
a.fldCollectionDate >= '$fechaini_ven' and 
a.fldCollectionDate <= '$fechafin_ven' and  
a.codConcept = b.pkyAccountsReceivableConcept and
a.codIncomeConcept = c.pkyIncomeConcept and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1
$valor_muestra_cero
order by $valor_orden;");
		
$result2 = mysqli_query($link, "select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldCollectionDate as fecha_cobro, b.fldName as concepto, 
c.fldName as concepto_ingreso, a.codClient as empresa, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, a.codCurrency as cod_moneda, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as deuda, a.fldBalance as saldo, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c,
agnPerson e, actCurrency f
where
$valor_concepto_cobro 
$valor_canal_venta2 
$valor_contexto 
a.fldCollectionDate >= '$fechaini_ven' and 
a.fldCollectionDate <= '$fechafin_ven' and 
a.codConcept = b.pkyAccountsReceivableConcept and
a.codIncomeConcept = c.pkyIncomeConcept and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1
$valor_muestra_cero
order by $valor_orden;");
		
		?>
<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><br /><center><font color="#000099" size="+1"><b>Cuentas por Cobrar</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="25%"><b>Deudor: </b><?php echo $nom_deudor; ?></TD>
        <TD width="25%"><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
        <TD width="25%"><b>Concepto: </b><?php echo $nom_concepto_cobro; ?></TD>
        <TD width="25%"><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        </TR>
        <TR>
        <TD><b>De Fecha (Venc.): </b><?php echo $fechaini_ven; ?> <b>a Fecha (Venc.): </b><?php echo $fechafin_ven; ?></TD>
        <TD><b>Canal (solo para empresas): </b><?php echo $nom_canal_venta; ?></TD>
        <TD><b>Mostrar saldo en cero: </b><?php echo $nom_muestra_cero; ?></TD>
        <TD><b>Expresado en: </b><?php echo $mostrar_txt_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"><b>Nro. Doc.</b></TD>
	<TD align="center" width="65"><b>Fecha</b></TD>
    <TD align="center" width="65"><b>Fecha de Cobro</b></TD>
    <TD align="center"><b>Vencida</b></TD>
    <TD align="center"><b>Dias Vto.</b></TD>
    <TD align="center"><b>Concepto</b></TD>
    <TD align="center"><b>Concepto de Ingreso</b></TD>
    <TD align="center"><b>Empresa</b></TD>
    <TD align="center"><b>Empleado</b></TD>
    <TD align="center"><b>Glosa</b></TD>
	<TD align="center"><b>Gen. Por</b></TD>
    <TD align="center"><b>Mon.</b></TD>
    <TD align="center"><b>Monto Inicial</b></TD>
    <TD align="center"><b>Cobrado</b></TD>
    <TD align="center"><b>Saldo</b></TD>
    <?php if($otra_moneda != 0){ ?>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Cobrado en <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Saldo en <?php echo $nom_otra_moneda; ?></b></TD>
    <?php } ?>
	<TD align="center"><b>Vendedor</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">

    <?php
    $color = 2;
	$suma_cant = 0;
	$suma_empleado = 0;
	$suma_empresa = 0;
	$suma_cant_om = 0;
	$suma_empleado_om = 0;
	$suma_empresa_om = 0;
	$suma_empresa_cobro_om = 0;
	$suma_empleado_cobro_om = 0;
	$suma_cant_cobro_om = 0;
	while ($row = mysqli_fetch_array($result)) { //while
		
		$codigo = $row['codigo'];
	 	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo&user=$usuario' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//cod
		} else {
		printf("<tr><td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo&user=$usuario' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//cod
		}//cierra if color
		printf("<td align='center'>%s</td>", $row['nrodoc']);//numero documento
		printf("<td align='right'>%s</td>", $row['fecha']);//fecha
		printf("<td align='right'>%s</td>", $row['fecha_cobro']);//fecha de cobro
		if($row['fecha_cobro'] < $fecha_hoy) {//if 1
			printf("<td align='center'><font color='#FF0000'>SI</font></td>");//vencida
			printf("<td align='center'><font color='#FF0000'>-%s</font></td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
		} else { //else if 1
			if($row['fecha_cobro'] == $fecha_hoy) {
				printf("<td align='center'>HOY</td>");//vence hoy
				printf("<td align='center'>%s</td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
			} else {
				printf("<td align='center'>NO</td>");//no vencida
				printf("<td align='center'>%s</td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
			}
		}//fin if 1
		
		//para enlazar al que genero el asiento
		$papa_componente = $row['papa'];
		$papa_codigo = $row['cod_papa'];
		//$usuario = nombre del usuario
		include "../../generadopor.php";
		
		printf("<td>%s</td>", $row['concepto']);//concepto
		printf("<td>%s</td>", $row['concepto_ingreso']);//concepto de ingreso
		printf("<td>%s</td>", $row['empresa']);//empresa
		printf("<td>%s</td>", $row['empleado']);//empleado
		printf("<td>%s</td>", $row['glosa']);//glosa
		printf("<td align='center'>%s</td>", $papa_enlace);//papa enlace
		printf("<td>%s</td>", $row['moneda']);//moneda
		printf("<td align='right'>%s</td>", number_format($row['deuda'],$n_decimales));//deuda
		if($otra_moneda != 0){ //if otra moneda
			if($row['cod_moneda'] == $otra_moneda){ //if monedas iguales
				$tasa_tc = 1;
			}//fin if monedas iguales 
			else { //else monedas iguales
		 	$i = 1;
		 	while($i <= $fin_om){ //while busca tasa tc
				if($row['cod_moneda'] == $matriz_tc [$i][3] && $otra_moneda == $matriz_tc [$i][2]){ //if linea tc
					$tasa_tc = $matriz_tc [$i][4];
				}//fin if linea tc
			$i++;
			}//fin while busca tasa tc
		 }//fin if monedas iguales
		 printf("<td align='right'>%s</td>", number_format($row['deuda'] - $row['saldo'],$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=1&otra_moneda_det=0&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'],$n_decimales));//saldo
		printf("<td align='right'>%s</td>", number_format($tasa_tc,$n_tipocambio));// tasa de tc
		printf("<td align='right'>%s</td>", number_format(($row['deuda'] - $row['saldo']) * $tasa_tc,$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=1&otra_moneda_det=$otra_moneda&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'] * $tasa_tc,$n_decimales));//saldo otra moneda
		}//fin if otra moneda 
		else {	//else otra moneda
		printf("<td align='right'>%s</td>", number_format($row['deuda'] - $row['saldo'],$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=1&otra_moneda_det=0&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'],$n_decimales));//saldo
		}//fin else otra moneda

		//para mostrar el vendedor de nota de pedido
		$cod_vendedor = NULL;
		$vendedor = NULL;

		if($papa_componente == 'ComponentOrderNote') 
		{ 
			$resultven = mysqli_query($link, "select a.codSeller as cod_vendedor, case when b.fldMiddleName is NULL then CONCAT(b.fldLastName,', ',b.fldFirstName) else CONCAT(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor
			from slsOrderNote a, agnPerson b
			where
			a.codSeller = b.pkyPerson and
			a.pkyOrderNote = '$papa_codigo';");
					
			while ($row = mysqli_fetch_array($resultven)) {
			$cod_vendedor = $row['cod_vendedor'];
			$vendedor = $row['vendedor'];
			}
			mysqli_free_result($resultven);
		}

		//para mostrar el vendedor de nota de venta
		if($papa_componente == 'ComponentSalesNote') 
		{ 
			$resultven = mysqli_query($link, "select a.codSeller as cod_vendedor, case when b.fldMiddleName is NULL then CONCAT(b.fldLastName,', ',b.fldFirstName) else CONCAT(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor
			from slsSalesNote a, agnPerson b
			where
			a.codSeller = b.pkyPerson and
			a.pkySalesNote = '$papa_codigo';");
					
			while ($row = mysqli_fetch_array($resultven)) {
			$cod_vendedor = $row['cod_vendedor'];
			$vendedor = $row['vendedor'];
			}
			mysqli_free_result($resultven);
		}
		printf("<td>%s</td></tr>", $vendedor);//vendedor

		$suma_empresa_om = $suma_empresa_om + $row['saldo'] * $tasa_tc;
	    $suma_empresa = $suma_empresa + $row['saldo'];
		$suma_empresa_cobro_om = $suma_empresa_cobro_om + ($row['deuda'] - $row['saldo']) * $tasa_tc;
	  	$color++;
	}//fin while
	mysqli_free_result($result);
	
?>
 <?php if($otra_moneda != 0){ ?>
 <tr BGCOLOR='#ffcccc'>
 <td colspan="17"><b>Total Cuentas por Cobrar Empresas</b></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_empresa_cobro_om,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_empresa_om,$n_decimales)); ?></td>
 <td>&nbsp;</td>
 </tr>
 <?php } else { ?>
 <tr BGCOLOR='#ffcccc'><td colspan="17">&nbsp;</td></tr>
 <?php } ?>           
 <?php
		
	while ($row = mysqli_fetch_array($result2)) { //while
		
	 	$codigo = $row['codigo'];
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo&user=$usuario' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//cod
		} else {
		printf("<tr><td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo&user=$usuario' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//cod
		} //cierra if color
		printf("<td align='center'>%s</td>", $row['nrodoc']);//numero documento
		printf("<td align='right'>%s</td>", $row['fecha']);//fecha
		printf("<td align='right'>%s</td>", $row['fecha_cobro']);//fecha de cobro
		if($row['fecha_cobro'] < $fecha_hoy) {//if 1
			printf("<td align='center'><font color='#FF0000'>SI</font></td>");//vencida
			printf("<td align='center'><font color='#FF0000'>-%s</font></td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
		} else { //else if 1
			if($row['fecha_cobro'] == $fecha_hoy) {
				printf("<td align='center'>HOY</td>");//vence hoy
				printf("<td align='center'>%s</td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
			} else {
				printf("<td align='center'>NO</td>");//no vencida
				printf("<td align='center'>%s</td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
			}
		}//fin if 1
		
		//para enlazar al que genero el asiento
		$papa_componente = $row['papa'];
		$papa_codigo = $row['cod_papa'];
		//$usuario = nombre del usuario
		include "../../generadopor.php";
		
		printf("<td>%s</td>", $row['concepto']);//concepto
		printf("<td>%s</td>", $row['concepto_ingreso']);//concepto de ingreso
		printf("<td>%s</td>", $row['empresa']);//empresa
		printf("<td>%s</td>", $row['empleado']);//empleado
		printf("<td>%s</td>", $row['glosa']);//glosa
		printf("<td align='center'>%s</td>", $papa_enlace);//papa enlace
		printf("<td>%s</td>", $row['moneda']);//moneda
		printf("<td align='right'>%s</td>", number_format($row['deuda'],$n_decimales));//deuda
		if($otra_moneda != 0){ //if otra moneda
			if($row['cod_moneda'] == $otra_moneda){ //if monedas iguales
				$tasa_tc = 1;
			}//fin if monedas iguales 
			else { //else monedas iguales
		 	$i = 1;
		 	while($i <= $fin_om){ //while busca tasa tc
				if($row['cod_moneda'] == $matriz_tc [$i][3] && $otra_moneda == $matriz_tc [$i][2]){ //if linea tc
					$tasa_tc = $matriz_tc [$i][4];
				}//fin if linea tc
			$i++;
			}//fin while busca tasa tc
		 }//fin if monedas iguales
		 printf("<td align='right'>%s</td>", number_format($row['deuda'] - $row['saldo'],$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=0&otra_moneda_det=0&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'],$n_decimales));//saldo
		printf("<td align='right'>%s</td>", number_format($tasa_tc,$n_tipocambio));// tasa de tc
		printf("<td align='right'>%s</td>", number_format(($row['deuda'] - $row['saldo']) * $tasa_tc,$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=0&otra_moneda_det=$otra_moneda&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'] * $tasa_tc,$n_decimales));//saldo otra moneda
		}//fin if otra moneda 
		else {	//else otra moneda
		printf("<td align='right'>%s</td>", number_format($row['deuda'] - $row['saldo'],$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=0&otra_moneda_det=0&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'],$n_decimales));//saldo
		}//fin else otra moneda
		$suma_empleado_om = $suma_empleado_om + $row['saldo'] * $tasa_tc;
		$suma_empleado = $suma_empleado + $row['saldo'];
		$suma_empleado_cobro_om = $suma_empleado_cobro_om + ($row['deuda'] - $row['saldo']) * $tasa_tc;
		$color++;

		//para mostrar el vendedor de nota de pedido
		$cod_vendedor = NULL;
		$vendedor = NULL;

		if($papa_componente == 'ComponentOrderNote') 
		{ 
			$resultven = mysqli_query($link, "select a.codSeller as cod_vendedor, case when b.fldMiddleName is NULL then CONCAT(b.fldLastName,', ',b.fldFirstName) else CONCAT(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor
			from slsOrderNote a, agnPerson b
			where
			a.codSeller = b.pkyPerson and
			a.pkyOrderNote = '$papa_codigo';");
					
			while ($row = mysqli_fetch_array($resultven)) {
			$cod_vendedor = $row['cod_vendedor'];
			$vendedor = $row['vendedor'];
			}
			mysqli_free_result($resultven);
		}

		//para mostrar el vendedor de nota de venta
		if($papa_componente == 'ComponentSalesNote') 
		{ 
			$resultven = mysqli_query($link, "select a.codSeller as cod_vendedor, case when b.fldMiddleName is NULL then CONCAT(b.fldLastName,', ',b.fldFirstName) else CONCAT(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor
			from slsSalesNote a, agnPerson b
			where
			a.codSeller = b.pkyPerson and
			a.pkySalesNote = '$papa_codigo';");
					
			while ($row = mysqli_fetch_array($resultven)) {
			$cod_vendedor = $row['cod_vendedor'];
			$vendedor = $row['vendedor'];
			}
			mysqli_free_result($resultven);
		}
		printf("<td>%s</td></tr>", $vendedor);//vendedor
	}//fin while
	mysqli_free_result($result2);
	
?>
<?php if($otra_moneda != 0){ ?>
<tr BGCOLOR='#ffcccc'>
<td colspan="17"><b>Total Cuentas por Cobrar Empleados</b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_empleado_cobro_om,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_empleado_om,$n_decimales)); ?></td>
<td>&nbsp;</td>
</tr>
<?php } else { ?>
<tr BGCOLOR='#ffcccc'><td colspan="17">&nbsp;</td></tr>
<?php } ?> 
    <?php
	
	$suma_cant = $suma_empleado + $suma_empresa;
	$suma_cant_om = $suma_empleado_om + $suma_empresa_om;
	$suma_cant_cobro_om = $suma_empleado_cobro_om + $suma_empresa_cobro_om;
	?>
    <?php if($otra_moneda != 0){ ?>
	<tr BGCOLOR='#ffcccc'><td colspan="17"><b>Total Cuentas por Cobrar</b></td>
    <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_cobro_om,$n_decimales)); ?></td>
    <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_om,$n_decimales)); ?></td>
	<td>&nbsp;</td></tr>
    <?php } else { ?>
    
    <?php } ?> 

</tbody>    
</table>
<?php
	}//fin if 2.2
	
	if ($empresaoempleado == -1) { // if 2.3
	
		if($nombreemp == 0) { //if 2.3.1
			$nom_deudor = 'Empresas';
			$valor_empresa = ' ';
		} else {
		//listado de empresas
		$resultemp = mysqli_query($link, "select pkyCompany as codigo, fldName as empresa
			from agnCompany
			where
			pkyCompany = '$nombreemp';");
		while ($row = mysqli_fetch_array($resultemp)) {   
			$nom_deudor = $row['codigo'].' - '.$row['empresa'];
			}
			mysqli_free_result($resultemp);
			$valor_empresa = 'a.codClient = '.$nombreemp.' and ';;
		}// fin if 2.3.1
		
		if ($orden_cobro == 0) { // if 2.3.2
			$nom_orden = 'Fecha generaci&oacute;n CxC y empresa';
			$valor_orden = 'fecha, empresa';
		}//fin if 2.3.2
		
		if ($orden_cobro == 1) { // if 2.3.3
			$nom_orden = 'Empresa y fecha generaci&oacute;n CxC';
			$valor_orden = 'empresa, fecha';
		}//fin if 2.3.3
		
		if ($orden_cobro == 2) { // if 2.3.4
			$nom_orden = 'Fecha vencimiento CxC y empresa';
			$valor_orden = 'fecha_cobro, empresa';
		}//fin if 2.3.4
		
		if ($orden_cobro == 3) { // if 2.3.5
			$nom_orden = 'Empresa y fecha vencimiento CxC';
			$valor_orden = 'empresa, fecha_cobro';
		}//fin if 2.3.5
		
			
		if($concepto_cobro == 0) { // if 2.3.6
		$nom_concepto_cobro = 'Todos';
		$valor_concepto_cobro = ' ';
		} else { 
		$result3 = mysqli_query($link, "select fldName as nombre
				from colAccountsReceivableConcept
				where
				pkyAccountsReceivableConcept = '$concepto_cobro';");
		while ($row = mysqli_fetch_array($result3)) {   
		$nom_concepto_cobro = $row['nombre'];
		}
		mysqli_free_result($result3);
		$valor_concepto_cobro = 'a.codConcept = '.$concepto_cobro.' and ';
		} // fin if 2.3.6
		
		if($canal_venta == 0) { // if 2.3.7
		$nom_canal_venta = 'Todos';
		$valor_canal_venta1 = ' ';
		$valor_canal_venta2 = ' ';
	} // fin if 2.3.7
		else { //else 2.3.7
		
		$result5 = mysqli_query($link, "select pkyChannel as codigo, fldName as canal
							from agnChannel
							where
							pkyChannel = '$canal_venta';");
		while ($row = mysqli_fetch_array($result5)) {   
		$nom_canal_venta = $row['canal'];
		}
		mysqli_free_result($result5);
		$valor_canal_venta1 = ' d.codChannel = '.$canal_venta.' and ';
		$valor_canal_venta2 = ' e.pkyPerson = 0 and ';
		} // fin else 2.3.7
		
				
		$result = mysqli_query($link, "select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldCollectionDate as fecha_cobro, b.fldName as concepto, 
c.fldName as concepto_ingreso, d.fldName as empresa, a.codCurrency as cod_moneda, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as deuda, a.fldBalance as saldo, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c,
agnCompany d, actCurrency f
where 
$valor_empresa 
$valor_concepto_cobro 
$valor_canal_venta1 
$valor_contexto 
a.fldCollectionDate >= '$fechaini_ven' and 
a.fldCollectionDate <= '$fechafin_ven' and  
a.codConcept = b.pkyAccountsReceivableConcept and
a.codIncomeConcept = c.pkyIncomeConcept and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1 
$valor_muestra_cero
order by $valor_orden;");
		
		?>
<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><br /><center><font color="#000099" size="+1"><b>Cuentas por Cobrar</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="25%"><b>Deudor: </b><?php echo $nom_deudor; ?></TD>
        <TD width="25%"><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
        <TD width="25%"><b>Concepto: </b><?php echo $nom_concepto_cobro; ?></TD>
        <TD width="25%"><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        </TR>
        <TR>
        <TD><b>De Fecha (Venc.): </b><?php echo $fechaini_ven; ?> <b>a Fecha (Venc.): </b><?php echo $fechafin_ven; ?></TD>
        <TD><b>Canal (solo para empresas): </b><?php echo $nom_canal_venta; ?></TD>
        <TD><b>Mostrar saldo en cero: </b><?php echo $nom_muestra_cero; ?></TD>
        <TD><b>Expresado en: </b><?php echo $mostrar_txt_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"><b>Nro. Doc.</b></TD>
	<TD align="center" width="65"><b>Fecha</b></TD>
    <TD align="center" width="65"><b>Fecha de Cobro</b></TD>
    <TD align="center"><b>Vencida</b></TD>
    <TD align="center"><b>Dias Vto.</b></TD>
    <TD align="center"><b>Concepto</b></TD>
    <TD align="center"><b>Concepto de Ingreso</b></TD>
    <TD align="center"><b>Empresa</b></TD>
    <TD align="center"><b>Glosa</b></TD>
	<TD align="center"><b>Gen. Por</b></TD>
    <TD align="center"><b>Mon.</b></TD>
    <TD align="center"><b>Monto Inicial</b></TD>
    <TD align="center"><b>Cobrado</b></TD>
    <TD align="center"><b>Saldo</b></TD>
    <?php if($otra_moneda != 0){ ?>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Cobrado en <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Saldo en <?php echo $nom_otra_moneda; ?></b></TD>
    <?php } ?>
	<TD align="center"><b>Vendedor</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">

    <?php
    $color = 2;
	$suma_empresa = 0;
	$suma_empresa_om = 0;
	$suma_empresa_cobro_om = 0;
	while ($row = mysqli_fetch_array($result)) { //while
		
		$codigo = $row['codigo'];
	 	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo&user=$usuario' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//cod
		} else {
		printf("<tr><td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo&user=$usuario' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//cod
		}//cierra if color
		printf("<td align='center'>%s</td>", $row['nrodoc']);//numero documento
		printf("<td align='right'>%s</td>", $row['fecha']);//fecha
		printf("<td align='right'>%s</td>", $row['fecha_cobro']);//fecha de cobro
		if($row['fecha_cobro'] < $fecha_hoy) {//if 1
			printf("<td align='center'><font color='#FF0000'>SI</font></td>");//vencida
			printf("<td align='center'><font color='#FF0000'>-%s</font></td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
		} else { //else if 1
			if($row['fecha_cobro'] == $fecha_hoy) {
				printf("<td align='center'>HOY</td>");//vence hoy
				printf("<td align='center'>%s</td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
			} else {
				printf("<td align='center'>NO</td>");//no vencida
				printf("<td align='center'>%s</td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
			}
		}//fin if 1
		
		//para enlazar al que genero el asiento
		$papa_componente = $row['papa'];
		$papa_codigo = $row['cod_papa'];
		//$usuario = nombre del usuario
		include "../../generadopor.php";
		
		printf("<td>%s</td>", $row['concepto']);//concepto
		printf("<td>%s</td>", $row['concepto_ingreso']);//concepto de ingreso
		printf("<td>%s</td>", $row['empresa']);//empresa
		printf("<td>%s</td>", $row['glosa']);//glosa
		printf("<td align='center'>%s</td>", $papa_enlace);//papa enlace
		printf("<td>%s</td>", $row['moneda']);//moneda
		printf("<td align='right'>%s</td>", number_format($row['deuda'],$n_decimales));//deuda
		
		if($otra_moneda != 0){ //if otra moneda
			if($row['cod_moneda'] == $otra_moneda){ //if monedas iguales
				$tasa_tc = 1;
			}//fin if monedas iguales 
			else { //else monedas iguales
		 	$i = 1;
		 	while($i <= $fin_om){ //while busca tasa tc
				if($row['cod_moneda'] == $matriz_tc [$i][3] && $otra_moneda == $matriz_tc [$i][2]){ //if linea tc
					$tasa_tc = $matriz_tc [$i][4];
				}//fin if linea tc
			$i++;
			}//fin while busca tasa tc
		 }//fin if monedas iguales
		 printf("<td align='right'>%s</td>", number_format($row['deuda'] - $row['saldo'],$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=1&otra_moneda_det=0&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'],$n_decimales));//saldo
		printf("<td align='right'>%s</td>", number_format($tasa_tc,$n_tipocambio));// tasa de tc
		printf("<td align='right'>%s</td>", number_format(($row['deuda'] - $row['saldo']) * $tasa_tc,$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=1&otra_moneda_det=$otra_moneda&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'] * $tasa_tc,$n_decimales));//saldo otra moneda
		}//fin if otra moneda 
		else {	//else otra moneda
		printf("<td align='right'>%s</td>", number_format($row['deuda'] - $row['saldo'],$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=1&otra_moneda_det=0&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'],$n_decimales));//saldo
		}//fin else otra moneda

		$suma_empresa_om = $suma_empresa_om + $row['saldo'] * $tasa_tc;
	    $suma_empresa = $suma_empresa + $row['saldo'];
		$suma_empresa_cobro_om = $suma_empresa_cobro_om + ($row['deuda'] - $row['saldo']) * $tasa_tc;
		$color++;

		//para mostrar el vendedor de nota de pedido
		$cod_vendedor = NULL;
		$vendedor = NULL;

		if($papa_componente == 'ComponentOrderNote') 
		{ 
			$resultven = mysqli_query($link, "select a.codSeller as cod_vendedor, case when b.fldMiddleName is NULL then CONCAT(b.fldLastName,', ',b.fldFirstName) else CONCAT(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor
			from slsOrderNote a, agnPerson b
			where
			a.codSeller = b.pkyPerson and
			a.pkyOrderNote = '$papa_codigo';");
					
			while ($row = mysqli_fetch_array($resultven)) {
			$cod_vendedor = $row['cod_vendedor'];
			$vendedor = $row['vendedor'];
			}
			mysqli_free_result($resultven);
		}

		//para mostrar el vendedor de nota de venta
		if($papa_componente == 'ComponentSalesNote') 
		{ 
			$resultven = mysqli_query($link, "select a.codSeller as cod_vendedor, case when b.fldMiddleName is NULL then CONCAT(b.fldLastName,', ',b.fldFirstName) else CONCAT(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor
			from slsSalesNote a, agnPerson b
			where
			a.codSeller = b.pkyPerson and
			a.pkySalesNote = '$papa_codigo';");
					
			while ($row = mysqli_fetch_array($resultven)) {
			$cod_vendedor = $row['cod_vendedor'];
			$vendedor = $row['vendedor'];
			}
			mysqli_free_result($resultven);
		}
		printf("<td>%s</td></tr>", $vendedor);//vendedor

	}//fin while
	mysqli_free_result($result);
	
	?>
	<?php if($otra_moneda != 0){ ?>
    <tr BGCOLOR='#ffcccc'><td colspan="16"><b>Total Cuentas por Cobrar <?php echo $nom_deudor; ?></b></td>
    <td align="right"><?php printf("<b>%s</b>", number_format($suma_empresa_cobro_om,$n_decimales)); ?></td>
    <td align="right"><?php printf("<b>%s</b>", number_format($suma_empresa_om,$n_decimales)); ?></td>
	<td>&nbsp;</td>
	</tr>
    <?php } else { ?>
    
    <?php } ?>

</tbody> 
</table>
<?php
	}//fin if 2.3
	
	if ($empresaoempleado == -2) { // if 2.4
		
		if($nombreemp == 0) { //if 2.4.1
			$nom_deudor = 'Empleados';
			$valor_empleado = ' ';
		} else {
		//listado de empleados
		$resultemp = mysqli_query($link, "select pkyPerson as codigo, 
			case when fldMiddleName is NULL then concat(fldLastName,', ',fldFirstName) else concat(fldLastName,' ',fldMiddleName,', ',fldFirstName) end as empleado
			from agnPerson
			where 
			pkyPerson = '$nombreemp';");
		while ($row = mysqli_fetch_array($resultemp)) {   
			$nom_deudor = $row['codigo'].' - '. $row['empleado'];
			}
			mysqli_free_result($resultemp);
			$valor_empleado = 'a.codEmployee = '.$nombreemp.' and ';;
		}// fin if 2.4.1
		
		if ($orden_cobro == 0) { // if 2.4.2
			$nom_orden = 'Fecha generaci&oacute;n CxC y empleado';
			$valor_orden = 'fecha, empleado';
		}//fin if 2.4.2
		
		if ($orden_cobro == 1) { // if 2.4.3
			$nom_orden = 'Empleado y fecha generaci&oacute;n CxC';
			$valor_orden = 'empleado, fecha';
		}//fin if 2.4.3
		
		if ($orden_cobro == 2) { // if 2.4.4
			$nom_orden = 'Fecha vencimiento CxC y empleado';
			$valor_orden = 'fecha_cobro, empleado';
		}//fin if 2.4.4
		
		if ($orden_cobro == 3) { // if 2.4.5
			$nom_orden = 'Empleado y fecha vencimiento CxC';
			$valor_orden = 'empleado, fecha_cobro';
		}//fin if 2.4.5
		
			
		if($concepto_cobro == 0) { // if 2.4.6
		$nom_concepto_cobro = 'Todos';
		$valor_concepto_cobro = ' ';
		} else { 
		$result3 = mysqli_query($link, "select fldName as nombre
				from colAccountsReceivableConcept
				where
				pkyAccountsReceivableConcept = '$concepto_cobro';");
		while ($row = mysqli_fetch_array($result3)) {   
		$nom_concepto_cobro = $row['nombre'];
		}
		mysqli_free_result($result3);
		$valor_concepto_cobro = 'a.codConcept = '.$concepto_cobro.' and ';
		} // fin if 2.4.6
		
		$result = mysqli_query($link, "select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nrodoc,
a.fldDate as fecha, a.fldCollectionDate as fecha_cobro, b.fldName as concepto, 
c.fldName as concepto_ingreso, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado,
f.fldName as moneda, a.codCurrency as cod_moneda, a.txtDescription as glosa, a.fldTotal as deuda, a.fldBalance as saldo, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c,
agnPerson e, actCurrency f
where 
$valor_concepto_cobro 
$valor_empleado 
$valor_contexto 
a.fldCollectionDate >= '$fechaini_ven' and 
a.fldCollectionDate <= '$fechafin_ven' and  
a.codConcept = b.pkyAccountsReceivableConcept and
a.codIncomeConcept = c.pkyIncomeConcept and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1 
$valor_muestra_cero
order by $valor_orden;");
		
		?>
<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><br /><center><font color="#000099" size="+1"><b>Cuentas por Cobrar</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>Deudor: </b><?php echo $nom_deudor; ?></TD>
        <TD width="33%"><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
        <TD width="33%"><b>Concepto: </b><?php echo $nom_concepto_cobro; ?></TD>
        </TR>
        <TR>
        <TD colspan="3"><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        </TR>
        <TR>
        <TD><b>De Fecha (Venc.): </b><?php echo $fechaini_ven; ?> <b>a Fecha (Venc.): </b><?php echo $fechafin_ven; ?></TD>
        <TD><b>Mostrar saldo en cero: </b><?php echo $nom_muestra_cero; ?></TD>
        <TD><b>Expresado en: </b><?php echo $mostrar_txt_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"><b>Nro. Doc.</b></TD>
	<TD align="center" width="65"><b>Fecha</b></TD>
    <TD align="center" width="65"><b>Fecha de Cobro</b></TD>
    <TD align="center"><b>Vencida</b></TD>
    <TD align="center"><b>Dias Vto.</b></TD>
    <TD align="center"><b>Concepto</b></TD>
   	<TD align="center"><b>Concepto de Ingreso</b></TD>
    <TD align="center"><b>Empleado</b></TD>
    <TD align="center"><b>Glosa</b></TD>
    <TD align="center"><b>Gen. Por</b></TD>
	<TD align="center"><b>Moneda</b></TD>
    <TD align="center"><b>Monto Inicial</b></TD>
    <TD align="center"><b>Cobrado</b></TD>
    <TD align="center"><b>Saldo</b></TD>
    <?php if($otra_moneda != 0){ ?>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Cobrado en <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Saldo en <?php echo $nom_otra_moneda; ?></b></TD>
    <?php } ?>
	<TD align="center"><b>Vendedor</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">

    <?php
    $color = 2;
	$suma_empleado = 0;
	$suma_empleado_om = 0;
	$suma_empleado_cobro_om = 0;
	while ($row = mysqli_fetch_array($result)) { //while
			
		$codigo = $row['codigo'];
	 	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo&user=$usuario' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//cod
		} else {
		printf("<tr><td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo&user=$usuario' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//cod
		}//cierra if color
		printf("<td align='center'>%s</td>", $row['nrodoc']);//numero documento
		printf("<td align='right'>%s</td>", $row['fecha']);//fecha
		printf("<td align='right'>%s</td>", $row['fecha_cobro']);//fecha de cobro
		if($row['fecha_cobro'] < $fecha_hoy) {//if 1
			printf("<td align='center'><font color='#FF0000'>SI</font></td>");//vencida
			printf("<td align='center'><font color='#FF0000'>-%s</font></td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
		} else { //else if 1
			if($row['fecha_cobro'] == $fecha_hoy) {
				printf("<td align='center'>HOY</td>");//vence hoy
				printf("<td align='center'>%s</td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
			} else {
				printf("<td align='center'>NO</td>");//no vencida
				printf("<td align='center'>%s</td>", resta_dias($fecha_hoy,$row['fecha_cobro']));//dias vencida
			}
		}//fin if 1
		
		//para enlazar al que genero el asiento
		$papa_componente = $row['papa'];
		$papa_codigo = $row['cod_papa'];
		//$usuario = nombre del usuario
		include "../../generadopor.php";
	
		printf("<td>%s</td>", $row['concepto']);//concepto
		printf("<td>%s</td>", $row['concepto_ingreso']);//concepto de ingreso
		printf("<td>%s</td>", $row['empleado']);//empleado
		printf("<td>%s</td>", $row['glosa']);//glosa
		printf("<td align='center'>%s</td>", $papa_enlace);//papa enlace
		printf("<td>%s</td>", $row['moneda']);//moneda
		printf("<td align='right'>%s</td>", number_format($row['deuda'],$n_decimales));//deuda
		
		if($otra_moneda != 0){ //if otra moneda
			if($row['cod_moneda'] == $otra_moneda){ //if monedas iguales
				$tasa_tc = 1;
			}//fin if monedas iguales 
			else { //else monedas iguales
		 	$i = 1;
		 	while($i <= $fin_om){ //while busca tasa tc
				if($row['cod_moneda'] == $matriz_tc [$i][3] && $otra_moneda == $matriz_tc [$i][2]){ //if linea tc
					$tasa_tc = $matriz_tc [$i][4];
				}//fin if linea tc
			$i++;
			}//fin while busca tasa tc
		 }//fin if monedas iguales
		 printf("<td align='right'>%s</td>", number_format($row['deuda'] - $row['saldo'],$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=0&otra_moneda_det=0&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'],$n_decimales));//saldo
		printf("<td align='right'>%s</td>", number_format($tasa_tc,$n_tipocambio));// tasa de tc
		printf("<td align='right'>%s</td>", number_format(($row['deuda'] - $row['saldo']) * $tasa_tc,$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=0&otra_moneda_det=$otra_moneda&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'] * $tasa_tc,$n_decimales));//saldo otra moneda
		}//fin if otra moneda 
		else {	//else otra moneda
		printf("<td align='right'>%s</td>", number_format($row['deuda'] - $row['saldo'],$n_decimales));//cobrado
		printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=0&otra_moneda_det=0&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'],$n_decimales));//saldo
		}//fin else otra moneda

		$suma_empleado_om = $suma_empleado_om + $row['saldo'] * $tasa_tc;
		$suma_empleado = $suma_empleado + $row['saldo'];
		$suma_empleado_cobro_om = $suma_empleado_cobro_om + ($row['deuda'] - $row['saldo']) * $tasa_tc;
		$color++;

		//para mostrar el vendedor de nota de pedido
		$cod_vendedor = NULL;
		$vendedor = NULL;

		if($papa_componente == 'ComponentOrderNote') 
		{ 
			$resultven = mysqli_query($link, "select a.codSeller as cod_vendedor, case when b.fldMiddleName is NULL then CONCAT(b.fldLastName,', ',b.fldFirstName) else CONCAT(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor
			from slsOrderNote a, agnPerson b
			where
			a.codSeller = b.pkyPerson and
			a.pkyOrderNote = '$papa_codigo';");
					
			while ($row = mysqli_fetch_array($resultven)) {
			$cod_vendedor = $row['cod_vendedor'];
			$vendedor = $row['vendedor'];
			}
			mysqli_free_result($resultven);
		}

		//para mostrar el vendedor de nota de venta
		if($papa_componente == 'ComponentSalesNote') 
		{ 
			$resultven = mysqli_query($link, "select a.codSeller as cod_vendedor, case when b.fldMiddleName is NULL then CONCAT(b.fldLastName,', ',b.fldFirstName) else CONCAT(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor
			from slsSalesNote a, agnPerson b
			where
			a.codSeller = b.pkyPerson and
			a.pkySalesNote = '$papa_codigo';");
					
			while ($row = mysqli_fetch_array($resultven)) {
			$cod_vendedor = $row['cod_vendedor'];
			$vendedor = $row['vendedor'];
			}
			mysqli_free_result($resultven);
		}
		printf("<td>%s</td></tr>", $vendedor);//vendedor

	}//fin while
	mysqli_free_result($result);
	
	?>
    <?php if($otra_moneda != 0){ ?>
	<tr BGCOLOR='#ffcccc'><td colspan="16"><b>Total Cuentas por Cobrar <?php echo $nom_deudor; ?></b></td>
    <td align="right"><?php printf("<b>%s</b>", number_format($suma_empleado_cobro_om,$n_decimales)); ?></td>
    <td align="right"><?php printf("<b>%s</b>", number_format($suma_empleado_om,$n_decimales)); ?></td>
	<td>&nbsp;</td>
	</tr>
    <?php } else { ?>
    
    <?php } ?>

</tbody>
</table>
<?php
	}//fin if 2.4
	
} //fin if 2



// inicio muestra detalle

if($rep == 6 && $tipo == 2) { // if 3

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


//valor del codigo del ultimo tipo de cambio
$fecha_hoy=date("Y-m-d");
$resultom = mysqli_query($link, "SELECT T2.pkyExchangeRate as tc FROM 
(SELECT fldDate, MAX(fldTime) AS fldTime FROM actExchangeRate WHERE fldExclusive = '0' GROUP BY fldDate) as T1,
actExchangeRate T2 
WHERE 
T1.fldDate = (SELECT MAX(fldDate) FROM actExchangeRate WHERE fldDate <= '$fecha_hoy' AND fldExclusive = '0') AND 
T2.fldDate = T1.fldDate AND 
T2.fldTime = T1.fldTime AND 
sysEnabled = 1 AND sysAuthorized = 1;");
while ($row = mysqli_fetch_array($resultom)) {
	$ultimo_tc = $row['tc'];
	}
	mysqli_free_result($resultom);

//valores de conversion tipo de cambio
$resulttc = mysqli_query($link, "SELECT pkyExchangeRateDetail as codigotc, codBaseCurrency as monedabase, codTargetCurrency as monedacambio, fldRate as tc
from
actExchangeRateDetail
where
codExchangeRate = '$ultimo_tc' and
codBaseCurrency = '$otra_moneda_det' and
sysEnabled = 1 AND sysAuthorized = 1;");
$matriz_tc = array();
$i = 1;
while ($row = mysqli_fetch_array($resulttc)) {
	$matriz_tc [$i][1] = $row['codigotc'];//codigo tc
	$matriz_tc [$i][2] = $row['monedabase'];//codigo moneda base
	$matriz_tc [$i][3] = $row['monedacambio'];//codigo moneda cambio
	$matriz_tc [$i][4] = $row['tc'];//valor a multiplicar del tc 
	$i++;
	}
	mysqli_free_result($resulttc);

$fin_om = $i - 1;


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


if($empresa == 1) { //if 3.1
$empoemp = 'Empresa';
$result = mysqli_query($link, "select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nrodoc,
a.fldDate as fecha, a.fldCollectionDate as fecha_cobro, b.fldName as concepto, 
c.fldName as concepto_ingreso, d.fldName as empresa, a.codCurrency as cod_moneda, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as deuda, a.fldBalance as saldo, a.fldParentComponent as papa, a.cosComponentKey as cod_papa 
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c, agnCompany d, actCurrency f
where
$valor_contexto_det 
a.pkyAccountsReceivable = '$cod_cxc' and
a.codConcept = b.pkyAccountsReceivableConcept and
a.codIncomeConcept = c.pkyIncomeConcept and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1;");

$result2 = mysqli_query($link, "select a.pkyCollection as cod_cobro, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, c.fldName as tipo, d.fldName as empresa, a.codCurrency as cod_moneda, f.fldName as moneda, a.codExchangeRate as cod_tc, b.fldTotal as total, a.fldParentComponent as papa, a.cosComponentKey as cod_papa 
from
colCollection a, colCollectionDetail b, colCollectionRoute c, agnCompany d, actCurrency f
where
$valor_contexto_det 
a.codRoute = c.pkyCollectionRoute and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
b.codAccountsReceivable = '$cod_cxc' and
a.pkyCollection = b.codCollection and
a.sysEnabled = 1 and a.sysAuthorized = 1

union all

select a.pkyPayment as cod_cobro, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, 
'Liquidacion' as tipo, d.fldName as empresa, a.codCurrency as cod_moneda, f.fldName as moneda, a.codExchangeRate as cod_tc, b.fldTotal as total, a.fldParentComponent as papa, a.cosComponentKey as cod_papa 
from
payPayment a, payPaymentDetail b, agnCompany d, actCurrency f
where
$valor_contexto_det 
a.pkyPayment = b.codPayment and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
b.codAccountsReceivable = '$cod_cxc' and
a.sysEnabled = 1 and a.sysAuthorized = 1
order by fecha;");
}// fin if 3.1

if($empresa == 0) { //if 3.2
$empoemp = 'Empleado';
$result = mysqli_query($link, "select a.pkyAccountsReceivable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldCollectionDate as fecha_cobro, b.fldName as concepto, 
c.fldName as concepto_ingreso, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empresa, a.codCurrency as cod_moneda, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as deuda, a.fldBalance as saldo, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c, agnPerson e, actCurrency f
where
$valor_contexto_det 
a.pkyAccountsReceivable = '$cod_cxc' and
a.codConcept = b.pkyAccountsReceivableConcept and
a.codIncomeConcept = c.pkyIncomeConcept and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1;");

$result2 = mysqli_query($link, "select a.pkyCollection as cod_cobro, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, c.fldName as tipo, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empresa, a.codCurrency as cod_moneda, f.fldName as moneda, a.codExchangeRate as cod_tc, b.fldTotal as total, a.fldParentComponent as papa, a.cosComponentKey as cod_papa 
from
colCollection a, colCollectionDetail b, colCollectionRoute c, agnPerson e, actCurrency f
where
$valor_contexto_det 
a.codRoute = c.pkyCollectionRoute and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
b.codAccountsReceivable = '$cod_cxc' and
a.pkyCollection = b.codCollection and
a.sysEnabled = 1 and a.sysAuthorized = 1

union all

select a.pkyPayment as cod_cobro, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, 
'Liquidacion' as tipo, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empresa, a.codCurrency as cod_moneda, f.fldName as moneda, a.codExchangeRate as cod_tc, b.fldTotal as total, a.fldParentComponent as papa, a.cosComponentKey as cod_papa 
from
payPayment a, payPaymentDetail b, agnPerson e, actCurrency f
where
$valor_contexto_det 
a.pkyPayment = b.codPayment and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
b.codAccountsReceivable = '$cod_cxc' and
a.sysEnabled = 1 and a.sysAuthorized = 1
order by fecha;");
}//fin if 3.2
?>
<?php if($otra_moneda_det != 0){ 
$mostrar_txt_moneda = $nom_otra_moneda;
} else {
$mostrar_txt_moneda = 'Moneda original';	
}
?>
<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_det; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Detalle Cuenta por Cobrar</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>Cod. CxC: </b><?php echo $cod_cxc; ?></TD>
        <TD width="33%"><b>Contexto: </b><?php echo $nom_contexto_det; ?></TD>
        <TD width="33%"><b>Expresado en: </b><?php echo $mostrar_txt_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"><b>Nro. Doc.</b></TD>
	<TD align="center" width="65"><b>Fecha</b></TD>
    <TD align="center" width="65"><b>Fecha de Cobro</b></TD>
    <TD align="center"><b>Concepto</b></TD>
    <TD align="center"><b>Concepto de Ingreso</b></TD>
    <TD align="center"><b><?php echo $empoemp; ?></b></TD>
    <TD align="center"><b>Glosa</b></TD>
	<TD align="center"><b>Gen. Por</b></TD>
    <TD align="center"><b>Mon.</b></TD>
    <TD align="center"><b>Monto Inicial</b></TD>
    <TD align="center"><b>Saldo</b></TD>
    <?php if($otra_moneda_det != 0){ ?>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Saldo en <?php echo $nom_otra_moneda; ?></b></TD>
    <?php } ?>
    </TR>
    </thead>
    <tbody class="marcafila">
<?php
while ($row = mysqli_fetch_array($result)) { //while
		$codigo_det = $row['codigo'];
		
		//para enlazar al que genero el asiento
		$papa_componente = $row['papa'];
		$papa_codigo = $row['cod_papa'];
		$usuario = $usuario_det;
		include "../../generadopor.php";
	
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo_det&user=$usuario_det' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//cod
		printf("<td align='center'>%s</td>", $row['nrodoc']);//numero documento
		printf("<td align='right'>%s</td>", $row['fecha']);//fecha
		printf("<td align='right'>%s</td>", $row['fecha_cobro']);//fecha de cobro
		printf("<td>%s</td>", $row['concepto']);//concepto
		printf("<td>%s</td>", $row['concepto_ingreso']);//concepto del ingreso
		printf("<td>%s</td>", $row['empresa']);//empresa o empleado
		printf("<td>%s</td>", $row['glosa']);//glosa
		printf("<td align='center'>%s</td>", $papa_enlace);//enlace papa
		printf("<td>%s</td>", $row['moneda']);//moneda
		$nom_moneda_cabecera = $row['moneda'];
		printf("<td align='right'>%s</td>", number_format($row['deuda'],$n_decimales));//deuda
			$cod_moneda_cxc = $row['cod_moneda'];
		if($otra_moneda_det != 0){ //if otra moneda
			if($row['cod_moneda'] == $otra_moneda_det){ //if monedas iguales
				$tasa_tc = 1;
			}//fin if monedas iguales 
			else { //else monedas iguales
		 	$i = 1;
		 	while($i <= $fin_om){ //while busca tasa tc
				if($row['cod_moneda'] == $matriz_tc [$i][3] && $otra_moneda_det == $matriz_tc [$i][2]){ //if linea tc
					$tasa_tc = $matriz_tc [$i][4];
				}//fin if linea tc
			$i++;
			}//fin while busca tasa tc
		 }//fin if monedas iguales
		printf("<td align='right'>%s</td>", number_format($row['saldo'],$n_decimales));//saldo
		printf("<td align='right'>%s</td>", number_format($tasa_tc,$n_tipocambio));// tasa de tc
		printf("<td align='right'>%s</td></tr>", number_format($row['saldo'] * $tasa_tc,$n_decimales));//saldo otra moneda
		}//fin if otra moneda 
		else {	//else otra moneda
		printf("<td align='right'>%s</td></tr>", number_format($row['saldo'],$n_decimales));//saldo
		}//fin else otra moneda
	}//fin while
	mysqli_free_result($result);
?>
</tbody>
</table>
<p>&nbsp;</p>
<table border="1" align="center" cellpadding="2" width="100%">
 	<thead>
    <TR> 
 	<TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Nro. Doc.</b></TD>
	<TD align="center" width="65"><b>Fecha</b></TD>
    <TD align="center"><b>V&iacute;a de Cobro</b></TD>
    <TD align="center"><b><?php echo $empoemp; ?></b></TD>
    <TD align="center"><b>Gen. Por</b></TD>
    <TD align="center"><b>Mon. de Cobro</b></TD>
    <TD align="center"><b>Tasa de Cobro</b></TD>
    <TD align="center"><b>Monto en Mon. de Cobro</b></TD>
	<TD align="center"><b>Total Cobrado en <?php echo $nom_moneda_cabecera; ?></b></TD>
    <?php if($otra_moneda_det != 0){ ?>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Total en <?php echo $nom_otra_moneda; ?></b></TD>
    <?php } ?>
    </TR>
    </thead>
    <tbody class="marcafila">
<?php
    $color = 2;
	$suma_cant = 0;
	$suma_cant_2 = 0;
	$suma_cant_om = 0;
	while ($row = mysqli_fetch_array($result2)) { //while
	
		$cod_cobro = $row['cod_cobro'];
	    $cod_moneda_cobro = $row['cod_moneda'];
	    $nrodoc2 = $row['nrodoc'];// nro doc
	    $fecha2 = $row['fecha'];//fecha
	    $tipo2 = $row['tipo'];//tipo
	    $empresa2 = $row['empresa'];//empresa o empleado
	    $moneda2 = $row['moneda'];//moneda de cobro
	    $total2 = $row['total'];//total cobro 
		$tc2 = $row['cod_tc'];//total cobro 
		
		//para enlazar al que genero el asiento
		$papa_componente = $row['papa'];
		$papa_codigo = $row['cod_papa'];
		$usuario = $usuario_det;
		include "../../generadopor.php";
		
		if($cod_moneda_cxc == $cod_moneda_cobro) { //if moneda cabecera igual al detalle	
	    	$tasa_fecha_cobro = 1;
	 	if($color % 2 == 0) { //if color
			if($tipo2 == 'Liquidacion') {//if liquidacion
				printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePayments/VoucherPayment/index.php?pkyPayment=$cod_cobro&user=$usuario_det' title='Nota de Pago'>%s</a></td>", $cod_cobro);//codigo
			} else {//else liquidacion
				printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleCollections/VoucherCollection/index.php?pkyCollection=$cod_cobro&user=$usuario_det' title='Nota de Cobro'>%s</a></td>", $cod_cobro);//codigo
			}//fin if liquidacion
		} else {
			if($tipo2 == 'Liquidacion') {//if liquidacion
				printf("<tr><td align='center'><a href='../../ModulePayments/VoucherPayment/index.php?pkyPayment=$cod_cobro&user=$usuario_det' title='Nota de Pago'>%s</a></td>", $cod_cobro);//codigo
			} else {//else liquidacion
				printf("<tr><td align='center'><a href='../../ModuleCollections/VoucherCollection/index.php?pkyCollection=$cod_cobro&user=$usuario_det' title='Nota de Cobro'>%s</a></td>", $cod_cobro);//codigo
			}//fin if liquidacion
		}//cierra if color
		printf("<td align='center'>%s</td>", $nrodoc2);//nro doc
		printf("<td align='right'>%s</td>",  $fecha2);//fecha
		printf("<td>%s</td>", $tipo2);//tipo
		printf("<td>%s</td>", $empresa2);//empresa o empleado
		printf("<td align='center'>%s</td>", $papa_enlace);//generado por
		printf("<td>%s</td>", $moneda2);//moneda de cobro
		printf("<td align='right'>%s</td>", number_format(1,$n_tipocambio));//tasa de cobro
		printf("<td align='right'>%s</td>", number_format($total2,$n_decimales));//monto cobrado
		if($otra_moneda_det != 0){ //if otra moneda
		
			if($cod_moneda_cobro == $otra_moneda_det){ //if monedas iguales
				$tasa_tc = 1;
			}//fin if monedas iguales 
			else { //else monedas iguales
			//valores de conversion tipo de cambio fecha de cobro
			
$resulttc = mysqli_query($link, "SELECT pkyExchangeRateDetail as codigotc, codBaseCurrency as monedabase, codTargetCurrency as monedacambio, fldRate as tc
from
actExchangeRateDetail
where
codExchangeRate = '$tc2' and
codBaseCurrency = '$otra_moneda_det' and
sysEnabled = 1 AND sysAuthorized = 1;");
$matriz_tc_fecha_cobro = array();
$i = 1;
while ($row = mysqli_fetch_array($resulttc)) {
	$matriz_tc_fecha_cobro [$i][1] = $row['codigotc'];//codigo tc
	$matriz_tc_fecha_cobro [$i][2] = $row['monedabase'];//codigo moneda base
	$matriz_tc_fecha_cobro [$i][3] = $row['monedacambio'];//codigo moneda cambio
	$matriz_tc_fecha_cobro [$i][4] = $row['tc'];//valor a multiplicar del tc 
	$i++;
	}
	mysqli_free_result($resulttc);

$fin_om_fp = $i - 1;
		 	$i = 1;
		 	while($i <= $fin_om_fp){ //while busca tasa tc
			
				if($cod_moneda_cobro == $matriz_tc_fecha_cobro [$i][3] && $otra_moneda_det == $matriz_tc_fecha_cobro [$i][2]){ //if linea tc
					$tasa_tc = $matriz_tc_fecha_cobro [$i][4];
					//echo 'tasa de cambio '.$tasa_tc;
				}//fin if linea tc
			$i++;
			}//fin while busca tasa tc
		 }//fin if monedas iguales
		printf("<td align='right'>%s</td>", number_format($total2,$n_decimales));//total
		printf("<td align='right'>%s</td>", number_format($tasa_tc,$n_tipocambio));// tasa de tc
		printf("<td align='right'>%s</td></tr>", number_format($total2 * $tasa_tc,$n_decimales));//total otra moneda
		}//fin if otra moneda 
		else {	//else otra moneda
		printf("<td align='right'>%s</td></tr>", number_format($total2,$n_decimales));//total
		}//fin else otra moneda
		
		}//fin if moneda cabecera igual al detalle
	  else { //else moneda cabecera igual al detalle
	  
	    $resultom = mysqli_query($link, "SELECT pkyExchangeRateDetail as codigotc, codBaseCurrency as monedabase, codTargetCurrency as monedacambio, fldRate as tasa
from
actExchangeRateDetail
where
codExchangeRate = '$tc2' and
codBaseCurrency = '$cod_moneda_cobro' and
codTargetCurrency = '$cod_moneda_cxc' and
sysEnabled = 1 AND sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($resultom)) {
			//$cod_moneda = $row['cod_moneda'];
			$tasa_fecha_cobro = $row['tasa'];
			}
			mysqli_free_result($resultom);
	  
	    $resultom2 = mysqli_query($link, "SELECT pkyExchangeRateDetail as codigotc, codBaseCurrency as monedabase, codTargetCurrency as monedacambio, fldRate as tasa
from
actExchangeRateDetail
where
codExchangeRate = '$tc2' and
codBaseCurrency = '$otra_moneda_det' and
codTargetCurrency = '$cod_moneda_cxc' and
sysEnabled = 1 AND sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($resultom2)) {
			//$cod_moneda = $row['cod_moneda'];
			$tasa_fecha_cobro2 = $row['tasa'];
			}
			mysqli_free_result($resultom2);
	  	  
	  if($color % 2 == 0) { //if color
			if($tipo2 == 'Liquidacion') {//if liquidacion
				printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePayments/VoucherPayment/index.php?pkyPayment=$cod_cobro&user=$usuario_det' title='Nota de Pago'>%s</a></td>", $cod_cobro);//codigo
			} else {//else liquidacion
				printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleCollections/VoucherCollection/index.php?pkyCollection=$cod_cobro&user=$usuario_det' title='Nota de Cobro'>%s</a></td>", $cod_cobro);//codigo
			}//fin if liquidacion
		} else {
			if($tipo2 == 'Liquidacion') {//if liquidacion
				printf("<tr><td align='center'><a href='../../ModulePayments/VoucherPayment/index.php?pkyPayment=$cod_cobro&user=$usuario_det' title='Nota de Pago'>%s</a></td>", $cod_cobro);//codigo
			} else {//else liquidacion
				printf("<tr><td align='center'><a href='../../ModuleCollections/VoucherCollection/index.php?pkyCollection=$cod_cobro&user=$usuario_det' title='Nota de Cobro'>%s</a></td>", $cod_cobro);//codigo
			}//fin if liquidacion
		}//cierra if color
		printf("<td align='center'>%s</td>", $nrodoc2);//nro doc
		printf("<td align='right'>%s</td>", $fecha2);//fecha
		printf("<td>%s</td>", $tipo2);//tipo
		printf("<td>%s</td>", $empresa2);//empresa o empleado
		printf("<td align='center'>%s</td>", $papa_enlace);//generado por
		printf("<td>%s</td>", $moneda2);//moneda de cobro
		printf("<td align='right'>%s</td>", number_format($tasa_fecha_cobro,$n_tipocambio));//tasa de cobro
		printf("<td align='right'>%s</td>", number_format($total2 * $tasa_fecha_cobro,$n_decimales));//monto cobrado
		
		if($otra_moneda_det != 0){ //if otra moneda
			if($cod_moneda_cxc == $otra_moneda_det){ //if monedas iguales
				$tasa_tc = 1;
			}//fin if monedas iguales 
			else { //else monedas iguales
		 		$tasa_tc = $tasa_fecha_cobro2;
		 }//fin else monedas iguales
		 printf("<td align='right'>%s</td>", number_format($total2,$n_decimales));//total	
		 printf("<td align='right'>%s</td>", number_format($tasa_tc,$n_tipocambio));// tasa de tc
		 printf("<td align='right'>%s</td></tr>", number_format($total2 * $tasa_tc,$n_decimales));//total otra moneda
		}//fin if otra moneda 
		else {	//else otra moneda
		printf("<td align='right'>%s</td></tr>", number_format($total2,$n_decimales));//total
		}//fin else otra moneda
	  }//fin else moneda cabecera igual al detalle
		$suma_cant = $suma_cant + $total2;
		$suma_cant_om = $suma_cant_om + $total2 * $tasa_tc;
		$suma_cant_2 = $suma_cant_2 + $total2 * $tasa_fecha_cobro;
		$color++;
	}//fin while
	mysqli_free_result($result2);
?>
<?php if($otra_moneda_det != 0){ ?>
<tr BGCOLOR='#ffcccc'><td colspan="8"><b>Total Cobros</b></td>
<td align="right"><?php //printf("<b>%s</b>", number_format($suma_cant_2,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant,$n_decimales)); ?></td>
<td>&nbsp;</td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_om,$n_decimales)); ?></td>
</tr>
<?php } else { ?>
<tr BGCOLOR='#ffcccc'><td colspan="8"><b>Total Cobros</b></td>
<td align="right"><?php //printf("<b>%s</b>", number_format($suma_cant_2,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant,$n_decimales)); ?></td>
</tr>
<?php } ?>

</tbody>
</table>
<?php

} //fin if 3
?>

</body>
</html>
