<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Movimientos Bancarios</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 "../../token.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$banco=$_POST["banco"];
$concepto=$_POST["concepto"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
$otra_moneda=$_POST["otra_moneda"];
$solo_saldos=$_POST["solo_saldos"];
$cod_contexto=$_POST["contexto"];
$orden=$_POST["orden"];
//$usuario=$_POST["usuario"];
/*
$fechainidet=$_GET["fechainidet"];
$fechafindet=$_GET["fechafindet"];
$nombredet=$_GET["nombredet"];
$totaldet=$_GET["totaldet"];
$empresa=$_GET["empresa"];

$otra_moneda_det=$_GET["otra_moneda_det"];
*/
//$n_decimales = 6;

?>
<body>
<?php

//Inicio reporte Movimientos Bancarios
if($concepto == -1) { $nom_concepto = 'Abono por cheque'; }
if($concepto == -2) { $nom_concepto = 'Egreso por cheque'; }
//verificar si es solo saldos

if($solo_saldos == 1) {//if solo_saldos
//el algoritmo con detalle contempla la fechaini solo MENOR por lo que hay que sumarle 1 dia
$fechaini= date("Y-m-d", strtotime("$fechafin + 1 days"));  
$concepto = 0;
//echo $fechaini;
}//fin if solo_saldos

//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);

	if($otra_moneda == 0){
		$nom_otra_moneda = 'Moneda Original';
	}

//valor del codigo del ultimo tipo de cambio (se cambio fecha_hoy por fechafin) para dar el TC a la fecha solicitada

$resultom = mysqli_query($link, "SELECT T2.pkyExchangeRate as tc FROM 
(SELECT fldDate, MAX(fldTime) AS fldTime FROM actExchangeRate WHERE fldExclusive = '0' GROUP BY fldDate) as T1,
actExchangeRate T2 
WHERE 
T1.fldDate = (SELECT MAX(fldDate) FROM actExchangeRate WHERE fldDate <= '$fechafin' AND fldExclusive = '0') AND 
T2.fldDate = T1.fldDate AND 
T2.fldTime = T1.fldTime AND 
sysEnabled = 1 AND sysAuthorized = 1;");
while ($row = mysqli_fetch_array($resultom)) {
	$ultimo_tc = $row['tc'];
	}
	mysqli_free_result($resultom);

//tipo de cambio para la cabecera
$resulttc = mysqli_query($link, "select pkyExchangeRate as cod_tc, fldDate as fecha, fldTime as hora
from
actExchangeRate
where
pkyExchangeRate = '$ultimo_tc' and 
sysEnabled = 1 and sysAuthorized = 1;");
while ($row = mysqli_fetch_array($resulttc)) {
	$tc_fecha = $row['fecha'];
	$tc_hora = $row['hora'];
}
mysqli_free_result($resulttc);

$resulttcdet = mysqli_query($link, "select a.pkyExchangeRateDetail as cod_tc_det, a.codBaseCurrency as cod_mon_target, b.fldName as mon_target, b.txtDescription as nom_mon_target, a.codTargetCurrency as cod_mon_base, c.fldName as mon_base, c.txtDescription as nom_mon_base, a.fldRate as tasa
from
actExchangeRateDetail a, actCurrency b, actCurrency c
where
a.codExchangeRate = '$ultimo_tc' and 
a.codBaseCurrency = '$cod_moneda_principal' and 
a.codBaseCurrency = b.pkyCurrency and 
a.codTargetCurrency = c.pkyCurrency 
order by cod_mon_target, cod_mon_base;");
$ma_tc = array();
$m = 1;
while ($row = mysqli_fetch_array($resulttcdet)) {
	$ma_tc [$m][1] = $row['mon_base'];
	$ma_tc [$m][2] = $row['mon_target'];
	$ma_tc [$m][3] = $row['tasa'];
	$m++;
}
mysqli_free_result($resulttcdet);
$fin_m = $m - 1;



//Inicio muestra cuentas
if($rep == 10 && $tipo == 1) { // if 2

	if ($banco == 0) { // if 2.2
		$nom_cuenta = 'Todos';
		$cuenta_de_banco = ' ';
	} else {
		$result = mysqli_query($link, "select a.pkyBankDetail as cod_cta, a.codBank as cod_banco, b.fldName as banco, a.fldNumber as nro_cta
from
tryBankDetail a, tryBank b
where
a.pkyBankDetail = '$banco' and
a.codBank = b.pkyBank and
a.sysEnabled = 1 and a.sysAuthorized = 1
order by a.codBank, a.pkyBankDetail;");
		while ($row = mysqli_fetch_array($result)) {   
		$var0 = $row['cod_cta'];
		$var1 = $row['banco'];
		$var2 = $row['nro_cta'];
		$nom_cuenta = $var0.' - '.$var1.' ('.$var2.')';
		}
		$cuenta_de_banco = ' a.pkyBankDetail = '.$banco. ' and ';
		//solo controlo que la cuenta sea la que viene de index si quito $cuenta_de_banco del query listado de cuentas funciona como todos.!!
		//echo $cuenta_de_banco;
		mysqli_free_result($result);
	}//fin if 2.2

	//contexto
	if($cod_contexto == -1){ 
		$nom_contexto = 'Todos';
		$valor_contexto = ' ';
		$valor_contexto2 = ' ';
		$valor_contexto3 = ' ';
		$valor_contexto4 = ' ';
	} 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 ';	
		$valor_contexto2 = 'where fin.cod_contexto = '.$cod_contexto;
		$valor_contexto3 = 'where t.cod_contexto = '.$cod_contexto;
		$valor_contexto4 = ' t.cod_contexto = '.$cod_contexto.' and ';
	}	
	
	if ($orden == 0) { // if 2.2.1
		$nom_orden = 'Fecha';
		$valor_orden = 'order by cod_banco, cod_cta, fecha, codigo';
	}//fin if 2.2.1  
	if ($orden == 1) { // if 2.2.2
		$nom_orden = 'C&oacute;digo';
		$valor_orden = 'order by cod_banco, cod_cta, codigo';
	}//fin if 2.2.2  
	
	if ($concepto == 0) { // if 2.3
		$nom_concepto = 'Todos';
		$concepto_de_movimiento = ' ';
		$cheques = " union all 
select a.pkyCheck as codigo, a.fldDate as fecha, 
a.codBank as cod_banco, b.fldName as banco, 
a.codLocalAccount as cod_cta, c.fldNumber as nro_cta,
a.codCurrency as cod_moneda, a.codCurrency as cod_moneda_cta,
f.fldName as moneda, 'Débito' as tipo, 
concat('Egreso por cheque Nro. ', a.fldNumber) as concepto,
a.fldRealTotal * -1 as total, 1 as tc, a.txtDescription as glosa, 
case a.fldParentComponent when 'ComponentPayment' then (select sysContext from payPayment where pkyPayment=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentSalesNote' then (select sysContext from slsSalesNote where pkySalesNote=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentCollection' then (select sysContext from colCollection where pkyCollection=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentEarlyPayment' then (select sysContext from colEarlyPayment where pkyEarlyPayment=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentEarlyCollection' then (select sysContext from payEarlyCollection where pkyEarlyCollection=a.cosComponentKey)
end end end end end as cod_contexto
from
tryCheck a, tryBank b, tryBankDetail c, actCurrency f
where
a.fldDate < '".$fechaini."' and
a.codBank = b.pkyBank and
a.codLocalAccount = c.pkyBankDetail and
a.codCurrency = f.pkyCurrency and
a.fldReceived = 0 and
(a.codStatus = 2 or a.codStatus = 5) and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
union all
select a.pkyCheckDeposit as codigo, a.fldDate as fecha, 
a.codBank as cod_banco, b.fldName as banco, 
a.codBankAccount as cod_cta, c.fldNumber as nro_cta,
c.codCurrency as cod_moneda, c.codCurrency as cod_moneda_cta,
f.fldName as moneda, 'Abono' as tipo, 
'Abono por cheque(s)' as concepto,
a.fldTotal as total, a.codExchangeRate as tc, a.txtDescription as glosa, 
a.sysContext as cod_contexto
from
tryCheckDeposit a, tryBank b, tryBankDetail c, actCurrency f
where
a.fldDate < '".$fechaini."' and
a.codBank = b.pkyBank and
a.codBankAccount = c.pkyBankDetail and
c.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1";

$cheques_det = " union all
select a.pkyCheck as codigo, a.fldDate as fecha, 
a.codBank as cod_banco, b.fldName as banco, 
a.codLocalAccount as cod_cta, c.fldNumber as nro_cta,
a.codCurrency as cod_moneda, a.codCurrency as cod_moneda_cta,
f.fldName as moneda, f.fldName as moneda_cta, 'Débito' as tipo, 
concat('Egreso por cheque Nro. ', a.fldNumber) as concepto,
a.fldRealTotal * -1 as total, 1 as tc, a.txtDescription as glosa, a.codStatus as cod_estado, a.fldParentComponent as papa, a.cosComponentKey as cod_papa, 
case a.fldParentComponent when 'ComponentPayment' then (select sysContext from payPayment where pkyPayment=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentSalesNote' then (select sysContext from slsSalesNote where pkySalesNote=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentCollection' then (select sysContext from colCollection where pkyCollection=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentEarlyPayment' then (select sysContext from colEarlyPayment where pkyEarlyPayment=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentEarlyCollection' then (select sysContext from payEarlyCollection where pkyEarlyCollection=a.cosComponentKey)
end end end end end as cod_contexto
from
tryCheck a, tryBank b, tryBankDetail c, actCurrency f
where
a.fldDate >= '".$fechaini."' and
a.fldDate <= '".$fechafin."' and
a.codBank = b.pkyBank and
a.codLocalAccount = c.pkyBankDetail and
a.codCurrency = f.pkyCurrency and
a.fldReceived = 0 and
(a.codStatus = 2 or a.codStatus = 5) and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
union all 
select a.pkyCheckDeposit as codigo, a.fldDate as fecha, 
a.codBank as cod_banco, b.fldName as banco, 
a.codBankAccount as cod_cta, c.fldNumber as nro_cta,
c.codCurrency as cod_moneda, c.codCurrency as cod_moneda_cta,
f.fldName as moneda, f.fldName as moneda_cta, 'Abono' as tipo, 
'Abono por cheque(s)' as concepto,
a.fldTotal as total, a.codExchangeRate as tc, a.txtDescription as glosa, -1 as cod_estado, a.fldParentComponent as papa, a.cosComponentKey as cod_papa, 
a.sysContext as cod_contexto
from
tryCheckDeposit a, tryBank b, tryBankDetail c, actCurrency f
where
a.fldDate >= '".$fechaini."' and
a.fldDate <= '".$fechafin."' and
a.codBank = b.pkyBank and
a.codBankAccount = c.pkyBankDetail and
c.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1";
//echo $cheques;
	} else {
		$result2 = mysqli_query($link, "select pkyBankMovementTypeConcept as cod_con, fldName as nombre
from
tryBankMovementTypeConcept
where
pkyBankMovementTypeConcept = '$concepto' and
sysEnabled = 1 and sysAuthorized = 1
order by fldName;");
		while ($row = mysqli_fetch_array($result2)) {   
		$nom_concepto = $row['nombre'];
		}
		$concepto_de_movimiento = ' a.codTypeConcept = '.$concepto. ' and';
		$cheques = ' ';
		$cheques_det = ' ';
		//echo $concepto_de_movimiento;
		mysqli_free_result($result2);
	}//fin if 2.3
	
		//listado de cuentas
		$result = mysqli_query($link, "select a.pkyBankDetail as cod_cta, a.codBank as cod_banco, 
b.fldName as banco, a.fldNumber as nro_cta, a.codCurrency as cod_moneda_cta, c.fldName as moneda_cta 
from
tryBankDetail a, tryBank b, actCurrency c
where
$cuenta_de_banco
a.codBank = b.pkyBank and
a.codCurrency = c.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1
order by a.codBank, a.pkyBankDetail;");
		
		//inicio calculo saldo inicial
	
	if($concepto == -1 || $concepto == -2){
		if($concepto == -1){
		$result2 = mysqli_query($link, "select fin.cod_banco as cod_banco, fin.cod_cta as cod_cta, 
fin.tc as tc, fin.cod_moneda as cod_moneda, 
fin.cod_moneda_cta as cod_moneda_cta, fin.total as total,
fin.tasa as tasa, sum(fin.totalfin) as totalfin
from
(select a.pkyCheckDeposit as codigo, a.fldDate as fecha, 
a.codBank as cod_banco, b.fldName as banco, 
a.codBankAccount as cod_cta, c.fldNumber as nro_cta,
c.codCurrency as cod_moneda, c.codCurrency as cod_moneda_cta,
f.fldName as moneda, 'Abono' as tipo, 
'Abono por cheque(s)' as concepto,
a.fldTotal as total, a.codExchangeRate as tc, a.txtDescription as glosa, 1 as tasa, a.fldTotal as totalfin
from
tryCheckDeposit a, tryBank b, tryBankDetail c, actCurrency f
where
$valor_contexto 
a.fldDate < '$fechaini' and
a.codBank = b.pkyBank and
a.codBankAccount = c.pkyBankDetail and
c.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1) fin
group by fin.cod_cta;");
		} else {
		$result2 = mysqli_query($link, "select fin.cod_banco as cod_banco, fin.cod_cta as cod_cta, 
fin.tc as tc, fin.cod_moneda as cod_moneda, 
fin.cod_moneda_cta as cod_moneda_cta, fin.total as total,
fin.tasa as tasa, sum(fin.totalfin) as totalfin
from
(select a.pkyCheck as codigo, a.fldDate as fecha, 
a.codBank as cod_banco, b.fldName as banco, 
a.codLocalAccount as cod_cta, c.fldNumber as nro_cta,
a.codCurrency as cod_moneda, a.codCurrency as cod_moneda_cta,
f.fldName as moneda, 'Débito' as tipo, 
concat('Egreso por cheque Nro. ', a.fldNumber) as concepto,
a.fldRealTotal * -1 as total, 1 as tc, a.txtDescription as glosa, 1 as tasa, a.fldRealTotal * -1 as totalfin, 
case a.fldParentComponent when 'ComponentPayment' then (select sysContext from payPayment where pkyPayment=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentSalesNote' then (select sysContext from slsSalesNote where pkySalesNote=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentCollection' then (select sysContext from colCollection where pkyCollection=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentEarlyPayment' then (select sysContext from colEarlyPayment where pkyEarlyPayment=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentEarlyCollection' then (select sysContext from payEarlyCollection where pkyEarlyCollection=a.cosComponentKey)
end end end end end as cod_contexto
from
tryCheck a, tryBank b, tryBankDetail c, actCurrency f
where
a.fldDate < '$fechaini' and
a.codBank = b.pkyBank and
a.codLocalAccount = c.pkyBankDetail and
a.codCurrency = f.pkyCurrency and
a.fldReceived = 0 and
(a.codStatus = 2 or a.codStatus = 5) and 
a.sysEnabled = 1 and a.sysAuthorized = 1) fin
$valor_contexto2 
group by fin.cod_cta;");	
		}//fin if $concepto = -1
	
	} else {
		
		$result2 = mysqli_query($link, "select fin.cod_banco as cod_banco, fin.cod_cta as cod_cta, 
fin.tc as tc, fin.cod_moneda as cod_moneda, 
fin.cod_moneda_cta as cod_moneda_cta, fin.total as total,
fin.tasa as tasa, sum(fin.totalfin) as totalfin, fin.cod_contexto as cod_contexto
from
(select otro.cod_banco as cod_banco, otro.cod_cta as cod_cta, 
otro.tc as tc, otro.cod_moneda as cod_moneda, 
otro.cod_moneda_cta as cod_moneda_cta, otro.total as total,
ca.fldRate as tasa, otro.total * ca.fldRate as totalfin, otro.cod_contexto as cod_contexto
from
(select t.cod_banco as cod_banco, t.cod_cta as cod_cta, 
t.tc as tc, t.cod_moneda as cod_moneda, 
t.cod_moneda_cta as cod_moneda_cta, sum(t.total) as total, t.cod_contexto as cod_contexto
from
(select a.pkyBankMovement as codigo, a.fldDate as fecha, a.codBank as cod_banco, 
b.fldName as banco, a.codBankAccount as cod_cta, 
c.fldNumber as nro_cta, a.codCurrency as cod_moneda, c.codCurrency as cod_moneda_cta,
f.fldName as moneda, d.fldName as tipo, g.fldName as concepto, 
case a.codType when 1 then a.fldTotal when 2 then a.fldTotal * -1 end as total, 
a.codExchangeRate as tc, a.txtDescription as glosa, a.sysContext as cod_contexto
from
tryBankMovement a, tryBank b, tryBankDetail c, tryBankMovementType d, 
actCurrency f, tryBankMovementTypeConcept g
where 
a.fldDate < '$fechaini' and
a.codBank = b.pkyBank and
a.codBankAccount = c.pkyBankDetail and
a.codCurrency = f.pkyCurrency and
a.codType = d.pkyBankMovementType and
$concepto_de_movimiento
a.codTypeConcept = g.pkyBankMovementTypeConcept and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
$valor_contexto3 
group by t.cod_cta, t.tc, t.cod_moneda) otro, actExchangeRateDetail ca
where
otro.tc = ca.codExchangeRate and
otro.cod_moneda = ca.codTargetCurrency and
otro.cod_moneda_cta = ca.codBaseCurrency

union all

select otro.cod_banco as cod_banco, otro.cod_cta as cod_cta, 
otro.tc as tc, otro.cod_moneda as cod_moneda, 
otro.cod_moneda_cta as cod_moneda_cta, otro.total as total,
1 as tasa, otro.total * 1 as totalfin, otro.cod_contexto as cod_contexto
from
(select t.cod_banco as cod_banco, t.cod_cta as cod_cta, 
t.tc as tc, t.cod_moneda as cod_moneda, 
t.cod_moneda_cta as cod_moneda_cta, sum(t.total) as total, t.cod_contexto as cod_contexto
from
(select a.pkyBankMovement as codigo, a.fldDate as fecha, a.codBank as cod_banco, 
b.fldName as banco, a.codBankAccount as cod_cta, 
c.fldNumber as nro_cta, a.codCurrency as cod_moneda, c.codCurrency as cod_moneda_cta,
f.fldName as moneda, d.fldName as tipo, g.fldName as concepto, 
case a.codType when 1 then a.fldTotal when 2 then a.fldTotal * -1 end as total, 
a.codExchangeRate as tc, a.txtDescription as glosa, a.sysContext as cod_contexto
from
tryBankMovement a, tryBank b, tryBankDetail c, tryBankMovementType d, 
actCurrency f, tryBankMovementTypeConcept g
where
a.fldDate < '$fechaini' and
a.codBank = b.pkyBank and
a.codBankAccount = c.pkyBankDetail and
a.codCurrency = f.pkyCurrency and
a.codType = d.pkyBankMovementType and
$concepto_de_movimiento
a.codTypeConcept = g.pkyBankMovementTypeConcept and
a.sysEnabled = 1 and a.sysAuthorized = 1
$cheques) t 
$valor_contexto3 
group by t.cod_cta, t.tc, t.cod_moneda) otro
where
otro.cod_moneda = otro.cod_moneda_cta) fin
$valor_contexto2 
group by fin.cod_cta;");
	}//fin if $concepto = -1 o -2

$matriz_salini = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_salini [$i][1] = $row['cod_banco'];
	$matriz_salini [$i][2] = $row['banco'];
	$matriz_salini [$i][3] = $row['cod_cta'];
	$matriz_salini [$i][4] = $row['nro_cta'];
	$matriz_salini [$i][5] = 0;
	$matriz_salini [$i][6] = $row['cod_moneda_cta'];
	$matriz_salini [$i][7] = $row['moneda_cta'];
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);
//saldo inicial
while ($row = mysqli_fetch_array($result2)) {
	$i = 1;
	$cod_banco = $row['cod_banco'];
	$cod_cta = $row['cod_cta'];
	while ($i <= $fin) {
		if ($cod_banco == $matriz_salini [$i][1] && $cod_cta == $matriz_salini [$i][3]) {
		$matriz_salini [$i][5] = $row['totalfin'];
		}
	$i++;
	}
}
mysqli_free_result($result2);

	
//fin calculo saldo inicial

//inicio calculo movimientos bancarios

	if($concepto == -1 || $concepto == -2){
		if($concepto == -1){
		$result3 = mysqli_query($link, "select a.pkyCheckDeposit as codigo, a.fldDate as fecha, 
a.codBank as cod_banco, b.fldName as banco, 
a.codBankAccount as cod_cta, c.fldNumber as nro_cta,
c.codCurrency as cod_moneda, c.codCurrency as cod_moneda_cta,
f.fldName as moneda, f.fldName as moneda_cta, 'Abono' as tipo, 
'Abono por cheque(s)' as concepto,
a.fldTotal as total, a.codExchangeRate as tc, a.txtDescription as glosa,
1 as tasa, a.fldTotal as totalfin, -1 as cod_estado, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from
tryCheckDeposit a, tryBank b, tryBankDetail c, actCurrency f
where
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codBank = b.pkyBank and
a.codBankAccount = c.pkyBankDetail and
c.codCurrency = f.pkyCurrency and
a.sysEnabled = 1 and a.sysAuthorized = 1
$valor_orden;");
		} else {
		$result3 = mysqli_query($link, "select t.codigo as codigo, t.fecha as fecha, t.cod_banco as cod_banco, t.banco as banco,t.cod_cta as cod_cta, t.nro_cta as nro_cta,
t.cod_moneda as cod_moneda, t.cod_moneda_cta as cod_moneda_cta, 
t.moneda as moneda, t.moneda_cta as moneda_cta, 
t.tipo as tipo, t.concepto as concepto,
t.total as total, t.tc as tc, t.glosa as glosa, t.tasa as tasa,
t.total * t.tasa as totalfin, t.cod_estado as cod_estado, 
t.papa as papa, t.cod_papa as cod_papa, t.cod_contexto as cod_contexto
from
(select a.pkyCheck as codigo, a.fldDate as fecha, 
a.codBank as cod_banco, b.fldName as banco, 
a.codLocalAccount as cod_cta, c.fldNumber as nro_cta,
a.codCurrency as cod_moneda, a.codCurrency as cod_moneda_cta,
f.fldName as moneda, f.fldName as moneda_cta, 'Débito' as tipo, 
concat('Egreso por cheque Nro. ', a.fldNumber) as concepto,
a.fldRealTotal * -1 as total, 1 as tc, a.txtDescription as glosa,
1 as tasa, a.fldRealTotal * -1 as totalfin, a.codStatus as cod_estado, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa, 
case a.fldParentComponent when 'ComponentPayment' then (select sysContext from payPayment where pkyPayment=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentSalesNote' then (select sysContext from slsSalesNote where pkySalesNote=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentCollection' then (select sysContext from colCollection where pkyCollection=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentEarlyPayment' then (select sysContext from colEarlyPayment where pkyEarlyPayment=a.cosComponentKey) else 
case a.fldParentComponent when 'ComponentEarlyCollection' then (select sysContext from payEarlyCollection where pkyEarlyCollection=a.cosComponentKey)
end end end end end as cod_contexto
from
tryCheck a, tryBank b, tryBankDetail c, actCurrency f
where
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codBank = b.pkyBank and
a.codLocalAccount = c.pkyBankDetail and
a.codCurrency = f.pkyCurrency and
a.fldReceived = 0 and
(a.codStatus = 2 or a.codStatus = 5) and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t 
$valor_contexto3 
$valor_orden;");	
		}//fin if $concepto = -1
	
	} else {
		$result3 = mysqli_query($link, "select t.codigo as codigo, t.fecha as fecha, t.cod_banco as cod_banco, t.banco as banco,t.cod_cta as cod_cta, t.nro_cta as nro_cta,
t.cod_moneda as cod_moneda, t.cod_moneda_cta as cod_moneda_cta, 
t.moneda as moneda, t.moneda_cta as moneda_cta, 
t.tipo as tipo, t.concepto as concepto,
t.total as total, t.tc as tc, t.glosa as glosa, ca.fldRate as tasa,
t.total * ca.fldRate as totalfin, t.cod_estado as cod_estado, 
t.papa as papa, t.cod_papa as cod_papa, t.cod_contexto as cod_contexto
from
(select a.pkyBankMovement as codigo, a.fldDate as fecha, 
a.codBank as cod_banco, b.fldName as banco, a.codBankAccount as cod_cta, 
c.fldNumber as nro_cta, a.codCurrency as cod_moneda, 
c.codCurrency as cod_moneda_cta, 
f.fldName as moneda, h.fldName as moneda_cta, 
d.fldName as tipo, g.fldName as concepto, 
case a.codType when 1 then a.fldTotal when 2 then a.fldTotal * -1 end as total, 
a.codExchangeRate as tc, a.txtDescription as glosa, 0 as cod_estado, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa, a.sysContext as cod_contexto
from
tryBankMovement a, tryBank b, tryBankDetail c, tryBankMovementType d, 
actCurrency f, tryBankMovementTypeConcept g, actCurrency h
where
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codBank = b.pkyBank and
a.codBankAccount = c.pkyBankDetail and
a.codCurrency = f.pkyCurrency and
c.codCurrency = h.pkyCurrency and
a.codType = d.pkyBankMovementType and
$concepto_de_movimiento
$valor_contexto 
a.codTypeConcept = g.pkyBankMovementTypeConcept and
a.sysEnabled = 1 and a.sysAuthorized = 1)t, actExchangeRateDetail ca
where
t.tc = ca.codExchangeRate and
t.cod_moneda = ca.codTargetCurrency and
t.cod_moneda_cta = ca.codBaseCurrency

union all

select t.codigo as codigo, t.fecha as fecha, t.cod_banco as cod_banco, 
t.banco as banco,t.cod_cta as cod_cta, t.nro_cta as nro_cta,
t.cod_moneda as cod_moneda, t.cod_moneda_cta as cod_moneda_cta, 
t.moneda as moneda, t.moneda_cta as moneda_cta, 
t.tipo as tipo, t.concepto as concepto,
t.total as total, t.tc as tc, t.glosa as glosa, 1 as tasa,
t.total * 1 as totalfin, t.cod_estado as cod_estado, 
t.papa as papa, t.cod_papa as cod_papa, t.cod_contexto as cod_contexto
from
(select a.pkyBankMovement as codigo, a.fldDate as fecha, 
a.codBank as cod_banco, b.fldName as banco, a.codBankAccount as cod_cta, 
c.fldNumber as nro_cta, a.codCurrency as cod_moneda, c.codCurrency as cod_moneda_cta, 
f.fldName as moneda, h.fldName as moneda_cta, 
d.fldName as tipo, g.fldName as concepto, 
case a.codType when 1 then a.fldTotal when 2 then a.fldTotal * -1 end as total, 
a.codExchangeRate as tc, a.txtDescription as glosa, 0 as cod_estado, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa, a.sysContext as cod_contexto
from
tryBankMovement a, tryBank b, tryBankDetail c, tryBankMovementType d, 
actCurrency f, tryBankMovementTypeConcept g, actCurrency h
where
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codBank = b.pkyBank and
a.codBankAccount = c.pkyBankDetail and
a.codCurrency = f.pkyCurrency and
c.codCurrency = h.pkyCurrency and
a.codType = d.pkyBankMovementType and
$concepto_de_movimiento
$valor_contexto 
a.codTypeConcept = g.pkyBankMovementTypeConcept and
a.sysEnabled = 1 and a.sysAuthorized = 1 
$cheques_det)t
where
$valor_contexto4 
t.cod_moneda = t.cod_moneda_cta
$valor_orden;");
	}//fin if $concepto = -1 o -2
	
$matriz_detalle = array();
$i = 1;
while ($row = mysqli_fetch_array($result3)) {
	$matriz_detalle [$i][1] = $row['codigo'];
	$matriz_detalle [$i][2] = $row['fecha'];
	$matriz_detalle [$i][3] = $row['cod_banco'];
	$matriz_detalle [$i][4] = $row['banco'];
	$matriz_detalle [$i][5] = $row['cod_cta'];
	$matriz_detalle [$i][6] = $row['nro_cta'];
	$matriz_detalle [$i][7] = $row['cod_moneda'];
	$matriz_detalle [$i][8] = $row['moneda'];
	$matriz_detalle [$i][9] = $row['tipo'];
	$matriz_detalle [$i][10] = $row['concepto'];
	$matriz_detalle [$i][11] = $row['total'];
	$matriz_detalle [$i][12] = $row['tc'];
	$matriz_detalle [$i][13] = $row['glosa'];
	$matriz_detalle [$i][14] = $row['cod_moneda_cta'];
	$matriz_detalle [$i][15] = $row['moneda_cta'];
	$matriz_detalle [$i][16] = $row['tasa'];
	$matriz_detalle [$i][17] = $row['totalfin'];
	$matriz_detalle [$i][18] = $row['cod_estado'];
	//para enlazar al que genero el asiento
	$papa_componente = $row['papa'];
	$papa_codigo = $row['cod_papa'];
	//$usuario = $usuario_det;
	include "../../generadopor.php";
	$matriz_detalle [$i][19] = $papa_enlace; 
	$i++;
}
$fin_det = $i - 1;
mysqli_free_result($result3);

//fin calculo registros

if($solo_saldos == 1) {//if solo_saldos
?>
<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>Saldos Bancarios</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>Banco / Cuenta: </b><?php echo $nom_cuenta; ?></TD>
        <TD width="25%"><b>A Fecha: </b><?php echo $fechafin; ?></TD>
        <TD width="25%"><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        <TD width="25%"><b>Valorado 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; ?>&token=<?php echo $token; ?>" 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></tr>", 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>Banco</b></TD>
    <TD align='center'><b>Nro. Cuenta</b></TD>
    <TD align='center'><b>Mon.</b></TD>
    <?php if($otra_moneda != 0){ ?>
    <TD align='center' width='100'><b>Saldo</b></TD>
    <TD align='center'><b>Tasa en <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align='center' width='100'><b>Saldo en <?php echo $nom_otra_moneda; ?></b></TD>
    <?php } else { ?>
    <TD align='center' width='100'><b>Saldo</b></TD>
    <?php } ?>
    </TR>
    </thead>
    <tbody class="marcafila">
<?php
//extrae el tc a la fecha que pide el saldo (cambiar si es el TC a fecha por fechafin)
$resultom = mysqli_query($link, "SELECT T2.pkyExchangeRate as tc FROM 
(SELECT fldDate, MAX(fldTime) AS fldTime FROM actExchangeRate WHERE fldExclusive = '0' GROUP BY fldDate) as T1,
actExchangeRate T2 
WHERE 
T1.fldDate = (SELECT MAX(fldDate) FROM actExchangeRate WHERE fldDate <= '$fechafin' AND fldExclusive = '0') AND 
T2.fldDate = T1.fldDate AND 
T2.fldTime = T1.fldTime AND 
sysEnabled = 1 AND sysAuthorized = 1;");
while ($row = mysqli_fetch_array($resultom)) {
	$ultimo_tc1 = $row['tc'];
	}
	mysqli_free_result($resultom);
	
  $sum_sal_tot = 0;
  $j = 1;
  $color = 2;
  while ($j <= $fin) { //while 1
  	
	$cod_moneda_cuenta1 = $matriz_salini [$j][6];
	if($cod_moneda_cuenta1 == $otra_moneda) {//if monedas iguales
		$tasa_tc1 = number_format(1,5);
	} else {
	
	//valores de conversion tipo de cambio
$resulttc = mysqli_query($link, "SELECT pkyExchangeRateDetail as codigotc, codBaseCurrency as monedabase, codTargetCurrency as monedacambio, fldRate as tc
from
actExchangeRateDetail
where
codExchangeRate = '$ultimo_tc1' and
codBaseCurrency = '$otra_moneda' and
codTargetCurrency = '$cod_moneda_cuenta1' and
sysEnabled = 1 AND sysAuthorized = 1;");

while ($row = mysqli_fetch_array($resulttc)) {
	$tasa_tc1 = $row['tc'];//tasa tc
	}
	mysqli_free_result($resulttc);
	}//fin if monedas iguales
	if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='left'>%s</td>", $matriz_salini [$j][2]);//banco
	} else {
	printf("<tr><td align='left'>%s</td>", $matriz_salini [$j][2]);//banco
	}//cierra if color
	printf("<td align='left'>%s</td>", $matriz_salini [$j][4]);//nro. de cuenta
	printf("<td align='left'>%s</td>", $matriz_salini [$j][7]);//moneda de la cuenta
	
	if($otra_moneda != 0){
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][5],$n_decimales));//saldo inicial
	printf("<td align='right'>%s</td>", number_format($tasa_tc1, $n_tipocambio));//tasa a otra moneda
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][5] * $tasa_tc1,$n_decimales));//saldo inicial otra moneda
	} else {
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][5],$n_decimales));//saldo inicial
	}
  $sum_sal_tot = $sum_sal_tot + $matriz_salini [$j][5] * $tasa_tc1;
  $j++;
  $color++;
  }//fin while 1
	?>
	<?php if($otra_moneda != 0){ ?>
<tr BGCOLOR='#ffcccc'><td colspan='5'><b>Total Saldos Bancarios, Valorado en <?php echo $nom_otra_moneda; ?></b></td>
<td align='right'><b><?php echo number_format($sum_sal_tot,$n_decimales); ?></b></td></tr>
	<?php } ?>
</tbody>
</table>
<?php

} else {//else solo_saldos
	
?>
<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>Movimientos Bancarios</b></font></center></p>

<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>	
    <table border="1" align="center" cellpadding="2" width="100%">
        <TR> 
        <TD width="33%"><b>Banco / Cuenta: </b><?php echo $nom_cuenta; ?></TD>
        <TD width="33%"><b>Concepto: </b><?php echo $nom_concepto; ?></TD>
        <TD width="33%"><b>Ordenado por: </b><?php echo $nom_orden; ?></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>Valorado 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; ?>&token=<?php echo $token; ?>" 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></tr>", 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>C&oacute;digo</b></TD>
    <TD align="center"><b>Banco</b></TD>
	<TD align="center"><b>Nro. Cuenta</b></TD>
    <TD align="center" width="60"><b>Fecha</b></TD>
    <TD align="center"><b>Tipo</b></TD>
    <TD align="center"><b>Concepto</b></TD>
    <TD align="center"><b>Glosa</b></TD>
	<TD align="center"><b>Moneda</b></TD>
    <TD align="center" width="100"><b>D&eacute;bito</b></TD>
    <TD align="center" width="100"><b>Abono</b></TD>
    <TD align="center" width="100"><b>Saldo</b></TD>
    </TR>-->

  <?php
  $sum_deb_tot = 0;
  $sum_abo_tot = 0;
  $sum_sal_tot = 0;
  $suma_periodo = 0;
  $saldo_inicial = 0;
  $j = 1;
  
   
  
  while ($j <= $fin) { //while 1
  	
	$cod_moneda_cuenta1 = $matriz_salini [$j][6];
	if($cod_moneda_cuenta1 == $otra_moneda) {//if monedas iguales
		$tasa_tc1 = number_format(1,$n_tipocambio);
	} else {
	
	//valores de conversion tipo de cambio
$resulttc = mysqli_query($link, "SELECT pkyExchangeRateDetail as codigotc, codBaseCurrency as monedabase, codTargetCurrency as monedacambio, fldRate as tc
from
actExchangeRateDetail
where
codExchangeRate = '$ultimo_tc' and
codBaseCurrency = '$otra_moneda' and
codTargetCurrency = '$cod_moneda_cuenta1' and
sysEnabled = 1 AND sysAuthorized = 1;");

while ($row = mysqli_fetch_array($resulttc)) {
	$tasa_tc1 = $row['tc'];//tasa tc
	}
	mysqli_free_result($resulttc);
	}//fin if monedas iguales
	
    printf("<TR><TD align='center'><b>C&oacute;digo</b></TD>");
    printf("<TD align='center' width='65'><b>Fecha</b></TD>");
    printf("<TD align='center'><b>Concepto</b></TD>");
    printf("<TD align='center'><b>Glosa</b></TD>");
	printf("<TD align='center'><b>Gen. Por</b></TD>");
	printf("<TD align='center'><b>Mon. Mov.</b></TD>");
    printf("<TD align='center' width='100'><b>D&eacute;bito Mon. Mov.</b></TD>");
	printf("<TD align='center' width='100'><b>Cr&eacute;dito Mon. Mov.</b></TD>");
	//activar para mostrar columnas con el valor de la fecha en la que se hizo el movimiento
	//printf("<TD align='center'><b>Tasa a %s</b></TD>", $matriz_salini [$j][7]);
	//printf("<TD align='center' width='100'><b>D&eacute;bito en %s</b></TD>", $matriz_salini [$j][7]);
	//printf("<TD align='center' width='100'><b>Cr&eacute;dito en %s</b></TD>", $matriz_salini [$j][7]);
	if($otra_moneda != 0){
	printf("<TD align='center' width='100'><b>Saldo en %s</b></TD>", $matriz_salini [$j][7]);
	printf("<TD align='center'><b>Tasa en %s</b></TD>", $nom_otra_moneda);
	printf("<TD align='center' width='100'><b>Saldo en %s</b></TD></TR></thead><tbody class='marcafila'>", $nom_otra_moneda);
	} else {
	printf("<TD align='center' width='100'><b>Saldo en %s</b></TD></TR></thead><tbody class='marcafila'>", $matriz_salini [$j][7]);
	}
	printf("<tr><td colspan='8' align='left'><i><b>%s - %s, Nro. de Cuenta (%s), Moneda de la Cuenta %s</b></i> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;----------- Saldo inicial -----------</td>", $matriz_salini [$j][3], $matriz_salini [$j][2], $matriz_salini [$j][4], $matriz_salini [$j][7]);//saldo anterior y moneda de la cuenta
	
	if($otra_moneda != 0){
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][5],$n_decimales));//saldo inicial
	printf("<td align='right'>%s</td>", number_format($tasa_tc1, $n_tipocambio));//tasa a otra moneda
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][5] * $tasa_tc1,$n_decimales));//saldo inicial otra moneda
	} else {
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][5],$n_decimales));//saldo inicial
	}
	
	$suma_debito = 0;
  	$suma_abono = 0;
  	$saldo_inicial = $matriz_salini [$j][5];
	$suma_saldo = $matriz_salini [$j][5];
	$color = 2;
	$i = 1;
	while($i <= $fin_det) {//while 2
	  if($matriz_salini [$j][1] == $matriz_detalle [$i][3] && $matriz_salini [$j][3] == $matriz_detalle [$i][5]) { //if igual
	    
		if($matriz_detalle [$i][18] == 0) { // if = 0 mov de banco
			$cod_mov_banco = $matriz_detalle [$i][1]; 
					
			if($color % 2 == 0) { //if color
			printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleTreasury/VoucherBankMovement/index.php?pkyBankMovement=$cod_mov_banco&user=$usuario&token=$token' title='Nota de Movimiento Bancario'>%s</a></td>", $matriz_detalle [$i][1]);//cod
			} else {
			printf("<tr><td align='center'><a href='../../ModuleTreasury/VoucherBankMovement/index.php?pkyBankMovement=$cod_mov_banco&user=$usuario&token=$token' title='Nota de Movimiento Bancario'>%s</a></td>", $matriz_detalle [$i][1]);//cod
			}//fin if color
			
		} //fin if = 0 mov de banco
		
		if($matriz_detalle [$i][18] == 2 || $matriz_detalle [$i][18] == 5) { //if = 2 o 5 cheque
			$cod_cheque = $matriz_detalle [$i][1];
			
			if($color % 2 == 0) { //if color
			printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleTreasury/VoucherCheck/index.php?pkyCheck=$cod_cheque&user=$usuario&token=$token' title='Cheque'>%s</a></td>", $matriz_detalle [$i][1]);//cod
			} else {
			printf("<tr><td align='center'><a href='../../ModuleTreasury/VoucherCheck/index.php?pkyCheck=$cod_cheque&user=$usuario&token=$token' title='Cheque'>%s</a></td>", $matriz_detalle [$i][1]);//cod
			}//fin if color
			
		} //fin if = 2 o 5 cheque
		
		if($matriz_detalle [$i][18] == -1) { // if = 2 o 5 deposito
			$cod_dep_cheque = $matriz_detalle [$i][1];
			
			if($color % 2 == 0) { //if color
			printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleTreasury/VoucherCheckDeposit/index.php?pkyCheckDeposit=$cod_dep_cheque&user=$usuario&token=$token' title='Nota de Dep&oacute;sito de Cheque'>%s</a></td>", $matriz_detalle [$i][1]);//cod
			} else {
			printf("<tr><td align='center'><a href='../../ModuleTreasury/VoucherCheckDeposit/index.php?pkyCheckDeposit=$cod_dep_cheque&user=$usuario&token=$token' title='Nota de Dep&oacute;sito de Cheque'>%s</a></td>", $matriz_detalle [$i][1]);//cod
			}//fin if color
			
		} //fin if = -1 deposito
		
		printf("<td align='right'>%s</td>", $matriz_detalle [$i][2]);//fecha
		printf("<td align='left'>%s</td>", $matriz_detalle [$i][10]);//concepto
		printf("<td align='left'>%s</td>", $matriz_detalle [$i][13]);//glosa
		printf("<td align='center'>%s</td>", $matriz_detalle [$i][19]);//gen por
		printf("<td align='left'>%s</td>", $matriz_detalle [$i][8]);//moneda
		
		if($matriz_detalle [$i][11] < 0) {
		printf("<td align='right'>%s</td>", number_format($matriz_detalle [$i][11] * -1,$n_decimales));//debito moneda mov
		printf("<td>&nbsp;</td>");//abono
		$suma_debito = $suma_debito + $matriz_detalle [$i][17];
		} else {
		printf("<td>&nbsp;</td>");//debito
		printf("<td align='right'>%s</td>", number_format($matriz_detalle [$i][11],$n_decimales));//abono moneda mov
		$suma_abono = $suma_abono + $matriz_detalle [$i][17];
		}
		//activar para mostrar columnas con el valor de la fecha en la que se hizo el movimiento
		/*
		printf("<td align='right'>%s</td>", number_format($matriz_detalle [$i][16], $n_tipocambio));//tasa
		if($matriz_detalle [$i][17] < 0) {
		printf("<td align='right'>%s</td>", number_format($matriz_detalle [$i][17] * -1,$n_decimales));//debito moneda cuenta
		printf("<td>&nbsp;</td>");//abono
		
		} else {
		printf("<td>&nbsp;</td>");//debito
		printf("<td align='right'>%s</td>", number_format($matriz_detalle [$i][17],$n_decimales));//abono moneda cuenta
		}
		*/
		$suma_saldo = $suma_saldo + $matriz_detalle [$i][17];
		if($otra_moneda != 0){
		printf("<td align='right'>%s</td>", number_format($suma_saldo,$n_decimales));//saldo
		printf("<td align='right'>%s</td>", number_format($tasa_tc1, $n_tipocambio));//tasa a otra moneda
		printf("<td align='right'>%s</td></tr>", number_format($suma_saldo * $tasa_tc1,$n_decimales));//saldo a otra moneda
		} else{
		printf("<td align='right'>%s</td></tr>", number_format($suma_saldo,$n_decimales));//saldo
		}
		$color++;
	  }//fin if igual
	$i++; 
	}//fin while 2
	if($otra_moneda != 0){
	printf("<tr BGCOLOR='#ffcccc'><td colspan='8'><b>Total D&eacute;bitos del Periodo</b></td>");//titulo debitos
	printf("<td align='right'><b>%s</b></td>", number_format($suma_debito * -1,$n_decimales));//total debitos
	printf("<td align='right'><b>%s</b></td>", number_format($tasa_tc1, $n_tipocambio));//tasa a otra moneda
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_debito * - 1 * $tasa_tc1,$n_decimales));//total debitos
	printf("<tr BGCOLOR='#ffcccc'><td colspan='8'><b>Total Abonos del Periodo</b></td>");//titulo abonos
	printf("<td align='right'><b>%s</b></td>", number_format($suma_abono,$n_decimales));//total abonos
	printf("<td align='right'><b>%s</b></td>", number_format($tasa_tc1, $n_tipocambio));//tasa a otra moneda
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_abono * $tasa_tc1,$n_decimales));//total abonos
	printf("<tr BGCOLOR='#ffcccc'><td colspan='8'><b>Total Saldo del Periodo</b></td>");//titulo saldo
	printf("<td align='right'><b>%s</b></td>", number_format($suma_abono + $suma_debito,$n_decimales));//total saldo del periodo
	printf("<td align='right'><b>%s</b></td>", number_format($tasa_tc1, $n_tipocambio));//tasa a otra moneda
	printf("<td align='right'><b>%s</b></td></tr>", number_format(($suma_abono + $suma_debito)* $tasa_tc1,$n_decimales));//total saldo del periodo
	printf("<tr BGCOLOR='#ffcccc'><td colspan='8'><b>Total Saldo a la Fecha</b></td>");//titulo saldo
	printf("<td align='right'><b>%s</b></td>", number_format($suma_saldo,$n_decimales));//total saldo
	printf("<td align='right'><b>%s</b></td>", number_format($tasa_tc1, $n_tipocambio));//tasa a otra moneda
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_saldo * $tasa_tc1,$n_decimales));//total saldo
	printf("<tr BGCOLOR='#66ff99'><td colspan='11'>&nbsp;</td></tr>");
	$sum_deb_tot = $sum_deb_tot + $suma_debito * $tasa_tc1;
	$sum_abo_tot = $sum_abo_tot + $suma_abono * $tasa_tc1;
	$sum_sal_tot = $sum_sal_tot + $suma_saldo * $tasa_tc1;
	} else {
	printf("<tr BGCOLOR='#ffcccc'><td colspan='8'><b>Total D&eacute;bitos del Periodo</b></td>");//titulo debitos
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_debito * -1,$n_decimales));//total debitos
	printf("<tr BGCOLOR='#ffcccc'><td colspan='8'><b>Total Abonos del Periodo</b></td>");//titulo abonos
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_abono,$n_decimales));//total abonos
	printf("<tr BGCOLOR='#ffcccc'><td colspan='8'><b>Total Saldo del Periodo</b></td>");//titulo saldo
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_debito + $suma_abono,$n_decimales));//total saldo
	printf("<tr BGCOLOR='#ffcccc'><td colspan='8'><b>Total Saldo a la Fecha</b></td>");//titulo saldo
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_saldo,$n_decimales));//total saldo
	printf("<tr BGCOLOR='#66ff99'><td colspan='9'>&nbsp;</td></tr>");
	}
	$j++;
  }//fin while 1


