<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Estado de Cuentas</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";
include "../../class.nroenletraver2.php"; //solo sirve para detail2.php

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$nombre=$_POST["nombre"];
$mostrar=$_POST["mostrar"];
$verdetalle=$_POST["verdetalle"];
$orden_detalle=$_POST["orden_detalle"];
$venta_detalle=$_POST["venta_detalle"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
$otra_moneda=$_POST["otra_moneda"];
$cod_contexto=$_POST["contexto"];
$con_cxp=$_POST["con_cxp"];
$con_cxc=$_POST["con_cxc"];
$quitar=$_POST["quitar"];
$cod_canal=$_POST["cod_canal"];
$usuario=$_POST["usuario"];

$fechainidet=$_GET["fechainidet"];
$fechafindet=$_GET["fechafindet"];
$nombredet=$_GET["nombredet"];
$ordendet=$_GET["ordendet"];
$ventadetalledet=$_GET["ventadetalledet"];
$totaldet=$_GET["totaldet"];
$empresa=$_GET["empresa"];
$usuario_det=$_GET["usuario_det"];
$otra_moneda_det=$_GET["otra_moneda_det"];
$cod_contexto_det=$_GET["contexto_det"];
$con_cxp_det=$_GET["con_cxp_det"];
$con_cxc_det=$_GET["con_cxc_det"];
//$cod_canal_det=$_GET["cod_canal_det"];
//$n_decimales = 6;
//echo $nombre;

//verificar el tipo de usuario 3 es normal (no muestra CxP)
$resultuser = mysqli_query($link, "select pkyUser as cod_user, codType as cod_tipo 
from accUser
where fldLogin = '$usuario' and
sysEnabled = 1 and sysAuthorized = 1;");
		
while ($row = mysqli_fetch_array($resultuser)) {
$cod_usuario = $row['cod_user'];
$cod_tipo_usuario = $row['cod_tipo'];
}
mysqli_free_result($resultuser);

if($cod_tipo_usuario == 3) { 
	$valor_ver_cxp = ' a.sysVisible <> 1 and ';
	$valor_ver_cxp2 = ' c.sysVisible <> 1 and ';
} else {
	$valor_ver_cxp = ' ';
	$valor_ver_cxp2 = ' ';
}

//verificar el tipo de usuario 3 es normal (no muestra CxP) para el detalle
$resultuser = mysqli_query($link, "select pkyUser as cod_user, codType as cod_tipo 
from accUser
where fldLogin = '$usuario_det' and
sysEnabled = 1 and sysAuthorized = 1;");
		
while ($row = mysqli_fetch_array($resultuser)) {
$cod_usuario_det = $row['cod_user'];
$cod_tipo_usuario_det = $row['cod_tipo'];
}
mysqli_free_result($resultuser);

if($cod_tipo_usuario_det == 3) { 
	$valor_ver_cxp_det = ' a.sysVisible <> 1 and ';
	$valor_ver_cxp_det2 = ' g.sysVisible <> 1 and ';
} else {
	$valor_ver_cxp_det = ' ';
	$valor_ver_cxp_det2 = ' ';
}

?>
<body>
<?php

//Inicio reporte estado de cuentas 

//controlar si la opcion es Quitar Títulos y Columnas en el Detalle de CxC, marcar Detalle Ordenado por Fecha
if($quitar == 1){
	$orden_detalle = 0;	
}

 //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 canal
if ($cod_canal == 0) { // if 2.0.0
		$nom_canal = 'Todos';
		$valor_canal = ' ';
		$valor_canal2 = ' ';
	} else {
		//nombre canal
		$result22 = mysqli_query($link, "select pkyChannel as cod_canal, fldName as canal 
								from agnChannel
								where
								pkyChannel = '$cod_canal' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result22)) {
			$nom_canal = $row['canal'];
		}
		mysqli_free_result($result22);
		
		$valor_canal = ' codChannel = ' .$cod_canal. ' and ';
		$valor_canal2 = ' p.codChannel = ' .$cod_canal. ' and ';
	} //fin if 2.0.0	

//nombre concepto de CxP
if ($con_cxp == 0) { // if 2.0
		$nom_con_cxp = 'Todos';
		$valor_con_cxp = ' ';
		$valor_con_cxp2 = ' ';
	} else {
		if ($con_cxp == -1) { // if 2.0.1
		$nom_con_cxp = 'Solo Concepto de CxC';
		} else { // else if 2.0.1
		//nombre con cxp
		$result2 = mysqli_query($link, "select pkyAccountsPayableConcept as cod_con_cxp, fldName as concepto 
								from payAccountsPayableConcept
								where
								pkyAccountsPayableConcept = '$con_cxp' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result2)) {
			$nom_con_cxp = $row['concepto'];
		}
		mysqli_free_result($result2);
		} //fin if 2.0.1
		$valor_con_cxp = ' a.codConcept = ' .$con_cxp. ' and ';
		$valor_con_cxp2 = ' c.codConcept = ' .$con_cxp. ' and ';
	} //fin if 2.0	
	
//nombre concepto de CxC
if ($con_cxc == 0) { // if 2.1
		$nom_con_cxc = 'Todos';
		$valor_con_cxc = ' ';
		$valor_con_cxc2 = ' ';
	} else {
		if ($con_cxc == -1) { // if 2.1.1
		$nom_con_cxc = 'Solo Concepto de CxP';
		} else { // else if 2.1.1
		//nombre con cxc
		$result3 = mysqli_query($link, "select pkyAccountsReceivableConcept as cod_con_cxc, fldName as concepto 
								from colAccountsReceivableConcept
								where
								pkyAccountsReceivableConcept = '$con_cxc' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result3)) {
			$nom_con_cxc = $row['concepto'];
		}
		mysqli_free_result($result3);
		} //fin if 2.1.1
		$valor_con_cxc = ' a.codConcept = ' .$con_cxc. ' and ';
		$valor_con_cxc2 = ' c.codConcept = ' .$con_cxc. ' and ';
	} //fin if 2.1	

//contexto 
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 ';	
}
	
//Inicio muestra empresas
if($rep == 8 && $tipo == 1) { // if 2

	if ($nombre == -1) { // if 2.2
		
		$nom_orden = 'Nombre';
		$nom_cuenta = 'Empresas';
		
		//listado de empresas
		$result = mysqli_query($link, "select pkyCompany as codigo, fldName as empresa
from agnCompany
where 
$valor_canal 
sysEnabled = 1 and sysAuthorized = 1
order by empresa;");
		
		//inicio calculo saldo inicial
		//CxP empresas
		$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and 
a.fldDate < '$fechaini' and 
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//CxC empresas
		$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Pagos empresas
		$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Cobros empresas
		$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros,
 otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
 otra.cod_moneda_cxc as cod_moneda_cxc, 
 otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
 otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");		

$matriz_salini = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_salini [$i][1] = $row['codigo'];
	$matriz_salini [$i][2] = $row['empresa'];
	$matriz_salini [$i][3] = 0;
	$matriz_salini [$i][4] = 0;
	$matriz_salini [$i][5] = 0;
	$matriz_salini [$i][6] = 0;
	$matriz_salini [$i][7] = 0;
	$matriz_salini [$i][8] = 0;
	$matriz_salini [$i][9] = 0;
	$matriz_salini [$i][10] = 0;
	$matriz_salini [$i][11] = 0;
	$matriz_salini [$i][12] = 0;
	$matriz_salini [$i][13] = 0;
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);
//CxP 
while ($row = mysqli_fetch_array($result2)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][3] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][4] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][5] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][6] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
	$reg_cxc = $matriz_salini [$i][4];
	$reg_cxp = $matriz_salini [$i][3];
	$reg_pagos = $matriz_salini [$i][5];
	$reg_cobros = $matriz_salini [$i][6];
	$matriz_salini [$i][7] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
	$i++;
}
	
//fin calculo saldo inicial

//inicio calculo registros
		//CxP empresas
		$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//CxC empresas
		$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Pagos empresas
		$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Cobros empresas
		$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros,
 otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
 otra.cod_moneda_cxc as cod_moneda_cxc, 
 otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
 otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");	

//CxP
while ($row = mysqli_fetch_array($result2)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][8] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][9] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][10] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][11] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
	$reg_cxc = $matriz_salini [$i][9];
	$reg_cxp = $matriz_salini [$i][8];
	$reg_pagos = $matriz_salini [$i][10];
	$reg_cobros = $matriz_salini [$i][11];
	$matriz_salini [$i][12] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
	$reg_salini = $matriz_salini [$i][7];
	$reg_periodo = $matriz_salini [$i][12];
	$matriz_salini [$i][13] = $reg_salini + $reg_periodo;
	$i++;
}
	
//fin calculo registros
		?>
<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>Estado de Cuentas</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>Cuenta: </b><?php echo $nom_cuenta; ?></TD>
        <TD width="25%"><b>Canal (Solo para Empresas): </b><?php echo $nom_canal; ?></TD>
        <TD width="25%"><b>Concepto de CxP: </b><?php echo $nom_con_cxp; ?></TD>
        <TD width="25%"><b>Concepto de CxC: </b><?php echo $nom_con_cxc; ?></TD>
        </TR>
        <TR>
        <TD><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        <TD><b>Ordenado por: </b><?php echo $nom_orden; ?></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"><b>Empresa</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en <?php echo $nom_otra_moneda; ?></b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">

  <?php
  $color = 2;
  $suma_total_cxc = 0;
  $suma_total_cobros = 0;
  $suma_total_cxp = 0;
  $suma_total_pagos = 0;
  $suma_total_periodo = 0;
  $suma_total = 0;
  $j = 1;
  while ($j <= $fin) { //while
	
	if($mostrar == 0) { //if mostrar 0
	
	while($matriz_salini [$j][13] == 0 && $j <= $fin)
	{ $j++; }
	
	} else { //else if mostrar
	
	while($matriz_salini [$j][7] == 0 && $matriz_salini [$j][9] == 0 && $matriz_salini [$j][11] == 0 && $matriz_salini [$j][8] == 0 && $matriz_salini [$j][10] == 0 && $matriz_salini [$j][12] == 0 && $j <= $fin)
	{ $j++; }
	
	} //fin if mostrar 0
	
	if($j > $fin) { //if ultimo 0
	} else { //else ultimo 0
	
	if($color % 2 == 0) { //if color
	$codigo = $matriz_salini [$j][1];
	$totaldet = $matriz_salini [$j][7];
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_salini [$j][1]);//cod
	printf("<td align='left'>%s</td>", $matriz_salini [$j][2]);//empresa
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][7],$n_decimales));//saldo inicial
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][9],$n_decimales));// total CxC
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][11],$n_decimales));// total Cobros
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][8],$n_decimales));// total CxP
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][10],$n_decimales));// total Pagos
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][12],$n_decimales));// total Periodo
	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=1&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=1;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empresa</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	
	} //fin if ver detalle 0
	
	} else { //else if color
	$codigo = $matriz_salini [$j][1];
	$totaldet = $matriz_salini [$j][7];
	printf("<tr><td align='center'>%s</td>", $matriz_salini [$j][1]);//cod
	printf("<td align='left'>%s</td>", $matriz_salini [$j][2]);//empresa
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][7],$n_decimales));//saldo inicial
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][9],$n_decimales));// total CxC
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][11],$n_decimales));// total Cobros
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][8],$n_decimales));// total CxP
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][10],$n_decimales));// total Pagos
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][12],$n_decimales));// total Periodo
	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=1&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=1;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empresa</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	
	} //fin if ver detalle 0
	
	} //cierra if color
    
	} //cierra else ultimo 0
	$suma_total_cxc = $suma_total_cxc + $matriz_salini [$j][9];
    $suma_total_cobros = $suma_total_cobros + $matriz_salini [$j][11];
    $suma_total_cxp = $suma_total_cxp + $matriz_salini [$j][8];
    $suma_total_pagos = $suma_total_pagos + $matriz_salini [$j][10];
    $suma_total_periodo = $suma_total_periodo + $matriz_salini [$j][12];
    $suma_total = $suma_total + $matriz_salini [$j][13];
	$color++;
    $j++;
}//fin while


?>
<tr BGCOLOR='#ffcccc'><td colspan="3"><b>Total Cuentas Empresas</b>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxc,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cobros,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxp,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_pagos,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_periodo,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total,$n_decimales)); ?>
</td></tr>
</tbody>
</table>
<?php

	} //fin if 2.2
	
