<?php
//include "clase.conexion.php";
class clsUnidadMedida extends clsConexion{
	public $varTitulo = "UNIDADES DE MEDIDA";
	public $varMyTabla  = "_sv_unid_medida";
	public $varMyID  	  = "id_um";
	function FormuBusqueda() {
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"500", "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", 
											 "alinear"=>"center","valor"=>$_POST["cod"], "ancho"=>"70")),
									   array("titulo"=>"Descripci&oacute;n :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											  "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam",
											  "valor"=>$_POST["nam"],  "ancho"=>"355"))
									 ),
					  "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
					   ) {
		$varCod = (isset($varForm["cod"]) && $varForm["cod"]<>'')? "'".$varForm["cod"]."'":"NULL";
		$varNom = $varForm["nam"];
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		
		$sql = "call spQuery_Unidad_Medida($varCod,'$varNom', $varFie, $varOrd)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"800", "sql_cab"=>$sql, "pages"=>50,
						"conte"=>array(array("titulo"=>"CLIS",	   	 "ancho"=>"40", "alinear"=>"center"), 									   
									   array("titulo"=>"DESCRIPCION","ancho"=>"300", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"CANT.",    "ancho"=>"40", "alinear"=>"center"), 
									   array("titulo"=>"FCREADO",  "ancho"=>"100", "fijo"=>"SI"),
									   array("titulo"=>"FUPDATE",  "ancho"=>"100", "fijo"=>"SI"),
									   array("titulo"=>"UCREADO",  "ancho"=>"100", "fijo"=>"SI", "alinear"=>"center"),
									   array("titulo"=>"UUPDATE",  "ancho"=>"100", "fijo"=>"SI", "alinear"=>"center")
									   ),
						"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"=>1, "tipo"=>"INDEX"),
																 array("valor"=>"</b>")
																),
											 "click"=>array("funcion"=>"FormuRegistroJS",
															"param"=>array(array("valor"=>"EDIT", "tipo"=>"TEXTO"),
																		   array("valor"=>0, 	  "tipo"=>"INDEX")
																		  )
															)
											 
											),
									   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"=>1, "tipo"=>"INDEX"),
																 array("valor"=>"</b>")
																),
											 "click"=>array("funcion"=>"xEliminarJS",
															"param"=>array(array("valor"=>0, "tipo"=>"INDEX"),
																		   array("valor"=>1, "tipo"=>"INDEX"),
																		   array("valor"=>"this", "tipo"=>"NUM"),
																		   )
															)
											 ),
									   array("tipo"=>"img", "id"=>"bbus", "name"=>"bbus", "img"=>"../images/16x16/user_go.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>ASIGNAR FACTORES DE CONVERSION</b><br> A LA UNIDAD DE MEDIDA : <b>"),
																 array("valor"=>0, "tipo"=>"INDEX"),
																 array("valor"=>" - "),
																 array("valor"=>1, "tipo"=>"INDEX"),
																 array("valor"=>"</b>")
																),
											 "click"=>array("funcion"=>"FormuPermisoJS",
															"param"=>array(array("valor"=>0, 	  "tipo"=>"INDEX"))
															)
											 
											)
									)
						);
		return $this->SetGridTabla2($varTabla);
		//return $sql;
		}
	
	function GridDatosDetalle($varCod  = NULL
							  ) 
		{
		
		$sql = "call spQuery_Factor_Conversion('$varCod')";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"800", "sql_cab"=>$sql, "pages"=>50,
						"conte"=>array(array("titulo"=>"CORI",	   		"ancho"=>"40", "alinear"=>"center"), 									   
									   array("titulo"=>"UNID. ORIGEN", 	"ancho"=>"250", "fijo"=>"SI"),
									   array("titulo"=>"OPERADOR",      "ancho"=>"40", "alinear"=>"center"), 
									   array("titulo"=>"FACTOR",       	"ancho"=>"40", "alinear"=>"right"), 
									   array("titulo"=>"CDES",	   		"ancho"=>"40", "alinear"=>"center"), 									   
									   array("titulo"=>"UNI. DESTINO", 	"ancho"=>"250", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"FCREADO",  		"ancho"=>"100", "fijo"=>"SI"),
									   array("titulo"=>"FUPDATE",  		"ancho"=>"100", "fijo"=>"SI", "oculto"=>"SI"),
									   array("titulo"=>"UCREADO",  		"ancho"=>"100", "fijo"=>"SI", "alinear"=>"center"),
									   array("titulo"=>"UUPDATE",  		"ancho"=>"100", "fijo"=>"SI", "oculto"=>"SI")
									   ),
						"boton"=>array(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"=>1, "tipo"=>"INDEX"),
																 array("valor"=>"</b>")
																),
											 "click"=>array("funcion"=>"xEliminarDetalleJS",
															"param"=>array(array("valor"=>4, "tipo"=>"INDEX"),
																		   array("valor"=>5, "tipo"=>"INDEX"),
																		   array("valor"=>$varCod, "tipo"=>"TEXT"),
																		   array("valor"=>"this", "tipo"=>"NUM")
																		   )
															)
											 )
									)
						);
		
		return $this->SetGridTabla($varTabla);
		}
	
	function FormuRegistro($varOpt=NULL, $varCod=NULL) {
		if($varOpt=="NEW") {
			$SubTit = $this->varTitulo." - NUEVO";
			$Permite = $_SESSION["_FINS"];
			$varLock = "NO";
			$varClas = "inputNormal";
			$Fila = NULL;			
			}
		else {
			$SubTit =  $this->varTitulo." - EDITAR";
			$varLock = "SI";
			$varClas = "inputDisable";
			$sql = "select * from ".$this->varMyTabla." where ".$this->varMyID." = '$varCod'";
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$Fila = $this->FetchArray($rs);
			}
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "clase"=>"FormTitulo", "titulo"=>$SubTit,
						"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"=>$varOpt)
									  ),
						"conte"=>array(array("titulo"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"100",
										 "campo" =>array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "clase"=>$varClas,
										 "valor"=>$Fila[$this->varMyID], "alinear"=>"center", "locked"=>$varLock, "ancho"=>"70")),								   
								   array("titulo"=>"Nombre :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", "valor"=>$Fila["nombre"], "ancho"=>"300"))
								   ),
						"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);	
		}
		
	function FormuPermiso($varCod=NULL) {
		$sql = "call spQuery_Unidad_Medida('$varCod',NULL,NULL,NULL)";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$Fila = $this->FetchArray($rs);

		$varReturn = "<form id=\"form\" onsubmit=\"return false;\" method=\"POST\" name=\"form\">";
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "clase"=>"FormTitulo", "titulo"=>$this->varTitulo." - FACTORES DE CONVERSION",					
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo." - ".$Fila["CUM"]),
									   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt",   "valor"=>"DET")
									  ),
						"conte"=>array(array("titulo"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"80",
											 "campo" =>array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "clase"=>"inputDisable",
											 "valor"=>$Fila["CUM"], "alinear"=>"center", "locked"=>"SI", "ancho"=>"70")),
									   array("titulo"=>"Descripcion :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"simple", "valor"=>$Fila["DESCRIPCION"])),
									   array("titulo"=>"Cant. Conv. :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"simple", "valor"=>$Fila["CANT"])),
									   )
						);		
		$varReturn.= $this->setTablaForm($varTabla);
		
		$varOper= array("*"=>"[*] MULTIPLICAR POR", "/"=>"[/] DIVIDIR ENTRE");
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "clase"=>"FormTitulo", "titulo"=>"AGREGAR FACTOR DE CONVERSION",					
						"conte"=>array(array("titulo"=>"Operacion :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										     "campo"=>array("tipo"=>"select_simple", "id"=>"ope", "name"=>"ope", "valor"=>$_POST[ope], "ancho"=>"100",
														    "combo"=>array("sql"=>$varOper, "sincod"=>"SI", "ancho"=>"200"))),									   
									   array("titulo"=>"Factor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"fac", "name"=>"fac", "valor"=>$_POST["fac"],
																					 "ancho"=>"60", "alinear"=>"center", "maxchar"=>4, 
																					 "keypress"=>"return SoloEnteros(event)"),
																			   array("tipo"=>"simple", "valor"=>"Ingresar Factor de Conversion")
																			   )
														 	)
											 ),
									   array("titulo"=>"UM Destino :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"80",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"ucod", "name"=>"ucod", "valor"=>$_POST["ucod"],
																					 "ancho"=>"60", "alinear"=>"center",
																					 "blur"=>"xajax_seleccionar_unidad_medida_factor(this.value, '$varCod')", 
																					 "dclick"=>"fn_cargar_listado_unidad_medida_factor('ucod', 'unom','$varCod')"),
																			   array("tipo"=>"text", "id"=>"unom", "name"=>"unom", "valor"=>$_POST["unom"], 
																					 "ancho"=>"260", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 	)
											 ),
									   ),
						"boton"=>array(array("tipo"=>"button", "id"=>"bsav", "name"=>"bsav", "ancho"=>"100",
											 "clase"=>"boton_agregar", "texto"=>"Agregar", "click"=>"javascrip:xGrabarDetalleJS()"),			  
									   array("tipo"=>"button", "id"=>"bexp", "name"=>"bexp", "ancho"=>"100", "texto"=>"Exportar",
									 	   "clase"=>"boton_exportar", "click"=>"javascript:xExportarDetalleJS()"),	
									   array("tipo"=>"button", "id"=>"breg", "name"=>"breg", "ancho"=>"100",
											 "clase"=>"boton_cancelar", "texto"=>"Cancelar", "click"=>"javascrip:xajax_xRegresar()"),
									)
						);
		
		$varReturn.= $this->setTablaForm($varTabla);
		$varReturn.= "</form>";
		
		$varReturn.= "<div id=\"divDatos\">";
		$varReturn.= $this->GridDatosDetalle($varCod);	
		$varReturn.= "</div>";
		
		return $varReturn;
		}
		
	function ProcesoGrabar($varForm = NULL) {
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varCod = ($varForm["cod"]=="")? "NULL" : "'$varForm[cod]'";
		$varNam = ($varForm["nam"]=="")? "NULL" : "'$varForm[nam]'";
		$varPes = ($varForm["pes"]=="")? "NULL" : $varForm[pes];
		
		$sql = "call spMante_Unidad_Medida('$varForm[opt]', $varCod, $varNam, $varUsr);";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$Fila = $this->FetchArray($rs);
		return $Fila[0];
		}
	
	function ProcesoGrabarDetalle($varForm = NULL) {
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;		
		$sql = "call spMante_Factor_Conversion('NEW', '$varForm[cod]', '$varForm[ucod]',  '$varForm[ope]', $varForm[fac], $varUsr);";	
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$Fila = $this->FetchArray($rs);
		return $Fila[0];
		}	
		
	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();
		}
	function ProcesoEliminarDetalle($varCod, $varPad) {
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$sql = "delete from _sv_factor_conv where id_um_origen = '$varCod' and id_um_destino='$varPad';";
		$sql = "delete from _sv_factor_conv where id_um_destino = '$varCod' and id_um_origen='$varPad';";
		$this->AbrirConexion();
		$this->Query($sql);
		$this->CerrarConexion();
		}
	}
	
?>