<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Proyecci&oacute;n de Producci&oacute;n</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"];
$proyeccion=$_POST["proyeccion"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
//$usuario=$_POST["usuario"];
$otra_moneda=$_POST["otra_moneda"];
$agrupar=$_POST["agrupar"];
$cod_contexto=$_POST["contexto"];
$modalidad=$_POST["modalidad"];
//$ordenar=$_POST["ordenar"];
//$usuario_det=$_GET["usuario_det"];
$cod_proy_det=$_GET["cod_proy_det"];
$cod_gasto_det=$_GET["cod_gasto_det"];
$otra_moneda_det=$_GET["otra_moneda_det"];
$cual_es=$_GET["cual_es"];
$fechaini_det=$_GET["fechaini_det"];
$fechafin_det=$_GET["fechafin_det"];
$cod_item_det=$_GET["cod_item_det"];
$cod_contexto_det=$_GET["contexto_det"];

?>
<body>

	<?php
	
//Inicio reporte proyeccion de produccion


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);
	
//tasa de cambio
if($otra_moneda == $cod_moneda_principal) {// if tasa
	$tasa = 1;
} else {// else if tasa
$resultcambio = mysqli_query($link, "select fldRate as tasa 
from actExchangeRateDetail 
where 
codExchangeRate = '$tc_hoy' and 
codBaseCurrency = '$otra_moneda' and 
codTargetCurrency = '$cod_moneda_principal' and 
sysEnabled = 1 and sysAuthorized = 1;");
		
	while ($row = mysqli_fetch_array($resultcambio)) {
	$tasa = $row['tasa'];
	}
	mysqli_free_result($resultcambio);
}// fin if tasa

if ($agrupar == 0) { 
	$nom_agrupar = 'Concepto de Gasto';
} else {
	$nom_agrupar = 'Item Proyectado';
}

/*
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 ($modalidad == 0 ) { //if modalidad
	
	$nom_modalidad = 'Proyecci&oacute;n';
	if ($proyeccion == 0) { //if proyeccion
		$valor_proyeccion = ' ';
		$valor_proyeccion2 = ' ';
		$valor_proyeccion3 = ' ';
		$valor_proyeccion4 = ' '; //para agrupar = 1
		$valor_proyeccion5 = ' '; //para agrupar = 1
		$valor_fecha = ' p.fldInitDate >= "'.$fechaini.'" and p.fldInitDate <= "'.$fechafin.'" and';
		$valor_fecha2 = ' p1.fldInitDate >= "'.$fechaini.'" and p1.fldInitDate <= "'.$fechafin.'" and';
		$valor_fecha4 = ' a.fldInitDate >= "'.$fechaini.'" and a.fldInitDate <= "'.$fechafin.'" and'; //para agrupar = 1
		$nom_proyeccion = 'Todos';
	  } else { //else if proyeccion
		$valor_proyeccion = ' a.codProductionProjection = '.$proyeccion.' and ';
		$valor_proyeccion2 = ' b.codProjection = '.$proyeccion.' and ';
		$valor_proyeccion3 = ' b1.codProjection = '.$proyeccion.' and ';
		$valor_proyeccion4 = ' a.pkyProductionProjection = '.$proyeccion.' and ';
		$valor_proyeccion5 = ' c.codProjection = '.$proyeccion.' and ';
		$valor_fecha = ' ';
		$valor_fecha2 = ' ';
		$valor_fecha4 = ' ';
		
		//nombre de proyeccion
		$resultpro = mysqli_query($link, "select pkyProductionProjection as codigo, fldDocumentNumber as nro_doc, fldInitDate as fechaini, fldEndDate as fechafin, txtDescription as glosa
	from prdProductionProjection
	where 
	pkyProductionProjection = '$proyeccion';");
		while ($row = mysqli_fetch_array($resultpro)) {
		$nom_proyeccion = $row['nro_doc'].' - ('.$row['fechaini'].' a '.$row['fechafin'].') - '.$row['glosa'];
		}
		mysqli_free_result($resultpro);
	  } //fin if proyeccion

} else { //else if modalidad
	
	$nom_modalidad = 'Factores';
	$valor_fecha5 = ' b.fldDate >= "'.$fechaini.'" and b.fldDate <= "'.$fechafin.'" and';
	$valor_fecha6 = ' a.fldProductionPeriod >= "'.$fechaini.'" and a.fldProductionPeriod <= "'.$fechafin.'" and';
	
} //fin if modalidad


//contexto
if($cod_contexto == -1){ 
	$nom_contexto = 'Todos';
	$valor_contexto = ' ';
	$valor_contexto2 = ' ';
	$valor_contexto4 = ' ';
	$valor_contexto5 = ' '; //factores
} 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 = ' p.sysContext = '.$cod_contexto.' and ';	
	$valor_contexto2 = ' p1.sysContext = '.$cod_contexto.' and ';
	$valor_contexto4 = ' a.sysContext = '.$cod_contexto.' and ';
	$valor_contexto5 = ' b.sysContext = '.$cod_contexto.' and '; //factores	
}



//modalidad = 0 es proyeccion
if($modalidad == 0) { //if modalidad0
//codigo del gasto extraido de la configuración exclusivo para el activo fijo
//$cod_gasto_af = 9998;
if($agrupar == 0) { //if agrupar0
	
//listado de conceptos de gasto, se adicionó verdadero_depre cuando el concepto de gasto esta en una deprecacion pero tambien puede estar en una cxp

$result = mysqli_query($link, "select fin.codigo as codigo, fin.cod_proy as cod_proy, fin.cod_ord_prod as cod_ord_prod, fin.cod_prod_con as cod_prod_con, fin.cod_gasto as cod_gasto, fin.gasto as gasto, sum(fin.proyectado) as proyectado, sum(fin.ejecutado) as ejecutado, sum(fin.verdadero) as verdadero, sum(fin.verdadero_depre) as verdadero_depre
from 
(select a.pkyProductionProjectionItemExpense as codigo, a.codProductionProjection as cod_proy, 0 as cod_ord_prod, a.codProductionConcept as cod_prod_con, a.codExpensesConcept as cod_gasto, b.fldName as gasto, a.fldAmount as proyectado, 0 as ejecutado, 0 as verdadero, 0 as verdadero_depre
from prdProductionProjectionItemExpense a, payExpensesConcept b, prdProductionProjection p
where 
$valor_proyeccion 
$valor_fecha 
$valor_contexto 
a.codExpensesConcept = b.pkyExpensesConcept and 
a.codProductionProjection = p.pkyProductionProjection and 
a.sysEnabled = 1 and a.sysAuthorized = 1

union all 

select a.pkyProductionOrderConcept as codigo, b.codProjection as cod_proy, a.codProductionOrder as cod_ord_prod, a.codProductionConcept as cod_prod_con, c.codExpensesConcept as cod_gasto, d.fldName as gasto, 0 as proyectado, a.fldAmount as ejecutado, 0 as verdadero, 0 as verdadero_depre
from prdProductionOrderConcept a, prdProductionOrder b, prdProductionProjectionItemExpense c, payExpensesConcept d, prdProductionProjection p
where 
$valor_proyeccion2 
$valor_fecha 
$valor_contexto 
a.codProductionOrder = b.pkyProductionOrder and 
a.codProductionConcept = c.codProductionConcept and 
b.codProjection = c.codProductionProjection and 
c.codExpensesConcept = d.pkyExpensesConcept and 
b.codProjection = p.pkyProductionProjection and 
a.sysEnabled = 1 and a.sysAuthorized = 1
union all
select a.pkyProductionOrderConcept as codigo, b.codProjection as cod_proy, a.codProductionOrder as cod_ord_prod, a.codProductionConcept as cod_prod_con, 0 as cod_gasto, 'NO TIENE' as gasto, 0 as proyectado, a.fldAmount as ejecutado, 0 as verdadero, 0 as verdadero_depre
from prdProductionOrderConcept a, prdProductionOrder b, prdProductionProjection p
where 
$valor_proyeccion2 
$valor_fecha 
$valor_contexto 
a.pkyProductionOrderConcept not in (select a1.pkyProductionOrderConcept 
from prdProductionOrderConcept a1, prdProductionOrder b1, prdProductionProjectionItemExpense c1, payExpensesConcept d1, prdProductionProjection p1
where 
$valor_proyeccion3 
$valor_fecha2 
$valor_contexto2 
a1.codProductionOrder = b1.pkyProductionOrder and 
a1.codProductionConcept = c1.codProductionConcept and 
b1.codProjection = c1.codProductionProjection and 
c1.codExpensesConcept = d1.pkyExpensesConcept and 
b1.codProjection = p1.pkyProductionProjection and 
a1.sysEnabled = 1 and a1.sysAuthorized = 1) and 
a.codProductionOrder = b.pkyProductionOrder and 
b.codProjection = p.pkyProductionProjection and 
a.sysEnabled = 1 and a.sysAuthorized = 1

union all 

select t.codigo as codigo, t.cod_proy as cod_proy, t.cod_ord_prod as cod_ord_prod, t.cod_prod_con as cod_prod_con, t.cod_gasto as cod_gasto, t.gasto as gasto, t.proyectado as proyectado, t.ejecutado as ejecutado, t.verdadero * r.fldRate as verdadero, t.verdadero_depre as verdadero_depre
from 
(select b.pkyAccountsPayableDetail as codigo, a.codProductionProjection as cod_proy, 0 as cod_ord_prod, 0 as cod_prod_con, b.codExpensesConcept as cod_gasto, c.fldName as gasto, 0 as proyectado, 0 as ejecutado, 
case when b.codDocumentType = 2 then (b.fldAmount - (b.fldAmount * $monto_iva / 100)) else 
case when b.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) * r1.fldRate as total from txsPurchasesLedger a1, actExchangeRateDetail r1 where a1.pkyPurchasesLedger = b.fldInvoice and a.codExchangeRate = r1.codExchangeRate and $cod_moneda_principal = r1.codTargetCurrency and
a.codCurrency = r1.codBaseCurrency) else b.fldAmount end end as verdadero, 0 as verdadero_depre, a.codCurrency as cod_moneda, a.codExchangeRate as tc, $cod_moneda_principal as otra_moneda
from payAccountsPayable a, payAccountsPayableDetail b, payExpensesConcept c, prdProductionProjection p
where 
$valor_proyeccion  
$valor_fecha 
$valor_contexto 
b.codAccountsPayable = a.pkyAccountsPayable and 
b.codExpensesConcept = c.pkyExpensesConcept and
a.codProductionProjection is not null and 
a.codProductionProjection = p.pkyProductionProjection and 
b.sysEnabled = 1 and b.sysAuthorized = 1) t, actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all 
select t.codigo as codigo, t.cod_proy as cod_proy, t.cod_ord_prod as cod_ord_prod, t.cod_prod_con as cod_prod_con, t.cod_gasto as cod_gasto, t.gasto as gasto, t.proyectado as proyectado, t.ejecutado as ejecutado, t.verdadero as verdadero, t.verdadero_depre as verdadero_depre
from 
(select b.pkyAccountsPayableDetail as codigo, a.codProductionProjection as cod_proy, 0 as cod_ord_prod, 0 as cod_prod_con, b.codExpensesConcept as cod_gasto, c.fldName as gasto, 0 as proyectado, 0 as ejecutado, 
case when b.codDocumentType = 2 then (b.fldAmount - (b.fldAmount * $monto_iva / 100)) else 
case when b.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) as total from txsPurchasesLedger a1 where a1.pkyPurchasesLedger = b.fldInvoice) else b.fldAmount end end as verdadero, 0 as verdadero_depre, a.codCurrency as cod_moneda, a.codExchangeRate as tc, $cod_moneda_principal as otra_moneda
from payAccountsPayable a, payAccountsPayableDetail b, payExpensesConcept c, prdProductionProjection p
where 
$valor_proyeccion 
$valor_fecha 
$valor_contexto 
b.codAccountsPayable = a.pkyAccountsPayable and 
b.codExpensesConcept = c.pkyExpensesConcept and
a.codProductionProjection is not null and 
a.codProductionProjection = p.pkyProductionProjection and 
b.sysEnabled = 1 and b.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda

union all 

select b.pkyFixedAssetDepreciationDetail as codigo, a.codProductionProjection as cod_proy, 0 as cod_ord_prod, 0 as cod_prod_con, a.codExpensesConcept as cod_gasto, c.fldName as gasto, 0 as proyectado, 0 as ejecutado, 0 as verdadero, b.fldValue as verdadero_depre 
from 
fasFixedAssetDepreciation a, fasFixedAssetDepreciationDetail b,payExpensesConcept c, prdProductionProjection p 
where 
$valor_proyeccion  
$valor_fecha   
$valor_contexto 
b.codFixedAssetDepreciation = a.pkyFixedAssetDepreciation and 
a.codExpensesConcept = c.pkyExpensesConcept and
a.codProductionProjection is not null and 
a.codProductionProjection = p.pkyProductionProjection and 
b.sysEnabled = 1 and b.sysAuthorized = 1) fin
group by fin.cod_gasto
order by fin.gasto;");

/* para que sume las retenciones se quito el 3 porque el query saca el dato del detalle no de la cabecera
case when b.codDocumentType = 1 or b.codDocumentType = 2 then (b.fldAmount - (b.fldAmount * $monto_iva / 100)) else case when b.codDocumentType = 3 then (b.fldAmount - (select sum(a1.fldTotal) as total1
from
txsRetentionDetail a1 
where 
a1.codAccountsPayable = b.codAccountsPayable 
group by a1.codAccountsPayable)) else b.fldAmount end end as verdadero
*/
?>
<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>Proyecci&oacute;n de Producci&oacute;n</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> 
        <?php if($proyeccion == 0) { ?>
        <TD width="25%"><b>Proyecci&oacute;n: </b><?php echo $nom_proyeccion; ?></TD>
        <TD width="25%"><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <?php } else { ?>
        <TD width="25%"><b>Proyecci&oacute;n: </b><a href="../../ModuleProduction/VoucherProductionProjection/index.php?pkyProductionProjection=<?php echo $proyeccion; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Proyecci&oacute;n de Producci&oacute;n"><?php echo $proyeccion; ?></a> - <?php echo $nom_proyeccion; ?></TD>
        <?php } ?>
        <TD width="25%"><b>Agrupado por: </b><?php echo $nom_agrupar; ?></TD>
        <TD width="25%"><b>Expresado en: </b><?php echo $nom_otra_moneda; ?></TD>
        </TR>
        <TR>
        <TD colspan="4"><b>Contexto: </b><?php echo $nom_contexto; ?></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" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Concepto de Gasto</b></TD>
    <TD align="center"><b>Proyectado</b></TD>
    <TD align="center"><b>Ejecutado</b></TD>
    <TD align="center"><b>Real</b></TD>
    </TR>
	</thead>
    <tbody class="marcafila">

<?php

$color = 2;
$suma_p = 0;
$suma_e = 0;
$suma_r = 0;

while ($row = mysqli_fetch_array($result)) {
	$cod_proy = $proyeccion;
	$cod_gasto = $row['cod_gasto'];
	$gasto = $row['gasto'];
	$proyectado = $row['proyectado'];
	$ejecutado = $row['ejecutado'];
	$verdadero = $row['verdadero'];
	$verdadero_depre = $row['verdadero_depre'];

	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $cod_gasto);//cod gasto
	} else {
		printf("<tr><td align='center'>%s</td>", $cod_gasto);//cod gasto
	}//cierra if color
	//si no esta proyectado entonces no se cruza
	if($cod_gasto == 0) {//if no proy
		printf("<td align='left'><font color='#FF0000'>%s</font></td>", $gasto);//gasto
	} else {
		if($verdadero == 0 && $verdadero_depre != 0) {//if depre 0
			printf("<td align='left'>%s (DEP.)</td>", $gasto);//gasto
		} // fin if depre 0
		if(($verdadero != 0 && $verdadero_depre == 0) || ($verdadero == 0 && $verdadero_depre == 0)) {//if depre 1
			printf("<td align='left'>%s</td>", $gasto);//gasto
		}// fin if depre 1
		if($verdadero != 0 && $verdadero_depre != 0) {//if depre 2
			printf("<td align='left'>%s (DEP. + CxP)</td>", $gasto);//gasto
		}//fin if depre 2
	}//fin if no proy
	printf("<td align='right'><a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=2&cod_proy_det=$cod_proy&cod_gasto_det=$cod_gasto&otra_moneda_det=$otra_moneda&cual_es=1&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Proyectado'>%s</a></td>", number_format($proyectado * $tasa,$n_decimales));//proyectado
	printf("<td align='right'><a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=2&cod_proy_det=$cod_proy&cod_gasto_det=$cod_gasto&otra_moneda_det=$otra_moneda&cual_es=2&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Ejecutado'>%s</a></td>", number_format($ejecutado * $tasa,$n_decimales));//ejecutado
	//para usar el concepto de gasto en depreciacion
	if($verdadero == 0 && $verdadero_depre != 0) {//if depre 3
		printf("<td align='right'><a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=2&cod_proy_det=$cod_proy&cod_gasto_det=$cod_gasto&otra_moneda_det=$otra_moneda&cual_es=4&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Real Dep.'>%s</a></td></tr>", number_format($verdadero_depre * $tasa,$n_decimales));//verdadero
	}//fin if depre 3
	
	if(($verdadero != 0 && $verdadero_depre == 0) || ($verdadero == 0 && $verdadero_depre == 0)) {//if depre 4
		printf("<td align='right'><a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=2&cod_proy_det=$cod_proy&cod_gasto_det=$cod_gasto&otra_moneda_det=$otra_moneda&cual_es=3&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Real CxP'>%s</a></td></tr>", number_format($verdadero * $tasa,$n_decimales));//verdadero
	}// fin if depre 4
	
	if($verdadero != 0 && $verdadero_depre != 0) {//if depre 5
		printf("<td align='right'><a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=2&cod_proy_det=$cod_proy&cod_gasto_det=$cod_gasto&otra_moneda_det=$otra_moneda&cual_es=4&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Real Dep.'>%s</a> + <a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=2&cod_proy_det=$cod_proy&cod_gasto_det=$cod_gasto&otra_moneda_det=$otra_moneda&cual_es=3&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Real CxP'>%s</a> = %s</td></tr>", number_format($verdadero_depre * $tasa,$n_decimales), number_format($verdadero * $tasa,$n_decimales), number_format(($verdadero_depre + $verdadero) * $tasa,$n_decimales));//verdadero
	}//fin if depre 5
	
	//$verdadero = $verdadero + $verdadero_depre;
	$suma_p = $suma_p + $proyectado * $tasa;
	$suma_e = $suma_e + $ejecutado * $tasa;
	$suma_r = $suma_r + ($verdadero + $verdadero_depre) * $tasa;
	
	$color++;
}//fin while
	
