IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'FAMSAMTAL'
          AND Object_ID = Object_ID(N'LD0P0C'))
BEGIN
ALTER TABLE LD0P0C ADD 
	FAMSAMTAL  DECIMAL(1)
	CONSTRAINT LD0D0C_FAMSAMTAL DEFAULT 0 NOT NULL
END
GO

IF NOT EXISTS(SELECT 1 FROM sys.columns 
          WHERE Name = N'FAMSAMTALFORL'
          AND Object_ID = Object_ID(N'LD0P0C'))
BEGIN
ALTER TABLE LD0P0C ADD 
	FAMSAMTALFORL  DECIMAL(1)
	CONSTRAINT LD0D0C_FAMSAMTALFORL DEFAULT 0 NOT NULL
END
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0CT')
  BEGIN
    DROP VIEW LD0V0CT
  END
GO
CREATE VIEW LD0V0CT 
AS 
SELECT * FROM LD0P0C WHERE ENDTIME='9999-12-31 23:59:59.9999999'
GO
GRANT ALL ON LD0V0CT TO LD0ROANV
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0C')
  BEGIN
    DROP VIEW LD0V0C
  END
GO
CREATE VIEW LD0V0C
AS 
SELECT * FROM LD0P0C 
GO
GRANT ALL ON LD0V0C TO LD0ROANV
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0VL1C')
  BEGIN
    DROP VIEW LD0VL1C
  END
GO
CREATE VIEW LD0VL1C
AS 
SELECT * FROM LD0P0C 
GO
GRANT ALL ON LD0VL1C TO LD0ROANV
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V1C')
  BEGIN
    DROP VIEW LD0V1C
  END
GO
CREATE VIEW LD0V1C
AS 
SELECT * FROM LD0P0C 
GO
GRANT ALL ON LD0V1C TO LD0ROANV
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0VLC')
  BEGIN
    DROP VIEW LD0VLC
  END
GO
CREATE VIEW LD0VLC
AS 
SELECT * FROM LD0P0C 
GO
GRANT ALL ON LD0VLC TO LD0ROANV
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0VYC')
  BEGIN
    DROP VIEW LD0VYC
  END
GO
CREATE VIEW LD0VYC
AS 
SELECT * FROM LD0P0C 
GO
GRANT ALL ON LD0VYC TO LD0ROANV
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0ApiInvestigation')
  BEGIN
    DROP VIEW LD0ApiInvestigation
  END
GO
CREATE VIEW LD0ApiInvestigation
AS
SELECT C.*,
C.STARTTIME,
C.ENDTIME,
B.TYPNR_T AKTTYP_T, 
Q.KLARTEXT AEVERKTYP_T, 
Q.XNUM40 WORKFLOW_T, 
O.NAMN ORG_T, 
H.NAMN CASEWORKER,
A.NAMN AKTPNR_NAMN,
A.KUNDNUMMER
FROM LD0P0C C 
INNER JOIN LD0VAAYB B 
ON C.AKTNR = B.AKTNR
INNER JOIN LD0V0Q01T Q 
ON Q.KODTYP = 'UTREDTYP' 
AND Q.KOD = C.AEVERKTYP
INNER JOIN LD0V0J01 O 
ON C.ORGID = O.ORGID 
AND C.ORGENHNR = O.ORGENHNR
INNER JOIN LD0V0I H 
ON C.HAND = H.HAND
LEFT OUTER JOIN LD0V0A A 
ON C.AKTPNR = A.PNR
WHERE C.TMARK = 0
GO
GRANT ALL ON LD0ApiInvestigation TO LD0ROANV
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0ApiExecution')
  BEGIN
    DROP VIEW LD0ApiExecution
  END
GO
CREATE VIEW LD0ApiExecution
AS
SELECT C.*,
C.STARTTIME,
C.ENDTIME,
B.TYPNR_T AKTTYP_T, 
Q.KLARTEXT AEVERKTYP_T, 
Q.XNUM40 WORKFLOW_T, 
O.NAMN ORG_T, 
H.NAMN CASEWORKER,
A.NAMN AKTPNR_NAMN,
A.KUNDNUMMER
FROM LD0P0C C 
INNER JOIN LD0VAAYB B 
ON C.AKTNR = B.AKTNR
INNER JOIN LD0V0Q01T Q 
ON Q.KODTYP = 'JOURNALTYP' 
AND Q.KOD = C.AEVERKTYP
INNER JOIN LD0V0J01 O 
ON C.ORGID = O.ORGID 
AND C.ORGENHNR = O.ORGENHNR
INNER JOIN LD0V0I H 
ON C.HAND = H.HAND
LEFT OUTER JOIN LD0V0A A 
ON C.AKTPNR = A.PNR
WHERE C.TMARK = 1
GO
GRANT ALL ON LD0ApiExecution TO LD0ROANV
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0VAA0INVESTIGATIONS')
  BEGIN
    DROP VIEW LD0VAA0INVESTIGATIONS
  END
GO
CREATE VIEW  LD0VAA0INVESTIGATIONS
AS
SELECT C.*, 
Q01B.KLARTEXT AS AKTTYP_T, 
Q.KLARTEXT AS AEVERKTYP_T,
Q.XNUM40 AS WORKFLOW_T,
O.NAMN AS ORG_T,
H.NAMN,
A.NAMN AS AKTPNR_NAMN,
A.KUNDNUMMER
FROM LD0VLC AS C 
INNER JOIN LD0VYB AS B ON C.AKTNR = B.AKTNR 
INNER JOIN ld0v0q01 Q01B ON Q01B.KODTYP='AKTTYP' AND Q01B.KOD=B.TYPNR
INNER JOIN LD0V0Q01 AS Q ON Q.KODTYP = 'UTREDTYP' AND Q.KOD = C.AEVERKTYP
INNER JOIN LD0V0J01 AS O ON C.ORGID = O.ORGID AND C.ORGENHNR = O.ORGENHNR
INNER JOIN LD0V0I AS H ON C.HAND = H.HAND
LEFT OUTER JOIN LD0V0A AS A ON C.AKTPNR = A.PNR
GO
GRANT ALL ON LD0VAA0INVESTIGATIONS TO LD0ROANV
GO

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0VAAYINVESTIGATIONS')
  BEGIN
    DROP VIEW LD0VAAYINVESTIGATIONS
  END
GO
CREATE VIEW LD0VAAYINVESTIGATIONS
AS
SELECT C.*,
B.TYPNR_T AS AKTTYP_T, 
Q.KLARTEXT AS AEVERKTYP_T, 
Q.XNUM40 AS WORKFLOW_T, 
O.NAMN AS ORG_T, 
H.NAMN AS NAMN,
A.NAMN AS AKTPNR_NAMN,
A.KUNDNUMMER
FROM LD0VYC C 
INNER JOIN LD0VAAYB B ON C.AKTNR = B.AKTNR
INNER JOIN LD0V0Q01 Q ON Q.KODTYP = 'UTREDTYP' AND Q.KOD = C.AEVERKTYP
INNER JOIN LD0V0J01 O ON C.ORGID = O.ORGID AND C.ORGENHNR = O.ORGENHNR
INNER JOIN LD0V0I H ON C.HAND = H.HAND
LEFT OUTER JOIN LD0V0A A ON C.AKTPNR = A.PNR
GO
GRANT ALL ON LD0VAAYINVESTIGATIONS TO LD0ROANV
GO
