Wednesday 7 July 2021

Post Customer and Vendor using BAPI_ACC_DOCUMENT_POST ((40/50 for G/L Accounts, 31/21 for vendor sub-ledger,11/01 for Customer sub-ledger))

Introduction:

This blog post will describe the various steps involved to post a FI document using BAPI ‘BAPI_ACC_DOCUMENT_POST’ from an TXT file.

Requirement:

The TXT file should be balanced for the document to be posted i.e. credit amount and debit amount should be equal.

STEPS:

Steps involved in posting a document from a file are:

1. Data upload from TXT file into internal table using FM gui_upload.

2. Using BAPI ‘BAPI_ACC_DOCUMEN_CHECK’ to check the data for document posting and finally post the document using BAPI ‘BAPI_ACC_DOCUMEN_POST’ and commit the work using FM ‘BAPI_TRANSACTION_COMMIT. – Most Challenging task is to map the data in the bapi function module.

SAP ABAP Exam Prep, SAP ABAP Material, SAP ABAP Tutorial and Material, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Guides

ABAP code: Refer Attachment

REPORT  ZFIIM10002 NO STANDARD PAGE HEADING MESSAGE-ID ZFI.

*-----------------------------------------------------------------------

* T A B L E S

*-----------------------------------------------------------------------

*TABLES:

*-----------------------------------------------------------------------

* T Y P E S

*-----------------------------------------------------------------------

*TYPES:

*-----------------------------------------------------------------------

* D A T A

*-----------------------------------------------------------------------

*DATA:

*-> work areas

DATA : G_REMINDER TYPE I.

DATA : G_FILE TYPE STRING.

*-> counters

DATA : CNT_RECORDS      TYPE I,

       CNT_REC_TOT_FILE TYPE I,

       CNT_REC_TOT      TYPE I,

       CNT_SUCCESS_REC  TYPE I,

       CNT_FAIL_REC     TYPE I.

*-> internal tables

*Internal table for GL data from the input file

DATA: BEGIN OF TBL_GL OCCURS 0,

*        zbukrs LIKE zglcnvt-zbukrs,  "Legacy Company Id

        BSCHL LIKE BSEG-BSCHL,       "Posting Key

        HKONT LIKE ZGLCNVT-HKONT,    "GL Account

        WRBTR LIKE BSEG-WRBTR,      "Amount

        MWSKZ LIKE BSEG-MWSKZ,       " Tax code                   "DSL 87/307901+

        KOSTL LIKE ZGLCNVT-KOSTL,    "Cost Center

        PRCTR LIKE ZGLCNVT-PRCTR,    "Profit Center

        RMVCT TYPE RMVCT,            "Transaction Type               285970+

        AUFNR LIKE BSEG-AUFNR,       "Internal order                 3942+

        VBUND TYPE RASSC,            "Trading Partner NR1K952550+

* Added by DEVK906772 - Ernie Tan (Added SGTXT field)

        SGTXT LIKE BSEG-SGTXT,       "Item text

        LDGRP TYPE BKPF-LDGRP,       "Ledger group                 "DSL 87+

*        CUST  TYPE  CHAR1,      "customer

*        VEND  TYPE  CHAR1,      "Vendor

      END OF TBL_GL.

DATA : TBL_BDCTAB LIKE BDCDATA OCCURS 0 WITH HEADER LINE.

DATA : TBL_MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.

DATA: LT_RETURN   TYPE STANDARD TABLE OF BAPIRET2,

      LT_RETURN1  TYPE STANDARD TABLE OF BAPIRET2,

      LWA_RETURN  TYPE  BAPIRET2,

      LWA_RETURN1 TYPE  BAPIRET2.

* Internal table for GL data from the input file

DATA : BEGIN OF TBL_INPUT OCCURS 0,

         TEXT(255) TYPE C,

       END OF TBL_INPUT .

*-> variables

DATA LV_NOT TYPE CHAR1.                                     "3466+

*-> totals

DATA : G_TOTAL_AMT    LIKE BSEG-WRBTR,  "Total Amt

       G_DEBIT_TOTAL  LIKE BSEG-WRBTR,

       G_CREDIT_TOTAL LIKE BSEG-WRBTR.

*-> flags

DATA : FL_NEW_TRANSACTION(1) TYPE C,

       FL_SESSION(1)         TYPE C.

*-----------------------------------------------------------------------

* C O N S T A N T S

*-----------------------------------------------------------------------

CONSTANTS: C_X(1)            TYPE C VALUE 'X',    "flag

*Start of DSL 87+

*          c_fb01 LIKE tstc-tcode VALUE 'FB01', "transaction FB01

           GC_FB01L          TYPE TSTC-TCODE VALUE 'FB01L', "transaction FB01L

           GC_DOC_TY_SX(2)   TYPE C VALUE 'SX',   "document type

*End of DSL 87+

           C_SA_DOC_TYPE(2)  TYPE C VALUE 'SA',   "document type

           C_USD(3)          TYPE C VALUE 'USD',  "Currency

           C_COMP_CODE_M9(2) TYPE C VALUE 'M9', "company code

           C_BDC_SESS_GL(6)  TYPE C VALUE 'GL-ALL', "Error BDC session

           C_BDC_SESS_C(6)   TYPE C VALUE 'C-ALL', "Error BDC session

           C_BDC_SESS_V(6)   TYPE C VALUE 'V-ALL', "Error BDC session

           C_CHECK           TYPE CHAR1 VALUE 'X'.          "Constant for 'X'

*-----------------------------------------------------------------------

* F I E L D – S Y M B O L S

*-----------------------------------------------------------------------

*FIELD-SYMBOLS:

*-----------------------------------------------------------------------

* F I E L D – G R O U P S

*-----------------------------------------------------------------------

