Sunday, July 19, 2020

Posting Credit Note to Customer via FB75 (BAPI: BAPI_ACC_DOCUMENT_POST)

Posting Credit Note to Customer via FB75 (BAPI: BAPI_ACC_DOCUMENT_POST)

Sample Program for posting credit note via BAPI '
BAPI_ACC_DOCUMENT_POST' : to post credit note to customer and include tax line item also  per business place .
But the main focus is : tax system and rate should be maintained in FTXP , otherwise it would give you errror for tax, then go for BDC for FB75.
 
Note: use
CALCULATE_TAX_FROM_GROSSAMOUNT' : to calculate tax from gross amount that you are posting .
if you want to calculate tax on amount then then calculate_tax_from_net amount.

It's a sample program to upload any document in sap fi using BAPI_ACC_DOCUMENT_POST .


*INCLUDE zfr_crnotes_top.


*TYPES : BEGIN OF ty_data,
*          bukrs        TYPE bukrs,
*          kunnr        TYPE kunnr,
*          doc_date     TYPE budat,
*          posting_date TYPE budat,
*          amount       TYPE dmbtr,
*          tax_code     TYPE mwskz,
*          bupla        TYPE bupla,
*          text         TYPE char100,
*          assignment   TYPE vbeln,
*          inv_date     TYPE budat,
*          gl_account   TYPE hkont,
*          amount1      TYPE amount,
*          assignment1  TYPE vbeln,
*          text1        TYPE char100,
*          kostl        TYPE kostl,
*          prctr        TYPE vbrp-prctr,
*        END OF ty_data.
*
*TYPES: BEGIN OF ty_inv,
*         inv_no   TYPE vbrp-vbeln,
*         inv_item TYPE vbrp-posnr,
*         inv_qty  TYPE vbrp-fkimg,
*         matnr    TYPE vbrp-matnr,
*         charg    TYPE vbrp-charg,
*         inv_date TYPE vbrk-fkdat,
*         gsber    TYPE vbrp-gsber,
*         prctr    TYPE vbrp-prctr,
*         knumv    TYPE vbrk-knumv,
*       END OF ty_inv,
*
*       BEGIN OF ty_konv,
*         knumv TYPE konv-knumv,
*         kposn TYPE konv-kposn,
*         kwert TYPE konv-kwert,
*       END OF ty_konv.

*data :  lv_counter TYPE char4,
*        lv_taxcounter TYPE char4.
**  * Local Data Declaration.
*
*DATA: lo_grid       TYPE REF TO cl_gui_alv_grid,
**      lt_data   TYPE STANDARD TABLE OF ty_data,
*      lt_ret        TYPE STANDARD TABLE OF bapiret2,
*      lt_cust       TYPE STANDARD TABLE OF bapiacar09,
*      lt_curr       TYPE STANDARD TABLE OF bapiaccr09,
*      lt_accgl      TYPE STANDARD TABLE OF bapiacgl09,
**        lt_cr     TYPE STANDARD TABLE OF zfi_cr_note,
**        lt_zrate  TYPE STANDARD TABLE OF zfi_rate_upload,
*      lw_output     TYPE ty_data,
*      lw_head       TYPE bapiache09,
*      lw_accgl      TYPE bapiacgl09,
*      lw_curr       TYPE bapiaccr09,
*      lw_cust       TYPE bapiacar09,
*      lw_ret        TYPE bapiret2,
*      lw_data       TYPE ty_data,
*      lt_data       TYPE TABLE OF ty_data,
*      lt_raw        TYPE truxs_t_text_data,
**        lw_cr     TYPE zfi_cr_note,
**        lw_zrate  TYPE zfi_rate_upload,
*      lv_amt        TYPE dmbtr,
*      lv_index      TYPE i,
*      lv_vbeln      TYPE bapiache09-obj_key,
*      lv_fwnav      TYPE bset-fwste,
*      lv_fwnvv      TYPE bset-fwste,
*      lv_fwste      TYPE bset-fwste,
*      lv_fwast      TYPE bset-fwste,
*      ls_accountwt  TYPE bapiacwt09,
*      lt_accountwt  TYPE STANDARD TABLE OF bapiacwt09,
*      lt_mwdat      TYPE STANDARD TABLE OF rtax1u15,
*      ls_accounttax TYPE bapiactx09,
*      lt_accounttax TYPE STANDARD TABLE OF bapiactx09,
*      lv_taxamount  TYPE dmbtr,
*      lv_year  TYPE char4.
*
*DATA obj_type TYPE awtyp.
*DATA obj_key  TYPE awkey.
*DATA obj_sys  TYPE awsys.
*
*SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
*PARAMETERS: filename TYPE rlgrap-filename OBLIGATORY.
*
*SELECTION-SCREEN END OF BLOCK b1.
*
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR filename.
*
*  CALL FUNCTION 'F4_FILENAME'
*    EXPORTING
*      field_name = 'filename'
*    IMPORTING
*      file_name  = filename.

