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

BAPI_INCOMINGINVOICE_CREATE



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Interfaces | Интерфейсы -> BAPI
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sat Apr 12, 2008 6:09 pm    Post subject: BAPI_INCOMINGINVOICE_CREATE Reply with quote

BAPI_INCOMINGINVOICE_CREATE - Invoice Verification: Post Invoice

Code:

REPORT  Z_BAPI_INCOMINGINVOICE_CREATE           .
----------------------------------------------------------------------

*Create Vendor Invoices using BAPI               *

  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
vga
Мастер
Мастер


Age: 160
Joined: 04 Oct 2007
Posts: 1218
Location: Санкт-Петербург

PostPosted: Thu May 21, 2009 9:28 am    Post subject: Reply with quote

If FM 'BAPI_INCOMINGINVOICE_CREATE' return error message

E M8 186 Balance is not equal to zero: xxx USD
( Сальдо счета-фактуры не равно нулю: xxx RUB )
you need to fill quantity and unit.

I_ACCOUNTINGDATA-QUANTITY = nnn.
I_ACCOUNTINGDATA-PO_UNIT = 'XX'.

This depend of type of PO (ekko-bsart)
Back to top
View user's profile Send private message Blog Visit poster's website
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Tue Jun 09, 2009 4:07 pm    Post subject: Reply with quote

Code:
*---------------------------------------------------------------------*
* Executa a BAPI MIRO
*----------------------------------------------------------------------*
FORM f_bapi_miro USING p_ent.

* Dados locais
  DATA: st_header_miro LIKE bapi_incinv_create_header,
        it_return_miro LIKE bapiret2
                       OCCURS 0 WITH HEADER LINE,
        vc_docto_miro  LIKE bapi_incinv_fld-inv_doc_no,
        vc_year_miro   LIKE bapi_incinv_fld-fisc_year.
*
  DATA: v_regio    TYPE t001w-regio,
        v_taxcode  TYPE vfkp-mwskz,
        v_inco1    TYPE likp-inco1,
        v_fkart    TYPE vfkk-fkart,
        v_meins    TYPE lips-meins,
        v_ntgew    TYPE lips-ntgew,
        v_aux      TYPE lips-ntgew,
        v_ekorg    TYPE ekko-ekorg,
        v_count(6) TYPE n,
        v_valor    TYPE vfkp-netwr.

  DATA: vc_valor LIKE st_header_miro-gross_amount.

  CLEAR: st_header_miro,
         vc_docto_miro,
         vc_year_miro,
         it_item_miro,
         it_return_miro,
         it_vfkp.

  REFRESH: it_item_miro, it_return_miro,it_vfkp.

* Seleзгo inicial

  CLEAR: st_lfa1, st_ekpo, st_likp.

  SELECT lblni
         fknum
         fkpos
         mwskz
         netwr
         mwsbp
         knumv
    INTO TABLE it_vfkp
    FROM vfkp
   WHERE rebel = it_saida-tknum
     AND lblni = it_saida-lblni
     AND fkpty <> 'Z022'.

  SELECT SINGLE regio INTO st_lfa1-regio
    FROM lfa1
   WHERE lifnr = it_saida-tdlnr.

  SELECT SINGLE ekpo~j_1bmatuse
                ekpo~j_1bindust
                t001w~regio
                t001w~land1
                ekpo~mwskz
    INTO st_ekpo
    FROM ekpo
    JOIN t001w ON t001w~werks = ekpo~werks
   WHERE ekpo~ebeln = it_saida-ebeln
     AND ekpo~ebelp = it_saida-ebelp.

  SELECT SINGLE likp~vbeln
                likp~kunnr
                kna1~regio
                kna1~land1
                kna1~txjcd
    INTO st_likp
    FROM likp
    JOIN kna1 ON kna1~kunnr = likp~kunnr
   WHERE likp~vbeln = it_saida-vbeln.

* Atribui dados Cabeзalho - MIRO

  st_header_miro-invoice_ind  = 'X'.
  st_header_miro-doc_type     = 'RE'.

  st_header_miro-doc_date     = it_saida-dctrc.
  st_header_miro-pstng_date   = sy-datum.
  st_header_miro-ref_doc_no   = it_saida-bolnr.
  st_header_miro-comp_code    = it_saida-bukrs.
  st_header_miro-diff_inv     = it_saida-tdlnr.
  st_header_miro-currency     = it_saida-waers.
  st_header_miro-currency_iso = it_saida-waers.
  st_header_miro-calc_tax_ind = 'X'.
  st_header_miro-gross_amount = it_saida-kzwi6.
  st_header_miro-j_1bnftype   = 'C1'.