?>
 
<tr BGCOLOR='#ffcccc'><td colspan="2"><b>Total</b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_e,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_r,$n_decimales)); ?></td>
</tr>
</table>
         
<?php


} else { //else if agrupar0

//cantidad proyectada y cantidad producida
$result = mysqli_query($link, "select t.cod_item as cod_item, t.item as item, sum(t.cant_proy) as cant_proy, sum(t.cant_prod) as cant_prod 
from 
(select a.pkyProductionProjection as cod_proy, a.fldDocumentNumber as nro_doc_proy, a.fldUsed as usada_proy, b.codItem as cod_item, i.fldName as item, b.fldAmount as cant_proy, 0 as cant_prod
from
prdProductionProjection a, prdProductionProjectionItem b, invItem i 
where 
$valor_proyeccion4 
$valor_fecha4 
$valor_contexto4 
a.pkyProductionProjection = b.codProductionProjection and 
b.codItem = i.pkyItem and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
union all 
select c.codProjection as cod_proy, c.fldDocumentNumber as nro_doc_proy, 10 as usada_proy, d.codItem as cod_item, i.fldName as item, 0 as cant_proy, d.fldQuantity as cant_prod
from
prdProductionOrder c, prdProductionOrderDetail d, invItem i, prdProductionProjection a  
where 
$valor_proyeccion5 
$valor_fecha4 
$valor_contexto4 
a.pkyProductionProjection = c.codProjection and  
c.codProjection is not null and 
c.pkyProductionOrder = d.codProductionOrder and 
d.codItem = i.pkyItem and 
c.sysEnabled = 1 and c.sysAuthorized = 1) t 
group by t.cod_item 
order by t.item;");

?>
<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>Proyecci&oacute;n de Producci&oacute;n</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> 
        <?php if($proyeccion == 0) { ?>
        <TD width="33%"><b>Proyecci&oacute;n: </b><?php echo $nom_proyeccion; ?></TD>
        <TD width="33%"><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <?php } else { ?>
        <TD width="33%"><b>Proyecci&oacute;n: </b><a href="../../ModuleProduction/VoucherProductionProjection/index.php?pkyProductionProjection=<?php echo $proyeccion; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Proyecci&oacute;n de Producci&oacute;n"><?php echo $proyeccion; ?></a> - <?php echo $nom_proyeccion; ?></TD>
        <?php } ?>
        <TD width="33%"><b>Agrupado por: </b><?php echo $nom_agrupar; ?></TD>
        <!--<TD width="25%"><b>Expresado en: </b><?php echo $nom_otra_moneda; ?></TD>-->
        </TR>
        <TR>
        <TD colspan="3"><b>Contexto: </b><?php echo $nom_contexto; ?></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" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Item</b></TD>
    <TD align="center"><b>Cantidad Proyectada</b></TD>
    <TD align="center"><b>Cantidad Producida</b></TD>
    <TD align="center"><b>Diferencia</b></TD>
    </TR>
	</thead>
    <tbody class="marcafila">

<?php
$color = 2;
$suma_proy = 0;
$suma_prod = 0;
$suma_dif = 0;

while ($row = mysqli_fetch_array($result)) {
	$cod_proy = $proyeccion;
	$cod_item = $row['cod_item'];
	$item = $row['item'];
	$proyectado = $row['cant_proy'];
	$producido = $row['cant_prod'];
	$diferencia = $proyectado - $producido;

	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $cod_item);//cod item
	} else {
		printf("<tr><td align='center'>%s</td>", $cod_item);//cod item
	}//cierra if color
	printf("<td align='left'>%s</td>", $item);//item
	printf("<td align='right'><a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=3&cod_proy_det=$cod_proy&cod_item_det=$cod_item&otra_moneda_det=$otra_moneda&cual_es=1&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Insumos Proyectados'>%s</a></td>", number_format($proyectado,$n_cantidad));//proyectado
	printf("<td align='right'><a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=3&cod_proy_det=$cod_proy&cod_item_det=$cod_item&otra_moneda_det=$otra_moneda&cual_es=2&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Insumos Ejecutados'>%s</a></td>", number_format($producido,$n_cantidad));//producido
	printf("<td align='right'><a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=3&cod_proy_det=$cod_proy&cod_item_det=$cod_item&otra_moneda_det=$otra_moneda&cual_es=3&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Insumos Diferencia'>%s</a></td></tr>", number_format($diferencia,$n_cantidad));//diferencia
	
	$suma_proy = $suma_proy + $proyectado;
	$suma_prod = $suma_prod + $producido;
	$suma_dif = $suma_dif + $diferencia;
	
	$color++;
}//fin while
	
