ALTER TRIGGER "tib_mt_kanne" before insert order 1 on
DBA.mt_kanne
referencing new as new_name
for each row
begin
  declare li_id integer;
  declare ls_doknr char(20);
  declare li_years integer;
  set li_years=years(new_name.kuup);
  if(new_name."id" is null) or(new_name."id" = 0) then
    select getid_mt_kanne(li_years) into li_id from DUMMY;
    set new_name."id"=li_id
  end if;
  if(new_name.doknr is null) or(new_name.doknr = '') then
    select get_doknr_mt(li_years,new_name.objnr,new_name.sisse,new_name.yksusnr) into ls_doknr from DUMMY;
    set new_name.doknr=ls_doknr
  end if;
  if (new_name.sisse=0 and ((new_name.arvenr is null) or(new_name.arvenr = ''))) then
    set new_name.arvenr=new_name.doknr;
    set new_name.arvekuup=new_name.kuup
  end if;
  if new_name.const is null then
    set new_name.const='K'
  end if;
  if new_name.sisse = 1 then
    set new_name.const='X'
  end if;
  if new_name.kogus is null then
    set new_name.kogus=0
  end if;
  if new_name.hind is null then
    set new_name.hind=0
  end if;
  if new_name.summa2 is null then
    set new_name.summa2=0
  end if;
  call mt_add_toode(new_name.tootnr,new_name.yksusnr);
  if li_years>2020 then
    call pr_kanne_add(month(new_name.kuup),li_years,new_name.yksusnr,7);
  end if;
  set new_name.aasta=li_years;
  set new_name.kell=Now(*);
  set new_name.kell2=Now(*)
end