--### Zuri Wed 11/14/2018 18:56:55.23 ###  
--### Zuri9.1811.2283 ###  
--### 30 WFForms Report ##############################################################################

/*
*******************    TABELAS    *******************
*/
IF OBJECT_ID('dbo.FFOR_FORMULARIO') IS NULL
	CREATE TABLE [dbo].[FFOR_FORMULARIO] (
		[FOR_N_CODIGO] [int] IDENTITY (1, 1) NOT NULL ,
		[FOR_N_POR_N_CODIGO] [int] NOT NULL ,
		[FOR_N_ARE_N_CODIGO] [int] NOT NULL ,
		[FOR_C_TITULO] [nvarchar] (60)  NOT NULL ,
		[FOR_C_APELIDO] [varchar] (30)  NOT NULL ,
		[FOR_N_VINCULA_USUARIO] [smallint] NOT NULL ,
		[FOR_B_SYSTEMFORM] [bit] NOT NULL 
	) 
GO

IF OBJECT_ID('dbo.FFXM_FORM_MODULO_INFO') IS NULL
	CREATE TABLE [dbo].[FFXM_FORM_MODULO_INFO] (
		[FXM_N_FOR_N_CODIGO] [int] NOT NULL ,
		[FXM_N_MOI_N_CODIGO] [int] NOT NULL 
	) 
GO

IF OBJECT_ID('dbo.FFXW_FORMULARIO_WORKFLOW') IS NULL
	CREATE TABLE [dbo].[FFXW_FORMULARIO_WORKFLOW] (
		FXW_N_ID INT IDENTITY,
		[FXW_N_NODE_ID] [int] NOT NULL ,
		[FXW_C_FOR_C_APELIDO] [varchar] (30)  NOT NULL ,
		[FXW_N_MULTIPLO] [smallint] NOT NULL ,
		[FXW_C_ACESSO_ANEXO] [varchar] (100)  NULL ,
		[FXW_C_DADOS] [text]  NULL ,
		[FXW_C_DIV] [varchar] (4000) NULL ,
		[FXW_C_DIVEDIT] [varchar] (4000) NULL,
		CONSTRAINT PK_FFXW PRIMARY KEY (FXW_N_ID)
	)  
GO

IF OBJECT_ID('dbo.FMOD_MODULO') IS NULL
	CREATE TABLE [dbo].[FMOD_MODULO] (
		[MOD_N_CODIGO] [int] IDENTITY (1, 1) NOT NULL ,
		[MOD_C_OWNERID] [uniqueidentifier] NULL ,
		[MOD_C_ITEMID] [uniqueidentifier] NOT NULL ,
		[MOD_C_TITULO] [varchar] (40)  NOT NULL ,
		[MOD_C_NOME_CONTROLE] [varchar] (15)  NOT NULL ,
		[MOD_C_TIPO_CONTROLE] [varchar] (100)  NOT NULL ,
		[MOD_C_ICONE] [varchar] (150)  NOT NULL ,
		[MOD_C_IMAGEM_CONTROLE] [varchar] (150)  NOT NULL ,
		[MOD_C_CATEGORIA] [varchar] (40)  NOT NULL 
	) 
GO

IF OBJECT_ID('dbo.FFXI_FORMULARIO_ITEM_PROCESSO') IS NULL
	CREATE TABLE [dbo].[FFXI_FORMULARIO_ITEM_PROCESSO] (
		[FXI_N_ITE_N_CODIGO] [int] NOT NULL ,
		[FXI_N_FOR_N_CODAUTOMATICO] [int] NOT NULL ,
		[FXI_C_FOR_C_APELIDO] [varchar] (30)  NOT NULL ,
		[FXI_C_FOR_C_GUID] [varchar] (36) NOT NULL,
		FXI_ISP_N_ID	INT,
		CONSTRAINT [PK_FFXI_FORMULARIO_ITEM_PROCESSO] PRIMARY KEY  CLUSTERED 
		(
			[FXI_N_ITE_N_CODIGO],
			[FXI_N_FOR_N_CODAUTOMATICO],
			[FXI_C_FOR_C_APELIDO],
			[FXI_C_FOR_C_GUID]
		)   
	) 
GO

IF OBJECT_ID('dbo.FFCW_FORMCHILDWORKFLOW') IS NULL
CREATE TABLE [dbo].[FFCW_FORMCHILDWORKFLOW](
	FCW_ATV_N_CODIGO		INT NOT NULL,
	FCW_C_ATTACHALIAS		NVARCHAR(50),
	FCW_B_ISPARENTMULTIPLE	BIT,
	FCW_C_CHILDALIAS		NVARCHAR(150),
	FCW_C_PARENTALIAS		NVARCHAR(150),
	FCW_N_PARENTACTIVITYID	INT,
	FCW_C_PARENTCONTROL		NVARCHAR(50),
	FCW_C_ACCESSLIST		NVARCHAR(300),
	CONSTRAINT PK_FCW_ATV_N_CODIGO PRIMARY KEY (FCW_ATV_N_CODIGO)
)

/*
SCRIPT DE CRIAÇÃO DA STRUTURA DE BANCO DO WFREPORTS
Este script pode ser executado mais de uma vez pois ele só cria as tabelas se ainda não existirem.
*/
--###########################################################################################################
IF OBJECT_ID('dbo.RREP_REPORTS') IS NULL
	CREATE TABLE dbo.RREP_REPORTS (
		REP_N_ID INT IDENTITY,
		REP_N_MOI_N_CODIGO int NOT NULL ,
		REP_N_FOR_N_CODIGO int NULL ,
		REP_C_TITULO nvarchar (100)  NULL ,
		REP_N_PAGECOUNT int NULL ,
		REP_N_TIPO char (2)  NOT NULL ,
		REP_C_PARAMETROS varchar (2000)  NULL ,
		REP_C_PROCEDURE varchar (200)  NULL ,
		REP_C_LISTAEDICAO varchar (200)  NOT NULL ,
		REP_C_LISTAEXCLUSAO varchar (200)  NOT NULL ,
		REP_C_CONNECTION varchar (400)  NULL , 
		REP_C_TITULOGRAFICO	nvarchar (100)  NULL ,
		REP_N_VISUALIZACAOGRAFICO INT NULL ,
		REP_N_LARGURAGRAFICO INT NULL , 
		REP_N_ALTURAGRAFICO INT NULL,
		REP_B_MOSTRARVALORESGRAFICO BIT NULL ,
		REP_C_CAMPONOMEGRAFICO VARCHAR(200) NULL ,
		REP_C_CAMPOVALORGRAFICO VARCHAR(200) NULL ,
		REP_C_POSICAOLEGENDAGRAFICO INT NULL ,
		REP_C_FUNCAOAGRUPAMENTOGRAFICO INT NULL , 
		REP_C_CAMPOAGRUPAMENTO VARCHAR(300) NULL,
		REP_B_VISUALIZAGRAFICO BIT NOT NULL DEFAULT 0,
		CONSTRAINT PK_RREP PRIMARY KEY (REP_N_ID)
	)
GO

ALTER TABLE RREP_REPORTS
ALTER COLUMN REP_C_TITULO VARCHAR(200)
GO

ALTER TABLE RREP_REPORTS
ALTER COLUMN REP_C_PARAMETROS VARCHAR(2000)
GO

ALTER TABLE RREP_REPORTS
ALTER COLUMN REP_C_PROCEDURE VARCHAR(200)
GO

ALTER TABLE RREP_REPORTS
ALTER COLUMN REP_C_CONNECTION VARCHAR(400)
GO

ALTER TABLE RREP_REPORTS
ALTER COLUMN REP_C_LISTAEDICAO VARCHAR(200)
GO

ALTER TABLE RREP_REPORTS
ALTER COLUMN REP_C_LISTAEXCLUSAO VARCHAR(200)
GO

ALTER TABLE RREP_REPORTS
ALTER COLUMN REP_C_CONNECTION VARCHAR(300)
GO

ALTER TABLE RREP_REPORTS
ALTER COLUMN REP_C_CAMPOAGRUPAMENTO VARCHAR(300)
GO

--###########################################################################################################
IF OBJECT_ID('dbo.RCAM_CAMPOS') IS NULL
	CREATE TABLE dbo.RCAM_CAMPOS (
		CAM_N_CODIGO int IDENTITY (1, 1) NOT NULL ,
		CAM_N_REP_N_CODIGO int NOT NULL ,
		CAM_C_ALIAS nvarchar (50)  NULL ,
		CAM_C_ACESSO varchar (10)  NULL ,
		CAM_N_POSICAO int NOT NULL ,
		CAM_N_WIDTH int NOT NULL ,
		CAM_C_APELIDO varchar (30)  NULL ,
		CAM_N_SEARCH_RANGE smallint NOT NULL ,
		CAM_N_SEARCH_BUSCA smallint NOT NULL ,
		CAM_N_SEARCH_FILTRO smallint NOT NULL ,
		CAM_N_VISIBLE smallint NOT NULL ,
		CAM_N_ORDEM int NULL,
		CONSTRAINT PK_RCAM PRIMARY KEY (CAM_N_CODIGO)
	)
GO


--###########################################################################################################
IF OBJECT_ID('dbo.RFIL_FILTROS') IS NULL
	CREATE TABLE dbo.RFIL_FILTROS (
		FIL_N_CODIGO int IDENTITY (1, 1) NOT NULL ,
		FIL_C_CAM_C_ALIAS nvarchar (50)  NOT NULL ,
		FIL_N_REP_N_CODIGO int NOT NULL ,
		FIL_C_OPERACAO char (2)  NOT NULL ,
		FIL_C_VALOR_COMPARACAO varchar (100)  NOT NULL ,
		FIL_C_TIPO_COMPARACAO char (10)  NOT NULL,
		CONSTRAINT PK_RFIL PRIMARY KEY (FIL_N_CODIGO)
	)
GO

--###########################################################################################################
IF OBJECT_ID('dbo.RFIP_FILTROS_PROCEDURE') IS NULL
	CREATE TABLE dbo.RFIP_FILTROS_PROCEDURE (
		FIP_N_ID INT IDENTITY,
		FIP_N_MOI_N_CODIGO int NOT NULL,
		FIP_C_PARAMETRO varchar(50) NOT NULL,
		FIP_C_PARAMETRO_VALOR varchar(100) NOT NULL,
		FIP_C_PARAMETRO_TIPO varchar(50) NOT NULL,
		FIP_C_TITULO varchar(50) NOT NULL,
		FIP_B_FILTRO bit NOT NULL,
		FIP_B_NULO bit NOT NULL,
		CONSTRAINT PK_RFIP PRIMARY KEY (FIP_N_ID)
	)
GO

/*
*******************    CONSTRAINTS    *******************
*/

IF OBJECT_ID('dbo.PK_FFOR_FORMULARIO') IS NULL
	ALTER TABLE [dbo].[FFOR_FORMULARIO] WITH NOCHECK ADD 
		CONSTRAINT [PK_FFOR_FORMULARIO] PRIMARY KEY  CLUSTERED 
		(
			[FOR_N_CODIGO]
		)   
GO

IF OBJECT_ID('dbo.PK_FFXM_FORM_MODULO_INFO') IS NULL
	ALTER TABLE [dbo].[FFXM_FORM_MODULO_INFO] WITH NOCHECK ADD 
		CONSTRAINT [PK_FFXM_FORM_MODULO_INFO] PRIMARY KEY  CLUSTERED 
		(
			[FXM_N_FOR_N_CODIGO],
			[FXM_N_MOI_N_CODIGO]
		)   
GO

IF OBJECT_ID('dbo.PK_FMOD_MODULO') IS NULL
	ALTER TABLE [dbo].[FMOD_MODULO] WITH NOCHECK ADD 
		CONSTRAINT [PK_FMOD_MODULO] PRIMARY KEY  CLUSTERED 
		(
			[MOD_N_CODIGO]
		)   
GO

IF OBJECT_ID('dbo.DF_FFOR_FORMULARIO_FOR_B_SYSTEMFORM') IS NULL
	ALTER TABLE [dbo].[FFOR_FORMULARIO] WITH NOCHECK ADD 
		CONSTRAINT [DF_FFOR_FORMULARIO_FOR_B_SYSTEMFORM] DEFAULT (0) FOR [FOR_B_SYSTEMFORM]
GO

