            with cte as (
                select cast(localtimestamp as date) as curDate
            ), t as (
                select
                    1 as N_ZAP
                    ,L.Lpu_id
                    ,ES.EvnSection_id as id
                    ,'EvnSection' as object
                    ,rmp.RIRMOParameter_id
                    ,rd.RUIDDirection_RUID as RUID_NAPR
                    ,to_char(rmp.RIRMOParameter_disDate, 'YYYY-MM-DD') OLD_D_VYB
                    ,COALESCE(rmp.RIRMOParameter_IsTransfer, 0) as OLD_PR_DVIGEN
                    ,1 as FOR_POM
                    ,L.Lpu_f003mcod as CODE_MO_R
                    ,coalesce(LpuBuildingCode.Value, cast(LB.LpuBuilding_Code as integer)) as KODLPU_R
                    ,case
                        when DATEDIFF('month', PS.Person_BirthDay, EPS.EvnPS_setDate) > 2 or PS.Person_EdNum is not null then '0'
                        else
                            CAST(ps.Sex_id as varchar(1)) ||
                            replace(to_char(PS.Person_BirthDay, 'DD-MM-YY') , '-', '') ||
                            CAST(COALESCE(pc.PersonChild_CountChild, 1) as varchar(1))
                    end as NOVOR
                    ,d.Diag_SCode as DS
                    ,COALESCE(fLSBP.LpuSectionBedProfile_Code, '0') as PROFIL
                    ,LSP.LpuSectionProfile_Code as PODR
                    ,to_char(EPS.EvnPS_setDT, 'YYYY-MM-DD') as D_GOSP
                    ,to_char(ES.EvnSection_disDT, 'YYYY-MM-DD') as D_VYB
                    ,case
                        when EPS.EvnPS_IsCont = 2 then 1
                        else 0
                    end as PR_DVIGEN
                    ,EPS.Person_id
                    ,pr.Person_id as Person_pid
                from
                    v_EvnPS EPS
                    inner join v_PersonState PS  on PS.Person_id = EPS.Person_id
                    inner join v_Lpu L  on L.Lpu_id = EPS.Lpu_id
                    left join v_EvnDirection ED  on ED.EvnDirection_id = EPS.EvnDirection_id
                    left join v_PersonChild pc  on pc.Person_id = EPS.Person_id
                    left join v_BirthSpecStac bss  on bss.BirthSpecStac_id = pc.BirthSpecStac_id
                    left join v_PersonRegister pr  on pr.PersonRegister_id = bss.PersonRegister_id
                    inner join v_EvnSection ES  on ES.EvnSection_pid = EPS.EvnPS_id and COALESCE(es.EvnSection_IsPriem, 1) = 1
                    inner join v_LpuSection LS  on LS.LpuSection_id = ES.LpuSection_id
                    inner join v_LpuBuilding LB  on LB.LpuBuilding_id = LS.LpuBuilding_id
                    inner join v_LpuSectionProfile LSP  on LSP.LpuSectionProfile_id = LS.LpuSectionProfile_id
                    LEFT JOIN LATERAL (
                        select LSBP.*
                        from fed.v_LpuSectionBedProfileLink LSBPL
                        inner join fed.v_LpuSectionBedProfile LSBP  on LSBP.LpuSectionBedProfile_id = LSBPL.LpuSectionBedProfile_fedid
                        where LSBPL.LpuSectionBedProfile_id = COALESCE(ES.LpuSectionBedProfile_id, LS.LpuSectionBedProfile_id)
                        limit 1
                    ) fLSBP ON true
                    inner join v_Diag d  on d.Diag_id = COALESCE(ES.Diag_id, EPS.Diag_pid)
                    left join stg.ServiceListPackage SLP on
                        SLP.ServiceListPackageType_id = '61' and
                        SLP.ServiceListPackage_ObjectID = ES.EvnSection_id and
                        SLP.ServiceListPackage_insDT > ES.EvnSection_updDT and
                        SLP.PackageStatus_id = 5
                    inner join stg.ServiceListPackage SLPH on
                        SLPH.ServiceListPackageType_id = 4 and -- MOTION_IN_HOSPITAL
                        SLPH.ServiceListPackage_ObjectID = ES.EvnSection_id and
                        SLPH.PackageStatus_id = 5
                    inner join r50.RIRMOParameter rmp  on rmp.EvnSection_id = ES.EvnSection_id
                    LEFT JOIN LATERAL (
                        select AV.AttributeValue_ValueInt as Value
                        from v_AttributeSignValue ASV
                        inner join v_AttributeSign ASign on ASign.AttributeSign_id = ASV.AttributeSign_id
                        inner join v_AttributeValue AV on AV.AttributeSignValue_id = ASV.AttributeSignValue_id
                        inner join v_Attribute A on A.Attribute_id = AV.Attribute_id
                        where
                            ASign.AttributeSign_Code = 22
                            and ASV.AttributeSignValue_TablePKey = L.Lpu_id
                            and A.Attribute_SysNick ilike 'LpuBuildingCode'
                            and (select curDate from cte) between ASV.AttributeSignValue_begDate and COALESCE(ASV.AttributeSignValue_endDate,(select curDate from cte))
                        order by
                            ASV.AttributeSignValue_begDate desc,
                            ASV.AttributeSignValue_insDT desc
                        limit 1
                    ) LpuBuildingCode On true
                    inner join r50.RUIDDirection rd on rd.Evn_id = COALESCE(EPS.EvnDirection_id, EPS.EvnPS_id)
                where
                    SLP.ServiceListPackage_id is null
                    and ES.EvnSection_updDT != ES.EvnSection_insDT
                    and EPS.PayType_id = 201
                    and EPS.PrehospWaifRefuseCause_id is null
                    and ES.EvnSection_disDate is not null
                    and nullif(nullif(L.Lpu_f003mcod,'0'),'') is not null
                    
                limit 100
            )

            select
                N_ZAP as "N_ZAP"
                ,RUID_NAPR as "RUID_NAPR"
                ,t.Lpu_id as "Lpu_id"
                ,dsl.DataStorage_Value as "LOGIN"
                ,dsp.DataStorage_Value as "PASSWORD"
                ,id as "id"
                ,object as "object"
                ,OLD_D_VYB as "OLD_D_VYB"
                ,OLD_PR_DVIGEN as "OLD_PR_DVIGEN"
                ,coalesce(ps_old.Person_SurName, PS.Person_SurName) as "OLD_FAM"
                ,coalesce(ps_old.Person_FirName, PS.Person_FirName) as "OLD_IM"
                ,coalesce(ps_old.Person_SecName, PS.Person_SecName) as "OLD_OT"
                ,to_char(coalesce(ps_old.Person_BirthDay, PS.Person_BirthDay), 'YYYY-MM-DD') as "OLD_DR"
                ,FOR_POM as "FOR_POM"
                ,CODE_MO_R as "CODE_MO_R"
                ,KODLPU_R as "KODLPU_R"
                ,case
                    when PS.PolisType_id = 1 then 1
                    when PS.PolisType_id = 3 then 2
                    when PS.PolisType_id = 4 then 3
                end as "VPOLIS"
                ,PS.Polis_Ser as "SPOLIS"
                ,PS.Polis_Num as "NPOLIS"
                ,os.Orgsmo_f002smocod as "SMO"
                ,case when os.Orgsmo_f002smocod is null then os.OrgSMO_Name end as "SMO_NAM"
                ,ost.OMSSprTerr_OKATO as "SMO_OK"
                ,DocTP.DocumentType_Code as "DOCTYPE"
                ,PS.Document_Ser as "DOCSER"
                ,PS.Document_Num as "DOCNUM"
                ,NOVOR as "NOVOR"
                ,PS.Person_SurName as "FAM"
                ,PS.Person_FirName as "IM"
                ,PS.Person_SecName as "OT"
                ,case when ps.Sex_id = 3 then 1 else ps.Sex_id end as "W"
                ,to_char(PS.Person_BirthDay, 'YYYY-MM-DD') as "DR"
                ,PS.Person_Phone as "PHONE"
                ,DS as "DS"
                ,PROFIL as "PROFIL"
                ,PODR as "PODR"
                ,D_GOSP as "D_GOSP"
                ,D_VYB as "D_VYB"
                ,PR_DVIGEN as "PR_DVIGEN"
            from t
                inner join DataStorage dsl  on dsl.Lpu_id = t.Lpu_id and dsl.DataStorage_Name = 'rir_login'
                inner join DataStorage dsp  on dsp.Lpu_id = t.Lpu_id and dsp.DataStorage_Name = 'rir_pass'
                inner join v_PersonState PS  on PS.Person_id = (case when t.NOVOR = '0' then t.Person_id else t.Person_pid end)
                LEFT JOIN LATERAL (
                    select
                        PersonSurName_SurName Person_SurName,
                        PersonFirName_FirName Person_FirName,
                        PersonSecName_SecName Person_SecName,
                        PersonBirthDay_BirthDay Person_BirthDay
                    from v_PersonStateAll psa
                    where psa.Person_id = PS.Person_id and psa.PersonEvn_updDT < CAST(OLD_D_VYB as date)
                    order by psa.PersonEvn_id desc
                    limit 1
                ) ps_old ON true
                left join v_Lpu lpu  on lpu.Lpu_id = PS.Lpu_id
                left join v_Polis p  on ps.Polis_id = p.Polis_id
                left join v_PolisType pt on pt.PolisType_id = p.PolisType_id
                left join v_OrgSmo os  on p.OrgSmo_id = os.OrgSMO_id
                left join v_OMSSprTerr ost  on ost.OMSSprTerr_id = p.OMSSprTerr_id
                left join v_Document Doc  on Doc.Document_id = PS.Document_id
                left join v_DocumentType DocTP  on Doc.DocumentType_id = DocTP.DocumentType_id
        where
            os.OrgSmo_id is not null
            and (PS.Person_EdNum is not null and pt.PolisType_id is not null) OR p.Polis_Num is not null
            and Doc.Document_Num is not null
            and DocTP.DocumentType_Code is not null
