<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Ordenes de Compra</title>
<link href="../../maincss.css" rel="stylesheet" type="text/css" />
<link href="../../printcss.css" rel="stylesheet" type="text/css" media="print" />

</head>
<?php

include "../../conn.php";
include "../../general.php";
include "../../token.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$estado=$_POST["estado"];
$orden_compra=$_POST["orden_compra"];
$proveedor=$_POST["proveedor"];
$almacen=$_POST["almacen"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
//$usuario = $_POST["usuario"];
$otra_moneda=$_POST["otra_moneda"];
$ordenar=$_POST["ordenar"];
$cod_contexto=$_POST["contexto"];

$cod_orden_det=$_GET["cod_orden_det"];
$muestra_gasto_det=$_GET["muestra_gasto_det"];
$cod_contexto_det=$_GET["cod_contexto_det"];
//$usuario_det=$_GET["usuario_det"];

?>
<body>

	<?php
	
//Inicio reporte orden de compra


if($rep == 14 && $tipo == 1) { // if 2

//nombre otra moneda
$resultotra = mysqli_query($link, "SELECT fldName as otra_moneda
from
actCurrency
where
pkyCurrency = '$otra_moneda' and
sysEnabled = 1 and sysAuthorized = 1;");
		
	while ($row = mysqli_fetch_array($resultotra)) {
	$nom_otra_moneda = $row['otra_moneda'];
	}
	mysqli_free_result($resultotra);


if ($ordenar == 0) { //if ordenar = 0
	$valor_ordenar = 'order by fin.fecha, fin.proveedor';
	$nom_orden = 'Fecha';
} //fin if ordenar = 0
if ($ordenar == 1) { //if ordenar = 1
  	$valor_ordenar = 'order by fin.nro_doc, fin.proveedor';
	$nom_orden = 'Nro. Documento';
} //fin if ordenar = 1
if ($ordenar == 2) { //if ordenar = 2
	$valor_ordenar = 'order by fin.proveedor, fin.fecha';
	$nom_orden = 'Proveedor';
} //fin if ordenar = 2
if ($ordenar == 3) { //if ordenar = 3
	$valor_ordenar = 'order by fin.cod_orden';
	$nom_orden = 'C&oacute;digo';
} //fin if ordenar = 3

if ($estado == 0) { //if estado
	$nom_estado = 'Todos';
	$valor_estado = ' ';
  } else { 
  	$valor_estado = ' a.codStatus = '.$estado.' and ';
	//nombre de estado
$resultesta = mysqli_query($link, "select pkyPurchaseOrderStatus as codigo, fldName as estado 
from 
pchPurchaseOrderStatus 
where 
pkyPurchaseOrderStatus = '$estado';");
	while ($row = mysqli_fetch_array($resultesta)) {
	$nom_estado = $row['estado'];
	}
	mysqli_free_result($resultesta);
  } //fin if estado

  if ($orden_compra == 0) { //if orden_compra
    $valor_orden_compra = ' ';
	$nom_orden_compra = 'Todos';
  } else { 
    $valor_orden_compra = ' a.pkyPurchaseOrder = '.$orden_compra.' and ';
	//nombre orden_compra
	$resultoc = mysqli_query($link, "select a.pkyPurchaseOrder as cod_oc, a.fldDate as fecha, a.fldNumber as nro_doc, b.fldName as proveedor
from pchPurchaseOrder a, agnCompany b 
where 
a.codSupplier = b.pkyCompany and 
a.pkyPurchaseOrder = '$orden_compra';");
	while ($row = mysqli_fetch_array($resultoc)) {
	$nom_orden_compra = $row['cod_oc'].' - '.$row['fecha'].' - '.$row['nro_doc'].' - '.$row['proveedor'];
	}
	mysqli_free_result($resultoc);
  } //fin if orden_compra

if ($proveedor == 0) { //if proveedor
    $valor_proveedor = ' ';
	$nom_proveedor = 'Todos';
  } else { 
    $valor_proveedor = ' a.codSupplier = '.$proveedor.' and ';
	//nombre de proveedor
	$resultpro = mysqli_query($link, "select pkyCompany as codigo, fldName as proveedor
from agnCompany
where
fldIsSupplier = 1 and 
pkyCompany = '$proveedor';");
	while ($row = mysqli_fetch_array($resultpro)) {
	$nom_proveedor = $row['codigo'].' - '.$row['proveedor'];
	}
	mysqli_free_result($resultpro);
  } //fin if proveedor

if ($almacen == 0) { //if almacen
    $valor_almacen = ' ';
	$nom_almacen = 'Todos';
  } else { 
    $valor_almacen = ' a.codWarehouse = '.$almacen.' and ';
	//nombre de almacen
	$resultalm = mysqli_query($link, "select pkyWarehouse as codigo, fldName as almacen
from invWarehouse
where
pkyWarehouse = '$almacen';");
	while ($row = mysqli_fetch_array($resultalm)) {
	$nom_almacen = $row['almacen'];
	}
	mysqli_free_result($resultalm);
  } //fin if almacen

//contexto
if($cod_contexto == -1){ 
	$nom_contexto = 'Todos';
	$valor_contexto = ' ';
} else {
	$resultcontx = mysqli_query($link, "select pkyContext as cod_contexto, fldName as contexto, codCostCenter as cod_cc 
	from srvContext
	where 
	pkyContext = '$cod_contexto';");
		while ($row = mysqli_fetch_array($resultcontx)) {   
			$nom_contexto = $row['cod_contexto'].' - '.$row['contexto'];
		}
		mysqli_free_result($resultcontx);
	$valor_contexto = ' a.sysContext = '.$cod_contexto.' and ';	
}
	
//listado de ordenes de compra del periodo

$result = mysqli_query($link, "select fin.cod_orden as cod_orden, fin.fecha as fecha, fin.nro_doc as nro_doc, fin.estado as estado, fin.cod_prove as cod_prove, fin.proveedor as proveedor, 
fin.cod_dir as cod_dir, fin.cod_tel as cod_tel, fin.cod_movil as cod_movil, fin.almacen as almacen, fin.cod_moneda as cod_moneda, fin.moneda as moneda, fin.cod_tc as cod_tc, fin.gen_cxp as gen_cxp, fin.total_bruto as total_bruto, fin.total_desc_porciento as total_desc_porciento, fin.total_desc_porciento_global as total_desc_porciento_global, fin.total_neto as total_neto, fin.cod_cxp as cod_cxp, fin.cod_asiento as cod_asiento, fin.dcto as dcto, fin.glosa as glosa, fin.tasa as tasa, fin.total_neto_om as total_neto_om, fin.usuario_reg as usuario_reg
from (
select t.cod_orden as cod_orden, t.fecha as fecha, t.nro_doc as nro_doc, t.estado as estado, 
t.cod_prove as cod_prove, t.proveedor as proveedor, t.cod_dir as cod_dir, t.cod_tel as cod_tel, t.cod_movil as cod_movil, t.almacen as almacen, t.cod_moneda as cod_moneda, t.moneda as moneda, t.cod_tc as cod_tc, t.gen_cxp as gen_cxp, t.total_bruto as total_bruto, t.total_desc_porciento as total_desc_porciento, t.total_desc_porciento_global as total_desc_porciento_global, t.total_neto as total_neto, t.cod_cxp as cod_cxp, t.cod_asiento as cod_asiento, t.dcto as dcto, t.glosa as glosa, tc.fldRate as tasa, t.total_neto * tc.fldRate as total_neto_om, t.usuario_reg as usuario_reg
from (
select a.pkyPurchaseOrder as cod_orden, a.fldDate as fecha, a.fldNumber as nro_doc, b.fldName as estado, a.codSupplier as cod_prove, c.fldName as proveedor, a.codAddress as cod_dir, a.codFixedContactPhone as cod_tel, a.codMovilContactPhone as cod_movil, d.fldName as almacen, a.codCurrency as cod_moneda, e.fldName as moneda, a.codExchangeRate as cod_tc, a.fldGenAccountPayable as gen_cxp, a.fldGrossTotal as total_bruto, a.fldTotalDiscountPercent as total_desc_porciento, a.fldGlobalDiscountPercent as total_desc_porciento_global, 
a.fldNetTotal as total_neto, a.codAccountPayable as cod_cxp, a.codAccountEntry as cod_asiento, f.fldName as dcto, a.txtDescription as glosa, g.fldName as usuario_reg
from
pchPurchaseOrder a, pchPurchaseOrderStatus b, agnCompany c, invWarehouse d, actCurrency e, txsDocumentType f, accGroup g 
where 
$valor_estado 
$valor_orden_compra 
$valor_almacen 
$valor_proveedor 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and 
a.codStatus = b.pkyPurchaseOrderStatus and 
a.codSupplier = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codCurrency = e.pkyCurrency and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.codDocumentType = f.pkyDocumentType and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t, actExchangeRateDetail tc
where
tc.codTargetCurrency = t.cod_moneda and
tc.codBaseCurrency = '$otra_moneda' and
tc.codExchangeRate = t.cod_tc 

union all 

select t.cod_orden as cod_orden, t.fecha as fecha, t.nro_doc as nro_doc, t.estado as estado, 
t.cod_prove as cod_prove, t.proveedor as proveedor, t.cod_dir as cod_dir, t.cod_tel as cod_tel, t.cod_movil as cod_movil, t.almacen as almacen, t.cod_moneda as cod_moneda, t.moneda as moneda, t.cod_tc as cod_tc, t.gen_cxp as gen_cxp, t.total_bruto as total_bruto, t.total_desc_porciento as total_desc_porciento, t.total_desc_porciento_global as total_desc_porciento_global, t.total_neto as total_neto, t.cod_cxp as cod_cxp, t.cod_asiento as cod_asiento, t.dcto as dcto, t.glosa as glosa, 1 as tasa, t.total_neto * 1 as total_neto_om, t.usuario_reg as usuario_reg
from (
select a.pkyPurchaseOrder as cod_orden, a.fldDate as fecha, a.fldNumber as nro_doc, b.fldName as estado, a.codSupplier as cod_prove, c.fldName as proveedor, a.codAddress as cod_dir, a.codFixedContactPhone as cod_tel, a.codMovilContactPhone as cod_movil, d.fldName as almacen, a.codCurrency as cod_moneda, e.fldName as moneda, a.codExchangeRate as cod_tc, a.fldGenAccountPayable as gen_cxp, a.fldGrossTotal as total_bruto, a.fldTotalDiscountPercent as total_desc_porciento, a.fldGlobalDiscountPercent as total_desc_porciento_global, 
a.fldNetTotal as total_neto, a.codAccountPayable as cod_cxp, a.codAccountEntry as cod_asiento, f.fldName as dcto, a.txtDescription as glosa, g.fldName as usuario_reg
from
pchPurchaseOrder a, pchPurchaseOrderStatus b, agnCompany c, invWarehouse d, actCurrency e, txsDocumentType f, accGroup g 
where 
$valor_estado 
$valor_orden_compra 
$valor_almacen 
$valor_proveedor 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and 
a.codStatus = b.pkyPurchaseOrderStatus and 
a.codSupplier = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codCurrency = e.pkyCurrency and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.codDocumentType = f.pkyDocumentType and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = '$otra_moneda') fin  
$valor_ordenar;");


?>
<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>Ordenes de Compra</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>Proveedor: </b><?php echo $nom_proveedor; ?></TD>
		<TD width="25%"><b>Orden de Compra: </b><?php echo $nom_orden_compra; ?></TD>
        <TD width="25%"><b>Estado: </b><?php echo $nom_estado; ?></TD>
		<TD width="25%"><b>Almac&eacute;n: </b><?php echo $nom_almacen; ?></TD>
        </TR>
        <TR>
        <TD><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
		<TD><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
        <TD><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        <TD><b>Expresado en: </b><?php echo $nom_otra_moneda; ?></TD>
        </TR>
    </table>

</td>
<!--
<td valign="top" width="150">

    <table border="1" align="center" cellpadding="2" width="100%">
    <tr>
    <TD><b>Tipo de Cambio:</b></TD>
    </tr><tr>
    <TD align="center"><a href="../../ModuleAccounting/VoucherExchangeRate/index.php?pkyExchangeRate=<?php echo $ultimo_tc; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Tipo de Cambio"><?php echo $ultimo_tc; ?></a> (<?php echo $tc_fecha; ?> - <?php echo $tc_hora; ?>)<br/ >
        <table border="0" cellpadding="2" align="center">
            <?php
            for($m = 1; $m <= $fin_m; $m++) {
            printf("<tr><td align='left'>1 %s</td>", $ma_tc [$m][1]);//moneda base
            printf("<td align='center'>%s</td>", ' = ');//letra a
            printf("<td align='left'>%s %s</td>", number_format($ma_tc [$m][3],$n_tipocambio), $ma_tc [$m][2]);//moneda target y tasa
            //printf("<td align='right'>%s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio));//tasa
            }
            ?>
        </table>
    </TD>
    </tr>
	</table>
    
</td>
-->
</tr>
</table>
<br />

<table border="1" align="center" cellpadding="2" width="100%">
 	<thead>
    <TR> 
 	<TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Nro. Doc.</b></TD>
    <TD align="center"><b>Fecha</b></TD>
    <TD align="center"><b>Proveedor</b></TD>
    <TD align="center"><b>Almac&eacute;n</b></TD>
    <TD align="center"><b>Estado</b></TD>
    <TD align="center"><b>Glosa</b></TD>
    <TD align="center"><b>Mon.</b></TD>
    <TD align="center"><b>Total Bruto</b></TD>
    <TD align="center"><b>Desc. Parcial (%)</b></TD>
    <TD align="center"><b>Desc. Global (%)</b></TD>
    <TD align="center"><b>Total Neto</b></TD>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Total Neto en <?php echo $nom_otra_moneda; ?></b></TD>
    </TR>
	</thead>
    <tbody class="marcafila">

<?php

$color = 2;
$suma_total = 0;

while ($row = mysqli_fetch_array($result)) {
	$cod_orden = $row['cod_orden'];
	$cod_moneda = $row['cod_moneda'];
	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePurchases/VoucherPurchaseOrder/index.php?pkyPurchaseOrder=$cod_orden&user=$usuario&token=$token' title='Orden de Compra'>%s</a></td>", $row['cod_orden']);//cod
	} else {
		printf("<tr><td align='center'><a href='../../ModulePurchases/VoucherPurchaseOrder/index.php?pkyPurchaseOrder=$cod_orden&user=$usuario&token=$token' title='Orden de Compra'>%s</a></td>", $row['cod_orden']);//cod
	}//cierra if color
	printf("<td align='center'>%s</td>", $row['nro_doc']);//nro documento
	printf("<td align='center'>%s</td>", $row['fecha']);//fecha
	printf("<td>%s</td>", $row['proveedor']);//proveedor
	printf("<td>%s</td>", $row['almacen']);//almacen
	printf("<td>%s</td>", $row['estado']);//estado
	$glosa = $row['glosa'];
	$glosa = nl2br($glosa);//quita salto de linea 
	$glosa = str_replace('\n','<br />',$glosa);//reemplaza para salto de linea
	printf("<td>%s</td>", $glosa);//glosa
	printf("<td>%s</td>", $row['moneda']);//moneda
	printf("<td align='right'>%s</td>", number_format($row['total_bruto'],$n_decimales));//total bruto
	printf("<td align='right'>%s</td>", number_format($row['total_desc_porciento'],$n_descuento));//descuento porciento
	printf("<td align='right'>%s</td>", number_format($row['total_desc_porciento_global'],$n_descuento));//descuento porciento global
	printf("<td align='right'>%s</td>", number_format($row['total_neto'],$n_decimales));//total neto
	printf("<td align='right'>%s</td>", number_format($row['tasa'],$n_decimales));//tasa
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&cod_orden_det=$cod_orden&muestra_gasto_det=0&cod_contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle de Compra'>%s</a></td></tr>", number_format($row['total_neto_om'],$n_decimales));//total neto otra moneda
	$suma_total = $suma_total + $row['total_neto_om'];
	$color++;
}//fin while
	
?>
 
<tr BGCOLOR='#ffcccc'><td colspan="13"><b>Total</b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_total,$n_decimales)); ?></td>
</tr>
</table>
         
<?php
	
} //fin if 2
?>




<?php

//Inicio detalle orden de compra


if($rep == 14 && $tipo == 2) { // if 3

	//nombre orden de compra
	$resultoc = mysqli_query($link, "select a.pkyPurchaseOrder as cod_oc, a.fldDate as fecha, a.fldNumber as nro_doc, b.fldName as proveedor
	from pchPurchaseOrder a, agnCompany b 
	where 
	a.codSupplier = b.pkyCompany and 
	a.pkyPurchaseOrder = '$cod_orden_det';");
	while ($row = mysqli_fetch_array($resultoc)) {
		$nom_orden_compra_det = $row['cod_oc'].' - '.$row['nro_doc'].' - '.$row['proveedor'];
	}
	mysqli_free_result($resultoc);

	//contexto
	if($cod_contexto_det == -1){ 
		$nom_contexto_det = 'Todos';
		$valor_contexto_det = ' ';
	} else {
		$resultcontx = mysqli_query($link, "select pkyContext as cod_contexto, fldName as contexto, codCostCenter as cod_cc 
		from srvContext
		where 
		pkyContext = '$cod_contexto_det';");
			while ($row = mysqli_fetch_array($resultcontx)) {   
				$nom_contexto_det = $row['cod_contexto'].' - '.$row['contexto'];
			}
			mysqli_free_result($resultcontx);
		$valor_contexto_det = ' a.sysContext = '.$cod_contexto_det.' and ';	
	}
	
	if($muestra_gasto_det == 1){
		$texto_orden_det = '<a href="report.php?rep=14&tipo=2&cod_orden_det='.$cod_orden_det.'&muestra_gasto_det=0&cod_contexto_det='.$cod_contexto_det.'&usuario_det='.$usuario_det.'&token='.$token.'" title="Detalle de Compra, Totales Gasto" style="text-decoration:none; color:#000099; font-size:18px; font-style:normal;">Detalle de Compra, Totales Gasto</a>';
	} else {
		$texto_orden_det = '<a href="report.php?rep=14&tipo=2&cod_orden_det='.$cod_orden_det.'&muestra_gasto_det=1&cod_contexto_det='.$cod_contexto_det.'&usuario_det='.$usuario_det.'&token='.$token.'" title="Detalle de Compra" style="text-decoration:none; color:#000099; font-size:18px; font-style:normal;">Detalle de Compra</a>';
	}

	//para la orden de compra 
	$resultoc = mysqli_query($link, "select a.pkyPurchaseOrder as cod_orden, a.fldDate as fecha, a.fldNumber as nro_doc, b.fldName as estado, 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, 
	(SELECT case when a.codCurrency = 1 then 1 ELSE (select b1.fldRate FROM actExchangeRateDetail b1 WHERE b1.codExchangeRate = a.codExchangeRate AND b1.codBaseCurrency = 1 AND b1.codTargetCurrency = a.codCurrency) END) AS tasa,
	a.fldGrossTotal * (SELECT case when a.codCurrency = 1 then 1 ELSE (select b1.fldRate FROM actExchangeRateDetail b1 WHERE b1.codExchangeRate = a.codExchangeRate AND b1.codBaseCurrency = 1 AND b1.codTargetCurrency = a.codCurrency) END) as total_bruto_om, 
	a.fldNetTotal * (SELECT case when a.codCurrency = 1 then 1 ELSE (select b1.fldRate FROM actExchangeRateDetail b1 WHERE b1.codExchangeRate = a.codExchangeRate AND b1.codBaseCurrency = 1 AND b1.codTargetCurrency = a.codCurrency) END) as total_neto_om, 
	case when a.fldGenAccountPayable = 1 and (a.codDocumentType = 1 or a.codDocumentType = 2) then $monto_iva / 100 ELSE 0 END as porcentaje_iva, 
	a.fldGenAccountPayable as gen_cxp, f.fldName as dcto, a.txtDescription as glosa, g.fldName as usuario_reg
		from
		pchPurchaseOrder a, pchPurchaseOrderStatus b, agnCompany c, invWarehouse d, actCurrency e, txsDocumentType f, accGroup g 
		where 
		$valor_contexto_det 
		a.pkyPurchaseOrder = '$cod_orden_det' and 
		a.codStatus = b.pkyPurchaseOrderStatus and 
		a.codSupplier = c.pkyCompany and 
		a.codWarehouse = d.pkyWarehouse and 
		a.codCurrency = e.pkyCurrency and 
		a.sysAuthorizationGroup = g.pkyGroup and 
		a.codDocumentType = f.pkyDocumentType and 
		a.sysEnabled = 1 and a.sysAuthorized = 1;");
	
	$mat_oc = array();
	$i = 1;
	while ($row = mysqli_fetch_array($resultoc)) {
		$mat_oc[$i][1] = $row['cod_orden'];
		$mat_oc[$i][2] = $row['nro_doc'];
		$mat_oc[$i][3] = $row['fecha'];
		$mat_oc[$i][4] = $row['estado'];
		$mat_oc[$i][5] = $row['proveedor'];
		$mat_oc[$i][6] = $row['almacen'];
		$glosa = $row['glosa'];
		$glosa = nl2br($glosa);//quita salto de linea 
		$glosa = str_replace('\n','<br />',$glosa);//reemplaza para salto de linea
		$mat_oc[$i][7] = $glosa;
		$mat_oc[$i][8] = $row['moneda'];
		$mat_oc[$i][9] = $row['total_bruto'];
		$mat_oc[$i][10] = $row['total_neto'];
		$mat_oc[$i][11] = $row['tasa'];
		$mat_oc[$i][12] = $row['total_bruto_om'];
		$mat_oc[$i][13] = $row['total_neto_om'];
		if($row['gen_cxp'] == 1) {
			$mat_oc[$i][14] = $row['dcto'];
		} else {
			$mat_oc[$i][14] = '';
		}
		$mat_oc[$i][15] = $row['total_neto_om'] - ($row['total_neto_om'] * $row['porcentaje_iva']); //calculo del costo menos impuesto iva
		$i++;
	}
	mysqli_free_result($resultoc);	
	$fin_oc = $i - 1;

	//para las planillas de gastos 
	$resultpg = mysqli_query($link, "select t.cod_pla as cod_pla, t.fecha as fecha, t.fecha_pago as fecha_pago, t.nro_doc as nro_doc, t.total as total, t.cod_orden as cod_orden, t.cod_prov as cod_prov, t.proveedor as proveedor, t.cod_moneda as cod_moneda, t.moneda as moneda, t.cod_tc as cod_tc, t.cod_cxp as cod_cxp, t.cod_asiento as cod_asiento, t.cod_mov_caja as cod_mov_caja, t.glosa as glosa, t.tasa as tasa, t.total * t.tasa as total_om, t.usuario_reg as usuario_reg, 
	t.suma_detalle as suma_detalle, t.suma_detalle - t.total as retenciones, (t.suma_costo_ext2 + t.suma_costo_ext3) as suma_costo_ext, 
	t.suma_detalle_om as suma_detalle_om, (t.suma_costo_ext2_om + t.suma_costo_ext3_om) as suma_costo_ext_om, t.usada as usada
	FROM
	(select a.pkyPayrollExpenses as cod_pla, a.fldDate as fecha, a.fldPayDate as fecha_pago, a.fldNumber as nro_doc, a.fldTotal as total, a.codPurchaseOrder as cod_orden, a.codSupplier as cod_prov, b.fldName as proveedor, a.codCurrency as cod_moneda, c.fldName as moneda, a.codExchangeRate as cod_tc, a.codAccountPayable as cod_cxp, a.codAccountEntry as cod_asiento, a.codPettyCashMovement as cod_mov_caja, a.txtDescription as glosa, 
	(SELECT case when a.codCurrency = 1 then 1 ELSE (select b1.fldRate FROM actExchangeRateDetail b1 WHERE b1.codExchangeRate = a.codExchangeRate AND b1.codBaseCurrency = 1 AND b1.codTargetCurrency = a.codCurrency) END) AS tasa, 
	g.fldName as usuario_reg, 
	
	(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,
	
	(select sum(fldAmount) from pchPayrollExpensesDetail where codPayrollExpenses = a.pkyPayrollExpenses group by codPayrollExpenses) * (SELECT case when a.codCurrency = 1 then 1 ELSE (select b1.fldRate FROM actExchangeRateDetail b1 WHERE b1.codExchangeRate = a.codExchangeRate AND b1.codBaseCurrency = 1 AND b1.codTargetCurrency = a.codCurrency) END) as suma_detalle_om, 
	
	(select sum(case when codDocumentType = 2 then (fldAmount - (fldAmount * $monto_iva / 100)) else 0 end) from pchPayrollExpensesDetail where codPayrollExpenses = a.pkyPayrollExpenses group by codPayrollExpenses) * (SELECT case when a.codCurrency = 1 then 1 ELSE (select b1.fldRate FROM actExchangeRateDetail b1 WHERE b1.codExchangeRate = a.codExchangeRate AND b1.codBaseCurrency = 1 AND b1.codTargetCurrency = a.codCurrency) END) as suma_costo_ext2_om,
	
	(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) * (SELECT case when a.codCurrency = 1 then 1 ELSE (select b1.fldRate FROM actExchangeRateDetail b1 WHERE b1.codExchangeRate = a.codExchangeRate AND b1.codBaseCurrency = 1 AND b1.codTargetCurrency = a.codCurrency) END) as suma_costo_ext3_om, 
	a.fldUsed as usada
	from 
	pchPayrollExpenses a, agnCompany b, actCurrency c, accGroup g  
	where 
	$valor_contexto_det
	a.codPurchaseOrder = '$cod_orden_det' and
	a.codSupplier = b.pkyCompany and 
	a.sysAuthorizationGroup = g.pkyGroup and 
	a.codCurrency = c.pkyCurrency and 
	a.sysEnabled = 1 and a.sysAuthorized = 1) t
	order by t.fecha, t.proveedor;");
	
	$mat_pg = array();
	$i = 1;
	while ($row = mysqli_fetch_array($resultpg)) {
		$mat_pg[$i][1] = $row['cod_pla'];
		$mat_pg[$i][2] = $row['nro_doc'];
		$mat_pg[$i][3] = $row['fecha'];
		//usada
		if($row['usada'] == 1) { 
			$mat_pg[$i][4] = "Usada";
		} else {
			$mat_pg[$i][4] = "No Usada";
		}
		$mat_pg[$i][5] = $row['proveedor'];
		$mat_pg[$i][6] = ''; //almacen
		$glosa = $row['glosa'];
		$glosa = nl2br($glosa);//quita salto de linea 
		$glosa = str_replace('\n','<br />',$glosa);//reemplaza para salto de linea
		$mat_pg[$i][7] = $glosa;
		$mat_pg[$i][8] = $row['moneda'];
		$mat_pg[$i][9] = $row['total'];//total bruto
		$mat_pg[$i][10] = 0; //total neto
		$mat_pg[$i][11] = $row['tasa'];
		$mat_pg[$i][12] = $row['total_om']; //total bruto om
		$mat_pg[$i][13] = 0; //total neto om
		$mat_pg[$i][14] = $row['suma_detalle_om'];
		$mat_pg[$i][15] = $row['suma_costo_ext_om'];
		$mat_pg[$i][16] = $row['cod_moneda'];
		$mat_pg[$i][17] = $row['cod_tc'];
		$mat_pg[$i][18] = $row['cod_pla'];
		$mat_pg[$i][19] = $row['cod_cxp'];
		$cod_moneda_exc = $mat_pg[$i][16];
		$cod_tc_exc = $mat_pg[$i][17];
		$cod_planilla = $mat_pg[$i][18];

		$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) * (SELECT case when $cod_moneda_exc = 1 then 1 ELSE (select c1.fldRate FROM actExchangeRateDetail c1 WHERE c1.codExchangeRate = $cod_tc_exc AND c1.codBaseCurrency = 1 AND c1.codTargetCurrency = $cod_moneda_exc) END) 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'];
			}
		mysqli_free_result($resultexc);	

		$mat_pg[$i][13] = $mat_pg[$i][15] + $suma_costo_exc;

		$i++;
	}
	mysqli_free_result($resultpg);	
	$fin_pg = $i - 1;

	//para la recepción de mercadería, calculo la suma de costo, precio, iva y neto 
	$resultrm = mysqli_query($link, "select a.pkyReceiptOfGoods AS cod_rec, a.fldDate AS fecha, a.fldNumber AS nro_doc, b.fldName AS tipo, c.fldName AS proveedor, d.fldName AS almacen, a.fldGrossTotal AS total_bruto, a.fldNetTotal AS total_neto, a.fldRetentions AS retenciones, a.codExchangeRate AS tc, a.txtDescription AS glosa, 
	(SELECT SUM(fldApportionCost) FROM pchReceiptOfGoodsDetail WHERE codReceiptOfGoods = a.pkyReceiptOfGoods) AS costo, 
	(SELECT SUM(fldTotalPrice) FROM pchReceiptOfGoodsDetail WHERE codReceiptOfGoods = a.pkyReceiptOfGoods) AS precio, 
	(SELECT SUM(fldVAT) FROM pchReceiptOfGoodsDetail WHERE codReceiptOfGoods = a.pkyReceiptOfGoods) AS iva, 
	(SELECT SUM(fldTotalNetPrice) FROM pchReceiptOfGoodsDetail WHERE codReceiptOfGoods = a.pkyReceiptOfGoods) AS neto
	FROM pchReceiptOfGoods a, txsDocumentType b, agnCompany c, invWarehouse d
	WHERE 
	a.codPurchaseOrder = '$cod_orden_det' AND 
	a.codDocumentType = b.pkyDocumentType AND 
	a.codSupplier = c.pkyCompany AND 
	a.codWarehouse = d.pkyWarehouse AND  
	a.sysEnabled = 1 and a.sysAuthorized = 1;");

	$mat_rm = array();
	$i = 1;
	while ($row = mysqli_fetch_array($resultrm)) {
		$mat_rm[$i][1] = $row['cod_rec'];
		$mat_rm[$i][2] = $row['nro_doc'];
		$mat_rm[$i][3] = $row['fecha'];
		$mat_rm[$i][4] = $row['tipo'];
		$mat_rm[$i][5] = $row['proveedor'];
		$mat_rm[$i][6] = $row['almacen'];
		$glosa = $row['glosa'];
		$glosa = nl2br($glosa);//quita salto de linea 
		$glosa = str_replace('\n','<br />',$glosa);//reemplaza para salto de linea
		$mat_rm[$i][7] = $glosa;
		$mat_rm[$i][8] = ''; //moneda
		$mat_rm[$i][9] = $row['precio'];
		$mat_rm[$i][10] = $row['costo'];
		$mat_rm[$i][11] = $row['iva'];
		$mat_rm[$i][12] = $row['neto'];
		$i++;
	}
	mysqli_free_result($resultrm);	
	$fin_rm = $i - 1;

	?>
	<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_det; ?></b>
	</td></tr></table>
	<p><br /><center><b><?php echo $texto_orden_det; ?></b></center></p>
	<table border="0" cellpadding="2" align="center" width="100%">
	<tr>
	<td>	
		<table border="1" align="center" cellpadding="2" width="100%">
			<TR> 
			<TD width="33%"><b>Orden de Compra: </b><?php echo $nom_orden_compra_det; ?></TD>
			<TD width="33%"><b>Expresado en: </b><?php echo $nom_moneda_principal; ?></TD>
			<TD width="33%"><b>Contexto: </b><?php echo $nom_contexto_det; ?></TD>
			</TR>
		</table>

	</td>
	<!--
	<td valign="top" width="150">

		<table border="1" align="center" cellpadding="2" width="100%">
		<tr>
		<TD><b>Tipo de Cambio:</b></TD>
		</tr><tr>
		<TD align="center"><a href="../../ModuleAccounting/VoucherExchangeRate/index.php?pkyExchangeRate=<?php echo $ultimo_tc; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Tipo de Cambio"><?php echo $ultimo_tc; ?></a> (<?php echo $tc_fecha; ?> - <?php echo $tc_hora; ?>)<br/ >
			<table border="0" cellpadding="2" align="center">
				<?php
				for($m = 1; $m <= $fin_m; $m++) {
				printf("<tr><td align='left'>1 %s</td>", $ma_tc [$m][1]);//moneda base
				printf("<td align='center'>%s</td>", ' = ');//letra a
				printf("<td align='left'>%s %s</td>", number_format($ma_tc [$m][3],$n_tipocambio), $ma_tc [$m][2]);//moneda target y tasa
				//printf("<td align='right'>%s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio));//tasa
				}
				?>
			</table>
		</TD>
		</tr>
		</table>
		
	</td>
	-->
	</tr>
	</table>
	<br />
	<table border="1" align="center" cellpadding="2" width="100%">
		<thead>
		<TR> 
		<TD align="center"><b>Cod.</b></TD>
		<TD align="center"><b>Nro. Doc.</b></TD>
		<TD align="center" width="65"><b>Fecha</b></TD>
		<TD align="center"><b>Estado</b></TD>
		<TD align="center"><b>Proveedor</b></TD>
		<TD align="center"><b>Almac&eacute;n</b></TD>
		<TD align="center"><b>Glosa</b></TD>
		<TD align="center"><b>Gasto</b></TD>
		<TD align="center"><b>Tipo</b></TD>
		<TD align="center"><b>Mon.</b></TD>
		<TD align="center"><b>Total</b></TD>
		<TD align="center"><b>Tasa a <?php echo $nom_moneda_principal; ?></b></TD>
		<TD align="center"><b>Total en <?php echo $nom_moneda_principal; ?></b></TD>
		<TD align="center"><b>Costo en <?php echo $nom_moneda_principal; ?></b></TD>
		<TD align="center"><b>Costo Recep. en <?php echo $nom_moneda_principal; ?></b></TD>
		</TR>
		</thead>
		<tbody class="marcafila">

		<?php
		$suma_total_om = 0;
		$suma_costo_om = 0;
		$suma_costo_recep = 0;
		$suma_total_om_ga = 0;
		$suma_costo_om_ga = 0;
		$color = 2;
		
		//muestra orden de compra
		for ($i = 1; $i <= $fin_oc; $i++) { //for 1
			printf("<tr BGCOLOR='#C8E6C'><td align='center'><a href='../../ModulePurchases/VoucherPurchaseOrder/index.php?pkyPurchaseOrder=$cod_orden_det&user=$usuario&token=$token' title='Orden de Compra'>%s</a></td>", $mat_oc[$i][1]);//cod
			printf("<td align='center'>%s</td>", $mat_oc[$i][2]);//numero documento
			printf("<td align='center'>%s</td>", $mat_oc[$i][3]);//fecha
			printf("<td>%s</td>", $mat_oc[$i][4]);//estado
			printf("<td>%s</td>", $mat_oc[$i][5]);//proveedor
			printf("<td>%s</td>", $mat_oc[$i][6]);//almacen
			printf("<td>%s</td>", $mat_oc[$i][7]);//glosa
			printf("<td>%s</td>", '');//gasto
			printf("<td>%s</td>", $mat_oc[$i][14]);//tipo
			printf("<td align='center'>%s</td>", $mat_oc[$i][8]);//moneda
			printf("<td align='right'>%s</td>", number_format($mat_oc[$i][9],$n_decimales));//total
			printf("<td align='right'>%s</td>", number_format($mat_oc[$i][11],$n_decimales));//tasa
			printf("<td align='right'>%s</td>", number_format($mat_oc[$i][13],$n_decimales));//total om
			printf("<td align='right'>%s</td>", number_format($mat_oc[$i][15],$n_decimales));//costo om
			printf("<td>%s</td></tr>", '');//costo recepcion
			$suma_total_om = $suma_total_om + $mat_oc[$i][13];
			$suma_costo_om = $suma_costo_om + $mat_oc[$i][15];
		}//fin for 1

		//muestra planilla de gastos
		for ($i = 1; $i <= $fin_pg; $i++) { //for 2
			$cod_pg = $mat_pg[$i][1];
			if($muestra_gasto_det == 1){ //if muestra gasto
				//solo totales de las planillas
				if($color % 2 == 0) { //if color
					printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$cod_pg&user=$usuario&token=$token' title='Planilla de Gasto'>%s</a></td>", $cod_pg);//cod
				} else {
					printf("<tr><td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$cod_pg&user=$usuario&token=$token' title='Planilla de Gasto'>%s</a></td>", $cod_pg);//cod
				}//cierra if color
				printf("<td align='center'>%s</td>", $mat_pg[$i][2]);//fecha
				printf("<td align='center'>%s</td>", $mat_pg[$i][3]);//numero documento
				printf("<td>%s</td>", $mat_pg[$i][4]);//estado
				printf("<td>%s</td>", $mat_pg[$i][5]);//proveedor
				printf("<td>%s</td>", $mat_pg[$i][6]);//almacen
				printf("<td>%s</td>", $mat_pg[$i][7]);//glosa
				printf("<td>%s</td>", '');//gasto
				printf("<td>%s</td>", '');//tipo
				printf("<td align='center'>%s</td>", $mat_pg[$i][8]);//moneda
				printf("<td align='right'>%s</td>", number_format($mat_pg[$i][9],$n_decimales));//total
				printf("<td align='right'>%s</td>", number_format($mat_pg[$i][11],$n_decimales));//tasa
				printf("<td align='right'>%s</td>", number_format($mat_pg[$i][12],$n_decimales));//total om
				printf("<td align='right'>%s</td>", number_format($mat_pg[$i][13],$n_decimales));//costo om
				printf("<td>%s</td></tr>", '');//costo recepcion

				$suma_total_om_ga = $suma_total_om_ga + $mat_pg[$i][12];
				$suma_costo_om_ga = $suma_costo_om_ga + $mat_pg[$i][13];

				$suma_total_om = $suma_total_om + $mat_pg[$i][12];
				$suma_costo_om = $suma_costo_om + $mat_pg[$i][13];
				$color++;

			} else { //else if muestra gasto

				//para el detalle de la planilla de gasto, fila por concepto de gasto
				$cod_moneda_exc = $mat_pg[$i][16];
				$cod_tc_exc = $mat_pg[$i][17];
				$cod_planilla = $mat_pg[$i][18];
				$cod_cxp = $mat_pg[$i][19];
				$control_fila = 0;
				$resultdet = mysqli_query($link, "select e1.fldName AS concepto_gasto, d1.pkyDocumentType AS cod_tipo, d1.fldName AS tipo, b1.fldAmount AS total, b1.codInvoice AS cod_fac, 
				b1.fldAmount * (SELECT case when $cod_moneda_exc = 1 then 1 ELSE (select c1.fldRate FROM actExchangeRateDetail c1 WHERE c1.codExchangeRate = $cod_tc_exc AND c1.codBaseCurrency = 1 AND c1.codTargetCurrency = $cod_moneda_exc) 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_exc = 1 then 1 ELSE (select c1.fldRate FROM actExchangeRateDetail c1 WHERE c1.codExchangeRate = $cod_tc_exc AND c1.codBaseCurrency = 1 AND c1.codTargetCurrency = $cod_moneda_exc) END) end) as suma_costo_exc, 
				(case when codDocumentType = 2 then (fldAmount - (fldAmount * $monto_iva / 100)) else 0 END) * (SELECT case when $cod_moneda_exc = 1 then 1 ELSE (select c1.fldRate FROM actExchangeRateDetail c1 WHERE c1.codExchangeRate = $cod_tc_exc AND c1.codBaseCurrency = 1 AND c1.codTargetCurrency = $cod_moneda_exc) 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_exc = 1 then 1 ELSE (select c1.fldRate FROM actExchangeRateDetail c1 WHERE c1.codExchangeRate = $cod_tc_exc AND c1.codBaseCurrency = 1 AND c1.codTargetCurrency = $cod_moneda_exc) END) as suma_costo_ext3
							from pchPayrollExpensesDetail b1, txsDocumentType d1, pchPurchaseConcept e1 
							where 
							b1.codPayrollExpenses = '$cod_planilla' and
							b1.codDocumentType = d1.pkyDocumentType and
							b1.codPurchaseConcept = e1.pkyPurchaseConcept
							ORDER BY concepto_gasto, tipo;");
				
				while ($row = mysqli_fetch_array($resultdet)) {
					if($control_fila == 0) { //if control 1
						if($color % 2 == 0) { //if color
							printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$cod_pg&user=$usuario&token=$token' title='Planilla de Gasto'>%s</a></td>", $cod_pg);//cod
						} else {
							printf("<tr><td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$cod_pg&user=$usuario&token=$token' title='Planilla de Gasto'>%s</a></td>", $cod_pg);//cod
						}//cierra if color
						printf("<td align='center'>%s</td>", $mat_pg[$i][2]);//fecha
						printf("<td align='center'>%s</td>", $mat_pg[$i][3]);//numero documento
						printf("<td>%s</td>", $mat_pg[$i][4]);//estado
						printf("<td>%s</td>", $mat_pg[$i][5]);//proveedor
						printf("<td>%s</td>", $mat_pg[$i][6]);//almacen
						printf("<td>%s</td>", $mat_pg[$i][7]);//glosa
						$control_fila = 1;
					} else { //else control 1
						if($color % 2 == 0) { //if color 2
							printf("<tr BGCOLOR='#e2e7fa'><td colspan='7'>%s</td>",''); //combinacion columnas
						} else { 
							printf("<tr><td colspan='7'>%s</td>",''); //combinacion columnas
						} //fin if color 2
					} //fin control 1

					printf("<td>%s</td>", $row['concepto_gasto']); //concepto de gasto
					$suma_costo_det = $row['suma_costo_exc'] + $row['suma_costo_ext2'] + $row['suma_costo_ext3'];
					$tipo_doc = $row['tipo'];
					$cod_tipo_doc = $row['cod_tipo']; 
					$cod_fac = $row['cod_fac']; 
					$total = $row['total'];
					$total_om = $row['total_om'];

					//busco si el detalle es posterior en la CxP (ojo que puede repetir si hay mas con factura y el mismo monto)
					if($cod_tipo_doc == 1 && $cod_fac == NULL){
						$resultdetcxp = mysqli_query($link, "SELECT a.codDocumentType AS cod_tipo_cxp, b.fldName AS tipo_cxp
						FROM
						payAccountsPayableDetail a, txsDocumentType b 
						WHERE 
						a.codAccountsPayable = '$cod_cxp' AND 
						a.fldAmount = '$total' AND 
						a.codDocumentType = 2 AND 
						a.codDocumentType = b.pkyDocumentType;");

						while ($row = mysqli_fetch_array($resultdetcxp)) {
							$tipo_doc = $row['tipo_cxp'];
						}
					}
					

					printf("<td>%s</td>", $tipo_doc); //tipo
					printf("<td align='center'>%s</td>", $mat_pg[$i][8]);//moneda
					printf("<td align='right'>%s</td>", number_format($total,$n_decimales));//total
					printf("<td align='right'>%s</td>", number_format($mat_pg[$i][11],$n_decimales));//tasa
					printf("<td align='right'>%s</td>", number_format($total_om,$n_decimales));//total om
					
					printf("<td align='right'>%s</td>", number_format($suma_costo_det,$n_decimales));//costo om
					printf("<td>%s</td></tr>", '');//costo recepcion

					$suma_total_om_ga = $suma_total_om_ga + $total_om;
					$suma_costo_om_ga = $suma_costo_om_ga + $suma_costo_det;

					$suma_total_om = $suma_total_om + $total_om;
					$suma_costo_om = $suma_costo_om + $suma_costo_det;
				}
				mysqli_free_result($resultdet);	

				
				$color++;
			} //fin if muestra gasto
		}//fin for 2

		//muestra recepcion de mercaderia
		for ($i = 1; $i <= $fin_rm; $i++) { //for 3
			$cod_rec = $mat_rm[$i][1];
			printf("<tr BGCOLOR='#C8E6C'><td align='center'><a href='../../ModulePurchases/VoucherReceiptOfGoods/index.php?pkyReceiptOfGoods=$cod_rec&user=$usuario&token=$token' title='Recepción de Mercadería'>%s</a></td>", $mat_rm[$i][1]);//cod
			printf("<td align='center'>%s</td>", $mat_rm[$i][2]);//numero documento
			printf("<td align='center'>%s</td>", $mat_rm[$i][3]);//fecha
			printf("<td>%s</td>", 'Recibida');//estado
			printf("<td>%s</td>", $mat_rm[$i][5]);//proveedor
			printf("<td>%s</td>", $mat_rm[$i][6]);//almacen
			printf("<td>%s</td>", $mat_rm[$i][7]);//glosa
			printf("<td>%s</td>", '');//gasto
			printf("<td>%s</td>", $mat_rm[$i][4]);//tipo
			printf("<td align='center'>%s</td>", $nom_moneda_principal);//moneda
			printf("<td align='right'>%s</td>", number_format($mat_rm[$i][9],$n_decimales));//total
			printf("<td align='right'>%s</td>", number_format(1,$n_decimales));//tasa
			printf("<td align='right'>%s</td>", number_format($mat_rm[$i][9],$n_decimales));//total om
			printf("<td align='right'>%s</td>", number_format($mat_rm[$i][10],$n_decimales));//costo om
			printf("<td align='right'>%s</td></tr>", number_format($mat_rm[$i][12],$n_decimales));//costo recepcion
			$suma_costo_recep = $suma_costo_recep + $mat_rm[$i][12];
		}//fin for 3
		

	?>
	<tr BGCOLOR='#ffcccc'><td colspan="12"><b>Total Planillas de Gastos (No suma OC ni RM)</b></td>
	<td align="right"><?php printf("<b>%s</b>", number_format($suma_total_om_ga,$n_decimales)); ?></td>
	<td align="right"><?php printf("<b>%s</b>", number_format($suma_costo_om_ga,$n_decimales)); ?></td>
	<td>&nbsp;</td>
	</tr>
	<tr BGCOLOR='#ffcccc'><td colspan="12"><b>Total (Suma OC y PG)</b></td>
	<td align="right"><?php printf("<b>%s</b>", number_format($suma_total_om,$n_decimales)); ?></td>
	<td align="right"><?php printf("<b>%s</b>", number_format($suma_costo_om,$n_decimales)); ?></td>
	<td align="right"><?php printf("<b>%s</b>", number_format($suma_costo_recep,$n_decimales)); ?></td>
	</tr>
	</table>
	<?php

} //fin if 3
?>
</body>
</html>
