#ifdef CNT
/*  $Revision$
 *  $Id$
 */

CREATE_TABLE(cnt_typecontract, 0)
(
  cnt_typecontract_key
    NUMBER(10)
    CONSTRAINT cnt_k_cnt_typecontract_key PRIMARY KEY,
  cnt_typecontract_omschrijving
    VARCHAR2(30)
    NOT_NULL(cnt_typecontract_omschrijving, cnt_c_cnt_typecontract_oms)
);
CREATE_TABLE(cnt_termijn, 0)
(
  cnt_termijn_key
    NUMBER(10)
    CONSTRAINT cnt_k_cnt_termijn_key PRIMARY KEY,
  cnt_termijn_omschrijving
    VARCHAR2(30)
    NOT_NULL(cnt_termijn_omschrijving, cnt_c_cnt_termijn_oms),
  cnt_termijn_type
    VARCHAR2(1)
    CHECK(cnt_termijn_type IN ('Y','M','W','D')),
  cnt_termijn_aantal
    NUMBER(3),
  cnt_termijn_volgnummer
    NUMBER(3)
);

CREATE_TABLE(cnt_disc_params, 0)
  ( cnt_disc_params_key
        NUMBER(10)
        CONSTRAINT cnt_k_cnt_disc_params_key PRIMARY KEY,
    cnt_ins_discipline_key
        NUMBER(10)
        NOT_NULL(cnt_ins_discipline_key, cnt_c_ins_discipline_key)
        CONSTRAINT cnt_r_ins_discipine_key REFERENCES ins_tab_discipline(ins_discipline_key),
    cnt_srtcontract_type
        NUMBER(1)
        DEFAULT 1
        NOT_NULL(cnt_srtcontract_type, cnt_c_cnt_srtcontract_type1)
        CONSTRAINT cnt_r_cnt_typecontract_key REFERENCES cnt_typecontract(cnt_typecontract_key),
    cnt_disc_params_opmerking
        VARCHAR2(320),
    cnt_disc_params_verkoopcontr
        NUMBER(1)
        DEFAULT 0
        NOT NULL,
    cnt_disc_params_factuurschema
        NUMBER(1)
        DEFAULT 0
        NOT NULL,
    cnt_disc_params_factuurmarge
        NUMBER(8,2)
        DEFAULT 0,
    cnt_disc_params_factuurappr
        NUMBER(1)
        DEFAULT 0,
    cnt_disc_params_factuurpct
        NUMBER
        DEFAULT 0,
    cnt_disc_params_factuurgrens
        NUMBER(8),
    /* bewust niet, irrelevant voor cnt: cnt_disc_params_factmatchmode    -- 2-weg of 3-weg matching? */
    cnt_disc_params_termijndefault
        NUMBER(10)
        CONSTRAINT cnt_r_cnt_disc_params_tdefault REFERENCES cnt_termijn(cnt_termijn_key),
    cnt_disc_params_fin NUMBER(1)
        DEFAULT 1 NOT NULL,
    cnt_disc_params_binding
        NUMBER(3)
        DEFAULT (3)
        NOT NULL,   /* 2=Persoon 1=Bedrijf 3=Alles*/
    cnt_disc_params_leverancier /* indien Bedrijf: Leveranciers? */
        NUMBER(1)
        DEFAULT 0
        NOT NULL,
    cnt_disc_params_uitvoerende /* indien Bedrijf: Uitvoerenden? */
        NUMBER(1)
        DEFAULT 0
        NOT NULL,
    cnt_disc_params_huurder /* indien Bedrijf: Huurders? */
        NUMBER(1)
        DEFAULT 0
        NOT NULL,
    cnt_disc_params_opties
        NUMBER(1)
        DEFAULT 0
        NOT NULL,
    cnt_disc_params_ksverplicht  /* Kostensoort verplicht {0=niet verplicht (default), 1=verplicht} */
        NUMBER(1)
        DEFAULT 0
        NOT NULL,
    cnt_disc_params_approve_new /* nieuw/gesplitst contract altijd acorderen {0=nee (default), 1=ja} */
        NUMBER(1)
        DEFAULT 0
        NOT NULL
);


