ALTER PROCEDURE "dba"."kp_pa_kintul"(in a_tas_s integer,in a_objnr integer,in a_vali_objnr integer,in a_user_code integer,in a_perlop date,in a_v_akuu numeric(2),in a_v_aasta numeric(4),in a_skoop integer)
begin
  declare li_asutnr integer;
  declare li_saajanr integer;
  declare ls_algol char(2);
  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_panknr integer;
  declare ls_sarve char(30);
  declare ls_viitnr char(35);
  declare ls_tulnud char(100);
  declare ld_arvekuup date;
  declare ls_arvenr char(20);
  declare ll_asutnr integer;
  declare li_ver2 smallint;
  declare err_notfound exception for sqlstate value '02000';
  declare c_tasu dynamic scroll cursor for select pa_tasu.tulnud
                 from pa_tasu left outer join pa_yksus on pa_tasu.yksusnr = pa_yksus.yksusnr
                 where pa_tasu.tas_s = a_tas_s and pa_tasu.v_aasta = a_v_aasta and pa_tasu.v_akuu = a_v_akuu and pa_yksus.skoop = a_skoop and (pa_yksus.kp_objnr = a_objnr or a_vali_objnr = 0);
  select (if euroforeve=1 and euroforeve_pa=0 then 1 else 0 endif) into li_ver2 from tm_rmpt where "id"=1;
  select pa_taslist.algol into ls_algol from pa_taslist where pa_taslist.tas_s=a_tas_s;
  delete from gl_kp_kanne;
  open c_tasu;
  tasu_loop: loop
    fetch next c_tasu into ls_tulnud;
    if sqlstate = err_notfound then
      leave tasu_loop
    end if;
    if ls_algol='7a' and Length(ls_tulnud)=60 then
      set ld_arvekuup=convert(date,substring(ls_tulnud,6,10));
      set ls_arvenr=trim(substring(ls_tulnud,21,20));
      set ll_asutnr=convert(integer,substring(ls_tulnud,46,10));
      insert into gl_kp_kanne( kuup,
                               summa,
                               sisse,
                               "info",
                               tulnud,
                               user_code,
                               objnr,
                               asutnr,
                               arvekuup,
                               arvenr,
                               konto,
                               meetnr,
                               kulnr,
                               talnr,
                               artnr,
                               tulnr,
                               objectnr,
                               osaknr,
                               subjectnr,
                               suundnr,
                               taotnr,
                               alliknr,
                               ravonr,
                               id_maks)
          select a_perlop,
                 if dk_kanne.sisse=1 then dk_kanne.summa+dk_kanne.summa2 else -1*(dk_kanne.summa+dk_kanne.summa2) endif,
                 1,
                 trim(dk_alus.nimi+' '+dk_kanne."info"),
                 'Kinnip.tul'+Trim(convert(char(5),a_tas_s)),
                 a_user_code,
                 a_objnr,
                 ll_asutnr,
                 ld_arvekuup,
                 ls_arvenr,
                 dk_obj.djkonto,
                 dk_kanne.meetnr,
                 dk_kanne.kulnr,
                 dk_kanne.talnr,
                 dk_kanne.artnr,
                 dk_kanne.tulnr,
                 dk_kanne.objectnr,
                 dk_kanne.osaknr,
                 dk_kanne.subjectnr,
                 dk_kanne.suundnr,
                 dk_kanne.taotnr,
                 dk_kanne.alliknr,
                 dk_kanne.ravonr,
                 dk_kanne.id_maks
             from dk_kanne key left outer join dk_obj,dk_kanne key left outer join dk_alus
             where dk_kanne.arvekuup = ld_arvekuup and dk_kanne.arvenr = ls_arvenr and dk_kanne.asutnr = ll_asutnr and (dk_obj.skoop = a_skoop or a_skoop = 0) and dk_obj.spank=0 and dk_obj.mke=1;
    end if;
  end loop tasu_loop;
  close c_tasu;
  delete from gl_kp_kanne where gl_kp_kanne.summa = 0;
end