<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Factura 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 "../../class.nroenletraver2.php";
include "../../general.php";
include "../../token.php";

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

$cod_fac = $_GET["pkyPurchasesLedger"];
//$mostrar_asiento_cc = $_GET["showCC"];
//$usuario = $_GET["user"];

$n_decimales = 2;
?>
<body>

<?php

// Inicio impresion factura

$result = mysqli_query($link, "select a.pkyPurchasesLedger as cod_fac, a.fldNumber as numero, a.fldAuthorization as nro_auto, a.fldDate as fecha, a.fldControlCode as cod_control, a.codSupplier as cod_prov, b.fldName as proveedor, a.fldName as nombre, a.fldNIT as nit, a.fldGrossTotal as total_bruto, a.fldICE as total_ice, a.fldExempt as excento, a.fldNetTotal as total_neto, a.fldTaxCredit as cred_fiscal, a.fldNetTotalAlternative as total5, a.fldTaxCreditAlternative as credito5, a.fldEmissionDate as fecha_emision, a.fldDiscount as descuento, a.codType as cod_tipo_compra, c.fldName as tipo_compra, a.txtDescription as glosa, a.sysEnabled as enabled, a.sysAuthorized as authorized, g.fldName as usuario_reg, a.codCurrency as cod_moneda, d.fldName as moneda,  a.codExchangeRate as cod_tc, a.fldParentComponent as papa, a.cosComponentKey as cod_papa, a.sysContext as cod_contexto, s.fldName as contexto 
from
txsPurchasesLedger a, agnCompany b, txsPurchaseInvoiceType c, actCurrency d, accGroup g, srvContext s
where 
a.pkyPurchasesLedger = '$cod_fac' and 
a.codSupplier = b.pkyCompany and 
a.codType = c.pkyPurchaseInvoiceType and 
a.codCurrency = d.pkyCurrency and 
a.sysContext = s.pkyContext and 
a.sysAuthorizationGroup = g.pkyGroup;");
	while ($row = mysqli_fetch_array($result)) {
		$cod_fac = $row['cod_fac'];
		$numero = $row['numero'];
		$nro_auto = $row['nro_auto'];
		$fecha = $row['fecha'];
		$cod_control = $row['cod_control'];
		$cod_prov = $row['cod_prov'];
		$proveedor = $row['proveedor'];
		$nombre = $row['nombre'];
		$nit = $row['nit'];
		$total_bruto = $row['total_bruto'];
		$total_ice = $row['total_ice'];
		$excento = $row['excento'];
		$total_neto = $row['total_neto'];
		$cred_fiscal = $row['cred_fiscal'];
		$descuento = $row['descuento'];
		$cod_tipo_compra = $row['cod_tipo_compra'];
		$tipo_compra = $row['tipo_compra'];
		$glosa = $row['glosa'];
		$glosa = nl2br($glosa);//quita salto de linea 
		$glosa = str_replace('\n','<br />',$glosa);//reemplaza para salto de linea
		$usuario_reg = $row['usuario_reg'];
		$enabled = $row['enabled'];
		$authorized = $row['authorized'];
		$total5 = $row['total5'];
		$credito5 = $row['credito5'];
		$fecha_emision = $row['fecha_emision'];
		$cod_contexto = $row['cod_contexto'];
		$contexto = $row['contexto'];
		
		$cod_moneda2 = $row['cod_moneda'];
		$moneda2 = $row['moneda'];
		$cod_tc2 = $row['cod_tc'];
		
		//para enlazar al que genero el asiento
		$papa_componente = $row['papa'];
		$papa_codigo = $row['cod_papa'];
		//$usuario = $usuario_det;
		include "../../generadopor.php";
		
	}
	mysqli_free_result($result);
	
//tipo de cambio para la cabecera
$resulttc = mysqli_query($link, "select pkyExchangeRate as cod_tc, fldDate as fecha, fldTime as hora
from
actExchangeRate
where
pkyExchangeRate = '$cod_tc2' and 
sysEnabled = 1 and sysAuthorized = 1;");
while ($row = mysqli_fetch_array($resulttc)) {
	$tc_fecha = $row['fecha'];
	$tc_hora = $row['hora'];
}
mysqli_free_result($resulttc);

$resulttcdet = mysqli_query($link, "select a.pkyExchangeRateDetail as cod_tc_det, a.codBaseCurrency as cod_mon_target, b.fldName as mon_target, b.txtDescription as nom_mon_target, a.codTargetCurrency as cod_mon_base, c.fldName as mon_base, c.txtDescription as nom_mon_base, a.fldRate as tasa
from
actExchangeRateDetail a, actCurrency b, actCurrency c
where
a.codExchangeRate = '$cod_tc2' and 
a.codBaseCurrency = '$cod_moneda_principal' and 
a.codBaseCurrency = b.pkyCurrency and 
a.codTargetCurrency = c.pkyCurrency 
order by cod_mon_target, cod_mon_base;");
$ma_tc = array();
$m = 1;
while ($row = mysqli_fetch_array($resulttcdet)) {
	$ma_tc [$m][1] = $row['mon_base'];
	$ma_tc [$m][2] = $row['mon_target'];
	$ma_tc [$m][3] = $row['tasa'];
	$m++;
}
mysqli_free_result($resulttcdet);
$fin_m = $m - 1;
	

?>
<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>Factura de compra</b></font></center></p>
<?php if($mi_cod_pais == 1) { //if bolivia ?>
<table border="1" align="center" cellpadding="2" width="100%">
    <TR> 
    <TD width="30%"><b>Cod. Reg.: </b><?php echo $cod_fac; ?></TD>
    <TD width="30%"><b>Proveedor: </b><?php echo $proveedor; ?></TD>
    <TD width="40%"><b>Fecha: </b><?php echo $fecha; ?></TD>
    </TR>
    <TR>
    <TD><b>N&uacute;mero: </b><?php echo $numero; ?></TD>
    <TD><b>Autorizaci&oacute;n / CUF: </b><?php echo $nro_auto; ?></TD>
    <TD><b>C&oacute;digo de Control: </b><?php echo $cod_control; ?></TD>
    </TR>
    <TR>
    <TD><b>Nombre: </b><?php echo $nombre; ?></TD>
    <TD><b>NIT: </b><?php echo $nit; ?></TD>
    <TD><b>Total Bruto: </b><?php echo number_format($total_bruto,$n_decimales); ?></TD>
    </TR>
    <TR>
    <TD><b>Total Neto: </b><?php echo number_format($total_neto,$n_decimales); ?></TD>
    <TD><b>Cr&eacute;dito Fiscal: </b><?php echo number_format($cred_fiscal,$n_decimales); ?></TD>
    <TD><b>Descuento: </b><?php echo number_format($descuento,$n_decimales); ?></TD>
    </TR>
    <TR>
    <TD><b>ICE: </b><?php echo number_format($total_ice,$n_decimales); ?></TD>
    <TD><b>Exento: </b><?php echo number_format($excento,$n_decimales); ?></TD>
    <TD><b>Generado Por: </b><?php echo $papa_enlace; ?></TD>
    </TR>
    <TR>
    <TD colspan="3"><b>Tipo de compra: </b><?php echo $cod_tipo_compra; ?> - <?php echo $tipo_compra; ?></TD>
    </TR>
    <TR>
    <TD colspan="3"><b>Contexto: </b><?php echo $cod_contexto; ?> - <?php echo $contexto; ?></TD>
    </TR>
    <TR>
    <TD colspan="3"><b>Glosa: </b><?php echo $glosa; ?></TD>
    </TR>
</table>
<?php } //fin if bolivia ?>

<?php if($mi_cod_pais == 176) { //if paraguay ?>
<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>
<table border="1" align="center" cellpadding="2" width="100%">
    <TR> 
    <TD width="30%"><b>Cod. Reg.: </b><?php echo $cod_fac; ?></TD>
    <TD width="60%" colspan="2"><b>Proveedor: </b><?php echo $proveedor; ?></TD>
    </TR>
    <TR>
    <TD><b>Fecha: </b><?php echo $fecha; ?></TD>
    <TD><b>Nombre: </b><?php echo $nombre; ?></TD>
    <TD><b>RUC: </b><?php echo $nit; ?></TD>
    </TR>
    <TR>
    <TD><b>N&uacute;mero: </b><?php echo $numero; ?></TD>
    <TD><b>Total Bruto: </b><?php echo number_format($total_bruto,$n_decimales); ?></TD>
    <TD><b>Generado Por: </b><?php echo $papa_enlace; ?></TD>
    </TR>
    <TR>
    <TD><b>Timbrado: </b><?php echo $nro_auto; ?></TD>
    <TD><b>Exento: </b><?php echo number_format($excento,$n_decimales); ?></TD>
    <TD><b>Vencimiento: </b><?php echo $fecha_emision; ?></TD>
    </TR>
    <TR>
    </TR>
    <TR>
    <TD colspan="2"><b>Gravable 10%: </b><?php echo number_format($total_neto,$n_decimales); ?></TD>
    <TD><b>Impuesto 10%: </b><?php echo number_format($cred_fiscal,$n_decimales); ?></TD>
    </TR>
    <TR>
    <TD colspan="2"><b>Gravable 5%: </b><?php echo number_format($total5,$n_decimales); ?></TD>
    <TD><b>Impuesto 5%: </b><?php echo number_format($credito5,$n_decimales); ?></TD>
    </TR>
    <TR>
    <TD colspan="2"><b>Moneda: </b><?php echo $moneda2; ?></TD>
	<TD><b>Total Impuesto: </b><?php echo number_format($cred_fiscal+$credito5,$n_decimales); ?></TD>
    </TR>
    <TR>
    <TD colspan="3"><b>Tipo de compra: </b><?php echo $cod_tipo_compra; ?> - <?php echo $tipo_compra; ?></TD>
    <TR>
    <TD colspan="3"><b>Contexto: </b><?php echo $cod_contexto; ?> - <?php echo $contexto; ?></TD>
    </TR>
    <TD colspan="3"><b>Glosa: </b><?php echo $glosa; ?></TD>
    </TR>
</table>
</td>

<td valign="top" width="150">

    <table border="1" align="center" cellpadding="2" width="100%">
    <tr>
    <TD><b>Tipo de Cambio:</b></TD>
    </tr><tr>
    <TD align="center"><a href="../../ModuleAccounting/VoucherExchangeRate/index.php?pkyExchangeRate=<?php echo $cod_tc2; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Tipo de Cambio"><?php echo $cod_tc2; ?></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></tr>", 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>
<?php } //fin if paraguay ?>
<br />
<?php
    if($enabled == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>REGISTRO ANULADO</font></td></tr></table>");
			}
	if($authorized == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>REGISTRO PENDIENTE DE AUTORIZACI&Oacute;N</font></td></tr></table>");
			}

?>

<br /><br /><br /><br /><br />
<table border="0" align="center" cellpadding="2" width="90%">
<tr>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
</tr><tr>
<td align="center">Elaborado por: <?php echo $usuario_reg; ?></td>
<td align="center"><?php echo $firma_contabilidad; ?></td>
<td align="center"><?php echo $firma_gerencia; ?></td>
<td align="center"><?php echo $firma_interesado; ?></td>
</tr><tr>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
</tr><tr>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
</tr>
</table>

<?php


?>
</body>
</html>
