SAP IDOC
阅读原文时间:2023年07月09日阅读:3

物料主数据
供应商主数据
价格档案
采购订单
采购计划协议
srm发货单
物料凭证
发票校验
发票校验过账或删除信息返回

  CALL FUNCTION 'BAPI\_INCOMINGINVOICE\_PARK'  
    EXPORTING  
      headerdata          = headerdata  
      addressdata         = addressdata  
    IMPORTING  
      invoicedocnumber    = invoicedocnumber  
      fiscalyear          = fiscalyear  
    TABLES  
      itemdata            = itemdata  
      accountingdata      = accountingdata  
      glaccountdata       = glaccountdata  
      materialdata        = materialdata  
      taxdata             = taxdata  
      withtaxdata         = withtaxdata  
      vendoritemsplitdata = vendoritemsplitdata  
      return              = return  
      extensionin         = extensionin.

*&---------------------------------------------------------------------*
*& Form prepare_bapi_invoice
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form prepare_bapi_invoice.

* Definitions
data: w_cnt type i,
w_netwr like ekpo-netwr,
w_menge like ekpo-menge.

data: begin of it_headerdata occurs 0.
include structure bapi_incinv_create_header.
data: end of it_headerdata.

data: begin of it_additional occurs 0.
include structure bapi_incinv_save_header_backgr.
data: end of it_additional.

data: begin of it_selectp occurs 0.
include structure bapi_incinv_select_po.
data: end of it_selectp.

data: begin of it_itemdata occurs 0.
include structure bapi_incinv_create_item.
data: end of it_itemdata.

data: begin of it_accountdata occurs 0.
include structure bapi_incinv_create_account.
data: end of it_accountdata.

data: begin of it_accdata occurs 0.
include structure bapi_incinv_create_gl_account.
data: end of it_accdata.

data: begin of it_taxdata occurs 0.
include structure bapi_incinv_create_tax.
data: end of it_taxdata.

data: begin of it_return occurs 0.
include structure bapiret2.
data: end of it_return.

* Fill tables for BAPI-Call
clear it_headerdata.
it_headerdata-ref_doc_no = save-refnr.
it_headerdata-invoice_ind = 'X'.
it_headerdata-doc_type = 'RN'.
it_headerdata-pstng_date = sy-datum.
if not save-redat is initial.
it_headerdata-doc_date = save-redat.
else.
it_headerdata-doc_date = sy-datum.
endif.
it_headerdata-comp_code = save-bukrs.
it_headerdata-currency = save-waers.
it_headerdata-gross_amount = save-wrbtr.
it_headerdata-supcountry = save-lndwe.
append it_headerdata.

clear it_additional.
it_additional-assign_deliv = 'X'.
it_additional-deliv_posting = 'S'.
it_additional-sel_goods = 'X'.
append it_additional.

clear: w_cnt, save-mwskz.
loop at xekpo.
* Read PO item
clear ekpo.
select single * from ekpo
where ebeln eq xekpo-ebeln
and ebelp eq xekpo-ebelp.
* Check if entry already exist
read table it_itemdata
with key po_number = xekpo-ebeln
po_item = xekpo-ebelp.
if sy-subrc = 0.
* Collect entry in item table
it_itemdata-item_amount = it_itemdata-item_amount + xekpo-netwr.
it_itemdata-quantity = it_itemdata-quantity + xekpo-menge.
modify it_itemdata index sy-tabix.
else.
* Load entry in item table
clear it_itemdata.
add 1 to w_cnt.
it_itemdata-invoice_doc_item = w_cnt.
it_itemdata-po_number = xekpo-ebeln.
it_itemdata-po_item = xekpo-ebelp.
it_itemdata-item_amount = xekpo-netwr.
it_itemdata-quantity = xekpo-menge.
it_itemdata-po_unit_iso = xekpo-meins.
it_itemdata-tax_code = xekpo-mwskz.
it_itemdata-item_text = xekpo-txz01.
* Set GR reference document
if not ekpo-webre is initial.
select * from ekbe
where ebeln eq xekpo-ebeln
and ebelp eq xekpo-ebelp
and vgabe eq '1'
and bewtp eq 'E'
and bwart eq '101'.
it_itemdata-ref_doc = ekbe-belnr.
it_itemdata-ref_doc_year = ekbe-gjahr.
it_itemdata-ref_doc_it = ekbe-buzei.
endselect.
endif.
append it_itemdata.
endif.
* Acoounting data
if not ekpo-twrkz is initial.
select * from ekkn
where ebeln eq xekpo-ebeln
and ebelp eq xekpo-ebelp.
clear it_accountdata.
it_accountdata-invoice_doc_item = w_cnt.
it_accountdata-serial_no = ekkn-zekkn.
it_accountdata-tax_code = xekpo-mwskz.
it_accountdata-gl_account = ekkn-sakto.
it_accountdata-costcenter = ekkn-kostl.
it_accountdata-sd_doc = ekkn-vbeln.
it_accountdata-sdoc_item = ekkn-vbelp.
it_accountdata-wbs_elem = ekkn-ps_psp_pnr.
w_netwr = xekpo-netwr * ekkn-vproz / 100.
it_accountdata-item_amount = w_netwr.
w_menge = xekpo-menge * ekkn-vproz / 100.
it_accountdata-quantity = w_menge.
it_accountdata-po_unit_iso = xekpo-meins.
append it_accountdata.
endselect.
endif.
* Determine MWSKZ for Header tax
if save-mwskz is initial.
save-mwskz = xekpo-mwskz.
elseif save-mwskz ne xekpo-mwskz.
save-mwskz = '??'.
endif.
endloop.
if save-mwskz = '??'.
clear save-mwskz.
endif.

