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

</head>
<?php
/*
Muestra las ventas combinando item con item, vendedor, almacen, cliente, local o punto de venta, ciudad. Permite elegir el tipo que puede ser banco, cheque, cortesia, cxc, efectivo y tarjeta de credito, además filtra por familia, subfamilia, grupo y subgrupo en un rango de fechas.
El detalle discrimina de acuerdo a la eleccion que se realiza item, vendedor, almacen, cliente, local o punto de venta, ciudad. Tambien pasa los parametros de banco, cheque, cortesia, cxc, efectivo y tarjeta de credito, familia, subfamilia, grupo y subgrupo. (Modificado por sugerencia de M)
*/
include "../../conn.php";
include "../../general.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$usuario=$_POST["usuario"];
$asientocc=$_POST["asientocc"];
$agrupado=$_POST["agrupado"];
$porfamilia=$_POST["porfamilia"];
$porfamiliacon=$_POST["porfamiliacon"];
$tipo_nota=$_POST["tiponota"];
$item=$_POST["item"];
$vendedor=$_POST["vendedor"];
$almacen=$_POST["almacen"];
$cliente=$_POST["cliente"];
$canal_venta=$_POST["canal_venta"];
$local=$_POST["local"];
$ciudad=$_POST["ciudad"];
$familia=$_POST["familia"];
$subfamilia=$_POST["subfamilia"];
$grupo=$_POST["grupo"];
$subgrupo=$_POST["subgrupo"];
$anoini=$_POST["anoini"];

$otra_moneda=$_POST["otra_moneda"];
$ventas=$_POST["ventas"];

//$cod_cxc=$_GET["cod_cxc"];
//$empresa=$_GET["empresa"];
$usuario_det=$_GET["usuario_det"];
$asientocc_det=$_GET["asientocc_det"];
$otra_moneda_det=$_GET["otra_moneda_det"];
$agrupado_det=$_GET["agrupado_det"];
$ventas_det=$_GET["ventas_det"]; //son las ventas anuales por item, vendedor, cliente, almacen, local, ciudad
$porfamilia_det=$_GET["porfamilia_det"];
$tipo_nota_det=$_GET["tipo_nota_det"];
$familia_det=$_GET["familia_det"];
$subfamilia_det=$_GET["subfamilia_det"];
$grupo_det=$_GET["grupo_det"];
$subgrupo_det=$_GET["subgrupo_det"];
$fechaini_det=$_GET["fechaini_det"];
$fechafin_det=$_GET["fechafin_det"];
$item_det=$_GET["item_det"];
$cliente_det=$_GET["cliente_det"];
$vendedor_det=$_GET["vendedor_det"]; 
$almacen_det=$_GET["almacen_det"];
$local_det=$_GET["local_det"];
$ciudad_det=$_GET["ciudad_det"];
$cod_varios_det=$_GET["cod_varios_det"];//codigo de item, vendedor, cliente, almacen, local, ciudad con agrupacion = 0
$nom_varios_det=$_GET["nom_varios_det"];//nombre de item, vendedor, cliente, almacen, local, ciudad con agrupacion = 0
$titulo_mes_det=$_GET["titulo_mes_det"];

//envio de $agrupado != 0 para mostrar el mismo reporte con la familia
$usuario_rev=$_GET["usuario_rev"];
$asientocc_rev=$_GET["asientocc_rev"];
$agrupado_rev=$_GET["agrupado_rev"];
$porfamilia_rev=$_GET["porfamilia_rev"];
$porfamiliacon_rev=$_GET["porfamiliacon_rev"];
$tipo_nota_rev=$_GET["tipo_nota_rev"];
$item_rev=$_GET["item_rev"];
$vendedor_rev=$_GET["vendedor_rev"];
$almacen_rev=$_GET["almacen_rev"];
$cliente_rev=$_GET["cliente_rev"];
$canal_venta_rev=$_GET["canal_venta_rev"];
$local_rev=$_GET["local_rev"];
$ciudad_rev=$_GET["ciudad_rev"];
$familia_rev=$_GET["familia_rev"];
$anoini_rev=$_GET["anoini_rev"];
$otra_moneda_rev=$_GET["otra_moneda_rev"];
$ventas_rev=$_GET["ventas_rev"];

if($usuario_rev != NULL) { $usuario = $usuario_rev; }
if($asientocc_rev != NULL) { $asientocc = $asientocc_rev; }
if($agrupado_rev != NULL) { $agrupado = $agrupado_rev; }
if($porfamilia_rev != NULL) { $porfamilia = $porfamilia_rev; }
if($porfamiliacon_rev != NULL) { $porfamiliacon = $porfamiliacon_rev; }
if($tipo_nota_rev != NULL) { $tipo_nota = $tipo_nota_rev; }
if($item_rev != NULL) { $item = $item_rev; }
if($vendedor_rev != NULL) { $vendedor = $vendedor_rev; }
if($almacen_rev != NULL) { $almacen = $almacen_rev; }
if($cliente_rev != NULL) { $cliente = $cliente_rev; }
if($canal_venta_rev != NULL) { $canal_venta = $canal_venta_rev; }
if($local_rev != NULL) { $local = $local_rev; }
if($ciudad_rev != NULL) { $ciudad = $ciudad_rev; }
if($familia_rev != NULL) { $familia = $familia_rev; }
if($anoini_rev != NULL) { $anoini = $anoini_rev; }
if($otra_moneda_rev != NULL) { $otra_moneda = $otra_moneda_rev; }
if($ventas_rev != NULL) { $ventas = $ventas_rev; }


//$n_decimales = 6;

if($familia == '') { $familia = 0;}
if($subfamilia == '') { $subfamilia = 0;}
if($grupo == '') { $grupo = 0;}
if($subgrupo == '') { $subgrupo = 0;}

$mes_ini = $mes_ini + 0;

?>
<body>

	<?php
	
//Inicio reporte Ventas anuales


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($agrupado == 0) { 
	$nom_agrupado = 'No'; 
} else { 
	$nom_agrupado = 'Si'; 
	if($porfamiliacon == 0) { 
		$nom_porfamiliacon = 'Detalle'; 
	} else { 
		$nom_porfamiliacon = 'Consolidado'; 
	}
}



//numero de columnas si no es item
$nro_col = 2;