* Busca condiзгo
  SELECT SINGLE ekorg
    INTO v_ekorg
    FROM ekko
   WHERE ebeln = it_saida-ebeln.

  SELECT SINGLE zterm
    INTO st_header_miro-pmnttrms
    FROM lfm1
   WHERE lifnr = it_saida-tdlnr
     AND ekorg = v_ekorg.

  IF st_header_miro-pmnttrms IS INITIAL.
    st_header_miro-pmnttrms     = '30DD'.
  ENDIF.

* Preenche os itens da MIRO
  v_count = 0.
* Cada item representa um custo
  LOOP AT it_vfkp.

    READ TABLE it_doc WITH KEY fknum = it_vfkp-fknum
                               fkpos = it_vfkp-fkpos.
    IF sy-subrc EQ 0.

      v_taxcode = st_ekpo-mwskz.

      v_regio = st_likp-txjcd.

      IF st_lfa1-regio = st_ekpo-regio.

        DATA: wa_a612 TYPE a612.

*       Substituiзгo tributбria
        SELECT SINGLE * INTO wa_a612
          FROM a612
         WHERE regioz = st_lfa1-regio.
        IF sy-subrc = 0.

          SELECT SINGLE inco1 INTO v_inco1
            FROM likp
             JOIN vfsi ON likp~vbeln = vfsi~vbeln
             JOIN vfkp ON vfsi~knumv = vfkp~knumv
           WHERE vfkp~fknum = it_vfkp-fknum
             AND likp~inco1 = 'CIF'.
          IF sy-subrc = 0.
            v_taxcode = 'P0'.
          ELSE.
            v_taxcode = 'P1'.
          ENDIF.

        ENDIF.

*      ELSE.
*        CONCATENATE 'Y' it_doc-regio INTO v_regio.
      ENDIF.

*     Verifica o FKART
      SELECT SINGLE fkart INTO v_fkart
        FROM vfkk
       WHERE fknum = it_vfkp-fknum.

      IF v_fkart = 'Z002'.
        st_header_miro-j_1bnftype   = ' '.
        v_taxcode = 'IE'.
      ELSEIF v_fkart = 'Z004'.
        st_header_miro-j_1bnftype   = 'C1'.
        v_taxcode = 'IE'.
      ENDIF.

      st_header_miro-header_txt = it_doc-xblnr.

      IF it_saida-lfart = 'ZLF'. "Venda
        CLEAR it_item_miro-cond_type.
        v_meins = ' '.
        v_ntgew = '0'.

*       ICMS = 0 e ISS <> 0
        IF it_saida-kzwi1 = 0 AND
           it_saida-kzwi4 > 0.
          st_header_miro-j_1bnftype   = 'E1'.
          v_taxcode = 'P0'.
        ENDIF.

      ELSE. "Transferencia

        it_item_miro-cond_type = 'ZFRB'.
        v_taxcode = 'P1'.

      ENDIF.

      LOOP AT it_aux WHERE tknum = it_saida-tknum
                       AND bolnr = it_saida-bolnr.

        v_count = v_count + 1.

        IF it_aux-lfart = 'NL'. "Transferencia
          CLEAR: v_ntgew, v_aux.
          SELECT meins ntgew
            INTO (v_meins, v_aux)
            FROM lips
           WHERE vbeln = it_aux-vbeln.
            v_ntgew = v_ntgew + v_aux.
          ENDSELECT.
        ENDIF.

        it_item_miro-invoice_doc_item = v_count. "Item fatura

        it_item_miro-po_number    = it_aux-ebeln.    "Pedido
        it_item_miro-po_item      = it_aux-ebelp.    "Item Pedido
        it_item_miro-tax_code     = v_taxcode.       "Cod.imposto IVA
        it_item_miro-po_unit      = v_meins.         "Unidade
        it_item_miro-sheet_no     = it_aux-lblni.    "Folha de Registro
        it_item_miro-taxjurcode   = v_regio.
        it_item_miro-final_inv    = ' '.             "Cуd.fatura final


        IF it_saida-lfart NE 'NL'.  " Quando VENDA gerar um sу registro

          READ TABLE it_item_miro WITH KEY po_number = it_aux-ebeln
                                             po_item = it_aux-ebelp.
          IF sy-subrc EQ 0.
            ADD it_aux-netwr TO it_item_miro-item_amount.
            ADD  v_ntgew TO it_item_miro-quantity.
            MODIFY it_item_miro INDEX sy-tabix.
          ELSE.
            it_item_miro-item_amount  = it_aux-netwr.    "Preзo liq
            it_item_miro-quantity     = v_ntgew.         "Quantidade
            APPEND it_item_miro.
          ENDIF.
        ENDIF.

        " Transferкncia or o primeiro registro de venda
        IF it_saida-lfart EQ 'NL'.   " Transferкncia
          it_item_miro-item_amount  = it_aux-netwr.    "Preзo liq
          it_item_miro-quantity     = v_ntgew.         "Quantidade
          APPEND it_item_miro.
        ENDIF.

      ENDLOOP.

    ENDIF.

  ENDLOOP.

  CLEAR: vc_docto_miro, it_return_miro, it_return_miro[], vc_type.

  vc_type = st_header_miro-j_1bnftype.

  CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE'
       EXPORTING
            headerdata       = st_header_miro
       IMPORTING
            invoicedocnumber = vc_docto_miro
            fiscalyear       = vc_year_miro
       TABLES
            itemdata         = it_item_miro
            return           = it_return_miro.

  IF NOT vc_docto_miro IS INITIAL.