CREATE_TABLE(cnt_contract,0)
(  cnt_contract_key
   NUMBER(10)
   CONSTRAINT cnt_k_cnt_contract PRIMARY KEY
,  cnt_contract_nummer
   VARCHAR2(30)
,  cnt_contract_versie
   VARCHAR2(10)
,  cnt_contract_omschrijving
   VARCHAR2(50)
   CONSTRAINT cnt_c_cnt_contract_omschr CHECK(cnt_contract_omschrijving IS NOT NULL)
,  cnt_contract_opmerking
   VARCHAR2(4000)
,  cnt_contract_mantel_key
   NUMBER(10)
   CONSTRAINT cnt_r_contract_mantel_key REFERENCES cnt_contract(cnt_contract_key)
-- Achteraan gezet, anders het het perl-script er moeite mee
--,  cnt_contract_looptijd_van
--   DATE
--   DEFAULT to_date('01-01-'|| DATE_TO_CHAR(sysdate,'yyyy'),'dd-mm-yyyy')
--   CONSTRAINT cnt_c_cnt_contract_van CHECK(cnt_contract_looptijd_van IS NOT NULL)
,  cnt_contract_looptijd_tot
   DATE
   CONSTRAINT cnt_c_cnt_contract_tot CHECK(cnt_contract_looptijd_tot IS NOT NULL)
,  cnt_contract_inactiveren  /* Automatisch sluiten na einddatum (0=nee, 1=ja) */
   NUMBER(1)
   DEFAULT 0
   CONSTRAINT cnt_r_cnt_contract_inactiveren CHECK(cnt_contract_inactiveren IN (0,1))
,  cnt_contract_kosten
   NUMBER(11,2)
,  cnt_contract_termijnkosten
   NUMBER(11,2)
,  cnt_contract_termijntermijn
    NUMBER(10)
    CONSTRAINT cnt_r_cnt_termijn_key4 REFERENCES cnt_termijn(cnt_termijn_key)
,  cnt_contract_uurloon
   NUMBER(6,2)
,  cnt_prs_bedrijf_key
   NUMBER(10)
   CONSTRAINT cnt_r_prs_bedrijf_key REFERENCES prs_bedrijf(prs_bedrijf_key)
,  cnt_prs_perslid_key
   NUMBER(10)
   CONSTRAINT cnt_r_prs_perslid_key2 REFERENCES prs_perslid(prs_perslid_key)
,  cnt_prs_afdeling_key
   NUMBER(10)
   CONSTRAINT cnt_r_prs_afdeling_key REFERENCES prs_afdeling(prs_afdeling_key)
,  prs_kostensoort_key
   NUMBER(10)
   CONSTRAINT cnt_r_prs_kostensoort1 REFERENCES prs_kostensoort(prs_kostensoort_key)
,  cnt_contract_aanmaak
   DATE
   DEFAULT SYSDATE
,  cnt_contract_verwijder
   DATE
   DEFAULT NULL
,  cnt_contract_nummer_intern
   VARCHAR2(30)
,  cnt_contract_korting
   NUMBER(5,2)
   CONSTRAINT cnt_c_cnt_contract_korting CHECK(0 <= cnt_contract_korting  AND cnt_contract_korting  <= 100)
,  prs_contactpersoon_key
   NUMBER(10)
   CONSTRAINT prs_r_prs_contactpersoon_key2 REFERENCES prs_contactpersoon(prs_contactpersoon_key) ON DELETE SET NULL
,  cnt_contract_contact_persoon
   VARCHAR2(30)
,  ins_discipline_key
   NUMBER(10)
   CONSTRAINT cnt_r_ins_discipline_key REFERENCES ins_tab_discipline (ins_discipline_key)
,  cnt_contract_document
   VARCHAR2(2048)
,  prs_perslid_key_eig
   NUMBER(10)
   CONSTRAINT cnt_r_cnt_perslid_key_eig REFERENCES prs_perslid (prs_perslid_key)
,  prs_perslid_key_beh
   NUMBER(10)
   CONSTRAINT cnt_r_cnt_perslid_key_beh REFERENCES prs_perslid (prs_perslid_key)
,  prs_kostenplaats_key NUMBER(10)
   CONSTRAINT cnt_r_prs_kostenplaats_key1 REFERENCES prs_kostenplaats(prs_kostenplaats_key)
,  prs_afdeling_key_eig
   NUMBER(10)
   CONSTRAINT cnt_r_prs_afdeling_key_eig REFERENCES prs_afdeling(prs_afdeling_key)
,  cnt_contract_status  /* 0=actief, 1=inactief, 2=nieuw, 3=ter goedkeuring */
   NUMBER(1)
   CONSTRAINT cnt_c_cnt_contract_status CHECK(cnt_contract_status IN (0,1,2,3))
,  cnt_contract_looptijd_van
   DATE
   DEFAULT to_date('01-01-'|| DATE_TO_CHAR(sysdate,'yyyy'),'dd-mm-yyyy')
   CONSTRAINT cnt_c_cnt_contract_van CHECK(cnt_contract_looptijd_van IS NOT NULL)
,  cnt_contract_opzegtermijn
    NUMBER(10)
    CONSTRAINT cnt_r_cnt_termijn_key2 REFERENCES cnt_termijn(cnt_termijn_key)
,  cnt_contract_rappeltermijn
    NUMBER(10)
    CONSTRAINT cnt_r_cnt_termijn_key3 REFERENCES cnt_termijn(cnt_termijn_key)
,  prs_dienst_key
    NUMBER(10)
    CONSTRAINT cnt_r_prs_dienst_key1 REFERENCES prs_dienst(prs_dienst_key)
,  cnt_contract_verlenging  /* 00=vragen nee (0), 01=vragen ja (1), 10=vast nee (2), 11=vast ja (3) */
   NUMBER(1)
   DEFAULT 2
   CONSTRAINT cnt_r_cnt_contract_verlenging CHECK(cnt_contract_verlenging IN (0,1,2,3,4,5,6,7))
,  cnt_contract_verleng_termijn
   NUMBER(10)
   CONSTRAINT cnt_r_cnt_termijn_key5 REFERENCES cnt_termijn(cnt_termijn_key)
,  cnt_contract_approved
   NUMBER(11,2)
,  cnt_contract_teverzenden
   NUMBER(1)
,  cnt_contract_locked_user_key
   NUMBER(10)
,  cnt_contract_locked_date
   DATE
-- In BES_TAB.SRC:
--,  bes_bestelopdr_key
--   NUMBER(10)
--   CONSTRAINT cnt_r_bes_bestelopdr_key REFERENCES bes_bestelopdr(bes_bestelopdr_key)
);