*FIELD-GROUPS:

*-----------------------------------------------------------------------

* P A R A M E T E R S   &   S E L E C T - O P T I O N S

*-----------------------------------------------------------------------

SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME TITLE TITLE.

PARAMETERS : P_RAD2 RADIOBUTTON GROUP RB1 USER-COMMAND UC1.

PARAMETERS : P_RAD3 RADIOBUTTON GROUP RB1.

SELECTION-SCREEN END OF BLOCK B1.

SELECTION-SCREEN: BEGIN OF BLOCK B2 WITH FRAME.

*PARAMETERS: pfile_gl LIKE filename-fileextern OBLIGATORY.

PARAMETERS: PFILE_GL LIKE IBIPPARMS-PATH OBLIGATORY.

PARAMETERS: P_BUDAT  LIKE BKPF-BUDAT OBLIGATORY,   "Posting Date

            P_BUKRS  LIKE BKPF-BUKRS OBLIGATORY,   "Company Code

            P_WAERS  LIKE BKPF-WAERS OBLIGATORY,   "Currency Key

            P_XBLNR  LIKE BKPF-XBLNR OBLIGATORY MODIF ID IN1 ,   "Reference

            P_GROUP1 LIKE APQI-GROUPID OBLIGATORY MODIF ID IN1 . "BDC Error session

SELECTION-SCREEN: END OF BLOCK B2.

*-----------------------------------------------------------------------

* I N I T I A L I Z A T I O N

*-----------------------------------------------------------------------

INITIALIZATION.

  TITLE = ' Accounts'.

  PFILE_GL = 'C:\data\gl_all.txt'.

  P_BUDAT  = SY-DATUM.

  P_BUKRS  = C_COMP_CODE_M9.

  P_WAERS  = C_USD.

  P_XBLNR  = 'GL-ALL'.

  P_GROUP1 = C_BDC_SESS_GL.

AT SELECTION-SCREEN OUTPUT.

  LOOP AT SCREEN.

    IF P_RAD2 = 'X'  . " If check box is selected

      IF SCREEN-GROUP1 = 'IN1' .

        SCREEN-ACTIVE = 0.

*        PFILE_GL = 'C:\data\ar_all.txt'.

        MODIFY SCREEN.

      ENDIF.

    ELSEIF P_RAD3 = 'X'.

      IF SCREEN-GROUP1 = 'IN1' .

        SCREEN-ACTIVE = 0.

*        PFILE_GL = 'C:\data\ap_all.txt'.

        MODIFY SCREEN.

      ENDIF.

    ENDIF.

  ENDLOOP.

*-----------------------------------------------------------------------

* A T   S E L E C T I O N - S C R E E N

*-----------------------------------------------------------------------

AT SELECTION-SCREEN ON VALUE-REQUEST FOR PFILE_GL.

  CALL FUNCTION 'F4_FILENAME'

    EXPORTING

*     program_name  = syst-cprog

*     dynpro_number = syst-dynnr

      FIELD_NAME = 'PFILE_GL'

    IMPORTING

      FILE_NAME  = PFILE_GL.

* Validate user input on selection screen

  PERFORM VALIDATE_INPUT.

*-----------------------------------------------------------------------

* S T A R T - O F - S E L E C T I O N

*-----------------------------------------------------------------------

START-OF-SELECTION.

*Restriction specific to customer or vendor upload as AP & AR

  PERFORM AUTHORITY_CHECK.

*-> read data

  PERFORM READ_INPUT_FILE.

*-----------------------------------------------------------------------

* E N D - O F - S E L E C T I O N

*-----------------------------------------------------------------------

END-OF-SELECTION.

*-> process data

IF P_RAD2 EQ 'X'.

* Customer Posting

    PERFORM POST_CUST.

  ELSEIF P_RAD3 EQ 'X'.

* Vendor Posting

    PERFORM POST_VEND.

  ENDIF.

* Write report

  PERFORM WRITE_REPORT.

*-----------------------------------------------------------------------

* T O P - O F – P A G E

*-----------------------------------------------------------------------

TOP-OF-PAGE.

*-----------------------------------------------------------------------

* AT LINE-SELECTION

*-----------------------------------------------------------------------

AT LINE-SELECTION.

*-----------------------------------------------------------------------

* F O R M S

*----------------------------------------------------------------------

*&---------------------------------------------------------------------*

*&      Form  validate_input

*&---------------------------------------------------------------------*

*       Validate input on selection screen

*----------------------------------------------------------------------*

FORM VALIDATE_INPUT .

  DATA : L_BUKRS LIKE T001-BUKRS,

         L_WAERS LIKE TCURC-WAERS.

*Validate company code

  SELECT SINGLE BUKRS INTO L_BUKRS

     FROM T001

  WHERE BUKRS = P_BUKRS.

  IF SY-SUBRC NE 0.

    MESSAGE E001.

  ENDIF.

*Validate currency

  SELECT SINGLE WAERS INTO L_WAERS

     FROM TCURC

  WHERE WAERS = P_WAERS.

  IF SY-SUBRC NE 0.

    MESSAGE E017. "Invalid Currency

  ENDIF.

ENDFORM.                       " validate_input

*&---------------------------------------------------------------------*

*&      Form  read_input_file

*&---------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

FORM READ_INPUT_FILE .

* Read input file for payroll work

  PERFORM READ_INPUT_FILE_GL.

ENDFORM.                    " read_input_file

*&---------------------------------------------------------------------*

*&      Form  read_input_file_gl

*&---------------------------------------------------------------------*

*       Read the input file for GL

*----------------------------------------------------------------------*

