<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Planillas de Gastos</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";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$usada=$_POST["usada"];
$ppara=$_POST["ppara"];
$proveedor=$_POST["proveedor"];
$orden_compra=$_POST["orden_compra"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
$usuario=$_POST["usuario"];
$otra_moneda=$_POST["otra_moneda"];
$ordenar=$_POST["ordenar"];
$cod_contexto=$_POST["contexto"];


?>
<body>

	<?php
	
//Inicio reporte planilla de gastos


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, fin.fecha';
	$nom_orden = 'Orden de Compra';
} //fin if ordenar = 3
if ($ordenar == 4) { //if ordenar = 4
	$valor_ordenar = 'order by fin.cod_pla';
	$nom_orden = 'C&oacute;digo';
} //fin if ordenar = 4


if ($usada == 0) { //if 0
	$nom_usada = 'Ambas';
	$valor_usada = ' ';
} //fin if 0

if ($usada == 1) { //if 1
	$nom_usada = 'Si';
	$valor_usada = ' a.fldUsed = 1 and ';
} //fin if 1

if ($usada == 2) { //if 2
	$nom_usada = 'No';
	$valor_usada = ' a.fldUsed = 0 and ';
} //fin if 2


if ($ppara == 0) { //if 0
	$nom_ppara = 'Ambas';
	$valor_ppara = ' ';
} //fin if 0

if ($ppara == 1) { //if 1
	$nom_ppara = 'Orden de Compra';
	$valor_ppara = ' a.codPurchaseOrder is not NULL and ';
} //fin if 1

if ($ppara == 2) { //if 2
	$nom_ppara = 'Traspaso';
	$valor_ppara = ' a.codPurchaseOrder is NULL and ';
} //fin if 2


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 ($orden_compra == 0) { //if orden_compra
    $valor_orden_compra = ' ';
	$nom_orden_compra = 'Todos';
  } else { 
    $valor_orden_compra = ' a.codPurchaseOrder = '.$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

//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 planillas de gasto
/*
$result = mysqli_query($link, "select fin.cod_pla as cod_pla, fin.fecha as fecha, fin.fecha_pago as fecha_pago, fin.nro_doc as nro_doc, fin.total as total, fin.cod_orden as cod_orden, fin.cod_prov as cod_prov, fin.proveedor as proveedor, fin.cod_moneda as cod_moneda, fin.moneda as moneda, fin.cod_tc as cod_tc, fin.cod_cxp as cod_cxp, fin.cod_asiento as cod_asiento, fin.cod_mov_caja as cod_mov_caja, fin.glosa as glosa, fin.tasa as tasa, fin.total_om as total_om, fin.usuario_reg as usuario_reg, fin.suma_detalle as suma_detalle, fin.retenciones as retenciones, fin.suma_costo_ext as suma_costo_ext, fin.usada as usada
from(
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, tc.fldRate as tasa, t.total * tc.fldRate 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_ext1 + t.suma_costo_ext2 + t.suma_costo_ext3) * tc.fldRate as suma_costo_ext, 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, g.fldName as usuario_reg, 
(select sum(fldAmount) from pchPayrollExpensesDetail where codPayrollExpenses = a.pkyPayrollExpenses group by codPayrollExpenses) as suma_detalle, 
(select sum(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) * tc1.fldRate from pchPayrollExpensesDetail b1, actExchangeRateDetail tc1 where b1.codPayrollExpenses = a.pkyPayrollExpenses and tc1.codTargetCurrency = '$cod_moneda_principal' and tc1.codBaseCurrency = a.codCurrency and tc1.codExchangeRate = a.codExchangeRate group by b1.codPayrollExpenses) as suma_costo_ext1,
(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, 
a.fldUsed as usada
from 
pchPayrollExpenses a, agnCompany b, actCurrency c, accGroup g  
where 
$valor_usada 
$valor_ppara 
$valor_proveedor 
$valor_orden_compra 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and 
a.codSupplier = b.pkyCompany and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.codCurrency = c.pkyCurrency 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_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, 1 as tasa, t.total * 1 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_ext1 + t.suma_costo_ext2 + t.suma_costo_ext3) * 1 as suma_costo_ext, 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, g.fldName as usuario_reg, (select sum(fldAmount) from pchPayrollExpensesDetail where codPayrollExpenses = a.pkyPayrollExpenses group by codPayrollExpenses) as suma_detalle, 
(select sum(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) * 1 from pchPayrollExpensesDetail b1 where b1.codPayrollExpenses = a.pkyPayrollExpenses group by b1.codPayrollExpenses) as suma_costo_ext1,
(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,  
a.fldUsed as usada
from 
pchPayrollExpenses a, agnCompany b, actCurrency c, accGroup g  
where 
$valor_usada 
$valor_ppara 
$valor_proveedor 
$valor_orden_compra 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and 
a.codSupplier = b.pkyCompany and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.codCurrency = c.pkyCurrency and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = '$otra_moneda') fin  
$valor_ordenar;");
*/

$result = mysqli_query($link, "select fin.cod_pla as cod_pla, fin.fecha as fecha, fin.fecha_pago as fecha_pago, fin.nro_doc as nro_doc, fin.total as total, fin.cod_orden as cod_orden, fin.cod_prov as cod_prov, fin.proveedor as proveedor, fin.cod_moneda as cod_moneda, fin.moneda as moneda, fin.cod_tc as cod_tc, fin.cod_cxp as cod_cxp, fin.cod_asiento as cod_asiento, fin.cod_mov_caja as cod_mov_caja, fin.glosa as glosa, fin.tasa as tasa, fin.total_om as total_om, fin.usuario_reg as usuario_reg, fin.suma_detalle as suma_detalle, fin.retenciones as retenciones, fin.suma_costo_ext as suma_costo_ext, fin.usada as usada, fin.cod_tc as cod_tc
from(
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, tc.fldRate as tasa, t.total * tc.fldRate 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) * tc.fldRate as suma_costo_ext, 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, 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, 
a.fldUsed as usada
from 
pchPayrollExpenses a, agnCompany b, actCurrency c, accGroup g  
where 
$valor_usada 
$valor_ppara 
$valor_proveedor 
$valor_orden_compra 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and 
a.codSupplier = b.pkyCompany and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.codCurrency = c.pkyCurrency 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_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, 1 as tasa, t.total * 1 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) * 1 as suma_costo_ext, 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, 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,  
a.fldUsed as usada
from 
pchPayrollExpenses a, agnCompany b, actCurrency c, accGroup g  
where 
$valor_usada 
$valor_ppara 
$valor_proveedor 
$valor_orden_compra 
$valor_contexto 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and 
a.codSupplier = b.pkyCompany and 
a.sysAuthorizationGroup = g.pkyGroup and 
a.codCurrency = c.pkyCurrency and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda = '$otra_moneda') fin  
$valor_ordenar;");

