
            --variables
            with mv as (
                select
                dbo.tzGetDate() as dt
            ),
            --end variables
        
                -- addit with
                
                                PPPersonCard as (
                                    select distinct PC.Person_id
                                    from PersonCard PC
                                    where (
                                        PC.PersonCard_endDate is null
                                        or PC.PersonCard_endDate::timestamp > (select dt from mv)
                                    )
                                  and PC.Lpu_id = '13100909' 
                                 )
                            
                -- end addit with
            
            select
                -- select
        
            PP.Lpu_id as "Lpu_id",
            PS.Server_id as "Server_id",
            PS.Lpu_id as "Lpu_did",
            PS.PersonEvn_id as "PersonEvn_id",
            PS.Person_id as "Person_id",
            PP.PersonPrivilege_id as "PersonPrivilege_id",
            PT.PrivilegeType_id as "PrivilegeType_id",
            RF.ReceptFinance_id as "ReceptFinance_id",
            RF.ReceptFinance_Code as "ReceptFinance_Code",
            PT.PrivilegeType_Code as "PrivilegeType_Code",
            RTRIM(coalesce(PT.PrivilegeType_VCode, PT.PrivilegeType_Code::varchar)) as "PrivilegeType_VCode",
            to_char(PP.PersonPrivilege_begDate, 'DD.MM.YYYY') as "Privilege_begDate",
            to_char(PP.PersonPrivilege_endDate, 'DD.MM.YYYY') as "Privilege_endDate",
            PT_WDCIT.WhsDocumentCostItemType_Nick as "WhsDocumentCostItemType_Nick",
            CASE WHEN PS.Person_IsRefuse = 1 and PT_WDCIT.WhsDocumentCostItemType_Nick = 'fl' THEN 'true' ELSE 'false' END as "Person_IsRefuse",
            CASE WHEN PS.Person_IsFedLgot = 1 THEN 'true' ELSE 'false' END as "Person_IsFedLgot",
            CASE WHEN PS.Person_IsRegLgot = 1 THEN 'true' ELSE 'false' END as "Person_IsRegLgot",
            CASE WHEN PS.Person_Is7Noz = 1 THEN 'true' ELSE 'false' END as "Person_Is7Noz",
            
                case when pls.Polis_endDate is not null and pls.Polis_endDate <= (select dt from mv)
then 'orange'
else case when ps.PersonCloseCause_id = 2 and ps.Person_closeDT is not null
        then 'red'
        else case when ps.Server_pid = 0
                then 'true'
                else 'false'
        end
