Help on BAPI_ACC_DOCUMENT_POST and BAPI_ACC_DOCUMENT_POST


Here our requirement is to Post New Invoice Document (with new document type- to get different number range) with all the other information (Vendor / GL / PO amount etc) from an existing parked invoice document. Park Document includes both categories of invoice document (PO invoice and Non-PO invoice). Though requirement is a bit specific this program will help you to get an idea of the parameters need to be fed to the above mentioned BAPIs.

  • BAPI_ACC_DOCUMENT_POST : For Non-PO invoice
  • BAPI_INCOMINGINVOICE_CREATE : For PO Invoice (The same BAPI can be used for Parking only)


*&---------------------------------------------------------------------*
*& Report name          : ZFIAPCC0001_POST_DOCUMENT
*& Program name         : ZFIAPCC0001_POST_DOCUMENT
*& Coded by             : Achirangshu De)
*& Date                 : February 2009
*& Purpose              : Post new FI document with new Document Type
*&                        from a previously Parked document
*&---------------------------------------------------------------------*

************************************************************************
REPORT  zfiapcc0001_post_document
  
NO STANDARD PAGE HEADING
  
LINE-SIZE 255
  
LINE-COUNT 65
  
MESSAGE-ID zfi.

************************************************************************
* D A T A   D E F I N I T I O N
************************************************************************
************************************************************************
* C O N S T A N T S
************************************************************************
CONSTANTS:
  gc_blart  
LIKE vbkpf-blart   VALUE 'KE'.

************************************************************************
* S E L E C T I O N   S C R E E N
************************************************************************
SELECTION-
SCREENBEGIN OF BLOCK blk1 WITH FRAME TITLE text-006.
PARAMETERS: p_bukrs LIKE vbkpf-bukrs,
            p_belnr 
LIKE vbkpf-belnr,
            p_gjahr 
LIKE vbkpf-gjahr.
SELECTION-
SCREENEND OF BLOCK blk1.

************************************************************************
* I N T E R N A L   S T R U C T U R E S   &   T A B L E S
************************************************************************
DATA:
  gs_vbkpf     
TYPE vbkpf ,
  gt_vbsegs    
TYPE STANDARD TABLE OF vbsegs WITH HEADER LINE,
  gt_vbsegk    
TYPE STANDARD TABLE OF vbsegk WITH HEADER LINE,
*  gt_rseg   TYPE STANDARD TABLE OF rseg WITH HEADER LINE,
  gt_with_item 
TYPE STANDARD TABLE OF with_item WITH HEADER LINE,
  gt_mwdat     
TYPE STANDARD TABLE OF rtax1u15  WITH HEADER LINE,

***---- Tables related to Non-PO Invoice Posting------------*
  gs_header    
TYPE bapiache09,
  gt_acctgl    
TYPE STANDARD TABLE OF bapiacgl09 WITH HEADER LINE,
  gt_accpay    
TYPE STANDARD TABLE OF bapiacap09 WITH HEADER LINE,
  gt_amnt      
TYPE STANDARD TABLE OF bapiaccr09 WITH HEADER LINE,
  gt_tax_nonpo 
TYPE STANDARD TABLE OF bapiactx09 WITH HEADER LINE,
  gt_extn      
TYPE STANDARD TABLE OF bapiparex  WITH HEADER LINE,
  gt_wtax      
TYPE STANDARD TABLE OF bapiacwt09 WITH HEADER LINE,
  gt_return    
TYPE STANDARD TABLE OF bapiret2   WITH HEADER LINE,


***---- Tables related to PO Invoice Posting----------------*
  gs_inv_header_in 
TYPE bapi_incinv_detail_header,
  gt_item_in    
TYPE STANDARD TABLE OF bapi_incinv_detail_item        WITH HEADER LINE,
  gt_tax_in     
TYPE STANDARD TABLE OF bapi_incinv_detail_tax         WITH HEADER LINE,
  gt_vsplit_in  
