-- Merge com a versão 1.15.9.0
--Fabiano, alterações para MG ficar mais rápido
update RDB$RELATION_FIELDS set RDB$COLLATION_ID = 6 where (RDB$FIELD_NAME = 'BDTELARP') and (RDB$RELATION_NAME = 'TRELATORIOS_PERSONALIZADOS');
CREATE INDEX TRELATORIOS_PERSONALIZADOS_IDX1 ON TRELATORIOS_PERSONALIZADOS (BDTELARP);
update RDB$RELATION_FIELDS set RDB$COLLATION_ID = 6 where (RDB$FIELD_NAME = 'BDFORMULARIO') and(RDB$RELATION_NAME = 'TSWS_TFORMREL');
CREATE INDEX TSWS_TFORMREL_IDX1 ON TSWS_TFORMREL (BDFORMULARIO);
update RDB$RELATION_FIELDS set RDB$COLLATION_ID = 6 where (RDB$FIELD_NAME = 'BDFORMCONREL') and (RDB$RELATION_NAME = 'TCONFIG_RELATORIOS');
update RDB$RELATION_FIELDS set RDB$COLLATION_ID = 6 where (RDB$FIELD_NAME = 'BDMODELOREL') and (RDB$RELATION_NAME = 'TCONFIG_RELATORIOS');
CREATE INDEX TCONFIG_RELATORIOS_IDX1 ON TCONFIG_RELATORIOS (BDFORMCONREL,BDMODELOREL);
CREATE DESCENDING INDEX PLANOS_TPLA_IDX2 ON PLANOS_TPLA (BDCODPLAPADRAO, BDNIVEL);
CREATE INDEX TSELECOESEMP_IDX1 ON TSELECOESEMP (BDCODSIS, BDCODUSU);
CREATE INDEX TLIVRO_IDX1 ON TLIVRO (BDCODEMP,BDCODREL);
CREATE INDEX EMPRESAS_TSOC_IDX1 ON EMPRESAS_TSOC (BDCODEMP,BDRESPONSAVELSOC);
CREATE INDEX PLANOS_TPLA_IDX3 ON PLANOS_TPLA (BDCODPLAPADRAO,BDTIPCTA);
CREATE INDEX VSUC_EMPRESAS_TBALANCETE_3_IDX2 ON VSUC_EMPRESAS_TBALANCETE_3 (BDCODEMP,BDCODPLAPADRAO,BDCODTPLA);

--alt:6178 - 10/11/2015 - Altera a quantidade de casas depois da vírgula para a tabela temporária utilizada na soma dos saldos do Balancete. Pedro
update RDB$FIELDS set RDB$FIELD_SCALE = -4 where RDB$FIELD_NAME = (select RDB$FIELD_SOURCE from RDB$RELATION_FIELDS where RDB$RELATION_NAME = 'VSUC_EMP_TSALDOS' and RDB$FIELD_NAME = 'BDVALOR');

--Sol: 12773 Patrick T. Sá 19/11/2015. Criada tabela temporária para a baixa coletiva de parcelamento, select estava muito grande e não executava.
CREATE GLOBAL TEMPORARY TABLE TMP_TBAIXAPARC (
  BDCHAVE    INTEGER NOT NULL,
  BDORDEMPAR INTEGER NOT NULL,
  BDCODEMP   INTEGER,
  BDTIPOLAN  INTEGER
) ON COMMIT PRESERVE ROWS;
ALTER TABLE TMP_TBAIXAPARC ADD CONSTRAINT PK_BDCHAVE PRIMARY KEY (BDCHAVE, BDORDEMPAR);
--  Fim Sol: 12773.

-- Alt.: 11996 - Criada tabela temporária dos demais documentos e ajustado os campos de CST de PIS e Cofins para NULL
CREATE GLOBAL TEMPORARY TABLE TMP_VEF_EMP_TMOVDEMAISDOC_F100 (
    BDCODEMP         INTEGER NOT NULL,
    BDCHAVE          INTEGER NOT NULL,
    BDIND_ORIG_CRED  INTEGER,
    BDCODCSTPIS      INTEGER NOT NULL,
    BDCODCSTCOF      INTEGER NOT NULL,
    BDNAT_BC_CRED    INTEGER,
    BDCODTER         INTEGER,
    BDCODPROD        INTEGER,
    BDIND_OPER       INTEGER NOT NULL,
    BDDT_OPER        DATE,
    BDVL_OPER        NUMERIC(15,2),
    BDVL_BC_PIS      NUMERIC(15,2),
    BDALIQ_PIS       NUMERIC(15,4),
    BDVLR_PIS        NUMERIC(15,2),
    BDVL_BC_COFINS   NUMERIC(15,2),
    BDALIQ_COFINS    NUMERIC(15,4),
    BDVLR_COFINS     NUMERIC(15,2),
    BDDESC_DOC_OPER  CHAR(250),
    BDCODCENTRO      INTEGER,
    BDCODTPCC        INTEGER,
    BDCODTIPO        INTEGER,
    BDCODNATREC      INTEGER,
    BDCODTIPODOC     INTEGER,
    BDCONDPGTO       INTEGER,
    BDNUMDOC         INTEGER,
    BDREFLAN         INTEGER,
    BDCODSTATUSLAN   INTEGER,
    BDDATALAN        DATE,
    BDHORALAN        TIME,
    BDCODUSU         INTEGER,
    BDDATAINCLUSAO   DATE,
    BDHORAINCLUSAO   TIME,
    BDCODUSUINCLU    INTEGER,
    BDTIPOLANINCLU   INTEGER,
    BDORDENATVPREV   INTEGER
) ON COMMIT PRESERVE ROWS;
CREATE INDEX TMP_VEF_EMP_TMOVDEM_IDX1 ON TMP_VEF_EMP_TMOVDEMAISDOC_F100 (BDCODEMP, BDCHAVE);