//inicio muestra empleados
	if ($nombre == -2) { // if 2.3
		
	$nom_orden = 'Nombre';
	$nom_cuenta = 'Empleados';
	
	//listado de empleados
	$result = 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
sysEnabled = 1 and sysAuthorized = 1
order by empleado;");
	
	//inicio calculo saldo inicial
	//CxP empleados
	$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//CxC empleados
	$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Pagos empleados
	$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Cobros empleados
	$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros, 
otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
otra.cod_moneda_cxc as cod_moneda_cxc,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");			

$matriz_salini2 = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
$matriz_salini2 [$i][1] = $row['codigo'];
$matriz_salini2 [$i][2] = $row['empleado'];
$matriz_salini2 [$i][3] = 0;
$matriz_salini2 [$i][4] = 0;
$matriz_salini2 [$i][5] = 0;
$matriz_salini2 [$i][6] = 0;
$matriz_salini2 [$i][7] = 0;
$matriz_salini2 [$i][8] = 0;
$matriz_salini2 [$i][9] = 0;
$matriz_salini2 [$i][10] = 0;
$matriz_salini2 [$i][11] = 0;
$matriz_salini2 [$i][12] = 0;
$matriz_salini2 [$i][13] = 0;
$i++;
}
$fin = $i - 1;
mysqli_free_result($result);
//CxP
while ($row = mysqli_fetch_array($result2)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][3] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][4] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][5] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][6] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
$reg_cxc = $matriz_salini2 [$i][4];
$reg_cxp = $matriz_salini2 [$i][3];
$reg_pagos = $matriz_salini2 [$i][5];
$reg_cobros = $matriz_salini2 [$i][6];
$matriz_salini2 [$i][7] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
$i++;
}

//fin calculo saldo inicial

//inicio calculo registros
	//CxP empleados
	$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//CxC empleados
	$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Pagos empleados
	$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Cobros empleados
	$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros, 
otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
otra.cod_moneda_cxc as cod_moneda_cxc,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");			

//CxP
while ($row = mysqli_fetch_array($result2)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][8] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][9] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][10] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][11] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
$reg_cxc = $matriz_salini2 [$i][9];
$reg_cxp = $matriz_salini2 [$i][8];
$reg_pagos = $matriz_salini2 [$i][10];
$reg_cobros = $matriz_salini2 [$i][11];
$matriz_salini2 [$i][12] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
$reg_salini2 = $matriz_salini2 [$i][7];
$reg_periodo = $matriz_salini2 [$i][12];
$matriz_salini2 [$i][13] = $reg_salini2 + $reg_periodo;
$i++;
}

//fin calculo registros
	?>
<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>Estado de Cuentas</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>Cuenta: </b><?php echo $nom_cuenta; ?></TD>
    <TD width="25%"><b>Concepto de CxP: </b><?php echo $nom_con_cxp; ?></TD>
    <TD width="25%"><b>Concepto de CxC: </b><?php echo $nom_con_cxc; ?></TD>
    <TD width="25%"><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>Ordenado por: </b><?php echo $nom_orden; ?></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"><b>Empleado</b></TD>
<TD align="center"><b>Saldo Inicial</b></TD>
<TD align="center"><b>Total CxC</b></TD>
<TD align="center"><b>Total Cobros</b></TD>
<TD align="center"><b>Total CxP</b></TD>
<TD align="center"><b>Total Pagos</b></TD>
<TD align="center"><b>Total Periodo</b></TD>
<TD align="center"><b>Total en <?php echo $nom_otra_moneda; ?></b></TD>
</TR>
</thead>
<tbody class="marcafila">

<?php
$color = 2;
$suma_total_cxc = 0;
$suma_total_cobros = 0;
$suma_total_cxp = 0;
$suma_total_pagos = 0;
$suma_total_periodo = 0;
$suma_total = 0;
$j = 1;
while ($j <= $fin) { //while

if($mostrar == 0) { //if mostrar 0
	
	while($matriz_salini2 [$j][13] == 0 && $j <= $fin)
	{ $j++; }
	
} else { //else if mostrar
	
	while($matriz_salini2 [$j][7] == 0 && $matriz_salini2 [$j][9] == 0 && $matriz_salini2 [$j][11] == 0 && $matriz_salini2 [$j][8] == 0 && $matriz_salini2 [$j][10] == 0 && $matriz_salini2 [$j][12] == 0 && $j <= $fin)
{ $j++; }
	
} //fin if mostrar 0


if($j > $fin) { //if ultimo 0
} else { //else ultimo 0

if($color % 2 == 0) { //if color
$codigo = $matriz_salini2 [$j][1];
$totaldet = $matriz_salini2 [$j][7];
printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_salini2 [$j][1]);//cod
printf("<td align='left'>%s</td>", $matriz_salini2 [$j][2]);//empleado
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][7],$n_decimales));//saldo inicial
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][9],$n_decimales));// total CxC
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][11],$n_decimales));// total Cobros
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][8],$n_decimales));// total CxP
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][10],$n_decimales));// total Pagos
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][12],$n_decimales));// total Periodo

	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=0&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=0;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empleado</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	} //fin if ver detalle 0

} else {
$codigo = $matriz_salini2 [$j][1];
$totaldet = $matriz_salini2 [$j][7];
printf("<tr><td align='center'>%s</td>", $matriz_salini2 [$j][1]);//cod
printf("<td align='left'>%s</td>", $matriz_salini2 [$j][2]);//empleado
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][7],$n_decimales));//saldo inicial
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][9],$n_decimales));// total CxC
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][11],$n_decimales));// total Cobros
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][8],$n_decimales));// total CxP
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][10],$n_decimales));// total Pagos
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][12],$n_decimales));// total Periodo

	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=0&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=0;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empleado</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	} //fin if ver detalle 0

} //cierra if color

} //cierra else ultimo 0
$suma_total_cxc = $suma_total_cxc + $matriz_salini2 [$j][9];
$suma_total_cobros = $suma_total_cobros + $matriz_salini2 [$j][11];
$suma_total_cxp = $suma_total_cxp + $matriz_salini2 [$j][8];
$suma_total_pagos = $suma_total_pagos + $matriz_salini2 [$j][10];
$suma_total_periodo = $suma_total_periodo + $matriz_salini2 [$j][12];
$suma_total = $suma_total + $matriz_salini2 [$j][13];
$color++;
$j++;
}//fin while


?>
<tr BGCOLOR='#ffcccc'><td colspan="3"><b>Total Cuentas Empleados</b>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxc,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cobros,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxp,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_pagos,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_periodo,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total,$n_decimales)); ?>
</td></tr>
</tbody>
</table>
<?php

	} //fin if 2.3


//inicio Todos

	if ($nombre == 0) { // if 2.4
	
	
		$nom_orden = 'Nombre';
		$nom_cuenta = 'Todos';
		
		//listado de empresas
		$result = mysqli_query($link, "select pkyCompany as codigo, fldName as empresa
from agnCompany
where 
$valor_canal 
sysEnabled = 1 and sysAuthorized = 1
order by empresa;");
		
		//inicio calculo saldo inicial
		//CxP empresas
		$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p 
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//CxC empresas
		$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p 
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p 
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Pagos empresas
		$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p 
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p 
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Cobros empresas
		$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros,
 otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
 otra.cod_moneda_cxc as cod_moneda_cxc, 
 otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
 otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");		
	

$matriz_salini = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_salini [$i][1] = $row['codigo'];
	$matriz_salini [$i][2] = $row['empresa'];
	$matriz_salini [$i][3] = 0;
	$matriz_salini [$i][4] = 0;
	$matriz_salini [$i][5] = 0;
	$matriz_salini [$i][6] = 0;
	$matriz_salini [$i][7] = 0;
	$matriz_salini [$i][8] = 0;
	$matriz_salini [$i][9] = 0;
	$matriz_salini [$i][10] = 0;
	$matriz_salini [$i][11] = 0;
	$matriz_salini [$i][12] = 0;
	$matriz_salini [$i][13] = 0;
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);
//CxP
while ($row = mysqli_fetch_array($result2)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][3] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][4] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][5] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][6] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
	$reg_cxc = $matriz_salini [$i][4];
	$reg_cxp = $matriz_salini [$i][3];
	$reg_pagos = $matriz_salini [$i][5];
	$reg_cobros = $matriz_salini [$i][6];
	$matriz_salini [$i][7] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
	$i++;
}
	
//fin calculo saldo inicial

//inicio calculo registros
		//CxP empresas
		$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p 
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p 
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//CxC empresas
		$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Pagos empresas
		$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p 
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p 
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Cobros empresas
		$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros,
 otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
 otra.cod_moneda_cxc as cod_moneda_cxc, 
 otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
 otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");		
	

//CxP
while ($row = mysqli_fetch_array($result2)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][8] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][9] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][10] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini [$i][1]) {
		$i++;
	}
	$matriz_salini [$i][11] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
	$reg_cxc = $matriz_salini [$i][9];
	$reg_cxp = $matriz_salini [$i][8];
	$reg_pagos = $matriz_salini [$i][10];
	$reg_cobros = $matriz_salini [$i][11];
	$matriz_salini [$i][12] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
	$reg_salini = $matriz_salini [$i][7];
	$reg_periodo = $matriz_salini [$i][12];
	$matriz_salini [$i][13] = $reg_salini + $reg_periodo;
	$i++;
}
	
//fin calculo registros
		?>
<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>Estado de Cuentas</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>Cuenta: </b><?php echo $nom_cuenta; ?></TD>
        <TD width="25%"><b>Canal (Solo para Empresas): </b><?php echo $nom_canal; ?></TD>
        <TD width="25%"><b>Concepto de CxP: </b><?php echo $nom_con_cxp; ?></TD>
        <TD width="25%"><b>Concepto de CxC: </b><?php echo $nom_con_cxc; ?></TD>
        </TR>
        <TR>
        <TD><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        <TD><b>Ordenado por: </b><?php echo $nom_orden; ?></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"><b>Empresa / Empleado</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en <?php echo $nom_otra_moneda; ?></b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">

  <?php
  $color = 2;
  $suma_total_cxc = 0;
  $suma_total_cobros = 0;
  $suma_total_cxp = 0;
  $suma_total_pagos = 0;
  $suma_total_periodo = 0;
  $suma_total = 0;
  $j = 1;
  while ($j <= $fin) { //while
	
	if($mostrar == 0) { //if mostrar 0
	
	while($matriz_salini [$j][13] == 0 && $j <= $fin)
	{ $j++; }
	
	} else { //else if mostrar
	
	while($matriz_salini [$j][7] == 0 && $matriz_salini [$j][9] == 0 && $matriz_salini [$j][11] == 0 && $matriz_salini [$j][8] == 0 && $matriz_salini [$j][10] == 0 && $matriz_salini [$j][12] == 0 && $j <= $fin)
	{ $j++; }
	
	} //fin if mostrar 0
	
	
	if($j > $fin) { //if ultimo 0
	} else { //else ultimo 0
	
	if($color % 2 == 0) { //if color
	$codigo = $matriz_salini [$j][1];
	$totaldet = $matriz_salini [$j][7];
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_salini [$j][1]);//cod
	printf("<td align='left'>%s</td>", $matriz_salini [$j][2]);//empresa
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][7],$n_decimales));//saldo inicial
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][9],$n_decimales));// total CxC
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][11],$n_decimales));// total Cobros
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][8],$n_decimales));// total CxP
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][10],$n_decimales));// total Pagos
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][12],$n_decimales));// total Periodo
		
	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=1&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=1;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empresa</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	} //fin if ver detalle 0
	
	} else {
	$codigo = $matriz_salini [$j][1];
	$totaldet = $matriz_salini [$j][7];
	printf("<tr><td align='center'>%s</td>", $matriz_salini [$j][1]);//cod
	printf("<td align='left'>%s</td>", $matriz_salini [$j][2]);//empresa
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][7],$n_decimales));//saldo inicial
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][9],$n_decimales));// total CxC
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][11],$n_decimales));// total Cobros
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][8],$n_decimales));// total CxP
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][10],$n_decimales));// total Pagos
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][12],$n_decimales));// total Periodo
	
	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=1&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=1;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empresa</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	} //fin if ver detalle 0
	
	} //cierra if color
    
	} //cierra else ultimo 0
	$suma_total_cxc = $suma_total_cxc + $matriz_salini [$j][9];
    $suma_total_cobros = $suma_total_cobros + $matriz_salini [$j][11];
    $suma_total_cxp = $suma_total_cxp + $matriz_salini [$j][8];
    $suma_total_pagos = $suma_total_pagos + $matriz_salini [$j][10];
    $suma_total_periodo = $suma_total_periodo + $matriz_salini [$j][12];
    $suma_total = $suma_total + $matriz_salini [$j][13];
	$color++;
    $j++;
}//fin while