FORM READ_INPUT_FILE_GL.

  DATA : L_TEXT(300) TYPE C,

         L_F1(2)     TYPE C,

         L_F2(10)    TYPE C,

         L_F3(16)    TYPE C,

         L_F3_1(2)   TYPE C,     "Tax code                          DSL 87/307901+

         L_F4(10)    TYPE C,

         L_F5(10)    TYPE C,

* Added by DEVK906772 - Ernie Tan (Added SGTXT field)

         L_F6(50)    TYPE C,

         L_F7(6)     TYPE C, "Trading Partner NR1K952550+

*Start of changes by lgajulapuri  NR2K962135 285970

*        l_f8(12) TYPE c. "Internal order number                    "3942+

         L_F8(12)    TYPE C, "Internal order number                    "3942+

         L_F9(3)     TYPE C,

         L_F10(4)    TYPE C.                                           "DSL 87+

*End of changes by lgajulapuri  NR2K962135 285970

*Begin of issue 3466

  DATA : LV_F3_PART1 TYPE CHAR16,

         LV_F3_PART2 TYPE CHAR2,

         LV_F3_LEN   TYPE I,

         LV_F3_LEN1  TYPE I.

*End of issue 3466

  CLASS : CL_ABAP_CHAR_UTILITIES  DEFINITION LOAD.

  G_FILE = PFILE_GL.

* Upload file from PC

  CALL FUNCTION 'GUI_UPLOAD'

    EXPORTING

      FILENAME                = G_FILE

      FILETYPE                = 'ASC'

    TABLES

      DATA_TAB                = TBL_INPUT

    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.

  IF SY-SUBRC <> 0.

    MESSAGE E019.  "File could not be opened

  ENDIF.

* populate internal table tbl_gl

  LOOP AT TBL_INPUT.

    SPLIT TBL_INPUT-TEXT AT CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB INTO

             L_F1

             L_F2

             L_F3

             L_F3_1 "Tax code                                       DSL 87/307901+

             L_F4

             L_F5

             L_F9                                           "285970+

             L_F8 "Internal order                                   "3942+

             L_F7 "Trading Partner NR1K952550+

* Added by DEVK906772 - Ernie Tan (Added SGTXT field)

             L_F6

             L_F10.                                                 "DSL 87+

    SHIFT L_F3 RIGHT DELETING TRAILING SPACE.

    SHIFT L_F3 RIGHT DELETING TRAILING

       CL_ABAP_CHAR_UTILITIES=>CR_LF.

*{   INSERT         NR1K920343                                        1

* Begin of change issue TM#1564.

*replace all occurrences of ',' in l_f3 with '.'.

* End of change issue TM#1564.

*}   INSERT

*Begin of issue 3466

    CONDENSE  L_F3 .

    LV_F3_LEN = STRLEN( L_F3 ).

    LV_F3_LEN1 = LV_F3_LEN - 2.

    LV_F3_PART2 = L_F3+LV_F3_LEN1(2).

    LV_F3_PART1 = L_F3+0(LV_F3_LEN1).

    REPLACE ALL OCCURRENCES OF ',' IN LV_F3_PART1 WITH ' '.

    REPLACE ALL OCCURRENCES OF '.' IN LV_F3_PART1 WITH ' '.

    CONDENSE LV_F3_PART1.

    CONCATENATE LV_F3_PART1 LV_F3_PART2 INTO L_F3 SEPARATED BY '.'.

*End of issue 3466

    TBL_GL-BSCHL = L_F1.

    TBL_GL-HKONT = L_F2.

    TBL_GL-WRBTR = L_F3.

    TBL_GL-MWSKZ = L_F3_1."Tax code                                 DSL 87/307901+

    TBL_GL-KOSTL = L_F4.

    TBL_GL-PRCTR = L_F5.

* Added by DEVK906772 - Ernie Tan (Added SGTXT field)

    TBL_GL-SGTXT = L_F6.

    TBL_GL-VBUND = L_F7. "Trading Parnter NR1K952550+

    TBL_GL-AUFNR = L_F8. "Internal order                            "3942+

    TBL_GL-RMVCT = L_F9. "Transaction Type                          "285970+

    TBL_GL-LDGRP = L_F10. "Ledger group                             "DSL 87+

    APPEND TBL_GL.

  ENDLOOP.

  DESCRIBE TABLE TBL_GL LINES CNT_REC_TOT_FILE.

ENDFORM.                    " read_input_file_gl

*&---------------------------------------------------------------------*

*&      Form  write_report

*&---------------------------------------------------------------------*

*       Write report

*----------------------------------------------------------------------*

FORM WRITE_REPORT.

  DATA : L_NUM_ERROR_REC TYPE I.

  IF P_RAD2 EQ 'X'.

    WRITE :/ 'Customer - ALL Posting:'.

    IF LT_RETURN[] IS NOT INITIAL.

      SKIP 3.

      LOOP AT LT_RETURN INTO LWA_RETURN.

        WRITE: LWA_RETURN-TYPE, LWA_RETURN-ID, LWA_RETURN-NUMBER, LWA_RETURN-MESSAGE.

        CLEAR LWA_RETURN.

      ENDLOOP.

    ENDIF.

    IF LT_RETURN1[] IS NOT INITIAL.

      SKIP 3.

      LOOP AT LT_RETURN1 INTO LWA_RETURN1.

        LWA_RETURN1-MESSAGE = 'Document posted successfully:'.

        CONCATENATE  LWA_RETURN1-MESSAGE LWA_RETURN1-MESSAGE_V2(10) LWA_RETURN1-MESSAGE_V2+10(2) INTO LWA_RETURN1-MESSAGE SEPARATED BY SPACE.

        WRITE: LWA_RETURN1-MESSAGE.

        CLEAR LWA_RETURN1.

      ENDLOOP.

    ENDIF.

  ELSEIF P_RAD3 EQ 'X'.

    WRITE :/ 'Vendor - ALL Posting:'.

    IF LT_RETURN[] IS NOT INITIAL.

      SKIP 3.

      LOOP AT LT_RETURN INTO LWA_RETURN.

        WRITE: LWA_RETURN-TYPE, LWA_RETURN-ID, LWA_RETURN-NUMBER, LWA_RETURN-MESSAGE.

        CLEAR LWA_RETURN.

      ENDLOOP.

    ENDIF.

    IF LT_RETURN1[] IS NOT INITIAL.

      SKIP 3.

      LOOP AT LT_RETURN1 INTO LWA_RETURN1.

        LWA_RETURN1-MESSAGE = 'Document posted successfully:'.

        CONCATENATE  LWA_RETURN1-MESSAGE LWA_RETURN1-MESSAGE_V2(10) LWA_RETURN1-MESSAGE_V2+10(2) INTO LWA_RETURN1-MESSAGE SEPARATED BY SPACE.

        WRITE: LWA_RETURN1-MESSAGE.

        CLEAR LWA_RETURN1.

      ENDLOOP.

    ENDIF.

  ENDIF.