update RDB$RELATION_FIELDS set RDB$NULL_FLAG = NULL where (RDB$FIELD_NAME = 'BDCODCSTPIS') and (RDB$RELATION_NAME = 'VEF_EMP_TMOVDEMAISDOC_F100');
update RDB$RELATION_FIELDS set RDB$NULL_FLAG = NULL where (RDB$FIELD_NAME = 'BDCODCSTCOF') and (RDB$RELATION_NAME = 'VEF_EMP_TMOVDEMAISDOC_F100');
update RDB$RELATION_FIELDS set RDB$NULL_FLAG = NULL where (RDB$FIELD_NAME = 'BDCODCSTPIS') and (RDB$RELATION_NAME = 'TMP_VEF_EMP_TMOVDEMAISDOC_F100');
update RDB$RELATION_FIELDS set RDB$NULL_FLAG = NULL where (RDB$FIELD_NAME = 'BDCODCSTCOF') and (RDB$RELATION_NAME = 'TMP_VEF_EMP_TMOVDEMAISDOC_F100');
commit work;
-- Fim: 11996

--Sol: 12413 Patrick T. Sá. Tabelas temporárias para importação do Bloco K.
CREATE GLOBAL TEMPORARY TABLE TMP_VEF_EMP_TMOVK200 (
BDCODEMP             INTEGER                        NOT NULL,
BDCHAVE              INTEGER                        NOT NULL,
BDCODPROD            INTEGER,
BDCODTER             INTEGER,
BDCODPOSSE           INTEGER,
BDDATAESTFIM         DATE,
BDQTDESTOQUE         NUMERIC(17,3),
BDREFLAN             INTEGER,
CONSTRAINT PK_TMP_VEF_EMP_TMOVK200 PRIMARY KEY (BDCHAVE, BDCODEMP)
)ON COMMIT PRESERVE ROWS;

CREATE GLOBAL TEMPORARY TABLE TMP_VEF_EMP_TMOVESTOQUEK220 (
BDCODEMP             INTEGER                        NOT NULL,
BDCHAVE              INTEGER                        NOT NULL,
BDCODPRODORI         INTEGER,
BDCODPRODDEST        INTEGER,
BDCODSAIDAORI        VARCHAR(60),
BDCODSAIDADEST       VARCHAR(60),
BDDATAMOV            DATE,
BDQTDMOV             NUMERIC(17,3),
BDREFLAN             INTEGER,
CONSTRAINT PK_TMP_VEF_EMP_TMOVESTOQUEK220 PRIMARY KEY (BDCHAVE, BDCODEMP)
)ON COMMIT PRESERVE ROWS;

CREATE GLOBAL TEMPORARY TABLE TMP_VEF_EMP_TMOVK230_K250 (
BDCODEMP             INTEGER                        NOT NULL,
BDCODPROD            INTEGER                        NOT NULL,
BDCHAVE              INTEGER                        NOT NULL,
BDCODTER             INTEGER,
BDINDUSTRIALIZADOCAO INTEGER,
BDORDEMPROD          VARCHAR(30),
BDDATAPRODINI        DATE,
BDDATAPRODFIN        DATE,
BDQTDPROD            NUMERIC(17,3),
BDREFLAN             INTEGER,
CONSTRAINT PK_TMP_VEF_EMP_TMOVK230_K250 PRIMARY KEY (BDCHAVE, BDCODEMP, BDCODPROD)
)ON COMMIT PRESERVE ROWS;

--Fim Sol: 12413.

--alt:4936 - 16/11/2015 - Criadas tabelas para impressão de relatórios em outros idiomas. Pedro
create table PLANOS_TFOR_IDIOMAS (
BDCODIDIOMA          INTEGER                        not null,
BDCODPFOR            INTEGER                        not null,
BDCODFOR             INTEGER                        not null,
BDNOMCTAIDIOMA       VARCHAR(100),
constraint PK_PLANOS_TFOR_IDIOMAS primary key (BDCODPFOR, BDCODIDIOMA, BDCODFOR)
);