$mat_p = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$mat_p[$i][1] = $row['cod_pla'];
	$mat_p[$i][2] = $row['cod_orden'];
	$mat_p[$i][3] = $row['nro_doc'];
	$mat_p[$i][4] = $row['fecha'];
	$mat_p[$i][5] = $row['proveedor'];
	$mat_p[$i][6] = $row['usada'];
	$mat_p[$i][7] = $row['suma_costo_ext'];
	$mat_p[$i][8] = $row['cod_asiento'];
	$mat_p[$i][9] = $row['cod_cxp'];
	$mat_p[$i][10] = $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
	$mat_p[$i][11] = $glosa;
	$mat_p[$i][12] = $row['retenciones'];
	$mat_p[$i][13] = $row['total'];
	$mat_p[$i][14] = $row['tasa'];
	$mat_p[$i][15] = $row['total_om'];
	$mat_p[$i][16] = $row['cod_moneda'];
	$mat_p[$i][17] = $row['moneda'];
	$mat_p[$i][18] = $row['cod_tc'];
	$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_principal' and 
		tc1.codBaseCurrency = '$cod_moneda_exc' 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);	
	
	if($cod_moneda_principal == $otra_moneda) {//if 2 
		$mat_p[$i][19] = $mat_p[$i][7] + $suma_costo_exc;
	} else {
		$resulttipocambio = mysqli_query($link, "select fldRate as tasa
		from actExchangeRateDetail
		where 
		codExchangeRate = '$cod_tc_exc' and 
		codTargetCurrency = '$cod_moneda_principal' and 
		codBaseCurrency = '$otra_moneda' and 
		sysEnabled = 1 and  sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($resulttipocambio)) {
			$tasa_r = $row['tasa'];
		}
		mysqli_free_result($resulttipocambio);	
		$mat_p[$i][19] = $mat_p[$i][7] + $suma_costo_exc * $tasa_r;
	}
	$i++;
}
mysqli_free_result($result);	
$fin_i = $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; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Planillas de Gastos</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>Usada: </b><?php echo $nom_usada; ?></TD>
        <TD width="25%"><b>Planilla para: </b><?php echo $nom_ppara; ?></TD>
        </TR>
        <TR>
        <TD><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD><b>Contexto: </b><?php echo $nom_contexto; ?></TD>
        <TD><b>Ordenado por: </b><?php echo $nom_orden; ?></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; ?>" 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>Orden de Compra</b></TD>
    <TD align="center"><b>Pla. Para</b></TD>
    <TD align="center"><b>Usada</b></TD>
    <TD align="center"><b>Asiento</b></TD>
    <TD align="center"><b>CxP</b></TD>
    <TD align="center"><b>Mov. Caja</b></TD>
    <TD align="center"><b>Glosa</b></TD>
    <TD align="center"><b>Mon.</b></TD>
    <TD align="center"><b>Retenciones</b></TD>
    <TD align="center"><b>Total</b></TD>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Total en <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Costo en <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Costo Recep. en <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Costo Por Recep. en <?php echo $nom_otra_moneda; ?></b></TD>
    </TR>
	</thead>
    <tbody class="marcafila">

