
            --variables
            with mv as (
                select
                localtimestamp as dt
            )
            --end variables
        
            select
                 count( distinct EUP.EvnUslugaPar_id  ) AS cnt 
            from
                -- from
         v_EvnUslugaPar EUP  inner join dbo.v_PersonState PS on EUP.Person_id = PS.Person_id
            left join v_LpuSection lpusec on lpusec.LpuSection_id = EUP.LpuSection_did
            left join v_EvnCostPrint ecp on ecp.Evn_id = EUP.EvnUslugaPar_id
            left join lateral(
                select Person_Fio
                from v_MedPersonal
                where MedPersonal_id = EUP.MedPersonal_id
                limit 1
            ) MP on true
            left join Usluga on Usluga.Usluga_id = EUP.Usluga_id
            left join v_UslugaComplex UslugaComplex on UslugaComplex.UslugaComplex_id = EUP.UslugaComplex_id
            left join v_EvnUslugaPar EvnParent on EvnParent.EvnUslugaPar_id = EUP.EvnUslugaPar_pid
            left join v_PayType PT on PT.PayType_id = EUP.PayType_id
            left join v_MedStaffFact MedStaffFact on MedStaffFact.MedStaffFact_id = EUP.MedStaffFact_id
            left join v_LpuSection LS on LS.LpuSection_id = coalesce(EUP.LpuSection_uid, MedStaffFact.LpuSection_id)
            left join v_Lpu lpu on lpu.Lpu_id = LS.Lpu_id
            left join v_PostMed PostMed on PostMed.PostMed_id = MedStaffFact.Post_id
            left join v_EvnDirection_all ED on EUP.EvnDirection_id = ED.EvnDirection_id
            left join v_Lpu LD on LD.Lpu_id = ED.Lpu_sid
            left join v_Org Org on Org.Org_id = ED.Org_sid
            left join v_Lpu L ON L.Lpu_id = ED.Lpu_sid -- NGS
            left join v_Lpu L2 ON L2.Lpu_id = EUP.Lpu_did -- NGS
            left join v_Lpu L3 on L3.Org_id = EUP.Org_uid
            left join v_Org O2 on O2.Org_id = EUP.Org_did
        
                left join lateral(
                    select EvnXml.EvnXml_id, EvnXml.EvnXml_IsSigned
                    from v_EvnXml  EvnXml
                    where Evn_id = EUP.EvnUslugaPar_id
                    order by EvnXml_insDT desc
                    limit 1
                ) doc on true
            
                    left join v_evnprescr ep on ep.evnprescr_id = eup.evnprescr_id
                    left join v_evnfuncrequest efr on efr.evnfuncrequest_pid = eup.evndirection_id
                    left join lateral(
                        select ucat.uslugacomplexattributetype_sysnick
                        from v_uslugacomplexattribute uca
                            left join v_uslugacomplexattributetype ucat
                                on ucat.uslugacomplexattributetype_id = uca.uslugacomplexattributetype_id
                         where uca.uslugacomplex_id = eup.uslugacomplex_id
                            and ucat.uslugacomplexattributetype_sysnick = 'func'
                    ) as ucat on true
                
                        -- end from
                    where
                        -- where
                        (1 = 1) and (
                EUP.Lpu_id = '9990000179'
                OR EUP.Lpu_uid = '9990000179'
            ) and eup.EvnUslugaPar_setDate is not null
                    and exists(
                        select Polis_id
                        from
                            Polis
                            left join OmsSprTerr on OmsSprTerr.OmsSprTerr_id = Polis.OmsSprTerr_id
                        where Polis.Polis_id = PS.Polis_id
                 and Polis.OmsSprTerr_id = '2505')  and EvnParent.EvnUslugaPar_id is null
                        -- end where
                
                    
                
