<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Nota de Movimiento de Mercader&iacute;a</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";
include "../../token.php";

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

//index.php?pkyItemMovement=213&showBatch=1&printAccountEntry=1&user=Luigi&token=123456
$cod_mov = $_GET["pkyItemMovement"];
$muestra_lote = $_GET["showBatch"];
$muestra_asiento = $_GET["printAccountEntry"];
//$mostrar_asiento_cc = $_GET["showCC"];
//$usuario = $_GET["user"];
//$n_decimales = 6;
if($rep == 1) { $muestra_cc = 1; }
if($rep == 2) { $muestra_cc = 0; }

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

if($cod_tipo_usuario == 3 && $imprime_costo == 0) { 
	$ver_costo = 0;
} else {
	$ver_costo = 1;
}

?>
<body>

<?php
	
// Inicio impresion Nota de Movimiento de Mercaderia

//ver de que tipo es ingreso, egreso o traspaso

$resulttipo = mysqli_query($link, "select pkyItemMovement as cod_mov, codType as cod_tipo
from invItemMovement 
where
pkyItemMovement = '$cod_mov';");

while ($row = mysqli_fetch_array($resulttipo)) {
	$cod_tipo = $row['cod_tipo'];
	}
mysqli_free_result($resulttipo);

//para ingreso o egreso
if($cod_tipo == 1) {//if tipo = 1
$valor_tipo = ' a.codTargetWarehouse = e.pkyWarehouse and ';
$valor_almacenes = ' null as almacen_orig, e.fldName as almacen_dest ';
}//fin if tipo = 1

if($cod_tipo == 2) {//if tipo = 2
$valor_tipo = ' a.codSourceWarehouse = e.pkyWarehouse and ';
$valor_almacenes = ' e.fldName as almacen_orig, null as almacen_dest ';
}//fin if tipo = 2

//ingreso o egreso
if($cod_tipo == 1 || $cod_tipo == 2) {//if tipo = 1 o 2
$result = mysqli_query($link, "select a.pkyItemMovement as cod_mov, a.fldDocumentNumber as nro_doc,
a.fldDate as fecha, b.fldName as tipo_mov, c.fldName as concepto_mov,
a.codSupplier as cod_empresa, $valor_almacenes, a.codCurrency as cod_moneda,
f.fldName as moneda, a.codExchangeRate as cod_tc, 
a.codAccountEntry as cod_asiento, a.txtDescription as glosa, a.sysEnabled as enabled,
a.sysAuthorized as authorized, a.fldParentComponent as papa, a.cosComponentKey as cod_papa, g.fldName as usuario_reg, a.sysContext as cod_contexto, s.fldName as contexto
from invItemMovement a, invItemMovementType b, invItemMovementTypeConcept c, invWarehouse e, actCurrency f, accGroup g, srvContext s
where
a.pkyItemMovement = '$cod_mov' and
a.codType = b.pkyItemMovementType and 
a.codTypeConcept = c.pkyItemMovementTypeConcept and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.codType = '$cod_tipo' and 
a.sysContext = s.pkyContext and 
$valor_tipo 
a.codCurrency = f.pkyCurrency;");
}//fin if tipo = 1 o 2

//traspaso
if($cod_tipo == 3) {//if tipo = 3
$result = mysqli_query($link, "select a.pkyItemMovement as cod_mov, a.fldDocumentNumber as nro_doc,
a.fldDate as fecha, b.fldName as tipo_mov, c.fldName as concepto_mov, a.codSupplier as cod_empresa,
e.fldName as almacen_orig, d.fldName as almacen_dest, a.codCurrency as cod_moneda,
f.fldName as moneda, a.codExchangeRate as cod_tc, 
a.codAccountEntry as cod_asiento, a.txtDescription as glosa, a.sysEnabled as enabled,
a.sysAuthorized as authorized, a.fldParentComponent as papa, a.cosComponentKey as cod_papa, g.fldName as usuario_reg, a.sysContext as cod_contexto, s.fldName as contexto 
from invItemMovement a, invItemMovementType b, invItemMovementTypeConcept c, invWarehouse d, invWarehouse e, actCurrency f, accGroup g, srvContext s
where
a.pkyItemMovement = '$cod_mov' and
a.codType = b.pkyItemMovementType and 
a.codTypeConcept = c.pkyItemMovementTypeConcept and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.codType = '$cod_tipo' and 
a.codTargetWarehouse = d.pkyWarehouse and 
a.codSourceWarehouse = e.pkyWarehouse and 
a.sysContext = s.pkyContext and 
a.codCurrency = f.pkyCurrency;");
}//fin if tipo = 3

	while ($row = mysqli_fetch_array($result)) {
		$cod_mov = $row['cod_mov'];
		$nro_doc = $row['nro_doc'];
		$fecha_mov = $row['fecha'];
		$tipo_mov = $row['tipo_mov'];
		$concepto_mov = $row['concepto_mov'];
		$almacen_orig = $row['almacen_orig'];
		$almacen_dest = $row['almacen_dest'];
		$cod_moneda = $row['cod_moneda'];
		$moneda = $row['moneda'];
		$cod_tc = $row['cod_tc'];
		$cod_asiento = $row['cod_asiento'];
		$glosa = $row['glosa'];
		//$glosa = nl2br($glosa);
		//$glosa = str_replace("a","XXX",$glosa);
		$glosa = nl2br($glosa);//quita salto de linea 
		$glosa = str_replace('\n','<br />',$glosa);//reemplaza para salto de linea
		$usuario_reg = $row['usuario_reg'];
		$enabled = $row['enabled'];
		$authorized = $row['authorized'];
		$cod_empresa = $row['cod_empresa'];
		$cod_mov_papa = $row['cod_papa'];
		$mov_papa = $row['papa'];
		$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";
	
		if($cod_empresa != NULL) {//if cod empresa
			$resultemp = mysqli_query($link, "select fldName as empresa
							from agnCompany
							where
							pkyCompany = '$cod_empresa';");
			while ($row = mysqli_fetch_array($resultemp)) {
				$empresa = $row['empresa'];
			}
			mysqli_free_result($resultemp);
		} else {
			$empresa = NULL;
		}//fin if cod empresa
	}
	mysqli_free_result($result);
	

$resulttc = mysqli_query($link, "select pkyExchangeRate as cod_tc, fldDate as fecha, fldTime as hora
from
actExchangeRate
where
pkyExchangeRate = '$cod_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 = '$cod_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;

	//listar el detalle
	$result2 = mysqli_query($link, "select a.pkyItemMovementDetail as cod_mov_det, b.fldName as item,  b.fldBarcode as cod_barra, a.codItem as cod_item, a.fldQuantity as cantidad, a.fldCost as costo, a.fldExtraCost as costo_extra, a.fldTotaCost as total_costo, b.fldFactoryKey as cod_fabrica, c.fldName as u_men
from invItemMovementDetail a, invItem b, invStorageUnit c
where 
b.codMinimumStorageUnit = c.pkyStorageUnit and 
a.codItemMovement = '$cod_mov' and
a.codItem = b.pkyItem 
order by b.fldName;");

if($muestra_lote == 1){//if $muestra_lote
	$texto_lote = ', Con Lotes';
	} else {
	$texto_lote = '';
	}

//listar gastos
	$result3 = mysqli_query($link, "select a.pkyTransferExpense as codigo, a.codPayrollExpenses as cod_pla, b.fldNumber as planilla, a.fldTotaCost as gasto 
from 
invTransferExpense a, pchPayrollExpenses b
where
codItemMovement = '$cod_mov' and
a.codPayrollExpenses = b.pkyPayrollExpenses and 
a.sysEnabled = 1 and a.sysAuthorized = 1;");

$hay_gasto = 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 Movimiento de Mercader&iacute;a<?php echo $texto_lote; ?></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_mov; ?></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_mov; ?></TD>
        </TR>
        <TR>
        <TD><b>Concepto de Mov.: </b><?php echo $concepto_mov; ?></TD>
        <TD><b>Tipo de Mov.: </b><?php echo $tipo_mov; ?></TD>
        <TD><b>Empresa: </b><?php echo $empresa; ?></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; ?>&token=<?php echo $token; ?>" title="Asiento Contable"><?php echo $cod_asiento; ?></a>
		<table border="0" align="right"><tr><td>
		<?php if($muestra_asiento == 0) { ?>
			<a href="../../ModuleInventory/VoucherItemMovement/index.php?pkyItemMovement=<?php echo $cod_mov; ?>&printAccountEntry=1&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Mostrar Asiento Contable" style="text-decoration:none; color:#0000FF; font-style:normal;"> + </a>
		<?php } else { ?>
			<a href="../../ModuleInventory/VoucherItemMovement/index.php?pkyItemMovement=<?php echo $cod_mov; ?>&printAccountEntry=0&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Ocultar Asiento Contable" style="text-decoration:none; color:#0000FF; font-style:normal;"> - </a>
        <?php } ?>
		</td></tr></table>
		</TD>
        <TD><b>Contexto: </b><?php echo $cod_contexto; ?> - <?php echo $contexto; ?></TD>
        <TD><b>Almac&eacute;n Origen: </b><?php echo $almacen_orig; ?></TD>
        <TD><b>Almac&eacute;n Destino: </b><?php echo $almacen_dest; ?></TD>
        </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; ?>&token=<?php echo $token; ?>" 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>

<p><font color="#000099"><b>&nbsp;Detalle</b></font></p>
<table border="1" align="center" cellpadding="2" width="100%">
 	<thead>
    <TR> 
    <TD align="center"><b>Cod. Item</b></TD>
    <TD align="center"><b>Cod. F&aacute;brica</b></TD>
    <TD align="center"><b>Cod. Barra</b></TD>
    <TD align="center" width="400"><b>Item</b></TD>
    <TD align="center"><b>U. Menor</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <?php if($ver_costo == 1) { ?>
    <TD align="center"><b>Costo</b></TD>
    <TD align="center"><b>Costo Extra</b></TD>
    <TD align="center"><b>Costo Total</b></TD>
    <?php } ?> 
    </TR>
    </thead>
    <tbody class="marcafila">
    <?php
    $color = 2;
	$suma_cant = 0;
	$suma_costo = 0;
	$suma_costo_extra = 0;
	$suma_costo_total = 0;
	while ($row = mysqli_fetch_array($result2)) { //while
		$cod_mov_det = $row['cod_mov_det'];
		$cod_item = $row['cod_item'];
		$cod_barra = $row['cod_barra'];
		$u_men = $row['u_men'];
		$cod_fabrica = $row['cod_fabrica'];
		$cant_det = $row['cantidad'];
		$costo_det = $row['costo'];
		$costo_extra_det = $row['costo_extra'];
		$costo_total_det = $row['total_costo'];
		$cod_item_det = $row['cod_item'];
		
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $cod_item);//cod item
		} else {
		printf("<tr><td align='center'>%s</td>", $cod_item);//cod item
		} //cierra if color
		
		printf("<td align='center'>%s</td>", $cod_fabrica);//cod fabrica
		printf("<td align='center'>%s</td>", $cod_barra);//codigo de barra
		
		if($muestra_lote == 1) {//if muestra_lote 1
		
		if($cod_tipo == 3) { // if 3
			$result4 = mysqli_query($link, "select a.codBatch as cod_lote, a.fldID as id_lote, case when a.codType = 1 then a.fldQuantity else a.fldQuantity * -1 end as cant_mov, a.fldExpirationDate as fecha_exp, a.fldPriority as prioridad, a.codType as cod_tipo
		from invItemMovementDetailBatch a
		where
		a.codItemMovementDetail = '$cod_mov_det' and 
		a.codItemMovement = '$cod_mov' 
		order by cod_tipo desc;");
		} else { // else if 3
			$result4 = mysqli_query($link, "select a.codBatch as cod_lote, a.fldID as id_lote,  a.fldQuantity as cant_mov, a.fldExpirationDate as fecha_exp, a.fldPriority as prioridad, a.codType as cod_tipo
		from invItemMovementDetailBatch a
		where
		a.codItemMovementDetail = '$cod_mov_det' and 
		a.codItemMovement = '$cod_mov' 
		order by cod_tipo desc;");
		} // fin if 3
		
		printf("<td>%s<br/>", $row['item']);//nombre item
		//abrir la tabla para los lotes
		echo '<table border="0" align="left" cellpadding="3">
		<TR> 
		<TD align="center"><b>Cod. Lote</b></TD>
		<TD align="center"><b>ID</b></TD>
		<TD align="center"><b>Cantidad</b></TD>
		<TD align="center"><b>Expiraci&oacute;n</b></TD>
		<TD align="center"><b>Prioridad</b></TD>
		</TR>';
		$suma_cant_lote = 0;
		while ($row = mysqli_fetch_array($result4)) { //while
		
		$cod_lote = $row['cod_lote'];
		$id_lote = $row['id_lote'];
		$cant_mov = $row['cant_mov'];
		$fecha_exp = $row['fecha_exp'];
		$prioridad = $row['prioridad'];
		
		printf("<tr><td align='center'>%s</td>", $cod_lote);//cod lote
		printf("<td align='center'>%s</td>", $id_lote);//id lote
		printf("<td align='right'>%s</td>", number_format($cant_mov, $n_cantidad));//cant mov
		printf("<td align='right'>%s</td>", $fecha_exp);//fecha expiracion
		printf("<td align='right'>%s</td></tr>", $prioridad);//prioridad
		if($cant_mov >= 0) { //if 0
			$suma_cant_lote = $suma_cant_lote + $cant_mov;
		}//fin if 0
		}//fin while
		mysqli_free_result($result4);
		
		echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td align='right'><HR width='90%' align='right'></td><td>&nbsp;</td><td>&nbsp;</td></tr>";
		echo '<tr><td colspan="2"><b>Total Lotes</b></td><td align="right">'. number_format($suma_cant_lote, $n_cantidad).'</td><td>&nbsp;</td><td>&nbsp;</td></tr>
		</table></td>';
		}//fin if muestra_lote 1
		else {//else muestra lote 1
		printf("<td>%s</td>", $row['item']);//nombre item
		}//fin else muestra lote 1
		
		printf("<td align='center'>%s</td>", $u_men);//unidad menor
		printf("<td align='right'>%s</td>", number_format($cant_det, $n_cantidad));//cant
		if($ver_costo == 1) { //if costo=1
		printf("<td align='right'>%s</td>", number_format($costo_det, $n_decimales));//costo
		printf("<td align='right'>%s</td>", number_format($costo_extra_det, $n_decimales));//costo extra
		printf("<td align='right'>%s</td>", number_format($costo_total_det, $n_decimales));//total costo
		} //fin if costo=1
		printf("</tr>");
		$suma_cant = $suma_cant + $cant_det;
		$suma_costo = $suma_costo + $costo_det;
		$suma_costo_extra = $suma_costo_extra + $costo_extra_det;
		$suma_costo_total = $suma_costo_total + $costo_total_det;
		$color++;
		
	}//fin while
	mysqli_free_result($result2);
	
	//convierte el total en literal
	$letras=new NroEnLetra($suma_costo_total,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_cant, $n_cantidad); ?></b></td>
    <?php if($ver_costo == 1) { ?>
    <td align="right"><b><?php echo number_format($suma_costo, $n_decimales); ?></b></td>
    <td align="right"><b><?php echo number_format($suma_costo_extra, $n_decimales); ?></b></td>
    <td align="right"><b><?php echo number_format($suma_costo_total, $n_decimales); ?></b></td>
     <?php } ?>
    </tr>
</tbody>    
</table>
<br />

<?php if($hay_gasto != 0) { //if hay_gasto ?>
<p><font color="#000099"><b>&nbsp;Gasto</b></font></p>
<table border="1" align="left" cellpadding="2" width="100%">
 	<thead>
    <TR> 
    <TD align="center" width="20%"><b>Cod. Planilla</b></TD>
    <TD align="center" width="60%"><b>Planilla de Gastos</b></TD>
    <TD align="center" width="20%"><b>Costo Total</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">
    <?php
    $color = 2;
	$suma_gasto = 0;
	while ($row = mysqli_fetch_array($result3)) { //while
		$cod_pla = $row['cod_pla'];
		$planilla = $row['planilla'];
		$gasto = $row['gasto'];
		
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$cod_pla&user=$usuario&token=$token' title='Planilla de Gasto'>%s</a></td>", $cod_pla);//cod planilla
		} else {
		printf("<tr><td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$cod_pla&user=$usuario&token=$token' title='Planilla de Gasto'>%s</a></td>", $cod_pla);//cod planilla
		} //cierra if color
		
		printf("<td align='left'>%s</td>", $planilla);//planilla
		printf("<td align='right'>%s</td></tr>", number_format($gasto, $n_decimales));//gasto
		
		$suma_gasto = $suma_gasto + $gasto;
		$color++;
	}
	mysqli_free_result($result3);
	
	//convierte el total en literal
	$letras=new NroEnLetra($suma_gasto,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="2"><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_gasto, $n_decimales); ?></b></td>	
</tr>	
</tbody>    
</table>
<br />

<?php } //fin if hay_gasto ?> 

<?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="../../ModuleInventory/VoucherItemMovement/index.php?pkyItemMovement='.$cod_mov.'&printAccountEntry=1&rep=2&user='.$usuario.'&token='.$token.'" 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="../../ModuleInventory/VoucherItemMovement/index.php?pkyItemMovement='.$cod_mov.'&printAccountEntry=1&rep=1&user='.$usuario.'&token='.$token.'" 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, b.codType as cod_tipo_cc, c.fldName as tipo_cc, a.fldPercent as porcentaje
			from
			actAccountEntryDetailCC a, actCostCenter b, actCostCenterType c
			where
			a.codAccountEntryDetail = '$cod_detalle_' and 
			a.codCostCenter = b.pkyCostCenter and 
			b.codType = c.pkyCostCenterType;");
			$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_;
			$matriz_cc [$c][6] = $row['cod_tipo_cc'];
			$matriz_cc [$c][7] = $row['tipo_cc'];
			$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
			$fila_titulo_cc = 0;
			echo "<table border='0' align='left' cellpadding='2'>";
			for($c = 1; $c <= $fin_cc; $c++){//for $c
				//si el cod_tipo es diferente de 0 para mostrar el titulo en cada grupo de cc
				if($fila_titulo_cc != $matriz_cc [$c][6]){
					echo "<tr><td colspan='4'><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .$matriz_cc [$c][7]. "</b></td></tr>";
					$fila_titulo_cc = $matriz_cc [$c][6];
				}
				echo "<tr><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];
				if($fila_titulo_cc != $matriz_cc [$c+1][6]){
					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>";
					$sum_cc = 0;
					$sum_porc_cc = 0;
				}
			}//fin for $c
			
			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>
