SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Posting Vendor Invoices using 'BAPI_INCOMINGINVOICE_CREATE'



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> SD
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sun Oct 14, 2007 9:19 pm    Post subject: Posting Vendor Invoices using 'BAPI_INCOMINGINVOICE_CREATE' Reply with quote

Requirement:
Invoice data is sent by the vendors in a text file and an inbound interface is required to create Vendor Invoices in SAP. The file contains the Purchase Order information, material quantity received from vendor along with the amount. Here the requirement becomes a little complex as there might be several occurrences of same Purchase Order item in one invoice.

Processing:
After reading data from the text File, it will be converted into Invoice header and item internal tables as required in the BAPI. Since there is a chance of multiple occurrence of same Purchase Order item in one Invoice, accounting data table is also required to be populated. It means accounting data table will have to have unplanned account assignments for re-appearing PO lines. Otherwise, the BAPI will encounter problems at runtime and a dump will be generated while trying to insert a duplicate row in table EKKN. Check OSS Note 518338. Further, BAPI 'BAPI_INCOMINGINVOICE_CREATE' is called to create Invoice. Here, populating internal tables for simple cases is fairly easy so code below will concentrate on populating accounting data for re-appearing Puchase Order items.

Code:

REPORT  Z_BAPI_INCOMINGINVOICE_CREATE           .
*----------------------------------------------------------------------*
*       Create Vendor Invoices using BAPI - Date - 14.05.2005
*----------------------------------------------------------------------*
*       Written By: Ram Manohar Tiwari             
*----------------------------------------------------------------------*
*       Presented By: http://www.rmtiwari.com
*----------------------------------------------------------------------*
*  Invoice data is sent by the vendors in a text file and an inbound
*  interface is required to create Vendor Invoices in SAP.
*  The file contains the Purchase Order information, material quantity
*  received from vendor along with the amount.
*  Here the requirement becomes a little complex as there might be
*  several ocuurences of same Purchase Order item in one  invoice.
*----------------------------------------------------------------------*
*  Only a incomplete sample coding is given here and it can only be used
*  as a base for writing a program.
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* Populate internal Table I_ITAB from the data uploaded
* from text data file.
*......
*......

*----------------------------------------------------------------------*
* Populate Item Table from item data in data file
*......
*......
*   Processing for header records of data file.
*   set up header data for BAPI call.
*   Check whwther it's an Incoice or Credit Memo.
*   And populate Invoice indicator accordingly.

IF I_ITAB-TRANS EQ '-'.
  I_HEADER-INVOICE_IND = C_X.
ELSE.
  CLEAR I_HEADER-INVOICE_IND.
ENDIF.
I_HEADER-PSTNG_DATE = I_ITAB-BUDAT.
I_HEADER-DOC_DATE = I_ITAB-BLDAT.
I_HEADER-CURRENCY = W_WAERS.
I_HEADER-GROSS_AMOUNT = I_ITAB-DMBTR.
I_HEADER-COMP_CODE = I_ITAB-BUKRS.
I_HEADER-HEADER_TXT = I_ITAB-SGTXT.
I_HEADER-REF_DOC_NO = I_ITAB-XBLNR.

IF NOT I_ITAB-INV_REC_DATE IS INITIAL.
  I_HEADER-INV_REC_DATE = I_ITAB-INV_REC_DATE.
ELSE.
  I_HEADER-INV_REC_DATE = I_ITAB-BLDAT.
ENDIF.

I_HEADER-PYMT_METH = I_ITAB-ZLSCH.
APPEND I_HEADER.

*....
*....
*----------------------------------------------------------------------*
* Populate Item Table from item data in data file

lv_count = 0.

LOOP AT I_ITAB.

*   Processing for header records of data file.

  lv_count = lv_count + 1.
* ...........
* ...........
* Item Data
  I_ITEM-INVOICE_DOC_ITEM = lv_COUNT.
  I_ITEM-PO_NUMBER    = I_ITAB-EBELN.
  I_ITEM-PO_ITEM      = I_ITAB-EBELP.
  I_ITEM-TAX_CODE     = I_ITAB-MWSKZ2.
  I_ITEM-ITEM_AMOUNT  = I_ITAB-NETWR.