if ($ventas == 0) { //if ventas = 0
	$nro_col = 3;
	$cliente = 0;
	$canal_venta = 0;
	$vendedor = 0;
	$almacen = 0;
	$local = 0;
	$ciudad = 0;
	if($agrupado == 0) {
	$valor_agrupar = 'group by fin.cod_item';
	$valor_ordenar = 'order by fin.item';
	$nom_porfamilia = '';
	} else {
		if($porfamilia == 0) {
		$valor_agrupar = 'group by fin.cod_familia, fin.cod_item';
		$valor_ordenar = 'order by fin.cod_familia, fin.item';
		$valor_agrupar2 = 'group by fin2.cod_familia';
		$valor_ordenar2 = 'order by fin2.cod_familia, fin2.item';
		$valor_porfamilia = 'count(fin2.cod_familia) as num_reg';
		$nom_porfamilia = '(Familia)';
		}
		if($porfamilia == 1) {
		$valor_agrupar = 'group by fin.cod_subfamilia, fin.cod_item';
		$valor_ordenar = 'order by fin.cod_subfamilia, fin.item';
		$valor_agrupar2 = 'group by fin2.cod_subfamilia';
		$valor_ordenar2 = 'order by fin2.cod_subfamilia, fin2.item';
		$valor_porfamilia = 'count(fin2.cod_subfamilia) as num_reg';
		$nom_porfamilia = '(Sub Familia)';
		}
		if($porfamilia == 2) {
		$valor_agrupar = 'group by fin.cod_grupo, fin.cod_item';
		$valor_ordenar = 'order by fin.cod_grupo, fin.item';
		$valor_agrupar2 = 'group by fin2.cod_grupo';
		$valor_ordenar2 = 'order by fin2.cod_grupo, fin2.item';
		$valor_porfamilia = 'count(fin2.cod_grupo) as num_reg';
		$nom_porfamilia = '(Grupo)';
		}
		if($porfamilia == 3) {
		$valor_agrupar = 'group by fin.cod_subgrupo, fin.cod_item';
		$valor_ordenar = 'order by fin.cod_subgrupo, fin.item';
		$valor_agrupar2 = 'group by fin2.cod_subgrupo';
		$valor_ordenar2 = 'order by fin2.cod_subgrupo, fin2.item';
		$valor_porfamilia = 'count(fin2.cod_subgrupo) as num_reg';
		$nom_porfamilia = '(Sub Grupo)';
		}
	}
	$nom_orden = 'Item';
	$nom_ventas = 'Item';
	$mat_ventas = array();
	$i = 1;
	$resultit = mysqli_query($link, "select pkyItem as codigo, fldName as item, fldFactoryKey as cod_fabrica
		from 
		invItem
		where
		sysEnabled = 1 and sysAuthorized = 1
		order by item;");
	while ($row = mysqli_fetch_array($resultit)) {
	$mat_ventas[$i][1] = $row['codigo'];
	$mat_ventas[$i][2] = $row['item'];
	$mat_ventas[$i][100] = $row['cod_fabrica'];
	$i++;
	}
	mysqli_free_result($resultit);
	$fin = $i - 1;
} //fin if ventas = 0
if ($ventas == 1) { //if ventas = 1
  	$item = 0;
	$cliente = 0;
	$canal_venta = 0;
	$almacen = 0;
	$local = 0;
	$ciudad = 0;
	if($agrupado == 0) {
	$valor_agrupar = 'group by fin.cod_vendedor';
	$valor_ordenar = 'order by fin.vendedor';
	$nom_porfamilia = '';
	} else {
		if($porfamilia == 0) {
		$valor_agrupar = 'group by fin.cod_familia, fin.cod_vendedor';
		$valor_ordenar = 'order by fin.cod_familia, fin.vendedor';
		$valor_agrupar2 = 'group by fin2.cod_familia';
		$valor_ordenar2 = 'order by fin2.cod_familia, fin2.vendedor';
		$valor_porfamilia = 'count(fin2.cod_familia) as num_reg';
		$nom_porfamilia = '(Familia)';
		}
		if($porfamilia == 1) {
		$valor_agrupar = 'group by fin.cod_subfamilia, fin.cod_vendedor';
		$valor_ordenar = 'order by fin.cod_subfamilia, fin.vendedor';
		$valor_agrupar2 = 'group by fin2.cod_subfamilia';
		$valor_ordenar2 = 'order by fin2.cod_subfamilia, fin2.vendedor';
		$valor_porfamilia = 'count(fin2.cod_subfamilia) as num_reg';
		$nom_porfamilia = '(Sub Familia)';
		}
		if($porfamilia == 2) {
		$valor_agrupar = 'group by fin.cod_grupo, fin.cod_vendedor';
		$valor_ordenar = 'order by fin.cod_grupo, fin.vendedor';
		$valor_agrupar2 = 'group by fin2.cod_grupo';
		$valor_ordenar2 = 'order by fin2.cod_grupo, fin2.vendedor';
		$valor_porfamilia = 'count(fin2.cod_grupo) as num_reg';
		$nom_porfamilia = '(Grupo)';
		}
		if($porfamilia == 3) {
		$valor_agrupar = 'group by fin.cod_subgrupo, fin.cod_vendedor';
		$valor_ordenar = 'order by fin.cod_subgrupo, fin.vendedor';
		$valor_agrupar2 = 'group by fin2.cod_subgrupo';
		$valor_ordenar2 = 'order by fin2.cod_subgrupo, fin2.vendedor';
		$valor_porfamilia = 'count(fin2.cod_subgrupo) as num_reg';
		$nom_porfamilia = '(Sub Grupo)';
		}
	}
	$nom_orden = 'Vendedor';
	$nom_ventas = 'Vendedor';
	$mat_ventas = array();
	$i = 1;
	$resultven = 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
		fldIsEmployee = 1 and
		fldCanSell = 1 and 
		sysEnabled = 1 and sysAuthorized = 1
		order by vendedor;");
	while ($row = mysqli_fetch_array($resultven)) {
	$mat_ventas[$i][1] = $row['codigo'];
	$mat_ventas[$i][2] = $row['vendedor'];
	$i++;
	}
	mysqli_free_result($resultven);
	$fin = $i - 1;
} //fin if ventas = 1
if ($ventas == 2) { //if ventas = 2
	$item = 0;
	$vendedor = 0;  
	$almacen = 0;
	$local = 0;
	$ciudad = 0;
	if($agrupado == 0) {
	$valor_agrupar = 'group by fin.cod_cli';
	$valor_ordenar = 'order by fin.cliente';
	$nom_porfamilia = '';
	} else {
		if($porfamilia == 0) {
		$valor_agrupar = 'group by fin.cod_familia, fin.cod_cli';
		$valor_ordenar = 'order by fin.cod_familia, fin.cliente';
		$valor_agrupar2 = 'group by fin2.cod_familia';
		$valor_ordenar2 = 'order by fin2.cod_familia, fin2.cliente';
		$valor_porfamilia = 'count(fin2.cod_familia) as num_reg';
		$nom_porfamilia = '(Familia)';
		}
		if($porfamilia == 1) {
		$valor_agrupar = 'group by fin.cod_subfamilia, fin.cod_cli';
		$valor_ordenar = 'order by fin.cod_subfamilia, fin.cliente';
		$valor_agrupar2 = 'group by fin2.cod_subfamilia';
		$valor_ordenar2 = 'order by fin2.cod_subfamilia, fin2.cliente';
		$valor_porfamilia = 'count(fin2.cod_subfamilia) as num_reg';
		$nom_porfamilia = '(Sub Familia)';
		}
		if($porfamilia == 2) {
		$valor_agrupar = 'group by fin.cod_grupo, fin.cod_cli';
		$valor_ordenar = 'order by fin.cod_grupo, fin.cliente';
		$valor_agrupar2 = 'group by fin2.cod_grupo';
		$valor_ordenar2 = 'order by fin2.cod_grupo, fin2.cliente';
		$valor_porfamilia = 'count(fin2.cod_grupo) as num_reg';
		$nom_porfamilia = '(Grupo)';
		}
		if($porfamilia == 3) {
		$valor_agrupar = 'group by fin.cod_subgrupo, fin.cod_cli';
		$valor_ordenar = 'order by fin.cod_subgrupo, fin.cliente';
		$valor_agrupar2 = 'group by fin2.cod_subgrupo';
		$valor_ordenar2 = 'order by fin2.cod_subgrupo, fin2.cliente';
		$valor_porfamilia = 'count(fin2.cod_subgrupo) as num_reg';
		$nom_porfamilia = '(Sub Grupo)';
		}
	}
	$nom_orden = 'Cliente';
	$nom_ventas = 'Cliente';
	$mat_ventas = array();
	$i = 1;
	$resultcli = mysqli_query($link, "select pkyCompany as codigo, fldName as cliente
		from agnCompany
		where
		fldIsClient = 1 and
		sysEnabled = 1 and sysAuthorized = 1
		order by cliente;");
	while ($row = mysqli_fetch_array($resultcli)) {
	$mat_ventas[$i][1] = $row['codigo'];
	$mat_ventas[$i][2] = $row['cliente'];
	$i++;
	}
	mysqli_free_result($resultcli);
	$fin = $i - 1;
} //fin if ventas = 2
if ($ventas == 3) { //if ventas = 3
	$item = 0;
	$cliente = 0;  
	$canal_venta = 0;
	$vendedor = 0;  
	$local = 0;
	$ciudad = 0;
	if($agrupado == 0) {
	$valor_agrupar = 'group by fin.cod_alm';
	$valor_ordenar = 'order by fin.almacen';
	$nom_porfamilia = '';
	} else {
		if($porfamilia == 0) {
		$valor_agrupar = 'group by fin.cod_familia, fin.cod_alm';
		$valor_ordenar = 'order by fin.cod_familia, fin.almacen';
		$valor_agrupar2 = 'group by fin2.cod_familia';
		$valor_ordenar2 = 'order by fin2.cod_familia, fin2.almacen';
		$valor_porfamilia = 'count(fin2.cod_familia) as num_reg';
		$nom_porfamilia = '(Familia)';
		}
		if($porfamilia == 1) {
		$valor_agrupar = 'group by fin.cod_subfamilia, fin.cod_alm';
		$valor_ordenar = 'order by fin.cod_subfamilia, fin.almacen';
		$valor_agrupar2 = 'group by fin2.cod_subfamilia';
		$valor_ordenar2 = 'order by fin2.cod_subfamilia, fin2.almacen';
		$valor_porfamilia = 'count(fin2.cod_subfamilia) as num_reg';
		$nom_porfamilia = '(Sub Familia)';
		}
		if($porfamilia == 2) {
		$valor_agrupar = 'group by fin.cod_grupo, fin.cod_alm';
		$valor_ordenar = 'order by fin.cod_grupo, fin.almacen';
		$valor_agrupar2 = 'group by fin2.cod_grupo';
		$valor_ordenar2 = 'order by fin2.cod_grupo, fin2.almacen';
		$valor_porfamilia = 'count(fin2.cod_grupo) as num_reg';
		$nom_porfamilia = '(Grupo)';
		}
		if($porfamilia == 3) {
		$valor_agrupar = 'group by fin.cod_subgrupo, fin.cod_alm';
		$valor_ordenar = 'order by fin.cod_subgrupo, fin.almacen';
		$valor_agrupar2 = 'group by fin2.cod_subgrupo';
		$valor_ordenar2 = 'order by fin2.cod_subgrupo, fin2.almacen';
		$valor_porfamilia = 'count(fin2.cod_subgrupo) as num_reg';
		$nom_porfamilia = '(Sub Grupo)';
		}
	}
	$nom_orden = 'Almacen';
	$nom_ventas = 'Almacen';
	$mat_ventas = array();
	$i = 1;
	$resultalm = mysqli_query($link, "select pkyWarehouse as codigo, fldName as almacen
		from invWarehouse
		where
		sysEnabled = 1 and sysAuthorized = 1
		order by almacen;");
	while ($row = mysqli_fetch_array($resultalm)) {
	$mat_ventas[$i][1] = $row['codigo'];
	$mat_ventas[$i][2] = $row['almacen'];
	$i++;
	}
	mysqli_free_result($resultalm);
	$fin = $i - 1;
}//fin if ventas = 3
if ($ventas == 4) { //if ventas = 4
	$item = 0;
	$cliente = 0;  
	$canal_venta = 0;
	$vendedor = 0;  
	$almacen = 0;
	$ciudad = 0;
	if($agrupado == 0) {
	$valor_agrupar = 'group by fin.cod_local';
	$valor_ordenar = 'order by fin.local_venta';
	$nom_porfamilia = '';
	} else {
		if($porfamilia == 0) {
		$valor_agrupar = 'group by fin.cod_familia, fin.cod_local';
		$valor_ordenar = 'order by fin.cod_familia, fin.local_venta';
		$valor_agrupar2 = 'group by fin2.cod_familia';
		$valor_ordenar2 = 'order by fin2.cod_familia, fin2.local_venta';
		$valor_porfamilia = 'count(fin2.cod_familia) as num_reg';
		$nom_porfamilia = '(Familia)';
		}
		if($porfamilia == 1) {
		$valor_agrupar = 'group by fin.cod_subfamilia, fin.cod_local';
		$valor_ordenar = 'order by fin.cod_subfamilia, fin.local_venta';
		$valor_agrupar2 = 'group by fin2.cod_subfamilia';
		$valor_ordenar2 = 'order by fin2.cod_subfamilia, fin2.local_venta';
		$valor_porfamilia = 'count(fin2.cod_subfamilia) as num_reg';
		$nom_porfamilia = '(Sub Familia)';
		}
		if($porfamilia == 2) {
		$valor_agrupar = 'group by fin.cod_grupo, fin.cod_local';
		$valor_ordenar = 'order by fin.cod_grupo, fin.local_venta';
		$valor_agrupar2 = 'group by fin2.cod_grupo';
		$valor_ordenar2 = 'order by fin2.cod_grupo, fin2.local_venta';
		$valor_porfamilia = 'count(fin2.cod_grupo) as num_reg';
		$nom_porfamilia = '(Grupo)';
		}
		if($porfamilia == 3) {
		$valor_agrupar = 'group by fin.cod_subgrupo, fin.cod_local';
		$valor_ordenar = 'order by fin.cod_subgrupo, fin.local_venta';
		$valor_agrupar2 = 'group by fin2.cod_subgrupo';
		$valor_ordenar2 = 'order by fin2.cod_subgrupo, fin2.local_venta';
		$valor_porfamilia = 'count(fin2.cod_subgrupo) as num_reg';
		$nom_porfamilia = '(Sub Grupo)';
		}
	}
	$nom_orden = 'Punto o Local de venta';
	$nom_ventas = 'Punto o Local de venta';
	$mat_ventas = array();
	$i = 1;
	$resultpto = mysqli_query($link, "select pkySalesPlace as codigo, fldName as local
		from slsSalesPlace
		where
		sysEnabled = 1 and sysAuthorized = 1
		order by local;");
	while ($row = mysqli_fetch_array($resultpto)) {
	$mat_ventas[$i][1] = $row['codigo'];
	$mat_ventas[$i][2] = $row['local'];
	$i++;
	}
	mysqli_free_result($resultpto);
	$fin = $i - 1;
}//fin if ventas = 4
if ($ventas == 5) { //if ventas = 5
	$item = 0;
	$cliente = 0;  
	$canal_venta = 0;
	$vendedor = 0;  
	$almacen = 0;
	$local = 0;
	if($agrupado == 0) {
	$valor_agrupar = 'group by fin.cod_ciudad';
	$valor_ordenar = 'order by fin.ciudad';
	$nom_porfamilia = '';
	} else {
		if($porfamilia == 0) {
		$valor_agrupar = 'group by fin.cod_familia, fin.cod_ciudad';
		$valor_ordenar = 'order by fin.cod_familia, fin.ciudad';
		$valor_agrupar2 = 'group by fin2.cod_familia';
		$valor_ordenar2 = 'order by fin2.cod_familia, fin2.ciudad';
		$valor_porfamilia = 'count(fin2.cod_familia) as num_reg';
		$nom_porfamilia = '(Familia)';
		}
		if($porfamilia == 1) {
		$valor_agrupar = 'group by fin.cod_subfamilia, fin.cod_ciudad';
		$valor_ordenar = 'order by fin.cod_subfamilia, fin.ciudad';
		$valor_agrupar2 = 'group by fin2.cod_subfamilia';
		$valor_ordenar2 = 'order by fin2.cod_subfamilia, fin2.ciudad';
		$valor_porfamilia = 'count(fin2.cod_subfamilia) as num_reg';
		$nom_porfamilia = '(Sub Familia)';
		}
		if($porfamilia == 2) {
		$valor_agrupar = 'group by fin.cod_grupo, fin.cod_ciudad';
		$valor_ordenar = 'order by fin.cod_grupo, fin.ciudad';
		$valor_agrupar2 = 'group by fin2.cod_grupo';
		$valor_ordenar2 = 'order by fin2.cod_grupo, fin2.ciudad';
		$valor_porfamilia = 'count(fin2.cod_grupo) as num_reg';
		$nom_porfamilia = '(Grupo)';
		}
		if($porfamilia == 3) {
		$valor_agrupar = 'group by fin.cod_subgrupo, fin.cod_ciudad';
		$valor_ordenar = 'order by fin.cod_subgrupo, fin.ciudad';
		$valor_agrupar2 = 'group by fin2.cod_subgrupo';
		$valor_ordenar2 = 'order by fin2.cod_subgrupo, fin2.ciudad';
		$valor_porfamilia = 'count(fin2.cod_subgrupo) as num_reg';
		$nom_porfamilia = '(Sub Grupo)';
		}
	}
	$nom_orden = 'Ciudad';
	$nom_ventas = 'Ciudad';
	$mat_ventas = array();
	$i = 1;
	$resultciu = mysqli_query($link, "select a.codCityLocation as codigo, b.fldName as ciudad
		from
		agnCompany a, regCity b
		where
		a.fldIsClient = 1 and 
		a.codCityLocation = b.pkyCity and 
		a.sysEnabled = 1 and a.sysAuthorized = 1 
		group by codigo 
		order by ciudad;");
	while ($row = mysqli_fetch_array($resultciu)) {
	$mat_ventas[$i][1] = $row['codigo'];
	$mat_ventas[$i][2] = $row['ciudad'];
	$i++;
	}
	mysqli_free_result($resultciu);
	$fin = $i - 1;
}//fin if ventas = 5


if ($tipo_nota == 0) { //if tipo nota
	$nom_tipo_nota = 'Todos';
	$valor_tipo_nota = ' ';
  } else { 
  	$valor_tipo_nota = ' a.codType = '.$tipo_nota.' and ';
	//nombre de tipo de nota
$resulttipo = mysqli_query($link, "select pkySalesNoteType as codigo, fldName as tipo
from 
slsSalesNoteType
where
pkySalesNoteType = '$tipo_nota' and 
sysEnabled = 1 and sysAuthorized = 1
order by tipo;");
	while ($row = mysqli_fetch_array($resulttipo)) {
	$nom_tipo_nota = $row['tipo'];
	}
	mysqli_free_result($resulttipo);
  } //fin if tipo nota

if ($item == 0) { //if item
	$nom_item = 'Todos';
	$valor_item = ' ';
  } else { 
  	$valor_item = ' h.codItem = '.$item.' and ';
	//nombre de item
$resultit = mysqli_query($link, "select pkyItem as codigo, fldName as item, fldFactoryKey as cod_fabrica
from 
invItem
where
pkyItem = '$item' and
sysEnabled = 1 and sysAuthorized = 1
order by item;");
	while ($row = mysqli_fetch_array($resultit)) {
	$nom_item = $row['codigo'].' - '.$row['cod_fabrica'].' - '.$row['item'];
	}
	mysqli_free_result($resultit);
  } //fin if item

 if ($vendedor == 0) { //if vendedor
    $nom_vendedor = 'Todos';
	$valor_vendedor = ' ';
  } else { 
    $valor_vendedor = ' a.codSeller = '.$vendedor.' and ';
	//$valor_vendedor1 = ' and g.codSeller = '.$vendedor.' ';
	//nombre de vendedor
	$resultven = 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
fldIsEmployee = 1 and
fldCanSell = 1 and 
pkyPerson = '$vendedor' and
sysEnabled = 1 and sysAuthorized = 1;");
	while ($row = mysqli_fetch_array($resultven)) {
	$nom_vendedor = $row['codigo'].' - '.$row['vendedor'];
	}
	mysqli_free_result($resultven);
  } //fin if vendedor

if ($cliente == 0) { //if cliente
    $nom_cliente = 'Todos';
	$valor_cliente = ' ';
  } else { 
    $valor_cliente = ' a.codClient = '.$cliente.' and ';
	//$valor_cliente1 = ' and g.codClient = '.$cliente.' ';
	//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 ($almacen == 0) { //if almacen
    $nom_almacen = 'Todos';
	$valor_almacen = ' ';
  } 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' and 
sysEnabled = 1 and sysAuthorized = 1
order by almacen;");
	while ($row = mysqli_fetch_array($resultalm)) {
	$nom_almacen = $row['almacen'];
	}
	mysqli_free_result($resultalm);
  } //fin if almacen
  
if ($local == 0) { //if local
    $nom_local = 'Todos';
	$valor_local = ' ';
  } else { 
    $valor_local = ' a.codSalesPlace = '.$local.' and ';
	//nombre de local
	$resultloc = mysqli_query($link, "select pkySalesPlace as codigo, fldName as local
from slsSalesPlace
where
pkySalesPlace = '$local' and 
sysEnabled = 1 and sysAuthorized = 1
order by local;");
	while ($row = mysqli_fetch_array($resultloc)) {
	$nom_local = $row['local'];
	}
	mysqli_free_result($resultloc);
  } //fin if local

if ($ciudad == 0) { //if ciudad
    $nom_ciudad = 'Todos';
	$valor_ciudad = ' ';
  } else { 
    $valor_ciudad = ' c.codCityLocation = '.$ciudad.' and ';
	//nombre de local
	$resultciu = mysqli_query($link, "select a.codCityLocation as codigo, b.fldName as ciudad
from
agnCompany a, regCity b
where
a.fldIsClient = 1 and 
a.codCityLocation = '$ciudad' and 
a.codCityLocation = b.pkyCity and 
a.sysEnabled = 1 and a.sysAuthorized = 1 
group by codigo 
order by ciudad;");
	while ($row = mysqli_fetch_array($resultciu)) {
	$nom_ciudad = $row['ciudad'];
	}
	mysqli_free_result($resultciu);
  } //fin if ciudad

if ($familia == 0) { // if familia
		$nom_familia = 'Todos';
		$valor_familia = ' ';
	} else {
		//nombre familia
		$resultfam = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$familia' and 
							sysEnabled = 1 and sysAuthorized = 1 
							order by cod;");
		while ($row = mysqli_fetch_array($resultfam)) {
			$nom_familia = $row['nombre'];
		}
		mysqli_free_result($resultfam);
		$valor_familia = ' i.codFamily = ' .$familia. ' and ';
	} //fin if familia

if ($subfamilia == 0) { // if subfamilia
		$nom_subfamilia = 'Todos';
		$valor_subfamilia = ' ';
	} else {
		//nombre sub familia
		$resultsubf = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$subfamilia' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($resultsubf)) {
			$nom_subfamilia = $row['nombre'];
		}
		mysqli_free_result($resultsubf);
		$valor_subfamilia = ' i.codSubFamily = ' .$subfamilia. ' and ';
	} //fin if subfamilia
	
if ($grupo == 0) { // if grupo
		$nom_grupo = 'Todos';
		$valor_grupo = ' ';
	} else {
		//nombre grupo
		$resultgru = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$grupo' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($resultgru)) {
			$nom_grupo = $row['nombre'];
		}
		mysqli_free_result($resultgru);
		$valor_grupo = ' i.codGroup = ' .$grupo. ' and ';
	} //fin if grupo
	
if ($subgrupo == 0) { // if subgrupo
		$nom_subgrupo = 'Todos';
		$valor_subgrupo = ' ';
	} else {
		//nombre sub grupo
		$resultsubg = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$subgrupo' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($resultsubg)) {
			$nom_subgrupo = $row['nombre'];
		}
		mysqli_free_result($resultsubg);
		$valor_subgrupo = ' i.codSubGroup = ' .$subgrupo. ' and ';
	} //fin if subgrupo


if($agrupado != 0) { //if agrupado != 0

	$mat_porfamilia = array();
	$f = 1;
	if ($porfamilia == 0) { // if porfamilia 0
		//todas las familias
		if($familia == 0) { //if familia 0
			$resultfam = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
								from 
								invItemCategory
								where 
								fldLevel = 1 and 
								sysEnabled = 1 and sysAuthorized = 1 
								order by cod;");
			while ($row = mysqli_fetch_array($resultfam)) {
				$mat_porfamilia[$f][1] = $row['cod'];
				$mat_porfamilia[$f][2] = $row['nombre'];
				//echo $mat_porfamilia[$f][1].' - ';
				$f++;
			}
			mysqli_free_result($resultfam);
		} else { //else if familia 0
		//filtro de familia
			$resultfam = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
								from 
								invItemCategory
								where
								pkyItemCategory = '$familia' and 
								sysEnabled = 1 and sysAuthorized = 1;");
			while ($row = mysqli_fetch_array($resultfam)) {
				$mat_porfamilia[$f][1] = $row['cod'];
				$mat_porfamilia[$f][2] = $row['nombre'];
				$f++;
			}
			mysqli_free_result($resultfam);
		} //fin if familia 0
	} //fin if porfamilia 0
	
	if ($porfamilia == 1) { // if porfamilia 1
		//todas las subfamilias
		if($subfamilia == 0) { //if subfamilia 0
			$resultfam = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
								from 
								invItemCategory
								where 
								fldLevel = 2 and 
								sysEnabled = 1 and sysAuthorized = 1 
								order by cod;");
			while ($row = mysqli_fetch_array($resultfam)) {
				$mat_porfamilia[$f][1] = $row['cod'];
				$mat_porfamilia[$f][2] = $row['nombre'];
				$f++;
			}
			mysqli_free_result($resultfam);
		} else { //else if subfamilia 0
		//filtro de subfamilia
			$resultfam = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
								from 
								invItemCategory
								where
								pkyItemCategory = '$subfamilia' and 
								sysEnabled = 1 and sysAuthorized = 1;");
			while ($row = mysqli_fetch_array($resultfam)) {
				$mat_porfamilia[$f][1] = $row['cod'];
				$mat_porfamilia[$f][2] = $row['nombre'];
				$f++;
			}
			mysqli_free_result($resultfam);
		} //fin if subfamilia 0
	} //fin if porfamilia 1
	
	if ($porfamilia == 2) { // if porfamilia 2
		//todas los grupos
		if($grupo == 0) { //if grupo 0
			$resultfam = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
								from 
								invItemCategory
								where 
								fldLevel = 3 and 
								sysEnabled = 1 and sysAuthorized = 1 
								order by cod;");
			while ($row = mysqli_fetch_array($resultfam)) {
				$mat_porfamilia[$f][1] = $row['cod'];
				$mat_porfamilia[$f][2] = $row['nombre'];
				$f++;
			}
			mysqli_free_result($resultfam);
		} else { //else if grupo 0
		//filtro de grupo
			$resultfam = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
								from 
								invItemCategory
								where
								pkyItemCategory = '$grupo' and 
								sysEnabled = 1 and sysAuthorized = 1;");
			while ($row = mysqli_fetch_array($resultfam)) {
				$mat_porfamilia[$f][1] = $row['cod'];
				$mat_porfamilia[$f][2] = $row['nombre'];
				$f++;
			}
			mysqli_free_result($resultfam);
		} //fin if grupo 0
	} //fin if porfamilia 2
	
	if ($porfamilia == 3) { // if porfamilia 3
		//todas los subgrupos
		if($grupo == 0) { //if subgrupo 0
			$resultfam = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
								from 
								invItemCategory
								where 
								fldLevel = 4 and 
								sysEnabled = 1 and sysAuthorized = 1 
								order by cod;");
			while ($row = mysqli_fetch_array($resultfam)) {
				$mat_porfamilia[$f][1] = $row['cod'];
				$mat_porfamilia[$f][2] = $row['nombre'];
				$f++;
			}
			mysqli_free_result($resultfam);
		} else { //else if subgrupo 0
		//filtro de subgrupo
			$resultfam = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
								from 
								invItemCategory
								where
								pkyItemCategory = '$subgrupo' and 
								sysEnabled = 1 and sysAuthorized = 1;");
			while ($row = mysqli_fetch_array($resultfam)) {
				$mat_porfamilia[$f][1] = $row['cod'];
				$mat_porfamilia[$f][2] = $row['nombre'];
				$f++;
			}
			mysqli_free_result($resultfam);
		} //fin if subgrupo 0
	} //fin if porfamilia 3

$fin_porfam = $f - 1;

} //fin if agrupado != 0

$fechames = array();

$fechames[1][1] = 1;
$fechames[1][2] = 'Enero';
$fechames[1][3] = '-01-01';
$fechames[1][4] = '-01-31';
$fechames[2][1] = 2;
$fechames[2][2] = 'Febrero';
$fechames[2][3] = '-02-01';
$es_bisiesto = esBisiesto($anoini);
if($es_bisiesto == 1) {
	$fechames[2][4] = '-02-29';
} else {
	$fechames[2][4] = '-02-28';
}
$fechames[3][1] = 3;
$fechames[3][2] = 'Marzo';
$fechames[3][3] = '-03-01';
$fechames[3][4] = '-03-31';
$fechames[4][1] = 4;
$fechames[4][2] = 'Abril';
$fechames[4][3] = '-04-01';
$fechames[4][4] = '-04-30';
$fechames[5][1] = 5;
$fechames[5][2] = 'Mayo';
$fechames[5][3] = '-05-01';
$fechames[5][4] = '-05-31';
$fechames[6][1] = 6;
$fechames[6][2] = 'Junio';
$fechames[6][3] = '-06-01';
$fechames[6][4] = '-06-30';
$fechames[7][1] = 7;
$fechames[7][2] = 'Julio';
$fechames[7][3] = '-07-01';
$fechames[7][4] = '-07-31';
$fechames[8][1] = 8;
$fechames[8][2] = 'Agosto';
$fechames[8][3] = '-08-01';
$fechames[8][4] = '-08-31';
$fechames[9][1] = 9;
$fechames[9][2] = 'Septiembre';
$fechames[9][3] = '-09-01';
$fechames[9][4] = '-09-30';
$fechames[10][1] = 10;
$fechames[10][2] = 'Octubre';
$fechames[10][3] = '-10-01';
$fechames[10][4] = '-10-31';
$fechames[11][1] = 11;
$fechames[11][2] = 'Noviembre';
$fechames[11][3] = '-11-01';
$fechames[11][4] = '-11-30';
$fechames[12][1] = 12;
$fechames[12][2] = 'Diciembre';
$fechames[12][3] = '-12-01';
$fechames[12][4] = '-12-31';

$m = $mes_ini;
$a = $anoini;

//generar la matriz ventas repitiendo por familia, subfamilia, grupo, subgrupo, segun la elección de item, vendedor, cliente, almacen, punto, ciudad
if($agrupado != 0) { //if agrupado 1

	//vacio la mat_ventas para llenar nuevamente
	$transit = array();
	for($mv = 1; $mv <= $fin; $mv++) { //for mv
		$transit[$mv][1] = $mat_ventas[$mv][1];
		$transit[$mv][2] = $mat_ventas[$mv][2];
	} //fin for mv
	$sec = 1;
	for($gf = 1; $gf <= $fin_porfam; $gf++) { //for gf
		for($gv = 1; $gv <= $fin; $gv++) { //for gv
			$mat_ventas[$sec][0] = $mat_porfamilia[$gf][1];
			$mat_ventas[$sec][1] = $transit[$gv][1];
			$mat_ventas[$sec][2] = $transit[$gv][2];
			$sec++;
		} //fin for gv
	} //fin for gf
	$fin = $sec - 1;
} //fin if agrupado 1

$tres = 0; //variable que aumenta en tres para los indices de la matriz al igualar

//for para calcular las ventas por mes 12 meses
for($cont = 1; $cont <= 12; $cont++) { //for cont
	if($m > 12) {//if $m
		$a++;
		$m = 1;
	}//fin $m
	$fechames[$m][3] = $a . $fechames[$m][3];
	$fechames[$m][4] = $a . $fechames[$m][4];
	$fechames[$m][5] = $a;
	$fechaini = $fechames[$m][3];
	$fechafin = $fechames[$m][4];
	
	if($cont == 1) { $fechaini2 = $fechaini; }
	if($cont == 12) { $fechafin2 = $fechafin; }
//listado de ventas del periodo
//cuidado con el movimiento de stock que no genera cuando el item es tipo servicio t3 en el query
$result = mysqli_query($link, "select fin.cod_item as cod_item, fin.item as item, 
fin.cod_cli as cod_cli, fin.cliente as cliente, 
fin.cod_vendedor as cod_vendedor, fin.vendedor as vendedor, 
fin.cod_alm as cod_alm, fin.almacen as almacen, 
fin.cod_local as cod_local, fin.local_venta as local_venta, 
fin.cod_ciudad as cod_ciudad, fin.ciudad as ciudad, 
fin.cod_familia as cod_familia, fin.familia as familia, 
fin.cod_subfamilia as cod_subfamilia, fin.cod_grupo as cod_grupo, fin.cod_subgrupo as cod_subgrupo, sum(fin.cantidad) as cantidad, fin.precio_fin as precio, 
fin.desc_porc as desc_porc, sum(fin.precio_total_fin) as precio_total, 
fin.costo_u_fin as costo_u, sum(fin.total_costo_fin) as total_costo, 
fin.margen_u as margen_u, sum(fin.margen) as margen, 
sum(fin.comision_total) as comision_total, fin.comision_u as comision_u, fin.cod_fabrica as cod_fabrica
from (
select t2.cod_item as cod_item, t2.item as item, 
t2.fecha as fecha, 
t2.nro_doc as nro_doc, t2.cliente as cliente, 
t2.vendedor as vendedor, t2.cod_alm as cod_alm,
t2.almacen as almacen, t2.local_venta as local_venta, t2.cod_local as cod_local,
t2.moneda_venta as moneda_venta, t2.cod_tc_venta as cod_tc_venta, 
t2.cantidad as cantidad, 
t2.precio as precio, t2.desc_porc as desc_porc,
t2.precio_total as precio_total,
t2.cod_cxc as cod_cxc, 
t2.cod_moneda_venta as cod_moneda_venta, 
t2.cod_moneda_costo as cod_moneda_costo, 
t2.moneda_costo as moneda_costo, 
t2.cant_costo as cant_costo, t2.costo_u as costo_u,
t2.total_costo as total_costo, t2.tasa as tasa,
t2.precio_fin as precio_fin,
t2.precio_total_fin as precio_total_fin, 
tc2.fldRate as tasa_costo,
t2.costo_u * tc2.fldRate as costo_u_fin,
t2.total_costo * tc2.fldRate as total_costo_fin,
(t2.precio_fin - t2.comision_u - (t2.costo_u * tc2.fldRate)) as margen_u,
(t2.precio_total_fin - t2.comision_total - (t2.total_costo * tc2.fldRate)) as margen,
t2.cod_cli as cod_cli,
t2.cod_vendedor as cod_vendedor, 
t2.cod_ciudad as cod_ciudad, t2.ciudad as ciudad, 
t2.cod_familia as cod_familia, t2.familia as familia, 
t2.cod_subfamilia as cod_subfamilia, t2.cod_grupo as cod_grupo, t2.cod_subgrupo as cod_subgrupo, 
t2.comision_total as comision_total, t2.comision_u as comision_u, t2.cod_fabrica as cod_fabrica 
from (
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc, t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
t.cod_moneda_costo as cod_moneda_costo, t.moneda_costo as moneda_costo, t.cant_costo as cant_costo, t.costo_u as costo_u, t.total_costo as total_costo, tc.fldRate as tasa, t.precio * tc.fldRate as precio_fin, t.precio_total * tc.fldRate as precio_total_fin, t.cod_cli as cod_cli,
t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo, t.comision * tc.fldRate as comision_total, t.comision * tc.fldRate / t.cantidad as comision_u, t.cod_fabrica as cod_fabrica
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta,  k.codCurrency as cod_moneda_costo, l.fldName as moneda_costo, j.fldQuantity as cant_costo, j.fldCost as costo_u, (h.fldQuantity * j.fldCost) + j.fldExtraCost as total_costo, a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo, h.fldCommission as comision, i.fldFactoryKey as cod_fabrica   
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f,  
slsSalesNoteDetail h, invItem i, invItemMovementDetail j, invItemMovement k, actCurrency l, regCity m, invItemCategory o
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini' and 
a.fldDate <= '$fechafin' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
k.codCurrency = l.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
h.codSalesNote = a.pkySalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement = j.codItemMovement and 
h.codItem = j.codItem and 
a.codItemMovement = k.pkyItemMovement and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t, actExchangeRateDetail tc
where
tc.codTargetCurrency = t.cod_moneda_venta and
tc.codBaseCurrency = '$otra_moneda' and
tc.codExchangeRate = t.cod_tc_venta 
union all
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc,
t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
t.cod_moneda_costo as cod_moneda_costo, t.moneda_costo as moneda_costo, t.cant_costo as cant_costo, t.costo_u as costo_u, t.total_costo as total_costo, 1 as tasa, t.precio as precio_fin, t.precio_total as precio_total_fin, t.cod_cli as cod_cli, t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo, t.comision as comision_total, t.comision / t.cantidad as comision_u, t.cod_fabrica as cod_fabrica 
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta,  k.codCurrency as cod_moneda_costo, l.fldName as moneda_costo, h.fldQuantity as cant_costo, j.fldCost as costo_u,
(h.fldQuantity * j.fldCost) + j.fldExtraCost as total_costo, a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo, h.fldCommission as comision, i.fldFactoryKey as cod_fabrica  
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f, 
slsSalesNoteDetail h, invItem i, invItemMovementDetail j, invItemMovement k, actCurrency l, regCity m, invItemCategory o
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini' and 
a.fldDate <= '$fechafin' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
k.codCurrency = l.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
h.codSalesNote = a.pkySalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement = j.codItemMovement and 
h.codItem = j.codItem and 
a.codItemMovement = k.pkyItemMovement and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_venta = '$otra_moneda') t2, actExchangeRateDetail tc2 
where
tc2.codTargetCurrency = t2.cod_moneda_costo and
tc2.codBaseCurrency = '$otra_moneda' and
tc2.codExchangeRate = t2.cod_tc_venta 

union all

select t2.cod_item as cod_item, t2.item as item, 
t2.fecha as fecha, 
t2.nro_doc as nro_doc, t2.cliente as cliente, 
t2.vendedor as vendedor, t2.cod_alm as cod_alm,
t2.almacen as almacen, t2.local_venta as local_venta, t2.cod_local as cod_local, 
t2.moneda_venta as moneda_venta, t2.cod_tc_venta as cod_tc_venta, 
t2.cantidad as cantidad, 
t2.precio as precio, t2.desc_porc as desc_porc,
t2.precio_total as precio_total,
t2.cod_cxc as cod_cxc, 
t2.cod_moneda_venta as cod_moneda_venta, 
t2.cod_moneda_costo as cod_moneda_costo, 
t2.moneda_costo as moneda_costo, 
t2.cant_costo as cant_costo, t2.costo_u as costo_u,
t2.total_costo as total_costo, t2.tasa as tasa,
t2.precio_fin as precio_fin,
t2.precio_total_fin as precio_total_fin, 
1 as tasa_costo,
t2.costo_u as costo_u_fin,
t2.total_costo as total_costo_fin,
(t2.precio_fin - t2.comision_u - t2.costo_u) as margen_u,
(t2.precio_total_fin - t2.comision_total - t2.total_costo) as margen,
t2.cod_cli as cod_cli,
t2.cod_vendedor as cod_vendedor, 
t2.cod_ciudad as cod_ciudad, t2.ciudad as ciudad, 
t2.cod_familia as cod_familia, t2.familia as familia, 
t2.cod_subfamilia as cod_subfamilia, t2.cod_grupo as cod_grupo, t2.cod_subgrupo as cod_subgrupo, 
t2.comision_total as comision_total, t2.comision_u as comision_u, t2.cod_fabrica as cod_fabrica
from (
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc,
t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
t.cod_moneda_costo as cod_moneda_costo, t.moneda_costo as moneda_costo, t.cant_costo as cant_costo, t.costo_u as costo_u, t.total_costo as total_costo, tc.fldRate as tasa, t.precio * tc.fldRate as precio_fin, t.precio_total * tc.fldRate as precio_total_fin, t.cod_cli as cod_cli,
t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo, t.comision * tc.fldRate as comision_total, t.comision * tc.fldRate / t.cantidad as comision_u, t.cod_fabrica as cod_fabrica 
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta, 
k.codCurrency as cod_moneda_costo, l.fldName as moneda_costo, h.fldQuantity as cant_costo, j.fldCost as costo_u,
(h.fldQuantity * j.fldCost) + j.fldExtraCost as total_costo, a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo, h.fldCommission as comision, i.fldFactoryKey as cod_fabrica  
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f, 
slsSalesNoteDetail h, invItem i, invItemMovementDetail j, invItemMovement k, actCurrency l, regCity m, invItemCategory o
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini' and 
a.fldDate <= '$fechafin' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
k.codCurrency = l.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
h.codSalesNote = a.pkySalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement = j.codItemMovement and 
h.codItem = j.codItem and 
a.codItemMovement = k.pkyItemMovement and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t, actExchangeRateDetail tc
where
tc.codTargetCurrency = t.cod_moneda_venta and
tc.codBaseCurrency = '$otra_moneda' and
tc.codExchangeRate = t.cod_tc_venta 
union all
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc, t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
t.cod_moneda_costo as cod_moneda_costo, t.moneda_costo as moneda_costo, t.cant_costo as cant_costo, t.costo_u as costo_u, t.total_costo as total_costo, 1 as tasa, t.precio as precio_fin, t.precio_total as precio_total_fin, t.cod_cli as cod_cli, t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo, t.comision as comision_total, t.comision / t.cantidad as comision_u, t.cod_fabrica as cod_fabrica 
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta, 
k.codCurrency as cod_moneda_costo, l.fldName as moneda_costo, h.fldQuantity as cant_costo, j.fldCost as costo_u,
(h.fldQuantity * j.fldCost) + j.fldExtraCost as total_costo, a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo, h.fldCommission as comision, i.fldFactoryKey as cod_fabrica  
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f, 
slsSalesNoteDetail h, invItem i, invItemMovementDetail j, invItemMovement k, actCurrency l, regCity m, invItemCategory o 
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini' and 
a.fldDate <= '$fechafin' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
k.codCurrency = l.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
h.codSalesNote = a.pkySalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement = j.codItemMovement and 
h.codItem = j.codItem and 
a.codItemMovement = k.pkyItemMovement and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_venta = '$otra_moneda') t2 
where
t2.cod_moneda_costo = '$otra_moneda' 



union all



select t3.cod_item as cod_item, t3.item as item, 
t3.fecha as fecha, 
t3.nro_doc as nro_doc, t3.cliente as cliente, 
t3.vendedor as vendedor, t3.cod_alm as cod_alm,
t3.almacen as almacen, t3.local_venta as local_venta, t3.cod_local as cod_local, 
t3.moneda_venta as moneda_venta, t3.cod_tc_venta as cod_tc_venta, 
t3.cantidad as cantidad, 
t3.precio as precio, t3.desc_porc as desc_porc,
t3.precio_total as precio_total,
t3.cod_cxc as cod_cxc, 
t3.cod_moneda_venta as cod_moneda_venta, 
NULL as cod_moneda_costo, 
NULL as moneda_costo, 
0 as cant_costo, 0 as costo_u,
0 as total_costo, t3.tasa as tasa,
t3.precio_fin as precio_fin,
t3.precio_total_fin as precio_total_fin, 
1 as tasa_costo,
0 as costo_u_fin,
0 as total_costo_fin,
(t3.precio_fin - t3.comision_u - 0) as margen_u,
(t3.precio_total_fin - t3.comision_total - 0) as margen,
t3.cod_cli as cod_cli,
t3.cod_vendedor as cod_vendedor, 
t3.cod_ciudad as cod_ciudad, t3.ciudad as ciudad, 
t3.cod_familia as cod_familia, t3.familia as familia, 
t3.cod_subfamilia as cod_subfamilia, t3.cod_grupo as cod_grupo, t3.cod_subgrupo as cod_subgrupo, 
t3.comision_total as comision_total, t3.comision_u as comision_u, t3.cod_fabrica as cod_fabrica
from (
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc, t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
tc.fldRate as tasa, t.precio * tc.fldRate as precio_fin, t.precio_total * tc.fldRate as precio_total_fin, t.cod_cli as cod_cli, t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo, t.comision * tc.fldRate as comision_total, t.comision * tc.fldRate / t.cantidad as comision_u, t.cod_fabrica as cod_fabrica 
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, 
c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta, 
a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo, a.codItemMovement as cod_mov_merc, h.fldCommission as comision, i.fldFactoryKey as cod_fabrica  
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f, 
slsSalesNoteDetail h, invItem i, regCity m, invItemCategory o  
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini' and 
a.fldDate <= '$fechafin' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
a.pkySalesNote = h.codSalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement is NULL and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t, actExchangeRateDetail tc
where
tc.codTargetCurrency = t.cod_moneda_venta and
tc.codBaseCurrency = '$otra_moneda' and
tc.codExchangeRate = t.cod_tc_venta 
union all
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc, t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
1 as tasa, t.precio as precio_fin, t.precio_total as precio_total_fin, t.cod_cli as cod_cli, t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo, t.comision as comision_total, t.comision / t.cantidad as comision_u, t.cod_fabrica as cod_fabrica
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, 
c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta, 
a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo, a.codItemMovement as cod_mov_merc, h.fldCommission as comision, i.fldFactoryKey as cod_fabrica 
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f, 
slsSalesNoteDetail h, invItem i, regCity m, invItemCategory o 
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini' and 
a.fldDate <= '$fechafin' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
a.pkySalesNote = h.codSalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement is NULL and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_venta = '$otra_moneda') t3 ) fin 
$valor_agrupar 
$valor_ordenar;");

//matriz con los resultados de la venta por cada mes
$matriz_item = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	
	if($porfamilia == 0) { $matriz_item[$i][0] = $row['cod_familia']; }
	if($porfamilia == 1) { $matriz_item[$i][0] = $row['cod_subfamilia']; }
	if($porfamilia == 2) { $matriz_item[$i][0] = $row['cod_grupo']; }
	if($porfamilia == 3) { $matriz_item[$i][0] = $row['cod_subgrupo']; }
	//echo $matriz_item[$i][0].' - ';
	if($ventas == 0) {
	$matriz_item[$i][1] = $row['cod_item'];
	$matriz_item[$i][2] = $row['item'];
	}
	if($ventas == 1) {
	$matriz_item[$i][1] = $row['cod_vendedor'];
	$matriz_item[$i][2] = $row['vendedor'];
	}
	if($ventas == 2) {
	$matriz_item[$i][1] = $row['cod_cli'];
	$matriz_item[$i][2] = $row['cliente'];
	}
	if($ventas == 3) {
	$matriz_item[$i][1] = $row['cod_alm'];
	$matriz_item[$i][2] = $row['almacen'];
	}
	if($ventas == 4) {
	$matriz_item[$i][1] = $row['cod_local'];
	$matriz_item[$i][2] = $row['local_venta'];
	}
	if($ventas == 5) {
	$matriz_item[$i][1] = $row['cod_ciudad'];
	$matriz_item[$i][2] = $row['cuidad'];
	}
	$matriz_item[$i][3] = $row['cantidad'];
	$matriz_item[$i][4] = $row['precio'];
	$matriz_item[$i][5] = $row['precio_total'];
	$matriz_item[$i][6] = $row['costo_u'];
	$matriz_item[$i][7] = $row['total_costo'];
	$matriz_item[$i][8] = $row['margen_u'];
	$matriz_item[$i][9] = $row['margen'];
	//echo $matriz_item[$i][1].' - '.$matriz_item[$i][3].'<br>';
	$i++;
	}
	mysqli_free_result($result);
$fin_item = $i - 1;

//hay que cruzar la matriz con el listado general con la matriz de cada mes
if($agrupado == 0) { //if agrupado 0
	
	for($igual = 1; $igual <= $fin; $igual++) {//for igual
		$encon = 0; //cuando encuentra cambia el valor
		$j = 1;
		while($j <= $fin_item && $encon == 0) {//while $j
			if($mat_ventas[$igual][1] == $matriz_item[$j][1]) {//if mat
				//uso de la variable $tres
				$mat_ventas[$igual][3+$tres] = $matriz_item[$j][3];
				$mat_ventas[$igual][4+$tres] = $matriz_item[$j][5];
				$mat_ventas[$igual][5+$tres] = $matriz_item[$j][9];
				$encon = 1;
			}//fin if mat
			$j++;
		}//fin while $j
	}//fin for igual

} else { //else if agrupado 0
	
	for($igual = 1; $igual <= $fin; $igual++) {//for igual
		$encon = 0; //cuando encuentra cambia el valor
		$j = 1;
		while($j <= $fin_item && $encon == 0) {//while $j
			if($mat_ventas[$igual][1] == $matriz_item[$j][1] && $mat_ventas[$igual][0] == $matriz_item[$j][0]) {//if mat
				//uso de la variable $tres
				$mat_ventas[$igual][3+$tres] = $matriz_item[$j][3];
				$mat_ventas[$igual][4+$tres] = $matriz_item[$j][5];
				$mat_ventas[$igual][5+$tres] = $matriz_item[$j][9];
				$encon = 1;
			}//fin if mat
			$j++;
		}//fin while $j
	}//fin for igual

} //fin if agrupado 0


$tres = $tres + 3;
$m++;
}//fin for cont

  
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 Anuales</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>Ventas anuales por: </b><?php echo $nom_ventas; ?></TD>
        <?php if($ventas == 0){ ?>
		<TD width="25%"><b>Item: </b><?php echo $nom_item; ?></TD>
		<?php } ?>
        <?php if($ventas == 1){ ?>
		<TD width="25%"><b>Vendedor: </b><?php echo $nom_vendedor; ?></TD>
		<?php } ?>
        <?php if($ventas == 2){ ?>
		<TD width="25%"><b>Cliente: </b><?php echo $nom_cliente; ?><br />
        <b>Canal (solo para empresas): </b><?php echo $nom_canal_venta; ?></TD>
		<?php } ?>
        <?php if($ventas == 3){ ?>
		<TD width="25%"><b>Almacen: </b><?php echo $nom_almacen; ?></TD>
		<?php } ?>
        <?php if($ventas == 4){ ?>
		<TD width="25%"><b>Punto o local de venta: </b><?php echo $nom_local; ?></TD>
		<?php } ?>
        <?php if($ventas == 5){ ?>
		<TD width="25%"><b>Ciudad (en base al cliente): </b><?php echo $nom_ciudad; ?></TD>
		<?php } ?>
        <TD width="25%"><b>Agrupado: </b><?php echo $nom_agrupado; ?> <?php echo $nom_porfamilia; ?>&nbsp;<?php echo $nom_porfamiliacon; ?></TD>
        <TD width="25%"><b>Tipo: </b><?php echo $nom_tipo_nota; ?></TD>
        </TR>
        <TR>
        <TD><b>Familia: </b><?php echo $nom_familia; ?></TD>
        <TD><b>Sub Familia: </b><?php echo $nom_subfamilia; ?></TD>
        <TD><b>Grupo: </b><?php echo $nom_grupo; ?></TD>
        <TD><b>Sub Grupo: </b><?php echo $nom_subgrupo; ?></TD>
        </TR>
        <TR>
        <TD><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
        <TD><b>A&ntilde;o: </b><?php echo $anoini; ?></TD>
        <TD colspan="2"><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; ?>" 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 />
<?php
//if($agrupar == 0) { //if agrupar = 0, lista todos los items o todos los clientes o todos los vendedores o todos los almacenes o todos los locales o todas las ciudades, tambien cuando se elige un solo item, cliente, vendedor, almacen, local o ciudad
$m = $mes_ini;
$mes1 = array();
for($me = 1; $me <= 12; $me++) {//for me
	if($m > 12) {//if $m
		$m = 1;
	}//fin $m
	$mes1[$me] = $m;
$m++;
}//fin for me

?>
<table border="1" align="center" cellpadding="2" width="100%">
 	<thead>
    <TR> 
 	<TD align="center" rowspan="2"><b>Cod.</b></TD>
    <?php if($ventas == 0) { ?>
    <?php if($porfamiliacon == 0) { ?>
    <TD align="center" rowspan="2"><b>Cod. Fab.</b></TD>
    <?php } ?>
    <?php } ?>
    <TD align="center" rowspan="2"><b><?php echo $nom_ventas; ?> <?php echo $nom_porfamilia; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[1]][2]; ?> <?php echo $fechames[$mes1[1]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[2]][2]; ?> <?php echo $fechames[$mes1[2]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[3]][2]; ?> <?php echo $fechames[$mes1[3]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[4]][2]; ?> <?php echo $fechames[$mes1[4]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[5]][2]; ?> <?php echo $fechames[$mes1[5]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[6]][2]; ?> <?php echo $fechames[$mes1[6]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[7]][2]; ?> <?php echo $fechames[$mes1[7]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[8]][2]; ?> <?php echo $fechames[$mes1[8]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[9]][2]; ?> <?php echo $fechames[$mes1[9]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[10]][2]; ?> <?php echo $fechames[$mes1[10]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[11]][2]; ?> <?php echo $fechames[$mes1[11]][5]; ?></b></TD>
    <TD align="center" colspan="3"><b><?php echo $fechames[$mes1[12]][2]; ?> <?php echo $fechames[$mes1[12]][5]; ?></b></TD>
	<TD align="center" colspan="3"><b>Totales</b></TD>
    </TR>
    <TR>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
        
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Monto</b></TD>
    <TD align="center"><b>Margen</b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">

    <?php

// cargar listado ventas anuales 
$color = 2;
$color2 = 2;
$suma_cant_1 = 0;
$suma_monto_1 = 0;
$suma_margen_1 = 0;

$suma_cant_2 = 0;
$suma_monto_2 = 0;
$suma_margen_2 = 0;

$suma_cant_3 = 0;
$suma_monto_3 = 0;
$suma_margen_3 = 0;

$suma_cant_4 = 0;
$suma_monto_4 = 0;
$suma_margen_4 = 0;

$suma_cant_5 = 0;
$suma_monto_5 = 0;
$suma_margen_5 = 0;

$suma_cant_6 = 0;
$suma_monto_6 = 0;
$suma_margen_6 = 0;

$suma_cant_7 = 0;
$suma_monto_7 = 0;
$suma_margen_7 = 0;

$suma_cant_8 = 0;
$suma_monto_8 = 0;
$suma_margen_8 = 0;

$suma_cant_9 = 0;
$suma_monto_9 = 0;
$suma_margen_9 = 0;

$suma_cant_10 = 0;
$suma_monto_10 = 0;
$suma_margen_10 = 0;

$suma_cant_11 = 0;
$suma_monto_11 = 0;
$suma_margen_11 = 0;

$suma_cant_12 = 0;
$suma_monto_12 = 0;
$suma_margen_12 = 0;

$suma_cant_1p = 0;
$suma_monto_1p = 0;
$suma_margen_1p = 0;

$suma_cant_2p = 0;
$suma_monto_2p = 0;
$suma_margen_2p = 0;

$suma_cant_3p = 0;
$suma_monto_3p = 0;
$suma_margen_3p = 0;

$suma_cant_4p = 0;
$suma_monto_4p = 0;
$suma_margen_4p = 0;

$suma_cant_5p = 0;
$suma_monto_5p = 0;
$suma_margen_5p = 0;

$suma_cant_6p = 0;
$suma_monto_6p = 0;
$suma_margen_6p = 0;

$suma_cant_7p = 0;
$suma_monto_7p = 0;
$suma_margen_7p = 0;

$suma_cant_8p = 0;
$suma_monto_8p = 0;
$suma_margen_8p = 0;

$suma_cant_9p = 0;
$suma_monto_9p = 0;
$suma_margen_9p = 0;

$suma_cant_10p = 0;
$suma_monto_10p = 0;
$suma_margen_10p = 0;

$suma_cant_11p = 0;
$suma_monto_11p = 0;
$suma_margen_11p = 0;

$suma_cant_12p = 0;
$suma_monto_12p = 0;
$suma_margen_12p = 0;

$suma_cant_h = 0;
$suma_monto_h = 0;
$suma_margen_h = 0;

$suma_cant_t = 0;
$suma_monto_t = 0;
$suma_margen_t = 0;

$suma_cant_tp = 0;
$suma_monto_tp = 0;
$suma_margen_tp = 0;

$i = 1;

if($agrupado == 0) { //if agrupado = 0, lista todos los items o todos los clientes o todos los vendedores o todos los almacenes o todos los locales o todas las ciudades, tambien cuando se elige un solo item, cliente, vendedor, almacen, local o ciudad    	

while($i <= $fin) { //while
	$suma_cant_h = $mat_ventas[$i][3] + $mat_ventas[$i][6] + $mat_ventas[$i][9] + $mat_ventas[$i][12] + $mat_ventas[$i][15] + $mat_ventas[$i][18] + $mat_ventas[$i][21] + $mat_ventas[$i][24] + $mat_ventas[$i][27] + $mat_ventas[$i][30] + $mat_ventas[$i][33] + $mat_ventas[$i][36];
	
	$suma_monto_h = $mat_ventas[$i][4] + $mat_ventas[$i][7] + $mat_ventas[$i][10] + $mat_ventas[$i][13] + $mat_ventas[$i][16] + $mat_ventas[$i][19] + $mat_ventas[$i][22] + $mat_ventas[$i][25] + $mat_ventas[$i][28] + $mat_ventas[$i][31] + $mat_ventas[$i][34] + $mat_ventas[$i][37];
	
	$suma_margen_h = $mat_ventas[$i][5] + $mat_ventas[$i][8] + $mat_ventas[$i][11] + $mat_ventas[$i][14] + $mat_ventas[$i][17] + $mat_ventas[$i][20] + $mat_ventas[$i][23] + $mat_ventas[$i][26] + $mat_ventas[$i][29] + $mat_ventas[$i][32] + $mat_ventas[$i][35] + $mat_ventas[$i][38];
	
	if($suma_cant_h != 0 || $suma_monto_h != 0 || $suma_margen_h != 0) {//if sin mov
	
	if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $mat_ventas[$i][1]);//cod
	} else {
	printf("<tr><td align='center'>%s</td>", $mat_ventas[$i][1]);//cod
	}//cierra if color
	if($ventas == 0) {
		printf("<td align='center'>%s</td>", $mat_ventas[$i][100]);//cod fabrica
	}
	printf("<td>%s</td>", $mat_ventas[$i][2]);//item o cliente o vendedor, etc
	$cod_varios = $mat_ventas[$i][1];
	$nom_varios = $mat_ventas[$i][2];
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][3],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[1]][3];
	$fechafin_mes = $fechames[$mes1[1]][4];
	$titulo_mes = $fechames[$mes1[1]][2]. ' ' .$fechames[$mes1[1]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][4],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][5],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][6],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[2]][3];
	$fechafin_mes = $fechames[$mes1[2]][4];
	$titulo_mes = $fechames[$mes1[2]][2]. ' ' .$fechames[$mes1[2]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][7],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][8],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][9],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[3]][3];
	$fechafin_mes = $fechames[$mes1[3]][4];
	$titulo_mes = $fechames[$mes1[3]][2]. ' ' .$fechames[$mes1[3]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][10],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][11],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][12],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[4]][3];
	$fechafin_mes = $fechames[$mes1[4]][4];
	$titulo_mes = $fechames[$mes1[4]][2]. ' ' .$fechames[$mes1[4]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][13],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][14],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][15],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[5]][3];
	$fechafin_mes = $fechames[$mes1[5]][4];
	$titulo_mes = $fechames[$mes1[5]][2]. ' ' .$fechames[$mes1[5]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][16],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][17],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][18],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[6]][3];
	$fechafin_mes = $fechames[$mes1[6]][4];
	$titulo_mes = $fechames[$mes1[6]][2]. ' ' .$fechames[$mes1[6]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][19],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][20],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][21],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[7]][3];
	$fechafin_mes = $fechames[$mes1[7]][4];
	$titulo_mes = $fechames[$mes1[7]][2]. ' ' .$fechames[$mes1[7]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][22],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][23],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][24],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[8]][3];
	$fechafin_mes = $fechames[$mes1[8]][4];
	$titulo_mes = $fechames[$mes1[8]][2]. ' ' .$fechames[$mes1[8]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][25],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][26],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][27],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[9]][3];
	$fechafin_mes = $fechames[$mes1[9]][4];
	$titulo_mes = $fechames[$mes1[9]][2]. ' ' .$fechames[$mes1[9]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][28],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][29],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][30],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[10]][3];
	$fechafin_mes = $fechames[$mes1[10]][4];
	$titulo_mes = $fechames[$mes1[10]][2]. ' ' .$fechames[$mes1[10]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][31],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][32],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][33],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[11]][3];
	$fechafin_mes = $fechames[$mes1[11]][4];
	$titulo_mes = $fechames[$mes1[11]][2]. ' ' .$fechames[$mes1[11]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][34],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][35],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][36],$n_cantidad));//cantidad
	$fechaini_mes = $fechames[$mes1[12]][3];
	$fechafin_mes = $fechames[$mes1[12]][4];
	$titulo_mes = $fechames[$mes1[12]][2]. ' ' .$fechames[$mes1[12]][5];
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas[$i][37],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas[$i][38],$n_decimales));//margen
	
		
	$suma_cant_1 = $suma_cant_1 + $mat_ventas[$i][3];
	$suma_monto_1 = $suma_monto_1 + $mat_ventas[$i][4];
	$suma_margen_1 = $suma_margen_1 + $mat_ventas[$i][5];
	
	$suma_cant_2 = $suma_cant_2 + $mat_ventas[$i][6];
	$suma_monto_2 = $suma_monto_2 + $mat_ventas[$i][7];
	$suma_margen_2 = $suma_margen_2 + $mat_ventas[$i][8];
	
	$suma_cant_3 = $suma_cant_3 + $mat_ventas[$i][9];
	$suma_monto_3 = $suma_monto_3 + $mat_ventas[$i][10];
	$suma_margen_3 = $suma_margen_3 + $mat_ventas[$i][11];
	
	$suma_cant_4 = $suma_cant_4 + $mat_ventas[$i][12];
	$suma_monto_4 = $suma_monto_4 + $mat_ventas[$i][13];
	$suma_margen_4 = $suma_margen_4 + $mat_ventas[$i][14];
	
	$suma_cant_5 = $suma_cant_5 + $mat_ventas[$i][15];
	$suma_monto_5 = $suma_monto_5 + $mat_ventas[$i][16];
	$suma_margen_5 = $suma_margen_5 + $mat_ventas[$i][17];
	
	$suma_cant_6 = $suma_cant_6 + $mat_ventas[$i][18];
	$suma_monto_6 = $suma_monto_6 + $mat_ventas[$i][19];
	$suma_margen_6 = $suma_margen_6 + $mat_ventas[$i][20];
	
	$suma_cant_7 = $suma_cant_7 + $mat_ventas[$i][21];
	$suma_monto_7 = $suma_monto_7 + $mat_ventas[$i][22];
	$suma_margen_7 = $suma_margen_7 + $mat_ventas[$i][23];
	
	$suma_cant_8 = $suma_cant_8 + $mat_ventas[$i][24];
	$suma_monto_8 = $suma_monto_8 + $mat_ventas[$i][25];
	$suma_margen_8 = $suma_margen_8 + $mat_ventas[$i][26];
	
	$suma_cant_9 = $suma_cant_9 + $mat_ventas[$i][27];
	$suma_monto_9 = $suma_monto_9 + $mat_ventas[$i][28];
	$suma_margen_9 = $suma_margen_9 + $mat_ventas[$i][29];
	
	$suma_cant_10 = $suma_cant_10 + $mat_ventas[$i][30];
	$suma_monto_10 = $suma_monto_10 + $mat_ventas[$i][31];
	$suma_margen_10 = $suma_margen_10 + $mat_ventas[$i][32];
	
	$suma_cant_11 = $suma_cant_11 + $mat_ventas[$i][33];
	$suma_monto_11 = $suma_monto_11 + $mat_ventas[$i][34];
	$suma_margen_11 = $suma_margen_11 + $mat_ventas[$i][35];
	
	$suma_cant_12 = $suma_cant_12 + $mat_ventas[$i][36];
	$suma_monto_12 = $suma_monto_12 + $mat_ventas[$i][37];
	$suma_margen_12 = $suma_margen_12 + $mat_ventas[$i][38];
	
	$suma_cant_t = $suma_cant_t + $suma_cant_h;
	$suma_monto_t = $suma_monto_t + $suma_monto_h;
	$suma_margen_t = $suma_margen_t + $suma_margen_h;
	
	$titulo_mes = $fechames[$mes1[1]][2]. ' ' .$fechames[$mes1[1]][5]. ' a ' .$fechames[$mes1[12]][2]. ' ' .$fechames[$mes1[12]][5];
	printf("<td align='right'>%s</td>", number_format($suma_cant_h,$n_cantidad));//total cantidad
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=0&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini2&fechafin_det=$fechafin2&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($suma_monto_h,$n_decimales));//total monto
	printf("<td align='right'>%s</td></tr>", number_format($suma_margen_h,$n_decimales));//total margen
	
	$color++;
	
	} //fin if sin mov
	$i++;
}//fin while


} else { //else agrupado = 0


//mismo query de arriba, solo se usa para ver el numero de registros
$result_it = mysqli_query($link, "select fin2.cod_item as cod_item, fin2.cod_vendedor as cod_vendedor, fin2.cod_cli as cod_cli, fin2.cod_alm as cod_alm, fin2.cod_local as cod_local, fin2.cod_ciudad as cod_ciudad, fin2.ciudad as ciudad, fin2.cod_familia as cod_familia, fin2.familia as familia, fin2.cod_subfamilia as cod_subfamilia, fin2.cod_grupo as cod_grupo, fin2.cod_subgrupo as cod_subgrupo, $valor_porfamilia 
from(
select fin.cod_item as cod_item, fin.item as item, 
fin.cod_cli as cod_cli, fin.cliente as cliente, 
fin.cod_vendedor as cod_vendedor, fin.vendedor as vendedor, 
fin.cod_alm as cod_alm, fin.almacen as almacen, 
fin.cod_local as cod_local, fin.local_venta as local_venta, 
fin.cod_ciudad as cod_ciudad, fin.ciudad as ciudad, 
fin.cod_familia as cod_familia, fin.familia as familia, 
fin.cod_subfamilia as cod_subfamilia, fin.cod_grupo as cod_grupo, fin.cod_subgrupo as cod_subgrupo, sum(fin.cantidad) as cantidad, fin.precio_fin as precio, 
fin.desc_porc as desc_porc, sum(fin.precio_total_fin) as precio_total, 
fin.costo_u_fin as costo_u, sum(fin.total_costo_fin) as total_costo, 
fin.margen_u as margen_u, sum(fin.margen) as margen
from (
select t2.cod_item as cod_item, t2.item as item, 
t2.fecha as fecha, 
t2.nro_doc as nro_doc, t2.cliente as cliente, 
t2.vendedor as vendedor, t2.cod_alm as cod_alm,
t2.almacen as almacen, t2.local_venta as local_venta, t2.cod_local as cod_local,
t2.moneda_venta as moneda_venta, t2.cod_tc_venta as cod_tc_venta, 
t2.cantidad as cantidad, 
t2.precio as precio, t2.desc_porc as desc_porc,
t2.precio_total as precio_total,
t2.cod_cxc as cod_cxc, 
t2.cod_moneda_venta as cod_moneda_venta, 
t2.cod_moneda_costo as cod_moneda_costo, 
t2.moneda_costo as moneda_costo, 
t2.cant_costo as cant_costo, t2.costo_u as costo_u,
t2.total_costo as total_costo, t2.tasa as tasa,
t2.precio_fin as precio_fin,
t2.precio_total_fin as precio_total_fin, 
tc2.fldRate as tasa_costo,
t2.costo_u * tc2.fldRate as costo_u_fin,
t2.total_costo * tc2.fldRate as total_costo_fin,
(t2.precio_fin - (t2.costo_u * tc2.fldRate)) as margen_u,
(t2.precio_total_fin - (t2.total_costo * tc2.fldRate)) as margen,
t2.cod_cli as cod_cli,
t2.cod_vendedor as cod_vendedor, 
t2.cod_ciudad as cod_ciudad, t2.ciudad as ciudad, 
t2.cod_familia as cod_familia, t2.familia as familia, 
t2.cod_subfamilia as cod_subfamilia, t2.cod_grupo as cod_grupo, t2.cod_subgrupo as cod_subgrupo
from (
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc, t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
t.cod_moneda_costo as cod_moneda_costo, t.moneda_costo as moneda_costo, t.cant_costo as cant_costo, t.costo_u as costo_u, t.total_costo as total_costo, tc.fldRate as tasa, t.precio * tc.fldRate as precio_fin, t.precio_total * tc.fldRate as precio_total_fin, t.cod_cli as cod_cli,
t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta,  k.codCurrency as cod_moneda_costo, l.fldName as moneda_costo, j.fldQuantity as cant_costo, j.fldCost as costo_u, j.fldTotaCost as total_costo, a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo 
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f,  
slsSalesNoteDetail h, invItem i, invItemMovementDetail j, invItemMovement k, actCurrency l, regCity m, invItemCategory o
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini2' and 
a.fldDate <= '$fechafin2' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
k.codCurrency = l.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
h.codSalesNote = a.pkySalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement = j.codItemMovement and 
h.codItem = j.codItem and 
a.codItemMovement = k.pkyItemMovement and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t, actExchangeRateDetail tc
where
tc.codTargetCurrency = t.cod_moneda_venta and
tc.codBaseCurrency = '$otra_moneda' and
tc.codExchangeRate = t.cod_tc_venta 
union all
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc,
t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
t.cod_moneda_costo as cod_moneda_costo, t.moneda_costo as moneda_costo, t.cant_costo as cant_costo, t.costo_u as costo_u, t.total_costo as total_costo, 1 as tasa, t.precio as precio_fin, t.precio_total as precio_total_fin, t.cod_cli as cod_cli, t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta,  k.codCurrency as cod_moneda_costo, l.fldName as moneda_costo, j.fldQuantity as cant_costo, j.fldCost as costo_u, j.fldTotaCost as total_costo, a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f, 
slsSalesNoteDetail h, invItem i, invItemMovementDetail j, invItemMovement k, actCurrency l, regCity m, invItemCategory o
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini2' and 
a.fldDate <= '$fechafin2' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
k.codCurrency = l.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
h.codSalesNote = a.pkySalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement = j.codItemMovement and 
h.codItem = j.codItem and 
a.codItemMovement = k.pkyItemMovement and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_venta = '$otra_moneda') t2, actExchangeRateDetail tc2 
where
tc2.codTargetCurrency = t2.cod_moneda_costo and
tc2.codBaseCurrency = '$otra_moneda' and
tc2.codExchangeRate = t2.cod_tc_venta 

union all

select t2.cod_item as cod_item, t2.item as item, 
t2.fecha as fecha, 
t2.nro_doc as nro_doc, t2.cliente as cliente, 
t2.vendedor as vendedor, t2.cod_alm as cod_alm,
t2.almacen as almacen, t2.local_venta as local_venta, t2.cod_local as cod_local, 
t2.moneda_venta as moneda_venta, t2.cod_tc_venta as cod_tc_venta, 
t2.cantidad as cantidad, 
t2.precio as precio, t2.desc_porc as desc_porc,
t2.precio_total as precio_total,
t2.cod_cxc as cod_cxc, 
t2.cod_moneda_venta as cod_moneda_venta, 
t2.cod_moneda_costo as cod_moneda_costo, 
t2.moneda_costo as moneda_costo, 
t2.cant_costo as cant_costo, t2.costo_u as costo_u,
t2.total_costo as total_costo, t2.tasa as tasa,
t2.precio_fin as precio_fin,
t2.precio_total_fin as precio_total_fin, 
1 as tasa_costo,
t2.costo_u as costo_u_fin,
t2.total_costo as total_costo_fin,
(t2.precio_fin - t2.costo_u) as margen_u,
(t2.precio_total_fin - t2.total_costo) as margen,
t2.cod_cli as cod_cli,
t2.cod_vendedor as cod_vendedor, 
t2.cod_ciudad as cod_ciudad, t2.ciudad as ciudad, 
t2.cod_familia as cod_familia, t2.familia as familia, 
t2.cod_subfamilia as cod_subfamilia, t2.cod_grupo as cod_grupo, t2.cod_subgrupo as cod_subgrupo
from (
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc,
t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
t.cod_moneda_costo as cod_moneda_costo, t.moneda_costo as moneda_costo, t.cant_costo as cant_costo, t.costo_u as costo_u, t.total_costo as total_costo, tc.fldRate as tasa, t.precio * tc.fldRate as precio_fin, t.precio_total * tc.fldRate as precio_total_fin, t.cod_cli as cod_cli,
t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta, 
k.codCurrency as cod_moneda_costo, l.fldName as moneda_costo, j.fldQuantity as cant_costo, j.fldCost as costo_u, j.fldTotaCost as total_costo, a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f, 
slsSalesNoteDetail h, invItem i, invItemMovementDetail j, invItemMovement k, actCurrency l, regCity m, invItemCategory o
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini2' and 
a.fldDate <= '$fechafin2' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
k.codCurrency = l.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
h.codSalesNote = a.pkySalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement = j.codItemMovement and 
h.codItem = j.codItem and 
a.codItemMovement = k.pkyItemMovement and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t, actExchangeRateDetail tc
where
tc.codTargetCurrency = t.cod_moneda_venta and
tc.codBaseCurrency = '$otra_moneda' and
tc.codExchangeRate = t.cod_tc_venta 
union all
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc, t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
t.cod_moneda_costo as cod_moneda_costo, t.moneda_costo as moneda_costo, t.cant_costo as cant_costo, t.costo_u as costo_u, t.total_costo as total_costo, 1 as tasa, t.precio as precio_fin, t.precio_total as precio_total_fin, t.cod_cli as cod_cli, t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta, 
k.codCurrency as cod_moneda_costo, l.fldName as moneda_costo, j.fldQuantity as cant_costo, j.fldCost as costo_u, j.fldTotaCost as total_costo, a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f, 
slsSalesNoteDetail h, invItem i, invItemMovementDetail j, invItemMovement k, actCurrency l, regCity m, invItemCategory o 
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini2' and 
a.fldDate <= '$fechafin2' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
k.codCurrency = l.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
h.codSalesNote = a.pkySalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement = j.codItemMovement and 
h.codItem = j.codItem and 
a.codItemMovement = k.pkyItemMovement and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_venta = '$otra_moneda') t2 
where
t2.cod_moneda_costo = '$otra_moneda' 



union all



select t3.cod_item as cod_item, t3.item as item, 
t3.fecha as fecha, 
t3.nro_doc as nro_doc, t3.cliente as cliente, 
t3.vendedor as vendedor, t3.cod_alm as cod_alm,
t3.almacen as almacen, t3.local_venta as local_venta, t3.cod_local as cod_local, 
t3.moneda_venta as moneda_venta, t3.cod_tc_venta as cod_tc_venta, 
t3.cantidad as cantidad, 
t3.precio as precio, t3.desc_porc as desc_porc,
t3.precio_total as precio_total,
t3.cod_cxc as cod_cxc, 
t3.cod_moneda_venta as cod_moneda_venta, 
NULL as cod_moneda_costo, 
NULL as moneda_costo, 
0 as cant_costo, 0 as costo_u,
0 as total_costo, t3.tasa as tasa,
t3.precio_fin as precio_fin,
t3.precio_total_fin as precio_total_fin, 
1 as tasa_costo,
0 as costo_u_fin,
0 as total_costo_fin,
(t3.precio_fin - 0) as margen_u,
(t3.precio_total_fin - 0) as margen,
t3.cod_cli as cod_cli,
t3.cod_vendedor as cod_vendedor, 
t3.cod_ciudad as cod_ciudad, t3.ciudad as ciudad, 
t3.cod_familia as cod_familia, t3.familia as familia, 
t3.cod_subfamilia as cod_subfamilia, t3.cod_grupo as cod_grupo, t3.cod_subgrupo as cod_subgrupo
from (
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc, t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
tc.fldRate as tasa, t.precio * tc.fldRate as precio_fin, t.precio_total * tc.fldRate as precio_total_fin, t.cod_cli as cod_cli, t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, 
c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta, 
a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo, a.codItemMovement as cod_mov_merc
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f, 
slsSalesNoteDetail h, invItem i, regCity m, invItemCategory o  
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini2' and 
a.fldDate <= '$fechafin2' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
a.pkySalesNote = h.codSalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement is NULL and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t, actExchangeRateDetail tc
where
tc.codTargetCurrency = t.cod_moneda_venta and
tc.codBaseCurrency = '$otra_moneda' and
tc.codExchangeRate = t.cod_tc_venta 
union all
select t.cod_item as cod_item, t.item as item, t.fecha as fecha, t.nro_doc as nro_doc, t.cliente as cliente, t.vendedor as vendedor, t.cod_alm as cod_alm, t.almacen as almacen, t.local_venta as local_venta, t.cod_local as cod_local, t.moneda_venta as moneda_venta, t.cod_tc_venta as cod_tc_venta, t.cantidad as cantidad, t.precio as precio, t.desc_porc as desc_porc, t.precio_total as precio_total, t.cod_cxc as cod_cxc, t.cod_moneda_venta as cod_moneda_venta, 
1 as tasa, t.precio as precio_fin, t.precio_total as precio_total_fin, t.cod_cli as cod_cli, t.cod_vendedor as cod_vendedor, t.cod_ciudad as cod_ciudad, t.ciudad as ciudad, t.cod_familia as cod_familia, t.familia as familia, t.cod_subfamilia as cod_subfamilia, t.cod_grupo as cod_grupo, t.cod_subgrupo as cod_subgrupo
from (
select h.codItem as cod_item, i.fldName as item, a.fldDate as fecha, a.fldNumber as nro_doc, 
c.fldName as cliente, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as vendedor, a.codWarehouse as cod_alm, d.fldName as almacen, e.fldName as local_venta, a.codSalesPlace as cod_local, f.fldName as moneda_venta, a.codExchangeRate as cod_tc_venta, 
h.fldQuantity as cantidad, h.fldPrice as precio, h.fldDiscount as desc_porc, h.fldTotalNetPrice as precio_total, a.codAccountReceivable as cod_cxc, a.codCurrency as cod_moneda_venta, 
a.codClient as cod_cli, a.codSeller as cod_vendedor, c.codCityLocation as cod_ciudad, m.fldName as ciudad, i.codFamily as cod_familia, o.fldName as familia, i.codSubFamily as cod_subfamilia, i.codGroup as cod_grupo, i.codSubGroup as cod_subgrupo, a.codItemMovement as cod_mov_merc
from slsSalesNote a, agnPerson b, agnCompany c, invWarehouse d, slsSalesPlace e, actCurrency f, 
slsSalesNoteDetail h, invItem i, regCity m, invItemCategory o 
where 
$valor_item 
$valor_vendedor 
$valor_cliente 
$valor_canal_venta 
$valor_almacen 
$valor_local 
$valor_ciudad 
$valor_familia 
$valor_subfamilia 
$valor_grupo 
$valor_subgrupo 
$valor_tipo_nota 
a.fldDate >= '$fechaini2' and 
a.fldDate <= '$fechafin2' and 
a.codSeller = b.pkyPerson and
a.codClient = c.pkyCompany and 
a.codWarehouse = d.pkyWarehouse and 
a.codSalesPlace = e.pkySalesPlace and 
a.codCurrency = f.pkyCurrency and 
c.codCityLocation = m.pkyCity and 
a.pkySalesNote = h.codSalesNote and 
i.pkyItem = h.codItem and 
a.codItemMovement is NULL and 
i.codFamily = o.pkyItemCategory and 
a.sysEnabled = 1 and a.sysAuthorized = 1) t
where
t.cod_moneda_venta = '$otra_moneda') t3 ) fin  
$valor_agrupar 
$valor_ordenar ) fin2
$valor_agrupar2 
$valor_ordenar2;");

	$matriz_it = array ();
	$it = 1;
	//verifica cuantos registros mostrara en el while de abajo si es vendedor, cliente, local,  almacen o ciudad
	if($porfamilia == 0) {//if porfamilia = 0
		while ($row = mysqli_fetch_array($result_it)) {
		$matriz_it[$it][1] = $row['cod_familia'];
		$matriz_it[$it][2] = $row['num_reg'];
		$it++;
		}
	} //fin if porfamilia = 0
	
	if($porfamilia == 1) {//if porfamilia = 1
		while ($row = mysqli_fetch_array($result_it)) {
		$matriz_it[$it][1] = $row['cod_subfamilia'];
		$matriz_it[$it][2] = $row['num_reg'];
		$it++;
		}
	} //fin if porfamilia = 1
	
	if($porfamilia == 2) {//if porfamilia = 2
		while ($row = mysqli_fetch_array($result_it)) {
		$matriz_it[$it][1] = $row['cod_grupo'];
		$matriz_it[$it][2] = $row['num_reg'];
		$it++;
		}
	} //fin if porfamilia = 2
	
	if($porfamilia == 3) {//if porfamilia = 3
		while ($row = mysqli_fetch_array($result_it)) {
		$matriz_it[$it][1] = $row['cod_subgrupo'];
		$matriz_it[$it][2] = $row['num_reg'];
		$it++;
		}
	} //fin if porfamilia = 3
	
	$fin_it = $it - 1;

mysqli_free_result($result_it);

//eliminar los registros con 0 o sin movimiento para que se pueda mostrar por agrupacion y vaciarla a la matriz mat_ventas2
$mat_ventas2 = array();
$i = 1;
$w = 1;
while($i <= $fin) {//while copia
	$suma_cant_h = $mat_ventas[$i][3] + $mat_ventas[$i][6] + $mat_ventas[$i][9] + $mat_ventas[$i][12] + $mat_ventas[$i][15] + $mat_ventas[$i][18] + $mat_ventas[$i][21] + $mat_ventas[$i][24] + $mat_ventas[$i][27] + $mat_ventas[$i][30] + $mat_ventas[$i][33] + $mat_ventas[$i][36];
	
	$suma_monto_h = $mat_ventas[$i][4] + $mat_ventas[$i][7] + $mat_ventas[$i][10] + $mat_ventas[$i][13] + $mat_ventas[$i][16] + $mat_ventas[$i][19] + $mat_ventas[$i][22] + $mat_ventas[$i][25] + $mat_ventas[$i][28] + $mat_ventas[$i][31] + $mat_ventas[$i][34] + $mat_ventas[$i][37];
	
	$suma_margen_h = $mat_ventas[$i][5] + $mat_ventas[$i][8] + $mat_ventas[$i][11] + $mat_ventas[$i][14] + $mat_ventas[$i][17] + $mat_ventas[$i][20] + $mat_ventas[$i][23] + $mat_ventas[$i][26] + $mat_ventas[$i][29] + $mat_ventas[$i][32] + $mat_ventas[$i][35] + $mat_ventas[$i][38];
	//$mat_ventas2[$i][100] = $mat_ventas[$i][100];//traspaso el codigo de fabrica
	
	if($suma_cant_h != 0 || $suma_monto_h != 0 || $suma_margen_h != 0) {//if sin mov
		for($f = 1; $f <= 38; $f++) {
		$mat_ventas2[$w][$f] = $mat_ventas[$i][$f];
		//echo $mat_ventas2[$w][$f].'<br>';
		}
		$w++;
	}//fin if sin mov
	//echo $mat_ventas2[$i][100].'<br>';
	//echo $mat_ventas2[$i][1].'-'.$mat_ventas2[$i][100].'<br>';
	$i++;
}//fin while copia
	
$i = 1;
$j = 1;
	
while($j <= $fin_it){//while $fin_it
	$k = 1;
while($k <= $matriz_it[$j][2]) { //while
  
  if($porfamiliacon	== 0) { //if con0	
	if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $mat_ventas2[$i][1]);//cod
	} else {
	printf("<tr><td align='center'>%s</td>", $mat_ventas2[$i][1]);//cod
	}//cierra if color
  } //fin if con0
	
	if($ventas == 0) {
		$para_cod_fab = $mat_ventas2[$i][1];	
		$result33 = mysqli_query($link, "select pkyItem as codigo, fldName as item, fldFactoryKey as cod_fabrica
		from 
		invItem
		where
		pkyItem = '$para_cod_fab';");
		while ($row = mysqli_fetch_array($result33)) {
			$cod_fab = $row['cod_fabrica'];
		}
		mysqli_free_result($result33);
	  if($porfamiliacon	== 0) { //if con1
		printf("<td align='center'>%s</td>", $cod_fab);//cod fabrica
	  } //fin if con1
	}
  if($porfamiliacon	== 0) { //if con2
	printf("<td>%s</td>", $mat_ventas2[$i][2]);//item o cliente o vendedor, etc
  } //fin if con2
	if($porfamilia == 0) { $familia = $matriz_it[$j][1]; }
	if($porfamilia == 1) { $subfamilia = $matriz_it[$j][1]; }
	if($porfamilia == 2) { $grupo = $matriz_it[$j][1]; }
	if($porfamilia == 3) { $subgrupo = $matriz_it[$j][1]; }
	$cod_varios = $mat_ventas2[$i][1];
	$nom_varios = $mat_ventas2[$i][2];
  if($porfamiliacon	== 0) { //if con3
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][3],$n_cantidad));//cantidad
  } //fin if con3
	$fechaini_mes = $fechames[$mes1[1]][3];
	$fechafin_mes = $fechames[$mes1[1]][4];
	$titulo_mes = $fechames[$mes1[1]][2]. ' ' .$fechames[$mes1[1]][5];
  if($porfamiliacon	== 0) { //if con4
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][4],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][5],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][6],$n_cantidad));//cantidad
  } //fin if con4
	$fechaini_mes = $fechames[$mes1[2]][3];
	$fechafin_mes = $fechames[$mes1[2]][4];
	$titulo_mes = $fechames[$mes1[2]][2]. ' ' .$fechames[$mes1[2]][5];
  if($porfamiliacon	== 0) { //if con5
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][7],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][8],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][9],$n_cantidad));//cantidad
  } //fin if con5
	$fechaini_mes = $fechames[$mes1[3]][3];
	$fechafin_mes = $fechames[$mes1[3]][4];
	$titulo_mes = $fechames[$mes1[3]][2]. ' ' .$fechames[$mes1[3]][5];
  if($porfamiliacon	== 0) { //if con6
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][10],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][11],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][12],$n_cantidad));//cantidad
  } //fin if con6
	$fechaini_mes = $fechames[$mes1[4]][3];
	$fechafin_mes = $fechames[$mes1[4]][4];
	$titulo_mes = $fechames[$mes1[4]][2]. ' ' .$fechames[$mes1[4]][5];
  if($porfamiliacon	== 0) { //if con7
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][13],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][14],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][15],$n_cantidad));//cantidad
  } //fin if con7
	$fechaini_mes = $fechames[$mes1[5]][3];
	$fechafin_mes = $fechames[$mes1[5]][4];
	$titulo_mes = $fechames[$mes1[5]][2]. ' ' .$fechames[$mes1[5]][5];
  if($porfamiliacon	== 0) { //if con8
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][16],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][17],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][18],$n_cantidad));//cantidad
  } //fin if con8
	$fechaini_mes = $fechames[$mes1[6]][3];
	$fechafin_mes = $fechames[$mes1[6]][4];
	$titulo_mes = $fechames[$mes1[6]][2]. ' ' .$fechames[$mes1[6]][5];
  if($porfamiliacon	== 0) { //if con9
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][19],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][20],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][21],$n_cantidad));//cantidad
  } //fin if con9
  	$fechaini_mes = $fechames[$mes1[7]][3];
	$fechafin_mes = $fechames[$mes1[7]][4];
	$titulo_mes = $fechames[$mes1[7]][2]. ' ' .$fechames[$mes1[7]][5];
  if($porfamiliacon	== 0) { //if con10
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][22],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][23],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][24],$n_cantidad));//cantidad
  } //fin if con10
	$fechaini_mes = $fechames[$mes1[8]][3];
	$fechafin_mes = $fechames[$mes1[8]][4];
	$titulo_mes = $fechames[$mes1[8]][2]. ' ' .$fechames[$mes1[8]][5];
  if($porfamiliacon	== 0) { //if con11
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][25],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][26],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][27],$n_cantidad));//cantidad
  } //fin if con11
	$fechaini_mes = $fechames[$mes1[9]][3];
	$fechafin_mes = $fechames[$mes1[9]][4];
	$titulo_mes = $fechames[$mes1[9]][2]. ' ' .$fechames[$mes1[9]][5];
  if($porfamiliacon	== 0) { //if con12
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][28],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][29],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][30],$n_cantidad));//cantidad
  } //fin con12
	$fechaini_mes = $fechames[$mes1[10]][3];
	$fechafin_mes = $fechames[$mes1[10]][4];
	$titulo_mes = $fechames[$mes1[10]][2]. ' ' .$fechames[$mes1[10]][5];
  if($porfamiliacon	== 0) { //if con13
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][31],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][32],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][33],$n_cantidad));//cantidad
  } //fin if con13
	$fechaini_mes = $fechames[$mes1[11]][3];
	$fechafin_mes = $fechames[$mes1[11]][4];
	$titulo_mes = $fechames[$mes1[11]][2]. ' ' .$fechames[$mes1[11]][5];
  if($porfamiliacon	== 0) { //if con14
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][34],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][35],$n_decimales));//margen
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][36],$n_cantidad));//cantidad
  } //fin if con14
	$fechaini_mes = $fechames[$mes1[12]][3];
	$fechafin_mes = $fechames[$mes1[12]][4];
	$titulo_mes = $fechames[$mes1[12]][2]. ' ' .$fechames[$mes1[12]][5];
  if($porfamiliacon	== 0) { //if con15
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini_mes&fechafin_det=$fechafin_mes&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($mat_ventas2[$i][37],$n_decimales));//monto
	
	printf("<td align='right'>%s</td>", number_format($mat_ventas2[$i][38],$n_decimales));//margen
  } //fin if con15
	
	$suma_cant_h = $mat_ventas2[$i][3] + $mat_ventas2[$i][6] + $mat_ventas2[$i][9] + $mat_ventas2[$i][12] + $mat_ventas2[$i][15] + $mat_ventas2[$i][18] + $mat_ventas2[$i][21] + $mat_ventas2[$i][24] + $mat_ventas2[$i][27] + $mat_ventas2[$i][30] + $mat_ventas2[$i][33] + $mat_ventas2[$i][36];
	
	$suma_monto_h = $mat_ventas2[$i][4] + $mat_ventas2[$i][7] + $mat_ventas2[$i][10] + $mat_ventas2[$i][13] + $mat_ventas2[$i][16] + $mat_ventas2[$i][19] + $mat_ventas2[$i][22] + $mat_ventas2[$i][25] + $mat_ventas2[$i][28] + $mat_ventas2[$i][31] + $mat_ventas2[$i][34] + $mat_ventas2[$i][37];
	
	$suma_margen_h = $mat_ventas2[$i][5] + $mat_ventas2[$i][8] + $mat_ventas2[$i][11] + $mat_ventas2[$i][14] + $mat_ventas2[$i][17] + $mat_ventas2[$i][20] + $mat_ventas2[$i][23] + $mat_ventas2[$i][26] + $mat_ventas2[$i][29] + $mat_ventas2[$i][32] + $mat_ventas2[$i][35] + $mat_ventas2[$i][38];
		
	$suma_cant_1 = $suma_cant_1 + $mat_ventas2[$i][3];
	$suma_monto_1 = $suma_monto_1 + $mat_ventas2[$i][4];
	$suma_margen_1 = $suma_margen_1 + $mat_ventas2[$i][5];
	
	$suma_cant_2 = $suma_cant_2 + $mat_ventas2[$i][6];
	$suma_monto_2 = $suma_monto_2 + $mat_ventas2[$i][7];
	$suma_margen_2 = $suma_margen_2 + $mat_ventas2[$i][8];
	
	$suma_cant_3 = $suma_cant_3 + $mat_ventas2[$i][9];
	$suma_monto_3 = $suma_monto_3 + $mat_ventas2[$i][10];
	$suma_margen_3 = $suma_margen_3 + $mat_ventas2[$i][11];
	
	$suma_cant_4 = $suma_cant_4 + $mat_ventas2[$i][12];
	$suma_monto_4 = $suma_monto_4 + $mat_ventas2[$i][13];
	$suma_margen_4 = $suma_margen_4 + $mat_ventas2[$i][14];
	
	$suma_cant_5 = $suma_cant_5 + $mat_ventas2[$i][15];
	$suma_monto_5 = $suma_monto_5 + $mat_ventas2[$i][16];
	$suma_margen_5 = $suma_margen_5 + $mat_ventas2[$i][17];
	
	$suma_cant_6 = $suma_cant_6 + $mat_ventas2[$i][18];
	$suma_monto_6 = $suma_monto_6 + $mat_ventas2[$i][19];
	$suma_margen_6 = $suma_margen_6 + $mat_ventas2[$i][20];
	
	$suma_cant_7 = $suma_cant_7 + $mat_ventas2[$i][21];
	$suma_monto_7 = $suma_monto_7 + $mat_ventas2[$i][22];
	$suma_margen_7 = $suma_margen_7 + $mat_ventas2[$i][23];
	
	$suma_cant_8 = $suma_cant_8 + $mat_ventas2[$i][24];
	$suma_monto_8 = $suma_monto_8 + $mat_ventas2[$i][25];
	$suma_margen_8 = $suma_margen_8 + $mat_ventas2[$i][26];
	
	$suma_cant_9 = $suma_cant_9 + $mat_ventas2[$i][27];
	$suma_monto_9 = $suma_monto_9 + $mat_ventas2[$i][28];
	$suma_margen_9 = $suma_margen_9 + $mat_ventas2[$i][29];
	
	$suma_cant_10 = $suma_cant_10 + $mat_ventas2[$i][30];
	$suma_monto_10 = $suma_monto_10 + $mat_ventas2[$i][31];
	$suma_margen_10 = $suma_margen_10 + $mat_ventas2[$i][32];
	
	$suma_cant_11 = $suma_cant_11 + $mat_ventas2[$i][33];
	$suma_monto_11 = $suma_monto_11 + $mat_ventas2[$i][34];
	$suma_margen_11 = $suma_margen_11 + $mat_ventas2[$i][35];
	
	$suma_cant_12 = $suma_cant_12 + $mat_ventas2[$i][36];
	$suma_monto_12 = $suma_monto_12 + $mat_ventas2[$i][37];
	$suma_margen_12 = $suma_margen_12 + $mat_ventas2[$i][38];
	
	$suma_cant_t = $suma_cant_t + $suma_cant_h;
	$suma_monto_t = $suma_monto_t + $suma_monto_h;
	$suma_margen_t = $suma_margen_t + $suma_margen_h;
	
	$suma_cant_1p = $suma_cant_1p + $mat_ventas2[$i][3];
	$suma_monto_1p = $suma_monto_1p + $mat_ventas2[$i][4];
	$suma_margen_1p = $suma_margen_1p + $mat_ventas2[$i][5];
	
	$suma_cant_2p = $suma_cant_2p + $mat_ventas2[$i][6];
	$suma_monto_2p = $suma_monto_2p + $mat_ventas2[$i][7];
	$suma_margen_2p = $suma_margen_2p + $mat_ventas2[$i][8];
	
	$suma_cant_3p = $suma_cant_3p + $mat_ventas2[$i][9];
	$suma_monto_3p = $suma_monto_3p + $mat_ventas2[$i][10];
	$suma_margen_3p = $suma_margen_3p + $mat_ventas2[$i][11];
	
	$suma_cant_4p = $suma_cant_4p + $mat_ventas2[$i][12];
	$suma_monto_4p = $suma_monto_4p + $mat_ventas2[$i][13];
	$suma_margen_4p = $suma_margen_4p + $mat_ventas2[$i][14];
	
	$suma_cant_5p = $suma_cant_5p + $mat_ventas2[$i][15];
	$suma_monto_5p = $suma_monto_5p + $mat_ventas2[$i][16];
	$suma_margen_5p = $suma_margen_5p + $mat_ventas2[$i][17];
	
	$suma_cant_6p = $suma_cant_6p + $mat_ventas2[$i][18];
	$suma_monto_6p = $suma_monto_6p + $mat_ventas2[$i][19];
	$suma_margen_6p = $suma_margen_6p + $mat_ventas2[$i][20];
	
	$suma_cant_7p = $suma_cant_7p + $mat_ventas2[$i][21];
	$suma_monto_7p = $suma_monto_7p + $mat_ventas2[$i][22];
	$suma_margen_7p = $suma_margen_7p + $mat_ventas2[$i][23];
	
	$suma_cant_8p = $suma_cant_8p + $mat_ventas2[$i][24];
	$suma_monto_8p = $suma_monto_8p + $mat_ventas2[$i][25];
	$suma_margen_8p = $suma_margen_8p + $mat_ventas2[$i][26];
	
	$suma_cant_9p = $suma_cant_9p + $mat_ventas2[$i][27];
	$suma_monto_9p = $suma_monto_9p + $mat_ventas2[$i][28];
	$suma_margen_9p = $suma_margen_9p + $mat_ventas2[$i][29];
	
	$suma_cant_10p = $suma_cant_10p + $mat_ventas2[$i][30];
	$suma_monto_10p = $suma_monto_10p + $mat_ventas2[$i][31];
	$suma_margen_10p = $suma_margen_10p + $mat_ventas2[$i][32];
	
	$suma_cant_11p = $suma_cant_11p + $mat_ventas2[$i][33];
	$suma_monto_11p = $suma_monto_11p + $mat_ventas2[$i][34];
	$suma_margen_11p = $suma_margen_11p + $mat_ventas2[$i][35];
	
	$suma_cant_12p = $suma_cant_12p + $mat_ventas2[$i][36];
	$suma_monto_12p = $suma_monto_12p + $mat_ventas2[$i][37];
	$suma_margen_12p = $suma_margen_12p + $mat_ventas2[$i][38];
	
	$suma_cant_tp = $suma_cant_tp + $suma_cant_h;
	$suma_monto_tp = $suma_monto_tp + $suma_monto_h;
	$suma_margen_tp = $suma_margen_tp + $suma_margen_h;
	
	$titulo_mes = $fechames[$mes1[1]][2]. ' ' .$fechames[$mes1[1]][5]. ' a ' .$fechames[$mes1[12]][2]. ' ' .$fechames[$mes1[12]][5];
  if($porfamiliacon	== 0) { //if con16
	printf("<td align='right'>%s</td>", number_format($suma_cant_h,$n_cantidad));//total cantidad
	printf("<td align='right'><a href='report.php?rep=14&tipo=2&ventas_det=$ventas&otra_moneda_det=$otra_moneda&agrupado_det=1&cod_varios_det=$cod_varios&nom_varios_det=$nom_varios&titulo_mes_det=$titulo_mes&fechaini_det=$fechaini2&fechafin_det=$fechafin2&tipo_nota_det=$tipo_nota&familia_det=$familia&subfamilia_det=$subfamilia&grupo_det=$grupo&subgrupo_det=$subgrupo&porfamilia_det=$porfamilia&usuario_det=$usuario&asientocc_det=$asientocc' title='Kardex de Ventas del Periodo'>%s</a></td>", number_format($suma_monto_h,$n_decimales));//total monto
	printf("<td align='right'>%s</td></tr>", number_format($suma_margen_h,$n_decimales));//total margen
  } //fin if con16
	
	$color++;
	
	$i++;
	$k++;

}//fin while