?>
 
<tr BGCOLOR='#ffcccc'><td colspan="2"><b>Total</b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_proy,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_prod,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_dif,$n_cantidad)); ?></td>
</tr>
</table>

<?php

} //fin if agrupar0




} else { //if else modalidad0


//modalidad = 1 es factores

$result = mysqli_query($link, "select fin.codigo as codigo, fin.cod_ord_prod as cod_ord_prod, fin.cod_prod_con as cod_prod_con, fin.cod_gasto as cod_gasto, fin.gasto as gasto, sum(fin.ejecutado) as ejecutado, sum(fin.verdadero) as verdadero, sum(fin.verdadero_depre) as verdadero_depre
from 
(select a.pkyProductionOrderConcept as codigo, a.codProductionOrder as cod_ord_prod, a.codProductionConcept as cod_prod_con, a.codExpensesConcept as cod_gasto, d.fldName as gasto, a.fldAmount as ejecutado, 0 as verdadero, 0 as verdadero_depre
from prdProductionOrderConcept a, prdProductionOrder b, payExpensesConcept d
where 
$valor_fecha5 
$valor_contexto5 
a.codProductionOrder = b.pkyProductionOrder and 
a.codExpensesConcept = d.pkyExpensesConcept and 
a.sysEnabled = 1 and a.sysAuthorized = 1

union all

select t.codigo as codigo, t.cod_ord_prod as cod_ord_prod, t.cod_prod_con as cod_prod_con, t.cod_gasto as cod_gasto, t.gasto as gasto, t.ejecutado as ejecutado, t.verdadero * r.fldRate as verdadero, t.verdadero_depre as verdadero_depre
from 
(select b.pkyAccountsPayableDetail as codigo, 0 as cod_ord_prod, 0 as cod_prod_con, b.codExpensesConcept as cod_gasto, c.fldName as gasto, 0 as ejecutado, 
case when b.codDocumentType = 2 then (b.fldAmount - (b.fldAmount * $monto_iva / 100)) else 
case when b.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) * r1.fldRate as total from txsPurchasesLedger a1, actExchangeRateDetail r1 where a1.pkyPurchasesLedger = b.fldInvoice and a.codExchangeRate = r1.codExchangeRate and $cod_moneda_principal = r1.codTargetCurrency and
a.codCurrency = r1.codBaseCurrency) else b.fldAmount end end as verdadero, 0 as verdadero_depre, a.codCurrency as cod_moneda, a.codExchangeRate as tc, $cod_moneda_principal as otra_moneda
from payAccountsPayable a, payAccountsPayableDetail b, payExpensesConcept c
where 
$valor_fecha6 
$valor_contexto4 
b.codAccountsPayable = a.pkyAccountsPayable and 
b.codExpensesConcept = c.pkyExpensesConcept and
a.fldProductionPeriod > '2000-01-01' and 
b.sysEnabled = 1 and b.sysAuthorized = 1) t, actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all 
select t.codigo as codigo, t.cod_ord_prod as cod_ord_prod, t.cod_prod_con as cod_prod_con, t.cod_gasto as cod_gasto, t.gasto as gasto, t.ejecutado as ejecutado, t.verdadero as verdadero, t.verdadero_depre as verdadero_depre
from 
(select b.pkyAccountsPayableDetail as codigo, 0 as cod_ord_prod, 0 as cod_prod_con, b.codExpensesConcept as cod_gasto, c.fldName as gasto, 0 as ejecutado, 
case when b.codDocumentType = 2 then (b.fldAmount - (b.fldAmount * $monto_iva / 100)) else 
case when b.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) as total from txsPurchasesLedger a1 where a1.pkyPurchasesLedger = b.fldInvoice) else b.fldAmount end end as verdadero, 0 as verdadero_depre, a.codCurrency as cod_moneda, a.codExchangeRate as tc, $cod_moneda_principal as otra_moneda
from payAccountsPayable a, payAccountsPayableDetail b, payExpensesConcept c
where 
$valor_fecha6 
$valor_contexto4 
b.codAccountsPayable = a.pkyAccountsPayable and 
b.codExpensesConcept = c.pkyExpensesConcept and
a.fldProductionPeriod > '2000-01-01' and 
b.sysEnabled = 1 and b.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) fin
group by fin.cod_gasto
order by fin.gasto;");


