sesion_admon();
if(!$_SESSION["login"] || !$_SESSION["name_session"] || !$_SESSION["id_session"] || !$_SESSION["usuario"]){
print("");
}else{
$order=($campo && $orden ? "order by $campo $orden" : "");
$plantilla="informe_nscs.html";
$title = "NEW SALES CATEGORY SERVICES";
switch ($accion){
case "":{
$Qryperiodo=$ln->query("select * from dbo_tiposperiodos");
$periodo[""]="<<-- SELECCIONE -->>";
if($ln->num_rows($Qryperiodo)){
while ($rowperiodo =$ln->fetch($Qryperiodo)) {
$periodo[$rowperiodo['id']]=$rowperiodo['descripcion'];
}
}
$smarty->assign('periodos',$periodo);
$ln->free_result($Qryperiodo);
$clientes[""]="<<-- TODOS -->>";
$Qryclientes=$ln->query("select * from dbo_clientes order by descripcion");
if($ln->num_rows($Qryclientes)){
while ($rowclientes =$ln->fetch($Qryclientes)) {
$clientes[$rowclientes['id']]=$rowclientes['descripcion'];
}
}
$smarty->assign('clientes',$clientes);
$ln->free_result($Qryclientes);
//Combo de Paises
$limites=limit_pais($idusuario);
unset($str_where);
if($limites["tipo"]=="SI")$str_where=" and id='".$limites["pais"]."'";
if($limites["tipo"]=="GRUPO")$str_where=" and grupo='".$limites["grupo"]."'";
if($limites["tipo"]!="SI")$paises[""]="<<-- SELECCIONE -->>";
if($limites["tipo"]=="NO"){
$Qrypaises=$ln->query("select distinct region from dbo_paises WHERE pais_reconocimiento='SI'");
if($ln->num_rows($Qrypaises)){
while ($rowpaises =$ln->fetch($Qrypaises)) {
$paises[$rowpaises['region']]=$rowpaises['region'];
}
}
$ln->free_result($Qrypaises);
}
if($limites["tipo"]!="SI"){
$Qrypaises=$ln->query("select distinct grupo from dbo_paises WHERE pais_reconocimiento='SI' $str_where");
if($ln->num_rows($Qrypaises)){
while ($rowpaises =$ln->fetch($Qrypaises)) {
$paises[$rowpaises['grupo']]=$rowpaises['grupo'];
}
}
$ln->free_result($Qrypaises);
}
$Qrypaises=$ln->query("select * from dbo_paises WHERE pais_reconocimiento='SI' $str_where");
if($ln->num_rows($Qrypaises)){
while ($rowpaises =$ln->fetch($Qrypaises)) {
$paises[$rowpaises['id']]=$rowpaises['descripcion'];
}
}
$smarty->assign('paises',$paises);
$ln->free_result($Qrypaises);
//Fin Combo paises
$Qrypaises=$ln->query("select distinct sigla_moneda,moneda from dbo_paises WHERE pais_reconocimiento='SI' and sigla_moneda <>''");
$monedas["LOCAL"]="LOCAL";
$monedas["USD"]="DOLAR";
$smarty->assign('monedas',$monedas);
$ln->free_result($Qrypaises);
$js->enviar();
$js->novacio();
}break;
case "buscar":{
//echo "";
//preguntar();
//print_r($clientes);
//echo "
";
$smarty->assign("c_t",5);
$ln->query("DROP TABLE IF EXISTS `rpt_nscs_$idusuario`;");
$ln->query("CREATE TABLE `rpt_nscs_$idusuario` (
`cliente` varchar(70) NOT NULL,
`categoria` varchar(70) NOT NULL,
`servicio` varchar(70) NOT NULL,
`totalnuevaventa` decimal(25,2) NOT NULL,
`participacion` decimal(6,2) NOT NULL
)");
$total_v4=0;
$total_v5=0;
$total_v6=0;
$registros=0;
if(strtoupper(substr($periodos1,0,1))=="A"){$tabla_ventas=$prefijo."ventas";$tabla_reconocimiento=$prefijo."reconocimiento";}
if(strtoupper(substr($periodos1,0,1))=="B"){$tabla_ventas=$prefijo."budget";$tabla_reconocimiento=$prefijo."transbudget";}
if(strtoupper(substr($periodos1,0,1))=="F"){$tabla_ventas=$prefijo."forecat";$tabla_reconocimiento=$prefijo."transforecat";}
if($tabla_ventas){
$QryClientes=$ln->query("SELECT * FROM ".$prefijo."clientes ORDER BY descripcion");
while($rowFila=$ln->fetch($QryClientes)){
unset($idcliente,$cliente,$v1,$v2,$v3,$v4,$v5,$v6,$v7,$v8,$v9,$v10,$v11,$v12,$vt);
$idcliente=$rowFila["id"];
$cliente=$rowFila["descripcion"];
$nompais=get_descripcion($prefijo."paises",$pais);
if(!$nompais)$nompais=$pais;
$mon=get_moneda($pais);
$str_pais=" and ($tabla_ventas.id_pais='".$pais."' or ".$prefijo."paises.grupo='".$pais."' or ".$prefijo."paises.region='".$pais."')";
$vacio=in_array("",$clientes);
$clave=in_array("$idcliente",$clientes);
if($clave || $vacio){
$escribir=false;
$Data["cliente"]="$cliente";
$Data_aux["cliente"]="$cliente";
//Determinar Comparar
if($moneda=="LOCAL"){
//Hallar el total de Ventas
$QryT=$ln->query("SELECT sum(if($tabla_ventas.id_moneda='".$mon["sigla"]."',$tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100),($tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100))/".$prefijo."paises.tasa)) as totalnuevaventa FROM ((( $tabla_ventas INNER JOIN $tabla_reconocimiento ON $tabla_ventas.id=$tabla_reconocimiento.id_ventas) INNER JOIN ".$prefijo."tiposperiodos ON $tabla_ventas.id_tipoperiodos=".$prefijo."tiposperiodos.id) INNER JOIN ".$prefijo."paises ON $tabla_ventas.id_pais=".$prefijo."paises.id) WHERE $tabla_ventas.id_tipoperiodos='".$periodos1."' and $tabla_reconocimiento.ano=".$prefijo."tiposperiodos.ano and $tabla_ventas.clienteactual like '%VENTA NUEVA' $str_pais and $tabla_ventas.activo='SI'");
$totalvta=1;
if($rowT=$ln->fetch($QryT)){
$totalvta=$rowT["totalnuevaventa"];
}
if(!$totalvta)$totalvta=1;
//Consultar Valores
$QryCant=$ln->query("SELECT ".$prefijo."categorias.descripcion as categoria,".$prefijo."servicios.descripcion as servicio,sum(if($tabla_ventas.id_moneda='".$mon["sigla"]."',$tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100),($tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100))/".$prefijo."paises.tasa)) as totalnuevaventa, (sum(if($tabla_ventas.id_moneda='".$mon["sigla"]."',$tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100),($tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100))/".$prefijo."paises.tasa))) / $totalvta as participacion FROM ((((( $tabla_ventas INNER JOIN $tabla_reconocimiento ON $tabla_ventas.id=$tabla_reconocimiento.id_ventas) INNER JOIN ".$prefijo."tiposperiodos ON $tabla_ventas.id_tipoperiodos=".$prefijo."tiposperiodos.id) INNER JOIN ".$prefijo."servicios ON $tabla_ventas.id_servicio=".$prefijo."servicios.id) INNER JOIN ".$prefijo."categorias ON $tabla_ventas.id_categoria=".$prefijo."categorias.id) INNER JOIN ".$prefijo."paises ON $tabla_ventas.id_pais=".$prefijo."paises.id) WHERE $tabla_ventas.id_tipoperiodos='".$periodos1."' and $tabla_reconocimiento.ano=".$prefijo."tiposperiodos.ano and $tabla_ventas.id_cliente='".$rowFila["id"]."' and $tabla_ventas.clienteactual like '%VENTA NUEVA' $str_pais and $tabla_ventas.activo='SI' GROUP BY $tabla_ventas.id_categoria, ".$prefijo."servicios.id order by participacion desc" );
}else{
//Hallar el total de Ventas
$QryT=$ln->query("SELECT sum(if($tabla_ventas.id_moneda='USD',$tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100),($tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100))/".$prefijo."paises.tasa)) as totalnuevaventa FROM ((( $tabla_ventas INNER JOIN $tabla_reconocimiento ON $tabla_ventas.id=$tabla_reconocimiento.id_ventas) INNER JOIN ".$prefijo."tiposperiodos ON $tabla_ventas.id_tipoperiodos=".$prefijo."tiposperiodos.id) INNER JOIN ".$prefijo."paises ON $tabla_ventas.id_pais=".$prefijo."paises.id) WHERE $tabla_ventas.id_tipoperiodos='".$periodos1."' and $tabla_reconocimiento.ano=".$prefijo."tiposperiodos.ano and $tabla_ventas.clienteactual like '%VENTA NUEVA' $str_pais and $tabla_ventas.activo='SI'");
$totalvta=1;
if($rowT=$ln->fetch($QryT)){
$totalvta=$rowT["totalnuevaventa"];
}
if(!$totalvta)$totalvta=1;
//Consultar Valores
$QryCant=$ln->query("SELECT ".$prefijo."categorias.descripcion as categoria,".$prefijo."servicios.descripcion as servicio,sum(if($tabla_ventas.id_moneda='USD',$tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100),($tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100))/".$prefijo."paises.tasa)) as totalnuevaventa, (sum(if($tabla_ventas.id_moneda='USD',$tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100),($tabla_reconocimiento.valor * ($tabla_reconocimiento.probabilidad/100))/".$prefijo."paises.tasa)))/$totalvta as participacion FROM ((((( $tabla_ventas INNER JOIN $tabla_reconocimiento ON $tabla_ventas.id=$tabla_reconocimiento.id_ventas) INNER JOIN ".$prefijo."tiposperiodos ON $tabla_ventas.id_tipoperiodos=".$prefijo."tiposperiodos.id) INNER JOIN ".$prefijo."servicios ON $tabla_ventas.id_servicio=".$prefijo."servicios.id) INNER JOIN ".$prefijo."categorias ON $tabla_ventas.id_categoria=".$prefijo."categorias.id) INNER JOIN ".$prefijo."paises ON $tabla_ventas.id_pais=".$prefijo."paises.id) WHERE $tabla_ventas.id_tipoperiodos='".$periodos1."' and $tabla_reconocimiento.ano=".$prefijo."tiposperiodos.ano and $tabla_ventas.id_cliente='".$rowFila["id"]."' and $tabla_ventas.clienteactual like '%VENTA NUEVA' $str_pais and $tabla_ventas.activo='SI' GROUP BY $tabla_ventas.id_categoria, ".$prefijo."servicios.id order by participacion desc");
}
while($rowClient=$ln->fetch($QryCant)){
$registros++;
//Dta aux para pantalla
$Data_aux["cliente"]=$cliente;
$Data_aux["v1"]=$rowClient["categoria"];
$Data_aux["v2"]=$rowClient["servicio"];
$Data_aux["v3"]=number_format($rowClient["totalnuevaventa"]);
$Data_aux["v4"]=number_format($rowClient["participacion"]*100,2)."%";
//Data para Reporte descargable
$Data["cliente"]=$cliente;
$Data["v1"]=$rowClient["categoria"];
$Data["v2"]=$rowClient["servicio"];
$Data["v3"]=$rowClient["totalnuevaventa"];
$Data["v4"]=$rowClient["participacion"]*100;
if($class=="class=\"odd\"")$class="";
else $class="class=\"odd\"";
$Data_aux["class"]=$class;
$smarty->append('Data',$Data_aux);
$Data["cliente"]=str_replace("'","\'",$Data["cliente"]);
$QryDw=$ln->query("INSERT INTO rpt_nscs_$idusuario (cliente,categoria,servicio,totalnuevaventa,participacion) values('".$Data["cliente"]."','".$Data["v1"]."','".$Data["v2"]."','".$Data["v3"]."','".$Data["v4"]."')");
}
}
}
$total_v5=($total_v6*100)/$total_v4;
$smarty->assign("total_v3",number_format($totalvta));
$smarty->assign("pais",$nompais." - ".get_descripcion($prefijo."tiposperiodos",$periodos1));
$moned=get_moneda($pais);
if($moneda=="LOCAL" && $moned)$str_mon=$moned["nombre"];
else $str_mon="DOLAR";
$smarty->assign("str_mon",$str_mon);
$smarty->assign("filas",$filas);
if($registros==0){
$mensaje=$mensaje."NO HAY DATOS";
$plantilla="resultado.html";
$accion="error";
}
}
}break;
case "descargar":{
$nomarchivo="NewSalesCategoryService_".date('Y-m-d_H-i-s',time()).".csv";
$file=$rootdir."files/descargables/".$nomarchivo;
unlink($file);
$fp = fopen($file,"w");
fwrite($fp,$cadena);
$periodos1=str_replace("
"," ",$periodos1);
//Titulos de la Tabla
$cadena="NEW SALES FOR CATEGORY SERVICE $pais $periodos1 $str_mon\r\n\r\nCLIENTE;CATEGORY;SERVICE;TOTAL NEW SALE;PARTICIPATION\r\n\r\n";
//Escribir los encabezados del informe
fwrite($fp,$cadena);
//Escribir las filas del informe
$QryInforme=$ln->query("SELECT * FROM rpt_nscs_$idusuario");
$v3=0;
while($ro=$ln->fetch($QryInforme)){
$ocliente=str_replace("'","\'",$ro["cliente"]);
$cadena="$ocliente;".$ro["categoria"].";".$ro["servicio"].";".$ro["totalnuevaventa"].";".$ro["participacion"]."\r\n";
$cadena=str_replace(".",",",$cadena);
fwrite($fp,$cadena);
$v3=$v3+$ro["totalnuevaventa"];
}
//Adicionar los Totales
$cadena="\r\n TOTALES;;;".$v3.";";
$cadena=str_replace(".",",",$cadena);
fwrite($fp,$cadena);
fclose($fp);
//forzar la descarga
$filename = $nomarchivo;
$size = filesize($file);
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
header("Content-Transfer-Encoding: text");
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: $size");
@readfile("$file");
exit;
}
break;
}
}
$smarty->assign('titulo',$title);
$smarty->assign('accion',$accion);
$smarty->assign('idpagina',$idpagina);
$smarty->assign('permiso_pagina',$permisos[$idpagina]);
$smarty->assign('pagina',strtoupper($titulos[$idpagina]));
$smarty->assign('mensaje',$mensaje);
$smarty->display($plantilla);
$ln->desconectar();
?>