--
-- $Id$
--
-- <<Korte omschrijving wat het script doet>>

DEFINE thisfile = 'TWYN0.SQL'
DEFINE dbuser   = '^TWYN'

SET ECHO ON
SET DEFINE ON
COLUMN fcltlogfile NEW_VALUE fcltlogfile NOPRINT;
WHENEVER SQLERROR EXIT;
SELECT adm.scriptspoolfile('&dbuser', '&thisfile') AS fcltlogfile FROM DUAL;
WHENEVER SQLERROR CONTINUE;
SPOOL &fcltlogfile
SET DEFINE OFF

------ payload begin ------

INSERT INTO fac_import_app
(fac_import_app_prefix, fac_import_app_code, fac_import_app_oms, fac_functie_key)
SELECT 'TWYN', 'reportdbf',  'Conversie BCS naar BGT', fac_functie_key
  FROM fac_functie
 WHERE fac_functie_code = 'WEB_PRSSYS';

INSERT INTO fac_import_app
(fac_import_app_prefix, fac_import_app_code, fac_import_app_oms, fac_functie_key)
SELECT 'TWYN', 'projectcsv', 'Default project inrichting', fac_functie_key
  FROM fac_functie
 WHERE fac_functie_code = 'WEB_PRSSYS';

INSERT INTO fac_usrtab
(fac_usrtab_naam, fac_usrtab_object, fac_usrtab_omschrijving, fac_functie_key)
SELECT 'project_import', 'USR_PROJECT_IMPORT', 'Default project indeling', fac_functie_key
  FROM fac_functie
 WHERE fac_functie_code = 'WEB_BGTMGT';

UPDATE fac_setting
   SET fac_setting_pvalue = '1'
 WHERE fac_setting_name = 'bgt_enabled';

INSERT INTO ins_srtdiscipline (ins_srtdiscipline_module, ins_srtdisciplineomschrijving, ins_srtdiscipline_alg, ins_srtdiscipline_ins, ins_srtdiscipline_bes)
VALUES ('MLD', 'Budgetcontrole', 0, NULL, NULL);

-- Tijdelijk: de url aanpassen. Komt uiteindelijk wel met fac_ini.src mee.
--UPDATE fac_menuitems
--   SET fac_menuitems_url = REPLACE(fac_menuitems_url, 'bgt_discipline.asp', 'bgt_search.asp')
-- WHERE fac_menuitems_label = 'lcl_menu_bgt_discipline_1';

CREATE TABLE twyn_imp_discipline
(
    bgt_disc_params_code            VARCHAR2(20),
    ins_discipline_omschrijving     VARCHAR2(30),
    bgt_disc_params_opdrachtgever   VARCHAR2(200),
    bgt_disc_params_startdatum      DATE,
    ins_discipline_btw              NUMBER(1),
    bgt_disc_params_leider          VARCHAR2(4)
);

CREATE TABLE twyn_imp_project
(
    bgt_project_discipline          VARCHAR2(20),
    bgt_project_code                VARCHAR2(20),
    bgt_project_omschrijving        VARCHAR2(60),
    bgt_project_volgnr              NUMBER(3),
    bgt_project_prijspeildatum      DATE,
    bgt_project_notes               VARCHAR2(4000),
    bgt_project_fase_nr             NUMBER(3),
    bgt_project_fase_omschr         VARCHAR2(20),
    bgt_project_soort_nr            NUMBER(3),
    bgt_project_soort_omschr        VARCHAR2(20)
);

CREATE TABLE twyn_imp_kostenrubriek
(
   bgt_project_discipline           VARCHAR2(20),
   bgt_project_code                 VARCHAR2(20),
   bgt_kostenrubriek_code           VARCHAR2(20),
   bgt_kostenrubriek_oms            VARCHAR2(60)
);

CREATE TABLE twyn_imp_kostensoortgrp
(
   bgt_project_discipline           VARCHAR2(20),
   bgt_project_code                 VARCHAR2(20),
   bgt_kostenrubriek_code           VARCHAR2(20),
   prs_kostensoortgrp_altcode       VARCHAR2(60),
   prs_kostensoortgrp_oms           VARCHAR2(60)
);

CREATE TABLE twyn_imp_kostensoort
(
   bgt_project_discipline           VARCHAR2(20),
   bgt_project_code                 VARCHAR2(20),
   bgt_kostenrubriek_code           VARCHAR2(20),
   prs_kostensoortgrp_altcode       VARCHAR2(60),
   prs_kostensoort_oms              VARCHAR2(10),
   prs_kostensoort_opmerking        VARCHAR2(60),
   bgt_btw_code                     VARCHAR2(20),
   bgt_btw_waarde                   NUMBER(2)
);

CREATE TABLE twyn_imp_budget
(
   key                              NUMBER(10),
   bgt_budget_key                   NUMBER(10),
   bgt_nr                           VARCHAR2(5),
   bgt_oms                          VARCHAR2(60),
   bgt_discipline                   VARCHAR2(20),
   bgt_project                      VARCHAR2(20),
   bgt_kostenrubriek                VARCHAR2(20),
   bgt_kostensoortgrp               VARCHAR2(60),
   bgt_kostensoort                  VARCHAR2(5),
   bgt_meerminder                   NUMBER(1),
   bgt_datum                        DATE,
   bedr_init_excl                   NUMBER(12,2),
   bedr_init_btw                    NUMBER(12,2),
   bedr_res_excl                    NUMBER(12,2),
   bedr_res_btw                     NUMBER(12,2),
   bedr_mmw_excl                    NUMBER(12,2),
   bedr_mmw_btw                     NUMBER(12,2),
   bedr_mut_excl                    NUMBER(12,2),
   bedr_mut_btw                     NUMBER(12,2),
   bedr_res_mut_excl                NUMBER(12,2),
   bedr_res_mut_btw                 NUMBER(12,2),
   reserve                          NUMBER(12,2),
   reserve2                         NUMBER(12,2),
   type                             VARCHAR2(1),
   status                           NUMBER(3),
   kostensoort                      VARCHAR2(2),
   soortnr                          NUMBER(3)
);

