
                
            with mv as (
                select
                dbo.tzGetDate() as dt
            )
            
                
                SELECT 
        
                EVizitPL.EvnVizitPL_id as "EvnVizitPL_id",
                EVizitPL.EvnVizitPL_pid as "EvnPL_id",
                --Evn.Person_id as "Person_id",
                EPL.Person_id as "Person_id",
                --Evn.PersonEvn_id as "PersonEvn_id",
                EPL.PersonEvn_id as "PersonEvn_id",
                --Evn.Server_id as "Server_id",
                EPL.Server_id as "Server_id",
                RTRIM(EPL.EvnPL_NumCard) as "EvnPL_NumCard",
                RTRIM(evpldiag.Diag_Code) || '. ' || RTRIM(evpldiag.Diag_Name) as "Diag_Name",
                RTRIM(evplls.LpuSection_Name) as "LpuSection_Name",
                RTRIM(evplmp.Person_Fio) as "MedPersonal_Fio",
                null as "UslugaComplex_Code",
                to_char(EVizitPL.EvnVizitPL_setDate, 'DD.MM.YYYY') as "EvnVizitPL_setDate",
                RTRIM(evplst.ServiceType_Name) as "ServiceType_Name",
                RTRIM(evplvt.VizitType_Name) as "VizitType_Name",
                RTRIM(evplpt.PayType_Name) as "PayType_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",
            
                RTRIM(evplhk.HealthKind_Name) as "HealthKind_Name"
            
,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"
                ,case when SES1.ServiceEvnStatus_SysNick in ('sendegis','loadegis') then 'true' else 'false' end as "fedservice_iemk"
            
                FROM
                
        
                    v_EvnVizitPL EVizitPL
                     inner join v_PersonState PS on EVizitPL.Person_id = PS.Person_id 
                    inner join v_EvnPL EPL on EVizitPL.EvnVizitPL_pid = EPL.EvnPL_id and  EVizitPL.Lpu_id = '3311074'
                    -- inner join Evn on Evn.Evn_id = EVizitPL.EvnVizitPL_pid and Evn.EvnClass_id = 3 and Evn.Evn_deleted = 1 and Evn.Lpu_id = '3311074'
                    --	inner join v_PersonState PS on  EPL.Person_id = PS.Person_id
                    --	left join v_Polis pls on pls.Polis_id = ps.Polis_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*/
                    
                inner join lateral(
                    select
                        EVPL.EvnVizitPL_id,
                        EVPL.EvnVizitPL_pid
                    from
                        v_EvnVizitPL EVPL
                        
                    where EVPL.Lpu_id = '3311074'
                     and EvnVizitPL_id = EVizitPL.EvnVizitPL_id limit 1 
                    ) as EVPL2 on true
            
                
                    --inner join v_MedPersonal as evplmp on evplmp.MedPersonal_id = EVizitPL.MedPersonal_id and evplmp.Lpu_id = Evn.Lpu_id
                    inner join v_MedPersonal as evplmp on evplmp.MedPersonal_id = EVizitPL.MedPersonal_id and evplmp.Lpu_id = EVizitPL.Lpu_id
                    left join v_Diag as evpldiag on evpldiag.Diag_id = EVizitPL.Diag_id
                    left join v_LpuSection as evplls on evplls.LpuSection_id = EVizitPL.LpuSection_id
                    left join v_PayType as evplpt on evplpt.PayType_id = EVizitPL.PayType_id
                    left join v_VizitType as evplvt on evplvt.VizitType_id = EVizitPL.VizitType_id
                    left join v_ServiceType as evplst on evplst.ServiceType_id = EVizitPL.ServiceType_id
                    left join v_HealthKind as evplhk on evplhk.HealthKind_id = EVizitPL.HealthKind_id
                    left join v_VizitType VT on VT.VizitType_id = EVizitPL.VizitType_id
                 left join v_Polis pls on pls.Polis_id = ps.Polis_id  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 coalesce(evpldiag.Diag_Code, '') not between 'F00.0' and 'F99' and coalesce(evpldiag.Diag_Code, '') not between 'B20.0' and 'B24' and coalesce(evpldiag.Diag_Code, '') not between 'A50.0' and 'A64'
                        
                
                        ORDER BY
                            
                            EVizitPL.EvnVizitPL_setDate DESC
                            
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