*
*--------end of top declaration part ----------------------------*
*--------main progam begins ----------------------------------*
*INITIALIZATION.
*
*START-OF-SELECTION.
*
*  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
*    EXPORTING
*      i_line_header        = 'X'
*      i_tab_raw_data       = lt_raw
*      i_filename           = filename
*    TABLES
*      i_tab_converted_data = lt_data
*    EXCEPTIONS
*      conversion_failed    = 1
*      OTHERS               = 2.
*
*
*
*
*
*  LOOP AT  lt_data INTO lw_data.
** Header Details
*    lw_head-comp_code  = lw_data-bukrs.
*    lw_head-username   = sy-uname.
*    lw_head-pstng_date = lw_data-posting_date.
*    lw_head-doc_date   = lw_data-doc_date.
*    lw_head-header_txt = lw_data-inv_date.
*    lw_head-doc_type   = 'DG'.
*    lw_head-ref_doc_no = lw_data-assignment.
*
**    CLEAR lv_year.
**
**    CALL FUNCTION 'GM_GET_FISCAL_YEAR'
**      EXPORTING
**        i_date                     = lw_data-posting_date
**        i_fyv                      = 'V3'
**      IMPORTING
**        e_fy                       = lv_year
**      EXCEPTIONS
**        fiscal_year_does_not_exist = 1
**        not_defined_for_date       = 2
**        OTHERS                     = 3.
**
**    IF sy-subrc = 0.
**      lw_head-fisc_year = lv_year.
**    ELSE.
**      MESSAGE e045(zfi).
**    ENDIF.
*
*
**Receivable Details
*    lv_counter = lv_counter + 1 .
*    lw_cust-itemno_acc = lv_counter.
*    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
*      EXPORTING
*        input  = lw_data-kunnr "gl account
*      IMPORTING
*        output = lw_data-kunnr. "gl account
*
*    lw_cust-customer      = lw_data-kunnr. "Customer code
*    lw_cust-comp_code     = lw_data-bukrs. "Customer code
**    lw_cust-c_ctr_area    = '1000'.      "credit control area
*    lw_cust-gl_account    = '0000202700'.    "GL for debitors
*    lw_cust-alloc_nmbr    = lw_data-assignment1.
*    lw_cust-tax_code      = lw_data-tax_code.
*    lw_cust-businessplace = lw_data-bupla.
*
*    APPEND lw_cust TO lt_cust.
*    CLEAR: lw_cust.
*
*** Receivable Total value
*    lw_curr-itemno_acc = lv_counter.
**    lw_curr-curr_type = '00'.
*    lw_curr-currency   = 'INR'.
*    lw_curr-amt_doccur = lw_data-amount * -1.  "amount
*    lw_curr-amt_base   = lv_taxamount .
*    APPEND lw_curr TO lt_curr.
*    CLEAR: lw_curr.
*    CLEAR : lv_taxamount.
*
** Fill Item details
*    lv_counter = lv_counter + 1 .
*    lw_accgl-itemno_acc = lv_counter.
*    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
*      EXPORTING
*        input  = lw_data-gl_account "gl account
*      IMPORTING
*        output = lw_data-gl_account. "gl account
*
*    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
*      EXPORTING
*        input  = lw_data-prctr "gl account
*      IMPORTING
*        output = lw_data-prctr. "gl account
*
*    lw_accgl-gl_account = lw_data-gl_account. "gl account
*    lw_accgl-profit_ctr = lw_data-prctr.  "profit center
**    lw_accgl-de_cre_ind = 'S'.
*    lw_accgl-doc_type   = 'DG'.
*    lw_accgl-costcenter = lw_data-kostl.
*    lw_accgl-alloc_nmbr = lw_data-assignment .
*    lw_accgl-item_text  = lw_data-text.  "item txt
**    lw_accgl-tax_code   = lw_data-tax_code.  "item txt
*    APPEND lw_accgl TO lt_accgl.
*    CLEAR: lw_accgl.
**                            doc_values-amt_doccur = ls_mwdat-wmwst.
**                            doc_values- = <lfs_komv>-kbetr.
*
**    *    ** G/L 2
*    lw_curr-itemno_acc = lv_counter.
**    lw_curr-curr_type = '00'.
*    lw_curr-currency   = 'INR'.
*    lw_curr-amt_doccur = lw_data-amount.
*    lw_curr-amt_base   = lv_taxamount.
*    APPEND lw_curr TO lt_curr.
*    CLEAR: lw_curr.
*
*    CALL FUNCTION 'CALCULATE_TAX_FROM_GROSSAMOUNT'
*      EXPORTING
*        i_bukrs                 = lw_data-bukrs
*        i_mwskz                 = lw_data-tax_code
**       I_TXJCD                 = ' '
*        i_waers                 = 'INR'
*        i_wrbtr                 = lw_data-amount
*      IMPORTING
*        e_fwnav                 = lv_fwnav
*        e_fwnvv                 = lv_fwnvv
*        e_fwste                 = lv_fwste
*        e_fwast                 = lv_fwast
*      TABLES
*        t_mwdat                 = lt_mwdat
*      EXCEPTIONS
*        bukrs_not_found         = 1
*        country_not_found       = 2
*        mwskz_not_defined       = 3
*        mwskz_not_valid         = 4
*        account_not_found       = 5
*        different_discount_base = 6
*        different_tax_base      = 7
*        txjcd_not_valid         = 8
*        not_found               = 9
*        ktosl_not_found         = 10
*        kalsm_not_found         = 11
*        parameter_error         = 12
*        knumh_not_found         = 13
*        kschl_not_found         = 14
*        unknown_error           = 15
*        OTHERS                  = 16.
*    IF lt_mwdat IS NOT INITIAL.
*      lv_taxcounter = lv_counter .
*      LOOP AT lt_mwdat INTO DATA(ls_mwdat).
*        lv_counter = lv_counter + 1.
*        ls_accounttax-itemno_acc = lv_counter .
**        *                   Fetch GL Account
*        SELECT SINGLE konts INTO ls_accounttax-gl_account FROM j_1it030k
*                                               WHERE ktosl = ls_mwdat-ktosl
*                                               AND   mwskz = lw_data-tax_code
*                                               AND   bupla = lw_data-bupla.
*        ls_accounttax-cond_key   = ls_mwdat-kschl.
*        ls_accounttax-acct_key   = ls_mwdat-ktosl.
*        ls_accounttax-tax_code   = lw_data-tax_code.
*        ls_accounttax-tax_rate   = ls_mwdat-msatz.
**        ls_accounttax-itemno_tax = '01'.
*        ls_accounttax-itemno_tax = lv_taxcounter.
**        ls_accounttax-direct_tax = abap_true.
*        lv_taxamount = lv_taxamount + ls_mwdat-wmwst.
*        APPEND ls_accounttax TO lt_accounttax.
**        lw_curr-itemno_acc = '3'.
*        lw_curr-itemno_acc = lv_taxcounter.
**        lw_curr-curr_type = '00'.
*        lw_curr-currency   = 'INR'.
*        lw_curr-amt_doccur = ls_mwdat-wmwst .
*        lw_curr-amt_base   = ls_mwdat-kawrt.
*        APPEND lw_curr TO lt_curr.
*        CLEAR: lw_curr.
*        CLEAR : ls_accounttax, ls_mwdat.
**        ** FOR TAXES
*      ENDLOOP.
*    ENDIF.
*
*
*
*    CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK'
*      EXPORTING
*        documentheader    = lw_head
*      TABLES
*        accountgl         = lt_accgl
*        accountreceivable = lt_cust                            
**       accountpayable    = lt_payable
*        accounttax        = lt_accounttax
*        currencyamount    = lt_curr
*        return            = lt_ret.
**      extension2        = lt_extension2.
*
*
*
*    CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
*      EXPORTING
*        documentheader    = lw_head
*      IMPORTING
*        obj_type          = obj_type
*        obj_key           = obj_key
*        obj_sys           = obj_sys
*      TABLES
*        accountgl         = lt_accgl
*        accountreceivable = lt_cust
*        currencyamount    = lt_curr
*        accounttax        = lt_accounttax
*        return            = lt_ret.
*
*    READ TABLE lt_ret INTO lw_ret
*      WITH KEY type = 'E'.
*    IF sy-subrc NE 0.
*      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*        EXPORTING
*          wait = abap_true.
*    ENDIF.
*  ENDLOOP.