
                
            with mv as (
                select
                dbo.tzGetDate() as dt
            )
            
                
                SELECT 
         DISTINCT
                EPL.EvnPL_id as "EvnPL_id",
                EPL.Person_id as "Person_id",
                EPL.PersonEvn_id as "PersonEvn_id",
                EPL.Server_id as "Server_id",
                CASE WHEN pls.PolisType_id = 4 THEN PS.Person_edNum ELSE pls.Polis_Num END as "Polis_Num",
                coalesce(EPL.EvnPL_IsTransit, 1) as "EvnPL_IsTransit",
                RTRIM(EPL.EvnPL_NumCard) as "EvnPL_NumCard",
                EPL.EvnPL_VizitCount as "EvnPL_VizitCount",
                IsFinish.YesNo_Name as "EvnPL_IsFinish",
                RTRIM(EVPLD.Diag_Code) || '. ' || RTRIM(EVPLD.Diag_Name) as "Diag_Name",
                RTRIM(MP.Person_Fio) as "MedPersonal_Fio",
                to_char(EPL.EvnPL_setDT, 'DD.MM.YYYY') as "EvnPL_setDate",
                to_char(EPL.EvnPL_disDT, 'DD.MM.YYYY') as "EvnPL_disDate",
                RTRIM(HK.HealthKind_Name) as "HealthKind_Name",
                
                case when pls.Polis_endDate is not null and pls.Polis_endDate <= (select dt from mv)
then 'orange'
else case when ps.PersonCloseCause_id = 2 and ps.Person_closeDT is not null
        then 'red'
        else case when ps.Server_pid = 0
                then 'true'
                else 'false'
        end
end
                end as "Person_IsBDZ",
            
                coalesce(VT.VizitType_Name,'') as "VizitType_Name",
                to_char(ecp.EvnCostPrint_setDT, 'DD.MM.YYYY') as "EvnCostPrint_setDT",
                case when ecp.EvnCostPrint_IsNoPrint = 2 then 'Отказ от справки' when ecp.EvnCostPrint_IsNoPrint = 1 then 'Справка выдана' else '' end as "EvnCostPrint_IsNoPrintText"
            ,case when SES1.ServiceEvnStatus_SysNick in ('sendegis','loadegis') then 'true' else 'false' end as "fedservice_iemk"
