MIRO发票校验BAPI_INCOMINGINVOICE_CREATE (2013-01-23 10:01:29)

时间:2023-12-21 09:35:38

form frm_invoice_create2 .
  data: str type string.
  data: ls_headerdata       like bapi_incinv_create_header,
        lt_itemdata         like table of bapi_incinv_create_item,
        ls_itemdata         like bapi_incinv_create_item,
        lt_return           like table of bapiret2,
        ls_return           like bapiret2,
        lv_invoicedocnumber like bapi_incinv_fld-inv_doc_no,
        lv_fiscalyear       like bapi_incinv_fld-fisc_year.
  data: lv_price1           type bapi_rmwwr.
  data: lv_item             type n length 6.
  data: lv_retpo type retpo.
  clear:l_menge,ls_headerdata,lv_fiscalyear,lv_price1,lv_item.

* 处理退货和非退货项目
  clear: gt_ret,gt_ret[],gt_inv,gt_inv[].
  loop at gt_alv into gs_alv where source = 'EKBE'.
    clear: lv_retpo.
    select single retpo into lv_retpo  from ekpo where ebeln = gs_alv-ebeln and ebelp = gs_alv-ebelp.
    loop at gt_invoice into gs_invoice where ebeln = gs_alv-ebeln and ebelp = gs_alv-ebelp.
*---------根据退货标识 放入不同的内表
      if lv_retpo = 'X'."退货项目
        if gs_invoice-shkzg = 'H'.
          gs_invoice-dmbtr = 0 - gs_invoice-dmbtr.
        endif.
        gt_ret-ebeln = gs_invoice-ebeln."采购订单
        gt_ret-ebelp = gs_invoice-ebelp."行项目
        gt_ret-lfbnr = gs_invoice-lfbnr. "参考凭证号
        gt_ret-lfpos = gs_invoice-lfpos."行项目
        gt_ret-lfgja = gs_invoice-lfgja."年度
        gt_ret-mwskz = gs_invoice-mwskz."税码
        gt_ret-dmbtr = gs_invoice-dmbtr. "金额
        gt_ret-menge = gs_invoice-menge. "数量
        select single meins into gt_ret-meins from ekpo where ebeln = gs_invoice-ebeln and ebelp = gs_invoice-ebelp.
        collect gt_ret.
      else.
        if gs_invoice-shkzg = 'H'.
          gs_invoice-dmbtr = 0 - gs_invoice-dmbtr.
        endif.
        gt_inv-ebeln = gs_invoice-ebeln."采购订单
        gt_inv-ebelp = gs_invoice-ebelp."行项目
        gt_inv-lfbnr = gs_invoice-lfbnr. "参考凭证号
        gt_inv-lfpos = gs_invoice-lfpos."行项目
        gt_inv-lfgja = gs_invoice-lfgja."年度
        gt_inv-mwskz = gs_invoice-mwskz."税码
        gt_inv-dmbtr = gs_invoice-dmbtr. "金额
        gt_inv-menge = gs_invoice-menge. "数量
        select single meins into gt_inv-meins from ekpo where ebeln = gs_invoice-ebeln and ebelp = gs_invoice-ebelp.
        collect gt_inv.
      endif.
    endloop.
  endloop.
*---正常订单发票校验
  clear: ls_headerdata.
  ls_headerdata-invoice_ind  = 'X'.    "当采购订单行项目是正常订单时该标识必为X,为miro界面上的业务处理为1.发票否则为2.贷方凭证(invoice_ind='X'表示miro界面最上部的业务处理是1.发票,invoice_ind='', 表示miro界面最上部的业务处理为2.贷方凭证)

ls_headerdata-doc_type     = 'YX'.
  ls_headerdata-doc_date     = p_datum.
  ls_headerdata-pstng_date   = g_post_date.
  ls_headerdata-bline_date   = g_post_date.    "基线日期/付款计算的起始日期
  ls_headerdata-comp_code    = p_bukr2.
  ls_headerdata-currency     = 'CNY'.
  ls_headerdata-calc_tax_ind = 'X'. "自动结算税额
  ls_headerdata-bus_area     = '9999'.
  clear: lv_item.
  loop at gt_inv.
    lv_item = lv_item + 1.   "发票行项目号
    ls_itemdata-invoice_doc_item = lv_item.
    ls_itemdata-po_number        = gt_inv-ebeln.
    ls_itemdata-po_item          = gt_inv-ebelp.
    ls_itemdata-ref_doc          = gt_inv-lfbnr.
    ls_itemdata-ref_doc_year     = gt_inv-lfgja.
    ls_itemdata-ref_doc_it       = gt_inv-lfpos.
    ls_itemdata-item_amount      = gt_inv-dmbtr.
    ls_itemdata-quantity         = gt_inv-menge.
    ls_itemdata-po_unit          = gt_inv-meins.
    if gt_ret-mwskz is not initial.
      ls_itemdata-tax_code         = gt_inv-mwskz.
    else.
      ls_itemdata-tax_code         = p_mwskz.
    endif.
    append ls_itemdata to lt_itemdata.
    clear: ls_itemdata.

lv_price1 = lv_price1 + abs( gt_inv-dmbtr ).
  endloop.
  ls_headerdata-gross_amount = lv_price1 * ( 100 + l_mwskz ) / 100.    "总金额

refresh: lt_return.clear: ls_return,invoicedocnumber.
  call function 'BAPI_INCOMINGINVOICE_CREATE'
    exporting
      headerdata       = ls_headerdata
    importing
      invoicedocnumber = invoicedocnumber
    tables
      itemdata         = lt_itemdata
      return           = lt_return.

