SELECT veu.Evn_setDate, case when veu.EvnUsluga_isCito =2 then 2 else 1 end as EvnUsluga_isCito, vex.EvnXml_Data
      		,veu.MedPersonal_id, veu.MedStaffFact_id, veu.Lpu_id, vpt.PASsportToken_tid
      		,veu.UslugaComplex_id, uc.nomenmedusluga_uslugacode AS UslugaComplex_Code, uc.nomenmedusluga_name AS UslugaComplex_Name
      FROM dbo.v_EvnPS veps  
       INNER JOIN LATERAL  ( SELECT ve.Evn_setDate, veu.EvnUsluga_isCito
                          ,veu.MedPersonal_id, veu.MedStaffFact_id, veu.Lpu_id, veu.EvnUsluga_id, veu.UslugaComplex_id
                  		FROM dbo.v_Evn ve   
                      JOIN dbo.v_EvnUsluga veu   ON veu.EvnDirection_id = ve.Evn_id
                      JOIN dbo.v_UslugaComplex vuc   ON vuc.UslugaComplex_id = veu.UslugaComplex_id
                      WHERE ve.Evn_pid = veps.EvnPS_id 
                    UNION 
                    SELECT ve.Evn_setDate, veu.EvnUsluga_isCito
                          ,veu.MedPersonal_id, veu.MedStaffFact_id, veu.Lpu_id, veu.EvnUsluga_id, veu.UslugaComplex_id
                  		FROM dbo.v_Evn ve   
                      JOIN dbo.v_EvnUsluga veu   ON veu.EvnUsluga_id = ve.Evn_id
                      JOIN dbo.v_UslugaComplex vuc   ON vuc.UslugaComplex_id = veu.UslugaComplex_id
                      WHERE ve.Evn_pid = veps.EvnPS_id 
                    UNION 
                    SELECT ve.Evn_setDate, veu.EvnUsluga_isCito
                          ,veu.MedPersonal_id, veu.MedStaffFact_id, veu.Lpu_id, veu.EvnUsluga_id, veu.UslugaComplex_id
                  		FROM dbo.v_Evn ve  
                      JOIN dbo.v_EvnUsluga veu   ON veu.EvnDirection_id = ve.Evn_id
                      JOIN dbo.v_UslugaComplex vuc   ON vuc.UslugaComplex_id = veu.UslugaComplex_id
                      WHERE ve.Evn_rid = veps.EvnPS_id 
                    UNION
                    SELECT ve.Evn_setDate, veu.EvnUsluga_isCito
                          ,veu.MedPersonal_id, veu.MedStaffFact_id, veu.Lpu_id, veu.EvnUsluga_id, veu.UslugaComplex_id
                  		FROM dbo.v_Evn ve  
                      JOIN dbo.v_EvnUsluga veu   ON veu.EvnUsluga_id = ve.Evn_id 
                      JOIN dbo.v_UslugaComplex vuc   ON vuc.UslugaComplex_id = veu.UslugaComplex_id
                      WHERE ve.Evn_rid = veps.EvnPS_id ) AS veu  on true 
      LEFT JOIN dbo.v_EvnXml vex ON vex.Evn_id = veu.EvnUsluga_id
      LEFT JOIN fed.v_PASsportToken vpt ON vpt.Lpu_id = veu.Lpu_id
       INNER JOIN LATERAL  ( SELECT  
                           uc.nomenmedusluga_uslugacode, uc.nomenmedusluga_name 
                      FROM nsi.p_NomenMedUsluga_getUsluga(veu.UslugaComplex_id) uc 
                     WHERE uc.nomenmedusluga_uslugacode like 'A08%'  LIMIT 1  ) uc  on true 
      WHERE veps.EvnPS_id = $1