?>
<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>Factores de Producci&oacute;n</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="50%"><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD width="50%"><b>Agrupado por: </b>Concepto de Gasto</TD>
        </TR>
        <TR>
        <TD><b>Expresado en: </b><?php echo $nom_otra_moneda; ?></TD>
        <TD><b>Contexto: </b><?php echo $nom_contexto; ?></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" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Concepto de Gasto</b></TD>
    <TD align="center"><b>Ejecutado</b></TD>
    <TD align="center"><b>Real</b></TD>
    </TR>
	</thead>
    <tbody class="marcafila">

<?php
$color = 2;
$suma_e = 0;
$suma_r = 0;

while ($row = mysqli_fetch_array($result)) {
	$cod_gasto = $row['cod_gasto'];
	$gasto = $row['gasto'];
	$ejecutado = $row['ejecutado'];
	$verdadero = $row['verdadero'];

	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $cod_gasto);//cod gasto
	} else {
		printf("<tr><td align='center'>%s</td>", $cod_gasto);//cod gasto
	}//cierra if color
	printf("<td align='left'>%s</td>", $gasto);//gasto
	printf("<td align='right'><a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=4&cod_gasto_det=$cod_gasto&otra_moneda_det=$otra_moneda&cual_es=2&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Ejecutado'>%s</a></td>", number_format($ejecutado * $tasa,$n_decimales));//ejecutado
	
	printf("<td align='right'><a href='../../ModuleProduction/ReportProductionProjection/report.php?rep=14&tipo=4&cod_gasto_det=$cod_gasto&otra_moneda_det=$otra_moneda&cual_es=3&fechaini_det=$fechaini&fechafin_det=$fechafin&contexto_det=$cod_contexto&usuario_det=$usuario&token=$token' title='Detalle Real CxP'>%s</a></td></tr>", number_format($verdadero * $tasa,$n_decimales));//verdadero
	
	$suma_e = $suma_e + $ejecutado * $tasa;
	$suma_r = $suma_r + $verdadero * $tasa;
	
	$color++;
}//fin while
	
?>
 
<tr BGCOLOR='#ffcccc'><td colspan="2"><b>Total</b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_e,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_r,$n_decimales)); ?></td>
</tr>
</table>

<?php


} //fin if modalidad0

} //fin if 2



//Detalle reporte proyeccion de produccion para agrupar=0


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