CREATE_TABLE(cnt_contract_object,0)
(  cnt_contract_object_key
   NUMBER(10)
   CONSTRAINT cnt_k_cnt_contract_object_key PRIMARY KEY
,  cnt_contract_key
   NUMBER(10)
   CONSTRAINT cnt_r_cnt_contract_key_object REFERENCES cnt_contract(cnt_contract_key) ON DELETE CASCADE
,  cnt_ins_srtdeel_key
   NUMBER(10)
   CONSTRAINT cnt_r_ins_srtdeel_key REFERENCES ins_srtdeel(ins_srtdeel_key)
,  cnt_ins_deel_key
   NUMBER(10)
   CONSTRAINT cnt_r_ins_deel_key REFERENCES ins_deel(ins_deel_key)
,  cnt_contract_object_aanmaak
   DATE
   DEFAULT SYSDATE
,  cnt_contract_object_verwijder
   DATE
   DEFAULT NULL
);

CREATE_TABLE(cnt_contract_plaats,0)
(  cnt_contract_plaats_key
   NUMBER(10)
   CONSTRAINT cnt_k_cnt_contract_plaats PRIMARY KEY
,  cnt_contract_key
   NUMBER(10)
   CONSTRAINT cnt_r_cnt_contract_key_plaats REFERENCES cnt_contract(cnt_contract_key) ON DELETE CASCADE
,  cnt_alg_plaats_key
   NUMBER(10)
,  cnt_alg_plaats_code
   VARCHAR2(1)
,  cnt_contract_plaats_gewicht
   NUMBER(11,2)
,  cnt_contract_plaats_aanmaak
   DATE
   DEFAULT SYSDATE
,  cnt_contract_plaats_verwijder
   DATE
   DEFAULT NULL
);