TYPE STANDARD TABLE OF bapi_incinv_detail_vendorsplit WITH HEADER LINE,
  gt_withtax_in 
TYPE STANDARD TABLE OF bapi_incinv_detail_withtax     WITH HEADER LINE,

  gs_inv_header 
TYPE bapi_incinv_create_header,
  gt_item    
TYPE STANDARD TABLE OF bapi_incinv_create_item         WITH HEADER LINE,
  gt_tax     
TYPE STANDARD TABLE OF bapi_incinv_create_tax          WITH HEADER LINE,
  gt_withtax 
TYPE STANDARD TABLE OF bapi_incinv_create_withtax      WITH HEADER LINE,
  gt_vsplit  
TYPE STANDARD TABLE OF bapi_incinv_create_vendorsplit  WITH HEADER LINE.


************************************************************************
* V A R I A B L E S
************************************************************************
DATA: g_invoicedocnumber LIKE  bapi_incinv_fld-inv_doc_no,
      g_fiscalyear       
LIKE  bapi_incinv_fld-fisc_year,
      g_compcode         
LIKE  bkpf-bukrs.

*----PO Invoice ---------------*
DATA: g_flag_po TYPE c,    "Flag is set if its a PO invoice
      g_belnr 
LIKE bapi_incinv_fld-inv_doc_no,  "Incoming Invoice No.
      g_gjahr 
LIKE bapi_incinv_fld-fisc_year.   "Incoming Invoice - Fiscal Year


************************************************************************
* E V E N T   P R O C E S S I N G
************************************************************************
INITIALIZATION.

START-
OF-SELECTION.

  
PERFORM a100_get_data_from_db.
  
PERFORM a150_process_data.
  
PERFORM a200_post_document.

END-OF-SELECTION.

  
PERFORM a300_display_output.

TOP-
OF-PAGE.

************************************************************************
* S U B R O U T I N E S
************************************************************************
*&---------------------------------------------------------------------*
*&      Form  A100_GET_DATA_FROM_DB
*&---------------------------------------------------------------------*
*       Get data from Database
*----------------------------------------------------------------------*

FORM a100_get_data_from_db .

***--- Select Header Information of Parked Document-----*
  
SELECT SINGLE * FROM vbkpf
           
INTO gs_vbkpf
           
WHERE bukrs EQ p_bukrs
             
AND belnr EQ p_belnr
             
AND gjahr EQ p_gjahr.

  
IF sy-subrc EQ 0.

***-----------------------------------------------------***
*** In case of PO Invoice , there will be no entries in ***
*** VBSEGS table as there is no line in that parked     ***
*** document. To get the PO related information we need ***
*** to search through RSEG Table.                       ***
***-----------------------------------------------------***
***--- PO Invoice -------*
    
IF gs_vbkpf-awtyp EQ 'RMRP' AND
       gs_vbkpf-awkey 
IS NOT INITIAL.

      g_flag_po = 
'X'.

      g_belnr = gs_vbkpf-awkey+
0(10) .
      g_gjahr = gs_vbkpf-awkey+
10(4) .

    
ELSE.

***--- Non-PO Invoice -------*
*---Vendor Line----------*
      
SELECT * FROM vbsegk
           
INTO TABLE gt_vbsegk
           
WHERE bukrs EQ p_bukrs
             
AND belnr EQ p_belnr
             
AND gjahr EQ p_gjahr.

*---GL Line----------*
      
SELECT * FROM vbsegs
               
INTO TABLE gt_vbsegs
               
WHERE bukrs EQ p_bukrs
                 
AND belnr EQ p_belnr
                 
AND gjahr EQ p_gjahr.

*---With Holding Tax ---------*
      
SELECT * FROM with_item
               
INTO TABLE gt_with_item
               
WHERE bukrs EQ p_bukrs
                 
AND belnr EQ p_belnr
                 
AND gjahr EQ p_gjahr.

    
ENDIF.

  
ELSE.
*-- Error Message--------*
    
