UPDATE TBDD_MODULES SET DB_VERSION = '1.5.4.0' WHERE SHORT_NAME = 'SIG_ENV_CR'
GO
IF  EXISTS(SELECT *
	FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TBSIG_ENVELOPE_RECEIVER'
AND COLUMN_NAME = 'TFA_ENABLED')
BEGIN
	ALTER TABLE [dbo].[TBSIG_ENVELOPE_RECEIVER] DROP CONSTRAINT [DF_TBSIG_ENVELOPE_RECEIVER_TFA_ENABLED];
	ALTER TABLE [dbo].TBSIG_ENVELOPE_RECEIVER DROP COLUMN TFA_ENABLED;
END
IF  EXISTS(SELECT *
	FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TBSIG_CONFIG'
AND COLUMN_NAME = 'DEF_VERIFICATION_TYPE')
BEGIN
	ALTER TABLE [dbo].TBSIG_CONFIG DROP CONSTRAINT [DF__TBSIG_CON__DEF_V__0AF366ED];
	ALTER TABLE [dbo].TBSIG_CONFIG DROP COLUMN DEF_VERIFICATION_TYPE;
END
ALTER TABLE [dbo].TBSIG_ENVELOPE ADD TFA_ENABLED BIT NOT NULL DEFAULT 0;
GO
ALTER TABLE [dbo].TBSIG_CONFIG ADD DEF_TFA_ENABLED BIT NOT NULL DEFAULT 0;
GO
-- Changed	23.01.2025	MS	TFA_ENABLED
-- Changed	07.01.2025	MS	VerifType integriert
ALTER VIEW [dbo].[VWSIG_ENVELOPE_DATA] AS
SELECT 
	ENV.[GUID] ENVELOPE_ID
    ,[USER_ID]	
	,USR.EMAIL USER_EMAIL
	,USR.PRENAME + ' ' + USR.NAME USER_PRE_NAME
    ,[STATUS] ENVELOPE_STATE
	,ENV.VERIFICATION_TYPE
	,TFA_ENABLED
    ,[ENVELOPE_UUID]
	,[LANG_CODE]			ENV_LANG_CODE
    ,[MESSAGE]
    ,[EXPIRES_WHEN]
    ,[EXPIRES_WARNING_WHEN]
    ,ENV.[ADDED_WHEN]
    ,ENV.[CHANGED_WHEN]
    ,[TITLE]
    ,[CONTRACT_TYPE]
    ,ENV.[LANGUAGE]
    ,[SEND_REMINDER_EMAILS]
    ,[FIRST_REMINDER_DAYS]
    ,[REMINDER_INTERVAL_DAYS]
    ,[ENVELOPE_TYPE]
    ,[CERTIFICATION_TYPE]
    ,[USE_ACCESS_CODE]
    ,[FINAL_EMAIL_TO_CREATOR]
    ,[FINAL_EMAIL_TO_RECEIVERS]
    ,[EXPIRES_WHEN_DAYS]
    ,[EXPIRES_WARNING_WHEN_DAYS]
	
  FROM 
	[TBSIG_ENVELOPE] ENV INNER JOIN TBDD_USER USR ON ENV.USER_ID = USR.GUID 
GO
IF  EXISTS(SELECT *
	FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TBSIG_ENVELOPE_RECEIVER'
AND COLUMN_NAME = 'PHONE_NUMBER')
BEGIN
	ALTER TABLE [dbo].TBSIG_ENVELOPE_RECEIVER ADD PHONE_NUMBER VARCHAR(30) NOT NULL DEFAULT '';
END
GO
ALTER VIEW [dbo].[VWSIG_ENVELOPE_RECEIVER] AS
SELECT
	 ENV.GUID			ENV_ID,
	 ENV.ENVELOPE_UUID,
     REC.EMAIL_ADDRESS	REC_EMAIL,
	 REC.GUID			REC_ID,
     E_REC.NAME,
	 E_REC.PHONE_NUMBER,
     E_REC.ADDED_WHEN
FROM 
	[DD_ECM].[dbo].[TBSIG_RECEIVER] REC INNER JOIN TBSIG_ENVELOPE_RECEIVER E_REC ON REC.GUID = E_REC.RECEIVER_ID
	INNER JOIN TBSIG_ENVELOPE ENV ON E_REC.ENVELOPE_ID = ENV.GUID