CREATE_TABLE (CNT_CONTRACT_DIENST,0)
( cnt_contract_dienst_key
   NUMBER(10)
   PRIMARY KEY,
  cnt_contract_key
   NUMBER(10)
   CONSTRAINT cnt_r_cnt_contract_dienst REFERENCES cnt_contract(cnt_contract_key) ON DELETE CASCADE,
  mld_stdmelding_key
   NUMBER(10)
   CONSTRAINT cnt_r_cnt_contract_stdmld REFERENCES mld_stdmelding(mld_stdmelding_key),
  prs_bedrijf_key
   NUMBER(10)
   CONSTRAINT cnt_r_cnt_contract_bedrijf REFERENCES prs_bedrijf(prs_bedrijf_key),
  cnt_contract_dienst_contactint
   NUMBER(10)
   CONSTRAINT cnt_r_cnt_contractint REFERENCES prs_perslid(prs_perslid_key),
  cnt_contract_dienst_contactext   VARCHAR2(30),
  cnt_contract_dienst_uitvoertd  NUMBER(3),
  cnt_contract_dienst_uren         NUMBER(6,2),
  cnt_contract_dienst_materiaal    NUMBER(6,2),
  cnt_contract_dienst_uurloon      NUMBER(6,2)
);


ALTER TABLE mld_opdr ADD
(
    cnt_contract_key
        NUMBER(10)
        CONSTRAINT mld_r_cnt_contract_key REFERENCES cnt_contract(cnt_contract_key) ON DELETE SET NULL
);

ALTER TABLE mld_opdr ADD
(
    cnt_contract_dienst_key
        NUMBER(10)
        CONSTRAINT mld_r_cnt_contractd_key REFERENCES cnt_contract_dienst(cnt_contract_dienst_key)
);

ALTER TABLE mld_opdr DROP CONSTRAINT mld_r_mld_melding_key1;
ALTER TABLE mld_opdr ADD
(
    CONSTRAINT mld_r_mld_melding_key3 CHECK(mld_melding_key IS NOT NULL OR cnt_contract_key IS NOT NULL OR prs_kostensoort_key IS NOT NULL)
);

CREATE_TABLE(cnt_srtkenmerk, 0)
(  cnt_srtkenmerk_key
      NUMBER(10)
   CONSTRAINT cnt_k_cnt_srtkenmerk_key PRIMARY KEY
,  cnt_srtkenmerk_omschrijving
      VARCHAR2(50)
,  cnt_srtkenmerk_upper
      VARCHAR2(50)
,  cnt_srtkenmerk_code
      VARCHAR2(60)
,  cnt_srtkenmerk_dimensie
      VARCHAR2(10)
,  cnt_srtkenmerk_kenmerktype
      VARCHAR2(1)
,  cnt_srtkenmerk_systeem
      NUMBER(2)
,  cnt_srtkenmerk_lengte
      NUMBER(4)
,  cnt_srtkenmerk_dec
      NUMBER(3)
,  cnt_srtkenmerk_nmin
      NUMBER(10)
,  cnt_srtkenmerk_nmax
      NUMBER(10)
,  fac_kenmerkdomein_key
      NUMBER(10)
      CONSTRAINT cnt_r_kenmerkdomein_key1 REFERENCES fac_kenmerkdomein(fac_kenmerkdomein_key)
,  cnt_srtkenmerk_aanmaak
      DATE
      DEFAULT SYSDATE
,  cnt_srtkenmerk_verwijder
      DATE
      DEFAULT NULLDATUM
,  CONSTRAINT cnt_c_cnt_srtkenmerk_omschr    CHECK(cnt_srtkenmerk_omschrijving IS NOT NULL)
,  CONSTRAINT cnt_srtkenmerk_omschrijving    CHECK(cnt_srtkenmerk_omschrijving IS NOT NULL)
,  CONSTRAINT cnt_u_cnt_srtkenmerk_upper    UNIQUE(cnt_srtkenmerk_upper, cnt_srtkenmerk_verwijder)
,  CONSTRAINT cnt_c_cnt_srtkenmerk_systeem   CHECK(cnt_srtkenmerk_systeem IS NULL
                                                OR cnt_srtkenmerk_systeem = 1)
,  CONSTRAINT cnt_c_cnt_srtkenmerk_lengte    CHECK(cnt_srtkenmerk_lengte >= 1
                                               AND cnt_srtkenmerk_lengte <= 4000)
,  CONSTRAINT cnt_c_cnt_srtkenmerk_max_min   CHECK(cnt_srtkenmerk_nmax >= cnt_srtkenmerk_nmin)
,  CONSTRAINT cnt_c_cnt_srtkenmerk_dec       CHECK(cnt_srtkenmerk_dec >= 0
                                               AND cnt_srtkenmerk_dec <= cnt_srtkenmerk_lengte)
,  CONSTRAINT cnt_c_cnt_srtkenmerk_aanm      CHECK(cnt_srtkenmerk_aanmaak IS NOT NULL)
);