MESSAGE s000 WITH 'Invalid Accounting Document !'(001)  DISPLAY LIKE 'E'.
    
LEAVE LIST-PROCESSING.

  
ENDIF.


ENDFORM.                    " A100_GET_DATA_FROM_DB
*&---------------------------------------------------------------------*
*&      Form  A150_PROCESS_DATA
*&---------------------------------------------------------------------*
*       Process Data for BAPI
*----------------------------------------------------------------------*

FORM a150_process_data .

  
DATA: l_itemno(3)      TYPE n,
        l_itemno_hold(
3TYPE n,
        l_wrbtr          
LIKE vbsegs-wrbtr.


  
IF g_flag_po EQ 'X'"PO Invoice

**** PO INVOICE ***************************************

    
CALL FUNCTION 'BAPI_INCOMINGINVOICE_GETDETAIL'
      
EXPORTING
        invoicedocnumber          = g_belnr
        fiscalyear                = g_gjahr
     
IMPORTING
        headerdata                = gs_inv_header_in
*       ADDRESSDATA               =
      
TABLES
        itemdata                  = gt_item_in
*       ACCOUNTINGDATA            =
*       GLACCOUNTDATA             =
*       MATERIALDATA              =
        taxdata                   = gt_tax_in
        withtaxdata               = gt_withtax_in
        vendoritemsplitdata       = gt_vsplit_in
        
return                    = gt_return
              .

    
IF gt_return[] IS INITIAL.
      
MOVE-CORRESPONDING gs_inv_header_in TO gs_inv_header.
      gs_inv_header-doc_type     = gc_blart .
      gs_inv_header-header_txt   = p_belnr.   
"Storing Referrence of Old Doc
      gs_inv_header-gross_amount = gs_inv_header_in-gross_amnt.
      
CLEAR gs_inv_header-inv_doc_no.


      
LOOP AT gt_item_in.
        
MOVE-CORRESPONDING gt_item_in TO gt_item.
        
APPEND gt_item.
      
ENDLOOP.

      
IF gs_inv_header-calc_tax_ind NE 'X'.
        
LOOP AT gt_tax_in.
          
MOVE-CORRESPONDING gt_tax_in TO gt_tax.
          
APPEND gt_tax.
        
ENDLOOP.
      
ENDIF.

      
LOOP AT gt_vsplit_in.
        
MOVE-CORRESPONDING gt_vsplit_in TO gt_vsplit.
        
APPEND gt_vsplit.
      
ENDLOOP.

      
LOOP AT gt_withtax_in.
        
MOVE-CORRESPONDING gt_withtax_in TO gt_withtax.
        
APPEND gt_withtax.
      
ENDLOOP.


    
ENDIF.


  
ELSE.

**** NON-PO INVOICE ***************************************

***---- Header ----------***
*  gs_header-obj_type     = 'RMRP'. " 'RFBU'.  'BKPFF'.
*  gs_header-OBJ_KEY      = gs_vbkpf-awkey. "l_belnr .
*  CONCATENATE sy-sysid 'CLNT' sy-mandt INTO gs_header-obj_sys.

    gs_header-bus_act     = 
'RMRP'"'RFBU'. "Invoicing : RMRP
    gs_header-username    = gs_vbkpf-usnam.
    gs_header-header_txt  = p_belnr.   
"Referrence of Old Doc
    gs_header-comp_code   = gs_vbkpf-bukrs.
    gs_header-doc_date    = gs_vbkpf-bldat.
    gs_header-pstng_date  = gs_vbkpf-budat.
*gs_header-TRANS_DATE =
    gs_header-fisc_year   = gs_vbkpf-gjahr.
    gs_header-fis_period  = gs_vbkpf-monat.
    gs_header-doc_type    = gc_blart .
    gs_header-ref_doc_no  = gs_vbkpf-xblnr.

***----- Vendor Line --------***
    
LOOP AT gt_vbsegk.

      l_itemno = l_itemno + 
10 .

      gt_accpay-itemno_acc  = l_itemno.
      gt_accpay-vendor_no   = gt_vbsegk-lifnr.
      gt_accpay-bus_area    = gt_vbsegk-gsber.
      gt_accpay-pmnttrms    = gt_vbsegk-zterm.
      gt_accpay-pymt_meth   = gt_vbsegk-zlsch.
      gt_accpay-pmnt_block  = gt_vbsegk-zlspr.
      gt_accpay-tax_code    = gt_vbsegk-mwskz.
      gt_accpay-item_text   = gt_vbsegk-sgtxt.

      
APPEND gt_accpay.

      
IF gt_vbsegk-shkzg EQ 'H'.
        gt_vbsegk-wrbtr = ( -
1 ) * gt_vbsegk-wrbtr.
        gt_vbsegk-wmwst = ( -
1 ) * gt_vbsegk-wmwst.
      
ENDIF.

      
CLEAR gt_amnt.
      gt_amnt-itemno_acc = l_itemno.
      gt_amnt-curr_type  = 
'00' . "Document Currency
      gt_amnt-
currency   = gs_vbkpf-waers.  " gt_vbsegk-swaer.
      gt_amnt-amt_doccur = gt_vbsegk-wrbtr.
      gt_amnt-tax_amt    = gt_vbsegk-wmwst.

      
APPEND gt_amnt.

***---- With Holding Tax----------------*
      
LOOP AT gt_with_item WHERE  bukrs EQ gt_vbsegk-bukrs AND
                                  belnr 
EQ gt_vbsegk-belnr AND
                                  gjahr 
EQ gt_vbsegk-gjahr AND
                                  buzei 
EQ gt_vbsegk-buzei.

        gt_wtax-itemno_acc = l_itemno.
        gt_wtax-wt_type    = gt_with_item-witht.
        gt_wtax-wt_code    = gt_with_item-wt_withcd.
        
APPEND gt_wtax.
      
ENDLOOP.


    
ENDLOOP.

***----- GL Lines --------***
    
LOOP AT gt_vbsegs.
      l_itemno = l_itemno + 
10 .

      gt_acctgl-itemno_acc = l_itemno .
      gt_acctgl-gl_account = gt_vbsegs-saknr.
      gt_acctgl-item_text  = gt_vbsegs-sgtxt.
      gt_acctgl-acct_type  = gt_vbsegs-koart.
      gt_acctgl-bus_area   = gt_vbsegs-gsber.
      gt_acctgl-plant      = gt_vbsegs-werks.
      gt_acctgl-tax_code   = gt_vbsegs-mwskz.
      gt_acctgl-taxjurcode = gt_vbsegs-txjcd.
      gt_acctgl-costcenter = gt_vbsegs-kostl.
*    gt_acctgl-acttype     = gt_vbsegs-lstar.
*    gt_acctgl-profit_ctr  = gt_vbsegs-prctr.
*    gt_acctgl-wbs_element = gt_vbsegs-ps_psp_pnr.

      
APPEND gt_acctgl.

***----- Populate Custom Fields (Location - ZZLOCA) if required for GL Line-------*
      gt_extn-valuepart1 = l_itemno.
      gt_extn-valuepart3 = gt_vbsegs-zzloca.

      
APPEND gt_extn.

      
IF gt_vbsegs-shkzg EQ 'H'.
        gt_vbsegs-wrbtr = ( -
1 ) * gt_vbsegs-wrbtr.
      
ENDIF.

      l_wrbtr = gt_vbsegs-wrbtr.
      l_itemno_hold = l_itemno.

      
IF gs_vbkpf-xmwst EQ 'X'.
*---- Calculate Tax-------------------*

        
CALL FUNCTION 'CALCULATE_TAX_FROM_GROSSAMOUNT'  "'CALCULATE_TAX_FROM_NET_AMOUNT'
          
EXPORTING
            i_bukrs                 = p_bukrs
            i_mwskz                 = gt_vbsegs-mwskz
            i_txjcd                 = gt_vbsegs-txjcd
            i_waers                 = gs_vbkpf-waers
            i_wrbtr                 = gt_vbsegs-wrbtr
*         I_ZBD1P                 = 0
*         I_PRSDT                 =
*         I_PROTOKOLL             =
*         I_TAXPS                 =
*         I_ACCNT_EXT             =
*         I_ACCDATA               =
*       IMPORTING
*         E_FWNAV                 =
*         E_FWNVV                 =
*         E_FWSTE                 =
*         E_FWAST                 =
          
TABLES
            t_mwdat                 = gt_mwdat
         
EXCEPTIONS
           bukrs_not_found         = 
1
           country_not_found       = 
2
           mwskz_not_defined       = 
3
           mwskz_not_valid         = 
4
           ktosl_not_found         = 
5
           kalsm_not_found         = 
6
           parameter_error         = 
7
           knumh_not_found         = 
8
           kschl_not_found         = 
9
           unknown_error           = 
10
           account_not_found       = 
11
           txjcd_not_valid         = 
12
           
OTHERS                  = 13
                  .
        
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
        
ENDIF.


        
IF gt_mwdat[] IS NOT INITIAL.

          
CLEAR l_wrbtr.

          
LOOP AT gt_mwdat.

            l_wrbtr = l_wrbtr + gt_mwdat-kawrt.

            
CLEAR gt_amnt.

            l_itemno = l_itemno + 
10.
            gt_amnt-itemno_acc = l_itemno.
            gt_amnt-curr_type  = 
'00' . "Company Code Currency
            gt_amnt-
currency   = gs_vbkpf-waers.
            gt_amnt-amt_doccur = gt_mwdat-wmwst.
*        gt_amnt-tax_amt    = gt_mwdat-wmwst.
            gt_amnt-amt_base   = gt_mwdat-kawrt.
            
APPEND gt_amnt.

            gt_tax_nonpo-itemno_acc = l_itemno.
*       gt_tax_nonpo-GL_ACCOUNT = gt_vbsegs-saknr.
            gt_tax_nonpo-cond_key   = gt_mwdat-kschl.
            gt_tax_nonpo-acct_key   = gt_mwdat-ktosl.
            gt_tax_nonpo-tax_code   = gt_vbsegs-mwskz.
*       TAX_RATE
*       TAX_DATE
*       TAXJURCODE
            
APPEND gt_tax_nonpo.

          
ENDLOOP.

        
ENDIF.
      
ENDIF.

*----- GL Line Amount -----------*
      
CLEAR gt_amnt.
      gt_amnt-itemno_acc = l_itemno_hold.
      gt_amnt-curr_type  = 
'00' . "Company Code Currency
      gt_amnt-
currency   = gs_vbkpf-waers.   "gt_vbsegs-swaer.
      gt_amnt-amt_doccur = l_wrbtr.

      
APPEND gt_amnt.

    
ENDLOOP.

  
ENDIF.

ENDFORM.                    " A150_PROCESS_DATA
*&---------------------------------------------------------------------*
*&      Form  A200_POST_DOCUMENT
*&---------------------------------------------------------------------*
*       Post Park Document
*----------------------------------------------------------------------*

FORM a200_post_document .

  
DATA: l_obj_type LIKE bapiache09-obj_type ,
        l_obj_key  
LIKE bapiache09-obj_key.


  
IF g_flag_po EQ 'X'.

***--- PO Invoice ----------------*

    
CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE'
      
EXPORTING
        headerdata                = gs_inv_header
*       ADDRESSDATA               =
     
IMPORTING
        invoicedocnumber          = g_invoicedocnumber
        fiscalyear                = g_fiscalyear
      
TABLES
        itemdata                  = gt_item
*       ACCOUNTINGDATA            =
*       GLACCOUNTDATA             =
*       MATERIALDATA              =
        taxdata                   = gt_tax
        withtaxdata               = gt_withtax
        vendoritemsplitdata       = gt_vsplit
        
return                    = gt_return
              .

  
ELSE.
**********************************************************************

***----Non-PO Invoices--------------*

    
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
      
EXPORTING
        documentheader          = gs_header
*       CUSTOMERCPD             =
*       CONTRACTHEADER          =
   
IMPORTING
        obj_type                = l_obj_type
        obj_key                 = l_obj_key
*       OBJ_SYS                 =
      
TABLES
        accountgl               = gt_acctgl
*       accountreceivable       =
        accountpayable          = gt_accpay
        accounttax              = gt_tax_nonpo
        currencyamount          = gt_amnt
*       CRITERIA                =
*       VALUEFIELD              =
*       EXTENSION1              =
        
return                  = gt_return
*       PAYMENTCARD             =
*       CONTRACTITEM            =
        extension2              = gt_extn
*       REALESTATE              =
        accountwt               = gt_wtax
              .

    g_invoicedocnumber  = l_obj_key+
0(10).
    g_fiscalyear        = l_obj_key+
14(4).
    g_compcode          = l_obj_key+
10(4).



  
ENDIF.

***---- Commit Work -----------*
  
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
* EXPORTING
*   WAIT          =
* IMPORTING
*   RETURN        =
            .

ENDFORM.                    " A200_POST_DOCUMENT
*&---------------------------------------------------------------------*
*&      Form  A300_DISPLAY_OUTPUT
*&---------------------------------------------------------------------*
*       Display Output
*----------------------------------------------------------------------*
FORM a300_display_output .

  
WRITE:/ 'Document Number:'(002) , g_invoicedocnumber,
        / 
'Fiscal Year    :'(003) , g_fiscalyear      ,
        / 
'Company Code   :'(004) , g_compcode.

  
IF g_invoicedocnumber IS INITIAL OR
     g_invoicedocnumber 
EQ '$'.

    
SKIP 1.
    
WRITE:/ 'Other Messages'(005).

    
LOOP AT gt_return.
      
WRITE:/ gt_return-type,
              gt_return-
id,
              gt_return-
number,
              gt_return-
message.
*            gt_return-LOG_NO,
*            gt_return-LOG_MSG_NO,
*            gt_return-MESSAGE_V1,
*            gt_return-MESSAGE_V2,
*            gt_return-MESSAGE_V3,
*            gt_return-MESSAGE_V4.
    
ENDLOOP.
  
ENDIF.

ENDFORM.                    " A300_DISPLAY_OUTPUT

***  E N D   O F   R E P O R T  ****************************************


Comments

  1. It would be a better example if you did not use global variables so much. Globals should only be used where they cannot be avoided such as in screens.

    It's a lazy method of programming and prone to errors that are difficult to find and correct.

    Always, always pass your globals as parameters to the relevant subroutines. This also has the by-product of making the subroutines more re-usable.

    ReplyDelete
  2. Thanks for the tip. Yes, I agree. These are example programs, so tried to keep less lengthy for quick reference.

    ReplyDelete
  3. Do you know if the bapi: BAPI_ACC_DOCUMENT_POST perform the FI validations? Trans. OBBH

    ReplyDelete
  4. Actually, OBBH is the tran for Substitution rules, OB28 is the tran for Validation rules. We utilize both for our AP invoice postings. Do you know if BAPI_ACC_DOCUMENT_POST calls them?

    ReplyDelete
  5. Do you know maybe how to use this BAPI for deductible taxes, for example to create for tax-lines
    1. gl 100
    2. cr -100
    3. tax -10
    4. tax 10
    ...also called reverse charge

    thanx!!!!

    ReplyDelete
  6. Tableau data visualization Software
    SQIAR (http://www.sqiar.com/solutions/technology/tableau) is a leading Business Intelligence company and provides Tableau Software consultancy across United Kingdom and USA

    ReplyDelete

Post a Comment

Popular posts from this blog

ABAP Tips - 1 : How to Display a Popup and Get Values from User ?

Few Tips for Transport Request Manipulation