Загрузка данных


do $fix_debit_group$
declare
    v_count integer;
begin
    select count(distinct si.id)
    into v_count
    from (
        values
            (49, '48306'),
            (50, '48301.3'),
            (51, '48306.1'),
            (52, '48306.3'),
            (53, '48309'),
            (54, '60302')
    ) as expected(id, account)
    join ourpension.spr_indicator_bu si
        on si.id = expected.id
    join ourpension.spr_accounts_indicator_bu sa
        on sa.spr_indicator_bu_id = si.id
       and (
           sa.account = expected.account
           or (
               expected.id = 50
               and sa.account = '60323'
           )
       );

    if v_count <> 6 then
        raise exception
            'Показатели 49–54 не соответствуют проверенным счетам';
    end if;

    if exists (
        select 1
        from ourpension.spr_accounts_indicator_bu
        where spr_indicator_bu_id = 49
          and account not in (
              '48301', '48306', '48309',
              '60302', '60323'
          )
    ) then
        raise exception
            'У группы 49 обнаружены дополнительные счета';
    end if;

    if (
        select count(*)
        from ourpension.spr_accounts_indicator_bu
        where spr_indicator_bu_id = 50
    ) <> 1 then
        raise exception
            'У показателя 50 должен быть один счет';
    end if;

    if exists (
        select 1
        from ourpension.spr_indicator_bu si
        join ourpension.spr_accounts_indicator_bu sa
            on sa.spr_indicator_bu_id = si.id
        where sa.account = '60323'
          and si.id not in (49, 50)
          and (
              'NPO' = any(si.service_type)
              or 'PDS' = any(si.service_type)
          )
    ) then
        raise exception
            'Счет 60323 уже связан с другим показателем НПО/ПДС';
    end if;

    update ourpension.spr_indicator_bu
    set name = 'Дебиторская задолженность, в т.ч.'
    where id = 49;

    update ourpension.spr_accounts_indicator_bu
    set account = '60302'
    where spr_indicator_bu_id = 49
      and account = '48301';

    insert into ourpension.spr_accounts_indicator_bu (
        spr_indicator_bu_id,
        account
    )
    select 49, d.account
    from (
        values ('60302'), ('60323')
    ) as d(account)
    where not exists (
        select 1
        from ourpension.spr_accounts_indicator_bu sa
        where sa.spr_indicator_bu_id = 49
          and sa.account = d.account
    );

    update ourpension.spr_indicator_bu
    set
        name = 'Расчеты по исполнительным листам',
        is_aggregate = 49
    where id = 50;

    update ourpension.spr_accounts_indicator_bu
    set account = '60323'
    where spr_indicator_bu_id = 50
      and account = '48301.3';

    update ourpension.spr_indicator_bu
    set
        name = 'Расчеты по НДФЛ',
        is_aggregate = 49
    where id = 54;

    raise notice
        'Дебиторская группа 49 исправлена; детали 50–54 связаны с ней';
end;
$fix_debit_group$;