ENDFORM.                    " write_report

*&---------------------------------------------------------------------*

*&      Form  BUILD_BDC_VDATA

*&---------------------------------------------------------------------*

*       Vendor posting

*----------------------------------------------------------------------*

*Vendor Posting

FORM POST_VEND.

  DATA : L_POSTING_KEY      LIKE RF05A-NEWBS,

         L_DOC_DATE         LIKE BKPF-BLDAT,

         L_POSTING_DATE     LIKE BKPF-BUDAT,

         L_AMOUNT(16)       TYPE C,

         LWA_BAPI_DOCHEADER TYPE BAPIACHE09,

         LV_TABIX           TYPE SY-TABIX,

         LT_BAPI_ACCTGL     TYPE STANDARD TABLE OF   BAPIACGL09,

         LWA_BAPI_ACCTGL    TYPE    BAPIACGL09,

         LT_BAPI_ACCTRCC    TYPE STANDARD TABLE OF BAPIACAP09,

         LWA_BAPI_ACCTRCC   TYPE  BAPIACAP09,

         LT_CURRENCYAMOUNT  TYPE STANDARD TABLE OF BAPIACCR09,

         LWA_CURRENCYAMOUNT TYPE  BAPIACCR09,

         LV_ITEM            TYPE I,

         LV_OBJKEY          TYPE BAPIACHE09-OBJ_KEY.

  CONSTANTS: LC_CHECK     TYPE CHAR1 VALUE 'X',

             LC_BAPIRET_S TYPE CHAR1 VALUE 'S'.

  CLEAR CNT_RECORDS.

  DESCRIBE TABLE TBL_GL LINES CNT_REC_TOT.

  L_DOC_DATE = SY-DATUM.

  LWA_BAPI_DOCHEADER-DOC_DATE = L_DOC_DATE.

  L_POSTING_DATE = P_BUDAT.

  LWA_BAPI_DOCHEADER-PSTNG_DATE = L_POSTING_DATE.

  IF TBL_GL-LDGRP NE SPACE.

    LWA_BAPI_DOCHEADER-DOC_TYPE = GC_DOC_TY_SX.

  ELSE.

    LWA_BAPI_DOCHEADER-DOC_TYPE = C_SA_DOC_TYPE.

  ENDIF.

  LWA_BAPI_DOCHEADER-COMP_CODE = P_BUKRS.

  LWA_BAPI_DOCHEADER-USERNAME = SY-UNAME.

**Populate line item details

  LV_ITEM  = 1.

  LOOP AT TBL_GL.

*Fill Credit line item data

    IF TBL_GL-BSCHL EQ '50'.

      LWA_BAPI_ACCTGL-ITEMNO_ACC = LV_ITEM.

      LWA_BAPI_ACCTGL-GL_ACCOUNT = TBL_GL-HKONT.      "GL Account

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

        EXPORTING

          INPUT  = LWA_BAPI_ACCTGL-GL_ACCOUNT

        IMPORTING

          OUTPUT = LWA_BAPI_ACCTGL-GL_ACCOUNT.

      LWA_BAPI_ACCTGL-COMP_CODE = P_BUKRS.       " Company code

      LWA_BAPI_ACCTGL-PSTNG_DATE = L_POSTING_DATE.

      IF TBL_GL-LDGRP NE SPACE.

        LWA_BAPI_ACCTGL-DOC_TYPE = GC_DOC_TY_SX.

      ELSE.

        LWA_BAPI_ACCTGL-DOC_TYPE = C_SA_DOC_TYPE.

      ENDIF.

      LWA_BAPI_ACCTGL-TAX_CODE = TBL_GL-MWSKZ.       " Tax code

      LWA_BAPI_ACCTGL-ITEM_TEXT = TBL_GL-SGTXT.     "Item text

      LWA_BAPI_ACCTGL-PROFIT_CTR = TBL_GL-PRCTR.     "Profit Center

      LWA_BAPI_ACCTGL-ORDERID = TBL_GL-AUFNR.       "Internal order

      LWA_BAPI_ACCTGL-COSTCENTER = TBL_GL-KOSTL.    "Cost Center

      LWA_BAPI_ACCTGL-CS_TRANS_T = TBL_GL-RMVCT.    "Transaction Type

      LWA_BAPI_ACCTGL-TRADE_ID = TBL_GL-VBUND.    "Trading Partner

      LWA_BAPI_ACCTGL-ACCT_TYPE = 'S'.    "Trading Partner

      APPEND LWA_BAPI_ACCTGL TO LT_BAPI_ACCTGL.

