ALTER PROCEDURE "dba"."pa_gl_pa_tgraaf"( in a_par smallint default 0, in a_lepnr integer default 0)
BEGIN
  delete from gl_pa_tgraaf;
  case a_par
  when 0 then -- koik mitte tool peatamised (kuupalk)
    insert into gl_pa_tgraaf(akuup,lkuup,peatnr)
      select distinct pa_tgraaf.akuup,pa_tgraaf.lkuup,pa_tgraaf.peatnr
        from pa_tgraaf left outer join peatlist on pa_tgraaf.peatnr=peatlist.peatnr
        where pa_tgraaf.lepnr=a_lepnr and peatlist.mtool=1
  when 1 then -- haigus
    insert into gl_pa_tgraaf(akuup,lkuup,peatnr)
      select distinct pa_tgraaf.akuup,pa_tgraaf.lkuup,pa_tgraaf.peatnr
        from pa_tgraaf left outer join peatlist on pa_tgraaf.peatnr=peatlist.peatnr
        where pa_tgraaf.lepnr=a_lepnr and peatlist.haig=1
  when 2 then -- puhkus
    insert into gl_pa_tgraaf(akuup,lkuup,peatnr)
      select distinct pa_tgraaf.akuup,pa_tgraaf.lkuup,pa_tgraaf.peatnr
        from pa_tgraaf left outer join peatlist on pa_tgraaf.peatnr=peatlist.peatnr
        where pa_tgraaf.lepnr=a_lepnr and peatlist.puhk=1
  when 3 then -- mitte tootasuga tasustatavad peatamised (keskmine tootasu)
    insert into gl_pa_tgraaf(akuup,lkuup,peatnr)
      select distinct pa_tgraaf.akuup,pa_tgraaf.lkuup,pa_tgraaf.peatnr
        from pa_tgraaf left outer join peatlist on pa_tgraaf.peatnr=peatlist.peatnr
        where pa_tgraaf.lepnr=a_lepnr and peatlist.kesk=1
  when 200 then -- koik
    insert into gl_pa_tgraaf(akuup,lkuup,peatnr)
      select distinct pa_tgraaf.akuup,pa_tgraaf.lkuup,pa_tgraaf.peatnr
        from pa_tgraaf
        where pa_tgraaf.lepnr=a_lepnr
  end case;
END