<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Nota Recepci&oacute;n 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";

//index.php?pkyReceiptOfGoods=46&user=Luigi&token=123456
$rep=$_GET["rep"];
$tipo=$_GET["tipo"];

$cod_orden = $_GET["pkyReceiptOfGoods"];
$muestra_asiento = $_GET["printAccountEntry"];
//$muestra_asiento = 1;
//$mostrar_asiento_cc = $_GET["showCC"];
//$usuario = $_GET["user"];

$muestra_gasto_det=$_GET["muestra_gasto_det"];

//$n_decimales = 6;
if($rep == 1) { $muestra_cc = 1; }
if($rep == 2) { $muestra_cc = 0; }

if($muestra_gasto_det == '') { $muestra_gasto_det = 1; }

?>
<body>

<?php

// Inicio impresion recepcion de mercaderia

$result = mysqli_query($link, "select a.pkyReceiptOfGoods as cod_orden, 
a.codPurchaseOrder as cod_orden_compra, a.fldDate as fecha, a.fldNumber as nro_doc,  
a.codDocumentType as cod_dcto, 
f.fldName as dcto, a.codSupplier as cod_prove, c.fldName as proveedor, 
d.fldName as almacen, a.codCurrency as cod_moneda, e.fldName as moneda, 
a.codExchangeRate as cod_tc, a.fldGrossTotal as total_bruto,  
a.fldNetTotal as total_neto, a.codAccountPayable as cod_cxp, 
a.codAccountEntry as cod_asiento, a.codItemMovement as cod_mov_merc, 
a.codPettyCashMovement as cod_mov_caja, a.txtDescription as glosa, 
a.sysEnabled as enabled, a.sysAuthorized as authorized, g.fldName as usuario_reg, a.sysContext as cod_contexto, s.fldName as contexto
from
pchReceiptOfGoods a, agnCompany c, invWarehouse d, actCurrency e, txsDocumentType f, accGroup g, srvContext s
where
a.pkyReceiptOfGoods = '$cod_orden' and 
a.codSupplier = c.pkyCompany and 
a.codSupplier = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codCurrency = e.pkyCurrency and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.sysContext = s.pkyContext and 
a.codDocumentType = f.pkyDocumentType;");
	while ($row = mysqli_fetch_array($result)) {
		$cod_orden = $row['cod_orden'];
		$cod_orden_compra = $row['cod_orden_compra'];
		$fecha_orden = $row['fecha'];
		$nro_doc = $row['nro_doc'];
		$documento = $row['dcto'];
		$cod_documento = $row['cod_dcto'];
		$estado = $row['estado'];
		$cod_prove = $row['cod_prove'];
		$proveedor = $row['proveedor'];
		$almacen = $row['almacen'];
		$cod_moneda = $row['cod_moneda'];
		$moneda = $row['moneda'];
		$cod_tc = $row['cod_tc'];
		$total_bruto = $row['total_bruto'];
		$total_neto = $row['total_neto'];
		$cod_cxp = $row['cod_cxp'];
		$cod_asiento = $row['cod_asiento'];
		$cod_mov_merc = $row['cod_mov_merc'];
		$cod_mov_caja = $row['cod_mov_caja'];
		$glosa = $row['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_contexto = $row['cod_contexto'];
		$contexto = $row['contexto'];
	}
	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';");
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;

//para encontrar la factura
if($cod_documento == 1) { 
$result9 = mysqli_query($link, "select pkyPurchasesLedger as codigo, fldNumber as nro_fac, fldName as nombre, fldNIT as nit
									from txsPurchasesLedger
									where 
									fldParentComponent = 'ComponentReceiptOfGoods' and 
									cosComponentKey = '$cod_orden';");
	while ($row = mysqli_fetch_array($result9)) {
	$cod_fac = $row['codigo'];
	$fac_compra = $row['nro_fac'];
	$emp_compra = $row['nombre'];
	$nit_compra = $row['nit'];
	}
	mysqli_free_result($result9);
$datos_fac = ' - <a href="../../ModuleTaxes/VoucherPurchasesLedger/index.php?pkyPurchasesLedger='.$cod_fac.'&user='.$usuario.'&token='.$token.'" title="Factura de Compra">'.$cod_fac.'</a> Nro: '.$fac_compra.' NIT: '.$nit_compra.' Emp: '.$emp_compra;
} else {
$datos_fac = ' ';	
}

	$result2 = mysqli_query($link, "select a.pkyReceiptOfGoodsDetail as cod_orden_det, 
a.codItem as cod_item, b.fldName as item, b.fldFactoryKey as cod_fabrica, 
a.fldQuantity as cantidad, a.fldPrice as costo, 
a.fldApportionCost as costo_extra, a.fldTotalPrice as precio_total, a.fldVAT as iva, 
a.fldTotalNetPrice as costo_ing_inv, c.fldName as u_mayor, d.fldName as u_menor, 
a.fldStorageUnitCoefficient as coeficiente
from
pchReceiptOfGoodsDetail a, invItem b, invStorageUnit c, invStorageUnit d
where
a.codMaximumStorageUnit = c.pkyStorageUnit and 
a.codMinimumStorageUnit = d.pkyStorageUnit and
a.codReceiptOfGoods = '$cod_orden' and 
a.codItem = b.pkyItem 
order by item;");
/*
//lista de planillas de gasto
$result3 = mysqli_query($link, "select pkyPayrollExpenses as cod_pg, fldNumber as num_pg 
from pchPayrollExpenses 
where 
codPurchaseOrder = '$cod_orden_compra' and 
sysEnabled = 1 and sysAuthorized = 1;");


//listar gastos
	$result3 = mysqli_query($link, "select fin.cod_pg as cod_pg, fin.num_pg as num_pg, fin.gasto as gasto 
from
(select pkyPayrollExpenses as cod_pg, fldNumber as num_pg, fldTotal as gasto, codCurrency as cod_moneda, codExchangeRate as cod_tc 
from pchPayrollExpenses 
where 
codPurchaseOrder = '$cod_orden_compra' and 
codCurrency = '$cod_moneda_principal' and 
sysEnabled = 1 and sysAuthorized = 1
union all 
select t.cod_pg as cod_pg, t.num_pg as num_pg, t.gasto * tc.fldRate as gasto, t.cod_moneda as cod_moneda, t.cod_tc as cod_tc 
from
(select pkyPayrollExpenses as cod_pg, fldNumber as num_pg, fldTotal as gasto, codCurrency as cod_moneda, codExchangeRate as cod_tc   
from pchPayrollExpenses 
where 
codPurchaseOrder = '$cod_orden_compra' and 
sysEnabled = 1 and sysAuthorized = 1) t, actExchangeRateDetail tc
where
tc.codTargetCurrency = t.cod_moneda and
tc.codBaseCurrency = '$cod_moneda_principal' and
tc.codExchangeRate = t.cod_tc) fin;");
*/
$result3 = mysqli_query($link, "select fin.cod_pla as cod_pla, fin.nro_doc as nro_doc, fin.total_om as total_om, fin.suma_costo_ext as suma_costo_ext, fin.cod_moneda as cod_moneda, fin.cod_tc as cod_tc, fin.tasa as tasa
from
(select t.cod_pla as cod_pla, t.nro_doc as nro_doc, t.total as total, t.cod_moneda as cod_moneda, t.cod_tc as cod_tc, 1 as tasa, t.total * 1 as total_om, t.suma_detalle as suma_detalle, t.suma_detalle - t.total as retenciones, (t.suma_costo_ext2 + t.suma_costo_ext3) * 1 as suma_costo_ext
from
(select a.pkyPayrollExpenses as cod_pla, a.fldNumber as nro_doc, a.fldTotal as total, a.codCurrency as cod_moneda, a.codExchangeRate as cod_tc, (select sum(fldAmount) from pchPayrollExpensesDetail where codPayrollExpenses = a.pkyPayrollExpenses group by codPayrollExpenses) as suma_detalle,
(select sum(case when codDocumentType = 2 then (fldAmount - (fldAmount * $monto_iva / 100)) else 0 end) from pchPayrollExpensesDetail where codPayrollExpenses = a.pkyPayrollExpenses group by codPayrollExpenses) as suma_costo_ext2,
(select sum(case when codDocumentType = 3 or codDocumentType = 4 or codDocumentType = 5 or codDocumentType = 6 then fldAmount else 0 end) from pchPayrollExpensesDetail where codPayrollExpenses = a.pkyPayrollExpenses group by codPayrollExpenses) as suma_costo_ext3
from 
pchPayrollExpenses a
where 
a.codPurchaseOrder = '$cod_orden_compra' and
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = '$cod_moneda_principal'
union all
select t.cod_pla as cod_pla, t.nro_doc as nro_doc, t.total as total, t.cod_moneda as cod_moneda, t.cod_tc as cod_tc, tc.fldRate as tasa, t.total * tc.fldRate as total_om, t.suma_detalle as suma_detalle, t.suma_detalle - t.total as retenciones, (t.suma_costo_ext2 + t.suma_costo_ext3) * tc.fldRate as suma_costo_ext
from
(select a.pkyPayrollExpenses as cod_pla, a.fldNumber as nro_doc, a.fldTotal as total, a.codCurrency as cod_moneda, a.codExchangeRate as cod_tc, (select sum(fldAmount) from pchPayrollExpensesDetail where codPayrollExpenses = a.pkyPayrollExpenses group by codPayrollExpenses) as suma_detalle,
(select sum(case when codDocumentType = 2 then (fldAmount - (fldAmount * $monto_iva / 100)) else 0 end) from pchPayrollExpensesDetail where codPayrollExpenses = a.pkyPayrollExpenses group by codPayrollExpenses) as suma_costo_ext2,
(select sum(case when codDocumentType = 3 or codDocumentType = 4 or codDocumentType = 5 or codDocumentType = 6 then fldAmount else 0 end) from pchPayrollExpensesDetail where codPayrollExpenses = a.pkyPayrollExpenses group by codPayrollExpenses) as suma_costo_ext3
from 
pchPayrollExpenses a
where 
a.codPurchaseOrder = '$cod_orden_compra' and
a.sysEnabled = 1 and a.sysAuthorized = 1) t, actExchangeRateDetail tc
where
tc.codTargetCurrency = t.cod_moneda and
tc.codBaseCurrency = '$cod_moneda_principal' and
tc.codExchangeRate = t.cod_tc) fin
order by fin.cod_pla;");

$hay_gasto = mysqli_num_rows($result3);	

$mat_p = array();
$i = 1;
while ($row = mysqli_fetch_array($result3)) {
	$mat_p[$i][1] = $row['cod_pla'];
	$mat_p[$i][2] = $row['nro_doc'];
	$mat_p[$i][3] = $row['suma_costo_ext'];
	$mat_p[$i][4] = $row['total_om'];
	$mat_p[$i][5] = $row['cod_moneda'];
	$mat_p[$i][6] = $row['cod_tc'];
	$mat_p[$i][7] = $row['tasa'];
	$cod_moneda_exc = $row['cod_moneda'];
	$cod_tc_exc = $row['cod_tc'];
	$cod_planilla = $row['cod_pla'];
	
	if($cod_moneda_principal != $cod_moneda_exc) {//if 1
	
	$resultexc = mysqli_query($link, "select sum(case when b1.codInvoice is not NULL then (case when b1.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) as total1 from txsPurchasesLedger a1 where a1.pkyPurchasesLedger = b1.codInvoice and a1.sysEnabled = 1 and a1.sysAuthorized = 1) else 0 end) 
	else (case when b1.codDocumentType = 1 then (b1.fldAmount - (b1.fldAmount * $monto_iva / 100)) else 0 end) * tc1.fldRate end) as suma_costo_exc
	from pchPayrollExpensesDetail b1, actExchangeRateDetail tc1 
	where b1.codPayrollExpenses = '$cod_planilla' and 
	tc1.codTargetCurrency = '$cod_moneda_exc' and 
	tc1.codBaseCurrency = '$cod_moneda_principal' and 
	tc1.codExchangeRate = '$cod_tc_exc' 
	group by b1.codPayrollExpenses;");
	while ($row = mysqli_fetch_array($resultexc)) {
	$suma_costo_exc = $row['suma_costo_exc'];
	}
		
	} else {//else if 1
	
	$resultexc = mysqli_query($link, "select sum(case when b1.codInvoice is not NULL then (case when b1.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) as total1 from txsPurchasesLedger a1 where a1.pkyPurchasesLedger = b1.codInvoice and a1.sysEnabled = 1 and a1.sysAuthorized = 1) else 0 end) 
	else (case when b1.codDocumentType = 1 then (b1.fldAmount - (b1.fldAmount * $monto_iva / 100)) else 0 end) * 1 end) as suma_costo_exc 
	from pchPayrollExpensesDetail b1 
	where b1.codPayrollExpenses = '$cod_planilla'  
	group by b1.codPayrollExpenses;");
	while ($row = mysqli_fetch_array($resultexc)) {
	$suma_costo_exc = $row['suma_costo_exc'];
	}
	
	}//fin if 1
	mysqli_free_result($resultexc);	
	$mat_p[$i][8] = $mat_p[$i][3] + $suma_costo_exc;
	$i++;
}
mysqli_free_result($result3);	
$fin_i = $i - 1;


if($muestra_gasto_det == 1){
	$texto_gasto_det = '<a href="index.php?pkyReceiptOfGoods='.$cod_orden.'&muestra_gasto_det=0&user='.$usuario.'&token='.$token.'" title="Gastos con Costos Parciales" style="text-decoration:none; color:#000099; font-style:normal;">Gastos</a>';
} else {
	$texto_gasto_det = '<a href="index.php?pkyReceiptOfGoods='.$cod_orden.'&muestra_gasto_det=1&user='.$usuario.'&token='.$token.'" title="Gastos con Costos Totales" style="text-decoration:none; color:#000099; font-style:normal;">Gastos con Costos Parciales</a>';
}

?>

<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 Recepci&oacute;n de Mercader&iacute;a</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_orden; ?></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_orden; ?></TD>
        </TR>
        <TR>
        <TD><b>Orden de Compra: </b><a href="../../ModulePurchases/VoucherPurchaseOrder/index.php?pkyPurchaseOrder=<?php echo $cod_orden_compra; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Orden de Compra"><?php echo $cod_orden_compra; ?></a></TD>
        <TD><b>Proveedor: </b><?php echo $proveedor; ?></TD>
        <TD><b>Documento: </b><?php echo $documento; ?><?php echo $datos_fac; ?></TD>
        <TD><b>Almac&eacute;n: </b><?php echo $almacen; ?></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 con Centro de Costo"><?php echo $cod_asiento; ?></a>
		<table border="0" align="right"><tr><td>
		<?php if($muestra_asiento == 0) { ?>
			<a href="../../ModulePurchases/VoucherReceiptOfGoods/index.php?pkyReceiptOfGoods=<?php echo $cod_orden; ?>&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="../../ModulePurchases/VoucherReceiptOfGoods/index.php?pkyReceiptOfGoods=<?php echo $cod_orden; ?>&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>Cuenta x Pagar: </b><a href="../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=<?php echo $cod_cxp; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Nota de Cuenta por Pagar"><?php echo $cod_cxp; ?></a></TD>
        <TD><b>Mov. de Caja: </b><a href="../../ModulePettyCash/VoucherPettyCashMovement/index.php?pkyPettyCashMovement=<?php echo $cod_mov_caja; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Nota de Movimiento de Caja"><?php echo $cod_mov_caja; ?></a></TD>
        <TD><b>Mov. de Mercader&iacute;a: </b><a href="../../ModuleInventory/VoucherItemMovement/index.php?pkyItemMovement=<?php echo $cod_mov_merc; ?>&showBatch=1&printAccountEntry=0&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Nota de Movimiento de Mercaderia con Lotes"><?php echo $cod_mov_merc; ?></a></TD>
        </TR>
        <TR>
        <TD colspan="2"><b>Total Bruto: </b><?php echo number_format($total_bruto, $n_decimales); ?></TD>
        <TD colspan="2"><b>Total Neto: </b><?php echo number_format($total_neto, $n_decimales); ?></TD> 
        </TR>
        <TR>
        <TD colspan="4"><b>Contexto: </b><?php echo $cod_contexto; ?> - <?php echo $contexto; ?></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>", 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. Item</b></TD>
    <TD align="center"><b>Cod. Fab.</b></TD>
    <TD align="center"><b>Item</b></TD>
    <TD align="center"><b>Uni. May.</b></TD>
    <TD align="center"><b>Uni. Men.</b></TD>
    <TD align="center"><b>Coef.</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Costo</b></TD>
    <TD align="center"><b>Costo Extra</b></TD>
    <TD align="center"><b>Costo Ing. Inv.</b></TD>
    <TD align="center"><b>IVA</b></TD>
    <TD align="center"><b>Precio Total</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">
    <?php
    $color = 2;
	$suma_costo_extra = 0;
	$suma_precio = 0;
	$suma_iva = 0;
	$suma_costo = 0;
	
	while ($row = mysqli_fetch_array($result2)) { //while
	
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $row['cod_item']);//cod item
		} else {
		printf("<tr><td align='center'>%s</td>", $row['cod_item']);//cod item
		} //cierra if color
		printf("<td align='center'>%s</td>", $row['cod_fabrica']);//cod fabrica
		printf("<td>%s</td>", $row['item']);//nombre item
		printf("<td align='left'>%s</td>", $row['u_mayor']);//unidad mayor
		printf("<td align='left'>%s</td>", $row['u_menor']);//unidad menor
		printf("<td align='right'>%s</td>", number_format($row['coeficiente'],0));//coeficiente
		printf("<td align='right'>%s</td>", number_format($row['cantidad'],$n_cantidad));//cant
		printf("<td align='right'>%s</td>", number_format($row['costo'], $n_decimales));//costo
		printf("<td align='right'>%s</td>", number_format($row['costo_extra'], $n_decimales));//costo extra
		printf("<td align='right'>%s</td>", number_format($row['costo_ing_inv'], $n_decimales));//costo ingreso a inventario
		printf("<td align='right'>%s</td>", number_format($row['iva'], $n_decimales));//iva
		printf("<td align='right'>%s</td></tr>", number_format($row['precio_total'], $n_decimales));//total
		
		$suma_costo_extra = $suma_costo_extra + $row['costo_extra'];
		$suma_precio = $suma_precio + $row['precio_total'];
		$suma_iva = $suma_iva + $row['iva'];
		$suma_costo = $suma_costo + $row['costo_ing_inv'];
		$color++;
	}//fin while
	mysqli_free_result($result2);
	
	//convierte el total en literal
	$letras=new NroEnLetra($suma_precio,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="8"><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_costo_extra, $n_decimales); ?></b></td>
    <td align="right"><b><?php echo number_format($suma_costo, $n_decimales); ?></b></td>
    <td align="right"><b><?php echo number_format($suma_iva, $n_decimales); ?></b></td>
    <td align="right"><b><?php echo number_format($suma_precio, $n_decimales); ?></b></td>
    </tr>
</tbody>    
</table>
<br />

<?php if($hay_gasto != 0) { //if hay_gasto ?>
<p><b>&nbsp;<?php echo $texto_gasto_det; ?></b></p>
<table border="1" align="left" cellpadding="2" width="100%">
 	<thead>
    <TR> 
    <TD align="center"><b>Cod. Planilla</b></TD>
    <TD align="center"><b>Planilla de Gastos</b></TD>
	<?php if($muestra_gasto_det == 0){//if muestra gasto ?>
		<TD align="center"><b>Gasto</b></TD>
		<TD align="center"><b>Tipo</b></TD>
		<TD align="center"><b>Costo Parcial</b></TD>
		<TD align="center"><b>Costo Parcial Ing.</b></TD>
	<?php }//fin if muestra gasto ?>
	<TD align="center"><b>Costo Total</b></TD>
    <TD align="center"><b>Costo Ingreso</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">
    <?php
    $color = 2;
	$suma_gasto = 0;
	$suma_gasto_ingreso = 0;
	$suma_costo_parcial = 0;
	$suma_costo_parcial_ing = 0;

	for ($i = 1; $i <= $fin_i; $i++) { //for
		//$cod_pla = $color;
		$cod_pla = $mat_p[$i][1];
		$planilla = $mat_p[$i][2];
		$cod_moneda_ga = $mat_p[$i][5];
		$cod_tc_ga = $mat_p[$i][6];
		$gasto = $mat_p[$i][4];
		$gasto_ingreso = $mat_p[$i][8];

		//query para concepto de gasto
		$resultdet = mysqli_query($link, "select e1.fldName AS concepto_gasto, d1.fldName AS tipo, b1.fldAmount AS total, 
			b1.fldAmount * (SELECT case when $cod_moneda_ga = 1 then 1 ELSE (select c1.fldRate FROM actExchangeRateDetail c1 WHERE c1.codExchangeRate = $cod_tc_ga AND c1.codBaseCurrency = 1 AND c1.codTargetCurrency = $cod_moneda_ga) END) AS total_om,
			(case when b1.codInvoice is not NULL then (case when b1.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) as total1 from txsPurchasesLedger a1 where a1.pkyPurchasesLedger = b1.codInvoice and a1.sysEnabled = 1 and a1.sysAuthorized = 1) else 0 end) 
			else (case when b1.codDocumentType = 1 then (b1.fldAmount - (b1.fldAmount * $monto_iva / 100)) else 0 END) * (SELECT case when $cod_moneda_ga = 1 then 1 ELSE (select c1.fldRate FROM actExchangeRateDetail c1 WHERE c1.codExchangeRate = $cod_tc_ga AND c1.codBaseCurrency = 1 AND c1.codTargetCurrency = $cod_moneda_ga) END) end) as suma_costo_exc, 
			(case when codDocumentType = 2 then (fldAmount - (fldAmount * $monto_iva / 100)) else 0 END) * (SELECT case when $cod_moneda_ga = 1 then 1 ELSE (select c1.fldRate FROM actExchangeRateDetail c1 WHERE c1.codExchangeRate = $cod_tc_ga AND c1.codBaseCurrency = 1 AND c1.codTargetCurrency = $cod_moneda_ga) END) as suma_costo_ext2, 
			(case when codDocumentType = 3 or codDocumentType = 4 or codDocumentType = 5 or codDocumentType = 6 then fldAmount else 0 end) * (SELECT case when $cod_moneda_ga = 1 then 1 ELSE (select c1.fldRate FROM actExchangeRateDetail c1 WHERE c1.codExchangeRate = $cod_tc_ga AND c1.codBaseCurrency = 1 AND c1.codTargetCurrency = $cod_moneda_ga) END) as suma_costo_ext3
						from pchPayrollExpensesDetail b1, txsDocumentType d1, pchPurchaseConcept e1 
						where 
						b1.codPayrollExpenses = '$cod_pla' and
						b1.codDocumentType = d1.pkyDocumentType and
						b1.codPurchaseConcept = e1.pkyPurchaseConcept
						ORDER BY concepto_gasto, tipo;");

		//muestra el detalle
		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
		if($muestra_gasto_det == 0){//if muestra gasto
			printf("<td colspan='4'>%s</td>", '');//4 columnas
		}
		printf("<td align='right'>%s</td>", number_format($gasto, $n_decimales));//gasto
		printf("<td align='right'>%s</td></tr>", number_format($gasto_ingreso, $n_decimales));//gasto

		if($muestra_gasto_det == 0){//if muestra gasto
			//muestra gastos
			while ($row = mysqli_fetch_array($resultdet)) { //while gastos
				if($color % 2 == 0) { //if color
					printf("<tr BGCOLOR='#e2e7fa'><td colspan='2'>%s</td>", '');//2 columnas
				} else {
					printf("<tr><td colspan='2'>%s</td>", '');//2 columnas
				}//cierra if color
				printf("<td>%s</td>", $row['concepto_gasto']); //concepto de gasto
				printf("<td>%s</td>", $row['tipo']); //tipo
				printf("<td align='right'>%s</td>", number_format($row['total_om'],$n_decimales));//costo parcial
				$suma_costo_det = $row['suma_costo_exc'] + $row['suma_costo_ext2'] + $row['suma_costo_ext3'];
				printf("<td align='right'>%s</td>", number_format($suma_costo_det,$n_decimales));//costo parcial ing
				printf("<td>%s</td>", ''); //costo total
				printf("<td>%s</td></tr>", ''); //costo total ingreso
				//printf("<td colspan='2'>%s</td></tr>", ''); //2 columnas

				$suma_costo_parcial = $suma_costo_parcial + $row['total_om'];
				$suma_costo_parcial_ing = $suma_costo_parcial_ing + $suma_costo_det;
			} //fin while gastos
			mysqli_free_result($resultdet);	
		}//fin if muestra gasto

		$suma_gasto = $suma_gasto + $gasto;
		$suma_gasto_ingreso = $suma_gasto_ingreso + $gasto_ingreso;
		$color++;
	}//fin for
	
	
	//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;
	}
	
	?>
	<?php if($muestra_gasto_det == 0){//if muestra gasto ?>
		<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_costo_parcial, $n_decimales); ?></b></td>
		<td align="right"><b><?php echo number_format($suma_costo_parcial_ing, $n_decimales); ?></b></td>	
		<td align="right"><b><?php echo number_format($suma_gasto, $n_decimales); ?></b></td>
		<td align="right"><b><?php echo number_format($suma_gasto_ingreso, $n_decimales); ?></b></td>	
		</tr>
	<?php } else {//else if muestra gasto ?>	
		<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>
		<td align="right"><b><?php echo number_format($suma_gasto_ingreso, $n_decimales); ?></b></td>	
		</tr>
	<?php }//fin if muestra gasto ?>	
</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="../../ModulePurchases/VoucherReceiptOfGoods/index.php?pkyReceiptOfGoods='.$cod_orden.'&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="../../ModulePurchases/VoucherReceiptOfGoods/index.php?pkyReceiptOfGoods='.$cod_orden.'&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 /><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>

<?php


?>
</body>
</html>
