ALTER PROCEDURE "DBA"."pr_pa_kan_erisood"(in a_skoop integer,in a_peralg date,in a_perlop date)
begin
  declare li_ii integer;
  declare li_v_aasta numeric(4);
  declare li_v_akuu numeric(2);
  declare li_tasu_v_aasta numeric(4);
  declare li_tasu_v_akuu numeric(2);
  declare li_tasu_a_aasta numeric(4);
  declare li_tasu_a_akuu numeric(2);
  declare li_tasu_t_aasta numeric(4);
  declare li_tasu_t_akuu numeric(2);
  declare ldc_summa decimal(15,2);
  declare ldc_tulumaks decimal(15,2);
  declare ldc_sotsmaks decimal(15,2);
  declare ldc_tatkmaks 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_tasudeeb char(7);
  declare ls_tasukree char(7);
  declare ls_vtasukree char(7);
  declare li_samart smallint;
  declare li_samkon smallint;
  declare ls_tulukree_mata char(7);
  declare ls_tvtkkree_mata char(7);
  declare ls_penskree_mata char(7);
  declare ls_sotskree_mata char(7);
  declare ls_tatkkree_mata char(7);
  declare ls_tulukree char(7);
  declare ls_tvtkkree char(7);
  declare ls_penskree char(7);
  declare ls_sotsdeeb char(7);
  declare ls_sotskree char(7);
  declare ls_sotskonto char(7);
  declare ls_sotsart char(16);
  declare li_sotsartnr integer;
  declare ls_tatkdeeb char(7);
  declare ls_tatkkree char(7);
  declare ls_tatkkonto char(7);
  declare ls_tatkart char(16);
  declare li_tatkartnr integer;
  declare ls_eristuludeeb char(7);
  declare ls_eristulukree char(7);
  declare ls_eristuluart char(16);
  declare li_eristuluartnr integer;
  declare ls_eris5tuludeeb char(7);
  declare ls_eris5tulukree char(7);
  declare ls_eris5tuluart char(16);
  declare li_eris5tuluartnr integer;
  declare ls_erissotsdeeb char(7);
  declare ls_erissotskree char(7);
  declare ls_erissotsart char(16);
  declare li_erissotsartnr integer;
  declare li_tasupartnr integer;
  declare li_asutpartnr integer;
  declare li_partnr integer;
  declare li_partnr2 integer;
  declare li_meetnr integer;
  declare li_kulnr integer;
  declare li_kul2nr 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_skoop integer;
  declare li_objnr integer;
  declare ls_algol char(2);
  declare ls_info char(50);
  declare li_sisse smallint;
  declare err_notfound exception for sqlstate value '02000';
  declare c_pa_tasu_arvest dynamic scroll cursor for
    select pa_tasu.summa,
          (if pa_taslist_a.sisse = 0 then pa_tasu.sotsmaks else(-1)*pa_tasu.sotsmaks endif),
          (if pa_taslist_a.sisse = 0 then pa_tasu.tatkmaks else(-1)*pa_tasu.tatkmaks endif),
           pa_tasu.v_akuu,pa_tasu.v_aasta,pa_tasu.a_akuu,pa_tasu.a_aasta,pa_tasu.t_akuu,pa_tasu.t_aasta,
           pa_taslist_a.nimi,
          (if pa_taslist_a.deeb is null then (if pa_taslist_b.deeb is null then 'xxxxxyz' else pa_taslist_b.deeb endif) else pa_taslist_a.deeb endif),
          (if pa_taslist_a.kree is null then (if pa_taslist_b.kree is null then 'xxxxxyz' else pa_taslist_b.kree endif) else pa_taslist_a.kree endif),
          (if pa_taslist_a.konto is null then (if pa_taslist_b.konto is null then 'xxxxxyz' else pa_taslist_b.konto endif) else pa_taslist_a.konto endif),
          (if pa_taslist_a.sisse = 0 then (if pa_taslist_a.kree is null then 'xxxxxyz' else pa_taslist_a.kree endif) else (if pa_taslist_b.kree is null then 'xxxxxyz' else pa_taslist_b.kree endif) endif),
          (if pa_taslist_a.partnr is null then(if asulist_b.partnr is null then 0 else asulist_b.partnr endif) else pa_taslist_a.partnr endif),
           if asulist_a.partnr is null then 0 else asulist_a.partnr endif,
           if pa_yksus.skoop is null then 0 else pa_yksus.skoop endif,
          (if pa_paryld_b.meetnr is null then(if pa_taslist_a.meetnr is null then (if pa_taslist_b.meetnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif) else pa_taslist_b.meetnr endif) else pa_taslist_a.meetnr endif) else pa_paryld_b.meetnr endif),
          (if pa_paryld_b.kulnr is null then (if pa_taslist_a.kulnr is null then (if pa_taslist_b.kulnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif) else pa_taslist_b.kulnr endif) else pa_taslist_a.kulnr endif) else pa_paryld_b.kulnr endif),
          (if pa_paryld_b.talnr is null then (if pa_taslist_a.talnr is null then (if pa_taslist_b.talnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif) else pa_taslist_b.talnr endif) else pa_taslist_a.talnr endif) else pa_paryld_b.talnr endif),
          (if pa_taslist_a.artnr is null then (if pa_taslist_b.artnr is null then (if pa_yksus.artnr is null then 0 else pa_yksus.artnr endif) else pa_taslist_b.artnr endif) else pa_taslist_a.artnr endif),
          (if pa_taslist_a.tulnr is null then(if pa_taslist_b.tulnr is null then (if pa_yksus.tulnr is null then 0 else pa_yksus.tulnr endif) else pa_taslist_b.tulnr endif) else pa_taslist_a.tulnr endif),
          (if pa_paryld_b.objectnr is null then (if pa_taslist_a.objectnr is null then (if pa_taslist_b.objectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_taslist_b.objectnr endif) else pa_taslist_a.objectnr endif) else pa_paryld_b.objectnr endif),
          (if pa_paryld_b.osaknr is null then (if pa_taslist_a.osaknr is null then (if pa_taslist_b.osaknr is null or pa_taslist_b.samkul=0 then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_taslist_b.osaknr endif) else pa_taslist_a.osaknr endif) else pa_paryld_b.osaknr endif),
          (if pa_paryld_b.subjectnr is null then (if pa_taslist_a.subjectnr is null then (if pa_taslist_b.subjectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_taslist_b.subjectnr endif) else pa_taslist_a.subjectnr endif) else pa_paryld_b.subjectnr endif),
          (if pa_paryld_b.suundnr is null then (if pa_taslist_a.suundnr is null then (if pa_taslist_b.suundnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_taslist_b.suundnr endif) else pa_taslist_a.suundnr endif) else pa_paryld_b.suundnr endif),
          (if pa_paryld_b.taotnr is null then (if pa_taslist_a.taotnr is null then (if pa_taslist_b.taotnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif) else pa_taslist_b.taotnr endif) else pa_taslist_a.taotnr endif) else pa_paryld_b.taotnr endif),
           pa_taslist_a.algol,
           pa_taslist_a.samart,
           pa_taslist_a.samkon
    from pa_tasu left outer join pa_taslist as pa_taslist_a on pa_tasu.tas_s = pa_taslist_a.tas_s left outer join pa_taslist as pa_taslist_b on pa_tasu.vtas_s = pa_taslist_b.tas_s left outer join pa_yksus on pa_tasu.yksusnr = pa_yksus.yksusnr left outer join pa_paryld as pa_paryld_a on pa_tasu.paryldnr = pa_paryld_a.paryldnr left outer join pa_paryld as pa_paryld_b on pa_tasu.vparyldnr = pa_paryld_b.paryldnr left outer join asulist as asulist_b on pa_paryld_a.saajanr = asulist_b.asutnr left outer join pa_leping on pa_tasu.lepnr = pa_leping.lepnr left outer join asulist as asulist_a on pa_leping.asutnr = asulist_a.asutnr
    where(pa_yksus.skoop = a_skoop or a_skoop = 0) and pa_tasu.palgaleht is not null and(pa_tasu.a_aasta = li_v_aasta and pa_tasu.a_akuu = li_v_akuu) and((pa_tasu.a_aasta > pa_tasu.v_aasta) or(pa_tasu.a_aasta = pa_tasu.v_aasta and pa_tasu.a_akuu > pa_tasu.v_akuu)) and pa_taslist_a.sisse = 0;
  declare c_pa_erisood dynamic scroll cursor for select lisalist.objnr,pa_erisood.summa,pa_erisood.tulumaks,pa_erisood.sotsmaks,if pa_erisood.deeb is null then 'xxxxxyz' else pa_erisood.deeb
      endif,if pa_erisood.partnr is null then 0 else pa_erisood.partnr endif,if pa_erisood.kree is null then 'xxxxxyz' else pa_erisood.kree
      endif,if pa_erisood.partnr2 is null then 0 else pa_erisood.partnr2 endif,if pa_erisood.meetnr is null then if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif else pa_erisood.meetnr endif,if pa_erisood.kulnr is null then if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif else pa_erisood.kulnr endif,if pa_erisood.kul2nr is null then if pa_erisood.kulnr is null then if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif else pa_erisood.kulnr endif else pa_erisood.kul2nr endif,if pa_erisood.talnr is null then if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif else pa_erisood.talnr endif,if pa_erisood.artnr is null then (if pa_yksus.artnr is null then 0 else pa_yksus.artnr endif) else pa_erisood.artnr endif,if pa_erisood.tulnr is null then (if pa_yksus.tulnr is null then 0 else pa_yksus.tulnr endif) else pa_erisood.tulnr endif,if pa_erisood.objectnr is null then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_erisood.objectnr endif,if pa_erisood.osaknr is null then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_erisood.osaknr endif,if pa_erisood.subjectnr is null then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_erisood.subjectnr endif,if pa_erisood.suundnr is null then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_erisood.suundnr endif,if pa_erisood.taotnr is null then if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif else pa_erisood.taotnr endif,if pa_erisood.alliknr is null then 0 else pa_erisood.alliknr endif,if pa_erisood.ravonr is null then 0 else pa_erisood.ravonr endif,(if pa_erisood.samart is null or pa_erisood.samart=0 then 0 else 1 endif),pa_yksus.skoop from pa_erisood left outer join pa_yksus on pa_erisood.yksusnr = pa_yksus.yksusnr left outer join lisalist on pa_erisood.lisanr=lisalist.lisanr where(pa_yksus.skoop = a_skoop or a_skoop = 0) and(pa_erisood.v_aasta = li_v_aasta and pa_erisood.v_akuu = li_v_akuu);
  select if k_tulukree is null or Trim(k_tulukree) = '' then 'xxxxxyz' else k_tulukree
    endif,if k_tulukree_mata is null or Trim(k_tulukree_mata) = '' then 'xxxxxyz' else k_tulukree_mata
    endif,if k_tvtkkree is null or Trim(k_tvtkkree) = '' then 'xxxxxyz' else k_tvtkkree
    endif,if k_tvtkkree_mata is null or Trim(k_tvtkkree_mata) = '' then 'xxxxxyz' else k_tvtkkree_mata
    endif,if k_penskree is null or Trim(k_penskree) = '' then 'xxxxxyz' else k_penskree
    endif,if k_penskree_mata is null or Trim(k_penskree_mata) = '' then 'xxxxxyz' else k_penskree_mata
    endif,if k_sotskree is null or Trim(k_sotskree) = '' then 'xxxxxyz' else k_sotskree
    endif,if k_sotskree_mata is null or Trim(k_sotskree_mata) = '' then 'xxxxxyz' else k_sotskree_mata
    endif,if k_tatkkree is null or Trim(k_tatkkree) = '' then 'xxxxxyz' else k_tatkkree
    endif,if k_tatkkree_mata is null or Trim(k_tatkkree_mata) = '' then 'xxxxxyz' else k_tatkkree_mata
    endif,if k_sotsdeeb is null or Trim(k_sotsdeeb) = '' then 'xxxxxyz' else k_sotsdeeb
    endif,if k_tatkdeeb is null or Trim(k_tatkdeeb) = '' then 'xxxxxyz' else k_tatkdeeb
    endif,if k_sotsart is null or Trim(k_sotsart) = '' then 'xxxxxyz' else k_sotsart
    endif,if k_tatkart is null or Trim(k_tatkart) = '' then 'xxxxxyz' else k_tatkart
    endif,if k_eristuludeeb is null or Trim(k_eristuludeeb) = '' then 'xxxxxyz' else k_eristuludeeb
    endif,if k_eristulukree is null or Trim(k_eristulukree) = '' then 'xxxxxyz' else k_eristulukree
    endif,if k_eristuluart is null or Trim(k_eristuluart) = '' then 'xxxxxyz' else k_eristuluart
    endif,if k_eris5tuludeeb is null or Trim(k_eris5tuludeeb) = '' then 'xxxxxyz' else k_eris5tuludeeb
    endif,if k_eris5tulukree is null or Trim(k_eris5tulukree) = '' then 'xxxxxyz' else k_eris5tulukree
    endif,if k_eris5tuluart is null or Trim(k_eris5tuluart) = '' then 'xxxxxyz' else k_eris5tuluart
    endif,if k_erissotsdeeb is null or Trim(k_erissotsdeeb) = '' then 'xxxxxyz' else k_erissotsdeeb
    endif,if k_erissotskree is null or Trim(k_erissotskree) = '' then 'xxxxxyz' else k_erissotskree
    endif,if k_erissotsart is null or Trim(k_erissotsart) = '' then 'xxxxxyz' else k_erissotsart
    endif,if k_sotskonto is null or Trim(k_sotskonto) = '' then 'xxxxxyz' else k_sotskonto
    endif,if k_tatkkonto is null or Trim(k_tatkkonto) = '' then 'xxxxxyz' else k_tatkkonto
    endif into ls_tulukree,ls_tulukree_mata,ls_tvtkkree,ls_tvtkkree_mata,ls_penskree,ls_penskree_mata,ls_sotskree,ls_sotskree_mata,ls_tatkkree,ls_tatkkree_mata,ls_sotsdeeb,ls_tatkdeeb,ls_sotsart,ls_tatkart,ls_eristuludeeb,ls_eristulukree,ls_eristuluart,ls_eris5tuludeeb,ls_eris5tulukree,ls_eris5tuluart,ls_erissotsdeeb,ls_erissotskree,ls_erissotsart,ls_sotskonto,ls_tatkkonto from pa_konfm where pa_konfm.id = 1;
  select first artlist.artnr into li_sotsartnr from artlist where Trim(artlist.art) = ls_sotsart;
  if li_sotsartnr is null then
    set li_sotsartnr=0
  end if;
  select first artlist.artnr into li_tatkartnr from artlist where Trim(artlist.art) = ls_tatkart;
  if li_tatkartnr is null then
    set li_tatkartnr=0
  end if;
  select first artlist.artnr into li_eristuluartnr from artlist where Trim(artlist.art) = ls_eristuluart;
  if li_eristuluartnr is null then
    set li_eristuluartnr=0
  end if;
  select first artlist.artnr into li_eris5tuluartnr from artlist where Trim(artlist.art) = ls_eris5tuluart;
  if li_eris5tuluartnr is null then
    set li_eris5tuluartnr=0
  end if;
  select first artlist.artnr into li_erissotsartnr from artlist where Trim(artlist.art) = ls_erissotsart;
  if li_erissotsartnr is null then
    set li_erissotsartnr=0
  end if;
  select pa_konfm.yld_palk_mem into ls_order from pa_konfm where pa_konfm.id = 1;
  set li_v_aasta=Years(a_perlop);
  set li_v_akuu=Month(a_perlop);
  open c_pa_tasu_arvest;
  pa_tasu_arvest_loop: loop
    fetch next c_pa_tasu_arvest into ldc_summa,ldc_sotsmaks,ldc_tatkmaks,li_tasu_v_akuu,li_tasu_v_aasta,li_tasu_a_akuu,li_tasu_a_aasta,li_tasu_t_akuu,li_tasu_t_aasta,ls_info,ls_tasudeeb,ls_tasukree,ls_konto,ls_vtasukree,li_tasupartnr,li_asutpartnr,li_skoop,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,ls_algol,li_samart,li_samkon;
    if sqlstate = err_notfound then -- kui eof
      leave pa_tasu_arvest_loop
    end if;
    if li_tasupartnr = 0 then
      set li_tasupartnr=li_asutpartnr
    end if;
    if ls_konto = 'xxxxxyz' or ls_sotskonto = 'xxxxxyz' or ls_tatkkonto = 'xxxxxyz' then
      set ldc_summa=0
    else
      set li_ii=5;
      while li_ii <= 7 loop
        case li_ii
        when 5 then
          set ls_deeb=ls_tasudeeb;
          set li_partnr=li_tasupartnr;
          set ls_kree=ls_konto;
          set li_partnr2=li_tasupartnr
        when 6 then
          set ldc_summa=ldc_sotsmaks;
          if li_samkon = 1 then
            set ls_deeb=ls_tasudeeb
          else
            set ls_deeb=ls_sotsdeeb
          end if;
          set li_partnr=li_asutpartnr;
          set ls_kree=ls_sotskonto;
          set li_partnr2=li_asutpartnr;
          if not(li_samart = 1) then
            set li_artnr=li_sotsartnr
          end if;
          set ls_info='Sotsiaalmaks'
        when 7 then
          set ldc_summa=ldc_tatkmaks;
          if li_samkon = 1 then
            set ls_deeb=ls_tasudeeb
          else
            set ls_deeb=ls_tatkdeeb
          end if;
          set ls_kree=ls_tatkkonto;
          if not(li_samart = 1) then
            set li_artnr=li_tatkartnr
          end if;
          set ls_info='Tööandja töötuskindlustus'
        end case
        ;
        if ldc_summa <> 0 then
          if exists(select 1 from gl_pr_kanne_import where gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import.info = ls_info and gl_pr_kanne_import.deeb = ls_deeb and gl_pr_kanne_import.kree = ls_kree and gl_pr_kanne_import.meetnr = li_meetnr 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.taotnr = li_taotnr and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.partnr2 = li_partnr2) then
            update gl_pr_kanne_import set summa = summa+ldc_summa where gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import.info = ls_info and gl_pr_kanne_import.deeb = ls_deeb and gl_pr_kanne_import.kree = ls_kree and gl_pr_kanne_import.meetnr = li_meetnr 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.taotnr = li_taotnr and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.partnr2 = li_partnr2
          else
            insert into gl_pr_kanne_import( tulnud,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,artnr,tulnr,objectnr,skoop,osaknr,subjectnr,suundnr,taotnr,partnr,partnr2,info) values( 'Palk',a_perlop,ldc_summa,ls_order,ls_deeb,ls_kree,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_skoop,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_partnr,li_partnr2,ls_info) 
          end if;
        end if;
        set li_ii=li_ii+1
      end loop
    end if
  end loop pa_tasu_arvest_loop;
  close c_pa_tasu_arvest;
  select pa_konfm.yld_eris_mem into ls_order from pa_konfm where pa_konfm.id = 1;
  open c_pa_erisood;
  pa_erisood_loop: loop
    fetch next c_pa_erisood into li_objnr,ldc_summa,ldc_tulumaks,ldc_sotsmaks,ls_deeb,li_partnr,ls_kree,li_partnr2,li_meetnr,li_kulnr,li_kul2nr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr,li_samart,li_skoop;
    if sqlstate = err_notfound then -- kui eof
      leave pa_erisood_loop
    end if;
    set li_ii=1;
    while li_ii <= 3 loop
      case li_ii
      when 1 then
        set ls_info='Erisoodustus'
      when 2 then
        set ldc_summa=ldc_tulumaks;
        if li_objnr=40 or ls_eris5tuludeeb='xxxxxyz' then
          set ls_deeb=ls_eristuludeeb
        else
          set ls_deeb=ls_eris5tuludeeb
        end if;
        if li_objnr=40 or ls_eris5tulukree='xxxxxyz' then
          set ls_kree=ls_eristulukree
        else
          set ls_kree=ls_eris5tulukree
        end if;
        set li_kulnr=li_kul2nr;
        if li_samart=0 then
          if li_objnr=40 or li_eris5tuluartnr=0 then
            set li_artnr=li_eristuluartnr
          else
            set li_artnr=li_eris5tuluartnr
          end if
        end if;
        set ls_info='Tulumaks erisoodustustelt'
      when 3 then
        set ldc_summa=ldc_sotsmaks;
        set ls_deeb=ls_erissotsdeeb;
        set ls_kree=ls_erissotskree;
        set li_kulnr=li_kul2nr;
        if li_samart=0 then
          set li_artnr=li_erissotsartnr
        end if;
        set ls_info='Sotsiaalmaks erisoodustustelt'
      end case
      ;
      if ldc_summa <> 0 and ls_deeb <> 'xxxxxyz' and ls_kree <> 'xxxxxyz' then
        if exists(select 1 from gl_pr_kanne_import where gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import.info = ls_info and gl_pr_kanne_import.deeb = ls_deeb and gl_pr_kanne_import.kree = ls_kree and gl_pr_kanne_import.meetnr = li_meetnr 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.taotnr = li_taotnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.partnr2 = li_partnr2) then
          update gl_pr_kanne_import set summa = summa+ldc_summa where gl_pr_kanne_import.skoop = li_skoop and gl_pr_kanne_import.info = ls_info and gl_pr_kanne_import.deeb = ls_deeb and gl_pr_kanne_import.kree = ls_kree and gl_pr_kanne_import.meetnr = li_meetnr 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.taotnr = li_taotnr and gl_pr_kanne_import.alliknr = li_alliknr and gl_pr_kanne_import.ravonr = li_ravonr and gl_pr_kanne_import.partnr = li_partnr and gl_pr_kanne_import.partnr2 = li_partnr2
        else
          insert into gl_pr_kanne_import( tulnud,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,artnr,tulnr,objectnr,skoop,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr,partnr,partnr2,info) values( 'Palk',a_perlop,ldc_summa,ls_order,ls_deeb,ls_kree,li_meetnr,li_kulnr,li_talnr,li_artnr,li_tulnr,li_objectnr,li_skoop,li_osaknr,li_subjectnr,li_suundnr,li_taotnr,li_alliknr,li_ravonr,li_partnr,li_partnr2,ls_info) 
        end if;
      end if;
      set li_ii=li_ii+1
    end loop
  end loop pa_erisood_loop;
  close c_pa_erisood;
end