Page 1 of 1

Przykład implementacji BADI dla VAT/ Example of BADI for VAT /BCC/JPK_IF_EXT_DOC~READ_DOC_AFT

Posted: Wed Aug 24, 2016 10:43 am
by moderator
Example of BADI implementation for VAT /BCC/JPK_IF_EXT_DOC~READ_DOC_AFT (allows changes to data already read)
Note, that BAdI implementations should be created separately for diffrent JPK/SAF-T file types (example of BADI implementation name ZJPK_IB_DOC_VAT). The separation is obtained by placing filter value for the field IV-JPK-TYP (in this example IV_JPK_TYP="VAT"). This allows writing code for specyfic JPK/SAF-T file in each implemented method.

Code: Select all

* data declarations
  FIELD-SYMBOLS <docs> TYPE table.
  FIELD-SYMBOLS <items> TYPE table.

  DATA: lt_docs TYPE TABLE OF /bcc/jpk_st_vat_dok,
        ls_doc LIKE LINE OF lt_docs,
        lt_items TYPE TABLE OF /bcc/jpk_st_vat_wier,
        lt_item LIKE LINE OF lt_items,
        lr_blart TYPE RANGE OF bkpf-blart,
        lt_bukrs TYPE TABLE OF t001,
        ls_bukrs TYPE t001,
        lv_bukrs TYPE bukrs,
        ls_adrc TYPE adrc,
        lv_index TYPE sy-tabix,
        ls_bkpf TYPE bkpf.
  DATA: lv_lifnr TYPE lfa1-lifnr,
        ls_lfa1 TYPE lfa1,
        ls_addr1 TYPE string,
        ls_name TYPE string,
        ls_addr2 TYPE string,
        ls_addr3 TYPE string,
        ls_addr4 TYPE string,
        ls_adres TYPE string.
  DATA: lv_adrnr TYPE lfa1-adrnr.


*  moving data from generic DATA type to defined for VAT table lt_docs
  ASSIGN ot_doc->* TO <docs>.
  lt_docs = <docs>.

*  reading additional data selection parameters (from include ZXJPK_DYN_VAT if implemented)
  /bcc/jpk_cl_data=>append_so( EXPORTING iv_so = 'S4_BLART' CHANGING ot_so = lr_blart ).
  /bcc/jpk_cl_data=>add_param( EXPORTING iv_param = 'P_BUKRS' CHANGING ov_param = lv_bukrs ).

  LOOP AT lt_docs INTO ls_doc.
* any specific for user logic performed on the table data
    lv_index = sy-tabix.
    IF lt_bukrs[] IS INITIAL.
      SELECT * FROM t001 INTO TABLE lt_bukrs.
      READ TABLE lt_bukrs INTO ls_bukrs WITH KEY bukrs = lv_bukrs.
      SELECT SINGLE * FROM adrc INTO ls_adrc WHERE addrnumber = ls_bukrs-adrnr AND date_to = '99991231'.
    ENDIF.

* Example logic to add business partner details if name is empty form Reference field
    IF ls_doc-nazwakontr IS INITIAL.
      CLEAR: ls_bkpf.
      SELECT SINGLE * FROM bkpf INTO ls_bkpf WHERE belnr = ls_doc-belnr AND gjahr = ls_doc-gjahr.
      IF sy-subrc = 0.
        SELECT SINGLE * FROM bkpf INTO ls_bkpf WHERE belnr = ls_bkpf-belnr AND gjahr = ls_doc-gjahr.
        IF sy-subrc = 0.
          CLEAR: ls_lfa1,
                ls_addr1,
                ls_name,
                ls_addr2,
                ls_addr3,
                ls_addr4,
                ls_adres,
                lv_lifnr.


          CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
            EXPORTING
              input  = ls_bkpf-xref1_hd
            IMPORTING
              output = lv_lifnr.

          CLEAR: ls_lfa1.
          SELECT SINGLE * FROM lfa1 INTO ls_lfa1 WHERE lifnr = lv_lifnr.
          IF sy-subrc = 0.

            CALL FUNCTION 'RTP_US_DB_ADRC_READ'
              EXPORTING
                i_address_number = ls_lfa1-adrnr
              IMPORTING
                e_adrc           = ls_adrc
              EXCEPTIONS
                not_found        = 1
                OTHERS           = 2.
            IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
            ELSE.
              ls_doc-nazwakontr = ls_adrc-name1.
              CONCATENATE ls_adrc-city1 ls_adrc-street ls_adrc-house_num1  INTO ls_adres SEPARATED BY space.
              ls_doc-adreskontr = ls_adres.
              IF ls_lfa1-stcd1 IS NOT INITIAL.
                ls_doc-nipkontr = ls_lfa1-stcd1.
              ELSE.
                ls_doc-nipkontr = ls_lfa1-stceg.
              ENDIF.

              DATA: lv_langu TYPE char2.
              CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT'
                EXPORTING
                  input  = ls_lfa1-spras
                IMPORTING
                  output = lv_langu.
            
 *            Example of adjusting the NIP value (TAX ID)
              IF ls_doc-nipkontr(2) <> lv_langu.
                CONCATENATE lv_langu ls_doc-nipkontr INTO ls_doc-nipkontr.
              ENDIF.
              REPLACE ALL OCCURRENCES OF '-' IN ls_doc-nipkontr WITH ''.
              MODIFY lt_docs FROM ls_doc INDEX lv_index.
            ENDIF.
          ENDIF.

        ENDIF.
      ENDIF.
    ENDIF.


* Example: filling business partner name and address.
    IF ls_doc-blart IN lr_blart AND lr_blart IS NOT INITIAL.
      CLEAR: ls_doc-kunnr, ls_doc-lifnr, ls_doc-nazwakontr, ls_doc-adreskontr, ls_doc-nipkontr.
      CONCATENATE ls_adrc-name1 ls_adrc-name2 ls_adrc-name3 INTO ls_doc-nazwakontr SEPARATED BY space.
      CONCATENATE ls_adrc-city1 ls_adrc-city2 ls_adrc-post_code1 ls_adrc-street ls_adrc-house_num1 INTO ls_doc-adreskontr SEPARATED BY space.
      MOVE ls_bukrs-stceg TO ls_doc-nipkontr.
      MODIFY lt_docs FROM ls_doc INDEX lv_index.
    ENDIF.

  ENDLOOP.

* returning changed data to output parameter
  <docs> = lt_docs.