
                
            with mv as (
                select
                localtimestamp as dt
            )
            
                
                SELECT 
                -- TOP SL, FIXDATE 18.05.2023, JIRA PROMEDWEB-148004
        
                EVPLS.EvnVizitPLStom_id as "EvnVizitPLStom_id",
                EVPLS.EvnVizitPLStom_pid as "EvnPLStom_id",
                EPLS.Person_id as "Person_id",
                EPLS.PersonEvn_id as "PersonEvn_id",
                EPLS.Server_id as "Server_id",
                RTRIM(EPLS.EvnPLStom_NumCard) as "EvnPLStom_NumCard",
                coalesce((RTRIM(evpldiag.Diag_Code) || '. ' || RTRIM(evpldiag.Diag_Name)),'') as "Diag_Name",
                RTRIM(evplls.LpuSection_Name) as "LpuSection_Name",
                RTRIM(MP.Person_Fio) as "MedPersonal_Fio",
                to_char(EVPLS.EvnVizitPLStom_setDate, 'DD.MM.YYYY') as "EvnVizitPLStom_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",
            
                EVPLS.EvnVizitPLStom_Uet as "EvnVizitPLStom_Uet"
            
,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
                
         dbo.v_PersonState PS 
                    inner join v_EvnVizitPLStom as EVPLS on EVPLS.Person_id = PS.Person_id and EVPLS.Lpu_id = '150471'
                    inner join v_EvnPLStom EPLS on EPLS.EvnPLStom_id = EVPLS.EvnVizitPLStom_pid and EPLS.Lpu_id = '150471' and EPLS.EvnClass_id = 6
                
                    left join v_LpuSection as evplls on evplls.LpuSection_id=EVPLS.LpuSection_id
                    left join v_PayType as evplpt on evplpt.PayType_id=EVPLS.PayType_id
                    left join v_VizitType as evplvt on evplvt.VizitType_id=EVPLS.VizitType_id
                    left join v_ServiceType as evplst on evplst.ServiceType_id=EVPLS.ServiceType_id
                    left join v_Diag as evpldiag on evpldiag.Diag_id = EVPLS.Diag_id
                 left join v_Polis pls on pls.Polis_id = ps.Polis_id
                 left join v_EvnCostPrint ecp on ecp.Evn_id = EPLS.EvnPLStom_id 
            left join lateral(
                select *
                from v_MedPersonal
                where MedPersonal_id = EVPLS.MedPersonal_id
                  and Lpu_id = '150471'
                order by case when Lpu_id = '150471' then 1 else 2 end
                limit 1
            ) as MP on true
        
                         WHERE 
                        (1 = 1)
                and exists (
                    select 1
                    from v_EvnVizitPLStom EVPLS2
                    where (1 = 1) and EVPLS2.Lpu_id = '150471'
             and EVPLS2.EvnVizitPLStom_id = EVPLS.EvnVizitPLStom_id and EVPLS2.LpuSection_id = '312' and EVPLS2.MedStaffFact_id = '23943' and EVPLS2.MedPersonal_id = '19051') and coalesce(evpldiag.Diag_Code, '') not in('R75','Z20.6','Z21','Z71.7','Z83.0') and coalesce(evpldiag.Diag_Code, '') not between 'F00.0' and 'F99' and coalesce(evpldiag.Diag_Code, '') not between 'B20.0' and 'B24'
                        
                
                        ORDER BY
                            
                        EVPLS.EvnVizitPLStom_setDate DESC
                        
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
