<?php
//include "clase.conexion.php";
class clsBonificacion extends clsConexion{
	public $varTitulo = "MAESTRO BONIFICICACIONES";
	public $varMyTabla  = "_sv_bonificacion";
	public $varMyID  	  = "id_bonif";
	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"];
		$varEst = (isset($varForm["est"]) && $varForm["est"]<>'')? $varForm["est"]:"NULL";
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		
		$sql = "call spQuery_Bonificacion($varCod,'$varNom', $varEst, $varFie, $varOrd)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"800", "sql_cab"=>$sql, "pages"=>50,
						"conte"=>array(array("titulo"=>"CBON",	   	 "ancho"=>"40", "alinear"=>"center"), 									   
									   array("titulo"=>"BONIFICACION","ancho"=>"300", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"CTIP",	   "ancho"=>"40", "alinear"=>"center"), 									   
									   array("titulo"=>"TIPO",	   "ancho"=>"300", "fijo"=>"SI"),
									   array("titulo"=>"LISTA PRECIOS",	"ancho"=>"300", "fijo"=>"SI"),
									   array("titulo"=>"FECHA INICIO",  "ancho"=>"40", "alinear"=>"center"), 
									   array("titulo"=>"FECHA TERMINO", "ancho"=>"40", "alinear"=>"center"), 
									   array("titulo"=>"ESTADO",   "ancho"=>"40", "alinear"=>"center"),
									   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> LA BONIFICACION<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/b_table_delete.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>ELIMINAR</b> LA BONIFICACION<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);
		//return $sql;
		}
	
	function GridDatosDetalle($varCod  = NULL, 
							  $varField = NULL, 
							  $varOrder = NULL
							  ) 
		{
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		
		$sql = "call spQuery_Lista_Precios_Detalle($varCod, $varFie, $varOrd)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"800", "sql_cab"=>$sql, "pages"=>50,
						"conte"=>array(array("titulo"=>"CPRO",	   "ancho"=>"40", "alinear"=>"center"), 									   
									   array("titulo"=>"PRODUCTO", "ancho"=>"300", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"UM",       "ancho"=>"40", "alinear"=>"center"), 
									   array("titulo"=>"PRECIO",   "ancho"=>"40", "alinear"=>"right", "decimal"=>5), 
									   array("titulo"=>"FCREADO",  "ancho"=>"100", "fijo"=>"SI"),
									   array("titulo"=>"UCREADO",  "ancho"=>"100", "fijo"=>"SI", "alinear"=>"center")
									   ),
						"boton"=>array(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><b>"),
																 array("valor"=>0, "tipo"=>"INDEX"),
																 array("valor"=>" - "),
																 array("valor"=>1, "tipo"=>"INDEX"),
																 array("valor"=>"</b>")
																),
											 "click"=>array("funcion"=>"xEliminarDetalleJS",
															"param"=>array(array("valor"=>0, "tipo"=>"INDEX"),
																		   array("valor"=>1, "tipo"=>"INDEX"),
																		   array("valor"=>$varCod, "tipo"=>"NUM"),
																		   array("valor"=>"this", "tipo"=>"NUM")
																		   )
															)
											 )
									)
						);
		
		return $this->SetGridTabla2($varTabla);
		}
	
	function FormuRegistro($varOpt=NULL, $varCod=NULL) {
		if($varOpt=="NEW") {
			$SubTit = $this->varTitulo." - NUEVO";			
			$varLock = "NO";
			$Fila = NULL;
			$sql1 = NULL;
			$sql2 = NULL;
			$sql3 = NULL;
			$varNum1 = 0;
			$varNum2 = 0;
			$varNum3 = 0;
			$varSeleccion = array("tipo"=>"simple",  "valor"=>NULL);
			}
		else {
			$SubTit =  $this->varTitulo." - EDITAR";
			$varLock = "SI";
			
			$sql = "select id_bonif, nombre, id_tipo_bonif, id_ruta, id_lista, estado, 
					DATE_FORMAT(fecha_ini,'%d/%m/%Y') as fecha_ini, DATE_FORMAT(fecha_fin,'%d/%m/%Y') as fecha_fin 
					from ".$this->varMyTabla." where ".$this->varMyID." = $varCod";
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$Fila = $this->FetchArray($rs);
			
			if($Fila["id_tipo_bonif"]=="R") {
				$sql = "call spQuery_Lista_Ruta('','%');";	
				$varSeleccion = array("tipo"=>"select", "id"=>"sel", "name"=>"sel", "valor"=>$Fila["id_ruta"],
									  "combo"=>array("sql"=>$sql, "titulo"=>" -- [[ SELECCIONAR ]] --", "ancho"=>"280")
									  );
				}
			elseif($Fila["id_tipo_bonif"]=="P") {
				$sql = "call spQuery_Combo_Lista_Precios();";
				$varSeleccion = array("tipo"=>"select", "id"=>"lp", "name"=>"lp", "valor"=>$Fila["id_lista"],
									  "combo"=>array("sql"=>$sql, "titulo"=>" -- [[ SELECCIONAR ]] --", "ancho"=>"280")
									  );
				}
			else {
				$varSeleccion = array("tipo"=>"simple",  "valor"=>NULL);
				}
			// Reglas y Condiciones de Venta
			$sql1= "select V.id_producto as CART, A.descripcion as ARTICULO, V.id_um as UM,
					V.cantidad, V.minimo, V.maximo, 1 as DEL
					from _sv_bonificacion_venta V
					inner join _sv_producto A ON V.id_producto = A.id_producto
					WHERE V.id_bonif = $varCod
					and V._estado = 'A'";
			$this->AbrirConexion();
			$rs = $this->Query($sql1);
			$this->CerrarConexion();
			$varNum1 = $this->NumFilas($rs);
		
			// Articulos a Bonificar
			$sql2= "select V.id_producto as CART, A.descripcion as ARTICULO, V.id_um as UM,
					V.cantidad, V.cuota, 1 as DEL
					from _sv_bonificacion_bonif V
					inner join _sv_producto A ON V.id_producto = A.id_producto
					WHERE V.id_bonif = $varCod
					and V._estado = 'A'";
			$this->AbrirConexion();
			$rs2 = $this->Query($sql2);
			$this->CerrarConexion();
			$varNum2 = $this->NumFilas($rs2);
			
			// Lista de Clientes
			$sql3= "select V.id_cliente as CCLI, C.nombre as CLIENTE, V.tope, 1 as DEL
					from _sv_bonificacion_cliente V
					inner join _sv_cliente C ON V.id_cliente = C.id_cliente
					WHERE V.id_bonif = $varCod
					and V._estado = 'A'";
			$this->AbrirConexion();
			$rs3 = $this->Query($sql3);
			$this->CerrarConexion();
			$varNum3 = $this->NumFilas($rs3);
			}
		
		$sqltip	= "select id_tipo_bonif, nombre from _sv_tipo_bonif;";
		$sqlest	= array("A"=>"ACTIVO", "I"=>"INCATIVO");
		$varFormu=array("id"=>"form", "name"=>"form", "metodo"=>"POST", "onsubmit"=>"return validaFormAccion(this,'N')");
		$varTabla=array("id"=>"table", "name"=>"table", "tamano"=>"400", "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",
										 "campo"=>array("tipo"=>"text", "id"=>"cod", "name"=>"cod", "valor"=>$Fila["id_bonif"], 
														"ancho"=>"50", "alinear"=>"center", "locked"=>"SI", "clase"=>"inputDisable")
										 ),
									   array("titulo"=>"Nombre :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"80",
											 "campo"=>array("tipo"=>"text", "id"=>"nam", "name"=>"nam", "valor"=>$Fila["nombre"], "ancho"=>"280")
											 ),
									   array("titulo"=>"Tipo Bonif :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo"=>array("tipo"=>"select", "id"=>"tipo", "name"=>"tipo", "valor"=>$Fila["id_tipo_bonif"],
															"combo"=>array("sql"=>$sqltip, "titulo"=>" -- [[ SELECCIONAR ]] --",
															"ancho"=>"200", "reload"=>"xajax_comboSeleccion(this.value)"))),
									   array("titulo"=>"Selecci&oacute;n :", "clase1"=>"FormCampo1", "clase2"=>"interior", "div"=>"divSel",
											 "campo"=>$varSeleccion),
									   array("titulo"=>"Fecha Inicio :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo"=>array("tipo"=>"fecha", "id"=>"fini", "name"=>"fini", "valor"=>$Fila["fecha_ini"],
															"ancho"=>"100", "alinear"=>"center")),
									   array("titulo"=>"Fecha Fin :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo"=>array("tipo"=>"fecha", "id"=>"ffin", "name"=>"ffin", "valor"=>$Fila["fecha_fin"],
															"ancho"=>"100", "alinear"=>"center")),
									   array("titulo"=>"Estado :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo"=>array("tipo"=>"select_simple", "id"=>"est", "name"=>"est", "valor"=>$Fila["estado"],
														"combo"=>array("sql"=>$sqlest, "ancho"=>"100"))),
									   ),
						"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()")
									  )
						);
		$Titulo1 = "<input id=\"btadd1\" name=\"btadd1\" type=\"button\" class=\"boton_agregar_producto\" style=\"cursor:pointer; width:150px;\"  
				   value=\"Agregar Articulo\" onClick=\"AddArticuloVenta()\">";
							
		$Titulo2 = "<input id=\"btadd2\" name=\"btadd2\" type=\"button\" class=\"boton_agregar_producto\" style=\"cursor:pointer; width:150px;\"  
				   value=\"Agregar Articulo\" onClick=\"AddArticuloBonif()\">";

		$Titulo3 = "<input id=\"btadd3\" name=\"btadd3\" type=\"button\" class=\"boton_agregar_cliente\" style=\"cursor:pointer; width:150px;\"  
				   value=\"Agregar Cliente\" onClick=\"AddCliente()\">";


		$varTab_1=array("id"=>"tab1", "name"=>"tab1", "tamano"=>"600", "clase"=>"FormTitulo", "titulo"=>$Titulo1, "sql"=>$sql1,
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"num1", "name"=>"num1", "valor"=>$varNum1)),
						"conte"=>array(array("titulo"=>"CCOD", "clase"=>"FormTitulo", "tamano"=>"90", "alinear"=>"center",
											 "campo"=>array("tipo"=>"texto", "id"=>"cod1", "name"=>"cod1", "alinear"=>"center", "ancho"=>"60", 
															"keypress"=>"return SoloEnteros(event)",
															"dclick"=>array("funcion"=>"fn_cargar_listado_producto",
																			"cuerpo" =>array(array("campo"=>0), array("campo"=>1), array("campo"=>2))
																			),
															"blur"  =>array("funcion"=>"xajax_seleccionar_producto_bonif",
																			"cuerpo" =>array(array("campo"=>"this.value", "tipo"=>"NUM"), 
																							 array("campo"=>0), 
																							 array("campo"=>1), 
																							 array("campo"=>2)
																							 )
																			)
															)
											 ),
									   array("titulo"=>"DESCRIPCION", "clase"=>"FormTitulo", "tamano"=>"400",
											 "campo"=>array("tipo"=>"texto", "id"=>"nom1", "name"=>"nom1", "clase"=>"inputDisable", "locked"=>"SI", "ancho"=>"310")
											),
									   array("titulo"=>"U.M.", "clase"=>"FormTitulo", "tamano"=>"50",
											 "campo"=>array("tipo"=>"texto", "id"=>"um1", "name"=>"um1", "clase"=>"inputDisable", 
															"locked"=>"SI", "ancho"=>"45", "alinear"=>"center")
											),										   
									   array("titulo"=>"CANT.", "clase"=>"FormTitulo", "tamano"=>"50", "alinear"=>"center",
											 "campo"=>array("tipo"=>"texto", "id"=>"qcan1", "name"=>"qcan1", "alinear"=>"center", "ancho"=>"45", 
															"keypress"=>"return SoloEnteros(event)"
															)
											),
									   array("titulo"=>"MIN.", "clase"=>"FormTitulo", "tamano"=>"50", "alinear"=>"center",
											 "campo"=>array("tipo"=>"texto", "id"=>"qmin1", "name"=>"qmin1", "alinear"=>"center", "ancho"=>"45", 
															"keypress"=>"return SoloNumeros(event)"
															)
											),
									   array("titulo"=>"MAX.", "clase"=>"FormTitulo", "tamano"=>"50", "alinear"=>"center",
											 "campo"=>array("tipo"=>"texto", "id"=>"qmax1", "name"=>"qmax1", "alinear"=>"center", "ancho"=>"45", 
															"keypress"=>"return SoloNumeros(event)"
															)
											),
									   array("titulo"=>"DEL", "clase"=>"FormTitulo", "tamano"=>"20", "alinear"=>"center",
											 "campo"=>array("tipo"=>"imagen", "click"=>"EliminarFilaTabla(this)", "img"=>"images/b_drop.png")
											)
									   )
						);


		$varTab_2=array("id"=>"tab2", "name"=>"tab2", "tamano"=>"600", "clase"=>"FormTitulo", "titulo"=>$Titulo2, "sql"=>$sql2,
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"num2", "name"=>"num2", "valor"=>$varNum2)),
						"conte"=>array(array("titulo"=>"CCOD", "clase"=>"FormTitulo", "tamano"=>"90", "alinear"=>"center",
											 "campo"=>array("tipo"=>"texto", "id"=>"cod2", "name"=>"cod2", "alinear"=>"center", "ancho"=>"60", 
															"keypress"=>"return SoloEnteros(event)",
															"dclick"=>array("funcion"=>"fn_cargar_listado_producto",
																			"cuerpo" =>array(array("campo"=>0), 
																							 array("campo"=>1),
																							 array("campo"=>2)
																							 )
																			),
															"blur"  =>array("funcion"=>"xajax_seleccionar_producto_bonif",
																			"cuerpo" =>array(array("campo"=>"this.value", "tipo"=>"NUM"), 
																							 array("campo"=>0), 
																							 array("campo"=>1), 
																							 array("campo"=>2)
																							 )
																			)
															)
											 ),
									   array("titulo"=>"DESCRIPCION", "clase"=>"FormTitulo", "tamano"=>"350",
											 "campo"=>array("tipo"=>"texto", "id"=>"nom2", "name"=>"nom2", "clase"=>"inputDisable", "locked"=>"SI", "ancho"=>"360")
											),
									   array("titulo"=>"U.M.", "clase"=>"FormTitulo", "tamano"=>"50",
											 "campo"=>array("tipo"=>"texto", "id"=>"um2", "name"=>"um2", "clase"=>"inputDisable", 
															"locked"=>"SI", "ancho"=>"45", "alinear"=>"center")
											),										   
									   array("titulo"=>"CANT.", "clase"=>"FormTitulo", "tamano"=>"50", "alinear"=>"center",
											 "campo"=>array("tipo"=>"texto", "id"=>"qcan2", "name"=>"qcan2", "alinear"=>"center", "ancho"=>"45", 
															"keypress"=>"return SoloNumeros(event)"
															)
											),
									   array("titulo"=>"CUOTA", "clase"=>"FormTitulo", "tamano"=>"50", "alinear"=>"center",
											 "campo"=>array("tipo"=>"texto", "id"=>"qcuo2", "name"=>"qcuo2", "alinear"=>"center", "ancho"=>"45", 
															"keypress"=>"return SoloNumeros(event)"
															)
											),
									   array("titulo"=>"DEL", "clase"=>"FormTitulo", "tamano"=>"20", "alinear"=>"center",
											 "campo"=>array("tipo"=>"imagen", "click"=>"EliminarFilaTabla(this)", "img"=>"images/b_drop.png")
											)
									   )
						);
		
		$varTab_3=array("id"=>"tab3", "name"=>"tab3", "tamano"=>"600", "clase"=>"FormTitulo", "titulo"=>$Titulo3, "sql"=>$sql3,
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"num3", "name"=>"num3", "valor"=>$varNum3)),
						"conte"=>array(array("titulo"=>"CCLI", "clase"=>"FormTitulo", "tamano"=>"90", "alinear"=>"center",
											 "campo"=>array("tipo"=>"texto", "id"=>"cod3", "name"=>"cod3", "alinear"=>"center", "ancho"=>"60", 
															"keypress"=>"return SoloEnteros(event)",
															"dclick"=>array("funcion"=>"fn_cargar_listado_cliente",
																			"cuerpo" =>array(array("campo"=>0), 
																							 array("campo"=>1)
																							 )
																			),
															"blur"  =>array("funcion"=>"xajax_seleccionar_cliente_bonif",
																			"cuerpo" =>array(array("campo"=>"this.value", "tipo"=>"NUM"), 
																							 array("campo"=>0), 
																							 array("campo"=>1)
																							 )
																			)
															)
											 ),
									   array("titulo"=>"NOMBRE DEL CLIENTE", "clase"=>"FormTitulo", "tamano"=>"350",
											 "campo"=>array("tipo"=>"texto", "id"=>"nom3", "name"=>"nom3", "clase"=>"inputDisable", "locked"=>"SI", "ancho"=>"450")
											),
									   array("titulo"=>"TOPE", "clase"=>"FormTitulo", "tamano"=>"50", "alinear"=>"center",
											 "campo"=>array("tipo"=>"texto", "id"=>"qtop3", "name"=>"qtop3", "alinear"=>"center", "ancho"=>"45", 
															"keypress"=>"return SoloEnteros(event)",
															"keyup"=>array("funcion"=>"CalcularItem",
																		   "cuerpo" =>array(array("campo"=>0, "tipo"=>"index"))
																		  )
															)
											),									   
									   array("titulo"=>"DEL", "clase"=>"FormTitulo", "tamano"=>"20", "alinear"=>"center",
											 "campo"=>array("tipo"=>"imagen", "click"=>"EliminarFilaTabla(this)", "img"=>"images/b_drop.png")
											)
									   )
						);
		
		
		$varTabs =array(array("name"=>"Reglas y Condiciones Venta",
							  "tipo"=>"det",  "alto"=>"300", "ancho"=>"620", "tabla"=>$varTab_1
							  ),
						array("name"=>"Productos a Bonificar",
							  "tipo"=>"det",  "alto"=>"300", "ancho"=>"620", "tabla"=>$varTab_2
							  ),	
						array("name"=>"Listado de CLientes",
							  "tipo"=>"det",  "alto"=>"300", "ancho"=>"620", "tabla"=>$varTab_3
							  )
						);
		$varMyTable = $this->setTablaFormTabs($varFormu, $varTabla, $varTabs);
		
		return $varMyTable;	
		}
		
	function FormuDetalle($varCod=NULL) {
		$sql = "call spQuery_Bonificacion($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"=>"500", "clase"=>"FormTitulo", "titulo"=>$this->varTitulo." - ASIGNACION DE CLIENTES",					
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo." - ".$Fila["LISTA"]),
									   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["CBON"], "alinear"=>"center", "locked"=>"SI", "ancho"=>"70")),
									   array("titulo"=>"Nombre :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"simple", "valor"=>$Fila["BONIFICACION"])),
									   array("titulo"=>"Tipo :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"simple", "valor"=>$Fila["CTIP"]." - ".$Fila["TIPO"])),
									   array("titulo"=>"Fecha Inicio :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"simple", "valor"=>$Fila["DESDE"])),
									   array("titulo"=>"Fecha Fin :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"simple", "valor"=>$Fila["HASTA"])),
									   array("titulo"=>"Estado :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"simple", "valor"=>$Fila["ESTADO"]))
									   )
						);		
		$varReturn.= $this->setTablaForm($varTabla);
		
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"500", "clase"=>"FormTitulo", "titulo"=>"AGREGAR PRODUCTO",					
						"conte"=>array(array("titulo"=>"Producto :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"80",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"pcod", "name"=>"pcod", "valor"=>$_POST["pcod"],
																					 "ancho"=>"60", "alinear"=>"center", 
																					 "keypress"=>"return SoloEnteros(event)",
																					 "blur"=>"xajax_seleccionar_producto_listap(this.value)", 
																					 "dclick"=>"fn_cargar_listado_producto_listap('pcod', 'pnom', 'pum')"),
																			   array("tipo"=>"text", "id"=>"pnom", "name"=>"pnom", "valor"=>$_POST["pnom"], 
																					 "ancho"=>"260", "clase"=>"inputDisable", "locked"=>"SI"),
																			   array("tipo"=>"text", "id"=>"pum", "name"=>"pum", "valor"=>$_POST["pum"], 
																					 "ancho"=>"60", "alinear"=>"center", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 	)
											 ),
									  array("titulo"=>"Precio :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"pre", "name"=>"pre", "valor"=>$_POST["pre"],
																					 "ancho"=>"100", "alinear"=>"right", 
																					 "keypress"=>"return SoloNumeros(event)"),
																			   array("tipo"=>"simple", "valor"=>"Ingresar Precio a Agregar")
																			   )
														 	)
											 )
									   ),
						"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);
		
		$sql = "call spQuery_Lista_Precios_Detalle($varCod)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"800", "sql_cab"=>$sql, "pages"=>50,
						"conte"=>array(array("titulo"=>"CPRO",	   "ancho"=>"40", "alinear"=>"center"), 									   
									   array("titulo"=>"PRODUCTO", "ancho"=>"300", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"UM",       "ancho"=>"40", "alinear"=>"center"), 
									   array("titulo"=>"PRECIO",   "ancho"=>"40", "alinear"=>"right", "decimal"=>5), 
									   array("titulo"=>"FCREADO",  "ancho"=>"100", "fijo"=>"SI"),
									   array("titulo"=>"UCREADO",  "ancho"=>"100", "fijo"=>"SI", "alinear"=>"center")
									   ),
						"boton"=>array(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><b>"),
																 array("valor"=>0, "tipo"=>"INDEX"),
																 array("valor"=>" - "),
																 array("valor"=>1, "tipo"=>"INDEX"),
																 array("valor"=>"</b>")
																),
											 "click"=>array("funcion"=>"xEliminarDetalleJS",
															"param"=>array(array("valor"=>0, "tipo"=>"INDEX"),
																		   array("valor"=>1, "tipo"=>"INDEX"),
																		   array("valor"=>$varCod, "tipo"=>"NUM"),
																		   array("valor"=>"this", "tipo"=>"NUM")
																		   )
															)
											 )
									)
						);
		$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]'";
		$varRut = ($varForm["sel"])? "'$varForm[sel]'":"NULL" ;
		$varLis = ($varForm["lp"])? $varForm["lp"]:"NULL" ;
		$varIni = ($varForm["fini"]=="")?"NULL":"'".FechaMysql($varForm["fini"])."'";
		$varFin = ($varForm["ffin"]=="")?"NULL":"'".FechaMysql($varForm["ffin"])."'";
		// Eliminamos Detalles
		$sql = "DELETE FROM _sv_bonificacion_venta   WHERE id_bonif=$varCod;";
		$this->AbrirConexion();
		$this->Query($sql);
		$this->CerrarConexion();
		$sql = "DELETE FROM _sv_bonificacion_bonif   WHERE id_bonif=$varCod;";
		$this->AbrirConexion();
		$this->Query($sql);
		$this->CerrarConexion();
		$sql = "DELETE FROM _sv_bonificacion_cliente WHERE id_bonif=$varCod;";
		$this->AbrirConexion();
		$this->Query($sql);
		$this->CerrarConexion();	
		
		$sql = "call spMante_Bonificacion('$varForm[opt]', $varCod, '$varForm[nam]', '$varForm[tipo]', $varRut, $varLis, $varIni, $varFin,  '$varForm[est]', $varUsr);";
		
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$Fila = $this->FetchArray($rs);
		
		$varNewCod = $Fila[1];
		
		$varCantVenta = $varForm["num1"];
		$varCantBonif = $varForm["num2"];
		$varCantClien = $varForm["num3"];
		
		//INSERTAMOS REGLAS Y CONDICIONES DE VENTA
		$varSqlVenta = "";
		for($i=0; $i<$varCantVenta;$i++) {
			$varArt = str_pad(trim($varForm["cod1"][$i]), 6, "0", STR_PAD_LEFT);
			$varUMe = ($varForm["um1"][$i]=="")?"NULL":"'".$varForm["um1"][$i]."'";
			$varCan = ($varForm["qcan1"][$i]=="")?0:$varForm["qcan1"][$i];
			$varMin = ($varForm["qmin1"][$i]=="")?0:$varForm["qmin1"][$i];
			$varMax = ($varForm["qmax1"][$i]=="")?9999:str_replace(",", "",$varForm["qmax1"][$i]);
			
			$varSqlVenta = "insert into _sv_bonificacion_venta values(".$varNewCod.", '$varArt', $varUMe, $varCan , $varMin, $varMax, NOW(), NOW(), $varUsr, $varUsr,'A');";			$this->AbrirConexion();
			$rs = $this->Query($varSqlVenta);
			$this->CerrarConexion();	
			}
		
		
		//INSERTAMOS PRODUCTOS A BONIFICAR
		$varSqlBonif= "";
		for($i=0; $i<$varCantBonif;$i++) {
			$varArt = str_pad(trim($varForm["cod2"][$i]), 6, "0", STR_PAD_LEFT);
			$varUMe = ($varForm["um2"][$i]=="")?"NULL":"'".$varForm["um2"][$i]."'";
			$varCan = ($varForm["qcan2"][$i]=="")?0:$varForm["qcan2"][$i];
			$varCuo = ($varForm["qcuo2"][$i]=="")?0:$varForm["qcuo2"][$i];
			
			$varSqlBonif = "insert into _sv_bonificacion_bonif values(".$varNewCod.", '$varArt', $varUMe, $varCan , $varCuo, NOW(), NOW(), $varUsr, $varUsr,'A');";				
			$this->AbrirConexion();
			$rs = $this->Query($varSqlBonif);
			$this->CerrarConexion();
			}
		
		
		//INSERTAMOS LISTADO DE CLIENTES
		$varSqlClien= "";
		for($i=0; $i<$varCantClien;$i++) {
			$varCli = trim($varForm["cod3"][$i]);
			$varCuo = ($varForm["qtop3"][$i]=="")?0:$varForm["qtop3"][$i];
			
			$varSqlClien = "insert into _sv_bonificacion_cliente values(".$varNewCod.", '$varCli', $varCuo, NOW(), NOW(), $varUsr, $varUsr,'A');";				
			$this->AbrirConexion();
			$rs = $this->Query($varSqlClien);
			$this->CerrarConexion();
			}
		
		
		return $Fila[0];
		
		}
	
	function ProcesoGrabarDetalle($varForm = NULL) {
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;		
		$sql = "call spMante_Lista_Precios_Detalle($varForm[cod], '".str_pad($varForm[pcod], 6, "0", STR_PAD_LEFT)."', '$varForm[pum]', $varForm[pre], $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, $varLis) {
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$sql = "delete from _sv_lista_producto where id_producto = '$varCod' and id_lista=$varLis;";
		$this->AbrirConexion();
		$this->Query($sql);
		$this->CerrarConexion();
		}
	}
	
?>