-- ABS-92716
----------- EMAILSETTING -----------
ALTER TABLE EMAILSETTING ADD (BUSINESSUNIT_ID NUMBER NULL);

COMMENT ON COLUMN EMAILSETTING.BUSINESSUNIT_ID
  IS 'Emailsettings per Business Unit is only supported for documenttype 1 and 9.';

DROP INDEX XAK1EMAILSETTING;
CREATE UNIQUE INDEX XAK1EMAILSETTING ON EMAILSETTING (PLANT_ID ASC, DOCUMENTTYPE ASC, NVL(BUSINESSUNIT_ID, 0) ASC);
CREATE INDEX XIF2EMAILSETTING ON EMAILSETTING (BUSINESSUNIT_ID ASC);

ALTER TABLE EMAILSETTING
  ADD CONSTRAINT EMAILSETTING_BUSINESSUNIT FOREIGN KEY (BUSINESSUNIT_ID)
  REFERENCES BUSINESSUNIT (BUSINESSUNIT_ID);

ALTER TABLE EMAILSETTING
  ADD CONSTRAINT CHK_EMAILSETTING_BU_DOCTYPE
  CHECK ((BUSINESSUNIT_ID IS NULL) OR
  ((BUSINESSUNIT_ID IS NOT NULL) AND
    (DOCUMENTTYPE IN (1, 9))));
