Post FI Document Using BAPI

April 3, 2018 | Author: Anonymous pgde6dmLX | Category: Database Transaction, File Format, C (Programming Language), Subroutine, Exchange Rate


Comments



Description

Post FI document using BAPIBy Rajesh Kumar, Infosys Technologies INTRODUCTION: The accounting documents record changes in values in a company code arising from accounting transactions. They consist of one or more line items (postings), each of which represents an individual transaction posted to an account. When posting an accounting document, the SAP system updates the transaction figures in the accounts to which the document is posted.An accounting document is a representation within the SAP System of the document (for example, an invoice) that triggered the posting. BAPI ‘BAPI_ACC_DOCUMENT_POST’ acts as the interface between your code and standard transactions FB01/FB50. This document explains the various steps involved to post a FI document using BAPI ‘BAPI_ACC_DOCUMENT_POST’ from an excel file.This is helpful to SAP Technical consultants as it gives the details of all the required parameters and data in document posting and validation to check if the GL account in which we are posting amounts is present in SAP database. Transaction code for FI documents are FB01/FB50(Create), FB02(Change) and FB03(Display). In one document, more than 999 line item cannot be posted because BAPI can post up to 999 lines in one document. The file should be balanced for the document to be posted i.e. credit amount and debit amount should be equal. In FI, header table for document data is BKPF and line item table is BSEG. STEPS: Various steps involved in posting a document from a file are: 1. Upload data from excel file into an internal table using FM ‘GUI_UPLOAD’ as explained in subroutine UPLOAD_FORM_EXCEL. 2. After we upload excel data into an internal table we need split the data into field patterns into header data and line item data as explained in subroutine ‘SPLIT_INTO_RECORDS’. This splitting is done according to my file format. In header data in the file in line no.2, posting date and header company code. That is why I have used case when 2. And all the other information is present at the lines as described by CASE statement. Line item details start from line number 14, that’s why LOOP AT t_upload INTO w_upload FROM 14is used. User can split the data according to their file format. 3. After the splitting, loop at the line item table and validate the GL account numbers. If the GL accounts are valid, Populate the line item data into BAPI GL and currency tables otherwise append that GL account number into error message table. This is explained in subroutines ‘VALIDATE_GL’ and ‘POPULATE_BAPI’. 4. In case of intercompany postings, in which line item and Header Company codes are different. Find out line item company code currency key and compare if the line item currency key and header currency key given is same or different. If these are different then we need to populate ‘EXCHANGE_RATE’ field of currency table if it is given in the file. Otherwise BAPI itself will fetch the exchange rate between 2 currencies and do the conversion. 5. Above step is required because a document is posted in 3 currencies document currency, company code currency and group currency. In case of the intra company code postings all the currencies are same so the amounts are same In all3 currencies. But in case of inter company code postings the currencies are different so the amounts are also different. Due to this we need to give and exchange rate base on which the BAPI will convert the amounts. 6. Next step in document posting is to check the data using BAPI ‘BAPI_ACC_DOCUMEN_CHECK’. If this BAPI do not return any error message, go to next step otherwise display error message given by BAPI. 7. Post the document using BAPI ‘BAPI_ACC_DOCUMEN_POST’. Check for sy-subrc, if initial; commit the work using ‘BAPI_TRANSACTION_COMMIT’. This step is explained in subroutine ‘POST_DOCUMENT’. While providing header data in W_HEADER work area do not provide OBJ_KEY, OBJ_TYPE and OBJ_SYS, these are automatically populated by BAPI. SUMMARY: This document explains the various steps involved in FI document posting from excel file. Main steps involved are uploading and splitting data into internal table, Populate data into BAPI tables and call BAPI to post the documents and committing the transaction. A sample source code is given for the references. Note:In case of intercompany code postings, if the exchange rate between 2 currencies is given in the file, programmer should pass only the exchange rate given. Currency value conversions will be done by the BAPI. Also Field status group configurations of GL accounts are not considered by BAPI as it is bypassed by BAPI, but these are considered when doing posting using FB50. SOURCE CODE: *Global DATA declarations TYPES: BEGIN OF type_upload, "Work table used for upload rec(500) TYPE c, END OF type_upload. *Global work area declarations DATA: w_postingsTYPE , w_header TYPE bapiache09, w_bukrs TYPE type_bukrs, w_upload TYPE type_upload, w_item_gl TYPE bapiacgl09, w_dummy(1) TYPE c, "#EC NEEDED w_item_curr TYPE bapiaccr09. DATA:t_posting TYPE STANDARD TABLE OF , t_return TYPE STANDARD TABLE OF bapiret2, t_bukrs TYPE STANDARD TABLE OF type_bukrs, t_item_gl TYPE STANDARD TABLE OF bapiacgl09, t_upload TYPE STANDARD TABLE OF type_upload, t_filename TYPE STANDARD TABLE OF type_filename, t_item_curr TYPE STANDARD TABLE OF bapiaccr09. *&--------------------------------------------------------------------* *&Form UPLOAD_FROM_EXCEL *&--------------------------------------------------------------------* FORM upload_from_excel. REFRESH t_upload. *--upload data from excel to internal table DATA: lw_filename TYPE string. lw_filename = p_file. CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = lw_filename filetype = 'ASC' TABLES data_tab = t_upload EXCEPTIONS file_open_error = 1 file_read_error = 2 no_batch = 3 gui_refuse_filetransfer = 4 invalid_type = 5 no_authority = 6 unknown_error = 7 bad_data_format = 8 header_not_allowed = 9 separator_not_allowed = 10 header_too_long = 11 unknown_dp_error = 12 access_denied = 13 dp_out_of_memory = 14 disk_full = 15 dp_timeout = 16 OTHERS = 17. *- End of change @ECC ENDFORM." UPLOAD_FROM_EXCEL *&--------------------------------------------------------------------* *&Form SPLIT_INTO_RECORDS *&--------------------------------------------------------------------* FORM split_into_records. DATA: lv_doc_date TYPE sy-datum, lv_account TYPE char10, lv_product TYPE char20, lv_period TYPE char2, lv_kursf TYPE char9, lv_aufnr TYPE char12, lv_plant TYPE char10, lv_customer TYPE kunnr, lv_cust_hie2 TYPE hiezu02, lv_pstng_date TYPE sy-datum, lv_amount TYPE char30. CLEAR:lv_doc_date, lv_account, lv_product, lv_period, lv_kursf, lv_plant, lv_customer, lv_cust_hie2, lv_pstng_date, lv_amount. *Split header data LOOP AT t_upload INTO w_upload. CASE sy-tabix. * start of insert @007 WHEN 2. SPLIT w_upload AT c_tab INTO w_dummyw_dummyw_dummy w_dummyw_dummyw_dummy w_dummyw_dummyw_dummy w_header-ac_doc_no w_dummy. * end of insert @007 WHEN 4. SPLIT w_upload AT c_tab INTO w_dummyw_dummy w_dummyw_dummy lv_doc_date w_dummyw_dummy w_header-header_txt w_dummyw_dummy w_header-comp_code w_dummy. WHEN 6. SPLIT w_upload AT c_tab INTO w_dummyw_dummy w_dummyw_dummy lv_pstng_date w_dummyw_dummy w_header-ref_doc_no w_dummyw_dummy w_header-doc_type w_dummy. WHEN 8. SPLIT w_upload AT c_tab INTO w_dummyw_dummy w_dummyw_dummy lv_period w_dummyw_dummy g_jtype w_dummyw_dummy"@008 w_header1-curr "@008 w_dummy. "@008 WHEN 10. SPLIT w_upload AT c_tab INTO w_dummyw_dummy w_dummyw_dummy g_rdate w_dummyw_dummy w_header-reason_rev w_dummyw_dummy "@008 lv_kursf "@008 w_dummy. "@008 EXIT. ENDCASE. *Split line item data LOOP AT t_upload INTO w_upload FROM 14. CLEAR lv_account. SPLIT w_upload AT c_tab INTO w_postings-company lv_account w_postings-ttype w_postings-pkey lv_amount w_postings-ccent w_postings-prcent w_postings-description w_postings-assignment w_postings-tax_code w_postings-juridiction_code lv_aufnr w_postings-psa lv_plant w_postings-wbs_element lv_product w_postings-product_hie w_postings-meins w_postings-quantity w_postings-asgm w_postings-asgc w_postings-billing_type lv_customer lv_cust_hie2 w_postings-order_reason w_postings-dsd_return_rsn w_postings-reson1 w_postings-item_sales w_postings-sales_order w_postings-dist_channel w_postings-sales_org w_postings-division w_postings-shipping_type w_postings-partner_pc w_postings-item_category w_postings-trading_partner w_postings-market w_postings-sales_office w_postings-country w_postings-resp w_postings-value_date w_postings-trade_type w_postings-controlling_area w_postings-cost_object w_postings-cust_hie1 w_postings-cust_hie3 w_postings-cust_hie4 w_postings-cust_hie5 w_postings-cust_hie6 w_postings-cust_hie7 w_postings-ship_to_party w_postings-fin_plning_lvl w_postings-ext_ic_mod_std w_postings-vv160_intrprtation w_postings-vv161_intrprtation w_postings-crm_marketing w_dummy. w_postings-amount = lv_amount. * END OF @007 IF NOT ( w_postings-pkey IS INITIAL ). APPEND w_postings TO t_posting. ENDIF. CLEAR: w_postings, lv_aufnr,lv_cust_hie2, lv_customer, lv_product, lv_plant, lv_account, lv_amount. ENDLOOP. IF t_bukrs IS NOT INITIAL. DELETE ADJACENT DUPLICATES FROM t_bukrs COMPARING bukrs. *Fecth the currency for company codes SELECT bukrs waers FROM t001 INTO TABLE t_curr FOR ALL ENTRIES IN t_bukrs WHERE bukrs EQ t_bukrs-bukrs. ENDIF. IF t_curr IS NOT INITIAL. SORT t_curr BY bukrs. ENDIF. ENDFORM." SPLIT_INTO_RECORDS *&---------------------------------------------------------------------* *& Form VALIDATE_GL *&---------------------------------------------------------------------* FORM validate_gl. TYPES: BEGIN OF ltype_gl, bukrs TYPE bukrs, saknr TYPE saknr, xintb TYPE xintb, END OF ltype_gl. DATA: lt_postings_temp TYPE STANDARD TABLE OF type_item, lt_gl_flag TYPE STANDARD TABLE OF ltype_gl, lw_gl_flag TYPE ltype_gl, lv_msg TYPE char100, lw_message TYPE type_message, lv_counter TYPE posnr_acc. REFRESH: lt_postings_temp, lt_gl_flag. CLEAR: lw_gl_flag, lv_counter. lt_postings_temp = t_posting. * lt_postings_temp = t_posting. IF lt_postings_temp IS NOT INITIAL. SORT lt_postings_tempBY company account. DELETE ADJACENT DUPLICATES FROM lt_postings_temp COMPARING company account. SELECT "#EC CI_SGLSELECT bukrs saknr xintb FROM skb1 INTO TABLE lt_gl_flag FOR ALL ENTRIES IN lt_postings_temp WHERE bukrs = lt_postings_temp-company AND saknr = lt_postings_temp-account. ENDIF. **Intialize counter lv_counter = 1. LOOP AT t_posting INTO w_postings. READ TABLE lt_gl_flag INTO lw_gl_flag WITH KEY saknr = w_postings-account bukrs = w_postings-company. IF sy-subrc IS INITIAL. IF lw_gl_flag-xintb = c_x. CONCATENATE text-004 w_postings-account text-006 INTO lv_msg SEPARATED BY space. * Else display the error message for GL account. lw_message-mes_type = c_e. lw_message-text = lv_msg. APPEND lw_message TO t_message. CLEAR lw_message. g_error_flag = c_x. ELSE. PERFORM populate_bapi USING lv_counter. * Increase counter by one lv_counter = lv_counter + 1. ENDIF. ELSE. "error message lv_msg * Else display the error message for GL account. lw_message-mes_type = c_e. lw_message-text = lv_msg. APPEND lw_message TO t_message. CLEAR lw_message. g_error_flag = c_x. ENDIF. ENDLOOP. ENDFORM." VALIDATE_GL *&---------------------------------------------------------------------* *& Form POPULATE_BAPI *&---------------------------------------------------------------------* FORM populate_bapi USING rlv_counter TYPE posnr_acc. **Populate line item details IF w_postings-company NE w_header-comp_code. g_cross_flag = c_x. ENDIF. *Fill Credit line item data IF w_postings-pkey=50. w_item_gl-itemno_acc = rlv_counter. w_item_gl-comp_code = w_postings-company. w_item_gl-gl_account = w_postings-account. w_item_gl-pstng_date = w_header-pstng_date. w_item_gl-cs_trans_t = w_postings-ttype. w_item_gl-costcenter = w_postings-ccent. w_item_gl-profit_ctr = w_postings-prcent. w_item_gl-wbs_element = w_postings-wbs_element. w_item_gl-orderid = w_postings-internal_order. w_item_gl-item_text = w_postings-description. w_item_gl-alloc_nmbr = w_postings-assignment. w_item_gl-tax_code = w_postings-tax_code. w_item_gl-taxjurcode = w_postings-juridiction_code. w_item_gl-orderid = w_postings-internal_order. w_item_gl-base_uom = w_postings-meins. w_item_gl-quantity = w_postings-quantity. w_item_gl-trade_id = w_postings-trading_partner. w_item_gl-value_date = w_postings-value_date. APPEND w_item_gl TO t_item_gl. IF w_header2-kursf IS NOT INITIAL. * To fetch company code currency READ TABLE t_curr INTO w_curr WITH KEY bukrs = w_postings-company BINARY SEARCH. IF w_curr-waers NE w_header1-curr. w_item_curr-exch_rate = w_header2-kursf. ENDIF. ENDIF. w_item_curr-itemno_acc =rlv_counter. w_item_curr-curr_type = c_doc_curr. "Document Currency w_item_curr-currency = w_header1-curr. w_item_curr-amt_doccur =w_postings-amount * ( -1 ). APPEND w_item_curr TO t_item_curr. CLEAR w_item_curr. g_credit_total = g_credit_total + w_postings-amount. * Clear variables CLEAR: w_postings, w_item_curr, w_curr, w_item_gl. *Fill Debit line item data ELSEIF w_postings-pkey= 40. w_item_gl-itemno_acc = rlv_counter. w_item_gl-comp_code = w_postings-company. w_item_gl-gl_account = w_postings-account. w_item_gl-pstng_date = w_header-pstng_date. w_item_gl-cs_trans_t = w_postings-ttype. w_item_gl-costcenter = w_postings-ccent. w_item_gl-profit_ctr = w_postings-prcent. w_item_gl-wbs_element = w_postings-wbs_element. w_item_gl-orderid = w_postings-internal_order. w_item_gl-item_text = w_postings-description. w_item_gl-alloc_nmbr = w_postings-assignment. w_item_gl-tax_code = w_postings-tax_code. w_item_gl-taxjurcode = w_postings-juridiction_code. w_item_gl-orderid = w_postings-internal_order. w_item_gl-base_uom = w_postings-meins. w_item_gl-quantity = w_postings-quantity. w_item_gl-part_prctr = w_postings-partner_pc. w_item_gl-trade_id = w_postings-trading_partner. w_item_gl-value_date = w_postings-value_date. APPEND w_item_gl TO t_item_gl. IF w_header2-kursf IS NOT INITIAL. * To fetch company code currency READ TABLE t_curr INTO w_curr WITH KEY bukrs = w_postings-company BINARY SEARCH. IF w_curr-waers NE w_header1-curr. w_item_curr-exch_rate = w_header2-kursf. ENDIF. ENDIF. w_item_curr-itemno_acc =rlv_counter. w_item_curr-curr_type = c_doc_curr. "Document Currency w_item_curr-currency = w_header1-curr. w_item_curr-amt_doccur =w_postings-amount . APPEND w_item_curr TO t_item_curr. CLEAR w_item_curr. g_debit_total = g_debit_total + w_postings-amount. * Clear variables CLEAR: w_postings, w_item_curr, w_curr, w_item_gl. ENDIF. ENDFORM." POPULATE_BAPI *&---------------------------------------------------------------------* *&Form POST_DOCUMENT *&---------------------------------------------------------------------* FORM post_document. TYPES:BEGIN OF type_bkpf, bukrs TYPE bkpf-bukrs, belnr TYPE bkpf-belnr, gjahr TYPE bkpf-gjahr, END OF type_bkpf. *Local variable declarations DATA: lv_belnr TYPE belnr_d, lv_bukrs TYPE bukrs, lv_gjahr TYPE gjahr, lw_message TYPE type_message, lv_msg TYPE symsgv, lv_rdate TYPE sy-datum, lt_return TYPE STANDARD TABLE OF bapiret2, lw_return TYPE bapiret2, "#EC NEEDED lt_bkpf TYPE STANDARD TABLE OF type_bkpf, lw_bkpf TYPE type_bkpf, lv_bvorg TYPE bvor-bvorg. *Clear local work area and variables CLEAR: lw_message, lv_msg, lw_return, lw_bkpf. CLEAR: w_header-obj_type, w_header-obj_key, w_header-obj_sys. IF g_error_flag NE c_x. IF g_credit_total<> 0 AND g_debit_total<> 0. CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK' EXPORTING documentheader= w_header TABLES accountgl = t_item_gl currencyamount = t_item_curr return = t_return. If t_return is not initial. * Posting journal entries CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST' EXPORTING documentheader = w_header IMPORTING obj_type = g_obj_type obj_key = g_obj_key obj_sys = g_obj_sys TABLES accountgl = t_item_gl currencyamount = t_item_curr return = t_return. IF sy-subrc<> 0. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = c_x. ENDIF. Endif. ENDIF.
Copyright © 2024 DOKUMEN.SITE Inc.