<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Ventas de Servicios por Cliente</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"];
//$estado=$_POST["estado"];
$almacen=$_POST["almacen"];
$cliente=$_POST["cliente"];
$canal_venta=$_POST["canal_venta"];
$fechaini=$_POST["fechaini"];
$fechafin=$_POST["fechafin"];
$orden=$_POST["orden"];
$otra_moneda=$_POST["otra_moneda"];
$participacion=$_POST["participacion"];
//$usuario = $_POST["usuario"];

/*
$cod_cxc=$_GET["cod_cxc"];
$empresa=$_GET["empresa"];
$otra_moneda_det=$_GET["otra_moneda_det"];
$usuario_det = $_GET["usuario_det"];
*/
//$n_decimales = 6;

?>
<body>

	<?php
	
//Inicio reporte Ventas por Cliente
//Se quito la opcion de la moneda 0 porque todo es en Bs pero queda para el futuro

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

if($participacion == 0) { //if partcipacion 0
$valor_agrupar = ' group by a.codClient ';
$nom_participacion = 'No mostrar';
} //fin if participacion 1

if($participacion == 1) { //if partcipacion 1
	$cliente = 0;
	$canal_venta = 0;
	if($otra_moneda == 0) { $otra_moneda = 1; }
	$valor_agrupar = ' group by a.codClient ';
	$nom_participacion = 'por Cliente';
} //fin if participacion 1

if($participacion == 2) { //if partcipacion 2
	$cliente = 0;
	$canal_venta = 0;
	if($otra_moneda == 0) { $otra_moneda = 1; }
	$valor_agrupar = ' group by c.codChannel ';
	$nom_participacion = 'por Tipo de Cliente';
} //fin if participacion 2

if($participacion == 3) { //if partcipacion 3
	$cliente = 0;
	$canal_venta = 0;
	if($otra_moneda == 0) { $otra_moneda = 1; }
	$valor_agrupar = ' group by c.codHeading ';
	$nom_participacion = 'por Rubro de Cliente';
} //fin if participacion 3