?>
<tr BGCOLOR='#ffcccc'><td colspan="3"><b>Total Cuentas Empresas</b>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxc,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cobros,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxp,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_pagos,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_periodo,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total,$n_decimales)); ?>
</td></tr>

<?php


		
		//listado de empleados
		$result = 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
sysEnabled = 1 and sysAuthorized = 1
order by empleado;");
		
		//inicio calculo saldo inicial
		//CxP empleados
		$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//CxC empleados
		$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Pagos empleados
		$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Cobros empleados
		$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros, 
otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
otra.cod_moneda_cxc as cod_moneda_cxc,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");		

$matriz_salini2 = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_salini2 [$i][1] = $row['codigo'];
	$matriz_salini2 [$i][2] = $row['empleado'];
	$matriz_salini2 [$i][3] = 0;
	$matriz_salini2 [$i][4] = 0;
	$matriz_salini2 [$i][5] = 0;
	$matriz_salini2 [$i][6] = 0;
	$matriz_salini2 [$i][7] = 0;
	$matriz_salini2 [$i][8] = 0;
	$matriz_salini2 [$i][9] = 0;
	$matriz_salini2 [$i][10] = 0;
	$matriz_salini2 [$i][11] = 0;
	$matriz_salini2 [$i][12] = 0;
	$matriz_salini2 [$i][13] = 0;
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);
//CxP
while ($row = mysqli_fetch_array($result2)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini2 [$i][1]) {
		$i++;
	}
	$matriz_salini2 [$i][3] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini2 [$i][1]) {
		$i++;
	}
	$matriz_salini2 [$i][4] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini2 [$i][1]) {
		$i++;
	}
	$matriz_salini2 [$i][5] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini2 [$i][1]) {
		$i++;
	}
	$matriz_salini2 [$i][6] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
	$reg_cxc = $matriz_salini2 [$i][4];
	$reg_cxp = $matriz_salini2 [$i][3];
	$reg_pagos = $matriz_salini2 [$i][5];
	$reg_cobros = $matriz_salini2 [$i][6];
	$matriz_salini2 [$i][7] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
	$i++;
}
	
//fin calculo saldo inicial

//inicio calculo registros
		//CxP empleados
		$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//CxC empleados
		$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Pagos empleados
		$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
		//suma Cobros empleados
		$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros, 
otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
otra.cod_moneda_cxc as cod_moneda_cxc,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");		

//CxP
while ($row = mysqli_fetch_array($result2)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini2 [$i][1]) {
		$i++;
	}
	$matriz_salini2 [$i][8] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini2 [$i][1]) {
		$i++;
	}
	$matriz_salini2 [$i][9] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini2 [$i][1]) {
		$i++;
	}
	$matriz_salini2 [$i][10] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
	$i = 1;
	$cod_emp = $row['cod_cli'];
	while ($cod_emp <> $matriz_salini2 [$i][1]) {
		$i++;
	}
	$matriz_salini2 [$i][11] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
	$reg_cxc = $matriz_salini2 [$i][9];
	$reg_cxp = $matriz_salini2 [$i][8];
	$reg_pagos = $matriz_salini2 [$i][10];
	$reg_cobros = $matriz_salini2 [$i][11];
	$matriz_salini2 [$i][12] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
	$reg_salini2 = $matriz_salini2 [$i][7];
	$reg_periodo = $matriz_salini2 [$i][12];
	$matriz_salini2 [$i][13] = $reg_salini2 + $reg_periodo;
	$i++;
}
	
//fin calculo registros
		?>
 	

  <?php
  $color = 2;
  $suma_total_cxc2 = 0;
  $suma_total_cobros2 = 0;
  $suma_total_cxp2 = 0;
  $suma_total_pagos2 = 0;
  $suma_total_periodo2 = 0;
  $suma_total2 = 0;
  $j = 1;
  while ($j <= $fin) { //while
	
	if($mostrar == 0) { //if mostrar 0
	
	while($matriz_salini2 [$j][13] == 0 && $j <= $fin)
	{ $j++; }
	
	} else { //else if mostrar
	
	while($matriz_salini2 [$j][7] == 0 && $matriz_salini2 [$j][9] == 0 && $matriz_salini2 [$j][11] == 0 && $matriz_salini2 [$j][8] == 0 && $matriz_salini2 [$j][10] == 0 && $matriz_salini2 [$j][12] == 0 && $j <= $fin)
	{ $j++; }
	
	} //fin if mostrar 0
	
	
	if($j > $fin) { //if ultimo 0
	} else { //else ultimo 0
	
	if($color % 2 == 0) { //if color
	$codigo = $matriz_salini2 [$j][1];
	$totaldet = $matriz_salini2 [$j][7];
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_salini2 [$j][1]);//cod
	printf("<td align='left'>%s</td>", $matriz_salini2 [$j][2]);//empleado
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][7],$n_decimales));//saldo inicial
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][9],$n_decimales));// total CxC
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][11],$n_decimales));// total Cobros
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][8],$n_decimales));// total CxP
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][10],$n_decimales));// total Pagos
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][12],$n_decimales));// total Periodo
	
	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=0&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=0;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empleado</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	} //fin if ver detalle 0
	
	} else {
	$codigo = $matriz_salini2 [$j][1];
	$totaldet = $matriz_salini2 [$j][7];
	printf("<tr><td align='center'>%s</td>", $matriz_salini2 [$j][1]);//cod
	printf("<td align='left'>%s</td>", $matriz_salini2 [$j][2]);//empleado
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][7],$n_decimales));//saldo inicial
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][9],$n_decimales));// total CxC
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][11],$n_decimales));// total Cobros
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][8],$n_decimales));// total CxP
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][10],$n_decimales));// total Pagos
	printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][12],$n_decimales));// total Periodo
	
	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=0&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=0;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empleado</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	} //fin if ver detalle 0
	
	} //cierra if color
    
	}//cierra else ultimo 0
	$suma_total_cxc2 = $suma_total_cxc2 + $matriz_salini2 [$j][9];
    $suma_total_cobros2 = $suma_total_cobros2 + $matriz_salini2 [$j][11];
    $suma_total_cxp2 = $suma_total_cxp2 + $matriz_salini2 [$j][8];
    $suma_total_pagos2 = $suma_total_pagos2 + $matriz_salini2 [$j][10];
    $suma_total_periodo2 = $suma_total_periodo2 + $matriz_salini2 [$j][12];
    $suma_total2 = $suma_total2 + $matriz_salini2 [$j][13];
	$color++;
    $j++;
}//fin while

$suma_total_cxc3 = $suma_total_cxc + $suma_total_cxc2;
$suma_total_cobros3 = $suma_total_cobros + $suma_total_cobros2;
$suma_total_cxp3 = $suma_total_cxp + $suma_total_cxp2;
$suma_total_pagos3 = $suma_total_pagos + $suma_total_pagos2;
$suma_total_periodo3 = $suma_total_periodo + $suma_total_periodo2;
$suma_total3 = $suma_total + $suma_total2;

?>
<tr BGCOLOR='#ffcccc'><td colspan="3"><b>Total Cuentas Empleados</b>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxc2,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cobros2,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxp2,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_pagos2,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_periodo2,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total2,$n_decimales)); ?>
</td></tr>
<tr BGCOLOR='#ffcccc'><td colspan="3"><b>Total Cuentas Empresas y Empleados</b>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxc3,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cobros3,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxp3,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_pagos3,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_periodo3,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total3,$n_decimales)); ?>
</td></tr>
</tbody>
</table>
<?php

	} //fin if 2.4

} //fin if 2






//inicio Empresa / Empleado

if ($nombre != 0 && $nombre != -1 && $nombre != -2) { // if 3
		
		$empresaoempleado = substr($nombre, -2);
		$nombreemp = substr($nombre, 0, -3);
				
	if($empresaoempleado == -1) { //if 3.3
	$nom_orden = 'Nombre';
	$nom_cuenta = 'Empresa';
	
	//nombre empresa, solo para el titulo, sin canal
	$resulte1 = mysqli_query($link, "select pkyCompany as codigo, fldName as empresa
from agnCompany
where 
pkyCompany = '$nombreemp' and
sysEnabled = 1 and sysAuthorized = 1
order by empresa;");
while ($row = mysqli_fetch_array($resulte1)) {  
	$empresa_codigo = $row['codigo'];
	$empresa_titulo = $row['empresa'];
}
mysqli_free_result($resulte1);

	//listado de empresas
	$result = mysqli_query($link, "select pkyCompany as codigo, fldName as empresa
from agnCompany
where 
$valor_canal 
pkyCompany = '$nombreemp' and
sysEnabled = 1 and sysAuthorized = 1
order by empresa;");
	
	//inicio calculo saldo inicial
	//CxP empresas
	$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p 
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and 
a.codSupplier = '$nombreemp' and
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p 
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and 
a.codSupplier = '$nombreemp' and
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//CxC empresas
	$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p 
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.codClient = '$nombreemp' and
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p 
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.codClient = '$nombreemp' and
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Pagos empresas
	$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p 
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.codClient = '$nombreemp' and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p 
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.codClient = '$nombreemp' and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Cobros empresas
	$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros,
otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
otra.cod_moneda_cxc as cod_moneda_cxc, 
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.codClient = '$nombreemp' and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.codClient = '$nombreemp' and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.codClient = '$nombreemp' and
a.fldDate < '$fechaini' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.codClient = '$nombreemp' and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");				

$matriz_salini = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
$matriz_salini [$i][1] = $row['codigo'];
$matriz_salini [$i][2] = $row['empresa'];
$matriz_salini [$i][3] = 0;
$matriz_salini [$i][4] = 0;
$matriz_salini [$i][5] = 0;
$matriz_salini [$i][6] = 0;
$matriz_salini [$i][7] = 0;
$matriz_salini [$i][8] = 0;
$matriz_salini [$i][9] = 0;
$matriz_salini [$i][10] = 0;
$matriz_salini [$i][11] = 0;
$matriz_salini [$i][12] = 0;
$matriz_salini [$i][13] = 0;
$i++;
}
$fin = $i - 1;
mysqli_free_result($result);
//CxP
while ($row = mysqli_fetch_array($result2)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini [$i][1]) {
	$i++;
}
$matriz_salini [$i][3] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini [$i][1]) {
	$i++;
}
$matriz_salini [$i][4] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini [$i][1]) {
	$i++;
}
$matriz_salini [$i][5] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini [$i][1]) {
	$i++;
}
$matriz_salini [$i][6] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
$reg_cxc = $matriz_salini [$i][4];
$reg_cxp = $matriz_salini [$i][3];
$reg_pagos = $matriz_salini [$i][5];
$reg_cobros = $matriz_salini [$i][6];
$matriz_salini [$i][7] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
$i++;
}

//fin calculo saldo inicial

//inicio calculo registros
	//CxP empresas
	$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p 
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and 
a.codSupplier = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codSupplier as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a, agnCompany p 
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_canal2 
$valor_contexto 
a.codSupplier = p.pkyCompany and 
a.codSupplier = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codSupplier is not NULL 
group by a.codSupplier, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//CxC empresas
	$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p 
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.codClient = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codClient as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a, agnCompany p
where 
$valor_con_cxc 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.codClient = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codClient is not NULL 
group by a.codClient, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Pagos empresas
	$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p 
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.codClient = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c, agnCompany p 
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.codClient = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Cobros empresas
	$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros,
otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
otra.cod_moneda_cxc as cod_moneda_cxc, 
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and  
a.pkyPayment = b.codPayment and
a.codClient = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.codClient = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyPayment = b.codPayment and
a.codClient = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codClient is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codClient as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c, agnCompany p 
where 
$valor_con_cxc2 
$valor_canal2 
$valor_contexto 
a.codClient = p.pkyCompany and 
a.pkyCollection = b.codCollection and
a.codClient = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codClient is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codClient, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");		

