<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Empresas</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"];
$empresa=$_POST["empresa"];
$nit=$_POST["nit"];
$rubro=$_POST["rubro"];
$tipoi=$_POST["tipoi"];
$ciudad=$_POST["ciudad"];
$vendedor=$_POST["vendedor"];
$cliente_proveedor=$_POST["cliente_proveedor"];
$canal=$_POST["canal"];
$orden=$_POST["orden"];
//$usuario=$_POST["usuario"];

$ciudad_ver=$_POST["ciudad_ver"];
$cod_alt_ver=$_POST["cod_alt_ver"];
$tipo_ver=$_POST["tipo_ver"];
$rubro_ver=$_POST["rubro_ver"];
$web_ver=$_POST["web_ver"];
$casilla_ver=$_POST["casilla_ver"];
$totalventa_ver=$_POST["totalventa_ver"];
$vendedor_ver=$_POST["vendedor_ver"];
$listaprecio_ver=$_POST["listaprecio_ver"];
$diascred_ver=$_POST["diascred_ver"];
$diasfac_ver=$_POST["diasfac_ver"];
$desc_ver=$_POST["desc_ver"];
$direccion_ver=$_POST["direccion_ver"];
$credito_ver=$_POST["credito_ver"];
$contacto_ver=$_POST["contacto_ver"];
$email_ver=$_POST["email_ver"];
$telefono_ver=$_POST["telefono_ver"];

$nro_emp = 4;

//$n_decimales = 6;
//echo $tipoventa_ver;
?>
<body>
<?php