create table PLANOS_TPCC_IDIOMAS (
BDCODIDIOMA          INTEGER                        not null,
BDCODCENTRO          INTEGER                        not null,
BDCODTPCC            INTEGER                        not null,
BDNOMCTAIDIOMA       VARCHAR(100),
constraint PK_PLANOS_TPCC_IDIOMAS primary key (BDCODCENTRO, BDCODIDIOMA, BDCODTPCC)
);

create table PLANOS_TPLA_IDIOMAS (
BDCODIDIOMA          INTEGER                        not null,
BDCODPLAPADRAO       INTEGER                        not null,
BDCODTPLA            INTEGER                        not null,
BDNOMCTAIDIOMA       VARCHAR(100),
constraint PK_PLANOS_TPLA_IDIOMAS primary key (BDCODPLAPADRAO, BDCODIDIOMA, BDCODTPLA)
);

create table TCONF_ITENS_IDIOMAS (
BDCODIDIOMA          INTEGER                        not null,
BDCODTIPODESCIDIOMA  INTEGER                        not null,
BDDESCRICAO          VARCHAR(100),
BDDESCABREVIADA      VARCHAR(50),
constraint PK_TCONF_ITENS_IDIOMAS primary key (BDCODIDIOMA, BDCODTIPODESCIDIOMA)
);

create table TIDIOMAS (
BDCODIDIOMA          INTEGER                        not null,
BDDESCRICAO          VARCHAR(100),
BDMOSTRADESCORIGINAL DM_BOOLEAN,
constraint PK_TIDIOMAS primary key (BDCODIDIOMA)
);

create table TTIPODESCIDIOMAS (
BDCODTIPODESCIDIOMA  INTEGER                        not null,
BDCLASSIFICACAO      VARCHAR(100),
BDCONSTANTE          VARCHAR(30),
BDDESCRICAO          VARCHAR(100),
BDCONFIGURA          DM_BOOLEAN,
BDPERMITEABREVIADO   DM_BOOLEAN,
constraint PK_TTIPODESCIDIOMAS primary key (BDCODTIPODESCIDIOMA)
);

alter table PLANOS_TFOR_IDIOMAS add constraint FK_PLANOS_T_FKSUC0010_TIDIOMAS foreign key (BDCODIDIOMA) references TIDIOMAS (BDCODIDIOMA);
alter table PLANOS_TFOR_IDIOMAS add constraint FK_PLANOS_T_FKSUC0011_VSUC_BAS foreign key (BDCODPFOR, BDCODFOR) references VSUC_BASE_TFORMULA_TPFO (BDCODPFOR, BDCODFOR);
alter table PLANOS_TPCC_IDIOMAS add constraint FK_PLANOS_T_FKSUC0012_TIDIOMAS foreign key (BDCODIDIOMA) references TIDIOMAS (BDCODIDIOMA);
alter table PLANOS_TPCC_IDIOMAS add constraint FK_PLANOS_T_FKSUC0013_CENTROS_ foreign key (BDCODCENTRO, BDCODTPCC) references CENTROS_TPCC (BDCODCENTRO, BDCODTPCC);
alter table PLANOS_TPLA_IDIOMAS add constraint FK_PLANOS_T_FKSUC0008_TIDIOMAS foreign key (BDCODIDIOMA) references TIDIOMAS (BDCODIDIOMA);
alter table PLANOS_TPLA_IDIOMAS add constraint FK_PLANOS_T_FKSUC0009_PLANOS_T foreign key (BDCODPLAPADRAO, BDCODTPLA) references PLANOS_TPLA (BDCODPLAPADRAO, BDCODTPLA);
alter table TCONF_ITENS_IDIOMAS add constraint FK_TCONF_IT_FKSUC0006_TIDIOMAS foreign key (BDCODIDIOMA) references TIDIOMAS (BDCODIDIOMA);
alter table TCONF_ITENS_IDIOMAS add constraint FK_TCONF_IT_FKSUC0007_TTIPODES foreign key (BDCODTIPODESCIDIOMA) references TTIPODESCIDIOMAS (BDCODTIPODESCIDIOMA);
--Fim Sol: 12413.

-- Início Solicitação: 12224. Denicio 04/12/2015
ALTER TABLE VRH_EMP_TRESCISAO_PREVIA ADD BDREFPAGRESCISAOPREV INTEGER;
ALTER TABLE VRH_EMP_TRESCISAO_PREVIA ADD BDREFPROJECAOAVISOPREV INTEGER;
-- Fim Solicitação: 12224. Denicio 04/12/2015

--alt:6178 - Criado índices para melhoria de tempo no Balancete em outros idiomas. Pedro
ALTER TABLE TINDVAL ADD BDDATAIND DATE;
CREATE ASCENDING INDEX TINDVAL_IDX_DATA_ASC ON TINDVAL (BDCODIND, BDDATAIND);
SET STATISTICS INDEX TINDVAL_IDX_DATA_ASC;
CREATE DESCENDING INDEX TINDVAL_IDX_DATA_DESC ON TINDVAL (BDCODIND, BDDATAIND);
SET STATISTICS INDEX TINDVAL_IDX_DATA_DESC;
--alt:6178 - Fim
