Document posting using BAPI_ACC_DOCUMENT_POST with tax and updating payment terms after posting
BDC (Batch Data Communication) call transactions are used to post and update the documents as a background jobs. In SAP S4HANA some transactions are obsolete, and some are not. The obsolete BDC transactions needs to be replaced with BAPI’s or Function modules.
In this blog, without using BDC’s we will try to post a vendor document through BAPI_ACC_DOCUMENT_POST along with TAX amount. Right after posting we will add a piece of code to update the payment terms of vendor.
The BAPI which we are going to use is used for posting invoices of customer and Vendor. We will use the below.
Vendor transaction details for posting.
|Document Date||Document Type||Company Code||Posting Date||Reference||Fiscal Year||Fiscal Period|
|Posting Key||Account||Amount||Tax Amount||Cost Center||Tax Code||Currency|
The transaction with posting key 31 indictees credit for vendor and 40 indicates debit to general ledger(G/L). We have to make sure that we maintain relevant sign for amount according to its posting key.
Program to post the vendor document:
DATA: it_item_gl TYPE TABLE OF bapiacgl09, "G/L Items it_item_vi TYPE TABLE OF bapiacap09, "Vendor Items it_item_currency TYPE TABLE OF bapiaccr09, "Currency items it_item_tax TYPE TABLE OF bapiactx09, "TAX account items it_return TYPE TABLE OF bapiret2. "Return messages DATA: wa_item_gl TYPE bapiacgl09, wa_item_vi TYPE bapiacap09, wa_item_currency TYPE bapiaccr09, wa_header TYPE bapiache09, wa_item_tax TYPE bapiactx09. DATA: d_belnr TYPE bseg-belnr, wa_accchg TYPE accchg, it_accchg TYPE fdm_t_accchg. wa_header-username = sy-uname. wa_header-doc_date = '20230814'. wa_header-doc_type = 'DT'. " Document Type wa_header-comp_code = '1000'. " Company code wa_header-pstng_date = '20230814'. wa_header-fisc_year = '2023' . wa_header-fis_period = '05'. wa_header-ref_doc_no = 'Reference'. wa_item_vi-itemno_acc = 1. wa_item_currency-itemno_acc = 1. wa_item_currency-amt_doccur = '-192.00'. " Posting key 31 wa_item_vi-vendor_no = '0000859634'. " Vendor Account wa_item_vi-pmnttrms = '*'. " Payment terms wa_item_currency-currency_iso = 'CCC'. APPEND: wa_item_vi TO it_item_vi, wa_item_currency TO it_item_currency. CLEAR wa_item_currency. wa_item_gl-itemno_acc = 2. wa_item_currency-itemno_acc = 2. wa_item_gl-tax_code = 'VT'. wa_item_gl-fisc_year = '2023' . wa_item_gl-gl_account = '0000083521'. "G/L Account wa_item_gl-costcenter = '0000576924'. "Cost Center wa_item_currency-amt_doccur = '160.00'. "Posting Key 40 wa_item_currency-currency_iso = 'CCC'. APPEND: wa_item_gl TO it_item_gl, wa_item_currency TO it_item_currency. CLEAR wa_item_currency. wa_item_tax-tax_code = 'VT'. wa_item_tax-itemno_acc = 3. wa_item_currency-itemno_acc = 3. wa_item_currency-amt_doccur = '32.00'. " Posting key 40 wa_item_currency-currency_iso = 'CCC'. wa_item_currency-amt_base = '160'. APPEND wa_item_tax TO it_item_tax. APPEND wa_item_currency TO it_item_currency. CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST' EXPORTING documentheader = wa_header TABLES accountgl = it_item_gl accountpayable = it_item_vi currencyamount = it_item_currency accounttax = it_item_tax return = it_return. IF sy-subrc = 0. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. IF sy-msgno = 605. WRITE: 'Document ',sy-msgv2. ENDIF. ENDIF.
Let’s have a look how CURRENCYAMOUNT is getting filed. In a normal scenario it will have two line-items one for vendor and another for G/L entries. In this case it will have 3 line-times to post the invoice with tax amount.
The return table from BAPI will holds the details of successful posted and failure documents as
shown in below screenshot. the successful positing details will be saved in IT_RETURN table with message number 605.
After the Invoice got posted form BAPI, we can verify the same the posted invoice details form T-code FB03.
While filling the vendor details, we have given ” * ” for its payment terms values. By this the BAPI will read the payment term which is associated with the vendor used for posting. Below details are for vendor line-item for this posted document.
Now we will add the below piece of code to our existing code in after BAPI COMMIT section to update the payment terms of this posted document. For this we will be using the function module FI_DOCUMENT_CHANGE. This FM will need few input parameters such as document number, vendor account, fiscal year and company code. It is also required to pass a table which has the details of the field which we are updating.
DATA: d_belnr TYPE bseg-belnr, wa_accchg TYPE accchg, it_accchg TYPE fdm_t_accchg. IF sy-subrc = 0. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. IF sy-msgno = 605. MOVE sy-msgv2 TO d_belnr. WRITE: 'Document ',sy-msgv2. SELECT bukrs, belnr, gjahr, lifnr , zterm FROM bseg INTO @DATA(wa_bseg) WHERE bukrs = @wa_header-comp_code AND belnr = @d_belnr AND gjahr = @wa_header-fisc_year AND koart = 'K'. ENDSELECT. wa_accchg-fdname = 'ZTERM'. wa_accchg-oldval = wa_bseg-zterm. wa_accchg-newval = 'Z098'. APPEND wa_accchg TO it_accchg. CALL FUNCTION 'FI_DOCUMENT_CHANGE' EXPORTING i_lifnr = wa_bseg-lifnr i_bukrs = wa_bseg-bukrs i_belnr = wa_bseg-belnr i_gjahr = wa_bseg-gjahr TABLES t_accchg = it_accchg EXCEPTIONS no_reference = 1 no_document = 2 many_documents = 3 wrong_input = 4 overwrite_creditcard = 5 OTHERS = 6. IF sy-subrc = 0. MESSAGE 'Document payment term is updated' TYPE 'I'. ELSE. MESSAGE 'Document payment is not updated' TYPE 'I'. ENDIF. ENDIF. ENDIF.
We have successfully posted a vendor invoice along with tax amount without using the BDC. Right after posting the Payment terms of the invoice is updated to new one. Along with Vendor invoices, this BAPI can be used to post general Ledger Financial transactions and Customer Invoices.