GO
--CHANGED	23.01.2025	MS PhoneNumber
--CHANGED	13.11.2024	MS	RECEIVER_TERM integriert
ALTER   VIEW [dbo].[VWSIG_ENVELOPE_RECEIVERS] as 
SELECT  R.GUID, 
		ENV.USER_ID As ENV_USERID_CREATED,
		R.EMAIL_ADDRESS, 
		R.SIGNATURE,
		ER.ENVELOPE_ID, 
		ER.PRIVATE_MESSAGE, 
		ER.ACCESS_CODE, 
		ER.PHONE_NUMBER,
		ER.[SEQUENCE], 
		ER.NAME, 
		ER.ADDED_WHEN,
		R.EMAIL_ADDRESS + ' - ' + ER.[NAME] AS RECEIVER_TERM
FROM TBSIG_ENVELOPE_RECEIVER ER
INNER JOIN TBSIG_RECEIVER R ON ER.RECEIVER_ID = R.GUID
INNER JOIN  TBSIG_ENVELOPE ENV ON ER.ENVELOPE_ID = ENV.GUID
GO
INSERT INTO TBDD_SQL_COMMANDS(TITLE,SQL_COMMAND) VALUES ('REPORT ENV USER THIS_MONTH','
Select 
	USER_EMAIL [ENVELOPE CREATOR]
	,COUNT(ENVELOPE_ID) [SUM]
FROM
	VWSIG_ENVELOPE_DATA 
WHERE 
	DATEDIFF(MONTH,ADDED_WHEN,GETDATE()) = 0
GROUP BY USER_EMAIL')
GO
INSERT INTO TBDD_SQL_COMMANDS(TITLE,SQL_COMMAND) VALUES ('REPORT ENV USER LAST_MONTH','
Select 
	USER_EMAIL [ENVELOPE CREATOR]
	,COUNT(ENVELOPE_ID) [SUM]
FROM
	VWSIG_ENVELOPE_DATA 
WHERE 
	DATEDIFF(MONTH,ADDED_WHEN,GETDATE()) = 1
GROUP BY USER_EMAIL')
GO
INSERT INTO TBDD_SQL_COMMANDS(TITLE,SQL_COMMAND) VALUES ('REPORT ENV USER YEAR','
Select 
	USER_EMAIL [ENVELOPE CREATOR]
	,COUNT(ENVELOPE_ID) [SUM]
FROM
	VWSIG_ENVELOPE_DATA 
WHERE 
	DATEDIFF(YEAR,ADDED_WHEN,GETDATE()) = 0
GROUP BY USER_EMAIL')
GO
INSERT INTO TBDD_SQL_COMMANDS(TITLE,SQL_COMMAND) VALUES ('REPORT ENV USER ALL','
Select 
	USER_EMAIL [ENVELOPE CREATOR]
	,COUNT(ENVELOPE_ID) [SUM]
FROM
	VWSIG_ENVELOPE_DATA 
GROUP BY USER_EMAIL')
GO
CREATE TABLE [dbo].[TBSIG_CHART](
	GUID		INTEGER	IDENTITY(1,1)	NOT NULL,
	SQL_COMMAND	VARCHAR(MAX)			NOT NULL,
	TYPE_CHART	VARCHAR(50)				NOT NULL,
	ARGUMENT	VARCHAR(50)				NOT NULL,
	[VALUE]		VARCHAR(50)				NOT NULL,
	TITLE		VARCHAR(50)				NOT NULL	DEFAULT '',
	ADDED_WHO	VARCHAR(50)							DEFAULT '',
	ADDED_WHEN	DATETIME							DEFAULT GETDATE(),
	CHANGED_WHO	VARCHAR(50),
	CHANGED_WHEN	DATETIME,
	GROUP_ID	VARCHAR(50)							DEFAULT '',
CONSTRAINT PK_TBSIG_CHART_GUID    PRIMARY KEY (GUID)

)
GO

CREATE TRIGGER [dbo].[TBSIG_CHART_AFT_UPD] ON [dbo].[TBSIG_CHART]
FOR UPDATE
AS
	UPDATE TBSIG_CHART SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBSIG_CHART.GUID = INSERTED.GUID
GO