IF OBJECT_ID('dbo.DF_FMOD_MODULO_MOD_C_ITEMID') IS NULL
	ALTER TABLE [dbo].[FMOD_MODULO] WITH NOCHECK ADD 
		CONSTRAINT [DF_FMOD_MODULO_MOD_C_ITEMID] DEFAULT (newid()) FOR [MOD_C_ITEMID]
GO

IF OBJECT_ID('dbo.DF_FMOD_MODULO_MOD_C_NOME_CONTROLE') IS NULL
	ALTER TABLE [dbo].[FMOD_MODULO] WITH NOCHECK ADD 
		CONSTRAINT [DF_FMOD_MODULO_MOD_C_NOME_CONTROLE] DEFAULT ('') FOR [MOD_C_NOME_CONTROLE]
GO

IF OBJECT_ID('dbo.DF_FMOD_MODULO_MOD_C_CATEGORIA') IS NULL
	ALTER TABLE [dbo].[FMOD_MODULO] WITH NOCHECK ADD 
		CONSTRAINT [DF_FMOD_MODULO_MOD_C_CATEGORIA] DEFAULT ('') FOR [MOD_C_CATEGORIA]
GO

IF OBJECT_ID('dbo.FK_FFXM_FORM_MODULO_INFO_FFOR_FORMULARIO') IS NULL
	ALTER TABLE [dbo].[FFXM_FORM_MODULO_INFO] ADD 
		CONSTRAINT [FK_FFXM_FORM_MODULO_INFO_FFOR_FORMULARIO] FOREIGN KEY 
		(
			[FXM_N_FOR_N_CODIGO]
		) REFERENCES [dbo].[FFOR_FORMULARIO] (
			[FOR_N_CODIGO]
		) ON DELETE CASCADE 
GO

--###########################################################################################################
-- Adiciona os campos de configuração do relatório para exibição de gráficos

if not exists(select * from syscolumns where id = object_id('RREP_REPORTS') and name = 'REP_C_TITULOGRAFICO')
	ALTER TABLE RREP_REPORTS ADD REP_C_TITULOGRAFICO	nvarchar (100)  NULL ,
								 REP_N_VISUALIZACAOGRAFICO INT NULL ,
								 REP_N_LARGURAGRAFICO INT NULL , 
								 REP_N_ALTURAGRAFICO INT NULL,
								 REP_B_MOSTRARVALORESGRAFICO BIT NULL ,
								 REP_C_CAMPONOMEGRAFICO VARCHAR(200) NULL ,
								 REP_C_CAMPOVALORGRAFICO VARCHAR(200) NULL ,
								 REP_C_POSICAOLEGENDAGRAFICO INT NULL ,
								 REP_C_FUNCAOAGRUPAMENTOGRAFICO INT NULL , 
								 REP_C_CAMPOAGRUPAMENTO VARCHAR(100) NULL,
								 REP_B_VISUALIZAGRAFICO BIT NOT NULL DEFAULT 'FALSE'
	
GO

if not exists(select * from syscolumns where id = object_id('RREP_REPORTS') and name = 'REP_B_VISUALIZAGRAFICO')
	ALTER TABLE RREP_REPORTS ADD REP_B_VISUALIZAGRAFICO BIT NOT NULL DEFAULT 'FALSE'
GO

/*
*******************    ALTER TABLES    *******************
*/

IF NOT OBJECT_ID('dbo.FFXW_FORMULARIO_WORKFLOW') IS NULL
	ALTER TABLE [dbo].FFXW_FORMULARIO_WORKFLOW ALTER COLUMN [FXW_C_DIV] [varchar] (4000) NULL
GO

IF NOT OBJECT_ID('dbo.FFXW_FORMULARIO_WORKFLOW') IS NULL
	ALTER TABLE [dbo].FFXW_FORMULARIO_WORKFLOW ALTER COLUMN [FXW_C_DIVEDIT] [varchar] (4000) NULL
GO

ALTER TABLE [dbo].FFOR_FORMULARIO ALTER COLUMN [FOR_C_TITULO] [varchar] (60) NULL


IF NOT EXISTS (SELECT 1 FROM SYS.COLUMNS WHERE NAME = 'FOR_D_PUBLICACAO' AND OBJECT_ID = OBJECT_ID('FFOR_FORMULARIO'))
BEGIN
	ALTER TABLE FFOR_FORMULARIO ADD  FOR_D_PUBLICACAO DATETIME NULL
	ALTER TABLE FFOR_FORMULARIO ADD  FOR_N_USU_PUBLICADOR INT NULL
END
GO

IF NOT EXISTS (SELECT 1 FROM SYS.COLUMNS WHERE NAME = 'FXI_ISP_N_ID' AND OBJECT_ID = OBJECT_ID('FFXI_FORMULARIO_ITEM_PROCESSO'))
BEGIN
	ALTER TABLE FFXI_FORMULARIO_ITEM_PROCESSO ADD FXI_ISP_N_ID INT
END
GO

ALTER TABLE FFOR_FORMULARIO ALTER COLUMN FOR_C_APELIDO VARCHAR(100) NOT NULL

/*
*******************    PROCEDURES    *******************
*/

IF NOT OBJECT_ID('dbo.frm_AddFormModuloInfo') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_AddFormModuloInfo
END
GO

CREATE PROCEDURE dbo.frm_AddFormModuloInfo
(
	@IN_FXM_N_MOI_N_CODIGO    int,
	@IN_FXM_N_FOR_N_CODIGO    int
)
AS
SET NOCOUNT ON
IF (SELECT COUNT(1) FROM FFXM_FORM_MODULO_INFO WHERE FXM_N_MOI_N_CODIGO = @IN_FXM_N_MOI_N_CODIGO) > 0
BEGIN
	UPDATE FFXM_FORM_MODULO_INFO
	SET FXM_N_FOR_N_CODIGO = @IN_FXM_N_FOR_N_CODIGO
	WHERE FXM_N_MOI_N_CODIGO = @IN_FXM_N_MOI_N_CODIGO
END
ELSE
BEGIN
	INSERT INTO FFXM_FORM_MODULO_INFO
	(
		FXM_N_MOI_N_CODIGO,
		FXM_N_FOR_N_CODIGO
	) VALUES (
		@IN_FXM_N_MOI_N_CODIGO,
		@IN_FXM_N_FOR_N_CODIGO)
END
GO

IF NOT OBJECT_ID('dbo.frm_AddFormulario') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_AddFormulario
END
GO
CREATE PROCEDURE dbo.frm_AddFormulario
(
	@IN_N_PORTAL    	INT,
	@IN_N_AREA    	INT,
	@IN_C_TITULO    	NVARCHAR(30),
	@IN_C_APELIDO	NVARCHAR(30),
	@IN_N_VINCULA    	SMALLINT
)	
AS
SET NOCOUNT ON
INSERT INTO FFOR_FORMULARIO
(
	FOR_N_POR_N_CODIGO,
	FOR_N_ARE_N_CODIGO,
	FOR_C_TITULO,
	FOR_C_APELIDO,
	FOR_N_VINCULA_USUARIO
) VALUES (
   @IN_N_PORTAL, 
   @IN_N_AREA,
	@IN_C_TITULO,
	@IN_C_APELIDO,
	@IN_N_VINCULA)
SELECT @@IDENTITY
GO

IF NOT OBJECT_ID('dbo.frm_DelCampoTabela') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_DelCampoTabela
END
GO

CREATE PROCEDURE dbo.frm_DelCampoTabela 
(
	@IN_NOMECAMPO VARCHAR(30),
	@IN_NOMETABELA VARCHAR(30)
)
AS
DECLARE @SQL NVARCHAR(500)
SET @SQL = 'ALTER TABLE ' + @IN_NOMETABELA + ' DROP CONSTRAINT ' + (SELECT SYSOBJECTS.NAME FROM SYSCOLUMNS INNER JOIN SYSOBJECTS ON SYSCOLUMNS.CDEFAULT = SYSOBJECTS.ID WHERE SYSCOLUMNS.ID = (SELECT ID FROM SYSOBJECTS WHERE NAME = @IN_NOMETABELA) AND SYSCOLUMNS.NAME = @IN_NOMECAMPO)
SET @SQL = @SQL + ' ALTER TABLE ' + @IN_NOMETABELA + ' DROP COLUMN ' + @IN_NOMECAMPO
EXECUTE sp_ExecuteSQL @SQL
GO

IF NOT OBJECT_ID('dbo.frm_DelFormulario') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_DelFormulario
END
GO

CREATE PROCEDURE dbo.frm_DelFormulario
(
	@IN_N_FORM_ID	INT
)
AS
	DECLARE @PROC_SAVE 	NVARCHAR(50),
	@PROC_DEL  		NVARCHAR(50),
	@PROC_LIST  		NVARCHAR(50),
	@PROC_GET		NVARCHAR(50),
	@PROC_GETPARENT	NVARCHAR(50),
	@APELIDO		NVARCHAR(30),
	@EXECSQL		NVARCHAR(1000)
	SET @APELIDO = (SELECT FOR_C_APELIDO FROM FFOR_FORMULARIO WHERE FOR_N_CODIGO = @IN_N_FORM_ID)	
	SET @PROC_SAVE 		=	'fd_Save_'		+ @APELIDO		
	SET @PROC_DEL 		=	'fd_Del_'		+ @APELIDO	
	SET @PROC_LIST 		= 	'fd_List_'		+ @APELIDO	
	SET @PROC_GET 		= 	'fd_Get_'		+ @APELIDO	
	SET @PROC_GETPARENT 	= 	'fd_GetByParent_'	+ @APELIDO	
	DELETE FROM FFOR_FORMULARIO WHERE FOR_N_CODIGO = @IN_N_FORM_ID
	if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[' + @APELIDO + ']') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
	BEGIN		
		SET @EXECSQL = 'drop table [dbo].[' + @APELIDO + ']'
		EXECUTE sp_executesql @EXECSQL
	END
	if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[' + @PROC_SAVE + ']') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	BEGIN
		SET @EXECSQL = 'drop procedure [dbo].[' + @PROC_SAVE + ']'
		EXECUTE sp_executesql @EXECSQL
	END
	if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[' + @PROC_DEL + ']') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	BEGIN
		SET @EXECSQL = 'drop procedure [dbo].[' + @PROC_DEL + ']'
		EXECUTE sp_executesql @EXECSQL
	END
	if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[' + @PROC_LIST + ']') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	BEGIN
		SET @EXECSQL = 'drop procedure [dbo].[' + @PROC_LIST + ']'	
		EXECUTE sp_executesql @EXECSQL	
	END
	if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[' + @PROC_GET + ']') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	BEGIN
		SET @EXECSQL = 'drop procedure [dbo].[' + @PROC_GET + ']'
		EXECUTE sp_executesql @EXECSQL
	END
	
	if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[' + @PROC_GETPARENT + ']') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	BEGIN
		SET @EXECSQL = 'drop procedure [dbo].[' + @PROC_GETPARENT + ']'
		EXECUTE sp_executesql @EXECSQL
	END

GO

IF NOT OBJECT_ID('dbo.frm_DropTable') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_DropTable
END
GO

CREATE PROCEDURE dbo.frm_DropTable
(
	@IN_C_APELIDO	NVARCHAR(30)
)
 AS
DECLARE @SQL	NVARCHAR(200)
SET @SQL =  'DROP TABLE ' +  @IN_C_APELIDO
EXECUTE sp_executeSQL @SQL

GO

IF NOT OBJECT_ID('dbo.frm_ExisteApelido') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_ExisteApelido
END
GO

CREATE PROCEDURE dbo.frm_ExisteApelido
(
	@IN_C_APELIDO	NVARCHAR(30) 
)
AS
DECLARE @COD INT
SELECT @COD = id FROM SYSOBJECTS WHERE ID= OBJECT_ID(@IN_C_APELIDO)
 
IF NOT @COD IS NULL
BEGIN
	SELECT  1
END
ELSE
BEGIN
	SELECT 0
END

GO

IF NOT OBJECT_ID('dbo.frm_ExisteCampo') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_ExisteCampo
END
GO

CREATE PROCEDURE dbo.frm_ExisteCampo (
	@IN_TABELA	NVARCHAR(30),
	@IN_CAMPO	NVARCHAR(30)
)
 AS
	SELECT COUNT(SYSCOLUMNS.NAME) FROM SYSCOLUMNS
	INNER JOIN SYSOBJECTS
	ON SYSCOLUMNS.ID = SYSOBJECTS.ID
	WHERE SYSCOLUMNS.NAME = @IN_CAMPO
	AND SYSOBJECTS.NAME = @IN_TABELA
	AND SYSOBJECTS.TYPE = 'U'

