<?php
//include "clase.conexion.php";
class clsMetas extends clsConexion{
	public $varTitulo 	= "COUTAS POR FAMILIA & SUBFAMILIA";
	function FormuBusqueda() {
		$sqlmes = "call spQuery_ComboMes_CuotaVentaFamilia('Ninguno');";
		$sqanio = "select distinct id_anio, id_anio from _sv_cuota_venta_familia order by id_anio desc;";
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"430", "clase"=>"FormTitulo", "titulo"=>$this->varTitulo." - BUSQUEDA",
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST", "onsubmit"=>"return false;"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo),
									   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt", "valor"=>$Opcion)
									  ),
						"conte"=>array(array("titulo"=>"Proveedor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"pcod", "name"=>"pcod", 
																					 "valor"=>$_POST["pcod"], 
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "blur"=>"xajax_seleccionar_proveedor(this.value)", 
																					 "dclick"=>"fn_cargar_listado_proveedor('pcod', 'pnom')"),
																			   array("tipo"=>"text", "id"=>"pnom", "name"=>"pnom", 
																			   		 "valor"=>$_POST["pnom"], 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
															)
											 ),									   
									   array("titulo"=>"Vendedor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"vcod", "name"=>"vcod", 
																					 "valor"=>$_POST["vcod"], 
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "blur"=>"xajax_seleccionar_vendedor(this.value)", 
																					 "dclick"=>"fn_cargar_listado_vendedor('vcod', 'vnom')"),
																			   array("tipo"=>"text", "id"=>"vnom", "name"=>"vnom", 
																			   		 "valor"=>$_POST["vnom"], 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
															)
											 ),
									   array("titulo"=>"Periodo :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										     "campo"=>array("tipo"=>"varios",
														    "contenido"=>array(array("tipo"=>"select", "id"=>"anio", "name"=>"anio", 
																					 "valor"=>"Ninguno",
																				     "combo"=>array("sql"=>$sqanio, "sincod"=>"SI", 
																					 "titulo"=>"--[[ANIO]]--", "ancho"=>"83", 
																					 "reload"=>"xajax_comboCuotaVentaFamilia_Mes(this.value)")),
																			   array("tipo"=>"select", "id"=>"mes", "name"=>"mes", "valor"=>NULL, 
																			   		 "div"=>"divMes",
																				     "combo"=>array("sql"=>$sqlmes, "titulo"=>"--[[ MES ]]--","ancho"=>"160"))
																			   )
														   )
										    )
									   ),
						  "boton"=>array(array("tipo"=>"submit", "id"=>"bcon", "name"=>"bcon", "ancho"=>"100", "texto"=>"Buscar",
											   "clase"=>"boton_buscar", "click"=>"javascript:xBuscarJS()"),
										 array("tipo"=>"button", "id"=>"bexp", "name"=>"bexp", "ancho"=>"100", "texto"=>"Exportar",
											   "clase"=>"boton_exportar", "click"=>"javascript:xExportarJS()"),								
										 array("tipo"=>"button", "id"=>"bnew", "name"=>"bnew", "ancho"=>"100", "texto"=>"Nuevo", 
											   "clase"=>"boton_nuevo", "click"=>"javascript:FormuRegistroJS('NEW')"),
										 )
						);
		return $this->setTablaForm($varTabla);
		}
 	function GridDatos($varForm  = NULL, 
					   $varField = NULL, 
					   $varOrder = NULL
					   ) {
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		$varAni = ($varForm[anio]=="Ninguno")?"NULL":"'".$varForm[anio]."'";
		$varMes = ($varForm[mes]=="Ninguno")?"NULL":"'".$varForm[mes]."'";
		$sql = "call spQuery_Cuota_Venta_Familia($varAni, $varMes, '$varForm[pcod]', '$varForm[vcod]', $varFie, $varOrd)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"900", "sql_cab"=>$sql,	"pages"=>50,
						"conte"=>array(array("titulo"=>"ANIO",   	"ancho"=>"20" , "alinear"=>"center"),
									   array("titulo"=>"MES",   	"ancho"=>"80" , "alinear"=>"center", "fijo"=>"SI"), 
									   array("titulo"=>"CVEN",     	"ancho"=>"20", "alinear"=>"center"), 
									   array("titulo"=>"VENDEDOR",	"ancho"=>"250", "clase"=>"sortable", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"CPRO",     	"ancho"=>"20", "alinear"=>"center"), 
									   array("titulo"=>"PROVEEDOR",	"ancho"=>"250", "clase"=>"sortable", "fijo"=>"SI"),
									   array("titulo"=>"CTIP",     	"ancho"=>"20", "alinear"=>"center"), 
									   array("titulo"=>"TIPO",	"ancho"=>"250", "clase"=>"sortable", "fijo"=>"SI"),
									   array("titulo"=>"CFAM",     	"ancho"=>"20", "alinear"=>"center"), 
									   array("titulo"=>"FAMILIA",	"ancho"=>"250", "clase"=>"sortable", "fijo"=>"SI"),
									   array("titulo"=>"CSUB",     	"ancho"=>"20", "alinear"=>"center"), 
									   array("titulo"=>"SUBFAMILIA",	"ancho"=>"250", "clase"=>"sortable", "fijo"=>"SI"),
									   array("titulo"=>"MONTO",	   "ancho"=>"100", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("titulo"=>"CANTIDAD",	   "ancho"=>"100", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("titulo"=>"FCREADO",  "ancho"=>"100", "oculto"=>"SI"),
									   array("titulo"=>"FUPDATE",  "ancho"=>"100", "oculto"=>"SI"),
									   array("titulo"=>"UCREADO",  "ancho"=>"100", "oculto"=>"SI"),
									   array("titulo"=>"UUPDATE",  "ancho"=>"100", "oculto"=>"SI")
									   ),
						"boton"=>array(array("tipo"=>"img", "id"=>"bbus", "name"=>"bbus", "img"=>"images/b_table_edit.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>EDITAR</b> EL REGISTRO<br>"),
															 array("valor"=>"<b>Periodo :</b> "),
															 array("valor"=>1, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>0, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Vendedor : </b>"),
															 array("valor"=>2, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>3, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Proveedor : </b>"),
															 array("valor"=>4, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>5, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Tipo : </b>"),
															 array("valor"=>6, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>7, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Familia : </b>"),
															 array("valor"=>8, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>9, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Sub Familia : </b>"),
															 array("valor"=>10, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>11, "tipo"=>"INDEX")
															 ),
											 "click"=>array("funcion"=>"FormuRegistroJS",
															"param"=>array(array("valor"=>"EDIT", "tipo"=>"TEXTO"),
																		   array("valor"=>0, 	  "tipo"=>"INDEX"),
																		   array("valor"=>1, 	  "tipo"=>"INDEX"),
																		   array("valor"=>2, 	  "tipo"=>"INDEX"),
																		   array("valor"=>4, 	  "tipo"=>"INDEX"),
																		   array("valor"=>6, 	  "tipo"=>"INDEX"),
																		   array("valor"=>8, 	  "tipo"=>"INDEX"),
																		   array("valor"=>10, 	  "tipo"=>"INDEX")
																		  )
															)
											 
											),
									   array("tipo"=>"img", "id"=>"badd", "name"=>"badd", "img"=>"images/b_table_delete.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>ELIMINAR</b> EL REGISTRO<br>"),
															 array("valor"=>"<b>Periodo :</b> "),
															 array("valor"=>1, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>0, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Vendedor : </b>"),
															 array("valor"=>2, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>3, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Proveedor : </b>"),
															 array("valor"=>4, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>5, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Tipo : </b>"),
															 array("valor"=>6, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>7, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Familia : </b>"),
															 array("valor"=>8, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>9, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Sub Familia : </b>"),
															 array("valor"=>10, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>11, "tipo"=>"INDEX")
															 ),
											 "click"=>array("funcion"=>"xEliminarJS",
															"param"=>array(array("valor"=>0, 	  "tipo"=>"INDEX"),
																		   array("valor"=>1, 	  "tipo"=>"INDEX"),
																		   array("valor"=>2, 	  "tipo"=>"INDEX"),
																		   array("valor"=>4, 	  "tipo"=>"INDEX"),
																		   array("valor"=>6, 	  "tipo"=>"INDEX"),
																		   array("valor"=>8, 	  "tipo"=>"INDEX"),
																		   array("valor"=>10, 	  "tipo"=>"INDEX"),
																		   array("valor"=>"this", "tipo"=>"NUM"),
																		   )
															)
											 )
									)
						);
		return $this->SetGridTabla2($varTabla);
		//return $sql;
		}
	function FormuRegistro($varOpt=NULL, $varAnio=NULL, $varMes=NULL, $varVen=NULL, $varPro=NULL, $varTip=NULL, $varFam=NULL, $varSub=NULL) {
		if($varOpt=="NEW") {
			$SubTit = $this->varTitulo." - NUEVO";
			$Permite = $_SESSION["_FINS"];
			$varLock = "NO";
			$Fila = NULL;
			$sqltip = "call spQuery_Combo_Producto_Tipo_Prov(0);";
			$sqlfam = "call spQuery_Combo_Producto_Familia_Prov(0, NULL);";
			$sqlsub = "call spQuery_Combo_Producto_SubFamilia_Prov(0, NULL);";
			}
		else {
			$SubTit =  $this->varTitulo." - EDITAR";
			$varLock = "SI";
			$sql = "select id_anio, id_mes, id_proveedor, id_vendedor, id_tipo, id_familia, id_sub_familia, monto, cantidad
					from _sv_cuota_venta_familia
					where id_anio 		= '$varAnio'
					and id_mes 	  		= '".substr($varMes,0,2)."'
					and id_vendedor 	= $varVen
					and id_proveedor 	= $varPro
					and id_tipo 		= $varTip
					and id_familia 		= $varFam
					and id_sub_familia 	= $varSub;";	
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$Fila = $this->FetchArray($rs);
			$sqltip = "call spQuery_Combo_Producto_Tipo_Prov($Fila[id_proveedor]);";
			$sqlfam = "call spQuery_Combo_Producto_Familia_Prov($Fila[id_proveedor], '$Fila[id_tipo]');";
			$sqlsub = "call spQuery_Combo_Producto_SubFamilia_Prov($Fila[id_proveedor], '$Fila[id_familia]');";
			}
		$sqanio = "call spQuery_ComboAnio_CuotaVentaFamilia();";
		$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");
		$sqlven = "call spQuery_Combo_Vendedor();";
		$sqlpro = "call spQuery_Combo_Proveedor_Meta('Ninguno','Ninguno', 'Ninguno');";
		
		
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"400", "clase"=>"FormTitulo", "titulo"=>$SubTit,
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo),
									   array("tipo"=>"oculto", "id"=>"opt",  "name"=>"opt",  "valor"=>$varOpt),
									   array("tipo"=>"oculto", "id"=>"anio1",  "name"=>"anio1",  "valor"=>$Fila["id_anio"]),
									   array("tipo"=>"oculto", "id"=>"mes1",  "name"=>"mes1",  "valor"=>$Fila["id_mes"]),
									   array("tipo"=>"oculto", "id"=>"pro1",  "name"=>"pro1",  "valor"=>$Fila["id_proveedor"]),
									   array("tipo"=>"oculto", "id"=>"ven1",  "name"=>"ven1",  "valor"=>$Fila["id_vendedor"]),
									   array("tipo"=>"oculto", "id"=>"tip1",  "name"=>"tip1",  "valor"=>$Fila["id_tipo"]),
									   array("tipo"=>"oculto", "id"=>"fam1",  "name"=>"fam1",  "valor"=>$Fila["id_familia"]),
									   array("tipo"=>"oculto", "id"=>"sub1",  "name"=>"sub1",  "valor"=>$Fila["id_sub_familia"])
									   ),
						"conte"=>array(array("titulo"=>"Periodo :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										     "campo"=>array("tipo"=>"varios",
														    "contenido"=>array(array("tipo"=>"select", "id"=>"anio", "name"=>"anio", 
																					 "valor"=>$Fila["id_anio"],
																				     "combo"=>array("sql"=>$sqanio, "sincod"=>"SI", 
																					 "titulo"=>"--[[ANIO]]--", "ancho"=>"83", "locked"=>$varLock)),
																			   array("tipo"=>"select_simple", "id"=>"mes", "name"=>"mes", 
																			   		 "valor"=>$Fila["id_mes"], 
																				     "combo"=>array("sql"=>$sqlmes, "titulo"=>"--[[ MES ]]--",
																					 "ancho"=>"160", "locked"=>$varLock))
																			   )
														   )
										     ),
										array("titulo"=>"Vendedor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										     "campo"=>array("tipo"=>"select", "id"=>"ven", "name"=>"ven", "valor"=>$Fila["id_vendedor"],
														    "combo"=>array("sql"=>$sqlven, "sincod"=>"SI", "titulo"=>"--[[SELECCIONAR]]--", 
																		   "ancho"=>"250", "locked"=>$varLock))
										    ),
										array("titulo"=>"Proveedor :", "clase1"=>"FormCampo1", "clase2"=>"interior", "div"=>"divPro",
										     "campo"=>array("tipo"=>"select", "id"=>"pro", "name"=>"pro", "valor"=>$Fila["id_proveedor"], 
														    "combo"=>array("sql"=>$sqlpro, "sincod"=>"SI", "titulo"=>"--[[SELECCIONAR]]--", 
																		  "ancho"=>"250", "locked"=>$varLock,
																		  "reload"=>"xajax_Combo_Producto_Tipo_Prov(this.value)"))
										    ),
									    array("titulo"=>"Tipo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "div"=>"divTip",
										     "campo"=>array("tipo"=>"select", "id"=>"tip", "name"=>"tip", "valor"=>$Fila["id_tipo"], 
														    "combo"=>array("sql"=>$sqltip, "sincod"=>"SI", "titulo"=>"--[[SELECCIONAR]]--", 
																		  "ancho"=>"250", "locked"=>$varLock,
																		  "reload"=>"xajax_Combo_Producto_Familia_Prov(pro,value, this.value)"))
										    ),
										array("titulo"=>"Familia :", "clase1"=>"FormCampo1", "clase2"=>"interior", "div"=>"divFam",
										     "campo"=>array("tipo"=>"select", "id"=>"fam", "name"=>"fam", "valor"=>$Fila["id_familia"], 
														    "combo"=>array("sql"=>$sqlfam, "sincod"=>"SI", "titulo"=>"--[[SELECCIONAR]]--", 
																		  "ancho"=>"250", "locked"=>$varLock,
																		  "reload"=>"xajax_Combo_Producto_SubFamilia_Prov(pro.value, this.value)"))
										    ),
										array("titulo"=>"Sub Familia :", "clase1"=>"FormCampo1", "clase2"=>"interior", "div"=>"divSub",
										     "campo"=>array("tipo"=>"select", "id"=>"sub", "name"=>"sub", "valor"=>$Fila["id_sub_familia"], 
														    "combo"=>array("sql"=>$sqlsub, "sincod"=>"SI", "titulo"=>"--[[SELECCIONAR]]--", 
																		  "ancho"=>"250", "locked"=>$varLock))
										    ),
										array("titulo"=>"Cuota Monto :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"texto", "id"=>"mon", "name"=>"mon", "valor"=>$Fila["monto"], "ancho"=>"100",
															 "keypress"=>"return SoloNumeros(event)", "alinear"=>"center")),
									    array("titulo"=>"Cuota Cantidad :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"texto", "id"=>"can", "name"=>"can", "valor"=>$Fila["cantidad"], "ancho"=>"100",
															 "keypress"=>"return SoloNumeros(event)", "alinear"=>"center")),					   
									   ),
						"boton"=>array(array("tipo"=>"button", "id"=>"bsav", "name"=>"bsav", "ancho"=>"100",
											 "clase"=>"boton_guardar", "texto"=>"Grabar", "click"=>"javascrip:xGrabarJS()"),			  
									   array("tipo"=>"button", "id"=>"breg", "name"=>"breg", "ancho"=>"100",
											 "clase"=>"boton_cancelar", "texto"=>"Cancelar", "click"=>"javascrip:xajax_xRegresar()"),
									)
						);
		return $this->setTablaForm($varTabla);	
		//return $sql;
		}
		
	function ProcesoGrabar($varForm = NULL) {		
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varOpt = "'".$varForm["opt"]."'";
		$varAni = ($varForm["opt"]=="NEW")?"'".$varForm["anio"]."'":"'".$varForm["anio1"]."'";
		$varMes = ($varForm["opt"]=="NEW")?"'".$varForm["mes"]."'":"'".$varForm["mes1"]."'";
		$varPro = ($varForm["opt"]=="NEW")?$varForm["pro"]:$varForm["pro1"];
		$varVen = ($varForm["opt"]=="NEW")?$varForm["ven"]:$varForm["ven1"];
		$varTip = ($varForm["opt"]=="NEW")?"'".$varForm["tip"]."'":"'".$varForm["tip1"]."'";
		$varFam = ($varForm["opt"]=="NEW")?"'".$varForm["fam"]."'":"'".$varForm["fam1"]."'";
		$varSub = ($varForm["opt"]=="NEW")?"'".$varForm["sub"]."'":"'".$varForm["sub1"]."'";
		$varMon = ($varForm["mon"]=="")?0:$varForm["mon"];
		$varCan = ($varForm["can"]=="")?0:$varForm["can"];
		$sql = "call spMante_Cuota_Venta_Familia($varOpt, $varAni, $varMes, $varVen, $varPro, $varTip, $varFam, $varSub, $varMon, $varCan, $varUsr);";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$Fila = $this->FetchArray($rs);
		return $Fila[0];
		}
	function ProcesoEliminar($varAni, $varMes, $varVen, $varPro, $varTip, $varFam, $varSub) {
		$sql = "delete from _sv_cuota_venta_familia 
				where id_anio		= '$varAni'
				and id_mes			= '".substr($varMes,0,2)."'
				and id_proveedor	= $varPro
				and id_vendedor		= $varVen
				and id_tipo			= '$varTip'
				and id_familia		= '$varFam'
				and id_sub_familia	= '$varSub'
				;";
		$this->AbrirConexion();
		$this->Query($sql);
		$this->CerrarConexion();
		return "REGISTRO ELIMINADO CORRECTAMENTE";
		}
	}
	
?>