<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Movimientos de Caja</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"];
$caja=$_POST["caja"];
$concepto=$_POST["concepto"];
$cod_contexto=$_POST["contexto"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
$otra_moneda=$_POST["otra_moneda"];
$solo_saldos=$_POST["solo_saldos"];
$orden=$_POST["orden"];
//$usuario = $_POST["usuario"];

?>
<body>
<?php

//Inicio reporte Movimientos de Caja


//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
//$fecha_hoy=date("Y-m-d");
$resultom = mysqli_query($link, "SELECT T2.pkyExchangeRate as tc FROM 
(SELECT fldDate, MAX(fldTime) AS fldTime FROM actExchangeRate WHERE fldExclusive = '0' GROUP BY fldDate) as T1,
actExchangeRate T2 
WHERE 
T1.fldDate = (SELECT MAX(fldDate) FROM actExchangeRate WHERE fldDate <= '$fecha_hoy' AND fldExclusive = '0') AND 
T2.fldDate = T1.fldDate AND 
T2.fldTime = T1.fldTime AND 
sysEnabled = 1 AND sysAuthorized = 1;");
while ($row = mysqli_fetch_array($resultom)) {
	$ultimo_tc = $row['tc'];
	}
	mysqli_free_result($resultom);

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

$fin_om = $i - 1;


//Inicio muestra caja
if($rep == 11 && $tipo == 1) { // if 2

	if ($caja == 0) { // if 2.2
		$nom_caja = 'Todos';
		$cuenta_de_caja = ' ';
	} else {
		$result = mysqli_query($link, "select a.pkyPettyCash as cod_caja, a.fldName as caja, a.txtDescription as descripcion, b.fldLogin as usuario, c.fldName as ciudad
from
ptcPettyCash a, accUser b, regCity c
where
a.pkyPettyCash = '$caja' and
a.codUser = b.pkyUser and
a.codCity = c.pkyCity and
a.sysEnabled = 1 and a.sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result)) {   
		$var0 = $row['cod_caja'];
		$var1 = $row['caja'];
		$var2 = $row['usuario'];
		$nom_caja = $var0.' - '.$var1.' ('.$var2.')';
		}
		$cuenta_de_caja = ' a.pkyPettyCash = '.$caja. ' and ';
		//solo controlo que la cuenta sea la que viene de index si quito $cuenta_de_caja del query listado de cuentas funciona como todos.!!
		//echo $cuenta_de_caja;
		mysqli_free_result($result);
	}//fin if 2.2
	
	
	if ($concepto == 0) { // if 2.3
		$nom_concepto = 'Todos';
		$concepto_de_movimiento = ' ';
		$caja_a_banco = ' ';
		$caja_a_banco_det = ' ';
		/*$caja_a_banco = "UNION ALL
select a.pkyBankMovement as codigo, a.fldDate as fecha,
a.codPettyCash as cod_caja, a.codCurrency as cod_moneda,
b.fldName as moneda, 2 as cod_tipo, c.fldName as tipo, 
a.codTypeConcept as cod_con, 
concat('Egreso a ',d.fldName,' ,Cta. ',e.fldNumber) as concepto,
a.txtDescription as glosa, a.fldTotal as total1, 
a.fldTotal * -1 as total
from tryBankMovement a, actCurrency b, ptcPettyCashMovementType c,
tryBank d, tryBankDetail e
where
a.fldDate < '".$fechaini."' and
a.codPettyCash is not NULL and
a.codCurrency = b.pkyCurrency and
c.pkyPettyCashMovementType = 2 and
a.codBank = d.pkyBank and
a.codBankAccount = e.pkyBankDetail and
a.sysEnabled = 1 and a.sysAuthorized = 1";

$caja_a_banco_det = "UNION ALL
select a.pkyBankMovement as codigo, a.fldDate as fecha,
a.codPettyCash as cod_caja, f.fldName as caja_orig, 
NULL as cod_caja_des, NULL as caja_dest, 
a.codCurrency as cod_moneda, b.fldName as moneda, 
2 as cod_tipo, c.fldName as tipo, a.codTypeConcept as cod_con, 
concat('Egreso a ',d.fldName,' ,Cta. ',e.fldNumber) as concepto,
a.txtDescription as glosa, a.fldTotal as total1, 
a.fldTotal * -1 as total
from tryBankMovement a, actCurrency b, ptcPettyCashMovementType c,
tryBank d, tryBankDetail e, ptcPettyCash f
where
a.fldDate >= '".$fechaini."' and
a.fldDate <= '".$fechafin."' and
a.codPettyCash is not NULL and
a.codCurrency = b.pkyCurrency and
c.pkyPettyCashMovementType = 2 and
a.codBank = d.pkyBank and
a.codBankAccount = e.pkyBankDetail and
a.codPettyCash = f.pkyPettyCash and
a.sysEnabled = 1 and a.sysAuthorized = 1";*/
	} else {
		$result2 = mysqli_query($link, "select pkyPettyCashMovementTypeConcept as cod_con, fldName as nombre
from
ptcPettyCashMovementTypeConcept
where
pkyPettyCashMovementTypeConcept = '$concepto' and
sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result2)) {   
		$nom_concepto = $row['nombre'];
		}
		$concepto_de_movimiento = ' a.codTypeConcept = '.$concepto. ' and';
		$caja_a_banco = ' ';
		$caja_a_banco_det = ' ';
		//echo $concepto_de_movimiento;
		mysqli_free_result($result2);
	}//fin if 2.3
	
	//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 ';	
}

	if ($orden == 0) { // if 2.2.1
		$nom_orden = 'Fecha';
		$valor_orden = 'order by cod_caja, cod_moneda, fecha, codigo';
	}//fin if 2.2.1  
	if ($orden == 1) { // if 2.2.2
		$nom_orden = 'C&oacute;digo';
		$valor_orden = 'order by cod_caja, cod_moneda, codigo';
	}//fin if 2.2.2  
	
		//listado de cajas con monedas
		$result = mysqli_query($link, "select t.cod_caja as cod_caja, t.caja as caja, t.descripcion as descripcion, t.usuario as usuario, t.ciudad as ciudad, r.codigo as cod_moneda, r.moneda as moneda
from
(select a.pkyPettyCash as cod_caja, a.fldName as caja, a.txtDescription as descripcion, b.fldLogin as usuario, c.fldName as ciudad
from
ptcPettyCash a, accUser b, regCity c
where
$cuenta_de_caja
a.codUser = b.pkyUser and
a.codCity = c.pkyCity and
a.sysEnabled = 1 and a.sysAuthorized = 1) t,

(select pkyCurrency as codigo, fldName as moneda
from actCurrency
where
sysEnabled = 1 and sysAuthorized = 1) r
order by t.cod_caja, r.codigo;");
		
		//inicio calculo saldo inicial
		if($concepto == -1){ //if concepto = -1
		/*$result2 = mysqli_query($link, "select t.cod_caja as cod_caja, t.cod_moneda as cod_moneda,
sum(t.total) as total
from
(select a.pkyBankMovement as codigo, a.fldDate as fecha,
a.codPettyCash as cod_caja, a.codCurrency as cod_moneda,
b.fldName as moneda, 2 as cod_tipo, c.fldName as tipo, 
a.codTypeConcept as cod_con, 
concat('Egreso a ',d.fldName,', Cta. ',e.fldNumber) as concepto,
a.txtDescription as glosa, a.fldTotal as total1, 
a.fldTotal * -1 as total
from tryBankMovement a, actCurrency b, ptcPettyCashMovementType c,
tryBank d, tryBankDetail e
where
a.fldDate < '$fechaini' and
a.codPettyCash is not NULL and
a.codCurrency = b.pkyCurrency and
c.pkyPettyCashMovementType = 2 and
a.codBank = d.pkyBank and
a.codBankAccount = e.pkyBankDetail and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
group by t.cod_caja, t.cod_moneda;");*/
		} else {
		$result2 = mysqli_query($link, "select t.cod_caja as cod_caja, t.cod_moneda as cod_moneda,
sum(t.total) as total
from
(select a.pkyPettyCashMovement as codigo, a.fldDate as fecha,
a.codPettyCash as cod_caja, a.codCurrency as cod_moneda,
b.fldName as moneda, a.codType as cod_tipo, c.fldName as tipo, 
a.codTypeConcept as cod_con, d.fldName as concepto,
a.txtDescription as glosa, a.fldTotal as total1,
case a.codType when 1 then a.fldTotal when 2 then (a.fldTotal * -1) when 3 then (a.fldTotal * -1) end as total
from ptcPettyCashMovement a, actCurrency b, ptcPettyCashMovementType c,
ptcPettyCashMovementTypeConcept d
where 
a.fldDate < '$fechaini' and
a.codCurrency = b.pkyCurrency and
a.codType = c.pkyPettyCashMovementType and
a.codTypeConcept = d.pkyPettyCashMovementTypeConcept and
$concepto_de_movimiento 
$valor_contexto 
a.sysEnabled = 1 and a.sysAuthorized = 1
UNION ALL
select a.pkyPettyCashMovement as codigo, a.fldDate as fecha,
a.codDestinyPettyCash as cod_caja, a.codCurrency as cod_moneda,
b.fldName as moneda, a.codType as cod_tipo, c.fldName as tipo, 
a.codTypeConcept as cod_con, d.fldName as concepto,
a.txtDescription as glosa, a.fldTotal as total1,
case a.codType when 3 then a.fldTotal end as total
from ptcPettyCashMovement a, actCurrency b, ptcPettyCashMovementType c,
ptcPettyCashMovementTypeConcept d
where 
a.fldDate < '$fechaini' and
a.codDestinyPettyCash is not NULL and
a.codCurrency = b.pkyCurrency and
a.codType = c.pkyPettyCashMovementType and
a.codTypeConcept = d.pkyPettyCashMovementTypeConcept and
$concepto_de_movimiento 
$valor_contexto 
a.sysEnabled = 1 and a.sysAuthorized = 1 
$caja_a_banco) t
group by t.cod_caja, t.cod_moneda;");
		}//fin if concepto = -1

$matriz_salini = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_salini [$i][1] = $row['cod_caja'];
	$matriz_salini [$i][2] = $row['caja'];
	$matriz_salini [$i][3] = $row['descripcion'];
	$matriz_salini [$i][4] = $row['usuario'];
	$matriz_salini [$i][5] = $row['ciudad'];
	$matriz_salini [$i][6] = $row['cod_moneda'];
	$matriz_salini [$i][7] = $row['moneda'];
	$matriz_salini [$i][8] = 0;
	if($matriz_salini [$i][6] == $otra_moneda) {
		$matriz_salini [$i][9] = number_format(1,5);
	} else {
		$j = 1;
		while($j <= $fin_om) {
			if( $matriz_tc [$j][3] == $matriz_salini [$i][6] && $matriz_tc [$j][2] == $otra_moneda) {
				$matriz_salini [$i][9] = $matriz_tc [$j][4];
			}
			$j++;
		}
	}
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);
//saldo inicial
while ($row = mysqli_fetch_array($result2)) {
	$i = 1;
	$cod_caja = $row['cod_caja'];
	$cod_moneda = $row['cod_moneda'];
	while ($i <= $fin) {
		if ($cod_caja == $matriz_salini [$i][1] && $cod_moneda == $matriz_salini [$i][6]) {
		$matriz_salini [$i][8] = $row['total'];
		}
	$i++;
	}
}
mysqli_free_result($result2);
/*
	$r = 1;
	while ($r <= $fin) {
		echo $matriz_salini [$r][1].' - ';
		echo $matriz_salini [$r][2].' - ';
		echo $matriz_salini [$r][3].' - ';
		echo $matriz_salini [$r][4].' - ';
		echo $matriz_salini [$r][5].' - ';
		echo $matriz_salini [$r][6].' - ';
		echo $matriz_salini [$r][7].' - ';
		echo $matriz_salini [$r][8].' - ';
		echo $matriz_salini [$r][9].'<br />';
		$r++;
	}
	*/
//fin calculo saldo inicial

//inicio calculo movimientos de caja
		
		if($concepto == -1){//if concepto = -1
		/*$result3 = mysqli_query($link, "select a.pkyBankMovement as codigo, a.fldDate as fecha,
a.codPettyCash as cod_caja, f.fldName as caja_orig, 
NULL as cod_caja_des, NULL as caja_dest, 
a.codCurrency as cod_moneda, b.fldName as moneda, 
2 as cod_tipo, c.fldName as tipo, a.codTypeConcept as cod_con, 
concat('Egreso a ',d.fldName,', Cta. ',e.fldNumber) as concepto,
a.txtDescription as glosa, a.fldTotal as total1, 
a.fldTotal * -1 as total
from tryBankMovement a, actCurrency b, ptcPettyCashMovementType c,
tryBank d, tryBankDetail e, ptcPettyCash f
where
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codPettyCash is not NULL and
a.codCurrency = b.pkyCurrency and
c.pkyPettyCashMovementType = 2 and
a.codBank = d.pkyBank and
a.codBankAccount = e.pkyBankDetail and
a.codPettyCash = f.pkyPettyCash and
a.sysEnabled = 1 and a.sysAuthorized = 1
order by cod_caja, cod_moneda, fecha, codigo;");*/
		
		} else {
			
		$result3 = mysqli_query($link, "select a.pkyPettyCashMovement as codigo, a.fldDate as fecha, a.codPettyCash as cod_caja, e.fldName as caja_orig,
a.codDestinyPettyCash as cod_caja_des, NULL as caja_dest,
a.codCurrency as cod_moneda,
b.fldName as moneda, a.codType as cod_tipo, c.fldName as tipo, 
a.codTypeConcept as cod_con, d.fldName as concepto,
a.txtDescription as glosa, a.fldTotal as total1,
case a.codType when 1 then a.fldTotal when 2 then (a.fldTotal * -1) end as total, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from ptcPettyCashMovement a, actCurrency b, ptcPettyCashMovementType c,
ptcPettyCashMovementTypeConcept d, ptcPettyCash e
where 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codPettyCash = e.pkyPettyCash and
a.codDestinyPettyCash is NULL and
a.codCurrency = b.pkyCurrency and
a.codType = c.pkyPettyCashMovementType and
a.codTypeConcept = d.pkyPettyCashMovementTypeConcept and
$concepto_de_movimiento 
$valor_contexto 
a.sysEnabled = 1 and a.sysAuthorized = 1

UNION ALL

select a.pkyPettyCashMovement as codigo, a.fldDate as fecha,
a.codPettyCash as cod_caja, e.fldName as caja_orig,
a.codDestinyPettyCash as cod_caja_des, f.fldName as caja_dest,
a.codCurrency as cod_moneda,
b.fldName as moneda, a.codType as cod_tipo, c.fldName as tipo, 
a.codTypeConcept as cod_con, d.fldName as concepto,
a.txtDescription as glosa, a.fldTotal as total1,
case a.codType when 1 then a.fldTotal when 2 then (a.fldTotal * -1) when 3 then (a.fldTotal * -1) end as total, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from ptcPettyCashMovement a, actCurrency b, ptcPettyCashMovementType c,
ptcPettyCashMovementTypeConcept d, ptcPettyCash e, ptcPettyCash f
where 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codPettyCash = e.pkyPettyCash and
a.codDestinyPettyCash = f.pkyPettyCash and
a.codCurrency = b.pkyCurrency and
a.codType = c.pkyPettyCashMovementType and
a.codTypeConcept = d.pkyPettyCashMovementTypeConcept and
$concepto_de_movimiento 
$valor_contexto 
a.sysEnabled = 1 and a.sysAuthorized = 1
UNION ALL
select a.pkyPettyCashMovement as codigo, a.fldDate as fecha,
a.codDestinyPettyCash as cod_caja, e.fldName as caja_orig,
a.codPettyCash as cod_caja_des, f.fldName as caja_dest,
a.codCurrency as cod_moneda,
b.fldName as moneda, a.codType as cod_tipo, c.fldName as tipo, 
a.codTypeConcept as cod_con, d.fldName as concepto,
a.txtDescription as glosa, a.fldTotal as total1,
case a.codType when 3 then a.fldTotal end as total, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from ptcPettyCashMovement a, actCurrency b, ptcPettyCashMovementType c,
ptcPettyCashMovementTypeConcept d, ptcPettyCash e, ptcPettyCash f
where 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
a.codDestinyPettyCash is not NULL and
a.codPettyCash = f.pkyPettyCash and
a.codDestinyPettyCash = e.pkyPettyCash and
a.codCurrency = b.pkyCurrency and
a.codType = c.pkyPettyCashMovementType and
a.codTypeConcept = d.pkyPettyCashMovementTypeConcept and
$concepto_de_movimiento 
$valor_contexto 
a.sysEnabled = 1 and a.sysAuthorized = 1 
$caja_a_banco_det 
$valor_orden;");
		}//fin if concepto = -1
		
$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_caja'];
	$matriz_detalle [$i][4] = $row['caja_orig'];
	$matriz_detalle [$i][5] = $row['cod_caja_des'];
	$matriz_detalle [$i][6] = $row['caja_dest'];
	$matriz_detalle [$i][7] = $row['cod_moneda'];
	$matriz_detalle [$i][8] = $row['moneda'];
	$matriz_detalle [$i][9] = $row['cod_tipo'];
	$matriz_detalle [$i][10] = $row['tipo'];
	$matriz_detalle [$i][11] = $row['cod_con'];
	$matriz_detalle [$i][12] = $row['concepto'];
	$matriz_detalle [$i][13] = $row['glosa'];
	$matriz_detalle [$i][14] = $row['total'];
	
	//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][15] = $papa_enlace; 
	$i++;
}
$fin_det = $i - 1;
mysqli_free_result($result3);


//fin calculo registros

if($solo_saldos == 1) {//if solo_saldos
$nom_saldos = 'Si';
?>
<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 de Caja</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>Caja: </b><?php echo $nom_caja; ?></TD>
        <TD width="25%"><b>Solo saldos: </b><?php echo $nom_saldos; ?></TD>
        <TD width="25%"><b>A Fecha: </b><?php echo $fechafin; ?></TD>
        <TD width="25%"><b>Expresado en: </b><?php echo $nom_otra_moneda; ?></TD>
        </TR>
        <TR>
        <TD colspan="4"><b>Contexto: </b><?php echo $nom_contexto; ?></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>", 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>Caja</b></TD>
<TD align='center'><b>Usuario</b></TD>
<TD align='center'><b>Ciudad</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>
</TR>
<?php } else { ?>
<TD align='center' width='100'><b>Saldo</b></TD></TR>
<?php } ?>
</thead>
<tbody class="marcafila">
<?php

  $sum_sal_tot = 0;
  $j = 1;
  $color = 2;
  while ($j <= $fin) { //while 1
	if($matriz_salini [$j][8] > 0){ //if no muestra 0
		if($color % 2 == 0) { //if color
			printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_salini [$j][1]);//codigo
		} else {
			printf("<tr><td align='center'>%s</td>", $matriz_salini [$j][1]);//codigo
		}//cierra if color
		printf("<td align='left'>%s</td>", $matriz_salini [$j][2]);//caja
		printf("<td align='left'>%s</td>", $matriz_salini [$j][4]);//usuario
		printf("<td align='left'>%s</td>", $matriz_salini [$j][5]);//ciudad
		printf("<td align='left'>%s</td>", $matriz_salini [$j][7]);//moneda de la caja
		
		if($otra_moneda != 0){
		printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][8],$n_decimales));//saldo inicial
		printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][9],$n_tipocambio));//tasa a otra moneda
		printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][8] * $matriz_salini [$j][9],$n_decimales));//saldo inicial otra moneda
		} else {
		printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][8],$n_decimales));//saldo inicial
		}
		$color++;
	} //fin if no muestra 0
		$sum_sal_tot = $sum_sal_tot + $matriz_salini [$j][8] * $matriz_salini [$j][9];
		$j++;
  }//fin while 1
	?>
	<?php if($otra_moneda != 0){ ?>
<tr BGCOLOR='#ffcccc'><td colspan='7'><b>Total Saldos Cajas, 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
	$nom_saldos = 'No';
		?>
<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 de Caja</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>Caja: </b><?php echo $nom_caja; ?></TD>
        <TD width="33%"><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
        <TD width="33%"><b>Concepto: </b><?php echo $nom_concepto; ?></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>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; ?>&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>", 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> 
  <?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
  	
	printf("<TR><TD align='center'><b>Cod.</b></TD>");
    printf("<TD align='center'><b>Caja Origen</b></TD>");
	printf("<TD align='center'><b>Caja Dest.</b></TD>");
    printf("<TD align='center' width='65'><b>Fecha</b></TD>");
    printf("<TD align='center'><b>Tipo</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.</b></TD>");
    printf("<TD align='center'><b>Debito</b></TD>");
	printf("<TD align='center'><b>Cr&eacute;dito</b></TD>");
    
	if($otra_moneda != 0){
	printf("<TD align='center'><b>Saldo</b></TD>");
	printf("<TD align='center'><b>Tasa en %s</b></TD>", $nom_otra_moneda);
	printf("<TD align='center'><b>Saldo en %s</b></TD></TR>", $nom_otra_moneda);
	} else {
	printf("<TD align='center'><b>Saldo</b></TD></TR>");
	}
	echo '</thead><tbody class="marcafila">';
	printf("<tr><td colspan='11' align='left'><i><b>%s - %s, Usuario (%s), %s, Mon. %s</b></i> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--------------- Saldo inicial ---------------</td>", $matriz_salini [$j][1], $matriz_salini [$j][2], $matriz_salini [$j][4], $matriz_salini [$j][5], $matriz_salini [$j][7]);//titulo saldo anterior
	
	if($otra_moneda != 0){
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][8],$n_decimales));//saldo inicial
	printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][9],$n_tipocambio));//tasa a otra moneda
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][8] * $matriz_salini [$j][9],$n_decimales));//saldo inicial otra moneda
	} else {
	printf("<td align='right'>%s</td></tr>", number_format($matriz_salini [$j][8],$n_decimales));//saldo inicial
	}
	
	$suma_debito = 0;
  	$suma_abono = 0;
  	$saldo_inicial = $matriz_salini [$j][8];
	$suma_saldo = $matriz_salini [$j][8];
	$color = 2;
	$i = 1;
	while($i <= $fin_det) {//while 2
	  if($matriz_salini [$j][1] == $matriz_detalle [$i][3] && $matriz_salini [$j][6] == $matriz_detalle [$i][7]) { //if igual
	    $cod_mov_caja = $matriz_detalle [$i][1];
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePettyCash/VoucherPettyCashMovement/index.php?pkyPettyCashMovement=$cod_mov_caja&user=$usuario&token=$token' title='Nota de Movimiento de Caja'>%s</a></td>", $matriz_detalle [$i][1]);//cod
		} else {
		printf("<tr><td align='center'><a href='../../ModulePettyCash/VoucherPettyCashMovement/index.php?pkyPettyCashMovement=$cod_mov_caja&user=$usuario&token=$token' title='Nota de Movimiento de Caja'>%s</a></td>", $matriz_detalle [$i][1]);//cod
		}//fin if color
		 if($matriz_detalle [$i][14] > 0 && $matriz_detalle [$i][9] == 3) {
		 printf("<td align='left'>%s</td>", $matriz_detalle [$i][6]);//caja origen
		 printf("<td align='left'>%s</td>", $matriz_detalle [$i][4]);//caja destino
		 } else {
		 printf("<td align='left'>%s</td>", $matriz_detalle [$i][4]);//caja origen
		 printf("<td align='left'>%s</td>", $matriz_detalle [$i][6]);//caja destino
		 }
		printf("<td align='right'>%s</td>", $matriz_detalle [$i][2]);//fecha
		printf("<td align='left'>%s</td>", $matriz_detalle [$i][10]);//tipo
		printf("<td align='left'>%s</td>", $matriz_detalle [$i][12]);//concepto
		printf("<td align='left'>%s</td>", $matriz_detalle [$i][13]);//glosa
		printf("<td align='center'>%s</td>", $matriz_detalle [$i][15]);//gen por
		printf("<td align='left'>%s</td>", $matriz_detalle [$i][8]);//moneda
		  if($matriz_detalle [$i][14] < 0) {
			printf("<td>&nbsp;</td>");//debito
			printf("<td align='right'>%s</td>", number_format($matriz_detalle [$i][14]*-1,$n_decimales));//credito
			$suma_abono = $suma_abono + $matriz_detalle [$i][14];
		  } else {
			printf("<td align='right'>%s</td>", number_format($matriz_detalle [$i][14],$n_decimales));//debito
			printf("<td>&nbsp;</td>");//credito
			$suma_debito = $suma_debito + $matriz_detalle [$i][14];
		  }
		
		$suma_saldo = $suma_saldo + $matriz_detalle [$i][14];
		printf("<td align='right'>%s</td>", number_format($suma_saldo,$n_decimales));//saldo
		
		if($otra_moneda != 0){
		printf("<td align='right'>%s</td>", number_format($matriz_salini [$j][9],$n_tipocambio));//tasa a otra moneda
		printf("<td align='right'>%s</td>", number_format($suma_saldo * $matriz_salini [$j][9],$n_decimales));//saldo a otra moneda
		}
		echo '</tr>';
		$color++;
	  }//fin if igual
	$i++; 
	}//fin while 2
	
	if($otra_moneda != 0){
	printf("<tr BGCOLOR='#ffcccc'><td colspan='11'><b>Total Ingresos del Periodo</b></td>");//titulo egresos
	printf("<td align='right'><b>%s</b></td>", number_format($suma_debito,$n_decimales));//total egresos
	printf("<td align='right'><b>%s</b></td>", number_format($matriz_salini [$j][9],$n_tipocambio));//tasa a otra moneda
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_debito * $matriz_salini [$j][9],$n_decimales));//total egresos
	printf("<tr BGCOLOR='#ffcccc'><td colspan='11'><b>Total Egresos del Periodo</b></td>");//titulo ingresos
	printf("<td align='right'><b>%s</b></td>", number_format($suma_abono*-1,$n_decimales));//total ingresos
	printf("<td align='right'><b>%s</b></td>", number_format($matriz_salini [$j][9],$n_tipocambio));//tasa a otra moneda
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_abono* -1 * $matriz_salini [$j][9],$n_decimales));//total ingresos
	printf("<tr BGCOLOR='#ffcccc'><td colspan='11'><b>Total Saldo del Periodo</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($matriz_salini [$j][9],$n_tipocambio));//tasa a otra moneda
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_saldo * $matriz_salini [$j][9],$n_decimales));//total saldo
	printf("<tr BGCOLOR='#66ff99'><td colspan='14'>&nbsp;</td></tr>");
	$sum_deb_tot = $sum_deb_tot + $suma_debito * $matriz_salini [$j][9];
	$sum_abo_tot = $sum_abo_tot + $suma_abono * $matriz_salini [$j][9];
	$sum_sal_tot = $sum_sal_tot + $suma_saldo * $matriz_salini [$j][9];
	} else {
	printf("<tr BGCOLOR='#ffcccc'><td colspan='11'><b>Total Ingresos del Periodo</b></td>");//titulo egresos
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_debito,$n_decimales));//total egresos
	printf("<tr BGCOLOR='#ffcccc'><td colspan='11'><b>Total Egresos del Periodo</b></td>");//titulo abonos
	printf("<td align='right'><b>%s</b></td></tr>", number_format($suma_abono*-1,$n_decimales));//total ingresos
	printf("<tr BGCOLOR='#ffcccc'><td colspan='11'><b>Total Saldo del Periodo</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='12'>&nbsp;</td></tr>");
	//$sum_deb_tot = $sum_deb_tot + $suma_debito;
	//$sum_abo_tot = $sum_abo_tot + $suma_abono;
	//$sum_sal_tot = $sum_sal_tot + $suma_saldo;
	}
	$j++;
  }//fin while 1


?>

<?php if($otra_moneda != 0){ ?>
<tr BGCOLOR='#ffcccc'><td colspan='13'><b>Total Ingresos del Periodo de Todas las Cajas, Valorado en <?php echo $nom_otra_moneda; ?></b></td>
<td align='right'><b><?php echo number_format($sum_deb_tot,$n_decimales); ?></b></td></tr>
<tr BGCOLOR='#ffcccc'><td colspan='13'><b>Total Egresos del Periodo de Todas las Cajas, Valorado en <?php echo $nom_otra_moneda; ?></b></td>
<td align='right'><b><?php echo number_format($sum_abo_tot*-1,$n_decimales); ?></b></td></tr>
<tr BGCOLOR='#ffcccc'><td colspan='13'><b>Total Saldo del Periodo de Todas las Cajas, 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>