clear w_cnt.
loop at it_cond where betrg ne 0.
clear it_accdata.
add 1 to w_cnt.
it_accdata-invoice_doc_item = w_cnt.
it_accdata-comp_code = save-bukrs.
it_accdata-gl_account = it_cond-skont.
it_accdata-tax_code = it_cond-mwskz.
it_accdata-item_amount = it_cond-betrg.
it_accdata-db_cr_ind = it_cond-shkzg.
it_accdata-costcenter = it_cond-kostl.
it_accdata-orderid = it_cond-aufnr.
it_accdata-item_text = save-name1.
append it_accdata.
endloop.

clear it_taxdata.
it_taxdata-tax_code = save-mwskz.
it_taxdata-tax_amount = save-wmwst.
append it_taxdata.

* Start of IDOC changes - Local
* =============================

select single * from yasekgfunct
where ybukrs = save-bukrs
and modulepool = 'LYAP_XIF01'
and cust_fn_id = '002'.

if sy-subrc = 0.
call function yasekgfunct-kgfunction
tables
t_itemdata = it_itemdata
t_glaccountdata = it_accdata
t_accountingdata = it_accountdata
t_taxdata = it_taxdata
changing
c_headerdata = it_headerdata.
endif.

* End of IDOC changes - Local
* ===========================

* Check if error message exist
if not it_headerdata-item_text is initial.
save-msgv2 = it_headerdata-item_text.
save-idocst = '51'.
exit.
endif.

*----------------------------------------------"Start
**Check if invoice already posted
* select single * from rbkp
* where xblnr eq save-refnr
* and lifnr eq save-lifnr
* and gjahr eq sy-datum(4)
* and rbstat ne '2'.
* if sy-subrc = 0.
* save-msgv2 = 'Invoice with same Reference number already exists'.
* save-idocst = '68'.
* exit.
* endif.
*----------------------------------------------"Ende
* Call BAPI
call function 'BAPI_INCOMINGINVOICE_PARK'
exporting
headerdata = it_headerdata
importing
invoicedocnumber = save-docnr
fiscalyear = save-gjahr
tables
itemdata = it_itemdata
glaccountdata = it_accdata
accountingdata = it_accountdata
taxdata = it_taxdata
return = it_return.

commit work.

* Processing depending on post status
if save-docnr is initial.
* Get error messages
save-idocst = '51'.
loop at it_return where type eq 'E'.
if sy-tabix eq 1.
save-msgv3 = it_return-message.
elseif sy-tabix eq 2.
save-msgv4 = it_return-message.
else.
exit.
endif.
endloop.
else.
* Determine FI number, could be different "Start
concatenate save-docnr save-gjahr into w_awkey.
select single * from bkpf
where bukrs eq save-bukrs
and awkey eq w_awkey. "Ende
if not bkpf-belnr is initial. "Start
save-fidoc = bkpf-belnr.
else.
save-fidoc = save-docnr.
endif. "Ende
* Start of local processing "Start
* =========================
select single * from yasekgfunct
where ybukrs = save-bukrs
and modulepool = 'LYAP_XIF01'
and cust_fn_id = '003'.
if sy-subrc = 0.
it_headerdata-inv_ref_no = save-docnr.
it_headerdata-inv_year = save-gjahr.
call function yasekgfunct-kgfunction
tables
t_itemdata = it_itemdata
t_glaccountdata = it_accdata
t_accountdata = it_accountdata
t_taxdata = it_taxdata
changing
c_headerdata = it_headerdata.
endif.
* End of local processing
* ======================= "End
endif.