//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 ($cliente == 0) { //if cliente
    $valor_cliente = ' ';
	$nom_cliente = 'Todos';
  } else { 
    $valor_cliente = ' a.codClient = '.$cliente.' and ';
	//nombre de cliente
	$resultcli = mysqli_query($link, "select pkyCompany as codigo, fldName as cliente
from agnCompany
where
fldIsClient = 1 and
pkyCompany = '$cliente' and 
sysEnabled = 1 and sysAuthorized = 1;");
	while ($row = mysqli_fetch_array($resultcli)) {
	$nom_cliente = $row['codigo'].' - '.$row['cliente'];
	}
	mysqli_free_result($resultcli);
  } //fin if cliente

if($canal_venta == 0) { // if canal
	$nom_canal_venta = 'Todos';
	$valor_canal_venta = ' ';
} else { 
	$resultcan = mysqli_query($link, "select pkyChannel as codigo, fldName as canal
						from agnChannel
						where
						pkyChannel = '$canal_venta';");
	while ($row = mysqli_fetch_array($resultcan)) {   
	$nom_canal_venta = $row['canal'];
	}
	mysqli_free_result($resultcan);
	$valor_canal_venta = ' c.codChannel = '.$canal_venta.' and ';
} // fin if canal

if ($orden == 0) { //if orden
    $valor_orden = ' order by fecha, cliente, vehiculo ';
	$nom_orden = 'Fecha';
  } else { 
    $valor_orden = ' order by cliente, fecha, vehiculo ';
	$nom_orden = 'Nombre Cliente';
  } //fin if orden
  
 if ($almacen == 0) { //if almacen
    $valor_almacen = '  ';
	$nom_almacen = 'Todos';
  } else { 
    $valor_almacen = ' e.pkyWarehouse = '.$almacen.' and ';
	//nombre de almacen
	$resultalm = mysqli_query($link, "select pkyWarehouse as cod_alm, fldName as almacen
from
invWarehouse
where
pkyWarehouse = '$almacen' and
sysEnabled = 1 and sysAuthorized = 1;");
	while ($row = mysqli_fetch_array($resultalm)) {
	$nom_almacen = $row['almacen'];
	}
	mysqli_free_result($resultalm);
  } //fin if almacen
 

if($participacion == 0 || $participacion == 1) {
	$valor_orden = ' order by cliente, fecha, vehiculo ';
}

if($participacion == 2) {
	$valor_orden = ' order by cod_canal_cli, cliente, fecha ';
}

if($participacion == 3) {
	$valor_orden = ' order by cod_rubro_cli, cliente, fecha ';
}

//listado de ventas de servicios del periodo

$result = mysqli_query($link, "select a.pkyVehicleContract as cod_con, a.fldNumber as nro_doc,
a.fldDate as fecha, a.codType as cod_tipo, b.fldName as tipo,
a.codClient as cod_cli, c.fldName as cliente,
a.codVehicle as cod_vehi, d.fldName as vehiculo, 
a.codSourceWarehouse as cod_ciudad, e.fldName as ciudad, 
a.fldMileageRented as km_acordados,
case a.codStatus when 3 then 
a.fldMileageRented 
when 4 then
CONVERT (a.fldArrivalMileage, SIGNED)-CONVERT(a.fldMileage, SIGNED) 
else 0 end as km_real, 
a.fldTotalBilled as pagado, a.fldRealTotal as total, 
c.codChannel as cod_canal_cli, c.codHeading as cod_rubro_cli 
from
vhcVehicleContract a, vhcVehicleContractType b, agnCompany c, 
invItem d, invWarehouse e
where 
$valor_almacen 
$valor_cliente 
$valor_canal_venta 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
(a.codStatus = 3 or a.codStatus = 4) and 
a.fldReplaced = 0 and
a.codType = b.pkyVehicleContractType and 
a.codClient = c.pkyCompany and 
a.codVehicle = d.pkyItem and 
a.codSourceWarehouse = e.pkyWarehouse and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
$valor_orden;");


$matriz_ventas = array ();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_ventas[$i][1] = $row['cod_con'];
	$matriz_ventas[$i][2] = $row['nro_doc'];
	$matriz_ventas[$i][3] = $row['fecha'];
	$matriz_ventas[$i][4] = $row['cod_tipo'];
	$matriz_ventas[$i][5] = $row['tipo'];
	$matriz_ventas[$i][6] = $row['cod_cli'];
	$matriz_ventas[$i][7] = $row['cliente'];
	$matriz_ventas[$i][8] = $row['cod_vehi'];
	$matriz_ventas[$i][9] = $row['vehiculo'];
	$matriz_ventas[$i][10] = $row['cod_ciudad'];
	$matriz_ventas[$i][11] = $row['ciudad'];
	$matriz_ventas[$i][12] = $row['km_acordados'];
	$matriz_ventas[$i][13] = $row['km_real'];
	$matriz_ventas[$i][14] = $row['pagado'];
	$matriz_ventas[$i][15] = $row['total'];
	$matriz_ventas[$i][16] = 1; //codigo moneda por defecto Bs
	$matriz_ventas[$i][17] = $row['total'] - $row['pagado'];
	$matriz_ventas[$i][18] = $row['cod_canal_cli'];
	$matriz_ventas[$i][19] = $row['cod_rubro_cli'];
	$i++;
	}
	mysqli_free_result($result);

$fin_ventas = $i - 1;

//valor del codigo del ultimo tipo de cambio

$resultom = mysqli_query($link, "SELECT T2.pkyExchangeRate as tc FROM 
(SELECT fldDate, MAX(fldTime) AS fldTime FROM actExchangeRate WHERE fldExclusive = '0' GROUP BY fldDate) as T1,
actExchangeRate T2 
WHERE 
T1.fldDate = (SELECT MAX(fldDate) FROM actExchangeRate WHERE fldDate <= '$fecha_hoy' AND fldExclusive = '0') AND 
T2.fldDate = T1.fldDate AND 
T2.fldTime = T1.fldTime AND 
sysEnabled = 1 AND sysAuthorized = 1;");
while ($row = mysqli_fetch_array($resultom)) {
	$ultimo_tc = $row['tc'];
	}
	mysqli_free_result($resultom);
	
//valor de conversion tipo de cambio
$resulttc = mysqli_query($link, "SELECT pkyExchangeRateDetail as codigotc, codBaseCurrency as monedabase, codTargetCurrency as monedacambio, fldRate as tc
from
actExchangeRateDetail
where
codExchangeRate = '$ultimo_tc' and
codBaseCurrency = '$otra_moneda' and
codTargetCurrency = 1 and
sysEnabled = 1 AND sysAuthorized = 1;");
$matriz_tc = array();
$i = 1;
while ($row = mysqli_fetch_array($resulttc)) {
	$matriz_tc [$i][1] = $row['codigotc'];//codigo tc
	$matriz_tc [$i][2] = $row['monedabase'];//codigo moneda base
	$matriz_tc [$i][3] = $row['monedacambio'];//codigo moneda cambio
	$matriz_tc [$i][4] = $row['tc'];//valor a multiplicar del tc 
	$i++;
	}
	mysqli_free_result($resulttc);
if($otra_moneda == 1) { $matriz_tc [1][4] = 1; }//valor a multiplicar del tc  

  
if($otra_moneda != 0){ 
$mostrar_txt_moneda = $nom_otra_moneda;
} else {
$mostrar_txt_moneda = 'Moneda original';	
}

?>
<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>Ventas de Servicios por Cliente</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>Cliente: </b><?php echo $nom_cliente; ?></TD>
        <TD width="25%"><b>Canal (solo para empresas): </b><?php echo $nom_canal_venta; ?></TD>
        <TD width="25%"><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
        <TD width="25%"><b>Participaci&oacute;n: </b><?php echo $nom_participacion; ?></TD>
        </TR>
        <TR>
        <TD><b>Almacen: </b><?php echo $nom_almacen; ?></TD>
        <TD colspan="2"><b>De Fecha: </b><?php echo $fechaini; ?> <b>a Fecha: </b><?php echo $fechafin; ?></TD>
        <TD><b>Expresado en: </b><?php echo $mostrar_txt_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; ?>&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>
    <TD align="center"><b>Nro. Doc.</b></TD>
	<TD align="center" width="65"><b>Fecha</b></TD>
    <TD align="center"><b>Tipo</b></TD>
    <TD align="center"><b>Cliente</b></TD>
    <TD align="center"><b>Veh&iacute;culo</b></TD>
    <TD align="center"><b>Almacen</b></TD>
    <TD align="center"><b>Km Acordados</b></TD>
    <TD align="center"><b>(Km.Llega-Km.Sale) Km Total</b></TD>
    <TD align="center"><b>Total Bs.</b></TD>
	<TD align="center"><b>Facturado Bs.</b></TD>
    <TD align="center"><b>Por Facturar Bs.</b></TD>
    <?php if($otra_moneda != 0){ ?>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Saldo en <?php echo $nom_otra_moneda; ?></b></TD>
    <?php } ?>
    </TR>
    </thead>
    <tbody class="marcafila">

    <?php
    //if($cliente == 0) { //if cliente = 0
	//lista de codigo de clientes del periodo, mismo query de listado de ventas del periodo pero con group by y num_reg (de acuerdo a la participacion es la agrupacion)
	$result_cli = mysqli_query($link, "select a.pkyVehicleContract as cod_con, a.fldNumber as nro_doc,
a.fldDate as fecha, a.codType as cod_tipo, b.fldName as tipo,
a.codClient as cod_cli, c.fldName as cliente,
a.codVehicle as cod_vehi, d.fldName as vehiculo, 
a.codSourceWarehouse as cod_ciudad, e.fldName as ciudad, 
a.fldMileageRented as km_acordados,
case a.codStatus when 3 then 
a.fldMileageRented 
when 4 then
CONVERT (a.fldArrivalMileage, SIGNED)-CONVERT(a.fldMileage, SIGNED) 
else 0 end as km_real, 
a.fldTotalBilled as pagado, a.fldRealTotal as total, c.codChannel as cod_canal_cli, c.codHeading as cod_rubro_cli, count(1) as num_reg
from
vhcVehicleContract a, vhcVehicleContractType b, agnCompany c, 
invItem d, invWarehouse e
where
$valor_almacen 
$valor_cliente 
$valor_canal_venta 
a.fldDate >= '$fechaini' and
a.fldDate <= '$fechafin' and
(a.codStatus = 3 or a.codStatus = 4) and 
a.fldReplaced = 0 and
a.codType = b.pkyVehicleContractType and 
a.codClient = c.pkyCompany and 
a.codVehicle = d.pkyItem and 
a.codSourceWarehouse = e.pkyWarehouse and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
$valor_agrupar  
$valor_orden;");
	$matriz_cli = array ();
	$i = 1;
	while ($row = mysqli_fetch_array($result_cli)) {
	$matriz_cli[$i][1] = $row['cod_cli'];
	$matriz_cli[$i][2] = $row['num_reg'];
	//echo $matriz_cli[$i][1].' - '.$matriz_cli[$i][2].'<br>';
	$i++;
	}
	
	$fin_cli = $i - 1;
	//echo $fin_cli.'<br>';
	mysqli_free_result($result_cli);
	
						
	$color = 2;
	$suma_cant = 0;
	$suma_om = 0;
	$suma_par = 0;
	$suma_participacion = 0;
	$suma_part_total = 0;
	$suma_km_a = 0;
	$suma_km_total = 0;
	$suma_km_a_par = 0;
	$suma_km_total_par = 0;
	$suma_pagado = 0;
	$suma_total = 0;
	$suma_saldo = 0;
	$suma_pagado_par = 0;
	$suma_total_par = 0;
	$suma_saldo_par = 0;
	$i = 1;
	$j = 1;
	$p = 1; //contador participacion
	$matriz_participacion = array();
	while($j <= $fin_cli){//while $fin_cli
		$k = 1;
	while($k <= $matriz_cli[$j][2]) { //while
		$codigo = $matriz_ventas[$i][1];
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_ventas[$i][1]);//cod
		} else {
		printf("<tr><td align='center'>%s</td>", $matriz_ventas[$i][1]);//cod
		}//cierra if color
		printf("<td align='center'>%s</td>", $matriz_ventas[$i][2]);//numero documento
		printf("<td align='right'>%s</td>", $matriz_ventas[$i][3]);//fecha
		printf("<td>%s</td>", $matriz_ventas[$i][5]);//tipo
		printf("<td>%s</td>", $matriz_ventas[$i][7]);//cliente
		printf("<td>%s</td>", $matriz_ventas[$i][9]);//vehiculo
		printf("<td>%s</td>", $matriz_ventas[$i][11]);//almacen
		printf("<td align='right'>%s</td>", number_format($matriz_ventas[$i][12],2));//km acordados
		printf("<td align='right'>%s</td>", number_format($matriz_ventas[$i][13],2));//km total
		printf("<td align='right'>%s</td>", number_format($matriz_ventas[$i][15],$n_decimales));//total
		printf("<td align='right'>%s</td>", number_format($matriz_ventas[$i][14],$n_decimales));//pagado
		$saldo_v = $matriz_ventas[$i][17];
							
		if($otra_moneda != 0){ //if otra moneda
		printf("<td align='right'>%s</td>", number_format($saldo_v,$n_decimales));//saldo
		printf("<td align='right'>%s</td>", number_format($matriz_tc [1][4],$n_tipocambio));// tasa de tc
		printf("<td align='right'>%s</td></tr>", number_format($saldo_v * $matriz_tc [1][4],$n_decimales));// saldo otra moneda
		}//fin if otra moneda 
		else {	//else otra moneda
		printf("<td align='right'>%s</td>", number_format($saldo_v,$n_decimales));//saldo
		}//fin else otra moneda
		
		$suma_om = $suma_om + $saldo_v * $matriz_tc [1][4];
	    $suma_par = $suma_par + $saldo_v * $matriz_tc [1][4];
		$suma_participacion = $suma_participacion + $matriz_ventas[$i][15] * $matriz_tc [1][4];
		$suma_part_total = $suma_part_total + $matriz_ventas[$i][15] * $matriz_tc[1][4];
		$suma_km_a = $suma_km_a + $matriz_ventas[$i][12];
		$suma_km_total = $suma_km_total + $matriz_ventas[$i][13];
		$suma_km_a_par = $suma_km_a_par + $matriz_ventas[$i][12];
		$suma_km_total_par = $suma_km_total_par + $matriz_ventas[$i][13];
		$suma_pagado = $suma_pagado + $matriz_ventas[$i][14];
		$suma_total = $suma_total + $matriz_ventas[$i][15];
		$suma_saldo = $suma_saldo + $matriz_ventas[$i][17];
		$suma_pagado_par = $suma_pagado_par + $matriz_ventas[$i][14];
		$suma_total_par = $suma_total_par + $matriz_ventas[$i][15];
		$suma_saldo_par = $suma_saldo_par + $matriz_ventas[$i][17];
	  	$color++;
		$i++;
		$k++;
	}//fin while
	
	$j++;
	if($otra_moneda != 0){//if otra moneda 2
	
		if ($participacion == 0) { $mostrar_nom_periodo = $matriz_ventas[$i-1][7]; }
		if ($participacion == 1) { $mostrar_nom_periodo = $matriz_ventas[$i-1][7]; }
		if ($participacion == 2) { //if part 2
			$valor_canal_cli = $matriz_ventas[$i-1][18];
			$resultcanal = mysqli_query($link, "select fldName as canal_cli
										from
										agnChannel
										where
										pkyChannel = '$valor_canal_cli' and
										sysEnabled = 1 and sysAuthorized = 1;");
			while ($row = mysqli_fetch_array($resultcanal)) {
			$mostrar_nom_periodo = $row['canal_cli'];
			}
			mysqli_free_result($resultcanal);
		 } //fin if part 2
		 if ($participacion == 3) { //if part 3
			$valor_rubro_cli = $matriz_ventas[$i-1][19];
			$resultrubro = mysqli_query($link, "select fldName as rubro_cli
									  	from
										agnCompanyHeading
										where
										pkyCompanyHeading = '$valor_rubro_cli' and
										sysEnabled = 1 and sysAuthorized = 1;");
			while ($row = mysqli_fetch_array($resultrubro)) {
			$mostrar_nom_periodo = $row['rubro_cli'];
			}
			mysqli_free_result($resultrubro);
		 } //fin if part 3
 ?>
 <tr BGCOLOR='#ffcccc'><td colspan="7"><b>Total Ventas Periodo <?php echo $mostrar_nom_periodo; ?></b></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_km_a_par,2)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_km_total_par,2)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_total_par,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_pagado_par,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_saldo_par,$n_decimales)); ?></td>
 <td>&nbsp;</td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_par,$n_decimales)); ?></td>
 </tr>
 <?php
	}//fin if otra moneda 2
	if ($participacion == 1 || $participacion == 2 || $participacion == 3){//if participacion
		$matriz_participacion[$p][1] = $mostrar_nom_periodo;
		$matriz_participacion[$p][2] = $suma_participacion;
		$suma_participacion = 0;
		$p++;
	}//fin if participacion
	
	$suma_par = 0;	
	$suma_km_a_par = 0;
	$suma_km_total_par = 0;
	$suma_pagado_par = 0;
	$suma_total_par = 0;
	$suma_saldo_par = 0;
	}//fin while $fin_cli
	/*
	} else { //else vendedor = 0 y cliente = 0
	$color = 2;
	$suma_cant = 0;
	$suma_om = 0;
	$i = 1;
	
	while($i <= $fin_ventas) { //while
		$codigo = $matriz_ventas[$i][14];
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_ventas[$i][1]);//cod
		} else {
		printf("<tr><td align='center'>%s</td>", $matriz_ventas[$i][1]);//cod
		}//cierra if color
		printf("<td align='center'>%s</td>", $matriz_ventas[$i][3]);//numero documento
		printf("<td align='right'>%s</td>", $matriz_ventas[$i][2]);//fecha
		printf("<td>%s</td>", $matriz_ventas[$i][4]);//cliente
		printf("<td>%s</td>", $matriz_ventas[$i][5]);//vendedor
		printf("<td>%s</td>", $matriz_ventas[$i][6]);//almacen
		printf("<td>%s</td>", $matriz_ventas[$i][7]);//local venta
		printf("<td>%s</td>", $matriz_ventas[$i][15]);//glosa
		printf("<td>%s</td>", $matriz_ventas[$i][8]);//moneda
		printf("<td align='right'>%s</td>", number_format($matriz_ventas[$i][10],$n_decimales));//total bruto
		printf("<td align='right'>%s</td>", number_format($matriz_ventas[$i][11],$n_decimales));//descuento %
		printf("<td align='right'>%s</td>", number_format($matriz_ventas[$i][12],$n_decimales));//descuento global %
		printf("<td align='right'>%s</td>", number_format($matriz_ventas[$i][13],$n_decimales));//total
		printf("<td align='right'>%s</td>", number_format($matriz_ventas[$i][16],$n_decimales));//cobros anteriores
		printf("<td align='right'>%s</td>", number_format($matriz_ventas[$i][17],$n_decimales));//cobro periodo
		$saldo_v = $matriz_ventas[$i][13] - $matriz_ventas[$i][16] - $matriz_ventas[$i][17];
		
		if($otra_moneda != 0){ //if otra moneda
		//printf("<td align='right'><a href='report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=1&otra_moneda_det=0' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($row['saldo'],$n_decimales));//saldo
		printf("<td align='right'><a href='../../ModuleCollections/ReportAccountsReceivable/report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=1&otra_moneda_det=0' title='Kardex de Cuenta por Cobrar'>%s</a></td>", number_format($saldo_v,$n_decimales));//saldo
		printf("<td align='right'>%s</td>", $matriz_ventas[$i][19]);// tasa de tc
		printf("<td align='right'><a href='../../ModuleCollections/ReportAccountsReceivable/report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=1&otra_moneda_det=$otra_moneda' title='Kardex de Cuenta por Cobrar'>%s</a></td></tr>", number_format($saldo_v * $matriz_ventas[$i][19],$n_decimales));// saldo otra moneda
		}//fin if otra moneda 
		else {	//else otra moneda
		printf("<td align='right'><a href='../../ModuleCollections/ReportAccountsReceivable/report.php?rep=6&tipo=2&cod_cxc=$codigo&empresa=1&otra_moneda_det=0' title='Kardex de Cuenta por Cobrar'>%s</a></td></tr>", number_format($saldo_v,$n_decimales));//saldo
		}//fin else otra moneda
		$suma_om = $suma_om + $saldo_v * $matriz_ventas[$i][19];
	    //$suma_empresa = $suma_empresa + $row['saldo'];
	  	$color++;
		$i++;
	}//fin while
	} // fin if vendedor = 0 y cliente = 0
*/
?>
 <?php if($otra_moneda != 0){ ?>
 <tr BGCOLOR='#ffcccc'><td colspan="7"><b>Total Ventas Periodo</b></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_km_a,2)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_km_total,2)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_total,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_pagado,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_saldo,$n_decimales)); ?></td>
 <td>&nbsp;</td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_om,$n_decimales)); ?></td>
 </tr>
 </tbody>
 </table>
	 <?php if($participacion == 1 || $participacion == 2 || $participacion == 3){ //if part 3
	 	if($participacion == 1) { $titulo_participacion = 'Cliente'; }
		if($participacion == 2) { $titulo_participacion = 'Canal'; }
		if($participacion == 3) { $titulo_participacion = 'Rubro'; }
		?>
     <p>
     <center><font color="#000099" size="+1"><b>Resumen de participaci&oacute;n en el Periodo</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>Participaci&oacute;n: </b><?php echo $nom_participacion; ?></TD>
        <TD width="50%"><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; ?>&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><?php echo $titulo_participacion; ?></b></TD>
        <TD align="center"><b>Suma Ventas del Periodo en <?php echo $nom_otra_moneda; ?></b></TD>
        <TD align="center"><b>Participaci&oacute;n del Periodo %</b></TD>
        </TR>
        </thead>
        <tbody class="marcafila">
        <?php 
		$sum_p_p = 0;
		$r = 1;
		$color2 = 2;
		while ($r < $p) {//while rp
			if($color2 % 2 == 0) { //if color
			printf("<tr BGCOLOR='#e2e7fa'><td align='left'>%s</td>", $matriz_participacion[$r][1]);//cliente
			} else { 
			printf("<tr><td align='left'>%s</td>", $matriz_participacion[$r][1]);//cliente
			} //fin if color
			printf("<td align='right'>%s</td>", number_format($matriz_participacion[$r][2], $n_decimales));//suma ventas
			if ($suma_part_total == 0) {
				$p_p = 0;
			} else {
				$p_p = ($matriz_participacion[$r][2] / $suma_part_total) * 100;
			}
			printf("<td align='right'>%s</td></tr>", number_format($p_p, 2));//% participacion
			$sum_p_p = $sum_p_p + $p_p;
			$r++;
			$color2++;
		}//fin while rp
		?>
        <tr BGCOLOR='#ffcccc'>
        <td align="left"><b>Totales</b></td>
        <td align="right"><?php echo number_format($suma_part_total, $n_decimales);?></td>
        <td align="right"><?php echo number_format($sum_p_p, 2);?></td></tr>
        </tbody>
        </table>
   <?php     } //fin if part 3 
  		} else { ?>
 </tbody>
 </table>
 <?php }  ?>           
 
<?php

} //fin if 2

?>

</body>
</html>