?>

<?php if($otra_moneda != 0){ ?>
<tr BGCOLOR='#ffcccc'><td colspan='10'><b>Total D&eacute;bitos Bancarios del Periodo de Todas las Cuentas, Valorado en <?php echo $nom_otra_moneda; ?></b></td>
<td align='right'><b><?php echo number_format($sum_deb_tot * -1,$n_decimales); ?></b></td></tr>
<tr BGCOLOR='#ffcccc'><td colspan='10'><b>Total Abonos Bancarios del Periodo de Todas las Cuentas, Valorado en <?php echo $nom_otra_moneda; ?></b></td>
<td align='right'><b><?php echo number_format($sum_abo_tot,$n_decimales); ?></b></td></tr>
<tr BGCOLOR='#ffcccc'><td colspan='10'><b>Total Saldo Bancario del Periodo de Todas las Cuentas, Valorado en <?php echo $nom_otra_moneda; ?></b></td>
<td align='right'><b><?php echo number_format($sum_deb_tot + $sum_abo_tot,$n_decimales); ?></b></td></tr>
<tr BGCOLOR='#ffcccc'><td colspan='10'><b>Total Saldo Bancario a Fecha de Todas las Cuentas, Valorado en <?php echo $nom_otra_moneda; ?></b></td>
<td align='right'><b><?php echo number_format($sum_sal_tot,$n_decimales); ?></b></td></tr>
<?php } ?>
</tbody>
</table>

<?php
}//fin if solo_saldos
} //fin if 2

?>

</body>
</html>
