ALTER PROCEDURE "DBA"."pr_pa_kan_ettemaks"( in a_skoop integer, in a_perlop date, in a_v_aasta numeric(4), in a_v_akuu numeric(2) )
BEGIN
  declare li_t_aasta numeric(4);
  declare li_t_akuu numeric(2);
  declare ls_order char(3);
  declare ls_erisorder char(3);
  declare ls_kree char(7);
  declare ls_deeb char(7);
  declare ls_k_tulukree char(7);
  declare ls_k_tvtkkree char(7);
  declare ls_k_penskree char(7);
  declare ls_k_sotskree char(7);
  declare ls_k_tatkkree char(7);
  declare ls_k_eristulukree char(7);
  declare ls_k_eris5tulukree char(7);
  declare ls_k_erissotskree char(7);
  set li_t_akuu=a_v_akuu-1;
  set li_t_aasta=a_v_aasta;
  if li_t_akuu=0 then
    set li_t_akuu=12;
    set li_t_aasta=li_t_aasta-1
  end if;
  select pa_konfm.yld_palk_mem,pa_konfm.yld_eris_mem,
        if (k_ettedeeb is null or k_ettedeeb_dk=1) then '' else Trim(k_ettedeeb) endif,
        if k_tulukree is null then '' else Trim(k_tulukree) endif,
        if k_tvtkkree is null then '' else Trim(k_tvtkkree) endif,
        if k_penskree is null then '' else Trim(k_penskree) endif,
        if k_sotskree is null then '' else Trim(k_sotskree) endif,
        if k_tatkkree is null then '' else Trim(k_tatkkree) endif,
        if k_eristulukree is null then '' else Trim(k_eristulukree) endif,
        if k_eris5tulukree is null or Length(Trim(k_eris5tulukree))=0 then if k_eristulukree is null then '' else Trim(k_eristulukree) endif else Trim(k_eris5tulukree) endif,
        if k_erissotskree is null then '' else Trim(k_erissotskree) endif
     into ls_order,ls_erisorder,
          ls_kree,
          ls_k_tulukree,
          ls_k_tvtkkree,
          ls_k_penskree,
          ls_k_sotskree,
          ls_k_tatkkree,
          ls_k_eristulukree,
          ls_k_eris5tulukree,
          ls_k_erissotskree
     from pa_konfm where pa_konfm."id"=1;
  if Length(ls_kree)>0 and ((li_t_aasta>2008) or (li_t_aasta=2008 and li_t_akuu=12)) then
    if Length(ls_k_tulukree)>0 then
      set ls_deeb=ls_k_tulukree;
      insert into gl_pr_kanne_import( tulnud,skoop,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,objectnr,osaknr,subjectnr,suundnr,taotnr,"info")
        select 'Palk',pa_yksus.skoop,a_perlop,sum(if pa_taslist_a.sisse = 0 then pa_tasu.tulumaks else (-1)*pa_tasu.tulumaks endif),ls_order,ls_deeb,ls_kree,
              (if pa_tasu.meetnr is null then (if pa_paryld.meetnr is null then (if pa_taslist_a.meetnr is null then (if pa_taslist_b.meetnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif) else pa_taslist_b.meetnr endif) else pa_taslist_a.meetnr endif) else pa_paryld.meetnr endif) else pa_tasu.meetnr endif) as pa_meetnr,
              (if pa_tasu.kulnr is null then (if pa_paryld.kulnr is null then (if pa_taslist_a.kulnr is null then (if pa_taslist_b.kulnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif) else pa_taslist_b.kulnr endif) else pa_taslist_a.kulnr endif) else pa_paryld.kulnr endif) else pa_tasu.kulnr endif) as pa_kulnr,
              (if pa_tasu.talnr is null then (if pa_paryld.talnr is null then (if pa_taslist_a.talnr is null then (if pa_taslist_b.talnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif) else pa_taslist_b.talnr endif) else pa_taslist_a.talnr endif) else pa_paryld.talnr endif) else pa_tasu.talnr endif) as pa_talnr,
              (if pa_tasu.objectnr is null then (if pa_paryld.objectnr is null then (if pa_taslist_a.objectnr is null then (if pa_taslist_b.objectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_taslist_b.objectnr endif) else pa_taslist_a.objectnr endif) else pa_paryld.objectnr endif) else pa_tasu.objectnr endif) as pa_objectnr,
              (if pa_tasu.osaknr is null then (if pa_paryld.osaknr is null then (if pa_taslist_a.osaknr is null then (if pa_taslist_b.osaknr is null or pa_taslist_b.samkul=0 then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_taslist_b.osaknr endif) else pa_taslist_a.osaknr endif) else pa_paryld.osaknr endif) else pa_tasu.osaknr endif) as pa_osaknr,
              (if pa_tasu.subjectnr is null then (if pa_paryld.subjectnr is null then (if pa_taslist_a.subjectnr is null then (if pa_taslist_b.subjectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_taslist_b.subjectnr endif) else pa_taslist_a.subjectnr endif) else pa_paryld.subjectnr endif) else pa_tasu.subjectnr endif) as pa_subjectnr,
              (if pa_tasu.suundnr is null then (if pa_paryld.suundnr is null then (if pa_taslist_a.suundnr is null then (if pa_taslist_b.suundnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_taslist_b.suundnr endif) else pa_taslist_a.suundnr endif) else pa_paryld.suundnr endif) else pa_tasu.suundnr endif) as pa_suundnr,
              (if pa_tasu.taotnr is null then (if pa_paryld.taotnr is null then (if pa_taslist_a.taotnr is null then (if pa_taslist_b.taotnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif) else pa_taslist_b.taotnr endif) else pa_taslist_a.taotnr endif) else pa_paryld.taotnr endif) else pa_tasu.taotnr endif) as pa_taotnr,
              'Tulumaks'
        from pa_tasu left outer join pa_taslist as pa_taslist_a on pa_tasu.tas_s = pa_taslist_a.tas_s left outer join pa_taslist as pa_taslist_b on pa_tasu.vtas_s = pa_taslist_b.tas_s left outer join pa_yksus on pa_tasu.yksusnr = pa_yksus.yksusnr left outer join pa_paryld on pa_tasu.vparyldnr=pa_paryld.paryldnr
        where (pa_yksus.skoop = a_skoop or a_skoop = 0) and pa_tasu.palgaleht is not null and pa_tasu.t_aasta = li_t_aasta and pa_tasu.t_akuu = li_t_akuu
        group by pa_yksus.skoop,pa_meetnr,pa_kulnr,pa_talnr,pa_objectnr,pa_osaknr,pa_subjectnr,pa_suundnr,pa_taotnr;
      insert into gl_pr_kanne_import( tulnud,skoop,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,objectnr,osaknr,subjectnr,suundnr,taotnr,"info")
        select 'Palk',pa_yksus.skoop,a_perlop,sum(pa_tasu.par1),ls_order,ls_deeb,ls_kree,
              (if pa_tasu.meetnr is null then (if pa_paryld.meetnr is null then (if pa_taslist_a.meetnr is null then (if pa_taslist_b.meetnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif) else pa_taslist_b.meetnr endif) else pa_taslist_a.meetnr endif) else pa_paryld.meetnr endif) else pa_tasu.meetnr endif) as pa_meetnr,
              (if pa_tasu.kulnr is null then (if pa_paryld.kulnr is null then (if pa_taslist_a.kulnr is null then (if pa_taslist_b.kulnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif) else pa_taslist_b.kulnr endif) else pa_taslist_a.kulnr endif) else pa_paryld.kulnr endif) else pa_tasu.kulnr endif) as pa_kulnr,
              (if pa_tasu.talnr is null then (if pa_paryld.talnr is null then (if pa_taslist_a.talnr is null then (if pa_taslist_b.talnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif) else pa_taslist_b.talnr endif) else pa_taslist_a.talnr endif) else pa_paryld.talnr endif) else pa_tasu.talnr endif) as pa_talnr,
              (if pa_tasu.objectnr is null then (if pa_paryld.objectnr is null then (if pa_taslist_a.objectnr is null then (if pa_taslist_b.objectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_taslist_b.objectnr endif) else pa_taslist_a.objectnr endif) else pa_paryld.objectnr endif) else pa_tasu.objectnr endif) as pa_objectnr,
              (if pa_tasu.osaknr is null then (if pa_paryld.osaknr is null then (if pa_taslist_a.osaknr is null then (if pa_taslist_b.osaknr is null or pa_taslist_b.samkul=0 then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_taslist_b.osaknr endif) else pa_taslist_a.osaknr endif) else pa_paryld.osaknr endif) else pa_tasu.osaknr endif) as pa_osaknr,
              (if pa_tasu.subjectnr is null then (if pa_paryld.subjectnr is null then (if pa_taslist_a.subjectnr is null then (if pa_taslist_b.subjectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_taslist_b.subjectnr endif) else pa_taslist_a.subjectnr endif) else pa_paryld.subjectnr endif) else pa_tasu.subjectnr endif) as pa_subjectnr,
              (if pa_tasu.suundnr is null then (if pa_paryld.suundnr is null then (if pa_taslist_a.suundnr is null then (if pa_taslist_b.suundnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_taslist_b.suundnr endif) else pa_taslist_a.suundnr endif) else pa_paryld.suundnr endif) else pa_tasu.suundnr endif) as pa_suundnr,
              (if pa_tasu.taotnr is null then (if pa_paryld.taotnr is null then (if pa_taslist_a.taotnr is null then (if pa_taslist_b.taotnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif) else pa_taslist_b.taotnr endif) else pa_taslist_a.taotnr endif) else pa_paryld.taotnr endif) else pa_tasu.taotnr endif) as pa_taotnr,
               'Tulumaks alimentidelt'
        from pa_tasu left outer join pa_taslist as pa_taslist_a on pa_tasu.tas_s = pa_taslist_a.tas_s left outer join pa_taslist as pa_taslist_b on pa_tasu.vtas_s = pa_taslist_b.tas_s left outer join pa_yksus on pa_tasu.yksusnr = pa_yksus.yksusnr left outer join pa_paryld on pa_tasu.vparyldnr=pa_paryld.paryldnr
        where (pa_yksus.skoop = a_skoop or a_skoop = 0) and pa_tasu.palgaleht is not null and pa_taslist_a.algol='6a' and pa_tasu.t_aasta = li_t_aasta and pa_tasu.t_akuu = li_t_akuu and pa_tasu.par1 is not null
        group by pa_yksus.skoop,pa_meetnr,pa_kulnr,pa_talnr,pa_objectnr,pa_osaknr,pa_subjectnr,pa_suundnr,pa_taotnr;
    end if;
    if Length(ls_k_tvtkkree)>0 then
      set ls_deeb=ls_k_tvtkkree;
      insert into gl_pr_kanne_import( tulnud,skoop,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,objectnr,osaknr,subjectnr,suundnr,taotnr,"info")
        select 'Palk',pa_yksus.skoop,a_perlop,sum(if pa_taslist_a.sisse = 0 then pa_tasu.tvtkmaks else (-1)*pa_tasu.tvtkmaks endif),ls_order,ls_deeb,ls_kree,
              (if pa_tasu.meetnr is null then (if pa_paryld.meetnr is null then (if pa_taslist_a.meetnr is null then (if pa_taslist_b.meetnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif) else pa_taslist_b.meetnr endif) else pa_taslist_a.meetnr endif) else pa_paryld.meetnr endif) else pa_tasu.meetnr endif) as pa_meetnr,
              (if pa_tasu.kulnr is null then (if pa_paryld.kulnr is null then (if pa_taslist_a.kulnr is null then (if pa_taslist_b.kulnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif) else pa_taslist_b.kulnr endif) else pa_taslist_a.kulnr endif) else pa_paryld.kulnr endif) else pa_tasu.kulnr endif) as pa_kulnr,
              (if pa_tasu.talnr is null then (if pa_paryld.talnr is null then (if pa_taslist_a.talnr is null then (if pa_taslist_b.talnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif) else pa_taslist_b.talnr endif) else pa_taslist_a.talnr endif) else pa_paryld.talnr endif) else pa_tasu.talnr endif) as pa_talnr,
              (if pa_tasu.objectnr is null then (if pa_paryld.objectnr is null then (if pa_taslist_a.objectnr is null then (if pa_taslist_b.objectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_taslist_b.objectnr endif) else pa_taslist_a.objectnr endif) else pa_paryld.objectnr endif) else pa_tasu.objectnr endif) as pa_objectnr,
              (if pa_tasu.osaknr is null then (if pa_paryld.osaknr is null then (if pa_taslist_a.osaknr is null then (if pa_taslist_b.osaknr is null or pa_taslist_b.samkul=0 then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_taslist_b.osaknr endif) else pa_taslist_a.osaknr endif) else pa_paryld.osaknr endif) else pa_tasu.osaknr endif) as pa_osaknr,
              (if pa_tasu.subjectnr is null then (if pa_paryld.subjectnr is null then (if pa_taslist_a.subjectnr is null then (if pa_taslist_b.subjectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_taslist_b.subjectnr endif) else pa_taslist_a.subjectnr endif) else pa_paryld.subjectnr endif) else pa_tasu.subjectnr endif) as pa_subjectnr,
              (if pa_tasu.suundnr is null then (if pa_paryld.suundnr is null then (if pa_taslist_a.suundnr is null then (if pa_taslist_b.suundnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_taslist_b.suundnr endif) else pa_taslist_a.suundnr endif) else pa_paryld.suundnr endif) else pa_tasu.suundnr endif) as pa_suundnr,
              (if pa_tasu.taotnr is null then (if pa_paryld.taotnr is null then (if pa_taslist_a.taotnr is null then (if pa_taslist_b.taotnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif) else pa_taslist_b.taotnr endif) else pa_taslist_a.taotnr endif) else pa_paryld.taotnr endif) else pa_tasu.taotnr endif) as pa_taotnr,
               'Töövõtja töötuskindlustus'
        from pa_tasu left outer join pa_taslist as pa_taslist_a on pa_tasu.tas_s = pa_taslist_a.tas_s left outer join pa_taslist as pa_taslist_b on pa_tasu.vtas_s = pa_taslist_b.tas_s left outer join pa_yksus on pa_tasu.yksusnr = pa_yksus.yksusnr left outer join pa_paryld on pa_tasu.vparyldnr=pa_paryld.paryldnr
        where (pa_yksus.skoop = a_skoop or a_skoop = 0) and pa_tasu.palgaleht is not null and pa_tasu.t_aasta = li_t_aasta and pa_tasu.t_akuu = li_t_akuu
        group by pa_yksus.skoop,pa_meetnr,pa_kulnr,pa_talnr,pa_objectnr,pa_osaknr,pa_subjectnr,pa_suundnr,pa_taotnr;
    end if;
    if Length(ls_k_penskree)>0 then
      set ls_deeb=ls_k_penskree;
      insert into gl_pr_kanne_import( tulnud,skoop,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,objectnr,osaknr,subjectnr,suundnr,taotnr,"info")
        select 'Palk',pa_yksus.skoop,a_perlop,sum(if pa_taslist_a.sisse = 0 then pa_tasu.pensmaks else (-1)*pa_tasu.pensmaks endif),ls_order,ls_deeb,ls_kree,
              (if pa_tasu.meetnr is null then (if pa_paryld.meetnr is null then (if pa_taslist_a.meetnr is null then (if pa_taslist_b.meetnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif) else pa_taslist_b.meetnr endif) else pa_taslist_a.meetnr endif) else pa_paryld.meetnr endif) else pa_tasu.meetnr endif) as pa_meetnr,
              (if pa_tasu.kulnr is null then (if pa_paryld.kulnr is null then (if pa_taslist_a.kulnr is null then (if pa_taslist_b.kulnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif) else pa_taslist_b.kulnr endif) else pa_taslist_a.kulnr endif) else pa_paryld.kulnr endif) else pa_tasu.kulnr endif) as pa_kulnr,
              (if pa_tasu.talnr is null then (if pa_paryld.talnr is null then (if pa_taslist_a.talnr is null then (if pa_taslist_b.talnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif) else pa_taslist_b.talnr endif) else pa_taslist_a.talnr endif) else pa_paryld.talnr endif) else pa_tasu.talnr endif) as pa_talnr,
              (if pa_tasu.objectnr is null then (if pa_paryld.objectnr is null then (if pa_taslist_a.objectnr is null then (if pa_taslist_b.objectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_taslist_b.objectnr endif) else pa_taslist_a.objectnr endif) else pa_paryld.objectnr endif) else pa_tasu.objectnr endif) as pa_objectnr,
              (if pa_tasu.osaknr is null then (if pa_paryld.osaknr is null then (if pa_taslist_a.osaknr is null then (if pa_taslist_b.osaknr is null or pa_taslist_b.samkul=0 then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_taslist_b.osaknr endif) else pa_taslist_a.osaknr endif) else pa_paryld.osaknr endif) else pa_tasu.osaknr endif) as pa_osaknr,
              (if pa_tasu.subjectnr is null then (if pa_paryld.subjectnr is null then (if pa_taslist_a.subjectnr is null then (if pa_taslist_b.subjectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_taslist_b.subjectnr endif) else pa_taslist_a.subjectnr endif) else pa_paryld.subjectnr endif) else pa_tasu.subjectnr endif) as pa_subjectnr,
              (if pa_tasu.suundnr is null then (if pa_paryld.suundnr is null then (if pa_taslist_a.suundnr is null then (if pa_taslist_b.suundnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_taslist_b.suundnr endif) else pa_taslist_a.suundnr endif) else pa_paryld.suundnr endif) else pa_tasu.suundnr endif) as pa_suundnr,
              (if pa_tasu.taotnr is null then (if pa_paryld.taotnr is null then (if pa_taslist_a.taotnr is null then (if pa_taslist_b.taotnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif) else pa_taslist_b.taotnr endif) else pa_taslist_a.taotnr endif) else pa_paryld.taotnr endif) else pa_tasu.taotnr endif) as pa_taotnr,
               'Pensionikindlustus'
        from pa_tasu left outer join pa_taslist as pa_taslist_a on pa_tasu.tas_s = pa_taslist_a.tas_s left outer join pa_taslist as pa_taslist_b on pa_tasu.vtas_s = pa_taslist_b.tas_s left outer join pa_yksus on pa_tasu.yksusnr = pa_yksus.yksusnr left outer join pa_paryld on pa_tasu.vparyldnr=pa_paryld.paryldnr
        where (pa_yksus.skoop = a_skoop or a_skoop = 0) and pa_tasu.palgaleht is not null and pa_tasu.t_aasta = li_t_aasta and pa_tasu.t_akuu = li_t_akuu
        group by pa_yksus.skoop,pa_meetnr,pa_kulnr,pa_talnr,pa_objectnr,pa_osaknr,pa_subjectnr,pa_suundnr,pa_taotnr;
    end if;
    if Length(ls_k_sotskree)>0 then
      set ls_deeb=ls_k_sotskree;
      insert into gl_pr_kanne_import( tulnud,skoop,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,objectnr,osaknr,subjectnr,suundnr,taotnr,"info")
        select 'Palk',pa_yksus.skoop,a_perlop,sum(if pa_taslist_a.sisse = 0 then pa_tasu.sotsmaks else (-1)*pa_tasu.sotsmaks endif),ls_order,ls_deeb,ls_kree,
              (if pa_tasu.meetnr is null then (if pa_paryld.meetnr is null then (if pa_taslist_a.meetnr is null then (if pa_taslist_b.meetnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif) else pa_taslist_b.meetnr endif) else pa_taslist_a.meetnr endif) else pa_paryld.meetnr endif) else pa_tasu.meetnr endif) as pa_meetnr,
              (if pa_tasu.kulnr is null then (if pa_paryld.kulnr is null then (if pa_taslist_a.kulnr is null then (if pa_taslist_b.kulnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif) else pa_taslist_b.kulnr endif) else pa_taslist_a.kulnr endif) else pa_paryld.kulnr endif) else pa_tasu.kulnr endif) as pa_kulnr,
              (if pa_tasu.talnr is null then (if pa_paryld.talnr is null then (if pa_taslist_a.talnr is null then (if pa_taslist_b.talnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif) else pa_taslist_b.talnr endif) else pa_taslist_a.talnr endif) else pa_paryld.talnr endif) else pa_tasu.talnr endif) as pa_talnr,
              (if pa_tasu.objectnr is null then (if pa_paryld.objectnr is null then (if pa_taslist_a.objectnr is null then (if pa_taslist_b.objectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_taslist_b.objectnr endif) else pa_taslist_a.objectnr endif) else pa_paryld.objectnr endif) else pa_tasu.objectnr endif) as pa_objectnr,
              (if pa_tasu.osaknr is null then (if pa_paryld.osaknr is null then (if pa_taslist_a.osaknr is null then (if pa_taslist_b.osaknr is null or pa_taslist_b.samkul=0 then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_taslist_b.osaknr endif) else pa_taslist_a.osaknr endif) else pa_paryld.osaknr endif) else pa_tasu.osaknr endif) as pa_osaknr,
              (if pa_tasu.subjectnr is null then (if pa_paryld.subjectnr is null then (if pa_taslist_a.subjectnr is null then (if pa_taslist_b.subjectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_taslist_b.subjectnr endif) else pa_taslist_a.subjectnr endif) else pa_paryld.subjectnr endif) else pa_tasu.subjectnr endif) as pa_subjectnr,
              (if pa_tasu.suundnr is null then (if pa_paryld.suundnr is null then (if pa_taslist_a.suundnr is null then (if pa_taslist_b.suundnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_taslist_b.suundnr endif) else pa_taslist_a.suundnr endif) else pa_paryld.suundnr endif) else pa_tasu.suundnr endif) as pa_suundnr,
              (if pa_tasu.taotnr is null then (if pa_paryld.taotnr is null then (if pa_taslist_a.taotnr is null then (if pa_taslist_b.taotnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif) else pa_taslist_b.taotnr endif) else pa_taslist_a.taotnr endif) else pa_paryld.taotnr endif) else pa_tasu.taotnr endif) as pa_taotnr,
               'Sotsiaalmaksmaks'
        from pa_tasu left outer join pa_taslist as pa_taslist_a on pa_tasu.tas_s = pa_taslist_a.tas_s left outer join pa_taslist as pa_taslist_b on pa_tasu.vtas_s = pa_taslist_b.tas_s left outer join pa_yksus on pa_tasu.yksusnr = pa_yksus.yksusnr left outer join pa_paryld on pa_tasu.vparyldnr=pa_paryld.paryldnr
        where (pa_yksus.skoop = a_skoop or a_skoop = 0) and pa_tasu.palgaleht is not null and pa_tasu.t_aasta = li_t_aasta and pa_tasu.t_akuu = li_t_akuu
        group by pa_yksus.skoop,pa_meetnr,pa_kulnr,pa_talnr,pa_objectnr,pa_osaknr,pa_subjectnr,pa_suundnr,pa_taotnr;
    end if;
    if Length(ls_k_tatkkree)>0 then
      set ls_deeb=ls_k_tatkkree;
      insert into gl_pr_kanne_import( tulnud,skoop,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,objectnr,osaknr,subjectnr,suundnr,taotnr,"info")
        select 'Palk',pa_yksus.skoop,a_perlop,sum(if pa_taslist_a.sisse = 0 then pa_tasu.tatkmaks else (-1)*pa_tasu.tatkmaks endif),ls_order,ls_deeb,ls_kree,
              (if pa_tasu.meetnr is null then (if pa_paryld.meetnr is null then (if pa_taslist_a.meetnr is null then (if pa_taslist_b.meetnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif) else pa_taslist_b.meetnr endif) else pa_taslist_a.meetnr endif) else pa_paryld.meetnr endif) else pa_tasu.meetnr endif) as pa_meetnr,
              (if pa_tasu.kulnr is null then (if pa_paryld.kulnr is null then (if pa_taslist_a.kulnr is null then (if pa_taslist_b.kulnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif) else pa_taslist_b.kulnr endif) else pa_taslist_a.kulnr endif) else pa_paryld.kulnr endif) else pa_tasu.kulnr endif) as pa_kulnr,
              (if pa_tasu.talnr is null then (if pa_paryld.talnr is null then (if pa_taslist_a.talnr is null then (if pa_taslist_b.talnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif) else pa_taslist_b.talnr endif) else pa_taslist_a.talnr endif) else pa_paryld.talnr endif) else pa_tasu.talnr endif) as pa_talnr,
              (if pa_tasu.objectnr is null then (if pa_paryld.objectnr is null then (if pa_taslist_a.objectnr is null then (if pa_taslist_b.objectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_taslist_b.objectnr endif) else pa_taslist_a.objectnr endif) else pa_paryld.objectnr endif) else pa_tasu.objectnr endif) as pa_objectnr,
              (if pa_tasu.osaknr is null then (if pa_paryld.osaknr is null then (if pa_taslist_a.osaknr is null then (if pa_taslist_b.osaknr is null or pa_taslist_b.samkul=0 then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_taslist_b.osaknr endif) else pa_taslist_a.osaknr endif) else pa_paryld.osaknr endif) else pa_tasu.osaknr endif) as pa_osaknr,
              (if pa_tasu.subjectnr is null then (if pa_paryld.subjectnr is null then (if pa_taslist_a.subjectnr is null then (if pa_taslist_b.subjectnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_taslist_b.subjectnr endif) else pa_taslist_a.subjectnr endif) else pa_paryld.subjectnr endif) else pa_tasu.subjectnr endif) as pa_subjectnr,
              (if pa_tasu.suundnr is null then (if pa_paryld.suundnr is null then (if pa_taslist_a.suundnr is null then (if pa_taslist_b.suundnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_taslist_b.suundnr endif) else pa_taslist_a.suundnr endif) else pa_paryld.suundnr endif) else pa_tasu.suundnr endif) as pa_suundnr,
              (if pa_tasu.taotnr is null then (if pa_paryld.taotnr is null then (if pa_taslist_a.taotnr is null then (if pa_taslist_b.taotnr is null or pa_taslist_b.samkul=0 then (if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif) else pa_taslist_b.taotnr endif) else pa_taslist_a.taotnr endif) else pa_paryld.taotnr endif) else pa_tasu.taotnr endif) as pa_taotnr,
               'Tööandja töötuskindlustus'
        from pa_tasu left outer join pa_taslist as pa_taslist_a on pa_tasu.tas_s = pa_taslist_a.tas_s left outer join pa_taslist as pa_taslist_b on pa_tasu.vtas_s = pa_taslist_b.tas_s left outer join pa_yksus on pa_tasu.yksusnr = pa_yksus.yksusnr left outer join pa_paryld on pa_tasu.vparyldnr=pa_paryld.paryldnr
        where (pa_yksus.skoop = a_skoop or a_skoop = 0) and pa_tasu.palgaleht is not null and pa_tasu.t_aasta = li_t_aasta and pa_tasu.t_akuu = li_t_akuu
        group by pa_yksus.skoop,pa_meetnr,pa_kulnr,pa_talnr,pa_objectnr,pa_osaknr,pa_subjectnr,pa_suundnr,pa_taotnr;
    end if;
    if Length(ls_k_eristulukree)>0 then
      set ls_deeb=ls_k_eristulukree;
      insert into gl_pr_kanne_import( tulnud,skoop,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,objectnr,osaknr,subjectnr,suundnr,taotnr,"info")
        select 'Palk',pa_yksus.skoop,a_perlop,sum(pa_erisood.tulumaks),ls_erisorder,ls_deeb,ls_kree,if pa_erisood.meetnr is null then if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif else pa_erisood.meetnr endif as pa_meetnr,if pa_erisood.kul2nr is null then if pa_erisood.kulnr is null then if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif else pa_erisood.kulnr endif else pa_erisood.kul2nr endif as pa_kulnr,if pa_erisood.talnr is null then if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif else pa_erisood.talnr endif as pa_talnr,if pa_erisood.objectnr is null then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_erisood.objectnr endif as pa_objectnr,if pa_erisood.osaknr is null then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_erisood.osaknr endif as pa_osaknr,if pa_erisood.subjectnr is null then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_erisood.subjectnr endif as pa_subjectnr,if pa_erisood.suundnr is null then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_erisood.suundnr endif as pa_suundnr,if pa_erisood.taotnr is null then if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif else pa_erisood.taotnr endif as pa_taotnr,'Erisoodustuste tulumaks lisa 4'
        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_yksus.skoop = a_skoop or a_skoop = 0) and pa_erisood.t_aasta = li_t_aasta and pa_erisood.t_akuu = li_t_akuu and lisalist.objnr=40
        group by pa_yksus.skoop,pa_meetnr,pa_kulnr,pa_talnr,pa_objectnr,pa_osaknr,pa_subjectnr,pa_suundnr,pa_taotnr;
    end if;
    if Length(ls_k_eris5tulukree)>0 then
      set ls_deeb=ls_k_eris5tulukree;
      insert into gl_pr_kanne_import( tulnud,skoop,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,objectnr,osaknr,subjectnr,suundnr,taotnr,"info")
        select 'Palk',pa_yksus.skoop,a_perlop,sum(pa_erisood.tulumaks),ls_erisorder,ls_deeb,ls_kree,if pa_erisood.meetnr is null then if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif else pa_erisood.meetnr endif as pa_meetnr,if pa_erisood.kul2nr is null then if pa_erisood.kulnr is null then if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif else pa_erisood.kulnr endif else pa_erisood.kul2nr endif as pa_kulnr,if pa_erisood.talnr is null then if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif else pa_erisood.talnr endif as pa_talnr,if pa_erisood.objectnr is null then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_erisood.objectnr endif as pa_objectnr,if pa_erisood.osaknr is null then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_erisood.osaknr endif as pa_osaknr,if pa_erisood.subjectnr is null then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_erisood.subjectnr endif as pa_subjectnr,if pa_erisood.suundnr is null then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_erisood.suundnr endif as pa_suundnr,if pa_erisood.taotnr is null then if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif else pa_erisood.taotnr endif as pa_taotnr,'Erisoodustuste tulumaks lisa 5'
        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_yksus.skoop = a_skoop or a_skoop = 0) and pa_erisood.t_aasta = li_t_aasta and pa_erisood.t_akuu = li_t_akuu and lisalist.objnr=50
        group by pa_yksus.skoop,pa_meetnr,pa_kulnr,pa_talnr,pa_objectnr,pa_osaknr,pa_subjectnr,pa_suundnr,pa_taotnr;
    end if;
    if Length(ls_k_erissotskree)>0 then
      set ls_deeb=ls_k_erissotskree;
      insert into gl_pr_kanne_import( tulnud,skoop,kuup,summa,"order",deeb,kree,meetnr,kulnr,talnr,objectnr,osaknr,subjectnr,suundnr,taotnr,"info")
        select 'Palk',pa_yksus.skoop,a_perlop,sum(pa_erisood.sotsmaks),ls_erisorder,ls_deeb,ls_kree,if pa_erisood.meetnr is null then if pa_yksus.meetnr is null then 0 else pa_yksus.meetnr endif else pa_erisood.meetnr endif as pa_meetnr,if pa_erisood.kul2nr is null then if pa_erisood.kulnr is null then if pa_yksus.kulnr is null then 0 else pa_yksus.kulnr endif else pa_erisood.kulnr endif else pa_erisood.kul2nr endif as pa_kulnr,if pa_erisood.talnr is null then if pa_yksus.talnr is null then 0 else pa_yksus.talnr endif else pa_erisood.talnr endif as pa_talnr,if pa_erisood.objectnr is null then (if pa_yksus.objectnr is null then 0 else pa_yksus.objectnr endif) else pa_erisood.objectnr endif as pa_objectnr,if pa_erisood.osaknr is null then (if pa_yksus.osaknr is null then 0 else pa_yksus.osaknr endif) else pa_erisood.osaknr endif as pa_osaknr,if pa_erisood.subjectnr is null then (if pa_yksus.subjectnr is null then 0 else pa_yksus.subjectnr endif) else pa_erisood.subjectnr endif as pa_subjectnr,if pa_erisood.suundnr is null then (if pa_yksus.suundnr is null then 0 else pa_yksus.suundnr endif) else pa_erisood.suundnr endif as pa_suundnr,if pa_erisood.taotnr is null then if pa_yksus.taotnr is null then 0 else pa_yksus.taotnr endif else pa_erisood.taotnr endif as pa_taotnr,'Erisoodustuste sotsiaalmaks'
        from pa_erisood left outer join pa_yksus on pa_erisood.yksusnr = pa_yksus.yksusnr
        where (pa_yksus.skoop = a_skoop or a_skoop = 0) and pa_erisood.t_aasta = li_t_aasta and pa_erisood.t_akuu = li_t_akuu
        group by pa_yksus.skoop,pa_meetnr,pa_kulnr,pa_talnr,pa_objectnr,pa_osaknr,pa_subjectnr,pa_suundnr,pa_taotnr;
    end if;
  end if;
END