//nombre otra moneda
$resultotra = mysqli_query($link, "SELECT fldName as otra_moneda
from
actCurrency
where
pkyCurrency = '$otra_moneda_det' and
sysEnabled = 1 and sysAuthorized = 1;");
		
	while ($row = mysqli_fetch_array($resultotra)) {
	$nom_otra_moneda_det = $row['otra_moneda'];
	}
	mysqli_free_result($resultotra);
	
//tasa de cambio
if($otra_moneda_det == $cod_moneda_principal) {// if tasa
	$tasa_det = 1;
} else {// else if tasa
$resultcambio = mysqli_query($link, "select fldRate as tasa 
from actExchangeRateDetail 
where 
codExchangeRate = '$tc_hoy' and 
codBaseCurrency = '$otra_moneda_det' and 
codTargetCurrency = '$cod_moneda_principal' and 
sysEnabled = 1 and sysAuthorized = 1;");
		
	while ($row = mysqli_fetch_array($resultcambio)) {
	$tasa_det = $row['tasa'];
	}
	mysqli_free_result($resultcambio);
}// fin if tasa

if ($cod_proy_det == 0) { //if proyeccion
    $valor_proy_det = ' ';
	$valor_proy_det2 = ' ';
	$valor_proy_det3 = ' ';
	$valor_fecha_det = ' p.fldInitDate >= "'.$fechaini_det.'" and p.fldInitDate <= "'.$fechafin_det.'" and';
	$valor_fecha_det2 = ' p1.fldInitDate >= "'.$fechaini_det.'" and p1.fldInitDate <= "'.$fechafin_det.'" and';
	$nom_proy_det = 'Todos';
  } else { 
    $valor_proy_det = ' a.codProductionProjection = '.$cod_proy_det.' and ';
	$valor_proy_det2 = ' b.codProjection = '.$cod_proy_det.' and ';
	$valor_proy_det3 = ' b1.codProjection = '.$cod_proy_det.' and ';
	$valor_fecha_det = ' ';
	$valor_fecha_det2 = ' ';
	//nombre de proyeccion
	$resultpro = mysqli_query($link, "select pkyProductionProjection as codigo, fldDocumentNumber as nro_doc, fldInitDate as fechaini, fldEndDate as fechafin, txtDescription as glosa
from prdProductionProjection
where 
pkyProductionProjection = '$cod_proy_det';");
	while ($row = mysqli_fetch_array($resultpro)) {
	$nom_proy_det = $row['nro_doc'].' - ('.$row['fechaini'].' a '.$row['fechafin'].') - '.$row['glosa'];
	}
	mysqli_free_result($resultpro);
  } //fin if proyeccion

//contexto 
if($cod_contexto_det == -1){ 
	$nom_contexto_det = 'Todos';
	$valor_contexto_det = ' ';
	$valor_contexto_det2 = ' ';
} 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 = ' p.sysContext = '.$cod_contexto_det.' and ';	
	$valor_contexto_det2 = ' p1.sysContext = '.$cod_contexto_det.' and ';	
}


if($cual_es == 1) {//if cual_es=1

$nombre_det = 'Proyectado';
$nro_col = 3;

$result = mysqli_query($link, "select a.pkyProductionProjectionItemExpense as codigo, a.codProductionProjection as cod_proy, a.codProductionConcept as cod_prod_con, c.fldName as con_prod, a.codExpensesConcept as cod_gasto, b.fldName as gasto, a.fldAmount as total
from prdProductionProjectionItemExpense a, payExpensesConcept b, prdProductionConcept c, prdProductionProjection p
where 
$valor_proy_det  
$valor_fecha_det 
$valor_contexto_det 
a.codExpensesConcept = '$cod_gasto_det' and 
a.codExpensesConcept = b.pkyExpensesConcept and 
a.codProductionConcept = c.pkyProductionConcept and 
a.codProductionProjection = p.pkyProductionProjection and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
order by con_prod, codigo;");

}//fin if cual_es=1

if($cual_es == 2) {//if cual_es=2

$nombre_det = 'Ejecutado';
$nro_col = 5;
	
	if($cod_gasto_det == 0) {//if cod_gasto=0
	$result = mysqli_query($link, "select a.pkyProductionOrderConcept as codigo, b.codProjection as cod_proy, a.codProductionOrder as cod_ord_prod, a.codProductionConcept as cod_prod_con, c.fldName as con_prod, 'NO TIENE' as gasto, a.codItem as cod_item, d.fldName as item, a.fldAmount as total
	from prdProductionOrderConcept a, prdProductionOrder b, prdProductionConcept c, invItem d, prdProductionProjection p
	where 
	$valor_proy_det2  
	$valor_fecha_det 
	$valor_contexto_det 
	a.pkyProductionOrderConcept not in (select a1.pkyProductionOrderConcept 
	from prdProductionOrderConcept a1, prdProductionOrder b1, prdProductionProjectionItemExpense c1, payExpensesConcept d1, prdProductionProjection p1
	where 
	$valor_proy_det3 
	$valor_fecha_det2 
	$valor_contexto_det2 
	a1.codProductionOrder = b1.pkyProductionOrder and 
	a1.codProductionConcept = c1.codProductionConcept and 
	b1.codProjection = c1.codProductionProjection and 
	c1.codExpensesConcept = d1.pkyExpensesConcept and 
	b1.codProjection = p1.pkyProductionProjection and 
	a1.sysEnabled = 1 and a1.sysAuthorized = 1) and 
	a.codProductionConcept = c.pkyProductionConcept and 
	a.codItem = d.pkyItem and 
	a.codProductionOrder = b.pkyProductionOrder and 
	b.codProjection = p.pkyProductionProjection and 
	a.sysEnabled = 1 and a.sysAuthorized = 1 
	order by con_prod, codigo;");
	} else {//else if cod_gasto=0
	$result = mysqli_query($link, "select a.pkyProductionOrderConcept as codigo, b.codProjection as cod_proy, a.codProductionOrder as cod_ord_prod, a.codProductionConcept as cod_prod_con, e.fldName as con_prod, c.codExpensesConcept as cod_gasto, d.fldName as gasto, a.codItem as cod_item, f.fldName as item, a.fldAmount as total
	from prdProductionOrderConcept a, prdProductionOrder b, prdProductionProjectionItemExpense c, payExpensesConcept d, prdProductionConcept e, invItem f, prdProductionProjection p
	where 
	$valor_proy_det2 
	$valor_fecha_det 
	$valor_contexto_det 
	c.codExpensesConcept = '$cod_gasto_det' and 
	a.codProductionOrder = b.pkyProductionOrder and 
	a.codProductionConcept = c.codProductionConcept and 
	b.codProjection = c.codProductionProjection and 
	c.codExpensesConcept = d.pkyExpensesConcept and 
	a.codProductionConcept = e.pkyProductionConcept and 
	a.codItem = f.pkyItem and 
	b.codProjection = p.pkyProductionProjection and 
	a.sysEnabled = 1 and a.sysAuthorized = 1 
	order by con_prod, codigo;");
	}//fin if cod_gasto=0

}//fin if cual_es=2

if($cual_es == 3) {//if cual_es=3

$nombre_det = 'Real';
$nro_col = 3;

$result = mysqli_query($link, "select fin.codigo as codigo, fin.cod_proy as cod_proy, fin.cod_gasto as cod_gasto, fin.gasto as gasto, fin.total as total, fin.verdadero as verdadero, fin.papa as papa, fin.cod_papa as cod_papa
from 
(select t.codigo as codigo, t.cod_proy as cod_proy, t.cod_gasto as cod_gasto, t.gasto as gasto, t.total * r.fldRate as total, t.verdadero * r.fldRate as verdadero, t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyAccountsPayable as codigo, a.codProductionProjection as cod_proy, b.codExpensesConcept as cod_gasto, c.fldName as gasto, case when b.codDocumentType = 2 then (b.fldAmount - (b.fldAmount * $monto_iva / 100)) else 
case when b.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) * r1.fldRate as total from txsPurchasesLedger a1, actExchangeRateDetail r1 where a1.pkyPurchasesLedger = b.fldInvoice and a.codExchangeRate = r1.codExchangeRate and $cod_moneda_principal = r1.codTargetCurrency and
a.codCurrency = r1.codBaseCurrency) else b.fldAmount end end as verdadero, b.fldAmount as total, a.codCurrency as cod_moneda, a.codExchangeRate as tc, $cod_moneda_principal as otra_moneda, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableDetail b, payExpensesConcept c, prdProductionProjection p
where 
$valor_proy_det  
$valor_fecha_det 
$valor_contexto_det 
c.pkyExpensesConcept = '$cod_gasto_det' and 
b.codAccountsPayable = a.pkyAccountsPayable and 
b.codExpensesConcept = c.pkyExpensesConcept and
a.codProductionProjection is not null and 
a.codProductionProjection = p.pkyProductionProjection and 
b.sysEnabled = 1 and b.sysAuthorized = 1) t, actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all 
select t.codigo as codigo, t.cod_proy as cod_proy, t.cod_gasto as cod_gasto, t.gasto as gasto, t.total as total, t.verdadero as verdadero, t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyAccountsPayable as codigo, a.codProductionProjection as cod_proy, b.codExpensesConcept as cod_gasto, c.fldName as gasto, case when b.codDocumentType = 2 then (b.fldAmount - (b.fldAmount * $monto_iva / 100)) else 
case when b.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) as total1 from txsPurchasesLedger a1 where a1.pkyPurchasesLedger = b.fldInvoice) else b.fldAmount end end as verdadero, b.fldAmount as total, a.codCurrency as cod_moneda, a.codExchangeRate as tc, $cod_moneda_principal as otra_moneda, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableDetail b, payExpensesConcept c, prdProductionProjection p
where 
$valor_proy_det 
$valor_fecha_det 
$valor_contexto_det 
c.pkyExpensesConcept = '$cod_gasto_det' and 
b.codAccountsPayable = a.pkyAccountsPayable and 
b.codExpensesConcept = c.pkyExpensesConcept and
a.codProductionProjection is not null and 
a.codProductionProjection = p.pkyProductionProjection and 
b.sysEnabled = 1 and b.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) fin 
order by fin.gasto, fin.codigo;");

}//fin if cual_es=3

if($cual_es == 4) {//if cual_es=4

$nombre_det = 'Real (DEP.)';
$nro_col = 3;

$result = mysqli_query($link, "select a.pkyFixedAssetDepreciation as codigo, a.codExpensesConcept as cod_gasto, c.fldName as gasto, a.codProductionProjection as cod_proy, d.fldName as item, b.fldValue as valor 
from 
fasFixedAssetDepreciation a, fasFixedAssetDepreciationDetail b,payExpensesConcept c, fasFixedAsset d, prdProductionProjection p 
where  
$valor_proy_det  
$valor_fecha_det 
$valor_contexto_det 
b.codFixedAssetDepreciation = a.pkyFixedAssetDepreciation and 
a.codExpensesConcept = c.pkyExpensesConcept and 
b.codFixedAsset = d.pkyFixedAsset and 
a.codExpensesConcept = '$cod_gasto_det' and 
a.codProductionProjection = p.pkyProductionProjection and 
b.sysEnabled = 1 and b.sysAuthorized = 1 
order by codigo, item;");

}//fin if cual_es=4

?>
<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><font color="#000099" size="+1"><b>Detalle <?php echo $nombre_det; ?></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> 
        <?php if($cod_proy_det == 0) { ?>
        <TD width="33%"><b>Proyecci&oacute;n: </b><?php echo $nom_proy_det; ?></TD>
        <TD width="33%"><b>De Fecha: </b><?php echo $fechaini_det; ?> <b>a Fecha: </b><?php echo $fechafin_det; ?></TD>
        <?php } else { ?>
        <TD width="33%"><b>Proyecci&oacute;n: </b><a href="../../ModuleProduction/VoucherProductionProjection/index.php?pkyProductionProjection=<?php echo $cod_proy_det; ?>&user=<?php echo $usuario_det; ?>&token=<?php echo $token; ?>" title="Proyecci&oacute;n de Producci&oacute;n"><?php echo $cod_proy_det; ?></a> - <?php echo $nom_proy_det; ?></TD>
        <?php } ?>
        
        <TD width="33%"><b>Expresado en: </b><?php echo $nom_otra_moneda_det; ?></TD>
        </TR>
        <TR>
        <TD colspan="3"><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> 
    <?php if($cual_es == 1) { ?>
 	<TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Concepto de Producci&oacute;n</b></TD>
    <TD align="center"><b>Concepto de Gasto</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <?php } ?>
    <?php if($cual_es == 2) { ?>
 	<TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Concepto de Producci&oacute;n</b></TD>
    <TD align="center"><b>Concepto de Gasto</b></TD>
    <TD align="center"><b>Cod. Item</b></TD>
    <TD align="center"><b>Item</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <?php } ?>
    <?php if($cual_es == 3) { ?>
 	<TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Concepto de Gasto</b></TD>
    <TD align="center" width="10%"><b>Gen. Por</b></TD>
    <TD align="center"><b>Monto con Impuestos</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <?php } ?>
    <?php if($cual_es == 4) { ?>
 	<TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Concepto de Gasto</b></TD>
    <TD align="center"><b>Item Depreciado</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <?php } ?>
    </TR>
	</thead>
    <tbody class="marcafila">

<?php
$color = 2;
$suma_det = 0;
$suma_deti = 0;

while ($row = mysqli_fetch_array($result)) {
		  
  if($cual_es == 1) {//if 1
	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $row['codigo']);//codigo
	} else {
		printf("<tr><td align='center'>%s</td>", $row['codigo']);//codigo
	}//cierra if color
	printf("<td align='left'>%s</td>", $row['con_prod']);//concepto produccion
	printf("<td align='left'>%s</td>", $row['gasto']);//concepto gasto
	printf("<td align='right'>%s</td></tr>", number_format($row['total'] * $tasa_det,$n_decimales));//verdadero
	
	$suma_det = $suma_det + $row['total'] * $tasa_det;
	
	$color++;
  }// fin if 1
  
  if($cual_es == 2) {//if 2
	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $row['codigo']);//codigo
	} else {
		printf("<tr><td align='center'>%s</td>", $row['codigo']);//codigo
	}//cierra if color
	printf("<td align='left'>%s</td>", $row['con_prod']);//concepto produccion
	printf("<td align='left'>%s</td>", $row['gasto']);//concepto gasto
	printf("<td align='right'>%s</td>", $row['cod_item']);//cod item
	printf("<td align='left'>%s</td>", $row['item']);//item
	printf("<td align='right'>%s</td></tr>", number_format($row['total'] * $tasa_det,$n_decimales));//verdadero
	
	$suma_det = $suma_det + $row['total'] * $tasa_det;
	
	$color++;
  }// fin if 2
  
  if($cual_es == 3) {//if 3
  	
	//para enlazar al que genero el asiento
	$papa_componente = $row['papa'];
	$papa_codigo = $row['cod_papa'];
	//$usuario = $usuario_det;
	include "../../generadopor.php";
	
    $cod_cxp = $row['codigo'];
	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$cod_cxp&user=$usuario_det&token=$token' title='Nota de Cuenta por Pagar'>%s</a></td>", $row['codigo']);//codigo
	} else {
		printf("<tr><td align='center'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$cod_cxp&user=$usuario_det&token=$token' title='Nota de Cuenta por Pagar'>%s</a></td>", $row['codigo']);//codigo
	}//cierra if color
	printf("<td align='left'>%s</td>", $row['gasto']);//concepto gasto
	printf("<td align='center'>%s</td>", $papa_enlace);//gen por
	printf("<td align='right'>%s</td>", number_format($row['total'] * $tasa_det,$n_decimales));//con imp.
	printf("<td align='right'>%s</td></tr>", number_format($row['verdadero'] * $tasa_det,$n_decimales));//sin imp. costo
	
	$suma_det = $suma_det + $row['verdadero'] * $tasa_det;
	$suma_deti = $suma_deti + $row['total'] * $tasa_det;
	
	$color++;
  }// fin if 3
  
  if($cual_es == 4) {//if 4
    $cod_dep = $row['codigo'];
	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleFixedAssets/VoucherFixedAssetDepreciation/index.php?pkyFixedAssetDepreciation=$cod_dep&user=$usuario_det&token=$token' title='Nota de Depreciaci&oacute;n de Activo Fijo'>%s</a></td>", $row['codigo']);//codigo
	} else {
		printf("<tr><td align='center'><a href='../../ModuleFixedAssets/VoucherFixedAssetDepreciation/index.php?pkyFixedAssetDepreciation=$cod_dep&user=$usuario_det&token=$token' title='Nota de Depreciaci&oacute;n de Activo Fijo'>%s</a></td>", $row['codigo']);//codigo
	}//cierra if color
	printf("<td align='left'>%s</td>", $row['gasto']);//concepto gasto
	printf("<td align='left'>%s</td>", $row['item']);//item af
	printf("<td align='right'>%s</td></tr>", number_format($row['valor'] * $tasa_det,$n_decimales));//valor dep
	
	$suma_det = $suma_det + $row['valor'] * $tasa_det;
	//$suma_deti = $suma_deti + $row['total'] * $tasa_det;
	
	$color++;
  }// fin if 4
  
}//fin while

