<?php
//include "clase.conexion.php";
class clsServicio extends clsConexion{
	public $varTitulo = "MAESTRO DE ACTIVOS";
	public $varTabla  = "_sv_producto";
	public $varId  	  = "id_producto";
	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),
									   array("tipo"=>"oculto", "id"=>"pro", "name"=>"pro", "valor"=>"0")
									  ),
						"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")),
									   array("titulo"=>"Linea :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"varios",
														   "contenido"=>array(array("tipo"=>"text", "id"=>"tcod", "name"=>"tcod", "valor"=>$_POST["tcod"], 
																					"ancho"=>"50",  "alinear"=>"center", 
																					"blur"=>"xajax_seleccionar_linea('S', this.value)", 
																					"dclick"=>"fn_cargar_listado_producto_linea('S', 'tcod', 'tnom')"),
																			  array("tipo"=>"text", "id"=>"tnom", "name"=>"tnom", "valor"=>$_POST["tnom"], 
																					"ancho"=>"300", "clase"=>"inputDisable", "locked"=>"SI")
																			  )
														   )
										   ),
									   array("titulo"=>"Familia :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"varios",
														   "contenido"=>array(array("tipo"=>"text", "id"=>"fcod", "name"=>"fcod", "valor"=>$_POST["fcod"], 
																					"ancho"=>"50",  "alinear"=>"center", 
																					"blur"=>"xajax_seleccionar_familia(tcod.value,this.value)", 
																					"dclick"=>"fn_cargar_listado_producto_familia(tcod.value, 'fcod', 'fnom')"),
																			  array("tipo"=>"text", "id"=>"fnom", "name"=>"fnom", "valor"=>$_POST["fnom"], 
																					"ancho"=>"300", "clase"=>"inputDisable", "locked"=>"SI")
																			  )
														   )
										   ),
									   array("titulo"=>"Sub Familia :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"varios",
														   "contenido"=>array(array("tipo"=>"text", "id"=>"scod", "name"=>"scod", "valor"=>$_POST["scod"], 
																					"ancho"=>"50",  "alinear"=>"center", 
																					"blur"=>"xajax_seleccionar_subfamilia(tcod.value,fcod.value,this.value)", 
																					"dclick"=>"fn_cargar_listado_producto_subfamilia(tcod.value, fcod.value, 
																																 'scod', 'snom')"),
																			  array("tipo"=>"text", "id"=>"snom", "name"=>"snom", "valor"=>$_POST["snom"], 
																					"ancho"=>"300", "clase"=>"inputDisable", "locked"=>"SI")
																			  )
														   )
										   ),
									   array("titulo"=>"Marca :", "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_marca(this.value)", 
																					 "dclick"=>"fn_cargar_listado_marca('mcod', 'mnom')"),
																			   array("tipo"=>"text", "id"=>"mnom", "name"=>"mnom", "valor"=>$_POST["mnom"], 
																					 "ancho"=>"300", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														   )
											),	
									  	
									 array("titulo"=>"Material :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										   "campo"=>array("tipo"=>"varios",
														  "contenido"=>array(array("tipo"=>"text", "id"=>"xcod", "name"=>"xcod", "valor"=>$_POST["xcod"], 
																					"ancho"=>"50", "alinear"=>"center", 
																					"blur"=>"xajax_seleccionar_material(this.value)", 
																					"dclick"=>"fn_cargar_listado_material('xcod', 'xnom')"),
																			 array("tipo"=>"text", "id"=>"xnom", "name"=>"xnom", "valor"=>$_POST["xnom"], 
																				   "ancho"=>"300", "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"=>"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 =  $varForm["cod"];
		$varNom =  $varForm["nam"];
		$varPro =  $varForm["pro"];
		$varMar =  $varForm["mcod"];
		
		$varMat =  $varForm["xcod"];
		$varTip = (isset($varForm["tcod"]) && $varForm["tcod"]<>"")?"'".$varForm["tcod"]."'":"NULL";
		$varFam = (isset($varForm["fcod"]) && $varForm["fcod"]<>"")?"'".$varForm["tcod"].$varForm["fcod"]."'":"NULL";
		$varSub = (isset($varForm["scod"]) && $varForm["scod"]<>"")?"'".$varForm["tcod"].$varForm["fcod"].$varForm["scod"]."'":"NULL";
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		$sql = "call spQuery_Producto('A','$varCod', '$varNom', $varPro, $varTip, $varFam, $varSub, '$varMar','$varMat', $varFie, $varOrd)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"1200", "sql_cab"=>utf8_decode($sql), "pages"=>50,
						"conte"=>array(array("titulo"=>"CART",	   "ancho"=>"40" , "alinear"=>"center",  "tipo"=>"texto"), 
									   array("titulo"=>"ARTICULO", "ancho"=>"300", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"CPRO",     "ancho"=>"40" , "oculto"=>"SI"), 
									   array("titulo"=>"PROVEEDOR","ancho"=>"120", "oculto"=>"SI"),
									   array("titulo"=>"UM COM",   "ancho"=>"40",  "alinear"=>"center"),
									   array("titulo"=>"UM COM",   "ancho"=>"40" , "oculto"=>"SI"),
									   array("titulo"=>"UM VEN",   "ancho"=>"40",  "alinear"=>"center"),
									   array("titulo"=>"UM VEN",   "ancho"=>"40" , "oculto"=>"SI"),
									   array("titulo"=>"UM STK",   "ancho"=>"40",  "alinear"=>"center"),
									   array("titulo"=>"UM STK",   "ancho"=>"40" , "oculto"=>"SI"),
									   array("titulo"=>"CLIN",     "ancho"=>"40" , "oculto"=>"SI"), 
									   array("titulo"=>"LINEA",    "ancho"=>"120", "fijo"=>"SI"),
									   array("titulo"=>"CFAM",     "ancho"=>"40" , "oculto"=>"SI"), 
									   array("titulo"=>"FAMILIA",  "ancho"=>"120", "fijo"=>"SI"),
									   array("titulo"=>"CSUB",     "ancho"=>"40" , "oculto"=>"SI"), 
									   array("titulo"=>"SUB FAMILIA", "ancho"=>"120", "fijo"=>"SI"),
									   array("titulo"=>"CMAR",     "ancho"=>"40" , "oculto"=>"SI"), 
									   array("titulo"=>"MARCA",    "ancho"=>"120", "oculto"=>"SI", "fijo"=>"SI"),
									   array("titulo"=>"CMAT",     "ancho"=>"40" , "oculto"=>"SI"), 
									   array("titulo"=>"MATERIAL", "ancho"=>"120", "oculto"=>"SI", "fijo"=>"SI"),
									   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"=>1, "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"=>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"),
																		   )
															)
											 )
									)
						);
		return $this->SetGridTabla2($varTabla);
		}
	function FormuRegistro($varCod=NULL,$varOpt=NULL) {
		if($varOpt=="NEW") {
			$SubTit = $this->varTitulo." - NUEVO";
			$Permite = $_SESSION["_FINS"];
			$varLock = "NO";
			$Fila = NULL;
			$varUMCom = "UND";
			$varUMVen = "UND";
			$varUMStk = "UND";
			}
		else {
			/*========================================================================================
			Verificamos si el Producto ya tiene movimientos
			Si el producto ya tiene movimientos entonces bloqueamos la edicion de la unidad de medida
			==========================================================================================*/
			$sql = "select count(*) from _sv_movimiento_almacen where _estado='A' and id_producto = '$varCod';";
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$Fila = $this->FetchArray($rs);
			$varFlagUM = ($Fila[0]>0)?"SI":"NO";
			
			$SubTit =  $this->varTitulo." - EDITAR";
			$varLock = "SI";
			$sql = "select * from ".$this->varTabla." where ".$this->varId." = '$varCod'";		
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$Fila = $this->FetchArray($rs);
			$varUMCom = (isset($_POST[umc]))?$_POST[umc]:$Fila["id_um_compra"];
			$varUMVen = (isset($_POST[umv]))?$_POST[umv]:$Fila["id_um_venta"];
			$varUMStk = (isset($_POST[ums]))?$_POST[ums]:$Fila["id_um_stock"];
			}
		$sqlmar = "call spQuery_Combo_Producto_Marca();";
		$sqlmat = "call spQuery_Combo_Producto_Material();";
		
		$sqltip = "call spQuery_Lista_Producto_Linea('A', NULL, NULL);";
		$sqlfam = "call spQuery_Combo_Producto_Familia('$Fila[id_tipo]');";
		$sqlsub = "call spQuery_Combo_Producto_Sub_Familia('$Fila[id_familia]');";		
		$sqlumc = "call spQuery_Combo_UM('');";
		
		$sqlumv = "call spQuery_Combo_UM('$varUMCom');";
		$sqlums = "call spQuery_Combo_UM('$varUMCom');";
		
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"430", "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"=>"pro", "name"=>"pro", "valor"=>"0"),
								   array("tipo"=>"oculto", "id"=>"umc1", "name"=>"umc1", "valor"=>$Fila["id_um_compra"]),
								   array("tipo"=>"oculto", "id"=>"umv1", "name"=>"umv1", "valor"=>$Fila["id_um_venta"]),
								   array("tipo"=>"oculto", "id"=>"ums1", "name"=>"ums1", "valor"=>$Fila["id_um_stock"]),
								  ),
					"conte"=>array(array("titulo"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"100",
										 "campo" =>array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "clase"=>"inputDisable",
										 "valor"=>$Fila[$this->varId], "alinear"=>"center", "locked"=>"SI", "ancho"=>"70")),
								   array("titulo"=>"Linea :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										 "campo"=>array("tipo"=>"select", "id"=>"tip", "name"=>"tip", "valor"=>$Fila["id_tipo"], "sql"=>$sqltip,
														"ancho"=>"300", "titulo"=>"--[[SELECCIONAR]]--", "reload"=>"xajax_comboFamilia(this.value)")),
								   array("titulo"=>"Familia :", "clase1"=>"FormCampo1", "clase2"=>"interior", "div"=>"divFam",
										 "campo"=>array("tipo"=>"select", "id"=>"fam", "name"=>"fam", "valor"=>$Fila["id_familia"], "sql"=>$sqlfam,
														"ancho"=>"300",  "titulo"=>"--[[SELECCIONAR]]--", "sincod"=>"SI", 
														"reload"=>"xajax_comboSubFamilia(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"],
														"sql"=>$sqlsub, "ancho"=>"300",  "titulo"=>"--[[SELECCIONAR]]--", "sincod"=>"SI")),
								   array("titulo"=>"Marca :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										 "campo"=>array("tipo"=>"select", "id"=>"mar", "name"=>"mar", "valor"=>$Fila["id_marca"],
														"sql"=>$sqlmar, "ancho"=>"300", "sincod"=>"SI")),
								   array("titulo"=>"Material :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										 "campo"=>array("tipo"=>"select", "id"=>"mat", "name"=>"mat", "valor"=>$Fila["id_material"],
														"sql"=>$sqlmat, "ancho"=>"300", "sincod"=>"SI")),
								   array("titulo"=>"Descripcion :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", "valor"=>$Fila["descripcion"], "ancho"=>"300")),
								   array("titulo"=>"UM Compra :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										 "campo"=>array("tipo"=>"select", "id"=>"umc", "name"=>"umc", "valor"=>$varUMCom,
														"sql"=>$sqlumc, "ancho"=>"200", "reload"=>"xajax_comboUM(this.value)", "locked"=>$varFlagUM)),
								   array("titulo"=>"UM Venta :", "clase1"=>"FormCampo1", "clase2"=>"interior", "div"=>"divUMV",
										 "campo"=>array("tipo"=>"select", "id"=>"umv", "name"=>"umv", "valor"=>$varUMVen,
														"sql"=>$sqlumv, "ancho"=>"200", "locked"=>$varFlagUM)),
								   array("titulo"=>"UM Stock :", "clase1"=>"FormCampo1", "clase2"=>"interior", "div"=>"divUMS",
										 "campo"=>array("tipo"=>"select", "id"=>"ums", "name"=>"ums", "valor"=>$varUMStk,
														"sql"=>$sqlums, "ancho"=>"200", "locked"=>$varFlagUM)),
								   array("titulo"=>"Stock Minimo :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo" =>array("tipo"=>"texto", "id"=>"stk", "name"=>"stk", "valor"=>$Fila["stock_minimo"], 
														 "ancho"=>"100", "keypress"=>"return SoloNumeros(event)"),
										 ),
								   array("titulo"=>"Peso Kg. :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo" =>array("tipo"=>"texto", "id"=>"pes", "name"=>"pes", "valor"=>$Fila["peso"], 
										 "ancho"=>"100", "keypress"=>"return SoloNumeros(event)"),
										 )
								   ),
					"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 $sqlmar."-".$sqltip."-".$sqlfam."-".$sqlsub."-".$sqlmat."-".$sqlpro."-".$sqlumc."-".$sqlumv."-".$sqlums;
		return $this->setTablaForm($varTabla);	
		}
		
	function ProcesoGrabar($varForm = NULL) {
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varOpt = ($varForm["opt"]=="")? "NULL" : $varForm[opt];
		$varCod = ($varForm["cod"]=="")? "NULL" : "'$varForm[cod]'";
		$varTip = ($varForm["tip"]=="")? "NULL" : "'$varForm[tip]'";
		$varTip = ($varForm["tip"]=="")? "NULL" : "'$varForm[tip]'";
		$varMar = ($varForm["mar"]=="")? "NULL" : "'$varForm[mar]'";
		$varMat = ($varForm["mat"]=="")? "NULL" : "'$varForm[mat]'";
		$varNam = ($varForm["nam"]=="")? "NULL" : "'$varForm[nam]'";
		//$varUmc = ($varForm["umc"]=="")? "NULL" : "'$varForm[umc]'";		
		//$varUmv = ($varForm["umv"]=="")? "NULL" : "'$varForm[umv]'";
		//$varUms = ($varForm["ums"]=="")? "NULL" : "'$varForm[ums]'";
		$varStk = ($varForm["stk"]=="")? "NULL" : $varForm[stk];
		$varPes = ($varForm["pes"]=="")? "NULL" : $varForm[pes];
		
		$varUmv = (isset($varForm["umv"]) && $varForm["umv"]<>"")?"'$varForm[umv]'":"'$varForm[umv1]'";
		$varUms = (isset($varForm["ums"]) && $varForm["ums"]<>"")?"'$varForm[ums]'":"'$varForm[ums1]'";
		$varUmc = (isset($varForm["umc"]) && $varForm["umc"]<>"")?"'$varForm[umc]'":"'$varForm[umc1]'";
		
		$sql = "call spMante_Producto('$varOpt', $varCod, $varForm[pro], '$varForm[tip]', '$varForm[fam]', '$varForm[sub]', $varMar, $varMat, 
									   $varUmc, $varUmv, $varUms, $varNam, $varStk, $varPes, $varUsr);";
		$this->AbrirConexion();
		$this->Query(utf8_decode($sql));
		$this->CerrarConexion();
		return $sql;
		}
	function ProcesoEliminar($varCod) {
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$sql = "update ".$this->varTabla." set _estado = 'X', _date_update=NOW(), _user_update=$varUsr where ".$this->varId."= '".$varCod."';";
		$this->AbrirConexion();
		$this->Query($sql);
		$this->CerrarConexion();
		}
	}
	
?>