//Inicio reporte empresas

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

  	if ($empresa == 0) { // if 2.0
		$nom_empresa = 'Todos';
		$valor_empresa = ' ';
	} else {
		//nombre empresa
		$result2 = mysqli_query($link, "select pkyCompany as cod_emp, fldName as empresa
								from
								agnCompany 
								where
								pkyCompany = '$empresa' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result2)) {
			$nom_empresa = $row['empresa'];
		}
		mysqli_free_result($result2);
		$valor_empresa = ' a.pkyCompany = ' .$empresa. ' and ';
	} //fin if 2.0
	
	
	if ($nit == -1) { // if 2.1
		$nom_nit = 'Todos';
		$valor_nit = ' ';
	} else {
		$nom_nit = $nit;
		$valor_nit = ' a.fldNIT = ' .$nit. ' and ';
	} //fin if 2.1
	
	
	if ($rubro == 0) { // if 2.2
		$nom_rubro = 'Todos';
		$valor_rubro = ' ';
	} else {
		//nombre rubro
		$result3 = mysqli_query($link, "select pkyCompanyHeading as cod_rubro, fldName as rubro
							    from 
							    agnCompanyHeading
							    where
							    pkyCompanyHeading = '$rubro' and 
							    sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result3)) {
			$nom_rubro = $row['rubro'];
		}
		mysqli_free_result($result3);
		$valor_rubro = ' a.codHeading = ' .$rubro. ' and ';
	} //fin if 2.2
	
	
	if ($tipoi == 0) { // if 2.3
		$nom_tipo = 'Todos';
		$valor_tipo = ' ';
	} else {
		//nombre tipo
		$result3 = mysqli_query($link, "select pkyCompanyType as cod_tipo, fldName as tipo
								from
								agnCompanyType 
								where
								pkyCompanyType = '$tipoi' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result3)) {
			$nom_tipo = $row['tipo'];
		}
		mysqli_free_result($result3);
		$valor_tipo = ' a.codType = ' .$tipoi. ' and ';
	} //fin if 2.3
	
	
	if ($ciudad == 0) { // if 2.4
		$nom_ciudad = 'Todos';
		$valor_ciudad = ' ';
	} else {
		//nombre ciudad
		$result4 = mysqli_query($link, "select a.pkyCity as cod_ciu, a.fldName as ciudad, b.fldName as pais
							    from 
								regCity a, regCountry b
								where
								a.pkyCity = '$ciudad' and 
								a.codCountry = b.pkyCountry and 
								a.sysEnabled = 1 and a.sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result4)) {
			$nom_ciudad = $row['ciudad'].' - '.$row['pais'];
		}
		mysqli_free_result($result4);
		$valor_ciudad = ' a.codCityLocation = ' .$ciudad. ' and ';
	} //fin if 2.4
		
	if ($cliente_proveedor == 0) { // if 2.7
		$nom_cliente_proveedor = 'Todos';
		$valor_cliente_proveedor = ' ';
	} //fin if 2.7
	if ($cliente_proveedor == 1) { // if 2.8
		$nom_cliente_proveedor = 'Cliente';
		$valor_cliente_proveedor = ' a.fldIsClient = 1 and ';
	} //fin if 2.8
	if ($cliente_proveedor == 2) { // if 2.9
		$nom_cliente_proveedor = 'Proveedor';
		$valor_cliente_proveedor = ' a.fldIsSupplier = 1 and ';
	} //fin if 2.9
	if ($cliente_proveedor == 3) { // if 2.10
		$nom_cliente_proveedor = 'Cliente y Proveedor';
		$valor_cliente_proveedor = ' a.fldIsClient = 1 and a.fldIsSupplier = 1 and ';
	} //fin if 2.10
	
	
	if ($orden == 0) { // if 2.11
		$nom_orden = 'Empresa';
		$valor_orden = ' order by a.fldName ';
	} //fin if 2.11
	if ($orden == 1) { // if 2.12
		$nom_orden = 'NIT';
		$valor_orden = ' order by a.fldNIT, a.fldName ';
	} //fin if 2.12
	if ($orden == 2) { // if 2.13
		$nom_orden = 'Rubro';
		$valor_orden = ' order by e.fldName, a.fldName ';
	} //fin if 2.13
	if ($orden == 3) { // if 2.14
		$nom_orden = 'Tipo';
		$valor_orden = ' order by b.fldName, a.fldName ';
	} //fin if 2.14
	if ($orden == 4) { // if 2.15
		$nom_orden = 'C&oacute;digo del Vendedor';
		$valor_orden = ' order by a.codSeller, a.fldName ';
	} //fin if 2.15
	
	
	if ($canal == 0) { // if 2.16
		$nom_canal = 'Todos';
		$valor_canal = ' ';
	} else {
		//nombre canal
		$result7 = mysqli_query($link, "select pkyChannel as cod_canal, fldName as canal
								from
								agnChannel 
								where
								pkyChannel = '$canal' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result7)) {
			$nom_canal = $row['canal'];
		}
		mysqli_free_result($result7);
		$valor_canal = ' a.codChannel = ' .$canal. ' and ';
	} //fin if 2.16
	
	
	if ($vendedor == 0) { // if 2.17
		$nom_vendedor = 'Todos';
		$valor_vendedor = ' ';
	} else {
		//nombre vendedor
		$result8 = mysqli_query($link, "select pkyPerson as codigo, case when fldMiddleName is NULL then concat(fldLastName,', ',fldFirstName) else concat(fldLastName,' ',fldMiddleName,', ',fldFirstName) end as vendedor
				from agnPerson
				where
				pkyPerson = '$vendedor' and
				fldIsEmployee = 1 and
				fldCanSell = 1 and 
				sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result8)) {
			$nom_vendedor = $row['vendedor'];
		}
		mysqli_free_result($result8);
		$valor_vendedor = ' a.codSeller = ' .$vendedor. ' and ';
	} //fin if 2.17

