ALTER PROCEDURE "DBA"."pr_pv_kan_arvele"(in a_skoop integer,in peralg date,in perlop date)
begin
  declare li_detimp smallint;
  declare li_imp_kuup smallint;
  declare li_imp_asutnr smallint;
  declare li_imp_arvenr smallint;
  declare li_yksusnr integer;
  declare ld_kuup date;
  declare ldc_summa decimal(15,2);
  declare ls_nimi char(100);
  declare ls_info char(200);
  declare ldc_ytk decimal(15,2);
  declare ls_order char(3);
  declare ls_deeb char(7);
  declare ls_kree char(7);
  declare ls_konto char(7);
  declare ls_yksuskonto char(7);
  declare ls_yksusdeeb char(7);
  declare ls_yksuskree char(7);
  declare ls_objdeeb char(7);
  declare ls_objkree char(7);
  declare li_partnr integer;
  declare li_asutnr integer;
  declare li_meetnr integer;
  declare li_kulnr integer;
  declare li_talnr integer;
  declare li_artnr integer;
  declare li_tulnr integer;
  declare li_objectnr integer;
  declare li_osaknr integer;
  declare li_subjectnr integer;
  declare li_suundnr integer;
  declare li_taotnr integer;
  declare li_alliknr integer;
  declare li_ravonr integer;
  declare li_ravonr4 integer;
  declare li_objravonr integer;
  declare li_skoop integer;
  declare ls_dk_kontod char(5000);
  declare ls_kp_kontod char(5000);
  declare err_notfound exception for sqlstate value '02000';
  declare c_pv_yksus dynamic scroll cursor for select pv_yksus.konto,pv_yksus.deeb,pv_yksus.kree,pv_yksus."order",pv_yksus.yksusnr,if pv_yksus.skoop is null then 0 else pv_yksus.skoop endif from pv_yksus where(pv_yksus.skoop = a_skoop or a_skoop = 0) and length(trim(pv_yksus."order")) > 0;
  declare c_pv_kanne_arvele dynamic scroll cursor for
    select pv_kanne.kree,
           pv_kanne.konto,
           if li_imp_kuup=1 then pv_kanne.kuup else perlop endif,
           if li_detimp=1 then pv_kanne.nimi else '' endif,
           pv_kanne.alg_m,
           pv_kanne.ytk,
           if asulist.partnr is null then 0 else asulist.partnr endif,
           if li_imp_asutnr=0 or pv_kanne.asutnr is null then 0 else pv_kanne.asutnr endif,
           if pv_kanne.meetnr is null then(if pv_yksus.meetnr is null then 0 else pv_yksus.meetnr endif) else pv_kanne.meetnr endif,
           if pv_kanne.kulnr is null then(if pv_yksus.kulnr is null then 0 else pv_yksus.kulnr endif) else pv_kanne.kulnr endif,
           if pv_kanne.talnr is null then(if pv_yksus.talnr is null then 0 else pv_yksus.talnr endif) else pv_kanne.talnr endif,
           if pv_kanne.artnr is null then 0 else pv_kanne.artnr endif,if pv_kanne.tulnr is null then 0 else pv_kanne.tulnr endif,
           if pv_kanne.objectnr is null then 0 else pv_kanne.objectnr endif,
           if pv_kanne.osaknr is null then(if pv_yksus.osaknr is null then 0 else pv_yksus.osaknr endif) else pv_kanne.osaknr endif,
           if pv_kanne.subjectnr is null then (if pv_yksus.subjectnr is null then 0 else pv_yksus.subjectnr endif) else pv_kanne.subjectnr endif,
           if pv_kanne.suundnr is null then(if pv_yksus.suundnr is null then 0 else pv_yksus.suundnr endif) else pv_kanne.suundnr endif,
           if pv_kanne.taotnr is null then(if pv_yksus.taotnr is null then 0 else pv_yksus.taotnr endif) else pv_kanne.taotnr endif,
           if pv_kanne.alliknr is null then 0 else pv_kanne.alliknr endif,if pv_kanne.ravonr is null then 0 else pv_kanne.ravonr endif,
           if pv_kanne.ravonr4 is null then 0 else pv_kanne.ravonr4 endif
      from pv_kanne left outer join pv_yksus on pv_kanne.yksusnr = pv_yksus.yksusnr
                    left outer join pv_obj on pv_kanne.objnr = pv_obj.objnr
                    left outer join asulist on pv_kanne.asutnr = asulist.asutnr
      where pv_kanne.yksusnr = li_yksusnr and pv_kanne.kuup >= peralg and pv_kanne.kuup <= perlop and not(locate(ls_dk_kontod,';'+"Left"(pv_kanne.kree+Space(7),7)+';') > 0) and not(locate(ls_kp_kontod,';'+"Left"(pv_kanne.kree+Space(7),7)+';') > 0);
  select yld_imp_pv_det,
         if yld_imp_pv_det=1 or yld_imp_kuup=1 then 1 else 0 endif,
         if yld_imp_pv_det=1 or yld_imp_arvenr=1 then 1 else 0 endif,
         if yld_imp_pv_det=1 or yld_imp_arvenr=1 or yld_imp_asutnr=1 then 1 else 0 endif
    into li_detimp,li_imp_kuup,li_imp_arvenr,li_imp_asutnr from pr_konfm where pr_konfm.id=1;
  open c_pv_yksus;
  pv_yksus_loop: loop
    fetch next c_pv_yksus into ls_yksuskonto,ls_yksusdeeb,ls_yksuskree,ls_order,li_yksusnr,li_skoop;
    if sqlstate = err_notfound then -- kui eof
      leave pv_yksus_loop
    end if;
    set ls_dk_kontod=get_dk_kontod(li_skoop,0);
    set ls_kp_kontod=get_kp_kontod(li_skoop,0);
    if ls_yksuskonto is not null then
      open c_pv_kanne_arvele;
      pv_kanne_arvele_loop: loop
        --  loe pohivahend
        fetch next c_pv_kanne_arvele into ls_kree,ls_konto,ld_kuup,ls_nimi,ldc_summa,ldc_ytk,li_partnr,li_asutnr,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr,li_ravonr4;
        if sqlstate = err_notfound then -- kui eof
          leave pv_kanne_arvele_loop
        end if;
        if ls_konto is null then
          set ls_konto = ls_kree
        end if;
        if li_ravonr4 = 0 then
          set li_ravonr4 = li_ravonr
        end if;
        if (ls_kree is null or ls_kree = ls_konto) and li_ravonr = li_ravonr4 then
          if ldc_ytk <> 0 and ls_yksuskree is not null then
            -- kas kulumi arvelevotmise kanne ei eksisteeri koondis
            if li_detimp=0 and exists(select 1 from gl_pr_kanne_import where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_yksuskonto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = ls_yksuskree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr) then
              update gl_pr_kanne_import set summa = summa+ldc_ytk where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_yksuskonto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = ls_yksuskree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr
            else
              set ls_info=Trim(ls_nimi+' kulumi arvelevőtmine');
              insert into gl_pr_kanne_import(tulnud,kuup,summa,"info","order",deeb,partnr,kree,partnr2,asutnr,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr,skoop) values('Pohivarad',ld_kuup,ldc_ytk,ls_info,ls_order,ls_yksuskonto,li_partnr,ls_yksuskree,li_partnr,li_asutnr,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr,li_skoop)
            end if
          end if;
          set ldc_summa=ldc_summa-ldc_ytk;
          if ldc_summa <> 0 and ls_kree is not null then
            -- kas arvelevotmise kanne ei eksisteeri koondis
            if li_detimp=0 and exists(select 1 from gl_pr_kanne_import where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_yksuskonto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = ls_kree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr) then
              update gl_pr_kanne_import set summa = summa+ldc_summa where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_yksuskonto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = ls_kree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr
            else
              set ls_info=Trim(ls_nimi+' arvelevőtmine');
              insert into gl_pr_kanne_import(tulnud,kuup,summa,"info","order",deeb,partnr,kree,partnr2,asutnr,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr,skoop) values('Pohivarad',ld_kuup,ldc_summa,ls_info,ls_order,ls_yksuskonto,li_partnr,ls_kree,li_partnr,li_asutnr,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr,li_skoop)
            end if
          end if
        else
          if li_ravonr = li_ravonr4 then
            if ldc_ytk <> 0 and ls_yksuskree is not null then
              -- kas kulumi arvelevotmise kanne ei eksisteeri koondis
              if li_detimp=0 and exists(select 1 from gl_pr_kanne_import where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_konto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = ls_yksuskree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr) then
                update gl_pr_kanne_import set summa = summa+ldc_ytk where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_konto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = ls_yksuskree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr
              else
                set ls_info=Trim(ls_nimi+' kulumi arvelevőtmine');
                insert into gl_pr_kanne_import(tulnud,kuup,summa,"info","order",deeb,partnr,kree,partnr2,asutnr,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr,skoop) values('Pohivarad',ld_kuup,ldc_ytk,ls_info,ls_order,ls_konto,li_partnr,ls_yksuskree,li_partnr,li_asutnr,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr,li_skoop)
              end if
            end if;
            if ldc_summa <> 0 and ls_kree is not null then
              -- kas arvelevotmise kanne ei eksisteeri koondis
              if li_detimp=0 and exists(select 1 from gl_pr_kanne_import where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_yksuskonto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = ls_kree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr) then
                update gl_pr_kanne_import set summa = summa+ldc_summa where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_yksuskonto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = ls_kree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr
              else
                set ls_info=Trim(ls_nimi+' arvelevőtmine');
                insert into gl_pr_kanne_import(tulnud,kuup,summa,"info","order",deeb,partnr,kree,partnr2,asutnr,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr,skoop) values('Pohivarad',ld_kuup,ldc_summa,ls_info,ls_order,ls_yksuskonto,li_partnr,ls_kree,li_partnr,li_asutnr,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr,li_skoop)
              end if
            end if
          else
            if ldc_ytk <> 0 and ls_yksuskree is not null then
              -- kas kulumi arvelevotmise deebet kanne ei eksisteeri koondis
              if li_detimp=0 and exists(select 1 from gl_pr_kanne_import where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_konto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = 'xxxxxyz' and gl_pr_kanne_import.partnr2 = 0 and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr4) then
                update gl_pr_kanne_import set summa = summa+ldc_ytk where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_konto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = 'xxxxxyz' and gl_pr_kanne_import.partnr2 = 0 and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr4
              else
                set ls_info=Trim(ls_nimi+' kulumi arvelevőtmine');
                insert into gl_pr_kanne_import(tulnud,kuup,summa,"info","order",deeb,partnr,kree,partnr2,asutnr,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr,skoop) values('Pohivarad',ld_kuup,ldc_ytk,ls_info,ls_order,ls_konto,li_partnr,'xxxxxyz',0,li_asutnr,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr4,li_skoop)
              end if;
              -- kas kulumi arvelevotmise kreedit kanne ei eksisteeri koondis
              if li_detimp=0 and exists(select 1 from gl_pr_kanne_import where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = 'xxxxxyz' and gl_pr_kanne_import.partnr = 0 and gl_pr_kanne_import.kree = ls_yksuskree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr) then
                update gl_pr_kanne_import set summa = summa+ldc_ytk where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = 'xxxxxyz' and gl_pr_kanne_import.partnr = 0 and gl_pr_kanne_import.kree = ls_yksuskree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr
              else
                set ls_info=Trim(ls_nimi+' kulumi arvelevőtmine');
                insert into gl_pr_kanne_import(tulnud,kuup,summa,"info","order",deeb,partnr,kree,partnr2,asutnr,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr,skoop) values('Pohivarad',ld_kuup,ldc_ytk,ls_info,ls_order,'xxxxxyz',0,ls_yksuskree,li_partnr,li_asutnr,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr,li_skoop)
              end if
            end if;
            if ldc_summa <> 0 and ls_kree is not null then
              -- kas arvelevotmise deebet kanne ei eksisteeri koondis
              if li_detimp=0 and exists(select 1 from gl_pr_kanne_import where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_yksuskonto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = 'xxxxxyz' and gl_pr_kanne_import.partnr2 = 0 and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr) then
                update gl_pr_kanne_import set summa = summa+ldc_summa where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = ls_yksuskonto and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.kree = 'xxxxxyz' and gl_pr_kanne_import.partnr2 = 0 and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr
              else
                set ls_info=Trim(ls_nimi+' arvelevőtmine');
                insert into gl_pr_kanne_import(tulnud,kuup,summa,"info","order",deeb,partnr,kree,partnr2,asutnr,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr,skoop) values('Pohivarad',ld_kuup,ldc_summa,ls_info,ls_order,ls_yksuskonto,li_partnr,'xxxxxyz',0,li_asutnr,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr,li_skoop)
              end if;
              -- kas arvelevotmise kreedit kanne ei eksisteeri koondis
              if li_detimp=0 and exists(select 1 from gl_pr_kanne_import where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = 'xxxxxyz' and gl_pr_kanne_import.partnr = 0 and gl_pr_kanne_import.kree = ls_kree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr4) then
                update gl_pr_kanne_import set summa = summa+ldc_summa where gl_pr_kanne_import.kuup = perlop and gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import."order" = ls_order and gl_pr_kanne_import.deeb = 'xxxxxyz' and gl_pr_kanne_import.partnr = 0 and gl_pr_kanne_import.kree = ls_kree and gl_pr_kanne_import.partnr2 = li_partnr and gl_pr_kanne_import.asutnr = li_asutnr and gl_pr_kanne_import.meetnr = li_meetnr and gl_pr_kanne_import.taotnr = li_taotnr and gl_pr_kanne_import.kulnr = li_kulnr and gl_pr_kanne_import.talnr = li_talnr and gl_pr_kanne_import.artnr = li_artnr and gl_pr_kanne_import.tulnr = li_tulnr and gl_pr_kanne_import.objectnr = li_objectnr and gl_pr_kanne_import.osaknr = li_osaknr and gl_pr_kanne_import.subjectnr = li_subjectnr and gl_pr_kanne_import.suundnr = li_suundnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr4
              else
                set ls_info=Trim(ls_nimi+' arvelevőtmine');
                insert into gl_pr_kanne_import(tulnud,kuup,summa,"info","order",deeb,partnr,kree,partnr2,asutnr,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr,skoop) values('Pohivarad',ld_kuup,ldc_summa,ls_info,ls_order,'xxxxxyz',0,ls_kree,li_partnr,li_asutnr,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr4,li_skoop)
              end if
            end if
          end if
        end if
      end loop pv_kanne_arvele_loop;
      close c_pv_kanne_arvele
    end if;
  end loop pv_yksus_loop;
  close c_pv_yksus;
end