ALTER PROCEDURE "DBA"."dk_koond1_simple"(in a_skoopkood char(16), inout id_skoop integer,in id_obj integer,in id_paring integer,in peralg date,in perlop date, in a_valver integer)
begin
  declare li_paring integer;
  declare ls_dk_kontod char(5000);
  delete from gl_dk_koond;
  delete from gl_pr_koond;
  if id_obj > 0 then
    select dk_obj.skoop into id_skoop from dk_obj where dk_obj.objnr = id_obj
  end if;
  -- arveldused
  set li_paring=set_gl_1(id_paring);
    insert into gl_pr_koond( konto,asutnr,adeeb,akree,deeb,kree)
      select '1',dk_kanne.asutnr,sum(if dk_kanne.sisse = 1 and dk_kanne.kuup < peralg then dk_kanne.summa+dk_kanne.summa2 else 0 endif),sum(if dk_kanne.sisse = 0 and dk_kanne.kuup < peralg then dk_kanne.summa+dk_kanne.summa2 else 0 endif),sum(if dk_kanne.sisse = 1 and dk_kanne.kuup >= peralg then dk_kanne.summa+dk_kanne.summa2 else 0 endif),sum(if dk_kanne.sisse = 0 and dk_kanne.kuup >= peralg then dk_kanne.summa+dk_kanne.summa2 else 0 endif) from
      dk_kanne left outer join gl_1 on(dk_kanne.id = gl_1.id) and(dk_kanne.kuup = gl_1.kuup) key left outer join dk_obj left outer join skoobid on dk_obj.skoop=skoobid.skoop where
      ((dk_kanne.id = gl_1.id) and(dk_kanne.kuup = gl_1.kuup) or id_paring = 0) and(dk_kanne.objnr = id_obj or id_obj = 0) and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and(dk_obj.skoop = id_skoop or id_skoop = 0) and dk_kanne.asutnr > 0 and dk_kanne.kuup <= perlop
      group by dk_kanne.asutnr order by dk_kanne.asutnr asc;
  -- kassa/pank
  set ls_dk_kontod=get_dk_kontod_koond(id_skoop,id_obj);
  set li_paring=set_gl_1_conv_dk_kp(id_paring);
    insert into gl_pr_koond( konto,asutnr,adeeb,akree,deeb,kree)
      select '2',kp_kanne.asutnr,sum(if kp_kanne.sisse = 0 and kp_kanne.kuup < peralg then kp_kanne.summa else 0 endif),sum(if kp_kanne.sisse = 1 and kp_kanne.kuup < peralg then kp_kanne.summa else 0 endif),sum(if kp_kanne.sisse = 0 and kp_kanne.kuup >= peralg then kp_kanne.summa else 0 endif),sum(if kp_kanne.sisse = 1 and kp_kanne.kuup >= peralg then kp_kanne.summa else 0 endif) from
      kp_kanne left outer join gl_1 on(kp_kanne.id = gl_1.id) and(kp_kanne.kuup = gl_1.kuup) left outer join kp_obj on kp_kanne.objnr = kp_obj.objnr left outer join skoobid on kp_obj.skoop=skoobid.skoop where
      ((kp_kanne.id = gl_1.id) and(kp_kanne.kuup = gl_1.kuup) or id_paring = 0) and locate(ls_dk_kontod,';'+"Left"(kp_kanne.konto+Space(7),7)+';') > 0 and Left(skoobid.kood,Length(a_skoopkood))=a_skoopkood and(kp_obj.skoop = id_skoop or id_skoop = 0) and kp_kanne.asutnr > 0 and kp_kanne.kuup <= perlop
      group by kp_kanne.asutnr order by kp_kanne.asutnr asc;
  -- koond
  insert into gl_dk_koond( asutnr,adeeb,akree,deeb,kree) 
    select gl_pr_koond.asutnr,sum(gl_pr_koond.adeeb),sum(gl_pr_koond.akree),sum(gl_pr_koond.deeb),sum(gl_pr_koond.kree) from
      gl_pr_koond group by gl_pr_koond.asutnr order by gl_pr_koond.asutnr asc;
  -- loppjaagid
  update gl_dk_koond set adeeb = adeeb-akree,akree = akree-adeeb;
  update gl_dk_koond set adeeb = 0 where gl_dk_koond.adeeb < 0;
  update gl_dk_koond set akree = 0 where gl_dk_koond.akree < 0;
  update gl_dk_koond set ldeeb = adeeb-akree+deeb-kree,lkree = akree-adeeb+kree-deeb;
  update gl_dk_koond set ldeeb = 0 where gl_dk_koond.ldeeb < 0;
  update gl_dk_koond set lkree = 0 where gl_dk_koond.lkree < 0;
  delete from gl_dk_koond where adeeb = 0 and akree = 0 and deeb = 0 and kree = 0
end