endform. " prepare_bapi_invoice

*&---------------------------------------------------------------------
*& Func :
*& Author :*& Category: Update
*& Title Process inbound IDOC - Invoices (INVOIC)
*&---------------------------------------------------------------------
*& Note: Authority-check is not necessary here.
*&---------------------------------------------------------------------
*& Short description:
*& This function will be called by IDOC-Inbound
*& to load orders into SAP with IDOC-Processing
*&---------------------------------------------------------------------*&---------------------------------------------------------------------
function zidoc_input_invoic.
*"----------------------------------------------------------------------
*"*"Global Interface:
*" IMPORTING
*" VALUE(INPUT_METHOD) LIKE BDWFAP_PAR-INPUTMETHD
*" VALUE(MASS_PROCESSING) LIKE BDWFAP_PAR-MASS_PROC
*" EXPORTING
*" VALUE(WORKFLOW_RESULT) LIKE BDWF_PARAM-RESULT
*" VALUE(APPLICATION_VARIABLE) LIKE BDWF_PARAM-APPL_VAR
*" VALUE(IN_UPDATE_TASK) LIKE BDWFAP_PAR-UPDATETASK
*" VALUE(CALL_TRANSACTION_DONE) LIKE BDWFAP_PAR-CALLTRANS
*" TABLES
*" IDOC_CONTRL STRUCTURE EDIDC
*" IDOC_DATA STRUCTURE EDIDD
*" IDOC_STATUS STRUCTURE BDIDOCSTAT
*" RETURN_VARIABLES STRUCTURE BDWFRETVAR
*" SERIALIZATION_INFO STRUCTURE BDI_SER
*" EXCEPTIONS
*" WRONG_FUNCTION_CALLED
*"----------------------------------------------------------------------

loop at idoc_contrl.

* Initial workfields and internal table
clear: save.
refresh: xekpo, it_cond.
free: xekpo, it_cond.

* Get IDOC-Fields into workfields and internal table
loop at idoc_data where docnum eq idoc_contrl-docnum.
perform fill_data_invoic.
endloop.

*--------------------------------------------------"Start
* if save-bukrs eq '5400' "Start
* or save-bukrs eq '1500' "
* or save-bukrs eq '1900' "
* or save-bukrs eq '3000'. "
** fill IDOC_Status
* idoc_status-docnum = idoc_contrl-docnum.
* idoc_status-status = '68'.
* append idoc_status.
* else. "
** Write invoice with BAPI
* perform prepare_bapi_invoice.
** fill IDOC_Status
* if save-docnr is initial.
** Set IDOC status
* idoc_status-docnum = idoc_contrl-docnum.
* idoc_status-status = save-idocst.
* idoc_status-msgv1 = 'Invoice not posted'.
* idoc_status-msgv2 = save-msgv2.
* idoc_status-msgv3 = save-msgv3.
* idoc_status-msgv4 = save-msgv4.
* append idoc_status.
* else.
** Set IDOC status
* idoc_status-docnum = idoc_contrl-docnum.
* idoc_status-status = '53'.
* idoc_status-msgv1 = 'Inovice posted'.
* idoc_status-msgv2 = save-docnr.
* append idoc_status.
** Create document links (IDOC/Attachments)
* perform document_links.
* endif.
* endif. "
*--------------------------------------------------"Ende

*--------------------------------------------------"Start
* Get control information for company code
select single * from yapxiinvctrl
where bukrs eq save-bukrs
and lifnr eq save-lifnr.

w\_arch\_sp = yapxiinvctrl-langu.  
w\_arch\_id = yapxiinvctrl-archivid.

clear w\_xiinterf.                                    "Ins  