?>
<?php if($porfamiliacon == 0) { //if con00 ?>
<tr BGCOLOR='#ffcccc'>
<?php } else { //else if con00 
$codnombrefam = $matriz_it[$j][1];
if($codnombrefam != NULL) { //if null 0
if($color2 % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $codnombrefam);//cod
	} else {
	printf("<tr><td align='center'>%s</td>", $codnombrefam);//cod
	}//cierra if color
$nro_col = 2;
} //fin if null 0 
} //fin if con00
?>
<?php if($porfamilia == 0) { //if porfamilia = 0 
$codnombrefam = $matriz_it[$j][1];

//nombre de familia
$result_nom = mysqli_query($link, "select fldName as nombre, fldLevel as nivel
from invItemCategory
where 
pkyItemCategory = '$codnombrefam' and 
sysEnabled = 1 and sysAuthorized = 1;");
while ($row = mysqli_fetch_array($result_nom)) {
	$nombre_mos = $row['nombre'];
}
mysqli_free_result($result_nom);
?>
<?php if($porfamiliacon == 0) { //if con01 ?>
<td colspan="<?php echo $nro_col; ?>"><b>Total Familia <?php echo $nombre_mos; ?></b></td>
<?php } else { //else if con01 ?>
<td><?php echo $nombre_mos; ?></td>
<?php } //fin if con01 ?> 
<?php } //fin if porfamilia = 0 ?>

<?php if($porfamilia == 1) { //if porfamilia = 1 
$codnombrefam = $matriz_it[$j][1];
if($codnombrefam != NULL) { //if null 1
//nombre de subfamilia
$result_nom = mysqli_query($link, "select fldName as nombre, fldLevel as nivel
from invItemCategory
where 
pkyItemCategory = '$codnombrefam' and 
sysEnabled = 1 and sysAuthorized = 1;");
while ($row = mysqli_fetch_array($result_nom)) {
	$nombre_mos = $row['nombre'];
}
mysqli_free_result($result_nom);
?>
<?php if($porfamiliacon == 0) { //if con02 ?>
<td colspan="<?php echo $nro_col; ?>"><b>Total Sub Familia <?php echo $nombre_mos; ?></b></td>
<?php } else { //else if con02 ?>
<td><?php echo $nombre_mos; ?></td>
<?php } //fin if con02 ?> 
<?php } //fin if null 1 ?>
<?php } //fin if porfamilia = 1 ?>

<?php if($porfamilia == 2) { //if porfamilia = 2 
$codnombrefam = $matriz_it[$j][1];
if($codnombrefam != NULL) { //if null 2
//nombre de grupo
$result_nom = mysqli_query($link, "select fldName as nombre, fldLevel as nivel
from invItemCategory
where 
pkyItemCategory = '$codnombrefam' and 
sysEnabled = 1 and sysAuthorized = 1;");
while ($row = mysqli_fetch_array($result_nom)) {
	$nombre_mos = $row['nombre'];
}
mysqli_free_result($result_nom);
?>
<?php if($porfamiliacon == 0) { //if con03 ?>
<td colspan="<?php echo $nro_col; ?>"><b>Total Grupo <?php echo $nombre_mos; ?></b></td>
<?php } else { //else if con03 ?>
<td><?php echo $nombre_mos; ?></td>
<?php } //fin if con03 ?>
<?php } //fin if null 2 ?>
<?php } //fin if porfamilia = 2 ?>

<?php if($porfamilia == 3) { //if porfamilia = 3 
$codnombrefam = $matriz_it[$j][1];
if($codnombrefam != NULL) { //if null 3
//nombre de subgrupo
$result_nom = mysqli_query($link, "select fldName as nombre, fldLevel as nivel
from invItemCategory
where 
pkyItemCategory = '$codnombrefam' and 
sysEnabled = 1 and sysAuthorized = 1;");
while ($row = mysqli_fetch_array($result_nom)) {
	$nombre_mos = $row['nombre'];
}
mysqli_free_result($result_nom);
?>
<?php if($porfamiliacon == 0) { //if con04 ?>
<td colspan="<?php echo $nro_col; ?>"><b>Total Sub Grupo <?php echo $nombre_mos; ?></b></td>
<?php } else { //else if con04 ?>
<td><?php echo $nombre_mos; ?></td>
<?php } //fin if con04 ?>
<?php } //fin if null 3 ?>
<?php } //fin if porfamilia = 3 ?>

<?php if($codnombrefam != NULL) { //if null parcial (si no existe nombre no muestra)?>
<!--muestra totales parciales -->
<?php if($porfamiliacon == 0) { //if con05 ?>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_1p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_1p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_1p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_2p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_2p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_2p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_3p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_3p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_3p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_4p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_4p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_4p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_5p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_5p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_5p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_6p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_6p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_6p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_7p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_7p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_7p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_8p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_8p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_8p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_9p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_9p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_9p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_10p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_10p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_10p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_11p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_11p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_11p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_12p,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_12p,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_12p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_tp,$n_cantidad)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_tp,$n_decimales)); ?></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_tp,$n_decimales)); ?></td>

<?php } else { //else if con05 ?>

<td align="right"><?php printf("%s", number_format($suma_cant_1p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_1p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_1p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_2p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_2p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_2p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_3p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_3p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_3p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_4p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_4p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_4p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_5p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_5p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_5p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_6p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_6p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_6p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_7p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_7p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_7p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_8p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_8p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_8p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_9p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_9p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_9p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_10p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_10p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_10p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_11p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_11p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_11p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_12p,$n_cantidad)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_monto_12p,$n_decimales)); ?></td>
<td align="right"><?php printf("%s", number_format($suma_margen_12p,$n_decimales)); ?></td>
 
<td align="right"><?php printf("%s", number_format($suma_cant_tp,$n_cantidad)); ?></td>
<?php if($porfamilia == 0) { //if fam1 ?>
<td align="right"><?php printf("<a href='report.php?rep=14&tipo=1&familia_rev=$codnombrefam&asientocc_rev=$asientocc&agrupado_rev=1&porfamilia_rev=0&porfamiliacon_rev=0&tipo_nota_rev=$tipo_nota&item_rev=$item&vendedor_rev=$vendedor&almacen_rev=$almacen&cliente_rev=$cliente&canal_venta_rev=$canal_venta&local_rev=$local&ciudad_rev=$ciudad&anoini_rev=$anoini&otra_moneda_rev=$otra_moneda&ventas_rev=$ventas&usuario_rev=$usuario' title='Reporte Ventas Anuales'>%s</a>", number_format($suma_monto_tp,$n_decimales)); ?></td>
<?php } else { //else if fam1 ?>
<td align="right"><?php printf("%s", number_format($suma_monto_tp,$n_decimales)); ?></td>
<?php } //fin if fam1 ?>
<td align="right"><?php printf("%s", number_format($suma_margen_tp,$n_decimales)); ?></td>

<?php } //fin if con05 ?>
<?php } //fin if null parcial ?>
</tr>
 <?php
 $j++;
 $color2++;
$suma_cant_1p = 0;
$suma_monto_1p = 0;
$suma_margen_1p = 0;

$suma_cant_2p = 0;
$suma_monto_2p = 0;
$suma_margen_2p = 0;

$suma_cant_3p = 0;
$suma_monto_3p = 0;
$suma_margen_3p = 0;

$suma_cant_4p = 0;
$suma_monto_4p = 0;
$suma_margen_4p = 0;

$suma_cant_5p = 0;
$suma_monto_5p = 0;
$suma_margen_5p = 0;

$suma_cant_6p = 0;
$suma_monto_6p = 0;
$suma_margen_6p = 0;

$suma_cant_7p = 0;
$suma_monto_7p = 0;
$suma_margen_7p = 0;

$suma_cant_8p = 0;
$suma_monto_8p = 0;
$suma_margen_8p = 0;

$suma_cant_9p = 0;
$suma_monto_9p = 0;
$suma_margen_9p = 0;

$suma_cant_10p = 0;
$suma_monto_10p = 0;
$suma_margen_10p = 0;

$suma_cant_11p = 0;
$suma_monto_11p = 0;
$suma_margen_11p = 0;

$suma_cant_12p = 0;
$suma_monto_12p = 0;
$suma_margen_12p = 0;

$suma_cant_tp = 0;
$suma_monto_tp = 0;
$suma_margen_tp = 0;
}//fin while $fin_it
	
}//fin else if agrupado = 0

?>
 
 <tr BGCOLOR='#ffcccc'><td colspan="<?php echo $nro_col; ?>"><b>Totales</b></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_1,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_1,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_1,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_2,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_2,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_2,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_3,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_3,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_3,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_4,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_4,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_4,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_5,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_5,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_5,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_6,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_6,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_6,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_7,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_7,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_7,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_8,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_8,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_8,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_9,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_9,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_9,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_10,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_10,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_10,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_11,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_11,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_11,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_12,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_12,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_12,$n_decimales)); ?></td>
 
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_cant_t,$n_cantidad)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_monto_t,$n_decimales)); ?></td>
 <td align="right"><?php printf("<b>%s</b>", number_format($suma_margen_t,$n_decimales)); ?></td>
 </tr>
 </tbody>
 </table>