//listado empresas
$result = mysqli_query($link, "select a.pkyCompany as cod_emp, a.fldName as empresa, a.codType as cod_tipo, b.fldName as tipo, a.fldNIT as nit, a.codCityLocation as cod_ciu, c.fldName as ciudad, d.fldName as pais, a.codHeading as cod_rubro, e.fldName as rubro, a.fldWebSite as sitio_web, a.fldZipCode as correo, a.fldAlternativeKey as cod_alt, a.fldIsClient as cliente, a.fldIsSupplier as proveedor, a.codChannel as cod_canal, a.fldCreditDays as dias_cred, a.fldMaxInvoiceReceptionDay as dia_fac, a.txtDescription as descripcion, a.codChannel as cod_canal, a.codSeller as cod_vendedor, a.codPriceList as cod_listaprecio, a.fldTotalSold as total_venta
from 
agnCompany a, agnCompanyType b, regCity c, regCountry d, agnCompanyHeading e
where
$valor_empresa
$valor_nit
$valor_rubro
$valor_tipo 
$valor_ciudad 
$valor_canal 
$valor_cliente_proveedor 
$valor_vendedor 
a.codType = b.pkyCompanyType and 
a.codCityLocation = c.pkyCity and
c.codCountry = d.pkyCountry and 
a.codHeading = e.pkyCompanyHeading and 
a.sysEnabled = 1 and a.sysAuthorized = 1  
$valor_orden ;");

//listado canal
$result4 = mysqli_query($link, "select pkyChannel as cod_canal, fldName as canal
from 
agnChannel 
where
sysEnabled = 1 and sysAuthorized = 1 
order by fldName;");

$matriz_canal = array();
$i = 1;
while ($row = mysqli_fetch_array($result4)) {
	$matriz_canal [$i][1] = $row['cod_canal'];
	$matriz_canal [$i][2] = $row['canal'];
	$i++;
}
$fin_canal = $i - 1;
mysqli_free_result($result4);

//listado vendedor
$result5 = mysqli_query($link, "select pkyPerson as cod_vendedor, case when fldMiddleName is NULL then concat(fldLastName,', ',fldFirstName) else concat(fldLastName,' ',fldMiddleName,', ',fldFirstName) end as vendedor
from agnPerson
where
fldIsEmployee = 1 and
fldCanSell = 1 and 
sysEnabled = 1 and sysAuthorized = 1
order by vendedor;");

$matriz_vendedor = array();
$i = 1;
while ($row = mysqli_fetch_array($result5)) {
	$matriz_vendedor [$i][1] = $row['cod_vendedor'];
	$matriz_vendedor [$i][2] = $row['vendedor'];
	$i++;
}
$fin_vendedor = $i - 1;
mysqli_free_result($result5);

//lista de precio
$result6 = mysqli_query($link, "select pkyPriceList as cod_listap, fldName as listap
from 
slsPriceList 
where
sysEnabled = 1 and sysAuthorized = 1 
order by fldName;");

$matriz_listap = array();
$i = 1;
while ($row = mysqli_fetch_array($result6)) {
	$matriz_listap [$i][1] = $row['cod_listap'];
	$matriz_listap [$i][2] = $row['listap'];
	$i++;
}
$fin_listap = $i - 1;
mysqli_free_result($result6);


$matriz_emp = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_emp [$i][1] = $row['cod_emp'];
	$matriz_emp [$i][2] = $row['empresa'];
	$matriz_emp [$i][3] = $row['cod_tipo'];
	$matriz_emp [$i][4] = $row['tipo'];
	$matriz_emp [$i][5] = $row['nit'];
	$matriz_emp [$i][6] = $row['cod_ciu'];
	$matriz_emp [$i][7] = $row['ciudad'];
	$matriz_emp [$i][8] = $row['pais'];
	$matriz_emp [$i][9] = $row['cod_rubro'];
	$matriz_emp [$i][10] = $row['rubro'];
	$matriz_emp [$i][11] = $row['sitio_web'];
	$matriz_emp [$i][12] = $row['correo'];
	$matriz_emp [$i][13] = $row['cod_alt'];
	$matriz_emp [$i][14] = $row['cliente'];
	$matriz_emp [$i][15] = $row['proveedor'];
		
	$matriz_emp [$i][19] = $row['total_venta'];
	
	$matriz_emp [$i][20] = $row['cod_canal'];
	$matriz_emp [$i][21] = $row['dias_cred'];
	$matriz_emp [$i][22] = $row['dia_fac'];
	$matriz_emp [$i][23] = $row['descripcion'];
	
	$matriz_emp [$i][24] = $row['cod_canal'];
	
	for($j = 1; $j <= $fin_canal; $j++) {
		if($row['cod_canal'] == $matriz_canal [$j][1]) {
			$matriz_emp [$i][25] = $matriz_canal [$j][2];
		}
	}
	
	for($j = 1; $j <= $fin_vendedor; $j++) {
		if($row['cod_vendedor'] == $matriz_vendedor [$j][1]) {
			$matriz_emp [$i][26] = $matriz_vendedor [$j][2];
			$matriz_emp [$i][28] = $matriz_vendedor [$j][1];
		}
	}
	
	for($j = 1; $j <= $fin_listap; $j++) {
		if($row['cod_listaprecio'] == $matriz_listap [$j][1]) {
			$matriz_emp [$i][27] = $matriz_listap [$j][2];
			$matriz_emp [$i][29] = $matriz_listap [$j][1];
		}
	}
	
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);


?>
<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>Listado de Empresas</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>Empresa: </b><?php echo $nom_empresa; ?></TD>
        <TD width="33%"><b>NIT: </b><?php echo $nom_nit; ?></TD>
        <TD width="33%"><b>Empresas que son: </b><?php echo $nom_cliente_proveedor; ?></TD>
        </TR>
        <TR>
		<TD><b>Ciudad: </b><?php echo $nom_ciudad; ?></TD>
		<TD><b>Rubro: </b><?php echo $nom_rubro; ?></TD>
        <TD><b>Tipo: </b><?php echo $nom_tipo; ?></TD>
		</TR>
        <TR>
		<TD><b>Canal: </b><?php echo $nom_canal; ?></TD>
		<TD><b>Vendedor: </b><?php echo $nom_vendedor; ?></TD>
		<TD><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
        </TR>
    </table>

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

    <table border="1" align="center" cellpadding="2" width="100%">
    <tr>
    <TD><b>Tipo de Cambio:</b></TD>
    </tr><tr>
    <TD align="center"><a href="../../ModuleAccounting/VoucherExchangeRate/index.php?pkyExchangeRate=<?php echo $ultimo_tc; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Tipo de Cambio"><?php echo $ultimo_tc; ?></a> (<?php echo $tc_fecha; ?> - <?php echo $tc_hora; ?>)<br/ >
        <table border="0" cellpadding="2" align="center">
            <?php
            for($m = 1; $m <= $fin_m; $m++) {
            printf("<tr><td align='left'>1 %s</td>", $ma_tc [$m][1]);//moneda base
            printf("<td align='center'>%s</td>", ' = ');//letra a
            printf("<td align='left'>%s %s</td>", number_format($ma_tc [$m][3],$n_tipocambio), $ma_tc [$m][2]);//moneda target y tasa
            //printf("<td align='right'>%s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio));//tasa
            }
            ?>
        </table>
    </TD>
    </tr>
	</table>
    
</td>
-->
</tr>
</table>
<br /> 
<table border="1" align="center" cellpadding="2" width="100%">
 	<thead>
        <TR> 
 		<TD align="center"><b>Cod.</b></TD>
        <?php if($cod_alt_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Cod. Alt.</b></TD>
        <?php } ?>
        <TD align="center"><b>Empresa</b></TD>
        <TD align="center"><b>NIT</b></TD>
        <?php if($ciudad_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Ciudad</b></TD>
        <?php } ?>
        <?php if($tipo_ver == 1) { $nro_emp++; ?>
		<TD align="center"><b>Tipo</b></TD>
        <?php } ?>
        <?php if($rubro_ver == 1) { $nro_emp++; ?>
    	<TD align="center"><b>Rubro</b></TD>
        <?php } ?>
        <?php if($web_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Sitio Web</b></TD>
        <?php } ?>
        <?php if($casilla_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Casilla</b></TD>
        <?php } ?>
        <TD align="center"><b>Cli.</b></TD>
        <TD align="center"><b>Pro.</b></TD>
        <?php if($totalventa_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Total Venta</b></TD>
        <?php } ?>
        <?php if($canal_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Canal</b></TD>
        <?php } ?>
        <?php if($vendedor_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Vendedor</b></TD>
        <?php } ?>
        <?php if($listaprecio_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Lista de Precio</b></TD>
        <?php } ?>
        <?php if($diascred_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>D&iacute;as Cr&eacute;dito</b></TD>
        <?php } ?>
        <?php if($diasfac_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Max. D&iacute;a Fac.</b></TD>
        <?php } ?>
        <?php if($desc_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Desc.</b></TD>
        <?php } ?>
        <?php if($direccion_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Direcci&oacute;n</b></TD>
        <?php } ?>
        <?php if($credito_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Tarj. Cred.</b></TD>
        <?php } ?>
        <?php if($contacto_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Doc. Contacto</b></TD>
        <?php } ?>
        <?php if($email_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>E-Mail</b></TD>
        <?php } ?>
        <?php if($telefono_ver == 1) { $nro_emp++; ?>
        <TD align="center"><b>Tel.</b></TD>
        <?php } ?>
        </TR>
	</thead>
    <tbody class="marcafila">
  <?php
  $color = 2;
     
  $j = 1;
  while ($j <= $fin) { //while
  	$cod_empresa2 = $matriz_emp [$j][1];
	$cod_vendedor2 = $matriz_emp [$j][28];
	$cod_listap2 = $matriz_emp [$j][29];
    if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleAgenda/VoucherCompany/index.php?pkyCompany=$cod_empresa2&user=$usuario&token=$token' title='Empresa'>%s</a></td>", $matriz_emp [$j][1]);//cod emp
	} else {
	printf("<tr><td align='center'><a href='../../ModuleAgenda/VoucherCompany/index.php?pkyCompany=$cod_empresa2&user=$usuario&token=$token' title='Empresa'>%s</a></td>", $matriz_emp [$j][1]);//cod emp
	}//fin if color
	if($cod_alt_ver == 1) {
	printf("<td align='center'>%s</td>", $matriz_emp [$j][13]);//cod alt
	}
	printf("<td align='left'>%s</td>", $matriz_emp [$j][2]);//empresa
	printf("<td align='left'>%s</td>", $matriz_emp [$j][5]);//nit
	if($ciudad_ver == 1) {
	printf("<td align='left'>%s</td>", $matriz_emp [$j][7]);//ciudad
	}
	if($tipo_ver == 1) {
	printf("<td align='left'>%s</td>", $matriz_emp [$j][4]);//tipo
	}
	if($rubro_ver == 1) {
	printf("<td align='left'>%s</td>", $matriz_emp [$j][10]);//rubro
	}
	if($web_ver == 1) {
	printf("<td align='left'>%s</td>", $matriz_emp [$j][11]);//sitio web
	}
	if($casilla_ver == 1) {
	printf("<td align='left'>%s</td>", $matriz_emp [$j][12]);//casilla
	}
	if($matriz_emp [$j][14] == 1) {
	printf("<td align='center'>Si</td>");//cliente
	} else {
	printf("<td>&nbsp;</td>");
	}
	if($matriz_emp [$j][15] == 1) {
	printf("<td align='center'>Si</td>");//proveedor
	} else {
	printf("<td>&nbsp;</td>");
	}
	if($totalventa_ver == 1) {
	printf("<td align='right'>%s</td>", number_format($matriz_emp [$j][19], 2));//total venta
	}
	if($canal_ver == 1) {
	printf("<td align='left'>%s</td>", $matriz_emp [$j][25]);//canal
	}
	if($vendedor_ver == 1) {
	printf("<td align='left'><a href='../../ModuleAgenda/VoucherPerson/index.php?pkyPerson=$cod_vendedor2&user=$usuario&token=$token' title='Persona'>%s</a></td>", $matriz_emp [$j][26]);//vendedor
	}
	if($listaprecio_ver == 1) {
	printf("<td align='left'><a href='../../ModuleSales/VoucherPriceList/index.php?pkyPriceList=$cod_listap2&user=$usuario&token=$token' title='Lista de Precio'>%s</a></td>", $matriz_emp [$j][27]);//lista precio
	}
	if($diascred_ver == 1) {
	printf("<td align='right'>%s</td>", $matriz_emp [$j][21]);//dias de credito
	}
	if($diasfac_ver == 1) {
	printf("<td align='right'>%s</td>", $matriz_emp [$j][22]);//dia de facturacion
	}
	if($desc_ver == 1) {
	printf("<td align='left'>%s</td>", $matriz_emp [$j][23]);//descripcion
	}
	
	if($direccion_ver == 1) {//if direccion
	echo "<td>";
	//direccion
	$result5 = mysqli_query($link, "select a.pkyCompanyAddress as cod_dir, a.fldAddress as direccion, b.fldName as tipo 
	from 
	agnCompanyAddress a, agnAddressType b
	where 
	a.codType = b.pkyAddressType and 
	a.codCompany = '$cod_empresa'  
	order by a.fldDefault desc, a.fldAddress;");
	
	echo "<table border='0' width='100%'>";
	$i = 1;
	while ($row = mysqli_fetch_array($result5)) {
		printf("<td align='left' valign='top'>%s</td>", $row['direccion']);
		printf("<td align='right' valign='top'>(%s)</td></tr>", $row['tipo']);
	}
	echo "</table>";
	echo "</td>";
	mysqli_free_result($result5);
	
	}//fin if direccion
	
	
	if($credito_ver == 1) {//if credito
	echo "<td>";
	//credito
	$result5 = mysqli_query($link, "select a.pkyCompanyCreditCard as cod_cre, b.fldName as tipo 
	from 
	agnCompanyCreditCard a, agnCreditCardType b
	where 
	a.codType = b.pkyCreditCardType and 
	a.codCompany = '$cod_empresa' 
	order by b.fldName;");
	
	echo "<table border='0' width='100%'>";
	$i = 1;
	while ($row = mysqli_fetch_array($result5)) {
		printf("<td align='left'>%s</td></tr>", $row['tipo']);
	}
	echo "</table>";
	echo "</td>";
	mysqli_free_result($result5);
	
	}//fin if credito
	
	
	if($contacto_ver == 1) {//if contacto
	echo "<td>";
	//contacto
	$result5 = mysqli_query($link, "select a.pkyCompanyIdentityDocument as cod_doc, b.fldName as tipo 
	from 
	agnCompanyIdentityDocument a, agnIdentityDocumentType b
	where 
	a.codType = b.pkyIdentityDocumentType and 
	a.codCompany = '$cod_empresa' 
	order by b.fldName;");
	
	echo "<table border='0' width='100%'>";
	$i = 1;
	while ($row = mysqli_fetch_array($result5)) {
		printf("<td align='left'>%s</td></tr>", $row['tipo']);
	}
	echo "</table>";
	echo "</td>";
	mysqli_free_result($result5);
	
	}//fin if contacto
	
	
	if($email_ver == 1) {//if email
	echo "<td>";
	//email
	$result5 = mysqli_query($link, "select a.pkyCompanyMail as cod_mail, a.fldMail as mail, b.fldName as tipo 
	from 
	agnCompanyMail a, agnMailType b
	where 
	a.codType = b.pkyMailType and 
	a.codCompany = '$cod_empresa'  
	order by a.fldDefault desc, a.fldMail;");
	
	echo "<table border='0' width='100%'>";
	$i = 1;
	while ($row = mysqli_fetch_array($result5)) {
		printf("<td align='left' valign='top'>%s</td>", $row['mail']);
		printf("<td align='right' valign='top'>(%s)</td></tr>", $row['tipo']);
	}
	echo "</table>";
	echo "</td>";
	mysqli_free_result($result5);
	
	}//fin if mail
	
	
	if($telefono_ver == 1) {//if telefono
	echo "<td>";
	//email
	$result5 = mysqli_query($link, "select a.pkyCompanyPhone as cod_tel, a.fldPhone as telefono, b.fldName as tipo 
	from 
	agnCompanyPhone a, agnPhoneType b
	where 
	a.codType = b.pkyPhoneType and 
	a.codCompany = '$cod_empresa'  
	order by a.fldDefault desc, a.fldPhone;");
	
	echo "<table border='0' width='100%'>";
	$i = 1;
	while ($row = mysqli_fetch_array($result5)) {
		printf("<td align='left' valign='top'>%s</td>", $row['telefono']);
		printf("<td align='right' valign='top'>(%s)</td></tr>", $row['tipo']);
	}
	echo "</table>";
	echo "</td>";
	mysqli_free_result($result5);
	
	}//fin if telefono
	
	echo "</tr>";
	
	$color++;
	$j++;
  }//fin while
?>
<tr BGCOLOR='#ffcccc'><td colspan="<?php echo $nro_emp++; ?>"><b>Total Empresas</b></td>
<td align="right"><?php printf("<b>%s</b>", $fin); ?></td></tr>
</tbody>
</table>
<?php
} //fin if 2

?>

</body>
</html>