end
                end as "Person_IsBDZ",
            
            coalesce(PUAdd.Address_Nick, PUAdd.Address_Address) as "Person_Address",
            to_char(PP.PersonPrivilege_delDT, 'DD.MM.YYYY HH24:MI:SS')||rtrim(UserDel.PMUser_Login)||', '||rtrim(UserDel.PMUser_Name) as "PersonPrivilege_deletedInfo",
            coalesce(PrivCT.PrivilegeCloseType_Name, '') as "PrivilegeCloseType_Name",
            coalesce(DocPriv.DocumentPrivilege_Data, '') as "DocumentPrivilege_Data",
            PersonRefuse.IDs as "PersonRefuse_IDs",
            PersonRefuse.Years as "PersonRefuse_Years",
            PersonRefuse.IsRefuses as "PersonRefuse_IsRefuses"
        ,0 as "cntPC",RTRIM(PT.PrivilegeType_Name) || coalesce('<br>Диагноз: ' || Diag.Diag_Code || ' ' || Diag.Diag_Name, '') as "PrivilegeType_Name"
                ,RTRIM(PS.Person_SurName) as "Person_Surname"
                ,RTRIM(PS.Person_FirName) as "Person_Firname"
                ,RTRIM(PS.Person_SecName) as "Person_Secname"
                ,to_char(PS.Person_Birthday, 'DD.MM.YYYY') as "Person_Birthday"
                ,dbo.Age2(PS.Person_BirthDay, (select dt from mv)) as "Person_Age"
                ,to_char(PS.Person_deadDT, 'DD.MM.YYYY') as "Person_deadDT"
            
            ,FRLLOA.FRLLO_RegNum as "FRLLOPersonPrivilege_RegNum"
        
            -- end select
            from
                -- from
        
                	    v_PersonState_all PS
                    
                            inner join PPPersonCard on PPPersonCard.person_id=PS.person_id
                        
                inner join PersonPrivilege PP on PP.Person_id = PS.Person_id
                inner join lateral (
                    select
                        _PT.PrivilegeType_id,
                        _PT.PrivilegeType_Code,
                        _PT.PrivilegeType_VCode,
                        _PT.PrivilegeType_Name,
                        _PT.WhsDocumentCostItemType_id,
                        _PT.ReceptFinance_id
                    from
                        v_PrivilegeType _PT
                    where
                        _PT.PrivilegeType_id = PP.PrivilegeType_id
                    limit 1
                ) PT on true
                left join LATERAL  (select * from v_Polis pls where  pls.Polis_id = ps.Polis_id limit 1) pls on true
                left join LATERAL  (select * from v_WhsDocumentCostItemType PT_WDCIT where PT_WDCIT.WhsDocumentCostItemType_id = PT.WhsDocumentCostItemType_id limit 1) PT_WDCIT on true
                left join  LATERAL  (select * from v_ReceptFinance RF where RF.ReceptFinance_id = PT.ReceptFinance_id limit 1) RF on true
                left join  LATERAL  (select * from v_Address PUAdd where PUAdd.Address_id = coalesce(PS.PAddress_id, PS.UAddress_id) limit 1) PUAdd on true
                left join  LATERAL  (select * from v_PMUserCache UserDel where UserDel.pmUser_id = PP.pmUser_delID limit 1) UserDel on true
                left join  LATERAL  (select * from v_PrivilegeCloseType PrivCT where PrivCT.PrivilegeCloseType_id = PP.PrivilegeCloseType_id limit 1) PrivCT on true
                left join lateral(
                    select
                        (
                            i_DocPriv.DocumentPrivilege_Ser || ' ' ||
                            i_DocPriv.DocumentPrivilege_Num || ' ' ||
                            to_char(i_DocPriv.DocumentPrivilege_begDate, 'DD.MM.YYYY') || ' ' ||
                            coalesce(i_Org.Org_Nick, i_DocPriv.DocumentPrivilege_Org, '')
                        ) as DocumentPrivilege_Data
                    from
                     	v_DocumentPrivilege i_DocPriv
                        left join lateral (
                            select * from v_Org i_Org
                            where i_Org.Org_id = i_DocPriv.Org_id limit 1
                        ) i_Org on true
                    where
                        i_DocPriv.PersonPrivilege_id = PP.PersonPrivilege_id
                    order by
                        i_DocPriv.DocumentPrivilege_id
                    limit 1
                ) as DocPriv on true
                left join lateral (
                    select
                        string_agg(CAST(_vPP.PersonRefuse_id as varchar), ',') AS IDs,
                        string_agg(CAST(_vPP.PersonRefuse_Year as varchar), ',') AS Years,
                        string_agg(CAST(YN.YesNo_Code as varchar), ',') AS IsRefuses
                    from
                        v_PersonRefuse AS _vPP,
                        v_YesNo AS YN
                    where 1=1
                        and _vPP.Person_id = PP.Person_id
                        and _vPP.PersonRefuse_IsRefuse = YN.YesNo_id
                        and(
                            _vPP.PersonRefuse_Year = (select YEAR(dt) from mv)
                            or _vPP.PersonRefuse_Year = (select YEAR(dt)+1 from mv)
                        )
                ) PersonRefuse on true
             left join lateral (
                    SELECT Diag_Code, Diag_Name FROM v_Diag WHERE Diag_id = PP.Diag_id LIMIT 1
                ) Diag ON TRUE
                LEFT JOIN LATERAL (
                    SELECT
                        fr.FRLLO_RegNum AS FRLLO_RegNum
                    FROM  v_FRLLOPersonPrivilegeLink vFPPL
                        LEFT JOIN dbo.v_FRLLO AS fr ON fr.frllo_id = vFPPL.frllo_id
                    WHERE vFPPL.personprivilege_id = PP.PersonPrivilege_id AND
                        fr.FRLLO_insDt = (
                            SELECT MAX(FRLLO_insDt)
                            FROM v_FRLLO b
                            WHERE b.Person_id = PS.Person_id AND b.FRLLO_RegNum IS NOT NULL and
                                  b.FRLLO_insDt <= (select dt from mv))
                    LIMIT 1
                ) AS FRLLOA ON TRUE
            
                        -- end from
                    where
                        -- where
                        (1 = 1) and (PP.PersonPrivilege_deleted = 1 or PP.PersonPrivilege_deleted IS NULL)
                and PP.PersonPrivilege_begDate is not null
                and PP.PersonPrivilege_begDate <= (select dt from mv)
                and (PP.PersonPrivilege_endDate is null or PP.PersonPrivilege_endDate > (select dt from mv))
              and PT.PrivilegeType_id not in('330','306','328','332','40','2746','314')
                        -- end where
                
                    order by
                        -- order by
                        Person_Surname,
                        Person_Firname,
                        Person_Secname,
                        PP.PersonPrivilege_id
                        -- end order by
                