<?php

$color = 2;
$suma_total = 0;
$suma_costo = 0;
$suma_costo_recep = 0;
$suma_costo_por_recep = 0;
$costo_recep = 0;

for ($i = 1; $i <= $fin_i; $i++) {
	$cod_pla = $mat_p[$i][1];
	$cod_orden = $mat_p[$i][2];
	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$cod_pla&user=$usuario' title='Planilla de Gasto'>%s</a></td>", $cod_pla);//cod
	} else {
		printf("<tr><td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$cod_pla&user=$usuario' title='Planilla de Gasto'>%s</a></td>", $cod_pla);//cod
	}//cierra if color
	printf("<td align='center'>%s</td>", $mat_p[$i][3]);//nro documento
	printf("<td align='center'>%s</td>", $mat_p[$i][4]);//fecha
	printf("<td>%s</td>", $mat_p[$i][5]);//proveedor
	printf("<td align='right'><a href='../../ModulePurchases/VoucherPurchaseOrder/index.php?pkyPurchaseOrder=$cod_orden&user=$usuario' title='Orden de Compra'>%s</a></td>", $cod_orden);//orden de compra
	if($cod_orden == NULL) { 
		$nom_ppara = 'Trasp.'; 
	} else {
		$nom_ppara = 'OC'; 
	}
	printf("<td align='center'>%s</td>", $nom_ppara);//planilla para
	
	//usada
	if($mat_p[$i][6] == 0) { 
		$nom_usada = 'No'; 
		$costo_recep = 0;
	} else {
		$nom_usada = 'Si'; 
		$costo_recep = $mat_p[$i][19];
	}
	printf("<td align='center'>%s</td>", $nom_usada);//usada
	$cod_asiento = $mat_p[$i][8];
	$cod_cxp = $mat_p[$i][9];
	$cod_mov_caja = $mat_p[$i][10];
	printf("<td align='right'><a href='../../ModuleAccounting/VoucherAccountEntry/index.php?pkyAccountEntry=$cod_asiento&showCC=$mostrar_asiento_cc&user=$usuario' title='Asiento Contable'>%s</a></td>", $cod_asiento);//asiento
	printf("<td align='right'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$cod_cxp&user=$usuario' title='Nota de Cuenta por Pagar'>%s</a></td>", $cod_cxp);//cxp
	printf("<td align='right'><a href='../../ModulePettyCash/VoucherPettyCashMovement/index.php?pkyPettyCashMovement=$cod_mov_caja&user=$usuario' title='Nota de Movimiento de Caja'>%s</a></td>", $cod_mov_caja);//mov caja
	
	printf("<td>%s</td>", $mat_p[$i][11]);//glosa
	printf("<td>%s</td>", $mat_p[$i][17]);//moneda
	printf("<td align='right'>%s</td>", number_format($mat_p[$i][12],$n_decimales));//retenciones
	printf("<td align='right'>%s</td>", number_format($mat_p[$i][13],$n_decimales));//total 
	printf("<td align='right'>%s</td>", number_format($mat_p[$i][14],$n_decimales));//tasa
	printf("<td align='right'>%s</td>", number_format($mat_p[$i][15],$n_decimales));//total neto otra moneda
	printf("<td align='right'>%s</td>", number_format($mat_p[$i][19],$n_decimales));//total costo otra moneda
	printf("<td align='right'>%s</td>", number_format($costo_recep,$n_decimales));//total costo recep otra moneda
	printf("<td align='right'>%s</td></tr>", number_format($mat_p[$i][19] - $costo_recep,$n_decimales));//total costo por recepcionar otra moneda
	
	$suma_total = $suma_total + $mat_p[$i][15];
	$suma_costo = $suma_costo + $mat_p[$i][19];
	$suma_costo_recep = $suma_costo_recep + $costo_recep;
	$suma_costo_por_recep = $suma_costo_por_recep + $mat_p[$i][19] - $costo_recep;
	$color++;
}//fin while