CREATE_TABLE(cnt_kenmerk, 0)
(  cnt_kenmerk_key
      NUMBER(10)
   CONSTRAINT cnt_k_cnt_kenmerk_key PRIMARY KEY
,  cnt_srtkenmerk_key
      NUMBER(10)
,  cnt_srtcontract_key
      NUMBER(10)  /* verwijst naar ins_discipline */
,  cnt_kenmerk_niveau
      VARCHAR2(1)
,  cnt_kenmerk_omschrijving
      VARCHAR2(50)  /* optionele overrule van srtkenmerk_omschrijving */
,  cnt_kenmerk_code
      VARCHAR2(60)
,  cnt_kenmerk_verplicht
      NUMBER(1)
,  cnt_kenmerk_groep
      NUMBER(1)
      DEFAULT 0 NOT NULL
,  cnt_kenmerk_toonbaar
      NUMBER(1)
,  cnt_kenmerk_inlijst
      NUMBER(1)
      DEFAULT 0 NOT NULL
,  cnt_kenmerk_volgnummer
      NUMBER(3)
      NOT NULL
,  cnt_kenmerk_rolcode 
      NUMBER(4) 
      DEFAULT 7 NOT NULL
,  cnt_kenmerk_uniek
      NUMBER(1)
,  cnt_kenmerk_regexp
    VARCHAR2(255)
,  cnt_kenmerk_hint
      VARCHAR2(255 CHAR)
,  cnt_kenmerk_aanmaak
      DATE
      DEFAULT SYSDATE
,  cnt_kenmerk_verwijder
      DATE
      DEFAULT NULLDATUM
,  cnt_kenmerk_default VARCHAR2(4000)
,  cnt_kenmerk_show_expr VARCHAR2(4000)
,  fac_functie_key
      NUMBER(10)
      CONSTRAINT fac_r_cnt_kenmerk_functie REFERENCES fac_functie (fac_functie_key)
,  CONSTRAINT cnt_r_cnt_srtkenmerk_key1      CHECK(cnt_srtkenmerk_key IS NOT NULL)
,  CONSTRAINT cnt_r_cnt_srtkenmerk_key2      FOREIGN KEY(cnt_srtkenmerk_key) REFERENCES cnt_srtkenmerk(cnt_srtkenmerk_key)
,  CONSTRAINT cnt_c_cnt_kenmerk_niveau1      CHECK(cnt_kenmerk_niveau IS NOT NULL)
,  CONSTRAINT cnt_c_cnt_kenmerk_niveau2      CHECK(cnt_kenmerk_niveau IN ('C','S'))
,  CONSTRAINT cnt_c_cnt_kenmerk_toonbaar     CHECK(cnt_kenmerk_toonbaar IS NULL
                                                OR cnt_kenmerk_toonbaar = 1)
,  CONSTRAINT cnt_c_cnt_kenmerk_aanmaak      CHECK(cnt_kenmerk_aanmaak IS NOT NULL)
,  CONSTRAINT cnt_u_cnt_kenmerk              UNIQUE(cnt_srtkenmerk_key, cnt_srtcontract_key, cnt_kenmerk_niveau, cnt_kenmerk_omschrijving, cnt_kenmerk_groep, cnt_kenmerk_verwijder)
);

