
            select
                
            case when (
                case
                    when ESB.Lpu_id = '13101853' and (ESB.MedPersonal_id is null or ESB.Lpu_outid = '13101853' and ESB.MedPersonal_id in (590101000001302)) then 1
when ESB.Lpu_id = '13101853' then 1
when exists (select EvnStickWorkRelease_id from v_EvnStickWorkRelease ESWR where ESWR.EvnStickBase_id = ESB.EvnStickBase_id and ESWR.Org_id = 13102584 limit 1) then 1
when exists (select EL.EvnLink_id from v_EvnLink EL inner join lateral (select E.Lpu_id from v_Evn E where E.Evn_id = EL.Evn_id limit 1) E on true where EL.Evn_lid = ESB.EvnStickBase_id and E.Lpu_id =  '13101853' limit 1) then 1
when exists(
                select
                    EvnStickWorkRelease_id
                from
                    v_EvnStickWorkRelease ESWRs
                    inner join v_MedStaffFact MSFs on MSFs.MedStaffFact_id = ESWRs.MedStaffFact_id
                where
                    ESWRs.EvnStickBase_id = ESB.EvnStickBase_id
                    and ESWRs.MedPersonal_id = 590101000001302
                    and MSFs.Lpu_id = '13101853'
                limit 1
            ) then 1
when exists (
                select EvnStickWorkRelease_id
                from v_EvnStickWorkRelease ESWR
                inner join v_Lpu lpu on lpu.Org_id = ESWR.Org_id
                where
                    ESWR.EvnStickBase_id = ESB.EvnStickBase_id
                    and lpu.Lpu_id in (13101853)
                )
             then 1
                    else 0
                end = 1
            )
             and coalesce(ESB.EvnStickBase_IsInReg, 1) = 1 and coalesce(ESB.EvnStickBase_IsPaid, 1) = 1 and coalesce(SFT.StickFSSType_Code, '0') != '040' and coalesce(ESB.EvnStickBase_IsDelQueue, 1) = 1 and coalesce(ESB.EvnStickBase_IsInRegDel, 1) = 1
            then 'edit' else 'view' end as "accessType", -- фильтры доступа для ЛВН
        
            case when (
                case
                    when ESB.MedPersonal_id is not null and ESB.MedPersonal_id in (590101000001302) then 1
when ESB.MedPersonal_id is null and (select MedPersonal_id from v_EvnStickWorkRelease where EvnStickBase_id = ESB.EvnStickBase_id order by EvnStickWorkRelease_begDT desc limit 1) in (590101000001302) then 1
when ESB.MedPersonal_id is null and (select MedPersonal2_id from v_EvnStickWorkRelease where EvnStickBase_id = ESB.EvnStickBase_id order by EvnStickWorkRelease_begDT desc limit 1) in (590101000001302) then 1
when ESB.MedPersonal_id is null and (select MedPersonal3_id from v_EvnStickWorkRelease where EvnStickBase_id = ESB.EvnStickBase_id order by EvnStickWorkRelease_begDT desc limit 1) in (590101000001302) then 1
                    else 0
                end = 1
            )
            and (
                case
                    
            when ESB.EvnStickBase_IsFSS = 2
             OR exists(
            select
                RegistryESStorage_id
            from
                v_RegistryESStorage
            where
                EvnStickBase_id = ESB.EvnStickBase_id
            limit 1
        )
             then
                case
                    when
                        coalesce(ESB.EvnStickBase_IsPaid, 1) = 1 and coalesce(ESB.EvnStickBase_IsInReg, 1) = 1
                        and not exists (
                            select EvnStickWorkRelease_id
                            from v_EvnStickWorkRelease
                            where
                                EvnStickBase_id = ESB.EvnStickBase_id
                                and (EvnStickWorkRelease_IsInReg = 2 or EvnStickWorkRelease_IsPaid = 2
                                    or coalesce(ESB.EvnStickBase_IsFSS, 1) = 1 and (EvnStickWorkRelease_IsBegDTReg = 2 or EvnStickWorkRelease_IsBegDTFSS = 2)
                                )
                            limit 1
                        )
                    then 1
                    else 0
                end
        
                    else 1
                end = 1
            )
             and coalesce(SFT.StickFSSType_Code, '0')!= '040'
            then 'edit' else 'view' end as "delAccessType", -- фильтры доступа для удаления ЛВН
        
            case when (
                case
                    when ESB.EvnStickBase_IsInReg = 2 or ESB.EvnStickBase_IsPaid = 2 then 1

        when exists(
            select EvnStickWorkRelease_id
            from
                v_EvnStickWorkRelease
            where
                EvnStickBase_id = ESB.EvnStickBase_id
                and (
                    EvnStickWorkRelease_IsInReg = 2 or EvnStickWorkRelease_IsPaid = 2
                    or coalesce(ESB.EvnStickBase_IsFSS, 1) = 1 and (EvnStickWorkRelease_IsBegDTReg = 2 or EvnStickWorkRelease_IsBegDTFSS = 2)
                )
            limit 1
        ) then 1
                    else 0
                end = 1
            )
            
            and (
                COALESCE(ESB.EvnStickBase_IsFSS, 1) = 2
                or exists(
                    select
                        RegistryESStorage_id
                    from
                        v_RegistryESStorage
                    where
                        EvnStickBase_id = ESB.EvnStickBase_id
                    limit 1
                )
            )
        
            then 'edit' else 'view' end as "cancelAccessType", -- фильтры доступа для аннулирования ЛВН
        
            case when (
                case
                    when coalesce(ESB.EvnStickBase_IsFSS, 1) = 1 then 1
                    else 0
                end = 1
            )
            then 'edit' else 'view' end as "addWorkReleaseAccessType", -- фильтры доступа для добавления освобождений
        
                ESB.Lpu_id as "Lpu_id",
                ESB.EvnStickBase_id as "EvnStick_id",
                ESB.EvnStickBase_pid as "EvnStick_pid",
                ESB.EvnStickBase_Num as "EvnStick_Num",
                ESB.EvnStickBase_Ser as "EvnStick_Ser",
                to_char(ESB.EvnStickBase_insDT,'dd.mm.yyyy') as "sortdate",
                RTRIM(COALESCE(SO.StickOrder_Name, '')) as "StickOrder_Name",
                case
                    when ESB.EvnStickBase_IsFSS = 2 then SOF.StickOrder_Name
                    else SO.StickOrder_Name
                end as StickOrder_Name,
                --convert(varchar(10), ESB.EvnStickBase_setDT, 104) as "EvnStick_setDate", -- Дата выдачи
                TO_CHAR (EBWR_d.evnStickWorkRelease_begDT,'dd.mm.yyyy') as "EvnStick_setDate",
                case
                    when ESB.EvnStickBase_mid = '890101029047790' then 'Текущий'
                    when EvnPL.EvnPL_id is not null then 'ТАП'
                    when EvnPLStom.EvnPLStom_id is not null then 'Стом. ТАП'
                    when EvnPS.EvnPS_id is not null then 'КВС'
                    else ''
                end as "EvnStick_ParentTypeName", -- тип родительского документа
                case
                    when ESB.EvnStickBase_mid = '890101029047790' then ''
                    when EvnPL.EvnPL_id is not null then EvnPL.EvnPL_NumCard
                    when EvnPLStom.EvnPLStom_id is not null then EvnPLStom.EvnPLStom_NumCard
                    when EvnPS.EvnPS_id is not null then EvnPS.EvnPS_NumCard
                    else ''
                end as "EvnStick_ParentNum", -- номер родительского документа
                case
                    when ESB.EvnStickBase_mid = '890101029047790' then ''
                    when EvnPL.EvnPL_id is not null then to_char (EvnPL.EvnPL_setDT, 'dd.mm.yyyy')
                    when EvnPLStom.EvnPLStom_id is not null then to_char (EvnPLStom.EvnPLStom_setDT, 'dd.mm.yyyy')
                    when EvnPS.EvnPS_id is not null then to_char (EvnPS.EvnPS_setDT, 'dd.mm.yyyy')
                    else ''
                end as "EvnStick_ParentDate", -- дата родительского документа
                '890101029047790' as "Evn_pid",
                SC.StickCause_Code as "StickCause_Code",
                1 as "evnStickType",
                case when ESB.EvnStickBase_disDT is not null then 1 else 0 end as "EvnStick_closed",
                case when ESB.EvnStickBase_disDT is not null then 'ЛВН закрыт' else 'ЛВН открыт' end as "EvnStick_closedName",
                to_char (ESB.EvnStickBase_disDT, 'dd.mm.yyyy') as "EvnStick_disDate"
                
            from v_EvnStickBase ESB
             	inner join EvnClass EC on EC.EvnClass_id = ESB.EvnClass_id
                left join lateral (
                    select
                        case when SFDG.StickFSSDataGet_StickFirstNum is null then 1 else 2 end as StickOrder_id
                    from
                        v_StickFSSDataGet SFDG
                    where
                        ESB.EvnStickBase_id = SFDG.EvnStickBase_id
                    limit 1
                ) FSS_DATA on true
                left join v_StickOrder SOF on SOF.StickOrder_id = FSS_DATA.StickOrder_id
                left join StickOrder SO on SO.StickOrder_id = ESB.StickOrder_id
                left join StickWorkType SWT on SWT.StickWorkType_id = ESB.StickWorkType_id
                left join v_MedStaffFact MSF on MSF.MedStaffFact_id = ESB.MedStaffFact_id
                left join v_StickFSSType SFT on SFT.StickFSSType_id = ESB.StickFSSType_id
                -- ТАП/КВС
                left join v_EvnPL EvnPL on ESB.EvnStickBase_mid = EvnPL.EvnPL_id
                left join v_EvnPLStom EvnPLStom on ESB.EvnStickBase_mid = EvnPLStom.EvnPLStom_id
                left join v_EvnPS EvnPS on ESB.EvnStickBase_mid = EvnPS.EvnPS_id
                left join v_StickCause SC on SC.StickCause_id = ESB.StickCause_id
                left join lateral (
                    select Org_id as Org_id
                    from v_EvnStickWorkRelease ESWR
                    where ESB.EvnStickBase_id = ESWR.EvnStickBase_id and ESWR.Org_id = 13102584
                    limit 1
                ) ESWR on true
                left join lateral(
                    select evnStickWorkRelease_begDT as evnStickWorkRelease_begDT
                    from v_EvnStickWorkRelease ESWR
                    where ESB.EvnStickBase_id = ESWR.EvnStickBase_id
                    limit 1
                ) EBWR_d on true
                left join lateral(
                    select RegistryESDataStatus_id as RegistryESDataStatus_id
                    from v_RegistryESData
                    where Evn_id = ESB.EvnStickBase_id
                    order by case when RegistryESDataStatus_id = 2 then 0 else 1 end
                    limit 1
                ) as RESD on true
                -- end ТАП/КВС
            where
                EC.EvnClass_SysNick = 'EvnStick'
                 and ESB.EvnStickBase_id in (
                    select EvnStickbase_id as EvnStickbase_id from v_EvnStickBase where EvnStickBase_mid = '890101029047790'
                    union all
                    select Evn_lid as Evn_lid from EvnLink where Evn_id = '890101029047790'
                )
            --order by ESB.EvnStickBase_insDT desc
        
