ALTER PROCEDURE "dba"."kp_eandmik2014"(in a_skoopkood char(16),in a_skoop integer, in a_perlop date )
BEGIN
  declare ll_rida integer;
  declare ld_peralg date;
  declare ldc_eaajaak numeric(15,2);
  declare ldc_ajaak numeric(15,2);
  declare ldc_ljaak numeric(15,2);
  declare ldc_deeb numeric(15,2);
  declare ldc_kree numeric(15,2);
  declare ldc_summa numeric(15,2);
  declare ls_u_partkood char(6);
  declare ll_omatp integer;
  declare ll_askood integer;
  declare ll_saldoandmik smallint;
  declare ls_sql char(500);
  declare err_notfound exception for sqlstate value '02000';
  declare c_kp_kuuaru2014 dynamic scroll cursor for select "id","sql_rida" from kp_kuuaru2014 where left("sql_rida",11)='insert into';
  delete from gl_ea_tulkond;
  delete from gl_ea_kulkond;
  delete from gl_tulkond;
  delete from gl_kulkond;
  delete from gl_kp_kuuaruanne;
  set ld_peralg=ymd(years(a_perlop),1,1);
  set ls_u_partkood=Left(get_u_partkood(a_skoop),6);
  select tr_strat_omatp,tr_strat_askood,tr_strat_saldoandmik into ll_omatp,ll_askood,ll_saldoandmik from kp_konfm where id=1;
  if ll_omatp=0 then
    set ls_u_partkood='xyzxyz'
  end if;
  -- koond
  -- eelarve
  insert into gl_ea_tulkond( tulnr,col01,tul_kood)
     select ea_kanne.tulnr,sum(ea_kanne.summa),tullist.osa as tul_kood
       from ea_kanne left outer join tullist on ea_kanne.tulnr=tullist.tulnr left outer join ea_obj on ea_kanne.objnr=ea_obj.objnr left outer join skoobid on ea_obj.skoop=skoobid.skoop
       where ((ea_obj.skoop = a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (ea_obj.eaobj = 1)) and (ea_kanne.kuup>=ld_peralg) and ea_kanne.kuup<=a_perlop and ea_kanne.artnr is null and ea_kanne.tulnr is not null
       group by ea_kanne.tulnr,tul_kood;
  insert into gl_ea_kulkond( talnr,artnr,col01,tal_kood,art_kood)
     select if ea_kanne.talnr is null then 0 else ea_kanne.talnr endif,ea_kanne.artnr,sum(ea_kanne.summa),if tallist.askood is null or Trim(tallist.askood)='' then tallist.osa else tallist.askood endif as tal_kood,artlist.art as art_kood
       from ea_kanne left outer join tallist on ea_kanne.talnr=tallist.talnr left outer join artlist on ea_kanne.artnr=artlist.artnr left outer join ea_obj on ea_kanne.objnr=ea_obj.objnr left outer join skoobid on ea_obj.skoop=skoobid.skoop
       where ((ea_obj.skoop = a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (ea_obj.eaobj = 1)) and (ea_kanne.kuup>=ld_peralg) and ea_kanne.kuup<=a_perlop and ea_kanne.artnr is not null 
       group by ea_kanne.talnr,tal_kood,ea_kanne.artnr,art_kood;
  if exists( select 1 from tm_rmpt where id=1 and k_ea_tekkep=1) then
    -- tekkepohine
    insert into gl_ea_tulkond( tulnr,col02,tul_kood)
      select pr_kanne.tulnr,(-1)*sum(pr_kanne.summa),tullist.osa as tul_kood
        from pr_kanne left outer join tullist on pr_kanne.tulnr=tullist.tulnr left outer join kontlist on pr_kanne.deeb = kontlist.konto left outer join skoobid on pr_kanne.skoop=skoobid.skoop
        where (pr_kanne.skoop = a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and kontlist.kulukonto=1 and pr_kanne.artnr is null and pr_kanne.tulnr is not null and pr_kanne.deeb is not null and pr_kanne.kuup >= ld_peralg and pr_kanne.kuup <= a_perlop
        group by pr_kanne.tulnr,tul_kood;
    insert into gl_ea_tulkond( tulnr,col02,tul_kood)
      select pr_kanne.tulnr,sum(pr_kanne.summa),tullist.osa as tul_kood
        from pr_kanne left outer join tullist on pr_kanne.tulnr=tullist.tulnr left outer join kontlist on pr_kanne.kree = kontlist.konto left outer join skoobid on pr_kanne.skoop=skoobid.skoop
        where (pr_kanne.skoop = a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and kontlist.kulukonto=1 and pr_kanne.artnr is null and pr_kanne.tulnr is not null and pr_kanne.kree is not null and pr_kanne.kuup >= ld_peralg and pr_kanne.kuup <= a_perlop
        group by pr_kanne.tulnr,tul_kood;
    insert into gl_ea_kulkond( talnr,artnr,col02,tal_kood,art_kood)
       select if pr_kanne.talnr is null then 0 else pr_kanne.talnr endif,pr_kanne.artnr,sum(pr_kanne.summa),if tallist.askood is null or Trim(tallist.askood)='' then tallist.osa else tallist.askood endif as tal_kood,artlist.art as art_kood
         from pr_kanne left outer join tallist on pr_kanne.talnr=tallist.talnr left outer join artlist on pr_kanne.artnr=artlist.artnr left outer join kontlist on pr_kanne.deeb = kontlist.konto left outer join skoobid on pr_kanne.skoop=skoobid.skoop
         where (pr_kanne.skoop = a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and kontlist.kulukonto=1 and pr_kanne.artnr is not null and pr_kanne.deeb is not null and pr_kanne.kuup >= ld_peralg and pr_kanne.kuup <= a_perlop
       group by pr_kanne.talnr,tal_kood,pr_kanne.artnr,art_kood;
    insert into gl_ea_kulkond( talnr,artnr,col02,tal_kood,art_kood)
       select if pr_kanne.talnr is null then 0 else pr_kanne.talnr endif,pr_kanne.artnr,(-1)*sum(pr_kanne.summa),if tallist.askood is null or Trim(tallist.askood)='' then tallist.osa else tallist.askood endif as tal_kood,artlist.art as art_kood
         from pr_kanne left outer join tallist on pr_kanne.talnr=tallist.talnr left outer join artlist on pr_kanne.artnr=artlist.artnr left outer join kontlist on pr_kanne.kree = kontlist.konto left outer join skoobid on pr_kanne.skoop=skoobid.skoop
         where (pr_kanne.skoop = a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and kontlist.kulukonto=1 and pr_kanne.artnr is not null and pr_kanne.kree is not null and pr_kanne.kuup >= ld_peralg and pr_kanne.kuup <= a_perlop
       group by pr_kanne.talnr,tal_kood,pr_kanne.artnr,art_kood;
  else
    -- kassapohine
    insert into gl_ea_tulkond( tulnr,col02,tul_kood)
       select kp_kanne.tulnr,sum(if kp_kanne.sisse=1 then kp_kanne.summa else -1*kp_kanne.summa endif),tullist.osa as tul_kood
         from kp_kanne left outer join tullist on kp_kanne.tulnr=tullist.tulnr left outer join kp_obj on kp_kanne.objnr=kp_obj.objnr left outer join skoobid on kp_obj.skoop=skoobid.skoop left outer join asulist on kp_kanne.asutnr=asulist.asutnr left outer join partlist on asulist.partnr=partlist.partnr
         where (kp_obj.skoop = a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (kp_obj.eaobj = 1) and(kp_obj.eelarve=0) and(kp_kanne.kuup >= ld_peralg) and(kp_kanne.kuup <=a_perlop) and(kp_kanne.artnr is null) and(kp_kanne.tulnr is not null) and (not(Left(partlist.part,6)=ls_u_partkood) or asulist.partnr is null)
         group by kp_kanne.tulnr,tul_kood;
    insert into gl_ea_kulkond( talnr,artnr,col02,tal_kood,art_kood)
       select if kp_kanne.talnr is null then 0 else kp_kanne.talnr endif,kp_kanne.artnr,sum(if kp_kanne.sisse=0 then kp_kanne.summa else -1*kp_kanne.summa endif),if tallist.askood is null or Trim(tallist.askood)='' then tallist.osa else tallist.askood endif as tal_kood,artlist.art as art_kood
         from kp_kanne left outer join tallist on kp_kanne.talnr=tallist.talnr left outer join artlist on kp_kanne.artnr=artlist.artnr left outer join kp_obj on kp_kanne.objnr=kp_obj.objnr left outer join skoobid on kp_obj.skoop=skoobid.skoop left outer join asulist on kp_kanne.asutnr=asulist.asutnr left outer join partlist on asulist.partnr=partlist.partnr
         where (kp_obj.skoop = a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (kp_obj.eaobj = 1) and(kp_obj.eelarve=0) and(kp_kanne.kuup >= ld_peralg) and(kp_kanne.kuup <=a_perlop) and(kp_kanne.artnr is not null) and (not(Left(partlist.part,6)=ls_u_partkood) or asulist.partnr is null)
       group by kp_kanne.talnr,tal_kood,kp_kanne.artnr,art_kood;
  end if;
  -- nulli tegevusala finants korral
  update gl_ea_kulkond left outer join artlist on gl_ea_kulkond.artnr=artlist.artnr set talnr=0,tal_kood='' where Left(artlist.art,3) in ('208','258');
  -- kokku
  insert into gl_tulkond(tulnr,easum,summa,tul_kood)
     select tulnr,sum(col01),sum(col02),tul_kood from gl_ea_tulkond group by tulnr,tul_kood;
  insert into gl_kulkond(talnr,artnr,easum,summa,tal_kood,art_kood)
     select talnr,artnr,sum(col01),sum(col02),tal_kood,art_kood from gl_ea_kulkond group by talnr,tal_kood,artnr,art_kood;


    insert into gl_kp_kuuaruanne (id,easum,summa) select 3,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3000';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 4,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3030';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 5,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3034';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 6,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3044';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 7,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3045';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 8,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3047';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 9,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,2)='32';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 11,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,5)='35200';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 12,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,5)='35201';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 14,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3500';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 15,sum(easum),sum(summa) from gl_tulkond where (Left(tul_kood,3)='352' and Left(tul_kood,5) not in ('35200','35201'));

    insert into gl_kp_kuuaruanne (id,easum,summa) select 17,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,5)='38250';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 18,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,5)='38251';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 19,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,5)='38252';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 20,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,5)='38254';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 21,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3882';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 22,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3880';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 23,sum(easum),sum(summa) from gl_tulkond where (Left(tul_kood,3)='382' and Left(tul_kood,5) not in ('38250','38251','38252','38254'));
    insert into gl_kp_kuuaruanne (id,easum,summa) select 24,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3818';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 25,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3888';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 28,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,2)='40';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 29,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,3)='413';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 30,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,4)='4500';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 31,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,3)='452';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 33,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,2)='50';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 34,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,2)='55';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 35,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,2)='60';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 38,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,3)='381' and Left(tul_kood,4) not in ('3818');
    insert into gl_kp_kuuaruanne (id,easum,summa) select 39,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,3) in ('154','155','156','157');
    insert into gl_kp_kuuaruanne (id,easum,summa) select 40,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='3502';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 41,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,4)='4502';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 42,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='1502';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 43,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,4)='1501';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 44,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='1512';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 45,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,4)='1511';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 46,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,4)='1532';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 47,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,4)='1531';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 48,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,3)='655';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 49,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,3)='650';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 52,sum(easum),sum(summa) from gl_tulkond where Left(tul_kood,3) in ('208','258');
    insert into gl_kp_kuuaruanne (id,easum,summa) select 53,(-1)*sum(easum),(-1)*sum(summa) from gl_kulkond where Left(art_kood,3) in ('208','258');
    insert into gl_kp_kuuaruanne (id,easum,summa) select 54,(-1)*sum(easum),0 from gl_tulkond where Left(tul_kood,3)='100';

  --if ll_saldoandmik=1 then
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where konto='300000';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=3;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where konto='303000';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=4;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where konto='303400';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=5;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where konto='304400';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=6;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where konto='304500';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=7;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where konto='304700';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=8;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,2)='32';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=9;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,6)='352001';

    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=11;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,6)='352000';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=12;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,4)='3500';

    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=14;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,3)='352' and Left(konto,6) not in ('352000','352001');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=15;

    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where konto='382500';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=17;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where konto='382510';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=18;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where konto='382520';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=19;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where konto='382540';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=20;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,4)='3882';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=21;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,4)='3880';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=22;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,4)='3823' or Left(konto,6)='382550' or Left(konto,6)='382560';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=23;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,4)='3818';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=24;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,4)='3888';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=25;

    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,2)='40';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=28;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,3)='413';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=29;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,4)='4500';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=30;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,3)='452';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=31;
  
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,2)='50';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=33;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,2)='55';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=34;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,2)='60' and Left(konto,6) not in ('601002');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=35;

    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where (Left(konto,3)='381' and Left(konto,4) not in ('3818')) or (Left(konto,3)='154' and ravo='02') or (Left(konto,3)='155' and ravo='02') or (Left(konto,3)='156' and ravo='02') or (Left(konto,3)='157' and ravo='02') or (Left(konto,3)='109' and ravo='02');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=38;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,6)='601002' or (Left(konto,3)='154' and ravo='01') or (Left(konto,3)='155' and ravo='01') or (Left(konto,3)='156' and ravo='01') or (Left(konto,3)='157' and ravo='01');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=39;

    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,4)='3502' and (ravo='01' or ravo='05' or ravo='' or ravo is null);
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=40;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,4)='4502' or (Left(konto,1)='1' and ravo='24');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=41;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,3)='150' and ravo='02';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=42;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,3)='150' and ravo='01';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=43;

    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where (Left(konto,6)='151910' and ravo='02') or (Left(konto,6)='101900' and ravo='02');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=44;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where (Left(konto,6)='151910' and ravo='01') or (Left(konto,6)='101900' and ravo='01');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=45;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where (Left(konto,4)='1032' and ravo='02') or (Left(konto,4)='1532' and ravo='02');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=46;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where (Left(konto,4)='1032' and ravo='01') or (Left(konto,4)='1532' and ravo='01');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=47;

    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where (Left(konto,3)='652' and Left(konto,6) not in ('652000','652030')) or (Left(konto,3)='655' and Left(konto,6) not in ('655400')) or (Left(konto,3)='658' and Left(konto,6) not in ('658950'));
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=48;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where Left(konto,3)='650' or Left(konto,6)='658950';
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=49;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where (Left(konto,3)='208' and ravo='05') or (Left(konto,3)='258' and ravo='05');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=52;
    select Sum(kreedit-deebet) into ldc_summa from gl_pr_saldoandmik where (Left(konto,3)='208' and ravo='06') or (Left(konto,3)='258' and ravo='06');
    update gl_kp_kuuaruanne set prsum=ldc_summa where "id"=53;


  --end if;

  if ll_askood=0 then
    open c_kp_kuuaru2014;
    kp_kuuaru2014_loop: loop
      fetch next c_kp_kuuaru2014 into ll_rida,ls_sql;
      if sqlstate = err_notfound then -- kui eof
        leave kp_kuuaru2014_loop
      end if;
      execute immediate ls_sql;
    end loop kp_kuuaru2014_loop;
    close c_kp_kuuaru2014;
  else
    insert into gl_kp_kuuaruanne (id,easum,summa) select 590,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01110';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 600,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01111';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 610,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01112';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 620,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01114';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 630,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01120';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 640,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01130';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 650,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01210';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 660,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01220';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 670,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01310';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 680,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01320';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 690,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01330';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 700,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01400';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 710,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01500';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 720,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01600';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 730,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01700';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 740,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='01800';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 760,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='02100';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 770,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='02200';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 780,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='02300';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 790,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='02400';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 800,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='02500';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 820,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='03100';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 830,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='03200';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 840,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='03300';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 850,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='03400';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 860,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='03500';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 870,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='03600';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 890,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04110';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 900,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04120';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 910,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04210';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 920,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04220';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 930,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04230';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 940,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04310';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 950,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04320';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 960,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04330';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 970,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04340';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 980,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04350';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 990,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04360';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1000,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04410';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1010,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04420';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1020,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04430';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1030,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04510';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1040,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04512';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1050,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04520';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1060,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04530';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1070,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04540';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1080,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04550';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1090,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04600';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1100,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04710';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1110,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04720';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1120,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04730';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1130,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04740';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1140,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,3)='048';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1150,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='04900';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 1170,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='05100';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1171,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='05101';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1180,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='05200';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1190,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='05300';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1200,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='05400';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1210,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='05500';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1220,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='05600';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 1240,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='06100';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1250,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='06200';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1260,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='06300';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1270,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='06400';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1280,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='06500';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1290,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='06605';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 1310,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,3)='071';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1320,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,3)='072';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1330,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,3)='073';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1340,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='07400';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1350,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='07500';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1360,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='07600';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 1380,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08102';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1390,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08103';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1400,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08105';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1410,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08106';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1420,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08107';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1430,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08108';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1440,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08109';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1450,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08201';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1460,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08202';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1470,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08203';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1510,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08207';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1520,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08208';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1530,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08209';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1540,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08210';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1550,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08211';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1560,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08212';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1561,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08231';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1562,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08232';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1563,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08233';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1564,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08234';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1565,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08235';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1566,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08236';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1570,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08300';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1580,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08400';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1590,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08500';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1600,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='08600';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 1620,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09110';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1630,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5) in ('09210','09211');
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1650,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09212';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1651,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09213';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1660,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09220';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1670,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09221';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1680,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09222';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1681,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09223';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1682,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09300';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1690,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09400';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1700,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09500';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1704,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09510';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1710,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09600';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1720,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09601';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1730,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09602';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1740,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09609';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1750,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09700';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1760,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='09800';

    insert into gl_kp_kuuaruanne (id,easum,summa) select 1780,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10110';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1790,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10120';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1800,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10121';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1803,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10123';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1804,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10124';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1805,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10125';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1806,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10126';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1807,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10127';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1810,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10200';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1820,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10201';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1821,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10202';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1830,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10300';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1840,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10400';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1850,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10402';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1853,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10403';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1854,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10404';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1860,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10500';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1870,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10600';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1880,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10700';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1890,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10701';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1900,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10702';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1904,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10704';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1910,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10800';
    insert into gl_kp_kuuaruanne (id,easum,summa) select 1920,sum(easum),sum(summa) from gl_kulkond where Left(tal_kood,5)='10900';

  end if;
  -- vaba jaak
  -- perioodi alguseks
  select sum(if kp_kanne.sisse=1 then kp_kanne.summa else (-1)*kp_kanne.summa endif) into ldc_ajaak from kp_kanne left outer join kp_obj on kp_kanne.objnr=kp_obj.objnr left outer join skoobid on kp_obj.skoop=skoobid.skoop where (kp_obj.eaobj = 1) and (kp_obj.rahjaak = 0) and (kp_obj.skoop=a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (kp_kanne.kuup<ld_peralg);
  if ldc_ajaak is null then
    set ldc_ajaak=0
  end if;
  -- perioodi lopuks
  select sum(if kp_kanne.sisse=1 then kp_kanne.summa else (-1)*kp_kanne.summa endif) into ldc_ljaak from kp_kanne left outer join kp_obj on kp_kanne.objnr=kp_obj.objnr left outer join skoobid on kp_obj.skoop=skoobid.skoop where (kp_obj.eaobj = 1) and (kp_obj.rahjaak = 0) and (kp_obj.skoop=a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (kp_kanne.kuup<=a_perlop);
  if ldc_ljaak is null then
    set ldc_ljaak=0
  end if;
  insert into gl_kp_kuuaruanne (id,easum,summa) values (1980,ldc_ajaak,ldc_ajaak);
  -- eelarves planeeritud vahe
  select sum(easum)into ldc_deeb from gl_kp_kuuaruanne where id in (54);
  if ldc_deeb is null then
    set ldc_deeb=0
  end if;
  insert into gl_kp_kuuaruanne (id,easum,summa) values (2020,ldc_ajaak+ldc_deeb,ldc_ljaak);
  update gl_kp_kuuaruanne set summa=ldc_ljaak-ldc_ajaak,prsum=ldc_ljaak-ldc_ajaak where id=54;
  -- kohustused
  select sum(pr_kanne.summa) into ldc_deeb from pr_kanne left outer join skoobid on pr_kanne.skoop=skoobid.skoop where (pr_kanne.skoop=a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (pr_kanne.kuup<ld_peralg) and (Left(pr_kanne.deeb,3) in ('208','258'));
  select sum(pr_kanne.summa) into ldc_kree from pr_kanne left outer join skoobid on pr_kanne.skoop=skoobid.skoop where (pr_kanne.skoop=a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (pr_kanne.kuup<ld_peralg) and (Left(pr_kanne.kree,3) in ('208','258'));
  set ldc_eaajaak=if ldc_kree is null then 0 else ldc_kree endif - if ldc_deeb is null then 0 else ldc_deeb endif;
  select sum(pr_kanne.summa) into ldc_deeb from pr_kanne left outer join skoobid on pr_kanne.skoop=skoobid.skoop where (pr_kanne.skoop=a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (pr_kanne.kuup<ld_peralg) and (Left(pr_kanne.deeb,3) in ('208','258','250','256') or Left(pr_kanne.deeb,4) in ('1535','2035','2038','2530','2535','2536') or Left(pr_kanne.deeb,5) in ('20365','20367'));
  select sum(pr_kanne.summa) into ldc_kree from pr_kanne left outer join skoobid on pr_kanne.skoop=skoobid.skoop where (pr_kanne.skoop=a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (pr_kanne.kuup<ld_peralg) and (Left(pr_kanne.kree,3) in ('208','258','250','256') or Left(pr_kanne.kree,4) in ('1535','2035','2038','2530','2535','2536') or Left(pr_kanne.kree,5) in ('20365','20367'));
  set ldc_ajaak=if ldc_kree is null then 0 else ldc_kree endif - if ldc_deeb is null then 0 else ldc_deeb endif;
  select sum(pr_kanne.summa) into ldc_deeb from pr_kanne left outer join skoobid on pr_kanne.skoop=skoobid.skoop where (pr_kanne.skoop=a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (pr_kanne.kuup<=a_perlop) and (Left(pr_kanne.deeb,3) in ('208','258','250','256') or Left(pr_kanne.deeb,4) in ('1535','2035','2038','2530','2535','2536') or Left(pr_kanne.deeb,5) in ('20365','20367'));
  select sum(pr_kanne.summa) into ldc_kree from pr_kanne left outer join skoobid on pr_kanne.skoop=skoobid.skoop where (pr_kanne.skoop=a_skoop or a_skoop = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and (pr_kanne.kuup<=a_perlop) and (Left(pr_kanne.kree,3) in ('208','258','250','256') or Left(pr_kanne.kree,4) in ('1535','2035','2038','2530','2535','2536') or Left(pr_kanne.kree,5) in ('20365','20367'));
  set ldc_ljaak=if ldc_kree is null then 0 else ldc_kree endif - if ldc_deeb is null then 0 else ldc_deeb endif;
  insert into gl_kp_kuuaruanne (id,easum,summa) values (1960,ldc_eaajaak,ldc_ajaak);
  select sum(easum),sum(summa) into ldc_deeb,ldc_kree from gl_kp_kuuaruanne where id in (52,53);
  insert into gl_kp_kuuaruanne (id,easum,summa) values (2000,ldc_eaajaak + if ldc_deeb is null then 0 else ldc_deeb endif,ldc_ljaak);
  -- eemalda nullid
  update gl_kp_kuuaruanne set easum=0 where easum is null;
  update gl_kp_kuuaruanne set prsum=0 where prsum is null;
  update gl_kp_kuuaruanne set summa=0 where summa is null;
END