* Populate quantities if not a blanket order
  IF I_ITAB-BLANKET EQ SPACE.
    I_ITEM-QUANTITY = I_ITAB-MENGE.
    PERFORM GET_MEINS USING I_ITAB-EBELN       " Use table EKPO
                            I_ITAB-EBELP
                      CHANGING I_ITEM-PO_UNIT.
    I_ACCOUNTINGDATA-PO_UNIT = I_ITEM-PO_UNIT.

    IF I_ITEM-QUANTITY EQ 0.
      I_ITEM-PO_UNIT = SPACE.
    ENDIF.
  ENDIF.

* Item Text
  I_ITEM-ITEM_TEXT = I_ITAB-ITEM_TEXT.
  APPEND I_ITEM.

* Populate Accounting Data
  IF I_ITAB-BLANKET EQ SPACE.
    I_ACCOUNTINGDATA-INVOICE_DOC_ITEM = lv_COUNT.
    I_ACCOUNTINGDATA-SERIAL_NO        = '01'.

    I_ACCOUNTINGDATA-TAX_CODE         = I_ITAB-MWSKZ2.
    I_ACCOUNTINGDATA-ITEM_AMOUNT      = I_ITAB-NETWR.

    SELECT SINGLE SAKTO KOSTL VBELN VBELP ANLN1 ANLN2 DABRZ
                  FISTL GEBER GRANT_NBR GSBER IMKEY KOKRS KSTRG PAOBJNR
                  PRCTR PS_PSP_PNR AUFNR MENGE
      FROM EKKN
      INTO (I_ACCOUNTINGDATA-GL_ACCOUNT, I_ACCOUNTINGDATA-COSTCENTER,
            I_ACCOUNTINGDATA-SD_DOC, I_ACCOUNTINGDATA-SDOC_ITEM,
            I_ACCOUNTINGDATA-ASSET_NO, I_ACCOUNTINGDATA-SUB_NUMBER,
            I_ACCOUNTINGDATA-REF_DATE, I_ACCOUNTINGDATA-FUNDS_CTR,
            I_ACCOUNTINGDATA-FUND, I_ACCOUNTINGDATA-GRANT_NBR,
            I_ACCOUNTINGDATA-BUS_AREA, I_ACCOUNTINGDATA-RL_EST_KEY,
            I_ACCOUNTINGDATA-CO_AREA, I_ACCOUNTINGDATA-COSTOBJECT,
           I_ACCOUNTINGDATA-PROFIT_SEGM_NO, I_ACCOUNTINGDATA-PROFIT_CTR,
            I_ACCOUNTINGDATA-WBS_ELEM, I_ACCOUNTINGDATA-ORDERID,
            I_ACCOUNTINGDATA-QUANTITY)
      WHERE EBELN EQ I_ITAB-EBELN
      AND   EBELP EQ I_ITAB-EBELP
      AND   ZEKKN EQ '01'.
    IF EKKO-BSART NE 'LTV'.
      CLEAR I_ACCOUNTINGDATA-QUANTITY.
      CLEAR I_ACCOUNTINGDATA-PO_UNIT.
    ENDIF.
    APPEND I_ACCOUNTINGDATA.
  ENDIF.

*.....
*.....
ENDLOOP.

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

