ALTER PROCEDURE "DBA"."ea_tr_tabel"(in id_paring integer,in id_obj integer,in id_skoop integer,in peralg date,in perlop date,in a_klassifikaator smallint)
begin
  declare li_paring integer;
  delete from gl_tr_tabel;
  set li_paring=set_gl_1(id_paring);
  case a_klassifikaator
    when 1 then
      insert into "gl_tr_tabel" ("id", "adeeb", "akree", "deeb", "kree")
        select "ea_kanne"."kulnr",sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif)
          from "ea_kanne" left outer join "ea_obj" on "ea_kanne"."objnr" = "ea_obj"."objnr" left outer join "gl_1" on "ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup"
          where (id_paring = 0 or("ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup")) and("ea_kanne"."objnr" = id_obj or id_obj = 0) and("ea_obj"."skoop" = id_skoop or id_skoop = 0) and("ea_kanne"."kuup" <= perlop) and "ea_kanne"."kulnr" > 0
          group by "ea_kanne"."kulnr"
          order by "ea_kanne"."kulnr";
      update "gl_tr_tabel" left outer join "kullist" on "gl_tr_tabel"."id"="kullist"."kulnr" set "gl_tr_tabel"."kood"="kullist"."ptk", "gl_tr_tabel"."kell"="kullist"."kell", "gl_tr_tabel"."nimi"="kullist"."nimi"
    when 2 then
      insert into "gl_tr_tabel" ("id","adeeb", "akree", "deeb", "kree")
        select "ea_kanne"."talnr",sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif)
          from "ea_kanne" left outer join "ea_obj" on "ea_kanne"."objnr" = "ea_obj"."objnr" left outer join "gl_1" on "ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup"
          where (id_paring = 0 or("ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup")) and("ea_kanne"."objnr" = id_obj or id_obj = 0) and("ea_obj"."skoop" = id_skoop or id_skoop = 0) and("ea_kanne"."kuup" <= perlop) and "ea_kanne"."talnr" > 0
          group by "ea_kanne"."talnr"
          order by "ea_kanne"."talnr";
      update "gl_tr_tabel" left outer join "tallist" on "gl_tr_tabel"."id"="tallist"."talnr" set "gl_tr_tabel"."kood"="tallist"."osa", "gl_tr_tabel"."kell"="tallist"."kell", "gl_tr_tabel"."nimi"="tallist"."nimi"
    when 3 then
      insert into "gl_tr_tabel" ("id", "akree", "kree")
        select "ea_kanne"."artnr",sum(if "ea_kanne"."kuup"<peralg then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg then "ea_kanne"."summa" else 0 endif)
          from "ea_kanne" left outer join "ea_obj" on "ea_kanne"."objnr" = "ea_obj"."objnr" left outer join "gl_1" on "ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup"
          where (id_paring = 0 or("ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup")) and("ea_kanne"."objnr" = id_obj or id_obj = 0) and("ea_obj"."skoop" = id_skoop or id_skoop = 0) and("ea_kanne"."kuup" <= perlop) and "ea_kanne"."artnr" > 0
          group by "ea_kanne"."artnr"
          order by "ea_kanne"."artnr";
      update "gl_tr_tabel" left outer join "artlist" on "gl_tr_tabel"."id"="artlist"."artnr" set "gl_tr_tabel"."kood"="artlist"."art", "gl_tr_tabel"."kell"="artlist"."kell", "gl_tr_tabel"."nimi"="artlist"."nimi"
    when 4 then
      insert into "gl_tr_tabel" ("id", "adeeb", "deeb")
        select "ea_kanne"."tulnr",sum(if "ea_kanne"."kuup"<peralg then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg then "ea_kanne"."summa" else 0 endif)
          from "ea_kanne" left outer join "ea_obj" on "ea_kanne"."objnr" = "ea_obj"."objnr" left outer join "gl_1" on "ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup"
          where (id_paring = 0 or("ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup")) and("ea_kanne"."objnr" = id_obj or id_obj = 0) and("ea_obj"."skoop" = id_skoop or id_skoop = 0) and("ea_kanne"."kuup" <= perlop) and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" > 0
          group by "ea_kanne"."tulnr"
          order by "ea_kanne"."tulnr";
      update "gl_tr_tabel" left outer join "tullist" on "gl_tr_tabel"."id"="tullist"."tulnr" set "gl_tr_tabel"."kood"="tullist"."osa", "gl_tr_tabel"."kell"="tullist"."kell", "gl_tr_tabel"."nimi"="tullist"."nimi"
    when 5 then
      insert into "gl_tr_tabel" ("id", "adeeb", "akree", "deeb", "kree")
        select "ea_kanne"."objectnr",sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif)
          from "ea_kanne" left outer join "ea_obj" on "ea_kanne"."objnr" = "ea_obj"."objnr" left outer join "gl_1" on "ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup"
          where (id_paring = 0 or("ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup")) and("ea_kanne"."objnr" = id_obj or id_obj = 0) and("ea_obj"."skoop" = id_skoop or id_skoop = 0) and("ea_kanne"."kuup" <= perlop) and "ea_kanne"."objectnr" > 0
          group by "ea_kanne"."objectnr"
          order by "ea_kanne"."objectnr";
      update "gl_tr_tabel" left outer join "objlist" on "gl_tr_tabel"."id"="objlist"."objectnr" set "gl_tr_tabel"."kood"="objlist"."object", "gl_tr_tabel"."kell"="objlist"."kell", "gl_tr_tabel"."nimi"="objlist"."nimi"
    when 6 then
      insert into "gl_tr_tabel" ("id", "adeeb", "akree", "deeb", "kree")
        select "ea_kanne"."osaknr",sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif)
          from "ea_kanne" left outer join "ea_obj" on "ea_kanne"."objnr" = "ea_obj"."objnr" left outer join "gl_1" on "ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup"
          where (id_paring = 0 or("ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup")) and("ea_kanne"."objnr" = id_obj or id_obj = 0) and("ea_obj"."skoop" = id_skoop or id_skoop = 0) and("ea_kanne"."kuup" <= perlop) and "ea_kanne"."osaknr" > 0
          group by "ea_kanne"."osaknr"
          order by "ea_kanne"."osaknr";
      update "gl_tr_tabel" left outer join "osaklist" on "gl_tr_tabel"."id"="osaklist"."osaknr" set "gl_tr_tabel"."kood"="osaklist"."osak", "gl_tr_tabel"."kell"="osaklist"."kell", "gl_tr_tabel"."nimi"="osaklist"."nimi"
    when 7 then
      insert into "gl_tr_tabel" ("id", "adeeb", "akree", "deeb", "kree")
        select "ea_kanne"."subjectnr",sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif)
          from "ea_kanne" left outer join "ea_obj" on "ea_kanne"."objnr" = "ea_obj"."objnr" left outer join "gl_1" on "ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup"
          where (id_paring = 0 or("ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup")) and("ea_kanne"."objnr" = id_obj or id_obj = 0) and("ea_obj"."skoop" = id_skoop or id_skoop = 0) and("ea_kanne"."kuup" <= perlop) and "ea_kanne"."subjectnr" > 0
          group by "ea_kanne"."subjectnr"
          order by "ea_kanne"."subjectnr";
      update "gl_tr_tabel" left outer join "sublist" on "gl_tr_tabel"."id"="sublist"."subjectnr" set "gl_tr_tabel"."kood"="sublist"."subject", "gl_tr_tabel"."kell"="sublist"."kell", "gl_tr_tabel"."nimi"="sublist"."nimi"
    when 8 then
      insert into "gl_tr_tabel" ("id", "adeeb", "akree", "deeb", "kree")
        select "ea_kanne"."suundnr",sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup"<peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is null and "ea_kanne"."tulnr" is not null then "ea_kanne"."summa" else 0 endif),sum(if "ea_kanne"."kuup">=peralg and "ea_kanne"."artnr" is not null then "ea_kanne"."summa" else 0 endif)
          from "ea_kanne" left outer join "ea_obj" on "ea_kanne"."objnr" = "ea_obj"."objnr" left outer join "gl_1" on "ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup"
          where (id_paring = 0 or("ea_kanne"."id" = "gl_1"."id" and "ea_kanne"."kuup" = "gl_1"."kuup")) and("ea_kanne"."objnr" = id_obj or id_obj = 0) and("ea_obj"."skoop" = id_skoop or id_skoop = 0) and("ea_kanne"."kuup" <= perlop) and "ea_kanne"."suundnr" > 0
          group by "ea_kanne"."suundnr"
          order by "ea_kanne"."suundnr";
      update "gl_tr_tabel" left outer join "suundlist" on "gl_tr_tabel"."id"="suundlist"."suundnr" set "gl_tr_tabel"."kood"="suundlist"."suund", "gl_tr_tabel"."kell"="suundlist"."kell", "gl_tr_tabel"."nimi"="suundlist"."nimi"
  end case;
  update gl_tr_tabel set adeeb = adeeb-akree,akree = akree-adeeb;
  update gl_tr_tabel set adeeb = 0 where gl_tr_tabel.adeeb < 0;
  update gl_tr_tabel set akree = 0 where gl_tr_tabel.akree < 0;
  update gl_tr_tabel set ldeeb = adeeb-akree+deeb-kree,lkree = akree-adeeb+kree-deeb;
  update gl_tr_tabel set ldeeb = 0 where gl_tr_tabel.ldeeb < 0;
  update gl_tr_tabel set lkree = 0 where gl_tr_tabel.lkree < 0;
  update gl_tr_tabel set sdeeb = deeb-kree,skree = kree-deeb;
  update gl_tr_tabel set sdeeb = 0 where gl_tr_tabel.sdeeb < 0;
  update gl_tr_tabel set skree = 0 where gl_tr_tabel.skree < 0;
  delete from gl_tr_tabel where adeeb = 0 and akree = 0 and deeb = 0 and kree = 0
end