GO

IF NOT OBJECT_ID('dbo.frm_ExisteTitulo') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_ExisteTitulo
END
GO

CREATE PROCEDURE dbo.frm_ExisteTitulo
(
	@IN_N_FORMID	INT = NULL,
	@IN_C_TITULO 	NVARCHAR(30) 
)
AS
DECLARE @COD INT
SELECT @COD = FOR_N_CODIGO FROM FFOR_FORMULARIO WHERE FOR_C_TITULO = @IN_C_TITULO
IF NOT @COD IS NULL
BEGIN
	--SE JA EXISTE
	IF @IN_N_FORMID <> @COD
		SELECT  1
	ELSE
		SELECT 0
END
ELSE
BEGIN
	SELECT 0
END

GO

IF NOT OBJECT_ID('dbo.frm_GetCodigoByUser') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_GetCodigoByUser
END
GO

CREATE PROCEDURE  dbo.frm_GetCodigoByUser
(
	@IN_N_USU_N_CODIGO int,
	@IN_C_FORM_APELIDO VARCHAR(30),
	@IN_C_CODIGOAUTOMATICO VARCHAR(30),
	@IN_C_CODIGOUSUARIO VARCHAR(30),
	@IN_C_GUID VARCHAR(30)
)
AS
DECLARE @SQL NVARCHAR(500)
SET @SQL = ' SELECT CONVERT(VARCHAR, ' + @IN_C_CODIGOAUTOMATICO + ') + '';'' + ' + @IN_C_GUID + ' FROM ' + @IN_C_FORM_APELIDO + '  WHERE ' + @IN_C_CODIGOUSUARIO + ' = ' + CONVERT(VARCHAR, @IN_N_USU_N_CODIGO)
EXECUTE sp_executeSQL @SQL

GO

IF NOT OBJECT_ID('dbo.frm_GetFormModuloInfo') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_GetFormModuloInfo
END
GO

CREATE PROCEDURE  dbo.frm_GetFormModuloInfo
(
	@IN_FXM_N_MOI_N_CODIGO  int
)
AS
SET NOCOUNT ON
SELECT 
	FXM_N_FOR_N_CODIGO
 FROM FFXM_FORM_MODULO_INFO
 WHERE 
		FXM_N_MOI_N_CODIGO = @IN_FXM_N_MOI_N_CODIGO
SET NOCOUNT OFF

GO

IF NOT OBJECT_ID('dbo.frm_GetFormulario') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_GetFormulario
END
GO

CREATE PROCEDURE dbo.frm_GetFormulario
(
	@IN_N_FORM_ID	INT
)
AS
SELECT FOR_N_CODIGO, 
	FOR_N_POR_N_CODIGO, 
	FOR_N_ARE_N_CODIGO,
	FOR_C_TITULO,
	FOR_C_APELIDO,
	FOR_N_VINCULA_USUARIO
FROM
	FFOR_FORMULARIO
WHERE
	FOR_N_CODIGO = @IN_N_FORM_ID

GO

IF NOT OBJECT_ID('dbo.frm_GetFormulario_Apelido') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_GetFormulario_Apelido
END
GO

CREATE PROCEDURE dbo.frm_GetFormulario_Apelido
(
	@IN_C_FORM_APELIDO	NVARCHAR(30)
)
AS
SELECT FOR_N_CODIGO, 
	FOR_N_POR_N_CODIGO, 
	FOR_N_ARE_N_CODIGO,
	FOR_C_TITULO,
	FOR_C_APELIDO,
	FOR_N_VINCULA_USUARIO
FROM
	FFOR_FORMULARIO
WHERE
	FOR_C_APELIDO = @IN_C_FORM_APELIDO

GO

IF NOT OBJECT_ID('dbo.frm_GetModuloItemID') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_GetModuloItemID
END
GO

CREATE PROCEDURE dbo.frm_GetModuloItemID(
	@IN_ITEMID char(36),
	@OUT_MOD_N_CODIGO int OUTPUT,
	@OUT_MOD_C_OWNERID char(36) OUTPUT,
	@OUT_MOD_C_ITEMID char(36) OUTPUT,
	@OUT_MOD_C_TITULO varchar(40) OUTPUT,
	@OUT_MOD_C_NOME_CONTROLE varchar(15) OUTPUT,
	@OUT_MOD_C_TIPO_CONTROLE varchar(100) OUTPUT,
	@OUT_MOD_C_ICONE varchar(150) OUTPUT,
	@OUT_MOD_C_IMAGEM_CONTROLE varchar(150) OUTPUT,
	@OUT_MOD_C_CATEGORIA varchar(40) OUTPUT
)
 AS
	SET NOCOUNT ON
	SELECT 
		@OUT_MOD_N_CODIGO = MOD_N_CODIGO,
		@OUT_MOD_C_OWNERID = MOD_C_OWNERID,
		@OUT_MOD_C_ITEMID = MOD_C_ITEMID,
		@OUT_MOD_C_TITULO = MOD_C_TITULO,
		@OUT_MOD_C_NOME_CONTROLE = MOD_C_NOME_CONTROLE,
		@OUT_MOD_C_TIPO_CONTROLE = MOD_C_TIPO_CONTROLE,
		@OUT_MOD_C_ICONE = MOD_C_ICONE,
		@OUT_MOD_C_IMAGEM_CONTROLE = MOD_C_IMAGEM_CONTROLE,
		@OUT_MOD_C_CATEGORIA = MOD_C_CATEGORIA
	 FROM 
		FMOD_MODULO
	 WHERE 
		LOWER(ISNULL(CONVERT(Char(36), MOD_C_ITEMID), '')) = LOWER(@IN_ITEMID)
	 SET NOCOUNT OFF

GO

IF NOT OBJECT_ID('dbo.frm_ListFormularios') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_ListFormularios
END
GO

CREATE PROCEDURE dbo.frm_ListFormularios
(
	@IN_N_ARE_ID INT = null,
	@IN_N_POR_ID INT = null
)
AS
IF @IN_N_ARE_ID IS NULL AND @IN_N_POR_ID IS NULL
	SELECT FOR_N_CODIGO, FOR_C_TITULO, FOR_N_POR_N_CODIGO, FOR_N_ARE_N_CODIGO, FOR_C_APELIDO, FOR_B_SYSTEMFORM FROM FFOR_FORMULARIO ORDER BY FOR_C_TITULO
ELSE
	IF @IN_N_POR_ID IS NOT NULL
		SELECT FOR_N_CODIGO, FOR_C_TITULO, FOR_N_POR_N_CODIGO, FOR_N_ARE_N_CODIGO, FOR_C_APELIDO, FOR_B_SYSTEMFORM FROM FFOR_FORMULARIO WHERE FOR_N_POR_N_CODIGO = @IN_N_POR_ID ORDER BY FOR_C_TITULO
	ELSE
		SELECT FOR_N_CODIGO, FOR_C_TITULO, FOR_N_POR_N_CODIGO, FOR_N_ARE_N_CODIGO, FOR_C_APELIDO, FOR_B_SYSTEMFORM FROM FFOR_FORMULARIO WHERE FOR_N_ARE_N_CODIGO = @IN_N_ARE_ID ORDER BY FOR_C_TITULO

GO

IF NOT OBJECT_ID('dbo.frm_ListModulo') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_ListModulo
END
GO

CREATE PROCEDURE dbo.frm_ListModulo(
	@IN_CATEGORIA varchar(40),
	@IN_TODOS bit = 0
)
 AS
IF  @IN_CATEGORIA = ''
	IF @IN_TODOS = 1
		SELECT 
			MOD_N_CODIGO,
			MOD_C_OWNERID,
			MOD_C_ITEMID,
			MOD_C_TITULO,
			MOD_C_NOME_CONTROLE,
			MOD_C_TIPO_CONTROLE,
			MOD_C_ICONE,
			MOD_C_IMAGEM_CONTROLE,
			MOD_C_CATEGORIA
		 FROM 
			FMOD_MODULO
		 WHERE 
			ISNULL(MOD_C_CATEGORIA, '') <> ''
		 ORDER BY
			MOD_C_CATEGORIA
	ELSE
		SELECT 
			MOD_N_CODIGO,
			MOD_C_OWNERID,
			MOD_C_ITEMID,
			MOD_C_TITULO,
			MOD_C_NOME_CONTROLE,
			MOD_C_TIPO_CONTROLE,
			MOD_C_ICONE,
			MOD_C_IMAGEM_CONTROLE,
			MOD_C_CATEGORIA
		 FROM 
			FMOD_MODULO
		 WHERE 
			ISNULL(MOD_C_CATEGORIA, '') <> '' AND
			MOD_C_OWNERID IS NULL OR 
			MOD_C_OWNERID <> '3CFC8AF3-C07E-4F14-B398-4A8917D97C92'
		 ORDER BY
			MOD_C_TITULO
ELSE
	SELECT 
		MOD_N_CODIGO,
		MOD_C_OWNERID,
		MOD_C_ITEMID,
		MOD_C_TITULO,
		MOD_C_NOME_CONTROLE,
		MOD_C_TIPO_CONTROLE,
		MOD_C_ICONE,
		MOD_C_IMAGEM_CONTROLE,
		MOD_C_CATEGORIA
	 FROM 
		FMOD_MODULO
	 WHERE 
		ISNULL(MOD_C_CATEGORIA, '') <> '' AND
		MOD_C_CATEGORIA = @IN_CATEGORIA 
	 ORDER BY
		MOD_C_CATEGORIA, 
		MOD_C_TITULO

GO

IF NOT OBJECT_ID('dbo.frm_ListParametrosProcedure') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_ListParametrosProcedure
END
GO

CREATE PROCEDURE dbo.frm_ListParametrosProcedure
	@objname nvarchar(776) = NULL  -- object name we're after
