<?php
//include "clase.conexion.php";
class clsTipoDocVenta extends clsConexion{
	public $varTitulo 	= "TIPOS DE DOCUMENTO VENTA";
	public $varMyTabla  = "_sv_tipo_doc_venta";
	public $varMyID  	= "id_tipo_doc";
	function FormuBusqueda() {
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"430", "clase"=>"FormTitulo", "titulo"=>$this->varTitulo." - BUSQUEDA",
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST", "onsubmit"=>"return false;"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo),
									   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt", "valor"=>$Opcion)
									  ),
						"conte"=>array(array("titulo"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"100",
										 "campo" =>array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "valor"=>$_POST["cod"], 
										 "alinear"=>"center", "ancho"=>"70")),								   
								   array("titulo"=>"Descripci&oacute;n :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", "valor"=>$_POST["nam"], "ancho"=>"306"))								   
								   ),
					  "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";
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		
		$sql = "call spQuery_Tipo_Doc_Venta($varCod, '$varForm[nam]', $varFie, $varOrd)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"700", "sql_cab"=>$sql,	"pages"=>20,
					    "conte"=>array(array("titulo"=>"CTIP",	"ancho"=>"40" , "alinear"=>"center", "clase"=>"sortable-numeric", "tipo"=>"texto"), 
									   array("titulo"=>"TIPO",	"ancho"=>"300", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"ABREV", "ancho"=>"50", "alinear"=>"center"),
									   array("titulo"=>"ITEMS", "ancho"=>"50", "alinear"=>"center"),
									   array("titulo"=>"SUNAT", "ancho"=>"50", "alinear"=>"center"),
									   array("titulo"=>"FVEN", 	"ancho"=>"50", "alinear"=>"center"),
									   array("titulo"=>"FCAM", 	"ancho"=>"50", "alinear"=>"center"),
									   array("titulo"=>"FPED", 	"ancho"=>"50", "alinear"=>"center"),
									   array("titulo"=>"FNCR", 	"ancho"=>"50", "alinear"=>"center"),
									   array("titulo"=>"FCREADO", "ancho"=>"100", "alinear"=>"center", "fijo"=>"SI", "oculto"=>"SI"),
									   array("titulo"=>"FUPDATE", "ancho"=>"100", "alinear"=>"center", "fijo"=>"SI", "oculto"=>"SI"),
									   array("titulo"=>"UCREADO", "ancho"=>"50", "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"UUPDATE", "ancho"=>"50", "alinear"=>"center", "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";
			$varLock  = "NO";
			$varStyle = "inputNormal";
			}
		else {
			$SubTit   =  $this->varTitulo." - EDITAR";
			$varLock  = "SI";
			$varStyle = "inputDisable";
			$sql = "select * from ".$this->varMyTabla." where ".$this->varMyID." = $varCod";
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$Fila = $this->FetchArray($rs);
			}
		$sqlfla	= array("N"=>"NO", "S"=>"SI");
		$varFormu=array("id"=>"form", "name"=>"form", "metodo"=>"POST");
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "clase"=>"FormTitulo", "titulo"=>$SubTit,
						"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"=>"inputDisable",
											 				 "valor"=>$Fila[$this->varMyID], "alinear"=>"center", "locked"=>"SI", "ancho"=>"70")),					   
									   array("titulo"=>"Nombre :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", "valor"=>$Fila["nombre"], "ancho"=>"310",
											 				 "locked"=>$varLock, "clase"=>$varStyle)),
									   array("titulo"=>"Abrev :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"texto", "id"=>"abr", "name"=>"abr", "valor"=>$Fila["abrev"], "ancho"=>"100",
											 				 "locked"=>$varLock, "clase"=>$varStyle)),
									   array("titulo"=>"Sunat :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"texto", "id"=>"sun", "name"=>"sun", "valor"=>$Fila["cod_sunat"], "ancho"=>"60",
											 				 "alinear"=>"center")),
									   array("titulo"=>"Cant. Item :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"texto", "id"=>"ite", "name"=>"ite", "valor"=>$Fila["num_item"], "ancho"=>"60",
											 				 "keypress"=>"return SoloEnteros(event)", "alinear"=>"center")),
									   array("titulo"=>"Flag Venta :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"varios",
														   "contenido"=>array(array("tipo"=>"select_simple", "id"=>"fven", "name"=>"fven", 
														   							"valor"=>$Fila["flag_venta"], 
																					"combo"=>array("sql"=>$sqlfla, "ancho"=>"100", "sincod"=>"SI")),
																			  array("tipo"=>"simple", "valor"=>"Flag Cambio : ", "clase_in"=>"FormCampo1", 
																			   		 "ancho"=>"80"),
																			  array("tipo"=>"select_simple", "id"=>"fcam", "name"=>"fcam", 
														   							"valor"=>$Fila["flag_cambio"], 
																					"combo"=>array("sql"=>$sqlfla, "ancho"=>"100", "sincod"=>"SI"))
																			  )
														   )
										   ),
									  array("titulo"=>"Flag Pedido :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"varios",
														   "contenido"=>array(array("tipo"=>"select_simple", "id"=>"fped", "name"=>"fped", 
														   							"valor"=>$Fila["flag_pedido"], 
																					"combo"=>array("sql"=>$sqlfla, "ancho"=>"100", "sincod"=>"SI")),
																			  array("tipo"=>"simple", "valor"=>"Flag N.C. : ", "clase_in"=>"FormCampo1", 
																			   		 "ancho"=>"80"),
																			  array("tipo"=>"select_simple", "id"=>"fncr", "name"=>"fncr", 
														   							"valor"=>$Fila["flag_nc"], 
																					"combo"=>array("sql"=>$sqlfla, "ancho"=>"100", "sincod"=>"SI"))
																			  )
														   )
										   )
								   	  ),
						"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()"),
									)
						);
		$sql1 = "select E.id_empresa, E.nombre,
				 (select formato from _sv_formato_impresion F where F.id_empresa = E.id_empresa and F.id_tipo_doc = $varCod) AS formato
				 from _sv_empresa E
				 where E._estado = 'A';";
		$this->AbrirConexion();
		$rs1 = $this->Query($sql1);
		//$this->CerrarConexion();
		$varExiste = $this->NumFilas($rs1);
		
		$varTab_1=array("id"=>"tab1", "name"=>"tab1", "tamano"=>"600", "clase"=>"FormTitulo", "titulo"=>"FORMATO DE IMPRESION POR EMPRESA", "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", "alinear"=>"center", "id"=>"emp", "name"=>"emp", 
											 				"clase"=>"inputDisable", "locked"=>"SI", "ancho"=>"70")
											),
									   array("titulo"=>"NOMBRE EMPRESA", "clase"=>"FormTitulo", "tamano"=>"300", 
											 "campo"=>array("tipo"=>"simple", "alinear"=>"center")
											),
									   array("titulo"=>"FORMATO IMPRESION", "clase"=>"FormTitulo", "tamano"=>"40", "alinear"=>"center", 
											 "campo"=>array("tipo"=>"texto", "id"=>"for", "name"=>"for")
											)							   
									   )
						);						
		$varTabs =array(array("name"=>"Asignacion de Formatos de Impresion por Empresa",
							  "tipo"=>"det",  "alto"=>"300", "ancho"=>"620", "tabla"=>$varTab_1
							  )
						);
		$varMyTable = $this->setTablaFormTabs($varFormu, $varTabla, $varTabs);
		
		return $varMyTable;	
		}
		
	function ProcesoGrabar($varForm = NULL) {		
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varCod = ($varForm["cod"]=="")?"NULL":$varForm["cod"];
		$varSun = ($varForm["sun"]=="")?"NULL":"'".$varForm["sun"]."'";
		$varIte = ($varForm["ite"]=="")?"NULL":$varForm["ite"];
		
		$varFVen = ($varForm["fven"]=="")?"NULL":"'".$varForm["fven"]."'";
		$varFCam = ($varForm["fcam"]=="")?"NULL":"'".$varForm["fcam"]."'";
		$varFPed = ($varForm["fped"]=="")?"NULL":"'".$varForm["fped"]."'";
		$varFNcr = ($varForm["fncr"]=="")?"NULL":"'".$varForm["fncr"]."'";
		
		$sql = "call spMante_Tipo_Doc_Venta('$varForm[opt]', $varCod, '$varForm[nam]','$varForm[abr]', $varSun, $varIte, 
											$varFVen, $varFCam, $varFPed, $varFNcr, $varUsr);";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$Fila = $this->FetchArray($rs);
		
		// 1. FORMATOS DE IMPRESION
		$sql = "DELETE FROM _sv_formato_impresion WHERE id_tipo_doc = $varCod;";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();	
		$sqlcab = "insert into _sv_formato_impresion(id_empresa, id_tipo_doc, formato, _date_create, _date_update, _user_create, _user_update) 
				   values ";
		$sqldet = "";
		foreach ($varForm["for"] as $varIde=>$varFor) {
			$sqldet.= ($sqldet=="")?" ":", ";
			$varEmp = $varForm["emp"][$varIde];
			$sqldet.= "($varEmp, $varCod, '$varFor', NOW(),NOW(),$varUsr,$varUsr)";	
			}
		if ($sqldet<>"") {
			$sqldet.= ";";
			$this->AbrirConexion();
			$this->Query($sqlcab.$sqldet);
			$this->CerrarConexion();
			}
			
		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();
		}
	}
	
?>