Post FI document using BAPI

...Previous

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.

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image