on ‎2020 Aug 27 10:56 AM
Request clarification before answering.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi sandra.rossi
It is a little difficult to change the code because it is a copy of a report of national and foreign currencies making calculations of credit and debit and the total spent. I am reusing the code and changed only the necessary one, transforming it into a Web service. Here is the part of the referred selects.
Thanks.
IF p_est EQ 'X'.
LOOP AT t_bsik.
SELECT SINGLE * FROM bkpf
WHERE bukrs EQ t_bsik-bukrs
AND belnr EQ t_bsik-belnr
AND gjahr EQ t_bsik-gjahr
AND stblg NE space.
IF sy-subrc NE 0.
APPEND t_bsik TO lt_bsik.
ENDIF.
ENDLOOP.
SORT lt_bsik BY blart.
* Tratar primeiro os AB
LOOP AT lt_bsik INTO ls_bsik WHERE blart EQ 'AB'.
SELECT *
FROM bsak INTO TABLE lt_bsak
WHERE augbl EQ ls_bsik-belnr " and ( blart eq 'ES' or blart eq 'RE').
AND budat IN so_budat.
IF sy-subrc EQ 0.
CLEAR: lv_re, lv_es, total_re, total_es, lv_total, total_ab_deb, total_ab_cre, ls_bsak_tmp.
* Calcular os totais de todos os items para doc RE e doc ES
LOOP AT lt_bsak INTO ls_bsak_tmp.
IF ls_bsak_tmp-blart EQ 'RE' AND ls_bsak_tmp-shkzg EQ 'S'.
lv_re = ls_bsak_tmp-belnr.
total_re = total_re + ls_bsak_tmp-wrbtr.
ELSEIF ls_bsak_tmp-blart EQ 'RE' AND ls_bsak_tmp-shkzg EQ 'H'.
lv_re = ls_bsak_tmp-belnr.
total_re = total_re + ls_bsak_tmp-wrbtr.
total_re = total_re * -1.
ELSEIF ls_bsak_tmp-blart EQ 'ES' AND ls_bsak_tmp-shkzg EQ 'H'.
lv_es = ls_bsak_tmp-belnr.
total_es = total_es + ls_bsak_tmp-wrbtr.
total_es = total_es * -1.
ELSEIF ls_bsak_tmp-blart EQ 'ES' AND ls_bsak_tmp-shkzg EQ 'S'.
lv_es = ls_bsak_tmp-belnr.
total_es = total_es + ls_bsak_tmp-wrbtr.
ELSEIF ls_bsak_tmp-blart EQ 'AB' AND ls_bsak_tmp-shkzg EQ 'S'.
total_ab_deb = total_ab_deb + ls_bsak_tmp-wrbtr.
ELSEIF ls_bsak_tmp-blart EQ 'AB' AND ls_bsak_tmp-shkzg EQ 'H'.
total_ab_cre = total_ab_cre + ls_bsak_tmp-wrbtr.
total_ab_cre = total_ab_cre * -1.
ENDIF.
ENDLOOP.
IF lv_re IS NOT INITIAL AND lv_es IS NOT INITIAL.
lv_total = total_es + total_re + total_ab_cre + total_ab_deb.
IF lv_total EQ 0.
CLEAR ls_excluir.
ls_excluir-num_excluir = ls_bsik-belnr.
APPEND ls_excluir TO lt_excluir.
CLEAR ls_excluir.
ls_excluir-num_excluir = lv_re.
APPEND ls_excluir TO lt_excluir.
CLEAR ls_excluir.
ls_excluir-num_excluir = lv_es.
APPEND ls_excluir TO lt_excluir.
ENDIF.
ENDIF.
ELSE.
CLEAR: lt_bsak[], ls_bsak_tmp, total_blart, total_ab_deb, total_ab_cre.
SELECT * FROM bsak INTO TABLE lt_bsak
WHERE augbl EQ ls_bsik-augbl
AND bukrs EQ ls_bsik-bukrs
AND lifnr EQ ls_bsik-lifnr
AND gjahr EQ ls_bsik-gjahr.
IF sy-subrc EQ 0.
LOOP AT lt_bsak INTO ls_bsak_tmp.
IF ls_bsak_tmp-blart NE 'AB' AND ls_bsak_tmp-shkzg EQ 'H'.
lv_blart = ls_bsak_tmp-belnr.
total_blart = total_blart + ls_bsak_tmp-wrbtr.
total_blart = total_blart * -1.
ELSEIF ls_bsak_tmp-blart NE 'AB' AND ls_bsak_tmp-shkzg EQ 'S'.
lv_blart = ls_bsak_tmp-belnr.
total_blart = total_blart + ls_bsak_tmp-wrbtr.
ELSEIF ls_bsak_tmp-blart EQ 'AB' AND ls_bsak_tmp-shkzg EQ 'S'.
total_ab_deb = total_ab_deb + ls_bsak_tmp-wrbtr.
ELSEIF ls_bsak_tmp-blart EQ 'AB' AND ls_bsak_tmp-shkzg EQ 'H'.
total_ab_cre = total_ab_cre + ls_bsak_tmp-wrbtr.
total_ab_cre = total_ab_cre * -1.
ENDIF.
ENDLOOP.
IF lv_blart IS NOT INITIAL.
lv_total = total_ab_cre + total_ab_deb + total_blart.
IF lv_total EQ 0.
CLEAR ls_excluir.
ls_excluir-num_excluir = ls_bsik-belnr.
APPEND ls_excluir TO lt_excluir.
CLEAR ls_excluir.
ls_excluir-num_excluir = lv_blart.
APPEND ls_excluir TO lt_excluir.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
CLEAR: ls_bsik.
loop at lt_bsik into ls_bsik where blart ne 'AB'.
* at new belnr.
clear: ls_bsak_tmp2, lt_bsak_tmp[], lv_total, total_es, total_re, lv_negative, lv_kz.
* INIT Delete JArgencio 29-03-2012
* apanhar todos os documentos que são possiveis estornos directos (KZ e ES respectivo)
* select * from bsak into table lt_bsak_tmp where augbl eq ls_bsik-augbl
* and augdt in so_budat2
* and blart ne 'AB'
* and blart ne 'RE'.
* END Delete JArgencio 29-03-2012
* INIT Insert JArgencio 29-03-2012
* apanhar todos os documentos que são possiveis estornos directos (KZ e ES respectivo)
* END Insert JArgencio 29-03-2012
select * from bsak into table lt_bsak_tmp where augbl eq ls_bsik-augbl
and augdt in so_budat
and blart ne 'AB'
and blart ne 'RE'
and gjahr eq ls_bsik-gjahr.
if sy-subrc eq 0.
loop at lt_bsak_tmp into ls_bsak_tmp2.
if ls_bsak_tmp2-shkzg eq 'S' and ls_bsak_tmp2-belnr eq ls_bsik-belnr.
total_es = total_es + ls_bsak_tmp2-wrbtr.
elseif ls_bsak_tmp2-shkzg eq 'H' and ls_bsak_tmp2-belnr eq ls_bsik-belnr.
if total_es eq 0.
total_es = total_es + ls_bsak_tmp2-wrbtr.
total_es = total_es * -1.
else.
clear lv_negative.
lv_negative = ls_bsak_tmp2-wrbtr * -1.
total_es = total_es + lv_negative.
endif.
elseif ls_bsak_tmp2-shkzg eq 'S' and ls_bsak_tmp2-belnr ne ls_bsik-belnr.
lv_kz = ls_bsak_tmp2-belnr.
total_re = total_re + ls_bsak_tmp2-wrbtr.
elseif ls_bsak_tmp2-shkzg eq 'H' and ls_bsak_tmp2-belnr ne ls_bsik-belnr.
lv_kz = ls_bsak_tmp2-belnr.
if total_re eq 0.
total_re = total_re + ls_bsak_tmp2-wrbtr.
total_re = total_re * -1.
else.
clear lv_negative.
lv_negative = ls_bsak_tmp2-wrbtr * -1.
total_re = total_re + lv_negative.
endif.
endif.
endloop.
It doesn't look complex to adapt. But of course you'll need to re-test the selection logic.
The first thing I see is that one of the first rules in ABAP is to not use SELECT * (unless there are few columns or you use more than given % of columns).
I don't see the SELECT ... FROM bsik.
You can do things like that, either using join via EXISTS, or via GTT, or pseudo joins (less efficient than previous solutions) via FOR ALL ENTRIES, or AMDP:
SELECT columns FROM bkpf
WHERE EXISTS ( SELECT * FROM bsik " or NOT EXISTS, depends what theoretically returns less records
WHERE " here the same selections as your original SELECT ... FROM BSIK
AND ...
AND bkpf~bukrs = bsik-bukrs
AND bkpf~belnr = bsik-belnr
AND bkpf~gjahr = bsik-gjahr )
AND stblg NE space
INTO TABLE @DATA(bkpf_lines).
| User | Count |
|---|---|
| 7 | |
| 6 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.