*      * Fill amounts

      CLEAR LWA_CURRENCYAMOUNT.

      LWA_CURRENCYAMOUNT-ITEMNO_ACC = LV_ITEM.

      LWA_CURRENCYAMOUNT-CURRENCY = P_WAERS.

      LWA_CURRENCYAMOUNT-AMT_DOCCUR = TBL_GL-WRBTR * -1.

      APPEND LWA_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.

      G_CREDIT_TOTAL = G_CREDIT_TOTAL + TBL_GL-WRBTR.

      CLEAR: LWA_BAPI_ACCTGL,

             LWA_CURRENCYAMOUNT.

    ELSEIF TBL_GL-BSCHL EQ '40'.

      LWA_BAPI_ACCTGL-ITEMNO_ACC = LV_ITEM.

      LWA_BAPI_ACCTGL-GL_ACCOUNT = TBL_GL-HKONT.      "GL Account

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

        EXPORTING

          INPUT  = LWA_BAPI_ACCTGL-GL_ACCOUNT

        IMPORTING

          OUTPUT = LWA_BAPI_ACCTGL-GL_ACCOUNT.

      LWA_BAPI_ACCTGL-COMP_CODE = P_BUKRS.       " Company code

      LWA_BAPI_ACCTGL-PSTNG_DATE = L_POSTING_DATE.

      IF TBL_GL-LDGRP NE SPACE.

        LWA_BAPI_ACCTGL-DOC_TYPE = GC_DOC_TY_SX.

      ELSE.

        LWA_BAPI_ACCTGL-DOC_TYPE = C_SA_DOC_TYPE.

      ENDIF.

      LWA_BAPI_ACCTGL-TAX_CODE = TBL_GL-MWSKZ.       " Tax code

      LWA_BAPI_ACCTGL-ITEM_TEXT = TBL_GL-SGTXT.     "Item text

      LWA_BAPI_ACCTGL-PROFIT_CTR = TBL_GL-PRCTR.     "Profit Center

      LWA_BAPI_ACCTGL-ORDERID = TBL_GL-AUFNR.       "Internal order

      LWA_BAPI_ACCTGL-COSTCENTER = TBL_GL-KOSTL.    "Cost Center

      LWA_BAPI_ACCTGL-CS_TRANS_T = TBL_GL-RMVCT.    "Transaction Type

      LWA_BAPI_ACCTGL-TRADE_ID = TBL_GL-VBUND.    "Trading Partner

      LWA_BAPI_ACCTGL-ACCT_TYPE = 'S'.    "Trading Partner

      APPEND LWA_BAPI_ACCTGL TO LT_BAPI_ACCTGL.

*      *      * Fill amounts

      CLEAR LWA_CURRENCYAMOUNT.

      LWA_CURRENCYAMOUNT-ITEMNO_ACC = LV_ITEM.

      LWA_CURRENCYAMOUNT-CURRENCY = P_WAERS.

      LWA_CURRENCYAMOUNT-AMT_DOCCUR = TBL_GL-WRBTR.

      APPEND LWA_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.

      G_DEBIT_TOTAL = G_DEBIT_TOTAL + TBL_GL-WRBTR.

      CLEAR: LWA_BAPI_ACCTGL,

             LWA_CURRENCYAMOUNT.

    ENDIF.

*      *********************Vendor 31(-)/21(+)************

    IF TBL_GL-BSCHL EQ '31'.

** Fill Account payable if it is a Vendor

      LWA_BAPI_ACCTRCC-ITEMNO_ACC = LV_ITEM.

      LWA_BAPI_ACCTRCC-VENDOR_NO = TBL_GL-HKONT.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

        EXPORTING

          INPUT  = LWA_BAPI_ACCTRCC-VENDOR_NO

        IMPORTING

          OUTPUT = LWA_BAPI_ACCTRCC-VENDOR_NO.

      LWA_BAPI_ACCTRCC-COMP_CODE = P_BUKRS.       " Company code

      LWA_BAPI_ACCTRCC-TAX_CODE = TBL_GL-MWSKZ.

      LWA_BAPI_ACCTRCC-ITEM_TEXT = TBL_GL-SGTXT.

      LWA_BAPI_ACCTRCC-PROFIT_CTR = TBL_GL-PRCTR.

      LWA_BAPI_ACCTRCC-PMNTTRMS = 'A001'.

      APPEND LWA_BAPI_ACCTRCC TO LT_BAPI_ACCTRCC.

*    * Fill amounts

      LWA_CURRENCYAMOUNT-ITEMNO_ACC = LV_ITEM.

      LWA_CURRENCYAMOUNT-CURRENCY = P_WAERS.

      LWA_CURRENCYAMOUNT-AMT_DOCCUR = TBL_GL-WRBTR * -1.

      APPEND LWA_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.

      G_CREDIT_TOTAL = G_CREDIT_TOTAL + TBL_GL-WRBTR.

      CLEAR: LWA_BAPI_ACCTRCC,

             LWA_CURRENCYAMOUNT.

    ELSEIF TBL_GL-BSCHL EQ '21'.

      LWA_BAPI_ACCTRCC-ITEMNO_ACC = LV_ITEM.

      LWA_BAPI_ACCTRCC-VENDOR_NO = TBL_GL-HKONT.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

        EXPORTING

          INPUT  = LWA_BAPI_ACCTRCC-VENDOR_NO

        IMPORTING

          OUTPUT = LWA_BAPI_ACCTRCC-VENDOR_NO.

      LWA_BAPI_ACCTRCC-COMP_CODE = P_BUKRS.       " Company code

      LWA_BAPI_ACCTRCC-TAX_CODE = TBL_GL-MWSKZ.

      LWA_BAPI_ACCTRCC-ITEM_TEXT = TBL_GL-SGTXT.

      LWA_BAPI_ACCTRCC-PROFIT_CTR = TBL_GL-PRCTR.

      LWA_BAPI_ACCTRCC-PMNTTRMS = 'A001'.

      APPEND LWA_BAPI_ACCTRCC TO LT_BAPI_ACCTRCC.