loop at lt_return into ls_return where type = 'E' or type = 'A'.
    cp_eind = 'X'.
    exit.
  endloop.
  if cp_eind ne 'X'.
    call function 'BAPI_TRANSACTION_COMMIT'
      exporting
        wait = 'X'.

*---插入值到表ZTFI014
    gs_ztfi014-tcode = sy-tcode.
    gs_ztfi014-seqno = p_datum.
    gs_ztfi014-objnr = p_bukr2.
    gs_ztfi014-gjahr = p_datum(4).
    gs_ztfi014-bukrs = p_bukrs.
    gs_ztfi014-belnr = invoicedocnumber.

modify ztfi014 from gs_ztfi014.
  else.
    call function 'BAPI_TRANSACTION_ROLLBACK'.

loop at lt_return into ls_return.
      clear gs_log.
      gs_log-type = ls_return-type.
      gs_log-docno = invoicedocnumber.
      call function 'MESSAGE_TEXT_BUILD'
        exporting
          msgid               = ls_return-id
          msgnr               = ls_return-number
          msgv1               = ls_return-message_v1
          msgv2               = ls_return-message_v2
          msgv3               = ls_return-message_v3
          msgv4               = ls_return-message_v4
        importing
          message_text_output = gs_log-message.

case gs_log-type.
        when 'S'.
          gs_log-icon = '3'.
        when 'W'.
          gs_log-icon = '2'.
        when 'E' or 'A'.
          gs_log-icon = '1'.
        when others.
      endcase.

append gs_log to gt_log.
      clear ls_return.
    endloop.

message e001(00) with '第一张发票预制失败,请点击显示消息查看详情!'.
  endif.
*---退货订单发票校验
  clear: ls_headerdata-invoice_ind,ls_headerdata-gross_amount.  "发票标志 退货是该标识必须为空
  clear: lv_item,ls_itemdata,ls_return,invoicedocnumber,lv_price1.
  refresh: lt_itemdata,lt_return.
  loop at gt_ret.
    lv_item = lv_item + 1.   "发票行项目号
    ls_itemdata-invoice_doc_item = lv_item.
    ls_itemdata-po_number        = gt_ret-ebeln.
    ls_itemdata-po_item          = gt_ret-ebelp.
    ls_itemdata-ref_doc          = gt_ret-lfbnr.
    ls_itemdata-ref_doc_year     = gt_ret-lfgja.
    ls_itemdata-ref_doc_it       = gt_ret-lfpos.
    ls_itemdata-item_amount      = abs( gt_ret-dmbtr ).
    ls_itemdata-quantity         = gt_ret-menge.
    ls_itemdata-po_unit          = gt_ret-meins.
    if gt_inv-mwskz is not initial.
      ls_itemdata-tax_code         = gt_ret-mwskz.
    else.
      ls_itemdata-tax_code         = p_mwskz.
    endif.
    append ls_itemdata to lt_itemdata.
    clear: ls_itemdata.

lv_price1 = lv_price1 + abs( gt_ret-dmbtr ).
  endloop.
  ls_headerdata-gross_amount = lv_price1 * ( 100 + l_mwskz ) / 100.    "总金额

refresh: lt_return.clear: ls_return.
  call function 'BAPI_INCOMINGINVOICE_CREATE'
    exporting
      headerdata       = ls_headerdata
    importing
      invoicedocnumber = invoicedocnumber
    tables
      itemdata         = lt_itemdata
      return           = lt_return.

loop at lt_return into ls_return where type = 'E' or type = 'A'.
    cp_eind = 'X'.
    exit.
  endloop.
  if cp_eind ne 'X'.
    call function 'BAPI_TRANSACTION_COMMIT'
      exporting
        wait = 'X'.

*---插入值到表ZTFI014
    gs_ztfi014-tcode = sy-tcode.
    gs_ztfi014-seqno = p_datum.
    gs_ztfi014-objnr = p_bukr2.
    gs_ztfi014-gjahr = p_datum(4).
    gs_ztfi014-bukrs = p_bukrs.
    gs_ztfi014-belnr = invoicedocnumber.

modify ztfi014 from gs_ztfi014.

else.
    call function 'BAPI_TRANSACTION_ROLLBACK'.

loop at lt_return into ls_return.
      clear gs_log.
      gs_log-type = ls_return-type.
      gs_log-docno = invoicedocnumber.
      call function 'MESSAGE_TEXT_BUILD'
        exporting
          msgid               = ls_return-id
          msgnr               = ls_return-number
          msgv1               = ls_return-message_v1
          msgv2               = ls_return-message_v2
          msgv3               = ls_return-message_v3
          msgv4               = ls_return-message_v4
        importing
          message_text_output = gs_log-message.

case gs_log-type.
        when 'S'.
          gs_log-icon = '3'.
        when 'W'.
          gs_log-icon = '2'.
        when 'E' or 'A'.
          gs_log-icon = '1'.
        when others.
      endcase.

append gs_log to gt_log.
      clear ls_return.
    endloop.

message e001(00) with '第二张发票预制失败,请点击显示消息查看详情!'.
  endif.
endform.                    " FRM_INVOICE_CREATE2

注意:

在PO收货后,做发票校验,发现一个问题,就是在headerdata中有个参数 calc_tax_ind ,如果将这个值‘X’,那么在

ME23N中会看不到生成的发票校验凭证invoicedocnumber的值。

headerdata中的必输值:

invoice_ind    为'X'

doc_date

pstng_date

comp_code

currency

gross_amount

itemdata中的必输值:

invoice_doc_item

po_number

po_item

item_amount

quantity

po_unit

ref_doc

ref_doc_year

ref_doc_it