<?php
	
} //fin if 2



// inicio muestra detalle

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 = $row['otra_moneda'];
	}
	mysqli_free_result($resultotra);

if($ventas_det == 0) { //if combinar_det = 0
$valor_item_det = ' a.codItem = ' .$cod_varios_det. ' and '; 
$valor_cli_det = ' ';
$valor_ven_det = ' ';
$valor_alm_det = ' ';
$valor_loc_det = ' ';
$valor_ciu_det = ' ';
$nom_det = 'Item';
	$result33 = mysqli_query($link, "select pkyItem as codigo, fldName as item, fldFactoryKey as cod_fabrica
	from 
	invItem
	where
	pkyItem = '$cod_varios_det';");
	while ($row = mysqli_fetch_array($result33)) {
		$nom_varios_det = $cod_varios_det.' - '.$row['cod_fabrica'].' - '.$nom_varios_det;
	}
	mysqli_free_result($result33);
}//fin if combinar_det = 0
if($ventas_det == 1) { //if combinar_det = 1
$valor_item_det = ' ';
$valor_cli_det = ' ';
$valor_ven_det = ' c.codSeller = ' .$cod_varios_det. ' and ';
$valor_alm_det = ' ';
$valor_loc_det = ' ';
$valor_ciu_det = ' ';
$nom_det = 'Vendedor';
}//fin if combinar_det = 1
if($ventas_det == 2) { //if combinar_det = 2
$valor_item_det = ' ';
$valor_cli_det = ' c.codClient = ' .$cod_varios_det. ' and ';
$valor_ven_det = ' ';
$valor_alm_det = ' ';
$valor_loc_det = ' ';
$valor_ciu_det = ' ';
$nom_det = 'Cliente';
}//fin if combinar_det = 2
if($ventas_det == 3) { //if combinar_det = 3
$valor_item_det = ' ';
$valor_cli_det = ' ';
$valor_ven_det = ' ';
$valor_alm_det = ' c.codWarehouse = ' .$cod_varios_det. ' and ';
$valor_loc_det = ' ';
$valor_ciu_det = ' ';
$nom_det = 'Almacen';
}//fin if combinar_det = 3
if($ventas_det == 4) { //if combinar_det = 4
$valor_item_det = ' ';
$valor_cli_det = ' ';
$valor_ven_det = ' ';
$valor_alm_det = ' ';
$valor_loc_det = ' c.codSalesPlace = ' .$cod_varios_det. ' and ';
$valor_ciu_det = ' ';
$nom_det = 'Punto o Local de venta';
}//fin if combinar_det = 4
if($ventas_det == 5) { //if combinar_det = 5
$valor_item_det = ' ';
$valor_cli_det = ' ';
$valor_ven_det = ' ';
$valor_alm_det = ' ';
$valor_loc_det = ' ';
$valor_ciu_det = ' d.codCityLocation = ' .$cod_varios_det. ' and ';
$nom_det = 'Ciudad';
}//fin if combinar_det = 5


if ($tipo_nota_det == 0) { //if tipo nota
	$nom_tipo_nota_det = 'Todos';
	$valor_tipo_nota_det = ' ';
  } else { 
  	$valor_tipo_nota_det = ' c.codType = '.$tipo_nota_det.' and ';
	//nombre de tipo de nota
$resulttipo = mysqli_query($link, "select pkyOrderNoteType as codigo, fldName as tipo
from 
slsOrderNoteType
where
pkyOrderNoteType = '$tipo_nota_det' and 
sysEnabled = 1 and sysAuthorized = 1
order by tipo;");
	while ($row = mysqli_fetch_array($resulttipo)) {
	$nom_tipo_nota_det = $row['tipo'];
	}
	mysqli_free_result($resulttipo);
  } //fin if tipo nota

if ($familia_det == 0) { // if familia
		$nom_familia_det = 'Todos';
		$valor_familia_det = ' ';
	} else {
		//nombre familia
		$resultfam = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$familia_det' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($resultfam)) {
			$nom_familia_det = $row['nombre'];
		}
		mysqli_free_result($resultfam);
		$valor_familia_det = ' b.codFamily = ' .$familia_det. ' and ';
	} //fin if familia

if ($subfamilia_det == 0) { // if subfamilia
		$nom_subfamilia_det = 'Todos';
		$valor_subfamilia_det = ' ';
	} else {
		//nombre sub familia
		$resultsubf = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$subfamilia_det' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($resultsubf)) {
			$nom_subfamilia_det = $row['nombre'];
		}
		mysqli_free_result($resultsubf);
		$valor_subfamilia_det = ' b.codSubFamily = ' .$subfamilia_det. ' and ';
	} //fin if subfamilia
	