mysqli_free_result($result);	
?>
 
<tr BGCOLOR='#ffcccc'><td colspan="<?php echo $nro_col; ?>"><b>Total</b></td>
<?php if($cual_es == 3) { ?>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_deti,$n_decimales)); ?></td>
<?php } ?>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_det,$n_decimales)); ?></td>
</tr>
</table>
         
<?php

} //fin if 3


//Detalle reporte proyeccion de produccion para agrupar=1

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

if ($cod_proy_det == 0) { //if proyeccion
    $valor_proy_det = ' ';
	$valor_fecha_det = ' a.fldInitDate >= "'.$fechaini_det.'" and a.fldInitDate <= "'.$fechafin_det.'" and';
	$nom_proy_det = 'Todos';
  } else { 
    $valor_proy_det = ' a.pkyProductionProjection = '.$cod_proy_det.' and ';
	$valor_fecha_det = ' ';
	//nombre de proyeccion
	$resultpro = mysqli_query($link, "select pkyProductionProjection as codigo, fldDocumentNumber as nro_doc, fldInitDate as fechaini, fldEndDate as fechafin, txtDescription as glosa
from prdProductionProjection
where 
pkyProductionProjection = '$cod_proy_det';");
	while ($row = mysqli_fetch_array($resultpro)) {
	$nom_proy_det = $row['nro_doc'].' - ('.$row['fechaini'].' a '.$row['fechafin'].') - '.$row['glosa'];
	}
	mysqli_free_result($resultpro);
  } //fin if proyeccion
  
//nombre de item
	$resultitem = mysqli_query($link, "select pkyItem as codigo, fldName as item
from invItem
where 
pkyItem = '$cod_item_det';");
	while ($row = mysqli_fetch_array($resultitem)) {
	$nom_item_det = $row['codigo'].' - '.$row['item'];
	}
	mysqli_free_result($resultitem);

//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($cual_es == 1) {//if cual_es=1

$nombre_det = 'Proyectados';
$nro_col = 2;

$result = mysqli_query($link, "select t.cod_item as cod_item, t.item as item, sum(t.cant_proy) as cant_proy, sum(t.cant_receta) as cant_receta, sum(t.cant_necesaria) as cant_necesaria
from
(select a.pkyProductionProjection as cod_proy, a.fldDocumentNumber as nro_doc_proy, a.fldUsed as usada_proy, d.codItem as cod_item, i.fldName as item, b.fldAmount as cant_proy, d.fldQuantity as cant_receta, b.fldAmount * d.fldQuantity as cant_necesaria
from
prdProductionProjection a, prdProductionProjectionItem b, prdRecipe c, prdRecipeDetail d, invItem i 
where 
$valor_proy_det  
$valor_fecha_det 
$valor_contexto_det 
a.pkyProductionProjection = b.codProductionProjection and 
b.codItem = '$cod_item_det' and 
c.codItem = '$cod_item_det' and 
c.pkyRecipe = d.codRecipe and 
d.codItem = i.pkyItem and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
group by t.cod_item 
order by t.item;");

$mat_cant = array ();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$mat_cant[$i][1] = $row['cod_item'];
	$mat_cant[$i][2] = $row['item'];
	$mat_cant[$i][3] = $row['cant_proy'];
	$mat_cant[$i][4] = $row['cant_receta'];
	$mat_cant[$i][5] = $row['cant_necesaria'];
	$i++;
}
mysqli_free_result($result);	
$fin_i = $i - 1;

}//fin if cual_es=1

if($cual_es == 2) {//if cual_es=2

$nombre_det = 'Producidos';
$nro_col = 2;

$result = mysqli_query($link, "select t.cod_item as cod_item, t.item as item, sum(t.cant_prod) as cant_prod, sum(t.cant_receta) as cant_receta, sum(t.cant_eje) as cant_eje
from
(select e.pkyProductionOrder as cod_prod, e.fldDocumentNumber as nro_doc_prod, d.codItem as cod_item, i.fldName as item, b.fldQuantity as cant_prod, d.fldQuantity as cant_receta, b.fldQuantity * d.fldQuantity as cant_eje
from
prdProductionOrder e, prdProductionOrderDetail b, prdRecipe c, prdRecipeDetail d, invItem i, prdProductionProjection a 
where 
$valor_proy_det  
$valor_fecha_det 
$valor_contexto_det 
a.pkyProductionProjection = e.codProjection and  
e.codProjection is not null and 
e.pkyProductionOrder = b.codProductionOrder and 
b.codItem = '$cod_item_det' and 
c.codItem = '$cod_item_det' and 
c.pkyRecipe = d.codRecipe and 
d.codItem = i.pkyItem and 
b.codRecipe = c.pkyRecipe and 
e.sysEnabled = 1 and e.sysAuthorized = 1) t
group by t.cod_item 
order by t.item;");

$mat_cant = array ();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$mat_cant[$i][1] = $row['cod_item'];
	$mat_cant[$i][2] = $row['item'];
	$mat_cant[$i][3] = $row['cant_prod'];
	$mat_cant[$i][4] = $row['cant_receta'];
	$mat_cant[$i][5] = $row['cant_eje'];
	$i++;
}
mysqli_free_result($result);	
$fin_i = $i - 1;

}//fin if cual_es=2

if($cual_es == 3) {//if cual_es=3

$nombre_det = 'Diferencia';
$nro_col = 2;

$result = mysqli_query($link, "select t.cod_item as cod_item, t.item as item, sum(t.cant_proy) as cant_proy, sum(t.cant_necesaria) as cant_necesaria, sum(t.cant_prod) as cant_prod, sum(t.cant_eje) as cant_eje
from
(select d.codItem as cod_item, i.fldName as item, b.fldAmount as cant_proy, 0 as cant_prod, d.fldQuantity as cant_receta, b.fldAmount * d.fldQuantity as cant_necesaria, 0 as cant_eje
from
prdProductionProjection a, prdProductionProjectionItem b, prdRecipe c, prdRecipeDetail d, invItem i 
where 
$valor_proy_det  
$valor_fecha_det 
$valor_contexto_det 
a.pkyProductionProjection = b.codProductionProjection and 
b.codItem = '$cod_item_det' and 
c.codItem = '$cod_item_det' and 
c.pkyRecipe = d.codRecipe and 
d.codItem = i.pkyItem and 
a.sysEnabled = 1 and a.sysAuthorized = 1
union all
select d.codItem as cod_item, i.fldName as item, 0 as cant_proy, b.fldQuantity as cant_prod, d.fldQuantity as cant_receta, 0 as cant_necesaria, b.fldQuantity * d.fldQuantity as cant_eje
from
prdProductionOrder e, prdProductionOrderDetail b, prdRecipe c, prdRecipeDetail d, invItem i, prdProductionProjection a 
where 
$valor_proy_det  
$valor_fecha_det 
$valor_contexto_det 
a.pkyProductionProjection = e.codProjection and  
e.codProjection is not null and 
e.pkyProductionOrder = b.codProductionOrder and 
b.codItem = '$cod_item_det' and 
c.codItem = '$cod_item_det' and 
c.pkyRecipe = d.codRecipe and 
d.codItem = i.pkyItem and 
b.codRecipe = c.pkyRecipe and 
e.sysEnabled = 1 and e.sysAuthorized = 1) t
group by t.cod_item 
order by t.item;");

$mat_cant = array ();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$mat_cant[$i][1] = $row['cod_item'];
	$mat_cant[$i][2] = $row['item'];
	$mat_cant[$i][3] = $row['cant_proy'];
	$mat_cant[$i][4] = $row['cant_necesaria'];
	$mat_cant[$i][5] = $row['cant_prod'];
	$mat_cant[$i][6] = $row['cant_eje'];
	$i++;
}
mysqli_free_result($result);	
$fin_i = $i - 1;

}//fin if cual_es=3


?>
<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><font color="#000099" size="+1"><b>Detalle Insumos <?php echo $nombre_det; ?></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="33%"><b>Item Proyectado: </b><?php echo $nom_item_det; ?></TD>
        <?php if($cual_es == 1 || $cual_es == 2) { ?>
        <TD width="33%"><b>Cantidad: </b><?php echo number_format($mat_cant[1][3], $n_cantidad); ?></TD>
        <?php } else { ?>
        <TD width="33%"><b>Cant. Proy.: </b><?php echo number_format($mat_cant[1][3], $n_cantidad); ?> - <b>Cant. Prod.: </b><?php echo number_format($mat_cant[1][5],$n_cantidad); ?> - <b>Cant. Dif.: </b><?php echo number_format($mat_cant[1][3] - $mat_cant[1][5],$n_cantidad); ?></TD>
        <?php } ?>
		<?php if($cod_proy_det == 0) { ?>
        <TD width="33%"><b>Proyecci&oacute;n: </b><?php echo $nom_proy_det; ?></TD>
        <TD width="33%"><b>De Fecha: </b><?php echo $fechaini_det; ?> <b>a Fecha: </b><?php echo $fechafin_det; ?></TD>
        <?php } else { ?>
        <TD width="33%"><b>Proyecci&oacute;n: </b><a href="../../ModuleProduction/VoucherProductionProjection/index.php?pkyProductionProjection=<?php echo $cod_proy_det; ?>&user=<?php echo $usuario_det; ?>&token=<?php echo $token; ?>" title="Proyecci&oacute;n de Producci&oacute;n"><?php echo $cod_proy_det; ?></a> - <?php echo $nom_proy_det; ?></TD>
        <?php } ?>
        
        </TR>
        <TR>
        <TD colspan="3"><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> 
    <?php if($cual_es == 1) { ?>
 	<TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Item Insumo</b></TD>
    <!-- no la uso porque suma si hay varias recetas o varias combinaciones en proyecciones por lo que la multiplicación no cuadra 
    <TD align="center"><b>Cantidad Insumo(s) Receta(s) (Referencial)</b></TD>
    <TD align="center"><b>Cantidad Proyectada</b></TD>
    -->
    <TD align="center"><b>Cantidad Necesaria</b></TD>
    <?php } ?>
    <?php if($cual_es == 2) { ?>
 	<TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Item Insumo</b></TD>
    <!-- no la uso porque suma si hay varias recetas o varias combinaciones en proyecciones por lo que la multiplicación no cuadra 
    <TD align="center"><b>Cantidad Insumo(s) Receta(s) (Referencial)</b></TD>
    <TD align="center"><b>Cantidad Producida</b></TD>
    -->
    <TD align="center"><b>Cantidad Usada</b></TD>
    <?php } ?>
    <?php if($cual_es == 3) { ?>
 	<TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Item Insumo</b></TD>
    <TD align="center"><b>Cant. Proyectada</b></TD>
    <TD align="center"><b>Cant. Usada</b></TD>
    <TD align="center"><b>Cant. Diferencia</b></TD>
    <?php } ?>
    </TR>
	</thead>
    <tbody class="marcafila">

<?php
$color = 2;
//$suma_receta = 0;
$suma_necesaria = 0;
$suma_ejecutada = 0;
$suma_diferencia = 0;

for ($i = 1; $i <= $fin_i; $i++) {//for p
  
  if($cual_es == 1 || $cual_es == 2) {//if 1
	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $mat_cant[$i][1]);//codigo item
	} else {
		printf("<tr><td align='center'>%s</td>", $mat_cant[$i][1]);//codigo item
	}//cierra if color
	printf("<td align='left'>%s</td>", $mat_cant[$i][2]);//item insumo
	//printf("<td align='right'>%s</td>", number_format($mat_cant[$i][3],$n_cantidad));//cant receta
	//printf("<td align='right'>%s</td>", number_format($mat_cant[$i][4],$n_cantidad));//cant proyectada o producida
	printf("<td align='right'>%s</td>", number_format($mat_cant[$i][5],$n_cantidad));//cant necesaria o usada
	
	//$suma_receta = $suma_receta + $mat_cant[$i][3];
	$suma_necesaria = $suma_necesaria + $mat_cant[$i][5];
	$suma_ejecutada = $suma_ejecutada + $mat_cant[$i][5];
	
	$color++;
  }// fin if 1
  
  
  
  if($cual_es == 3) {//if 3
    if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $mat_cant[$i][1]);//codigo item
	} else {
		printf("<tr><td align='center'>%s</td>", $mat_cant[$i][1]);//codigo item
	}//cierra if color
	printf("<td align='left'>%s</td>", $mat_cant[$i][2]);//item insumo
	printf("<td align='right'>%s</td>", number_format($mat_cant[$i][4],$n_cantidad));//cant proyectada
	printf("<td align='right'>%s</td>", number_format($mat_cant[$i][6],$n_cantidad));//cant usada
	printf("<td align='right'>%s</td>", number_format($mat_cant[$i][4] - $mat_cant[$i][6],$n_cantidad));//diferencia
	
	$suma_necesaria = $suma_necesaria + $mat_cant[$i][4];
	$suma_ejecutada = $suma_ejecutada + $mat_cant[$i][6];
	$suma_diferencia = $suma_diferencia + $mat_cant[$i][4] - $mat_cant[$i][6];
	
	$color++;
  }// fin if 3
  
  
}//fin for p

