IF EXISTS (SELECT Name FROM sysindexes WHERE Name = 'IX_InformarRubSubRubProtocolo') DROP INDEX TBConfiguracao.IX_InformarRubSubRubProtocolo;

IF EXISTS (SELECT * 
  FROM sys.foreign_keys 
   WHERE object_id = OBJECT_ID(N'dbo.DF_TBConfiguracao_AtestarProcessoCompra')
   AND parent_object_id = OBJECT_ID(N'dbo.TBConfiguracao'))
ALTER TABLE TBConfiguracao DROP CONSTRAINT DF_TBConfiguracao_AtestarProcessoCompra;
GO --

alter table TBConfiguracao1 alter column ObrigatoriIdentidadeWEB bit;
alter table TBConfiguracao1 alter column UtilizarDetalheRateioSol bit;
alter table TBConfiguracao1 alter column UtilizarCategoriaFinanceiraWEB bit;
alter table TBConfiguracao1 alter column RendLiquidoRelProjetoPer bit;
alter table TBConfiguracao1 alter column UtilizarSolAdiantWEB bit;
alter table TBConfiguracao1 alter column InfoProvLiquidaRelExtProj bit;
alter table TBConfiguracao1 alter column GravaSolicitacaoCaixaAlta bit;
alter table TBConfiguracao1 alter column ObrigatorioIndicarFornecedores bit;
alter table TBConfiguracao1 alter column CriarProtocoloInsSolicitacao bit;
alter table TBConfiguracao1 alter column ObrigatorioPISPASEPINSSWeb bit;
alter table TBConfiguracao1 alter column DispCamposImpostosImpressao bit;
alter table TBConfiguracao1 alter column AssinarSolicitacoesWeb bit;
alter table TBConfiguracao1 alter column TrabalharAdiantOutrasDespesas bit;
alter table TBConfiguracao1 alter column LimiteArquivoWEB int;
alter table TBConfiguracao1 alter column UtilDLPrestContasAdiantamento bit;
alter table TBConfiguracao1 alter column MostrarAssinaturaProjeto bit;
alter table TBConfiguracao1 alter column ApenasContaPadraoEnvioSolicit bit;

alter table TBConfiguracao alter column ApenasContaCorrente bit;
alter table TBConfiguracao alter column SelecaoDeContaWeb bit;
alter table TBConfiguracao alter column FiltrarLancCCWeb bit;
alter table TBConfiguracao alter column RelatWEBFinanceiro bit;
alter table TBConfiguracao alter column EXIBIRRUBRICASUBRUBRICASOLWEB bit;
alter table TBConfiguracao alter column ExibirDataPgtoSolicitacaoPgto bit;
alter table TBConfiguracao alter column ExibirApenasOpcaoValorBruto bit;
alter table TBConfiguracao alter column ExibirOpcaoPoupanca bit;
alter table TBConfiguracao alter column UtilizarValorEstimado bit;
alter table TBConfiguracao alter column TipoRelWebFinProjeto bit;
alter table TBConfiguracao alter column ExibirValorAplicRelFin bit;
alter table TBConfiguracao alter column IncluirAReceberCalculoSaldo bit;
alter table TBConfiguracao alter column AcessoAlunoApenasCPF bit;
alter table TBConfiguracao alter column UtilizarPlanilhaOrcaRubrica bit;
alter table TBConfiguracao alter column ObrigatorioObjetivoCompra bit;
alter table TBConfiguracao alter column UsoEmailLiberarSolicitacao bit;
alter table TBConfiguracao alter column UtilizarDescricaoProduto bit;
alter table TBConfiguracao alter column UtilizarRateioSolicitacoes bit;
alter table TBConfiguracao alter column ExibirEnderecoFundCabecalho bit;
alter table TBConfiguracao alter column MensagemAlertaCoordLiberarSol bit;
alter table TBConfiguracao alter column SolUtilizaSelecaoTipoCompra bit;
alter table TBConfiguracao alter column UtilizarNrProtocoloWEB bit;
alter table TBConfiguracao alter column LiberacaoSolWEBDoisEmails bit;


IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelLancamentoFinanceiro'))
ALTER TABLE TBRemotoProjeto add RelLancamentoFinanceiro bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelLancamentoFinanceiroResumo'))
	ALTER TABLE TBRemotoProjeto add RelLancamentoFinanceiroResumo bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelLancamentoFinanceiroProjeto'))
	ALTER TABLE TBRemotoProjeto add RelLancamentoFinanceiroProjeto bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelLancamentoAplicacao'))
ALTER TABLE TBRemotoProjeto add RelLancamentoAplicacao bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelContasPagarReceber'))
ALTER TABLE TBRemotoProjeto add RelContasPagarReceber bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelRubrica'))
ALTER TABLE TBRemotoProjeto add RelRubrica bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelVerificacaoLancamento'))
ALTER TABLE TBRemotoProjeto add RelVerificacaoLancamento bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolPagamentoPessoaFisica'))
ALTER TABLE TBRemotoProjeto add SolPagamentoPessoaFisica bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolCompras'))
ALTER TABLE TBRemotoProjeto add SolCompras bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolReembolso'))
ALTER TABLE TBRemotoProjeto add SolReembolso bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('AtestarNotaFiscal'))
ALTER TABLE TBRemotoProjeto add AtestarNotaFiscal bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelSubrubrica'))
ALTER TABLE TBRemotoProjeto add RelSubrubrica bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelImpostosPorProjeto'))
ALTER TABLE TBRemotoProjeto add RelImpostosPorProjeto bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('PlanilhaOrcamentaria'))
ALTER TABLE TBRemotoProjeto add PlanilhaOrcamentaria bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolPagamentoPessoaJuridica'))
ALTER TABLE TBRemotoProjeto add SolPagamentoPessoaJuridica bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolCompraDireta'))
ALTER TABLE TBRemotoProjeto add SolCompraDireta bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolAdiantamento'))
ALTER TABLE TBRemotoProjeto add SolAdiantamento bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('DeclaracaoDespesas'))
ALTER TABLE TBRemotoProjeto add DeclaracaoDespesas bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolFaturamento'))
ALTER TABLE TBRemotoProjeto add SolFaturamento bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelMensalidadeAluno'))
ALTER TABLE TBRemotoProjeto add RelMensalidadeAluno bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelCompras'))
ALTER TABLE TBRemotoProjeto add RelCompras bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelPedidoCompra'))
ALTER TABLE TBRemotoProjeto add RelPedidoCompra bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelProvisao'))
ALTER TABLE TBRemotoProjeto add RelProvisao bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolInvoice'))
ALTER TABLE TBRemotoProjeto add SolInvoice bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelProtocolos'))
ALTER TABLE TBRemotoProjeto add RelProtocolos bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolFaturamentoMC'))
ALTER TABLE TBRemotoProjeto add SolFaturamentoMC bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelLancamentoProvisao'))
ALTER TABLE TBRemotoProjeto add RelLancamentoProvisao bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolReembolsoTerceiro'))
ALTER TABLE TBRemotoProjeto add SolReembolsoTerceiro bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('SolBolsa'))
ALTER TABLE TBRemotoProjeto add SolBolsa bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelSaldos'))
ALTER TABLE TBRemotoProjeto add RelSaldos bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelLancamentoCambio'))
ALTER TABLE TBRemotoProjeto add RelLancamentoCambio bit;
GO --


IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelLancamentoFinanceiro'))
Begin
	update TBRemotoProjeto 
	set 
	RelLancamentoFinanceiro = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 1, 1), 0), 
	RelLancamentoAplicacao = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 2, 1), 0), 
	RelContasPagarReceber = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 3, 1), 0), 
	RelRubrica = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 4, 1), 0), 
	RelVerificacaoLancamento = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 5, 1), 0), 
	SolPagamentoPessoaFisica = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 6, 1), 0), 
	SolCompras = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 7, 1), 0), 
	SolReembolso = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 8, 1), 0), 
	AtestarNotaFiscal = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 9, 1), 0), 
	RelSubrubrica = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 10, 1), 0), 
	RelImpostosPorProjeto = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 11, 1), 0), 
	PlanilhaOrcamentaria = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 12, 1), 0), 
	SolPagamentoPessoaJuridica = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 13, 1), 0), 
	SolCompraDireta = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 14, 1), 0), 
	SolAdiantamento = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 15, 1), 0), 
	DeclaracaoDespesas = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 16, 1), 0), 
	SolFaturamento = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 17, 1), 0), 
	RelMensalidadeAluno = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 18, 1), 0), 
	RelCompras = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 19, 1), 0), 
	RelPedidoCompra = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 20, 1), 0), 
	RelProvisao = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 21, 1), 0), 
	SolInvoice = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 22, 1), 0), 
	RelProtocolos = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 23, 1), 0), 
	SolFaturamentoMC = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 24, 1), 0), 
	RelLancamentoProvisao = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 25, 1), 0),
	SolReembolsoTerceiro = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 26, 1), 0),
	SolBolsa = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 26, 1), 0),
	RelSaldos = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 26, 1), 0),
	RelLancamentoCambio = COALESCE(SUBSTRING(T2.PermissoesAcessoRemoto, 26, 1), 0)
	FROM TBRemotoProjeto AS T2
	WHERE IDCoordenador = T2.IDCoordenador
	AND IDProjeto = T2.IDProjeto;
End

alter table TBSolicitacaoCompra alter column AlteracaoLocalEntrega bit;
alter table TBItemSolicitacaoCompra alter column ControleTermico bit;
alter table TBItemSolicitacaoCompra alter column Fragil bit;
alter table TBItemSolicitacaoCompra alter column SolventeReagente bit;
alter table TBItemSolicitacaoCompra alter column Inflamavel bit;
alter table TBItemSolicitacaoCompra alter column Toxico bit;
alter table TBItemSolicitacaoCompra alter column VivoPerecivel bit;
alter table TBItemSolicitacaoCompra alter column ControladoPoliciaFederal bit;
alter table TBItemSolicitacaoCompra alter column ControladoExercito bit;

IF EXISTS (SELECT * 
  FROM sys.foreign_keys 
   WHERE object_id = OBJECT_ID(N'dbo.DF_TBProjeto_UtilizarDefinicaoSubrubrica')
   AND parent_object_id = OBJECT_ID(N'dbo.TBProjeto'))
ALTER TABLE TBProjeto DROP CONSTRAINT DF_TBProjeto_UtilizarDefinicaoSubrubrica;
GO --

alter table TBProjeto alter column UtilizarMetaEtapaProjeto bit;
alter table TBProjeto alter column UtilizarDefinicaoSubrubrica bit;

-- alter table TBPessoaFisicaJuridica add HashSenhaPessoaFisicaJuridica varchar;

alter table TBRemotoProjeto alter column LiberarSolWeb1 bit;
alter table TBRemotoProjeto alter column LiberarSolWeb2 bit;

alter table TBSolicitacao alter column PertenceQuadro bit;
alter table TBSolicitacao alter column INSSOutraFonte bit;

IF EXISTS (SELECT Name FROM sysindexes WHERE Name = 'IX_ApenasIda') DROP INDEX TBSolicitacaoReembolso.IX_ApenasIda;
alter table TBSolicitacaoReembolso alter column ApenasIda bit;
alter table TBSolicitacaoReembolso alter column DiariaCeletista bit;

IF EXISTS (SELECT Name FROM sysindexes WHERE Name = 'IX_IDCodItemServico') DROP INDEX TBSolicitacaoFaturamento.IX_IDCodItemServico;
alter table TBSolicitacaoFaturamento alter column IDCodItemServico varchar(10);

IF EXISTS (SELECT Name FROM sysindexes WHERE Name = 'IX_IDCodItemServico') DROP INDEX TBSolicitacaoFaturamentoMC.IX_IDCodItemServico;
alter table TBSolicitacaoFaturamentoMC alter column IDCodItemServico varchar(10);

alter table TBServicoProjeto alter column Inativo bit;

alter table TBConfiguracao1 alter column UtilizarSolAdiantPassagemWEB bit;

alter table TBPessoaFisicaJuridica alter column FuncionarioFundacao smallint;
alter table TBPessoaFisicaJuridica alter column PessoaFisicaJuridica smallint;
alter table TBPessoaFisicaJuridica alter column EstadoCivil smallint;
alter table TBPessoaFisicaJuridica alter column Sexo smallint;
alter table TBPessoaFisicaJuridica alter column TipoQuadro smallint;

alter table TBSolicitacaoFaturamento alter column StatusSolicitacao int;
alter table TBSolicitacaoFaturamento alter column LiberacaoCoordenador float;
alter table TBSolicitacaoFaturamento alter column LiberacaoCoordenador2 float;

alter table TBSolicitacaoFaturamentoMC alter column StatusSolicitacao int;
alter table TBSolicitacaoFaturamentoMC alter column LiberacaoCoordenador float;
alter table TBSolicitacaoFaturamentoMC alter column LiberacaoCoordenador2 float;

if not exists (select * from sysobjects where name='TBApi' and xtype='U')
CREATE TABLE TBApi(
	[Hash] [varchar](300) NOT NULL,
	[CriadoEm] [datetime] NOT NULL
) ON [PRIMARY];
GO --

alter table TBSolicitacaoAdiantamento alter column StatusSolicitacao int;
alter table TBSolicitacaoAdiantamento alter column LiberacaoCoordenador float;
alter table TBSolicitacaoAdiantamento alter column LiberacaoCoordenador2 float;

--alter table TBRubrica alter column IDRubrica int;
--alter table TBSubRubrica alter column IDSubRubrica int;

alter table TBSolAdiantDespViagensRateio alter column CNPJ varchar(20) null;
alter table TBSolAdiantDespViagensRateio alter column Empresa varchar(150) null;
alter table TBSolAdiantDespViagensRateio alter column Valor float null;
alter table TBSolAdiantDespViagensRateio alter column Obs varchar(150) null;

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseSolBolsa'))
alter table TBConfiguracao1 add PastaBaseSolBolsa varchar(200);
GO --

if not exists (select * from sysobjects where name='TBSolicitacaoBolsa' and xtype='U')
CREATE TABLE TBSolicitacaoBolsa(
	[IDSolicitacaoBolsa] [int] IDENTITY(1,1) NOT NULL,
	[IDCoordenador] [int] NULL,
	[IDProjeto] [int] NULL,
	[TipoBolsa] [tinyint] NULL,
	[DataSolicitacao] [float] NULL,
	[Nome] [varchar](100) NULL,
	[CPFCNPJ] [varchar](20) NULL,
	[Observacao] [text] NULL,
	[Agencia] [varchar](20) NULL,
	[Banco] [varchar](20) NULL,
	[Conta] [varchar](20) NULL,
	[TipoConta] [int] NULL,
	[ValorContrato] [float] NULL,
	[DataNascimento] [int] NULL,
	[Identidade] [varchar](20) NULL,
	[INSS] [varchar](20) NULL,
	[PISPASEP] [varchar](20) NULL,
	[Endereco] [varchar](60) NULL,
	[Numero] [varchar](10) NULL,
	[Complemento] [varchar](30) NULL,
	[Bairro] [varchar](60) NULL,
	[Cidade] [varchar](60) NULL,
	[CEP] [varchar](9) NULL,
	[UF] [varchar](2) NULL,
	[Telefone1] [varchar](20) NULL,
	[Telefone2] [varchar](20) NULL,
	[Email] [varchar](300) NULL,
	[StatusSolicitacao] [int] NULL,
	[DataLiberacao] [int] NULL,
	[ObservacaoLiberacao] [varchar](200) NULL,
	[LiberacaoCoordenador] [float] NULL,
	[DadosVerificados] [int] NULL,
	[IDUsuario] [int] NULL,
	[LiberacaoCoordenador2] [float] NULL,
	[IDContrato] [int] NULL,
	[IDLiberadoCoordenador] [int] NULL,
	[IDLiberadoCoordenador2] [int] NULL,
	[DataVigenciaInicio] [int] NULL,
	[DataVigenciaFim] [int] NULL,
	[IDTipoContrato] [int] NULL,
	[IDSubTipoContrato] [int] NULL,
 CONSTRAINT [PK__TBSolici__0CF517A5115AACD6] PRIMARY KEY CLUSTERED 
(
	[IDSolicitacaoBolsa] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];
GO --

update TBPessoaFisicaJuridica set PISPASEP = '' where PISPASEP = '   .     .  / ';
update TBPessoaFisicaJuridica set INSS = '' where INSS = '   .     .  / ';

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('NaoRepetirSenhaWeb'))
alter table TBConfiguracao1 add NaoRepetirSenhaWeb bit
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('RegistrarLogAcoesWeb'))
alter table TBConfiguracao1 add RegistrarLogAcoesWeb bit
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('QuantidadeTentativaLogin'))
alter table TBConfiguracao1 add QuantidadeTentativaLogin int
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('ExpirarSenhaWebDias'))
alter table TBConfiguracao1 add ExpirarSenhaWebDias int

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('RelLancamentoFinanceiroResumo'))
ALTER TABLE TBConfiguracao1 add RelLancamentoFinanceiroResumo bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('RelLancamentoFinanceiroProjeto'))
ALTER TABLE TBConfiguracao1 add RelLancamentoFinanceiroProjeto bit;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('DataAlteracaoSenhaWeb'))
alter table TBConfiguracao1 add DataAlteracaoSenhaWeb date

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('QuantidadeErroSenhaWeb'))
alter table TBConfiguracao1 add QuantidadeErroSenhaWeb int

GO --

update TBConfiguracao1
set NaoRepetirSenhaWeb = 0
where NaoRepetirSenhaWeb is null

GO --
update TBConfiguracao1
set RegistrarLogAcoesWeb = 0
where RegistrarLogAcoesWeb is null

GO --
update TBConfiguracao1
set QuantidadeTentativaLogin = 0
where QuantidadeTentativaLogin is null

GO --
update TBConfiguracao1
set ExpirarSenhaWebDias = 0
where ExpirarSenhaWebDias is null

GO --
update TBPessoaFisicaJuridica
set QuantidadeErroSenhaWeb = 0
where QuantidadeErroSenhaWeb is null

GO --
update TBPessoaFisicaJuridica
set DataAlteracaoSenhaWeb = '1900-01-01'
where DataAlteracaoSenhaWeb is null

GO --

if not exists (select * from sysobjects where name='TBHistoricoSenhaWeb' and xtype='U')
CREATE TABLE TBHistoricoSenhaWeb(
	IDHistoricoSenhaWeb int IDENTITY(1,1) NOT NULL,
	IDPessoaFisicaJuridica int NULL,
	SenhaPessoaFisicaJuridica varchar(18) NULL,
 CONSTRAINT [pk_idhistoricosenhaweb] PRIMARY KEY CLUSTERED 
(
	[IDHistoricoSenhaWeb] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY];

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolso') AND name IN ('IdProjetoValorDiaria'))
alter table TBSolicitacaoReembolso 
add IdProjetoValorDiaria int 

GO --

update TBSolicitacaoReembolso
set IdProjetoValorDiaria = 0
where IdProjetoValorDiaria is null

GO -- 

Alter table TBPessoaFisicaJuridica
alter column SenhaPessoaFisicaJuridica varchar(36)

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('QuantidadeMinSenha'))
alter table TBConfiguracao1 add QuantidadeMinSenha int

GO --

update TBConfiguracao1
set QuantidadeMinSenha = 0
where QuantidadeMinSenha is null

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('ObrigatorioCNPJUnidadeInstituicao'))
alter table TBConfiguracao1 add ObrigatorioCNPJUnidadeInstituicao bit

GO --

update TBConfiguracao1
set ObrigatorioCNPJUnidadeInstituicao = 0
where ObrigatorioCNPJUnidadeInstituicao is null

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('ObrigatorioUnidadeInstituicao'))
alter table TBConfiguracao1 add ObrigatorioUnidadeInstituicao bit

GO --

update TBConfiguracao1
set ObrigatorioUnidadeInstituicao = 0
where ObrigatorioUnidadeInstituicao is null

GO --

if not exists (select * from sysobjects where name='TBMensagemSistema' and xtype='U')
CREATE TABLE [dbo].[TBMensagemSistema](
	[IDMensagemSistema] [int] IDENTITY(1,1) NOT NULL,
	[DataHora] [datetime] NULL,
	[UsuarioSistema] [varchar](max) NULL,
	[ID] [int] NULL,
	[Modulo] [int] NULL,
	[InternaExterna] [int] NULL,
	[Leitura] [datetime] NULL,
	[CampoMensagem] [varchar](max) NULL,
 CONSTRAINT [PK_TBMensagemSistema] PRIMARY KEY CLUSTERED 
(
	[IDMensagemSistema] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];
GO --

Alter table TBItemSolicitacaoCompra alter column materialConsumo bit
Alter table TBItemSolicitacaoCompra alter column DespesaCapital bit

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('UtilizarDadosFundacaoNF'))
alter table TBConfiguracao1 add UtilizarDadosFundacaoNF bit

GO --

update TBConfiguracao1
set UtilizarDadosFundacaoNF = 0
where UtilizarDadosFundacaoNF is null

GO --

if not exists (select * from sysobjects where name='TBAcompanhamentoProjetoFUSP' and xtype='U')
Create Table TBAcompanhamentoProjetoFUSP
(
	IDAcompanhamentoProjetoFUSP int primary key identity(1,1),
	IDProjeto int,
	NumeroRelatorio int,
	PeriodoRelatorioInicio Date,
	PeriodoRelatorioFim Date,
	PrevisaoConclusao Date,
	AtividadesIniciadas bit,
	AtividadesIniciadasJustificativa varchar(max),
	CronogramaCumprido bit,
	CronogramaCumpridoJustificativa varchar(max),
	PlanoOrcamentoCumprido bit,
	PlanoOrcamentoCumpridoJustificativa varchar(max),
	NormasRespeitadas bit,
	NormasRespeitadasJustificativa varchar(max),
	Intercorrencia bit,
	IntercorrenciaJustificativa varchar(max),
	ComentariosAdicionais varchar(max),
	DataAssinatura Datetime,
	IDAssinatura int
)

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('NomeResponsavel'))
Alter table TBConfiguracao1 add NomeResponsavel Varchar(30)

GO --

if not exists (select * from sysobjects where name='TBConfiguracaoWeb' and xtype='U')
Begin
	Create Table TBConfiguracaoWeb
	(
		IDConfiguracaoWeb int primary key identity(1,1),
		SolPgtoDeposito bit default 1,
		SolPgtoCheque bit default 1,
		SolPgtoOP bit default 1,
		SolPgtoBoleto bit default 1,
		SolPgtoValorBruto bit default 1,
		SolPgtoValorLiquido  bit default 1,
		SolReembDiariaDeposito bit default 1,
		SolReembDiariaCheque bit default 1,
		SolReembDiariaOP bit default 1,
		SolReembDiariaBoleto bit default 1,
		SolAdiantamentoDeposito bit default 1,
		SolAdiantamentoCheque bit default 1,
		SolAdiantamentoOP bit default 1,
		SolAdiantamentoBoleto bit default 1	
	)

	Insert into TBConfiguracaoWeb (SolPgtoDeposito)
	Values (1)
End

GO --

if ((select coalesce((Select count(*) from TBConfiguracaoWeb),0)) = 0)
Begin
	Insert into TBConfiguracaoWeb (SolPgtoDeposito)
	Values (1)
End

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolsoTerceiro') AND name IN ('IDCoordenador'))
Begin
	Alter table TBSolicitacaoReembolsoTerceiro add IDCoordenador int
	Alter table TBSolicitacaoAdiantamento alter column DataSolicitacao float
	Alter table TBSolicitacaoFaturamento alter column DataSolicitacao float
	Alter table TBSolicitacaoReembolsoTerceiro alter column DataSolicitacao float
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolAdiantamentoTipoContaCC'))
Begin
	Alter table TBConfiguracaoWeb add SolAdiantamentoTipoContaCC bit default 1
	Alter table TBConfiguracaoWeb add SolAdiantamentoTipoContaCP bit default 1
	Alter table TBConfiguracaoWeb add SolAdiantamentoTipoContaCS bit default 1

	Alter table TBConfiguracaoWeb add SolBolsaTipoContaCC bit default 1
	Alter table TBConfiguracaoWeb add SolBolsaTipoContaCP bit default 1
	Alter table TBConfiguracaoWeb add SolBolsaTipoContaCS bit default 1

	Alter table TBConfiguracaoWeb add SolReembolsoTipoContaCC bit default 1
	Alter table TBConfiguracaoWeb add SolReembolsoTipoContaCP bit default 1
	Alter table TBConfiguracaoWeb add SolReembolsoTipoContaCS bit default 1

	Alter table TBConfiguracaoWeb add SolPgtoPFTipoContaCC bit default 1
	Alter table TBConfiguracaoWeb add SolPgtoPFTipoContaCP bit default 1
	Alter table TBConfiguracaoWeb add SolPgtoPFTipoContaCS bit default 1

	Alter table TBConfiguracaoWeb add SolPgtoPJTipoContaCC bit default 1
	Alter table TBConfiguracaoWeb add SolPgtoPJTipoContaCP bit default 1
	Alter table TBConfiguracaoWeb add SolPgtoPJTipoContaCS bit default 1

	Alter table TBConfiguracaoWeb add SolReembolsoTerceiroTipoContaCC bit default 1
	Alter table TBConfiguracaoWeb add SolReembolsoTerceiroTipoContaCP bit default 1
	Alter table TBConfiguracaoWeb add SolReembolsoTerceiroTipoContaCS bit default 1
	
	Alter table TBConfiguracaoWeb add InstrucoesSolAdiantamento varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesSolBolsa varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesSolCompra varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesSolReembolso varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesSolFaturamento varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesSolPagamentoPF varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesSolPagamentoPJ varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesSolReembolsoTerceiro varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolAdiantamentoMsgConfirmacao'))
Begin
	Alter table TBConfiguracaoWeb add SolAdiantamentoMsgConfirmacao varchar(max)
	Alter table TBConfiguracaoWeb add SolBolsaMsgConfirmacao varchar(max)
	Alter table TBConfiguracaoWeb add SolCompraMsgConfirmacao varchar(max)
	Alter table TBConfiguracaoWeb add SolReembolsoMsgConfirmacao varchar(max)
	Alter table TBConfiguracaoWeb add SolFaturamentoMsgConfirmacao varchar(max)
	Alter table TBConfiguracaoWeb add SolPagamentoPFMsgConfirmacao varchar(max)
	Alter table TBConfiguracaoWeb add SolPagamentoPJMsgConfirmacao varchar(max)
	Alter table TBConfiguracaoWeb add SolReembolsoTerceiroMsgConfirmacao varchar(max)

	Update TBConfiguracaoWeb
	set	
		InstrucoesSolCompra = c.InstrucoesSolicitacaoCompras,
		InstrucoesSolReembolso = c.InstrucoesSolicitacaoReembolso,
		InstrucoesSolFaturamento = c.InstrucoesSolicitacaoFaturamento,
		InstrucoesSolPagamentoPF = C.InstrucoesSolicitacaoPagamentoPf,
		InstrucoesSolPagamentoPJ = C.InstrucoesSolicitacaoPagamentoPj,
		InstrucoesSolReembolsoTerceiro = InstrucoesSolicitacaoReembolsoTerceiro
	from  TBConfiguracaoWeb CW
	full join TBConfiguracao1 C
	on (CW.IDConfiguracaoWeb > 0)
End

GO --

if (not exists (select * from sysobjects where name='tgr_pessoa'))
Begin
	Update TBPessoaFisicaJuridica
	set DataAlteracaoSenhaWeb = '1900-01-01'
	where DataAlteracaoSenhaWeb is null

	Declare @trig varchar(max)

	set @trig = 'CREATE TRIGGER tgr_pessoa
					ON  TBPessoaFisicaJuridica
				AFTER Insert
				AS 
				BEGIN
					Update TBPessoaFisicaJuridica
					set DataAlteracaoSenhaWeb = ''1900-01-01''
					where DataAlteracaoSenhaWeb is null
				END'
	exec(@trig)
End

GO --

if (not exists (select * from sysobjects where name='RetornaProjetoSolPgtoRateio'))
Begin
	Declare @funct varchar(max)

	set @funct = '
		Create FUNCTION RetornaProjetoSolPgtoRateio (    @IDSol int  )   
		RETURNS varchar(max)   
		AS   
		BEGIN     
			DECLARE @Projetos as varchar(max)    
			Declare @AuxNomeProjeto as varchar(max)    
	
			set @Projetos = ''''  
			DECLARE CURSOR_OBJECTS_SolProjetos 
			CURSOR FOR   
				Select P.NomeProjeto
				From TBSolicitacaoHasSolicitacao SHS with(nolock) 
				inner join TBSolicitacaoRateio SR with(nolock) 
				on (SHS.IDSolicitacao = Sr.IDSolicitacao)
				Inner Join TBProjeto P with(nolock) 
				on (SR.IDProjeto = P.IDProjeto)
				where SHS.IDSolicitacaoHasSolicitacao = @IDSol   
	
			OPEN CURSOR_OBJECTS_SolProjetos     
	
			FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			WHILE @@FETCH_STATUS = 0     
			BEGIN      
				IF (len(@Projetos) = 0)        
				Set @Projetos = @AuxNomeProjeto;      
				Else        
				Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
				FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			END     
	
			CLOSE CURSOR_OBJECTS_SolProjetos     
			DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
			RETURN coalesce(@Projetos,'''') 
		END'

	exec(@funct)
end

If ((SELECT coalesce(max_length,0) FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemSolicitacaoCompra') AND name IN ('ObservacaoItemCompra')) = 120)
Begin
	Alter table TBItemSolicitacaoCompra
	alter column ObservacaoItemCompra varchar(500) 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseAcompanhamentoProjeto'))
alter table TBConfiguracaoWeb add PastaBaseAcompanhamentoProjeto varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolCompras'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolCompras varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolPagamentos'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolPagamentos varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolReembolso'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolReembolso varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolFaturamento'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolFaturamento varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolFaturamentoMC'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolFaturamentoMC varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolAdiantamento'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolAdiantamento varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolReembolsoTerceiro'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolReembolsoTerceiro varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseContratos'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseContratos varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBasePFJ'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBasePFJ varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseProjetos'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseProjetos varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseAcompanhamentoProjeto_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseAcompanhamentoProjeto_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseCertidoes'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseCertidoes varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseCertidoes'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseCertidoes varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseCertidoes_Azure'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseCertidoes_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseCertidoes_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseCertidoes_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseCertidoes_AmazonS3'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseCertidoes_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseCertidoes_AmazonS3'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseCertidoes_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseWebCertidoes'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseWebCertidoes varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseWebCertidoes'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseWebCertidoes varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseWebCertidoes_Azure'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseWebCertidoes_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseWebCertidoes_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseWebCertidoes_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseWebCertidoes_AmazonS3'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseWebCertidoes_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseWebCertidoes_AmazonS3'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseWebCertidoes_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseContratos_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseContratos_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBasePFJ_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBasePFJ_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseProjetos_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseProjetos_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolAdiantamento_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolAdiantamento_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolBolsa_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolBolsa_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolCompras_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolCompras_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolFaturamento_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolFaturamento_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolFaturamentoMC_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolFaturamentoMC_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolicitacaoCursoEventoServico_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolicitacaoCursoEventoServico_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolPagamentos_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolPagamentos_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolReembolso_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolReembolso_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolReembolsoTerceiro_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolReembolsoTerceiro_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseWebPFJ'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseWebPFJ varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseWebPFJ_Azure'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseWebPFJ_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseWebPFJ'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseWebPFJ varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseWebPFJ_Azure'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseWebPFJ_Azure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBasePFJ_AmazonS3'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBasePFJ_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseWebPFJ_AmazonS3'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseWebPFJ_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolBolsa_AmazonS3'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolBolsa_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolRPA_AmazonS3'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolRPA_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolicitacaoCursoEventoServico_AmazonS3'))
ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolicitacaoCursoEventoServico_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBasePFJ_AmazonS3'))
ALTER TABLE TBConfiguracao1 ADD PastaBasePFJ_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseWebPFJ_AmazonS3'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseWebPFJ_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseSolBolsa_AmazonS3'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseSolBolsa_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseSolRPA_AmazonS3'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseSolRPA_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseSolicitacaoCursoEventoServico_AmazonS3'))
ALTER TABLE TBConfiguracao1 ADD PastaBaseSolicitacaoCursoEventoServico_AmazonS3 varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('OpcaoArmazenamentoWeb'))
ALTER TABLE TBConfiguracaoWeb ADD OpcaoArmazenamentoWeb INT NOT NULL DEFAULT 1;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ContaAzure'))
ALTER TABLE TBConfiguracaoWeb ADD ContaAzure varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SenhaAzure'))
ALTER TABLE TBConfiguracaoWeb ADD SenhaAzure text;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('InstrucoesSolTransferencia'))
Begin
	Alter table TBConfiguracaoWeb add 
		PastaBaseSolicitacaoTransferencia varchar(200),
		PastaBaseSolicitacaoTransferencia_Azure varchar(200),
		PastaBaseSolicitacaoTransferencia_AmazonS3 varchar(200),
		InstrucoesSolTransferencia varchar(max),
		SolTransferenciaMsgConfirmacao varchar(max)
END
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBconfiguracao1') AND name IN ('PastaBaseSolFornecedores_AmazonS3'))
Begin
	ALTER TABLE TBconfiguracao1 ADD PastaBaseSolFornecedores_AmazonS3 VARCHAR(300)
	ALTER TABLE TBconfiguracao1 ADD PastaBaseSolTransferencias_AmazonS3 VARCHAR(300)
END
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseAtividadeContrato'))
alter table TBConfiguracao1 add PastaBaseAtividadeContrato varchar(200);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseAtividadeContrato'))
alter table TBConfiguracaoWeb add PastaBaseAtividadeContrato varchar(200);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseAtividadeContrato_Azure'))
alter table TBConfiguracao1 add PastaBaseAtividadeContrato_Azure varchar(200);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseAtividadeContrato_Azure'))
alter table TBConfiguracaoWeb add PastaBaseAtividadeContrato_Azure varchar(200);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseAtividadeContrato_AmazonS3'))
alter table TBConfiguracao1 add PastaBaseAtividadeContrato_AmazonS3 varchar(200);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseAtividadeContrato_AmazonS3'))
alter table TBConfiguracaoWeb add PastaBaseAtividadeContrato_AmazonS3 varchar(200);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBasePesquisaCompra'))
alter table TBConfiguracao1 add PastaBasePesquisaCompra varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBasePesquisaCompra'))
alter table TBConfiguracaoWeb add PastaBasePesquisaCompra varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBasePesquisaCompra_Azure'))
alter table TBConfiguracao1 add PastaBasePesquisaCompra_Azure varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBasePesquisaCompra_Azure'))
alter table TBConfiguracaoWeb add PastaBasePesquisaCompra_Azure varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBasePesquisaCompra_AmazonS3'))
alter table TBConfiguracao1 add PastaBasePesquisaCompra_AmazonS3 varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBasePesquisaCompra_AmazonS3'))
alter table TBConfiguracaoWeb add PastaBasePesquisaCompra_AmazonS3 varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseInscricao'))
alter table TBConfiguracao1 add PastaBaseInscricao varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseInscricao'))
alter table TBConfiguracaoWeb add PastaBaseInscricao varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseInscricao_Azure'))
alter table TBConfiguracao1 add PastaBaseInscricao_Azure varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseInscricao_Azure'))
alter table TBConfiguracaoWeb add PastaBaseInscricao_Azure varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseInscricao_AmazonS3'))
alter table TBConfiguracao1 add PastaBaseInscricao_AmazonS3 varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseInscricao_AmazonS3'))
alter table TBConfiguracaoWeb add PastaBaseInscricao_AmazonS3 varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('TextoDeclaracaoResumoAceiteFornecedor'))
alter table TBConfiguracao1 add TextoDeclaracaoResumoAceiteFornecedor varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('TextoDeclaracaoResumoAceiteFornecedor'))
alter table TBConfiguracaoWeb add TextoDeclaracaoResumoAceiteFornecedor varchar(max);

GO --

Declare @trig_configuracao_web varchar(max)
IF (not exists (select * from sysobjects where name='tgr_pasta_local_configuracao_web'))
Begin
	set @trig_configuracao_web = '	CREATE TRIGGER tgr_pasta_local_configuracao_web
					ON TBConfiguracaoWeb
					AFTER UPDATE
					AS
					BEGIN
						IF TRIGGER_NESTLEVEL() > 1
							RETURN

						DECLARE
						@IDConfiguracaoWeb int,
						@PastaBaseSolCompras varchar(200),
						@PastaBaseSolPagamentos varchar(200),
						@PastaBaseSolReembolso varchar(200),
						@PastaBaseSolFaturamento varchar(200),
						@PastaBaseSolFaturamentoMC varchar(200),
						@PastaBaseSolAdiantamento varchar(200),
						@PastaBaseSolReembolsoTerceiro varchar(200),
						@PastaBaseCertidoes varchar(200),
						@PastaBaseWebCertidoes varchar(200),
						-- @PastaBaseProjetos varchar(200),
						@PastaBaseContratos varchar(200),
						@PastaBasePFJ varchar(200), 
						@PastaBaseWebPFJ varchar(200), 

						@OpcaoArmazenamentoWeb int, 
						@ContaAzure varchar(200),
						-- @SenhaAzure varchar(400),

						-- @PastaBaseAcompanhamentoProjeto_Azure varchar(200), 
						@PastaBaseCertidoes_Azure varchar(200), 
						@PastaBaseWebCertidoes_Azure varchar(200),
						@PastaBaseContratos_Azure varchar(200),
						-- @PastaBasePFJ_Azure varchar(200),
						@PastaBaseProjetos_Azure varchar(200),
						@PastaBaseSolAdiantamento_Azure varchar(200),
						-- @PastaBaseSolBolsa_Azure varchar(200),
						@PastaBaseSolCompras_Azure varchar(200),
						@PastaBaseSolFaturamento_Azure varchar(200),
						@PastaBaseSolFaturamentoMC_Azure varchar(200),
						-- @PastaBaseSolicitacaoCursoEventoServico_Azure varchar(200),
						@PastaBaseSolPagamentos_Azure varchar(200),
						@PastaBaseSolReembolso_Azure varchar(200),
						@PastaBaseSolReembolsoTerceiro_Azure varchar(200),
						@PastaBaseWebPFJ_Azure varchar(200),
						@PastaBaseCertidoes_AmazonS3 varchar(200),
						@PastaBaseWebCertidoes_AmazonS3 varchar(200),
						@PastaBasePFJ_AmazonS3 varchar(200),
						@PastaBaseWebPFJ_AmazonS3 varchar(200),
						@PastaBaseSolBolsa_AmazonS3 varchar(200),
						@PastaBaseSolRPA_AmazonS3 varchar(200),
						@PastaBaseSolicitacaoCursoEventoServico_AmazonS3 varchar(200),
						@PastaBaseSolicitacaoTransferencia_AmazonS3 varchar(200),
						@PastaBaseAtividadeContrato varchar(200),
						@PastaBaseAtividadeContrato_Azure varchar(200),
						@PastaBaseAtividadeContrato_AmazonS3 varchar(200),
						@PastaBasePesquisaCompra varchar(max),
						@PastaBasePesquisaCompra_Azure varchar(max),
						@PastaBasePesquisaCompra_AmazonS3 varchar(max),
						@PastaBaseInscricao varchar(max),
						@PastaBaseInscricao_Azure varchar(max),
						@PastaBaseInscricao_AmazonS3 varchar(max),
						@TextoDeclaracaoResumoAceiteFornecedor varchar(max)

						SELECT 
							@IDConfiguracaoWeb = IDConfiguracaoWeb,
							@PastaBaseSolCompras = PastaBaseSolCompras,
							@PastaBaseSolPagamentos = PastaBaseSolPagamentos,
							@PastaBaseSolReembolso = PastaBaseSolReembolso,
							@PastaBaseSolFaturamento = PastaBaseSolFaturamento,
							@PastaBaseSolFaturamentoMC = PastaBaseSolFaturamentoMC,
							@PastaBaseSolAdiantamento = PastaBaseSolAdiantamento,
							@PastaBaseSolReembolsoTerceiro = PastaBaseSolReembolsoTerceiro,
							@PastaBaseCertidoes = PastaBaseCertidoes,
							@PastaBaseWebCertidoes = PastaBaseWebCertidoes,
							-- @PastaBaseProjetos = PastaBaseProjetos,
							@PastaBaseContratos = PastaBaseContratos,
							@PastaBasePFJ = PastaBasePFJ,
							@PastaBaseWebPFJ = PastaBaseWebPFJ,
							
							@OpcaoArmazenamentoWeb = OpcaoArmazenamentoWeb,
							@ContaAzure = ContaAzure,
							-- @SenhaAzure = SenhaAzure,
							
							-- @PastaBaseAcompanhamentoProjeto_Azure = PastaBaseAcompanhamentoProjeto_Azure,
							@PastaBaseCertidoes_Azure = PastaBaseCertidoes_Azure,
							@PastaBaseWebCertidoes_Azure = PastaBaseWebCertidoes_Azure,
							@PastaBaseContratos_Azure = PastaBaseContratos_Azure,
							-- @PastaBasePFJ_Azure = PastaBasePFJ_Azure,
							@PastaBaseProjetos_Azure = PastaBaseProjetos_Azure,
							@PastaBaseSolAdiantamento_Azure = PastaBaseSolAdiantamento_Azure,
							-- @PastaBaseSolBolsa_Azure = PastaBaseSolBolsa_Azure,
							@PastaBaseSolCompras_Azure = PastaBaseSolCompras_Azure,
							@PastaBaseSolFaturamento_Azure = PastaBaseSolFaturamento_Azure,
							@PastaBaseSolFaturamentoMC_Azure = PastaBaseSolFaturamentoMC_Azure,
							-- @PastaBaseSolicitacaoCursoEventoServico_Azure = PastaBaseSolicitacaoCursoEventoServico_Azure,
							@PastaBaseSolPagamentos_Azure = PastaBaseSolPagamentos_Azure,
							@PastaBaseSolReembolso_Azure = PastaBaseSolReembolso_Azure,
							@PastaBaseSolReembolsoTerceiro_Azure = PastaBaseSolReembolsoTerceiro_Azure,
							@PastaBaseWebPFJ_Azure = PastaBaseWebPFJ_Azure,
							@PastaBaseCertidoes_AmazonS3 = PastaBaseCertidoes_AmazonS3,
							@PastaBaseWebCertidoes_AmazonS3 = PastaBaseWebCertidoes_AmazonS3,
							@PastaBasePFJ_AmazonS3 = PastaBasePFJ_AmazonS3,
							@PastaBaseWebPFJ_AmazonS3 = PastaBaseWebPFJ_AmazonS3,
							@PastaBaseSolBolsa_AmazonS3 = PastaBaseSolBolsa_AmazonS3,
							@PastaBaseSolRPA_AmazonS3 = PastaBaseSolRPA_AmazonS3,
							@PastaBaseSolicitacaoCursoEventoServico_AmazonS3 = PastaBaseSolicitacaoCursoEventoServico_AmazonS3,
							@PastaBaseSolicitacaoTransferencia_AmazonS3 = PastaBaseSolicitacaoTransferencia_AmazonS3,
							@PastaBaseAtividadeContrato = PastaBaseAtividadeContrato,
							@PastaBaseAtividadeContrato_Azure = PastaBaseAtividadeContrato_Azure,
							@PastaBaseAtividadeContrato_AmazonS3 = PastaBaseAtividadeContrato_AmazonS3,
							@PastaBasePesquisaCompra = PastaBasePesquisaCompra,
							@PastaBasePesquisaCompra_Azure = PastaBasePesquisaCompra_Azure,
							@PastaBasePesquisaCompra_AmazonS3 = PastaBasePesquisaCompra_AmazonS3,
							@PastaBaseInscricao = PastaBaseInscricao,
							@PastaBaseInscricao_Azure = PastaBaseInscricao_Azure,
							@PastaBaseInscricao_AmazonS3 = PastaBaseInscricao_AmazonS3,
							@TextoDeclaracaoResumoAceiteFornecedor = TextoDeclaracaoResumoAceiteFornecedor
						FROM
							INSERTED;

						UPDATE TBConfiguracao1 SET 
							PastaBaseSolCompras = @PastaBaseSolCompras,
							PastaBaseSolPagamentos = @PastaBaseSolPagamentos,
							PastaBaseSolReembolso = @PastaBaseSolReembolso,
							PastaBaseSolFaturamento = @PastaBaseSolFaturamento,
							PastaBaseSolFaturamentoMC = @PastaBaseSolFaturamentoMC,
							PastaBaseSolAdiantamento = @PastaBaseSolAdiantamento,
							PastaBaseSolReembolsoTerceiro = @PastaBaseSolReembolsoTerceiro,
							PastaBaseCertidoes = @PastaBaseCertidoes,
							PastaBaseWebCertidoes = @PastaBaseWebCertidoes,
							-- PastaBaseProjetos = @PastaBaseProjetos,
							PastaBaseContratos = @PastaBaseContratos,
							PastaBasePFJ = @PastaBasePFJ,	
							PastaBaseWebPFJ = @PastaBaseWebPFJ, 

							OpcaoArmazenamentoWeb = @OpcaoArmazenamentoWeb, 
							ContaAzure = @ContaAzure,
							-- SenhaAzure = @SenhaAzure,
							
							-- PastaBaseAcompanhamentoProjeto_Azure = @PastaBaseAcompanhamentoProjeto_Azure,
							PastaBaseCertidoes_Azure = @PastaBaseCertidoes_Azure,
							PastaBaseWebCertidoes_Azure = @PastaBaseWebCertidoes_Azure,
							PastaBaseContratos_Azure = @PastaBaseContratos_Azure,
							-- PastaBasePFJ_Azure = @PastaBasePFJ_Azure,
							PastaBaseProjetos_Azure = @PastaBaseProjetos_Azure,
							PastaBaseSolAdiantamento_Azure = @PastaBaseSolAdiantamento_Azure,
							-- PastaBaseSolBolsa_Azure = @PastaBaseSolBolsa_Azure,
							PastaBaseSolCompras_Azure = @PastaBaseSolCompras_Azure,
							PastaBaseSolFaturamento_Azure = @PastaBaseSolFaturamento_Azure,
							PastaBaseSolFaturamentoMC_Azure = @PastaBaseSolFaturamentoMC_Azure,
							-- PastaBaseSolicitacaoCursoEventoServico_Azure = @PastaBaseSolicitacaoCursoEventoServico_Azure,
							PastaBaseSolPagamentos_Azure = @PastaBaseSolPagamentos_Azure,
							PastaBaseSolReembolso_Azure = @PastaBaseSolReembolso_Azure,
							PastaBaseSolReembolsoTerceiro_Azure = @PastaBaseSolReembolsoTerceiro_Azure, 
							PastaBaseWebPFJ_Azure = @PastaBaseWebPFJ_Azure,
							PastaBaseCertidoes_AmazonS3 = @PastaBaseCertidoes_AmazonS3,
							PastaBaseWebCertidoes_AmazonS3 = @PastaBaseWebCertidoes_AmazonS3,
							PastaBasePFJ_AmazonS3 = @PastaBasePFJ_AmazonS3,
							PastaBaseWebPFJ_AmazonS3 = @PastaBaseWebPFJ_AmazonS3,
							PastaBaseSolBolsa_AmazonS3 = @PastaBaseSolBolsa_AmazonS3,
							PastaBaseSolRPA_AmazonS3 = @PastaBaseSolRPA_AmazonS3,
							PastaBaseSolicitacaoCursoEventoServico_AmazonS3 = @PastaBaseSolicitacaoCursoEventoServico_AmazonS3,
							PastaBaseSolTransferencias_AmazonS3 = @PastaBaseSolicitacaoTransferencia_AmazonS3,
							PastaBaseAtividadeContrato = @PastaBaseAtividadeContrato,
							PastaBaseAtividadeContrato_Azure = @PastaBaseAtividadeContrato_Azure,
							PastaBaseAtividadeContrato_AmazonS3 = @PastaBaseAtividadeContrato_AmazonS3,
							PastaBasePesquisaCompra = @PastaBasePesquisaCompra,
							PastaBasePesquisaCompra_Azure = @PastaBasePesquisaCompra_Azure,
							PastaBasePesquisaCompra_AmazonS3 = @PastaBasePesquisaCompra_AmazonS3,
							PastaBaseInscricao = @PastaBaseInscricao,
							PastaBaseInscricao_Azure = @PastaBaseInscricao_Azure,
							PastaBaseInscricao_AmazonS3 = @PastaBaseInscricao_AmazonS3,
							TextoDeclaracaoResumoAceiteFornecedor = @TextoDeclaracaoResumoAceiteFornecedor
						WHERE 
							IDConfiguracao = @IDConfiguracaoWeb;
					END';
	exec(@trig_configuracao_web)
End
ELSE
Begin
	set @trig_configuracao_web = '	ALTER TRIGGER tgr_pasta_local_configuracao_web
					ON TBConfiguracaoWeb
					AFTER UPDATE
					AS
					BEGIN
						IF TRIGGER_NESTLEVEL() > 1
							RETURN

						DECLARE
						@IDConfiguracaoWeb int,
						@PastaBaseSolCompras varchar(200),
						@PastaBaseSolPagamentos varchar(200),
						@PastaBaseSolReembolso varchar(200),
						@PastaBaseSolFaturamento varchar(200),
						@PastaBaseSolFaturamentoMC varchar(200),
						@PastaBaseSolAdiantamento varchar(200),
						@PastaBaseSolReembolsoTerceiro varchar(200),
						@PastaBaseCertidoes varchar(200),
						@PastaBaseWebCertidoes varchar(200),
						-- @PastaBaseProjetos varchar(200),
						@PastaBaseContratos varchar(200),
						@PastaBasePFJ varchar(200), 	
						@PastaBaseWebPFJ varchar(200), 

						@OpcaoArmazenamentoWeb int, 
						@ContaAzure varchar(200),
						-- @SenhaAzure varchar(400),
						
						-- @PastaBaseAcompanhamentoProjeto_Azure varchar(200), 
						@PastaBaseCertidoes_Azure varchar(200),
						@PastaBaseWebCertidoes_Azure varchar(200),
						@PastaBaseContratos_Azure varchar(200),
						-- @PastaBasePFJ_Azure varchar(200),
						@PastaBaseProjetos_Azure varchar(200),
						@PastaBaseSolAdiantamento_Azure varchar(200),
						-- @PastaBaseSolBolsa_Azure varchar(200),
						@PastaBaseSolCompras_Azure varchar(200),
						@PastaBaseSolFaturamento_Azure varchar(200),
						@PastaBaseSolFaturamentoMC_Azure varchar(200),
						-- @PastaBaseSolicitacaoCursoEventoServico_Azure varchar(200),
						@PastaBaseSolPagamentos_Azure varchar(200),
						@PastaBaseSolReembolso_Azure varchar(200),
						@PastaBaseSolReembolsoTerceiro_Azure varchar(200), 
						@PastaBaseWebPFJ_Azure varchar(200),
						@PastaBaseCertidoes_AmazonS3 varchar(200),
						@PastaBaseWebCertidoes_AmazonS3 varchar(200),
						@PastaBasePFJ_AmazonS3 varchar(200),
						@PastaBaseWebPFJ_AmazonS3 varchar(200),
						@PastaBaseSolBolsa_AmazonS3 varchar(200),
						@PastaBaseSolRPA_AmazonS3 varchar(200),
						@PastaBaseSolicitacaoCursoEventoServico_AmazonS3 varchar(200),
						@PastaBaseSolicitacaoTransferencia_AmazonS3 varchar(200),
						@PastaBaseAtividadeContrato varchar(200),
						@PastaBaseAtividadeContrato_Azure varchar(200),
						@PastaBaseAtividadeContrato_AmazonS3 varchar(200),
						@PastaBasePesquisaCompra varchar(max),
						@PastaBasePesquisaCompra_Azure varchar(max),
						@PastaBasePesquisaCompra_AmazonS3 varchar(max),
						@PastaBaseInscricao varchar(max),
						@PastaBaseInscricao_Azure varchar(max),
						@PastaBaseInscricao_AmazonS3 varchar(max),
						@TextoDeclaracaoResumoAceiteFornecedor varchar(max)

						SELECT 
							@IDConfiguracaoWeb = IDConfiguracaoWeb,
							@PastaBaseSolCompras = PastaBaseSolCompras,
							@PastaBaseSolPagamentos = PastaBaseSolPagamentos,
							@PastaBaseSolReembolso = PastaBaseSolReembolso,
							@PastaBaseSolFaturamento = PastaBaseSolFaturamento,
							@PastaBaseSolFaturamentoMC = PastaBaseSolFaturamentoMC,
							@PastaBaseSolAdiantamento = PastaBaseSolAdiantamento,
							@PastaBaseSolReembolsoTerceiro = PastaBaseSolReembolsoTerceiro,
							@PastaBaseCertidoes = PastaBaseCertidoes,
							@PastaBaseWebCertidoes = PastaBaseWebCertidoes,
							-- @PastaBaseProjetos = PastaBaseProjetos,
							@PastaBaseContratos = PastaBaseContratos,
							@PastaBasePFJ = PastaBasePFJ, 
							@PastaBaseWebPFJ = @PastaBaseWebPFJ, 

							@OpcaoArmazenamentoWeb = OpcaoArmazenamentoWeb, 
							@ContaAzure = ContaAzure,
							-- @SenhaAzure = SenhaAzure,

							-- @PastaBaseAcompanhamentoProjeto_Azure = PastaBaseAcompanhamentoProjeto_Azure,
							@PastaBaseCertidoes_Azure = PastaBaseCertidoes_Azure,
							@PastaBaseWebCertidoes_Azure = PastaBaseWebCertidoes_Azure,
							@PastaBaseContratos_Azure = PastaBaseContratos_Azure,
							-- @PastaBasePFJ_Azure = PastaBasePFJ_Azure,
							@PastaBaseProjetos_Azure = PastaBaseProjetos_Azure,
							@PastaBaseSolAdiantamento_Azure = PastaBaseSolAdiantamento_Azure,
							-- @PastaBaseSolBolsa_Azure = PastaBaseSolBolsa_Azure,
							@PastaBaseSolCompras_Azure = PastaBaseSolCompras_Azure,
							@PastaBaseSolFaturamento_Azure = PastaBaseSolFaturamento_Azure,
							@PastaBaseSolFaturamentoMC_Azure = PastaBaseSolFaturamentoMC_Azure,
							-- @PastaBaseSolicitacaoCursoEventoServico_Azure = PastaBaseSolicitacaoCursoEventoServico_Azure,
							@PastaBaseSolPagamentos_Azure = PastaBaseSolPagamentos_Azure,
							@PastaBaseSolReembolso_Azure = PastaBaseSolReembolso_Azure,
							@PastaBaseSolReembolsoTerceiro_Azure = PastaBaseSolReembolsoTerceiro_Azure, 
							@PastaBaseWebPFJ_Azure = PastaBaseWebPFJ_Azure,
							@PastaBaseCertidoes_AmazonS3 = PastaBaseCertidoes_AmazonS3,
							@PastaBaseWebCertidoes_AmazonS3 = PastaBaseWebCertidoes_AmazonS3,
							@PastaBasePFJ_AmazonS3 = PastaBasePFJ_AmazonS3,
							@PastaBaseWebPFJ_AmazonS3 = PastaBaseWebPFJ_AmazonS3,
							@PastaBaseSolBolsa_AmazonS3 = PastaBaseSolBolsa_AmazonS3,
							@PastaBaseSolRPA_AmazonS3 = PastaBaseSolRPA_AmazonS3,
							@PastaBaseSolicitacaoCursoEventoServico_AmazonS3 = PastaBaseSolicitacaoCursoEventoServico_AmazonS3,
							@PastaBaseSolicitacaoTransferencia_AmazonS3 = PastaBaseSolicitacaoTransferencia_AmazonS3,
							@PastaBaseAtividadeContrato = PastaBaseAtividadeContrato,
							@PastaBaseAtividadeContrato_Azure = PastaBaseAtividadeContrato_Azure,
							@PastaBaseAtividadeContrato_AmazonS3 = PastaBaseAtividadeContrato_AmazonS3,
							@PastaBasePesquisaCompra = PastaBasePesquisaCompra,
							@PastaBasePesquisaCompra_Azure = PastaBasePesquisaCompra_Azure,
							@PastaBasePesquisaCompra_AmazonS3 = PastaBasePesquisaCompra_AmazonS3,
							@PastaBaseInscricao = PastaBaseInscricao,
							@PastaBaseInscricao_Azure = PastaBaseInscricao_Azure,
							@PastaBaseInscricao_AmazonS3 = PastaBaseInscricao_AmazonS3,
							@TextoDeclaracaoResumoAceiteFornecedor = TextoDeclaracaoResumoAceiteFornecedor
						FROM
							INSERTED;

						UPDATE TBConfiguracao1 SET 
							PastaBaseSolCompras = @PastaBaseSolCompras,
							PastaBaseSolPagamentos = @PastaBaseSolPagamentos,
							PastaBaseSolReembolso = @PastaBaseSolReembolso,
							PastaBaseSolFaturamento = @PastaBaseSolFaturamento,
							PastaBaseSolFaturamentoMC = @PastaBaseSolFaturamentoMC,
							PastaBaseSolAdiantamento = @PastaBaseSolAdiantamento,
							PastaBaseSolReembolsoTerceiro = @PastaBaseSolReembolsoTerceiro,
							PastaBaseCertidoes = @PastaBaseCertidoes,
							PastaBaseWebCertidoes = @PastaBaseWebCertidoes,
							-- PastaBaseProjetos = @PastaBaseProjetos,
							PastaBaseContratos = @PastaBaseContratos,
							PastaBasePFJ = @PastaBasePFJ, 
							PastaBaseWebPFJ = @PastaBaseWebPFJ, 

							OpcaoArmazenamentoWeb = @OpcaoArmazenamentoWeb, 
							ContaAzure = @ContaAzure,
							-- SenhaAzure = @SenhaAzure,

							-- PastaBaseAcompanhamentoProjeto_Azure = @PastaBaseAcompanhamentoProjeto_Azure,
							PastaBaseCertidoes_Azure = @PastaBaseCertidoes_Azure,
							PastaBaseWebCertidoes_Azure = @PastaBaseWebCertidoes_Azure,
							PastaBaseContratos_Azure = @PastaBaseContratos_Azure,
							-- PastaBasePFJ_Azure = @PastaBasePFJ_Azure,
							PastaBaseProjetos_Azure = @PastaBaseProjetos_Azure,
							PastaBaseSolAdiantamento_Azure = @PastaBaseSolAdiantamento_Azure,
							-- PastaBaseSolBolsa_Azure = @PastaBaseSolBolsa_Azure,
							PastaBaseSolCompras_Azure = @PastaBaseSolCompras_Azure,
							PastaBaseSolFaturamento_Azure = @PastaBaseSolFaturamento_Azure,
							PastaBaseSolFaturamentoMC_Azure = @PastaBaseSolFaturamentoMC_Azure,
							-- PastaBaseSolicitacaoCursoEventoServico_Azure = @PastaBaseSolicitacaoCursoEventoServico_Azure,
							PastaBaseSolPagamentos_Azure = @PastaBaseSolPagamentos_Azure,
							PastaBaseSolReembolso_Azure = @PastaBaseSolReembolso_Azure,
							PastaBaseSolReembolsoTerceiro_Azure = @PastaBaseSolReembolsoTerceiro_Azure, 
							PastaBaseWebPFJ_Azure = @PastaBaseWebPFJ_Azure,
							PastaBaseCertidoes_AmazonS3 = @PastaBaseCertidoes_AmazonS3,
							PastaBaseWebCertidoes_AmazonS3 = @PastaBaseWebCertidoes_AmazonS3,
							PastaBasePFJ_AmazonS3 = @PastaBasePFJ_AmazonS3,
							PastaBaseWebPFJ_AmazonS3 = @PastaBaseWebPFJ_AmazonS3,
							PastaBaseSolBolsa_AmazonS3 = @PastaBaseSolBolsa_AmazonS3,
							PastaBaseSolRPA_AmazonS3 = @PastaBaseSolRPA_AmazonS3,
							PastaBaseSolicitacaoCursoEventoServico_AmazonS3 = @PastaBaseSolicitacaoCursoEventoServico_AmazonS3,
							PastaBaseSolTransferencias_AmazonS3 = @PastaBaseSolicitacaoTransferencia_AmazonS3,
							PastaBaseAtividadeContrato = @PastaBaseAtividadeContrato,
							PastaBaseAtividadeContrato_Azure = @PastaBaseAtividadeContrato_Azure,
							PastaBaseAtividadeContrato_AmazonS3 = @PastaBaseAtividadeContrato_AmazonS3,
							PastaBasePesquisaCompra = @PastaBasePesquisaCompra,
							PastaBasePesquisaCompra_Azure = @PastaBasePesquisaCompra_Azure,
							PastaBasePesquisaCompra_AmazonS3 = @PastaBasePesquisaCompra_AmazonS3,
							PastaBaseInscricao = @PastaBaseInscricao,
							PastaBaseInscricao_Azure = @PastaBaseInscricao_Azure,
							PastaBaseInscricao_AmazonS3 = @PastaBaseInscricao_AmazonS3,
							TextoDeclaracaoResumoAceiteFornecedor = @TextoDeclaracaoResumoAceiteFornecedor
						WHERE 
							IDConfiguracao = @IDConfiguracaoWeb;
					END';
	exec(@trig_configuracao_web)
End
GO --

Declare @trig_configuracao_1 varchar(max)
IF (not exists (select * from sysobjects where name='tgr_pasta_local_configuracao_1'))
Begin
	set @trig_configuracao_1 = '	CREATE TRIGGER tgr_pasta_local_configuracao_1
					ON TBConfiguracao1
					AFTER UPDATE
					AS
					BEGIN
						IF TRIGGER_NESTLEVEL() > 1
							RETURN

						DECLARE
						@IDConfiguracao int,
						@PastaBaseSolCompras varchar(200),
						@PastaBaseSolPagamentos varchar(200),
						@PastaBaseSolReembolso varchar(200),
						@PastaBaseSolFaturamento varchar(200),
						@PastaBaseSolFaturamentoMC varchar(200),
						@PastaBaseSolAdiantamento varchar(200),
						@PastaBaseSolReembolsoTerceiro varchar(200),
						@PastaBaseCertidoes varchar(200),
						@PastaBaseWebCertidoes varchar(200),
						-- @PastaBaseProjetos varchar(200),
						@PastaBaseContratos varchar(200),
						@PastaBasePFJ varchar(200), 

						@OpcaoArmazenamentoWeb int, 
						@ContaAzure varchar(200),
						-- @SenhaAzure varchar(400), 

						-- @PastaBaseAcompanhamentoProjeto_Azure varchar(200), 
						@PastaBaseCertidoes_Azure varchar(200), 
						@PastaBaseWebCertidoes_Azure varchar(200),
						@PastaBaseContratos_Azure varchar(200),
						-- @PastaBasePFJ_Azure varchar(200),
						@PastaBaseProjetos_Azure varchar(200),
						@PastaBaseSolAdiantamento_Azure varchar(200),
						-- @PastaBaseSolBolsa_Azure varchar(200),
						@PastaBaseSolCompras_Azure varchar(200),
						@PastaBaseSolFaturamento_Azure varchar(200),
						@PastaBaseSolFaturamentoMC_Azure varchar(200),
						-- @PastaBaseSolicitacaoCursoEventoServico_Azure varchar(200),
						@PastaBaseSolPagamentos_Azure varchar(200),
						@PastaBaseSolReembolso_Azure varchar(200),
						@PastaBaseSolReembolsoTerceiro_Azure varchar(200),
						@PastaBaseWebPFJ_Azure varchar(200),
						@PastaBaseCertidoes_AmazonS3 varchar(200),
						@PastaBaseWebCertidoes_AmazonS3 varchar(200),
						@PastaBasePFJ_AmazonS3 varchar(200),
						@PastaBaseWebPFJ_AmazonS3 varchar(200),
						@PastaBaseSolBolsa_AmazonS3 varchar(200),
						@PastaBaseSolRPA_AmazonS3 varchar(200),
						@PastaBaseSolicitacaoCursoEventoServico_AmazonS3 varchar(200),
						@PastaBaseSolTransferencias_AmazonS3 varchar(200),
						@PastaBaseAtividadeContrato varchar(200),
						@PastaBaseAtividadeContrato_Azure varchar(200),
						@PastaBaseAtividadeContrato_AmazonS3 varchar(200),
						@PastaBasePesquisaCompra varchar(max),
						@PastaBasePesquisaCompra_Azure varchar(max),
						@PastaBasePesquisaCompra_AmazonS3 varchar(max),
						@PastaBaseInscricao varchar(max),
						@PastaBaseInscricao_Azure varchar(max),
						@PastaBaseInscricao_AmazonS3 varchar(max),
						@TextoDeclaracaoResumoAceiteFornecedor varchar(max)

						SELECT 
							@IDConfiguracao = IDConfiguracao,
							@PastaBaseSolCompras = PastaBaseSolCompras,
							@PastaBaseSolPagamentos = PastaBaseSolPagamentos,
							@PastaBaseSolReembolso = PastaBaseSolReembolso,
							@PastaBaseSolFaturamento = PastaBaseSolFaturamento,
							@PastaBaseSolFaturamentoMC = PastaBaseSolFaturamentoMC,
							@PastaBaseSolAdiantamento = PastaBaseSolAdiantamento,
							@PastaBaseSolReembolsoTerceiro = PastaBaseSolReembolsoTerceiro,
							@PastaBaseCertidoes = PastaBaseCertidoes,
							@PastaBaseWebCertidoes = PastaBaseWebCertidoes,
							-- @PastaBaseProjetos = PastaBaseProjetos,
							@PastaBaseContratos = PastaBaseContratos,
							@PastaBasePFJ = PastaBasePFJ,			
							
							@OpcaoArmazenamentoWeb = OpcaoArmazenamentoWeb, 
							@ContaAzure = ContaAzure,
							-- @SenhaAzure = SenhaAzure,

							-- @PastaBaseAcompanhamentoProjeto_Azure = PastaBaseAcompanhamentoProjeto_Azure,
							@PastaBaseCertidoes_Azure = PastaBaseCertidoes_Azure,
							@PastaBaseWebCertidoes_Azure = PastaBaseWebCertidoes_Azure,
							@PastaBaseContratos_Azure = PastaBaseContratos_Azure,
							-- @PastaBasePFJ_Azure = PastaBasePFJ_Azure,
							@PastaBaseProjetos_Azure = PastaBaseProjetos_Azure,
							@PastaBaseSolAdiantamento_Azure = PastaBaseSolAdiantamento_Azure,
							-- @PastaBaseSolBolsa_Azure = PastaBaseSolBolsa_Azure,
							@PastaBaseSolCompras_Azure = PastaBaseSolCompras_Azure,
							@PastaBaseSolFaturamento_Azure = PastaBaseSolFaturamento_Azure,
							@PastaBaseSolFaturamentoMC_Azure = PastaBaseSolFaturamentoMC_Azure,
							-- @PastaBaseSolicitacaoCursoEventoServico_Azure = PastaBaseSolicitacaoCursoEventoServico_Azure,
							@PastaBaseSolPagamentos_Azure = PastaBaseSolPagamentos_Azure,
							@PastaBaseSolReembolso_Azure = PastaBaseSolReembolso_Azure,
							@PastaBaseSolReembolsoTerceiro_Azure = PastaBaseSolReembolsoTerceiro_Azure,
							@PastaBaseWebPFJ_Azure = PastaBaseWebPFJ_Azure,
							@PastaBaseCertidoes_AmazonS3 = PastaBaseCertidoes_AmazonS3,
							@PastaBaseWebCertidoes_AmazonS3 = PastaBaseWebCertidoes_AmazonS3,
							@PastaBasePFJ_AmazonS3 = PastaBasePFJ_AmazonS3,
							@PastaBaseWebPFJ_AmazonS3 = PastaBaseWebPFJ_AmazonS3,
							@PastaBaseSolBolsa_AmazonS3 = PastaBaseSolBolsa_AmazonS3,
							@PastaBaseSolRPA_AmazonS3 = PastaBaseSolRPA_AmazonS3,
							@PastaBaseSolicitacaoCursoEventoServico_AmazonS3 = PastaBaseSolicitacaoCursoEventoServico_AmazonS3,
							@PastaBaseSolTransferencias_AmazonS3 = PastaBaseSolTransferencias_AmazonS3,
							@PastaBaseAtividadeContrato = PastaBaseAtividadeContrato,
							@PastaBaseAtividadeContrato_Azure = PastaBaseAtividadeContrato_Azure,
							@PastaBaseAtividadeContrato_AmazonS3 = PastaBaseAtividadeContrato_AmazonS3,
							@PastaBasePesquisaCompra = PastaBasePesquisaCompra,
							@PastaBasePesquisaCompra_Azure = PastaBasePesquisaCompra_Azure,
							@PastaBasePesquisaCompra_AmazonS3 = PastaBasePesquisaCompra_AmazonS3,
							@PastaBaseInscricao = PastaBaseInscricao,
							@PastaBaseInscricao_Azure = PastaBaseInscricao_Azure,
							@PastaBaseInscricao_AmazonS3 = PastaBaseInscricao_AmazonS3,
							@TextoDeclaracaoResumoAceiteFornecedor = TextoDeclaracaoResumoAceiteFornecedor
						FROM
							INSERTED;

						UPDATE TBConfiguracaoWeb SET 
							PastaBaseSolCompras = @PastaBaseSolCompras,
							PastaBaseSolPagamentos = @PastaBaseSolPagamentos,
							PastaBaseSolReembolso = @PastaBaseSolReembolso,
							PastaBaseSolFaturamento = @PastaBaseSolFaturamento,
							PastaBaseSolFaturamentoMC = @PastaBaseSolFaturamentoMC,
							PastaBaseSolAdiantamento = @PastaBaseSolAdiantamento,
							PastaBaseSolReembolsoTerceiro = @PastaBaseSolReembolsoTerceiro,
							PastaBaseCertidoes = @PastaBaseCertidoes,
							PastaBaseWebCertidoes = @PastaBaseWebCertidoes,
							-- PastaBaseProjetos = @PastaBaseProjetos,
							PastaBaseContratos = @PastaBaseContratos,
							PastaBasePFJ = @PastaBasePFJ, 

							OpcaoArmazenamentoWeb = @OpcaoArmazenamentoWeb, 
							ContaAzure = @ContaAzure,
							-- SenhaAzure = @SenhaAzure,
							
							-- PastaBaseAcompanhamentoProjeto_Azure = @PastaBaseAcompanhamentoProjeto_Azure,
							PastaBaseCertidoes_Azure = @PastaBaseCertidoes_Azure,
							PastaBaseWebCertidoes_Azure = @PastaBaseWebCertidoes_Azure,
							PastaBaseContratos_Azure = @PastaBaseContratos_Azure,
							-- PastaBasePFJ_Azure = @PastaBasePFJ_Azure,
							PastaBaseProjetos_Azure = @PastaBaseProjetos_Azure,
							PastaBaseSolAdiantamento_Azure = @PastaBaseSolAdiantamento_Azure,
							-- PastaBaseSolBolsa_Azure = @PastaBaseSolBolsa_Azure,
							PastaBaseSolCompras_Azure = @PastaBaseSolCompras_Azure,
							PastaBaseSolFaturamento_Azure = @PastaBaseSolFaturamento_Azure,
							PastaBaseSolFaturamentoMC_Azure = @PastaBaseSolFaturamentoMC_Azure,
							-- PastaBaseSolicitacaoCursoEventoServico_Azure = @PastaBaseSolicitacaoCursoEventoServico_Azure,
							PastaBaseSolPagamentos_Azure = @PastaBaseSolPagamentos_Azure,
							PastaBaseSolReembolso_Azure = @PastaBaseSolReembolso_Azure,
							PastaBaseSolReembolsoTerceiro_Azure = @PastaBaseSolReembolsoTerceiro_Azure,
							PastaBaseWebPFJ_Azure = @PastaBaseWebPFJ_Azure,
							PastaBaseCertidoes_AmazonS3 = @PastaBaseCertidoes_AmazonS3,
							PastaBaseWebCertidoes_AmazonS3 = @PastaBaseWebCertidoes_AmazonS3,
							PastaBasePFJ_AmazonS3 = @PastaBasePFJ_AmazonS3,
							PastaBaseWebPFJ_AmazonS3 = @PastaBaseWebPFJ_AmazonS3,
							PastaBaseSolBolsa_AmazonS3 = @PastaBaseSolBolsa_AmazonS3,
							PastaBaseSolRPA_AmazonS3 = @PastaBaseSolRPA_AmazonS3,
							PastaBaseSolicitacaoCursoEventoServico_AmazonS3 = @PastaBaseSolicitacaoCursoEventoServico_AmazonS3,
							PastaBaseSolicitacaoTransferencia_AmazonS3 = @PastaBaseSolTransferencias_AmazonS3,
							PastaBaseAtividadeContrato = @PastaBaseAtividadeContrato,
							PastaBaseAtividadeContrato_Azure = @PastaBaseAtividadeContrato_Azure,
							PastaBaseAtividadeContrato_AmazonS3 = @PastaBaseAtividadeContrato_AmazonS3,
							PastaBasePesquisaCompra = @PastaBasePesquisaCompra,
							PastaBasePesquisaCompra_Azure = @PastaBasePesquisaCompra_Azure,
							PastaBasePesquisaCompra_AmazonS3 = @PastaBasePesquisaCompra_AmazonS3,
							PastaBaseInscricao = @PastaBaseInscricao,
							PastaBaseInscricao_Azure = @PastaBaseInscricao_Azure,
							PastaBaseInscricao_AmazonS3 = @PastaBaseInscricao_AmazonS3,
							TextoDeclaracaoResumoAceiteFornecedor = @TextoDeclaracaoResumoAceiteFornecedor
						WHERE 
							IDConfiguracaoWeb = @IDConfiguracao;
					END';
	exec(@trig_configuracao_1)
End
ELSE
Begin
	set @trig_configuracao_1 = '	ALTER TRIGGER tgr_pasta_local_configuracao_1
					ON TBConfiguracao1
					AFTER UPDATE
					AS
					BEGIN
						IF TRIGGER_NESTLEVEL() > 1
							RETURN

						DECLARE
						@IDConfiguracao int,
						@PastaBaseSolCompras varchar(200),
						@PastaBaseSolPagamentos varchar(200),
						@PastaBaseSolReembolso varchar(200),
						@PastaBaseSolFaturamento varchar(200),
						@PastaBaseSolFaturamentoMC varchar(200),
						@PastaBaseSolAdiantamento varchar(200),
						@PastaBaseSolReembolsoTerceiro varchar(200),
						@PastaBaseCertidoes varchar(200),
						@PastaBaseWebCertidoes varchar(200),
						-- @PastaBaseProjetos varchar(200),
						@PastaBaseContratos varchar(200),
						@PastaBasePFJ varchar(200), 

						@OpcaoArmazenamentoWeb int, 
						@ContaAzure varchar(200),
						-- @SenhaAzure varchar(400),

						-- @PastaBaseAcompanhamentoProjeto_Azure varchar(200), 
						@PastaBaseCertidoes_Azure varchar(200), 
						@PastaBaseWebCertidoes_Azure varchar(200),
						@PastaBaseContratos_Azure varchar(200),
						-- @PastaBasePFJ_Azure varchar(200),
						@PastaBaseProjetos_Azure varchar(200),
						@PastaBaseSolAdiantamento_Azure varchar(200),
						-- @PastaBaseSolBolsa_Azure varchar(200),
						@PastaBaseSolCompras_Azure varchar(200),
						@PastaBaseSolFaturamento_Azure varchar(200),
						@PastaBaseSolFaturamentoMC_Azure varchar(200),
						-- @PastaBaseSolicitacaoCursoEventoServico_Azure varchar(200),
						@PastaBaseSolPagamentos_Azure varchar(200),
						@PastaBaseSolReembolso_Azure varchar(200),
						@PastaBaseSolReembolsoTerceiro_Azure varchar(200),
						@PastaBaseWebPFJ_Azure varchar(200),
						@PastaBaseCertidoes_AmazonS3 varchar(200),
						@PastaBaseWebCertidoes_AmazonS3 varchar(200),
						@PastaBasePFJ_AmazonS3 varchar(200),
						@PastaBaseWebPFJ_AmazonS3 varchar(200),
						@PastaBaseSolBolsa_AmazonS3 varchar(200),
						@PastaBaseSolRPA_AmazonS3 varchar(200),
						@PastaBaseSolicitacaoCursoEventoServico_AmazonS3 varchar(200),
						@PastaBaseSolTransferencias_AmazonS3 varchar(200),
						@PastaBaseAtividadeContrato varchar(200),
						@PastaBaseAtividadeContrato_Azure varchar(200),
						@PastaBaseAtividadeContrato_AmazonS3 varchar(200),
						@PastaBasePesquisaCompra varchar(max),
						@PastaBasePesquisaCompra_Azure varchar(max),
						@PastaBasePesquisaCompra_AmazonS3 varchar(max),
						@PastaBaseInscricao varchar(max),
						@PastaBaseInscricao_Azure varchar(max),
						@PastaBaseInscricao_AmazonS3 varchar(max),
						@TextoDeclaracaoResumoAceiteFornecedor varchar(max)

						SELECT 
							@IDConfiguracao = IDConfiguracao,
							@PastaBaseSolCompras = PastaBaseSolCompras,
							@PastaBaseSolPagamentos = PastaBaseSolPagamentos,
							@PastaBaseSolReembolso = PastaBaseSolReembolso,
							@PastaBaseSolFaturamento = PastaBaseSolFaturamento,
							@PastaBaseSolFaturamentoMC = PastaBaseSolFaturamentoMC,
							@PastaBaseSolAdiantamento = PastaBaseSolAdiantamento,
							@PastaBaseSolReembolsoTerceiro = PastaBaseSolReembolsoTerceiro,
							@PastaBaseCertidoes = PastaBaseCertidoes,
							@PastaBaseWebCertidoes = PastaBaseWebCertidoes,
							-- @PastaBaseProjetos = PastaBaseProjetos,
							@PastaBaseContratos = PastaBaseContratos,
							@PastaBasePFJ = PastaBasePFJ, 

							@OpcaoArmazenamentoWeb = OpcaoArmazenamentoWeb, 
							@ContaAzure = ContaAzure,
							-- @SenhaAzure = SenhaAzure,

							-- @PastaBaseAcompanhamentoProjeto_Azure = PastaBaseAcompanhamentoProjeto_Azure,
							@PastaBaseCertidoes_Azure = PastaBaseCertidoes_Azure,
							@PastaBaseWebCertidoes_Azure = PastaBaseWebCertidoes_Azure,
							@PastaBaseContratos_Azure = PastaBaseContratos_Azure,
							-- @PastaBasePFJ_Azure = PastaBasePFJ_Azure,
							@PastaBaseProjetos_Azure = PastaBaseProjetos_Azure,
							@PastaBaseSolAdiantamento_Azure = PastaBaseSolAdiantamento_Azure,
							-- @PastaBaseSolBolsa_Azure = PastaBaseSolBolsa_Azure,
							@PastaBaseSolCompras_Azure = PastaBaseSolCompras_Azure,
							@PastaBaseSolFaturamento_Azure = PastaBaseSolFaturamento_Azure,
							@PastaBaseSolFaturamentoMC_Azure = PastaBaseSolFaturamentoMC_Azure,
							-- @PastaBaseSolicitacaoCursoEventoServico_Azure = PastaBaseSolicitacaoCursoEventoServico_Azure,
							@PastaBaseSolPagamentos_Azure = PastaBaseSolPagamentos_Azure,
							@PastaBaseSolReembolso_Azure = PastaBaseSolReembolso_Azure,
							@PastaBaseSolReembolsoTerceiro_Azure = PastaBaseSolReembolsoTerceiro_Azure,
							@PastaBaseWebPFJ_Azure = PastaBaseWebPFJ_Azure,
							@PastaBaseCertidoes_AmazonS3 = PastaBaseCertidoes_AmazonS3,
							@PastaBaseWebCertidoes_AmazonS3 = PastaBaseWebCertidoes_AmazonS3,
							@PastaBasePFJ_AmazonS3 = PastaBasePFJ_AmazonS3,
							@PastaBaseWebPFJ_AmazonS3 = PastaBaseWebPFJ_AmazonS3,
							@PastaBaseSolBolsa_AmazonS3 = PastaBaseSolBolsa_AmazonS3,
							@PastaBaseSolRPA_AmazonS3 = PastaBaseSolRPA_AmazonS3,
							@PastaBaseSolicitacaoCursoEventoServico_AmazonS3 = PastaBaseSolicitacaoCursoEventoServico_AmazonS3,
							@PastaBaseSolTransferencias_AmazonS3 = PastaBaseSolTransferencias_AmazonS3,
							@PastaBaseAtividadeContrato = PastaBaseAtividadeContrato,
							@PastaBaseAtividadeContrato_Azure = PastaBaseAtividadeContrato_Azure,
							@PastaBaseAtividadeContrato_AmazonS3 = PastaBaseAtividadeContrato_AmazonS3,
							@PastaBasePesquisaCompra = PastaBasePesquisaCompra,
							@PastaBasePesquisaCompra_Azure = PastaBasePesquisaCompra_Azure,
							@PastaBasePesquisaCompra_AmazonS3 = PastaBasePesquisaCompra_AmazonS3,
							@PastaBaseInscricao = PastaBaseInscricao,
							@PastaBaseInscricao_Azure = PastaBaseInscricao_Azure,
							@PastaBaseInscricao_AmazonS3 = PastaBaseInscricao_AmazonS3,
							@TextoDeclaracaoResumoAceiteFornecedor = TextoDeclaracaoResumoAceiteFornecedor
						FROM
							INSERTED;

						UPDATE TBConfiguracaoWeb SET 
							PastaBaseSolCompras = @PastaBaseSolCompras,
							PastaBaseSolPagamentos = @PastaBaseSolPagamentos,
							PastaBaseSolReembolso = @PastaBaseSolReembolso,
							PastaBaseSolFaturamento = @PastaBaseSolFaturamento,
							PastaBaseSolFaturamentoMC = @PastaBaseSolFaturamentoMC,
							PastaBaseSolAdiantamento = @PastaBaseSolAdiantamento,
							PastaBaseSolReembolsoTerceiro = @PastaBaseSolReembolsoTerceiro,
							PastaBaseCertidoes = @PastaBaseCertidoes,
							PastaBaseWebCertidoes = @PastaBaseWebCertidoes,
							-- PastaBaseProjetos = @PastaBaseProjetos,
							PastaBaseContratos = @PastaBaseContratos,
							PastaBasePFJ = @PastaBasePFJ, 

							OpcaoArmazenamentoWeb = @OpcaoArmazenamentoWeb, 
							ContaAzure = @ContaAzure,
							-- SenhaAzure = @SenhaAzure,

							-- PastaBaseAcompanhamentoProjeto_Azure = @PastaBaseAcompanhamentoProjeto_Azure,
							PastaBaseCertidoes_Azure = @PastaBaseCertidoes_Azure,
							PastaBaseWebCertidoes_Azure = @PastaBaseWebCertidoes_Azure,
							PastaBaseContratos_Azure = @PastaBaseContratos_Azure,
							-- PastaBasePFJ_Azure = @PastaBasePFJ_Azure,
							PastaBaseProjetos_Azure = @PastaBaseProjetos_Azure,
							PastaBaseSolAdiantamento_Azure = @PastaBaseSolAdiantamento_Azure,
							-- PastaBaseSolBolsa_Azure = @PastaBaseSolBolsa_Azure,
							PastaBaseSolCompras_Azure = @PastaBaseSolCompras_Azure,
							PastaBaseSolFaturamento_Azure = @PastaBaseSolFaturamento_Azure,
							PastaBaseSolFaturamentoMC_Azure = @PastaBaseSolFaturamentoMC_Azure,
							-- PastaBaseSolicitacaoCursoEventoServico_Azure = @PastaBaseSolicitacaoCursoEventoServico_Azure,
							PastaBaseSolPagamentos_Azure = @PastaBaseSolPagamentos_Azure,
							PastaBaseSolReembolso_Azure = @PastaBaseSolReembolso_Azure,
							PastaBaseSolReembolsoTerceiro_Azure = @PastaBaseSolReembolsoTerceiro_Azure,
							PastaBaseWebPFJ_Azure = @PastaBaseWebPFJ_Azure,
							PastaBaseCertidoes_AmazonS3 = @PastaBaseCertidoes_AmazonS3,
							PastaBaseWebCertidoes_AmazonS3 = @PastaBaseWebCertidoes_AmazonS3,
							PastaBasePFJ_AmazonS3 = @PastaBasePFJ_AmazonS3,
							PastaBaseWebPFJ_AmazonS3 = @PastaBaseWebPFJ_AmazonS3,
							PastaBaseSolBolsa_AmazonS3 = @PastaBaseSolBolsa_AmazonS3,
							PastaBaseSolRPA_AmazonS3 = @PastaBaseSolRPA_AmazonS3,
							PastaBaseSolicitacaoCursoEventoServico_AmazonS3 = @PastaBaseSolicitacaoCursoEventoServico_AmazonS3,
							PastaBaseSolicitacaoTransferencia_AmazonS3 = @PastaBaseSolTransferencias_AmazonS3,
							PastaBaseAtividadeContrato = @PastaBaseAtividadeContrato,
							PastaBaseAtividadeContrato_Azure = @PastaBaseAtividadeContrato_Azure,
							PastaBaseAtividadeContrato_AmazonS3 = @PastaBaseAtividadeContrato_AmazonS3,
							PastaBasePesquisaCompra = @PastaBasePesquisaCompra,
							PastaBasePesquisaCompra_Azure = @PastaBasePesquisaCompra_Azure,
							PastaBasePesquisaCompra_AmazonS3 = @PastaBasePesquisaCompra_AmazonS3,
							PastaBaseInscricao = @PastaBaseInscricao,
							PastaBaseInscricao_Azure = @PastaBaseInscricao_Azure,
							PastaBaseInscricao_AmazonS3 = @PastaBaseInscricao_AmazonS3,
							TextoDeclaracaoResumoAceiteFornecedor = @TextoDeclaracaoResumoAceiteFornecedor
						WHERE 
							IDConfiguracaoWeb = @IDConfiguracao;
					END';
	exec(@trig_configuracao_1)
End
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoHasFaturamento') AND name IN ('ID'))
Begin
	Alter table TBSolicitacaoHasFaturamento Drop CONSTRAINT PK_TBSolicitacaoHasRateio;
	Alter table TBSolicitacaoHasFaturamento add ID int IDENTITY (1, 1) NOT NULL 
	CONSTRAINT PK_TBSolicitacaoHasRateio PRIMARY KEY  CLUSTERED (ID)  ON [PRIMARY];
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoHasSolicitacao') AND name IN ('ID'))
Begin
	Alter table TBSolicitacaoHasSolicitacao Drop CONSTRAINT PK_TBSolicitacaoHasSolicitacao;
	Alter table TBSolicitacaoHasSolicitacao add ID int IDENTITY (1, 1) NOT NULL 
	CONSTRAINT PK_TBSolicitacaoHasSolicitacao PRIMARY KEY  CLUSTERED (ID)  ON [PRIMARY];
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('OcultarSubTipoContrato'))
Begin
	Alter table TBConfiguracaoWeb add OcultarSubTipoContrato bit default 0;
	Alter table TBSolicitacao alter column EndComplemento varchar(80)	
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('UtilizaVersaoNovaWeb'))
Begin
	Alter table TBConfiguracao1 add UtilizaVersaoNovaWeb bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('MensagemInicialWeb'))
Begin
	Alter table TBConfiguracaoWeb add MensagemInicialWeb varchar(max) default '';
	Update TBConfiguracao1 set UtilizaVersaoNovaWeb = 1

	update TBInfoBancaria
	set tipoconta = -1
	where TipoConta not between 0 and 2
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('DataLimiteSolAdiantamento'))
Begin
	Alter table TBProjeto add 
		DataLimiteSolAdiantamento int default 0,
		DataLimiteSolFaturamento int default 0,
		DataLimiteSolReembolsoTerceiro int default 0,
		DataLimiteSolBolsaRpaEstagio int default 0		
End

GO --


IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('IDPedidoCompra'))
Begin
	Alter Table TBSolicitacao
		Add IDPedidoCompra int	
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('RealizarPagamentoPedido'))
Begin
	Alter Table TBProjeto
		Add RealizarPagamentoPedido bit	default 0
End

GO --

if not exists (select * from sysobjects where name='TBSolicitacaoCursoEventoServico' and xtype='U')
Begin
	CREATE TABLE TBSolicitacaoCursoEventoServico(
	[IdSolicitacaoCursoEventoServico] [int] IDENTITY(1,1) NOT NULL,
	[DataSolicitacao] [datetime] NULL,
	[TipoSolicitacao] [tinyint] NULL,
	[NomeSolicitante] [varchar](100) NULL,
	[EmailSolicitante] [varchar](300) NULL,
	[TelefoneSolicitante] [varchar](20) NULL,
	[LiberacaoCoordenador] [float] NULL,
	[LiberacaoCoordenador2] [float] NULL,
	[IDLiberadoCoordenador] [int] NULL,
	[IDLiberadoCoordenador2] [int] NULL,
	[IDSolicitante] [int] NULL,
	[StatusSolicitacao] [int] NULL,
	[Descricao] [varchar](200) NULL
		CONSTRAINT [PK_TBSolicitacaoCursoEventoServico] PRIMARY KEY CLUSTERED 
	(
		[IdSolicitacaoCursoEventoServico] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoServico') AND name IN ('Resumo'))
Begin
	Alter Table TBSolicitacaoCursoEventoServico
		Add Resumo varchar(300)
End

GO --

if not exists (select * from sysobjects where name='TBSolicitacaoCursoEvento' and xtype='U')
Begin
	CREATE TABLE TBSolicitacaoCursoEvento(
		[IDSolicitacaoCursoEvento] [int] IDENTITY(1,1) NOT NULL,
		[IDSolicitacaoCursoEventoServico] [int] NULL,
		[Informacoes] [varchar](max) NULL,
		[Observacoes] [varchar](max) NULL,
		[Justificativa] [varchar](max) NULL,
		[Objetivos] [varchar](max) NULL,
		[Metodologia] [varchar](max) NULL,
		[Infraestrutura] [varchar](max) NULL,
		[EmentaDisciplina] [varchar](max) NULL,
		[Investimento] [varchar](max) NULL,
		[Local] [varchar](max) NULL,
		[OutrasInformacoes] [varchar](max) NULL,
		[Duracao] [varchar](max) NULL,
		[AlunosMaximo] [int] NULL,
		[AlunosMinimo] [int] NULL,
		[DataInicio] [date] NULL,
		[DataFim] [date] NULL,
		[DataInicioInscricao] [date] NULL,
		[DataLimiteInscricao] [date] NULL,
		[DataVencimentoParcela] [date] NULL,
		[Parcelas] [int] NULL,
		[ValorParcela] [float] NULL,
		[ValorInscricao] [float] NULL,
	 CONSTRAINT [PK_TBSolicitacaoCursoEvento] PRIMARY KEY CLUSTERED 
	(
		[IDSolicitacaoCursoEvento] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

	ALTER TABLE TBSolicitacaoCursoEvento  WITH CHECK ADD  CONSTRAINT [FK_TBSolicitacaoCursoEvento_TBSolicitacaoCursoEventoServico] FOREIGN KEY([IDSolicitacaoCursoEventoServico])
	REFERENCES TBSolicitacaoCursoEventoServico ([IdSolicitacaoCursoEventoServico])
	ALTER TABLE TBSolicitacaoCursoEvento CHECK CONSTRAINT [FK_TBSolicitacaoCursoEvento_TBSolicitacaoCursoEventoServico]
End

GO --

if not exists (select * from sysobjects where name='TBSolicitacaoCursoEventoServicoProjeto' and xtype='U')
Begin
	CREATE TABLE TBSolicitacaoCursoEventoServicoProjeto(
	[IDSolicitacaoCursoEventoServicoProjeto] [int] IDENTITY(1,1) NOT NULL,
	[IDSolicitacaoCursoEventoServico] [int] NULL,
	[IDProjeto] [int] NULL,
	[Percentual] [float] NULL,
	 CONSTRAINT [PK_TBSolicitacaoCursoEventoServicoProjeto] PRIMARY KEY CLUSTERED 
	(
		[IDSolicitacaoCursoEventoServicoProjeto] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE TBSolicitacaoCursoEventoServicoProjeto  WITH CHECK ADD  CONSTRAINT [FK_TBSolicitacaoCursoEventoServicoProjeto_TBSolicitacaoCursoEventoServico] FOREIGN KEY([IDSolicitacaoCursoEventoServico])
	REFERENCES TBSolicitacaoCursoEventoServico ([IdSolicitacaoCursoEventoServico])

	ALTER TABLE TBSolicitacaoCursoEventoServicoProjeto CHECK CONSTRAINT [FK_TBSolicitacaoCursoEventoServicoProjeto_TBSolicitacaoCursoEventoServico]
End

GO --

if not exists (select * from sysobjects where name='RetornaProjetoSolCursoEventoServico')
Begin
	Declare @func_configuracao_1 varchar(max)

	set @func_configuracao_1 = '
				Create FUNCTION RetornaProjetoSolCursoEventoServico (    @IDSol int  )   
					RETURNS varchar(max)   
					AS   
					BEGIN     
						DECLARE @Projetos as varchar(max)    
						Declare @AuxNomeProjeto as varchar(max)    
	
						set @Projetos = ''''  
						DECLARE CURSOR_OBJECTS_SolProjetos 
						CURSOR FOR   
							Select P.NomeProjeto
							From TBSolicitacaoCursoEventoServicoProjeto SR with(nolock) 
							Inner Join TBProjeto P with(nolock) 
							on (SR.IDProjeto = P.IDProjeto)
							where SR.IDSolicitacaoCursoEventoServico = @IDSol   
	
						OPEN CURSOR_OBJECTS_SolProjetos     
	
						FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
						WHILE @@FETCH_STATUS = 0     
						BEGIN      
							IF (len(@Projetos) = 0)        
							Set @Projetos = @AuxNomeProjeto;      
							Else        
							Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
							FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
						END     
	
						CLOSE CURSOR_OBJECTS_SolProjetos     
						DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
						RETURN coalesce(@Projetos,'''') 
					END';
	exec(@func_configuracao_1)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolicitacaoCursoEventoServico'))
alter table TBConfiguracaoWeb add PastaBaseSolicitacaoCursoEventoServico varchar(200);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('DataLimiteSolCursoEventoServico'))
Begin
	Alter table TBProjeto add 
		DataLimiteSolCursoEventoServico int default 0		
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolCursoEventoServicoMsgConfirmacao'))
Begin
	Alter table TBConfiguracaoWeb add SolCursoEventoServicoMsgConfirmacao varchar(max), 
									  InstrucoesSolCursoEventoServico varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('SolCursoEventoServico'))
Begin
	alter table TBRemotoProjetoPerfil add SolCursoEventoServico bit;
	alter table TBRemotoProjeto add SolCursoEventoServico bit;
End

GO --

if not exists (select * from sysobjects where name='TBInscricao' and xtype='U')
Begin
	CREATE TABLE TBInscricao(
		IDInscricao int IDENTITY(1,1) NOT NULL,
		IdSolicitacaoCursoEventoServico int NULL,
		Nome varchar(100) NULL,
		Documento varchar(20) NULL,
		Tipo tinyint NULL,
		Endereco varchar(100) NULL,
		Numero varchar(10) NULL,
		Complemento varchar(80) NULL,
		Bairro varchar(80) NULL,
		Cidade varchar(40) NULL,
		UF varchar(2) NULL,
		CEP varchar(9) NULL,
		Telefone varchar(20) NULL,
		Celular varchar(15) NULL,
		DataNascimento int NULL,
		Email varchar(300) NULL,
		IDLancamento int NULL,
		valor float NULL,
	 CONSTRAINT [PK_TBInscricao] PRIMARY KEY CLUSTERED 
	(
		[IDInscricao] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolBolsa'))
BEGIN
	ALTER TABLE TBConfiguracaoWeb ADD PastaBaseSolBolsa varchar(200);
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('Matricula'))
BEGIN
	ALTER TABLE TBSolicitacaoBolsa ADD Matricula varchar(20) default NULL;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('Siafi'))
BEGIN
	ALTER TABLE TBSolicitacaoBolsa ADD Siafi varchar(20) default NULL;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoServico') AND name IN ('IDTurma'))
Begin
	Alter Table TBSolicitacaoCursoEventoServico
		Add IDTurma int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('StatusPagamento'))
Begin
	Alter Table TBLancamento
		Add StatusPagamento varchar(50)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('CodigoPagamento'))
Begin
	Alter Table TBLancamento
		Add CodigoPagamento varchar(255)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('MostrarSomenteContasPadroesSolicitacao'))
Begin
	Alter Table TBConfiguracaoWeb
		Add MostrarSomenteContasPadroesSolicitacao bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('MostrarSomenteDadosMembrosEquipeSolicitacao'))
Begin
	Alter Table TBConfiguracaoWeb
		Add MostrarSomenteDadosMembrosEquipeSolicitacao bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoDoacao'))
Begin
	CREATE TABLE TBSolicitacaoDoacao(
		IDSolicitacaoDoacao int IDENTITY(1,1) NOT NULL,
		IDSolicitacaoCursoEventoServico int NULL,
		ValorMinimo float NULL,
		ValorMaximo float NULL,
		ValorPadrao1 float NULL,
		ValorPadrao2 float NULL,
		ValorPadrao3 float NULL,
		ValorPadrao4 float NULL,
		DescricaoDoacao text NULL,
		CONSTRAINT [PK_TBSolicitacaoDoacao] PRIMARY KEY CLUSTERED 
	(
		[IDSolicitacaoDoacao] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]


	ALTER TABLE TBSolicitacaoDoacao  WITH CHECK ADD  CONSTRAINT [FK_TBSolicitacaoDoacao_TBSolicitacaoCursoEventoServico] FOREIGN KEY([IDSolicitacaoCursoEventoServico])
	REFERENCES TBSolicitacaoCursoEventoServico ([IdSolicitacaoCursoEventoServico])


	ALTER TABLE TBSolicitacaoDoacao CHECK CONSTRAINT [FK_TBSolicitacaoDoacao_TBSolicitacaoCursoEventoServico]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao'))
Begin
	CREATE TABLE TBDoacao(
		IDDoacao int IDENTITY(1,1) NOT NULL,
		Descricao varchar(200) NULL,
		Resumo varchar(300) NULL,
		Ativo bit NULL,
		DescricaoDoacao text NULL,
		ValorMaximo float NULL,
		ValorMinimo float NULL,
		ValorPadrao1 float NULL,
		ValorPadrao2 float NULL,
		ValorPadrao3 float NULL,
		ValorPadrao4 float NULL,
		IDConta int null,
		IDConvenio int null
		CONSTRAINT PK_TBDoacao PRIMARY KEY CLUSTERED 
	(
		IDDoacao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacaoProjeto'))
Begin
	CREATE TABLE dbo.TBDoacaoProjeto(
		IDDoacaoProjeto int IDENTITY(1,1) NOT NULL,
		IDDoacao int NULL,
		IDProjeto int NULL,
		IDRubrica int NULL,
		IDSubRubrica int NULL,
		Percentual float NULL,
		CONSTRAINT PK_TBDoacaoProjeto PRIMARY KEY CLUSTERED 
	(
		IDDoacaoProjeto ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoServico') AND name IN ('IDDoacao'))
Begin
	Alter Table TBSolicitacaoCursoEventoServico
		Add IDDoacao int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('CadastroCompleto'))
Begin
	Alter Table TBDoacao
		Add CadastroCompleto bit default 0  
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('PrazoDoacao'))
Begin
	Alter Table TBDoacao
		Add PrazoDoacao tinyint default 0  
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBVerificacaoData'))
Begin
	CREATE TABLE dbo.TBVerificacaoData(
		IDVerificacaoData int IDENTITY(1,1) PRIMARY KEY,
		DataNascimentoPessoa int NULL,
		DataNascimentoSolicitacao int NULL,
		IDSolicitacao int NULL,
		DocumentoPessoa varchar(15) NULL,
		TipoSolicitacao int NULL,
		IDCoordenador int NULL,
		CriadoEm Datetime NULL
	)
End

GO --

if (not exists (select * from sysobjects where name='tgr_verificacao_data_nascimento_sol'))
Begin
	Declare @trig_verificacao_data varchar(max)

	set @trig_verificacao_data = '	CREATE TRIGGER tgr_verificacao_data_nascimento_sol
				ON TBSolicitacao
				AFTER INSERT, UPDATE
				AS
				BEGIN
					IF TRIGGER_NESTLEVEL() > 1
						RETURN

					DECLARE
					@IDSolicitacao int,
					@DocumentoPessoa varchar(15),
					@DataNascimentoSol int,
					@DataNascimentoPessoa int,
					@TipoSolicitacao int,
					@IDCoordenador int

					SELECT 
						@IDSolicitacao = IDSolicitacao,
						@DocumentoPessoa = DocumentoTerceiro,
						@DataNascimentoSol = DataNasc,
						@TipoSolicitacao = TipoSolicitacao,
						@IDCoordenador = IDCoordenador
					FROM
						INSERTED;

					SELECT 
						@DataNascimentoPessoa = DataNascimento
					FROM
						TBPessoaFisicaJuridica
					WHERE
						TBPessoaFisicaJuridica.CPFCNPJ = @DocumentoPessoa;

					IF (@DataNascimentoSol != @DataNascimentoPessoa) 
					BEGIN 
						INSERT INTO TBVerificacaoData 
							(DataNascimentoPessoa, DataNascimentoSolicitacao, IDSolicitacao, DocumentoPessoa, TipoSolicitacao, IDCoordenador, CriadoEm)
						VALUES
							(@DataNascimentoPessoa, @DataNascimentoSol, @IDSolicitacao, @DocumentoPessoa, @TipoSolicitacao, @IDCoordenador, GETDATE())
					END
				END';
	exec(@trig_verificacao_data)
End

GO --

if not exists (select * from sysobjects where name='RetornaProjetoDoacao')
Begin
	Declare @func_configuracao_1 varchar(max)

	set @func_configuracao_1 = '
				Create FUNCTION RetornaProjetoDoacao (    @IDDoacao int  )   
					RETURNS varchar(max)   
					AS   
					BEGIN     
						DECLARE @Projetos as varchar(max)    
						Declare @AuxNomeProjeto as varchar(max)    
	
						set @Projetos = ''''  
						DECLARE CURSOR_OBJECTS_Projetos 
						CURSOR FOR   
							Select P.NomeProjeto
							From TBDoacaoProjeto DP with(nolock) 
							Inner Join TBProjeto P with(nolock) 
							on (DP.IDProjeto = P.IDProjeto)
							where DP.IDDoacao = @IDDoacao   
	
						OPEN CURSOR_OBJECTS_Projetos     
	
						FETCH NEXT FROM CURSOR_OBJECTS_Projetos INTO @AuxNomeProjeto     
						WHILE @@FETCH_STATUS = 0     
						BEGIN      
							IF (len(@Projetos) = 0)        
							Set @Projetos = @AuxNomeProjeto;      
							Else        
							Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
							FETCH NEXT FROM CURSOR_OBJECTS_Projetos INTO @AuxNomeProjeto     
						END     
	
						CLOSE CURSOR_OBJECTS_Projetos     
						DEALLOCATE CURSOR_OBJECTS_Projetos     
	
						RETURN coalesce(@Projetos,'''') 
					END';
	exec(@func_configuracao_1)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('Boleto'))
Begin
	Alter Table TBDoacao
		Add Boleto bit default 0,
		CartaoCredito bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('Boleto'))
Begin
	Alter Table TBTurma
		Add Boleto bit default 0,
		CartaoCredito bit default 0

	Update TBDoacao
	set Boleto = 1,
	CartaoCredito = 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('MeioPagamento1'))
Begin
	Alter Table TBDoacao
		Add MeioPagamento1 bit default 0,
		MeioPagamento2 bit default 0,
		MeioPagamento3 bit default 0,
		MeioPagamento1Titulo varchar(17),
		MeioPagamento2Titulo varchar(17),
		MeioPagamento3Titulo varchar(17),
		MeioPagamento1Descricao varchar(max),
		MeioPagamento2Descricao varchar(max),
		MeioPagamento3Descricao varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('MeioPagamento1'))
Begin
	Alter Table TBTurma
		Add MeioPagamento1 bit default 0,
		MeioPagamento2 bit default 0,
		MeioPagamento3 bit default 0,
		MeioPagamento1Titulo varchar(17),
		MeioPagamento2Titulo varchar(17),
		MeioPagamento3Titulo varchar(17),
		MeioPagamento1Descricao varchar(max),
		MeioPagamento2Descricao varchar(max),
		MeioPagamento3Descricao varchar(max)

	Update TBDoacao
		set MeioPagamento1 = 0,
		MeioPagamento2 = 0,
		MeioPagamento3 = 0,
		MeioPagamento1Titulo = '',
		MeioPagamento2Titulo = '',
		MeioPagamento3Titulo = '',
		MeioPagamento1Descricao = '',
		MeioPagamento2Descricao = '',
		MeioPagamento3Descricao = ''
End

GO --


IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricaoForm'))
Begin
	CREATE TABLE TBInscricaoForm(
		IDInscricaoForm int IDENTITY(1,1) NOT NULL,
		IDInscricao int NULL,
		Pergunta varchar(max) NULL,
		Selecionado bit NULL,
	 CONSTRAINT PK_TBInscricaoForm PRIMARY KEY CLUSTERED 
	(
		IDInscricaoForm ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

	ALTER TABLE TBInscricaoForm  WITH CHECK ADD  CONSTRAINT FK_TBInscricaoForm_TBInscricao FOREIGN KEY(IDInscricao)
	REFERENCES TBInscricao (IDInscricao)

	ALTER TABLE TBInscricaoForm CHECK CONSTRAINT FK_TBInscricaoForm_TBInscricao
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacaoForm'))
Begin
	CREATE TABLE TBDoacaoForm(
	IDDoacaoForm int IDENTITY(1,1) NOT NULL,
	IDDoacao int NULL,
	Pergunta varchar(max) NULL,
	Obrigatorio bit NULL,
	 CONSTRAINT PK_TBDoacaoForm PRIMARY KEY CLUSTERED 
	(
		IDDoacaoForm ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
	

	ALTER TABLE TBDoacaoForm  WITH CHECK ADD  CONSTRAINT FK_TBDoacaoForm_TBDoacao FOREIGN KEY(IDDoacao)
	REFERENCES TBDoacao (IDDoacao)
	

	ALTER TABLE TBDoacaoForm CHECK CONSTRAINT FK_TBDoacaoForm_TBDoacao
End


IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurmaForm'))
Begin
	CREATE TABLE TBTurmaForm(
	IDTurmaForm int IDENTITY(1,1) NOT NULL,
	IDTurma int NULL,
	Pergunta varchar(max) NULL,
	Obrigatorio bit NULL,
	 CONSTRAINT PK_TBTurmaForm PRIMARY KEY CLUSTERED 
	(
		IDTurmaForm ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
	

	ALTER TABLE TBTurmaForm  WITH CHECK ADD  CONSTRAINT FK_TBTurmaForm_TBTurma FOREIGN KEY(IDTurma)
	REFERENCES TBTurma (IDTurma)
	

	ALTER TABLE TBTurmaForm CHECK CONSTRAINT FK_TBTurmaForm_TBTurma
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('MensagemFinalSalvar'))
Begin
	Update TBTurma
		set MeioPagamento1 = 0,
		MeioPagamento2 = 0,
		MeioPagamento3 = 0,
		MeioPagamento1Titulo = '',
		MeioPagamento2Titulo = '',
		MeioPagamento3Titulo = '',
		MeioPagamento1Descricao = '',
		MeioPagamento2Descricao = '',
		MeioPagamento3Descricao = ''

	Alter Table TBDoacao
		Add MensagemFinalSalvar varchar(max),
		URLDirecionamento varchar(400)

	Alter Table TBTurma
		Add MensagemFinalSalvar varchar(max),
		URLDirecionamento varchar(400)
End

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('EmailPagSeguro'))
Begin
	Alter table TBConfiguracaoWeb add EmailPagSeguro varchar(max)
	Alter table TBConfiguracaoWeb add TokenPagSeguro varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricao') AND name IN ('DataCriacao'))
Begin
	Alter table TBInscricao add DataCriacao datetime default getdate()
End

GO --


IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBCodigoPagSeguro'))
Begin
	CREATE TABLE TBCodigoPagSeguro(
    IDCodigoPagSeguro int IDENTITY(1,1) NOT NULL,
	Codigo varchar(30) NULL,
	Mensagem  varchar(max) NULL,
	MensagemTraduzida bit null,
	DataCriacao datetime default getDate(),
		CONSTRAINT [PK_TBCodigoPagSeguro] PRIMARY KEY CLUSTERED 
	(
		IDCodigoPagSeguro ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


	Update TBInscricao
	set dataCriacao = (case l.datacriacao when null then getdate() else l.DataCriacao end)
	from TBInscricao I
	left join TBLancamento l
	on i.IDLancamento = l.IDLancamento

	Alter table TBConfiguracaoWeb add UtilizarDoacao bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('UtilizarCurso'))
Begin
	Alter table TBConfiguracaoWeb add UtilizarCurso bit
	Alter table TBConfiguracaoWeb add UtilizarEvento bit	
	Alter table TBConfiguracaoWeb add UtilizarServico bit
	Alter table TBConfiguracaoWeb add UtilizarFornecedor bit
	Alter table TBConfiguracaoWeb add UtilizarAluno bit


	Update TBConfiguracaoWeb set UtilizarDoacao = 1
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('PagSeguroCheckout'))
Begin
	Alter Table TBDoacao
		Add PagSeguroCheckout bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('PagSeguroCheckout'))
Begin
	Alter Table TBTurma
		Add PagSeguroCheckout bit default 0

	Update TBDoacao set PagSeguroCheckout = 0
End

GO --

UPDATE TBPessoaFisicaJuridica SET TBPessoaFisicaJuridica.PISPASEP = '' 
WHERE TBPessoaFisicaJuridica.PISPASEP = '.     .  /' OR TBPessoaFisicaJuridica.PISPASEP = '   .     .  / ';

GO --

UPDATE TBPessoaFisicaJuridica SET TBPessoaFisicaJuridica.INSS = '' 
WHERE TBPessoaFisicaJuridica.INSS = '.     .  /' OR TBPessoaFisicaJuridica.INSS = '   .     .  / ';

GO --

UPDATE TBSolicitacaoReembolso SET TBSolicitacaoReembolso.DespacharBagagem = 0 
WHERE TBSolicitacaoReembolso.DespacharBagagem IS NULL;

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolso') AND name IN ('ValorTotalKmRodado'))
Begin
	ALTER TABLE TBSolicitacaoReembolso ADD ValorTotalKmRodado float;
End

GO --

Alter table TBSolicitacao
alter column EndComplemento varchar(max)

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SerialCertificado'))
Begin
	ALTER TABLE TBConfiguracaoWeb ADD SerialCertificado varchar(max);
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConta') AND name IN ('EstacaoSantander'))
Begin
	ALTER TABLE TBConta ADD EstacaoSantander varchar(8);
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('AcessoWebLiberado'))
Begin
	ALTER TABLE TBPessoaFisicaJuridica ADD AcessoWebLiberado bit NOT NULL DEFAULT 0;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('DataAlteracaoAcessoWeb'))
Begin
	ALTER TABLE TBPessoaFisicaJuridica ADD DataAlteracaoAcessoWeb Datetime;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('AlteracaoFornecedorConfirmacao'))
Begin
	ALTER TABLE TBConfiguracaoWeb ADD AlteracaoFornecedorConfirmacao varchar(max);
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('InstrucoesFornecedor'))
Begin
	ALTER TABLE TBConfiguracaoWeb ADD InstrucoesFornecedor varchar(max);
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ExibirBolsa'))
Begin
	Alter table TBConfiguracaoWeb add ExibirBolsa bit default 1,
									  ExibirRPA bit default 1,
									  ExibirEstagio bit default 1		
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('MatriculaObrigatoria'))
Begin
	Alter table TBConfiguracaoWeb add MatriculaObrigatoria bit default 1,
									  SIAPEObrigatorio bit default 1	
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('LGPDAvisoGeral'))
Begin
	ALTER TABLE TBConfiguracaoWeb ADD LGPDAvisoGeral varchar(max);
	ALTER TABLE TBConfiguracaoWeb ADD LGPDAvisoSolAdiantamento varchar(max);
	ALTER TABLE TBConfiguracaoWeb ADD LGPDAvisoSolBolsa varchar(max);
	ALTER TABLE TBConfiguracaoWeb ADD LGPDAvisoSolPagamentoPF varchar(max);
	ALTER TABLE TBConfiguracaoWeb ADD LGPDAvisoSolReembolso varchar(max);
	ALTER TABLE TBConfiguracaoWeb ADD LGPDAvisoSolReembolsoTerceiro varchar(max);
End

GO --

if not exists (select * from sysobjects where name='TBContratoLiberacao' and xtype='U')
Begin
	CREATE TABLE TBContratoLiberacao(
		IDContratoLiberacao int IDENTITY(1,1) NOT NULL,
		IDContrato int NOT NULL,
		IDUsuarioNivel1 int NULL,
		DataHoraLiberacaoNivel1 datetime NULL,
		IDUsuarioNivel2 int NULL,
		DataHoraLiberacaoNivel2 datetime NULL,
		IDUsuarioNivel3 int NULL,
		DataHoraLiberacaoNivel3 datetime NULL,
		IDUsuarioContratoLiberado int NULL,
		DataContratoLiberado datetime NULL,
		CONSTRAINT PK_TBContratoLiberacao PRIMARY KEY CLUSTERED 
		(
			IDContratoLiberacao ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]

	
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBContratos') AND name IN ('DataCriacao'))
Begin
	Alter table TBContratos
	add DataCriacao Datetime default GetDate()
End

GO --

GO --

if (not exists (select * from sysobjects where name='RetornaProjetoContrato'))
Begin
	Declare @funct varchar(max)

	set @funct = '
		Create FUNCTION RetornaProjetoContrato (    @IDContrato int  )   
		RETURNS varchar(max)   
		AS   
		BEGIN     
			DECLARE @Projetos as varchar(max)    
			Declare @AuxNomeProjeto as varchar(max)    
	
			set @Projetos = ''''  
			DECLARE CURSOR_OBJECTS_SolProjetos 
			CURSOR FOR   
				Select P.NomeProjeto
				From TBProjetoContrato PC with(nolock) 
				Inner Join TBProjeto P with(nolock) 
				on (PC.IDProjeto = P.IDProjeto)
				where PC.IDContrato = @IDContrato   
	
			OPEN CURSOR_OBJECTS_SolProjetos     
	
			FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			WHILE @@FETCH_STATUS = 0     
			BEGIN      
				IF (len(@Projetos) = 0)        
				Set @Projetos = @AuxNomeProjeto;      
				Else        
				Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
				FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			END     
	
			CLOSE CURSOR_OBJECTS_SolProjetos     
			DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
			RETURN coalesce(@Projetos,'''') 
		END'

	exec(@funct)
end

GO --

IF NOT EXISTS (SELECT * FROM TBFormulario WHERE  IDFormulario = 10100)
Begin
	Insert TBFormulario 
	(IDFormulario, DescricaoFormulario, IDModuloSuporte, TipoFormulario)
	Values 
		(10100,
		(select '100 - Permissao para liberar contratos.' collate SQL_Latin1_General_CP1_CI_AI),
		0,
		2
		),
		(10101,
		(select '101 - Permissao para liberacao de contrato - nivel 1.' collate SQL_Latin1_General_CP1_CI_AI),
		0,
		2
		),
		(10102,
		(select '102 - Permissao para liberacao de contrato - nivel 2.' collate SQL_Latin1_General_CP1_CI_AI),
		0,
		2
		),
		(10103,
		(select '103 - Permissao para liberacao de contrato - nivel 3.' collate SQL_Latin1_General_CP1_CI_AI),
		0,
		2
		)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('UtilizarLiberacaoPedidoCoordenador'))
Begin
	ALTER TABLE TBConfiguracaoWeb ADD UtilizarLiberacaoPedidoCoordenador bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPedidoCompra') AND name IN ('IDLiberacaoCoordenador'))
Begin
	ALTER TABLE TBPedidoCompra 
		ADD IDLiberacaoCoordenador int,
		DataLiberacaoCoordenador Datetime;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('LiberarPedidoCompra'))
Begin
	alter table TBRemotoProjetoPerfil add LiberarPedidoCompra bit;
	alter table TBRemotoProjeto add LiberarPedidoCompra bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('UtilizarLiberacaoContratoNiveis'))
Begin
	ALTER TABLE TBConfiguracaoWeb ADD UtilizarLiberacaoContratoNiveis bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSubTiposContrato') AND name IN ('UtilizarWeb'))
Begin
	Alter Table TBSubTiposContrato add UtilizarWeb bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjetoContrato') AND name IN ('IDMetaEtapa'))
Begin
	Alter Table TBProjetoContrato add 
		IDMetaEtapa int,
		IDRubrica int,
		IDSubRubrica int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBUsuarioNotificacao') AND name IN ('SolBolsaRPAEstagioMsg'))
Begin
	Alter Table TBUsuarioNotificacao add 
		SolBolsaRPAEstagioMsg bit,
		SolCompraMsg bit,
		SolCursoEventoDoacaoServicoMsg bit
End

GO --

Update TBPessoaFisicaJuridica set AcessoWebLiberado = 0
where AcessoWebLiberado is null

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('UtilizarAcompanhamentoItemCompra'))
Begin
	ALTER TABLE TBConfiguracaoWeb ADD UtilizarAcompanhamentoItemCompra bit;
End

GO --

UPDATE TBSolicitacao SET TipoArmazenamentoWeb = 0 
WHERE TipoArmazenamentoWeb IS NULL;

GO --


if not exists (select * from sysobjects where name='TBLancamentoInscricao' and xtype='U')
Begin
	CREATE TABLE TBLancamentoInscricao(
	IDLancamentoInscricao int IDENTITY(1,1) NOT NULL,
	IDInscricao int NULL,
	IDLancamento int NULL,
		CONSTRAINT [PK_TBLancamentoInscricao] PRIMARY KEY CLUSTERED 
	(
		IDLancamentoInscricao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]


	Insert into TBLancamentoInscricao
	(IDInscricao, IDLancamento)
	Select IDInscricao, IDLancamento
	From TBInscricao

	/*Alter Table TBInscricao
	alter column IDLancamento*/
End

GO --

if not exists (select * from sysobjects where name='RetornaProjetoSolPagamentoPFPJ')
Begin
	Declare @func_configuracao_1 varchar(max)

	set @func_configuracao_1 = '
				Create FUNCTION RetornaProjetoSolPagamentoPFPJ (    @IDSol int  )   
					RETURNS varchar(max)   
					AS   
					BEGIN     
						DECLARE @Projetos as varchar(max)    
						Declare @AuxNomeProjeto as varchar(max)    
	
						set @Projetos = ''''  
						DECLARE CURSOR_OBJECTS_SolProjetos 
						CURSOR FOR   
							Select P.NomeProjeto
							From TBSolicitacaoRateio SR with(nolock) 
							Inner Join TBProjeto P with(nolock) 
							on (SR.IDProjeto = P.IDProjeto)
							where SR.IDSolicitacao = @IDSol   
	
						OPEN CURSOR_OBJECTS_SolProjetos     
	
						FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
						WHILE @@FETCH_STATUS = 0     
						BEGIN      
							IF (len(@Projetos) = 0)        
							Set @Projetos = @AuxNomeProjeto;      
							Else        
							Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
							FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
						END     
	
						CLOSE CURSOR_OBJECTS_SolProjetos     
						DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
						RETURN coalesce(@Projetos,'''') 
					END';
	exec(@func_configuracao_1)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBUsuarioNotificacao') AND name IN ('SolPagamentoPFMsg'))
Begin
	Alter Table TBUsuarioNotificacao add 
		SolPagamentoPFMsg bit,
		SolAdiantamanetoMsg bit,
		SolReembolsoMsg bit,
		SolFaturamentoMsg bit,
		SolPagamentoPJMsg bit,
		SolReembolsoTerceiroMsg bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTiposContrato') AND name IN ('Bolsa'))
Begin
	Alter Table TBTiposContrato add 
		Bolsa bit,
		RPA bit,
		Estagio bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('UtilizarViaAPI'))
Begin
	Alter Table TBTurma
		Add UtilizarViaAPI bit default 0
End

GO --

IF EXISTS (SELECT * FROM TBConfiguracao where AbreviaturaFundacao = 'FUSP')
Begin
	Update TBConfiguracaoWeb 
	set UtilizarServico = 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('LiberarAtividadeContrato'))
Begin
	alter table TBRemotoProjetoPerfil add LiberarAtividadeContrato bit;
	alter table TBRemotoProjeto add LiberarAtividadeContrato bit;
End

GO --


IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBHistoricoSenhaWeb') AND name IN ('SenhaPessoaFisicaJuridica'))
Begin
	alter table TBHistoricoSenhaWeb add SenhaPessoaFisicaJuridica varchar(80);
END
Begin
	alter table TBHistoricoSenhaWeb alter column SenhaPessoaFisicaJuridica varchar(80);
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('UtilizaImportacao'))
Begin
	Alter Table TBPessoaFisicaJuridica add 
		UtilizaImportacao bit
End

GO --

Update TBPessoaFisicaJuridica
set UtilizaImportacao = 0
where UtilizaImportacao is null


if (not exists (select * from sysobjects where name='tgr_pessoa_importacao'))
Begin
	Declare @trig_pessoa_importacao varchar(max)

	set @trig_pessoa_importacao = '	CREATE TRIGGER tgr_pessoa_importacao
				ON TBPessoaFisicaJuridica
				AFTER INSERT
				AS
				BEGIN
					IF TRIGGER_NESTLEVEL() > 1
						RETURN

					DECLARE
					@IDPessoa int

					SELECT 
						@IDPessoa = IDPessoaFisicaJuridica
					FROM
						INSERTED;

					Update TBPessoaFisicaJuridica
					set UtilizaImportacao = 0
					where IDPessoaFisicaJuridica = @IDPessoa
				END';
	exec(@trig_pessoa_importacao)
End

GO --

if not exists (select * from sysobjects where name='TBPessoaImportacao' and xtype='U')
Begin
	
	CREATE TABLE TBPessoaImportacao(
		IDPessoaImportacao int IDENTITY(1,1) NOT NULL,
		IDPessoaFisicaJuridica int NULL,
		IDPessoaAutorizada int NULL,
	 CONSTRAINT [PK_TBPessoaImportacao] PRIMARY KEY CLUSTERED 
	(
		[IDPessoaImportacao] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBUsuarioNotificacao') AND name IN ('SolBolsaRPAEstagioContratoMsg'))
Begin
	Alter Table TBUsuarioNotificacao add 
		SolBolsaRPAEstagioContratoMsg bit
End

GO --

if not exists (select * from sysobjects where name='TBSolicitacaoImportacao' and xtype='U')
Begin
	
	CREATE TABLE TBSolicitacaoImportacao(
		IDSolicitacaoImportacao int IDENTITY(1,1) NOT NULL,
		IDSolicitante int NULL,
		DataSolicitacao datetime NULL,
		ObservacaoSolicitacao text NULL,
		IDProtocolo int NULL,
		NomeSolicitante varchar(100) NULL,
		EmailSolicitante varchar(300) NULL,
		TelefoneSolicitante varchar(20) NULL,
		DataAprovacao int NULL,
		NumeroProcesso varchar(50) NULL,
		DescricaoImportacaoFinanciador varchar(max) NULL,
		CepLocalAlocado varchar(9) NULL,
		EnderecoLocalAlocado varchar(100) NULL,
		NumeroLocalAlocado varchar(10) NULL,
		ComplementoLocalAlocado varchar(80) NULL,
		BairroLocalAlocado varchar(80) NULL,
		CidadelocalAlocado varchar(40) NULL,
		EstadoLocalAlocado varchar(40) NULL,
		UnidadeLocalAlocado varchar(40) NULL,
		CnpjUnidadeLocalAlocado varchar(18) NULL,
		NomeEmpresaInvoice varchar(100) NULL, 
		ContatoEmpresaInvoice varchar(200) NULL, 
		EmailEmpresaInvoice varchar(300) NULL, 
		SiteEmpresaInvoice varchar(100) NULL,
		Telefone1EmpresaInvoice varchar(15) NULL,
		Telefone2EmpresaInvoice varchar(15) NULL,
		PaisEmpresaInvoice varchar(30) NULL,
		NomeExportador varchar(100) NULL, 
		EmailExportador varchar(300) NULL, 
		SiteExportador varchar(100) NULL,
		Telefone1Exportador varchar(15) NULL,
		Telefone2Exportador varchar(15) NULL,
		PaisExportador varchar(30) NULL,
		EnderecoExportador varchar(300) NULL,
		ZipCodeExportador varchar(30) NULL,
		CidadeExportador varchar(40) NULL,
		EstadoExportador varchar(40) NULL,
		DataProforma int NULL,
		NumeroProformaInvoice varchar(20) NULL, 
		ValidadeProposta varchar(15) NULL,
		DataNR int NULL,
		NossaReferencia varchar(20) NULL,
		DataInvoice int NULL,
		NumeroInvoice varchar(20) NULL,
		FormaPagamento varchar(15) NULL,
		PrevisaoEntrega varchar(30) NULL,
		FormaEmbarque varchar(50) NULL,
		AeroportoPortoOrigem varchar(100) NULL,
		AeroportoPortoDestino varchar(100) NULL,
		IDMoeda int NULL,		
		OutrasCondicoesCompra varchar(max) NULL,
		InstituicaoFinanceira varchar(100) NULL,
		EnderecoAgencia varchar(200) NULL,
		NumeroAgencia varchar(20) NULL,
		NumeroConta varchar(30) NULL,
		IBAN varchar(50) NULL,
		SWFITCode varchar(50) NULL,
		InformacoesComplementares varchar(max) NULL,
	 CONSTRAINT [PK_TBSolicitacaoImportacao] PRIMARY KEY CLUSTERED 
	(
		IDSolicitacaoImportacao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

END


GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('InstrucoesSolImportacao'))
Begin
	Alter table TBConfiguracaoWeb add 
		InstrucoesSolImportacao varchar(max)

End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('JustificativaImportacaoObrigatoria'))
Begin
	Alter table TBConfiguracaoWeb add 
		PastaBaseSolicitacaoImportacao varchar(200),
		PastaBaseSolicitacaoImportacao_Azure varchar(200),
		JustificativaImportacaoObrigatoria bit,
		ObrigatorioLocalAlocadoSolImportacao bit,
		ObrigatorioIndicarFornecedoresImportacao bit,
		UtilizarValorEstimadoImportacao bit,
		UtilizarAprovacaoPesquisaPreco bit

End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('AprovarPesquisaPreco'))
Begin
	alter table TBRemotoProjetoPerfil add AprovarPesquisaPreco bit;
	alter table TBRemotoProjeto add AprovarPesquisaPreco bit;
End

GO --

if not exists (select * from sysobjects where name='TBItemSolicitacaoImportacao' and xtype='U')
Begin
	
	CREATE TABLE TBItemSolicitacaoImportacao(
						IDItemSolicitacaoImportacao int IDENTITY(1,1) NOT NULL,
						IDSolicitacaoImportacao int NULL,
						CodigoItem varchar(20) NULL,
						NCMHSCode varchar(25) NULL,
						NomeFabricante varchar(35) NULL,
						PaisProcedencia varchar(35) NULL,
						Unidade varchar(15) NULL,
						Quantidade float NULL,
						Descricao text NULL,
						ValorEstimado float NULL,	
						Desconto float null,
						VAT float null,											
						ObjetivoImportacao varchar(500) NULL,
						ObservacaoItemImportacao varchar(500) NULL,
						DespesaCapital bit NULL,
						MaterialConsumo bit NULL,
						Outros varchar(100) NULL,					
					 CONSTRAINT [PK_TBItemSolicitacaoImportacao] PRIMARY KEY CLUSTERED 
					(
						[IDItemSolicitacaoImportacao] ASC
					)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
					) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemSolicitacaoImportacao') AND name IN ('servico'))
Begin
	alter table TBItemSolicitacaoImportacao add servico bit;
End

GO --

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBCompra') AND name IN ('PesquisaLiberadaAprovacao'))
Begin
	alter table TBCompra add PesquisaLiberadaAprovacao bit default 0;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ObrigatorioAnexarArquivoDeclaracao'))
Begin
	Alter table TBConfiguracaoWeb add 
		ObrigatorioAnexarArquivoDeclaracao bit

End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('PermitirEstrangeiro'))
Begin
	Alter table TBTurma add 
		PermitirEstrangeiro bit,
		TraduzirIngles bit,
		TraduzirEspanhol bit

	Alter table TBInscricao
		alter column documento varchar(300)
End

GO --


Update TBTurma
set PermitirEstrangeiro = 0
where PermitirEstrangeiro is null

Update TBTurma
set TraduzirIngles = 0
where TraduzirIngles is null

Update TBTurma
set TraduzirEspanhol = 0
where TraduzirEspanhol is null


GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBContratoAnaliseTecnica') AND name IN ('IDUsuarioAprovacao'))
Begin
	Alter table TBContratoAnaliseTecnica add 
		IDUsuarioAprovacao int,
		DataAprovacao datetime
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBUsuarioNotificacao') AND name IN ('SolCompraMsgCompra'))
Begin
	Alter Table TBUsuarioNotificacao add 
		SolCompraMsgCompra bit
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoMoeda' and xtype='U')
Begin
	
	CREATE TABLE TBImportacaoMoeda (
	IDImportacaoMoeda int IDENTITY(1,1) NOT NULL,
	NomeMoeda varchar(60) NULL,
		 CONSTRAINT [PK_TBImportacaoMoeda] PRIMARY KEY CLUSTERED 
		(
			IDImportacaoMoeda ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]

END

GO --

if not exists (select * from sysobjects where name='TBImportacaoFormaPagamento' and xtype='U')
Begin
	
	CREATE TABLE TBImportacaoFormaPagamento (
	IDImportacaoFormaPagamento int IDENTITY(1,1) NOT NULL,
	FormaPagamento varchar(100) NULL,
		 CONSTRAINT [PK_TBImportacaoFormaPagamento] PRIMARY KEY CLUSTERED 
		(
			IDImportacaoFormaPagamento ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]

END

GO --

if not exists (select * from sysobjects where name='TBImportacaoPrevisaoEntrega' and xtype='U')
Begin
	
	CREATE TABLE TBImportacaoPrevisaoEntrega (
	IDImportacaoPrevisaoEntrega int IDENTITY(1,1) NOT NULL,
	PrevisaoEntrega varchar(100) NULL,
		 CONSTRAINT [PK_TBImportacaoPrevisaoEntrega] PRIMARY KEY CLUSTERED 
		(
			IDImportacaoPrevisaoEntrega ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]

END

GO --

if not exists (select * from sysobjects where name='TBImportacaoIncoterm' and xtype='U')
Begin
	
	CREATE TABLE TBImportacaoIncoterm (
	IDImportacaoIncoterm int IDENTITY(1,1) NOT NULL,
	Incoterm varchar(100) NULL,
		 CONSTRAINT [PK_TBImportacaoIncoterm] PRIMARY KEY CLUSTERED 
		(
			IDImportacaoIncoterm ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]

END

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('PrevisaoEntrega'))
Begin
	Alter Table TBSolicitacaoImportacao drop 
		column PrevisaoEntrega
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('FormaPagamento'))
Begin
	Alter Table TBSolicitacaoImportacao drop 
		column FormaPagamento
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('FormaEmbarque'))
Begin
	Alter Table TBSolicitacaoImportacao drop 
		column FormaEmbarque
End

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('IDImportacaoPrevisaoEntrega'))
Begin
	Alter Table TBSolicitacaoImportacao add 
		IDImportacaoPrevisaoEntrega int,
		IDImportacaoFormaPagamento int,
		IDImportacaoIncoterm int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('IDUsuarioLibCanc'))
Begin
	Alter Table TBSolicitacaoImportacao add 
		IDUsuarioLibCanc int,
		DataUsuarioLibCanc Datetime,
		StatusLibCanc int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('GetNetID'))
Begin
	Alter Table TBLancamento add 
		GetNetID varchar(max),
		GetNetTipo varchar(max)
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('PayPalCheckout'))
Begin
	Alter Table TBDoacao
		drop column PayPalCheckout
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('PayPalCheckout'))
Begin
	Alter Table TBTurma
		drop column PayPalCheckout
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ClientIDGetNet'))
Begin
	Alter table TBConfiguracaoWeb add 
		ClientIDGetNet varchar(max),
		ClientSecretGetNet varchar(max),
		SellerIDGetNet varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('GetNetBoleto'))
Begin
	Alter Table TBDoacao
		Add GetNetBoleto bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('GetNetCredito'))
Begin
	Alter Table TBDoacao
		Add GetNetCredito bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('GetNetDebito'))
Begin
	Alter Table TBDoacao
		Add GetNetDebito bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('GetNetCreditoEstrangeiro'))
Begin
	Alter Table TBDoacao
		Add GetNetCreditoEstrangeiro bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('GetNetBoleto'))
Begin
	Alter Table TBTurma
		Add GetNetBoleto bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('GetNetCredito'))
Begin
	Alter Table TBTurma
		Add GetNetCredito bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('GetNetDebito'))
Begin
	Alter Table TBTurma
		Add GetNetDebito bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('GetNetCreditoEstrangeiro'))
Begin
	Alter Table TBTurma
		Add GetNetCreditoEstrangeiro bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('PermitirEstrangeiro'))
Begin
	Alter table TBDoacao add 
		PermitirEstrangeiro bit,
		TraduzirIngles bit,
		TraduzirEspanhol bit
End

GO --


Update TBDoacao
set PermitirEstrangeiro = 0
where PermitirEstrangeiro is null

Update TBDoacao
set TraduzirIngles = 0
where TraduzirIngles is null

Update TBDoacao
set TraduzirEspanhol = 0
where TraduzirEspanhol is null

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('DataTermoAditivoTa'))
Begin
	Alter table TBSolicitacaoBolsa add 
		DataTermoAditivoTa int, 
		DataVigenciaInicioTa int,
		DataVigenciaTerminoTa int,
		TipoPlanoTrabalho bit, 
		TipoTermoContratual bit, 	
		TipoPrazo tinyint, 
		Dias int,
		TipoValor tinyint, 
		Valortermo float,
		DescricaoObjetoTa text,
		ObservacaoTa text		 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ExibirTermoAditivo'))
Begin
	Alter table TBConfiguracaoWeb add ExibirTermoAditivo bit default 1	
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('GetNetBoletoMensalidade'))
Begin
	Alter Table TBTurma
		Add GetNetBoletoMensalidade bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('GetNetCreditoMensalidade'))
Begin
	Alter Table TBTurma
		Add GetNetCreditoMensalidade bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('GetNetDebitoMensalidade'))
Begin
	Alter Table TBTurma
		Add GetNetDebitoMensalidade bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('BoletoMensalidade'))
Begin
	Alter Table TBTurma
		Add BoletoMensalidade bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('PagSeguroMensalidade'))
Begin
	Alter Table TBTurma
		Add PagSeguroMensalidade bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('GetNetCreditoEstrangeiroMensalidade'))
Begin
	Alter Table TBTurma
		Add GetNetCreditoEstrangeiroMensalidade bit
End

GO --

if not exists (select * from sysobjects where name='TBImportacao' and xtype='U')
Begin
		CREATE TABLE TBImportacao(
			IDImportacao int IDENTITY(1,1) NOT NULL,
			IDSolicitacaoImportacao int null,
			DataImportacao datetime NULL,
			DataAprovacao int NULL,
			NumeroProcesso varchar(50) NULL,
			DescricaoImportacaoFinanciador varchar(max) NULL,
			CepLocalAlocado varchar(9) NULL,
			EnderecoLocalAlocado varchar(100) NULL,
			NumeroLocalAlocado varchar(10) NULL,
			ComplementoLocalAlocado varchar(80) NULL,
			BairroLocalAlocado varchar(80) NULL,
			CidadelocalAlocado varchar(40) NULL,
			EstadoLocalAlocado varchar(40) NULL,
			UnidadeLocalAlocado varchar(40) NULL,
			CnpjUnidadeLocalAlocado varchar(18) NULL,
			NomeEmpresaInvoice varchar(100) NULL,
			ContatoEmpresaInvoice varchar(200) NULL,
			EmailEmpresaInvoice varchar(300) NULL,
			SiteEmpresaInvoice varchar(100) NULL,
			Telefone1EmpresaInvoice varchar(15) NULL,
			Telefone2EmpresaInvoice varchar(15) NULL,
			PaisEmpresaInvoice varchar(30) NULL,
			NomeExportador varchar(100) NULL,
			EmailExportador varchar(300) NULL,
			SiteExportador varchar(100) NULL,
			Telefone1Exportador varchar(15) NULL,
			Telefone2Exportador varchar(15) NULL,
			PaisExportador varchar(30) NULL,
			EnderecoExportador varchar(300) NULL,
			ZipCodeExportador varchar(30) NULL,
			CidadeExportador varchar(40) NULL,
			EstadoExportador varchar(40) NULL,
			DataProforma int NULL,
			NumeroProformaInvoice varchar(20) NULL,
			ValidadeProposta varchar(15) NULL,
			DataNR int NULL,
			NossaReferencia varchar(20) NULL,
			DataInvoice int NULL,
			NumeroInvoice varchar(20) NULL,
			AeroportoPortoOrigem varchar(100) NULL,
			AeroportoPortoDestino varchar(100) NULL,
			IDMoeda int NULL,
			OutrasCondicoesCompra varchar(max) NULL,
			InstituicaoFinanceira varchar(100) NULL,
			EnderecoAgencia varchar(200) NULL,
			NumeroAgencia varchar(20) NULL,
			NumeroConta varchar(30) NULL,
			IBAN varchar(50) NULL,
			SWFITCode varchar(50) NULL,
			InformacoesComplementares varchar(max) NULL,
			IDImportacaoPrevisaoEntrega int NULL,
			IDImportacaoFormaPagamento int NULL,
			IDImportacaoIncoterm int NULL
		 CONSTRAINT PK_TBImportacao PRIMARY KEY CLUSTERED 
	(
		IDImportacao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

if not exists (select * from sysobjects where name='TBItemImportacao' and xtype='U')
Begin
	CREATE TABLE TBItemImportacao(
		IDItemImportacao int IDENTITY(1,1) NOT NULL,
		IDImportacao int NULL,
		CodigoItem varchar(20) NULL,
		NCMHSCode varchar(25) NULL,
		NomeFabricante varchar(35) NULL,
		PaisProcedencia varchar(35) NULL,
		Unidade varchar(15) NULL,
		Quantidade float NULL,
		Descricao text NULL,
		ValorEstimado float NULL,
		Desconto float NULL,
		VAT float NULL,
		ObjetivoImportacao varchar(500) NULL,
		ObservacaoItemImportacao varchar(500) NULL,
		DespesaCapital bit NULL,
		MaterialConsumo bit NULL,
		Outros varchar(100) NULL,
		servico bit NULL,
	 CONSTRAINT PK_TBItemImportacao PRIMARY KEY CLUSTERED 
	(
		IDItemImportacao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('Dias'))
Begin
	Alter Table TBSolicitacaoBolsa
		alter column Dias int null

	Alter Table TBSolicitacaoBolsa
		alter column TipoPlanoTrabalho bit null

	Alter Table TBSolicitacaoBolsa
		alter column TipoTermoContratual bit null

	Alter Table TBSolicitacaoBolsa
		alter column TipoPrazo tinyint null

	Alter Table TBSolicitacaoBolsa
		alter column TipoValor tinyint null
End

GO --

update TBSolicitacaoBolsa
set Valortermo = 0
where Valortermo is null

update TBSolicitacaoBolsa
set Dias = 0
where Dias is null

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('TermoApostilamento'))
Begin
	Alter table TBSolicitacaoBolsa
		drop column TermoApostilamento 
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('TipoQuantidade'))
Begin
	Alter table TBSolicitacaoBolsa
		drop column TipoQuantidade 
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('Quantidade'))
Begin
	Alter table TBSolicitacaoBolsa
		drop column Quantidade 
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoFlag' and xtype='U')
Begin
	CREATE TABLE TBImportacaoFlag(
		IDImportacaoFlag int IDENTITY(1,1) NOT NULL,
		DescricaoFlag varchar(50) NULL
	 CONSTRAINT PK_TBImportacaoFlag PRIMARY KEY CLUSTERED 
	(
		IDImportacaoFlag ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoTipoCusto' and xtype='U')
Begin
	CREATE TABLE TBImportacaoTipoCusto(
		IDImportacaoTipoCusto int IDENTITY(1,1) NOT NULL,
		DescricaoTipo varchar(150) NULL,
		Tipo tinyint null
	 CONSTRAINT PK_TBImportacaoTipoCusto PRIMARY KEY CLUSTERED 
	(
		IDImportacaoTipoCusto ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoDadosCusto' and xtype='U')
Begin
	CREATE TABLE TBImportacaoDadosCusto(
		IDImportacaoDadosCusto int IDENTITY(1,1) NOT NULL,
		Tipo tinyint null,
		IDImportacao int null,
		IDImportacaoTipoServicoInformacao int null,
		Prestador varchar(250) NULL,
		NumeroDocumento varchar(40) NULL,
		Observacao varchar(max) NULL
	 CONSTRAINT PK_TBImportacaoDadosCusto PRIMARY KEY CLUSTERED 
	(
		IDImportacaoDadosCusto ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoCusto' and xtype='U')
Begin
	CREATE TABLE TBImportacaoCusto(
		IDImportacaoCusto int IDENTITY(1,1) NOT NULL,
		IDImportacaoTipoCusto int null,
		IDImportacao int null,
		Descricao varchar(300) NULL,
		IDImportacaoMoeda int null,
		ValorOriginal float null,
		TaxaConversao float null,
		IDImportacaoFlag int null,
		Observacao varchar(200) NULL
	 CONSTRAINT PK_TBImportacaoCusto PRIMARY KEY CLUSTERED 
	(
		IDImportacaoCusto ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoTipoServicoInformacao' and xtype='U')
Begin
	CREATE TABLE TBImportacaoTipoServicoInformacao(
		IDImportacaoTipoServicoInformacao int IDENTITY(1,1) NOT NULL,
		ImportacaoTipoServicoInformacao varchar(200) NULL
	 CONSTRAINT PK_TBImportacaoTipoServicoInformacao PRIMARY KEY CLUSTERED 
	(
		IDImportacaoTipoServicoInformacao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('UrlManagerWeb'))
Begin
	Alter table TBConfiguracaoWeb add UrlManagerWeb varchar(500)
End

GO --

if not exists (select * from sysobjects where name='TBAcessoEspecialWeb' and xtype='U')
Begin
	CREATE TABLE TBAcessoEspecialWeb(
		IDAcessoEspecialWeb int IDENTITY(1,1) NOT NULL,
		Descricao varchar(80) NULL,
		Usuario varchar(80) NULL,
		Senha varchar(80) NULL
	 CONSTRAINT PK_TBAcessoEspecialWeb PRIMARY KEY CLUSTERED 
	(
		IDAcessoEspecialWeb ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

if not exists (select * from sysobjects where name='TBAcessoEspecialProjetosWeb' and xtype='U')
Begin
	CREATE TABLE TBAcessoEspecialProjetosWeb(
		IDAcessoEspecialProjetosWeb int IDENTITY(1,1) NOT NULL,
		IDAcessoEspecialWeb int null,
		IDProjeto int null
	 CONSTRAINT PK_TBAcessoEspecialProjetosWeb PRIMARY KEY CLUSTERED 
	(
		IDAcessoEspecialProjetosWeb ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoCusto') AND name IN ('DataOcorrencia'))
Begin
	Alter table TBImportacaoCusto add DataOcorrencia int
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoInformacaoMonitor' and xtype='U')
Begin
	CREATE TABLE TBImportacaoInformacaoMonitor(
		IDImportacaoInformacaoMonitor int IDENTITY(1,1) NOT NULL,
		InformacaoMonitor varchar(150) NULL,
		Tipo tinyint null
	 CONSTRAINT PK_TBImportacaoInformacaoMonitor PRIMARY KEY CLUSTERED 
	(
		IDImportacaoInformacaoMonitor ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBAcessoEspecialWeb') AND name IN ('RelRubrica'))
Begin
	Alter table TBAcessoEspecialWeb add RelRubrica bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBAcessoEspecialWeb') AND name IN ('RelLancamentoFinanceiro'))
Begin
	Alter table TBAcessoEspecialWeb add RelLancamentoFinanceiro bit
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoMonitor' and xtype='U')
Begin
	CREATE TABLE TBImportacaoMonitor(
		IDImportacaoMonitor int IDENTITY(1,1) NOT NULL,
		IDImportacao int null,
		IDImportacaoInformacaoMonitor int NULL,
		DataMonitor Date null,
		Observacao Varchar(300) null
	 CONSTRAINT PK_TBImportacaoMonitor PRIMARY KEY CLUSTERED 
	(
		IDImportacaoMonitor ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoMonitor') AND name IN ('NaoAplica'))
Begin
	Alter table TBImportacaoMonitor add NaoAplica bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjeto') AND name IN ('RelControleItem'))
Begin
	alter table TBRemotoProjetoPerfil add RelControleItem bit;
	alter table TBRemotoProjeto add RelControleItem bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBAcessoEspecialWeb') AND name IN ('RelControleItem'))
Begin
	alter table TBAcessoEspecialWeb add RelControleItem bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoCusto') AND name IN ('CreditoDebito'))
Begin
	Alter table TBImportacaoCusto add CreditoDebito tinyint
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBWebPessoaFisicaJuridica' AND xtype='U')
Begin
	CREATE TABLE TBWebPessoaFisicaJuridica(
		-- DADOS CADASTRAIS
		IDPessoaFisicaJuridica int IDENTITY(1,1) NOT NULL, 	
		NomePessoaFisicaJuridica varchar(100) NOT NULL, 
		NomeFantasia varchar(100) NOT NULL, 
		DataNascimento int NULL, 
		Endereco varchar(100) NULL, 
		Numero varchar(10) NULL, 
		Complemento varchar(80) NULL, 
		Bairro varchar(80) NULL, 
		Cidade varchar(40) NULL, 
		Estado varchar(2) NULL, 		
		Cep varchar(9) NULL, 
		CPFCNPJ varchar(20) NOT NULL, 
		IdentidadeInscEstadual varchar(20) NULL, 
		CarteiraTrabalhoInscMunicipal varchar(20) NULL, 

		-- CONTATO
		Telefone1 varchar(20) NULL, 
		Telefone2 varchar(20) NULL, 
		Fax varchar(15) NULL, 

		-- CONTATO FINANCEIRO
		Contato varchar(200) NULL, 
		CargoFinanceiro varchar(40) NULL, 
		TelContatoFinanceiro varchar(15) NULL, 
		Email varchar(300) NULL, 

		-- CONTATO COMPRAS
		ContatoCompras varchar(60) NULL, 
		CargoCompras varchar(40) NULL, 
		TelContatoCompras varchar(15) NULL, 
		EmailPesquisa varchar(300) NULL, 
		EmailCompras varchar(300) NULL, 

		-- OUTROS CAMPOS
		DataCriacaoAcessoWeb datetime NULL, 
		DataAlteracaoAcessoWeb datetime NULL, 
		PessoaFisicaJuridica smallint NULL, 
		SenhaPessoaFisicaJuridica varchar(36) NULL 
	CONSTRAINT PK_TBWebPessoaFisicaJuridica PRIMARY KEY CLUSTERED 
	(
		IDPessoaFisicaJuridica ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBWebPessoaAtividade' AND xtype='U')
Begin
	CREATE TABLE TBWebPessoaAtividade(
		IDPessoaFisicaJuridica int NOT NULL, 
		IDAtividade int NOT NULL, 
		IDUsuario int NULL 
	CONSTRAINT PK_TBWebPessoaAtividade PRIMARY KEY CLUSTERED 
	(
		IDPessoaFisicaJuridica ASC,
		IDAtividade ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBWebPessoaCertidao' AND xtype='U')
Begin
	CREATE TABLE TBWebPessoaCertidao(
		IDPessoaCertidao int IDENTITY(1,1) NOT NULL, 
		IDPessoaFisicaJuridica int NOT NULL, 
		IDCertidao int NOT NULL, 
		DataValidade int NOT NULL, 
		DataValidadeFim int NULL, 
		NrCertidao varchar(30) NULL, 
		ObservacaoCertidao varchar(300) NULL 
	CONSTRAINT PK_TBWebPessoaCertidao PRIMARY KEY CLUSTERED 
	(
		IDPessoaCertidao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaCertidao') AND name IN ('NomeArquivo'))
ALTER TABLE TBPessoaCertidao ADD NomeArquivo varchar(300);
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebPessoaCertidao') AND name IN ('NomeArquivo'))
ALTER TABLE TBWebPessoaCertidao ADD NomeArquivo varchar(300);
GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBWebInfoBancaria' AND xtype='U')
Begin
	CREATE TABLE TBWebInfoBancaria(
		IDInfoBancaria int IDENTITY(1,1) NOT NULL, 
		IDPessoaFisicaJuridica int NOT NULL, 
		IDAgencia int NOT NULL, 
		NumeroConta varchar(50) NULL, 
		ContaPadrao smallint NULL, 
		TipoConta int NULL, 
		TipoPessoa tinyint NULL, 
		IDUsuario int NULL 
	CONSTRAINT PK_TBWebInfoBancaria PRIMARY KEY CLUSTERED 
	(
		IDInfoBancaria ASC,
		IDPessoaFisicaJuridica ASC,
		IDAgencia ASC 
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBWebPessoaSocio' AND xtype='U')
Begin
	CREATE TABLE TBWebPessoaSocio(
		IDPessoaSocio int IDENTITY(1,1) NOT NULL, 
		IDPessoaJuridica int NOT NULL, 
		DataEntrada int NOT NULL, 
		DataSaida int NULL, 
		NomeSocio varchar(60) NULL, 
		CPFSocio varchar(20) NULL, 
		PercentualCotas float NULL 
	CONSTRAINT PK_TBWebPessoaSocio PRIMARY KEY CLUSTERED 
	(
		IDPessoaSocio ASC,
		IDPessoaJuridica ASC 
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF EXISTS (SELECT * FROM sys.columns s
				left join sys.types t
				on s.system_type_id = t.system_type_id
				WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoMonitor') AND s.name IN ('DataMonitor') and t.name in ('date'))
Begin
	Alter table TBImportacaoMonitor
	drop column DataMonitor;


	Alter table TBImportacaoMonitor
	add DataMonitor float;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolPagamentoMenosRestricao'))
Begin
	Alter table TBConfiguracaoWeb add SolPagamentoMenosRestricao bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('IDRubrica'))
Begin
	Alter table TBSolicitacaoBolsa add IDRubrica int NULL;
	Alter table TBSolicitacaoBolsa add IDSubRubrica int NULL;
End

GO --

IF NOT EXISTS (SELECT * FROM TBConfiguracao1 WHERE UtilizaVersaoNovaWeb = 1)
Begin
	UPDATE TBConfiguracao1 set UtilizaVersaoNovaWeb = 1;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolPgtoPFEstrangeiro'))
Begin
	Alter table TBConfiguracaoWeb add SolPgtoPFEstrangeiro bit
	Alter table TBConfiguracaoWeb add SolPgtoPJEstrangeiro bit
	Alter table TBConfiguracaoWeb add InstrucoesSolPagamentoEstrangeiroPF varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesSolPagamentoEstrangeiroPJ varchar(max)
	Alter table TBConfiguracaoWeb add SolPagamentoEstrangeiroPFMsgConfirmacao varchar(max)
	Alter table TBConfiguracaoWeb add SolPagamentoEstrangeiroPJMsgConfirmacao varchar(max)
END
GO --
IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('EmailPrincipal'))
Begin
	Alter table TBPessoaFisicaJuridica add EmailPrincipal varchar(300)
END
GO --
IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('Pais'))
Begin
	Alter table TBSolicitacao add Pais varchar(30)
	Alter table TBSolicitacao alter column DocumentoTerceiro varchar(100)
END
GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBSolicitacaoCompraComplemento' AND xtype='U')
Begin
	CREATE TABLE TBSolicitacaoCompraComplemento(
	IDSolicitacaoCompraComplemento int IDENTITY(1,1) NOT NULL,
	IDSolicitacao int NULL,
	BemConsumo bit NULL,
	Equipamento bit NULL,
	ServicoComum bit NULL,
	ServicoEngenharia bit NULL,
	ImportacaoBemConsumo bit NULL,
	ImportacaoEquipamento bit NULL,
	ImportacaoServico bit NULL,
	LocacaoVeiculo bit NULL,
	CotacaoPrevia bit NULL,
	Dispensa bit NULL,
	Inexigibilidade bit NULL,
	SelecaoPublica bit NULL,
	AtaRegistroPreco bit NULL,
	 CONSTRAINT PK_TBSolicitacaoComplemento PRIMARY KEY CLUSTERED 
	(
		IDSolicitacaoCompraComplemento ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBLogAzure' AND xtype='U')
Begin
	CREATE TABLE TBLogAzure(
		IDLogAzure int IDENTITY(1,1) NOT NULL, 
		Operacao varchar(6) NOT NULL, 
		NomeModulo varchar(50) NOT NULL, 
		IDModulo int NOT NULL, 
		NomeContainer varchar(300) NOT NULL, 
		NomeBlob varchar(300) NOT NULL, 
		DataLog Datetime NOT NULL,
		TamanhoArquivo_KBs float NULL,
		IDUsuario int NOT NULL
	CONSTRAINT PK_TBLogAzure PRIMARY KEY CLUSTERED 
	(
		IDLogAzure ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBLogArquivoLocal' AND xtype='U')
Begin
	CREATE TABLE TBLogArquivoLocal(
		IDLogArquivoLocal int IDENTITY(1,1) NOT NULL, 
		Operacao varchar(6) NOT NULL, 
		NomeModulo varchar(50) NOT NULL, 
		IDModulo int NOT NULL, 
		CaminhoCompletoObjeto varchar(300) NOT NULL, 
		NomeObjeto varchar(300) NOT NULL, 
		DataLog Datetime NOT NULL,
		TamanhoArquivo_KBs float NULL,
		IDUsuario int NOT NULL
	CONSTRAINT PK_TBLogArquivoLocal PRIMARY KEY CLUSTERED 
	(
		IDLogArquivoLocal ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebPessoaFisicaJuridica') AND name IN ('inss'))
Begin
	Alter table TBWebPessoaFisicaJuridica add inss varchar(20), pispasep varchar(20)
End

GO --

IF Not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ClientIDSantander'))
Begin
	Alter table TBConfiguracaoWeb add ClientIDSantander  varchar(max)
	Alter table TBConfiguracaoWeb add ClientSecretSantander  varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('PixSantander'))
Begin
	Alter Table TBDoacao
		Add PixSantander bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('PixSantander'))
Begin
	Alter Table TBTurma
		Add PixSantander bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('ChavePagarme'))
Begin
	Alter Table TBConfiguracao1
		add ChavePagarme varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('FornecedorAvisoGeral'))
Begin
	ALTER TABLE TBConfiguracaoWeb ADD FornecedorAvisoGeral varchar(max);
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebPessoaFisicaJuridica') AND name IN ('PagamentoBoleto'))
Begin
	ALTER TABLE TBWebPessoaFisicaJuridica ADD PagamentoBoleto bit NULL DEFAULT 0;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('PagamentoBoleto'))
Begin
	ALTER TABLE TBPessoaFisicaJuridica ADD PagamentoBoleto bit NULL DEFAULT 0;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('EmailRecuperacaoSenha'))
Begin
	ALTER TABLE TBPessoaFisicaJuridica ADD EmailRecuperacaoSenha varchar(100)
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWEB') AND name IN ('MensagemRecuperacaoSenha'))
Begin
	ALTER TABLE TBConfiguracaoWEB ADD MensagemRecuperacaoSenha varchar(max)
END

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBTarefaStatus' AND xtype='U')
Begin
	CREATE TABLE TBTarefaStatus(
	IDTarefaStatus int IDENTITY(1,1) NOT NULL,
	Status varchar(150) NULL,
	 CONSTRAINT [PK_TBTarefaStatus] PRIMARY KEY CLUSTERED 
	(
		[IDTarefaStatus] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBTarefa' AND xtype='U')
Begin
	CREATE TABLE TBTarefa(
	IDTarefa int IDENTITY(1,1) NOT NULL,
	DataCriacao int NULL,
	DataEntrega int NULL,
	Titulo varchar(150) NULL,
	Descricao varchar(max) NULL,
	IDUsuarioCriacao int NULL,
	IDProjeto int NULL,
	IDTarefaStatus int NULL,
	DataFinalizacao int NULL,
	 CONSTRAINT [PK_TBTarefa] PRIMARY KEY CLUSTERED 
	(
		IDTarefa ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


	ALTER TABLE TBTarefa  WITH CHECK ADD  CONSTRAINT FK_TBTarefa_TBTarefaStatus FOREIGN KEY(IDTarefaStatus)
	REFERENCES TBTarefaStatus (IDTarefaStatus)


	ALTER TABLE TBTarefa CHECK CONSTRAINT[FK_TBTarefa_TBTarefaStatus]
End

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBTarefaUsuarios' AND xtype='U')
Begin
	CREATE TABLE TBTarefaUsuarios(
		IDTarefaUsuarios int NOT NULL,
		IDTarefa int NULL,
		PercentualConclusao float NULL,
	 CONSTRAINT PK_TBTarefaUsuarios PRIMARY KEY CLUSTERED 
	(
		IDTarefaUsuarios ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE TBTarefaUsuarios WITH CHECK ADD  CONSTRAINT FK_TBTarefaUsuarios_TBTarefa FOREIGN KEY(IDTarefa)
	REFERENCES TBTarefa (IDTarefa)

	ALTER TABLE TBTarefaUsuarios CHECK CONSTRAINT [FK_TBTarefaUsuarios_TBTarefa]
End

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('SenhaExpirada'))
Begin
	ALTER TABLE TBPessoaFisicaJuridica ADD SenhaExpirada bit NOT NULL DEFAULT 0;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('HabilitarRecorrencia'))
Begin
	Alter Table TBDoacao
		Add HabilitarRecorrencia bit,
		MensagemRecorrencia varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricao') AND name IN ('HabilitarRecorrencia'))
Begin
	Alter Table TBInscricao
		Add HabilitarRecorrencia bit
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBFormPergunta' AND xtype='U')
Begin
	CREATE TABLE TBFormPergunta(
		IDFormPergunta int IDENTITY(1,1) NOT NULL, 
		Pergunta  varchar(300) NOT NULL, 
		IsObrigatorio bit NOT NULL, 
		DataCriacao Datetime NOT NULL,

	CONSTRAINT PK_TBFormPergunta PRIMARY KEY CLUSTERED 
	(
		IDFormPergunta ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --


if ((select coalesce((Select count(*) from TBFormPergunta),0)) = 0)
Begin
	insert  into TBFormPergunta (Pergunta, IsObrigatorio, DataCriacao) values
	('Possui pessoa(s) jurídica(s) com participação acionária na empresa? Se sim, inclua na a(s) pessoa(s) jurídica(s) na lista abaixo.', '0', GetDate()), 
	('Algum integrante da Alta Administração ou seus familiares (até terceiro grau), mantém relação contratual com algum agente público? Se sim, inclua a(s) pessoa(s) na lista abaixo.', '0', GetDate()), 
	('Algum integrante da Alta Administração é familiar (até terceiro grau) de algum colaborador da(o) VAR_FUNDACAO, que ocupe posição gerencial, de membro da Diretoria Executiva ou do Conselho da(o) VAR_FUNDACAO? Se sim, inclua a(s) pessoa(s) na lista abaixo.', '0', GetDate()), 
	('A empresa, suas controladoras, controladas, coligadas ou consorciadas já foram acusadas, investigadas, processadas ou condenadas por fraude ou corrupção nos últimos 10 anos, no pais ou no exterior?', '0', GetDate()), 
	('Conselheiras(os), diretores, proprietárias(os) ou colaboradores sofreram condenações por tribunal, no pais ou no exterior, como infrator por fraude ou corrupção nos últimos 10 anos?', '0', GetDate()),
	('A empresa possui um Programa de Integridade com o objetivo de evitar, detectar e interromper a ocorrência de irregularidades, fraudes e corrupção?A empresa possui um Programa de Integridade com o objetivo de evitar, detectar e interromper a ocorrência de irregularidades, fraudes e corrupção?', '0', GetDate()), 
	('A empresa possui um Código de Conduta como referência institucional de comportamento para seus integrantes e partes interessadas?', '0', GetDate()), 
	('A empresa faz utilização de terceiros (pessoas físicas ou jurídicas) para atuarem como seus representantes no relacionamento proposto?', '0', GetDate()),
	('Cite, pelo menos, três empresas, com endereço, telefone e nome de contato que podem ser utilizadas como referências. Inclua as empresas na lista abaixo.', '0', GetDate()) 
End

GO -- 


if ((select coalesce((select IDFormPergunta from TBFormPergunta where Pergunta like 'Possui pessoa(s) jurídica(s) com participação acionária na empresa? Se sim, inclua na a(s) pessoa(s) jurídica(s) na lista abaixo.'),0)) > 0)
Begin	
	update TBFormPergunta set Pergunta = 
		'Possui pessoa(s) jurídica(s) com participação acionária na empresa? Se sim, inclua a(s) pessoa(s) jurídica(s) na lista abaixo.'
			where IDFormPergunta = (select IDFormPergunta from TBFormPergunta where 
				Pergunta like 'Possui pessoa(s) jurídica(s) com participação acionária na empresa? Se sim, inclua na a(s) pessoa(s) jurídica(s) na lista abaixo.')
End

GO -- 


if ((select coalesce((select IDFormPergunta from TBFormPergunta where Pergunta 
	like 'A empresa possui um Programa de Integridade com o objetivo de evitar, detectar e interromper a ocorrência de irregularidades, fraudes e corrupção?A empresa possui um Programa de Integridade com o objetivo de evitar, detectar e interromper a ocorrência de irregularidades, fraudes e corrupção?'),0)) > 0)
Begin	
	update TBFormPergunta set Pergunta = 
		'A empresa possui um Programa de Integridade com o objetivo de evitar, detectar e interromper a ocorrência de irregularidades, fraudes e corrupção?' 
			where IDFormPergunta = (select IDFormPergunta from TBFormPergunta where 
				Pergunta like 'A empresa possui um Programa de Integridade com o objetivo de evitar, detectar e interromper a ocorrência de irregularidades, fraudes e corrupção?A empresa possui um Programa de Integridade com o objetivo de evitar, detectar e interromper a ocorrência de irregularidades, fraudes e corrupção?')
End

GO -- 

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBFormRespostaFornecedor' AND xtype='U')
Begin
	CREATE TABLE TBFormRespostaFornecedor(
		IDFormRespostaFornecedor int IDENTITY(1,1) NOT NULL,
		IDFormPergunta int NOT NULL,
		IDFornecedor int NOT NULL,
		Resposta bit NOT NULL,
		DataCriacao Datetime NOT NULL

	-- Chave Primaria

	 CONSTRAINT PK_TBFormRespostaFornecedor PRIMARY KEY CLUSTERED 
	(
		IDFormRespostaFornecedor ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	-- Chave Estrangeira da tabela TBFormPergunta

	ALTER TABLE TBFormRespostaFornecedor WITH CHECK ADD CONSTRAINT FK_TBFormRespostaFornecedor_TBFormPergunta FOREIGN KEY(IDFormPergunta)
	REFERENCES TBFormPergunta (IDFormPergunta)

	ALTER TABLE TBFormRespostaFornecedor CHECK CONSTRAINT [FK_TBFormRespostaFornecedor_TBFormPergunta]

	-- Chave Estrangeira da tabela TBPessoaFisicaJuridica

	ALTER TABLE TBFormRespostaFornecedor WITH CHECK ADD CONSTRAINT FK_TBFormRespostaFornecedor_TBPessoaFisicaJuridica FOREIGN KEY(IDFornecedor)
	REFERENCES TBPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBFormRespostaFornecedor CHECK CONSTRAINT [FK_TBFormRespostaFornecedor_TBPessoaFisicaJuridica]

End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFormRespostaFornecedor') AND name IN ('Resposta') AND is_nullable = 0)
Begin
	Alter Table TBFormRespostaFornecedor
		alter column Resposta bit NULL
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFormRespostaFornecedor') AND name IN ('Resposta') AND TYPE_NAME(user_type_id) = 'bit')
Begin
	Alter Table TBFormRespostaFornecedor
		alter column Resposta tinyint NULL
End

GO --


IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBWebFormRespostaFornecedor' AND xtype='U')
Begin
	CREATE TABLE TBWebFormRespostaFornecedor(
		IDWebFormRespostaFornecedor int IDENTITY(1,1) NOT NULL,
		IDFormPergunta int NOT NULL,
		IDWebFornecedor int NOT NULL,
		Resposta bit NOT NULL,
		DataCriacao Datetime NOT NULL

	-- Chave Primaria

	 CONSTRAINT PK_TBWebFormRespostaFornecedor PRIMARY KEY CLUSTERED 
	(
		IDWebFormRespostaFornecedor ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	-- Chave Estrangeira da tabela TBFormPergunta

	ALTER TABLE TBWebFormRespostaFornecedor WITH CHECK ADD  CONSTRAINT FK_TBWebFormRespostaFornecedor_TBFormPergunta FOREIGN KEY(IDFormPergunta)
	REFERENCES TBFormPergunta (IDFormPergunta)

	ALTER TABLE TBWebFormRespostaFornecedor CHECK CONSTRAINT [FK_TBWebFormRespostaFornecedor_TBFormPergunta]

	-- Chave Estrangeira da tabela TBWebPessoaFisicaJuridica

	ALTER TABLE TBWebFormRespostaFornecedor WITH CHECK ADD  CONSTRAINT FK_TBWebFormRespostaFornecedor_TBWebPessoaFisicaJuridica FOREIGN KEY(IDWebFornecedor)
	REFERENCES TBWebPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBWebFormRespostaFornecedor CHECK CONSTRAINT [FK_TBWebFormRespostaFornecedor_TBWebPessoaFisicaJuridica]

End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebFormRespostaFornecedor') AND name IN ('Resposta') AND is_nullable = 0)
Begin
	Alter Table TBWebFormRespostaFornecedor
		alter column Resposta bit NULL
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebFormRespostaFornecedor') AND name IN ('Resposta') AND TYPE_NAME(user_type_id) = 'bit')
Begin
	Alter Table TBWebFormRespostaFornecedor
		alter column Resposta tinyint NULL
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBFormRespostaParticipacaoAcionaria' AND xtype='U')
Begin
	CREATE TABLE TBFormRespostaParticipacaoAcionaria(
		IDFormRespostaParticipacaoAcionaria int IDENTITY(1,1) NOT NULL,
		IDFormRespostaFornecedor int NOT NULL,
		CNPJ varchar(20) NOT NULL,
		NomeEmpresa varchar(100) NOT NULL, 	 
		Percentual float NOT NULL

	-- Chave Primaria

	 CONSTRAINT PK_TBFormRespostaParticipacaoAcionaria PRIMARY KEY CLUSTERED 
	(
		IDFormRespostaParticipacaoAcionaria ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	-- Chave Estrangeira da tabela TBFormRespostaFornecedor

	ALTER TABLE TBFormRespostaParticipacaoAcionaria WITH CHECK ADD CONSTRAINT FK_TBFormRespostaParticipacaoAcionaria_TBFormRespostaFornecedor FOREIGN KEY(IDFormRespostaFornecedor)
	REFERENCES TBFormRespostaFornecedor (IDFormRespostaFornecedor)

	ALTER TABLE TBFormRespostaParticipacaoAcionaria CHECK CONSTRAINT [FK_TBFormRespostaParticipacaoAcionaria_TBFormRespostaFornecedor]

End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFormRespostaParticipacaoAcionaria') AND name IN ('IDFornecedor'))
Begin
	Alter Table TBFormRespostaParticipacaoAcionaria
		Add IDFornecedor int NOT NULL

	ALTER TABLE TBFormRespostaParticipacaoAcionaria WITH CHECK ADD CONSTRAINT FK_TBFormRespostaParticipacaoAcionaria_TBPessoaFisicaJuridica FOREIGN KEY(IDFornecedor)
	REFERENCES TBPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBFormRespostaParticipacaoAcionaria CHECK CONSTRAINT [FK_TBFormRespostaParticipacaoAcionaria_TBPessoaFisicaJuridica]
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFormRespostaParticipacaoAcionaria') AND name IN ('IDFormRespostaFornecedor'))
Begin
	ALTER TABLE TBFormRespostaParticipacaoAcionaria DROP CONSTRAINT [FK_TBFormRespostaParticipacaoAcionaria_TBFormRespostaFornecedor]
	Alter Table TBFormRespostaParticipacaoAcionaria Drop Column IDFormRespostaFornecedor
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBWebFormRespostaParticipacaoAcionaria' AND xtype='U')
Begin
	CREATE TABLE TBWebFormRespostaParticipacaoAcionaria(
		IDWebFormRespostaParticipacaoAcionaria int IDENTITY(1,1) NOT NULL,
		IDWebFormRespostaFornecedor int NOT NULL,
		CNPJ varchar(20) NOT NULL,
		NomeEmpresa varchar(100) NOT NULL, 	 
		Percentual float NOT NULL

	-- Chave Primaria

	 CONSTRAINT PK_TBWebFormRespostaParticipacaoAcionaria PRIMARY KEY CLUSTERED 
	(
		IDWebFormRespostaParticipacaoAcionaria ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	-- Chave Estrangeira da tabela TBWebFormRespostaFornecedor

	ALTER TABLE TBWebFormRespostaParticipacaoAcionaria WITH CHECK ADD  CONSTRAINT FK_TBWebFormRespostaParticipacaoAcionaria_TBWebFormRespostaFornecedor FOREIGN KEY(IDWebFormRespostaFornecedor)
	REFERENCES TBWebFormRespostaFornecedor (IDWebFormRespostaFornecedor)

	ALTER TABLE TBWebFormRespostaParticipacaoAcionaria CHECK CONSTRAINT [FK_TBWebFormRespostaParticipacaoAcionaria_TBWebFormRespostaFornecedor]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebFormRespostaParticipacaoAcionaria') AND name IN ('IDWebFornecedor'))
Begin
	Alter Table TBWebFormRespostaParticipacaoAcionaria
		Add IDWebFornecedor int NOT NULL

	ALTER TABLE TBWebFormRespostaParticipacaoAcionaria WITH CHECK ADD CONSTRAINT FK_TBWebFormRespostaParticipacaoAcionaria_TBWebPessoaFisicaJuridica FOREIGN KEY(IDWebFornecedor)
	REFERENCES TBWebPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBWebFormRespostaParticipacaoAcionaria CHECK CONSTRAINT [FK_TBWebFormRespostaParticipacaoAcionaria_TBWebPessoaFisicaJuridica]
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebFormRespostaParticipacaoAcionaria') AND name IN ('IDWebFormRespostaFornecedor'))
Begin
	ALTER TABLE TBWebFormRespostaParticipacaoAcionaria DROP CONSTRAINT [FK_TBWebFormRespostaParticipacaoAcionaria_TBWebFormRespostaFornecedor]
	Alter Table TBWebFormRespostaParticipacaoAcionaria Drop Column IDWebFormRespostaFornecedor
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBFormRespostaRelacaoContratual' AND xtype='U')
Begin
	CREATE TABLE TBFormRespostaRelacaoContratual(
		IDFormRespostaRelacaoContratual int IDENTITY(1,1) NOT NULL,
		IDFormRespostaFornecedor int NOT NULL,
		NomeIntegrante varchar(100) NOT NULL,
		GrauParentesco varchar(20) NOT NULL,
		OrgaoAgentePublico varchar(30) NOT NULL,
		CargoAgentePublico varchar(30) NOT NULL,
		NomeAgentePublico varchar(100) NOT NULL,

	-- Chave Primaria

	 CONSTRAINT PK_TBFormRespostaRelacaoContratual PRIMARY KEY CLUSTERED 
	(
		IDFormRespostaRelacaoContratual ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	-- Chave Estrangeira da tabela TBFormRespostaFornecedor

	ALTER TABLE TBFormRespostaRelacaoContratual WITH CHECK ADD  CONSTRAINT FK_TBFormRespostaRelacaoContratual_TBFormRespostaFornecedor FOREIGN KEY(IDFormRespostaFornecedor)
	REFERENCES TBFormRespostaFornecedor (IDFormRespostaFornecedor)

	ALTER TABLE TBFormRespostaRelacaoContratual CHECK CONSTRAINT [FK_TBFormRespostaRelacaoContratual_TBFormRespostaFornecedor]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFormRespostaRelacaoContratual') AND name IN ('IDFornecedor'))
Begin
	Alter Table TBFormRespostaRelacaoContratual
		Add IDFornecedor int NOT NULL

	ALTER TABLE TBFormRespostaRelacaoContratual WITH CHECK ADD CONSTRAINT FK_TBFormRespostaRelacaoContratual_TBPessoaFisicaJuridica FOREIGN KEY(IDFornecedor)
	REFERENCES TBPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBFormRespostaRelacaoContratual CHECK CONSTRAINT [FK_TBFormRespostaRelacaoContratual_TBPessoaFisicaJuridica]
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFormRespostaRelacaoContratual') AND name IN ('IDFormRespostaFornecedor'))
Begin
	ALTER TABLE TBFormRespostaRelacaoContratual DROP CONSTRAINT [FK_TBFormRespostaRelacaoContratual_TBFormRespostaFornecedor]
	Alter Table TBFormRespostaRelacaoContratual Drop Column IDFormRespostaFornecedor
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBWebFormRespostaRelacaoContratual' AND xtype='U')
Begin
	CREATE TABLE TBWebFormRespostaRelacaoContratual(
		IDWebFormRespostaRelacaoContratual int IDENTITY(1,1) NOT NULL,
		IDWebFormRespostaFornecedor int NOT NULL,
		NomeIntegrante varchar(100) NOT NULL,
		GrauParentesco varchar(20) NOT NULL,
		OrgaoAgentePublico varchar(30) NOT NULL,
		CargoAgentePublico varchar(30) NOT NULL,
		NomeAgentePublico varchar(100) NOT NULL,

	-- Chave Primaria

	 CONSTRAINT PK_TBWebFormRespostaRelacaoContratual PRIMARY KEY CLUSTERED 
	(
		IDWebFormRespostaRelacaoContratual ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	-- Chave Estrangeira da tabela TBWebFormRespostaFornecedor

	ALTER TABLE TBWebFormRespostaRelacaoContratual WITH CHECK ADD  CONSTRAINT FK_TBWebFormRespostaRelacaoContratual_TBWebFormRespostaFornecedor FOREIGN KEY(IDWebFormRespostaFornecedor)
	REFERENCES TBWebFormRespostaFornecedor (IDWebFormRespostaFornecedor)

	ALTER TABLE TBWebFormRespostaRelacaoContratual CHECK CONSTRAINT [FK_TBWebFormRespostaRelacaoContratual_TBWebFormRespostaFornecedor]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebFormRespostaRelacaoContratual') AND name IN ('IDWebFornecedor'))
Begin
	Alter Table TBWebFormRespostaRelacaoContratual
		Add IDWebFornecedor int NOT NULL

	ALTER TABLE TBWebFormRespostaRelacaoContratual WITH CHECK ADD CONSTRAINT FK_TBWebFormRespostaRelacaoContratual_TBWebPessoaFisicaJuridica FOREIGN KEY(IDWebFornecedor)
	REFERENCES TBWebPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBWebFormRespostaRelacaoContratual CHECK CONSTRAINT [FK_TBWebFormRespostaRelacaoContratual_TBWebPessoaFisicaJuridica]
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebFormRespostaRelacaoContratual') AND name IN ('IDWebFormRespostaFornecedor'))
Begin
	ALTER TABLE TBWebFormRespostaRelacaoContratual DROP CONSTRAINT [FK_TBWebFormRespostaRelacaoContratual_TBWebFormRespostaFornecedor]
	Alter Table TBWebFormRespostaRelacaoContratual Drop Column IDWebFormRespostaFornecedor
End

GO --


IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBFormRespostaRelacaoFundacao' AND xtype='U')
Begin
	CREATE TABLE TBFormRespostaRelacaoFundacao(
		IDFormRespostaRelacaoFundacao int IDENTITY(1,1) NOT NULL,
		IDFormRespostaFornecedor int NOT NULL,
		NomeIntegrante varchar(100) NOT NULL,
		CargoIntegrante varchar(30) NOT NULL,
		GrauParentesco varchar(20) NOT NULL,
		NomeFuncionario varchar(100) NOT NULL,
		CargoFuncionario varchar(30) NOT NULL,

	-- Chave Primaria

	 CONSTRAINT PK_TBFormRespostaRelacaoFundacao PRIMARY KEY CLUSTERED 
	(
		IDFormRespostaRelacaoFundacao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	-- Chave Estrangeira da tabela TBFormRespostaFornecedor

	ALTER TABLE TBFormRespostaRelacaoFundacao WITH CHECK ADD  CONSTRAINT FK_TBFormRespostaRelacaoFundacao_TBFormRespostaFornecedor FOREIGN KEY(IDFormRespostaFornecedor)
	REFERENCES TBFormRespostaFornecedor (IDFormRespostaFornecedor)

	ALTER TABLE TBFormRespostaRelacaoFundacao CHECK CONSTRAINT [FK_TBFormRespostaRelacaoFundacao_TBFormRespostaFornecedor]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFormRespostaRelacaoFundacao') AND name IN ('IDFornecedor'))
Begin
	Alter Table TBFormRespostaRelacaoFundacao
		Add IDFornecedor int NOT NULL

	ALTER TABLE TBFormRespostaRelacaoFundacao WITH CHECK ADD CONSTRAINT FK_TBFormRespostaRelacaoFundacao_TBPessoaFisicaJuridica FOREIGN KEY(IDFornecedor)
	REFERENCES TBPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBFormRespostaRelacaoFundacao CHECK CONSTRAINT [FK_TBFormRespostaRelacaoFundacao_TBPessoaFisicaJuridica]
End

GO --


IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFormRespostaRelacaoFundacao') AND name IN ('IDFormRespostaFornecedor'))
Begin
	ALTER TABLE TBFormRespostaRelacaoFundacao DROP CONSTRAINT [FK_TBFormRespostaRelacaoFundacao_TBFormRespostaFornecedor]
	Alter Table TBFormRespostaRelacaoFundacao Drop Column IDFormRespostaFornecedor
End

GO --


IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBWebFormRespostaRelacaoFundacao' AND xtype='U')
Begin
	CREATE TABLE TBWebFormRespostaRelacaoFundacao(
		IDWebFormRespostaRelacaoFundacao int IDENTITY(1,1) NOT NULL,
		IDWebFormRespostaFornecedor int NOT NULL,
		NomeIntegrante varchar(100) NOT NULL,
		CargoIntegrante varchar(30) NOT NULL,
		GrauParentesco varchar(20) NOT NULL,
		NomeFuncionario varchar(100) NOT NULL,
		CargoFuncionario varchar(30) NOT NULL,

	-- Chave Primaria

	 CONSTRAINT PK_TBWebFormRespostaRelacaoFundacao PRIMARY KEY CLUSTERED 
	(
		IDWebFormRespostaRelacaoFundacao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	-- Chave Estrangeira da tabela TBWebFormRespostaFornecedor

	ALTER TABLE TBWebFormRespostaRelacaoFundacao WITH CHECK ADD  CONSTRAINT FK_TBWebFormRespostaRelacaoFundacao_TBWebFormRespostaFornecedor FOREIGN KEY(IDWebFormRespostaFornecedor)
	REFERENCES TBWebFormRespostaFornecedor (IDWebFormRespostaFornecedor)

	ALTER TABLE TBWebFormRespostaRelacaoFundacao CHECK CONSTRAINT [FK_TBWebFormRespostaRelacaoFundacao_TBWebFormRespostaFornecedor]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebFormRespostaRelacaoFundacao') AND name IN ('IDWebFornecedor'))
Begin
	Alter Table TBWebFormRespostaRelacaoFundacao
		Add IDWebFornecedor int NOT NULL

	ALTER TABLE TBWebFormRespostaRelacaoFundacao WITH CHECK ADD CONSTRAINT FK_TBWebFormRespostaRelacaoFundacao_TBWebPessoaFisicaJuridica FOREIGN KEY(IDWebFornecedor)
	REFERENCES TBWebPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBWebFormRespostaRelacaoFundacao CHECK CONSTRAINT [FK_TBWebFormRespostaRelacaoFundacao_TBWebPessoaFisicaJuridica]
End

GO --


IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebFormRespostaRelacaoFundacao') AND name IN ('IDWebFormRespostaFornecedor'))
Begin
	ALTER TABLE TBWebFormRespostaRelacaoFundacao DROP CONSTRAINT [FK_TBWebFormRespostaRelacaoFundacao_TBWebFormRespostaFornecedor]
	Alter Table TBWebFormRespostaRelacaoFundacao Drop Column IDWebFormRespostaFornecedor
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBFormRespostaEmpresaReferencia' AND xtype='U')
Begin
	CREATE TABLE TBFormRespostaEmpresaReferencia(
		IDFormRespostaEmpresaReferencia int IDENTITY(1,1) NOT NULL,
		IDFormRespostaFornecedor int NOT NULL,
		Nome varchar(100) NOT NULL,
		Endereco varchar(200) NOT NULL,
		Telefone varchar(20) NOT NULL,
		NomePessoaContato varchar(100) NOT NULL

	-- Chave Primaria

	 CONSTRAINT PK_TBFormRespostaEmpresaReferencia PRIMARY KEY CLUSTERED 
	(
		IDFormRespostaEmpresaReferencia ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	-- Chave Estrangeira da tabela TBFormRespostaFornecedor

	ALTER TABLE TBFormRespostaEmpresaReferencia WITH CHECK ADD  CONSTRAINT FK_TBFormRespostaEmpresaReferencia_TBFormRespostaFornecedor FOREIGN KEY(IDFormRespostaFornecedor)
	REFERENCES TBFormRespostaFornecedor (IDFormRespostaFornecedor)

	ALTER TABLE TBFormRespostaEmpresaReferencia CHECK CONSTRAINT [FK_TBFormRespostaEmpresaReferencia_TBFormRespostaFornecedor]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFormRespostaEmpresaReferencia') AND name IN ('IDFornecedor'))
Begin
	Alter Table TBFormRespostaEmpresaReferencia
		Add IDFornecedor int NOT NULL

	ALTER TABLE TBFormRespostaEmpresaReferencia WITH CHECK ADD CONSTRAINT FK_TBFormRespostaEmpresaReferencia_TBPessoaFisicaJuridica FOREIGN KEY(IDFornecedor)
	REFERENCES TBPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBFormRespostaEmpresaReferencia CHECK CONSTRAINT [FK_TBFormRespostaEmpresaReferencia_TBPessoaFisicaJuridica]
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFormRespostaEmpresaReferencia') AND name IN ('IDFormRespostaFornecedor'))
Begin
	ALTER TABLE TBFormRespostaEmpresaReferencia DROP CONSTRAINT [FK_TBFormRespostaEmpresaReferencia_TBFormRespostaFornecedor]
	Alter Table TBFormRespostaEmpresaReferencia Drop Column IDFormRespostaFornecedor
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBWebFormRespostaEmpresaReferencia' AND xtype='U')
Begin
	CREATE TABLE TBWebFormRespostaEmpresaReferencia(
		IDWebFormRespostaEmpresaReferencia int IDENTITY(1,1) NOT NULL,
		IDWebFormRespostaFornecedor int NOT NULL,
		Nome varchar(100) NOT NULL,
		Endereco varchar(200) NOT NULL,
		Telefone varchar(20) NOT NULL,
		NomePessoaContato varchar(100) NOT NULL

	-- Chave Primaria

	 CONSTRAINT PK_TBWebFormRespostaEmpresaReferencia PRIMARY KEY CLUSTERED 
	(
		IDWebFormRespostaEmpresaReferencia ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	-- Chave Estrangeira da tabela TBWebFormRespostaFornecedor

	ALTER TABLE TBWebFormRespostaEmpresaReferencia WITH CHECK ADD  CONSTRAINT FK_TBWebFormRespostaEmpresaReferencia_TBWebFormRespostaFornecedor FOREIGN KEY(IDWebFormRespostaFornecedor)
	REFERENCES TBWebFormRespostaFornecedor (IDWebFormRespostaFornecedor)

	ALTER TABLE TBWebFormRespostaEmpresaReferencia CHECK CONSTRAINT [FK_TBWebFormRespostaEmpresaReferencia_TBWebFormRespostaFornecedor]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebFormRespostaEmpresaReferencia') AND name IN ('IDWebFornecedor'))
Begin
	Alter Table TBWebFormRespostaEmpresaReferencia
		Add IDWebFornecedor int NOT NULL

	ALTER TABLE TBWebFormRespostaEmpresaReferencia WITH CHECK ADD CONSTRAINT FK_TBWebFormRespostaEmpresaReferencia_TBWebPessoaFisicaJuridica FOREIGN KEY(IDWebFornecedor)
	REFERENCES TBWebPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBWebFormRespostaEmpresaReferencia CHECK CONSTRAINT [FK_TBWebFormRespostaEmpresaReferencia_TBWebPessoaFisicaJuridica]

End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebFormRespostaEmpresaReferencia') AND name IN ('IDWebFormRespostaFornecedor'))
Begin
	ALTER TABLE TBWebFormRespostaEmpresaReferencia DROP CONSTRAINT [FK_TBWebFormRespostaEmpresaReferencia_TBWebFormRespostaFornecedor]
	Alter Table TBWebFormRespostaEmpresaReferencia Drop Column IDWebFormRespostaFornecedor
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemSolicitacaoCompra') AND name IN ('DataHora_BI_Criacao'))
Begin
	Alter table TBItemSolicitacaoCompra
	add DataHora_BI_Criacao Datetime default GETDATE(),
		DataHora_BI_Liberacao Datetime null,
		DataHora_BI_Cancelamento Datetime null

	Declare @trig varchar(max)

	IF NOT EXISTS (SELECT * FROM sys.triggers WHERE  OBJECT_ID = OBJECT_ID(N'trgSolicitacaoCompra'))
	Begin
		set @trig = 'Create TRIGGER trgSolicitacaoCompra 
						ON TBSolicitacaoCompra 
						AFTER Update AS 
							DECLARE @SolWeb int
							DECLARE @Data_Liberacao_Antes int, @Data_Liberacao_Depois int 
							DECLARE @Status_Antes int, @Status_Depois int 

							Select @Data_Liberacao_Antes = coalesce(LiberacaoCoordenador,0) FROM DELETED 
							Select @Data_Liberacao_Depois = coalesce(LiberacaoCoordenador,0) FROM INSERTED 

							Select @Status_Antes = coalesce(StatusSolicitacao,0) FROM DELETED 
							Select @Status_Depois = coalesce(StatusSolicitacao,0) FROM INSERTED

							SELECT @SolWeb = IDSolicitacaocompra 
								FROM INSERTED 
	
							If (@SolWeb > 0) 
							Begin  
								If (@Data_Liberacao_Antes <> @Data_Liberacao_Depois)  
								Begin   
									Update TBItemSolicitacaoCompra   
									Set DataHora_BI_Liberacao  = GETDATE() 
									Where IDSolicitacaocompra = @SolWeb  
								End 

								If ( (@Status_Antes <> @Status_Depois)  and (@Status_Depois = 3) )
								Begin   
									Update TBItemSolicitacaoCompra   
									Set DataHora_BI_Cancelamento  = GETDATE() 
									Where IDSolicitacaocompra = @SolWeb  
								End 
							End'

		exec(@trig)
	End
	Else 
	Begin
		set @trig = 'ALTER TRIGGER trgSolicitacaoCompra 
					ON TBSolicitacaoCompra 
					AFTER Update AS 
						DECLARE @SolWeb int
						DECLARE @Data_Liberacao_Antes int, @Data_Liberacao_Depois int 
						DECLARE @Status_Antes int, @Status_Depois int 

						Select @Data_Liberacao_Antes = coalesce(LiberacaoCoordenador,0) FROM DELETED 
						Select @Data_Liberacao_Depois = coalesce(LiberacaoCoordenador,0) FROM INSERTED 

						Select @Status_Antes = coalesce(StatusSolicitacao,0) FROM DELETED 
						Select @Status_Depois = coalesce(StatusSolicitacao,0) FROM INSERTED

						SELECT @SolWeb = IDSolicitacaocompra 
							FROM INSERTED 
	
						If (@SolWeb > 0) 
						Begin  
							If (@Data_Liberacao_Antes <> @Data_Liberacao_Depois)  
							Begin   
								Update TBItemSolicitacaoCompra   
								Set DataHora_BI_Liberacao  = GETDATE() 
								Where IDSolicitacaocompra = @SolWeb  
							End 

							If ( (@Status_Antes <> @Status_Depois)  and (@Status_Depois = 3) )
							Begin   
								Update TBItemSolicitacaoCompra   
								Set DataHora_BI_Cancelamento  = GETDATE() 
								Where IDSolicitacaocompra = @SolWeb  
							End 
						End'
		exec(@trig)
	End
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemSolicitacaoCompra') AND name IN ('DataHora_BI_Fundacao_Cancelamento'))
Begin
	Alter table TBItemSolicitacaoCompra
	add DataHora_BI_Fundacao_Cancelamento Datetime null,
	DataHora_BI_Fundacao_Liberacao Datetime null

	Declare @trig varchar(max)

	set @trig = 'Create TRIGGER trgItemSolicitacaoCompra 
					ON TBItemSolicitacaoCompra 
					AFTER Update AS 
						DECLARE @IDItemSolicitacaoCompra int
						DECLARE @Item_Cancelamento_Antes int, @Item_Cancelamento_Depois int 
						DECLARE @Item_Compra_Antes int, @Item_Compra_Depois int 

						Select @Item_Cancelamento_Antes = coalesce(IDUsuarioCancelamento,0) FROM DELETED 
						Select @Item_Cancelamento_Depois = coalesce(IDUsuarioCancelamento,0) FROM INSERTED 

						Select @Item_Compra_Antes = coalesce(IDCompra,0) FROM DELETED 
						Select @Item_Compra_Depois = coalesce(IDCompra,0) FROM INSERTED

						SELECT @IDItemSolicitacaoCompra = IDItemSolicitacaoCompra 
							FROM INSERTED 
	
						If (@IDItemSolicitacaoCompra > 0) 
						Begin  
							If (@Item_Cancelamento_Antes <> @Item_Cancelamento_Depois)  
							Begin
								If (@Item_Cancelamento_Depois <> 0)  
								Begin
									Update TBItemSolicitacaoCompra   
									Set DataHora_BI_Fundacao_Cancelamento  = GETDATE() 
									Where IDItemSolicitacaoCompra = @IDItemSolicitacaoCompra
								End
								Else
								Begin
									Update TBItemSolicitacaoCompra   
									Set DataHora_BI_Fundacao_Cancelamento  = NULL 
									Where IDItemSolicitacaoCompra = @IDItemSolicitacaoCompra
								End								  
							End 

							If (@Item_Compra_Antes <> @Item_Compra_Depois)  
							Begin
								If (@Item_Compra_Depois <> 0)  
								Begin
									Update TBItemSolicitacaoCompra   
									Set DataHora_BI_Fundacao_Liberacao  = GETDATE() 
									Where IDItemSolicitacaoCompra = @IDItemSolicitacaoCompra 
								End
								Else
								Begin
									Update TBItemSolicitacaoCompra   
									Set DataHora_BI_Fundacao_Liberacao  = Null
									Where IDItemSolicitacaoCompra = @IDItemSolicitacaoCompra
								End
							End 
						End'

	exec(@trig)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolFatPFEstrangeiro'))
Begin
	Alter table TBConfiguracaoWeb add SolFatPFEstrangeiro bit
	Alter table TBConfiguracaoWeb add SolFatPJEstrangeiro bit
	Alter table TBConfiguracaoWeb add InstrucoesSolFaturamentoEstrangeiroPF varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesSolFaturamentoEstrangeiroPJ varchar(max)
	Alter table TBConfiguracaoWeb add SolFaturamentoEstrangeiroPFMsgConfirmacao varchar(max)
	Alter table TBConfiguracaoWeb add SolFaturamentoEstrangeiroPJMsgConfirmacao varchar(max)
END
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoFaturamento') AND name IN ('TipoSolicitacao'))
Begin
	Alter table TBSolicitacaoFaturamento add TipoSolicitacao tinyint
	Alter table TBSolicitacaoFaturamento Alter column CNPJ varchar(200)
END
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoFaturamento') AND name IN ('Pais'))
Begin
	Alter table TBSolicitacaoFaturamento add Pais varchar(30)
	Alter table TBSolicitacaoFaturamento add PaisCobranca varchar(30)
END
GO --

Update TBSolicitacaoFaturamento set TipoSolicitacao = 1
where TipoSolicitacao is null

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('ExibirValorAplicRelFin'))
Begin
	Alter table TBProjeto add ExibirValorAplicRelFin tinyint;	
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTarefaUsuarios') AND name IN ('IDUsuario'))
Begin
	Drop table TBTarefaUsuarios
	
	CREATE TABLE TBTarefaUsuarios(
		IDTarefaUsuarios int IDENTITY(1,1) NOT NULL,
		IDUsuario int NULL,
		IDTarefa int NULL,
		PercentualConclusao float NULL,
	 CONSTRAINT PK_TBTarefaUsuarios PRIMARY KEY CLUSTERED 
	(
		IDTarefaUsuarios ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE TBTarefaUsuarios WITH CHECK ADD  CONSTRAINT FK_TBTarefaUsuarios_TBTarefa FOREIGN KEY(IDTarefa)
	REFERENCES TBTarefa (IDTarefa)

	ALTER TABLE TBTarefaUsuarios CHECK CONSTRAINT [FK_TBTarefaUsuarios_TBTarefa]

	Update TBProjeto set ExibirValorAplicRelFin = (Select ExibirValorAplicRelFin from TBConfiguracao);
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('SolTransferencia'))
Begin
	alter table TBRemotoProjetoPerfil add SolTransferencia bit;
	alter table TBRemotoProjeto add SolTransferencia bit;
End

GO --

if (not exists (select * from sysobjects where name='RetornaProjetoSolTransfCredito'))
Begin
	Declare @funct varchar(max)

	set @funct = '
		Create FUNCTION RetornaProjetoSolTransfCredito (    @IDSol int  )   
		RETURNS varchar(max)   
		AS   
		BEGIN     
			DECLARE @Projetos as varchar(max)    
			Declare @AuxNomeProjeto as varchar(max)    
	
			set @Projetos = ''''  
			DECLARE CURSOR_OBJECTS_SolProjetos 
			CURSOR FOR   
				Select 
					distinct(P.NomeProjeto) as NomeProjeto
				from TBSolTransRateioCredito SRC with(nolock) 
				inner join TBProjeto P with(nolock) 
				on SRC.IDProjetoCredito = P.IDProjeto
				where SRC.IDSolicitacaoTransferencia = @IDSol 
	
			OPEN CURSOR_OBJECTS_SolProjetos     
	
			FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			WHILE @@FETCH_STATUS = 0     
			BEGIN      
				IF (len(@Projetos) = 0)        
				Set @Projetos = @AuxNomeProjeto;      
				Else        
				Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
				FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			END     
	
			CLOSE CURSOR_OBJECTS_SolProjetos     
			DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
			RETURN coalesce(@Projetos,'''') 
		END'

	exec(@funct)
end

GO --

if (not exists (select * from sysobjects where name='RetornaProjetoSolTransfDebito'))
Begin
	Declare @funct varchar(max)

	set @funct = '
		Create FUNCTION RetornaProjetoSolTransfDebito (    @IDSol int  )   
		RETURNS varchar(max)   
		AS   
		BEGIN     
			DECLARE @Projetos as varchar(max)    
			Declare @AuxNomeProjeto as varchar(max)    
	
			set @Projetos = ''''  
			DECLARE CURSOR_OBJECTS_SolProjetos 
			CURSOR FOR   
				Select 
					distinct(P.NomeProjeto) as NomeProjeto
				from TBSolTransRateioDebito SRD with(nolock) 
				inner join TBProjeto P with(nolock) 
				on SRD.IDProjetoDebito = P.IDProjeto
				where SRD.IDSolicitacaoTransferencia = @IDSol 
	
			OPEN CURSOR_OBJECTS_SolProjetos     
	
			FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			WHILE @@FETCH_STATUS = 0     
			BEGIN      
				IF (len(@Projetos) = 0)        
				Set @Projetos = @AuxNomeProjeto;      
				Else        
				Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
				FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			END     
	
			CLOSE CURSOR_OBJECTS_SolProjetos     
			DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
			RETURN coalesce(@Projetos,'''') 
		END'

	exec(@funct)
end

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTarefa') AND name IN ('MensagemStatus'))
Begin
	alter table TBTarefa 
		add MensagemStatus varchar(max),
		DataInicioTarefa int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBUsuarioNotificacao') AND name IN ('SolTarefasMsg'))
Begin
	Alter Table TBUsuarioNotificacao add 
		SolTarefasMsg bit
End

GO --

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='TBTarefaUsuariosMensagem' AND xtype='U')
Begin
	CREATE TABLE TBTarefaUsuariosMensagem(
		IDTarefaUsuariosMensagem int IDENTITY(1,1) NOT NULL,
		IDUsuario int NULL,
		IDTarefa int NULL,
		Mensagem varchar(max) NULL,
		DataHoraCriacao datetime default getdate(),
	 CONSTRAINT [PK_TBTarefaUsuariosMensagem] PRIMARY KEY CLUSTERED 
	(
		[IDTarefaUsuariosMensagem] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE TBTarefa add LembreteTarefaDias int null
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoFaturamento') AND name IN ('IDMoeda'))
Begin
	Alter table TBSolicitacaoFaturamento add IDMoeda int
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseTarefa'))
Begin
	Alter table TBConfiguracaoWeb add 
		PastaBaseTarefa varchar(200)
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('CargaHoraria'))
Begin
	Alter table TBSolicitacaoBolsa add 
		CargaHoraria varchar(50)
END

GO --

--IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ObrigatorioArquivo'))
--BEGIN
--	ALTER TABLE TBConfiguracaoWeb ADD ObrigatorioArquivo bit DEFAULT 0;
--END

--GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ObrigatorioArquivo'))
Begin
	exec sp_rename 'TBConfiguracaoWeb.[ObrigatorioArquivo]', 'ObrigatorioArquivoSolBolsa', 'column';
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ObrigatorioArquivoSolBolsa'))
BEGIN
	ALTER TABLE TBConfiguracaoWeb ADD ObrigatorioArquivoSolBolsa bit DEFAULT 0;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ObrigatorioArquivoSolReembolsoTerceiro'))
BEGIN
	ALTER TABLE TBConfiguracaoWeb ADD ObrigatorioArquivoSolReembolsoTerceiro bit DEFAULT 0;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoTipoArquivo'))
Begin
	CREATE TABLE TBSolicitacaoTipoArquivo(
		IDSolicitacaoTipoArquivo int IDENTITY(1,1) NOT NULL,
		TipoArquivo varchar(300) NOT NULL,
		Obrigatorio bit NOT NULL DEFAULT 0,
		Ativo bit NOT NULL DEFAULT 1,
		IDUsuario int NULL,
		Modulo int NULL,
		DataCriacao datetime NOT NULL DEFAULT getdate(),
		DataAlteracao datetime NOT NULL DEFAULT getdate(),
		CONSTRAINT PK_TBSolicitacaoTipoArquivo PRIMARY KEY CLUSTERED 
	(
		IDSolicitacaoTipoArquivo ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO --

if ((SELECT coalesce((SELECT COUNT(*) FROM TBSolicitacaoTipoArquivo WHERE TipoArquivo = 'Sem Tipo Definido' AND Modulo = 2), 0)) = 0)
Begin
	INSERT INTO TBSolicitacaoTipoArquivo (TipoArquivo, Obrigatorio, Modulo) VALUES ('Sem Tipo Definido', 0, 2);
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsaArquivo'))
Begin
	CREATE TABLE TBSolicitacaoBolsaArquivo(
		IDSolicitacaoBolsaArquivo int IDENTITY(1,1) NOT NULL,
		IDSolicitacaoTipoArquivo int NOT NULL,
		IDSolicitacaoBolsa int NOT NULL,
		NomeArquivo varchar(300) NOT NULL,
		CONSTRAINT PK_TBSolicitacaoBolsaArquivo PRIMARY KEY CLUSTERED 
	(
		IDSolicitacaoBolsaArquivo ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 

	ALTER TABLE TBSolicitacaoBolsaArquivo WITH CHECK ADD CONSTRAINT FK_TBSolicitacaoBolsaArquivo_TBSolicitacaoTipoArquivo FOREIGN KEY(IDSolicitacaoTipoArquivo)
	REFERENCES TBSolicitacaoTipoArquivo (IDSolicitacaoTipoArquivo)

	ALTER TABLE TBSolicitacaoBolsaArquivo WITH CHECK ADD CONSTRAINT FK_TBSolicitacaoBolsaArquivo_TBSolicitacaoBolsa FOREIGN KEY(IDSolicitacaoBolsa)
	REFERENCES TBSolicitacaoBolsa (IDSolicitacaoBolsa)
End

GO --

if ((select coalesce((Select count(*) from TBSolicitacaoTipoArquivo), 0)) > 0)
Begin
	UPDATE TBSolicitacaoBolsaArquivo 
		SET IDSolicitacaoTipoArquivo = (SELECT IDSolicitacaoTipoArquivo FROM TBSolicitacaoTipoArquivo WHERE TipoArquivo = 'Sem Tipo Definido' AND Modulo = 2)
		WHERE IDSolicitacaoTipoArquivo IN 
			(SELECT IDSolicitacaoTipoArquivo FROM TBSolicitacaoTipoArquivo WHERE DataCriacao < CONVERT(DATETIME, CONVERT(VARCHAR, '2022-07-01', 120), 120) AND Modulo = 2)

	DELETE FROM TBSolicitacaoTipoArquivo WHERE DataCriacao < CONVERT(DATETIME, CONVERT(VARCHAR, '2022-07-01', 120), 120);
End

GO -- 

if (not exists (select * from sysobjects where name='RetornaUsuariosTarefa'))
Begin
	Declare @funct varchar(max)

	set @funct = '
		Create FUNCTION RetornaUsuariosTarefa (    @IDTarefa int  )   
		RETURNS varchar(max)   
		AS   
		BEGIN     
			DECLARE @Usuarios as varchar(max)    
			Declare @AuxNomeUsuario as varchar(max)    
	
			set @Usuarios = ''''  
			DECLARE CURSOR_OBJECTS_USUARIOS 
			CURSOR FOR   
				Select 
					distinct(U.NomeUsuario) as NomeUsuario
				from TBTarefaUsuarios TU with(nolock) 
				inner join TBUsuario U with(nolock) 
				on TU.IDUsuario = U.IDUsuario
				where TU.IDTarefa = @IDTarefa 
	
			OPEN CURSOR_OBJECTS_USUARIOS     
	
			FETCH NEXT FROM CURSOR_OBJECTS_USUARIOS INTO @AuxNomeUsuario     
			WHILE @@FETCH_STATUS = 0     
			BEGIN      
				IF (len(@Usuarios) = 0)        
				Set @Usuarios = @AuxNomeUsuario;      
				Else        
				Set @Usuarios = @Usuarios + '', '' + @AuxNomeUsuario;       
			
				FETCH NEXT FROM CURSOR_OBJECTS_USUARIOS INTO @AuxNomeUsuario     
			END     
	
			CLOSE CURSOR_OBJECTS_USUARIOS     
			DEALLOCATE CURSOR_OBJECTS_USUARIOS     
	
			RETURN coalesce(@Usuarios,'''') 
		END'

	exec(@funct)
end

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTarefa') AND name IN ('DataNotificacao'))
Begin
	alter table TBTarefa 
		add DataNotificacao datetime
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ValorLimiteCompraDireta'))
Begin
	alter table TBConfiguracaoWeb 
		add ValorLimiteCompraDireta float,
		ObservacaoCompraDiretaReembolso varchar(max),
		ObservacaoCompraDiretaPagamento varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('ValorLimiteCompraDireta'))
Begin
	alter table TBProjeto 
		add ValorLimiteCompraDireta float,
		PermiteUtilizarCompraDireta bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('FromAdressEmailSenhaWeb'))
BEGIN
	ALTER TABLE TBConfiguracaoWeb ADD FromAdressEmailSenhaWeb varchar(80) DEFAULT NULL;
	ALTER TABLE TBConfiguracaoWeb ADD FromNameEmailSenhaWeb varchar(80) DEFAULT NULL;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('HabilitarDUEDiligence'))
BEGIN
	ALTER TABLE TBConfiguracaoWeb ADD HabilitarDUEDiligence bit DEFAULT 0;
	ALTER TABLE TBConfiguracaoWeb ADD HabilitarDUEDiligenceWeb bit DEFAULT 0;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('HabilitarDUEDiligence'))
BEGIN
	ALTER TABLE TBPessoaFisicaJuridica ADD HabilitarDUEDiligence bit DEFAULT 0;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('SolCompraDireta'))
Begin
	alter table TBRemotoProjetoPerfil add SolCompraDireta bit;
	alter table TBRemotoProjeto add SolCompraDireta bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('CompraDireta'))
Begin
	alter table TBSolicitacao add CompraDireta bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolsoTerceiro') AND name IN ('CompraDireta'))
Begin
	alter table TBSolicitacaoReembolsoTerceiro add CompraDireta bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('IDMoeda'))
Begin
	Alter table TBSolicitacao add IDMoeda int
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjetoPlanoTrabalhoExecucao'))
Begin
	CREATE TABLE TBProjetoPlanoTrabalhoExecucao(
		IDProjetoPlanoTrabalhoExecucao int IDENTITY(1,1) NOT NULL,
		IDRelatorioTecnico int NULL,
		Meta varchar(max) NULL,
		Etapa varchar(max) NULL,
		Atividade varchar(max) NULL,
		QuantidadePrevista float NULL,
		Unidade varchar(max) NULL,
		QuantidadeExecutada float NULL,
		IDCoordenador int NULL,
		DataHoraCriacao datetime NULL,
	 CONSTRAINT PK_TBProjetoPlanoTrabalhoExecucao PRIMARY KEY CLUSTERED 
	(
		IDProjetoPlanoTrabalhoExecucao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END

GO -- 

Alter Table TBPessoaFisicaJuridica
alter column PagamentoBoleto bit null

Alter Table TBPessoaFisicaJuridica
alter column SenhaExpirada bit null

Alter Table TBPessoaFisicaJuridica
alter column AcessoWebLiberado bit null

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolsoTerceiro') AND name IN ('Observacao'))
ALTER TABLE TBSolicitacaoReembolsoTerceiro ADD Observacao text;

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjetoPlanoTrabalho'))
Begin
	CREATE TABLE TBProjetoPlanoTrabalho(
		IDProjetoPlanoTrabalho int IDENTITY(1,1) NOT NULL,
		IDProjeto int NULL,
		Meta varchar(max) NULL,
		Etapa varchar(max) NULL,
		Atividade varchar(max) NULL,
		QuantidadePrevista float NULL,
		Unidade varchar(max) NULL,
	 CONSTRAINT PK_TBProjetoPlanoTrabalho PRIMARY KEY CLUSTERED 
	(
		IDProjetoPlanoTrabalho ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWEB') AND name IN ('RelatTecnicoModSimplificado'))
	ALTER TABLE TBConfiguracaoWEB ADD RelatTecnicoModSimplificado bit;

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjetoEquipeAcompanhamento'))
Begin
	CREATE TABLE TBProjetoEquipeAcompanhamento(
		IDProjetoEquipeAcompanhamento int IDENTITY(1,1) NOT NULL,
		IDAcompanhamentoProjeto int NOT NULL,
	    IDProjetoEquipe int NOT NULL,
		IDProjeto int NOT NULL,
		IDPessoaFisicaJuridica int NOT NULL,
		IDUsuario int NOT NULL,
		Valor float NULL,
		Conta varchar(20) NULL,
		Agencia varchar(10) NULL,
		Banco varchar(3) NULL,
		IDAtividade int NULL,
		Voluntario tinyint NULL,
		DataInicio int NULL,
		DataFim int NULL,
	 CONSTRAINT PK_TBProjetoEquipeAcompanhamento PRIMARY KEY CLUSTERED 
	(
		IDProjetoEquipeAcompanhamento ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
END

GO -- 

IF EXISTS (SELECT * FROM TBConfiguracao where AbreviaturaFundacao = 'FUSP')
Begin
	Update TBConfiguracaoWeb 
	set RelatTecnicoModSimplificado = 1
End

GO --

IF EXISTS (SELECT * FROM TBConfiguracao where AbreviaturaFundacao = 'FUSP')
Begin
	IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('IDUsuarioAssinaturaRecibo'))
	begin
		ALTER TABLE TBLancamento 
		ADD IDUsuarioAssinaturaRecibo int,
		DataAssinaturaRecibo datetime
	end
End

GO --

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE  OBJECT_ID = OBJECT_ID(N'TBMensagemSistema') AND name IN ('ID'))
BEGIN
	CREATE NONCLUSTERED INDEX ID ON TBMensagemSistema
	(
		ID ASC,
		Modulo ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
END

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjetoPlanoTrabalhoMeta'))
Begin
	CREATE TABLE TBProjetoPlanoTrabalhoMeta(
		IDProjetoPlanoTrabalhoMeta int NOT NULL,
		IDProjeto int NOT NULL,
		NomeProjetoPlanoTrabalhoMeta varchar(180) NULL,
		CONSTRAINT [PK_TBProjetoPlanoTrabalhoMeta] PRIMARY KEY CLUSTERED 
	(
		[IDProjetoPlanoTrabalhoMeta] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY];

	Alter table TBProjetoPlanoTrabalho add IDProjetoPlanoTrabalhoMeta int;

	Alter table TBProjetoPlanoTrabalhoExecucao add IDProjetoPlanoTrabalhoMeta int;

	ALTER INDEX ID ON TBMensagemSistema REORGANIZE  WITH ( LOB_COMPACTION = ON )
End

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjetoEquipeAcompanhamento'))
Begin
	CREATE TABLE TBProjetoEquipeAcompanhamento(
		IDProjetoEquipeAcompanhamento int IDENTITY(1,1) NOT NULL,
		IDAcompanhamentoProjeto int NOT NULL,
		IDProjetoEquipe int NOT NULL,
		IDProjeto int NOT NULL,
		IDPessoaFisicaJuridica int NOT NULL,
		IDUsuario int NOT NULL,
		Valor float NULL,
		Conta varchar(20) NULL,
		Agencia varchar(10) NULL,
		Banco varchar(3) NULL,
		IDAtividade int NULL,
		Voluntario tinyint NULL,
		DataInicio int NULL,
		DataFim int NULL,
	 CONSTRAINT [PK_TBProjetoEquipeAcompanhamento] PRIMARY KEY CLUSTERED 
	(
		IDProjetoEquipeAcompanhamento ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBAcompanhamentoProjeto') AND name IN ('IDResponsavelEnvio'))
	Alter table TBAcompanhamentoProjeto add IDResponsavelEnvio int;

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBModeloImportacao'))
Begin
	CREATE TABLE TBModeloImportacao(
	IDModeloImportacao int identity(1,1) NOT NULL,
	DescricaoModeloImportacao varchar(80) NULL,
	CaminhoModeloImportacao varchar(200) NULL,
	IDUsuario int NULL,
	TipoArmazenamento tinyint NULL,
	NomeArquivoModeloImportacao varchar(300) NULL,
	 CONSTRAINT [PK_TBModeloImportacao] PRIMARY KEY CLUSTERED 
	(
		IDModeloImportacao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWEB') AND name IN ('RelatExtratoProjetoModelo'))
	Alter table TBConfiguracaoWEB add RelatExtratoProjetoModelo tinyint;

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('IDContaTarifaGetNet'))
	Alter table TBProjeto add IDContaTarifaGetNet int, IDRubProjetoTarifaGetNet int;

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('TituloProjeto'))
Begin
	Alter table TBSolicitacaoImportacao add TituloProjeto varchar(250), 
											ObjetivoProjeto  varchar(350),
											Area  varchar(150),
											SubArea  varchar(150),
											InstituicaoSede  varchar(150),
											CNPJExecutora varchar(20),
											CredenciamentoCNPQ  varchar(100); 
End
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWEB') AND name IN ('ObrigatorioContratoTermoAditivo'))
Begin
	Alter table TBConfiguracaoWEB add ObrigatorioContratoTermoAditivo bit;

	Alter table TBSolicitacaoBolsa add IDContratoTermoAditivo int;

	Create index IX_IDContratoTermoAditivo on TBSolicitacaoBolsa (IDContratoTermoAditivo);
End
GO --

if not exists (select * from sysobjects where name='TBImportacaoTipoEmbarque' and xtype='U')
Begin
	
	CREATE TABLE TBImportacaoTipoEmbarque (
	IDImportacaoTipoEmbarque int IDENTITY(1,1) NOT NULL,
	TipoEmbarque varchar(150) NULL,
		 CONSTRAINT [PK_TBImportacaoTipoEmbarque] PRIMARY KEY CLUSTERED 
		(
			IDImportacaoTipoEmbarque ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]

END

GO --

if not exists (select * from sysobjects where name='TBImportacaoDestinoCarga' and xtype='U')
Begin
	
	CREATE TABLE TBImportacaoDestinoCarga (
	IDImportacaoDestinoCarga int IDENTITY(1,1) NOT NULL,
	DestinoCarga varchar(150) NULL,
		 CONSTRAINT [PK_TBImportacaoDestinoCarga] PRIMARY KEY CLUSTERED 
		(
			IDImportacaoDestinoCarga ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]

END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('IDImportacaoTipoEmbarque'))
Begin
	Alter Table TBSolicitacaoImportacao add 
		IDImportacaoTipoEmbarque int,
		IDImportacaoDestinoCarga int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('IDImportacaoBancoDebito'))
Begin
	Alter Table TBSolicitacaoImportacao add 
		IDImportacaoBancoDebito int,
		IDImportacaoAgenciaDebito int,
		IDImportacaoContaDebito int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('RBMSignURLBase'))
Begin
	ALTER TABLE TBConfiguracao1 ADD 
		RBMSignUtilizar bit,
		RBMSignURLBase varchar(80),
		RBMSignEmailLogin varchar(80),
		RBMSignSenhaLogin varchar(50),
		RBMSignTokenJWT varchar(300)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('RBMSignURLBase'))
Begin
	ALTER TABLE TBConfiguracaoWeb ADD 
		RBMSignUtilizar bit,
		RBMSignURLBase varchar(80),
		RBMSignEmailLogin varchar(80),
		RBMSignSenhaLogin varchar(50),
		RBMSignTokenJWT varchar(300)
End

GO --

Declare @trig_assinatura_digital_rbmsign_configuracao_1 varchar(max)
IF (not exists (select * from sysobjects where name='tgr_assinatura_digital_rbmsign_configuracao_1'))
Begin
	set @trig_assinatura_digital_rbmsign_configuracao_1 = '	
				CREATE TRIGGER tgr_assinatura_digital_rbmsign_configuracao_1
					ON TBConfiguracao1
					AFTER UPDATE
					AS
					BEGIN
						IF TRIGGER_NESTLEVEL() > 1
							RETURN

						DECLARE
							@IDConfiguracao int,
							@RBMSignUtilizar bit,
							@RBMSignURLBase varchar(80),
							@RBMSignEmailLogin varchar(80),
							@RBMSignSenhaLogin varchar(50),
							@RBMSignTokenJWT varchar(300)

						SELECT 
							@IDConfiguracao = IDConfiguracao,
							@RBMSignUtilizar = RBMSignUtilizar,
							@RBMSignURLBase = RBMSignURLBase,
							@RBMSignEmailLogin = RBMSignEmailLogin,
							@RBMSignSenhaLogin = RBMSignSenhaLogin,
							@RBMSignTokenJWT = RBMSignTokenJWT
						FROM
							INSERTED;

						UPDATE TBConfiguracaoWeb SET 
							RBMSignUtilizar = @RBMSignUtilizar,
							RBMSignURLBase = @RBMSignURLBase,
							RBMSignEmailLogin = @RBMSignEmailLogin,
							RBMSignSenhaLogin = @RBMSignSenhaLogin,
							RBMSignTokenJWT = @RBMSignTokenJWT
						WHERE 
							IDConfiguracaoWeb = @IDConfiguracao;
					END';
	exec(@trig_assinatura_digital_rbmsign_configuracao_1)
End
ELSE
Begin
	set @trig_assinatura_digital_rbmsign_configuracao_1 = '
				ALTER TRIGGER tgr_assinatura_digital_rbmsign_configuracao_1
					ON TBConfiguracao1
					AFTER UPDATE
					AS
					BEGIN
						IF TRIGGER_NESTLEVEL() > 1
							RETURN

						DECLARE
							@IDConfiguracao int,
							@RBMSignUtilizar bit,
							@RBMSignURLBase varchar(80),
							@RBMSignEmailLogin varchar(80),
							@RBMSignSenhaLogin varchar(50),
							@RBMSignTokenJWT varchar(300)

						SELECT 
							@IDConfiguracao = IDConfiguracao,
							@RBMSignUtilizar = RBMSignUtilizar,
							@RBMSignURLBase = RBMSignURLBase,
							@RBMSignEmailLogin = RBMSignEmailLogin,
							@RBMSignSenhaLogin = RBMSignSenhaLogin,
							@RBMSignTokenJWT = RBMSignTokenJWT
						FROM
							INSERTED;

						UPDATE TBConfiguracaoWeb SET 
							RBMSignUtilizar = @RBMSignUtilizar,
							RBMSignURLBase = @RBMSignURLBase,
							RBMSignEmailLogin = @RBMSignEmailLogin,
							RBMSignSenhaLogin = @RBMSignSenhaLogin,
							RBMSignTokenJWT = @RBMSignTokenJWT
						WHERE 
							IDConfiguracaoWeb = @IDConfiguracao;
					END';
	exec(@trig_assinatura_digital_rbmsign_configuracao_1)
End

GO --

Declare @trig_assinatura_digital_rbmsign_configuracao_web varchar(max)
IF (not exists (select * from sysobjects where name='tgr_assinatura_digital_rbmsign_configuracao_web'))
Begin
	set @trig_assinatura_digital_rbmsign_configuracao_web = '	
				CREATE TRIGGER tgr_assinatura_digital_rbmsign_configuracao_web
					ON TBConfiguracaoWeb
					AFTER UPDATE
					AS
					BEGIN
						IF TRIGGER_NESTLEVEL() > 1
							RETURN

						DECLARE
							@IDConfiguracaoWeb int,
							@RBMSignUtilizar bit,
							@RBMSignURLBase varchar(80),
							@RBMSignEmailLogin varchar(80),
							@RBMSignSenhaLogin varchar(50),
							@RBMSignTokenJWT varchar(300)

						SELECT 
							@IDConfiguracaoWeb = IDConfiguracaoWeb,
							@RBMSignUtilizar = RBMSignUtilizar,
							@RBMSignURLBase = RBMSignURLBase,
							@RBMSignEmailLogin = RBMSignEmailLogin,
							@RBMSignSenhaLogin = RBMSignSenhaLogin,
							@RBMSignTokenJWT = RBMSignTokenJWT
						FROM
							INSERTED;

						UPDATE TBConfiguracao1 SET 
							RBMSignUtilizar = @RBMSignUtilizar,
							RBMSignURLBase = @RBMSignURLBase,
							RBMSignEmailLogin = @RBMSignEmailLogin,
							RBMSignSenhaLogin = @RBMSignSenhaLogin,
							RBMSignTokenJWT = @RBMSignTokenJWT
						WHERE 
							IDConfiguracao = @IDConfiguracaoWeb;
					END';
	exec(@trig_assinatura_digital_rbmsign_configuracao_web)
End
ELSE
Begin
	set @trig_assinatura_digital_rbmsign_configuracao_web = '
				ALTER TRIGGER tgr_assinatura_digital_rbmsign_configuracao_web
					ON TBConfiguracaoWeb
					AFTER UPDATE
					AS
					BEGIN
						IF TRIGGER_NESTLEVEL() > 1
							RETURN

						DECLARE
							@IDConfiguracaoWeb int,
							@RBMSignUtilizar bit,
							@RBMSignURLBase varchar(80),
							@RBMSignEmailLogin varchar(80),
							@RBMSignSenhaLogin varchar(50),
							@RBMSignTokenJWT varchar(300)

						SELECT 
							@IDConfiguracaoWeb = IDConfiguracaoWeb,
							@RBMSignUtilizar = RBMSignUtilizar,
							@RBMSignURLBase = RBMSignURLBase,
							@RBMSignEmailLogin = RBMSignEmailLogin,
							@RBMSignSenhaLogin = RBMSignSenhaLogin,
							@RBMSignTokenJWT = RBMSignTokenJWT
						FROM
							INSERTED;

						UPDATE TBConfiguracao1 SET 
							RBMSignUtilizar = @RBMSignUtilizar,
							RBMSignURLBase = @RBMSignURLBase,
							RBMSignEmailLogin = @RBMSignEmailLogin,
							RBMSignSenhaLogin = @RBMSignSenhaLogin,
							RBMSignTokenJWT = @RBMSignTokenJWT
						WHERE 
							IDConfiguracao = @IDConfiguracaoWeb;
					END';
	exec(@trig_assinatura_digital_rbmsign_configuracao_web)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE  OBJECT_ID = OBJECT_ID(N'TBPEDIDOCOMPRA') AND name IN ('IDX_COMPRA_PEDIDO'))
BEGIN
	CREATE NONCLUSTERED INDEX IDX_COMPRA_PEDIDO ON TBPEDIDOCOMPRA (IDCompra)
	CREATE NONCLUSTERED INDEX IDX_COMPRA_LIBERACAO ON TBCOMPRALIBERACAO (IDCompra) INCLUDE (IDUSUARIOCOMPRALIBERADA)
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBAcompanhamentoProjeto') AND name IN ('DescricaoFatoresImpactoProjeto'))
Begin
	Alter table TBAcompanhamentoProjeto add DescricaoFatoresImpactoProjeto varchar(max);
	Alter table TBAcompanhamentoProjeto add DescricaoAcoesMitigacao varchar(max);
	Alter table TBAcompanhamentoProjeto add ConclusoesParciais varchar(max);
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacao') AND name IN ('TituloProjeto'))
Begin
	Alter table TBImportacao add TituloProjeto varchar(250), 
											ObjetivoProjeto  varchar(350),
											Area  varchar(150),
											SubArea  varchar(150),
											InstituicaoSede  varchar(150),
											CNPJExecutora varchar(20),
											CredenciamentoCNPQ  varchar(100); 
End
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacao') AND name IN ('IDImportacaoTipoEmbarque'))
Begin
	Alter Table TBImportacao add 
		IDImportacaoTipoEmbarque int,
		IDImportacaoDestinoCarga int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacao') AND name IN ('IDImportacaoBancoDebito'))
Begin
	Alter Table TBImportacao add 
		IDImportacaoBancoDebito int,
		IDImportacaoAgenciaDebito int,
		IDImportacaoContaDebito int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('URLImagemPerfil'))
Begin
	Alter Table TBDoacao
		Add URLImagemPerfil varchar(400)

	Alter Table TBTurma
		Add URLImagemPerfil varchar(400)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCompra') AND name IN ('IDDocumentoRBMSign'))
Begin
	ALTER TABLE TBSolicitacaoAdiantamento ADD IDDocumentoRBMSign int;
	ALTER TABLE TBSolicitacaoBolsa ADD IDDocumentoRBMSign int;
	ALTER TABLE TBSolicitacaoCompra ADD IDDocumentoRBMSign int;
	ALTER TABLE TBSolicitacaoReembolso ADD IDDocumentoRBMSign int;
	ALTER TABLE TBSolicitacaoReembolsoTerceiro ADD IDDocumentoRBMSign int;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('IDDocumentoRBMSign'))
Begin
	ALTER TABLE TBSolicitacao ADD IDDocumentoRBMSign int;
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoRotuloMonitor' and xtype='U')
Begin
	
	CREATE TABLE TBImportacaoRotuloMonitor (
	IDImportacaoRotuloMonitor int IDENTITY(1,1) NOT NULL,
	ImportacaoRotuloMonitor varchar(150) NULL,
	Posicao int NULL,
	Monitor bit NULL,
		 CONSTRAINT [PK_TBImportacaoRotuloMonitor] PRIMARY KEY CLUSTERED 
		(
			IDImportacaoRotuloMonitor ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
		) ON [PRIMARY]

END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolPagamentoMenosRestricaoEstrangeiros'))
Begin
	Alter table TBConfiguracaoWeb add SolPagamentoMenosRestricaoEstrangeiros bit
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoRotuloMonitorExecucao' and xtype='U')
Begin
	CREATE TABLE TBImportacaoRotuloMonitorExecucao (
	IDImportacaoRotuloMonitorExecucao int IDENTITY(1,1) NOT NULL,
	IDImportacaoRotuloMonitor int NULL,
	IDImportacao int NULL,
	IDUsuario int NULL,
	DataHora datetime NULL default GETDATE(),
	CONSTRAINT [PK_TBImportacaoRotuloMonitorExecucao] PRIMARY KEY CLUSTERED 
	(
		IDImportacaoRotuloMonitorExecucao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE TBImportacaoRotuloMonitorExecucao  WITH CHECK ADD  CONSTRAINT FK_TBImportacaoRotuloMonitor_TBImportacaoRotulo FOREIGN KEY(IDImportacaoRotuloMonitor)
	REFERENCES TBImportacaoRotuloMonitor (IDImportacaoRotuloMonitor)

	ALTER TABLE TBImportacaoRotuloMonitorExecucao CHECK CONSTRAINT FK_TBImportacaoRotuloMonitor_TBImportacaoRotulo
END

GO --

IF EXISTS (SELECT C.name, t.name FROM sys.columns C left join sys.types t on C.system_type_id = t.system_type_id WHERE  OBJECT_ID = OBJECT_ID(N'TBAcompanhamentoProjeto') AND C.name IN ('DataEnvio')  AND T.name IN ('date'))
Begin
	Alter table TBAcompanhamentoProjeto alter column DataEnvio datetime
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBAcompanhamentoProjeto') AND name IN ('Reprovado'))
Begin
	Alter table TBAcompanhamentoProjeto add Reprovado bit
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInfoBancaria') AND name IN ('IDBanco'))
Begin
	ALTER TABLE TBInfoBancaria DROP COLUMN IDBanco;
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInfoBancaria') AND name IN ('Agencia'))
Begin
	ALTER TABLE TBInfoBancaria DROP COLUMN Agencia;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebInfoBancaria') AND name IN ('Agencia'))
Begin
	ALTER TABLE TBWebInfoBancaria add IDBanco int;
	ALTER TABLE TBWebInfoBancaria add Agencia varchar(10);
End

GO --

IF EXISTS (SELECT * FROM sysconstraints WHERE object_name(id) = 'TBWebInfoBancaria' and object_name(constid) = 'PK_TBWebInfoBancaria')
Begin
	ALTER TABLE TBWebInfoBancaria DROP CONSTRAINT PK_TBWebInfoBancaria;

	ALTER TABLE TBWebInfoBancaria ALTER COLUMN IDAgencia int NULL;
End

GO --

IF NOT EXISTS (SELECT * FROM sysconstraints WHERE object_name(id) = 'TBWebInfoBancaria' and object_name(constid) = 'PK_TBWebInfoBancaria2')
Begin
	ALTER TABLE TBWebInfoBancaria
	ADD CONSTRAINT PK_TBWebInfoBancaria2 PRIMARY KEY CLUSTERED 
	(
		IDInfoBancaria ASC,
		IDPessoaFisicaJuridica ASC
	) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('InformarPendenciaSolCompra'))
	alter table TBConfiguracaoWeb add InformarPendenciaSolCompra bit;
GO --

if not exists (select * from sysobjects where name='TBChatSistema' and xtype='U')
Begin
	CREATE TABLE TBChatSistema (
		IDChatSistema int IDENTITY(1,1) NOT NULL,
		Descricao varchar(200),
		Tipo int NULL,
	CONSTRAINT [PK_TBChatSistema] PRIMARY KEY CLUSTERED 
	(
		IDChatSistema ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
END

GO --

if not exists (select * from sysobjects where name='TBChatChannel' and xtype='U')
Begin
	CREATE TABLE TBChatChannel (
		IDChatChannel int IDENTITY(1,1) NOT NULL,
		IDChatSistema int NULL,
		Descricao varchar(200),
	CONSTRAINT [PK_TBChatChannel] PRIMARY KEY CLUSTERED 
	(
		IDChatChannel ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
END

GO --

if not exists (select * from sysobjects where name='TBChatChannelUsers' and xtype='U')
Begin
	CREATE TABLE TBChatChannelUsers (
		IDChatChannelUsers int IDENTITY(1,1) NOT NULL,
		IDChatChannel int NULL,
		IDUser int NULL
	CONSTRAINT [PK_TBChatChannelUsers] PRIMARY KEY CLUSTERED 
	(
		IDChatChannelUsers ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	Insert into TBChatSistema
	(Descricao, Tipo)
	Values
	('Chat',1),
	('Solicitação de Adiantamento',2),
	('Solicitação de Bolsa, RPA ou Estágio',3),
	('Solicitação de Compra',4),
	('Solicitação de Curso, Evento, Doação',5),
	('Solicitação de Faturamento',6),
	('Solicitação de Importação',7),
	('Solicitação de Pagamento',8),
	('Solicitação de Passagens, Diárias e Adiantamento',9),
	('Solicitação de Reembolso Terceiro',10),
	('Solicitação de Transferência',11),
	('Solicitação de cadastro de fornecedor',12),
	('Grupo',13)
END

GO --

if not exists (select * from sysobjects where name='TBAquisicoesAcompanhamento' and xtype='U')
Begin
	CREATE TABLE TBAquisicoesAcompanhamento(
		IDAquisicoesAcompanhamento int IDENTITY(1,1) NOT NULL,
		IDAcompanhamentoProjeto int NOT NULL, 
		Quantidade float NULL, 
		Produto varchar(Max) NULL, 
		Complemento varchar(Max) NULL,
	CONSTRAINT [PK_TBAquisicoesAcompanhamento] PRIMARY KEY CLUSTERED 
	( 
		IDAquisicoesAcompanhamento ASC 
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY];
END

GO --

if not exists (select * from sysobjects where name='TBChatHistorico' and xtype='U')
Begin
	CREATE TABLE TBChatHistorico (
		IDChatHistorico int IDENTITY(1,1) NOT NULL,
		Descricao varchar(400) NULL,
		Usuario varchar(50) NULL,
		DataHora datetime NULL default GETDATE(),
		Mensagem varchar(max) NULL
	CONSTRAINT [PK_TBChatHistorico] PRIMARY KEY CLUSTERED 
	(
		IDChatHistorico ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
END

GO --

if not exists (select * from sysobjects where name='TBChatHistoricoUsuario' and xtype='U')
Begin
	CREATE TABLE TBChatHistoricoUsuario (
		IDChatHistoricoUsuario int IDENTITY(1,1) NOT NULL,
		IDChatHistorico int NULL,
		IDUsuario int NULL
	CONSTRAINT [PK_TBChatHistoricoUsuario] PRIMARY KEY CLUSTERED 
	(
		IDChatHistoricoUsuario ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
END

GO --

IF not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBChatHistoricoUsuario') AND name IN ('DataHoraLeitura'))
Begin
	ALTER TABLE TBChatHistoricoUsuario Add DataHoraLeitura Datetime NULL;
End

GO --

IF not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('PertenceQuadro'))
Begin
	ALTER TABLE TBSolicitacaoBolsa Add 
		PertenceQuadro bit NULL,
		TipoQuadro smallint NULL;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('UtilizarChatInterno'))
Begin
	Alter table TBConfiguracaoWeb add UtilizarChatInterno bit

	Declare @funct varchar(max)

	set @funct = '
		Alter FUNCTION RetornaProjetoContrato (    @IDContrato int  )   
		RETURNS varchar(max)   
		AS   
		BEGIN     
			DECLARE @Projetos as varchar(max)    
			Declare @AuxNomeProjeto as varchar(max)    
	
			set @Projetos = ''''  
			DECLARE CURSOR_OBJECTS_SolProjetos 
			CURSOR FOR   
				Select dbo.RetornaNomeProjeto(P.IDProjeto) as NomeProjeto
				From TBProjetoContrato PC with(nolock) 
				Inner Join TBProjeto P with(nolock) 
				on (PC.IDProjeto = P.IDProjeto)
				where PC.IDContrato = @IDContrato   
	
			OPEN CURSOR_OBJECTS_SolProjetos     
	
			FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			WHILE @@FETCH_STATUS = 0     
			BEGIN      
				IF (len(@Projetos) = 0)        
				Set @Projetos = @AuxNomeProjeto;      
				Else        
				Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
				FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			END     
	
			CLOSE CURSOR_OBJECTS_SolProjetos     
			DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
			RETURN coalesce(@Projetos,'''') 
		END'

	exec(@funct)

	Declare @func_configuracao_1 varchar(max)

	set @func_configuracao_1 = '
				Alter FUNCTION RetornaProjetoSolCursoEventoServico (    @IDSol int  )   
					RETURNS varchar(max)   
					AS   
					BEGIN     
						DECLARE @Projetos as varchar(max)    
						Declare @AuxNomeProjeto as varchar(max)    
	
						set @Projetos = ''''  
						DECLARE CURSOR_OBJECTS_SolProjetos 
						CURSOR FOR   
							Select dbo.RetornaNomeProjeto(P.IDProjeto) as NomeProjeto
							From TBSolicitacaoCursoEventoServicoProjeto SR with(nolock) 
							Inner Join TBProjeto P with(nolock) 
							on (SR.IDProjeto = P.IDProjeto)
							where SR.IDSolicitacaoCursoEventoServico = @IDSol   
	
						OPEN CURSOR_OBJECTS_SolProjetos     
	
						FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
						WHILE @@FETCH_STATUS = 0     
						BEGIN      
							IF (len(@Projetos) = 0)        
							Set @Projetos = @AuxNomeProjeto;      
							Else        
							Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
							FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
						END     
	
						CLOSE CURSOR_OBJECTS_SolProjetos     
						DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
						RETURN coalesce(@Projetos,'''') 
					END';
	exec(@func_configuracao_1)

	Declare @func_configuracao_2 varchar(max)

	set @func_configuracao_2 = '
				Alter FUNCTION RetornaProjetoDoacao (    @IDDoacao int  )   
					RETURNS varchar(max)   
					AS   
					BEGIN     
						DECLARE @Projetos as varchar(max)    
						Declare @AuxNomeProjeto as varchar(max)    
	
						set @Projetos = ''''  
						DECLARE CURSOR_OBJECTS_Projetos 
						CURSOR FOR   
							Select dbo.RetornaNomeProjeto(P.IDProjeto) as NomeProjeto
							From TBDoacaoProjeto DP with(nolock) 
							Inner Join TBProjeto P with(nolock) 
							on (DP.IDProjeto = P.IDProjeto)
							where DP.IDDoacao = @IDDoacao   
	
						OPEN CURSOR_OBJECTS_Projetos     
	
						FETCH NEXT FROM CURSOR_OBJECTS_Projetos INTO @AuxNomeProjeto     
						WHILE @@FETCH_STATUS = 0     
						BEGIN      
							IF (len(@Projetos) = 0)        
							Set @Projetos = @AuxNomeProjeto;      
							Else        
							Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
							FETCH NEXT FROM CURSOR_OBJECTS_Projetos INTO @AuxNomeProjeto     
						END     
	
						CLOSE CURSOR_OBJECTS_Projetos     
						DEALLOCATE CURSOR_OBJECTS_Projetos     
	
						RETURN coalesce(@Projetos,'''') 
					END';
	exec(@func_configuracao_2)

	Declare @func_configuracao_3 varchar(max)

	set @func_configuracao_3 = '
				Alter FUNCTION RetornaProjetoSolPagamentoPFPJ (    @IDSol int  )   
					RETURNS varchar(max)   
					AS   
					BEGIN     
						DECLARE @Projetos as varchar(max)    
						Declare @AuxNomeProjeto as varchar(max)    
	
						set @Projetos = ''''  
						DECLARE CURSOR_OBJECTS_SolProjetos 
						CURSOR FOR   
							Select dbo.RetornaNomeProjeto(P.IDProjeto) as NomeProjeto
							From TBSolicitacaoRateio SR with(nolock) 
							Inner Join TBProjeto P with(nolock) 
							on (SR.IDProjeto = P.IDProjeto)
							where SR.IDSolicitacao = @IDSol   
	
						OPEN CURSOR_OBJECTS_SolProjetos     
	
						FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
						WHILE @@FETCH_STATUS = 0     
						BEGIN      
							IF (len(@Projetos) = 0)        
							Set @Projetos = @AuxNomeProjeto;      
							Else        
							Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
							FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
						END     
	
						CLOSE CURSOR_OBJECTS_SolProjetos     
						DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
						RETURN coalesce(@Projetos,'''') 
					END';
	exec(@func_configuracao_3)

	Declare @func_configuracao_4 varchar(max)

	set @func_configuracao_4 = '
		Alter FUNCTION RetornaProjetoSolPgtoRateio (    @IDSol int  )   
		RETURNS varchar(max)   
		AS   
		BEGIN     
			DECLARE @Projetos as varchar(max)    
			Declare @AuxNomeProjeto as varchar(max)    
	
			set @Projetos = ''''  
			DECLARE CURSOR_OBJECTS_SolProjetos 
			CURSOR FOR   
				Select dbo.RetornaNomeProjeto(P.IDProjeto) as NomeProjeto
				From TBSolicitacaoHasSolicitacao SHS with(nolock) 
				inner join TBSolicitacaoRateio SR with(nolock) 
				on (SHS.IDSolicitacao = Sr.IDSolicitacao)
				Inner Join TBProjeto P with(nolock) 
				on (SR.IDProjeto = P.IDProjeto)
				where SHS.IDSolicitacaoHasSolicitacao = @IDSol   
	
			OPEN CURSOR_OBJECTS_SolProjetos     
	
			FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			WHILE @@FETCH_STATUS = 0     
			BEGIN      
				IF (len(@Projetos) = 0)        
				Set @Projetos = @AuxNomeProjeto;      
				Else        
				Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
				FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			END     
	
			CLOSE CURSOR_OBJECTS_SolProjetos     
			DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
			RETURN coalesce(@Projetos,'''') 
		END'

	exec(@func_configuracao_4)

	Declare @func_configuracao_5 varchar(max)

	set @func_configuracao_5 = '
		Alter FUNCTION RetornaProjetoSolTransfCredito (    @IDSol int  )   
		RETURNS varchar(max)   
		AS   
		BEGIN     
			DECLARE @Projetos as varchar(max)    
			Declare @AuxNomeProjeto as varchar(max)    
	
			set @Projetos = ''''  
			DECLARE CURSOR_OBJECTS_SolProjetos 
			CURSOR FOR   
				Select 
					distinct(dbo.RetornaNomeProjeto(P.IDProjeto)) as NomeProjeto
				from TBSolTransRateioCredito SRC with(nolock) 
				inner join TBProjeto P with(nolock) 
				on SRC.IDProjetoCredito = P.IDProjeto
				where SRC.IDSolicitacaoTransferencia = @IDSol 
	
			OPEN CURSOR_OBJECTS_SolProjetos     
	
			FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			WHILE @@FETCH_STATUS = 0     
			BEGIN      
				IF (len(@Projetos) = 0)        
				Set @Projetos = @AuxNomeProjeto;      
				Else        
				Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
				FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			END     
	
			CLOSE CURSOR_OBJECTS_SolProjetos     
			DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
			RETURN coalesce(@Projetos,'''') 
		END'

	exec(@func_configuracao_5)

	Declare @func_configuracao_6 varchar(max)

	set @func_configuracao_6 = '
		Alter FUNCTION RetornaProjetoSolTransfDebito (    @IDSol int  )   
		RETURNS varchar(max)   
		AS   
		BEGIN     
			DECLARE @Projetos as varchar(max)    
			Declare @AuxNomeProjeto as varchar(max)    
	
			set @Projetos = ''''  
			DECLARE CURSOR_OBJECTS_SolProjetos 
			CURSOR FOR   
				Select 
					distinct(dbo.RetornaNomeProjeto(P.IDProjeto)) as NomeProjeto
				from TBSolTransRateioDebito SRD with(nolock) 
				inner join TBProjeto P with(nolock) 
				on SRD.IDProjetoDebito = P.IDProjeto
				where SRD.IDSolicitacaoTransferencia = @IDSol 
	
			OPEN CURSOR_OBJECTS_SolProjetos     
	
			FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			WHILE @@FETCH_STATUS = 0     
			BEGIN      
				IF (len(@Projetos) = 0)        
				Set @Projetos = @AuxNomeProjeto;      
				Else        
				Set @Projetos = @Projetos + '', '' + @AuxNomeProjeto;       
			
				FETCH NEXT FROM CURSOR_OBJECTS_SolProjetos INTO @AuxNomeProjeto     
			END     
	
			CLOSE CURSOR_OBJECTS_SolProjetos     
			DEALLOCATE CURSOR_OBJECTS_SolProjetos     
	
			RETURN coalesce(@Projetos,'''') 
		END'

	exec(@func_configuracao_6)
END

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBChatChannel') AND name IN ('Descricao'))
Begin
	ALTER TABLE TBChatChannel Drop column Descricao;
	ALTER TABLE TBChatChannel Add Titulo varchar(100);
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBChatHistorico') AND name IN ('Descricao'))
Begin
	ALTER TABLE TBChatHistorico Drop column Descricao;
	ALTER TABLE TBChatHistorico Add IDChatChannel int;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolDiariaEstrangeiro'))
Begin
	Alter table TBConfiguracaoWeb add SolDiariaEstrangeiro bit
	Alter table TBConfiguracaoWeb add InstrucoesSolDiariaEstrangeiro varchar(max)
	Alter table TBConfiguracaoWeb add SolDiariaEstrangeiroMsgConfirmacao varchar(max)
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ChatInterno'))
Begin
	Alter table TBConfiguracaoWeb add ChatInterno bit
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolso') AND name IN ('Estrangeiro'))
Begin
	Alter table TBSolicitacaoReembolso add Estrangeiro bit
END

GO --

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE  name = 'IX_IDChatChannel_TBChatHistorico')
Begin
	CREATE NONCLUSTERED INDEX IX_IDChatChannel_TBChatHistorico   
		ON TBChatHistorico (IDChatChannel);
		
	Update TBSolicitacaoReembolso
	set Estrangeiro = 0
	where Estrangeiro is null
END

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolso') AND name IN ('Pais'))
Begin
	Alter table TBSolicitacaoReembolso add Pais varchar(30)
	Alter table TBSolicitacaoReembolso alter column DocumentoTerceiro varchar(100)
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjetoEquipeAcompanhamento') AND name IN ('IDVinculo'))
Begin
	--ALTER TABLE TBProjetoEquipeAcompanhamento Drop column IDAtividade;
	ALTER TABLE TBProjetoEquipeAcompanhamento Add IDVinculo int;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('InstrucoesLiberacaoSolAdiantamento'))
Begin
	Alter table TBConfiguracaoWeb add InstrucoesLiberacaoSolAdiantamento varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesLiberacaoSolBolsa varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesLiberacaoSolCompra varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesLiberacaoSolImportacao varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesLiberacaoSolReembolso varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesLiberacaoSolFaturamento varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesLiberacaoSolPagamentoPF varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesLiberacaoSolPagamentoPJ varchar(max)
	Alter table TBConfiguracaoWeb add InstrucoesLiberacaoSolReembolsoTerceiro varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('CursoDoContratado'))
Begin
	Alter table TBSolicitacaoBolsa add CursoDoContratado varchar(200)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBChatChannel') AND name IN ('IDUsuarioCriacao'))
Begin
	Alter table TBChatChannel add IDUsuarioCriacao int
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('TipoQuadroSolBolsaObrigatorio'))
Begin
	Alter table TBConfiguracaoWeb add TipoQuadroSolBolsaObrigatorio bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SenhaCertificadoDigital'))
Begin
	Alter table TBConfiguracaoWeb add SenhaCertificadoDigital varchar(200)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBAcompanhamentoProjeto') AND name IN ('Lei95894Justificativa'))
Begin
	Alter table TBAcompanhamentoProjeto add 
		Lei95894Justificativa varchar(max),
		Lei95894 bit,
		ProjetoAndamentoJustificativa varchar(max),
		ProjetoAndamento bit	
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBBanco ') AND name IN ('DisponibilizarWEB'))
Begin
	ALTER TABLE TBBanco ADD DisponibilizarWEB bit DEFAULT 1;
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBBanco ') AND name IN ('DisponibilizarWEB'))
Begin
	UPDATE TBBanco SET DisponibilizarWEB = 1 WHERE DisponibilizarWEB IS NULL;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('SolCancelamentoContrato'))
Begin
	alter table TBRemotoProjetoPerfil add SolCancelamentoContrato bit;
	alter table TBRemotoProjeto add SolCancelamentoContrato bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('LabelManuais'))
Begin
	Alter table TBConfiguracaoWeb add 
		LabelManuais varchar(max),
		URLManuais varchar(max)
END
GO --

if not exists (select * from sysobjects where name='TBSolicitacaoCancelamentoContrato' and xtype='U')
Begin
	
	CREATE TABLE TBSolicitacaoCancelamentoContrato(
		IDSolicitacaoCancelamentoContrato int IDENTITY(1,1) NOT NULL,
		IDContrato int NULL,
		IDSolicitante int NULL,
		DataSolicitacao datetime NULL,
		NomeSolicitante varchar(100) NULL,
		EmailSolicitante varchar(300) NULL,
		TelefoneSolicitante varchar(20) NULL,
	 CONSTRAINT [PK_TBSolicitacaoCancelamentoContrato] PRIMARY KEY CLUSTERED 
	(
		IDSolicitacaoCancelamentoContrato ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 

END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCancelamentoContrato') AND name IN ('DataCancelamento'))
Begin
	alter table TBSolicitacaoCancelamentoContrato 
		add DataCancelamento datetime NULL,
		MotivoCancelamento varchar(max) NULL;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('IDContaPagamentoPix'))
	Alter table TBProjeto add IDContaPagamentoPix int;

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCancelamentoContrato') AND name IN ('IDProjeto'))
	Alter table TBSolicitacaoCancelamentoContrato add IDProjeto int null,
	LiberacaoCoordenador int null,
	IdLiberadoCoordenador int null,
	StatusSolicitacao int null;

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBChatChannelUsers') AND name IN ('Confirmado'))
	Alter table TBChatChannelUsers add Confirmado bit null;

GO --

if not exists (select * from sysobjects where name='TBLoginUsuarioChat' and xtype='U')
CREATE TABLE TBLoginUsuarioChat(
	IDLoginUsuarioChat int IDENTITY(1,1) NOT NULL,
	IDPessoaFisicaJuridica int NULL,
	Token varchar(max) NULL,
 CONSTRAINT [pk_IDLoginUsuarioChat] PRIMARY KEY CLUSTERED 
(
	IDLoginUsuarioChat ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY];

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBUsuarioNotificacao') AND name IN ('SolImportacaoMsg'))
Begin
	Alter Table TBUsuarioNotificacao add 
		SolImportacaoMsg bit
End

GO --

IF EXISTS (SELECT C.name, t.name FROM sys.columns C left join sys.types t on C.system_type_id = t.system_type_id WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCancelamentoContrato') AND C.name IN ('LiberacaoCoordenador')  AND T.name IN ('int'))
Begin
	Alter table TBSolicitacaoCancelamentoContrato alter column LiberacaoCoordenador float null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('PastaBaseSolicitacaoCancelamentoContrato'))
Begin
	Alter table TBConfiguracaoWeb add 
		PastaBaseSolicitacaoCancelamentoContrato varchar(200),
		PastaBaseSolicitacaoCancelamentoContrato_Azure varchar(200),
		PastaBaseSolicitacaoCancelamentoContrato_AmazonS3 varchar(200)
END
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCancelamentoContrato') AND name IN ('IDUsuarioLibCanc'))
Begin
	Alter Table TBSolicitacaoCancelamentoContrato add 
		IDUsuarioLibCanc int,
		DataUsuarioLibCanc Datetime
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBUsuario') AND name IN ('SenhaCrypt'))
Begin
	Alter Table TBUsuario add 
		SenhaCrypt varchar(max) null

	Alter Table TBConfiguracao1 add 
		UsarSenhaCrypt bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ClientIDItau'))
Begin
	Alter table TBConfiguracaoWeb add 
		ClientIDItau varchar(max),
		ClientSecretItau varchar(max),
		ItauKey varchar(max)
End

GO --

if exists (select * from sysobjects where name='UFPR_TBCobranca' and xtype='U')
Begin
	DROP TABLE UFPR_TBCobranca
	DROP TABLE UFPR_TBPessoa
	DROP TABLE UFPR_TBTURMA
	DROP TABLE UFPR_TBAluno
End

GO --

if not exists (select * from sysobjects where name='UFPR_TBCURSO' and xtype='U')
Begin	
	CREATE TABLE UFPR_TBCURSO(
		IDExterno varchar(40) NOT NULL,
		Descricao varchar(80) NOT NULL,
		IDCursoManager int Null,
		DataCriacao Datetime default GetDate(),
		CONSTRAINT [PK_UFPR_TBCURSO] PRIMARY KEY CLUSTERED 
	(
		IDExterno ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
END

GO --

if not exists (select * from sysobjects where name='UFPR_TBAluno' and xtype='U')
Begin	
	CREATE TABLE UFPR_TBAluno(
		IDExternoTurma varchar(40) not NULL,
		NroDocumento varchar(14) not NULL,
		ValorTotal float null,
		DataInicioContrato datetime null,
		DataFimContrato datetime null,
		NumeroParcelas int null,
		DataCriacaoContrato datetime null,
		DiaVencimento int null,
		DataAtualizacaoContrato datetime null,
		ResponsavelAtualizacao varchar(6) null,
		Observacao varchar(max) null,
		SituacaoContrato varchar(1) null,
		ResponsavelExclusao varchar(6) null,
		Multa float null,
		TipoMulta varchar(1) null,
		Juros float null,
		Estrangeiro varchar(1) null,
		RazaoSocial varchar(60) null,
		NomeFantasia varchar(60) null,
		Endereco varchar(100) null,
		Bairro varchar(30) null,
		UF varchar(2) null,
		Cep varchar(8) null,
		Municipio varchar(5) null,
		NomeMunicipio varchar(40) null, 
		Telefone varchar(20) null, 
		Email varchar(300) null, 
		Nascimento Datetime null,
		Pais varchar(60) null,
		IDAlunoManager int null,
		DataCriacao Datetime default GetDate(),
		CONSTRAINT [PK_UFPR_TBAluno] PRIMARY KEY CLUSTERED 
		(
		IDExternoTurma ASC,
		NroDocumento ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
END

GO --

if not exists (select * from sysobjects where name='UFPR_TBTURMA' and xtype='U')
Begin	
	CREATE TABLE UFPR_TBTURMA(
		IDExterno varchar(40) NOT NULL,
		Descricao varchar(80) NOT NULL,
		IDExternoCurso varchar(40) NOT NULL,
		QtdMensal int null,
		DataInicio Date null,
		DataFim Date null,
		DiaVencimento int null,
		ValorMensalidade float null,	
		IDTurmaManager int Null,
		ValorInscricao float null,
		ValorMatricula float null,
		DataCriacao Datetime default GetDate(),
		CONSTRAINT [PK_UFPR_TBTURMA] PRIMARY KEY CLUSTERED 
	(
		IDExterno ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
END

GO --

IF EXISTS (SELECT C.name, C.* FROM sys.columns C 
WHERE  
	OBJECT_ID = OBJECT_ID(N'TBTurma') 
	AND C.name IN ('CodigoTurma')  
	AND C.max_length = 20)
Begin
	Alter table TBTurma alter column CodigoTurma varchar(40) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('IDLancamentoInscricao'))
Begin
	Alter table UFPR_TBAluno add 
		IDLancamentoInscricao int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('Matricular'))
Begin
	Alter table UFPR_TBAluno add 
		Matricular bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('Numero'))
Begin
	Alter table UFPR_TBAluno add 
		Numero varchar(10) null,
		Complemento varchar(80) null
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ItauKey'))
Begin
	Alter Table TBConfiguracaoWeb drop 
		column ItauKey
End

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTipoBoleto') AND name IN ('EstacaoSantander'))
Begin
	Alter table TBTipoBoleto add 
		EstacaoSantander varchar(8)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ClientIDBB'))
Begin
	Alter table TBConfiguracaoWeb add 
		ClientIDBB varchar(max),
		ClientSecretBB varchar(max),
		DeveloperApplicationKey varchar(max)

	IF EXISTS (SELECT * FROM TBConfiguracao where AbreviaturaFundacao = 'FUSP')
	Begin
		Update TBTipoBoleto
		set EstacaoSantander = case coalesce(NumeroConvenio,'') when '3909280' then 'VT8K' else 'VT8E' end
		Where IDBoleto in
		(
		Select IDBoleto 
		from TBTipoBoleto
		Where IDConta in 
		(Select IDConta
		from TBConta C 
		left join TBAgencia A 
		on A.IDAgencia = C.IDAgencia
		left join TBBanco B
		on B.IDBanco = A.IDBanco
		Where B.NumeroBanco = '033')
		)
	End
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('PixTxID'))
Begin
	Alter table TBLancamento add 
		PixTxID varchar(max) null,
		PixLink varchar(max) null,
		PixQrCode varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolCompraOcultarObservacoes'))
Begin
	Alter table TBConfiguracaoWeb add SolCompraOcultarObservacoes bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('IncluirAReceberCalculoSaldo'))
Begin
	Alter table TBConfiguracaoWeb add IncluirAReceberCalculoSaldo bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoAdiantamento') AND name IN ('TipoPix'))
Begin
	Alter Table TBSolicitacaoAdiantamento
		add TipoPix tinyint null,
		ChavePix varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolso') AND name IN ('InstituicaoVisitada'))
Begin
	ALTER TABLE TBSolicitacaoReembolso ADD
		InstituicaoVisitada varchar(max) null,
		LocalVisitado varchar(max) null,
		CidadeVisitada varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('TipoPix'))
Begin
	Alter Table TBSolicitacaoBolsa
		add TipoPix tinyint null,
		ChavePix varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolso') AND name IN ('TipoPix'))
Begin
	Alter Table TBSolicitacaoReembolso
		add TipoPix tinyint null,
		ChavePix varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('TipoPix'))
Begin
	Alter Table TBSolicitacao
		add TipoPix tinyint null,
		ChavePix varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolsoTerceiro') AND name IN ('TipoPix'))
Begin
	Alter Table TBSolicitacaoReembolsoTerceiro
		add TipoPix tinyint null,
		ChavePix varchar(max) null
	
	Alter Table TBSolicitacaoReembolsoTerceiro
		Alter column Agencia varchar(20) null

	Alter Table TBSolicitacaoReembolsoTerceiro
		Alter column  Banco varchar(20) null

	Alter Table TBSolicitacaoReembolsoTerceiro
		Alter column  Conta varchar(20) null

	Alter Table TBSolicitacaoReembolsoTerceiro
		Alter column  TipoConta int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolAdiantamentoTipoContaPix'))
Begin
	Alter Table TBConfiguracaoWeb
		add SolAdiantamentoTipoContaPix bit null default 0,
		SolBolsaTipoContaPix bit null default 0,
		SolPgtoPFTipoContaPix bit null default 0,
		SolPgtoPJTipoContaPix bit null default 0,
		SolReembDiariaTipoContaPix bit null default 0,
		SolReembolsoTerceiroTipoContaPix bit null default 0
End

GO --

if exists (select * from sysobjects where name='TBAuxLancamento' and xtype='U')
Begin
	IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBAuxLancamento') AND name IN ('Descricao'))
		ALTER TABLE TBAuxLancamento add Descricao varchar(max);
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoServico') AND name IN ('Instituicao'))
Begin
	ALTER TABLE TBSolicitacaoCursoEventoServico ADD
		Instituicao varchar(max) null,
		Unidade varchar(max) null,
		Departamento varchar(max) null,
		SiteCursoEvento varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEvento') AND name IN ('AreaConhecimento'))
Begin
	ALTER TABLE TBSolicitacaoCursoEvento ADD
		AreaConhecimento varchar(max) null,
		PreRequisito varchar(max) null,
		PublicoAlvo varchar(max) null,
		FormaPagamento varchar(max) null,
		DevolucaoInscricao varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoServico') AND name IN ('IDDocumentoRBMSign'))
Begin
	ALTER TABLE TBSolicitacaoCursoEventoServico ADD IDDocumentoRBMSign int;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemSolicitacaoCompra') AND name IN ('NaoSeAplica'))
Begin
	alter table TBItemSolicitacaoCompra ADD NaoSeAplica bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolCompraObrigatorioCuidadoEspecial'))
Begin
	Alter table TBConfiguracaoWeb add SolCompraObrigatorioCuidadoEspecial bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolso') AND name IN ('EnderecoIp'))
Begin
	ALTER TABLE TBSolicitacaoReembolso ADD EnderecoIp varchar(60) null
End

GO --

if not exists (select * from sysobjects where name='TBSolicitacaoCursoEventoFaixaValores' and xtype='U')
Begin
	CREATE TABLE TBSolicitacaoCursoEventoFaixaValores(
		IDSolicitacaoCursoEventoFaixaValores int IDENTITY(1,1) NOT NULL,
		IDSolicitacaoCursoEvento int NULL,
		Descricao varchar(max) NULL,
		Valor float NULL,
		AnexoObrigatorio bit NULL,
		TextoAnexo varchar(max) NULL,
	 CONSTRAINT [PK_TBSolicitacaoCursoEventoFaixaValores] PRIMARY KEY CLUSTERED 
	(
		IDSolicitacaoCursoEventoFaixaValores ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE TBSolicitacaoCursoEventoFaixaValores  
	WITH CHECK ADD  CONSTRAINT FK_TBSolicitacaoCursoEventoFaixaValores_TBSolicitacaoCursoEvento FOREIGN KEY(IDSolicitacaoCursoEvento)
	REFERENCES TBSolicitacaoCursoEvento (IDSolicitacaoCursoEvento)

	ALTER TABLE TBSolicitacaoCursoEventoFaixaValores CHECK CONSTRAINT FK_TBSolicitacaoCursoEventoFaixaValores_TBSolicitacaoCursoEvento
End

GO --

IF NOT EXISTS (SELECT * FROM TBSolicitacaoCursoEventoFaixaValores)
Begin
	Insert into TBSolicitacaoCursoEventoFaixaValores
	(IDSolicitacaoCursoEvento, Descricao, Valor, AnexoObrigatorio, TextoAnexo)
	Select 
		IDSolicitacaoCursoEvento,
		'Inscrição',
		coalesce(ValorInscricao,0),
		0,
		''
	from TBSolicitacaoCursoEvento
End

GO --

if not exists (select * from sysobjects where name='TBTurmaFaixaValores' and xtype='U')
Begin
	CREATE TABLE TBTurmaFaixaValores(
		IDTurmaFaixaValores int IDENTITY(1,1) NOT NULL,
		IDTurma int NULL,
		Descricao varchar(max) NULL,
		Valor float NULL,
		AnexoObrigatorio bit NULL,
		TextoAnexo varchar(max) NULL,
	 CONSTRAINT [PK_TBTurmaFaixaValores] PRIMARY KEY CLUSTERED 
	(
		IDTurmaFaixaValores ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE TBTurmaFaixaValores  
	WITH CHECK ADD  CONSTRAINT FK_TBTurmaFaixaValores_TBTurma FOREIGN KEY(IDTurma)
	REFERENCES TBTurma (IDTurma)

	ALTER TABLE TBTurmaFaixaValores CHECK CONSTRAINT FK_TBTurmaFaixaValores_TBTurma
End

GO --


IF NOT EXISTS (SELECT * FROM TBTurmaFaixaValores)
Begin
	Insert into TBTurmaFaixaValores
	(IDTurma, Descricao, Valor, AnexoObrigatorio, TextoAnexo)
	Select 
		IDTurma,
		'Inscrição',
		coalesce(ValorInscricao,0),
		0,
		''
	from TBTurma
	where IDTurma in (Select IDTurma From TBSolicitacaoCursoEventoServico Where coalesce(IDTurma,0)  <> 0)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE OBJECT_ID = OBJECT_ID(N'TBFornecedorResumoAceite'))
	AND NOT EXISTS (SELECT * FROM sys.columns WHERE OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridicaResumoAceite'))
Begin
	CREATE TABLE TBFornecedorResumoAceite(
		IDFornecedorResumoAceite int IDENTITY(1,1) NOT NULL,
		nomePessoaAceite varchar(max) NOT NULL,
		cpfPessoaAceite varchar(max) NOT NULL,
		emailPessoaAceite varchar(max) NOT NULL,
		resumoAceite text NULL,
		DataCriacao datetime default getDate(),
		CONSTRAINT [PK_TBFornecedorResumoAceite] PRIMARY KEY CLUSTERED 
	(
		IDFornecedorResumoAceite ASC
	) WITH (
		PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TbSolicitacao') AND name IN ('OrigemApi'))
Begin
	ALTER TABLE TbSolicitacao ADD OrigemApi bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFornecedorResumoAceite') AND name IN ('IDFornecedor'))
	AND NOT EXISTS (SELECT * FROM sys.columns WHERE OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridicaResumoAceite'))
Begin
	Alter Table TBFornecedorResumoAceite
		Add IDFornecedor int NOT NULL;

	ALTER TABLE TBFornecedorResumoAceite WITH CHECK ADD CONSTRAINT FK_TBFornecedorResumoAceite_TBPessoaFisicaJuridica FOREIGN KEY(IDFornecedor)
	REFERENCES TBPessoaFisicaJuridica (IDPessoaFisicaJuridica)

	ALTER TABLE TBFornecedorResumoAceite CHECK CONSTRAINT [FK_TBFornecedorResumoAceite_TBPessoaFisicaJuridica]
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBFornecedorResumoAceite'))
BEGIN
	-- Remove a chave primária para alterar o novo nome da coluna
	ALTER table TBFornecedorResumoAceite DROP CONSTRAINT PK_TBFornecedorResumoAceite;

	-- Remove a chave estrangeira para refletir o novo nome da coluna
	ALTER table TBFornecedorResumoAceite DROP CONSTRAINT FK_TBFornecedorResumoAceite_TBPessoaFisicaJuridica;

	-- Renomeia a tabela
	EXEC sp_rename 'TBFornecedorResumoAceite', 'TBPessoaFisicaJuridicaResumoAceite';
	
	-- Renomeia a coluna IDFornecedorResumoAceite para IDPessoaFisicaJuridicaResumoAceite
    EXEC sp_rename 'TBPessoaFisicaJuridicaResumoAceite.IDFornecedorResumoAceite', 'IDPessoaFisicaJuridicaResumoAceite', 'COLUMN';
    
	-- Renomeia a coluna IDFornecedor para IDPessoaFisicaJuridica
    EXEC sp_rename 'TBPessoaFisicaJuridicaResumoAceite.IDFornecedor', 'IDPessoaFisicaJuridica', 'COLUMN';

	-- Refaz a chave primária para refletir o novo nome da coluna
	ALTER table TBPessoaFisicaJuridicaResumoAceite	ADD CONSTRAINT PK_TBPessoaFisicaJuridicaResumoAceite PRIMARY KEY CLUSTERED 
		(IDPessoaFisicaJuridicaResumoAceite ASC) 
		WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY];
	
	-- Refaz a chave estrangeira para refletir o novo nome da coluna
	ALTER TABLE TBPessoaFisicaJuridicaResumoAceite WITH CHECK ADD CONSTRAINT FK_TBPessoaFisicaJuridicaResumoAceite_TBPessoaFisicaJuridica
		FOREIGN KEY(IDPessoaFisicaJuridica) REFERENCES TBPessoaFisicaJuridica (IDPessoaFisicaJuridica);
	ALTER TABLE TBPessoaFisicaJuridicaResumoAceite CHECK CONSTRAINT FK_TBPessoaFisicaJuridicaResumoAceite_TBPessoaFisicaJuridica;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TbSolicitacao') AND name IN ('IdCodigoExterno'))
Begin
	ALTER TABLE TbSolicitacao ADD IdCodigoExterno varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricao') AND name IN ('FaixaValores'))
Begin
	Alter table TBInscricao add FaixaValores varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM TBInscricao where coalesce(FaixaValores,'') <> '')
Begin
	Update TBInscricao
	set FaixaValores = 'Inscrição'
End

GO --

IF Not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ClientIDSantanderPIX'))
Begin
	Alter table TBConfiguracaoWeb add ClientIDSantanderPIX  varchar(max)
	Alter table TBConfiguracaoWeb add ClientSecretSantanderPIX  varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('SolCursoEventoMatricular'))
Begin
	alter table TBRemotoProjetoPerfil add SolCursoEventoMatricular bit;
	alter table TBRemotoProjeto add SolCursoEventoMatricular bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricao') AND name IN ('Matricular'))
Begin
	Alter table TBInscricao add 
		Matricular bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('UtilizarArquivo'))
Begin
	Alter Table TBTurma
		Add UtilizarArquivo bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConta') AND name IN ('ClientIDGetNet'))
Begin
	Alter table TBConta add 
		ClientIDGetNet varchar(max),
		ClientSecretGetNet varchar(max),
		SellerIDGetNet varchar(max)
End

GO --


IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurmaFaixaValores') AND name IN ('QuantidadeMinimaArquivos'))
Begin
	Alter table TBTurmaFaixaValores add 
		QuantidadeMinimaArquivos int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoFaixaValores') AND name IN ('QuantidadeMinimaArquivos'))
Begin
	Alter table TBSolicitacaoCursoEventoFaixaValores add 
		QuantidadeMinimaArquivos int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurmaForm') AND name IN ('TipoCampoResposta'))
Begin
	Alter table TBTurmaForm add 
		TipoCampoResposta bit NULL,
		TipoCampoSimNao bit NULL 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacaoForm') AND name IN ('TipoCampoResposta'))
Begin
	Alter table TBDoacaoForm add 
		TipoCampoResposta bit NULL,
		TipoCampoSimNao bit NULL 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricaoForm') AND name IN ('TipoResposta'))
Begin
	Alter table TBInscricaoForm add 
		TipoResposta tinyint NULL
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricaoForm') AND name IN ('CampoResposta'))
Begin
	Alter table TBInscricaoForm add 
		CampoResposta varchar(max) NULL

	Update TBTurmaForm
	set TipoCampoResposta = 0, 
	TipoCampoSimNao = 1

	Update TBDoacaoForm
	set TipoCampoResposta = 0, 
	TipoCampoSimNao = 1

	Update TBInscricaoForm
	set TipoResposta = 1
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('TokenValicaoWeb'))
Begin
	alter table TBPessoaFisicaJuridica ADD EmailValidacaoWeb varchar(max);
	alter table TBPessoaFisicaJuridica ADD TokenValicaoWeb varchar(6);
	alter table TBPessoaFisicaJuridica ADD TokenValicaoWebValidado bit;
	alter table TBPessoaFisicaJuridica ADD DataExpiracaoTokenValidacaoWeb datetime;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('MeioPagamentoEstrangeiro1'))
Begin
	Alter Table TBTurma
		Add MeioPagamentoEstrangeiro1 bit default 0,
		MeioPagamentoEstrangeiro2 bit default 0,
		MeioPagamentoEstrangeiro3 bit default 0,
		MeioPagamentoEstrangeiro1Titulo varchar(17),
		MeioPagamentoEstrangeiro2Titulo varchar(17),
		MeioPagamentoEstrangeiro3Titulo varchar(17),
		MeioPagamentoEstrangeiro1Descricao varchar(max),
		MeioPagamentoEstrangeiro2Descricao varchar(max),
		MeioPagamentoEstrangeiro3Descricao varchar(max)
End

GO --

if not exists (select * from sysobjects where name='TBInscricaoNotas' and xtype='U')
Begin
	CREATE TABLE TBInscricaoNotas(
		IDInscricaoNotas int IDENTITY(1,1) NOT NULL,
		IDInscricao int NULL,
		DescricaoNota varchar(max) NULL,
	 CONSTRAINT [PK_TBInscricaoNotas] PRIMARY KEY CLUSTERED 
	(
		IDInscricaoNotas ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE TBInscricaoNotas  
	WITH CHECK ADD  CONSTRAINT FK_TBInscricaoNotas_TBInscricao FOREIGN KEY(IDInscricao)
	REFERENCES TBInscricao (IDInscricao)

	ALTER TABLE TBInscricaoNotas CHECK CONSTRAINT FK_TBInscricaoNotas_TBInscricao
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricao') AND name IN ('ObservacaoNotas'))
Begin
	Alter table TBInscricao add 
		ObservacaoNotas varchar(max) NULL
End

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricao') AND name IN ('ArquivosAprovados'))
Begin
	Alter table TBInscricao add 
		ArquivosAprovados bit NULL,
		ObservacaoArquivosAprovados varchar(max) NULL
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurmaFaixaValores') AND name IN ('QuantidadeMaximaArquivos'))
Begin
	Alter table TBTurmaFaixaValores add 
		QuantidadeMaximaArquivos int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoFaixaValores') AND name IN ('QuantidadeMaximaArquivos'))
Begin
	Alter table TBSolicitacaoCursoEventoFaixaValores add 
		QuantidadeMaximaArquivos int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('ValorMensalidade'))
Begin
	Alter table UFPR_TBAluno add 
		ValorMensalidade float null,	
		ValorInscricao float null,
		ValorMatricula float null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurmaForm') AND name IN ('TipoCampoSelecao'))
Begin
	Alter table TBTurmaForm add 
		TipoCampoSelecao bit NULL, 
		TipoCampoSelecaoItens varchar(max) NULL  
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacaoForm') AND name IN ('TipoCampoSelecao'))
Begin
	Alter table TBDoacaoForm add 
		TipoCampoSelecao bit NULL, 
		TipoCampoSelecaoItens varchar(max) NULL  
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricaoForm') AND name IN ('IDTurmaForm'))
Begin
	Alter table TBInscricaoForm add 
		IDTurmaForm int NULL
End

GO --

update TBInscricaoForm
set IDTurmaForm = 0
where IDTurmaForm is null

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ValidarCEP'))
Begin
	Alter Table TBConfiguracaoWeb
		Add ValidarCEP bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ValorAtividadeContratoVariavel'))
Begin
	Alter Table TBConfiguracaoWeb
		Add ValorAtividadeContratoVariavel bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('FusoHorarioInscricao'))
Begin
	Alter Table TBConfiguracaoWeb
		Add FusoHorarioInscricao int default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBContratoAnaliseTecnica') AND name IN ('ValorLiberado'))
Begin
	Alter table TBContratoAnaliseTecnica 
	add ValorLiberado float null 
End

GO --

IF NOT EXISTS (SELECT * FROM TBContratoAnaliseTecnica where coalesce(ValorLiberado,0) > 0)
Begin
	Update TBContratoAnaliseTecnica
	set ValorLiberado = ValorALiberar 
	where (DataAprovacao is not null) or (coalesce(DataEntrada,0) > 10)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('NaoUtilizarMensageria'))
Begin
	Alter Table TBConfiguracaoWeb
		Add NaoUtilizarMensageria bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('DataPrimeiroVencimento'))
Begin
	Alter table UFPR_TBAluno add 
		DataPrimeiroVencimento datetime null
End

GO --

IF EXISTS (
select * from TBContratoAnaliseTecnica
where ValorALiberar = ValorLiberado / 100 and ValorALiberar > 0.005
)
Begin
	update TBContratoAnaliseTecnica
	set ValorLiberado = ValorLiberado / 100
	where ValorALiberar = ValorLiberado / 100
End

GO --


IF EXISTS (
select * from TBContratoAnaliseTecnica
where ValorALiberar = ValorLiberado / 10 and ValorALiberar > 0.005
)
Begin
	update TBContratoAnaliseTecnica
	set ValorLiberado = ValorLiberado / 10
	where ValorALiberar = ValorLiberado / 10
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('EnviarEmailCriacaoSolicitacao'))
Begin
	Alter Table TBConfiguracaoWeb
		Add EnviarEmailCriacaoSolicitacao bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('EnviarEmailCriacaoSolicitacaoProjeto'))
Begin
	Alter Table TBConfiguracaoWeb
		Add EnviarEmailCriacaoSolicitacaoProjeto bit default 0,
		EnviarEmailCriacaoSolicitacaoCoordenadorFinanceiro bit default 0,
		EnviarEmailCriacaoSolicitacaoCoordenadorPrincipal bit default 0,
		EnviarEmailCriacaoSolicitacaoProjetoResponsavelLiberacao bit default 0,
		EnviarEmailCriacaoSolicitacaoOutrosEmail varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('TipoDocumento'))
Begin
	Alter Table TBSolicitacao
		Add TipoDocumento int default -1
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolsoTerceiro') AND name IN ('IdConta'))
Begin
	Alter Table TBSolicitacaoReembolsoTerceiro
		Add IdConta int default 0

	update TBSolicitacao set TipoDocumento = -1
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolsoTerceiro') AND name IN ('OrigemApi'))
Begin
	ALTER TABLE TBSolicitacaoReembolsoTerceiro ADD OrigemApi bit null
End

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoReembolsoTerceiro') AND name IN ('IdCodigoExterno'))
Begin
	ALTER TABLE TBSolicitacaoReembolsoTerceiro ADD IdCodigoExterno varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('UFPR_AlunoRescisao'))
Begin
	ALTER TABLE TBLancamento ADD UFPR_AlunoRescisao bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('UFPR_AlunoRescisao'))
Begin
	ALTER TABLE UFPR_TBAluno ADD UFPR_AlunoRescisao bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('AcessoRelatorioTecnicoProjeto'))
Begin
	alter table TBRemotoProjetoPerfil add AcessoRelatorioTecnicoProjeto bit;
	alter table TBRemotoProjeto add AcessoRelatorioTecnicoProjeto bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('DataVencimentoInscricao'))
Begin
	ALTER TABLE UFPR_TBAluno ADD DataVencimentoInscricao datetime null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('PreecherEquipeRelatorioTecnico'))
Begin
	Alter table TBProjeto add PreecherEquipeRelatorioTecnico bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacao') AND name IN ('IDUsuarioResponsavel'))
Begin
	Alter table TBImportacao add 
		IDUsuarioResponsavel int null,
		InstitutoProcesso varchar(max) null,
		DescricaoImportacao varchar(max) null,
		IDProjeto int null,
		LeadTime int null,
		NomeSolicitante varchar(max) null,
		EmailSolicitante varchar(max) null,
		TelefoneSolicitante varchar(max) null,
		Coordenador varchar(max) null
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoEmbarque' and xtype='U')
Begin
	CREATE TABLE TBImportacaoEmbarque(
		IDImportacaoEmbarque int IDENTITY(1,1) NOT NULL,
		IDImportacao int null,
		Invoice varchar(max) NULL,
		PackingList varchar(max) NULL,
		Volume varchar(max) NULL,
		Dimensao varchar(max) NULL,
		PesoLiquido varchar(max) NULL,
		PesoBruto varchar(max) NULL,
		Origem varchar(max) NULL,
		Destino varchar(max) NULL,
		ETD varchar(max) NULL,
		ETA varchar(max) NULL,
		AgenteCarga varchar(max) NULL,
		IDMoeda int NULL,
		Valor float NULL,
	 CONSTRAINT PK_TBImportacaoEmbarque PRIMARY KEY CLUSTERED 
	(
		IDImportacaoEmbarque ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoDesembaraco' and xtype='U')
Begin
	CREATE TABLE TBImportacaoDesembaraco(
		IDImportacaoDesembaraco int IDENTITY(1,1) NOT NULL,
		IDImportacao int NULL,
		DI varchar(max) NULL,
		DataDI datetime NULL,
		Parametrizacao tinyint NULL,
		MAWB varchar(max) NULL,
		House varchar(max) NULL,
		DataEmbarque datetime NULL,
		DataChegada datetime NULL,
		DataEntrega datetime NULL,
		RecebidoPor varchar(max) NULL,
		Avarias varchar(max) NULL,
	 CONSTRAINT PK_TBImportacaoDesembaraco PRIMARY KEY CLUSTERED 
	(
	IDImportacaoDesembaraco ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjetoPlanoTrabalho') AND name IN ('DataInicioExecucao'))
Begin
	Alter table TBProjetoPlanoTrabalho  add 
		DataInicioExecucao DateTime null,
		DataFinalExecucao DateTime null,
		Observacao varchar(max) null
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoDespesas' and xtype='U')
Begin
	CREATE TABLE TBImportacaoDespesas(
		IDImportacaoDespesas int IDENTITY(1,1) NOT NULL,
		IDImportacaoTipoCusto int NULL,
		IDImportacao int NULL,
		Descricao varchar(max) NULL,
		Documento varchar(max) NULL,
		IDImportacaoMoeda int NULL,
		ValorOriginal float NULL,
		TaxaConversao float NULL,
		ResponsavelCredito int NULL,
		Observacao varchar(max) NULL,
		DataOcorrencia int NULL,
		IDConta int NULL,
		CreditoDebito tinyint NULL,
		CONSTRAINT PK_TTBImportacaoDespesas PRIMARY KEY CLUSTERED 
	(
		IDImportacaoDespesas ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoLI' and xtype='U')
Begin
	CREATE TABLE TBImportacaoLI(
		IDImportacaoLI int IDENTITY(1,1) NOT NULL,
		IDImportacao int NULL,
		Licenciamento varchar(max) NULL,
		Data int NULL,
		ValidadeEmbarque int NULL,
		ValidadeDespacho int NULL,
		Observacao varchar(max) NULL,
		IDImportacaoOrgao int NULL,
	 CONSTRAINT PK_TBImportacaoLI PRIMARY KEY CLUSTERED 
	(
		IDImportacaoLI ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoLIOrgao' and xtype='U')
Begin
	CREATE TABLE TBImportacaoLIOrgao(
		IDImportacaoLIOrgao int IDENTITY(1,1) NOT NULL,
		NomeOrgao varchar(max) NULL,
	 CONSTRAINT PK_TBImportacaoLIOrgao PRIMARY KEY CLUSTERED 
	(
		IDImportacaoLIOrgao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO --

if not exists (select * from TBImportacaoLIOrgao)
Begin
	Insert Into TBImportacaoLIOrgao
	(NomeOrgao)
	Values
	('Agência Nacional de Energia Elétrica - ANEEL'),
	('Agência Nacional de Vigilância Sanitária - ANVISA'),
	('Agência Nacional do Cinema - ANCINE'),
	('Agência Nacional do Petróleo, Gás Natural e Biocombustíveis - ANP'),
	('Comando do Exército - COMEXE'),
	('Conselho Nacional de Desenvolvimento Científico e Tecnológico - CNPq'),
	('Departamento de Operações de Comércio Exterior - DECEX'),
	('Departamento de Polícia Federal - DPF'),
	('Departamento Nacional de Produção Mineral - DNPM'),
	('Empresa Brasileira de Correios e Telégrafos - EBC'),
	('Instituto Brasileiro do Meio Ambiente e dos Recursos Naturais Renováveis - IBAMA'),
	('Instituto Nacional de Metrologia, Normalização e Qualidade Industrial - INMETRO'),
	('Ministério da Agricultura, Pecuária e Abastecimento - MAPA'),
	('Ministério da Ciência e Tecnologia - MCT'),
	('Superintendência da Zona Franca de Manaus - SUFRAMA')
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjetoPlanoTrabalhoExecucao') AND name IN ('DataInicioExecucao'))
Begin
	Alter table TBProjetoPlanoTrabalhoExecucao  add 
		DataInicioExecucao DateTime null,
		DataFinalExecucao DateTime null,
		Observacao varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemImportacao') AND name IN ('DescricaoTraduzida'))
Begin
	Alter table TBItemImportacao  add 
		DescricaoTraduzida varchar(max) null
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoEmbarque') AND name IN ('Volume'))
Begin
	Alter Table TBImportacaoEmbarque
		drop column Volume,
		Dimensao,
		PesoLiquido,
		PesoBruto
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoEmbarquePackingList' and xtype='U')
Begin
	CREATE TABLE TBImportacaoEmbarquePackingList(		
		IDImportacaoEmbarquePackingList int IDENTITY(1,1) NOT NULL,
		IDImportacaoEmbarque int null,
		Volume varchar(max) NULL,
		Dimensao varchar(max) NULL,
		PesoLiquido varchar(max) NULL,
		PesoBruto varchar(max) NULL,
	 CONSTRAINT PK_TBImportacaoEmbarquePackingList PRIMARY KEY CLUSTERED 
	(
		IDImportacaoEmbarquePackingList ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoEmbarqueItem' and xtype='U')
Begin
	CREATE TABLE TBImportacaoEmbarqueItem(	
	    IDImportacaoEmbarqueItem int IDENTITY(1,1) NOT NULL,
		IDImportacaoEmbarquePackingList int null,
		IDItemImportacao int null,
		Quantidade float null,
	 CONSTRAINT PK_TBImportacaoEmbarqueItem PRIMARY KEY CLUSTERED 
	(
		IDImportacaoEmbarqueItem ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemImportacao') AND name IN ('EquipamentoComputador'))
Begin
	Alter Table TBItemImportacao
		Add EquipamentoComputador bit default 0,
			EquipamentoMedicaoControle bit default 0,
			EquipamentoHospitalar bit default 0,
			EquipamentoMecanicoEletronico bit default 0,
			EquipamentoOtico bit default 0,
			EquipamentoOutros bit default 0,
			BensConsumoQuimico bit default 0,
			BensConsumoPerecivel bit default 0,
			BensConsumoEletronico bit default 0,
			BensConsumoPecasAcessorios bit default 0,
			BensConsumoBiologico bit default 0,
			BensConsumoOutros bit default 0,
			ServicoCongresso bit default 0,
			ServicoAmostras bit default 0,
			ServicoEdicaoTexto bit default 0,
			ServicoPublicacao bit default 0,
			ServicoSoftware bit default 0,
			ServicoOutros bit default 0
End

GO --

IF Not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoEmbarque') AND name IN ('EmbarqueData'))
Begin
	Alter Table TBImportacaoEmbarque
		add EmbarqueData int null,
		EmbarqueValidade varchar(max) null
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoStatus' and xtype='U')
Begin
	CREATE TABLE TBImportacaoStatus(	
	    IDImportacaoStatus int IDENTITY(1,1) NOT NULL,
		IDImportacao int null,
		IDImportacaoInformacaoMonitor int null,
		DataStatus int null,
		Concluido bit default 0,
		Observacao varchar(max) null,
	 CONSTRAINT PK_TBImportacaoStatus PRIMARY KEY CLUSTERED 
	(
		IDImportacaoStatus ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF Not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoInformacaoMonitor') AND name IN ('IDImportacaoRotuloMonitor'))
Begin
	Alter Table TBImportacaoInformacaoMonitor
		add IDImportacaoRotuloMonitor int null
End

GO --

IF Not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacao') AND name IN ('StatusImportacao'))
Begin
	Alter Table TBImportacao
		add StatusImportacao int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoDespesas') AND name IN ('IDProjeto'))
Begin
	Alter table TBImportacaoDespesas  add 
		IDProjeto int null,
		IDRubrica int null,
		IDSubRubrica int null,
		Fechamento bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('IDImportacaoDespesas'))
Begin
	Alter table TBLancamento  add 
		IDImportacaoDespesas int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolicitacaoPJAnexarNF'))
Begin
	Alter table TBConfiguracaoWeb add 
		SolicitacaoPJAnexarNF bit default 0,
		SolicitacaoPJAnexarBoleto bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('SolicitacaoPJAnexouNF'))
Begin
	Alter table TBSolicitacao add 
		SolicitacaoPJAnexouNF bit default 0,
		SolicitacaoPJJustificouNF bit default 0,
		SolicitacaoPJAnexouBoleto bit default 0,
		SolicitacaoPJJustificouBoleto bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('SolicitacaoPJAnexouNF'))
Begin
	Alter table TBSolicitacao add 
		SolicitacaoPJAnexouNF bit default 0,
		SolicitacaoPJJustificouNF bit default 0,
		SolicitacaoPJAnexouBoleto bit default 0,
		SolicitacaoPJJustificouBoleto bit default 0
End

GO --

IF Not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoEmbarque') AND name IN ('IDImportacaoTipoEmbarque'))
Begin
	Alter Table TBImportacaoEmbarque
		add IDImportacaoTipoEmbarque int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('DataHoraCriacao'))
Begin
	ALTER TABLE UFPR_TBAluno ADD 
		DataHoraCriacao datetime default GETDATE(),
		DataHoraAssinaturaContrato datetime null,
		IPAssinaturaContrato varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('SolicitacaoPJJustificativaNF'))
Begin
	Alter table TBSolicitacao add 
		SolicitacaoPJJustificativaNF varchar(max)  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoTransferencia') AND name IN ('Viaweb'))
Begin
	Alter table TBSolicitacaoTransferencia add 
		Viaweb bit  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('ObrigatorioProdutoSolCompra'))
Begin
	Alter table TBProjeto add 
		ObrigatorioProdutoSolCompra bit  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseUFPR'))
alter table TBConfiguracao1 add PastaBaseUFPR varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseUFPR_Azure'))
alter table TBConfiguracao1 add PastaBaseUFPR_Azure varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracao1') AND name IN ('PastaBaseUFPR_AmazonS3'))
alter table TBConfiguracao1 add PastaBaseUFPR_AmazonS3 varchar(max);

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('IDUFPRAluno'))
Begin
	Alter table UFPR_TBAluno add 
		IDUFPRAluno int IDENTITY(1,1)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRubricaProjeto') AND name IN ('LiberarTransferenciaWEB'))
Begin
	Alter table TBRubricaProjeto add 
		LiberarTransferenciaWEB bit  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolTransferenciaSomenteRubricasLiberadas'))
Begin
	Alter table TBConfiguracaoWeb add 
		SolTransferenciaSomenteRubricasLiberadas bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProduto') AND name IN ('LiberarWEB'))
Begin
	Alter table TBProduto add 
		LiberarWEB bit  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRubricaProjeto') AND name IN ('LiberarTransferenciaWEBDebito'))
Begin
	Alter table TBRubricaProjeto add 
		LiberarTransferenciaWEBDebito bit  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTiposProjeto') AND name IN ('LiberarTransferenciaWEBDebito'))
Begin
	Alter table TBTiposProjeto add 
		LiberarTransferenciaWEBDebito bit  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacao') AND name IN ('Departamento'))
Begin
	Alter table TBImportacao add 
		Departamento varchar(max)  null,
		Laboratorio varchar(max)  null 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemImportacao') AND name IN ('DespesaEnvio'))
Begin
	Alter table TBItemImportacao add 
		DespesaEnvio bit  null
End

GO --

IF EXISTS (SELECT * FROM sys.columns s
				left join sys.types t
				on s.system_type_id = t.system_type_id
				WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoDesembaraco') AND s.name IN ('DataDI') and t.name in ('datetime'))
Begin
	Alter table TBImportacaoDesembaraco
	drop column DataDI, DataEmbarque, DataChegada, DataEntrega;


	Alter table TBImportacaoDesembaraco
	add DataDI int null,
	DataEmbarque int null,
	DataChegada int null,
	DataEntrega int null,
	Observacao varchar(max) null,
	IDImportacaoFlag int null;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('HabilitarMateriaisConsumo'))
Begin
	Alter table TBProjeto add 
		HabilitarMateriaisConsumo bit  null,
		HabilitarLivros bit null,
		HabilitarEquipamentos bit null,
		HabilitarContratacoesServico bit null,
		HabilitarLocacaoVeiculo bit null,
		HabilitarReservaHotel bit null,
		HabilitarImportacao bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('HabilitarTiposCompraPorProjeto'))
Begin
	Alter table TBConfiguracaoWeb add 
		HabilitarTiposCompraPorProjeto bit  null

	Update TBProjeto
		set HabilitarMateriaisConsumo = 1,
		HabilitarLivros = 1,
		HabilitarEquipamentos = 1,
		HabilitarContratacoesServico = 1,
		HabilitarLocacaoVeiculo = 1,
		HabilitarReservaHotel = 1,
		HabilitarImportacao = 1
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemSolicitacaoCompra') AND name IN ('IdTipoProduto'))
Begin
	Alter table TBItemSolicitacaoCompra add 
		IdTipoProduto int  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('RG'))
Begin
	Alter table UFPR_TBAluno add 
		RG varchar(max)  null,
		NomeEstado varchar(max)  null,
		Curso varchar(max)  null,
		Modalidade varchar(max)  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('NomeRepresentante'))
Begin
	Alter table UFPR_TBAluno add 
		NomeRepresentante varchar(max)  null,
		RGRepresentante varchar(max)  null,
		CPFRepresentante varchar(max)  null,
		CidadeRepresentante varchar(max)  null,
		NomeEstadoRepresentante varchar(max)  null,
		ProfissaoRepresentante varchar(max)  null,
		EstadoCivilRepresentante varchar(max)  null,
		TelefoneRepresentante varchar(max)  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolBolsaObrigatorioPISInss'))
Begin
	Alter table TBConfiguracaoWeb add 
		SolBolsaObrigatorioPISInss bit default 0,
		SolBolsaObrigatorioCargaHoraria bit default 0
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoDesembaraco') AND name IN ('Parametrizacao'))
Begin
	Alter table TBImportacaoDesembaraco
	drop column Parametrizacao, MAWB, House, RecebidoPor, Avarias, DataEmbarque, DataChegada, DataEntrega;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('HabilitarComprasOnline'))
Begin
	Alter table TBProjeto add 
		HabilitarComprasOnline bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('UFRepresentante'))
Begin
	Alter table UFPR_TBAluno add 
		UFRepresentante varchar(max)
End

GO --

IF EXISTS (SELECT * FROM sys.columns s
				left join sys.types t
				on s.system_type_id = t.system_type_id
				WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoTransferencia') AND s.name IN ('IDContaDebito') and s.is_nullable = 0)
Begin
	Alter table TBSolicitacaoTransferencia
	alter column IDContaDebito int null

	Alter table TBSolicitacaoTransferencia
	alter column IDContacredito int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemSolicitacaoCompra') AND name IN ('executora'))
Begin
	alter table TBItemSolicitacaoCompra add 
		executora varchar(1) null,
		linkProduto varchar(max) null,
		empresaProduto varchar(max) null,
		projetoAceitarRetirarMercadoria bit null,
		projetoAceitarVariacaoPreco bit null,
		projetoAceitarFrete bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCompra') AND name IN ('pessoaAssinaturaTermo'))
Begin
	alter table TBSolicitacaoCompra add 
		pessoaAssinaturaTermo varchar(max) null,
		cpfAssinaturaTermo varchar(max) null,
		dataAssinaturaTermo datetime null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('IDRubProjetoTarifaPix'))
	Alter table TBProjeto add 
		IDRubProjetoTarifaPix int null,
		PercentualTarifaPix float null,
		LimitadorTarifaPix float null;
GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoServicosAviso') AND name IN ('ControleTransacaoSantanderPIX'))
Begin
	Alter table TBConfiguracaoServicosAviso add 
		ControleTransacaoSantanderPIX bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemSolicitacaoCompra') AND name IN ('percentualVariacaoValor'))
Begin
	alter table TBItemSolicitacaoCompra add 
		percentualVariacaoValor varchar(10) null,
		percentualVariacaoFrete varchar(10) null
End

GO --

IF Not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('NaoMostrarPeriodoInscricao'))
Begin
	Alter Table TBTurma
		Add NaoMostrarPeriodoInscricao bit default 0,
		NaoMostrarDataInicioInscricao bit default 0,
		NaoMostrarDataFimInscricao bit default 0,
		NaoMostrarDataInicioCurso bit default 0,
		NaoMostrarDataFimCurso bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoDespesas') AND name IN ('Provisao'))
Begin
	Alter table TBImportacaoDespesas  add 
		Provisao bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacao') AND name IN ('DescricaoImportacaoProjeto'))
Begin
	Alter table TBImportacao add 
		DescricaoImportacaoProjeto varchar(max)  null 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoDespesas') AND name IN ('Relatorio'))
Begin
	Alter table TBImportacaoDespesas  add 
		Relatorio bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('Moeda'))
Begin
	Alter table UFPR_TBAluno add 
		Moeda int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoServicosAviso') AND name IN ('ProcessarDoacoesRecorrentes'))
Begin
	Alter table TBConfiguracaoServicosAviso add 
		ProcessarDoacoesRecorrentes bit null

	delete from TBLancamentoInscricao
	where idlancamento not in (select idlancamento from TBLancamento)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCompra') AND name IN ('IDSolicitacaoOrigem'))
Begin
	Alter table TBSolicitacaoCompra add 
		IDSolicitacaoOrigem int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCompra') AND name IN ('ObservacaoCancelamento'))
Begin
	Alter table TBSolicitacaoCompra add 
		ObservacaoCancelamento varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacao') AND name IN ('Fapesp'))
Begin
	Alter table TBImportacao add 
		Fapesp bit null,
		DataStatus int null
End

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBWebPessoaFisicaJuridica') AND name IN ('PagamentoBoleto') AND is_nullable = 0)
Begin
	ALTER TABLE TBWebPessoaFisicaJuridica ALTER COLUMN PagamentoBoleto bit NULL;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBUsuario') AND name IN ('AcessoIrrestritoModuloImportacao'))
Begin
	Alter table TBUsuario add 
		AcessoIrrestritoModuloImportacao bit null
End

GO --

if not exists (select * from sysobjects where name='TBImportacaoFornecedores' and xtype='U')
Begin
	CREATE TABLE TBImportacaoFornecedores(
		IDImportacaoFornecedores int IDENTITY(1,1) NOT NULL,
		IDFornecedor int null,
	 CONSTRAINT PK_TBImportacaoFornecedores PRIMARY KEY CLUSTERED 
	(
		IDImportacaoFornecedores ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('IDImportacao'))
Begin
	Alter table TBSolicitacao add 
		IDImportacao int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoDespesas') AND name IN ('IDSolicitacaoPagamento'))
Begin
	Alter table TBImportacaoDespesas add 
		IDSolicitacaoPagamento int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ViewCoreme'))
Begin
	Alter table TBConfiguracaoWeb add 
		ViewCoreme varchar(max),
		ViewCoremu varchar(max),
		ViewDemaisCursos varchar(max)
END

GO --

IF EXISTS (Select * From TBConfiguracaoWeb CW full join TBConfiguracao C on (C.IDConfiguracao <> 0) 
			Where coalesce(ViewCoreme,'') = '' and c.AbreviaturaFundacao = 'FAPUNIFESP')
Begin
	update TBConfiguracaoWeb set ViewCoreme = 'imagenet_fapunifesp_coreme2025'
END

GO --

IF EXISTS (Select * From TBConfiguracaoWeb CW full join TBConfiguracao C on (C.IDConfiguracao <> 0) 
			Where coalesce(ViewCoremu,'') = '' and c.AbreviaturaFundacao = 'FAPUNIFESP')
Begin
	update TBConfiguracaoWeb set ViewCoremu = 'imagenet_fapunifesp_coremu2021'
END

GO --

IF EXISTS (Select * From TBConfiguracaoWeb CW full join TBConfiguracao C on (C.IDConfiguracao <> 0) 
			Where coalesce(ViewDemaisCursos,'') = '' and c.AbreviaturaFundacao = 'FAPUNIFESP')
Begin
	update TBConfiguracaoWeb set ViewDemaisCursos = 'imagenet_fapunifesp_gateway'
END

GO --

IF EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacao') AND name IN ('DescricaoImportacao'))
Begin
	--update TBImportacao
	--set TituloProjeto = DescricaoImportacao

	Alter table TBImportacao
	drop column DescricaoImportacao;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('Coordenador'))
Begin
	Alter table TBSolicitacaoImportacao add 
		Coordenador varchar(max) null,
		IDProjeto int null,
		IDCoordenadorLibCanc int null,
		DataCoordenadorLibCanc Datetime null,
		ViaWeb bit null

	Alter table TBItemSolicitacaoImportacao alter column CodigoItem varchar(max) NULL;
	Alter table TBItemSolicitacaoImportacao alter column NCMHSCode varchar(max) NULL;
	Alter table TBItemSolicitacaoImportacao alter column NomeFabricante varchar(max) NULL;
	Alter table TBItemSolicitacaoImportacao alter column PaisProcedencia varchar(max) NULL;
	Alter table TBItemSolicitacaoImportacao alter column Unidade varchar(max) NULL;

	Alter table TBItemImportacao alter column CodigoItem varchar(max) NULL;
	Alter table TBItemImportacao alter column NCMHSCode varchar(max) NULL;
	Alter table TBItemImportacao alter column NomeFabricante varchar(max) NULL;
	Alter table TBItemImportacao alter column PaisProcedencia varchar(max) NULL;
	Alter table TBItemImportacao alter column Unidade varchar(max) NULL;
END

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBItemSolicitacaoImportacao') AND name IN ('EquipamentoComputador'))
Begin
	Alter Table TBItemSolicitacaoImportacao
		Add EquipamentoComputador bit default 0,
			EquipamentoMedicaoControle bit default 0,
			EquipamentoHospitalar bit default 0,
			EquipamentoMecanicoEletronico bit default 0,
			EquipamentoOtico bit default 0,
			EquipamentoOutros bit default 0,
			BensConsumoQuimico bit default 0,
			BensConsumoPerecivel bit default 0,
			BensConsumoEletronico bit default 0,
			BensConsumoPecasAcessorios bit default 0,
			BensConsumoBiologico bit default 0,
			BensConsumoOutros bit default 0,
			ServicoCongresso bit default 0,
			ServicoAmostras bit default 0,
			ServicoEdicaoTexto bit default 0,
			ServicoPublicacao bit default 0,
			ServicoSoftware bit default 0,
			ServicoOutros bit default 0,
			DescricaoTraduzida varchar(max) null,
			DespesaEnvio bit  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ObrigatorioAnexarArquivoSolPF'))
Begin
	Alter table TBConfiguracaoWeb add 
		ObrigatorioAnexarArquivoSolPF bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoForm'))
Begin
	CREATE TABLE TBSolicitacaoCursoEventoForm(
	IDSolicitacaoCursoEventoForm int IDENTITY(1,1) NOT NULL,
	IdSolicitacaoCursoEventoServico int NULL,
	Pergunta varchar(max) NULL,
	Obrigatorio bit NULL,
	TipoCampoResposta bit NULL,
	TipoCampoSimNao bit NULL,
	TipoCampoSelecao bit NULL, 
	TipoCampoSelecaoItens varchar(max) NULL,
	 CONSTRAINT PK_TBSolicitacaoCursoEventoForm PRIMARY KEY CLUSTERED 
	(
		IDSolicitacaoCursoEventoForm ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
	

	ALTER TABLE TBSolicitacaoCursoEventoForm  WITH CHECK ADD  CONSTRAINT FK_TBTBSolicitacaoCursoEventoForm_TBSolicitacaoCursoEventoServico FOREIGN KEY(IdSolicitacaoCursoEventoServico)
	REFERENCES TBSolicitacaoCursoEventoServico (IdSolicitacaoCursoEventoServico)
	

	ALTER TABLE TBSolicitacaoCursoEventoForm CHECK CONSTRAINT FK_TBTBSolicitacaoCursoEventoForm_TBSolicitacaoCursoEventoServico
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('SolImportacao'))
Begin
	alter table TBRemotoProjetoPerfil add SolImportacao bit;
	alter table TBRemotoProjeto add SolImportacao bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ExibirAdicionalVariavel'))
Begin
	Alter table TBConfiguracaoWeb 
	add ExibirAdicionalVariavel bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCompra') AND name IN ('Pendente'))
Begin
	Alter table TBSolicitacaoCompra 
		add Pendente bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('Objetivo'))
Begin
	Alter table TBSolicitacaoBolsa 
		add Objetivo varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolBolsaObrigatorioObjetivo'))
Begin
	Alter table TBConfiguracaoWeb add 
		SolBolsaObrigatorioObjetivo bit default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTiposContrato') AND name IN ('AdicionalVariavel'))
Begin
	Alter Table TBTiposContrato add 
		AdicionalVariavel bit
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('FuncionarioPublico'))
Begin
	Alter Table TBSolicitacaoBolsa add 
		FuncionarioPublico bit null,
		Cargo varchar(max) null,
		Departamento varchar(max) null,
		InstituicaoPublica varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBImportacaoMoeda') AND name IN ('Simbolo'))
Begin
	Alter Table TBImportacaoMoeda add 
		Simbolo varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('CotacaoEstimada'))
Begin
	Alter Table TBSolicitacaoImportacao add 
		CotacaoEstimada float null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurmaFaixaValores') AND name IN ('UtilizarArquivo'))
Begin
	Alter table TBTurmaFaixaValores add 
		UtilizarArquivo bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoFaixaValores') AND name IN ('UtilizarArquivo'))
Begin
	Alter table TBSolicitacaoCursoEventoFaixaValores add 
		UtilizarArquivo bit null

	Update TBTurmaFaixaValores
	set UtilizarArquivo = 1
	where AnexoObrigatorio = 1
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('Unidade'))
Begin
	Alter Table TBSolicitacaoBolsa add 
		Unidade varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('TipoQuadroUSP'))
Begin
	Alter Table TBSolicitacaoBolsa add 
		TipoQuadroUSP bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('RegimeTrabalhoUSP'))
Begin
	Alter Table TBSolicitacaoBolsa add 
		RegimeTrabalhoUSP tinyint null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('RPA'))
Begin
	Alter table TBSolicitacao add 
		RPA int null,
		DTInicioContrato int null,
		DTFimContrato int null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('CNPJInstituicaoPublica'))
Begin
	Alter Table TBSolicitacaoBolsa add 
		CNPJInstituicaoPublica varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('SolBolsaMsgAdicionalVariavel'))
Begin
	Alter table TBConfiguracaoWeb add SolBolsaMsgAdicionalVariavel varchar(max)
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('CargaHorariaInstituicao'))
Begin
	Alter Table TBSolicitacaoBolsa add 
		CargaHorariaInstituicao varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('pessoaAssinaturaTermo'))
Begin
	alter table TBSolicitacaoBolsa add 
		pessoaAssinaturaTermo varchar(max) null,
		cpfAssinaturaTermo varchar(max) null,
		dataAssinaturaTermo datetime null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('SolicitacaoJustificativaAnexo'))
Begin
	Alter table TBSolicitacaoBolsa add 
		SolicitacaoJustificativaAnexo varchar(max)  null,
		SolicitacaoAnexouArquivo bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoServico') AND name IN ('InstagramCursoEventoDoacao'))
Begin
	ALTER TABLE TBSolicitacaoCursoEventoServico ADD
		InstagramCursoEventoDoacao varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('InstagramCursoEventoDoacao'))
Begin
	Alter Table TBDoacao
		Add InstagramCursoEventoDoacao varchar(max) null

	Alter Table TBDoacao
		Alter column URLDirecionamento varchar(max) null
End

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('InstagramCursoEventoDoacao'))
Begin
	Alter Table TBTurma
		Add InstagramCursoEventoDoacao varchar(max) null

	Alter Table TBTurma
		Alter column URLDirecionamento varchar(max) null
End

update TBDoacao
set URLDirecionamento = ''
where URLDirecionamento = 'https:\\'

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoServico') AND name IN ('PermitirEstrangeiro'))
Begin
	Alter table TBSolicitacaoCursoEventoServico add 
		PermitirEstrangeiro bit null,
		CadastroCompleto bit null,
		TraduzirIngles bit null,
		TraduzirEspanhol bit null,
		TraduzirFrances bit null,
		TraduzirItaliano bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('TraduzirFrances'))
Begin
	Alter table TBTurma add 
		TraduzirFrances bit null,
		TraduzirItaliano bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('TraduzirFrances'))
Begin
	Alter table TBDoacao add 
		TraduzirFrances bit null,
		TraduzirItaliano bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('MeioPagamentoEstrangeiro1'))
Begin
	Alter Table TBDoacao
		Add MeioPagamentoEstrangeiro1 bit default 0,
		MeioPagamentoEstrangeiro2 bit default 0,
		MeioPagamentoEstrangeiro3 bit default 0,
		MeioPagamentoEstrangeiro1Titulo varchar(17),
		MeioPagamentoEstrangeiro2Titulo varchar(17),
		MeioPagamentoEstrangeiro3Titulo varchar(17),
		MeioPagamentoEstrangeiro1Descricao varchar(max),
		MeioPagamentoEstrangeiro2Descricao varchar(max),
		MeioPagamentoEstrangeiro3Descricao varchar(max)
End

GO --

IF NOT EXISTS (select * from sysobjects where name='TBDoacaoTraducao' and xtype='U')
Begin
	CREATE TABLE TBDoacaoTraducao(
	IDDoacaoTraducao int IDENTITY(1,1) NOT NULL,
	IDDoacao int NULL,
	IDLinguagem int null,
	Descricao varchar(max) NULL,
	Resumo varchar(max) NULL,
	DescricaoDoacao varchar(max) NULL,
	MensagemFinalSalvar varchar(max) NULL,
    CONSTRAINT TBDoacaoTranslate PRIMARY KEY CLUSTERED 
	(
		IDDoacaoTraducao ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
	

	ALTER TABLE TBDoacaoTraducao  WITH CHECK ADD  CONSTRAINT FK_TBDoacaoTraducao_TBDoacao FOREIGN KEY(IDDoacao)
	REFERENCES TBDoacao (IDDoacao)
	
	ALTER TABLE TBDoacaoTraducao CHECK CONSTRAINT FK_TBDoacaoTraducao_TBDoacao
End

GO --

IF NOT EXISTS (select * from TBDoacaoTraducao)
Begin
	Insert Into TBDoacaoTraducao
	(IDDoacao, IDLinguagem, Descricao, Resumo, DescricaoDoacao, MensagemFinalSalvar)
	Select
		IDDoacao, 1, Descricao, Resumo, DescricaoDoacao, MensagemFinalSalvar
	From TBDoacao
END

GO --

IF NOT EXISTS (select * from sysobjects where name='TBTurmaTraducoes' and xtype='U')
Begin
	CREATE TABLE TBTurmaTraducoes(
	IDTurmaTraducoes int IDENTITY(1,1) NOT NULL,
	IDTurma int NULL,
	IDLinguagem int null,
	Descricao varchar(max) NULL,
	Resumo varchar(max) NULL,
	MensagemFinalSalvar varchar(max) NULL,
	Informacoes varchar(max) NULL,
	Observacoes varchar(max) NULL,
	Justificativa varchar(max) NULL,
	Objetivos varchar(max) NULL,
	Metodologia varchar(max) NULL,
	Infraestrutura varchar(max) NULL,
	EmentaDisciplina varchar(max) NULL,
	Investimento varchar(max) NULL,
	Local varchar(max) NULL,
	OutrasInformacoes varchar(max) NULL,
	Duracao varchar(max) NULL,
	AreaConhecimento varchar(max) null,
	PreRequisito varchar(max) null,
	PublicoAlvo varchar(max) null,
	FormaPagamento varchar(max) null,
	DevolucaoInscricao varchar(max) null,
    CONSTRAINT TBTurmaTraducao PRIMARY KEY CLUSTERED 
	(
		IDTurmaTraducoes ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
	

	ALTER TABLE TBTurmaTraducoes  WITH CHECK ADD  CONSTRAINT FK_TBTurmaTraducoes_TBTurma FOREIGN KEY(IDTurma)
	REFERENCES TBTurma (IDTurma)
	
	ALTER TABLE TBTurmaTraducoes CHECK CONSTRAINT FK_TBTurmaTraducoes_TBTurma
End

GO --

IF NOT EXISTS (select * from TBTurmaTraducoes)
Begin
	Insert Into TBTurmaTraducoes
	(IDTurma, IDLinguagem, Descricao, Resumo, MensagemFinalSalvar,
	Informacoes,
	Observacoes,
	Justificativa,
	Objetivos,
	Metodologia,
	Infraestrutura,
	EmentaDisciplina,
	Investimento,
	Local,
	OutrasInformacoes,
	Duracao,
	AreaConhecimento,
	PreRequisito,
	PublicoAlvo,
	FormaPagamento,
	DevolucaoInscricao)
	Select
		T.IDTurma,
		1,
		T.Nometurma,
		SCES.Resumo,
		T.MensagemFinalSalvar,
		SCE.Informacoes,
		SCE.Observacoes,
		SCE.Justificativa,
		SCE.Objetivos,
		SCE.Metodologia,
		SCE.Infraestrutura,
		SCE.EmentaDisciplina,
		SCE.Investimento,
		SCE.Local,
		SCE.OutrasInformacoes,
		SCE.Duracao,
		SCE.AreaConhecimento,
		SCE.PreRequisito,
		SCE.PublicoAlvo,
		SCE.FormaPagamento,
		SCE.DevolucaoInscricao
	From  TBSolicitacaoCursoEventoServico SCES
	inner Join TBTurma T
	on SCES.IDTurma = T.IDTurma
	inner join TBSolicitacaoCursoEvento SCE
	on SCES.IdSolicitacaoCursoEventoServico = SCE.IdSolicitacaoCursoEventoServico
END

GO --

IF EXISTS (SELECT * FROM sys.columns s
				left join sys.types t
				on s.system_type_id = t.system_type_id
				WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND s.name IN ('TipoQuadroUSP') and t.name in ('bit'))
Begin
	Alter Table TBSolicitacaoBolsa 
	alter column TipoQuadroUSP tinyint null

	Alter table TBSolicitacaoBolsa
	add PertenceQuadroUSP bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoCursoEventoServico') AND name IN ('MensagemFinalSalvar'))
Begin
	Alter Table TBSolicitacaoCursoEventoServico
		Add MensagemFinalSalvar varchar(max)
End

GO --

IF NOT EXISTS (select * from sysobjects where name='TBSolicitacaoCursoEventoServicoTraducoes' and xtype='U')
Begin
	CREATE TABLE TBSolicitacaoCursoEventoServicoTraducoes(
		IDSolicitacaoCursoEventoServicoTraducoes int IDENTITY(1,1) NOT NULL,
		IdSolicitacaoCursoEventoServico int NULL,
		IDLinguagem int null,
		Descricao varchar(max) NULL,
		Resumo varchar(max) NULL,
		DescricaoDoacao varchar(max) NULL,
		MensagemFinalSalvar varchar(max) NULL,
		Informacoes varchar(max) NULL,
		Observacoes varchar(max) NULL,
		Justificativa varchar(max) NULL,
		Objetivos varchar(max) NULL,
		Metodologia varchar(max) NULL,
		Infraestrutura varchar(max) NULL,
		EmentaDisciplina varchar(max) NULL,
		Investimento varchar(max) NULL,
		Local varchar(max) NULL,
		OutrasInformacoes varchar(max) NULL,
		Duracao varchar(max) NULL,
		AreaConhecimento varchar(max) null,
		PreRequisito varchar(max) null,
		PublicoAlvo varchar(max) null,
		FormaPagamento varchar(max) null,
		DevolucaoInscricao varchar(max) null,
    CONSTRAINT PK_TBSolicitacaoCursoEventoServicoTraducoes PRIMARY KEY CLUSTERED 
	(
		IDSolicitacaoCursoEventoServicoTraducoes ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
	

	ALTER TABLE TBSolicitacaoCursoEventoServicoTraducoes  WITH CHECK ADD  CONSTRAINT FK_TBSolicitacaoCursoEventoServicoTraducoes_TBSolicitacaoCursoEventoServico FOREIGN KEY(IdSolicitacaoCursoEventoServico)
	REFERENCES TBSolicitacaoCursoEventoServico(IdSolicitacaoCursoEventoServico)
	
	ALTER TABLE TBSolicitacaoCursoEventoServicoTraducoes CHECK CONSTRAINT FK_TBSolicitacaoCursoEventoServicoTraducoes_TBSolicitacaoCursoEventoServico
End

GO --

IF NOT EXISTS (select * from TBSolicitacaoCursoEventoServicoTraducoes)
Begin
	Insert Into dbo.TBSolicitacaoCursoEventoServicoTraducoes
	(IdSolicitacaoCursoEventoServico, 
	IDLinguagem, 
	Descricao, 
	Resumo, 
	MensagemFinalSalvar,
	Informacoes,
	Observacoes,
	Justificativa,
	Objetivos,
	Metodologia,
	Infraestrutura,
	EmentaDisciplina,
	Investimento,
	Local,
	OutrasInformacoes,
	Duracao,
	AreaConhecimento,
	PreRequisito,
	PublicoAlvo,
	FormaPagamento,
	DevolucaoInscricao)
	Select
		SCES.IdSolicitacaoCursoEventoServico,
		1,
		SCES.Descricao,
		SCES.Resumo,
		SCES.MensagemFinalSalvar,
		SCE.Informacoes,
		SCE.Observacoes,
		SCE.Justificativa,
		SCE.Objetivos,
		SCE.Metodologia,
		SCE.Infraestrutura,
		SCE.EmentaDisciplina,
		SCE.Investimento,
		SCE.Local,
		SCE.OutrasInformacoes,
		SCE.Duracao,
		SCE.AreaConhecimento,
		SCE.PreRequisito,
		SCE.PublicoAlvo,
		SCE.FormaPagamento,
		SCE.DevolucaoInscricao
	From  TBSolicitacaoCursoEventoServico SCES
	inner join TBSolicitacaoCursoEvento SCE
	on SCES.IdSolicitacaoCursoEventoServico = SCE.IdSolicitacaoCursoEventoServico
	where Sces.TipoSolicitacao in (1,2,3)

	Insert Into TBSolicitacaoCursoEventoServicoTraducoes
	(IdSolicitacaoCursoEventoServico, IDLinguagem, Descricao, Resumo, DescricaoDoacao, MensagemFinalSalvar)
	Select
		SCES.IdSolicitacaoCursoEventoServico, 1, Descricao, Resumo, DescricaoDoacao, SCES.MensagemFinalSalvar
	From  TBSolicitacaoCursoEventoServico SCES
	inner join TBSolicitacaoDoacao SCE
	on SCES.IdSolicitacaoCursoEventoServico = SCE.IDSolicitacaoCursoEventoServico
	where Sces.TipoSolicitacao in (4)
END

GO -- 

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('PayPalID'))
Begin
	Alter Table TBLancamento add 
		PayPalID varchar(max) null
End

GO --

IF Not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ClientIDPayPal'))
Begin
	Alter table TBConfiguracaoWeb add ClientIDPayPal varchar(max) null
End

GO --

IF Not EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('TokenPayPal'))
Begin
	Alter table TBConfiguracaoWeb add TokenPayPal varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('GoogleTranslateAPIKey'))
Begin
	Alter table TBConfiguracaoWeb 
	add HabilitarGoogleTranslateCoordenador bit null,
	GoogleTranslateAPIKey varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacao') AND name IN ('FuncionarioPublico'))
Begin
	Alter Table TBSolicitacao add 
		FuncionarioPublico bit null,
		Cargo varchar(max) null,
		Departamento varchar(max) null,
		CNPJInstituicaoPublica varchar(max) null,
		InstituicaoPublica varchar(max) null,
		CargaHorariaInstituicao varchar(max) null,
	    Unidade varchar(max) null,
	    TipoQuadroUSP tinyint null,
		PertenceQuadroUSP bit null,
		TipoSolicitacaoPagamento tinyint null,
		RegimeTrabalhoUSP tinyint null,
		pessoaAssinaturaTermo varchar(max) null,
		cpfAssinaturaTermo varchar(max) null,
		dataAssinaturaTermo datetime null,
		SolicitacaoJustificativaAnexo varchar(max)  null,
		SolicitacaoAnexouArquivo bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('PayPalEstrangeiro'))
Begin
	Alter Table TBDoacao
		Add PayPalEstrangeiro bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('PayPalEstrangeiro'))
Begin
	Alter Table TBTurma
		Add PayPalEstrangeiro bit null,
		PayPalEstrangeiroMensalidade bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('PayPalStatus'))
Begin
	Alter Table TBLancamento add 
		PayPalStatus varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBInscricao') AND name IN ('LinguagemSelecionada'))
Begin
	Alter Table TBInscricao add 
		LinguagemSelecionada varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('ObrigatorioSelecionarProduto'))
Begin
	Alter table TBConfiguracaoWeb 
	add ObrigatorioSelecionarProduto bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoBolsa') AND name IN ('NomeSocial'))
Begin
	Alter table TBSolicitacaoBolsa add NomeSocial varchar(max) NULL;
	Alter table TBPessoaFisicaJuridica add NomeSocial varchar(max) NULL;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'UFPR_TBAluno') AND name IN ('DocumentoContrato'))
Begin
	Alter table UFPR_TBAluno add 
		DocumentoContrato varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('IDContaTarifaPayPal'))
Begin
	Alter table TBProjeto add IDContaTarifaPayPal int, IDRubProjetoTarifaPayPal int;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoServicosAviso') AND name IN ('ControleTransacaoPayPal'))
Begin
	Alter table TBConfiguracaoServicosAviso add 
		ControleTransacaoPayPal bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('PayPalTarifa'))
Begin
	Alter Table TBLancamento add 
		PayPalTarifa float null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBDoacao') AND name IN ('EnviarEmailRecebimento'))
Begin
	Alter Table TBDoacao
		Add EnviarEmailRecebimento bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBTurma') AND name IN ('EnviarEmailRecebimento'))
Begin
	Alter Table TBTurma
		Add EnviarEmailRecebimento bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBLancamento') AND name IN ('EnviarEmailInscricao'))
Begin
	Alter Table TBLancamento add 
		EnviarEmailInscricao tinyint null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoServicosAviso') AND name IN ('ProcessarEnviarEmailInscricao'))
Begin
	Alter table TBConfiguracaoServicosAviso add 
		ProcessarEnviarEmailInscricao bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('HabilitarComprasInternas'))
Begin
	Alter table TBProjeto add 
		HabilitarComprasInternas bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProduto') AND name IN ('HabilitarMateriaisConsumo'))
Begin
	Alter table TBProduto add 
		HabilitarMateriaisConsumo bit  null,
		HabilitarLivros bit null,
		HabilitarEquipamentos bit null,
		HabilitarContratacoesServico bit null,
		HabilitarLocacaoVeiculo bit null,
		HabilitarReservaHotel bit null,
		HabilitarImportacao bit null,
		HabilitarComprasOnline bit null,
		HabilitarComprasInternas bit null
End


IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRubricaProjeto') AND name IN ('RubSubrubBolsa'))
Begin
	Alter table TBRubricaProjeto add 
		RubSubrubBolsa bit  null,
		RubSubrubRPA bit  null,
		RubSubrubAdicional bit  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBCategoriaFinanceira') AND name IN ('CategoriaBolsa'))
Begin
	Alter table TBCategoriaFinanceira add 
		CategoriaBolsa bit  null,
		CategoriaRPA bit  null,
		CategoriaAdicional bit  null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBSolicitacaoImportacao') AND name IN ('DescricaoImportacaoProjeto'))
Begin
	Alter table TBSolicitacaoImportacao add 
		DescricaoImportacaoProjeto varchar(max)  null 
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('MensagemAvisoGeral'))
Begin
	Alter table TBConfiguracaoWeb 
	add MensagemAvisoGeral varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPessoaFisicaJuridica') AND name IN ('FuncionarioPublico'))
Begin
	Alter Table TBPessoaFisicaJuridica add 
		FuncionarioPublico bit null,
		PertenceQuadroUSP bit null,
		TipoQuadroUSP tinyint null,
		--RegimeTrabalhoUSP tinyint null,
		Cargo varchar(max) null,
		Unidade varchar(max) null,
		Departamento varchar(max) null,
		CNPJInstituicaoPublica varchar(max) null,
		InstituicaoPublica varchar(max) null,
		CargaHorariaInstituicao varchar(max) null
End

GO --

IF NOT EXISTS (select * from sysobjects where name='TBLancamentoDadosPessoa' and xtype='U')
Begin
	CREATE TABLE TBLancamentoDadosPessoa(
		IDLancamentoDadosPessoa int IDENTITY(1,1) NOT NULL,
		IdLancamento int NULL,
		DataCriacao datetime null,
		FuncionarioPublico bit null,
		PertenceQuadroUSP bit null,
		TipoQuadroUSP tinyint null,
		RegimeTrabalhoUSP tinyint null,
		Cargo varchar(max) null,
		Unidade varchar(max) null,
		Departamento varchar(max) null,
		CNPJInstituicaoPublica varchar(max) null,
		InstituicaoPublica varchar(max) null,
		CargaHorariaInstituicao varchar(max) null,
		Matricula varchar(max) null,
		SIAPE varchar(max) null,
		Quadro int null,
		TipoQuadro smallint null,
		CONSTRAINT PK_TBLancamentoDadosPessoa PRIMARY KEY CLUSTERED 
		(
			IDLancamentoDadosPessoa ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('ProjetoInterno'))
Begin
	Alter table TBProjeto add 
		ProjetoInterno bit null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBPedidoCompra') AND name IN ('InformacaoRastreio'))
Begin
	Alter table TBPedidoCompra add 
		InformacaoRastreio varchar(max) null,
		NumeroPedidoECommerce varchar(max) null
End

GO --

IF EXISTS (SELECT * FROM sys.columns s
				left join sys.types t
				on s.system_type_id = t.system_type_id
				WHERE  OBJECT_ID = OBJECT_ID(N'TBAuxLancamento') AND s.name IN ('IdCodigoExterno') and t.name in ('int'))
Begin
	IF EXISTS (SELECT Name, * FROM sys.indexes WHERE Name = 'IX_IDCodigoExterno') Drop index IX_IDCodigoExterno on TBAuxLancamento

	Alter Table TBAuxLancamento 
	alter column IdCodigoExterno varchar(max) null
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('RealizarPagamentoProvisoes'))
Begin
	Alter Table TBProjeto
		Add RealizarPagamentoProvisoes bit	default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBRemotoProjetoPerfil') AND name IN ('LiberarProvisoesPagamento'))
Begin
	alter table TBRemotoProjetoPerfil add LiberarProvisoesPagamento bit;
	alter table TBRemotoProjeto add LiberarProvisoesPagamento bit;
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBProjeto') AND name IN ('LiberarPagamentoProvisaoPelaData'))
Begin
	Alter Table TBProjeto
		Add LiberarPagamentoProvisaoPelaData bit	default 0
End

GO --

IF NOT EXISTS (SELECT * FROM sys.columns WHERE  OBJECT_ID = OBJECT_ID(N'TBConfiguracaoWeb') AND name IN ('DiaLimiteLiberacaoProvisao'))
Begin
	Alter table TBConfiguracaoWeb 
	add DiaLimiteLiberacaoProvisao int null,
	PrazoPagamentoProvisao int null
End

GO --
