
        Select
            EvD.MedPersonal_id as "ED_MedPersonal_id",
            case when EvnUslugaPar.Lpu_id = '180101000000012' and EvnUslugaPar.MedPersonal_id = MSF.MedPersonal_id and EvnUslugaPar.LpuSection_uid = MSF.LpuSection_id then 'edit' else 'view' end as "accessType",
            EvnUslugaPar.Lpu_id as "Lpu_id",
            COALESCE(EvnUslugaPar.Lpu_did, EvnUslugaPar.Lpu_id) as "AccessLpu_id",
            EvnUslugaPar.EvnUslugaPar_id as "EvnUslugaPar_id",
            EvnUslugaPar.EvnClass_Name as "EvnClass_Name",
            EvnUslugaPar.EvnUslugaPar_setDate as "EvnUslugaPar_setDate",
            EvnUslugaPar.EvnUslugaPar_setTime as "EvnUslugaPar_setTime",
            Usluga.Usluga_id as "Usluga_id",
            coalesce(UslugaComplex.UslugaComplex_Code, Usluga.Usluga_Code) as "Usluga_Code",
            coalesce(ucms.UslugaComplex_Name, UslugaComplex.UslugaComplex_Name, Usluga.Usluga_Name) as "Usluga_Name",
            PayType.PayType_SysNick as "PayType_SysNick"
            ,coalesce(MP.Person_FIO,'') as "MedPersonal_Fio"
            ,coalesce(LS.LpuSection_Name,'') as "LpuSection_Name"
            ,coalesce(Lpu.Lpu_Nick,'') as "Lpu_Name"
            ,case when exists (
                select 1
                from dbo.v_MedService vMS
                inner join dbo.v_MedServiceType vMST on vMST.MedServiceType_id = vMS.MedServiceType_id
                where vMS.MedService_id = ELS.MedService_id and vMST.MedServiceType_SysNick = 'microbiolab'
                limit 1
            ) THEN 2
                ELSE 1
            end as "isMicroLab"
            
        from v_EvnUslugaPar EvnUslugaPar
            
            left join lateral (
                select *
                from v_EvnDirection_all EvD
                where EvD.EvnDirection_id = EvnUslugaPar.EvnDirection_id
                limit 1
            ) EvD on true
            
                LEFT JOIN LATERAL (
                    select
                        UT.UslugaComplex_id as UslugaComplex_id
                    from
                        v_UslugaTest UT
                    where
                        UT.UslugaTest_pid = EvnUslugaPar.EvnUslugaPar_id
                        and coalesce(UT.UslugaComplex_id, 0) in('224638')
                    LIMIT 1
                ) as UCp ON TRUE
            
            left join v_Usluga Usluga on Usluga.Usluga_id = EvnUslugaPar.Usluga_id
            left join v_UslugaComplex UslugaComplex on UslugaComplex.UslugaComplex_id = EvnUslugaPar.UslugaComplex_id
            left join v_PayType PayType on PayType.PayType_id = EvnUslugaPar.PayType_id
            left join v_EvnLabSample ELS on ELS.EvnLabSample_id = EvnUslugaPar.EvnLabSample_id
            left join v_UslugaComplexMedService UCMS on UCMS.MedService_id = ELS.MedService_id and UCMS.UslugaComplex_id = UslugaComplex.UslugaComplex_id and UCMS.UslugaComplexMedService_pid is null
            LEFT JOIN LATERAL (
                select MP.Person_FIO as Person_FIO from v_MedStaffFact MP
                where MP.LpuSection_id = coalesce(EvnUslugaPar.LpuSection_uid,ELS.LpuSection_aid)
                    AND MP.MedPersonal_id = coalesce(EvnUslugaPar.MedPersonal_id,ELS.MedPersonal_aid)
                LIMIT 1
            ) MP ON TRUE
            left join v_LpuSection LS on LS.LpuSection_id = coalesce(EvnUslugaPar.LpuSection_uid,ELS.LpuSection_aid)
            left join v_Lpu Lpu on Lpu.Lpu_id = coalesce(EvnUslugaPar.Lpu_uid,ELS.Lpu_aid,EvnUslugaPar.Lpu_id)
            left join v_MedStaffFact MSF on MSF.MedStaffFact_id = '180101000115235'
        where (1=1) 
                    AND EvnUslugaPar.EvnUslugaPar_pid IS NULL
                    and (
                        EvD.EvnDirection_pid IS NULL
                        or not exists (
                            select 1
                            from v_EvnPLDispMigrant
                            where
                                evnpldispmigrant_id = EvnUslugaPar.EvnUslugaPar_pid
                                or evnpldispmigrant_id = EvD.EvnDirection_pid
                            limit 1
                        )
                    )
                 and EvnUslugaPar.Person_id='180101001354777' and ( (coalesce(UslugaComplex.UslugaComplex_id, 0) not in('224638') and UCp.UslugaComplex_id is null)  or EvD.MedPersonal_id = MSF.MedPersonal_id  )
            AND COALESCE(EvD.DirType_id, 0) != 11
            AND EvnUslugaPar.EvnUslugaPar_setDate IS NOT NULL
            AND EvnUslugaPar.EvnClass_id = 47
            and NOT EXISTS (
                SELECT 1
                FROM v_EvnHistologicProto EHP
                WHERE
                    EHP.EvnDirectionHistologic_id = EvnUslugaPar.EvnDirection_id
                    and EHP.EvnHistologicProto_IsBad = 2
                LIMIT 1
            )
        
