CREATE TABLE [dbo].[TBCUST_PM_RPT_2BCREATED](
	[DocID] [bigint] NOT NULL,
	[GEN_ID] [varchar](3) NOT NULL,
	[COMMENT] [varchar](500) NULL,
	[ADDED_WHEN] [datetime] NOT NULL,
 CONSTRAINT [PK_TBCUST_PM_RPT_2BCREATED] PRIMARY KEY CLUSTERED 
(
	[DocID] ASC,
	[GEN_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED 
(
	[DocID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[TBCUST_PM_RPT_2BCREATED] ADD  DEFAULT ('001') FOR [GEN_ID]
GO

ALTER TABLE [dbo].[TBCUST_PM_RPT_2BCREATED] ADD  DEFAULT (getdate()) FOR [ADDED_WHEN]
GO
CREATE TABLE [dbo].[TBCUST_PM_RPT_CREATED](
	[GUID] [bigint] IDENTITY(1,1) NOT NULL,
	[DocID] [bigint] NOT NULL,
	[COMMENT] [varchar](500) NULL,
	[ADDED_WHEN] [datetime] NOT NULL,
 CONSTRAINT [PK_TBCUST_PM_RPT_CREATED] PRIMARY KEY CLUSTERED 
(
	[GUID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED 
(
	[DocID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[TBCUST_PM_RPT_CREATED] ADD  DEFAULT (getdate()) FOR [ADDED_WHEN]
GO

ALTER VIEW [dbo].[VWCUST_STAMP] AS
--SELECT 'FF' FG_TYP,4711 as DocID, 'm.schreiber@honey.de' as Email, 'Ja' as Freigabe, convert(Datetime,'2019-12-17 12:40:25',120) Wann
--UNION
--SELECT 'VA' FG_TYP,4711 as DocID, 'n.schreiber@honey.de' as Email, 'Ja' as Freigabe, convert(Datetime,'2019-12-17 12:32:40',120) Wann
--UNION
--SELECT 'VA' FG_TYP,4711 as DocID, 'v.schreiber@honey.de' as Email, 'Ja' as Freigabe, convert(Datetime,'2019-12-17 12:28:19',120) Wann
--UNION
--SELECT 'FF' FG_TYP,4712 as DocID, 'm.Kamm@dd-works.de' as Email, 'Ja' as Freigabe, convert(Datetime,'2019-12-17 12:40:25',120) Wann
--UNION
--SELECT 'VA' FG_TYP,4712 as DocID, 'v.feygin@ukraina.de' as Email, 'Ja' as Freigabe, convert(Datetime,'2019-12-16 12:11:08',120) Wann
--UNION
--SELECT 'VA' FG_TYP,4712 as DocID, 'j.hoss@karate.de' as Email, 'Ja' as Freigabe, convert(Datetime,'2019-12-14 11:28:23',120) Wann
--UNION
--SELECT 'VA' FG_TYP,4712 as DocID, 'a.treubenbach@stitching.de' as Email, 'Ja' as Freigabe, convert(Datetime,'2019-12-13 18:28:23',120) Wann
--GO
SELECT
	Vec.dwDocID as DocID,
	(SELECT Data FROM FNDD_SPLIT_STRING(Vec.szvalue,'~') where SequentialOrder = 1) as Email,
	(SELECT Data FROM FNDD_SPLIT_STRING(Vec.szvalue,'~') where SequentialOrder = 2) as [Freigabe],
	(SELECT Data FROM FNDD_SPLIT_STRING(Vec.szvalue,'~') where SequentialOrder = 5) as [FG_TYP],
	(SELECT Data FROM FNDD_SPLIT_STRING(Vec.szvalue,'~') where SequentialOrder = 4) as [Wann]
FROM 
	sDD_VMP01_APS01_windream60.dbo.Vector Vec 
WHERE 
	vec.dwDocID = 19138 AND
	vec.dwAttrID = 333

GO
CREATE VIEW [dbo].[VWCUST_RPTERGEBNISBERICHT_HEAD] as
SELECT DISTINCT

	BA.dwDocID as DocID,
	CONVERT(date,COALESCE(EML.EMAIL_DATE,CAST(CONVERT(varchar(20),ba.decCreationTime) as CHAR(8)))) [DatumIn],
	CONVERT(date,CAST(ba.dwdate09 AS char(8))) AS Belegdatum,
	vec_KRED.szValue as KreditorNummer,
	ba.sztext08 AS KreditorName_Lang,
	ba.sztext08 AS KreditorName,
	ba.sztext19 as RechnungNr,
	ba.sztext36 as Referenz,
	ba.lfFloat10 as Nettobetrag,
	ba.lfFloat11 as Bruttobetrag,
	'1999-12-24' Zahlungsziel,
	'4711' as Mandant,
	'Mandant1' as [Mandant Name],
	'4711' as Kostenstelle,
	'4712' as BestellNr,
	COALESCE(eml.EMAIL_FROM,'') as Absender,
	'' as [Finaler Freigeber]
FROM 
	windream60.dbo.BaseAttributes BA LEFT join 
	TBEMLP_HISTORY EML ON BA.szText07 = EML.EMAIL_MSGID LEFT join
	(Select dwDocID, szValue From windream60.dbo.Vector where dwAttrID = 10010) vec_KRED ON BA.dwDocID = vec_KRED.dwDocID
--WHERE 
--	ba.dwDocID = 785040
	
GO

ALTER VIEW [dbo].[VWCUST_RPTERGEBNISBERICHT] as 
SELECT T.*, T1.Prozessschritt,T1.Durch, T1.Status_Change, T1.Wann wannPos, T1.Kommentar
FROM VWCUST_RPTERGEBNISBERICHT_HEAD T INNER JOIN VWCUST_RPTERGEBNISBERICHT_POS T1 ON T.DocID = T1.DocID
GO