*      * Fill amounts

      LWA_CURRENCYAMOUNT-ITEMNO_ACC = LV_ITEM.

      LWA_CURRENCYAMOUNT-CURRENCY = P_WAERS.

      LWA_CURRENCYAMOUNT-AMT_DOCCUR = TBL_GL-WRBTR.

      APPEND LWA_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.

      G_DEBIT_TOTAL = G_DEBIT_TOTAL + TBL_GL-WRBTR.

      CLEAR: LWA_BAPI_ACCTRCC,

             LWA_CURRENCYAMOUNT.

    ENDIF.

    LV_ITEM = LV_ITEM + 1.

  ENDLOOP.

  IF G_CREDIT_TOTAL <> 0 AND G_DEBIT_TOTAL <> 0.

    CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK'

      EXPORTING

        DOCUMENTHEADER = LWA_BAPI_DOCHEADER

      TABLES

        ACCOUNTGL      = LT_BAPI_ACCTGL

        ACCOUNTPAYABLE = LT_BAPI_ACCTRCC

        CURRENCYAMOUNT = LT_CURRENCYAMOUNT

        RETURN         = LT_RETURN.

    READ TABLE LT_RETURN INTO LWA_RETURN INDEX 1.

    IF LWA_RETURN-TYPE EQ LC_BAPIRET_S.

*  * Post the account document

      CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'

        EXPORTING

          DOCUMENTHEADER = LWA_BAPI_DOCHEADER

        IMPORTING

          OBJ_KEY        = LV_OBJKEY

        TABLES

          ACCOUNTGL      = LT_BAPI_ACCTGL

          ACCOUNTPAYABLE = LT_BAPI_ACCTRCC

          CURRENCYAMOUNT = LT_CURRENCYAMOUNT

          RETURN         = LT_RETURN1.

*      if posting is successful

      READ TABLE LT_RETURN1 INTO LWA_RETURN1 INDEX 1.

      IF LWA_RETURN1-TYPE = LC_BAPIRET_S.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

          EXPORTING

            WAIT = C_CHECK.

**************************************

      ENDIF.

    ENDIF.

  ENDIF.

*ENDIF.

*      LV_ITEM = LV_ITEM + 1.

*    ENDLOOP.

ENDFORM.

*&---------------------------------------------------------------------*

*&      Form  POST_CUST

*&---------------------------------------------------------------------*

*      Customer Posting

*----------------------------------------------------------------------*

FORM POST_CUST .

***************************************

  DATA : L_POSTING_KEY      LIKE RF05A-NEWBS,

         L_DOC_DATE         LIKE BKPF-BLDAT,

         L_POSTING_DATE     LIKE BKPF-BUDAT,

         L_AMOUNT(16)       TYPE C,

         LWA_BAPI_DOCHEADER TYPE BAPIACHE09,

         LV_TABIX           TYPE SY-TABIX,

         LT_BAPI_ACCTGL     TYPE STANDARD TABLE OF   BAPIACGL09,

         LWA_BAPI_ACCTGL    TYPE    BAPIACGL09,

         LT_BAPI_ACCTRCV    TYPE STANDARD TABLE OF BAPIACAR09,

         LWA_BAPI_ACCTRCV   TYPE  BAPIACAR09,

         LT_CURRENCYAMOUNT  TYPE STANDARD TABLE OF BAPIACCR09,

         LWA_CURRENCYAMOUNT TYPE  BAPIACCR09,

         LV_ITEM            TYPE I,

         LV_OBJKEY          TYPE BAPIACHE09-OBJ_KEY.

  CONSTANTS: LC_CHECK     TYPE CHAR1 VALUE 'X',

             LC_BAPIRET_S TYPE CHAR1 VALUE 'S'.

  CLEAR CNT_RECORDS.

  DESCRIBE TABLE TBL_GL LINES CNT_REC_TOT.

  L_DOC_DATE = SY-DATUM.

  LWA_BAPI_DOCHEADER-DOC_DATE = L_DOC_DATE.

  L_POSTING_DATE = P_BUDAT.

  LWA_BAPI_DOCHEADER-PSTNG_DATE = L_POSTING_DATE.

  IF TBL_GL-LDGRP NE SPACE.

    LWA_BAPI_DOCHEADER-DOC_TYPE = GC_DOC_TY_SX.

  ELSE.

    LWA_BAPI_DOCHEADER-DOC_TYPE = C_SA_DOC_TYPE.

  ENDIF.

  LWA_BAPI_DOCHEADER-COMP_CODE = P_BUKRS.

  LWA_BAPI_DOCHEADER-USERNAME = SY-UNAME.

**Populate line item details

  LV_ITEM  = 1.

  LOOP AT TBL_GL.

*    LV_ITEM = LV_ITEM + 1.

*    CNT_RECORDS = CNT_RECORDS + 1.

