Learn how to use a Business Add-In (BAdI) to populate the profit center for the tax lines when you use BAPI_ACC_DOCUMENT_POST to post a document. Follow step-by-step instructions to use the BAPI and BAdI with the sample code. In this way, you can extend the BAPI functionality and easily populate the profit center by yourself.
Key Concept
BAPI BAPI_ACC_DOCUMENT_POST is the BAPI function module widely used for posting of FI documents. It has a function similar to transaction FB01. You may face a situation in which a business user wants to allocate e-commerce sales/cost of goods sold (COGS) to a retail store based on some rules such as the nearest store or by address. The solution is to post the sales revenue /COGS to the profit center that links to the target store. As the profit center is a mandatory field and is required to be zero-balanced in the SAP system, you have to ensure that profit centers are populated for each line item of the document. If you implement this requirement, your first inclination maybe to use BAPI BAPI_ACC_DOCUMENT_POST to create the financial accounting document. However, I recommend that you to extend its functionality with a BAdI. However, the issue is that the profit center can’t be populated for two tax line items. At first glance, you can use substitution, transaction code OKB9, or even a manual input to meet the requirements. However, on closer consideration you can see those won’t work. OKB9 only provides the default cost center/profit center, but the profit center in my example is variable. Substitution doesn’t work for the same reason. Document splitting also doesn’t work as the document splitting rule is not so advanced that it can populate the profit center for two tax lines. Fortunately you can use a Business Add-In (BAdI) to change table C_ACCIT, which includes the tax lines. The solution is to implement the BAdI, populate the profit center for the two tax line items, and post the document. The BAPI provides structure EXTENSION2, which can be retrieved in the BADI AC_DOCUMENT. I explain how to implement the BAdI to populate the profit center and then provide sample code to demonstrate it.
A Business Case
The business requires allocation of sales revenue or COGS to the profit center based on some rules. The profit center is a mandatory field and is required to be zero balanced in the SAP system. Document splitting is also activated so that the profit centers in the BAPI have to be populated.
As a result of integration of Profit Center Accounting (PCA) into the SAP General Ledger, new functions such as document splitting are available. Using the document splitting function (online document split), you can create balance sheets for company codes as well as for other entities such as the profit center. The balance is then set to zero for each document for the profit center. To check the document splitting characteristic for general ledger accounting, follow IMG menu path Financial Accounting (New) > General Ledger Accounting (New) > Business Transactions > Document Splitting > Define Document Splitting Characteristics for General Ledger Accounting. In the screen that appears (
Figure 1), you can specify a mandatory field after document splitting. In my example, I have specified the partner field named PRCTR and entered a description (e.g., PRCTR Profit Center). I also selected the indicators for Zero balance and Mandatory Field. Because the Mandatory Field indicator is set, all postings in which no value is set for the specified field after document splitting are rejected with an error message.
Figure 1
Set up a mandatory field after document splitting
Because the profit center is configured to be a mandatory field and is a document splitting characteristic, you have to feed the profit center to the BAPI BAPI_ACC_DOCUMENT_POST.
Note SAP has provided many BAPIs to post an accounting document, including BAPI_ACC_GL_POSTING_POST, BAPI_ACC_DOCUMENT_POST, and BAPI_ACC_BILLING_POST. I only discuss BAPI BAPI_ACC_DOCUMENT_POST in this article as I chose to calculate the tax by myself to feed the BAPI under this business scenario.
I use the following sample accounting document shown in
Figure 2. This document is created after I execute the sample program and call the BAPI or BAdI within it.
Figure 2
Sample accounting document to demonstrate the BAPI/BAdI
I now briefly explain how the SAP system sets the profit center for the document line items. There are different options to set a profit center: manual entry (if the transaction permits this), FI substitution, implementation of the BAdI AC_DOCUMENT (for postings using the accounting interface only, for example, materials management [MM] and sales and distribution [SD] postings), and PCA transactions 3KEH or 3KEI. In Release ERP 2005 and above, transaction codes 3KEH and 3KEI from the classic PCA are no longer used to set a profit center. The new transaction code FAGL3KEH, which combines the functionality of 3KEH and 3KEI, and the BAdI FAGL_3KEH_DEFPRCTR are available for maintaining the profit centers. You can easily populate the profit center for sales revenue, discount, and customer or administrator fee line items. However, it’s hard for you to populate the profit center for the tax lines 0030 and 0060. You can’t use manual input as the profit center on the document is variable. For this reason, the substitution and FAGL3KEH do not work. Document splitting can be used to set a proposal profit center while it derives the profit center based on the splitting method. In this case, the profit center derivation for tax lines is based on a general ledger (G/L) account that belongs to the base item category such as expense or revenue account. Unfortunately, the splitting method is not that flexible. Therefore, the profit center derived by the document splitting is not what is expected.
BAPI (BAPI_ACC_DOCUMENT_POST)
To view the definition of the BAPI, use transaction code SE37. After you fill in BAPI name BAPI_ACC_DOCUMENT_POST and execute transaction code SE37, you can switch to a different tab page to display the different parameters for the BAPI (
Figure 3).
Figure 3
Table parameters in BAPI BAPI_ACC_DOCUMENT_POST
You have to fill in several interface structure tables including the accounting document header (import parameter DOCUMENTHEADER of structure BAPIACHE09), account information for the document line item (table parameter ACCOUNTGL of structure BAPIACGL09), accounting information for the tax item(table parameter ACCOUNTTAX of structure BAPIACTX09, and currency amount information for the document line item (table parameter CURRENCYAMOUNT of structure BAPIACCR09). These interface tables are transferred to the accounting interface and then posted to the accounting document during the BAPI call.
Tables 1 and
2 describe the components of BAPIACGL09 and BAPIACTX09, respectively. The information in these structures such as company code, account, and tax code composes the document line item and tax item of accounting document. After viewing these tables, you see that compared with structure BAPIACGL09-- G/L account item, the structure BAPIACTX09 tax item has no component of PROFIT_CTR. You can use document splitting to propose the profit center, but the results won’t be what the business needs. Therefore, I use a BAdI that can change the C_ACCIT table so that it includes the tax lines (
Table 3.)
Component | Component type | Short description |
ITEMNO_ACC | POSNR_ACC | Accounting document line-item number |
GL_ACCOUNT | HKONT | General ledger account |
| | |
COMP_CODE | BUKRS | Company code |
PROFIT_CTR | PRCTR | Profit center |
| | |
Table 1
Structure of BAPIACGL09 — G/L account item (only list part of the whole structure)
Component | Component type | Short description |
ITEMNO_ACC | POSNR_ACC | Accounting document line-item number |
GL_ACCOUNT | HKONT | General ledger account |
COMP_CODE | BUKRS | Company code |
TAX_CODE | MWSKZ | Tax on sales or purchases code |
Table 2
Structure BAPIACTX09 — Tax item (only list part of the whole structure)
Component | Component type | Short description |
POSNR | POSNR_ACC | Accounting document line-item number |
HKONT | HKONT | General ledger account |
BUKRS | BUKRS | Company code |
GJAHR | GJAHR | Fiscal year |
PRCTR | PRCTR | Profit center |
ZUONR | DZUONR | Assignment number |
SGTXT | SGTXT | Item text |
Table 3
Structure ACCIT — Accounting Interface: Item Information (only list part of the whole structure)
In sample code ZFI_SAMPLE_POSTING (
Figure 4), I populated the interface structure C_EXTENSION2 with several text fields, including a description of the transaction, line item number, proposed profit center, line item assignment, and text field.
Figure 4
Sample code for populate structure C_EXTENSION2
After you populate the structure C_EXTENSION2 (header and item information population is omitted here, but is included in the sample program), you can call the BAPI BAPI_ACC_DOCUMENT_POST with the code in
Figure 5.
Figure 5
The information includes C_EXTENSION2 feed to the BAPI during the BAPI call
The structure EXTENSION2 (
Figure 4) can be retrieved in BAdI AC_DOCUMENT and be addressed in the method CHANGE (
Figure 6).
Figure 6
Parameters of Method CHANGE in BAdI
How to Implement the BAdI AC_Document
The BAdI AC_Document is called up within the BAPI’s accounting document check BAPI_ACC_DOCUMENT_CHECK and accounting document post BAPI_ACC_DOCUMENT_POST.
At this point in time, the accounting document can be derived and changed before the active accounting components are accessed.
To implement this BAdI, follow menu path Tools > ABAP Workbench > Utilities > Business Add-Ins > SE18 Definition/ SE19 Implementation. In the screen that appears (Figure 7), fill in the BAdI name ACC_DOCUMENT and click the Create Impl button to create your implementation of the BAdI.
Figure 7
Create the implementation for BAdI ACC_DOCUMENT
In the next screen (
Figure 8) click the insert row icon

.
Figure 8
Create one implementation for BAdI ACC_DOCUMENT
Click the blank field below Short text Ref. Tran. This action opens a screen (
Figure 9) with a drop-down list.
Figure 9
Set the filter value for BAdI implementation
Note Because the BAdI is filter dependent, you need to enter the reference transaction (sender document) as the filter type during implementation. One BAdI can have several implementations. The system decides which active
add-in implementation to execute, based on this filter value.
Now set the BKPFF value for AWTYP as the reference transaction. The values can be selected from a drop-down list of options in the field below the Ref. Tran column. In my example, I use the BKPFF-Accounting doc direct input. Now click the Interface tab in
Figure 9 to see the methods (
Figure 10).
Figure 10
Methods in the BAdI
In the field under the Method column, double-click CHANGE. This action opens the ABAP editor (
Figure 11).
Figure 11
The coding of method CHANGE
In this code snippet, you retrieve the interface structure table C_EXTENSION2 that was already populated during call BAPI in the sample program ZFI_SAMPLE_POSTING. As shown in the code above, you now have the profit center, assignment of line item (ZUONR), and text of the line item using the code lines:
ls_it-posnr = ls_ext-valuepart1 ls_it-prctr = ls_ext-valuepart2 ls_it-zuonr = ls_ext-valuepart3 ls_it-sgtxt = ls_ext-valuepart4 You then modify the accounting interface table C_ACCIT with these values. The code lines from 21 to 25 of method CHANGE help for this (Figure 12).
Figure 12
Modify the accounting interface table C_ACCIT
Structure C_ACCIT is an important accounting interface table that includes the document item information. The items that are later contained in the FI document are transferred to this table. Depending on the release version, this table has more than 300 columns. Each document item contains, for example, information such as the company code (BUKRS), the fiscal year (GJAHR), the posting date (BUDAT), or the account number (HKONT). The profit center (PRCTR) that you want to change in the BAPI is also included in this structure. Sample Programs Two sample programs (ZFI_SAMPLE_POSTING and Code of Method Change in BADI) are available for you to download. I recommend that you download these sample codes when you read the article, as each program represents a working example (they only require a change the G/L account, customer, and tax code according to your SAP environment). With these samples you can try the functionality demonstrated in the article as long as you have access to an SAP system and have sufficient authorizations. Click here to download the ZFI_SAMPLE_POSTING code. Click here to download the Code of Method Change in BADI. Charles Liu
Charles Liu is SAP manager at Luxottica Retail Co. Ltd. with 15 years consulting and industry experience. He has experience in FI and CO along with ABAP programming. He has a degree in computer science from Jiangxi Normal University and a master’s degree in information and systems from Fudan University. You may contact the author at
charlesliu@foxmail.com. If you have comments about this article or publication, or would like to submit an article idea, please contact the
editor.