if ($grupo_det == 0) { // if grupo
		$nom_grupo_det = 'Todos';
		$valor_grupo_det = ' ';
	} else {
		//nombre grupo
		$resultgru = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$grupo_det' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($resultgru)) {
			$nom_grupo_det = $row['nombre'];
		}
		mysqli_free_result($resultgru);
		$valor_grupo_det = ' b.codGroup = ' .$grupo_det. ' and ';
	} //fin if grupo
	
if ($subgrupo_det == 0) { // if subgrupo
		$nom_subgrupo_det = 'Todos';
		$valor_subgrupo_det = ' ';
	} else {
		//nombre sub grupo
		$resultsubg = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$subgrupo_det' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($resultsubg)) {
			$nom_subgrupo_det = $row['nombre'];
		}
		mysqli_free_result($resultsubg);
		$valor_subgrupo_det = ' b.codSubGroup = ' .$subgrupo_det. ' and ';
	} //fin if subgrupo

if($agrupado_det == 0) {
	$nom_agrupado_det = 'No';
} else {
	$nom_agrupado_det = 'Si';
}

if($porfamilia_det == 0) { $nom_porfamilia_det = '(Familia)'; }
if($porfamilia_det == 1) { $nom_porfamilia_det = '(Sub Familia)'; }
if($porfamilia_det == 2) { $nom_porfamilia_det = '(Grupo)'; }
if($porfamilia_det == 3) { $nom_porfamilia_det = '(Sub Grupo)'; }

