IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0PROCESS')
BEGIN
DROP VIEW LD0V0PROCESS
END
;
CREATE VIEW LD0V0PROCESS AS SELECT * FROM LD0P0PROCESS
;
IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V1PROCESSMETADATA')
BEGIN
DROP VIEW LD0V1PROCESSMETADATA 
END
;
CREATE VIEW LD0V1PROCESSMETADATA AS 
SELECT M.*, P.PROCESSOWNERTYPE AS OWNERTYPE FROM LD0P0PROCESSMETADATA M
LEFT JOIN  LD0V0PROCESS P ON M.PROCESSID = P.ID
;
IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0UNFINISHEDTASKS')
BEGIN
DROP VIEW LD0V0UNFINISHEDTASKS            
END
;
CREATE VIEW LD0V0UNFINISHEDTASKS
 AS            
SELECT             
CT.ID AS CLIENTTASKID,            
T.TASKID,           
T.TASKNAME,          
T.PRIORITY,      
T.ENDDATE,          
CT.PROCESSMETADATAID,            
CT.CASEWORKERID,                  
CT.ISCOMPLETED,            
M.OWNERTYPE,        
M.OWNERID,        
M.PERSONKEY,        
M.PROCESSID,  
PER.PNR PERSONID,    
PER.NAMN CLIENTNAME,       
V2.NAMN AS CASEWORKERNAME,
P.ACTIVE AS ISPROCESSACTIVE    
FROM LD0V0TASK T            
INNER JOIN LD0P0CLIENTTASKS CT ON T.TASKID = CT.TASKID            
INNER JOIN LD0V0I V2 ON CT.CASEWORKERID = V2.HAND        
INNER JOIN LD0V1PROCESSMETADATA M ON M.ID = CT.PROCESSMETADATAID    
INNER JOIN LD0V0A PER ON PER.KUNDNUMMER = M.PERSONKEY
INNER JOIN LD0P0PROCESS P ON M.PROCESSID = P.ID
;



IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V1PROCESSSTEPS')
BEGIN
DROP VIEW LD0V1PROCESSSTEPS 
END
;
CREATE VIEW LD0V1PROCESSSTEPS AS
SELECT S.*,  
CASE WHEN S.ID IN (
     SELECT DISTINCT(STEPID) FROM LD0P0PROCESSSTEPCOMPONENTMAPPING SCM  WHERE EXISTS  
    (SELECT ID FROM LD0P0SAVEDCOMPONENTS SC WHERE SC.STEPCOMPONENTMAPPINGID = SCM.ID)
     ) 
 THEN 1 
 ELSE 0 
 END AS ISSTEPUSED
FROM LD0P0PROCESSSTEPS S
;
IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V1SEARCHPROCESS')
BEGIN
DROP VIEW LD0V1SEARCHPROCESS
END
;
CREATE VIEW LD0V1SEARCHPROCESS
AS   
SELECT DISTINCT   
PS.ID,  
PS.NAME,  
PR.PNR AS 'PERSONID',  
PR.NAMN AS 'PERSONNAME',  
K1.VALUE AS 'PROCESSTYPE',  
PS.PROCESSTYPEID,  
PMD.ID AS 'PROCESSMETADATAID',  
PMD.OWNERTYPE,  
PMD.OWNERID,  
PMD.PERSONKEY,  
PMD.STARTDATE,  
PMD.ENDDATE,  
PMD.CASEWORKERID,  
PMD.COCASEWORKERID,  
PMD.ORGANIZATIONID,  
PMD.CASEWORKERORGUNITID,  
PMD.COCASEWORKERORGUNITID,  
H1.NAMN AS 'CASEWORKERNAME',  
H2.NAMN AS 'COCASEWORKERNAME',  
J1.NAMN AS 'CASEWORKERORGANIZATIONNAME',  
J2.NAMN AS 'COCASEWORKERORGANIZATIONNAME'  
FROM  
LD0V0PROCESS PS  
INNER JOIN LD0V1PROCESSMETADATA PMD   
ON PS.ID = PMD.PROCESSID  
INNER JOIN LD0V0A PR   
ON PMD.PERSONKEY = PR.KUNDNUMMER  
LEFT JOIN LD0V0I H1  
ON PMD.CASEWORKERID = H1.HAND  
LEFT JOIN LD0V0I H2  
ON PMD.COCASEWORKERID = H2.HAND  
LEFT JOIN LD0V0J01 J1  
ON PMD.ORGANIZATIONID = J1.ORGID AND PMD.CASEWORKERORGUNITID = J1.ORGENHNR  
LEFT JOIN LD0V0J01 J2  
ON PMD.ORGANIZATIONID = J2.ORGID AND PMD.COCASEWORKERORGUNITID = J2.ORGENHNR  
LEFT JOIN LD0V0KANTA K1   
ON PS.PROCESSTYPEID = K1.SERVCATID  
;

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0PROCESSSTEPS')
BEGIN
DROP VIEW LD0V0PROCESSSTEPS
END
;
CREATE VIEW LD0V0PROCESSSTEPS AS SELECT * FROM LD0P0PROCESSSTEPS
;

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0PROCESSSTEPCOMPONENTMAPPING')
BEGIN
DROP VIEW LD0V0PROCESSSTEPCOMPONENTMAPPING
END
;
CREATE VIEW LD0V0PROCESSSTEPCOMPONENTMAPPING AS SELECT * FROM LD0P0PROCESSSTEPCOMPONENTMAPPING
;

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0PROCESSMETADATA')
BEGIN
DROP VIEW LD0V0PROCESSMETADATA
END
;
CREATE VIEW LD0V0PROCESSMETADATA AS SELECT * FROM LD0P0PROCESSMETADATA
;

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V0NAVIGATIONCOMPONENTMAPPING')
BEGIN
DROP VIEW LD0V0NAVIGATIONCOMPONENTMAPPING
END
;
CREATE VIEW LD0V0NAVIGATIONCOMPONENTMAPPING AS SELECT * FROM LD0P0NAVIGATIONCOMPONENTMAPPING
;

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V1EHRWELFAREDOCUMENTDETAILS')
BEGIN
DROP VIEW LD0V1EHRWELFAREDOCUMENTDETAILS
END
;
CREATE VIEW LD0V1EHRWELFAREDOCUMENTDETAILS AS  
SELECT    
  CAST(ROW_NUMBER() OVER (ORDER BY ED.MODIFYDATE DESC)  AS INT) ROWNUM,  
        ED.ID,  
        DM.EHRDOCUMENTNAME,  
        ED.MODIFYDATE MODIFYDATE,  
        ED.COMPONENTDOCUMENTTYPEMAPPINGID,  
        LD.HAND CASEWORKERID,  
        LD.NAMN CASEWORKERNAME,  
        PMD.ID METADATAID,  
        PMD.PROCESSID,   
        P.[NAME] PROCESSNAME,  
		P.PROCESSTYPEID,  
		PER.KUNDNUMMER PERSONKEY,  
        PER.PNR PERSONID,  
        PER.NAMN APPLICANTNAME,  
        PMD.OWNERID,  
        P.PROCESSOWNERTYPE OWNERTYPE,  
        ED.ENCOUNTERID,  
        ED.COMPOSITIONSTATE,  
        K1.VALUE PROCESSTYPETEXT                       