*   The following coding is to solve the problem
*   mentioned in OSS Note 518338.
*   Same PO item within several invoice items.
SORT I_ITEM BY PO_NUMBER PO_ITEM.
LOOP AT I_ITEM.

  ON CHANGE OF I_ITEM-PO_NUMBER OR I_ITEM-PO_ITEM.
    W_COUNTER = 1.
    LOOP AT I_ITEM WHERE PO_NUMBER = I_ITEM-PO_NUMBER
                     AND PO_ITEM   = I_ITEM-PO_ITEM.

      IF W_COUNTER EQ 1.
        I_ACCOUNTINGDATA-SERIAL_NO = '01'.
        I_ACCOUNTINGDATA-XUNPL     = ' '.
      ELSE.
        I_ACCOUNTINGDATA-SERIAL_NO = ' '.
        I_ACCOUNTINGDATA-XUNPL     = 'X'.
      ENDIF.
      MODIFY I_ACCOUNTINGDATA
       TRANSPORTING SERIAL_NO XUNPL
      WHERE INVOICE_DOC_ITEM = I_ITEM-INVOICE_DOC_ITEM.
      W_COUNTER = W_COUNTER + 1.
    ENDLOOP.

*     To solve the repetition of PO item in subsequent invoices.
  ELSEIF SY-TABIX EQ 1.
    W_COUNTER = 1.
    LOOP AT I_ITEM WHERE PO_NUMBER = I_ITEM-PO_NUMBER
                     AND PO_ITEM   = I_ITEM-PO_ITEM.

      IF W_COUNTER EQ 1.
        I_ACCOUNTINGDATA-SERIAL_NO = '01'.
        I_ACCOUNTINGDATA-XUNPL     = ' '.
      ELSE.
        I_ACCOUNTINGDATA-SERIAL_NO = ' '.
        I_ACCOUNTINGDATA-XUNPL     = 'X'.
      ENDIF.
      MODIFY I_ACCOUNTINGDATA
       TRANSPORTING SERIAL_NO XUNPL
      WHERE INVOICE_DOC_ITEM = I_ITEM-INVOICE_DOC_ITEM.
      W_COUNTER = W_COUNTER + 1.
    ENDLOOP.

  ENDON.
ENDLOOP.
*   Changes over for  OSS Note 518338.

SORT I_ITEM BY INVOICE_DOC_ITEM PO_NUMBER PO_ITEM.
CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE'
  EXPORTING
    HEADERDATA       = I_HEADER
  IMPORTING
    INVOICEDOCNUMBER = W_BELNR
    FISCALYEAR       = W_GJAHR
  TABLES
    ITEMDATA         = I_ITEM
    ACCOUNTINGDATA   = I_ACCOUNTINGDATA
    TAXDATA          = I_TAX
    RETURN           = I_RETURN.


if sy-subrc < >  0.
  message e999(re) with  'Problem occured'.
else.
  loop at return.
    if not return is initial.
      clear bapi_retn_info.
      move-corresponding return to bapi_retn_info.
      if return-type = 'A' or return-type = 'E'.
        error_flag = 'X'.
      endif.
      append bapi_retn_info.
    endif.
  endloop.
  if error_flag = 'X'.
    message e999(re) with  'Problem occured'.
    rollback work.
  else.
     *       Return Table from BAPI call is empty
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        WAIT   = 'X'
      IMPORTING
        RETURN = I_RETURN.
  endif.
endif.
Back to top
View user's profile Send private message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sun Jan 11, 2009 1:41 pm    Post subject: Reply with quote

Note 512282 - BAPIs: Service-based invoice verification

Quote:
Example for the correct filling of the interface parameters for function module BAPI_INCOMINGINVOICE_CREATE:

You receive an invoice with reference to purchase order number 4500000191, purchase order item 10. The system calculates two services A and B with a quantity of six service units each. Service A has a gross amount of 580 euro including 16 % value-added tax. For service B, the system calculates 812 euro including 16 % value-added tax. 50 % of service A was assigned in each case to the two cost centers 1000 and 2000, similarly 50 % of service B was also assigned in each case to cost centers 1000 and 3000. The acceptance of services performed is only executed for the two services together with service entry sheet no. 1000000022.

For this, enter the following data:

Table HEADERDATA

INVOICE_IND (Post invoice/credit memo): X
DOC_TYPE (Document type): RE
DOC_DATE (Document date): 01/10/2002
PSTNG_DATE (Posting date): 01/10/2002
COMP_CODE (Company code): 0001
CURRENCY (Currency key): EUR
GROSS_AMOUNT (Gross amount): 1392,00
CALC_TAX_IND (Automatically Calculate Tax): X
PMNTTRMS (Terms of payment key): 0001