CREATE_TABLE(cnt_kenmerkcontract, 0)
(  cnt_kenmerkcontract_key
      NUMBER(10)
      CONSTRAINT cnt_k_cnt_kenmerkcontract_key PRIMARY KEY
,  cnt_contract_key
      NUMBER(10)
,  cnt_kenmerk_key
      NUMBER(10)
,  cnt_kenmerkcontract_waarde
      VARCHAR2(4000)
,  cnt_kenmerkcontract_aanmaak
      DATE
      DEFAULT SYSDATE
,  cnt_kenmerkcontract_wijzig
      DATE
,  cnt_kenmerkcontract_verwijder
      DATE
      DEFAULT NULLDATUM
,  CONSTRAINT cnt_r_cnt_kenmerk_contract_key     CHECK(cnt_contract_key IS NOT NULL)
,  CONSTRAINT cnt_r_cnt_kenmerk_key1             CHECK(cnt_kenmerk_key IS NOT NULL)
,  CONSTRAINT cnt_r_cnt_kenmerk_key2       FOREIGN KEY(cnt_kenmerk_key) REFERENCES cnt_kenmerk(cnt_kenmerk_key)
,  CONSTRAINT cnt_c_cnt_kenmerkcontract_aan      CHECK(cnt_kenmerkcontract_aanmaak IS NOT NULL)
,  CONSTRAINT cnt_u_cnt_kenmerkcontract          UNIQUE(cnt_contract_key, cnt_kenmerk_key, cnt_kenmerkcontract_verwijder)
,  CONSTRAINT cnt_r_kenmerk_contract       FOREIGN KEY (cnt_contract_key) REFERENCES cnt_contract(cnt_contract_key) ON DELETE CASCADE
);

CREATE_TABLE(cnt_contract_note, 0)
(
   cnt_contract_note_key            NUMBER (10) PRIMARY KEY,
   cnt_contract_key                 NUMBER (10) NOT NULL REFERENCES cnt_contract (cnt_contract_key) ON DELETE CASCADE,
   cnt_contract_note_aanmaak        DATE        DEFAULT SYSDATE,
   prs_perslid_key                  NUMBER (10) REFERENCES prs_perslid (prs_perslid_key) ON DELETE SET NULL,
   cnt_contract_note_omschrijving   VARCHAR2 (4000 CHAR)
);

CREATE_TABLE(cnt_factuurschema , 0)
(
    cnt_factuurschema_key
        NUMBER(10) PRIMARY KEY,
    cnt_contract_key
        NUMBER(10)
        NOT NULL
        CONSTRAINT cnt_r_cnt_factuurschema1 REFERENCES cnt_contract(cnt_contract_key) ON DELETE CASCADE,
    cnt_factuurschema_boekmaand  /* format YYYY-MM */
        VARCHAR2(7),
    cnt_factuurschema_bedrag
        NUMBER(11,2),
    cnt_factuurschema_opmerking
        VARCHAR2(320),
    cnt_factuurschema_aanmaak
        DATE
        DEFAULT SYSDATE
);

CREATE_TABLE(cnt_verlengschema, 0)
(
   cnt_verlengschema_key
      NUMBER(10)
      PRIMARY KEY,
   cnt_contract_key
      NUMBER(10)
      CONSTRAINT cnt_r_cnt_contract_key2 REFERENCES cnt_contract(cnt_contract_key),
   cnt_verlengschema_volgnr
      NUMBER(2),
   cnt_verlengschema_verlengtermijn
      NUMBER(10)
      CONSTRAINT cnt_r_cnt_termijn_key6 REFERENCES cnt_termijn(cnt_termijn_key)
);

REGISTERONCE('$Id$')


#endif // CNT