FROM LD0V0PROCESSMETADATA PMD  
INNER JOIN LD0V0EHRWELFAREDOCUMENTS ED ON PMD.ID = ED.METADATAID  
INNER JOIN LD0V0PROCESS P ON P.Id = PMD.PROCESSID  
INNER JOIN LD0V0A PER ON PER.KUNDNUMMER = PMD.PERSONKEY  
INNER JOIN LD0V0I LD ON ED.CASEWORKERID = LD.HAND   
INNER JOIN LD0V0COMPONENTDOCUMENTTYPEMAPPING DM ON DM.ID=ED.COMPONENTDOCUMENTTYPEMAPPINGID  
INNER JOIN LD0V0KANTA K1 ON P.PROCESSTYPEID = K1.SERVCATID 
;


IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V1PROCESSSTEPCOMPONENTMAPPING')
BEGIN
DROP VIEW LD0V1PROCESSSTEPCOMPONENTMAPPING
END
;
CREATE VIEW LD0V1PROCESSSTEPCOMPONENTMAPPING AS 
SELECT PM.ID, PM.STEPID, PC.ID AS COMPONENTID, PC.COMPONENTMANIFESTID,   
PC.NAME, PC.FRIENDLYNAME, PM.ORDERNUMBER, PC.COMPONENTSTATE, PM.MANDATORY, PM.ISACTIVE, 
CASE WHEN PM.ID IN (SELECT SC.STEPCOMPONENTMAPPINGID FROM LD0P0SAVEDCOMPONENTS SC ) THEN 1 ELSE 0 END AS ISCOMPONENTUSED
FROM LD0V0PROCESSCOMPONENTS PC 
INNER JOIN LD0V0PROCESSSTEPCOMPONENTMAPPING PM ON PC.ID = PM.COMPONENTID
;

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V1GETSTEPCOMPONENTMAPPINGID')
BEGIN
DROP VIEW LD0V1GETSTEPCOMPONENTMAPPINGID
END
;
CREATE VIEW LD0V1GETSTEPCOMPONENTMAPPINGID
AS 
SELECT 
PS.ID AS STEPID,
PS.ISSTEPUSED,
PCM.ID AS COMPONENTMAPPINGID,
PS.PROCESSID,
PCM.COMPONENTMANIFESTID
FROM LD0V1PROCESSSTEPS PS
INNER JOIN LD0V1PROCESSSTEPCOMPONENTMAPPING PCM ON PS.ID = PCM.STEPID
;

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V1PROCESS')
BEGIN
DROP VIEW LD0V1PROCESS
END
;
CREATE VIEW LD0V1PROCESS
AS 
SELECT P.*, K.OID AS PROCESSTYPEOID 
FROM
LD0P0PROCESS P
LEFT JOIN LD0V0KANTA K ON K.SERVCATID = P.PROCESSTYPEID AND P.PROCESSTYPEID != 0 
;

IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE NAME='LD0V1EHRJOURNALNOTES')
BEGIN
DROP VIEW LD0V1EHRJOURNALNOTES
END
;
CREATE VIEW LD0V1EHRJOURNALNOTES 
 AS
 SELECT ED.ENCOUNTERID,
		ED.DSCID,
		M.PERSONKEY,
		ED.METADATAID,
		ED.MODIFYDATE,
		ED.CASEWORKERID,
		ED.COMPOSITIONSTATE,
		JN.PROCESSID,
		JN.[NAME]
 FROM LD0V0EHRWELFAREDOCUMENTS ED
 JOIN LD0V0PROCESSMETADATA M ON ED.METADATAID = M.ID
 JOIN LD0V0JOURNALNOTE JN ON ED.COMPONENTDOCUMENTTYPEMAPPINGID = JN.ID AND ED.EHRDOCUMENTTYPE = 2 
;