
                
            with mv as (
                select
                localtimestamp as dt
            )
            
                
                SELECT 
                -- TOP SL, FIXDATE 18.05.2023, JIRA PROMEDWEB-148004
        
            EPLDP.EvnPLDispProf_id as "EvnPLDispProf_id",
            coalesce(EPLDP.EvnPLDispProf_IsTransit, 1) as "EvnPLDispProf_IsTransit",
            PS.Person_id as "Person_id",
            PS.Server_id as "Server_id",
            EPLDP.PersonEvn_id as "PersonEvn_id",
            coalesce(UAdd.Address_Nick, UAdd.Address_Address) as "ua_name",
            coalesce(PAdd.Address_Nick, PAdd.Address_Address) as "pa_name",
            IsFinish.YesNo_Name as "EvnPLDispProf_IsEndStage",
            to_char(EPLDP.EvnPLDispProf_consDT, 'DD.MM.YYYY') as "EvnPLDispProf_setDate",
            to_char(EPLDP.EvnPLDispProf_disDate, 'DD.MM.YYYY') as "EvnPLDispProf_disDate",
            HK.HealthKind_Name as "EvnPLDispProf_HealthKind_Name",
            EPLDP.EvnPLDispProf_isSigned as "EvnPLDispProf_isSigned",
            case when EPLDP.EvnPLDispProf_IsRefusal = 2 then to_char(EPLDP.EvnPLDispProf_consDT, 'DD.MM.YYYY') else null end as "EvnPLDispProf_rejDate",
            case
                when exists (select EvnPLDispProf_id from v_EvnPLDispProf where Person_id = PS.Person_id and date_part('year', EvnPLDispProf_setDate) = 2023 and Lpu_id != '13102006' limit 1) then 4
                when EPLDP.EvnPLDispProf_id is null then 0
                when EPLDP.Lpu_id = '13102006' then 0
                when EPLDP.Lpu_id = '13102006' and coalesce(EPLDP.EvnPLDispProf_IsTransit, 1) = 2 then 0
                else 4
            end as "AccessType_Code",
            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"
        
                ,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"
            
            
                FROM
                
         dbo.v_PersonState PS 
                inner join v_EvnPLDispProf EPLDP on PS.Person_id = EPLDP.Person_id
                    and date_part('year', EvnPLDispProf_setDate) = 2023
            
            inner join v_Lpu L on L.Lpu_id = EPLDP.Lpu_id
            left join v_EvnCostPrint ecp on ecp.Evn_id = EPLDP.EvnPLDispProf_id
            left join YesNo IsFinish on IsFinish.YesNo_id = coalesce(EPLDP.EvnPLDispProf_IsEndStage, 1)
            left join v_HealthKind HK on HK.HealthKind_id = EPLDP.HealthKind_id
            left join v_Address UAdd on UAdd.Address_id = ps.UAddress_id
                left join v_Address PAdd on PAdd.Address_id = ps.PAddress_id
        
                         WHERE 
                        (1 = 1) and coalesce(EPLDP.EvnPLDispProf_IsEndStage, 1) = '2'  and ( exists (
                        select
                            PC.personcard_id
                        from
                            
                                v_PersonCard_all PC
                                left join v_LpuRegion LR on LR.LpuRegion_id = PC.LpuRegion_id
                                left join v_LpuRegion LR_Fap on LR_Fap.LpuRegion_id = PC.LpuRegion_fapid
                            
                        where
                            PC.Person_id = PS.Person_id and PC.Lpu_id = '13102006'
                        limit 1
                    ))  and EPLDP.EvnPLDispProf_insDT::date >= '2023-07-01'::date and EPLDP.EvnPLDispProf_insDT::date <= '2023-07-31'::date
                        
                
                        ORDER BY
                            
                        
                        PS.Person_SurName,
                        PS.Person_FirName,
                        PS.Person_SecName
                        
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
