rewrite for greenplum 9.4, do not skip any rows, include objects creation scripts separately ```CREATE OR REPLACE EDITIONABLE PROCEDURE "PROT"."INP_CALC_FIN_RATES" (p_fid in number, p_orgid in number, p_repdateid in number, p_formid in number, p_rate in varchar2)
is
v_sqlerrm varchar2(255);
v_rate varchar2(100);
v_tmp1 number;
v_tmp2 number;
v_tmp3 number;
v_rate_id number;
v_prevdateid number;
v_dateid number;
v_mn number;
v_tmpdate date;
v_period varchar2(5);
v_rep_date date;
v_value number;
v_roa_koef number; 
v_cnt number;
v_metod number;

TYPE TUk IS TABLE OF number INDEX BY pls_integer;
v_uk_arr TUk;

function get_frate(v_orgid number, v_dtid number, v_code varchar2) return number
is
v_rid number;
v_val number := null;
begin
    select id into v_rid from ref_ko_fin_rates where code = v_code;
    
    select value into v_val from data_fin_rates where orgid = v_orgid and repdateid = v_dtid and rate_id = v_rid;
    
    return v_val;
end;
procedure set_frate(v_orgid number, v_dtid number, v_rateid number, v_val number)
is
v_cn number;
begin
    select count(*) into v_cn from data_fin_rates where orgid = v_orgid and repdateid = v_dtid and rate_id = v_rateid;
    if(v_cn =0)
    then
        insert into data_fin_rates(id, orgid, repdateid, rate_id, value, updatedt) values (data_fin_rates_seq.nextval, v_orgid, v_dtid, v_rateid, v_val, sysdate);
    else
        update data_fin_rates set value = v_val, updatedt = sysdate where orgid = v_orgid and repdateid = v_dtid and rate_id = v_rateid;
    end if;
end;

procedure set_tmp_rate(v_orgid number, v_dtid number, v_rateid number, v_val number)
is
v_cn number;
begin
    select count(*) into v_cn from tmp_fin_rates where orgid = v_orgid and repdateid = v_dtid and rate_id = v_rateid;
    if(v_cn =0)
    then
        insert into tmp_fin_rates(id, orgid, repdateid, rate_id, value, updatedt) values (tmp_fin_rates_seq.nextval, v_orgid, v_dtid, v_rateid, v_val, sysdate);
    else
        update tmp_fin_rates set value = v_val, updatedt = sysdate where orgid = v_orgid and repdateid = v_dtid and rate_id = v_rateid;
    end if;
end;

procedure copy_qrate(p_rate varchar2, v_orgid number, v_dtid number, v_fid number)
is
v_v1 number;
v_rt_id number;
v_err varchar2(255);
v_qdtid number;
begin
    v_v1 := 0;
    --inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || p_rate, v_fid);
    begin
        select max(id) into v_qdtid from uds.report_calendar where rep_date < (select rep_date from uds.report_calendar where id = v_dtid) and period = 'q';
        select id into v_rt_id from ref_ko_fin_rates where code = p_rate;
                
        v_v1 := get_frate(v_orgid, v_qdtid, p_rate);

        set_frate(v_orgid, v_dtid, v_rt_id, v_v1);
        --inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'new dtid: ' || v_dtid || ', qdtid: ' || v_qdtid || ', rt_id: ' || v_rt_id, v_fid);
                
        exception when others then 
            v_err := substr(sqlerrm, 1, 200);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, p_rate || ' ' || v_err, v_fid);
    end;
end;

procedure calc_frate(p_rate varchar2, v_orgid number, v_dtid number, v_prevdtid number, v_rdate date, v_fid number)
is
v_v1 number := 0;
v_v2 number := 0;
v_v3 number := 0;
v_rid number;
v_rvalue number;
v_koef number := 0;
v_tdate date;
v_tdtid number;
v_mon number := 0;
v_yearid number := 0;
v_is_year number := 0;
begin
    --inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || p_rate, v_fid);
    begin
        select id into v_rid from ref_ko_fin_rates where code = p_rate;
        
        select case when to_number(to_char(rep_date, 'MM')) = 1 then 12 else to_number(to_char(rep_date, 'MM')) -1 end,
        decode(to_char(rep_date, 'MM'), '01', 1, 0)  into v_mon, v_is_year  
        from uds.report_calendar where id = v_dtid;

        select id into v_yearid from uds.report_calendar where rep_date = trunc(v_rdate, 'YYYY');
        
        if (p_rate = 'KAP_CURR')--1
        then
            select val into v_rvalue from data_f123 where code = '000' and orgid = v_orgid and repdateid = v_dtid;
        elsif (p_rate = 'KAP_PREV')--2
        then
            select val into v_rvalue from data_f123 where code = '000' and orgid = v_orgid and repdateid = v_prevdtid;
        elsif (p_rate = 'KAP_MAX')--3
        then
            select max(val) into v_rvalue from data_f123 where code = '000' and orgid = v_orgid and repdateid >= v_prevdtid and repdateid <= v_dtid;
        elsif (p_rate = 'KAP_DYN')--4
        then 
            v_v1 := get_frate(v_orgid, v_dtid, 'KAP_CURR');
            v_v2 := get_frate(v_orgid, v_dtid, 'KAP_PREV');
            v_rvalue := ((v_v1 - v_v2)/abs(v_v2))*100;
        elsif (p_rate = 'KAPMAX_DYN')--5
        then    
            v_v1 := get_frate(v_orgid, v_dtid, 'KAP_CURR');
            v_v2 := get_frate(v_orgid, v_dtid, 'KAP_MAX');
            v_rvalue := ((v_v1 - v_v2)/abs(v_v2))*100;
        elsif (p_rate = 'UK_KAP')--6
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'UK');
            v_v2 := get_frate(v_orgid, v_dtid, 'KAP_CURR');
            v_rvalue := (v_v1/v_v2)*100;
        elsif (p_rate = 'FRES_KAP')--7
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'FRES_CURR');
            v_v2 := get_frate(v_orgid, v_dtid, 'KAP_CURR');
            v_rvalue := (v_v1/v_v2)*100;
        elsif (p_rate = 'MAIN_KAP')--8
        then
            select val into v_rvalue from data_f123 where code = '106' and orgid = v_orgid and repdateid = v_dtid;
        elsif (p_rate = 'ROE')--9
        then
            v_koef := 0;   

            v_v1 := get_frate(v_orgid, v_dtid, 'FRES_CURR')*(12/v_mon);
            for i in 0..3
            loop
                select add_months(rep_date, -3*i) into v_tdate from uds.report_calendar where id = v_dtid;
                select id into v_tdtid from uds.report_calendar where rep_date = v_tdate;
                begin
                    v_v2 := get_frate(v_orgid, v_tdtid, 'KAP_CURR');
                exception when no_data_found
                    then v_v2 := null;
                end;
                
                if (v_v2 is not null) 
                then
                    v_v3 := v_v3 + v_v2;
                    v_koef := v_koef + 1;
                end if;
            end loop;
            v_rvalue := (v_v1/(v_v3/v_koef))*100;
        elsif (p_rate = 'ACT_RSV_MAIN_KAP_IV_V')--10
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'ACT_IV_V');
            v_v2 := get_frate(v_orgid, v_dtid, 'RSV_ACT_IV_V');
            v_v3 := get_frate(v_orgid, v_dtid, 'MAIN_KAP');
            v_rvalue := ((v_v1 - v_v2)/v_v3)*100;
        elsif (p_rate = 'ACT_BRUTTO')--11
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto 
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type1 = 'Активы';
        elsif (p_rate = 'RESERVES')--12
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type1 = 'Резервы';
        elsif (p_rate = 'ACT_CURR')--13
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'ACT_BRUTTO');
            v_v2 := get_frate(v_orgid, v_dtid, 'RESERVES');
            v_rvalue := v_v1 - v_v2;
        elsif (p_rate = 'ACT_PREV')--14
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_v1
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= add_months(v_rdate,-12) and add_months(v_rdate,-12) <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_prevdtid and t2.type1 = 'Активы';

            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_v2
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= add_months(v_rdate,-12) and add_months(v_rdate,-12) <= t2.dto
            where t1.orgid = p_orgid and t1.repdateid = v_prevdtid and t2.type1 = 'Резервы';
            
            v_rvalue := v_v1 - v_v2;
        elsif (p_rate = 'ACT_DYN')--15
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'ACT_CURR');
            v_v2 := get_frate(v_orgid, v_dtid, 'ACT_PREV');
            v_rvalue := ((v_v1 - v_v2)/abs(v_v2))*100;
        elsif (p_rate = 'UK')--16
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type2 = 'Уставный капитал';
        elsif (p_rate = 'FRES_CURR')--17
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t1.acc_type = t2.acctype_eng and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type2 = 'Финансовый результат текущего периода';
        elsif (p_rate = 'FRES_PREV')--18
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= add_months(v_rdate,-12) and add_months(v_rdate,-12) <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_prevdtid and t2.type2 = 'Финансовый результат текущего периода';
        elsif (p_rate = 'FRES_DYN')--19
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'FRES_CURR');
            v_v2 := get_frate(v_orgid, v_dtid, 'FRES_PREV');
            v_rvalue := ((v_v1 - v_v2)/abs(v_v2))*100;
        elsif (p_rate = 'ROA')--20
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'FRES_CURR')*(12/v_mon);
            for i in 0..3
            loop
                select add_months(rep_date, -3*i) into v_tdate from uds.report_calendar where id = v_dtid;
                select id into v_tdtid from uds.report_calendar where rep_date = v_tdate;
                begin
                    v_v2 := get_frate(v_orgid, v_tdtid, 'ACT_CURR');
                exception when no_data_found then v_v2 := null;
                end;
                    
                if (v_v2 is not null) 
                then
                    v_v3 := v_v3 + v_v2;
                    v_koef := v_koef + 1;
                end if;
            end loop;
            
            v_rvalue := (v_v1/(v_v3/v_koef))*100;
        elsif (p_rate = 'MBK_IN')--21
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type2 = 'МБК привлеченные';
        elsif (p_rate = 'MBK_OUT')--22
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type2 = 'МБК размещенные';
        elsif (p_rate = 'ASSETS_IN')--23
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'ACT_CURR');
                
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_v2
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type1 = 'Капитал';

            v_rvalue := v_v1 - v_v2;
        elsif (p_rate = 'MBK')--24
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'MBK_IN');
            v_v2 := get_frate(v_orgid, v_dtid, 'MBK_OUT');
            v_v3 := get_frate(v_orgid, v_dtid, 'ASSETS_IN');
            v_rvalue := ((v_v1 - v_v2)/v_v3)*100;
        elsif (p_rate = 'PRTF')--25
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type2 = 'Кредитный портфель';
        elsif (p_rate = 'OVERDUE')--26
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type1 = 'Активы' and t2.type2 = 'Кредитный портфель' and t2.type3 = 'Просроченная задолженность';
        elsif (p_rate = 'OVERDUE_PRTF')--27
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'OVERDUE');
            v_v2 := get_frate(v_orgid, v_dtid, 'PRTF');
            if (v_v1 = 0 or v_v2 = 0)
            then
                v_rvalue := 0;
            else 
                v_rvalue := (v_v1/v_v2)*100;
            end if;
        elsif (p_rate = 'CARD')--28
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type3 = 'Картотека';
        elsif (p_rate = 'PROFIT_LOSS')--29
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) = to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type2 = 'Нераспределенная прибыль (непокрытый убыток)';
        elsif (p_rate = 'WRK_ACT_BRUTTO')--30
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
            from data_f101 t1
            join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rdate and v_rdate <= t2.dto 
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.type1 = 'Активы' and t2.TYPE2 in ('Ценные бумаги','МБК размещенные','Кредитный портфель');
        elsif (p_rate = 'NIM')--31
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'PRC_INCOME');
            v_v2 := get_frate(v_orgid, v_dtid, 'PRC_COST');
            v_v3 := get_frate(v_orgid, v_dtid, 'WRK_ACT_BRUTTO');

            v_rvalue := (((v_v1+v_v2)*(12/v_mon))/(v_v3))*100;
        elsif (p_rate = 'OP_OUT')--32
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f102 t1
            join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.symb_type = 'Операционные расходы';
        elsif (p_rate = 'OP_IN')--33
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f102 t1
            join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.symb_type = 'Операционные доходы';
        elsif (p_rate = 'CIR')--34
        then
            v_v1 := abs(get_frate(v_orgid, v_dtid, 'OP_OUT'));
            v_v2 := get_frate(v_orgid, v_dtid, 'OP_IN');

            v_rvalue := (v_v1/v_v2)*100;
        elsif (p_rate = 'PRC_INCOME')--35
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f102 t1
            join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.SYMB_TYPE2 = 'Процентные доходы';
        elsif (p_rate = 'PRC_COST')--36
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f102 t1
            join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.symb_type2 = 'Процентные расходы';
        elsif (p_rate = 'RSV_COST')--37
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f102 t1
            join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.symb_type2 = 'Формирование резервов';
        elsif (p_rate = 'RSV_INCOME')--38
        then
            select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_rvalue
            from data_f102 t1
            join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rdate and v_rdate <= t2.dto
            where t1.orgid = v_orgid and t1.repdateid = v_dtid and t2.symb_type2 = 'Восстановление резервов';
        elsif (p_rate = 'COR')--39
        then
            v_v1 := abs(get_frate(v_orgid, v_dtid, 'RSV_COST'));
            v_v2 := get_frate(v_orgid, v_dtid, 'ACT_RSV');

            v_rvalue := ((v_v1*12/v_mon)/(v_v2))*100;
        elsif (p_rate = 'NPL90')--40
        then
            select nvl(pz91_180,0) + nvl(pz181,0) into v_v1 from PROT.DATA_F115_1 t1
            join ref_ko_115 t2 on t1.LINE_ID = t2.id and t2.section = '1' and t2.line = '4' 
            where orgid = v_orgid and repdateid = v_dtid;

            select nvl(sum(summ),0) into v_v2 from data_f115_2 t1
            join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '2' and t2.STR_TYPE = 'NPL 90+'
            where orgid = v_orgid and repdateid = v_dtid;
                
            select nvl(sum(summ),0) into v_v3 from data_f115_3 t1
            join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '3' and t2.STR_TYPE = 'NPL 90+'
            where orgid = v_orgid and repdateid = v_dtid;

            v_rvalue := v_v1 + v_v2 + v_v3;
        elsif (p_rate = 'ACT_RSV')--41
        then
            select nvl(summ,0) into v_v1 from PROT.DATA_F115_1 t1
            join ref_ko_115 t2 on t1.LINE_ID = t2.id and t2.section = '1' and t2.line = '4' 
            where orgid = v_orgid and repdateid = v_dtid;

            select nvl(sum(summ),0) into v_v2 from data_f115_2 t1
            join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '2' and t2.STR_TYPE = 'Всего'
            where orgid = v_orgid and repdateid = v_dtid;
                
            select nvl(sum(summ),0) into v_v3 from data_f115_3 t1
            join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '3' and t2.STR_TYPE = 'Всего'
            where orgid = v_orgid and repdateid = v_dtid;
            
            v_rvalue := v_v1 + v_v2 + v_v3;
        elsif (p_rate = 'NPL90_PART') --42
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'NPL90');
            v_v2 := get_frate(v_orgid, v_dtid, 'ACT_RSV');

            v_rvalue := (v_v1/v_v2)*100;
        elsif (p_rate = 'ACT_IV_V')--43
        then    
            select nvl(kk4,0) + nvl(kk5,0) into v_v1 from PROT.DATA_F115_1 t1
            join ref_ko_115 t2 on t1.LINE_ID = t2.id and t2.section = '1' and t2.line = '4' 
            where orgid = v_orgid and repdateid = v_dtid;

            select nvl(sum(summ),0) into v_v2 from data_f115_2 t1
            join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '2' and t2.STR_TYPE = 'IV-V'
            where orgid = v_orgid and repdateid = v_dtid;
                
            select nvl(sum(summ),0) into v_v3 from data_f115_3 t1
            join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '3' and t2.STR_TYPE = 'IV-V'
            where orgid = v_orgid and repdateid = v_dtid;

            v_rvalue := v_v1 + v_v2 + v_v3;
        elsif (p_rate = 'RSV_ACT_IV_V')--44
        then
            select nvl(rvp_kk4,0) + nvl(rvp_kk5,0) into v_v1 from PROT.DATA_F115_1 t1
            join ref_ko_115 t2 on t1.LINE_ID = t2.id and t2.section = '1' and t2.line = '4' 
            where orgid = v_orgid and repdateid = v_dtid;

            select nvl(sum(rvp_fact),0) into v_v2 from data_f115_2 t1
            join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '2' and t2.STR_TYPE = 'IV-V'
            where orgid = v_orgid and repdateid = v_dtid;
                
            select nvl(sum(rvp_fact),0) into v_v3 from data_f115_3 t1
            join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '3' and t2.STR_TYPE = 'IV-V'
            where orgid = v_orgid and repdateid = v_dtid;

            v_rvalue := v_v1 + v_v2 + v_v3;
        elsif (p_rate = 'ACT_RSV_IV_V')--45
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'ACT_IV_V');
            v_v2 := get_frate(v_orgid, v_dtid, 'RSV_ACT_IV_V');

            v_rvalue := (v_v2/v_v1)*100;
        elsif (p_rate = 'DELTA_RVP_SS') --67
        then
            select nvl(rvp_bez_obesp - rvp_itogo,0) into v_v1 from DATA_F115_1 t1
            join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = 1 and t2.line = '4'
            where orgid = v_orgid and repdateid = v_dtid;
            
            v_v2 := get_frate(v_orgid, v_dtid, 'KAP_CURR');
            
            v_rvalue := (v_v1/v_v2)*100;
            
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);
        elsif (p_rate = 'FRES_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'FRES_CURR');
            if (v_is_year = 0)
            then
                v_v2 := get_frate(v_orgid, v_prevdtid, 'FRES_CURR');
                v_v3 := get_frate(v_orgid, v_yearid, 'FRES_CURR');
            end if;
            v_rvalue := v_v1 - v_v2 + v_v3;
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);
        elsif (p_rate = 'ROA_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'FRES_LTM');
            v_v2 := get_frate(v_orgid, v_dtid, 'ACT_CURR');
            v_rvalue := (v_v1/v_v2)*100;
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);
        elsif (p_rate = 'ROE_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'FRES_LTM');
            v_v2 := get_frate(v_orgid, v_dtid, 'KAP_CURR');
            v_rvalue := (v_v1/v_v2)*100;
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);
        elsif (p_rate = 'OP_IN_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'OP_IN');
            if (v_is_year = 0)
            then
                v_v2 := get_frate(v_orgid, v_prevdtid, 'OP_IN');
                v_v3 := get_frate(v_orgid, v_yearid, 'OP_IN');
            end if;
            v_rvalue := v_v1 - v_v2 + v_v3;
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);
        elsif (p_rate = 'OP_OUT_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'OP_OUT');
            if (v_is_year = 0)
            then
                v_v2 := get_frate(v_orgid, v_prevdtid, 'OP_OUT');
                v_v3 := get_frate(v_orgid, v_yearid, 'OP_OUT');
            end if;
            v_rvalue := v_v1 - v_v2 + v_v3;  
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);
        elsif (p_rate = 'CIR_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'OP_OUT_LTM');
            v_v2 := get_frate(v_orgid, v_dtid, 'OP_IN_LTM');
            v_rvalue := (abs(v_v1)/v_v2)*100;
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);
        elsif (p_rate = 'PRC_INCOME_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'PRC_INCOME');
            if (v_is_year = 0)
            then
                v_v2 := get_frate(v_orgid, v_prevdtid, 'PRC_INCOME');
                v_v3 := get_frate(v_orgid, v_yearid, 'PRC_INCOME');
            end if;
            v_rvalue := v_v1 - v_v2 + v_v3;   
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);         
        elsif (p_rate = 'PRC_COST_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'PRC_COST');
            if (v_is_year = 0)
            then
                v_v2 := get_frate(v_orgid, v_prevdtid, 'PRC_COST');
                v_v3 := get_frate(v_orgid, v_yearid, 'PRC_COST');
            end if;
            v_rvalue := v_v1 - v_v2 + v_v3;    
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);
        elsif (p_rate = 'NIM_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'PRC_INCOME_LTM');  
            v_v2 := get_frate(v_orgid, v_prevdtid, 'PRC_COST_LTM');
            v_v3 := get_frate(v_orgid, v_prevdtid, 'WRK_ACT_BRUTTO'); 
            v_rvalue := ((v_v1 + v_v2)/v_v3)*100;
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);
        elsif (p_rate = 'RSV_COST_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'RSV_COST');
            if (v_is_year = 0)
            then
                v_v2 := get_frate(v_orgid, v_prevdtid, 'RSV_COST');
                v_v3 := get_frate(v_orgid, v_yearid, 'RSV_COST');
            end if;
            v_rvalue := v_v1 - v_v2 + v_v3;           
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);             
        elsif (p_rate = 'RSV_INCOME_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'RSV_INCOME');
            if (v_is_year = 0)
            then
                v_v2 := get_frate(v_orgid, v_prevdtid, 'RSV_INCOME');
                v_v3 := get_frate(v_orgid, v_yearid, 'RSV_INCOME');
            end if;
            v_rvalue := v_v1 - v_v2 + v_v3; 
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);         
        elsif (p_rate = 'COR_LTM')
        then
            v_v1 := get_frate(v_orgid, v_dtid, 'RSV_COST_LTM');
            v_v2 := get_frate(v_orgid, v_dtid, 'ACT_RSV');
            v_rvalue := (abs(v_v1)/v_v2)*100;  
            set_frate(v_orgid, v_dtid, v_rid, v_rvalue);        
        end if;
        
        set_tmp_rate(p_orgid, p_repdateid, v_rid, v_rvalue);
                                
        exception when others then 
            v_sqlerrm := substr(sqlerrm, 1, 200);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
    end;
end;


begin
    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, '---begin CALC_RATES---: orgid = ' || p_orgid || ', repdateid = ' || p_repdateid || ', form_id = ' || 
    p_formid || ', rate = ' || p_rate, p_fid);
    
    select period, rep_date into v_period, v_rep_date from uds.report_calendar where id = p_repdateid;
    
    select id into v_prevdateid from uds.report_calendar where rep_date = (select add_months(rep_date, -12) from uds.report_calendar where id = p_repdateid);
    
    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'prevrepdateid = ' || v_prevdateid, p_fid);
    
    if (p_formid is not null and lower(p_formid) != 'null' and (p_rate is null or lower(p_rate) = 'null'))
    then
        delete from data_fin_rates where orgid = p_orgid and repdateid = p_repdateid and rate_id in 
        (select t6.id rate_id from inp_files t1
         join ref_file_type t2 on t1.file_typeid = t2.id
         join ref_form_file t3 on t3.file_id = t2.id
         join ref_form_type t4 on t4.id = t3.form_id
         join ref_ko_rates_forms t5 on t4.id = t5.form_id
         join ref_ko_fin_rates t6 on t6.id = t5.rate_id
         where t1.id = p_fid and t4.id = p_formid);
        
        inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'formid = ' || p_formid || ', ' || sql%rowcount || ' rows deleted from data_fin_rates', p_fid);
    end if;
    
    if (p_rate is not null and lower(p_rate) != 'null')
    then
        select id into v_rate_id from ref_ko_fin_rates where code = p_rate;
        
        delete from data_fin_rates where orgid = p_orgid and repdateid = p_repdateid and rate_id = v_rate_id;
        inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'rate = ' || p_rate || ', ' || sql%rowcount || ' rows deleted from data_fin_rates', p_fid);
        v_rate_id := 0;
    end if;
    
    if ((p_formid is null or lower(p_formid) = 'null') and (p_rate is null or lower(p_rate) = 'null'))
    then
        delete from data_fin_rates where orgid = p_orgid and repdateid = p_repdateid;
        inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'formid is null and rate is null: ' || sql%rowcount || ' rows deleted from data_fin_rates', p_fid);
    end if;
    
    v_rate := 'KO_UK_LOWERING';
    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
    
    begin
    for x1 in 
    (select case when b_date = v_rep_date then 1 else 0 end date_num, greatest(n_cu, n_se, n_de, n_rts,n_sd, n_cr) as max_cat
    from ( 
    select b_date, nvl(t_cu.ord,0) n_cu, nvl(t_se.ord,0) n_se, nvl(t_de.ord,0) n_de, nvl(t_rts.ord,0) n_rts, nvl(t_sd.ord,0) n_sd, nvl(t_cr.ord,0) n_cr
    from PROT.V_MDM_UK_CAT t1
    join v_mdm_org t2 on t1.gold_id = t2.id
    left join ref_uk_order t_cu on t1.cu_clear_category = t_cu.code
    left join ref_uk_order t_se on t1.se_clear_category = t_se.code 
    left join ref_uk_order t_de on t1.de_clear_category = t_de.code 
    left join ref_uk_order t_rts on t1.rts_clear_category = t_rts.code 
    left join ref_uk_order t_sd on t1.sd_clear_category = t_sd.code 
    left join ref_uk_order t_cr on t1.cr_clear_category = t_cr.code  
    where b_date in ( v_rep_date, add_months(v_rep_date, -1)) and t2.prot_orgid = p_orgid ))
    loop
        v_uk_arr(x1.date_num) := x1.max_cat;
    end loop;
    
    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'current uk cat = ' || v_uk_arr(1) || ', previous uk cat = ' || v_uk_arr(0), p_fid);
    
    if (v_uk_arr(1) < v_uk_arr(0))
    then
        select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
        select count(*) into v_cnt from data_fin_rates where orgid = p_orgid and repdateid = p_repdateid and rate_id = v_rate_id;
        if (v_cnt = 0)
        then
            insert into data_fin_rates (id, orgid, repdateid, rate_id, value, updatedt) values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_uk_arr(1) - v_uk_arr(0), sysdate);
        end if;
    end if;
    exception when others then
        v_sqlerrm := substr(sqlerrm, 1, 200);
        inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
    end;
    
    ---------------------123---------------------
    if (p_formid is null or p_formid = 12)
    then
        if (p_rate is null or p_rate = 'KAP_CURR')
        then
            calc_frate('KAP_CURR', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            v_rate := 'KAP_CURR';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select val into v_tmp1 from data_f123 where code = '000' and orgid = p_orgid and repdateid = p_repdateid;
                
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;
        
        if (p_rate is null or p_rate = 'KAP_PREV')
        then
            calc_frate('KAP_PREV', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            v_rate := 'KAP_PREV';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select val into v_tmp1 from data_f123 where code = '000' and orgid = p_orgid and repdateid = v_prevdateid;
                
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate is null or p_rate = 'KAP_MAX')
        then
            v_rate := 'KAP_MAX';
            calc_frate('KAP_MAX', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select max(val) into v_tmp1 from data_f123 where code = '000' and orgid = p_orgid and repdateid >= v_prevdateid and repdateid <= p_repdateid;
                
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;    
        
        if (p_rate is null or p_rate = 'KAP_DYN')
        then
            v_rate := 'KAP_DYN';
            calc_frate('KAP_DYN', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'KAP_CURR');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'KAP_PREV');

                set_frate(p_orgid, p_repdateid, v_rate_id, ((v_tmp1 - v_tmp2)/abs(v_tmp2))*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;
        
        if (p_rate is null or p_rate = 'KAPMAX_DYN')
        then
            v_rate := 'KAPMAX_DYN';
            calc_frate('KAPMAX_DYN', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'KAP_CURR');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'KAP_MAX');

                set_frate(p_orgid, p_repdateid, v_rate_id, ((v_tmp1 - v_tmp2)/abs(v_tmp2))*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;                 
        
        if (p_rate is null or p_rate = 'UK_KAP')
        then
            v_rate := 'UK_KAP';
            calc_frate('UK_KAP', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'UK');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'KAP_CURR');

                set_frate(p_orgid, p_repdateid, v_rate_id, (v_tmp1/v_tmp2)*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        

        if (p_rate is null or p_rate = 'FRES_KAP')
        then
            v_rate := 'FRES_KAP';
            calc_frate('FRES_KAP', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'FRES_CURR');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'KAP_CURR');

                set_frate(p_orgid, p_repdateid, v_rate_id, (v_tmp1/v_tmp2)*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;
        
        if (p_rate is null or p_rate = 'MAIN_KAP')
        then
            v_rate := 'MAIN_KAP';
            calc_frate('MAIN_KAP', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            v_tmp1 := 0;
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select val into v_tmp1 from data_f123 where code = '106' and orgid = p_orgid and repdateid = p_repdateid;

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;
        
        if (p_rate is null or p_rate = 'ROE')
        then
            v_rate := 'ROE';
            calc_frate('ROE', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            v_roa_koef := 0;
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select case when to_number(to_char(rep_date, 'MM')) = 1 then 12 else to_number(to_char(rep_date, 'MM')) -1 end into v_mn from uds.report_calendar where id = p_repdateid;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'FRES_CURR')*(12/v_mn);
                
                for i in 0..3
                loop
                    select add_months(rep_date, -3*i) into v_tmpdate from uds.report_calendar where id = p_repdateid;
                    select id into v_dateid from uds.report_calendar where rep_date = v_tmpdate;
                    begin
                        v_tmp2 := get_frate(p_orgid, v_dateid, 'KAP_CURR');
                    exception when no_data_found
                    then v_tmp2 := null;
                    end;
                    
                    if (v_tmp2 is not null) 
                    then
                        v_tmp3 := v_tmp3 + v_tmp2;
                        v_roa_koef := v_roa_koef + 1;
                    end if;
                end loop;
                
                set_frate(p_orgid, p_repdateid, v_rate_id, (v_tmp1/(v_tmp3/v_roa_koef))*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;    
        
        if (p_rate = 'ACT_RSV_MAIN_KAP_IV_V' or p_rate is null)
        then
            v_rate := 'ACT_RSV_MAIN_KAP_IV_V';
            calc_frate('ACT_RSV_MAIN_KAP_IV_V', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'ACT_IV_V');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'RSV_ACT_IV_V');
                v_tmp3 := get_frate(p_orgid, p_repdateid, 'MAIN_KAP');

                set_frate(p_orgid, p_repdateid, v_rate_id, ((v_tmp1 - v_tmp2)/v_tmp3)*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;  
        
        if (p_rate is null or p_rate = 'DELTA_RVP_SS')
        then
            calc_frate('DELTA_RVP_SS', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if; 
        
        if (p_rate is null or p_rate = 'ROE_LTM')
        then
            calc_frate('ROE_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;         
    end if;
    
    ---------------------101---------------------
    if (p_formid is null or p_formid = 1)
    then
        v_tmp1 := 0;
        v_tmp2 := 0;
        v_tmp3 := 0;
        if (p_rate is null or p_rate = 'ACT_BRUTTO')
        then
            v_rate := 'ACT_BRUTTO';
            calc_frate('ACT_BRUTTO', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto 
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type1 = 'Активы';

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;            
        end if;
        
        if (p_rate is null or p_rate = 'RESERVES')
        then
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            v_rate := 'RESERVES';
            calc_frate('RESERVES', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type1 = 'Резервы';

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;            
        end if;        

        if (p_rate is null or p_rate = 'ACT_CURR')
        then
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            v_rate := 'ACT_CURR';
            calc_frate('ACT_CURR', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'ACT_BRUTTO');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'RESERVES');
                
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1 - v_tmp2);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;
        
        if (p_rate is null or p_rate = 'ACT_PREV')
        then
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            v_rate := 'ACT_PREV';
            calc_frate('ACT_PREV', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= add_months(v_rep_date,-12) and add_months(v_rep_date,-12) <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = v_prevdateid and t2.type1 = 'Активы';

                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp2
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= add_months(v_rep_date,-12) and add_months(v_rep_date,-12) <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = v_prevdateid and t2.type1 = 'Резервы';
                
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1 - v_tmp2);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;  
        
        if (p_rate is null or p_rate = 'ACT_DYN')
        then
            v_rate := 'ACT_DYN';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('ACT_DYN', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'ACT_CURR');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'ACT_PREV');
                
                set_frate(p_orgid, p_repdateid, v_rate_id, ((v_tmp1 - v_tmp2)/abs(v_tmp2))*100);
                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;       
        
        if (p_rate is null or p_rate = 'UK')
        then
            v_rate := 'UK';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('UK', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type2 = 'Уставный капитал';

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate is null or p_rate = 'UK_KAP')
        then
            v_rate := 'UK_KAP';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('UK_KAP', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'UK');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'KAP_CURR');

                set_frate(p_orgid, p_repdateid, v_rate_id, (v_tmp1/v_tmp2)*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate is null or p_rate = 'FRES_CURR')
        then
            v_rate := 'FRES_CURR';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('FRES_CURR', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t1.acc_type = t2.acctype_eng and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type2 = 'Финансовый результат текущего периода';

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate is null or p_rate = 'FRES_PREV')
        then
            v_rate := 'FRES_PREV';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('FRES_PREV', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= add_months(v_rep_date,-12) and add_months(v_rep_date,-12) <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = v_prevdateid and t2.type2 = 'Финансовый результат текущего периода';
                
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;    
        
        if (p_rate is null or p_rate = 'FRES_DYN')
        then
            v_rate := 'FRES_DYN';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('FRES_DYN', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'FRES_CURR');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'FRES_PREV');

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, ((v_tmp1 - v_tmp2)/abs(v_tmp2))*100, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, ((v_tmp1 - v_tmp2)/abs(v_tmp2))*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;    
        
        if (p_rate is null or p_rate = 'ROA')
        then
            v_rate := 'ROA';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            v_roa_koef := 0;
            calc_frate('ROA', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select case when to_number(to_char(rep_date, 'MM')) = 1 then 12 else to_number(to_char(rep_date, 'MM')) -1 end into v_mn from uds.report_calendar where id = p_repdateid;
                --inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'koef = ' || (12/v_mn) || ', v_mn = ' || v_mn, -1);
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'FRES_CURR')*(12/v_mn);
                
                for i in 0..3
                loop
                    select add_months(rep_date, -3*i) into v_tmpdate from uds.report_calendar where id = p_repdateid;
                    select id into v_dateid from uds.report_calendar where rep_date = v_tmpdate;
                    begin
                        v_tmp2 := get_frate(p_orgid, v_dateid, 'ACT_CURR');
                    exception when no_data_found
                    then v_tmp2 := null;
                    end;
                    
                    if (v_tmp2 is not null) 
                    then
                        v_tmp3 := v_tmp3 + v_tmp2;
                        v_roa_koef := v_roa_koef + 1;
                    end if;
                end loop;
                
                set_frate(p_orgid, p_repdateid, v_rate_id, (v_tmp1/(v_tmp3/v_roa_koef))*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;  
        
        if (p_rate is null or p_rate = 'ROE')
        then
            v_rate := 'ROE';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            v_roa_koef := 0;
            calc_frate('ROE', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select case when to_number(to_char(rep_date, 'MM')) = 1 then 12 else to_number(to_char(rep_date, 'MM')) -1 end into v_mn from uds.report_calendar where id = p_repdateid;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'FRES_CURR')*(12/v_mn);
                
                for i in 0..3
                loop
                    select add_months(rep_date, -3*i) into v_tmpdate from uds.report_calendar where id = p_repdateid;
                    select id into v_dateid from uds.report_calendar where rep_date = v_tmpdate;
                    begin
                        v_tmp2 := get_frate(p_orgid, v_dateid, 'KAP_CURR');
                    exception when no_data_found
                    then v_tmp2 := null;
                    end;
                    
                    if (v_tmp2 is not null) 
                    then
                        v_tmp3 := v_tmp3 + v_tmp2;
                        v_roa_koef := v_roa_koef + 1;
                    end if;
                end loop;
                
                set_frate(p_orgid, p_repdateid, v_rate_id, (v_tmp1/(v_tmp3/v_roa_koef))*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate is null or p_rate = 'FRES_KAP')
        then
            v_rate := 'FRES_KAP';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('FRES_KAP', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'FRES_CURR');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'KAP_CURR');

                set_frate(p_orgid, p_repdateid, v_rate_id, (v_tmp1/v_tmp2)*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;      
        
        if (p_rate is null or p_rate = 'MBK_IN')
        then
            v_rate := 'MBK_IN';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('MBK_IN', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type2 = 'МБК привлеченные';

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate is null or p_rate = 'MBK_OUT')
        then
            v_rate := 'MBK_OUT';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('MBK_OUT', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type2 = 'МБК размещенные';

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_tmp1, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate is null or p_rate = 'ASSETS_IN')
        then
            v_rate := 'ASSETS_IN';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('ASSETS_IN', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'ACT_CURR');
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp2
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type1 = 'Капитал';

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_tmp1 - v_tmp2, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1 - v_tmp2);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;   
        
        if (p_rate is null or p_rate = 'MBK')
        then
            v_rate := 'MBK';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            v_value := 0;
            calc_frate('MBK', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'MBK_IN');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'MBK_OUT');
                v_tmp3 := get_frate(p_orgid, p_repdateid, 'ASSETS_IN');
                v_value := ((v_tmp1 - v_tmp2)/v_tmp3)*100;

                set_frate(p_orgid, p_repdateid, v_rate_id, v_value);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if; 
        
        if (p_rate is null or p_rate = 'PRTF')
        then
            v_rate := 'PRTF';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('PRTF', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type2 = 'Кредитный портфель';

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_tmp1, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;            
        
        if (p_rate is null or p_rate = 'OVERDUE')
        then
            v_rate := 'OVERDUE';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('OVERDUE', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type1 = 'Активы' and t2.type2 = 'Кредитный портфель' and t2.type3 = 'Просроченная задолженность';

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;     
        
        if (p_rate is null or p_rate = 'OVERDUE_PRTF')
        then
            v_rate := 'OVERDUE_PRTF';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('OVERDUE_PRTF', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'OVERDUE');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'PRTF');
                if (v_tmp1 = 0 or v_tmp2 = 0)
                then
                    v_tmp3 := 0;
                else 
                    v_tmp3 := (v_tmp1/v_tmp2)*100;
                end if;

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp3);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;  
        
        if (p_rate is null or p_rate = 'CARD')
        then
            v_rate := 'CARD';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('CARD', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type3 = 'Картотека';

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_tmp1, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;   
        
        if (p_rate is null or p_rate = 'PROFIT_LOSS')
        then
            v_rate := 'PROFIT_LOSS';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('PROFIT_LOSS', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) = to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type2 = 'Нераспределенная прибыль (непокрытый убыток)';

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;   
        
        if (p_rate is null or p_rate = 'WRK_ACT_BRUTTO')
        then
            v_tmp1 := 0;
            v_rate := 'WRK_ACT_BRUTTO';
            calc_frate('WRK_ACT_BRUTTO', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f101 t1
                join ref_ko_acc_101 t2 on trim(t1.acc) =  to_char(t2.accnum2) and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto 
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.type1 = 'Активы' and t2.TYPE2 in ('Ценные бумаги','МБК размещенные','Кредитный портфель');

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;            
        end if; 
        
        if (p_rate is null or p_rate = 'FRES_LTM')
        then
            calc_frate('FRES_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;         
          
        if (p_rate is null or p_rate = 'ROA_LTM')
        then
            calc_frate('ROA_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;    
        
        if (p_rate is null or p_rate = 'ROE_LTM')
        then
            calc_frate('ROE_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;         
        
        if (v_period = 'q')
        then
            if (p_rate = 'NIM' or p_rate is null)
            then
                v_rate := 'NIM';
                v_tmp1 := 0;
                v_tmp2 := 0;
                v_tmp3 := 0;
                calc_frate('NIM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
                inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
                begin
                    select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                    select case when to_number(to_char(rep_date, 'MM')) = 1 then 12 else to_number(to_char(rep_date, 'MM')) -1 end into v_mn from uds.report_calendar where id = p_repdateid;
                
                    v_tmp1 := get_frate(p_orgid, p_repdateid, 'PRC_INCOME');
                    v_tmp2 := get_frate(p_orgid, p_repdateid, 'PRC_COST');
                    v_tmp3 := get_frate(p_orgid, p_repdateid, 'WRK_ACT_BRUTTO');

                    set_frate(p_orgid, p_repdateid, v_rate_id, (((v_tmp1+v_tmp2)*(12/v_mn))/(v_tmp3))*100);
                                
                    exception when others then 
                        v_sqlerrm := substr(sqlerrm, 1, 200);
                        inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
                end;
            end if;
            
            if (p_rate is null or p_rate = 'NIM_LTM')
            then
                calc_frate('NIM_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            end if;         

        end if; 
        
        if (v_period != 'q') 
        then
            copy_qrate('OP_OUT', p_orgid, p_repdateid, p_fid);
            copy_qrate('OP_IN', p_orgid, p_repdateid, p_fid);
            copy_qrate('CIR', p_orgid, p_repdateid, p_fid);
            copy_qrate('NIM', p_orgid, p_repdateid, p_fid);
            copy_qrate('PRC_INCOME', p_orgid, p_repdateid, p_fid);
            copy_qrate('PRC_COST', p_orgid, p_repdateid, p_fid);
            copy_qrate('RSV_COST', p_orgid, p_repdateid, p_fid);
            copy_qrate('RSV_INCOME', p_orgid, p_repdateid, p_fid);
            copy_qrate('NIM_LTM', p_orgid, p_repdateid, p_fid);
        end if;
    end if;
    
    ---------------------102---------------------
    if ((p_formid = 2 or p_formid is null) and v_period = 'q')
    then
        if (p_rate = 'OP_OUT' or p_rate is null)
        then
            v_rate := 'OP_OUT';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('OP_OUT', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f102 t1
                join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.symb_type = 'Операционные расходы';

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;    
        
        if (p_rate = 'OP_IN' or p_rate is null)
        then
            v_rate := 'OP_IN';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('OP_IN', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f102 t1
                join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.symb_type = 'Операционные доходы';

                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate = 'CIR' or p_rate is null)
        then
            v_rate := 'CIR';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('CIR', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := abs(get_frate(p_orgid, p_repdateid, 'OP_OUT'));
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'OP_IN');

                set_frate(p_orgid, p_repdateid, v_rate_id, (v_tmp1/v_tmp2)*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        

        if (p_rate = 'PRC_INCOME' or p_rate is null)
        then
            v_rate := 'PRC_INCOME';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('PRC_INCOME', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f102 t1
                join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.SYMB_TYPE2 = 'Процентные доходы';
                
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate = 'PRC_COST' or p_rate is null)
        then
            v_rate := 'PRC_COST';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('PRC_COST', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f102 t1
                join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.symb_type2 = 'Процентные расходы';
                
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if; 
        
        if (p_rate = 'RSV_COST' or p_rate is null)
        then
            v_rate := 'RSV_COST';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('RSV_COST', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f102 t1
                join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.symb_type2 = 'Формирование резервов';
                
                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_tmp1, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;               
        
        if (p_rate = 'RSV_INCOME' or p_rate is null)
        then
            v_rate := 'RSV_INCOME';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('RSV_INCOME', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(sum(t1.summ*nvl(t2.koef, 0)),0) into v_tmp1
                from data_f102 t1
                join ref_ko_symb_102 t2 on trim(t1.acc) =  t2.symb and t2.dfrom <= v_rep_date and v_rep_date <= t2.dto
                where t1.orgid = p_orgid and t1.repdateid = p_repdateid and t2.symb_type2 = 'Восстановление резервов';
                
                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_tmp1, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate = 'NIM' or p_rate is null)
        then
            v_rate := 'NIM';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('NIM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select case when to_number(to_char(rep_date, 'MM')) = 1 then 12 else to_number(to_char(rep_date, 'MM')) -1 end into v_mn from uds.report_calendar where id = p_repdateid;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'PRC_INCOME');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'PRC_COST');
                v_tmp3 := get_frate(p_orgid, p_repdateid, 'WRK_ACT_BRUTTO');

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, (((v_tmp1-v_tmp2)*(12/v_mn))/(v_tmp3))*100, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, (((v_tmp1+v_tmp2)*(12/v_mn))/(v_tmp3))*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;
                
        if (p_rate = 'COR' or p_rate is null)
        then
            v_rate := 'COR';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('COR', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select case when to_number(to_char(rep_date, 'MM')) = 1 then 12 else to_number(to_char(rep_date, 'MM')) -1 end into v_mn from uds.report_calendar where id = p_repdateid;
                
                v_tmp1 := abs(get_frate(p_orgid, p_repdateid, 'RSV_COST'));
                v_tmp3 := get_frate(p_orgid, p_repdateid, 'ACT_RSV');

                set_frate(p_orgid, p_repdateid, v_rate_id, ((v_tmp1*12/v_mn)/(v_tmp3))*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if; 
        
        if (p_rate is null or p_rate = 'COR_LTM')
        then
            calc_frate('COR_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;         
        
        if (p_rate is null or p_rate = 'OP_IN_LTM')
        then
            calc_frate('OP_IN_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;        
              
        if (p_rate is null or p_rate = 'OP_OUT_LTM')
        then
            calc_frate('OP_OUT_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;        
        
        if (p_rate is null or p_rate = 'CIR_LTM')
        then
            calc_frate('CIR_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if; 
        
        if (p_rate is null or p_rate = 'PRC_INCOME_LTM')
        then
            calc_frate('PRC_INCOME_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;        
               
        if (p_rate is null or p_rate = 'PRC_COST_LTM')
        then
            calc_frate('PRC_COST_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;        
        
        if (p_rate is null or p_rate = 'NIM_LTM')
        then
            calc_frate('NIM_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;     
        
        if (p_rate is null or p_rate = 'RSV_COST_LTM')
        then
            calc_frate('RSV_COST_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;        
           
        if (p_rate is null or p_rate = 'RSV_INCOME_LTM')
        then
            calc_frate('RSV_INCOME_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;   
        
        begin
        for cd in (select id, rep_date from uds.report_calendar where rep_date > v_rep_date and rep_date < add_months(v_rep_date, 3))
        loop
            copy_qrate('NIM', p_orgid, cd.id, p_fid);
            copy_qrate('COR', p_orgid, cd.id, p_fid);
            copy_qrate('NIM_LTM', p_orgid, cd.id, p_fid);
            copy_qrate('COR_LTM', p_orgid, cd.id, p_fid);
        end loop;
        exception when others then
            v_sqlerrm := substr(sqlerrm, 1, 200);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'error copying NIM and COR:' || v_sqlerrm, p_fid);
        end; 
    end if;
    
    ---------------------135---------------------
    if (p_formid = 4 or p_formid is null)
    then
		select nvl(max(norm_value),1) into v_metod from data_norms t1
		join ref_norms t2 on t1.norm_id = t2.id and t2.code = 'F_METOD'
		where orgid = p_orgid and repdateid = p_repdateid;
		
		if (v_metod = 2)
		then
			for c1 in (select t1.id f_id, t1.orgid, t1.repdateid, t1.norm_id f_norm_id, t1.norm_value f_value, t2.code f_code, t4.norm_id, t3.code, t4.id, t4.norm_value from data_norms t1 
						join ref_norms t2 on t1.norm_id = t2.id and t2.code like 'F_H1%' 
						join ref_norms t3 on t2.code = 'F_' || t3.code 
						join data_norms t4 on t1.orgid = t4.orgid and t1.repdateid = t4.repdateid and t3.id = t4.norm_id 
						where t1.orgid = p_orgid and t1.repdateid = p_repdateid)
			loop
				inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'orgid ' || p_orgid || ' repdateid ' || p_repdateid || ' updating ' || c1.code || ' ' || c1.norm_value || ' with value from ' || c1.f_code || ' ' || c1.f_value, p_fid);
				update data_norms set norm_value = c1.f_value*100, updatedt = sysdate where id = c1.id;
			end loop;
		end if;
	
        insert into data_fin_rates select data_fin_rates_seq.nextval, p_orgid, p_repdateid, t2.id, t4.NORM_VALUE, sysdate from ref_ko_rates_forms t1
        join ref_ko_fin_rates t2 on t1.rate_id = t2.id
        join ref_norms t3 on t2.code = t3.code and t2.CODE != 'H18'
        join data_norms t4 on t4.orgid = p_orgid and t4.repdateid = p_repdateid and t4.norm_id = t3.id
        where form_id = 4;
        
        inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'orgid ' || p_orgid || ' repdateid ' || p_repdateid || ' ' || sql%rowcount || ' rows inserted into data_fin_rates', p_fid);
        
        insert into data_fin_rates select data_fin_rates_seq.nextval, p_orgid, p_repdateid, t2.id, t4.norm_value, sysdate from ref_ko_fin_rates t2
        join ref_norms t3 on t2.code = t3.code and t3.id = 65
        join data_norms t4 on t4.orgid = p_orgid and t4.repdateid = p_repdateid and t4.norm_id = t3.id
        join data_f135 t5 on t4.orgid = t5.orgid and t4.repdateid = t5.repdateid and t5.parnum = '8951' and t5.PARVALUE != 0;
        
        inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'orgid ' || p_orgid || ' repdateid ' || p_repdateid || ' ' || sql%rowcount || ' rows inserted into data_fin_rates: ref_norms.id = 65', p_fid);
    end if; 
    
    ---------------------118---------------------
    if (p_formid = 6 or p_formid is null)
    then
        for ee in (select data_fin_rates_seq.nextval pid, p_orgid, p_repdateid, t2.id, t4.NORM_VALUE from ref_ko_rates_forms t1
        join ref_ko_fin_rates t2 on t1.rate_id = t2.id
        join ref_norms t3 on t2.code = t3.code 
        join data_norms t4 on t4.orgid = p_orgid and t4.repdateid = p_repdateid and t4.norm_id = t3.id
        where form_id = 6)
        loop
            insert into data_fin_rates(id, orgid, repdateid, rate_id, value, updatedt)
            values (ee.pid, ee.p_orgid, ee.p_repdateid, ee.id, ee.norm_value, sysdate); 
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 
            'f6: id = ' || ee.pid || ', orgid = ' || ee.p_orgid || ', repdateid = ' || ee.p_repdateid || ', rate_id = '|| ee.id || ', value = ' || ee.norm_value, p_fid);
        end loop;
        --inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, sql%rowcount || ' rows inserted into data_fin_rates for formid = 6', p_fid);  
    end if;
    
    ---------------------115---------------------
    if (p_formid = 32 or p_formid is null)
    then
        if (p_rate = 'NPL90' or p_rate is null)
        then
            v_rate := 'NPL90';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('NPL90', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(pz91_180,0) + nvl(pz181,0) into v_tmp1 from PROT.DATA_F115_1 t1
                join ref_ko_115 t2 on t1.LINE_ID = t2.id and t2.section = '1' and t2.line = '4' 
                where orgid = p_orgid and repdateid = p_repdateid;

                select nvl(sum(summ),0) into v_tmp2 from data_f115_2 t1
                join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '2' and t2.STR_TYPE = 'NPL 90+'
                where orgid = p_orgid and repdateid = p_repdateid;
                
                select nvl(sum(summ),0) into v_tmp3 from data_f115_3 t1
                join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '3' and t2.STR_TYPE = 'NPL 90+'
                where orgid = p_orgid and repdateid = p_repdateid;

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_tmp1 + v_tmp2 + v_tmp3, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1 + v_tmp2 + v_tmp3);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;
        
        if (p_rate = 'ACT_RSV' or p_rate is null)
        then
            v_rate := 'ACT_RSV';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('ACT_RSV', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(summ,0) into v_tmp1 from PROT.DATA_F115_1 t1
                join ref_ko_115 t2 on t1.LINE_ID = t2.id and t2.section = '1' and t2.line = '4' 
                where orgid = p_orgid and repdateid = p_repdateid;

                select nvl(sum(summ),0) into v_tmp2 from data_f115_2 t1
                join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '2' and t2.STR_TYPE = 'Всего'
                where orgid = p_orgid and repdateid = p_repdateid;
                
                select nvl(sum(summ),0) into v_tmp3 from data_f115_3 t1
                join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '3' and t2.STR_TYPE = 'Всего'
                where orgid = p_orgid and repdateid = p_repdateid;

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_tmp1 + v_tmp2 + v_tmp3, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1 + v_tmp2 + v_tmp3);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;    
        
        if (p_rate = 'NPL90_PART' or p_rate is null)
        then
            v_rate := 'NPL90_PART';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('NPL90_PART', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'NPL90');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'ACT_RSV');

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, (v_tmp1/v_tmp2)*100, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, (v_tmp1/v_tmp2)*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;       
        
        if (p_rate = 'ACT_IV_V' or p_rate is null)
        then
            v_rate := 'ACT_IV_V';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('ACT_IV_V', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(kk4,0) + nvl(kk5,0) into v_tmp1 from PROT.DATA_F115_1 t1
                join ref_ko_115 t2 on t1.LINE_ID = t2.id and t2.section = '1' and t2.line = '4' 
                where orgid = p_orgid and repdateid = p_repdateid;

                select nvl(sum(summ),0) into v_tmp2 from data_f115_2 t1
                join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '2' and t2.STR_TYPE = 'IV-V'
                where orgid = p_orgid and repdateid = p_repdateid;
                
                select nvl(sum(summ),0) into v_tmp3 from data_f115_3 t1
                join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '3' and t2.STR_TYPE = 'IV-V'
                where orgid = p_orgid and repdateid = p_repdateid;

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_tmp1 + v_tmp2 + v_tmp3, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1 + v_tmp2 + v_tmp3);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;         
        
        if (p_rate = 'RSV_ACT_IV_V' or p_rate is null)
        then
            v_rate := 'RSV_ACT_IV_V';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('RSV_ACT_IV_V', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                select nvl(rvp_kk4,0) + nvl(rvp_kk5,0) into v_tmp1 from PROT.DATA_F115_1 t1
                join ref_ko_115 t2 on t1.LINE_ID = t2.id and t2.section = '1' and t2.line = '4' 
                where orgid = p_orgid and repdateid = p_repdateid;

                select nvl(sum(rvp_fact),0) into v_tmp2 from data_f115_2 t1
                join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '2' and t2.STR_TYPE = 'IV-V'
                where orgid = p_orgid and repdateid = p_repdateid;
                
                select nvl(sum(rvp_fact),0) into v_tmp3 from data_f115_3 t1
                join ref_ko_115 t2 on t1.line_id = t2.id and t2.section = '3' and t2.STR_TYPE = 'IV-V'
                where orgid = p_orgid and repdateid = p_repdateid;

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, v_tmp1 + v_tmp2 + v_tmp3, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, v_tmp1 + v_tmp2 + v_tmp3);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
        
        if (p_rate = 'ACT_RSV_MAIN_KAP_IV_V' or p_rate is null)
        then
            v_rate := 'ACT_RSV_MAIN_KAP_IV_V';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('ACT_RSV_MAIN_KAP_IV_V', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'ACT_IV_V');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'RSV_ACT_IV_V');
                v_tmp3 := get_frate(p_orgid, p_repdateid, 'MAIN_KAP');

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, ((v_tmp1 - v_tmp2)/v_tmp3)*100, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, ((v_tmp1 - v_tmp2)/v_tmp3)*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;        
            
        if (p_rate = 'ACT_RSV_IV_V' or p_rate is null)
        then
            v_rate := 'ACT_RSV_IV_V';
            v_tmp1 := 0;
            v_tmp2 := 0;
            v_tmp3 := 0;
            calc_frate('ACT_RSV_IV_V', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
            begin
                select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                v_tmp1 := get_frate(p_orgid, p_repdateid, 'ACT_IV_V');
                v_tmp2 := get_frate(p_orgid, p_repdateid, 'RSV_ACT_IV_V');

                --insert into data_fin_rates values (data_fin_rates_seq.nextval, p_orgid, p_repdateid, v_rate_id, (v_tmp2/v_tmp1)*100, sysdate);
                set_frate(p_orgid, p_repdateid, v_rate_id, (v_tmp2/v_tmp1)*100);
                                
                exception when others then 
                    v_sqlerrm := substr(sqlerrm, 1, 200);
                    inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
            end;
        end if;
      
        if (v_period = 'q')
        then
            if (p_rate = 'COR' or p_rate is null)
            then
                v_rate := 'COR';
                v_tmp1 := 0;
                v_tmp2 := 0;
                v_tmp3 := 0;
                inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'begin calculate ' || v_rate, p_fid);            
                begin
                    select id into v_rate_id from ref_ko_fin_rates where code = v_rate;
                
                    select case when to_number(to_char(rep_date, 'MM')) = 1 then 12 else to_number(to_char(rep_date, 'MM')) -1 end into v_mn from uds.report_calendar where id = p_repdateid;
                
                    v_tmp1 := abs(get_frate(p_orgid, p_repdateid, 'RSV_COST'));
                    v_tmp3 := get_frate(p_orgid, p_repdateid, 'ACT_RSV');

                    set_frate(p_orgid, p_repdateid, v_rate_id, ((v_tmp1*12/v_mn)/(v_tmp3))*100);
                              
                    exception when others then 
                        v_sqlerrm := substr(sqlerrm, 1, 200);
                        inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, v_rate || ' ' || v_sqlerrm, p_fid);
                end;
            end if; 
            
            if (p_rate is null or p_rate = 'COR_LTM')
            then
                calc_frate('COR_LTM', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
            end if;
        else
            copy_qrate('COR', p_orgid, p_repdateid, p_fid);
            copy_qrate('COR_LTM', p_orgid, p_repdateid, p_fid);
        end if;
        
        if (p_rate is null or p_rate = 'DELTA_RVP_SS')
        then
            calc_frate('DELTA_RVP_SS', p_orgid, p_repdateid, v_prevdateid, v_rep_date, p_fid);
        end if;          

    end if;
    
    ---------------------121---------------------
    if (p_formid = 34 or p_formid is null)
    then
        for dd in (select data_fin_rates_seq.nextval pid, p_orgid, p_repdateid, t2.id, t4.NORM_VALUE from ref_ko_rates_forms t1
        join ref_ko_fin_rates t2 on t1.rate_id = t2.id
        join ref_norms t3 on t2.code = t3.code 
        join data_norms t4 on t4.orgid = p_orgid and t4.repdateid = p_repdateid and t4.norm_id = t3.id
        where form_id = 34)
        loop
            insert into data_fin_rates(id, orgid, repdateid, rate_id, value, updatedt) 
            values (dd.pid, dd.p_orgid, dd.p_repdateid, dd.id, dd.norm_value, sysdate);
            
            inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, 'f34: id = ' || dd.pid, p_fid);
        end loop;
        --inp_add_log(1, 1000, 'CALC_RATES', $$PLSQL_UNIT, $$PLSQL_UNIT, sql%rowcount || ' rows inserted into data_fin_rates for formid = 34', p_fid);
    end if;
    
    ---------------------722---------------------
    if (p_formid = 35 or p_formid is null)
    then
        insert into data_fin_rates select data_fin_rates_seq.nextval, p_orgid, p_repdateid, t2.id, t4.VALUE, sysdate 
        from ref_ko_rates_forms t1
        join ref_ko_fin_rates t2 on t1.rate_id = t2.id
        join ref_ko_722 t3 on t2.name = t3.name
        join data_F722 t4 on t4.orgid = p_orgid and t4.repdateid = p_repdateid and t4.norm_id = t3.id
        where form_id = 35;
    end if;    
    
    exception when others then
        v_sqlerrm := substr(sqlerrm, 1, 200);
        inp_add_log(1, 1000, 'CHECK_FORMS', $$PLSQL_UNIT, $$PLSQL_UNIT, v_sqlerrm, p_fid);
end;
/
```