
            select
                rr.RegistryRecept_id as "RegistryRecept_id",
                rr.ReceptStatusFLKMEK_id as "ReceptStatusFLKMEK_id"
            from r33.registryrecept rr
                left join lateral (
                    select l.Lpu_Ouz,
                           l.Lpu_id,
                           ldlo.LpuPeriodDLO_begDate,
                           ldlo.LpuPeriodDLO_endDate
                    from
                        dbo.v_LpuPeriodDLO ldlo
                            inner join v_Lpu l on l.Lpu_id = ldlo.Lpu_id
                    where
                            l.Lpu_Ouz = cast(rr.RegistryRecept_LpuMod as integer)
                      and l.Lpu_OGRN = rr.RegistryRecept_LpuOGRN
                ) ldlo on true
                left join lateral (
                    select *
                    from r33.RegistryReceptPerson rrp
                        left join v_PersonState ps on (ps.Person_SurName ilike rrp.RegistryReceptPerson_SurName and
                                                       ps.Person_Snils = rrp.RegistryReceptPerson_Snils)
                    where rr.registryrecept_snils = rrp.RegistryReceptPerson_Snils
                      and rrp.ReceptUploadLog_id = '330101000000216'
                    order by rrp.RegistryReceptPerson_id
                    limit 1
                ) rrp on true
                left join lateral (
                    select
                        PP.PersonPrivilege_id,
                        pt.WhsDocumentCostItemType_id
                    from
                        v_PersonPrivilege PP
                            left join v_PrivilegeType pt on pt.PrivilegeType_id = PP.PrivilegeType_id
                    where
                        PP.Person_id = rrp.Person_id
                            and pt.PrivilegeType_Code = cast(rr.C_Katl as bigint)
                            and cast(PP.PersonPrivilege_begDate as date) <= cast(rr.RegistryRecept_setDT as date)
                            and (
                                cast(PP.PersonPrivilege_endDate as date) >= cast(rr.RegistryRecept_setDT as date)
                                    or PP.PersonPrivilege_endDate is null
                            )
                ) pp on true
                left join lateral (
                    select
                        MP.MedPersonal_id
                    from
                        v_MedPersonal MP
                        left join persis.v_Medworker pMW on pMW.MedWorker_Id = MP.MedPersonal_id
                        left join v_MedStaffFact msf on MP.person_id = msf.person_id
                        left join v_WorkPlace4DloApplyProg wpdap on wpdap.MedStaffFact_id = msf.MedStaffFact_id
                    where
                        pMW.CodeDLO = rr.RegistryRecept_MedPersonalCode
                        and MP.Lpu_id = ldlo.Lpu_id
                        and wpdap.WhsDocumentCostItemType_id = pp.WhsDocumentCostItemType_id
                        and ((wpdap.WorkPlace4DloApplyProg_begDate <= rr.RegistryRecept_setDT) and
                             (wpdap.WorkPlace4DloApplyProg_endDate is null or
                             (wpdap.WorkPlace4DloApplyProg_endDate >= rr.RegistryRecept_setDT)))
                ) mp on true
                left join lateral (
                    select
                        msf.MedPersonal_id
                    from
                        v_medstafffact msf
                    where
                        msf.Lpu_id = ldlo.Lpu_id and
                        msf.MedPersonal_Code = rr.RegistryRecept_MedPersonalCode and
                        msf.WorkData_dlobegDate is not null
                        and ((msf.WorkData_dlobegDate <= rr.RegistryRecept_setDT) and
                             (msf.WorkData_dloendDate is null or (msf.WorkData_dloendDate >= rr.RegistryRecept_setDT ))
                        )
                ) msf on true
            where rr.receptuploadlog_id = '330101000000216'
                and (case when cast(rr.RegistryRecept_ReceptFinance as bigint) in(3,4)
                        then mp.MedPersonal_id
                        else msf.MedPersonal_id end
                    ) is null
        