as
 -- PRELIMINARY
 set nocount on
 declare @dbname sysname
 
 -- OBTAIN DISPLAY STRINGS FROM spt_values UP FRONT --
 declare @no varchar(35), @yes varchar(35), @none varchar(35)
 set @no = 'no'--name from master.dbo.spt_values where type = 'B' and number = 0
 set @yes = 'yes' --name from master.dbo.spt_values where type = 'B' and number = 1
 set @none = 'none'--name from master.dbo.spt_values where type = 'B' and number = 2
 
 -- Make sure the @objname is local to the current database.
 select @dbname = parsename(@objname,3)
 
 if @dbname is not null and @dbname <> db_name()
  begin
   raiserror(15250,-1,-1)
   return(1)
  end
 
 -- @objname must be either sysobjects or systypes: first look in sysobjects
 declare @objid int
 declare @sysobj_type char(2)
 select @objid = id, @sysobj_type = xtype from sysobjects where id = object_id(@objname)
 
 -- IF NOT IN SYSOBJECTS, TRY SYSTYPES --
 if @objid is null
 begin
  -- UNDONE: SHOULD CHECK FOR AND DISALLOW MULTI-PART NAME
  select @objid = xusertype from systypes where name = @objname
 
  -- IF NOT IN SYSTYPES, GIVE UP
  if @objid is null
  begin
   select @dbname=db_name()
   raiserror(15009,-1,-1,@objname,@dbname)
   return(1)
  end
 
  -- DATA TYPE HELP (prec/scale only valid for numerics)
  select
   Type_name  = name,
   Storage_type = type_name(xtype),
   Length  = length,
   Prec   = TypeProperty(name, 'precision'),
   Scale   = TypeProperty(name, 'scale'),
   Nullable  = case when allownulls=1 then @yes else @no end,
   Default_name = isnull(object_name(tdefault), @none),
   Rule_name  = isnull(object_name(domain), @none),
   Collation  = collation
  from systypes
  where xusertype = @objid
 
  return(0)
 end
 
 -- DISPLAY COLUMN IF TABLE / VIEW
 if @sysobj_type in ('S ','U ','V ','TF','IF')
 begin
 
  -- SET UP NUMERIC TYPES: THESE WILL HAVE NON-BLANK PREC/SCALE
  declare @numtypes nvarchar(80)
  select @numtypes = N'tinyint,smallint,decimal,int,real,money,float,numeric,smallmoney'
 
  select
   Column_name   = name,
   Type     = type_name(xusertype),
   Length    = convert(int, length),
   Prec     = case when charindex(type_name(xtype), @numtypes) > 0
		  then convert(char(5),ColumnProperty(id, name, 'precision'))
		  else '     ' end,
   Scale     = case when charindex(type_name(xtype), @numtypes) > 0
		 then convert(char(5),OdbcScale(xtype,xscale))
		 else '     ' end,
   Nullable    = case when isnullable = 0 then @no else @yes end,
   Collation  = collation
  from syscolumns where id = @objid and number = 0 order by colid
 
  -- IDENTITY COLUMN?
  if @sysobj_type in ('S ','U ','V ','TF')
  begin
   declare @colname sysname
   select @colname = name from syscolumns where id = @objid
	  and colstat & 1 = 1
   select
	[Identity]    = isnull(@colname,'No identity column defined.'),
	Seed     = ident_seed(@objname),
	Increment    = ident_incr(@objname),
	Not_For_Replication = ColumnProperty(@objid, @colname, 'IsIDNotForRepl')
   -- ROWGUIDCOL?
   select @colname = null
   select @colname = name from syscolumns where id = @objid and number = 0
	  and ColumnProperty(@objid, name, 'IsRowGuidCol') = 1
   select [RowGuidCol] = isnull(@colname,'No rowguidcol column defined.')
  end
 end
 
 -- DISPLAY PROC PARAMS
 if @sysobj_type in ('P ') --RF too?
 begin
  -- ANY PARAMS FOR THIS PROC?
  if exists (select id from syscolumns where id = @objid)
  begin
   -- INFO ON PROC PARAMS
   select
	Parameter_name = name,
	[Type]    = type_name(xusertype),
	[Length]   = length,
	Prec    = case when type_name(xtype) = 'uniqueidentifier' then xprec
				   else OdbcPrec(xtype, length, xprec) end,
	Scale    = OdbcScale(xtype,xscale),
	Param_order  = colid,
	Collation  = collation
 
   from syscolumns where id = @objid
  end
 end
 
 -- DISPLAY TABLE INDEXES & CONSTRAINTS
 if @sysobj_type in ('S ','U ')
 begin
  execute sp_objectfilegroup @objid
  execute sp_helpindex @objname
  execute sp_helpconstraint @objname,'nomsg'
  if (select count(*) from sysdepends where depid = @objid and deptype = 1) = 0
  begin
   raiserror(15647,-1,-1) -- 'No views with schemabinding reference this table.'
  end
  else
  begin
			select distinct [Table is referenced by views] = obj.name from sysobjects obj, sysdepends deps
	where obj.xtype ='V' and obj.id = deps.id and deps.depid = @objid
	 and deps.deptype = 1 group by obj.name
 
  end
 end
 else if @sysobj_type in ('V ')
 begin
  -- VIEWS DONT HAVE CONSTRAINTS, BUT PRINT THESE MESSAGES BECAUSE 6.5 DID
  raiserror(15469,-1,-1) -- No constraints defined
  raiserror(15470,-1,-1) --'No foreign keys reference this table.'
  execute sp_helpindex @objname
 end
 
 return (0) -- sp_help

GO

IF NOT OBJECT_ID('dbo.frm_UpdFormulario') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_UpdFormulario
END
GO

CREATE PROCEDURE frm_UpdFormulario(
	@IN_N_FORM_ID		INT,
	@IN_N_PORTAL		INT,
	@IN_N_AREA			INT,
	@IN_C_TITULO		NVARCHAR(60),
	@IN_C_APELIDO		NVARCHAR(30),
	@IN_C_FECHAMENTO	NVARCHAR(400) = NULL,
	@IN_N_VINCULA		SMALLINT,
	@IN_C_URL			NVARCHAR(200) = NULL
)
AS
BEGIN
	UPDATE FFOR_FORMULARIO
	SET
		FOR_N_POR_N_CODIGO 		= @IN_N_PORTAL,
		FOR_N_ARE_N_CODIGO 		= @IN_N_AREA,
		FOR_C_TITULO 			= @IN_C_TITULO,
		FOR_C_APELIDO 			= @IN_C_APELIDO,
		FOR_N_VINCULA_USUARIO 		= @IN_N_VINCULA
	WHERE
		FOR_N_CODIGO = @IN_N_FORM_ID
END
GO

IF NOT OBJECT_ID('dbo.wfm_AddWFFormulario') IS NULL
BEGIN
	DROP PROCEDURE dbo.wfm_AddWFFormulario
END
GO

CREATE PROCEDURE dbo.wfm_AddWFFormulario
(
	@IN_FXW_N_NODE_ID    int,
	@IN_FXW_C_FOR_C_APELIDO    VARCHAR(30),
	@IN_FXW_N_MULTIPLO    smallint,
	@IN_FXW_C_ACESSO_ANEXO    varchar(100),
	@IN_FXW_C_DADOS	text,
	@IN_FXW_C_DIV    varchar(4000),
	@IN_FXW_C_DIVEDIT    varchar(4000)
)
AS
SET NOCOUNT ON
IF (SELECT COUNT(1) FROM FFXW_FORMULARIO_WORKFLOW WHERE FXW_N_NODE_ID = @IN_FXW_N_NODE_ID) > 0
BEGIN
	UPDATE FFXW_FORMULARIO_WORKFLOW
	SET 
		FXW_C_FOR_C_APELIDO = @IN_FXW_C_FOR_C_APELIDO,
		FXW_N_MULTIPLO = @IN_FXW_N_MULTIPLO,
		FXW_C_ACESSO_ANEXO = @IN_FXW_C_ACESSO_ANEXO,
		FXW_C_DADOS = @IN_FXW_C_DADOS,
		FXW_C_DIV = @IN_FXW_C_DIV,
		FXW_C_DIVEDIT = @IN_FXW_C_DIVEDIT
	WHERE FXW_N_NODE_ID = @IN_FXW_N_NODE_ID	
END
ELSE
BEGIN
	INSERT INTO FFXW_FORMULARIO_WORKFLOW
	(
		FXW_N_NODE_ID,
		FXW_C_FOR_C_APELIDO,
		FXW_N_MULTIPLO,
		FXW_C_ACESSO_ANEXO,
		FXW_C_DADOS,
		FXW_C_DIV,
		FXW_C_DIVEDIT
	) VALUES (
		@IN_FXW_N_NODE_ID,
		@IN_FXW_C_FOR_C_APELIDO,
		@IN_FXW_N_MULTIPLO,
		@IN_FXW_C_ACESSO_ANEXO,
			@IN_FXW_C_DADOS,
		@IN_FXW_C_DIV,
		@IN_FXW_C_DIVEDIT)
END
SET NOCOUNT OFF

GO

IF NOT OBJECT_ID('dbo.wfm_DelWFFormulario') IS NULL
BEGIN
	DROP PROCEDURE dbo.wfm_DelWFFormulario
END
GO

CREATE PROCEDURE dbo.wfm_DelWFFormulario
(
	@IN_FXW_N_NODE_ID    int
)
AS
SET NOCOUNT ON
DELETE FROM FFXW_FORMULARIO_WORKFLOW
	WHERE FXW_N_NODE_ID = @IN_FXW_N_NODE_ID
SET NOCOUNT OFF

GO

IF NOT OBJECT_ID('dbo.wfm_GetWFFormulario') IS NULL
BEGIN
	DROP PROCEDURE dbo.wfm_GetWFFormulario
END
GO

CREATE PROCEDURE dbo.wfm_GetWFFormulario
(
	@IN_FXW_N_NODE_ID    int
)
AS
SET NOCOUNT ON
SELECT 
	FXW_C_FOR_C_APELIDO,
	FXW_N_MULTIPLO,
	FXW_C_ACESSO_ANEXO,
	ISNULL(FXW_C_DADOS, '') AS FXW_C_DADOS,
	ISNULL(FXW_C_DIV, '') AS FXW_C_DIV,
	ISNULL(FXW_C_DIVEDIT, '') AS FXW_C_DIVEDIT
 FROM FFXW_FORMULARIO_WORKFLOW
 WHERE 
		FXW_N_NODE_ID = @IN_FXW_N_NODE_ID
SET NOCOUNT OFF

GO

IF NOT OBJECT_ID('dbo.frm_ListFormItemProcesso') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_ListFormItemProcesso
END
GO

CREATE PROCEDURE dbo.frm_ListFormItemProcesso
(
 @IN_FXI_N_ITE_N_CODIGO  int = null,
 @IN_FXI_N_FOR_N_CODAUTOMATICO int = null,
 @IN_FXI_C_FOR_C_APELIDO  varchar(30)= null,
 @IN_FXI_C_FOR_C_GUID varchar(36) = null,
 @IN_FXI_ISP_N_ID INT = NULL
)
AS
DECLARE @SQL	VARCHAR(8000)
DECLARE @AND	VARCHAR(10)
BEGIN
	  SET @SQL = 'SELECT 
					FXI_N_ITE_N_CODIGO,
					FXI_N_FOR_N_CODAUTOMATICO,
					FXI_C_FOR_C_APELIDO,
					FXI_C_FOR_C_GUID,
					FXI_ISP_N_ID
				FROM 
					FFXI_FORMULARIO_ITEM_PROCESSO '
	  SET @AND = 'WHERE '
	  
	  IF @IN_FXI_N_ITE_N_CODIGO IS NOT NULL OR @IN_FXI_N_ITE_N_CODIGO > 0 
		 BEGIN
			  SET @SQL = @SQL +  @AND + '
						FXI_N_ITE_N_CODIGO = ' + CONVERT(VARCHAR, @IN_FXI_N_ITE_N_CODIGO)
			  SET @AND = ' AND '
		 END
	
	  
	  IF @IN_FXI_N_FOR_N_CODAUTOMATICO IS NOT NULL OR @IN_FXI_N_FOR_N_CODAUTOMATICO > 0 
		 BEGIN
			  SET @SQL = @SQL +  @AND + '
						FXI_N_FOR_N_CODAUTOMATICO = ' + CONVERT(VARCHAR, @IN_FXI_N_FOR_N_CODAUTOMATICO)
			  SET @AND = ' AND '
		 END

	  
	  IF @IN_FXI_C_FOR_C_APELIDO IS NOT NULL OR @IN_FXI_C_FOR_C_APELIDO <> '' 
		 BEGIN
			  SET @SQL = @SQL +  @AND + '
						UPPER(FXI_C_FOR_C_APELIDO) LIKE UPPER(''' + @IN_FXI_C_FOR_C_APELIDO + ''')'
			  SET @AND = ' AND '
		 END

	  IF @IN_FXI_C_FOR_C_GUID IS NOT NULL OR @IN_FXI_C_FOR_C_GUID <> '' 
		 BEGIN
			  SET @SQL = @SQL +  @AND + '
						UPPER(FXI_C_FOR_C_GUID) LIKE UPPER(''' + @IN_FXI_C_FOR_C_GUID + ''')'
			  SET @AND = ' AND '
		 END
	IF @IN_FXI_C_FOR_C_GUID IS NOT NULL OR @IN_FXI_C_FOR_C_GUID <> '' 
		BEGIN
			SET @SQL = @SQL +  @AND + '
				FXI_ISP_N_ID = ' + @IN_FXI_ISP_N_ID
			SET @AND = ' AND '
		END
END

EXEC (@SQL)
GO

IF NOT OBJECT_ID('dbo.frm_AddFormItemProcesso') IS NULL
BEGIN
	DROP PROCEDURE dbo.frm_AddFormItemProcesso
END
GO