*   Se gerou o documento dou o commit work
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
         EXPORTING
              wait = 'X'.

*   ATUALIZAЗГO FEITA PARA QUE DURANTE A VISUALIZAЗГO DA FATURA SEJA
*   POSSIVEL A VISUALIZAЗГO DA NOTA FISCAL.
*   POR SER LOCALIZAЗГO BRASIL ESTE CAMPO NГO ESTБ NA BAPI E NГO TEM
*   OUTRA FORMA DE ATUALIZA-LO.
    UPDATE rbkp SET j_1bnftype = vc_type
    WHERE belnr = vc_docto_miro AND
          gjahr = vc_year_miro.

    IF sy-subrc NE 0.
      vc_flag_erro = 'X'.
*     ERRO ATUALIZAЗГO J_1BNFTYPE. DOCTO &
      PERFORM atribui_erro USING 'ZVGMM'
                                '027'
                                'J_1BNFTYPE' vc_docto_miro  '' .
      MOVE-CORRESPONDING it_saida TO wa_ztlog.
      PERFORM monta_log USING '214' sy-msgty 'MIRO'.
      p_mensagem = '01'.
      EXPORT p_mensagem TO MEMORY ID 'Z_214'.
      MOVE-CORRESPONDING it_saida TO wa_ztlog.
      wa_ztlog-docmiro = vc_docto_miro.
      log_registro = wa_ztlog.
      PERFORM gravar_log_interface USING p_ent 'MIRO'.
      COMMIT WORK AND WAIT.
    ELSE.

      PERFORM f_altera_erekz USING vc_docto_miro
                                   p_ent.

      PERFORM f_aprovacao_fiscal.

      IF sy-subrc = 0.

        CLEAR v_valor.

        IF it_saida-lfart NE 'NL'.  " Venda
          v_valor = it_vfkp-netwr + it_vfkp-mwsbp.
        ENDIF.

        PERFORM f_altera_nf USING vc_docto_miro
                                  vc_year_miro
                                  v_valor.
      ENDIF.

    ENDIF.
  ELSE.
    LOOP AT it_return_miro WHERE type = 'E'.
      vc_flag_erro = 'X'.
*     Tratamento log de erros para ztlog
      PERFORM atribui_erro USING it_return_miro-id
                                 it_return_miro-number
                                 it_return_miro-message_v1
                                 it_return_miro-message_v2
                                 it_return_miro-message_v3.
      MOVE-CORRESPONDING it_saida TO wa_ztlog.

      PERFORM monta_log USING '214' sy-msgty 'MIRO'.
      p_mensagem = '01'.
      EXPORT p_mensagem TO MEMORY ID 'Z_214'.
      MOVE-CORRESPONDING it_saida TO wa_ztlog.
      wa_ztlog-docmiro = vc_docto_miro.
      log_registro = wa_ztlog.
      PERFORM gravar_log_interface USING p_ent 'MIRO'.
      COMMIT WORK AND WAIT.
    ENDLOOP.

  ENDIF.

ENDFORM.                    " f_bapi_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 -> Interfaces | Интерфейсы -> BAPI 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 can 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.