<?php
//include "clase.conexion.php";
class clsReporteVentaDetFamilia extends clsConexion{
	public $varTitulo 	= "REPORTE DETALLADO DE VENTAS POR FAMILIA";
	function FormuBusqueda() {
		$sqanio = "select distinct id_anio, id_anio from _sv_metas order by id_anio desc;";
		$sqlmes	= array("01"=>"ENERO", "02"=>"FEBRERO", "03"=>"MARZO", "04"=>"ABRIL", "05"=>"MAYO", "06"=>"JUNIO",
						"07"=>"JULIO", "08"=>"AGOSTO", "09"=>"SETIEMBRE", "10"=>"OCTUBRE", "11"=>"NOVIEMBRE", "12"=>"DICIEMBRE");
						
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"470", "clase"=>"Formtitulo", "titulo"=>$this->varTitulo,
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST", "onsubmit"=>"return false;"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo)
									  ),
						"conte"=>array(array("titulo"=>"Periodo :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										     "campo"=>array("tipo"=>"varios",
														    "contenido"=>array(array("tipo"=>"select", "id"=>"anio", "name"=>"anio", "valor"=>NULL,
																				     "combo"=>array("sql"=>$sqanio, "sincod"=>"SI", "titulo"=>"--[[ANIO]]--",
																				     "ancho"=>"83")),
																			   array("tipo"=>"select_simple", "id"=>"mes", "name"=>"mes", 
																			   		 "combo"=>array("sql"=>$sqlmes, "ancho"=>"150"))
																			   )
														   )
										    ),
									   array("titulo"=>"Proveedor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"pcod", "name"=>"pcod", 
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "keypress"=>"return SoloEnteros(event)",
																					 "blur"=>"xajax_seleccionar_proveedor(this.value)", 
																					 "dclick"=>"fn_cargar_listado_proveedor('pcod', 'pnom')"),
																			   array("tipo"=>"text", "id"=>"pnom", "name"=>"pnom", 
																					 "ancho"=>"300", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 	)
											 )
									   ),
						"boton"=>array(array("tipo"=>"button", "id"=>"bexp", "name"=>"bexp", "ancho"=>"100", "texto"=>"Exportar",
									 	     "clase"=>"boton_exportar", "click"=>"javascript:xExportarJS()")
									 )
						);
		return $this->setTablaForm($varTabla);
		}
 	function GridDatos($varForm = NULL
					   )  {						  
		$varFIni = ($varForm["fini"]=="") ? "NULL" : "'".FechaMySql($varForm["fini"])."'";
		$varFFin = ($varForm["ffin"]=="") ? "NULL" : "'".FechaMySql($varForm["ffin"])."'";
		
		if($varForm["tip"]=="R") {
			$sql = "call spQuery_Avance_Ventas_Vendedor('$varForm[anio]', '$varForm[mes]', '$varForm[pcod]', $varFIni, $varFFin);";
			$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"700", "sql_cab"=>$sql,	"pages"=>100,
							"conte"=>array(array("tamano"=>20, "titulo"=>"CVEN", "alinear"=>"center"),
										   array("tamano"=>200,"titulo"=>"VENDEDOR", "fijo"=>"SI", "funcion"=>array("contar")),										  
										   array("tamano"=>70, "titulo"=>"CUOTA", "alinear"=>"right", "funcion"=>array("autosuma")),
										   array("tamano"=>70, "titulo"=>"COBERT", "alinear"=>"right", "funcion"=>array("autosuma")),
										   array("tamano"=>70, "titulo"=>"MAYORI", "alinear"=>"right", "funcion"=>array("autosuma")),
										   array("tamano"=>70, "titulo"=>"MERCAD", "alinear"=>"right", "funcion"=>array("autosuma")),
										   array("tamano"=>70, "titulo"=>"PUEBLO", "alinear"=>"right", "funcion"=>array("autosuma")),
										   array("tamano"=>70, "titulo"=>"VENTA", "alinear"=>"right", "funcion"=>array("autosuma")),
										   array("tamano"=>70, "titulo"=>"%AVAN", "alinear"=>"right", "funcion"=>array("porc","7","2"), "tipodat"=>"porc"),
										   array("tamano"=>70, "titulo"=>"NEC.DIA.", "alinear"=>"right", "funcion"=>array("autosuma")),
										   array("tamano"=>70, "titulo"=>"TEND.", "alinear"=>"right", "funcion"=>array("autosuma")),
										   array("tamano"=>70, "titulo"=>"%TEND.", "alinear"=>"right", "funcion"=>array("porc","10","2"), "tipodat"=>"porc"),
										   )
							);
			$varReturn = $this->SetGridTabla($varTabla);
			}
		else {
			$sql = "call spQuery_Avance_Ventas_Vendedor_Max('$varForm[anio]', '$varForm[mes]', '$varForm[pcod]', $varFIni, $varFFin);";
			$varCab = array(array("cab"=>"PREVENTA FACTURADA", "det"=>array("PREV","PEDID","PDV")),
							array("cab"=>"VENTA EFECTIVA", "det"=>array("CUOTA","COBERT","MAYORI","MERCAD","PUEBLO","VENTA","%AVAN","%EFEC")),
							array("cab"=>"TENDENCIA", "det"=>array("NECDIA", "TEND", "%TEND")),
							array("cab"=>"VENTA RECHAZADA", "det"=>array("MONTO", "%META", "%PREV")),
							array("cab"=>"PEDIDOS", "det"=>array("EFEC.", "RECH.", "%RECH.")),
							array("cab"=>"PUNTOS DE VENTA", "det"=>array("EFEC.", "RECH.", "%RECH."))
							);
			$varNiv = array(0 =>array("COD"=>"CVEN","DES"=>"VENDEDOR"),
							1 =>array("COD"=>"CTIP","DES"=>"TIPO"),
							2 =>array("COD"=>"CTOT","DES"=>"TOTAL"));
			$varFor = array(15=>array("tipodat"=>"porc", "formula"=>array("valor1"=>9, "valor2"=>14, "funcion"=>"porc")),
							16=>array("tipodat"=>"porc", "formula"=>array("valor1"=>6, "valor2"=>14, "funcion"=>"porc")),
							19=>array("tipodat"=>"porc", "formula"=>array("valor1"=>9, "valor2"=>18, "funcion"=>"porc")),
							
							21=>array("tipodat"=>"porc", "formula"=>array("valor1"=>9, "valor2"=>20, "funcion"=>"porc")),
							22=>array("tipodat"=>"porc", "formula"=>array("valor1"=>6, "valor2"=>20, "funcion"=>"porc")),
							
							25=>array("tipodat"=>"porc", "formula"=>array("valor1"=>7, "valor2"=>24, "funcion"=>"porc")),
							28=>array("tipodat"=>"porc", "formula"=>array("valor1"=>8, "valor2"=>27, "funcion"=>"porc"))
							);
			$varReturn =  $this->setGridCombina($varCab, $sql, $varNiv, NULL, $varFor);
			}
		return $varReturn;
		}
	function Imprimir ($varAni = NULL, 
					   $varMes = NULL,
					   $varPro = NULL, 					    
					   $varIni = NULL, 
					   $varFin = NULL
					   )  {
						  
		$varAniC = (isset($varAni) && $varAni<>"")?"'".$varAni."'":"NULL";
		$varMesC = (isset($varMes) && $varMes<>"")?"'".$varMes."'":"NULL";
		$varProC = (isset($varPro) && $varPro<>"")?$varPro:"NULL";
		$varIniC = (isset($varIni) && $varIni<>"")?"'".FechaMySql($varIni)."'":"NULL";
		$varFinC = (isset($varFin) && $varFin<>"")?"'".FechaMySql($varFin)."'":"NULL";
		
		
		$sql = "call spQuery_Avance_Ventas_Vendedor($varAniC, $varMesC, $varProC, $varIniC, $varFinC);";	
		
		$html ='<table style="width: 98%; padding: 0; margin-top:10px; margin-bottom:10px; border-collapse:collapse;" align="center">';
		$html.='<thead>';
		$html.='<tr>';
		$html.='<td>';
		$html.='<table style="width: 100%;">';
		$html.='<tr>';
		$html.='<td style="width: 9%; vertical-align: middle; text-align:left;"><b>Periodo</b></td>';
		$html.='<td style="width: 1%; vertical-align: middle; text-align:center;"><b>:</b></td>';
		$html.='<td style="width: 40%; vertical-align: middle; text-align:left;"><b>'.MesNombreLargo($varMes).' - '.$varAni.'</b></td>';
		$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b>'.Config("SYSEMP").'</b></td>';
		$html.='</tr>';
		$html.='<tr>';
		$html.='<td style="vertical-align: middle; text-align:left;"><b>Fecha</b></td>';
		$html.='<td style="vertical-align: middle; text-align:center;"><b>:</b></td>';
		$html.='<td style="vertical-align: middle; text-align:left;"><b>'.$varIni.' - '.$varFin.'</b></td>';
		$html.='<td style="vertical-align: middle; text-align:right;"><b>'.$this->varTitulo.'</b></td>';
		$html.='</tr>';
		$html.='<tr>';
		$html.='<td style="vertical-align: middle; text-align:left;"><b>Linea</b></td>';
		$html.='<td style="vertical-align: middle; text-align:center;"><b>:</b></td>';
		$html.='<td style="width: 50%; vertical-align: middle; text-align:left;"><b>';
		if ($_GET[pro]<>"") {
			$sqlp = "select id_proveedor, nombre from _sv_proveedor where id_proveedor=".$varPro;
			$this->AbrirConexion();
			$rsp = $this->Query($sqlp);
			$this->CerrarConexion();
			$filap = $this->FetchArray($rsp);
			$html.= $filap[0].' - '.$filap[1];
			}
		else {
			$html.= 'TODOS LOS PROVEEDORES';
			}
		$html.='</b></td>';
		$html.='<td style="width: 50%; vertical-align: middle; text-align:right;">'.MostrarFechaActual().'</td>';
		$html.='</tr>';
		$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
		$html.='</thead>';	
		$html.='<tr>';
		$html.='<td>';
		$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
		$html.='<thead>';
		$html.='<tr class="titu_tabla">';
		$html.='<td width="2" class="center">&nbsp;</td>';
		$html.='<td width="2" class="center">CVEN</td>';
		$html.='<td width="150">VENDEDOR</td>';
		$html.='<td width="60" class="right">META</td>';
		$html.='<td width="60" class="right">COBERT</td>';
		$html.='<td width="60" class="right">MAYORI</td>';
		$html.='<td width="60" class="right">MERCAD</td>';
		$html.='<td width="60" class="right">PUEBLO</td>';
		$html.='<td width="60" class="right">AVANCE</td>';
		$html.='<td width="60" class="right">%AVANC</td>';
		$html.='<td width="60" class="right">NEC.DIARIA</td>';
		$html.='<td width="60" class="right">TENDENCIA</td>';
		$html.='<td width="60" class="right">%TEND.</td>';
		$html.='</tr>';
		$html.='</thead>';
		//$sql = "call spQuery_Avance_Ventas_Vendedor('$_GET[anio]', '$_GET[mes]', '$_GET[pro]', $varFIni, $varFFin);";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$varTot[] = NULL;
		$varNumDoc = 0;
		$varNumF = $this->NumField($rs);
		while($fila = $this->FetchArray($rs))	{
			for($i=2; $i<$varNumF;$i++) {
				$varTot[$i] = $varTot[$i] + $fila[$i];
				}
			$varNumDoc++;
			$html.='<tr class="list_row">';
			$html.='<td class="center">&nbsp;</td>';
			$html.='<td class="center">'.$fila["CVEN"].'</td>';
			$html.='<td NOWRAP>'.$fila["VENDEDOR"].'</td>';
			$html.='<td class="right">';
			if(!is_null($fila["META"]) && $fila["META"] <> 0)
				$html.=number_format($fila["META"],"2",".",",");
			$html.='</td>';
			
			$html.='<td class="right">';
			if(!is_null($fila["COBERTURA"]) && $fila["COBERTURA"] <> 0)
				$html.=number_format($fila["COBERTURA"],"2",".",",");
			$html.='</td>';
			
			$html.='<td class="right">';
			if(!is_null($fila["MAYORISTA"]) && $fila["MAYORISTA"] <> 0)
				$html.=number_format($fila["MAYORISTA"],"2",".",",");
			$html.='</td>';
			
			$html.='<td class="right">';
			if(!is_null($fila["MERCADO"]) && $fila["MERCADO"] <> 0)
				$html.=number_format($fila["MERCADO"],"2",".",",");
			$html.='</td>';
			
			$html.='<td class="right">';
			if(!is_null($fila["PUEBLOS"]) && $fila["PUEBLOS"] <> 0)
				$html.=number_format($fila["PUEBLOS"],"2",".",",");
			$html.='</td>';
			
			$html.='<td class="right">';
			if(!is_null($fila["VENTA"]) && $fila["VENTA"] <> 0)
				$html.=number_format($fila["VENTA"],"2",".",",");
			$html.='</td>';
			
			$html.='<td class="right">';
			if(!is_null($fila["PORC_AVANCE"]) && $fila["PORC_AVANCE"] <> 0)
				$html.=number_format($fila["PORC_AVANCE"],"2",".",",")."%";
			$html.='</td>';
			$html.='<td class="right">';
			if(!is_null($fila["NEC_DIARIA"]) && $fila["NEC_DIARIA"] <> 0)
				$html.=number_format($fila["NEC_DIARIA"],"2",".",",");
			$html.='</td>';
			$html.='<td class="right">';
			if(!is_null($fila["TENDENCIA"]) && $fila["TENDENCIA"] <> 0)
				$html.=number_format($fila["TENDENCIA"],"2",".",",");
			$html.='</td>';
			$html.='<td class="right">';
			if(!is_null($fila["PORC_TENDENCIA"]) && $fila["PORC_TENDENCIA"] <> 0)
				$html.=number_format($fila["PORC_TENDENCIA"],"2",".",",")."%";
			$html.='</td>';
			$html.='</tr>';
			}
		$html.='<tr class="titu_tabla">';
		$html.='<td class="left"></td>';
		$html.='<td class="left"></td>';
		$html.='<td class="left">'.number_format($varNumDoc,"0",".",",").' Registros</td>';
		for($i=2; $i<$varNumF;$i++) {
			if ($i==8) 
				$html.='<td class="right">'.number_format($varTot[7]/$varTot[2]*100,"2",".",",").'%</td>';
			elseif($i==11)
				$html.='<td class="right">'.number_format($varTot[10]/$varTot[2]*100,"2",".",",").'%</td>';
			else
				$html.='<td class="right">'.number_format($varTot[$i],"2",".",",").'</td>';
			}
		$html.='</tr>';
		$html.='</table>';
		$html.='</td>';
		$html.='</tr>';			
		$html.='</table>';
		return $html;		
		}
	function ImprimirDet (
					$varAni = NULL, 
					$varMes = NULL,
					$varPro = NULL, 					    
					$varIni = NULL, 
					$varFin = NULL
					)  {
						  
		$varAniC = (isset($varAni) && $varAni<>"")?"'".$varAni."'":"NULL";
		$varMesC = (isset($varMes) && $varMes<>"")?"'".$varMes."'":"NULL";
		$varProC = (isset($varPro) && $varPro<>"")?$varPro:"NULL";
		$varIniC = (isset($varIni) && $varIni<>"")?"'".FechaMySql($varIni)."'":"NULL";
		$varFinC = (isset($varFin) && $varFin<>"")?"'".FechaMySql($varFin)."'":"NULL";
		
		$sql = "call spQuery_Avance_Ventas_Vendedor_Max($varAniC, $varMesC, $varProC, $varIniC, $varFinC);";	
		
		$varCab = array(array("cab"=>"PREVENTA FACTURADA", "det"=>array("PREV","PEDID","PDV")),
						array("cab"=>"VENTA EFECTIVA", "det"=>array("CUOTA","COBERT","MAYORI","MERCAD","PUEBLO","VENTA","%AVAN","%EFEC")),
						array("cab"=>"TENDENCIA", "det"=>array("NECDIA", "TEND", "%TEND")),
						array("cab"=>"VENTA RECHAZADA", "det"=>array("MONTO", "%META", "%PREV")),
						array("cab"=>"PEDIDOS", "det"=>array("EFEC.", "RECH.", "%RECH.")),
						array("cab"=>"PUNTOS DE VENTA", "det"=>array("EFEC.", "RECH.", "%RECH."))
						);
		$varNiv = array(0 =>array("COD"=>"CVEN","DES"=>"VENDEDOR"),
						1 =>array("COD"=>"CTIP","DES"=>"TIPO"),
						2 =>array("COD"=>"CTOT","DES"=>"TOTAL"));
		$varFor = array(15=>array("tipodat"=>"porc", "formula"=>array("valor1"=>9, "valor2"=>14, "funcion"=>"porc")),
						16=>array("tipodat"=>"porc", "formula"=>array("valor1"=>6, "valor2"=>14, "funcion"=>"porc")),
						19=>array("tipodat"=>"porc", "formula"=>array("valor1"=>9, "valor2"=>18, "funcion"=>"porc")),
						
						21=>array("tipodat"=>"porc", "formula"=>array("valor1"=>9, "valor2"=>20, "funcion"=>"porc")),
						22=>array("tipodat"=>"porc", "formula"=>array("valor1"=>6, "valor2"=>20, "funcion"=>"porc")),
						
						25=>array("tipodat"=>"porc", "formula"=>array("valor1"=>7, "valor2"=>24, "funcion"=>"porc")),
						28=>array("tipodat"=>"porc", "formula"=>array("valor1"=>8, "valor2"=>27, "funcion"=>"porc"))
						);
		
		$html='<table style="width: 98%; padding: 0; margin-top:10px; margin-bottom:10px; border-collapse:collapse;" align="center">';
		$html.='<thead>';
		$html.='<tr>';
		$html.='<td>';
		$html.='<table style="width: 100%;">';
		$html.='<tr>';
		$html.='<td style="width: 9%; vertical-align: middle; text-align:left;"><b>Periodo</b></td>';
		$html.='<td style="width: 1%; vertical-align: middle; text-align:center;"><b>:</b></td>';
		$html.='<td style="width: 40%; vertical-align: middle; text-align:left;"><b>'.MesNombreLargo($varMes).' - '.$varAni.'</b></td>';
		$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b>'.Config("SYSEMP").'</b></td>';
		$html.='</tr>';
		$html.='<tr>';
		$html.='<td style="vertical-align: middle; text-align:left;"><b>Fecha</b></td>';
		$html.='<td style="vertical-align: middle; text-align:center;"><b>:</b></td>';
		$html.='<td style="vertical-align: middle; text-align:left;"><b>'.$varIni.' - '.$varFin.'</b></td>';
		$html.='<td style="vertical-align: middle; text-align:right;"><b>'.$this->varTitulo.'</b></td>';
		$html.='</tr>';
		$html.='<tr>';
		$html.='<td style="vertical-align: middle; text-align:left;"><b>Linea</b></td>';
		$html.='<td style="vertical-align: middle; text-align:center;"><b>:</b></td>';
		$html.='<td style="width: 50%; vertical-align: middle; text-align:left;"><b>';
		if ($_GET[pro]<>"") {
			$sqlp = "select id_proveedor, nombre from _sv_proveedor where id_proveedor=".$varPro;
			$this->AbrirConexion();
			$rsp = $this->Query($sqlp);
			$this->CerrarConexion();
			$filap = $this->FetchArray($rsp);
			$html.= $filap[0].' - '.$filap[1];
			}
		else {
			$html.= 'TODOS LOS PROVEEDORES';
			}
		$html.='</b></td>';
		$html.='<td style="width: 50%; vertical-align: middle; text-align:right;">'.MostrarFechaActual().'</td>';
		$html.='</tr>';
		$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
		$html.='</thead>';	
		$html.='<tr>';
		$html.='<td>';				
		$html.= $this->setGridCombinaPrint($varCab, $sql, $varNiv, $varFor);
		$html.='</td>';
		$html.='</tr>';			
		$html.='</table>';
		return $html;
		}
	}
?>