CREATE PROCEDURE dbo.frm_AddFormItemProcesso
(
	@IN_FXI_N_ITE_N_CODIGO int,
	@IN_FXI_N_FOR_N_CODAUTOMATICO int,
	@IN_FXI_C_FOR_C_APELIDO varchar(30),
	@IN_FXI_C_FOR_C_GUID varchar(36),
	@IN_FXI_ISP_N_ID INT
)
AS
BEGIN
 IF NOT EXISTS (SELECT FXI_N_ITE_N_CODIGO FROM FFXI_FORMULARIO_ITEM_PROCESSO
			WHERE 
					FXI_N_ITE_N_CODIGO = @IN_FXI_N_ITE_N_CODIGO AND
					FXI_N_FOR_N_CODAUTOMATICO = @IN_FXI_N_FOR_N_CODAUTOMATICO AND
					UPPER(FXI_C_FOR_C_APELIDO) = UPPER(@IN_FXI_C_FOR_C_APELIDO)  AND
		FXI_C_FOR_C_GUID = @IN_FXI_C_FOR_C_GUID
		)
		INSERT INTO FFXI_FORMULARIO_ITEM_PROCESSO
		(
			FXI_N_ITE_N_CODIGO,
			FXI_N_FOR_N_CODAUTOMATICO,
			FXI_C_FOR_C_APELIDO,
			FXI_C_FOR_C_GUID,
			FXI_ISP_N_ID
		) VALUES (
			@IN_FXI_N_ITE_N_CODIGO,
			@IN_FXI_N_FOR_N_CODAUTOMATICO,
			@IN_FXI_C_FOR_C_APELIDO,
			@IN_FXI_C_FOR_C_GUID,
			@IN_FXI_ISP_N_ID
		)

END
GO