/*
while ($row = mysqli_fetch_array($result)) {
	$cod_pla = $row['cod_pla'];
	$cod_orden = $row['cod_orden'];
	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$cod_pla&user=$usuario' title='Planilla de Gasto'>%s</a></td>", $row['cod_pla']);//cod
	} else {
		printf("<tr><td align='center'><a href='../../ModulePurchases/VoucherPayrollExpenses/index.php?pkyPayrollExpenses=$cod_pla&user=$usuario' title='Planilla de Gasto'>%s</a></td>", $row['cod_pla']);//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 align='right'><a href='../../ModulePurchases/VoucherPurchaseOrder/index.php?pkyPurchaseOrder=$cod_orden&user=$usuario' title='Orden de Compra'>%s</a></td>", $row['cod_orden']);//orden de compra
	if($cod_orden == NULL) { 
		$nom_ppara = 'Trasp.'; 
	} else {
		$nom_ppara = 'OC'; 
	}
	printf("<td align='center'>%s</td>", $nom_ppara);//planilla para
	
	if($row['usada'] == 0) { 
		$nom_usada = 'No'; 
		$costo_recep = 0;
	} else {
		$nom_usada = 'Si'; 
		$costo_recep = $row['suma_costo_ext'];
	}
	printf("<td align='center'>%s</td>", $nom_usada);//usada
	$cod_asiento = $row['cod_asiento'];
	$cod_cxp = $row['cod_cxp'];
	$cod_mov_caja = $row['cod_mov_caja'];
	printf("<td align='right'><a href='../../ModuleAccounting/VoucherAccountEntry/index.php?pkyAccountEntry=$cod_asiento&showCC=$mostrar_asiento_cc&user=$usuario' title='Asiento Contable'>%s</a></td>", $row['cod_asiento']);//asiento
	printf("<td align='right'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$cod_cxp&user=$usuario' title='Nota de Cuenta por Pagar'>%s</a></td>", $row['cod_cxp']);//cxp
	printf("<td align='right'><a href='../../ModulePettyCash/VoucherPettyCashMovement/index.php?pkyPettyCashMovement=$cod_mov_caja&user=$usuario' title='Nota de Movimiento de Caja'>%s</a></td>", $row['cod_mov_caja']);//mov caja
	$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['retenciones'],$n_decimales));//retenciones
	printf("<td align='right'>%s</td>", number_format($row['total'],$n_decimales));//total 
	printf("<td align='right'>%s</td>", number_format($row['tasa'],$n_decimales));//tasa
	printf("<td align='right'>%s</td>", number_format($row['total_om'],$n_decimales));//total neto otra moneda
	printf("<td align='right'>%s</td>", number_format($row['suma_costo_ext'],$n_decimales));//total costo otra moneda
	printf("<td align='right'>%s</td>", number_format($costo_recep,$n_decimales));//total costo recep otra moneda
	printf("<td align='right'>%s</td></tr>", number_format($row['suma_costo_ext'] - $costo_recep,$n_decimales));//total costo por recepcionar otra moneda
	
	$suma_total = $suma_total + $row['total_om'];
	$suma_costo = $suma_costo + $row['suma_costo_ext'];
	$suma_costo_recep = $suma_costo_recep + $costo_recep;
	$suma_costo_por_recep = $suma_costo_por_recep + $row['suma_costo_ext'] - $costo_recep;
	$color++;
}//fin while

mysqli_free_result($result);		
*/
?>
 
<tr BGCOLOR='#ffcccc'><td colspan="15"><b>Total</b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_total,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_costo,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_costo_recep,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_costo_por_recep,$n_decimales)); ?></td>
</tr>
</table>
         
<?php
	
} //fin if 2
?>
</table>

</body>
</html>
