-- ABS-29180
-------------------------   REASONCODEDIALOGS-----------------------------------
INSERT INTO REASONCODEDIALOGS
  (REASONCODE_ID, DIALOG_ID, SELECTABLE, SYSTEMUSER_ID, TIMESTAMP)
  SELECT R.REASONCODE_ID,
         D.DIALOG_ID,
         'Y',
         GETMINSYSTEMUSER(),
         GETCURRENTDATETIME()
    FROM REASONCODE R,
         DIALOGS    D WHERE D.CODE = 'CheckInItemsFromPO';
commit;

INSERT INTO REASONCODEDIALOGS
  (REASONCODE_ID, DIALOG_ID, SELECTABLE, SYSTEMUSER_ID, TIMESTAMP)
  SELECT R.REASONCODE_ID,
         D.DIALOG_ID,
         'Y',
         GETMINSYSTEMUSER(),
         GETCURRENTDATETIME()
    FROM REASONCODE R,
         DIALOGS    D WHERE D.CODE = 'CorrectItemsCheckedInFromPO';
commit;

INSERT INTO REASONCODEDIALOGS
  (REASONCODE_ID, DIALOG_ID, SELECTABLE, SYSTEMUSER_ID, TIMESTAMP)
  SELECT R.REASONCODE_ID,
         D.DIALOG_ID,
         'Y',
         GETMINSYSTEMUSER(),
         GETCURRENTDATETIME()
    FROM REASONCODE R,
         DIALOGS    D WHERE D.CODE = 'MakeUniqueItemsCheckedInFromPO';
commit;

-------------------------   RECEIVEINSTOCKHISTORY   -----------------------------------
ALTER TABLE RECEIVEINSTOCKHISTORY ADD (PURCHASEORDERLINE_ID NUMBER NULL,
					TRANSACTIONTYPE_ID NUMBER NULL,
					STOCKRESERVATIONPERGRADE_ID NUMBER NULL,
					STOCKROOMLOCATION_ID NUMBER NULL,
					CHECKEDOUTCOMPLETELY NUMBER NULL 
						CONSTRAINT CHECKEDOUTCOMPLETELY_VALID1_1 CHECK ((CHECKEDOUTCOMPLETELY IS NULL) OR (CHECKEDOUTCOMPLETELY BETWEEN 0 AND 1)));

COMMENT ON COLUMN RECEIVEINSTOCKHISTORY.PURCHASEORDERLINE_ID IS 'Will only be filled when module ADVANCEDPURCHASING is in the license';

COMMENT ON COLUMN RECEIVEINSTOCKHISTORY.TRANSACTIONTYPE_ID IS 'Will only be filled when module ADVANCEDPURCHASING is in the license';

COMMENT ON COLUMN RECEIVEINSTOCKHISTORY.CHECKEDOUTCOMPLETELY IS 'Flag that indicates if all checked-in items have been checked-out again.Possible values:
null – Not applicable
0 – Not checked out complete yet
1 – Checked out completely';

CREATE INDEX XIF9RECEIVEINSTOCKHISTORY ON RECEIVEINSTOCKHISTORY (PURCHASEORDERLINE_ID ASC);

CREATE INDEX XIF10RECEIVEINSTOCKHISTORY ON RECEIVEINSTOCKHISTORY (TRANSACTIONTYPE_ID ASC);

CREATE INDEX XIF11RECEIVEINSTOCKHISTORY ON RECEIVEINSTOCKHISTORY (STOCKRESERVATIONPERGRADE_ID ASC);

CREATE INDEX XIF12RECEIVEINSTOCKHISTORY ON RECEIVEINSTOCKHISTORY (STOCKROOMLOCATION_ID ASC);

ALTER TABLE RECEIVEINSTOCKHISTORY ADD CONSTRAINT RECEIVEINSTOCKHISTORY_POLID FOREIGN KEY (PURCHASEORDERLINE_ID)
REFERENCES PURCHASEORDERLINE (PURCHASEORDERLINE_ID) ON DELETE SET NULL;

ALTER TABLE RECEIVEINSTOCKHISTORY ADD CONSTRAINT RECEIVEINSTOCKHISTORY_TTID FOREIGN KEY (TRANSACTIONTYPE_ID)
REFERENCES TRANSACTIONTYPE (TRANSACTIONTYPE_ID) ON DELETE SET NULL;

ALTER TABLE RECEIVEINSTOCKHISTORY ADD CONSTRAINT RECEIVEINSTOCKHISTORY_SRPGID FOREIGN KEY (STOCKRESERVATIONPERGRADE_ID)
REFERENCES STOCKRESERVATIONPERGRADE (STOCKRESERVATIONPERGRADE_ID) ON DELETE SET NULL;

ALTER TABLE RECEIVEINSTOCKHISTORY ADD CONSTRAINT RECEIVEINSTOCKHISTORY_SRLID FOREIGN KEY (STOCKROOMLOCATION_ID)
REFERENCES STOCKROOMLOCATION (STOCKROOMLOCATION_ID) ON DELETE SET NULL;

-------------------------   STOCKTRANSFERLINE   ----------------------------------------
ALTER TABLE STOCKTRANSFERLINE ADD (PURCHASEORDERLINE_ID NUMBER NULL);

CREATE INDEX XIF9STOCKTRANSFERLINE ON STOCKTRANSFERLINE (PURCHASEORDERLINE_ID ASC);

ALTER TABLE STOCKTRANSFERLINE ADD CONSTRAINT STOCKTRANSFERLINE_POLID FOREIGN KEY (PURCHASEORDERLINE_ID)
REFERENCES PURCHASEORDERLINE (PURCHASEORDERLINE_ID) ON DELETE SET NULL;