?>
 
<tr BGCOLOR='#ffcccc'><td colspan="<?php echo $nro_col; ?>"><b>Total</b></td>
<?php if($cual_es == 1) { ?>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_necesaria,$n_cantidad)); ?></td>
<?php } ?>
<?php if($cual_es == 2) { ?>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_ejecutada,$n_cantidad)); ?></td>
<?php } ?>
<?php if($cual_es == 3) { ?>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_necesaria,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_ejecutada,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_diferencia,$n_cantidad)); ?></td>
<?php } ?>
</tr>
</table>
         
<?php

} //fin if 4


//Detalle reporte proyeccion de produccion para factores


if($rep == 14 && $tipo == 4) { // if 5

//nombre otra moneda
$resultotra = mysqli_query($link, "SELECT fldName as otra_moneda
from
actCurrency
where
pkyCurrency = '$otra_moneda_det' and
sysEnabled = 1 and sysAuthorized = 1;");
		
	while ($row = mysqli_fetch_array($resultotra)) {
	$nom_otra_moneda_det = $row['otra_moneda'];
	}
	mysqli_free_result($resultotra);
	
//tasa de cambio
if($otra_moneda_det == $cod_moneda_principal) {// if tasa
	$tasa_det = 1;
} else {// else if tasa
$resultcambio = mysqli_query($link, "select fldRate as tasa 
from actExchangeRateDetail 
where 
codExchangeRate = '$tc_hoy' and 
codBaseCurrency = '$otra_moneda_det' and 
codTargetCurrency = '$cod_moneda_principal' and 
sysEnabled = 1 and sysAuthorized = 1;");
		
	while ($row = mysqli_fetch_array($resultcambio)) {
	$tasa_det = $row['tasa'];
	}
	mysqli_free_result($resultcambio);
}// fin if tasa


//contexto 
if($cod_contexto_det == -1){ 
	$nom_contexto_det = 'Todos';
	$valor_contexto_det = ' ';
	$valor_contexto_det2 = ' ';
} 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 = ' b.sysContext = '.$cod_contexto_det.' and ';	
	$valor_contexto_det2 = ' a.sysContext = '.$cod_contexto_det.' and ';	
}

$valor_fecha_det = ' b.fldDate >= "'.$fechaini_det.'" and b.fldDate <= "'.$fechafin_det.'" and';
$valor_fecha_det2 = ' a.fldProductionPeriod >= "'.$fechaini_det.'" and a.fldProductionPeriod <= "'.$fechafin_det.'" and';

if($cual_es == 2) {//if cual_es=2

$nombre_det = 'Ejecutado';
$nro_col = 5;
	
	$result = mysqli_query($link, "select a.pkyProductionOrderConcept as codigo, a.codProductionOrder as cod_ord_prod, a.codProductionConcept as cod_prod_con, e.fldName as con_prod, a.codExpensesConcept as cod_gasto, d.fldName as gasto, a.codItem as cod_item, f.fldName as item, a.fldAmount as total
from prdProductionOrderConcept a, prdProductionOrder b, payExpensesConcept d, prdProductionConcept e, invItem f
where 
$valor_fecha_det 
$valor_contexto_det 
a.codExpensesConcept = '$cod_gasto_det' and 
a.codProductionOrder = b.pkyProductionOrder and 
a.codProductionConcept = e.pkyProductionConcept and 
a.codItem = f.pkyItem and 
a.codExpensesConcept = d.pkyExpensesConcept and 
a.sysEnabled = 1 and a.sysAuthorized = 1
	order by con_prod, codigo;");

}//fin if cual_es=2

if($cual_es == 3) {//if cual_es=3

$nombre_det = 'Real';
$nro_col = 3;

$result = mysqli_query($link, "select fin.codigo as codigo, fin.cod_gasto as cod_gasto, fin.gasto as gasto, fin.total as total, fin.verdadero as verdadero, fin.papa as papa, fin.cod_papa as cod_papa
from 
(select t.codigo as codigo, t.cod_gasto as cod_gasto, t.gasto as gasto, t.total * r.fldRate as total, t.verdadero * r.fldRate as verdadero, t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyAccountsPayable as codigo, a.codProductionProjection as cod_proy, b.codExpensesConcept as cod_gasto, c.fldName as gasto, case when b.codDocumentType = 2 then (b.fldAmount - (b.fldAmount * $monto_iva / 100)) else 
case when b.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) * r1.fldRate as total from txsPurchasesLedger a1, actExchangeRateDetail r1 where a1.pkyPurchasesLedger = b.fldInvoice and a.codExchangeRate = r1.codExchangeRate and 1 = r1.codTargetCurrency and
a.codCurrency = r1.codBaseCurrency) else b.fldAmount end end as verdadero, b.fldAmount as total, a.codCurrency as cod_moneda, a.codExchangeRate as tc, $cod_moneda_principal as otra_moneda, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableDetail b, payExpensesConcept c
where 
$valor_fecha_det2 
$valor_contexto_det2 
c.pkyExpensesConcept = '$cod_gasto_det' and 
b.codAccountsPayable = a.pkyAccountsPayable and 
b.codExpensesConcept = c.pkyExpensesConcept and
a.fldProductionPeriod > '2000-01-01' and 
b.sysEnabled = 1 and b.sysAuthorized = 1) t, actExchangeRateDetail r
where
t.tc = r.codExchangeRate and
t.cod_moneda = r.codTargetCurrency and
t.otra_moneda = r.codBaseCurrency
union all 
select t.codigo as codigo, t.cod_gasto as cod_gasto, t.gasto as gasto, t.total as total, t.verdadero as verdadero, t.papa as papa, t.cod_papa as cod_papa
from 
(select a.pkyAccountsPayable as codigo, b.codExpensesConcept as cod_gasto, c.fldName as gasto, case when b.codDocumentType = 2 then (b.fldAmount - (b.fldAmount * $monto_iva / 100)) else 
case when b.codDocumentType = 1 then (select (a1.fldGrossTotal - a1.fldTaxCredit) as total1 from txsPurchasesLedger a1 where a1.pkyPurchasesLedger = b.fldInvoice) else b.fldAmount end end as verdadero, b.fldAmount as total, a.codCurrency as cod_moneda, a.codExchangeRate as tc, $cod_moneda_principal as otra_moneda, a.fldParentComponent as papa, a.cosComponentKey as cod_papa
from payAccountsPayable a, payAccountsPayableDetail b, payExpensesConcept c
where 
$valor_fecha_det2 
$valor_contexto_det2  
c.pkyExpensesConcept = '$cod_gasto_det' and 
b.codAccountsPayable = a.pkyAccountsPayable and 
b.codExpensesConcept = c.pkyExpensesConcept and
a.fldProductionPeriod > '2000-01-01' and 
b.sysEnabled = 1 and b.sysAuthorized = 1) t
where
t.cod_moneda = t.otra_moneda) fin 
order by fin.gasto, fin.codigo;");

}//fin if cual_es=3


?>
<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><font color="#000099" size="+1"><b>Detalle <?php echo $nombre_det; ?> (Factores)</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="33%"><b>De Fecha: </b><?php echo $fechaini_det; ?> <b>a Fecha: </b><?php echo $fechafin_det; ?></TD>
        <TD width="33%"><b>Expresado en: </b><?php echo $nom_otra_moneda_det; ?></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> 
    <?php if($cual_es == 2) { ?>
 	<TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Concepto de Producci&oacute;n</b></TD>
    <TD align="center"><b>Concepto de Gasto</b></TD>
    <TD align="center"><b>Cod. Item</b></TD>
    <TD align="center"><b>Item</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <?php } ?>
    <?php if($cual_es == 3) { ?>
 	<TD align="center" width="10%"><b>Cod.</b></TD>
    <TD align="center"><b>Concepto de Gasto</b></TD>
    <TD align="center" width="10%"><b>Gen. Por</b></TD>
    <TD align="center"><b>Monto con Impuestos</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <?php } ?>
    </TR>
	</thead>
    <tbody class="marcafila">

<?php
$color = 2;
$suma_det = 0;
$suma_deti = 0;

while ($row = mysqli_fetch_array($result)) {
 
  
  if($cual_es == 2) {//if 2
	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $row['codigo']);//codigo
	} else {
		printf("<tr><td align='center'>%s</td>", $row['codigo']);//codigo
	}//cierra if color
	printf("<td align='left'>%s</td>", $row['con_prod']);//concepto produccion
	printf("<td align='left'>%s</td>", $row['gasto']);//concepto gasto
	printf("<td align='right'>%s</td>", $row['cod_item']);//cod item
	printf("<td align='left'>%s</td>", $row['item']);//item
	printf("<td align='right'>%s</td></tr>", number_format($row['total'] * $tasa_det,$n_decimales));//verdadero
	
	$suma_det = $suma_det + $row['total'] * $tasa_det;
	
	$color++;
  }// fin if 2
  
  if($cual_es == 3) {//if 3
  	
	//para enlazar al que genero el asiento
	$papa_componente = $row['papa'];
	$papa_codigo = $row['cod_papa'];
	//$usuario = $usuario_det;
	include "../../generadopor.php";
	
    $cod_cxp = $row['codigo'];
	if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$cod_cxp&user=$usuario_det&token=$token' title='Nota de Cuenta por Pagar'>%s</a></td>", $row['codigo']);//codigo
	} else {
		printf("<tr><td align='center'><a href='../../ModulePayments/VoucherAccountsPayable/index.php?pkyAccountsPayable=$cod_cxp&user=$usuario_det&token=$token' title='Nota de Cuenta por Pagar'>%s</a></td>", $row['codigo']);//codigo
	}//cierra if color
	printf("<td align='left'>%s</td>", $row['gasto']);//concepto gasto
	printf("<td align='center'>%s</td>", $papa_enlace);//gen por
	printf("<td align='right'>%s</td>", number_format($row['total'] * $tasa_det,$n_decimales));//con imp.
	printf("<td align='right'>%s</td></tr>", number_format($row['verdadero'] * $tasa_det,$n_decimales));//sin imp. costo
	
	$suma_det = $suma_det + $row['verdadero'] * $tasa_det;
	$suma_deti = $suma_deti + $row['total'] * $tasa_det;
	
	$color++;
  }// fin if 3
  
}//fin while

mysqli_free_result($result);	
?>
 
<tr BGCOLOR='#ffcccc'><td colspan="<?php echo $nro_col; ?>"><b>Total</b></td>
<?php if($cual_es == 3) { ?>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_deti,$n_decimales)); ?></td>
<?php } ?>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_det,$n_decimales)); ?></td>
</tr>
</table>
         
<?php

} //fin if 5


?>
</table>

</body>
</html>