,RTRIM(PS.Person_SurName) as "Person_Surname"
,RTRIM(PS.Person_FirName) as "Person_Firname"
,RTRIM(PS.Person_SecName) as "Person_Secname"
,to_char(PS.Person_Birthday, 'DD.MM.YYYY') as "Person_Birthday"
,to_char(PS.Person_deadDT, 'DD.MM.YYYY') as "Person_deadDT"
                
            
                FROM
                
        
                    v_EvnPL EPL
                        inner join v_EvnVizitPL  on v_EvnVizitPL.EvnVizitPL_pid = EPL.EvnPL_id and  v_EvnVizitPL.Lpu_id = '13101853'
                        inner join v_PersonState PS on  EPL.Person_id = PS.Person_id
                        inner join Evn on Evn.Evn_id = EPL.EvnPL_id and Evn.EvnClass_id = 3 and Evn.Evn_deleted = 1 and Evn.Lpu_id = '13101853'
                        left join v_Polis pls on pls.Polis_id = ps.Polis_id
                        -- left join NationalityStatus ns on ns.NationalityStatus_id = ps.NationalityStatus_id
                        left join lateral(
                            select
                                EvnPLBase.EvnPLBase_IsFinish,
                                EvnPLBase.EvnPLBase_VizitCount
                            from v_EvnPLBase EvnPLBase
                            where EvnPLBase.EvnPLBase_id = Evn.Evn_id
                            limit 1
                             ) as EvnPLBase on true
                
                        left join lateral(
                            select
                                    EVPL.EvnVizitPL_id,
                                    EVPL.HealthKind_id,
                                    EVPL.VizitType_id,
                                    EVPL.MedPersonal_id,
                                    EVPL.Lpu_id,
                                    EVPL.Diag_id,
                                    EVPL.LpuSection_id,
                                    EVPL.DeseaseType_id,
                                    EVPL.MedPersonal_sid,
                                    EVPL.EvnVizitPL_isPaid,
                                    EVPL.ServiceType_id,
                                    EVPL.TreatmentClass_id,
                                    EVPL.EvnVizitPL_setDT,
                                    EVPL.UslugaComplex_id,
                                    EVPL.EvnVizitPL_IsInReg,
                                    EVPL.VizitClass_id
                            from v_EvnVizitPL EVPL
                            where EVPL.EvnVizitPL_pid = EPL.EvnPL_id and
                                  EVPL.EvnVizitPL_Index = coalesce(EVPL.EvnVizitPL_IndexMinusOne, 0)
                            limit 1
                        ) as EVPL on true
                        left join v_Diag EVPLD on EVPLD.Diag_id = EPL.Diag_id
                        left join v_YesNo IsFinish on IsFinish.YesNo_id = EPL.EvnPL_IsFinish
                        left join v_HealthKind as HK on HK.HealthKind_id = EVPL.HealthKind_id
                        left join v_VizitType VT on VT.VizitType_id = EVPL.VizitType_id
                        left join Lpu dlpu on dlpu.Lpu_id = EPL.Lpu_did
                        left join lateral(
                            select MP.Person_Fio, MP.MedPersonal_id
                            from v_MedPersonal MP
                            where MP.MedPersonal_id = EPL.MedPersonal_id and MP.Lpu_id = EPL.Lpu_id
                            limit 1 ) as MP on true
                        
                inner join lateral(
                    select
                        EVPL.EvnVizitPL_id,
                        EVPL.EvnVizitPL_pid
                    from
                        v_EvnVizitPL EVPL
                        
                    where EVPL.Lpu_id = '13101853'
                     and EvnVizitPL_pid = EPL.EvnPL_id limit 1 
                    ) as EVPL2 on true
            
                 LEFT JOIN LATERAL (
                SELECT
                    EvnCostPrint_setDT,
                    EvnCostPrint_IsNoPrint
                FROM v_EvnCostPrint ecp
                where ecp.Evn_id = EPL.EvnPL_id
                LIMIT 1
            ) ecp on true
            inner join v_LpuSection LS on LS.LpuSection_id = EPL.LpuSection_id
            inner join LpuUnit LU on LU.LpuUnit_id = LS.LpuUnit_id
            left join lateral(
                select ServiceEvnStatus_id
                from v_ServiceEvnHist
                where Evn_id = EPL.EvnPL_id and ServiceEvnList_id = 1
                order by ServiceEvnHist_id desc
                limit 1
            ) as SEH1 on true
            left join v_ServiceEvnStatus SES1 on SES1.ServiceEvnStatus_id = SEH1.ServiceEvnStatus_id
        
                         WHERE 
                        (1 = 1) and MP.MedPersonal_id is not NULL  and EPL.Lpu_id = '13101853'  and exists (select AR.Address_id from Address AR  where AR.Address_id = PS.UAddress_id and AR.KLCountry_id = '643' and AR.KLRgn_id = '89' and AR.KLSubRgn_id = '1963' and AR.KLCity_id = '3739' and AR.KLStreet_id = '654736' and AR.Address_House = '17')  and coalesce(EVPLD.Diag_Code, '') not in('B18.0','B18.1','B17.1','B18.2','Z11.3') and coalesce(EVPLD.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(EVPLD.Diag_Code, '') not between 'A50.0' and 'A64' and coalesce(EVPLD.Diag_Code, '') not between 'B16.0' and 'B16.9' and coalesce(EVPLD.Diag_Code, '') not between 'A15.0' and 'A19.9'
                        
                
                        ORDER BY
                            
                        EPL.EvnPL_id
                        
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