*Fill Credit line item data

    IF TBL_GL-BSCHL EQ '50'.

      LWA_BAPI_ACCTGL-ITEMNO_ACC = LV_ITEM.

      LWA_BAPI_ACCTGL-GL_ACCOUNT = TBL_GL-HKONT.      "GL Account

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

        EXPORTING

          INPUT  = LWA_BAPI_ACCTGL-GL_ACCOUNT

        IMPORTING

          OUTPUT = LWA_BAPI_ACCTGL-GL_ACCOUNT.

      LWA_BAPI_ACCTGL-COMP_CODE = P_BUKRS.       " Company code

      LWA_BAPI_ACCTGL-PSTNG_DATE = L_POSTING_DATE.

      IF TBL_GL-LDGRP NE SPACE.

        LWA_BAPI_ACCTGL-DOC_TYPE = GC_DOC_TY_SX.

      ELSE.

        LWA_BAPI_ACCTGL-DOC_TYPE = C_SA_DOC_TYPE.

      ENDIF.

      LWA_BAPI_ACCTGL-TAX_CODE = TBL_GL-MWSKZ.       " Tax code

      LWA_BAPI_ACCTGL-ITEM_TEXT = TBL_GL-SGTXT.     "Item text

      LWA_BAPI_ACCTGL-PROFIT_CTR = TBL_GL-PRCTR.     "Profit Center

      LWA_BAPI_ACCTGL-ORDERID = TBL_GL-AUFNR.       "Internal order

      LWA_BAPI_ACCTGL-COSTCENTER = TBL_GL-KOSTL.    "Cost Center

      LWA_BAPI_ACCTGL-CS_TRANS_T = TBL_GL-RMVCT.    "Transaction Type

      LWA_BAPI_ACCTGL-TRADE_ID = TBL_GL-VBUND.    "Trading Partner

      LWA_BAPI_ACCTGL-ACCT_TYPE = 'S'.    "Trading Partner

      APPEND LWA_BAPI_ACCTGL TO LT_BAPI_ACCTGL.

*      * Fill amounts

      CLEAR LWA_CURRENCYAMOUNT.

      LWA_CURRENCYAMOUNT-ITEMNO_ACC = LV_ITEM.

      LWA_CURRENCYAMOUNT-CURRENCY = P_WAERS.

      LWA_CURRENCYAMOUNT-AMT_DOCCUR = TBL_GL-WRBTR * -1.

      APPEND LWA_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.

      G_CREDIT_TOTAL = G_CREDIT_TOTAL + TBL_GL-WRBTR.

      CLEAR: LWA_BAPI_ACCTGL,

             LWA_CURRENCYAMOUNT.

    ELSEIF TBL_GL-BSCHL EQ '40'.

      LWA_BAPI_ACCTGL-ITEMNO_ACC = LV_ITEM.

      LWA_BAPI_ACCTGL-GL_ACCOUNT = TBL_GL-HKONT.      "GL Account

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

        EXPORTING

          INPUT  = LWA_BAPI_ACCTGL-GL_ACCOUNT

        IMPORTING

          OUTPUT = LWA_BAPI_ACCTGL-GL_ACCOUNT.

      LWA_BAPI_ACCTGL-COMP_CODE = P_BUKRS.       " Company code

      LWA_BAPI_ACCTGL-PSTNG_DATE = L_POSTING_DATE.

      IF TBL_GL-LDGRP NE SPACE.

        LWA_BAPI_ACCTGL-DOC_TYPE = GC_DOC_TY_SX.

      ELSE.

        LWA_BAPI_ACCTGL-DOC_TYPE = C_SA_DOC_TYPE.

      ENDIF.

      LWA_BAPI_ACCTGL-TAX_CODE = TBL_GL-MWSKZ.       " Tax code

      LWA_BAPI_ACCTGL-ITEM_TEXT = TBL_GL-SGTXT.     "Item text

      LWA_BAPI_ACCTGL-PROFIT_CTR = TBL_GL-PRCTR.     "Profit Center

      LWA_BAPI_ACCTGL-ORDERID = TBL_GL-AUFNR.       "Internal order

      LWA_BAPI_ACCTGL-COSTCENTER = TBL_GL-KOSTL.    "Cost Center

      LWA_BAPI_ACCTGL-CS_TRANS_T = TBL_GL-RMVCT.    "Transaction Type

      LWA_BAPI_ACCTGL-TRADE_ID = TBL_GL-VBUND.    "Trading Partner

      LWA_BAPI_ACCTGL-ACCT_TYPE = 'S'.    "Trading Partner

      APPEND LWA_BAPI_ACCTGL TO LT_BAPI_ACCTGL.

*      *      * Fill amounts

      CLEAR LWA_CURRENCYAMOUNT.

      LWA_CURRENCYAMOUNT-ITEMNO_ACC = LV_ITEM.

      LWA_CURRENCYAMOUNT-CURRENCY = P_WAERS.

      LWA_CURRENCYAMOUNT-AMT_DOCCUR = TBL_GL-WRBTR.

      APPEND LWA_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.

      G_DEBIT_TOTAL = G_DEBIT_TOTAL + TBL_GL-WRBTR.

      CLEAR: LWA_BAPI_ACCTGL,

             LWA_CURRENCYAMOUNT.

    ENDIF.

*      *********************** Customer 11/01************

    IF TBL_GL-BSCHL EQ '11'.

** Fill Account receivable if it is a customer

      LWA_BAPI_ACCTRCV-ITEMNO_ACC = LV_ITEM.

      LWA_BAPI_ACCTRCV-CUSTOMER = TBL_GL-HKONT.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

        EXPORTING

          INPUT  = LWA_BAPI_ACCTRCV-CUSTOMER

        IMPORTING

          OUTPUT = LWA_BAPI_ACCTRCV-CUSTOMER.

      LWA_BAPI_ACCTRCV-COMP_CODE = P_BUKRS.       " Company code

      LWA_BAPI_ACCTRCV-TAX_CODE = TBL_GL-MWSKZ.

      LWA_BAPI_ACCTRCV-ITEM_TEXT = TBL_GL-SGTXT.

      LWA_BAPI_ACCTRCV-PROFIT_CTR = TBL_GL-PRCTR.

      APPEND LWA_BAPI_ACCTRCV TO LT_BAPI_ACCTRCV.

