<?php
//include "clase.conexion.php";
class clsCliente extends clsConexion{
	public $varTitulo 	= "MAESTRO DE CLIENTES";
	public $varMyTabla  = "_sv_cliente";
	public $varMyID  	= "id_cliente";
	function FormuBusqueda() {
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "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"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"100",
										 "campo" =>array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "valor"=>$_POST["cod"], 
										 "alinear"=>"center", "ancho"=>"70")),								   
								   array("titulo"=>"Nombre :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", "valor"=>$_POST["nam"], "ancho"=>"306")),
								   array("titulo"=>"Direccion :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo" =>array("tipo"=>"texto", "id"=>"dire", "name"=>"dire", "valor"=>$_POST["dire"], "ancho"=>"306")),
								   array("titulo"=>"Ruta :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										 "campo"=>array("tipo"=>"varios",
														"contenido"=>array(array("tipo"=>"text", "id"=>"rcod", "name"=>"rcod", "valor"=>$_POST["rcod"], 
																				 "ancho"=>"50", "alinear"=>"center", 
																				 "blur"=>"xajax_seleccionar_ruta(this.value)", 
																				 "dclick"=>"fn_cargar_listado_ruta('rcod', 'rnom')"),
																		   array("tipo"=>"text", "id"=>"rnom", "name"=>"rnom", "valor"=>$_POST["rnom"], 
																			   	 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																		   )
														)
										 ),
								   array("titulo"=>"Modulo :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										 "campo"=>array("tipo"=>"varios",
														"contenido"=>array(array("tipo"=>"text", "id"=>"mcod", "name"=>"mcod", "valor"=>$_POST["mcod"], 
																				 "ancho"=>"50", "alinear"=>"center", 
																				 "blur"=>"xajax_seleccionar_modulo(rcod.value, this.value)", 
																				 "dclick"=>"fn_cargar_listado_modulo(rcod.value, 'mcod', 'mnom')"),
																		   array("tipo"=>"text", "id"=>"mnom", "name"=>"mnom", "valor"=>$_POST["mnom"], 
																			   	 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																		   )
														)
										 )
								   ),
					  "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"=>"bimp", "name"=>"bimp", "ancho"=>"100", "texto"=>"Imprimir", 
										   "clase"=>"boton_imprimir", "click"=>"javascript:xImprimirJS()"),
									 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";
		$sql = "call spQuery_Cliente('$varForm[cod]', '$varForm[nam]', '$varForm[dire]', '$varForm[rcod]', '$varForm[mcod]', $varFie, $varOrd)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"1000", "sql_cab"=>utf8_decode($sql),	"pages"=>50,
						"conte"=>array(array("titulo"=>"CCLI",   	"ancho"=>"20" , "alinear"=>"center", "clase"=>"sortable-numeric", "tipo"=>"texto"),
									   array("titulo"=>"SECC",   	"ancho"=>"30" , "alinear"=>"center", "clase"=>"sortable-numeric", "tipo"=>"texto"), 
									   array("titulo"=>"CLIENTE",	"ancho"=>"300", "clase"  =>"sortable", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"SEXO",     	"ancho"=>"20", "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"ESTCIVIL",  "ancho"=>"20", "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"FNAC",  	"ancho"=>"20", "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"DNI", 	 	"ancho"=>"60" , "alinear"=>"center", "clase"=>"sortable-numeric"),
									   array("titulo"=>"RUC", 	 	"ancho"=>"60" , "alinear"=>"center", "clase"=>"sortable-numeric"),
									   array("titulo"=>"DIRECCION", "ancho"=>"200", "fijo"=>"SI"),
									   array("titulo"=>"REFERENCIA","ancho"=>"200", "oculto"=>"SI"),
									   array("titulo"=>"CRUT",     	"ancho"=>"20", "alinear"=>"center"), 
									   array("titulo"=>"RUTA",		"ancho"=>"100", "clase"  =>"sortable", "fijo"=>"SI"),
									   array("titulo"=>"CMOD",     	"ancho"=>"20", "alinear"=>"center"), 
									   array("titulo"=>"MODULO",	"ancho"=>"100", "clase"  =>"sortable"),
									   array("titulo"=>"CUBI",     	"ancho"=>"20" , "oculto"=>"SI"),
									   array("titulo"=>"DEPARTAMENTO", "ancho"=>"100", "clase"=>"sortable", "oculto"=>"SI"),
									   array("titulo"=>"PROVINCIA", "ancho"=>"100", "clase"=>"sortable", "oculto"=>"SI"),
									   array("titulo"=>"DISTRITO", 	"ancho"=>"100", "clase"=>"sortable", "oculto"=>"SI"),
									   array("titulo"=>"TELEF1",  	"ancho"=>"60",  "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"TELEF2",  	"ancho"=>"60",  "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"CELU.",  	"ancho"=>"60",  "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"RPM",    	"ancho"=>"60",  "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"RPC",    	"ancho"=>"60",  "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"NEXTEL", 	"ancho"=>"60",  "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"ULTCOMP",  	"ancho"=>"100", "oculto"=>"SI"),
									   array("titulo"=>"NOCOMP",  	"ancho"=>"100"),
									   array("titulo"=>"ESTADO",  	"ancho"=>"100"),
									   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/16x16/table_edit.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>EDITAR</b> EL REGISTRO<br><b>"),
																 array("valor"=>0, "tipo"=>"INDEX"),
																 array("valor"=>" - "),
																 array("valor"=>2, "tipo"=>"INDEX"),
																 array("valor"=>"</b>")
																),
											 "click"=>array("funcion"=>"FormuRegistroJS",
															"param"=>array(array("valor"=>0, 	  "tipo"=>"INDEX"),
																		   array("valor"=>"EDIT", "tipo"=>"TEXTO")
																		  )
															)
											 
											),
									   array("tipo"=>"img", "id"=>"badd", "name"=>"badd", "img"=>"../images/16x16/table_delete.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>ELIMINAR</b> EL REGISTRO<br><b>"),
																 array("valor"=>0, "tipo"=>"INDEX"),
																 array("valor"=>" - "),
																 array("valor"=>2, "tipo"=>"INDEX"),
																 array("valor"=>"</b>")
																),
											 "click"=>array("funcion"=>"xEliminarJS",
															"param"=>array(array("valor"=>0, "tipo"=>"INDEX"),
																		   array("valor"=>2, "tipo"=>"INDEX"),
																		   array("valor"=>"this", "tipo"=>"NUM"),
																		   )
															)
											 )
									)
						);
		return $this->SetGridTabla2($varTabla);
		}
	function FormuRegistro($varCod=NULL,$varOpt=NULL) {
		if($varOpt=="NEW") {
			$SubTit = $this->varTitulo." - NUEVO";
			$Permite = $_SESSION["_FINS"];
			$varLock = "NO";
			$Fila = NULL;
			$Fila["id_tipo_cliente"] = "1";
			$Fila["id_segmento"] = "1";
			$Fila["id_ruta"] = "GEN";
			$Fila["id_pais"] = "01";
			$Fila["CDEP"] = "14";
			$Fila["CPRO"] = "1401";
			$Fila["CDIS"] = "140101";
			}
		else {
			$SubTit =  $this->varTitulo." - EDITAR";
			$varLock = "SI";
			$sql = "select C.id_cliente, C.id_pais, 
					substring(id_ubigeo,1,P.dig_dep) as CDEP, substring(id_ubigeo,1,P.dig_dep+P.dig_pro) as CPRO, id_ubigeo as CDIS,
					C.id_ruta, C.id_modulo, C.id_tipo_cliente, C.id_segmento, C.nombre, C.sexo, C.estado_civil, 
					date_format(C.fecha_nacimiento,'%d/%m/%Y') as fecha_nacimiento, 
					C.dni, C.ruc, C.direccion, C.referencia, C.telefono1, C.telefono2, C.celular, C.rpm, C.rpc, C.nextel, 
					C.secuencia, C.email, C.estado
					from ".$this->varMyTabla." C
					left join _sv_pais P ON C.id_pais = P.id_pais 
					where ".$this->varMyID." = $varCod";	
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$Fila = $this->FetchArray($rs);
			}
		$sqlpai = "call spQuery_Lista_Ubigeo_Pai();";
		$sqldep = "call spQuery_Lista_Ubigeo_Dep('$Fila[id_pais]');";
		$sqlpro = "call spQuery_Lista_Ubigeo_Pro('$Fila[id_pais]', '$Fila[CDEP]');";
		$sqldis = "call spQuery_Lista_Ubigeo_Dis('$Fila[id_pais]', '$Fila[CPRO]');";
		$sqlrut = "call spQuery_Combo_Ruta();";
		$sqltip = "call spQuery_Combo_Tipo_Cliente();";
		$sqlmod = "call spQuery_Combo_Modulo('$Fila[id_ruta]');";
		$sqlseg = "call spQuery_Lista_Segmento(NULL,NULL);";
		$sqlsex	= array("M"=>"MASCULINO", "F"=>"FEMENINO");
		$sqlesc	= array("S"=>"SOLTERO", "C"=>"CASADO", "V"=>"VIUDO", "D"=>"DIVORCIADO", "Z"=>"CONVIVIENTE");
		$sqlest	= array("A"=>"ACTIVO", "I"=>"INACTIVO");
		$varFormu=array("id"=>"form", "name"=>"form", "metodo"=>"POST", "onsubmit"=>"return validaFormAccion(this,'N')");
		$varTabla=array("id"=>"table", "name"=>"table", "tamano"=>"570", "clase"=>"FormTitulo", "titulo"=>$SubTit,						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$SubTit),
									   array("tipo"=>"oculto", "id"=>"opt" , "name"=>"opt" , "valor"=>$varOpt)
									  ),
						"conte"=>array(array("titulo"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"80",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "clase"=>"inputDisable",
											 										 "valor"=>$Fila[$this->varMyID], "alinear"=>"center", 
																					 "locked"=>"SI", "ancho"=>"80")))),
									   array("titulo"=>"Nombre :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", 
																					 "valor"=>$Fila["nombre"], "ancho"=>"277"),
																			   array("tipo"=>"simple", "valor"=>"Estado : ", 
																			   		 "clase_in"=>"FormCampo1", "ancho"=>"60"),
																			   array("tipo"=>"select_simple", "id"=>"est", "name"=>"est", "valor"=>$Fila["estado"],
														    						 "combo"=>array("sql"=>$sqlest, "ancho"=>"100"))
																					 ))),
									   array("titulo"=>"Nro. DNI :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"dni", "name"=>"dni", "valor"=>$Fila["dni"],
																					 "ancho"=>"100", "keypress"=>"return SoloEnteros(event)", "maxchar"=>8),
																			   array("tipo"=>"simple", "valor"=>"Nro. RUC : ", "clase_in"=>"FormCampo1", 
																			   		 "ancho"=>"60"),
																			   array("tipo"=>"text", "id"=>"ruc", "name"=>"ruc", "valor"=>$Fila["ruc"],
																					 "ancho"=>"100", "maxchar"=>11),
																			   array("tipo"=>"simple", "valor"=>"Fec. Nac. : ", "clase_in"=>"FormCampo1", 
																			   		 "ancho"=>"60"),
																			   array("tipo"=>"texto", "id"=>"fnac", "name"=>"fnac", "alinear"=>"center",
																					 "ancho"=>"100", "valor"=>$Fila["fecha_nacimiento"]),
																			   )
														 	)
											 ),
									   array("titulo"=>"Direccion :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"dir", "name"=>"dir", 
																					 "valor"=>$Fila["direccion"], "ancho"=>"453")))),
									   array("titulo"=>"Referencia :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"area", "id"=>"ref", "name"=>"ref",
																					 "valor"=>$Fila["referencia"], "ancho"=>"453", "alto"=>"40")))),
									   array("titulo"=>"Sexo :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"select_simple", "id"=>"sex", "name"=>"sex", 
																					 "valor"=>$Fila["sexo"],
																					 "combo"=>array("sql"=>$sqlsex,  "sincod"=>"SI", "ancho"=>"180")),
																			   array("tipo"=>"simple", "valor"=>"Est.Civil : ", 
																			   		 "clase_in"=>"FormCampo1", "ancho"=>"77"),
																			   array("tipo"=>"select_simple", "id"=>"civ", "name"=>"civ", 
																			   		 "valor"=>$Fila["estado_civil"], 
																			   		 "combo"=>array("sql"=>$sqlesc, "ancho"=>"180"))
																			   )
														 	)
											 ),
									   array("titulo"=>"Tipo Cliente :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"select", "id"=>"tip", "name"=>"tip", "sql"=>$sqltip, 
																					 "valor"=>$Fila["id_tipo_cliente"], "sincod"=>"SI",
																					 "titulo"=>" --[[SELECCIONAR]]-- ",  "ancho"=>"180"),
																			   array("tipo"=>"simple", "valor"=>"Segmento : ", 
																			   		 "clase_in"=>"FormCampo1", "ancho"=>"77"),
																			   array("tipo"=>"select", "id"=>"seg", "name"=>"seg", "sql"=>$sqlseg, 
																					 "valor"=>$Fila["id_segmento"], "sincod"=>"SI",
																					 "titulo"=>" --[[SELECCIONAR]]-- ",  "ancho"=>"180")
																			   )
														 	)
											 ),
									   array("titulo"=>"Ruta :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"select", "id"=>"rut", "name"=>"rut", "valor"=>$Fila["id_ruta"],
																					 "sql"=>$sqlrut, "titulo"=>" --[[SELECCIONAR]]-- ", "sincod"=>"SI", 
																					 "ancho"=>"180", "reload"=>"xajax_comboModulo(this.value)"),
																			   array("tipo"=>"simple", "valor"=>"Modulo : ", 
																			   		 "clase_in"=>"FormCampo1", "ancho"=>"77"),
																			   array("tipo"=>"select", "id"=>"mod", "name"=>"mod", "sql"=>$sqlmod, 
																			   		 "valor"=>$Fila["id_modulo"], "div"=>"divMod",
																					 "titulo"=>" --[[SELECCIONAR]]-- ", "sincod"=>"SI", "ancho"=>"180")
																			   )
														 	)
											 ),
									   array("titulo"=>"Pais :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"select", "id"=>"pai", "name"=>"pai", "valor"=>$Fila["id_pais"],
																					 "sql"=>$sqlpai, "titulo"=>" --[[SELECCIONAR]]-- ", "ancho"=>"180",
																					 "sincod"=>"SI", "reload"=>"xajax_comboDepa(this.value)"),
																			   array("tipo"=>"simple", "valor"=>"Depart. : ", 
																			   		 "clase_in"=>"FormCampo1", "ancho"=>"77"),
																			   array("tipo"=>"select", "id"=>"dep", "name"=>"dep", "valor"=>$Fila["CDEP"], 
																					 "sql"=>$sqldep, "titulo"=>" --[[SELECCIONAR]]-- ", "ancho"=>"180",
																					 "sincod"=>"SI", "reload"=>"xajax_comboProv(pai.value, this.value)", 
																					 "div"=>"divDepa"),
																			   )
														 	)
											 ),
									   array("titulo"=>"Provincia :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"select", "id"=>"pro", "name"=>"pro", "valor"=>$Fila["CPRO"], 			
																			   		 "sql"=>$sqlpro, "titulo"=>" --[[SELECCIONAR]]-- ", "sincod"=>"SI", 
																					 "ancho"=>"180", "reload"=>"xajax_comboDist(pai.value, this.value)", 
																					 "div"=>"divProv"),
																			   array("tipo"=>"simple", "valor"=>"Distrito : ", 
																			   		 "clase_in"=>"FormCampo1", "ancho"=>"77"),
																			   array("tipo"=>"select", "id"=>"dis", "name"=>"dis", "valor"=>$Fila["CDIS"],
																					 "sql"=>$sqldis, "titulo"=>" --[[SELECCIONAR]]-- ", "sincod"=>"SI", 
																					 "ancho"=>"180", "div"=>"divDist")
																			   
																			   )
														 	)
											 ),
									   array("titulo"=>"Telef. 1 :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"te1", "name"=>"te1", "valor"=>$Fila["telefono1"],
																					 "ancho"=>"100", "keypress"=>"return SoloEnteros(event)"),
																			   array("tipo"=>"simple", "valor"=>"Telef. 2 : ", "clase_in"=>"FormCampo1", 
																			   		 "ancho"=>"60"),
																			   array("tipo"=>"text", "id"=>"te2", "name"=>"te2", "valor"=>$Fila["telefono2"],
																					 "ancho"=>"100", "keypress"=>"return SoloEnteros(event)"),
																			   array("tipo"=>"simple", "valor"=>"Celular : ", "clase_in"=>"FormCampo1", 
																			   		 "ancho"=>"60"),
																			   array("tipo"=>"text", "id"=>"cel", "name"=>"cel", "valor"=>$Fila["celular"],
																					 "ancho"=>"100", "keypress"=>"return SoloEnteros(event)"),
																			   )
														 	)
											 ),
									   array("titulo"=>"# RPM :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"rpm", "name"=>"rpm", "valor"=>$Fila["rpm"],
																					 "ancho"=>"100"),
																			   array("tipo"=>"simple", "valor"=>"# RPC : ", "clase_in"=>"FormCampo1", 
																			   		 "ancho"=>"60"),
																			   array("tipo"=>"text", "id"=>"rpc", "name"=>"rpc", "valor"=>$Fila["rpc"],
																					 "ancho"=>"100"),
																			   array("tipo"=>"simple", "valor"=>"# Nextel : ", "clase_in"=>"FormCampo1", 
																			   		 "ancho"=>"60"),
																			   array("tipo"=>"text", "id"=>"nex", "name"=>"nex", "valor"=>$Fila["nextel"],
																					 "ancho"=>"100")
																			   )
														 	)
											 ),
									   array("titulo"=>"Secuencia :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"sec", "name"=>"sec", "valor"=>$Fila["secuencia"],
																					 "ancho"=>"100", "keypress"=>"return SoloEnteros(event)"),
																			   array("tipo"=>"simple", "valor"=>"Email: ", "clase_in"=>"FormCampo1",
																			   		 "ancho"=>"60"),
																			   array("tipo"=>"texto", "id"=>"ema", "name"=>"ema", "ancho"=>"277",
																			   		 "valor"=>$Fila["email"])
																			   )
														 	)
											 )
									   ),
						"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()")
									  )
						);
		$varCli = (isset($Fila["id_cliente"]) && $Fila["id_cliente"]<>"")?$Fila["id_cliente"]:"0";
		$sql1 = "select id_empresa, nombre, 
				 (select id_lista from _sv_cliente_lista L where L.id_empresa=E.id_empresa and L.id_cliente=$varCli) as id_lista 
				 from _sv_empresa E
				 where id_empresa in (select distinct id_empresa from _sys_usuario_almacen where id_usuario=$_SESSION[_CUSE]);";
		$this->AbrirConexion();
		$rs1 = $this->Query($sql1);
		$this->CerrarConexion();
		$varExiste = $this->NumFilas($rs1);
		$sqlrol = "select id_lista, nombre from _sv_lista where _estado='A' and id_empresa = varSis;";
		$varTab_1=array("id"=>"tab1", "name"=>"tab1", "tamano"=>"600", "clase"=>"FormTitulo", "titulo"=>"ALMACENES x EMPRESA ASIGNADOS", "sql"=>$sql1,
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"num1", "name"=>"num1", "valor"=>$varNum1)),
						"conte"=>array(array("titulo"=>"CEMP", "clase"=>"FormTitulo", "tamano"=>"40", "alinear"=>"center", 
											 "campo"=>array("tipo"=>"texto", "id"=>"emp", "name"=>"emp", "alinear"=>"center", "ancho"=>"40",
											 				"locked"=>"SI", "clase"=>"inputDisable")
											),
									   array("titulo"=>"EMPRESA", "clase"=>"FormTitulo", "tamano"=>"300", 
											 "campo"=>array("tipo"=>"simple", "alinear"=>"center")
											),
									   array("titulo"=>"LISTA", "clase"=>"FormTitulo", "tamano"=>"250", 									   		 
											 "campo"=>array("tipo"=>"select", "id"=>"lis", "name"=>"lis", "alinear"=>"center", "valor"=>2,					 				
											 				"sql"=>$sqlrol, "titulo"=>"--[[SELECCIONAR]]--", "ancho"=>"250",
															"var"  =>array("varSis"=>0))
											)							   
									   )
						);
										
		$varTabs =array(array("name"=>"Lista de Precios por Empresa",
							  "tipo"=>"det",  "alto"=>"300", "ancho"=>"620", "tabla"=>$varTab_1
							  )
						);
		return $this->setTablaFormTabs($varFormu, $varTabla, $varTabs);
		}
		
	function Imprimir($varRut = NULL,
					  $varMod = NULL
					  )  {		
		$varRut = ($varRut<>"")?"'".$varRut."'":"NULL";
		$varMod = ($varMod<>"")?"'".$varMod."'":"NULL";
		$sql = "call spQuery_Cliente(NULL, NULL, NULL, $varRut, $varMod, NULL, NULL)";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$varNumCol = $this->NumField($rs);
		$varNumCom = 
		$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%;" cellspacing="0" cellpadding="0">';
			$html.='<tr class="titu_sup" height="25px">';
			$html.='<td colspan="2"><b>PADRON DE CLIENTES</b></td>';
			$html.='</tr>';
			$html.='<tr class="titu_inf">';
			$html.='<td colspan="2">';
			$html.='<div align="left" style=" width:50%;float:left;">'.$_SESSION["_CABR"].' : '.$_SESSION["_NSYS"].'</div>';
    		$html.='<div align="right" style="width:50%;float:left">'.MostrarFechaActual().'</div>';
			$html.='</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="15">RUT</td>';
			$html.='<td width="15">SEC</td>';
			$html.='<td width="30">CCLI</td>';
			$html.='<td width="150">CLIENTE</td>';
			$html.='<td width="150">DIRECCION</td>';
			$html.='<td width="50" align="center">DNI/RUC</td>';
			$html.='<td width="50" align="center">ULT.COMP.</td>';
			$html.='<td width="50" align="center">D.NC</td>';
			$html.='</tr>';
			$html.='</thead>';
			
			$varRut_Ant = $varRut_Act = "";
			$varMod_Ant = $varMod_Act = "";
			while($fila = $this->FetchArray($rs))	{
				$varRut_Act = $fila["CRUT"];
				$varMod_Act = $fila["CMOD"];
				$varTotal1 = $varTotal1 + $fila["CANTIDAD"];
				$varTotal2 = $varTotal2 + $fila["MONTO"];
				$varNumDoc++;
				if($varRut_Act <> $varRut_Ant) {
					$varRut_Ant = $fila["CRUT"];
					$varMod_Ant = $fila["CMOD"];
					$html.='<tr class="titu_tabla">';
					$html.='<td>'.$fila["CRUT"].'</td>';
					$html.='<td colspan="7">'.$fila["RUTA"].'</td>';
					$html.='</tr>';
					$html.='<tr class="titu_tabla">';
					$html.='<td></td>';
					$html.='<td>'.$fila["CMOD"].'</td>';
					$html.='<td colspan="7">'.$fila["MODULO"].'</td>';
					$html.='</tr>';
					}
				if($varMod_Act <> $varMod_Ant) {
					$varMod_Ant = $fila["CMOD"];					
					$html.='<tr class="titu_tabla">';
					$html.='<td></td>';
					$html.='<td>'.$fila["CMOD"].'</td>';
					$html.='<td colspan="7">'.$fila["MODULO"].'</td>';
					$html.='</tr>';
					}
				$html.='<tr class="cont_tabla">';
				$html.='<td></td>';
				$html.='<td NOWRAP  align="center">'.$fila["SECUENCIA"].'</td>';
				$html.='<td NOWRAP>'.$fila["CCLI"].'</td>';
				$html.='<td NOWRAP>'.$fila["CLIENTE"].'</td>';
				$html.='<td NOWRAP>'.substr($fila["DIRECCION"],0,50).'</td>';
				$html.='<td align="center" NOWRAP>';
				$html.=(!is_null($fila["RUC"]))?"RUC/".$fila["RUC"]:"DNI/".$fila["DNI"];
				$html.='</td>';
				$html.='<td align="center" NOWRAP>'.$fila["ULTIMA_COMPRA"].'</td>';
				$html.='<td align="center" NOWRAP>'.$fila["DIAS_NO_COMPRA"].'</td>';
				$html.='</tr>';
				}
			$html.='<tr class="titu_tabla">';
			$html.='<td></td>';
			$html.='<td></td>';
			$html.='<td colspan="6">';
			$html.=number_format($varNumDoc,"0",".",",").' Clientes';
			$html.='</td>';	
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>		
				</body>
				</html>';
		return $html;
		}
	
	function ProcesoGrabar($varForm = NULL) {		
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varCod = ($varForm["cod"]=="")? "NULL" : "$varForm[cod]";
		$varNam = ($varForm["nam"]=="")? "NULL" : "'$varForm[nam]'";
		$varDni = ($varForm["dni"]=="")? "NULL" : "'$varForm[dni]'";
		$varRuc = ($varForm["ruc"]=="")? "NULL" : "'$varForm[ruc]'";	
		$varDir = ($varForm["dir"]=="")? "NULL" : "'$varForm[dir]'";
		$varSex = ($varForm["sex"]=="Ninguno")? "NULL" : "'$varForm[sex]'";
		$varCiv = ($varForm["civ"]=="Ninguno")? "NULL" : "'$varForm[civ]'";
		$varNac = ($varForm["nac"]=="")? "NULL" : "'".FechaMysql($varForm[nac])."'";
		$varTip = ($varForm["tip"]=="Ninguno")? "NULL" : $varForm[tip];
		$varSeg = ($varForm["seg"]=="Ninguno")? "NULL" : $varForm[seg];
		$varRut = ($varForm["rut"]=="Ninguno")? "NULL" : "'$varForm[rut]'";
		$varMod = ($varForm["mod"]=="Ninguno")? "NULL" : "'$varForm[mod]'";	
		$varPai = ($varForm["pai"]=="Ninguno")? "NULL" : "'$varForm[pai]'";	
		$varDis = ($varForm["dis"]=="Ninguno")? "NULL" : "'$varForm[dis]'";	
		$varTe1 = ($varForm["te1"]=="")? "NULL" : "'$varForm[te1]'";
		$varTe2 = ($varForm["te2"]=="")? "NULL" : "'$varForm[te2]'";
		$varCel = ($varForm["cel"]=="")? "NULL" : "'$varForm[cel]'";
		$varRpm = ($varForm["rpm"]=="")? "NULL" : "'$varForm[rpm]'";
		$varRpc = ($varForm["rpc"]=="")? "NULL" : "'$varForm[rpc]'";
		$varNex = ($varForm["nex"]=="")? "NULL" : "'$varForm[nex]'";
		$varSec = ($varForm["sec"]=="")? "NULL" : "$varForm[sec]";
		$varRef = ($varForm["ref"]=="")? "NULL" : "'$varForm[ref]'";
		$varEma = ($varForm["ema"]=="")? "NULL" : "'$varForm[ema]'";
		$varEst = ($varForm["est"]=="")? "NULL" : "'$varForm[est]'";
		$sql = "call spMante_Cliente('$varForm[opt]', $varCod, $varNam, $varSex, $varCiv, $varNac, $varDni, $varRuc, $varDir, $varRef, $varTip,  $varSeg,
									 $varRut, $varMod, $varPai, $varDis, $varTe1, $varTe2, $varCel, $varRpm, $varRpc, $varNex, $varSec, $varEma, $varEst, 
									 $varUsr);";
		$sqlret= $sql;						 
		$this->AbrirConexion();
		$rs = $this->Query(utf8_decode($sql));
		$this->CerrarConexion();
		$Fila = $this->FetchArray($rs);
		$varErr = $Fila[1];
		//Insertamos Lista de Precios por Empresa
		$varCli = $Fila[0];
		$sql = "DELETE FROM _sv_cliente_lista WHERE id_cliente = $varCli;";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();	
		$sqldet = "";
		$sqlcab = "insert into _sv_cliente_lista(id_empresa, id_lista, id_cliente, _date_create, _date_update, _user_create, _user_update) values ";
		foreach ($varForm["lis"] as $varIx =>$varLis) {
			$varEmp = $varForm["emp"][$varIx];
			if ($varLis<>"Ninguno") {
				$sqldet.= ($sqldet=="")?" ":",";
				$sqldet.= "($varEmp, $varLis, $varCli, NOW(), NOW(), $varUsr, $varUsr)";
				}
			}
		if ($sqldet<>"") {
			$sqldet.=";";
			$this->AbrirConexion();
			$this->Query($sqlcab.$sqldet);
			$this->CerrarConexion();
			}
		return $varErr;
		}
	function ProcesoEliminar($varCod) {
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$sql = "update ".$this->varMyTabla." set _estado = 'X', _date_update=NOW(), _user_update=$varUsr where ".$this->varMyID."= '".$varCod."';";
		$this->AbrirConexion();
		$this->Query($sql);
		$this->CerrarConexion();
		}
	}
	
?>