`

BAPI_INCOMINGINVOICE_CREATE usage

 
阅读更多
The BAPI is equivalent to Tcode MIRO.
LOOP AT itab_bapi.
*Populate header Data
    l_header-invoice_ind    = 'X'.
    CONCATENATE itab_bapi-inv_date+6(4) itab_bapi-inv_date+3(2)
                itab_bapi-inv_date(2) INTO l_header-doc_date.
    l_header-pstng_date     = sy-datum.
    l_header-comp_code      = itab_bapi-bukrs.
    l_header-currency       = itab_bapi-waers.
    l_header-header_txt     = itab_bapi-reference.
    l_header-gross_amount   = l_header-gross_amount + itab_bapi-tot_subtract_gst.
    l_header-alloc_nmbr     = itab_bapi-inv_no.
    l_header-item_text      = itab_bapi-ebeln.
*Populate Item Date
    l_item-po_number        = itab_bapi-ebeln.
    l_item-po_item          = itab_bapi-ebelp.
    SELECT SINGLE lfbnr lfgja lfpos gjahr INTO
        (l_item-ref_doc, l_item-ref_doc_year, l_item-ref_doc_it, l_gjahr)
         FROM ekbe WHERE ebeln EQ itab_bapi-ebeln AND
         ebelp EQ itab_bapi-ebelp AND belnr EQ itab_bapi-belnr.
*Populate GRS Exchange Rate.
*IF checkbox for Exchange Rate Fixed was ticked, the exchange rate will be extracted from PO header
*Else GRS Exchange rate
    CONCATENATE itab_bapi-belnr l_gjahr INTO l_awkey.
    SELECT SINGLE kufix wkurs INTO (l_kufix, l_header-exch_rate) FROM ekko
                 WHERE ebeln EQ itab_bapi-ebeln AND bukrs EQ itab_bapi-bukrs.
    IF l_kufix IS INITIAL.
      SELECT SINGLE kursf INTO l_header-exch_rate FROM bkpf
                WHERE awtyp EQ 'MKPF' AND awkey EQ l_awkey AND bukrs EQ itab_bapi-bukrs.
    ENDIF.
    SELECT SINGLE lebre bprme pstyp webre INTO (l_lebre,l_item-po_pr_uom, l_pstyp, l_webre) FROM ekpo
             WHERE ebeln EQ itab_bapi-ebeln AND ebelp EQ itab_bapi-ebelp AND bukrs EQ itab_bapi-bukrs.
    REFRESH: it_ekbe.
    l_item-tax_code           = itab_bapi-mwskz.
    SELECT zekkn wrbtr menge lfbnr lfgja lfpos INTO
         (l_zekkn, l_amt, l_accounting-quantity, l_item-ref_doc, l_item-ref_doc_year, l_item-ref_doc_it)
                                             FROM ekbe WHERE ebeln EQ itab_bapi-ebeln AND
                                     ebelp EQ itab_bapi-ebelp AND belnr EQ itab_bapi-belnr.
      CLEAR: l_menge, l_wrbtr, l_grn_menge, l_grn_wrbtr.
      IF l_webre IS NOT INITIAL.
        IF l_pstyp EQ '9'.
          SELECT * FROM ekbe INTO CORRESPONDING FIELDS OF TABLE it_ekbe
                                   WHERE ebeln = itab_bapi-ebeln AND ebelp = itab_bapi-ebelp
          AND lfbnr = l_item-ref_doc AND lfpos EQ l_item-ref_doc_it AND bewtp IN ('R','Q', 'T').
          IF sy-subrc EQ 0.
            LOOP AT it_ekbe.
              IF it_ekbe-shkzg = 'H'.
                l_menge = l_menge - it_ekbe-menge.
                l_wrbtr = l_wrbtr - it_ekbe-wrbtr.
              ELSEIF it_ekbe-shkzg = 'S'.
                l_menge = l_menge + it_ekbe-menge.
                l_wrbtr = l_wrbtr + it_ekbe-wrbtr.
              ENDIF.
            ENDLOOP.
          ENDIF.
        ELSE.
          SELECT  * FROM ekbe INTO CORRESPONDING FIELDS OF TABLE it_ekbe
                                       WHERE ebeln = itab_bapi-ebeln AND ebelp = itab_bapi-ebelp
          AND lfbnr = l_item-ref_doc AND belnr NE l_item-ref_doc AND lfpos EQ l_item-ref_doc_it.
          IF sy-subrc EQ 0.
            LOOP AT it_ekbe.
              CASE it_ekbe-bewtp.
*get the quantity and amount invoiced or parked
                WHEN 'R' OR 'Q' OR 'T'.
                  IF it_ekbe-shkzg = 'H'.
                    l_menge = l_menge - it_ekbe-menge.
                    l_wrbtr = l_wrbtr - it_ekbe-wrbtr.
                  ELSE.
                    l_menge = l_menge + it_ekbe-menge.
                    l_wrbtr = l_wrbtr + it_ekbe-wrbtr.
                  ENDIF.
*get the quantity and amount returned or canceled
                WHEN 'E'.
                  CHECK it_ekbe-belnr GT itab_bapi-belnr.
                  ADD it_ekbe-menge TO l_grn_menge.
                  ADD it_ekbe-wrbtr TO l_grn_wrbtr.
              ENDCASE.
            ENDLOOP.
          ENDIF.
        ENDIF.
      ELSE. "GR-based IV is not ticked
        l_amt                 = itab_bapi-tot_subtract_gst.
        l_accounting-quantity = itab_bapi-menge.
      ENDIF.
      ADD 1 TO l_index.
      l_item-item_amount      = l_amt - l_wrbtr - l_grn_wrbtr.
*Convert internal amount to external amount
      CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
        EXPORTING
          currency        = l_header-currency
          amount_internal = l_item-item_amount
        IMPORTING
          amount_external = l_item-item_amount.
      l_item-invoice_doc_item = l_index.
      l_item-sheet_no         = l_item-ref_doc.
      l_item-sheet_item       = l_zekkn * 10.
      IF ( l_pstyp NE '9' OR l_lebre IS NOT INITIAL ) AND l_pstyp NE '1'.
        l_item-po_unit        = itab_bapi-meins.
        l_item-quantity       = l_accounting-quantity - l_menge - l_grn_menge.
      ELSE.
        CLEAR: l_item-po_unit,  l_item-po_unit_iso,
               l_item-quantity, l_item-po_pr_uom.
      ENDIF.
      APPEND l_item TO l_items.
*Populate account data
      l_accounting-invoice_doc_item = l_index.
      IF l_zekkn NE 0.
        l_accounting-serial_no      = l_zekkn.
      ELSE.
        l_accounting-serial_no      = '01'.
      ENDIF.
      l_accounting-tax_code         = itab_bapi-mwskz.
      l_accounting-item_amount      = l_amt - l_wrbtr - l_grn_wrbtr.
*Convert internal amount to external amount
      CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
        EXPORTING
          currency        = l_header-currency
          amount_internal = l_accounting-item_amount
        IMPORTING
          amount_external = l_accounting-item_amount.
      IF ( l_pstyp NE '9' OR l_lebre IS NOT INITIAL ) AND l_pstyp NE '1'.
        l_accounting-po_unit        = itab_bapi-meins.
        l_accounting-po_pr_uom      = l_item-po_pr_uom.
        l_accounting-quantity       = l_item-quantity.
      ELSE.
        CLEAR: l_accounting-po_unit,  l_accounting-po_unit_iso,
               l_accounting-quantity, l_item-po_pr_uom.
      ENDIF.
      CLEAR:  l_accounting-gl_account,     l_accounting-costcenter, l_accounting-sd_doc,
              l_accounting-sdoc_item,      l_accounting-asset_no,   l_accounting-sub_number,
              l_accounting-orderid,        l_accounting-ref_date,   l_accounting-funds_ctr,
              l_accounting-fund,           l_accounting-grant_nbr,  l_accounting-bus_area,
              l_accounting-rl_est_key,     l_accounting-co_area,    l_accounting-costobject,
              l_accounting-profit_segm_no, l_accounting-profit_ctr, l_accounting-wbs_elem.
      IF l_pstyp NE '9' OR ( l_pstyp EQ '9' AND l_webre IS INITIAL ).
        SELECT SINGLE sakto kostl vbeln vbelp anln1 anln2 aufnr   dabrz fistl geber
                      grant_nbr   gsber imkey kokrs kstrg paobjnr prctr ps_psp_pnr
               INTO (l_accounting-gl_account,     l_accounting-costcenter, l_accounting-sd_doc,
                     l_accounting-sdoc_item,      l_accounting-asset_no,   l_accounting-sub_number,
                     l_accounting-orderid,        l_accounting-ref_date,   l_accounting-funds_ctr,
                     l_accounting-fund,           l_accounting-grant_nbr,  l_accounting-bus_area,
                     l_accounting-rl_est_key,     l_accounting-co_area,    l_accounting-costobject,
                     l_accounting-profit_segm_no, l_accounting-profit_ctr, l_accounting-wbs_elem)
           FROM ekkn WHERE ebeln EQ itab_bapi-ebeln AND ebelp EQ itab_bapi-ebelp AND
                           zekkn EQ l_accounting-serial_no.
        IF sy-subrc EQ 0.
          APPEND l_accounting TO l_accountings.
        ENDIF.
      ELSE.
        SELECT SINGLE sakto kostl vbeln vbelp anln1 anln2 aufnr dabrz fistl geber
                      grant_nbr   gsber imkey kokrs kstrg paobjnr prctr ps_psp_pnr
               INTO (l_accounting-gl_account,     l_accounting-costcenter, l_accounting-sd_doc,
                     l_accounting-sdoc_item,      l_accounting-asset_no,   l_accounting-sub_number,
                     l_accounting-orderid,        l_accounting-ref_date,   l_accounting-funds_ctr,
                     l_accounting-fund,           l_accounting-grant_nbr,  l_accounting-bus_area,
                     l_accounting-rl_est_key,     l_accounting-co_area,    l_accounting-costobject,
                     l_accounting-profit_segm_no, l_accounting-profit_ctr, l_accounting-wbs_elem)
           FROM eskn WHERE packno EQ l_item-ref_doc AND zekkn EQ l_accounting-serial_no.
        IF sy-subrc EQ 0.
          APPEND l_accounting TO l_accountings.
        ENDIF.
      ENDIF.
      IF l_webre IS INITIAL.
        EXIT.
      ENDIF.
    ENDSELECT.
    MOVE-CORRESPONDING itab_bapi TO w_bapi.
    AT END OF inv_no.
      SUM.
*Convert internal amount to external amount
      CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
        EXPORTING
          currency        = l_header-currency
          amount_internal = l_header-gross_amount
        IMPORTING
          amount_external = l_header-gross_amount.
*Post FI Invoice
      CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE'
        EXPORTING
          headerdata       = l_header
        IMPORTING
          invoicedocnumber = itab_bapi-belnr_new
        TABLES
          itemdata         = l_items
          accountingdata   = l_accountings
          return           = l_returns.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
*Retrieve Error Message
      LOOP AT l_returns INTO l_return.
        IF sy-tabix EQ 1.
          itab_bapi-err_msg = l_return-message.
        ELSE.
          CONCATENATE itab_bapi-err_msg ';' l_return-message INTO itab_bapi-err_msg.
        ENDIF.
      ENDLOOP.
      MODIFY itab_bapi TRANSPORTING belnr_new err_msg WHERE inv_no EQ itab_bapi-inv_no.
      CLEAR: l_header, l_item, l_accounting, l_index.
      REFRESH: l_items[], l_accountings[].
    ENDAT.
  ENDLOOP.
分享到:
评论

相关推荐

    BAPI_NETWORK_COMP_CHANGE 修改项目网络组件的BAPI

    ### BAPI_NETWORK_COMP_CHANGE:修改项目网络组件的BAPI #### 概述 在SAP系统中,项目管理(Project Management, PM)是用于管理和控制工程项目的重要工具之一。项目网络则是PM模块中用来规划和控制项目活动的关键...

    凭证导入BAPI_ACC_DOCUMENT_POST

    标题与描述中的“凭证导入 BAPI_ACC_DOCUMENT_POST”指向了一个特定的SAP系统功能,用于自动化导入财务凭证,尤其在银行流水与企业财务系统间建立接口时至关重要。此过程不仅涉及标准参数的传递,还触及了如何处理和...

    用BAPI_PO_CHANGE修改(ME23N)PO出貨日期的方法

    ### 使用BAPI_PO_CHANGE修改(ME23N)采购订单(PO)出货日期的方法 在SAP系统中,采购订单(Purchase Order, PO)管理是供应链管理中的一个关键环节,涉及采购请求、供应商选择、合同谈判等多个流程。在实际业务操作...

    BAPI_PO_CREATE.zip_bapi_po_create help

    通过JCo接口利用BAPI函数创建PO订单

    sap abap常用bapi

    6. BAPI_CHARACT_CREATE/BAPI_CHARACT_CHANGE/BAPI_CHARACT_DELETE/BAPI_CHARACT_RENAME/BAPI_CHARACT_GETDETAIL/BAPI_CHARACT_ADDLONGTEXT/BAPI_CHARACT_REMOVELONGTEXT/BAPI_CHARACT_GETLONGTEXT/CARD_...

    预置发票批量过帐

    调用BAPI函数:BAPI_INCOMINGINVOICE_POST实现批量MIR4功能。

    如何利用BAPI_SALESORDER_CHANGE修改vbak增强字段

    在本案例中,我们需要利用BAPI_SALESORDER_CHANGE来修改销售订单(Sales Order,简称SO)的抬头增强字段。这个需求在实际项目中常见,因为企业往往需要自定义一些标准字段来满足特定的业务需求。 首先,我们要了解...

    关于用BAPI批量创建工艺路线

    该解决方案是围绕 BAPI_ROUTING_CREATE 函数的使用,旨在解决 BAPI_ROUTING_CREATE 不可以创建定额工艺路线的问题。 首先,让我们了解 BAPI_ROUTING_CREATE 函数的基本参数。该函数主要包括三个参数:TASK、...

    BAPI_ACC_DOCUMENT_POST增强

    SAP系统中的BAPI_ACC_DOCUMENT_POST增强 在SAP系统中,会计凭证过账时,需要将预留预算清掉,但是在接口中没有该字段。为了解决这个问题,可以使用BAPI_ACC_DOCUMENT_POST中的EXTENSION2传入值来实现。下面是实现的...

    BAPI_NETWORK_MAINTAIN 下达项目网络的BAPI

    ### BAPI_NETWORK_MAINTAIN:下达项目网络的BAPI #### 概述 在SAP系统中,**BAPI_NETWORK_MAINTAIN** 是一个用于维护和管理项目网络的业务应用程序接口(Business Application Programming Interface,简称BAPI)...

    实用BAPI做内部调拨

    本示例中的"实用BAPI做内部调拨"是指使用BAPI_GOODSMVT_CREATE来执行物料在同一个工厂内的库存移动,即内部调拨。内部调拨在供应链管理中是一种常见的操作,用于调整库存位置,例如将物料从仓库的一个存储位置转移到...

    采购订单BAPI增强数值型字段转换方法.docx

    8. 再双击IF_EX_ME_BAPI_PO_CREATE_02~MAP2I_EXTENSIONIN。 9. 点击“是”。 10. 点击“是”。 11. 开始编辑代码。 在编辑代码时,我们需要使用ABAP语言来实现数值型字段的转换。下面是实现代码: ```abap DATA: lr...

    创建销售订单

    BAPI_SALESORDER_CREATEFROMDAT2是SAP系统中创建销售订单的API接口,通过调用这个接口,可以快速创建销售订单。 在创建销售订单时,需要设置订单的头信息、项目信息、伙伴信息和计划信息等。头信息包括订单类型、...

    BAPI-ACC-DOCUMENT-POST 结构字段扩展

    在SAP系统中,`BAPI_ACC_DOCUMENT_POST` 是一个重要的业务应用程序接口(BAPI),用于执行财务凭证的过账处理。但在实际应用过程中,可能会遇到现有BAPI无法完全满足特定业务需求的情况,比如需要额外的字段来存储...

    创建及修改物料主数据

    本文将详细介绍如何使用BAPI_TRANSACTION_COMMIT创建及修改物料主数据,包括物料主数据的创建和修改、物料描述的设置、客户端层次物料数据的设置、物料类型的设置等。 物料主数据的创建 在SAP系统中,物料主数据是...

    SAP 所有BAPI的清单

    SAP中很多情况下客户提出增强的需求时,我们通常会用到BAPI或者BADI。虽然你可以在SAP中直接输入TCd:BAPI来查找。但是并不是所有的BAPI都能查得到的。希望这个清单能够帮到你。

    【测试】SAP 委外加工收货BAPI Demo

    三、委外PO的收货(这里使用BAPI_GOODSMVT_CREATE) *&---------------------------------------------------------------------* *& Report ZTEST_WWJG *& *&---------------------------------------------------...

    LSMW_BAPI_MM02_CS01.xlsx

    LSMW_BAPI_MM02_CS01

    BAPI_GOODS的函数说明

    这是SAP ABAP 中BAPI GOODS的函数说明,有利于用户进行更深入的了解

    PP常用bapi.pdf

    - BAPI_ROUTING_CREATE:创建新的工艺路线。 - BAPI_ROUTING_EXISTENCE_CHECK:检查工艺路线是否已在系统中存在。 2. **参考操作集(Reference Operation Set)**: - BAPI_REFSETOFOPERATIONS_CREATE:创建参考...

Global site tag (gtag.js) - Google Analytics