ALTER PROCEDURE "dba"."kp_pa_maks_eris"(in a_maks char(12),in a_objnr integer,in a_user_code integer,in a_perlop date,in a_t_akuu numeric(2),in a_t_aasta numeric(4),in a_skoop integer)
begin
  declare li_asutnr integer;
  declare ls_konto char(7);
  declare ls_eris5tulukree char(7);
  declare li_erisartnr integer;
  declare li_eris5tuluartnr integer;
  declare li_panknr integer;
  declare ls_sarve char(30);
  declare ls_viitnr char(35);
  declare ls_erisart char(16);
  declare ls_eris5tuluart char(16);

  delete from gl_kp_kanne;

  select first asulist.asutnr into li_asutnr from asulist where asulist.nimi='Maksu- ja Tolliamet';
  if li_asutnr is null or li_asutnr = 0 then
    set li_asutnr=existid_asulist('','Maksu- ja Tolliamet','')
  end if;

  case a_maks
    when 'eristulumaks' then
      select if Length(Trim(k_ettedeeb))=0 or k_ettedeeb is null then if k_eristulukree is null then '' else Trim(k_eristulukree) endif else k_ettedeeb endif,
             if k_eristuluart is null then '' else Trim(k_eristuluart) endif,
             if Length(Trim(k_ettedeeb))=0 or k_ettedeeb is null then if k_eris5tulukree is null or Trim(k_eris5tulukree)='' then if k_eristulukree is null then '' else Trim(k_eristulukree) endif else Trim(k_eris5tulukree) endif else k_ettedeeb endif,
             if k_eris5tuluart is null or Trim(k_eris5tuluart)='' then if k_eristuluart is null then '' else Trim(k_eristuluart) endif else k_eris5tuluart endif,
             pa_konfm.k_tulusarve
       into ls_konto,
            ls_erisart,
            ls_eris5tulukree,
            ls_eris5tuluart,
            ls_sarve
       from pa_konfm
       where pa_konfm."id"=1;
    when 'erissotsmaks' then
      select if Length(Trim(k_ettedeeb))=0 or k_ettedeeb is null then if k_erissotskree is null then '' else Trim(k_erissotskree) endif else k_ettedeeb endif,
             if k_erissotsart is null then '' else Trim(k_erissotsart) endif,
             pa_konfm.k_tulusarve
       into ls_konto,
            ls_erisart,
            ls_sarve
       from pa_konfm
       where pa_konfm."id"=1;
  end case;

  select first artlist.artnr into li_erisartnr from artlist where Trim(artlist.art)=ls_erisart;
  select first artlist.artnr into li_eris5tuluartnr from artlist where Trim(artlist.art)=ls_eris5tuluart;

  set li_panknr=existid_panklist_sarve(ls_sarve);
  set ls_viitnr=get_tuluviitnr(a_skoop);

  insert into gl_kp_kanne( kuup,summa,sisse,"info",tulnud,user_code,objnr,asutnr,sarve,panknr,viitnr,konto,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr)
    select a_perlop,
           sum(if a_maks='eristulumaks' then pa_erisood.tulumaks else pa_erisood.sotsmaks endif),
           0,
           if a_maks='eristulumaks' then 'Tulumaks erisoodustustelt' else 'Sotsiaalmaks erisoodustustelt' endif as pa_info,
           'Maks:'+a_maks,
           a_user_code,
           a_objnr,
           li_asutnr,
           ls_sarve,
           li_panknr,
           ls_viitnr,
           if lisalist.objnr=40 then ls_konto else ls_eris5tulukree endif as pa_konto,
           if pa_erisood.meetnr is null then pa_yksus.meetnr else pa_erisood.meetnr endif as pa_meetnr,
           if pa_erisood.kul2nr is null then if pa_erisood.kulnr is null then pa_yksus.kulnr else pa_erisood.kulnr endif else pa_erisood.kul2nr endif as pa_kulnr,
           if pa_erisood.talnr is null then pa_yksus.talnr else pa_erisood.talnr endif as pa_talnr,
           if pa_erisood.samart is null or pa_erisood.samart=0 then if lisalist.objnr=40 then li_erisartnr else li_eris5tuluartnr endif else if pa_erisood.artnr is null then pa_yksus.artnr else pa_erisood.artnr endif endif as pa_artnr,
           if pa_erisood.tulnr is null then pa_yksus.tulnr else pa_erisood.tulnr endif as pa_tulnr,
           if pa_erisood.objectnr is null then pa_yksus.objectnr else pa_erisood.objectnr endif as pa_objectnr,
           if pa_erisood.osaknr is null then pa_yksus.osaknr else pa_erisood.osaknr endif as pa_osaknr,
           if pa_erisood.subjectnr is null then pa_yksus.subjectnr else pa_erisood.subjectnr endif as pa_subjectnr,
           if pa_erisood.suundnr is null then pa_yksus.suundnr else pa_erisood.suundnr endif as pa_suundnr,
           if pa_erisood.taotnr is null then pa_yksus.taotnr else pa_erisood.taotnr endif as pa_taotnr,
           pa_erisood.alliknr as pa_alliknr,
           pa_erisood.ravonr as pa_ravonr
      from pa_erisood left outer join pa_yksus on pa_erisood.yksusnr = pa_yksus.yksusnr
                      left outer join lisalist on pa_erisood.lisanr=lisalist.lisanr
      where pa_erisood.t_aasta=a_t_aasta and pa_erisood.t_akuu=a_t_akuu and
            pa_yksus.skoop = a_skoop and
           (pa_yksus.kp_objnr=a_objnr or pa_yksus.kp_maksud=1)
      group by pa_info,pa_konto,pa_meetnr,pa_kulnr,pa_talnr,pa_artnr,pa_tulnr,pa_objectnr,pa_osaknr,pa_subjectnr,pa_suundnr,pa_taotnr,pa_alliknr,pa_ravonr;

  delete from gl_kp_kanne where gl_kp_kanne.summa = 0;
end