CREATE TABLE twyn_imp_contract
(
   bgt_contract_nr                  NUMBER(5),
   bgt_contract_volgnr              NUMBER(5),
   bgt_contract_meerminder          NUMBER(1),
   bgt_bedrijf_code                 VARCHAR2(4),
   bgt_contract_omschr              VARCHAR2(60),
   bgt_contract_datum               DATE,
   bgt_contract_bedrag_excl         NUMBER(12,2),
   bgt_contract_bedrag_btw          NUMBER(12,2),
   bgt_contract_bedrag_incl         NUMBER(12,2),
   bgt_contract_status              NUMBER(2),
   bgt_contract_referentie          VARCHAR2(20),
   bgt_discipline_code              VARCHAR2(20),
   bgt_project_code                 VARCHAR2(20),
   bgt_kostenrubriek_code           VARCHAR2(20),
   prs_kostensoortgrp_code          VARCHAR2(60),
   prs_kostensoort_code             VARCHAR2(5),
   prs_kostenplaats_nr              NUMBER(10),
   prs_kostenplaats_oms             VARCHAR2(60)
);

CREATE TABLE twyn_imp_factuur
(
   bgt_factuur_nr                   NUMBER(10),
   bgt_factuur_volgnr               NUMBER(10),
   bgt_factuur_refnr                VARCHAR2(12),
   bgt_factuur_omschr               VARCHAR2 (60),
   bgt_factuur_datum                DATE,
   bgt_factuur_bedrag_excl          NUMBER(12,2),
   bgt_factuur_bedrag_btw           NUMBER(12,2),
   bgt_factuur_bedrag_incl          NUMBER(12,2),
   bgt_factuur_adviesdatum          DATE,
   bgt_factuur_grootboek            NUMBER(10),
   bgt_factuur_status               VARCHAR2(20),
   bgt_contract_nr                  NUMBER(5),
   bgt_contract_volgnr              NUMBER(5),
   bgt_contract_referentie          VARCHAR2(20),
   bgt_discipline_code              VARCHAR2(20),
   bgt_project_code                 VARCHAR2(20),
   bgt_kostenrubriek_code           VARCHAR2(20),
   prs_kostensoortgrp_code          VARCHAR2(60),
   prs_kostensoort_code             VARCHAR2(5)
);

CREATE TABLE twyn_bgtlog
(
   volgnr    NUMBER(10),
   actie     VARCHAR2(20),
   loglevel  VARCHAR2(3),
   datum     DATE,
   msg       VARCHAR2(2000),
   disc_key  NUMBER(10) DEFAULT NULL,
   prj_key   NUMBER(10) DEFAULT NULL,
   rub_key   NUMBER(10) DEFAULT NULL,
   grp_key   NUMBER(10) DEFAULT NULL,
   srt_key   NUMBER(10) DEFAULT NULL,
   bgt_key   NUMBER(10) DEFAULT NULL,
   mut_key   NUMBER(10) DEFAULT NULL,
   opdr_key  NUMBER(10) DEFAULT NULL,
   fact_key  NUMBER(10) DEFAULT NULL,
   bedr_key  NUMBER(10) DEFAULT NULL,
   kpls_key  NUMBER(10) DEFAULT NULL,
   kgrp_key  NUMBER(10) DEFAULT NULL
);

INSERT INTO fac_menu (fac_menu_volgnr, fac_menu_altgroep, fac_menuitems_key)
     SELECT 6910, 0, fac_menuitems_key FROM fac_menuitems WHERE fac_menuitems_label = 'lcl_menu_bgt_account';

INSERT INTO fac_menu (fac_menu_volgnr, fac_menu_altgroep, fac_menuitems_key)
     SELECT 6911, 0, fac_menuitems_key FROM fac_menuitems WHERE fac_menuitems_label = 'lcl_menu_bgt_bedrijf';

INSERT INTO fac_menu (fac_menu_volgnr, fac_menu_altgroep, fac_menuitems_key)
     SELECT 6912, 0, fac_menuitems_key FROM fac_menuitems WHERE fac_menuitems_label = 'lcl_menu_bgt_budget';

INSERT INTO fac_menu (fac_menu_volgnr, fac_menu_altgroep, fac_menuitems_key)
     SELECT 6913, 0, fac_menuitems_key FROM fac_menuitems WHERE fac_menuitems_label = 'lcl_menu_bgt_opdracht';

INSERT INTO fac_menu (fac_menu_volgnr, fac_menu_altgroep, fac_menuitems_key)
     SELECT 6914, 0, fac_menuitems_key FROM fac_menuitems WHERE fac_menuitems_label = 'lcl_menu_bgt_factuur';

INSERT INTO fac_menu (fac_menu_volgnr, fac_menu_altgroep, fac_menuitems_key)
     SELECT 6915, 0, fac_menuitems_key FROM fac_menuitems WHERE fac_menuitems_label = 'lcl_menu_bgt_rapport';

------ payload end   ------

SET DEFINE OFF
BEGIN adm.systrackscriptId ('$Id$', 1); END;
/

COMMIT;
SET ECHO OFF
SPOOL OFF
SET DEFINE ON
PROMPT Logfile of this upgrade is: &fcltlogfile
