<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Nota de Pago</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 "../../class.nroenletraver2.php";
include "../../general.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];

//index.php?pkyPayment=2&balance=100&printAccountEntry=1&user=Luigi
$cod_pago = $_GET["pkyPayment"];
//$balance = $_GET["balance"]; se quito el saldo a pagar pongo 0 para que no salga error
$balance = 0;
$muestra_asiento = $_GET["printAccountEntry"];
//$muestra_asiento = 1;
//$mostrar_asiento_cc = $_GET["showCC"];
$usuario = $_GET["user"];
//$n_decimales = 6;
if($rep == 1) { $muestra_cc = 1; }
if($rep == 2) { $muestra_cc = 0; }

?>
<body>

<?php
	
// Inicio impresion nota de pago

$result3 = mysqli_query($link, "select a.pkyPayment as cod_pago, a.fldDocumentNumber as nro_doc, 
a.fldDate as fecha, b.fldName as via_pago, a.codClient as cod_cliente, a.codEmployee as cod_emp, a.codCurrency as cod_moneda, d.fldName as moneda, a.codExchangeRate as cod_tc, a.codAccountEntry as cod_asiento, a.codBankMovement as cod_mov_banco, a.codCheck as cod_cheque, a.codPettyCashMovement as cod_mov_caja, a.txtDescription as glosa, a.sysEnabled as enabled, a.sysAuthorized as authorized, g.fldName as usuario_reg, 
a.fldParentComponent as papa, a.cosComponentKey as cod_papa, a.sysContext as cod_contexto, s.fldName as contexto  
from payPayment a, payPaymentRoute b, actCurrency d, accGroup g, srvContext s
where 
a.codRoute = b.pkyPaymentRoute and 
a.codCurrency = d.pkyCurrency and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.sysContext = s.pkyContext and 
a.pkyPayment = '$cod_pago';");
	while ($row = mysqli_fetch_array($result3)) {
	$nro_doc = $row['nro_doc'];
	$fecha = $row['fecha'];
	$via_pago = $row['via_pago'];
	$cod_cliente = $row['cod_cliente'];
	$cod_emp = $row['cod_emp'];
	$moneda = $row['moneda'];
	$cod_moneda = $row['cod_moneda'];
	$cod_tc = $row['cod_tc'];
	$cod_asiento = $row['cod_asiento'];
	$cod_mov_banco = $row['cod_mov_banco'];
	$cod_cheque = $row['cod_cheque'];
	$cod_mov_caja = $row['cod_mov_caja'];
	$glosa = $row['glosa'];
	$glosa = nl2br($glosa);
	$glosa = str_replace('\n','<br />',$glosa);
	$usuario_reg = $row['usuario_reg'];
	$enabled = $row['enabled'];
	$authorized = $row['authorized'];
	$cod_contexto = $row['cod_contexto'];
	$contexto = $row['contexto'];
	//para enlazar al que genero el asiento
	$papa_componente = $row['papa'];
	$papa_codigo = $row['cod_papa'];
	//$usuario = $usuario_det;
	include "../../generadopor.php";
	}
	mysqli_free_result($result3);
//$cod_tc=14;
$resulttc = mysqli_query($link, "select pkyExchangeRate as cod_tc, fldDate as fecha, fldTime as hora
from
actExchangeRate
where
pkyExchangeRate = '$cod_tc';");
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 = '$cod_tc' and 
a.codBaseCurrency = '$cod_moneda_principal' and 
a.codBaseCurrency = b.pkyCurrency and 
a.codTargetCurrency = c.pkyCurrency 
order by cod_mon_target;");
$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;

$resultcli = mysqli_query($link, "select fldName as empresa
from
agnCompany
where
pkyCompany = '$cod_cliente';");
while ($row = mysqli_fetch_array($resultcli)) {
	$empresa = $row['empresa'];
}
mysqli_free_result($resultcli);

$resultemp = mysqli_query($link, "select case when fldMiddleName is NULL then concat(fldLastName,', ',fldFirstName) else concat(fldLastName,' ',fldMiddleName,', ',fldFirstName) end as empleado
from
agnPerson
where
pkyPerson = '$cod_emp';");
while ($row = mysqli_fetch_array($resultemp)) {
	$empleado = $row['empleado'];
}
mysqli_free_result($resultemp);

//datos de cheque segun cod_cheque
$resultcheque = mysqli_query($link, "select fldNumber as nro_cheque, fldName as nom_giro_cheque, fldForeignAccount as banco_cta
from tryCheck 
where
pkyCheck = '$cod_cheque';");
while ($row = mysqli_fetch_array($resultcheque)) {
	$nro_cheque = $row['nro_cheque'];
	$nom_giro_cheque = $row['nom_giro_cheque'];
	//$banco_cta = $row['banco_cta'];
}
mysqli_free_result($resultcheque);

//datos del banco y cta segun el cheque
$resultbanco = mysqli_query($link, "select b.fldName as banco, c.fldNumber as nro_cta
from 
tryCheck a, tryBank b, tryBankDetail c
where
a.codBank = b.pkyBank and 
a.codLocalAccount = c.pkyBankDetail and 
a.pkyCheck = '$cod_cheque';");
while ($row = mysqli_fetch_array($resultbanco)) {
	$banco_cheque = $row['banco'];
	$banco_cta = $row['nro_cta'];
}
mysqli_free_result($resultbanco);

//datos del banco y cta segun el movimiento bancario
$resultbanco2 = mysqli_query($link, "select b.fldName as banco, c.fldNumber as nro_cta
from
tryBankMovement a, tryBank b, tryBankDetail c
where
a.pkyBankMovement = '$cod_mov_banco' and 
a.codBank = b.pkyBank and 
a.codBankAccount = c.pkyBankDetail;");
while ($row = mysqli_fetch_array($resultbanco2)) {
	$banco_mov = $row['banco'];
	$banco_cta_mov = $row['nro_cta'];
}
mysqli_free_result($resultbanco2);


	$result2 = mysqli_query($link, "select a.pkyPaymentDetail as cod_pago_det, a.codAccountsPayable as cod_cxp, b.fldDocumentNumber as nro_doc_cxp, b.fldDate as fecha_det, b.codCurrency as cod_moneda_cxp, c.fldName as moneda_cxp, a.fldTotal as total
from payPaymentDetail a, payAccountsPayable b, actCurrency c
where 
a.codPayment = '$cod_pago' and 
a.codAccountsPayable = b.pkyAccountsPayable and 
b.codCurrency = c.pkyCurrency
order by b.fldDate;");
	
	$result3 = mysqli_query($link, "select a.pkyPaymentDetail as cod_cobro_det, a.codAccountsReceivable as cod_cxc, b.fldDocumentNumber as nro_doc_cxc, b.fldDate as fecha_det, b.codCurrency as cod_moneda_cxc, c.fldName as moneda_cxc, a.fldTotal as total
from payPaymentDetail a, colAccountsReceivable b, actCurrency c
where 
a.codPayment = '$cod_pago' and 
a.codAccountsReceivable = b.pkyAccountsReceivable and 
b.codCurrency = c.pkyCurrency
order by b.fldDate;");

$numero_filas = mysqli_num_rows($result3);
?>
<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>Nota de Pago</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>Cod. Reg.: </b><?php echo $cod_pago; ?></TD>
        <TD width="25%"><b>Nro. Doc.: </b><?php echo $nro_doc; ?></TD>
        <TD width="25%"><b>Moneda: </b><?php echo $moneda; ?></TD>
        <TD width="25%"><b>Fecha de Reg.: </b><?php echo $fecha; ?></TD>
        </TR>
        
        <TR>
        <TD><b>V&iacute;a Pago: </b><?php echo $via_pago; ?></TD>
        <?php if($empresa != NULL) { ?>
        <TD><b>Empresa: </b><?php echo $cod_cliente; ?> - <?php echo $empresa; ?></TD>
        <?php } else { ?>
        <TD><b>Empleado: </b><?php echo $cod_emp; ?> - <?php echo $empleado; ?></TD>
        <?php } ?>
        <TD><b>Contexto: </b><?php echo $cod_contexto; ?> - <?php echo $contexto; ?></TD>
        <TD><b>Generado Por: </b><?php echo $papa_enlace; ?></TD>
        </TR>
        
        <TR>
        <TD><b>Asiento: </b><a href="../../ModuleAccounting/VoucherAccountEntry/index.php?pkyAccountEntry=<?php echo $cod_asiento; ?>&showCC=<?php echo $mostrar_asiento_cc; ?>&user=<?php echo $usuario; ?>" title="Asiento Contable"><?php echo $cod_asiento; ?></a></TD>
        <TD><b>Mov. de Caja: </b><a href="../../ModulePettyCash/VoucherPettyCashMovement/index.php?pkyPettyCashMovement=<?php echo $cod_mov_caja; ?>&user=<?php echo $usuario; ?>" title="Nota de Movimiento de Caja"><?php echo $cod_mov_caja; ?></a></TD>
        <TD><b>Mov. de Banco: </b><a href="../../ModuleTreasury/VoucherBankMovement/index.php?pkyBankMovement=<?php echo $cod_mov_banco; ?>&user=<?php echo $usuario; ?>" title="Nota de Movimiento Bancario"><?php echo $cod_mov_banco; ?></a> <?php echo $banco_mov; ?> <?php echo $banco_cta_mov; ?></TD>
        <?php if($cod_cheque != NULL) { ?> 
        <TD><b>Cheque: </b><a href="../../ModuleTreasury/VoucherCheck/index.php?pkyCheck=<?php echo $cod_cheque; ?>&user=<?php echo $usuario; ?>" title="Cheque"><?php echo $cod_cheque; ?></a> - <?php echo $nro_cheque; ?> - <?php echo $banco_cheque; ?> (<?php echo $banco_cta; ?>) - <?php echo $nom_giro_cheque; ?></TD>
        <?php } else { ?>
        <TD><b>Cheque: </b><?php echo $cod_cheque; ?></TD>
        <?php } ?>
        </TR>
        <TR> 
        <TD colspan="4"><b>Glosa: </b><?php echo $glosa; ?></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 $cod_tc; ?>&user=<?php echo $usuario; ?>" title="Tipo de Cambio"><?php echo $cod_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 />
<!--detalle del pago CxP -->
<table border="1" align="center" cellpadding="2" width="100%">
<thead>
 	<TR> 
    <TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center" width="10%"><b>Cod. CxP</b></TD>
    <TD align="center" width="40%"><b>Nro. Doc. CxP</b></TD>
    <TD align="center" width="10%"><b>Fecha</b></TD>
    <TD align="center" width="10%"><b>Mon.</b></TD>
    <TD align="center" width="20%"><b>Monto</b></TD>
    </TR>
	</thead>
	<tbody class="marcafila">
    <?php
    $color = 2;
	$suma_monto = 0;
		
	while ($row = mysqli_fetch_array($result2)) { //while
		$cod_cxp_det = $row['cod_cxp'];
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $row['cod_pago_det']);//cod pago detalle
		} else {
		printf("<tr><td align='center'>%s</td>", $row['cod_pago_det']);//cod pago detalle
		} //cierra if color
		printf("<td align='center'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$cod_cxp_det&user=$usuario' title='Nota de Cuenta por Pagar'>%s</a></td>", $cod_cxp_det);//codigo cxp
		printf("<td align='left'>%s</td>", $row['nro_doc_cxp']);//nro doc cxp
		printf("<td align='right'>%s</td>", $row['fecha_det']);//fecha cxp
		printf("<td align='left'>%s</td>", $row['moneda_cxp']);//moneda cxp
		printf("<td align='right'>%s</td></tr>", number_format($row['total'], $n_decimales));//total
		$cod_moneda_det = $row['cod_moneda_cxp'];
		$tot = $row['total'];
		if($cod_moneda == $cod_moneda_det) { //if cod_moneda
			$suma_monto = $suma_monto + $tot;
		} else { //else if cod_moneda
			
			if($cod_moneda > $cod_moneda_det) {//if cabeza > detalle
			$resulttcdet = mysqli_query($link, "select fldRate as tasa
			from 
			actExchangeRateDetail
			where 
			codExchangeRate = '$cod_tc' and 
			codBaseCurrency = '$cod_moneda_det' and
			codTargetCurrency = '$cod_moneda' and 
			sysEnabled = 1 and sysAuthorized = 1;");
			while ($row = mysqli_fetch_array($resulttcdet)) {
				$tc_para_total = $row['tasa'];
				$tc_para_total = 1 / $tc_para_total;
			}
			mysqli_free_result($resulttcdet);
			} else {//else if cabeza > detalle
			$resulttcdet = mysqli_query($link, "select fldRate as tasa
			from 
			actExchangeRateDetail
			where 
			codExchangeRate = '$cod_tc' and 
			codBaseCurrency = '$cod_moneda' and 
			codTargetCurrency = '$cod_moneda_det' and 
			sysEnabled = 1 and sysAuthorized = 1;");
			while ($row = mysqli_fetch_array($resulttcdet)) {
				$tc_para_total = $row['tasa'];
			}
			mysqli_free_result($resulttcdet);
			}//fin if cabeza > detalle
			
			$suma_monto = $suma_monto + $tot * $tc_para_total;
		}// fin if cod_moneda
		$color++;
	}//fin while
	mysqli_free_result($result2);
	
	//convierte el total en literal
	$letras=new NroEnLetra($suma_monto,2);
	
	$mostrar_letras = ucfirst($letras->getLetra());
	
	$busca_espacio = substr($mostrar_letras,-6,1);
	if($busca_espacio == ' ') {
		$fin_letras = substr($mostrar_letras,-5);
		$mostrar_letras = substr($mostrar_letras,0,-5);
		$mostrar_letras = $mostrar_letras .'0'. $fin_letras;
	}
	$resultmoneda = mysqli_query($link, "select txtDescription as nom_moneda
	from actCurrency
	where
	pkyCurrency = '$cod_moneda';");
	while ($row = mysqli_fetch_array($resultmoneda)) {
		$nom_moneda_letras_tot = $row['nom_moneda'];
	}
	mysqli_free_result($resultmoneda);
	?>
	<tr BGCOLOR='#ffcccc'><td colspan="5"><b>Total &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $mostrar_letras; ?> <?php echo $nom_moneda_letras_tot; ?></b></td>
    <td align="right"><b><?php echo number_format($suma_monto, $n_decimales); ?></b></td>
    </tr>
    </tbody>
</table>

<?php if($numero_filas != NULL) { ?>
<br />
<!--detalle del cobro CxC -->
<table border="1" align="center" cellpadding="2" width="100%">
<thead>
 	<TR> 
    <TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center" width="10%"><b>Cod. CxC</b></TD>
    <TD align="center" width="40%"><b>Nro. Doc. CxC</b></TD>
    <TD align="center" width="10%"><b>Fecha</b></TD>
    <TD align="center" width="10%"><b>Mon.</b></TD>
    <TD align="center" width="20%"><b>Monto</b></TD>
    </TR>
	</thead>
	<tbody class="marcafila">
    <?php
    $color = 2;
	$suma_monto2 = 0;
		
	while ($row = mysqli_fetch_array($result3)) { //while
		$cod_cxc_det = $row['cod_cxc'];
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $row['cod_cobro_det']);//cod cobro detalle  
		} else {
		printf("<tr><td align='center'>%s</td>", $row['cod_cobro_det']);//cod cobro detalle
		} //cierra if color
		printf("<td align='center'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$cod_cxc_det&user=$usuario' title='Nota de Cuenta por Cobrar'>%s</a></td>", $cod_cxc_det);//codigo cxc
		printf("<td align='left'>%s</td>", $row['nro_doc_cxc']);//nro doc cxc
		printf("<td align='right'>%s</td>", $row['fecha_det']);//fecha cxc
		printf("<td align='left'>%s</td>", $row['moneda_cxc']);//moneda cxc
		printf("<td align='right'>%s</td></tr>", number_format($row['total'], $n_decimales));//total
		$cod_moneda_det = $row['cod_moneda_cxc'];
		$tot = $row['total'];
		if($cod_moneda == $cod_moneda_det) { //if cod_moneda
			$suma_monto2 = $suma_monto2 + $tot;
		} else {
			$resulttcdet = mysqli_query($link, "select fldRate as tasa
			from 
			actExchangeRateDetail
			where 
			codExchangeRate = '$cod_tc' and 
			codTargetCurrency = '$cod_moneda_det' and 
			codBaseCurrency = '$cod_moneda' and 
			sysEnabled = 1 and sysAuthorized = 1;");
			while ($row = mysqli_fetch_array($resulttcdet)) {
				$tc_para_total = $row['tasa'];
			}
			mysqli_free_result($resulttcdet);
			$suma_monto2 = $suma_monto2 + $tot * $tc_para_total;
		}// fin if cod_moneda
		$color++;
	}//fin while
	mysqli_free_result($result3);
	
	//convierte el total en literal
	$letras=new NroEnLetra($suma_monto2,2);
	
	$mostrar_letras = ucfirst($letras->getLetra());
	
	$busca_espacio = substr($mostrar_letras,-6,1);
	if($busca_espacio == ' ') {
		$fin_letras = substr($mostrar_letras,-5);
		$mostrar_letras = substr($mostrar_letras,0,-5);
		$mostrar_letras = $mostrar_letras .'0'. $fin_letras;
	}
	$resultmoneda = mysqli_query($link, "select txtDescription as nom_moneda
	from actCurrency
	where
	pkyCurrency = '$cod_moneda';");
	while ($row = mysqli_fetch_array($resultmoneda)) {
		$nom_moneda_letras_tot = $row['nom_moneda'];
	}
	mysqli_free_result($resultmoneda);
	?>
	<tr BGCOLOR='#ffcccc'><td colspan="5"><b>Total &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $mostrar_letras; ?> <?php echo $nom_moneda_letras_tot; ?></b></td>
    <td align="right"><b><?php echo number_format($suma_monto2, $n_decimales); ?></b></td>
    </tr>
    
    <?php 
	$pago_realizado = $suma_monto - $suma_monto2;
	
	//convierte el total en literal
	$letras=new NroEnLetra($pago_realizado,2);
	
	$mostrar_letras = ucfirst($letras->getLetra());
	
	$busca_espacio = substr($mostrar_letras,-6,1);
	if($busca_espacio == ' ') {
		$fin_letras = substr($mostrar_letras,-5);
		$mostrar_letras = substr($mostrar_letras,0,-5);
		$mostrar_letras = $mostrar_letras .'0'. $fin_letras;
	}
	$resultmoneda = mysqli_query($link, "select txtDescription as nom_moneda
	from actCurrency
	where
	pkyCurrency = '$cod_moneda';");
	while ($row = mysqli_fetch_array($resultmoneda)) {
		$nom_moneda_letras_tot = $row['nom_moneda'];
	}
	mysqli_free_result($resultmoneda);
	
	if($balance >= 0) { 
		$nom_saldo = 'Saldo a pagar';
	} else {
		$nom_saldo = 'Saldo a cobrar';
	}
	
	//convierte el total en literal
	$letras2=new NroEnLetra($balance,2);
	
	$mostrar_letras2 = ucfirst($letras2->getLetra());
	
	$busca_espacio = substr($mostrar_letras2,-6,1);
	if($busca_espacio == ' ') {
		$fin_letras2 = substr($mostrar_letras2,-5);
		$mostrar_letras2 = substr($mostrar_letras2,0,-5);
		$mostrar_letras2 = $mostrar_letras2 .'0'. $fin_letras2;
	}
	$resultmoneda2 = mysqli_query($link, "select txtDescription as nom_moneda
	from actCurrency
	where
	pkyCurrency = '$cod_moneda';");
	while ($row = mysqli_fetch_array($resultmoneda2)) {
		$nom_moneda_letras_tot2 = $row['nom_moneda'];
	}
	mysqli_free_result($resultmoneda2);
	?>
    <tr><td colspan="6">&nbsp;</td></tr>
    <tr BGCOLOR='#ffcccc'><td colspan="5"><b>Total pagado (<?php echo $via_pago; ?>)  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $mostrar_letras; ?> <?php echo $nom_moneda_letras_tot; ?></b></td>
    <td align="right"><b><?php echo number_format($pago_realizado, $n_decimales); ?></b></td>
    </tr>
    <!-- se comento hasta que se habilite el envio del saldo total desde el Pulzar, solo la muestra del resultado el algoritmo esta intacto arriba (se inicia balance=0 par evitar errores)
    <tr BGCOLOR='#ffcccc'><td colspan="5"><b><?php echo $nom_saldo; ?>   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $mostrar_letras2; ?> <?php echo $nom_moneda_letras_tot2; ?></b></td>
    <td align="right"><b><?php echo number_format($balance, $n_decimales); ?></b></td>
    </tr>
    -->
	</tbody>
</table>
<br />

<?php } ?>

<?php
    if($enabled == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>REGISTRO ANULADO</font></td></tr></table>");
			}
	if($authorized == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>REGISTRO PENDIENTE DE AUTORIZACI&Oacute;N</font></td></tr></table>");
			}
?>

<?php
if($muestra_asiento == 1) {//if muestra_asiento 1
	
	if($cod_asiento != NULL){//if cod_asiento
		$result3 = mysqli_query($link, "select a.pkyAccountEntry as cod_mov_det, a.fldTransactionNumber as nro_trans, a.fldDocumentNumber as nro_doc, a.fldDate as fecha, b.fldName as tipo, a.codExchangeRate as cod_tc, a.txtDescription as glosa, a.sysEnabled as enabled, a.sysAuthorized as authorized
from actAccountEntry a, actAccountEntryType b
where
a.pkyAccountEntry = '$cod_asiento' and
a.codType = b.pkyAccountEntryType;");
		while ($row = mysqli_fetch_array($result3)) {
		$cod_asiento2 = $row['cod_mov_det'];
		$nro_trans2 = $row['nro_trans'];
		$nro_doc2 = $row['nro_doc'];
		$fecha_asiento2 = $row['fecha'];
		$tipo_asiento2 = $row['tipo'];
		$cod_tc2 = $row['cod_tc'];
		$glosa2 = $row['glosa'];
		$glosa2 = nl2br($glosa2);
		$glosa2 = str_replace('\n','<br />',$glosa2);
		$enabled2 = $row['enabled'];
		$authorized2 = $row['authorized'];
		}
		mysqli_free_result($result3);
		
		$result6 = mysqli_query($link, "select a.pkyAccountEntryDetail as cod_asiento_det, a.codAccountEntry as cod_asiento, a.codAccount as cod_cuenta, b.fldName as nom_cuenta, a.fldDebit as debe, a.fldCredit as haber
from actAccountEntryDetail a, actAccount b
where
a.codAccountEntry = '$cod_asiento2' and
a.codAccount = b.pkyAccount 
order by haber, cod_asiento_det;");
		
		if($muestra_cc == 1){
		$texto_asiento = '<a href="../../ModulePayments/VoucherPayment/index.php?pkyPayment='.$cod_pago.'&printAccountEntry=1&rep=2&user='.$usuario.'" title="Asiento Contable, Sin Centros de Costo" style="text-decoration:none; color:#000099; font-style:normal;">Asiento Contable, Con Centros de Costo</a>';
		} else {
		$texto_asiento = '<a href="../../ModulePayments/VoucherPayment/index.php?pkyPayment='.$cod_pago.'&printAccountEntry=1&rep=1&user='.$usuario.'" title="Asiento Contable, Con Centros de Costo" style="text-decoration:none; color:#000099; font-style:normal;">Asiento Contable</a>';
		}
		$control_vista_asiento = 1;
	} else {//else cod_asiento
		$texto_asiento = "Asiento Contable<br /><br /><table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>NO EXISTE ASIENTO CONTABLE ASOCIADO A ESTE MOVIMIENTO</font></td></tr></table>";
		$enabled2 = 1;
		$authorized2 = 1;
		$control_vista_asiento = 0;
	}//fin if else cod_asiento
	
	
?>
<p>&nbsp;</p>
<p><center><font color="#000099"><b><?php echo $texto_asiento; ?></b></font></center></p>
<?php
if($control_vista_asiento == 1) {//if $control_vista_asiento = 1
?>
<table border="1" align="center" cellpadding="2" width="100%">
	<TR> 
	<TD width="25%"><b>Cod. Reg.: </b><?php echo $cod_asiento2; ?></TD>
    <TD width="25%"><b>Nro. Doc.: </b><?php echo $nro_doc2; ?></TD>
    <TD width="25%"><b>Moneda: </b><?php echo $nom_moneda_principal; ?></TD>
    <TD width="25%"><b>Fecha de Reg.: </b><?php echo $fecha_asiento2; ?></TD>
    </TR>
    <TR>
    <TD colspan="2"><b>Tipo de Asiento: </b><?php echo $tipo_asiento2; ?></TD>
    <TD colspan="2"><b>Nro. Transacci&oacute;n: </b><?php echo $nro_trans2; ?></TD>
    </TR>
    <TR>
    <TD colspan="4"><b>Glosa: </b><?php echo $glosa2; ?></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>Cod. Asiento</b></TD>
    <TD align="center"><b>Cod. Cuenta</b></TD>
    <TD align="center"><b>Cuenta</b></TD>
    <TD align="center"><b>Debe</b></TD>
    <TD align="center"><b>Haber</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">
    <?php
    $color = 2;
	$suma_debe = 0;
	$suma_haber = 0;
	while ($row = mysqli_fetch_array($result6)) { //while 6
		$cod_detalle_ = $row['cod_asiento_det'];
		$cod_asiento_ = $row['cod_asiento'];//cod asiento
		$cod_cuenta_ = $row['cod_cuenta'];//cod cuenta
		$nom_cuenta_ = $row['nom_cuenta'];//nombre cuenta
		if($mi_cod_pais == 1) {
		$debe_ = $row['debe'];
		$haber_ = $row['haber'];
		}
		if($mi_cod_pais == 176) {
		$debe_ = round($row['debe'], $n_decimales);
		$haber_ = round($row['haber'], $n_decimales);
		}
			$result7 = mysqli_query($link, "select a.codCostCenter as cod_cc, b.fldName as nombre,
			a.fldPercent as porcentaje
			from
			actAccountEntryDetailCC a, actCostCenter b
			where
			a.codAccountEntryDetail = '$cod_detalle_' and 
			a.codCostCenter = b.pkyCostCenter;");
			$matriz_cc = array();
			$c = 1;
			while ($row = mysqli_fetch_array($result7)) {
			$matriz_cc [$c][1] = $row['cod_cc'];
			$matriz_cc [$c][2] = $row['nombre'];
			$matriz_cc [$c][3] = $row['porcentaje'];
			$matriz_cc [$c][4] = $row['porcentaje'] / 100 * $debe_;
			$matriz_cc [$c][5] = $row['porcentaje'] / 100 * $haber_;
			$c++;
			}
			mysqli_free_result($result7);
			$fin_cc = $c - 1;
			
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $cod_detalle_);//cod 
		} else {
		printf("<tr><td align='center'>%s</td>", $cod_detalle_);//cod
		} //cierra if color
		printf("<td align='center'>%s</td>", $cod_asiento_);//cod asiento
		printf("<td align='center'>%s</td>", $cod_cuenta_);//cod cuenta
		if($muestra_cc == 0) {//if muestra_cc = 0
			if($haber_ == 0) { //if identado haber
				printf("<td align='left'>%s</td>", $nom_cuenta_);//nombre cuenta
			} else {
				printf("<td align='left'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%s</td>", $nom_cuenta_);//nombre cuenta
			} //fin if identado haber
		} else {
			$sum_porc_cc = 0;
			$sum_cc = 0;
			if($haber_ == 0) { //if identado haber2
				echo "<td align='left'>" .$nom_cuenta_. "<br/>";
			} else {
				echo "<td align='left'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .$nom_cuenta_. "<br/>";
			} //fin if identado haber2
			echo "<table border='0' align='left' cellpadding='2'><tr>";
			for($c = 1; $c <= $fin_cc; $c++){//for $c
			echo "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .$matriz_cc [$c][1]. "</td>";
			echo "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .$matriz_cc [$c][2]. "</td>";
			echo "<td align='right'>&nbsp;&nbsp;&nbsp;&nbsp;" .number_format($matriz_cc [$c][3], $n_descuento). "%" . "</td>";
			if($matriz_cc [$c][4] != 0){
			echo "<td align='right'>&nbsp;&nbsp;&nbsp;&nbsp;" .number_format($matriz_cc [$c][4], $n_decimales). "</td>";
			$sum_cc = $sum_cc + $matriz_cc [$c][4];
			} else {
			echo "<td align='right'>&nbsp;&nbsp;&nbsp;&nbsp;" .number_format($matriz_cc [$c][5], $n_decimales). "</td>";
			$sum_cc = $sum_cc + $matriz_cc [$c][5];
			}
			echo "</tr>";
			$sum_porc_cc = $sum_porc_cc + $matriz_cc [$c][3];
			
			}//fin for $c
			echo "<tr><td colspan='2'>&nbsp;</td>";
			echo "<td align='right'><HR width='90%' align='right'>" . "</td>";
			echo "<td align='right'><HR width='90%' align='right'>" . "</td>";
			echo "<tr><td colspan='2'>&nbsp;</td>";
			echo "<td align='right'>&nbsp;&nbsp;&nbsp;&nbsp;" .number_format($sum_porc_cc, $n_descuento). "%" . "</td>";
			echo "<td align='right'>&nbsp;&nbsp;&nbsp;&nbsp;" .number_format($sum_cc, $n_decimales). "</td></tr>";
			echo "</table></td>";
		}//fin if muestra_cc = 0
		printf("<td align='right'>%s</td>", number_format($debe_, $n_decimales));//debe
		printf("<td align='right'>%s</td></tr>", number_format($haber_, $n_decimales));//haber
		
		$suma_debe = $suma_debe + $debe_;
		$suma_haber = $suma_haber + $haber_;
		$color++;
	}//fin while 6
	mysqli_free_result($result6);
	
	//convierte el total en literal
	$letras=new NroEnLetra($suma_debe,2);
	
	$mostrar_letras = ucfirst($letras->getLetra());
	
	$busca_espacio = substr($mostrar_letras,-6,1);
	if($busca_espacio == ' ') {
		$fin_letras = substr($mostrar_letras,-5);
		$mostrar_letras = substr($mostrar_letras,0,-5);
		$mostrar_letras = $mostrar_letras .'0'. $fin_letras;
	}
	?>
	<tr BGCOLOR='#ffcccc'><td colspan="4"><b>Total &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $mostrar_letras; ?> <?php echo $nom_moneda_letras; ?></b></td>
    <td align="right"><b><?php echo number_format($suma_debe, $n_decimales); ?></b></td>
    <td align="right"><b><?php echo number_format($suma_haber, $n_decimales); ?></b></td>
    </tr>
</tbody>    
</table>
<br />

<?php
    if($enabled2 == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>ASIENTO ANULADO</font></td></tr></table>");
			}
	if($authorized2 == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>ASIENTO PENDIENTE DE AUTORIZACI&Oacute;N</font></td></tr></table>");
			}
	} //fin if $control_vista_asiento = 1
}//fin if muestra_asiento 1
?>

<p>&nbsp;</p>
<br /><br /><br />
<table border="0" align="center" cellpadding="2" width="90%">
<tr>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
</tr><tr>
<td align="center">Elaborado por: <?php echo $usuario_reg; ?></td>
<td align="center"><?php echo $firma_contabilidad; ?></td>
<td align="center"><?php echo $firma_gerencia; ?></td>
<td align="center"><?php echo $firma_interesado; ?></td>
</tr><tr>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
</tr><tr>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
</tr>
</table>

</body>
</html>
