
            with curDT as (select localtimestamp as curDate)
            select
                PC.PersonCard_Code as "ID_PAC",
                rtrim(upper(PS.Person_SurName)) as "FAM",
                rtrim(upper(PS.Person_FirName)) as "IM",
                coalesce(rtrim(upper(case when replace(PS.Person_Secname, ' ', '') = '---' or PS.Person_Secname = '' then 'НЕТ' else PS.Person_Secname end)), 'НЕТ') as "OT",
                PS.Sex_id as "W",
                to_char(PS.Person_BirthDay, 'YYYY-MM-DD HH24:MI:SS') as "DR",
                PT.PolisType_CodeF008 as "VPOLIS",
                rtrim(case when PLS.PolisType_id = 4 then '' else PLS.Polis_Ser end) as "SPOLIS",
                rtrim(case when PLS.PolisType_id = 4 then PS.Person_EdNum else PLS.Polis_Num end) as "NPOLIS"
            from
                v_PersonState PS
                inner join v_PersonCard PC  on PC.Person_id = PS.Person_id
                inner join v_Polis PLS  on PLS.Polis_id = ps.Polis_id
                inner join v_PolisType PT  on PT.PolisType_id = PLS.PolisType_id
            where PC.LpuAttachType_id = 1
              and (PC.CardCloseCause_id is null or PC.CardCloseCause_id <> 4)
              and (PLS.Polis_endDate is null or PLS.Polis_endDate > (select curDate from curDT))
              and (
                (PLS.PolisType_id = 4 and 18 <> 2 and PS.Person_EdNum is not null) or
                ((PLS.PolisType_id <> 4 or 18 = 2) and PLS.Polis_Num is not null)
              )
              and PT.PolisType_CodeF008 is not null
              and PC.Lpu_id = '180101000000058'
        