$resultdet = mysqli_query($link, "select c.pkySalesNote as codigo, a.fldQuantity as cantidad,
a.fldPrice as precio, a.fldDiscount as descuento, a.fldTotalNetPrice as total, b.fldName as item, c.fldDate as fecha, c.fldNumber as nro_doc, c.codClient as cod_cliente, d.fldName as cliente, c.codAccountReceivable as cod_cxc, c.codAccountEntry as cod_asiento, c.codItemMovement as cod_mov_mercaderia, e.fldName as moneda, c.codCurrency as cod_moneda, c.codExchangeRate as cod_tc, c.codWarehouse as cod_alm, f.fldName as almacen, c.codSeller as cod_vendedor, 
case when fldMiddleName is NULL then concat(g.fldLastName,', ',g.fldFirstName) else concat(g.fldLastName,' ',g.fldMiddleName,', ',g.fldFirstName) end as vendedor, c.codSalesPlace as cod_local, h.fldName as local, d.codCityLocation as cod_ciudad, i.fldName as ciudad, b.codFamily as cod_familia, j.fldName as familia, c.codInvoice as cod_fac, c.codPettyCashMovement as cod_mov_caja, c.codCheck as cod_cheque, c.codBankMovement as cod_mov_banco 
from
slsSalesNoteDetail a, invItem b, slsSalesNote c, agnCompany d, actCurrency e, invWarehouse f, 
agnPerson g, slsSalesPlace h, regCity i, invItemCategory j
where 
$valor_item_det 
$valor_cli_det 
$valor_ven_det 
$valor_alm_det 
$valor_loc_det 
$valor_ciu_det 
$valor_tipo_nota_det 
$valor_familia_det 
$valor_subfamilia_det 
$valor_grupo_det 
$valor_subgrupo_det 
c.fldDate >= '$fechaini_det' and 
c.fldDate <= '$fechafin_det' and 
a.codItem = b.pkyItem and 
a.codSalesNote = c.pkySalesNote and 
c.codClient = d.pkyCompany and 
c.codCurrency = e.pkyCurrency and 
c.codWarehouse = f.pkyWarehouse and 
c.codSeller = g.pkyPerson and 
c.codSalesPlace = h.pkySalesPlace and 
d.codCityLocation = i.pkyCity and 
b.codFamily = j.pkyItemCategory and
c.sysEnabled = 1 and c.sysAuthorized = 1
order by cast(nro_doc as unsigned), fecha, codigo;");
$matriz_det = array();
$i = 1;

		while ($row = mysqli_fetch_array($resultdet)) {
		$matriz_det[$i][1] = $row['codigo'];
		$matriz_det[$i][2] = $row['nro_doc'];
		$matriz_det[$i][3] = $row['item'];
		$matriz_det[$i][4] = $row['fecha'];
		$matriz_det[$i][5] = $row['cliente'];
		$matriz_det[$i][6] = $row['cod_cxc'];
		$matriz_det[$i][7] = $row['cantidad'];
		$matriz_det[$i][8] = $row['precio'];
		$matriz_det[$i][9] = $row['descuento'];
		$matriz_det[$i][10] = $row['total'];
		$matriz_det[$i][11] = $row['moneda'];
		$matriz_det[$i][12] = $row['cod_moneda'];
		$matriz_det[$i][13] = $row['cod_tc'];
		$cod_tc = $matriz_det[$i][13];
		$cod_moneda_base = $matriz_det[$i][12];
		//por alguna razon estos valores deben ir antes de la comprobacion de la moneda sino se pierden
		$matriz_det[$i][15] = $row['almacen'];
		$matriz_det[$i][16] = $row['vendedor'];
		$matriz_det[$i][17] = $row['local'];
		$matriz_det[$i][18] = $row['ciudad'];
		$matriz_det[$i][19] = $row['cod_asiento'];
		$matriz_det[$i][20] = $row['cod_mov_mercaderia'];
		$matriz_det[$i][21] = $row['cod_fac'];//cod factura
		$matriz_det[$i][22] = $row['cod_mov_caja'];//cod movimiento de caja
		$matriz_det[$i][23] = $row['cod_cheque'];//cod cheque
		$matriz_det[$i][24] = $row['cod_mov_banco'];//cod movimiento de banco
		$matriz_det[$i][25] = $row['familia'];//familia
		
		if($matriz_det[$i][12] != $otra_moneda_det) {
			//valores de conversion tipo de cambio a cada fecha de venta o cotizacion
			$resulttc = mysqli_query($link, "SELECT pkyExchangeRateDetail as codigotc, codBaseCurrency as monedabase, codTargetCurrency as monedacambio, fldRate as tc
			from
			actExchangeRateDetail
			where
			codExchangeRate = '$cod_tc' and
			codBaseCurrency = '$otra_moneda_det' and
			codTargetCurrency = '$cod_moneda_base' and
			sysEnabled = 1 AND sysAuthorized = 1;");
			while ($row = mysqli_fetch_array($resulttc)) {
				$matriz_det [$i][14] = $row['tc'];//valor a multiplicar del tc 
				}
				mysqli_free_result($resulttc);
		} else {
			$matriz_det[$i][14] = 1;
		}
		$i++;
		}
		mysqli_free_result($resultdet);
		$fin_det = $i - 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_det; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Detalle Ventas <?php echo $titulo_mes_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="25%"><b>Ventas mensuales por: </b><?php echo $nom_det; ?></TD>
        <?php if($ventas_det == 0){ ?>
		<TD width="25%"><b>Item: </b><?php echo $nom_varios_det; ?></TD>
		<?php } ?>
        <?php if($ventas_det == 1){ ?>
		<TD width="25%"><b>Vendedor: </b><?php echo $nom_varios_det; ?></TD>
		<?php } ?>
        <?php if($ventas_det == 2){ ?>
		<TD width="25%"><b>Cliente: </b><?php echo $nom_varios_det; ?></TD>
		<?php } ?>
        <?php if($ventas_det == 3){ ?>
		<TD width="25%"><b>Almacen: </b><?php echo $nom_varios_det; ?></TD>
		<?php } ?>
        <?php if($ventas_det == 4){ ?>
		<TD width="25%"><b>Punto o local de venta: </b><?php echo $nom_varios_det; ?></TD>
		<?php } ?>
        <?php if($ventas_det == 5){ ?>
		<TD width="25%"><b>Ciudad (en base al cliente): </b><?php echo $nom_varios_det; ?></TD>
		<?php } ?>
        <?php if($agrupado_det == 0){ ?>
        <TD width="25%"><b>Agrupado: </b><?php echo $nom_agrupado_det; ?></TD>
        <?php } else { ?>
        <TD width="25%"><b>Agrupado: </b><?php echo $nom_agrupado_det; ?> <?php echo $nom_porfamilia_det; ?></TD>
        <?php } ?>
        <TD width="25%"><b>Tipo: </b><?php echo $nom_tipo_nota_det; ?></TD>
        </TR>
        <TR>
        <TD><b>Familia: </b><?php echo $nom_familia_det; ?></TD>
        <TD><b>Sub Familia: </b><?php echo $nom_subfamilia_det; ?></TD>
        <TD><b>Grupo: </b><?php echo $nom_grupo_det; ?></TD>
        <TD><b>Sub Grupo: </b><?php echo $nom_subgrupo_det; ?></TD>
        </TR>
        <TR>
        <TD><b>Ordenado por: </b>Nro. de Doc., Fecha y c&oacute;digo</TD>
        <TD colspan="2"><b>De Fecha: </b><?php echo $fechaini_det; ?> <b>a Fecha: </b><?php echo $fechafin_det; ?></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" width="65"><b>Fecha</b></TD>
    <TD align="center"><b>Item</b></TD>
    <TD align="center"><b>Familia</b></TD>
    <TD align="center"><b>Cliente</b></TD>
    <TD align="center"><b>Vendedor</b></TD>
    <TD align="center"><b>Almac&eacute;n</b></TD>
    <TD align="center"><b>Punto o Local de venta</b></TD>
    <TD align="center"><b>Ciudad</b></TD>
    <TD align="center"><b>CxC</b></TD>
    <TD align="center"><b>Asiento</b></TD>
    <TD align="center"><b>Mov. Merc.</b></TD>
    <TD align="center"><b>Fac.</b></TD>
    <TD align="center"><b>Mov. Caja</b></TD>
    <TD align="center"><b>Cheq.</b></TD>
    <TD align="center"><b>Mov. Banco</b></TD>
    <TD align="center"><b>Cant.</b></TD>
    <TD align="center"><b>Mon.</b></TD>
    <TD align="center"><b>Precio</b></TD>
    <TD align="center"><b>Desc. %</b></TD>
    <TD align="center"><b>Total</b></TD>
    <TD align="center"><b>Tasa a <?php echo $nom_otra_moneda; ?></b></TD>
    <TD align="center"><b>Total en <?php echo $nom_otra_moneda; ?></b></TD>
    </TR>
    </thead>
    <tbody class="marcafila">

    <?php
	$suma_total = 0;
	$suma_total_om = 0;
	$suma_cant = 0;
	$color = 2;
	for ($i = 1; $i <= $fin_det; $i++) { //for
		$codigo_nota_venta = $matriz_det[$i][1];
		$codigo_cxc = $matriz_det[$i][6];
		$codigo_asiento = $matriz_det[$i][19];
		$codigo_mov_mercaderia = $matriz_det[$i][20];
		$codigo_mov_caja = $matriz_det[$i][22];
		$codigo_mov_banco = $matriz_det[$i][24];
		$codigo_factura = $matriz_det[$i][21];
		$cod_cheque = $matriz_det[$i][23];
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'><a href='../../ModuleSales/VoucherSalesNote/index.php?pkySalesNote=$codigo_nota_venta&user=$usuario_det' title='Nota de Venta'>%s</a></td>", $matriz_det[$i][1]);//cod
		} else {
		printf("<tr><td align='center'><a href='../../ModuleSales/VoucherSalesNote/index.php?pkySalesNote=$codigo_nota_venta&user=$usuario_det' title='Nota de Venta'>%s</a></td>", $matriz_det[$i][1]);//cod
		}//fin if color
		printf("<td align='center'>%s</td>", $matriz_det[$i][2]);//numero documento
		printf("<td align='right'>%s</td>", $matriz_det[$i][4]);//fecha
		printf("<td>%s</td>", $matriz_det[$i][3]);//item
		printf("<td>%s</td>", $matriz_det[$i][25]);//familia
		printf("<td>%s</td>", $matriz_det[$i][5]);//cliente
		printf("<td>%s</td>", $matriz_det[$i][16]);//vendedor
		printf("<td>%s</td>", $matriz_det[$i][15]);//almacen
		printf("<td>%s</td>", $matriz_det[$i][17]);//punto o local de venta
		printf("<td>%s</td>", $matriz_det[$i][18]);//ciudad
		printf("<td align='right'><a href='../../ModuleCollections/VoucherAccountsReceivable/index.php?pkyAccountsReceivable=$codigo_cxc&user=$usuario_det' title='Nota de Cuenta por Cobrar'>%s</a></td>", $matriz_det[$i][6]);//cxc
		printf("<td align='right'><a href='../../ModuleAccounting/VoucherAccountEntry/index.php?pkyAccountEntry=$codigo_asiento&showCC=$asientocc_det&user=$usuario_det' title='Asiento Contable'>%s</a></td>", $matriz_det[$i][19]);//asiento
		printf("<td align='right'><a href='../../ModuleInventory/VoucherItemMovement/index.php?pkyItemMovement=$codigo_mov_mercaderia&showBatch=1&printAccountEntry=0' title='Movimiento de Mercadería con Lotes'>%s</a></td>", $matriz_det[$i][20]);//movimiento de mercaderia (printAccountEntry=0 porque no genera asiento, lo genera la nota de pedido) 
		
		printf("<td align='right'><a href='../../ModuleTaxes/VoucherSalesLedger/index.php?pkySalesLedger=$codigo_factura&sheetSize=$formato_factura&head=$muestra_cabecera' title='Factura'>%s</a></td>", $matriz_det[$i][21]);//factura
		printf("<td align='right'><a href='../../ModulePettyCash/VoucherPettyCashMovement/index.php?pkyPettyCashMovement=$codigo_mov_caja&user=$usuario_det' title='Movimiento de Caja'>%s</a></td>", $matriz_det[$i][22]);//mov de caja
		printf("<td align='right'><a href='../../ModuleTreasury/VoucherCheck/index.php?pkyCheck=$cod_cheque&user=$usuario_det' title='Cheque'>%s</a></td>", $matriz_det[$i][23]);//cheque
		printf("<td align='right'><a href='../../ModuleTreasury/VoucherBankMovement/index.php?pkyBankMovement=$codigo_mov_banco&user=$usuario' title='Nota de Movimiento Bancario'>%s</a></td>", $matriz_det[$i][24]);//mov de banco
		printf("<td align='right'>%s</td>", number_format($matriz_det[$i][7],$n_cantidad));//cantidad
		printf("<td>%s</td>", $matriz_det[$i][11]);//moneda
		printf("<td align='right'>%s</td>", number_format($matriz_det[$i][8],$n_decimales));//precio
		printf("<td align='right'>%s</td>", number_format($matriz_det[$i][9], $n_descuento));//descuento
		
		printf("<td align='right'>%s</td>", number_format($matriz_det[$i][10], $n_decimales));//total
		printf("<td align='right'>%s</td>", number_format($matriz_det[$i][14], $n_tipocambio));//tasa
		printf("<td align='right'>%s</td></tr>", number_format($matriz_det[$i][10] * $matriz_det[$i][14], $n_decimales));//total otra moneda
		//aqui toy
		 $suma_total = $suma_total + $matriz_det[$i][10];
		 $suma_total_om = $suma_total_om + $matriz_det[$i][10] * $matriz_det[$i][14];
		 $suma_cant = $suma_cant + $matriz_det[$i][7];
		 $color++;
		}//fin for
?>
<tr BGCOLOR='#ffcccc'><td colspan="17"><b>Total</b></td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_cant,$n_cantidad)); ?></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_total,$n_decimales)); ?></td>
<td>&nbsp;</td>
<td align="right"><?php printf("<b>%s</b>", number_format($suma_total_om,$n_decimales)); ?></td></tr>

<?php  
	
} //fin if 3
?>
</tbody>
</table>

</body>
</html>
