Citation: http://blog.csdn.net/champaignwolf/article/details/51422329
FUNCTION Zincominginvoice_park.
*"----------------------------------------------------------------------
* "*" Local Interface:
* "Exporting
* "VALUE (CODE) TYPE CHAR01
* "VALUE (MESSAGE) TYPE CHAR200
* "TABLES
* "INPUT STRUCTURE zfc_incominginvoice_park_input OPTIONAL
*"----------------------------------------------------------------------
Data:vp_eind (1),
Ls_headerdataLike Bapi_incinv_create_header,
Lt_itemdataLikeTABLEof Bapi_incinv_create_item,
Ls_itemdataLike Bapi_incinv_create_item,
Lt_materialdataLikeTABLEof Bapi_incinv_create_material,
Ls_materialdataLike Bapi_incinv_create_material,
Lt_taxdataLikeTABLEof Bapi_incinv_create_tax,
Ls_taxdataLike Bapi_incinv_create_tax,
Lt_returnLikeTABLEof Bapiret2,
Ls_returnLike Bapiret2,
Lv_invoicedocnumberLike Bapi_incinv_fld-inv_doc_no,
Lv_fiscalyearLike Bapi_incinv_fld-fisc_year.
Data:lv_mwskzLike Ekpo-mwskz.
Data:lv_rblgpTYPE RBLGP.
*1/3 invoice_ind= ' X ' db_cr_ind= ' S ' (head Up), 2/4 invoice_ind= "db_cr_ind= ' H ' (external item related items)
*1/2 de_cre_ind = ", 3/4 de_cre_ind= ' X ' (PO Purchase order related items)
* BAPI Assignment
CLEARInput.
READTABLEInputINDEX1.
IF SY-SUBRCEQ0.
Ls_headerdata-invoice_ind =' X '."Sp_head-xrech. The invoice flag
* Ls_headerdata-doc_type = ' RE '. " Sp_head-blart.
Ls_headerdata-doc_date = Sy-datum."Sp_head-bldat.
Ls_headerdata-pstng_date = Sy-datum."Sp_head-budat.
Ls_headerdata-bline_date = Sy-datum."SP_HEAD-ZFBDT. The start date of the baseline date/payment calculation
Ls_headerdata-comp_code =' 6100 '."Sp_head-bukrs.
SELECTSingle
Waers
Into ls_headerdata-Currency
From Ekko
WHERE Ebeln =Input-ebeln.
* Ls_headerdata-gross_amount = ' 24 '. " SP_HEAD-RMWWR. The total amount
* ls_headerdata-del_costs = Sp_head-beznk. "Unplanned Delivery Charges
* Ls_headerdata-pmnttrms = sp_head-zterm. "Payment Terms
* Ls_headerdata-alloc_nmbr = Sp_head-zuonr. "Assigned number/External invoice number
* LS_HEADERDATA-DIFF_INV = Sp_head-lifnr. "Ticketing Party/Supplier
* Ls_headerdata-ref_doc_no = Sp_head-xblnr.
Ls_headerdata-header_txt =Input-bktxt."' 123 '." Sp_head-bktxt.
Ls_headerdata-item_text =Input-sgtxt."' 456 '." Sp_head-sgtxt.
ENDIF.
CLEAR LV_RBLGP.
LOOPAtInput.
LV_RBLGP = LV_RBLGP +1.
CLEAR Lv_mwskz.
SELECTSingle
Mwskz
Meins
Into (Lv_mwskz,ls_itemdata-po_unit)
From Ekpo
WHERE Ebeln =Input-ebeln
and ebelp =Input-ebelp.
Ls_headerdata-gross_amount = Ls_headerdata-gross_amount +Input-wrbtr.
Ls_itemdata-invoice_doc_item = LV_RBLGP."Ls_poitem-buzei. The invoice line item number
Ls_itemdata-po_number =Input-ebeln."' 4500038014 '." Ls_poitem-ebeln.
Ls_itemdata-po_item =Input-ebelp."' 10 '." Ls_poitem-ebelp.
SELECTSingle
Belnr
Gjahr
Buzei
Into (LS_ITEMDATA-REF_DOC,LS_ITEMDATA-REF_DOC_YEAR,LS_ITEMDATA-REF_DOC_IT)
From Ekbe
WHERE Ebeln =Input-ebeln
and ebelp =Input-ebelp
and BELNR =Input-mblnr.
* Ls_itemdata-ref_doc = ' 5000188866 '. " LS_POITEM-LFBNR.
* ls_itemdata-ref_doc_year = ' 2016 '. " Ls_poitem-lfgja.
* Ls_itemdata-ref_doc_it = ' 1 '. " Ls_poitem-lfpos.
Ls_itemdata-item_amount =Input-wrbtr."' 24 '." Ls_poitem-wrbtr.
Ls_itemdata-quantity =Input-menge."' 2 '." Ls_poitem-menge.
* Ls_itemdata-po_unit = ' EA '. Ls_poitem-meins.
Ls_itemdata-tax_code = Lv_mwskz."Ls_poitem-mwskz.
APPEND Ls_itemdataTo Lt_itemdata.
Clear:ls_itemdata.
"LOOP at Tp_baitem to Ls_baitem."
Ls_materialdata-invoice_doc_item = LV_RBLGP."Ls_baitem-buzei.
Ls_materialdata-material =Input-matnr."' VBJHH05000000000 '." Ls_baitem-matnr.
Ls_materialdata-val_area =' 6100 '."Ls_baitem-bwkey. "Scope of assessment/factory
Ls_materialdata-valuation_type =' 6100 '."Ls_baitem-bwtar. The evaluation type
Ls_materialdata-db_cr_ind =' S '."Ls_baitem-shkzg. "Debit/Credit identification
Ls_materialdata-item_amount =' 0 '."LS_BAITEM-WRBTR.
Ls_materialdata-quantity =Input-menge."Ls_baitem-menge.
SELECTSingle
Meins
Into Ls_materialdata-base_uom
From Mara
WHERE Matnr =Input-matnr.
* Ls_materialdata-base_uom = ' EA '. Ls_baitem-meins.
Ls_materialdata-tax_code = Lv_mwskz."Ls_baitem-mwskz.
APPEND Ls_materialdataTo Lt_materialdata.
Clear:ls_materialdata.
Ls_taxdata-tax_code = Lv_mwskz."Ls_taitem-mwskz.
"Ls_taxdata-tax_amount =". Ls_taitem-wrbtr.
APPEND Ls_taxdataTo Lt_taxdata.
Clear:ls_taxdata.
Endloop.
PagerFUNCTION' Bapi_incominginvoice_park '
Exporting
Headerdata = Ls_headerdata
* Addressdata =
Importing
Invoicedocnumber = Lv_invoicedocnumber
FiscalYear = Lv_fiscalyear
TABLES
ItemData = Lt_itemdata
* Accountingdata =
* Glaccountdata =
Materialdata = Lt_materialdata
Taxdata = Lt_taxdata
* Withtaxdata =
* Vendoritemsplitdata =
return = Lt_return.
IF Lv_invoicedocnumberIsINITIALOR Lv_fiscalyearIsINITIAL.
Vp_eind =' X '.
ENDIF.
IF Vp_eind =' X '.
PagerFUNCTION' Bapi_transaction_rollback '.
Code =' 1 '.
LOOPAt Lt_returnInto Ls_returnWHEREType =' E '.
IFMessageIsINITIAL.
message = ls_return-Message.
ELSE.
ConcatenateMessage'; ' ls_return-MessageIntoMessage.
ENDIF.
Concatenate' Pre-fabricated invoice failed: 'MessageIntomessage.
endloop.
else.
call function Bapi_transaction_commit '
EXPORTING
wait = ' X '.
code = ' 0 '.
concatenate prefabricated invoice success, invoice number: ' lv_ Invoicedocnumber ' Invoice year: ' lv_fiscalyear INTO message.
endif.
endfunction.
Function Use five: MIR7 invoice prefabrication Bapi_incominginvoice_park