declare
  v_amsh_rest number(31,11);
  v_com_rest number(22,2);
  v_com_0 number (22,2);
  v_com number (22,2);
begin
  update GK_REESTR_COMMS
     set COMM_SUM_NO_NDS = round(COMM_SUM * dbo.S_COM_CURS(1,COMM_VALUT,34,COMM_DATE) * 100
                                 /(100 + nvl(dbo.S_GETVAR(case COMM_TYPE  
                                                            when 1 then 's_gk_com_birga'  
                                                            when 2 then 's_gk_com_cliring'  
                                                            when 3 then 's_gk_com_ITS'  
                                                            when 4 then 's_gk_com_diler'  
                                                            when 7 then 's_gk_com_birga'  
                                                          end,COMM_DATE),0)),2)
   where COMM_TYPE < 20
     and exists (select null from GK_REESTR where ID = GK_REESTR_COMMS.VNESHID and TOPDOCOID = 1003117);

  for q0 in (select R.AMSHALL, dbo.S_GKO_FNOMINAL(R.CDKDSH_ID,R.TIMEDEAL) NOMINAL
                  , case when RC.COMM_TYPE > 20 then RC.COMM_TYPE-20 else RC.COMM_TYPE+20 end as COMM_TYPE_NEW
                  , R.ID
                  , RC.ID as RCID
                  , case when R.PARENT_ID is null then 0 else 1 end IS_REPO
                  , RC.COMM_SUM
                  , RC.COMM_SUM_RUB
               from GK_REESTR R
               join GK_REESTR_COMMS RC on RC.VNESHID = R.ID/* and RC.COMM_SUM_NO_NDS <> 0*/
              where (R.PARENT_ID is null and RC.COMM_TYPE < 20
                  or R.PARENT_ID is not null and R.DOCOID = 1004904 and R.REPOPARENT_ID is null and RC.COMM_TYPE > 20)
              order by R.ID
             )
  loop

    begin
      select COMM_SUM_NO_NDS
        into v_com_0
        from GK_REESTR_COMMS
       where ID = q0.RCID;
    exception
      when NO_DATA_FOUND then
        raise_application_error(-20202,'#I проблема со скриптом, обратитесь к разработчику');
    end;

    if v_com_0 = q0.COMM_SUM then
      update GK_REESTR_COMMS RC
         set COMM_SUM_NO_NDS = COMM_SUM
       where VNESHID in (select ID from GK_REESTR where case q0.IS_REPO when 0 then PARENT_ID else REPOPARENT_ID end = q0.ID);
    elsif v_com_0 = q0.COMM_SUM_RUB then
      update GK_REESTR_COMMS RC
         set COMM_SUM_NO_NDS = COMM_SUM_RUB
       where VNESHID in (select ID from GK_REESTR where case q0.IS_REPO when 0 then PARENT_ID else REPOPARENT_ID end = q0.ID);
    else
      v_amsh_rest := q0.AMSHALL;
      v_com_rest := v_com_0;
  
      for q1 in (select R.DOCOID, nvl(R.AMSHALL,0) as AMSHALL, RC.ID as RCID, RC.COMM_SUM_NO_NDS, dbo.S_GKO_FNOMINAL(R.CDKDSH_ID,R.TIMEDEAL) NOMINAL
                   from GK_REESTR R
                   join GK_REESTR_COMMS RC on RC.VNESHID = R.ID and RC.COMM_TYPE = q0.COMM_TYPE_NEW
                  where case q0.IS_REPO when 0 then R.PARENT_ID else R.REPOPARENT_ID end = q0.ID
                  order by R.TIMEDEAL,R.ID
                 )
      loop
        if q1.COMM_SUM_NO_NDS <> 0 then
          raise_application_error(-20202,'#I Повторный запуск скрипта. q0.ID = '||to_char(q0.ID)||', q1.RCID = '||to_char(q1.RCID));
        end if;
  
        if q1.AMSHALL > v_amsh_rest then
          raise_application_error(-20202,'#I попытка списать больше ц/б, чем осталось. q0.ID = '||to_char(q0.ID)||', q1.RCID = '||to_char(q1.RCID));
        end if;
  
        v_com := v_com_rest - round(v_com_0 * (v_amsh_rest - q1.AMSHALL) * q1.NOMINAL / q0.AMSHALL / q0.NOMINAL, 2);
  
        if v_com > v_com_rest then
          raise_application_error(-20202,'#I попытка списать больше комиссии, чем осталось. q0.ID = '||to_char(q0.ID)||', q1.RCID = '||to_char(q1.RCID));
        end if;
        
        update GK_REESTR_COMMS set COMM_SUM_NO_NDS = v_com where ID = q1.RCID;
        v_com_rest := v_com_rest - v_com;
        v_amsh_rest := v_amsh_rest - q1.AMSHALL;
        
      end loop;
    end if;

  end loop;

end;