*--------------------------------------------------"Start
** { UC Insert
* l_string = idoc_contrl-arckey+20(16).
* call function 'HR_KR_STRING_TO_XSTRING'
* exporting
* codepage_to = '4110' "UTF-8 "UC
* unicode_string = l_string
* importing
* xstring_stream = l_xstring.
* w_ximessid-x = l_xstring.
* w_ximessid-c = idoc_contrl-arckey+36(32).
* w_ximessid-c25 = idoc_contrl-arckey+68(2).
** } UC Insert
*
** Holen Interfacename aus XI
* concatenate idoc_contrl-sndpor+3(3) '200EXCH' "Ins MB11112009
* into w_remote_system. "Ins MB11112009
*
* call function 'YAP_CHECK_ADS_MESSAGES'
* destination w_remote_system
* exporting
* messageid = w_ximessid
* importing
* interface_name = w_xiinterf.
*
** Check if the content repository is ready "
* if w_xiinterf cs 'ADS'.
* select single arc_doc_id from toa01 into w_arch_docid
* where archiv_id eq w_arch_id.
* call function 'ARCHIVOBJECT_STATUS'
* exporting
* archiv_doc_id = w_arch_docid
* archiv_id = w_arch_id
* importing
* archiv_object_status = w_object_status
* exceptions
* error_archiv = 1
* error_communicationtable = 2
* error_kernel = 3
* others = 4.
* call function 'SCMS_HTTP_SERVER_INFO'
* exporting
* crep_id = w_arch_id
* importing
* contrep_status = w_contrep_status
* exceptions
* bad_request = 1
* internal_server_error = 2
* error_http = 3
* error_url = 4
* error_signature = 5
* contrep_not_found = 6
* others = 7.
* if sy-subrc > 0.
* idoc_status-docnum = idoc_contrl-docnum.
* idoc_status-status = '51'.
* idoc_status-msgv1 = 'Document not posted'.
* idoc_status-msgv2 = 'Archive not ready'.
* append idoc_status.
* continue.
* endif.
* endif.
*--------------------------------------------------"Ende Del

* Processing depending on control table
case yapxiinvctrl-process.

  when '0'.                                         "Do nothing (set status=68)

    idoc\_status-docnum = idoc\_contrl-docnum.  
    idoc\_status-status = '68'.  
    idoc\_status-msgv1  = 'Document not posted'.  
    idoc\_status-msgv2 = 'No action required'.  
    append idoc\_status.

  when '1'.                                         "Post regular (BAPI\_INCOMINGINVOICE\_PARK)

*--------------------------------------------------"Start
** Check if non ADS invoice must be posted
* if yapxiinvctrl-ads eq 'X'
* and w_xiinterf ns 'ADS'.
* idoc_status-docnum = idoc_contrl-docnum.
* idoc_status-status = '68'.
* idoc_status-msgv1 = 'Invoice not posted'.
* idoc_status-msgv2 = 'Not ADS invoice, no action required'.
* append idoc_status.
* continue.
* endif.
*--------------------------------------------------"Ende

* Check if document already posted
select single * from rbkp
where xblnr eq save-refnr
and lifnr eq save-lifnr
and gjahr eq sy-datum(4)
and rbstat ne '2'.
if sy-subrc = 0.
idoc_status-docnum = idoc_contrl-docnum.
idoc_status-status = '68'.
idoc_status-msgv1 = 'Invoice not posted'.
idoc_status-msgv2 = 'Invoice with same Reference number already exists'.
append idoc_status.
continue.
endif.

* Post invoice with BAPI
perform prepare_bapi_invoice.

    if save-docnr is initial.  
      idoc\_status-docnum = idoc\_contrl-docnum.  
      idoc\_status-status = save-idocst.  
      idoc\_status-msgv1  = 'Invoice not posted'.  
      idoc\_status-msgv2  = 'Error in BAPI'.  
      idoc\_status-msgv2  = save-msgv2.  
      idoc\_status-msgv3  = save-msgv3.  
      idoc\_status-msgv4  = save-msgv4.  
      append idoc\_status.  
    else.  
      idoc\_status-docnum = idoc\_contrl-docnum.  
      idoc\_status-status = '53'.  
      idoc\_status-msgv1  = 'Invoice posted'.  
      idoc\_status-msgv2  = save-docnr.  
      append idoc\_status.

* Create document links
perform document_links.

    endif.

  when '2'.                                           "Will be posted by Readsoft (deactivated )

*--------------------------------------------------"Start
idoc_status-docnum = idoc_contrl-docnum.
idoc_status-status = '68'.
idoc_status-msgv1 = 'Document not posted'.
idoc_status-msgv2 = 'No action required'.
append idoc_status.
*--------------------------------------------------"Ende
*--------------------------------------------------"Start
* call function '/COCKPIT/EDI_IDOC_TRANSFORM'
* exporting
* input_method = input_method
* mass_processing = mass_processing
* importing
* workflow_result = workflow_result
* application_variable = application_variable
* in_update_task = in_update_task
* call_transaction_done = call_transaction_done
* tables
* idoc_contrl = idoc_contrl
* idoc_data = idoc_data
* idoc_status = idoc_status
* return_variables = return_variables
* serialization_info = serialization_info.
*--------------------------------------------------"Ende Del

endcase.  

*--------------------------------------------------"Ende

endloop.

endfunction.