CREATE PROCEDURE "DBA"."dk_koond_saldo_nimek"( in id_skoop integer,in id_obj integer,in peralg date,in perlop date, in a_id_komb integer)
BEGIN
  declare ls_dk_kontod char(7000);
  declare li_spank smallint;
  declare li_param smallint;
  delete from gl_dk_koond;
  delete from gl_pr_koond;
  if id_obj > 0 then
    select dk_obj.skoop,dk_obj.spank into id_skoop,li_spank from dk_obj where dk_obj.objnr = id_obj
  else
    if a_id_komb>0 then
      select dk_teatis_komb.spank into li_spank from dk_teatis_komb where dk_teatis_komb.id=a_id_komb;
    else
      set li_spank=2
    end if
  end if;
  if li_spank=2 then
    set li_param=1
  else
    set li_param=li_spank
  end if;
  -- arveldused
  insert into gl_pr_koond( konto,asutnr,adeeb,deeb)
    select '1',
           dk_kanne.asutnr,
           sum(if dk_kanne.kuup < peralg then if li_param-dk_kanne.sisse=0 then -1*(dk_kanne.summa+dk_kanne.summa2) else dk_kanne.summa+dk_kanne.summa2 endif else 0 endif),
           sum(if dk_kanne.kuup >= peralg then if li_param-dk_kanne.sisse=0 then -1*(dk_kanne.summa+dk_kanne.summa2) else dk_kanne.summa+dk_kanne.summa2 endif else 0 endif)
      from dk_kanne key left outer join dk_obj
      where (dk_obj.spank=li_spank or li_spank=2) and (dk_obj.skoop=id_skoop or id_skoop=0) and (dk_kanne.objnr=id_obj or id_obj=0) and dk_kanne.asutnr>0 and dk_kanne.kuup<=perlop
      group by dk_kanne.asutnr;
  -- kassa/pank
  set ls_dk_kontod=get_dk_kontod_spank(id_skoop,li_spank,id_obj);
  insert into gl_pr_koond( konto,asutnr,akree,kree)
    select '2',
           kp_kanne.asutnr,
           sum(if kp_kanne.kuup < peralg then if li_param-kp_kanne.sisse=0 then -1*kp_kanne.summa else kp_kanne.summa endif else 0 endif),
           sum(if kp_kanne.kuup >= peralg then if li_param-kp_kanne.sisse=0 then -1*kp_kanne.summa else kp_kanne.summa endif else 0 endif)
      from kp_kanne left outer join kp_obj on kp_kanne.objnr = kp_obj.objnr
      where locate(ls_dk_kontod,';'+"Left"(kp_kanne.konto+Space(7),7)+';')>0 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;
  -- koond
  if a_id_komb>0 then
    select koond_regnr into li_param from dk_teatis_komb where id=a_id_komb
  else
    set li_param=0
  end if;
  if li_param=0 then
    insert into gl_dk_koond( asutnr,adeeb,deeb,kree,ldeeb)
      select gl_pr_koond.asutnr,sum(gl_pr_koond.adeeb - gl_pr_koond.akree),sum(gl_pr_koond.deeb),sum(gl_pr_koond.kree),sum(gl_pr_koond.adeeb - gl_pr_koond.akree + gl_pr_koond.deeb - gl_pr_koond.kree) from gl_pr_koond group by gl_pr_koond.asutnr
  else
    insert into gl_dk_koond( reg_nr,asutnr,adeeb,deeb,kree,ldeeb)
      select if asulist.reg_nr is null or Length(Trim(asulist.reg_nr))=0 then cast(asulist.asutnr as char(11)) else asulist.reg_nr endif as asulist_reg_nr,min(gl_pr_koond.asutnr),sum(gl_pr_koond.adeeb - gl_pr_koond.akree),sum(gl_pr_koond.deeb),sum(gl_pr_koond.kree),sum(gl_pr_koond.adeeb - gl_pr_koond.akree + gl_pr_koond.deeb - gl_pr_koond.kree)
        from gl_pr_koond left outer join asulist on gl_pr_koond.asutnr=asulist.asutnr
        group by asulist_reg_nr
  end if;
  -- loppjaagid
  delete from gl_dk_koond where deeb=0 and kree=0 and ldeeb=0
END