CREATE PROCEDURE "DBA"."update_tullist_artlist"( in a_par smallint default 3)
BEGIN
  declare ll_count integer;
  if exists(select 1 from tullist where Left(osa,4)='3030') and exists(select 1 from artlist where Left(art,4)='5500') then
    if a_par=1 then
      -- lisa
      -- tulud
      update tullist set preosa='1' where left(osa,1)='3';
      update tullist set preosa='3' where left(osa,3) in ('381','655') and left(osa,4)<>'3818';
      update tullist set preosa='3' where left(osa,4) in ('3502','1502','1512','1532');
      update tullist set preosa='4' where left(osa,3) in ('208','258');
      update tullist set preosa='5' where left(osa,3)='100';
      -- kokkuvotted
      set ll_count=0;
      select count() into ll_count from tullist where preosa='1';
      if ll_count>0 and not exists(select 1 from tullist where preosa='1' and osa='                ') then
        insert into tullist(preosa,osa,nimi,valitav) values (1,'','Põhitegevuse tulud',0)
      end if;
      set ll_count=0;
      select count() into ll_count from tullist where preosa='3';
      if ll_count>0 and not exists(select 1 from tullist where preosa='3' and osa='                ') then
        insert into tullist(preosa,osa,nimi,valitav) values (3,'','Investeerimine',0)
      end if;
      set ll_count=0;
      select count() into ll_count from tullist where preosa='4';
      if ll_count>0 and not exists(select 1 from tullist where preosa='4' and osa='                ') then
        insert into tullist(preosa,osa,nimi,valitav) values (4,'','Finantseerimine',0)
      end if;
      set ll_count=0;
      select count() into ll_count from tullist where preosa='5';
      if ll_count>0 and not exists(select 1 from tullist where preosa='5' and osa='                ') then
        insert into tullist(preosa,osa,nimi,valitav) values (5,'','Likviidsed varad',0)
      end if;
      -- kulud
      update artlist set preart='2' where left(art,1) in ('4','5','6');
      update artlist set preart='3' where left(art,3) in ('154','155','156','157','650');
      update artlist set preart='3' where left(art,4) in ('4502','1501','1511','1531');
      update artlist set preart='4' where left(art,3) in ('208','258');
      update artlist set preart='5' where left(art,3)='100';
      -- kokkuvotted
      set ll_count=0;
      select count() into ll_count from artlist where preart='2';
      if ll_count>0 and not exists(select 1 from artlist where preart='2' and art='                ') then
        insert into artlist(preart,art,nimi,valitav) values (2,'','Põhitegevuse kulud',0)
      end if;
      set ll_count=0;
      select count() into ll_count from artlist where preart='3';
      if ll_count>0 and not exists(select 1 from artlist where preart='3' and art='                ') then
        insert into artlist(preart,art,nimi,valitav) values (3,'','Investeerimine',0)
      end if;
      set ll_count=0;
      select count() into ll_count from artlist where preart='4';
      if ll_count>0 and not exists(select 1 from artlist where preart='4' and art='                ') then
        insert into artlist(preart,art,nimi,valitav) values (4,'','Finantseerimine',0)
      end if;
      set ll_count=0;
      select count() into ll_count from artlist where preart='5';
      if ll_count>0 and not exists(select 1 from artlist where preart='5' and art='                ') then
        insert into artlist(preart,art,nimi,valitav) values (5,'','Likviidsed varad',0)
      end if;
    else
      if a_par=0 then
        -- kustuta
        update tullist set preosa='';
        update artlist set preart='';
        delete from tullist where Trim(osa)='' and nimi in ('Põhitegevuse tulud','Investeerimine','Finantseerimine','Likviidsed varad');
        delete from artlist where Trim(art)='' and nimi in ('Põhitegevuse kulud','Investeerimine','Finantseerimine','Likviidsed varad');
      end if;
    end if;
  end if;
END