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

</head>
<?php

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

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


?>
<body>

	<?php
	
//Inicio reporte compras por Item


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

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


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

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


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

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

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

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

union all 

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


?>
<table border="0" align="left" width="100%" cellpadding="5">
<tr><td><?php echo $mostrar_logo_reportes; ?>
</td><td align="right"><b>Fecha: <?php echo date("Y-m-d"); ?><br/>Hora: <?php echo date("H:i:s"); ?><br/>Usuario: <?php echo $usuario; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Ordenes de Compra</b></font></center></p>
<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>	
    <table border="1" align="center" cellpadding="2" width="100%">
        <TR> 
        <TD width="25%"><b>Proveedor: </b><?php echo $nom_proveedor; ?></TD>
        <TD width="25%"><b>Almac&eacute;n: </b><?php echo $nom_almacen; ?></TD>
        <TD width="25%"><b>Estado: </b><?php echo $nom_estado; ?></TD>
        <TD width="25%"><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
        </TR>
        <TR>
        <TD colspan="2"><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>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>Almac&eacute;n</b></TD>
    <TD align="center"><b>Estado</b></TD>
    <TD align="center"><b>Glosa</b></TD>
    <TD align="center"><b>Mon.</b></TD>
    <TD align="center"><b>Total Bruto</b></TD>
    <TD align="center"><b>Desc. Parcial (%)</b></TD>
    <TD align="center"><b>Desc. Global (%)</b></TD>
    <TD align="center"><b>Total Neto</b></TD>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Total Neto en <?php echo $nom_otra_moneda; ?></b></TD>
    </TR>
	</thead>
    <tbody class="marcafila">

<?php

$color = 2;
$suma_total = 0;

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

</body>
</html>