//CxP
while ($row = mysqli_fetch_array($result2)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini [$i][1]) {
	$i++;
}
$matriz_salini [$i][8] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini [$i][1]) {
	$i++;
}
$matriz_salini [$i][9] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini [$i][1]) {
	$i++;
}
$matriz_salini [$i][10] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini [$i][1]) {
	$i++;
}
$matriz_salini [$i][11] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
$reg_cxc = $matriz_salini [$i][9];
$reg_cxp = $matriz_salini [$i][8];
$reg_pagos = $matriz_salini [$i][10];
$reg_cobros = $matriz_salini [$i][11];
$matriz_salini [$i][12] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
$reg_salini = $matriz_salini [$i][7];
$reg_periodo = $matriz_salini [$i][12];
$matriz_salini [$i][13] = $reg_salini + $reg_periodo;
$i++;
}

//fin calculo registros
	?>
<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>Estado de Cuentas</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>Cuenta: </b><?php echo $empresa_codigo;?> - <?php echo $empresa_titulo;?></TD>
    <TD width="25%"><b>Canal (Solo para Empresas): </b><?php echo $nom_canal; ?></TD>
    <TD width="25%"><b>Concepto de CxP: </b><?php echo $nom_con_cxp; ?></TD>
    <TD width="25%"><b>Concepto de CxC: </b><?php echo $nom_con_cxc; ?></TD>
    </TR>
    <TR>
    <TD><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
    <TD><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
    <TD><b>Ordenado por: </b><?php echo $nom_orden; ?></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"><b>Empresa</b></TD>
<TD align="center"><b>Saldo Inicial</b></TD>
<TD align="center"><b>Total CxC</b></TD>
<TD align="center"><b>Total Cobros</b></TD>
<TD align="center"><b>Total CxP</b></TD>
<TD align="center"><b>Total Pagos</b></TD>
<TD align="center"><b>Total Periodo</b></TD>
<TD align="center"><b>Total en <?php echo $nom_otra_moneda; ?></b></TD>
</TR>
</thead>
<tbody class="marcafila">

<?php
$color = 2;
$suma_total_cxc = 0;
$suma_total_cobros = 0;
$suma_total_cxp = 0;
$suma_total_pagos = 0;
$suma_total_periodo = 0;
$suma_total = 0;
$j = 1;
while ($j <= $fin) { //while

if($mostrar == 0) { //if mostrar 0
	
	while($matriz_salini [$j][13] == 0 && $j <= $fin)
	{ $j++; }
	
} else { //else if mostrar
	
	while($matriz_salini [$j][7] == 0 && $matriz_salini [$j][9] == 0 && $matriz_salini [$j][11] == 0 && $matriz_salini [$j][8] == 0 && $matriz_salini [$j][10] == 0 && $matriz_salini [$j][12] == 0 && $j <= $fin)
	{ $j++; }
	
} //fin if mostrar 0


if($j > $fin) { //if ultimo 0
} else { //else ultimo 0

if($color % 2 == 0) { //if color
$codigo = $matriz_salini [$j][1];
$totaldet = $matriz_salini [$j][7];
printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_salini [$j][1]);//cod
printf("<td align='left'>%s</td>", $matriz_salini [$j][2]);//empresa
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][7],$n_decimales));//saldo inicial
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][9],$n_decimales));// total CxC
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][11],$n_decimales));// total Cobros
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][8],$n_decimales));// total CxP
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][10],$n_decimales));// total Pagos
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][12],$n_decimales));// total Periodo

	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=1&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=1;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empresa</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	} //fin if ver detalle 0

} else {
$codigo = $matriz_salini [$j][1];
$totaldet = $matriz_salini [$j][7];
printf("<tr><td align='center'>%s</td>", $matriz_salini [$j][1]);//cod
printf("<td align='left'>%s</td>", $matriz_salini [$j][2]);//empresa
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][7],$n_decimales));//saldo inicial
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][9],$n_decimales));// total CxC
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][11],$n_decimales));// total Cobros
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][8],$n_decimales));// total CxP
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][10],$n_decimales));// total Pagos
printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][12],$n_decimales));// total Periodo

	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=1&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][13],$n_decimales));//total
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=1;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empresa</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	} //fin if ver detalle 0

} //cierra if color

} //cierra else ultimo 0
$suma_total_cxc = $suma_total_cxc + $matriz_salini [$j][9];
$suma_total_cobros = $suma_total_cobros + $matriz_salini [$j][11];
$suma_total_cxp = $suma_total_cxp + $matriz_salini [$j][8];
$suma_total_pagos = $suma_total_pagos + $matriz_salini [$j][10];
$suma_total_periodo = $suma_total_periodo + $matriz_salini [$j][12];
$suma_total = $suma_total + $matriz_salini [$j][13];
$color++;
$j++;
}//fin while


?>
<tr BGCOLOR='#ffcccc'><td colspan="3"><b>Total Cuentas Empresas</b>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxc,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cobros,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxp,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_pagos,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_periodo,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total,$n_decimales)); ?>
</td></tr>
</tbody>
</table>

<?php
	
	}//fin if 3.3

	if($empresaoempleado == -2) { //if 3.4
	$nom_orden = 'Nombre';
	$nom_cuenta = 'Empleado';
	
	//listado de empleados
	$result = 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' and
sysEnabled = 1 and sysAuthorized = 1
order by empleado;");
	
	//inicio calculo saldo inicial
	//CxP empleados
	$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.codEmployee = '$nombreemp' and
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.codEmployee = '$nombreemp' and
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//CxC empleados
	$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.codEmployee = '$nombreemp' and
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.codEmployee = '$nombreemp' and
a.fldDate < '$fechaini' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Pagos empleados
	$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombreemp' and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombreemp' and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Cobros empleados
	$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros, 
otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
otra.cod_moneda_cxc as cod_moneda_cxc,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2  
$valor_contexto 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombreemp' and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.codEmployee = '$nombreemp' and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombreemp' and
a.fldDate < '$fechaini' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.codEmployee = '$nombreemp' and
a.fldDate < '$fechaini' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");				

$matriz_salini2 = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
$matriz_salini2 [$i][1] = $row['codigo'];
$matriz_salini2 [$i][2] = $row['empleado'];
$matriz_salini2 [$i][3] = 0;
$matriz_salini2 [$i][4] = 0;
$matriz_salini2 [$i][5] = 0;
$matriz_salini2 [$i][6] = 0;
$matriz_salini2 [$i][7] = 0;
$matriz_salini2 [$i][8] = 0;
$matriz_salini2 [$i][9] = 0;
$matriz_salini2 [$i][10] = 0;
$matriz_salini2 [$i][11] = 0;
$matriz_salini2 [$i][12] = 0;
$matriz_salini2 [$i][13] = 0;
$i++;
}
$fin = $i - 1;
mysqli_free_result($result);
//CxP
while ($row = mysqli_fetch_array($result2)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][3] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][4] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][5] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][6] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
$reg_cxc = $matriz_salini2 [$i][4];
$reg_cxp = $matriz_salini2 [$i][3];
$reg_pagos = $matriz_salini2 [$i][5];
$reg_cobros = $matriz_salini2 [$i][6];
$matriz_salini2 [$i][7] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
$i++;
}

//fin calculo saldo inicial

//inicio calculo registros
	//CxP empleados
	$result2 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxp
from(
select otra.cod_cli as cod_cli, otra.cxp as cxp, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxp * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.codEmployee = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxp as cxp, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxp, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from payAccountsPayable a
where 
$valor_con_cxp 
$valor_ver_cxp 
$valor_contexto 
a.codEmployee = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//CxC empleados
	$result3 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as cxc
from(
select otra.cod_cli as cod_cli, otra.cxc as cxc, otra.cod_moneda as cod_moneda,
otra.tc as tc, otra.otra_moneda as otra_moneda, otra.tasa as tasa, otra.cxc * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.codEmployee = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.cxc as cxc, t.cod_moneda as cod_moneda,
t.tc as tc, t.otra_moneda as otra_moneda, 1 as tasa
from
(select a.codEmployee as cod_cli, sum(a.fldTotal) as cxc, 
a.codCurrency as cod_moneda, a.codExchangeRate as tc, '$otra_moneda' as otra_moneda
from colAccountsReceivable a
where 
$valor_con_cxc 
$valor_contexto 
a.codEmployee = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.sysEnabled = 1 and a.sysAuthorized = 1 and
a.codEmployee is not NULL 
group by a.codEmployee, a.codCurrency, a.codExchangeRate) t
where
t.cod_moneda = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Pagos empleados
	$result4 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_pagos
from(
select otra.cod_cli as cod_cli, otra.suma_pagos as suma_pagos, 
otra.cod_moneda_pago as cod_moneda_pago, otra.tc as tc, 
otra.cod_moneda_cxp as cod_moneda_cxp,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_pagos * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_pagos as suma_pagos, 
t.cod_moneda_pago as cod_moneda_pago, t.tc as tc, 
t.cod_moneda_cxp as cod_moneda_cxp, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_pagos,
a.codCurrency as cod_moneda_pago, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxp, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, payAccountsPayable c
where 
$valor_con_cxp2 
$valor_ver_cxp2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = c.pkyAccountsPayable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxp = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");
	//suma Cobros empleados
	$result5 = mysqli_query($link, "select final.cod_cli as cod_cli, sum(final.total) as suma_cobros
from(
select otra.cod_cli as cod_cli, otra.suma_cobros as suma_cobros, 
otra.cod_moneda_cobro as cod_moneda_cobro, otra.tc as tc, 
otra.cod_moneda_cxc as cod_moneda_cxc,
otra.otra_moneda as otra_moneda, otra.tasa as tasa, 
otra.suma_cobros * otra.tasa as total
from
(select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
r.fldRate as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.codEmployee = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.cod_cli as cod_cli, t.suma_cobros as suma_cobros, 
t.cod_moneda_cobro as cod_moneda_cobro, t.tc as tc, 
t.cod_moneda_cxc as cod_moneda_cxc, t.otra_moneda as otra_moneda, 
1 as tasa
from
(select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from payPayment a, payPaymentDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codEmployee is not null and
b.codAccountsReceivable is not null and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate
union all
select a.codEmployee as cod_cli, sum(b.fldTotal) as suma_cobros,
a.codCurrency as cod_moneda_cobro, a.codExchangeRate as tc, 
c.codCurrency as cod_moneda_cxc, '$otra_moneda' as otra_moneda
from colCollection a, colCollectionDetail b, colAccountsReceivable c
where 
$valor_con_cxc2 
$valor_contexto 
a.pkyCollection = b.codCollection and
a.codEmployee = '$nombreemp' and
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
b.codAccountsReceivable = c.pkyAccountsReceivable and
a.codEmployee is not null and
a.sysEnabled = 1 and a.sysAuthorized = 1
group by a.codEmployee, a.codCurrency, c.codCurrency, a.codExchangeRate) t
where
t.cod_moneda_cxc = t.otra_moneda) otra) final
group by final.cod_cli
order by final.cod_cli;");	

//CxP
while ($row = mysqli_fetch_array($result2)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][8] = $row['cxp'];
}
mysqli_free_result($result2);
//CxC
while ($row = mysqli_fetch_array($result3)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][9] = $row['cxc'];
}
mysqli_free_result($result3);
//Pagos
while ($row = mysqli_fetch_array($result4)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][10] = $row['suma_pagos'];
}
mysqli_free_result($result4);
//Cobros
while ($row = mysqli_fetch_array($result5)) {
$i = 1;
$cod_emp = $row['cod_cli'];
while ($cod_emp <> $matriz_salini2 [$i][1]) {
	$i++;
}
$matriz_salini2 [$i][11] = $row['suma_cobros'];
}
mysqli_free_result($result5);

$i = 1;
while($i <= $fin) {
$reg_cxc = $matriz_salini2 [$i][9];
$reg_cxp = $matriz_salini2 [$i][8];
$reg_pagos = $matriz_salini2 [$i][10];
$reg_cobros = $matriz_salini2 [$i][11];
$matriz_salini2 [$i][12] = $reg_cxc - $reg_cxp + $reg_pagos - $reg_cobros;
$reg_salini = $matriz_salini2 [$i][7];
$reg_periodo = $matriz_salini2 [$i][12];
$matriz_salini2 [$i][13] = $reg_salini + $reg_periodo;
$i++;
}

//fin calculo registros
	?>
<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>Estado de Cuentas</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>Cuenta: </b><?php echo $matriz_salini2 [1][1]; ?> - <?php echo $matriz_salini2 [1][2]; ?></TD>
    <TD width="25%"><b>Concepto de CxP: </b><?php echo $nom_con_cxp; ?></TD>
    <TD width="25%"><b>Concepto de CxC: </b><?php echo $nom_con_cxc; ?></TD>
    <TD width="25%"><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>Ordenado por: </b><?php echo $nom_orden; ?></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"><b>Empleado</b></TD>
<TD align="center"><b>Saldo Inicial</b></TD>
<TD align="center"><b>Total CxC</b></TD>
<TD align="center"><b>Total Cobros</b></TD>
<TD align="center"><b>Total CxP</b></TD>
<TD align="center"><b>Total Pagos</b></TD>
<TD align="center"><b>Total Periodo</b></TD>
<TD align="center"><b>Total en <?php echo $nom_otra_moneda; ?></b></TD>
</TR>
</thead>
<tbody class="marcafila">

<?php
$color = 2;
$suma_total_cxc2 = 0;
$suma_total_cobros2 = 0;
$suma_total_cxp2 = 0;
$suma_total_pagos2 = 0;
$suma_total_periodo2 = 0;
$suma_total2 = 0;
$j = 1;
while ($j <= $fin) { //while

if($mostrar == 0) { //if mostrar 0
	
	while($matriz_salini2 [$j][13] == 0 && $j <= $fin)
	{ $j++; }
	
} else { //else if mostrar 0
	
	while($matriz_salini2 [$j][7] == 0 && $matriz_salini2 [$j][9] == 0 && $matriz_salini2 [$j][11] == 0 && $matriz_salini2 [$j][8] == 0 && $matriz_salini2 [$j][10] == 0 && $matriz_salini2 [$j][12] == 0 && $j <= $fin)
	{ $j++; }
	
} //fin if mostrar 0


if($j > $fin) { //if ultimo 0
} else { //else ultimo 0

if($color % 2 == 0) { //if color
$codigo = $matriz_salini2 [$j][1];
$totaldet = $matriz_salini2 [$j][7];
printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_salini2 [$j][1]);//cod
printf("<td align='left'>%s</td>", $matriz_salini2 [$j][2]);//empleado
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][7],$n_decimales));//saldo inicial
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][9],$n_decimales));// total CxC
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][11],$n_decimales));// total Cobros
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][8],$n_decimales));// total CxP
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][10],$n_decimales));// total Pagos
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][12],$n_decimales));// total Periodo

	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=0&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=0;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empleado</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	} //fin if ver detalle 0

} else {
$codigo = $matriz_salini2 [$j][1];
$totaldet = $matriz_salini2 [$j][7];
printf("<tr><td align='center'>%s</td>", $matriz_salini2 [$j][1]);//cod
printf("<td align='left'>%s</td>", $matriz_salini2 [$j][2]);//empleado
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][7],$n_decimales));//saldo inicial
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][9],$n_decimales));// total CxC
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][11],$n_decimales));// total Cobros
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][8],$n_decimales));// total CxP
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][10],$n_decimales));// total Pagos
printf("<td align='right'>%s</td>", number_format($matriz_salini2 [$j][12],$n_decimales));// total Periodo

	if($verdetalle == 0) { //if ver detalle 0
	printf("<td align='right'><a href='report.php?rep=8&tipo=2&nombredet=$codigo&empresa=0&fechainidet=$fechaini&fechafindet=$fechafin&totaldet=$totaldet&otra_moneda_det=$otra_moneda&ordendet=$orden_detalle&ventadetalledet=$venta_detalle&con_cxp_det=$con_cxp&con_cxc_det=$con_cxc&contexto_det=$cod_contexto&usuario_det=$usuario' title='Kardex de Cuenta'>%s</a></td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	} else { //else ver detalle 0
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini2 [$j][13],$n_decimales));//total
	$rep=8;$tipo=3;$nombredet=$codigo;$empresa=0;$fechainidet=$fechaini;$fechafindet=$fechafin;$totaldet=$totaldet;$otra_moneda_det=$otra_moneda;$ordendet=$orden_detalle;$ventadetalledet=$venta_detalle;$con_cxp_det=$con_cxp;$con_cxc_det=$con_cxc;$cod_contexto_det=$cod_contexto;$usuario_det=$usuario;
	echo '<tr><td colspan="9">';	
	include 'detail.php';
	echo '</td></tr>';
	echo '<tr BGCOLOR="#66ff99"><td colspan="9">&nbsp;</td></tr>';	
	echo '<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Empleado</b></TD>
    <TD align="center"><b>Saldo Inicial</b></TD>
    <TD align="center"><b>Total CxC</b></TD>
    <TD align="center"><b>Total Cobros</b></TD>
    <TD align="center"><b>Total CxP</b></TD>
    <TD align="center"><b>Total Pagos</b></TD>
    <TD align="center"><b>Total Periodo</b></TD>
    <TD align="center"><b>Total en '.$nom_otra_moneda.'</b></TD>
    </TR>';
	} //fin if ver detalle 0

} //cierra if color

}//cierra else ultimo 0
$suma_total_cxc2 = $suma_total_cxc2 + $matriz_salini2 [$j][9];
$suma_total_cobros2 = $suma_total_cobros2 + $matriz_salini2 [$j][11];
$suma_total_cxp2 = $suma_total_cxp2 + $matriz_salini2 [$j][8];
$suma_total_pagos2 = $suma_total_pagos2 + $matriz_salini2 [$j][10];
$suma_total_periodo2 = $suma_total_periodo2 + $matriz_salini2 [$j][12];
$suma_total2 = $suma_total2 + $matriz_salini2 [$j][13];
$color++;
$j++;
}//fin while


?>
<tr BGCOLOR='#ffcccc'><td colspan="3"><b>Total Cuentas Empleados</b>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxc2,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cobros2,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_cxp2,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_pagos2,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total_periodo2,$n_decimales)); ?>
</td><td align="right"><?php printf("<b>%s</b>", number_format($suma_total2,$n_decimales)); ?>
</td></tr>
</tbody>
</table>
<?php

	} //fin if 3.4

} //fin if 3






// inicio muestra detalle

// OJO SI SE CAMBIA ALGO EN ESTE DETALLE CAMBIAR TAMBIEN EN EL ARCHIVO detail.php (solo en detail.php esta la variable quitar no aqui para que muestre el detalle con titulos)

if($rep == 8 && $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_det = $row['otra_moneda'];
	}
	mysqli_free_result($resultotra);

//nombre concepto de CxP
if ($con_cxp_det == 0) { // if 2.0
		$nom_con_cxp_det = 'Todos';
		$valor_con_cxp_det = ' ';
		$valor_con_cxp_det2 = ' ';
	} else {
		//nombre con cxp
		if ($con_cxp_det == -1) { // if 2.0.1
		$nom_con_cxp_det = 'Solo Concepto de CxC';
		} else { // else if 2.0.1
		$result2 = mysqli_query($link, "select pkyAccountsPayableConcept as cod_con_cxp, fldName as concepto 
								from payAccountsPayableConcept
								where
								pkyAccountsPayableConcept = '$con_cxp_det' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result2)) {
			$nom_con_cxp_det = $row['concepto'];
		}
		mysqli_free_result($result2);
		} //fin if 2.0.1
		$valor_con_cxp_det = ' a.codConcept = ' .$con_cxp_det. ' and ';
		$valor_con_cxp_det2 = ' g.codConcept = ' .$con_cxp_det. ' and ';
	} //fin if 2.0	
	
//nombre concepto de CxC
if ($con_cxc_det == 0) { // if 2.1
		$nom_con_cxc_det = 'Todos';
		$valor_con_cxc_det = ' ';
		$valor_con_cxc_det2 = ' ';
	} else {
		//nombre con cxc
		if ($con_cxc_det == -1) { // if 2.1.1
		$nom_con_cxc_det = 'Solo Concepto de CxP';
		} else { // else if 2.1.1
		$result3 = mysqli_query($link, "select pkyAccountsReceivableConcept as cod_con_cxc, fldName as concepto 
								from colAccountsReceivableConcept
								where
								pkyAccountsReceivableConcept = '$con_cxc_det' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result3)) {
			$nom_con_cxc_det = $row['concepto'];
		}
		mysqli_free_result($result3);
		} //fin if 2.1.1
		$valor_con_cxc_det = ' a.codConcept = ' .$con_cxc_det. ' and ';
		$valor_con_cxc_det2 = ' g.codConcept = ' .$con_cxc_det. ' and ';
	} //fin if 2.1	

// orden detalle por fecha (desordenado pero con saldos), por cuenta (agrupado por cuenta)
if($ordendet == 0) { 
	$txt_orden_det = 'Fecha';
	$valor_orden = 'order by final.fecha, final.orden, final.codigo';
} else {
	$txt_orden_det = 'Cuenta';
	$valor_orden = 'order by final.codigo_orden, final.orden, final.codigo';
}