IF NOT OBJECT_ID('dbo.FRM_LISTCHILDITEMS') IS NULL
DROP PROCEDURE [dbo].[FRM_LISTCHILDITEMS]
GO
CREATE PROCEDURE [dbo].[FRM_LISTCHILDITEMS](
	@FORMALIAS AS NVARCHAR(50),
	@PARENTGUID AS NVARCHAR(40)
)
AS
BEGIN
	DECLARE @SQL NVARCHAR(4000)
	SET @SQL = 'SELECT * FROM ' + @FORMALIAS + ' WHERE PARENTGUID = ''' + @PARENTGUID + ''''
	EXEC (@SQL)
END
GO


SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_AddFiltros') IS NULL
	DROP PROCEDURE dbo.rep_AddFiltros
GO
CREATE PROCEDURE dbo.rep_AddFiltros (
	@IN_FIL_C_CAM_ALIAS   NVARCHAR(50),
	@IN_FIL_N_REP_N_CODIGO    int,
	@IN_FIL_C_OPERACAO    char(2),
	@IN_FIL_C_VALOR_COMPARACAO    varchar(100),
	@IN_FIL_C_TIPO_COMPARACAO    char(2)
) AS
BEGIN
	SET NOCOUNT ON
	INSERT INTO RFIL_FILTROS
	(
		FIL_C_CAM_C_ALIAS,
		FIL_N_REP_N_CODIGO,
		FIL_C_OPERACAO,
		FIL_C_VALOR_COMPARACAO,
		FIL_C_TIPO_COMPARACAO
	) VALUES (
		@IN_FIL_C_CAM_ALIAS,
		@IN_FIL_N_REP_N_CODIGO,
		@IN_FIL_C_OPERACAO,
		@IN_FIL_C_VALOR_COMPARACAO,
		@IN_FIL_C_TIPO_COMPARACAO)
	SET NOCOUNT OFF
	SELECT @@IDENTITY
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_AtualizaCampos') IS NULL
	DROP PROCEDURE dbo.rep_AtualizaCampos
GO
CREATE PROCEDURE dbo.rep_AtualizaCampos (
	@IN_FOR_ID INT,
	@IN_CAM_N_REP_N_CODIGO    int,
	@IN_CAM_C_ALIAS    nvarchar(100),
	@IN_CAM_C_ACESSO    varchar(10),
	@IN_CAM_N_POSICAO    int,
	@IN_CAM_N_WIDTH    int,
	@IN_CAM_C_APELIDO    varchar(30),
	@IN_CAM_N_SEARCH_RANGE    smallint,
	@IN_CAM_N_SEARCH_BUSCA    smallint,
	@IN_CAM_N_SEARCH_FILTRO    smallint,
	@IN_CAM_N_VISIBLE    smallint,
	@IN_CAM_N_ORDEM    int
) AS
BEGIN
	DECLARE @CAM_ID INT

	SELECT	@CAM_ID = CAM_N_CODIGO 
	FROM	RCAM_CAMPOS 
	INNER JOIN RREP_REPORTS ON REP_N_MOI_N_CODIGO = CAM_N_REP_N_CODIGO
	WHERE 
		CAM_N_REP_N_CODIGO = @IN_CAM_N_REP_N_CODIGO AND
		REP_N_FOR_N_CODIGO = @IN_FOR_ID AND
		CAM_C_ALIAS = @IN_CAM_C_ALIAS

	IF @CAM_ID IS NULL 
	BEGIN
		SET NOCOUNT ON
		INSERT INTO RCAM_CAMPOS (
			CAM_N_REP_N_CODIGO,
			CAM_C_ALIAS,
			CAM_C_ACESSO,
			CAM_N_POSICAO,
			CAM_N_WIDTH,
			CAM_C_APELIDO,
			CAM_N_SEARCH_RANGE,
			CAM_N_SEARCH_BUSCA,
			CAM_N_SEARCH_FILTRO,
			CAM_N_VISIBLE,
			CAM_N_ORDEM
		) VALUES (
			@IN_CAM_N_REP_N_CODIGO,
			@IN_CAM_C_ALIAS,
			@IN_CAM_C_ACESSO,
			@IN_CAM_N_POSICAO,
			@IN_CAM_N_WIDTH,
			@IN_CAM_C_APELIDO,
			@IN_CAM_N_SEARCH_RANGE,
			@IN_CAM_N_SEARCH_BUSCA,
			@IN_CAM_N_SEARCH_FILTRO,
			@IN_CAM_N_VISIBLE,
			@IN_CAM_N_ORDEM)
		SET NOCOUNT OFF
		SELECT @@IDENTITY
	END
	ELSE
	BEGIN
		UPDATE RCAM_CAMPOS SET
			CAM_N_REP_N_CODIGO = @IN_CAM_N_REP_N_CODIGO,
			CAM_C_ALIAS = @IN_CAM_C_ALIAS,
			CAM_C_ACESSO = @IN_CAM_C_ACESSO,
			CAM_N_POSICAO=  @IN_CAM_N_POSICAO,
			CAM_N_WIDTH = @IN_CAM_N_WIDTH,
			CAM_C_APELIDO = @IN_CAM_C_APELIDO,
			CAM_N_SEARCH_RANGE = @IN_CAM_N_SEARCH_RANGE,
			CAM_N_SEARCH_BUSCA = @IN_CAM_N_SEARCH_BUSCA,
			CAM_N_SEARCH_FILTRO = @IN_CAM_N_SEARCH_FILTRO,
			CAM_N_VISIBLE = @IN_CAM_N_VISIBLE,
			CAM_N_ORDEM = @IN_CAM_N_ORDEM
		WHERE CAM_N_CODIGO = @CAM_ID
	END
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_AtualizaCamposProc') IS NULL
	DROP PROCEDURE dbo.rep_AtualizaCamposProc
GO
CREATE PROCEDURE dbo.rep_AtualizaCamposProc (
	@IN_CAM_N_REP_N_CODIGO    int,
	@IN_CAM_C_ALIAS    nvarchar(100),
	@IN_CAM_C_ACESSO    varchar(10),
	@IN_CAM_N_POSICAO    int,
	@IN_CAM_N_WIDTH    int,
	@IN_CAM_C_APELIDO    varchar(30),
	@IN_CAM_N_SEARCH_RANGE    smallint,
	@IN_CAM_N_SEARCH_BUSCA    smallint,
	@IN_CAM_N_SEARCH_FILTRO    smallint,
	@IN_CAM_N_VISIBLE    smallint,
	@IN_CAM_N_ORDEM    int
) AS
BEGIN
	DECLARE @CAM_ID INT

	SELECT @CAM_ID = CAM_N_CODIGO 
	FROM   RCAM_CAMPOS 
	WHERE
		CAM_N_REP_N_CODIGO = @IN_CAM_N_REP_N_CODIGO AND
		CAM_C_ALIAS = @IN_CAM_C_ALIAS

	IF @CAM_ID IS NULL 
	BEGIN
		SET NOCOUNT ON
		INSERT INTO RCAM_CAMPOS (
			CAM_N_REP_N_CODIGO,
			CAM_C_ALIAS,
			CAM_C_ACESSO,
			CAM_N_POSICAO,
			CAM_N_WIDTH,
			CAM_C_APELIDO,
			CAM_N_SEARCH_RANGE,
			CAM_N_SEARCH_BUSCA,
			CAM_N_SEARCH_FILTRO,
			CAM_N_VISIBLE,
			CAM_N_ORDEM
		) VALUES (
			@IN_CAM_N_REP_N_CODIGO,
			@IN_CAM_C_ALIAS,
			@IN_CAM_C_ACESSO,
			@IN_CAM_N_POSICAO,
			@IN_CAM_N_WIDTH,
			@IN_CAM_C_APELIDO,
			@IN_CAM_N_SEARCH_RANGE,
			@IN_CAM_N_SEARCH_BUSCA,
			@IN_CAM_N_SEARCH_FILTRO,
			@IN_CAM_N_VISIBLE,
			@IN_CAM_N_ORDEM)
		SET NOCOUNT OFF
		SELECT @@IDENTITY
	END
	ELSE
	BEGIN
		UPDATE RCAM_CAMPOS SET
			CAM_N_REP_N_CODIGO = @IN_CAM_N_REP_N_CODIGO,
			CAM_C_ALIAS = @IN_CAM_C_ALIAS,
			CAM_C_ACESSO = @IN_CAM_C_ACESSO,
			CAM_N_POSICAO=  @IN_CAM_N_POSICAO,
			CAM_N_WIDTH = @IN_CAM_N_WIDTH,
			CAM_C_APELIDO = @IN_CAM_C_APELIDO,
			CAM_N_SEARCH_RANGE = @IN_CAM_N_SEARCH_RANGE,
			CAM_N_SEARCH_BUSCA = @IN_CAM_N_SEARCH_BUSCA,
			CAM_N_SEARCH_FILTRO = @IN_CAM_N_SEARCH_FILTRO,
			CAM_N_VISIBLE = @IN_CAM_N_VISIBLE,
			CAM_N_ORDEM = @IN_CAM_N_ORDEM
		WHERE CAM_N_CODIGO = @CAM_ID
	END
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_AtualizaReports') IS NULL
	DROP PROCEDURE dbo.rep_AtualizaReports
GO
CREATE PROCEDURE dbo.rep_AtualizaReports (
	@IN_REC_ID INT,
	@IN_REP_N_FOR_N_CODIGO    int,
	@IN_REP_C_TITULO    nvarchar(200),
	@IN_REP_N_PAGECOUNT    int,
	@IN_REP_N_TIPO    char(2),
	@IN_REP_C_PARAMETROS    varchar(2000),
	@IN_REP_C_PROCEDURE    varchar(200),
	@IN_REP_C_LISTAEDICAO    varchar(200),
	@IN_REP_C_LISTAEXCLUSAO    varchar(200),
	@IN_REP_C_CONNECTION    varchar(400),
	@IN_REP_C_TITULOGRAFICO nvarchar(100),
	@IN_REP_N_VISUALIZACAOG int,
	@IN_REP_N_LARGURAGRAFICO int,
	@IN_REP_N_ALTURAGRAFICO int,
	@IN_REP_B_MOSTRARVALORESG bit,
	@IN_REP_C_CAMPONOMEGRAFICO varchar(200),
	@IN_REP_C_CAMPOVALORGRAFICO varchar(200),
	@IN_REP_C_POSICAOLEGENDAG int,
	@IN_REP_C_FUNCAOAGRUPAMENTOG int,
	@IN_REP_C_CAMPOAGRUPAMENTO varchar(300),
	@IN_REP_B_VISUALIZAGRAFICO BIT
) AS
BEGIN
	DECLARE @REP_COD INT
	SET @REP_COD = 0
	SELECT @REP_COD = REP_N_MOI_N_CODIGO FROM RREP_REPORTS WHERE REP_N_MOI_N_CODIGO = @IN_REC_ID
	IF @REP_COD = 0
	BEGIN
		SET NOCOUNT ON
		INSERT INTO RREP_REPORTS (
			REP_N_MOI_N_CODIGO,	
			REP_N_FOR_N_CODIGO,
			REP_C_TITULO,
			REP_N_PAGECOUNT,
			REP_N_TIPO,
			REP_C_PARAMETROS,
			REP_C_PROCEDURE,
			REP_C_LISTAEDICAO,
			REP_C_LISTAEXCLUSAO,
			REP_C_CONNECTION,
			REP_C_TITULOGRAFICO,
			REP_N_VISUALIZACAOGRAFICO,
			REP_N_LARGURAGRAFICO,
			REP_N_ALTURAGRAFICO,
			REP_B_MOSTRARVALORESGRAFICO,
			REP_C_CAMPONOMEGRAFICO,
			REP_C_CAMPOVALORGRAFICO,
			REP_C_POSICAOLEGENDAGRAFICO,
			REP_C_FUNCAOAGRUPAMENTOGRAFICO,
			REP_C_CAMPOAGRUPAMENTO,
			REP_B_VISUALIZAGRAFICO
		) VALUES (
			@IN_REC_ID,
			@IN_REP_N_FOR_N_CODIGO,
			@IN_REP_C_TITULO,
			@IN_REP_N_PAGECOUNT,
			@IN_REP_N_TIPO,
			@IN_REP_C_PARAMETROS,
			@IN_REP_C_PROCEDURE,
			@IN_REP_C_LISTAEDICAO,
			@IN_REP_C_LISTAEXCLUSAO,
			@IN_REP_C_CONNECTION,
			@IN_REP_C_TITULOGRAFICO,
			@IN_REP_N_VISUALIZACAOG,
			@IN_REP_N_LARGURAGRAFICO,
			@IN_REP_N_ALTURAGRAFICO,
			@IN_REP_B_MOSTRARVALORESG,
			@IN_REP_C_CAMPONOMEGRAFICO,
			@IN_REP_C_CAMPOVALORGRAFICO,
			@IN_REP_C_POSICAOLEGENDAG,
			@IN_REP_C_FUNCAOAGRUPAMENTOG,
			@IN_REP_C_CAMPOAGRUPAMENTO,
			@IN_REP_B_VISUALIZAGRAFICO)
		SET NOCOUNT OFF
		SELECT @@IDENTITY
	END
	ELSE
	BEGIN
		UPDATE RREP_REPORTS SET 
		REP_N_FOR_N_CODIGO = @IN_REP_N_FOR_N_CODIGO,
		REP_C_TITULO = @IN_REP_C_TITULO,
		REP_N_PAGECOUNT = @IN_REP_N_PAGECOUNT,
		REP_N_TIPO = @IN_REP_N_TIPO,
		REP_C_PARAMETROS = @IN_REP_C_PARAMETROS,
		REP_C_PROCEDURE = @IN_REP_C_PROCEDURE,
		REP_C_LISTAEDICAO = @IN_REP_C_LISTAEDICAO,
		REP_C_LISTAEXCLUSAO = @IN_REP_C_LISTAEXCLUSAO,
		REP_C_CONNECTION = @IN_REP_C_CONNECTION,
		REP_C_TITULOGRAFICO = @IN_REP_C_TITULOGRAFICO,
		REP_N_VISUALIZACAOGRAFICO = @IN_REP_N_VISUALIZACAOG,
		REP_N_LARGURAGRAFICO = @IN_REP_N_LARGURAGRAFICO,
		REP_N_ALTURAGRAFICO = @IN_REP_N_ALTURAGRAFICO,
		REP_B_MOSTRARVALORESGRAFICO = @IN_REP_B_MOSTRARVALORESG,
		REP_C_CAMPONOMEGRAFICO = @IN_REP_C_CAMPONOMEGRAFICO,
		REP_C_CAMPOVALORGRAFICO = @IN_REP_C_CAMPOVALORGRAFICO,
		REP_C_POSICAOLEGENDAGRAFICO = @IN_REP_C_POSICAOLEGENDAG,
		REP_C_FUNCAOAGRUPAMENTOGRAFICO = @IN_REP_C_FUNCAOAGRUPAMENTOG,
		REP_C_CAMPOAGRUPAMENTO = @IN_REP_C_CAMPOAGRUPAMENTO,
		REP_B_VISUALIZAGRAFICO = @IN_REP_B_VISUALIZAGRAFICO
		WHERE REP_N_MOI_N_CODIGO = @IN_REC_ID
		SELECT @IN_REC_ID
	END
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_DelCamposReports') IS NULL
	DROP PROCEDURE dbo.rep_DelCamposReports
GO
CREATE PROCEDURE dbo.rep_DelCamposReports (
	@IN_REP_N_CODIGO    int
) AS
BEGIN
	SET NOCOUNT ON

	DELETE FROM RCAM_CAMPOS
	WHERE CAM_N_REP_N_CODIGO = @IN_REP_N_CODIGO

	SET NOCOUNT OFF
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_DelFiltros') IS NULL
	DROP PROCEDURE dbo.rep_DelFiltros
GO
CREATE PROCEDURE dbo.rep_DelFiltros (
	@IN_FIL_N_CODIGO    int
) AS
BEGIN
	SET NOCOUNT ON

	DELETE FROM RFIL_FILTROS
	WHERE FIL_N_CODIGO = @IN_FIL_N_CODIGO

	SET NOCOUNT OFF
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_DelFiltrosReport') IS NULL
	DROP PROCEDURE dbo.rep_DelFiltrosReport
GO
CREATE PROCEDURE dbo.rep_DelFiltrosReport (
	@IN_REP_N_CODIGO    int
) AS
BEGIN
	SET NOCOUNT ON

	DELETE FROM RFIL_FILTROS
	WHERE FIL_N_REP_N_CODIGO = @IN_REP_N_CODIGO

	SET NOCOUNT OFF
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_DelRegistroForm') IS NULL
	DROP PROCEDURE dbo.rep_DelRegistroForm
GO
CREATE PROCEDURE dbo.rep_DelRegistroForm (
	@IN_DATABASE NVARCHAR(30),
	@IN_TABELA NVARCHAR(30),
	@IN_COD_AUTO NVARCHAR(30),
	@COD_ID INT
) AS
BEGIN
	DECLARE @SQL NVARCHAR(500)
	SET @SQL = 'DELETE FROM [' + @IN_DATABASE + '].dbo.[' + @IN_TABELA + ']  WHERE ' + @IN_COD_AUTO + '  =  '  + CONVERT(VARCHAR, @COD_ID)
	EXECUTE sp_executesql @SQL
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_GetCampos') IS NULL
	DROP PROCEDURE dbo.rep_GetCampos
GO
CREATE PROCEDURE dbo.rep_GetCampos (
	@IN_CAM_N_REP_N_CODIGO    int ,
	@IN_FOR_ID    int ,
	@IN_ALIAS nvarchar(50) ,	
	@OUT_CAM_N_CODIGO    int OUTPUT,
	@OUT_CAM_C_ALIAS    nvarchar(100) OUTPUT ,
	@OUT_CAM_C_ACESSO    varchar(10) OUTPUT ,
	@OUT_CAM_N_POSICAO    int OUTPUT ,
	@OUT_CAM_N_WIDTH    int OUTPUT ,
	@OUT_CAM_C_APELIDO    varchar(30) OUTPUT ,
	@OUT_CAM_N_SEARCH_RANGE    smallint OUTPUT ,
	@OUT_CAM_N_SEARCH_BUSCA    smallint OUTPUT ,
	@OUT_CAM_N_SEARCH_FILTRO    smallint OUTPUT ,
	@OUT_CAM_N_VISIBLE    smallint OUTPUT,
	@OUT_CAM_N_ORDEM    int OUTPUT
) AS
BEGIN
	SET NOCOUNT ON
	SELECT 
		@OUT_CAM_N_CODIGO = CAM_N_CODIGO,
		@OUT_CAM_C_ALIAS = CAM_C_ALIAS,
		@OUT_CAM_C_ACESSO = CAM_C_ACESSO,
		@OUT_CAM_N_POSICAO = CAM_N_POSICAO,
		@OUT_CAM_N_WIDTH = CAM_N_WIDTH,
		@OUT_CAM_C_APELIDO = CAM_C_APELIDO,
		@OUT_CAM_N_SEARCH_RANGE = CAM_N_SEARCH_RANGE,
		@OUT_CAM_N_SEARCH_BUSCA = CAM_N_SEARCH_BUSCA,
		@OUT_CAM_N_SEARCH_FILTRO = CAM_N_SEARCH_FILTRO,
		@OUT_CAM_N_VISIBLE = CAM_N_VISIBLE,
		@OUT_CAM_N_ORDEM = CAM_N_ORDEM
	FROM RCAM_CAMPOS
	INNER JOIN RREP_REPORTS ON REP_N_MOI_N_CODIGO = CAM_N_REP_N_CODIGO
	WHERE 
		CAM_N_REP_N_CODIGO = @IN_CAM_N_REP_N_CODIGO AND
		CAM_C_ALIAS = @IN_ALIAS AND
		REP_N_FOR_N_CODIGO = @IN_FOR_ID
	SET NOCOUNT OFF
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_GetCampos_Apelido') IS NULL
	DROP PROCEDURE dbo.rep_GetCampos_Apelido
GO
CREATE PROCEDURE dbo.rep_GetCampos_Apelido (
	@IN_CAM_N_REP_N_CODIGO    int ,
	@IN_FOR_ID    int ,
	@IN_APELIDO  nvarchar(50) ,	
	@OUT_CAM_N_CODIGO    int OUTPUT,
	@OUT_CAM_C_ALIAS    nvarchar(100) OUTPUT ,
	@OUT_CAM_C_ACESSO    varchar(10) OUTPUT ,
	@OUT_CAM_N_POSICAO    int OUTPUT ,
	@OUT_CAM_N_WIDTH    int OUTPUT ,
	@OUT_CAM_C_APELIDO    varchar(30) OUTPUT ,
	@OUT_CAM_N_SEARCH_RANGE    smallint OUTPUT ,
	@OUT_CAM_N_SEARCH_BUSCA    smallint OUTPUT ,
	@OUT_CAM_N_SEARCH_FILTRO    smallint OUTPUT ,
	@OUT_CAM_N_VISIBLE    smallint OUTPUT,
	@OUT_CAM_N_ORDEM    int OUTPUT
) AS
BEGIN
	SET NOCOUNT ON
	SELECT 
		@OUT_CAM_N_CODIGO = CAM_N_CODIGO,
		@OUT_CAM_C_ALIAS = CAM_C_ALIAS,
		@OUT_CAM_C_ACESSO = CAM_C_ACESSO,
		@OUT_CAM_N_POSICAO = CAM_N_POSICAO,
		@OUT_CAM_N_WIDTH = CAM_N_WIDTH,
		@OUT_CAM_C_APELIDO = CAM_C_APELIDO,
		@OUT_CAM_N_SEARCH_RANGE = CAM_N_SEARCH_RANGE,
		@OUT_CAM_N_SEARCH_BUSCA = CAM_N_SEARCH_BUSCA,
		@OUT_CAM_N_SEARCH_FILTRO = CAM_N_SEARCH_FILTRO,
		@OUT_CAM_N_VISIBLE = CAM_N_VISIBLE,
		@OUT_CAM_N_ORDEM = CAM_N_ORDEM
	FROM RCAM_CAMPOS
	INNER JOIN RREP_REPORTS ON REP_N_MOI_N_CODIGO = CAM_N_REP_N_CODIGO
	WHERE 
		CAM_N_REP_N_CODIGO = @IN_CAM_N_REP_N_CODIGO AND
		CAM_C_APELIDO = @IN_APELIDO AND
		REP_N_FOR_N_CODIGO = @IN_FOR_ID
	SET NOCOUNT OFF
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_GetReports') IS NULL
	DROP PROCEDURE dbo.rep_GetReports
GO
CREATE PROCEDURE dbo.rep_GetReports (
	@IN_REP_N_MOI_N_CODIGO    int,
	@OUT_REP_N_FOR_N_CODIGO    int OUTPUT ,
	@OUT_REP_C_TITULO    nvarchar(200) OUTPUT ,
	@OUT_REP_N_PAGECOUNT    int OUTPUT ,
	@OUT_REP_N_TIPO   char(2)  OUTPUT ,
	@OUT_REP_C_PARAMETROS    varchar(2000) OUTPUT ,
	@OUT_REP_C_PROCEDURE    varchar(200) OUTPUT ,
	@OUT_REP_C_LISTAEDICAO    varchar(200) OUTPUT,
	@OUT_REP_C_LISTAEXCLUSAO    varchar(200) OUTPUT,
	@OUT_REP_C_CONNECTION    varchar(400) OUTPUT,
	
	@OUT_REP_C_TITULOGRAFICO nvarchar(100) OUTPUT ,
	@OUT_REP_N_VISUALIZACAOG int OUTPUT,
	@OUT_REP_N_LARGURAGRAFICO INT OUTPUT,
	@OUT_REP_N_ALTURAGRAFICO INT OUTPUT,
	@OUT_REP_B_MOSTRARVALORESG BIT OUTPUT,
	@OUT_REP_C_CAMPONOMEGRAFICO	VARCHAR(200) OUTPUT,
	@OUT_REP_C_CAMPOVALORGRAFICO VARCHAR(200) OUTPUT,
	@OUT_REP_C_POSICAOLEGENDAG	INT OUTPUT,
	@OUT_REP_C_FUNCAOAGRUPAMENTOG INT OUTPUT,
	@OUT_REP_C_CAMPOAGRUPAMENTO VARCHAR(300) OUTPUT,
	@OUT_REP_B_VISUALIZAGRAFICO BIT OUTPUT
) AS
BEGIN
	SET NOCOUNT ON
	SELECT 
		@OUT_REP_N_FOR_N_CODIGO = REP_N_FOR_N_CODIGO,
		@OUT_REP_C_TITULO = REP_C_TITULO,
		@OUT_REP_N_PAGECOUNT = REP_N_PAGECOUNT,
		@OUT_REP_N_TIPO = REP_N_TIPO,
		@OUT_REP_C_PARAMETROS = REP_C_PARAMETROS,
		@OUT_REP_C_PROCEDURE = REP_C_PROCEDURE,
		@OUT_REP_C_LISTAEDICAO = REP_C_LISTAEDICAO,
		@OUT_REP_C_LISTAEXCLUSAO = REP_C_LISTAEXCLUSAO,
		@OUT_REP_C_CONNECTION = REP_C_CONNECTION,
		@OUT_REP_C_TITULOGRAFICO = REP_C_TITULOGRAFICO,
		@OUT_REP_N_VISUALIZACAOG = REP_N_VISUALIZACAOGRAFICO,
		@OUT_REP_N_LARGURAGRAFICO = REP_N_LARGURAGRAFICO,
		@OUT_REP_N_ALTURAGRAFICO = REP_N_ALTURAGRAFICO,
		@OUT_REP_B_MOSTRARVALORESG = REP_B_MOSTRARVALORESGRAFICO,
		@OUT_REP_C_CAMPONOMEGRAFICO = REP_C_CAMPONOMEGRAFICO,
		@OUT_REP_C_CAMPOVALORGRAFICO = REP_C_CAMPOVALORGRAFICO,
		@OUT_REP_C_POSICAOLEGENDAG = REP_C_POSICAOLEGENDAGRAFICO,
		@OUT_REP_C_FUNCAOAGRUPAMENTOG = REP_C_FUNCAOAGRUPAMENTOGRAFICO,
		@OUT_REP_C_CAMPOAGRUPAMENTO = REP_C_CAMPOAGRUPAMENTO,
		@OUT_REP_B_VISUALIZAGRAFICO = REP_B_VISUALIZAGRAFICO
	FROM RREP_REPORTS
	WHERE 
		REP_N_MOI_N_CODIGO = @IN_REP_N_MOI_N_CODIGO
	SET NOCOUNT OFF
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_ListCampos') IS NULL
	DROP PROCEDURE dbo.rep_ListCampos
GO
CREATE PROCEDURE dbo.rep_ListCampos (
	@IN_REP_N_CODIGO INT
) AS
BEGIN
	SELECT 
		CAM_N_CODIGO,
		CAM_N_REP_N_CODIGO,
		CAM_C_ALIAS as 'Name',
		CAM_C_ACESSO,
		CAM_N_POSICAO,
		CAM_N_WIDTH,
		CAM_C_APELIDO,
		CAM_N_SEARCH_RANGE,
		CAM_N_SEARCH_BUSCA,
		CAM_N_SEARCH_FILTRO,
		CAM_N_VISIBLE,
		CAM_N_ORDEM
	FROM RCAM_CAMPOS
	WHERE CAM_N_REP_N_CODIGO = @IN_REP_N_CODIGO
	ORDER BY CAM_N_ORDEM
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_ListFiltro') IS NULL
	DROP PROCEDURE dbo.rep_ListFiltro
GO
CREATE PROCEDURE dbo.rep_ListFiltro (
	@IN_RELATORIOID INT
) AS
BEGIN
	SELECT
		FIL_N_CODIGO, 
		FIL_C_CAM_C_ALIAS,
		FIL_C_OPERACAO,
		FIL_C_VALOR_COMPARACAO,
		FIL_C_TIPO_COMPARACAO
	
	FROM RFIL_FILTROS
	WHERE Fil_N_REP_N_CODIGO = @IN_RELATORIOID
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_UpdOrdemCampoRelatorio') IS NULL
	DROP PROCEDURE dbo.rep_UpdOrdemCampoRelatorio
GO
CREATE PROCEDURE dbo.rep_UpdOrdemCampoRelatorio (
	@CAM_C_ALTERAR 		NVARCHAR(30),
	@IN_CAM_N_REP_N_CODIGO	INT,
	@IN_ALIAS			NVARCHAR(30),
	@IN_FOR_ID			INT
	
) AS
BEGIN
	DECLARE @ORDEMCAMPO1 	INT
	DECLARE @ORDEMCAMPO2 	INT
	DECLARE @CAM_N_ID		INT
	DECLARE @CAM_N_ALTERAR	INT

	SELECT @CAM_N_ID = CAM_N_CODIGO
	FROM RCAM_CAMPOS
	INNER JOIN RREP_REPORTS ON REP_N_MOI_N_CODIGO = CAM_N_REP_N_CODIGO
	WHERE 
		CAM_N_REP_N_CODIGO = @IN_CAM_N_REP_N_CODIGO AND
		CAM_C_ALIAS = @CAM_C_ALTERAR AND
		REP_N_FOR_N_CODIGO = @IN_FOR_ID

	SELECT @CAM_N_ALTERAR = CAM_N_CODIGO
	FROM RCAM_CAMPOS 
	INNER JOIN RREP_REPORTS ON REP_N_MOI_N_CODIGO = CAM_N_REP_N_CODIGO
	WHERE 
		CAM_N_REP_N_CODIGO = @IN_CAM_N_REP_N_CODIGO AND
		CAM_C_ALIAS = @IN_ALIAS AND
		REP_N_FOR_N_CODIGO = @IN_FOR_ID

	--Seleciona ordem do campo 
	SELECT @ORDEMCAMPO2 = CAM_N_ORDEM 
	FROM RCAM_CAMPOS
	INNER JOIN RREP_REPORTS ON REP_N_MOI_N_CODIGO = CAM_N_REP_N_CODIGO
	WHERE 
		CAM_N_REP_N_CODIGO = @IN_CAM_N_REP_N_CODIGO AND
		CAM_C_ALIAS = @IN_ALIAS AND
		REP_N_FOR_N_CODIGO = @IN_FOR_ID

	--Seleciona ordem do campo a ser alterado
	SELECT @ORDEMCAMPO1 = CAM_N_ORDEM 
	FROM RCAM_CAMPOS
	INNER JOIN RREP_REPORTS ON REP_N_MOI_N_CODIGO = CAM_N_REP_N_CODIGO
	WHERE 
		CAM_N_REP_N_CODIGO = @IN_CAM_N_REP_N_CODIGO AND
		CAM_C_ALIAS = @CAM_C_ALTERAR  AND
		REP_N_FOR_N_CODIGO = @IN_FOR_ID

	UPDATE RCAM_CAMPOS 
	SET CAM_N_ORDEM = @ORDEMCAMPO2 
	WHERE CAM_N_CODIGO = @CAM_N_ID

	UPDATE RCAM_CAMPOS 
	SET CAM_N_ORDEM = @ORDEMCAMPO1 
	WHERE CAM_N_CODIGO = @CAM_N_ALTERAR
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.rep_AddFiltrosProcedure') IS NULL
BEGIN
	DROP PROCEDURE dbo.rep_AddFiltrosProcedure
END
GO
CREATE PROCEDURE dbo.rep_AddFiltrosProcedure (
	 @IN_FIP_N_MOI_N_CODIGO INT,
	 @IN_FIP_C_PARAMETRO VARCHAR(50),
	 @IN_FIP_C_PARAMETRO_VALOR VARCHAR(100),
	 @IN_FIP_C_PARAMETRO_TIPO VARCHAR(50),
	 @IN_FIP_C_TITULO VARCHAR(50),
	 @IN_FIP_B_FILTRO BIT,
	 @IN_FIP_B_NULO BIT
) AS 
BEGIN
	INSERT INTO RFIP_FILTROS_PROCEDURE (
		 FIP_N_MOI_N_CODIGO,
		 FIP_C_PARAMETRO,
		 FIP_C_PARAMETRO_VALOR,
		 FIP_C_PARAMETRO_TIPO,
		 FIP_C_TITULO,
		 FIP_B_FILTRO,
		 FIP_B_NULO
	) VALUES (
		 @IN_FIP_N_MOI_N_CODIGO,
		 @IN_FIP_C_PARAMETRO,
		 @IN_FIP_C_PARAMETRO_VALOR,
		 @IN_FIP_C_PARAMETRO_TIPO,
		 @IN_FIP_C_TITULO,
		 @IN_FIP_B_FILTRO,
		 @IN_FIP_B_NULO			    
	)
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.REP_DELPROCEDURE_REPORTS') IS NULL
BEGIN
	DROP PROCEDURE dbo.REP_DELPROCEDURE_REPORTS
END
GO
CREATE PROCEDURE dbo.REP_DELPROCEDURE_REPORTS (
	@IN_REP_N_MOI_N_CODIGO		INT,
	@IN_REP_C_PROCEDURE		VARCHAR(100)
) AS
BEGIN
	IF (SELECT COUNT(REP_N_MOI_N_CODIGO) FROM RREP_REPORTS WHERE REP_N_MOI_N_CODIGO = @IN_REP_N_MOI_N_CODIGO AND REP_C_PROCEDURE <> @IN_REP_C_PROCEDURE) > 0
	BEGIN
		--REMOVE PROCEDURE FROM RREP_REPORTS
		
		UPDATE RREP_REPORTS 
		SET 			 
			REP_C_PROCEDURE = @IN_REP_C_PROCEDURE,
			REP_C_PARAMETROS = ''  
		WHERE
			REP_N_MOI_N_CODIGO = @IN_REP_N_MOI_N_CODIGO
			AND REP_C_PROCEDURE <> @IN_REP_C_PROCEDURE
				
		DELETE FROM RFIP_FILTROS_PROCEDURE WHERE FIP_N_MOI_N_CODIGO = @IN_REP_N_MOI_N_CODIGO
			
		DELETE FROM RCAM_CAMPOS WHERE CAM_N_REP_N_CODIGO = @IN_REP_N_MOI_N_CODIGO
	END 
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.REP_DELFILTROSPROCEDURE') IS NULL
BEGIN
	DROP PROCEDURE dbo.REP_DELFILTROSPROCEDURE
END
GO
CREATE PROCEDURE dbo.REP_DELFILTROSPROCEDURE (
	@IN_FIP_N_MOI_N_CODIGO INT
) AS
BEGIN 
	SET NOCOUNT ON

	DELETE FROM RFIP_FILTROS_PROCEDURE
	WHERE FIP_N_MOI_N_CODIGO = @IN_FIP_N_MOI_N_CODIGO

	SET NOCOUNT OFF
END
GO

--###########################################################################################################
IF NOT OBJECT_ID('dbo.REP_LISTCONFFILTROS_PROCEDURE') IS NULL
BEGIN
	DROP PROCEDURE dbo.REP_LISTCONFFILTROS_PROCEDURE
END
GO
CREATE PROCEDURE dbo.REP_LISTCONFFILTROS_PROCEDURE (
		@IN_FIP_N_MOI_N_CODIGO INTEGER
) AS 
BEGIN
	SELECT
		FIP_N_MOI_N_CODIGO,
		FIP_C_PARAMETRO,
		FIP_C_PARAMETRO_VALOR,
		FIP_C_PARAMETRO_TIPO,
		FIP_C_TITULO,
		FIP_B_FILTRO,
		FIP_B_NULO        
	FROM
		RFIP_FILTROS_PROCEDURE
	WHERE
		FIP_N_MOI_N_CODIGO = @IN_FIP_N_MOI_N_CODIGO
END
GO
--###########################################################################################################
IF NOT OBJECT_ID('rep_DelReport') IS NULL 
BEGIN
	DROP PROC dbo.rep_DelReport
END
GO
CREATE PROC dbo.rep_DelReport (@IN_REP_N_MOI_N_CODIGO INT)
AS
BEGIN
	DELETE RREP_REPORTS WHERE REP_N_MOI_N_CODIGO = @IN_REP_N_MOI_N_CODIGO
END
GO

--###########################################################################################################


--------------------------------------------------------------------------------------------------------
-----	DADOS   ----------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------

IF NOT EXISTS (SELECT 1 FROM FMOD_MODULO WHERE MOD_C_ITEMID = N'67076d67-ce44-49f4-adfa-8d36c5c0b779')
BEGIN
	SET IDENTITY_INSERT [FMOD_MODULO] ON
	INSERT [FMOD_MODULO] ([MOD_N_CODIGO], [MOD_C_OWNERID], [MOD_C_ITEMID], [MOD_C_TITULO], [MOD_C_NOME_CONTROLE], [MOD_C_TIPO_CONTROLE], [MOD_C_ICONE], [MOD_C_IMAGEM_CONTROLE], [MOD_C_CATEGORIA]) 
	VALUES (1, N'3cfc8af3-c07e-4f14-b398-4a8917d97c92', N'67076d67-ce44-49f4-adfa-8d36c5c0b779', 'Texto', 'TextBox', 'asp:TextBox','textbox.gif','textbox.gif', 'Básico')
	INSERT [FMOD_MODULO] ([MOD_N_CODIGO], [MOD_C_OWNERID], [MOD_C_ITEMID], [MOD_C_TITULO], [MOD_C_NOME_CONTROLE], [MOD_C_TIPO_CONTROLE], [MOD_C_ICONE], [MOD_C_IMAGEM_CONTROLE], [MOD_C_CATEGORIA]) 
	VALUES (2, N'3cfc8af3-c07e-4f14-b398-4a8917d97c92', N'584c7deb-1025-4721-bbbb-40f937cc1617', 'Caixa de Seleção' , 'Combo', 'asp:DropDownList', 'combo.gif', 'combo.gif', 'Básico')
	INSERT [FMOD_MODULO] ([MOD_N_CODIGO], [MOD_C_OWNERID], [MOD_C_ITEMID], [MOD_C_TITULO], [MOD_C_NOME_CONTROLE], [MOD_C_TIPO_CONTROLE], [MOD_C_ICONE], [MOD_C_IMAGEM_CONTROLE], [MOD_C_CATEGORIA]) 
	VALUES (3, N'3cfc8af3-c07e-4f14-b398-4a8917d97c92', N'9949beef-d317-4456-89d4-94527291fbd4', 'Seleção Simples' , 'Radio' , 'asp:RadioButton' , 'radio.gif', 'radio.gif' , 'Básico')
	INSERT [FMOD_MODULO] ([MOD_N_CODIGO], [MOD_C_OWNERID], [MOD_C_ITEMID], [MOD_C_TITULO], [MOD_C_NOME_CONTROLE], [MOD_C_TIPO_CONTROLE], [MOD_C_ICONE], [MOD_C_IMAGEM_CONTROLE], [MOD_C_CATEGORIA]) 
	VALUES (4, N'3cfc8af3-c07e-4f14-b398-4a8917d97c92', N'28c2d99f-3aa8-48ea-bc20-48d12d3074b2', 'Seleção Múltipla' , 'CheckBox' , 'asp:CheckBox' , 'check.gif' , 'check.gif' , 'Básico' )
	INSERT [FMOD_MODULO] ([MOD_N_CODIGO], [MOD_C_OWNERID], [MOD_C_ITEMID], [MOD_C_TITULO], [MOD_C_NOME_CONTROLE], [MOD_C_TIPO_CONTROLE], [MOD_C_ICONE], [MOD_C_IMAGEM_CONTROLE], [MOD_C_CATEGORIA]) 
	VALUES (5, N'3cfc8af3-c07e-4f14-b398-4a8917d97c92', N'de8a3aaa-d888-4117-860a-7cde4df3f3e3', 'UpLoad' , 'UpLoad', 'asp:UpLoad', 'upload.gif', 'upload.gif', 'Básico')
	INSERT [FMOD_MODULO] ([MOD_N_CODIGO], [MOD_C_OWNERID], [MOD_C_ITEMID], [MOD_C_TITULO], [MOD_C_NOME_CONTROLE], [MOD_C_TIPO_CONTROLE], [MOD_C_ICONE], [MOD_C_IMAGEM_CONTROLE], [MOD_C_CATEGORIA]) 
	VALUES (6, N'3cfc8af3-c07e-4f14-b398-4a8917d97c92', N'd7bbe01d-a427-42ea-9486-9e609e33c932', 'Sub Formulario', 'FormChild', 'asp:FormChild', 'formfilho.gif', 'formfilho.gif', 'Básico')
	INSERT [FMOD_MODULO] ([MOD_N_CODIGO], [MOD_C_OWNERID], [MOD_C_ITEMID], [MOD_C_TITULO], [MOD_C_NOME_CONTROLE], [MOD_C_TIPO_CONTROLE], [MOD_C_ICONE], [MOD_C_IMAGEM_CONTROLE], [MOD_C_CATEGORIA]) 
	VALUES (7, N'3cfc8af3-c07e-4f14-b398-4a8917d97c92', N'6eebaba1-68ca-4b76-a6f6-3559880a2e3f', 'Label', 'Label', 'asp:Label', 'label.gif', 'label.gif', 'Básico')
	INSERT [FMOD_MODULO] ([MOD_N_CODIGO], [MOD_C_OWNERID], [MOD_C_ITEMID], [MOD_C_TITULO], [MOD_C_NOME_CONTROLE], [MOD_C_TIPO_CONTROLE], [MOD_C_ICONE], [MOD_C_IMAGEM_CONTROLE], [MOD_C_CATEGORIA]) 
	VALUES (8, N'3cfc8af3-c07e-4f14-b398-4a8917d97c92', N'5abe82cd-ffab-497e-943b-219454a00c88', 'Lista de seleção', 'ListaAcesso', 'asp:ListaAcesso', 'listaacesso.gif', 'listaacesso.gif', 'Básico')
	INSERT [FMOD_MODULO] ([MOD_N_CODIGO], [MOD_C_OWNERID], [MOD_C_ITEMID], [MOD_C_TITULO], [MOD_C_NOME_CONTROLE], [MOD_C_TIPO_CONTROLE], [MOD_C_ICONE], [MOD_C_IMAGEM_CONTROLE], [MOD_C_CATEGORIA]) 
	VALUES (9, N'3cfc8af3-c07e-4f14-b398-4a8917d97c92', N'7ef7c972-91a5-49ab-82a0-b7afd7aed5b1', 'DIV', 'DIV', 'asp:div', 'div.gif', ' ', 'Básico')
	SET IDENTITY_INSERT [FMOD_MODULO] OFF
END
GO

IF NOT EXISTS (SELECT 1 FROM FMOD_MODULO WHERE MOD_C_ITEMID = N'DD374049-07A9-4B86-BD35-7B94441B8EF6')
BEGIN
	INSERT FMOD_MODULO (MOD_C_OWNERID, MOD_C_ITEMID, MOD_C_TITULO, MOD_C_NOME_CONTROLE, MOD_C_TIPO_CONTROLE, MOD_C_ICONE, MOD_C_IMAGEM_CONTROLE, MOD_C_CATEGORIA) 
	VALUES (N'3cfc8af3-c07e-4f14-b398-4a8917d97c92', N'dd374049-07a9-4b86-bd35-7b94441b8ef6', 'Conteúdo HTML', 'HTMLControl', 'asp:HTMLControl','html.gif','html.gif', 'Básico')
END
GO

IF NOT EXISTS (select 1 from sys.indexes where name = 'IX_FORMULARIO_ITEM_PROCESSO__N_CODIGO')
BEGIN
	CREATE NONCLUSTERED INDEX IX_FORMULARIO_ITEM_PROCESSO__N_CODIGO 
		ON dbo.FFXI_FORMULARIO_ITEM_PROCESSO (FXI_N_ITE_N_CODIGO ASC)
	WITH (
			PAD_INDEX  = OFF, 
			STATISTICS_NORECOMPUTE  = OFF, 
			SORT_IN_TEMPDB = OFF, 
			IGNORE_DUP_KEY = OFF, 
			DROP_EXISTING = OFF, 
			ONLINE = OFF, 
			ALLOW_ROW_LOCKS  = ON, 
			ALLOW_PAGE_LOCKS  = ON
		)
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID('FFXI_FORMULARIO_ITEM_PROCESSO') AND name = 'IX_ITEM_PROCESSO_FULL')
	CREATE NONCLUSTERED INDEX IX_ITEM_PROCESSO_FULL ON FFXI_FORMULARIO_ITEM_PROCESSO (FXI_C_FOR_C_APELIDO, FXI_N_FOR_N_CODAUTOMATICO) INCLUDE (FXI_C_FOR_C_GUID, FXI_N_ITE_N_CODIGO)
GO

SET NOCOUNT ON
UPDATE [FMOD_MODULO] SET [MOD_C_IMAGEM_CONTROLE] = 'check.png'  WHERE [MOD_C_ITEMID] = '28C2D99F-3AA8-48EA-BC20-48D12D3074B2'
UPDATE [FMOD_MODULO] SET [MOD_C_IMAGEM_CONTROLE] = 'childform.png' WHERE [MOD_C_ITEMID] = 'D7BBE01D-A427-42EA-9486-9E609E33C932'
UPDATE [FMOD_MODULO] SET [MOD_C_IMAGEM_CONTROLE] = 'dropdown.png' WHERE [MOD_C_ITEMID] = '584C7DEB-1025-4721-BBBB-40F937CC1617'
UPDATE [FMOD_MODULO] SET [MOD_C_IMAGEM_CONTROLE] = 'htmledit.png' WHERE [MOD_C_ITEMID] = 'DD374049-07A9-4B86-BD35-7B94441B8EF6'
UPDATE [FMOD_MODULO] SET [MOD_C_IMAGEM_CONTROLE] = 'label.png', [MOD_C_TITULO]='Texto sem edição' WHERE [MOD_C_ITEMID] = '6EEBABA1-68CA-4B76-A6F6-3559880A2E3F'
UPDATE [FMOD_MODULO] SET [MOD_C_IMAGEM_CONTROLE] = 'listasel.png' WHERE [MOD_C_ITEMID] = '5ABE82CD-FFAB-497E-943B-219454A00C88'
UPDATE [FMOD_MODULO] SET [MOD_C_IMAGEM_CONTROLE] = 'radio.png' WHERE [MOD_C_ITEMID] = '9949BEEF-D317-4456-89D4-94527291FBD4'
UPDATE [FMOD_MODULO] SET [MOD_C_IMAGEM_CONTROLE] = 'textbox.png' WHERE [MOD_C_ITEMID] = '67076D67-CE44-49F4-ADFA-8D36C5C0B779'
UPDATE [FMOD_MODULO] SET [MOD_C_IMAGEM_CONTROLE] = 'upload.png', [MOD_C_TITULO]='Carga de arquivo' WHERE [MOD_C_ITEMID] = 'DE8A3AAA-D888-4117-860A-7CDE4DF3F3E3'
UPDATE [FMOD_MODULO] SET [MOD_C_TITULO]='Seção' WHERE [MOD_C_ITEMID] = '7EF7C972-91A5-49AB-82A0-B7AFD7AED5B1'
GO


--### SET ZURI VERSION ###
begin try
	declare @v sysname = '9.1811.2283'--zuriversion
	declare @d varchar(50) = SYSDATETIMEOFFSET()
	if not @v is null and exists (select 1 from sys.extended_properties where class = 0 and name = 'Version')
	begin
		exec sp_updateextendedproperty Version, @v
		exec sp_updateextendedproperty VersionDate, @d
	end 
	else
	begin
		exec sp_addextendedproperty Version, @v
		exec sp_addextendedproperty VersionDate, @d
	end
	select * from sys.extended_properties 
end try
begin catch
end catch

--### Zuri9.1811.2283 ###  
