BAPI_INCOMINGINVOICE_CREATE usage

11146 단어 create
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.

좋은 웹페이지 즐겨찾기