//contexto
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
  
 // detalle empresa
  $result = mysqli_query($link, "select pkyCompany as codigo, fldName as empresa
from agnCompany
where
pkyCompany = '$nombredet' and
sysEnabled = 1 and sysAuthorized = 1;");
 
while ($row = mysqli_fetch_array($result)) {
	$nomempdet = $row['codigo'].' - '.$row['empresa'];
}
mysqli_free_result($result);


 $result2 = mysqli_query($link, "select final.codigo as codigo, final.nrodoc as nrodoc, final.fecha as fecha, final.fecha_pago as fecha_pagocob, final.concepto as concepto, final.concepto_gasto as concepto_pagocob, final.via as via_pagocob, final.empresa as empresa, final.moneda as moneda, final.glosa as glosa, final.cxp as total, final.tipo as tipo, final.tasa as tasa, final.total as total_om, final.orden as orden, final.codigo_orden as codigo_orden, final.papa as papa, final.cod_papa as cod_papa
from
(select otro.codigo as codigo, otro.nrodoc as nrodoc, otro.fecha as fecha, otro.fecha_pago as fecha_pago, otro.concepto as concepto, otro.concepto_gasto as concepto_gasto, otro.via as via, otro.empresa as empresa, otro.moneda as moneda, otro.glosa as glosa, otro.cxp as cxp, otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, otro.cod_moneda as cod_moneda, otro.tasa as tasa, otro.tasa * otro.cxp as total, otro.orden as orden, otro.codigo_orden as codigo_orden, otro.papa as papa, otro.cod_papa as cod_papa
from (
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_pago as fecha_pago, t.concepto as concepto, t.concepto_gasto as concepto_gasto, t.via as via, t.empresa as empresa, t.moneda as moneda, t.glosa as glosa, t.cxp as cxp, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa
from (
select a.pkyAccountsPayable as codigo, a.fldDocumentNumber as nrodoc,
a.fldDate as fecha, a.fldPaymentDate as fecha_pago, b.fldName as concepto, 
null as concepto_gasto, null as via, d.fldName as empresa, f.fldName as moneda, 
a.txtDescription as glosa, (a.fldTotal * -1) as cxp, 'CxP' as tipo, 
a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 3 as orden, a.pkyAccountsPayable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableConcept b, agnCompany d, actCurrency f
where 
$valor_con_cxp_det 
$valor_ver_cxp_det 
$valor_contexto_det 
a.codSupplier = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsPayableConcept and

a.codSupplier = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_pago as fecha_pago, t.concepto as concepto, t.concepto_gasto as concepto_gasto, t.via as via, t.empresa as empresa, t.moneda as moneda, t.glosa as glosa, t.cxp as cxp, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, 1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa
from (
select a.pkyAccountsPayable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldPaymentDate as fecha_pago, b.fldName as concepto, null as concepto_gasto, null as via, d.fldName as empresa, f.fldName as moneda, a.txtDescription as glosa, (a.fldTotal * -1) as cxp, 'CxP' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 3 as orden, a.pkyAccountsPayable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableConcept b, agnCompany d, actCurrency f
where 
$valor_con_cxp_det 
$valor_ver_cxp_det 
$valor_contexto_det 
a.codSupplier = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsPayableConcept and

a.codSupplier = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, otro.fecha as fecha, otro.fecha_cobro as fecha_cobro, otro.concepto as concepto, otro.concepto_ingreso as concepto_ingreso, otro.via as via, otro.empresa as empresa, otro.moneda as moneda, otro.glosa as glosa, otro.cxc as cxc,
otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, otro.cod_moneda as cod_moneda, otro.tasa as tasa, otro.tasa * otro.cxc as total, otro.orden as orden, otro.codigo_orden as codigo_orden, otro.papa as papa, otro.cod_papa as cod_papa
from (
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_cobro as fecha_cobro, t.concepto as concepto, t.concepto_ingreso as concepto_ingreso, t.via as via, t.empresa as empresa, t.moneda as moneda, t.glosa as glosa, t.cxc as cxc, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa
from (
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, null as via, d.fldName as empresa, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as cxc, 'CxC' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 1 as orden, a.pkyAccountsReceivable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c,
agnCompany d, actCurrency f
where 
$valor_con_cxc_det
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' 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) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_cobro as fecha_cobro, t.concepto as concepto, t.concepto_ingreso as concepto_ingreso, t.via as via, t.empresa as empresa, t.moneda as moneda, t.glosa as glosa, t.cxc as cxc, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, 1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa
from (
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, null as via, d.fldName as empresa, f.fldName as moneda, a.txtDescription as glosa, a.fldTotal as cxc, 'CxC' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 1 as orden, a.pkyAccountsReceivable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c,
agnCompany d, actCurrency f
where 
$valor_con_cxc_det
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' 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) t
where
t.cod_moneda = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, 
otro.fecha as fecha, otro.fecha_cob_pag as fecha_cob_pag, 
otro.concepto as concepto, otro.con_ing_egr as con_ing_egr, 
otro.via_pago as via_pago, otro.empresa as empresa, 
otro.moneda_cxp as moneda, otro.glosa as glosa, otro.pago as pago,
otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, 
otro.cod_moneda_cxp as cod_moneda, otro.tasa as tasa, 
otro.tasa * otro.pago as total, otro.orden as orden, otro.codigo_orden as codigo_orden, 
otro.papa as papa, otro.cod_papa as cod_papa
from 
(select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_pago as via_pago, 
t.cod_moneda_cxp as cod_moneda_cxp, t.moneda_cxp as moneda_cxp,
t.empresa as empresa, 
t.moneda_pago as moneda_pago, t.glosa as glosa, 
t.pago as pago, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_pago as cod_moneda_pago, 
r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, null as via_pago, d.fldName as empresa, 
g.codCurrency as cod_moneda_cxp, h.fldName as moneda_cxp,
f.fldName as moneda_pago, a.txtDescription as glosa, b.fldTotal as pago, 
'Pago' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_pago, 4 as orden, g.pkyAccountsPayable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payPayment a, payPaymentDetail b, agnCompany d, actCurrency f, 
payAccountsPayable g, actCurrency h
where 
$valor_con_cxp_det2 
$valor_ver_cxp_det2
$valor_contexto_det 
a.pkyPayment = b.codPayment and
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = g.pkyAccountsPayable and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.codClient = d.pkyCompany and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_pago as via_pago, 
t.cod_moneda_cxp as cod_moneda_cxp, t.moneda_cxp as moneda_cxp, 
t.empresa as empresa, 
t.moneda_pago as moneda_pago, t.glosa as glosa, 
t.pago as pago, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_pago as cod_moneda_pago, 
1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, null as via_pago, d.fldName as empresa, 
g.codCurrency as cod_moneda_cxp, h.fldName as moneda_cxp,
f.fldName as moneda_pago, a.txtDescription as glosa, b.fldTotal as pago, 
'Pago' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_pago, 4 as orden, g.pkyAccountsPayable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payPayment a, payPaymentDetail b, agnCompany d, actCurrency f, 
payAccountsPayable g, actCurrency h
where 
$valor_con_cxp_det2 
$valor_ver_cxp_det2 
$valor_contexto_det 
a.pkyPayment = b.codPayment and
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codClient is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = g.pkyAccountsPayable and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.codClient = d.pkyCompany and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_cxp = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, 
otro.fecha as fecha, otro.fecha_cob_pag as fecha_cob_pag, 
otro.concepto as concepto, otro.con_ing_egr as con_ing_egr, 
otro.via_cobro as via_cobro, otro.empresa as empresa, 
otro.moneda_cxc as moneda, otro.glosa as glosa, 
otro.cobro as cobro, otro.tipo as tipo, otro.tc as tc, 
otro.otra_moneda as otra_moneda, 
otro.cod_moneda_cxc as cod_moneda, otro.tasa as tasa, 
otro.tasa * otro.cobro as total, otro.orden as orden, otro.codigo_orden as codigo_orden,
otro.papa as papa, otro.cod_papa as cod_papa
from 
(select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_cobro as via_cobro, 
t.cod_moneda_cxc as cod_moneda_cxc, t.moneda_cxc as moneda_cxc,
t.empresa as empresa, 
t.moneda_cobro as moneda_cobro, t.glosa as glosa, 
t.cobro as cobro, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_cobro as cod_moneda_cobro, 
r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyCollection as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, c.fldName as via_cobro, d.fldName as empresa, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc,
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
colCollection a, colCollectionDetail b, colCollectionRoute c, agnCompany d, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codRoute = c.pkyCollectionRoute and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.pkyCollection = b.codCollection and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
union all
select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, 'Liquidacion' as via_cobro, d.fldName as empresa,
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc, 
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
payPayment a, payPaymentDetail b, agnCompany d, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.pkyPayment = b.codPayment and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
b.codAccountsReceivable is not NULL and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency

union all

select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_cobro as via_cobro, 
t.cod_moneda_cxc as cod_moneda_cxc, t.moneda_cxc as moneda_cxc,
t.empresa as empresa, 
t.moneda_cobro as moneda_cobro, t.glosa as glosa, 
t.cobro as cobro, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_cobro as cod_moneda_cobro, 
1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa 
from 
(select a.pkyCollection as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, c.fldName as via_cobro, d.fldName as empresa, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc,
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
colCollection a, colCollectionDetail b, colCollectionRoute c, agnCompany d, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2 
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codRoute = c.pkyCollectionRoute and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.pkyCollection = b.codCollection and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
union all
select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, 'Liquidacion' as via_cobro, d.fldName as empresa,
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc, 
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
payPayment a, payPaymentDetail b, agnCompany d, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2
$valor_contexto_det 
a.codClient = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.pkyPayment = b.codPayment and
a.codClient = d.pkyCompany and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
b.codAccountsReceivable is not NULL and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_cxc = t.otra_moneda) otro) final
$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_det; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Detalle Estado de Cuentas</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>Empresa: </b><?php echo $nomempdet; ?></TD>
        <TD width="25%"><b>Concepto de CxP: </b><?php echo $nom_con_cxp_det; ?></TD>
        <TD width="25%"><b>Concepto de CxC: </b><?php echo $nom_con_cxc_det; ?></TD>
        <TD width="25%"><b>Contexto: </b><?php echo $nom_contexto_det; ?></TD>
        </TR>
        <TR>
        <TD colspan="2"><b>De Fecha: </b><?php echo $fechainidet; ?> <b>a Fecha: </b><?php echo $fechafindet; ?></TD>
        <TD><b>Ordenado por: </b><?php echo $txt_orden_det; ?></TD>
        <TD><b>Expresado en: </b><?php echo $nom_otra_moneda_det; ?></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%">
	<TR BGCOLOR='#ffcccc'> 
    <?php if($ordendet == 0) { // if orden ?>
 	<TD align="left" colspan="16"><b>Saldo inicial en <?php echo $nom_otra_moneda_det; ?></b></TD>
	<?php } else { // if else orden ?>
    <TD align="left" colspan="15"><b>Saldo inicial en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php }// fin if orden ?>
    <TD align="right"><b><?php echo number_format($totaldet,$n_decimales); ?></b></TD></TR> 
    <thead>
    <TR> 
    <TD align="center"><b>Tipo</b></TD>
    <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 Pago/Cobro</b></TD>
    <TD align="center"><b>Concepto</b></TD>
    <TD align="center"><b>Concepto Pago/Cobro</b></TD>
    <TD align="center"><b>Via Pago/Cobro</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</b></TD>
    <TD align="center"><b>Tasa</b></TD>
    <TD align="center"><b>Debito en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <TD align="center"><b>Cr&eacute;dito en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php if($ordendet == 0) { // if orden ?>
    <TD align="center"><b>Saldo en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php }// fin if orden ?>
    </TR>
    </thead>
    <tbody class="marcafila">

<?php
$color = 2;
$suma_total = 0;
$suma_periodo = 0;
$suma_deb = 0;
$suma_cred = 0;
$saldo_det = $totaldet;
  
while ($row = mysqli_fetch_array($result2)) { //while
    
	//para enlazar al que genero el asiento
	$papa_componente = $row['papa'];
	$papa_codigo = $row['cod_papa'];
	$usuario = $usuario_det;
	include "../../generadopor.php";
	 
	if($color % 2 == 0) { //if color
		if($row['orden'] == 2 || $row['orden'] == 4){ //if orden
		printf("<tr BGCOLOR='#e2e7fa'><td align='left'>%s %s</td>", $row['tipo'], $row['codigo_orden']);//tipo
		} else {
		printf("<tr BGCOLOR='#e2e7fa'><td align='left'>%s</td>", $row['tipo']);//tipo	
		} //fin if orden
	} else {
		if($row['orden'] == 2 || $row['orden'] == 4){ //if orden
		printf("<tr><td align='left'>%s %s</td>", $row['tipo'], $row['codigo_orden']);//tipo
		} else {
		printf("<tr><td align='left'>%s</td>", $row['tipo']);//tipo
		} //fin if orden
	} //fin if color
	
	if($row['orden'] == 1) { //if orden 1
		$codigo_cxc = $row['codigo'];
		printf("<td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo_cxc&user=$usuario_det' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//codigo
	} //fin if orden 1
	if($row['orden'] == 2) { //if orden 2
		$cod_cobro = $row['codigo'];
		$tipo2 = $row['via_pagocob'];
		if($tipo2 == 'Liquidacion') {//if liquidacion
			printf("<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("<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
	} //fin if orden 2
	if($row['orden'] == 3) { //if orden 3
		$codigo_cxp = $row['codigo'];
		printf("<td align='center'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$codigo_cxp&user=$usuario_det' title='Nota de Cuenta por Pagar'>%s</a></td>", $row['codigo']);//codigo	
	}//fin if orden 3
	if($row['orden'] == 4) { //if orden 4
		$cod_pago = $row['codigo'];
		printf("<td align='center'><a href='../../ModulePayments/VoucherPayment/index.php?pkyPayment=$cod_pago&user=$usuario_det' title='Nota de Pago'>%s</a></td>", $cod_pago);//codigo	
	} //fin if orden 4
	
	printf("<td align='center'>%s</td>", $row['nrodoc']);//nro doc
	printf("<td align='right'>%s</td>", $row['fecha']);//fecha
	printf("<td align='right'>%s</td>", $row['fecha_pagocob']);//fecha pago o cobro
	printf("<td>%s</td>", $row['concepto']);//concepto 
	printf("<td>%s</td>", $row['concepto_pagocob']);//concepto pago o cobro
	printf("<td>%s</td>", $row['via_pagocob']);//via pago o cobro
	printf("<td>%s</td>", $row['empresa']);//empresa
	printf("<td>%s</td>", $row['glosa']);//glosa
	printf("<td align='center'>%s</td>", $papa_enlace);//gen por
	printf("<td>%s</td>", $row['moneda']);//moneda
	
	if($row['total'] >= 0) {
		printf("<td align='right'>%s</td>", number_format($row['total'],$n_decimales));//total
	} else {
		printf("<td align='right'>%s</td>", number_format($row['total']*-1,$n_decimales));//total
	}
	printf("<td>%s</td>", number_format($row['tasa'], $n_tipocambio));//tasa
	
	if($row['orden'] == 1 || $row['orden'] == 4) {//if cxc y pago
		printf("<td align='right'>%s</td>", number_format($row['total_om'],$n_decimales));//total otra moneda debito
		printf("<td>&nbsp;</td>");//total otra moneda credito
		$suma_deb = $suma_deb + $row['total_om'];
	} //fin if cxc y pago
	if($row['orden'] == 2 || $row['orden'] == 3) { //if cobro y cxp
		printf("<td>&nbsp;</td>");//total otra moneda debito
		printf("<td align='right'>%s</td>", number_format($row['total_om']*-1,$n_decimales));//total otra moneda credito
		$suma_cred = $suma_cred + $row['total_om']*-1;
	} //fin if cobro y cxp
	
  if($ordendet == 0) { //if orden
    $saldo_det = $saldo_det + $row['total_om'];
	printf("<td align='right'>%s</td>", number_format($saldo_det,$n_decimales));//saldo
  } //fin if orden
  
	echo '</tr>';
	$suma_periodo = $suma_periodo + $row['total_om'];
	$color++;
	
	//muestra el detalle de la nota de venta
	
	if($row['orden'] == 1 && $ventadetalledet == 1 && strcmp($row['papa'],'ComponentSalesNote') == 0) { //if venta detalle orden 1.1
		$cod_venta = $row['cod_papa'];
		
		if($ordendet == 0) { // if orden 0.0
		echo '<tr><td colspan="16">';	
		include 'detail2.php';
		echo '</td></tr>';
		echo '<tr BGCOLOR="#66ff99"><td colspan="16">&nbsp;</td></tr>';	
		} else { // else if orden 0.0
		echo '<tr><td colspan="15">';	
		include 'detail2.php';
		echo '</td></tr>';
		echo '<tr BGCOLOR="#66ff99"><td colspan="15">&nbsp;</td></tr>';	
		} //fin if orden 0.0
		echo '<TR> 
		<TD align="center"><b>Tipo</b></TD>
		<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 Pago/Cobro</b></TD>
		<TD align="center"><b>Concepto</b></TD>
		<TD align="center"><b>Concepto Pago/Cobro</b></TD>
		<TD align="center"><b>Via Pago/Cobro</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</b></TD>
		<TD align="center"><b>Tasa</b></TD>
		<TD align="center"><b>Debito en '.$nom_otra_moneda_det.'</b></TD>
		<TD align="center"><b>Cr&eacute;dito en '.$nom_otra_moneda_det.'</b></TD>';
		if($ordendet == 0) { // if orden
		echo '<TD align="center"><b>Saldo en '.$nom_otra_moneda_det.'</b></TD>';
		}// fin if orden
		echo '</TR>';
	} //fin if venta detalle orden 1.1
$papa_componente = NULL;
$papa_codigo = NULL;
$usuario = NULL;		
}//fin while
	mysqli_free_result($result2);
	$suma_total = $suma_periodo + $totaldet; 
	
 ?>
<TR BGCOLOR='#ffcccc'> 
 <TD align="left" colspan="14"><b>Total Periodo en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <TD align="right"><b><?php echo number_format($suma_deb,$n_decimales); ?></b></TD>
 <TD align="right"><b><?php echo number_format($suma_cred,$n_decimales); ?></b></TD>
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="right"><b><?php echo number_format($suma_periodo,$n_decimales); ?></b></TD>
 <?php }// fin if orden ?>
 </TR> 
 <TR BGCOLOR='#ffcccc'> 
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="left" colspan="16"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php } else { // if else orden ?>
 <TD align="left" colspan="15"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php }// fin if orden ?>
 <TD align="right"><b><?php echo number_format($suma_total,$n_decimales); ?></b></TD>
 </TR> 
</tbody>
</table>
  <?php
 } //fin if 3.1
 
 if($empresa == 0) { //if 3.2
 
 // detalle empleado
 
 $result = 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 = '$nombredet' and
sysEnabled = 1 and sysAuthorized = 1;");
 
while ($row = mysqli_fetch_array($result)) {
	$nomempdet = $row['codigo'].' - '.$row['empleado'];
}
mysqli_free_result($result);

 $result2 = mysqli_query($link, "select final.codigo as codigo, final.nrodoc as nrodoc, final.fecha as fecha, final.fecha_pago as fecha_pagocob, final.concepto as concepto, final.concepto_gasto as concepto_pagocob, final.via as via_pagocob, final.empleado as empleado, final.moneda as moneda, final.glosa as glosa, final.cxp as total, final.tipo as tipo, final.tasa as tasa, final.total as total_om, final.orden as orden, final.codigo_orden as codigo_orden, final.papa as papa, final.cod_papa as cod_papa
from
(select otro.codigo as codigo, otro.nrodoc as nrodoc, otro.fecha as fecha, otro.fecha_pago as fecha_pago, otro.concepto as concepto, otro.concepto_gasto as concepto_gasto, otro.via as via, otro.empleado as empleado, otro.moneda as moneda, otro.glosa as glosa, otro.cxp as cxp, otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, otro.cod_moneda as cod_moneda, otro.tasa as tasa, otro.tasa * otro.cxp as total, otro.orden as orden, otro.codigo_orden as codigo_orden, otro.papa as papa, otro.cod_papa as cod_papa
from (
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_pago as fecha_pago, t.concepto as concepto, t.concepto_gasto as concepto_gasto, t.via as via, t.empleado as empleado, t.moneda as moneda, t.glosa as glosa, t.cxp as cxp, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa
from (
select a.pkyAccountsPayable as codigo, a.fldDocumentNumber as nrodoc,
a.fldDate as fecha, a.fldPaymentDate as fecha_pago, b.fldName as concepto, 
null as concepto_gasto, null as via, 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.txtDescription as glosa, (a.fldTotal * -1) as cxp, 'CxP' as tipo, 
a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 3 as orden, a.pkyAccountsPayable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableConcept b, agnPerson e, actCurrency f
where 
$valor_con_cxp_det 
$valor_ver_cxp_det 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsPayableConcept and

a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_pago as fecha_pago, t.concepto as concepto, t.concepto_gasto as concepto_gasto, t.via as via, t.empleado as empleado, t.moneda as moneda, t.glosa as glosa, t.cxp as cxp, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, 1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa
from (
select a.pkyAccountsPayable as codigo, a.fldDocumentNumber as nrodoc, a.fldDate as fecha, a.fldPaymentDate as fecha_pago, b.fldName as concepto, null as concepto_gasto, null as via, 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.txtDescription as glosa, (a.fldTotal * -1) as cxp, 'CxP' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 3 as orden, a.pkyAccountsPayable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableConcept b, agnPerson e, actCurrency f
where 
$valor_con_cxp_det 
$valor_ver_cxp_det 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codConcept = b.pkyAccountsPayableConcept and

a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, otro.fecha as fecha, otro.fecha_cobro as fecha_cobro, otro.concepto as concepto, otro.concepto_ingreso as concepto_ingreso, otro.via as via, otro.empleado as empleado, otro.moneda as moneda, otro.glosa as glosa, otro.cxc as cxc, otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, otro.cod_moneda as cod_moneda, otro.tasa as tasa, otro.tasa * otro.cxc as total, otro.orden as orden, otro.codigo_orden as codigo_orden, otro.papa as papa, otro.cod_papa as cod_papa
from (
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_cobro as fecha_cobro, t.concepto as concepto, t.concepto_ingreso as concepto_ingreso, t.via as via, t.empleado as empleado, t.moneda as moneda, t.glosa as glosa, t.cxc as cxc, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa
from (
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, null as via, 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.txtDescription as glosa, a.fldTotal as cxc, 'CxC' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 1 as orden, a.pkyAccountsReceivable as codigo_orden,a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c, agnPerson e, actCurrency f
where 
$valor_con_cxc_det 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' 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) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, t.fecha_cobro as fecha_cobro, t.concepto as concepto, t.concepto_ingreso as concepto_ingreso, t.via as via, t.empleado as empleado, t.moneda as moneda, t.glosa as glosa, t.cxc as cxc, t.tipo as tipo, t.tc as tc, t.otra_moneda as otra_moneda, t.cod_moneda as cod_moneda, 1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, t.papa as papa, t.cod_papa as cod_papa
from (
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, null as via, 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.txtDescription as glosa, a.fldTotal as cxc, 'CxC' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, a.codCurrency as cod_moneda, 1 as orden, a.pkyAccountsReceivable as codigo_orden, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from colAccountsReceivable a, colAccountsReceivableConcept b, colIncomeConcept c,
agnPerson e, actCurrency f
where 
$valor_con_cxc_det 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' 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) t
where
t.cod_moneda = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, 
otro.fecha as fecha, otro.fecha_cob_pag as fecha_cob_pag, 
otro.concepto as concepto, otro.con_ing_egr as con_ing_egr, 
otro.via_pago as via_pago, otro.empleado as empleado, 
otro.moneda_cxp as moneda, otro.glosa as glosa, otro.pago as pago, 
otro.tipo as tipo, otro.tc as tc, otro.otra_moneda as otra_moneda, 
otro.cod_moneda_cxp as cod_moneda, otro.tasa as tasa, 
otro.tasa * otro.pago as total, otro.orden as orden, otro.codigo_orden as codigo_orden, 
otro.papa as papa, otro.cod_papa as cod_papa
from 
(select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_pago as via_pago, 
t.cod_moneda_cxp as cod_moneda_cxp, t.moneda_cxp as moneda_cxp, 
t.empleado as empleado,
t.moneda_pago as moneda_pago, t.glosa as glosa, 
t.pago as pago, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_pago as cod_moneda_pago, 
r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, null as via_pago, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxp, h.fldName as moneda_cxp,
f.fldName as moneda_pago, a.txtDescription as glosa, b.fldTotal as pago, 
'Pago' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_pago, 4 as orden, g.pkyAccountsPayable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payPayment a, payPaymentDetail b, agnPerson e, actCurrency f,
payAccountsPayable g, actCurrency h
where 
$valor_con_cxp_det2 
$valor_ver_cxp_det2 
$valor_contexto_det 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = g.pkyAccountsPayable and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.codEmployee = e.pkyPerson and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxp = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_pago as via_pago, 
t.cod_moneda_cxp as cod_moneda_cxp, t.moneda_cxp as moneda_cxp, 
t.empleado as empleado, 
t.moneda_pago as moneda_pago, t.glosa as glosa, 
t.pago as pago, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_pago as cod_moneda_pago, 
1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, null as via_pago, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxp, h.fldName as moneda_cxp,
f.fldName as moneda_pago, a.txtDescription as glosa, b.fldTotal as pago, 
'Pago' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_pago, 4 as orden, g.pkyAccountsPayable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payPayment a, payPaymentDetail b, agnPerson e, actCurrency f,
payAccountsPayable g, actCurrency h
where 
$valor_con_cxp_det2 
$valor_ver_cxp_det2 
$valor_contexto_det 
a.pkyPayment = b.codPayment and
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codEmployee is not null and
b.codAccountsPayable is not null and
b.codAccountsPayable = g.pkyAccountsPayable and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.codEmployee = e.pkyPerson and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_cxp = t.otra_moneda) otro

union all

select otro.codigo as codigo, otro.nrodoc as nrodoc, 
otro.fecha as fecha, otro.fecha_cob_pag as fecha_cob_pag, 
otro.concepto as concepto, otro.con_ing_egr as con_ing_egr, 
otro.via_cobro as via_cobro, otro.empleado as empleado, 
otro.moneda_cxc as moneda, otro.glosa as glosa, 
otro.cobro as cobro, otro.tipo as tipo, otro.tc as tc, 
otro.otra_moneda as otra_moneda, 
otro.cod_moneda_cxc as cod_moneda, otro.tasa as tasa, 
otro.tasa * otro.cobro as total, otro.orden as orden, otro.codigo_orden as codigo_orden,
otro.papa as papa, otro.cod_papa as cod_papa
from 
(select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_cobro as via_cobro, 
t.cod_moneda_cxc as cod_moneda_cxc, t.moneda_cxc as moneda_cxc,
t.empleado as empleado, 
t.moneda_cobro as moneda_cobro, t.glosa as glosa, 
t.cobro as cobro, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_cobro as cod_moneda_cobro, 
r.fldRate as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyCollection as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, c.fldName as via_cobro, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc,
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
colCollection a, colCollectionDetail b, colCollectionRoute c, agnPerson e, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codRoute = c.pkyCollectionRoute and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.pkyCollection = b.codCollection and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
UNION ALL
select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, 'Liquidacion' as via_cobro, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc, 
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
payPayment a, payPaymentDetail b, agnPerson e, 
actCurrency f, colAccountsReceivable g, actCurrency h
where
$valor_con_cxc_det2  
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.pkyPayment = b.codPayment and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
b.codAccountsReceivable is not NULL and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,
actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda_cxc = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all
select t.codigo as codigo, t.nrodoc as nrodoc, t.fecha as fecha, 
t.fecha_cob_pag as fecha_cob_pag, t.concepto as concepto, 
t.con_ing_egr as con_ing_egr, t.via_cobro as via_cobro, 
t.cod_moneda_cxc as cod_moneda_cxc, t.moneda_cxc as moneda_cxc,
t.empleado as empleado, 
t.moneda_cobro as moneda_cobro, t.glosa as glosa, 
t.cobro as cobro, t.tipo as tipo, t.tc as tc, 
t.otra_moneda as otra_moneda, t.cod_moneda_cobro as cod_moneda_cobro, 
1 as tasa, t.orden as orden, t.codigo_orden as codigo_orden, 
t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyCollection as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, c.fldName as via_cobro, case when e.fldMiddleName is NULL then concat(e.fldLastName,', ',e.fldFirstName) else concat(e.fldLastName,' ',e.fldMiddleName,', ',e.fldFirstName) end as empleado, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc,
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
colCollection a, colCollectionDetail b, colCollectionRoute c, agnPerson e, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.codRoute = c.pkyCollectionRoute and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
a.pkyCollection = b.codCollection and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1
UNION ALL
select a.pkyPayment as codigo, a.fldDocumentNumber as nrodoc, 
a.fldDate as fecha, null as fecha_cob_pag, null as concepto, 
null as con_ing_egr, 'Liquidacion' as via_cobro, concat(e.fldLastName,', ',e.fldFirstName) as empleado, 
g.codCurrency as cod_moneda_cxc, h.fldName as moneda_cxc, 
f.fldName as moneda_cobro, a.txtDescription as glosa, (b.fldTotal * -1) as cobro, 
'Cobro' as tipo, a.codExchangeRate as tc, '$otra_moneda_det' as otra_moneda, 
a.codCurrency as cod_moneda_cobro, 2 as orden, g.pkyAccountsReceivable as codigo_orden, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
payPayment a, payPaymentDetail b, agnPerson e, 
actCurrency f, colAccountsReceivable g, actCurrency h
where 
$valor_con_cxc_det2 
$valor_contexto_det 
a.codEmployee = '$nombredet' and
a.fldDate >= '$fechainidet' and
a.fldDate <= '$fechafindet' and
a.pkyPayment = b.codPayment and
a.codEmployee = e.pkyPerson and
a.codCurrency = f.pkyCurrency and
g.codCurrency = h.pkyCurrency and
b.codAccountsReceivable is not NULL and
b.codAccountsReceivable = g.pkyAccountsReceivable and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_cxc = t.otra_moneda) otro) final
$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_det; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Detalle Estado de Cuentas</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>Empleado: </b><?php echo $nomempdet; ?></TD>
        <TD width="25%"><b>Concepto de CxP: </b><?php echo $nom_con_cxp_det; ?></TD>
        <TD width="25%"><b>Concepto de CxC: </b><?php echo $nom_con_cxc_det; ?></TD>
        <TD width="25%"><b>Contexto: </b><?php echo $nom_contexto_det; ?></TD>
        </TR>
        <TR>
        <TD colspan="2"><b>De Fecha: </b><?php echo $fechainidet; ?> <b>a Fecha: </b><?php echo $fechafindet; ?></TD>
        <TD><b>Ordenado por: </b><?php echo $txt_orden_det; ?></TD>
        <TD><b>Expresado en: </b><?php echo $nom_otra_moneda_det; ?></TD>
    </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%">
	<TR BGCOLOR='#ffcccc'> 
 	<?php if($ordendet == 0) { // if orden ?>
 	<TD align="left" colspan="16"><b>Saldo inicial en <?php echo $nom_otra_moneda_det; ?></b></TD>
	<?php } else { // if else orden ?>
    <TD align="left" colspan="15"><b>Saldo inicial en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php }// fin if orden ?>
    <TD align="right"><b><?php echo number_format($totaldet,$n_decimales); ?></b></TD></TR> 
 	<thead>
    <TR> 
    <TD align="center"><b>Tipo</b></TD>
 	<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 Pago/Cobro</b></TD>
    <TD align="center"><b>Concepto</b></TD>
    <TD align="center"><b>Concepto Pago/Cobro</b></TD>
    <TD align="center"><b>Via Pago/Cobro</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</b></TD>
    <TD align="center"><b>Tasa</b></TD>
    <TD align="center"><b>Debito en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <TD align="center"><b>Cr&eacute;dito en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php if($ordendet == 0) { // if orden ?>
    <TD align="center"><b>Saldo en <?php echo $nom_otra_moneda_det; ?></b></TD>
    <?php }// fin if orden ?>
    </TR>
    </thead>
    <tbody class="marcafila">

<?php
$color = 2;
$suma_total = 0;
$suma_periodo = 0;
$suma_deb = 0;
$suma_cred = 0;
$saldo_det = $totaldet;
  
while ($row = mysqli_fetch_array($result2)) { //while

	//para enlazar al que genero el asiento
	$papa_componente = $row['papa'];
	$papa_codigo = $row['cod_papa'];
	$usuario = $usuario_det;
	include "../../generadopor.php";
		
	if($color % 2 == 0) { //if color
		if($row['orden'] == 2 || $row['orden'] == 4){ //if orden
		printf("<tr BGCOLOR='#e2e7fa'><td align='left'>%s %s</td>", $row['tipo'], $row['codigo_orden']);//tipo
		} else {
		printf("<tr BGCOLOR='#e2e7fa'><td align='left'>%s</td>", $row['tipo']);//tipo
		} //fin if orden
	} else {
		if($row['orden'] == 2 || $row['orden'] == 4){ //if orden
		printf("<tr><td align='left'>%s %s</td>", $row['tipo'], $row['codigo_orden']);//tipo
		} else {
		printf("<tr><td align='left'>%s</td>", $row['tipo']);//tipo
		} //fin if orden
	} //fin if color
	
	if($row['orden'] == 1) { //if orden 1
		$codigo_cxc = $row['codigo'];
		printf("<td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo_cxc&user=$usuario_det' title='Nota de Cuenta por Cobrar'>%s</a></td>", $row['codigo']);//codigo
	} //fin if orden 1
	if($row['orden'] == 2) { //if orden 2
		$cod_cobro = $row['codigo'];
		$tipo2 = $row['via_pagocob'];
		if($tipo2 == 'Liquidacion') {//if liquidacion
			printf("<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("<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
	} //fin if orden 2
	if($row['orden'] == 3) { //if orden 3
		$codigo_cxp = $row['codigo'];
		printf("<td align='center'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$codigo_cxp&user=$usuario_det' title='Nota de Cuenta por Pagar'>%s</a></td>", $row['codigo']);//codigo	
	}//fin if orden 3
	if($row['orden'] == 4) { //if orden 4
		$cod_pago = $row['codigo'];
		printf("<td align='center'><a href='../../ModulePayments/VoucherPayment/index.php?pkyPayment=$cod_pago&user=$usuario_det' title='Nota de Pago'>%s</a></td>", $cod_pago);//codigo	
	} //fin if orden 4
	
	printf("<td align='center'>%s</td>", $row['nrodoc']);//nro doc
	printf("<td align='right'>%s</td>", $row['fecha']);//fecha
	printf("<td align='right'>%s</td>", $row['fecha_pagocob']);//fecha pago o cobro
	printf("<td>%s</td>", $row['concepto']);//concepto
	printf("<td>%s</td>", $row['concepto_pagocob']);//concepto pago o cobro
	printf("<td>%s</td>", $row['via_pagocob']);//via pago o cobro
	printf("<td>%s</td>", $row['empleado']);//empleado
	printf("<td>%s</td>", $row['glosa']);//glosa
	printf("<td align='center'>%s</td>", $papa_enlace);//gen por
	printf("<td>%s</td>", $row['moneda']);//moneda
	
	if($row['total'] >= 0) {
		printf("<td align='right'>%s</td>", number_format($row['total'],$n_decimales));//total
	} else {
		printf("<td align='right'>%s</td>", number_format($row['total']*-1,$n_decimales));//total
	}
	
	printf("<td>%s</td>", number_format($row['tasa'],$n_tipocambio));//tasa
	
	if($row['orden'] == 1 || $row['orden'] == 4) {//if cxc y pago
		printf("<td align='right'>%s</td>", number_format($row['total_om'],$n_decimales));//total otra moneda debito
		printf("<td>&nbsp;</td>");//total otra moneda credito
		$suma_deb = $suma_deb + $row['total_om'];
	} //fin if cxc y pago
	if($row['orden'] == 2 || $row['orden'] == 3) { //if cobro y cxp
		printf("<td>&nbsp;</td>");//total otra moneda debito
		printf("<td align='right'>%s</td>", number_format($row['total_om']*-1,$n_decimales));//total otra moneda credito
		$suma_cred = $suma_cred + $row['total_om']*-1;
	} //fin if cobro y cxp
	
	if($ordendet == 0) { //if orden
    $saldo_det = $saldo_det + $row['total_om'];
	printf("<td align='right'>%s</td>", number_format($saldo_det,$n_decimales));//saldo
    } //fin if orden
		
	echo '</tr>';
	$suma_periodo = $suma_periodo + $row['total_om'];
	$color++;
	
	//muestra el detalle de la nota de venta
	
	if($row['orden'] == 1 && $ventadetalledet == 1 && strcmp($row['papa'],'ComponentSalesNote') == 0) { //if venta detalle orden 1.1
		$cod_venta = $row['cod_papa'];
		
		if($ordendet == 0) { // if orden 0.0
		echo '<tr><td colspan="16">';	
		include 'detail2.php';
		echo '</td></tr>';
		echo '<tr BGCOLOR="#66ff99"><td colspan="16">&nbsp;</td></tr>';	
		} else { // else if orden 0.0
		echo '<tr><td colspan="15">';	
		include 'detail2.php';
		echo '</td></tr>';
		echo '<tr BGCOLOR="#66ff99"><td colspan="15">&nbsp;</td></tr>';	
		} //fin if orden 0.0
		echo '<TR> 
		<TD align="center"><b>Tipo</b></TD>
		<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 Pago/Cobro</b></TD>
		<TD align="center"><b>Concepto</b></TD>
		<TD align="center"><b>Concepto Pago/Cobro</b></TD>
		<TD align="center"><b>Via Pago/Cobro</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</b></TD>
		<TD align="center"><b>Tasa</b></TD>
		<TD align="center"><b>Debito en '.$nom_otra_moneda_det.'</b></TD>
		<TD align="center"><b>Cr&eacute;dito en '.$nom_otra_moneda_det.'</b></TD>';
		if($ordendet == 0) { // if orden
		echo '<TD align="center"><b>Saldo en '.$nom_otra_moneda_det.'</b></TD>';
		}// fin if orden
		echo '</TR>';
	} //fin if venta detalle orden 1.1
$papa_componente = NULL;
$papa_codigo = NULL;
$usuario = NULL;		
}//fin while
	mysqli_free_result($result2);
	$suma_total = $suma_periodo + $totaldet; 
 ?>
<TR BGCOLOR='#ffcccc'> 
 <TD align="left" colspan="14"><b>Total Periodo en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <TD align="right"><b><?php echo number_format($suma_deb,$n_decimales); ?></b></TD>
 <TD align="right"><b><?php echo number_format($suma_cred,$n_decimales); ?></b></TD>
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="right"><b><?php echo number_format($suma_periodo,$n_decimales); ?></b></TD>
 <?php }// fin if orden ?>
 </TR> 
 <TR BGCOLOR='#ffcccc'> 
 <?php if($ordendet == 0) { // if orden ?>
 <TD align="left" colspan="16"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php } else { // if else orden ?>
 <TD align="left" colspan="15"><b>Total en <?php echo $nom_otra_moneda_det; ?></b></TD>
 <?php }// fin if orden ?>
 <TD align="right"><b><?php echo number_format($suma_total,$n_decimales); ?></b></TD></TR> 
</tbody>
</table>
  <?php
 } //fin if 3.2
 
} //fin if 3
?>

</body>
</html>
