This example shows changing correction invoices data after being read, depending on VAT code:
Code: Select all
DATA: lt_doc TYPE TABLE OF /bcc/jpk_st_fa_dok,
lt_item TYPE TABLE OF /bcc/jpk_st_fa_wier,
ls_doc TYPE /bcc/jpk_st_fa_dok,
ls_item TYPE /bcc/jpk_st_fa_wier,
ls_bset TYPE bset,
ls_bkpf TYPE bkpf,
lt_bseg TYPE TABLE OF bseg,
ls_bseg TYPE bseg,
lv_doc_index TYPE sy-tabix,
lv_success TYPE sy-ucomm.
FIELD-SYMBOLS <docs> TYPE table.
FIELD-SYMBOLS <items> TYPE table.
ASSIGN ot_doc->* TO <docs>.
ASSIGN ot_item->* TO <items>.
DATA: lr_mwskz TYPE RANGE OF mwskz.
CALL METHOD /bcc/jpk_cl_fa=>append_so
EXPORTING
iv_so = 'S5_MWSKZ' "Kod podatku dla korekty
CHANGING
ot_so = lr_mwskz.
lt_doc = <docs>.
lt_item = <items>.
LOOP AT lt_doc INTO ls_doc.
CLEAR: lv_success, ls_bkpf.
lv_doc_index = sy-tabix.
SELECT SINGLE * FROM bkpf INTO ls_bkpf WHERE gjahr = ls_doc-gjahr AND belnr = ls_doc-belnr.
LOOP AT lt_item INTO ls_item WHERE gjahr = ls_doc-gjahr AND belnr = ls_doc-belnr.
SELECT SINGLE * FROM bset INTO ls_bset
WHERE gjahr = ls_item-gjahr
AND belnr = ls_item-belnr.
" AND buzei = ls_item-buzei.
IF sy-subrc = 0.
IF lr_mwskz IS NOT INITIAL AND ls_bset-mwskz IN lr_mwskz.
SELECT * FROM bseg INTO TABLE lt_bseg WHERE gjahr = ls_item-gjahr AND belnr = ls_item-belnr AND buzei = ls_item-buzei.
SORT lt_bseg ASCENDING BY buzei.
LOOP AT lt_bseg INTO ls_bseg.
IF ls_bseg-sgtxt IS NOT INITIAL.
ls_doc-rodzfak = 'KOREKTA'.
ls_doc-przyczkor = ls_bseg-sgtxt.
ls_doc-nrfakkor = ls_bkpf-xref1_hd.
ls_doc-okrfakkor = ls_bkpf-xref2_hd.
MODIFY lt_doc FROM ls_doc INDEX lv_doc_index.
lv_success = 'YES'.
EXIT.
ENDIF.
ENDLOOP.
IF lv_success IS NOT INITIAL.
EXIT.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
ENDLOOP.
<docs> = lt_doc.
<items> = lt_item.