Table ITEMDATA
First entry for service A:


INVOICE_DOC_ITEM (Item of invoice document): 000001
PO_NUMBER (Purchase order number): 4500000191
PO_ITEM (Purchase order item): 00010
TAX_CODE (Tax code): V1
ITEM_AMOUNT (Item amount): 250.00
QUANTITY (Quantity): 3
PO_UNIT (Unit of measure): LE
SHEET_NO (Entry sheet number): 1000000022
SHEET_ITEM (Line number) : 10

Second entry for service A:

INVOICE_DOC_ITEM (Item of invoice document): 000002
PO_NUMBER (Purchase order number): 4500000191
PO_ITEM (Purchase order item): 00010
TAX_CODE (Tax code): V1
ITEM_AMOUNT (Item amount): 250.00
QUANTITY (Quantity): 3
PO_UNIT (Unit of measure): LE
SHEET_NO (Entry sheet number): 1000000022
SHEET_ITEM (Line number): 10

First entry for service B:

INVOICE_DOC_ITEM (Item of invoice document): 000003
PO_NUMBER (Purchase order number): 4500000191
PO_ITEM (Purchase order item): 00010
TAX_CODE (Tax code): V1
ITEM_AMOUNT (Item amount): 350.00
QUANTITY (Quantity): 3
PO_UNIT (Unit of measure): LE
SHEET_NO (Entry sheet number): 1000000022
SHEET_ITEM (Line number) : 20

Second entry for service B:

INVOICE_DOC_ITEM (Item of invoice document): 000004
PO_NUMBER (Purchase order number): 4500000191
PO_ITEM (Purchase order item): 00010
TAX_CODE (Tax code): V1
ITEM_AMOUNT (Item amount): 350,00
QUANTITY (Quantity): 3
PO_UNIT (Unit of measure): LE
SHEET_NO (Entry sheet number): 1000000022
SHEET_ITEM (Line number) : 20

Table ACCOUNTINGDATA
First account assignment for service A:


INVOICE_DOC_ITEM (Document item in invoice document): 000001
SERIAL_NO (Serial number of account assignment): 01
TAX_CODE (Tax code): V1
ITEM_AMOUNT (Amount in document currency): 250.00
QUANTITY (Quantity): 3
PO_UNIT (Order unit): LE
GL_ACCOUNT (Number of G/L account): 400000
COSTCENTER (Cost center): 1000

Second account assignment for service A:

INVOICE_DOC_ITEM (Document item in invoice document): 000002
SERIAL_NO (Serial number of account assignment): 02
TAX_CODE (Tax code): V1
ITEM_AMOUNT (Amount in document currency): 250.00
QUANTITY (Quantity): 3
PO_UNIT (Order unit): LE
GL_ACCOUNT (Number of G/L account): 400000
COSTCENTER (Cost center): 2000

First account assignment for service B:

INVOICE_DOC_ITEM (Document item in invoice document): 000003
SERIAL_NO (Serial number of account assignment): 01
TAX_CODE (Tax code): V1
ITEM_AMOUNT (Amount in document currency): 350.00
QUANTITY (Quantity): 3
PO_UNIT (Order unit): LE
GL_ACCOUNT (Number of G/L account): 400000
COSTCENTER (Cost center): 1000

Second account assignment for service B:

INVOICE_DOC_ITEM (Document item in invoice document): 000004
SERIAL_NO (Serial number of account assignment): 03
TAX_CODE (Tax code): V1
ITEM_AMOUNT (Amount in document currency): 350.00
QUANTITY (Quantity): 3
PO_UNIT (Order unit): LE
GL_ACCOUNT (Number of G/L account): 400000
COSTCENTER (Cost center): 3000

The entry in table 'ITEMDATA' adheres to the logic of the display of the items of an incoming invoice in Transaction 'Enter Incoming Invoice' (MIRO).
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> SD All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.