*    * Fill amounts

      LWA_CURRENCYAMOUNT-ITEMNO_ACC = LV_ITEM.

      LWA_CURRENCYAMOUNT-CURRENCY = P_WAERS.

      LWA_CURRENCYAMOUNT-AMT_DOCCUR = TBL_GL-WRBTR * -1.

      APPEND LWA_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.

      G_CREDIT_TOTAL = G_CREDIT_TOTAL + TBL_GL-WRBTR.

      CLEAR: LWA_BAPI_ACCTRCV,

             LWA_CURRENCYAMOUNT.


    ELSEIF TBL_GL-BSCHL EQ '01'.

      LWA_BAPI_ACCTRCV-ITEMNO_ACC = LV_ITEM.

      LWA_BAPI_ACCTRCV-CUSTOMER = TBL_GL-HKONT.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

        EXPORTING

          INPUT  = LWA_BAPI_ACCTRCV-CUSTOMER

        IMPORTING

          OUTPUT = LWA_BAPI_ACCTRCV-CUSTOMER.

      LWA_BAPI_ACCTRCV-COMP_CODE = P_BUKRS.       " Company code

      LWA_BAPI_ACCTRCV-TAX_CODE = TBL_GL-MWSKZ.

      LWA_BAPI_ACCTRCV-ITEM_TEXT = TBL_GL-SGTXT.

      LWA_BAPI_ACCTRCV-PROFIT_CTR = TBL_GL-PRCTR.

      APPEND LWA_BAPI_ACCTRCV TO LT_BAPI_ACCTRCV.

*      * Fill amounts

      LWA_CURRENCYAMOUNT-ITEMNO_ACC = LV_ITEM.

      LWA_CURRENCYAMOUNT-CURRENCY = P_WAERS.

      LWA_CURRENCYAMOUNT-AMT_DOCCUR = TBL_GL-WRBTR.

      APPEND LWA_CURRENCYAMOUNT TO LT_CURRENCYAMOUNT.

      G_DEBIT_TOTAL = G_DEBIT_TOTAL + TBL_GL-WRBTR.

      CLEAR: LWA_BAPI_ACCTRCV,

             LWA_CURRENCYAMOUNT.

    ENDIF.

    LV_ITEM = LV_ITEM + 1.

  ENDLOOP.




  IF G_CREDIT_TOTAL <> 0 AND G_DEBIT_TOTAL <> 0.

    CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK'

      EXPORTING

        DOCUMENTHEADER    = LWA_BAPI_DOCHEADER

      TABLES

        ACCOUNTGL         = LT_BAPI_ACCTGL

        ACCOUNTRECEIVABLE = LT_BAPI_ACCTRCV

        CURRENCYAMOUNT    = LT_CURRENCYAMOUNT

        RETURN            = LT_RETURN.


    READ TABLE LT_RETURN INTO LWA_RETURN INDEX 1.

    IF LWA_RETURN-TYPE = LC_BAPIRET_S.

*  * Post the account document

      CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'

        EXPORTING

          DOCUMENTHEADER    = LWA_BAPI_DOCHEADER

        IMPORTING

          OBJ_KEY           = LV_OBJKEY

        TABLES

          ACCOUNTGL         = LT_BAPI_ACCTGL

          ACCOUNTRECEIVABLE = LT_BAPI_ACCTRCV

          CURRENCYAMOUNT    = LT_CURRENCYAMOUNT

          RETURN            = LT_RETURN1.

*      if posting is successful

      READ TABLE LT_RETURN1 INTO LWA_RETURN1 INDEX 1.

      IF LWA_RETURN1-TYPE = LC_BAPIRET_S.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

          EXPORTING

            WAIT = C_CHECK.


**************************************


      ENDIF.

    ENDIF.

  ENDIF.

*ENDIF.


ENDFORM.

*&---------------------------------------------------------------------*

*&      Form  AUTHORITY_CHECK

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM AUTHORITY_CHECK .

  IF P_BUKRS IS NOT INITIAL.

    AUTHORITY-CHECK OBJECT 'F_BKPF_BUK'

                ID 'BUKRS' FIELD P_BUKRS

                ID 'ACTVT' FIELD '10'.

    IF SY-SUBRC <> 0.

      MESSAGE 'No authorization to post' TYPE 'E'.

      LEAVE LIST-PROCESSING.

    ENDIF.


    AUTHORITY-CHECK OBJECT 'F_BKPF_BUK'

                ID 'BUKRS' FIELD P_BUKRS

                ID 'ACTVT' FIELD '77'.

    IF SY-SUBRC <> 0.

      MESSAGE 'No authorization' TYPE 'E'.

      LEAVE LIST-PROCESSING.

    ENDIF.

  ENDIF.

*  ********************************************

*  ********************************************

*  ********************************************

  IF   P_RAD1 EQ 'X'.

    AUTHORITY-CHECK OBJECT 'F_BKPF_KOA'

              ID 'KOART' FIELD 'S'

              ID 'ACTVT' FIELD '01'.

    IF SY-SUBRC <> 0.

      MESSAGE 'No authorization to post GL' TYPE 'E'.

    ENDIF.

  ENDIF.

  IF    P_RAD2 EQ 'X'.

    AUTHORITY-CHECK OBJECT 'F_BKPF_KOA'

              ID 'KOART' FIELD 'D'

              ID 'ACTVT' FIELD '01'.

    IF SY-SUBRC <> 0.

      MESSAGE 'No authorization to post AR' TYPE 'E'.

    ENDIF.

  ENDIF.

  IF   P_RAD3 EQ 'X'.

    AUTHORITY-CHECK OBJECT 'F_BKPF_KOA'

              ID 'KOART' FIELD 'K'

              ID 'ACTVT' FIELD '01'.

    IF SY-SUBRC <> 0.

      MESSAGE 'No authorization to post AP' TYPE 'E'.

    ENDIF.

  ENDIF.

ENDFORM.

No comments:

Post a Comment