ABAP 预制发票 调整物料金额 ‘BAPI_INCOMINGINVOICE_PARK’

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_materialdata LIKE TABLE OF bapi_incinv_create_material,
ls_materialdata LIKE bapi_incinv_create_material,
lt_glaccountdata LIKE TABLE OF bapi_incinv_create_gl_account,
ls_glaccountdata LIKE bapi_incinv_create_gl_account,
lt_taxdata LIKE TABLE OF bapi_incinv_create_tax,
ls_taxdata LIKE bapi_incinv_create_tax,
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.

CLEAR:ls_headerdata,lv_invoicedocnumber,lv_fiscalyear,lv_rblgp,ls_glaccountdata.
REFRESH:lt_itemdata,lt_materialdata,lt_glaccountdata,lt_taxdata,lt_return,lt_glaccountdata.

"header
ls_headerdata-invoice_ind = ‘X’. "发票标志
ls_headerdata-doc_type = ‘RE’. "凭证类型
ls_headerdata-comp_code = wa_head-bukrs. "公司代码
ls_headerdata-doc_date = wa_head-bldat. "凭证日期
ls_headerdata-pstng_date = wa_head-budat. "过账日期
ls_headerdata-bline_date = wa_head-zfbdt. "到期日期计算的起算日期
ls_headerdata-diff_inv = wa_head-lifnr. "出票方
ls_headerdata-gross_amount = wa_head-rmwwr. "总金额(调整后金额)
IF ls_headerdata-gross_amount < 0.
ls_headerdata-gross_amount = ls_headerdata-gross_amount * -1.
ls_headerdata-invoice_ind = ‘’.
ENDIF.
ls_headerdata-currency = wa_head-waers. "货币码
ls_headerdata-header_txt = wa_head-bktxt. "抬头文本
ls_headerdata-item_text = wa_head-sgtxt. "文本
ls_headerdata-ref_doc_no = wa_head-xblnr. "参考凭证编号
ls_headerdata-pmnttrms = wa_head-zterm. "付款条款
ls_headerdata-deliv_posting = ‘S’.
ls_headerdata-return_posting = ‘H’.

IF wa_head-waers = ‘JPY’.
ls_headerdata-gross_amount = ls_headerdata-gross_amount * 100.
ENDIF.

"item / tax
LOOP AT gt_out INTO wa_out WHERE sel = ‘X’.

  • lv_rblgp = lv_rblgp + 1.
    CLEAR: ls_itemdata,lv_bpumz,lv_bpumn."ls_taxdata.

  • ls_itemdata-invoice_doc_item = lv_rblgp. "行项目
    ls_itemdata-ref_doc_year = wa_out-gjahr. "物料凭证财年
    IF wa_out-bwart =‘105’ OR wa_out-bwart =‘106’ OR wa_out-bwart =‘122’ OR wa_out-bwart =‘123’ OR wa_out-bwart =‘162’ OR wa_out-bwart =‘102’ .
    ls_itemdata-ref_doc_year = wa_out-lfgja. "物料凭证财年
    ls_itemdata-ref_doc = wa_out-lfbnr. "参考物料凭证
    ls_itemdata-ref_doc_it = wa_out-lfpos. "参考物料凭证项目
    ELSE.
    ls_itemdata-ref_doc_year = wa_out-gjahr. "物料凭证财年
    ls_itemdata-ref_doc = wa_out-belnr. "参考物料凭证
    ls_itemdata-ref_doc_it = wa_out-buzei. "参考物料凭证项目
    ENDIF.

ls_itemdata-po_number    = wa_out-ebeln.            "采购订单号
ls_itemdata-po_item      = wa_out-ebelp.              "采购凭证的项目编号
ls_itemdata-quantity     =  wa_out-bpmng.             "数量
  • ls_itemdata-quantity = abs( wa_out-bpmng ). "数量
    ls_itemdata-item_amount = wa_out-netwr. "未税金额
    IF wa_head-waers = ‘JPY’.
    ls_itemdata-item_amount = ls_itemdata-item_amount * 100.
    ENDIF.
    SELECT SINGLE meins bprme bpumz bpumn INTO ( ls_itemdata-po_unit,ls_itemdata-po_pr_uom,lv_bpumz,lv_bpumn )
    FROM ekpo WHERE ebeln = wa_out-ebeln AND ebelp = wa_out-ebelp.
    IF lv_bpumn = ‘’.
    lv_bpumn = 1.
    ENDIF.
    ls_itemdata-po_pr_qnt = wa_out-bpmng * lv_bpumz / lv_bpumn. "采购订单价格单位的数量

  • ls_itemdata-po_pr_qnt = abs( wa_out-bpmng * lv_bpumz / lv_bpumn ). "采购订单价格单位的数量
    ls_itemdata-tax_code = wa_out-mwskz. "税码

  • APPEND ls_itemdata TO lt_itemdata.
    COLLECT ls_itemdata INTO lt_itemdata.

  • ls_taxdata-tax_code = wa_out-mwskz.

  • ls_taxdata-tax_amount = wa_out-zse.

  • COLLECT ls_taxdata INTO lt_taxdata.

ENDLOOP.

LOOP AT lt_itemdata INTO ls_itemdata.
lv_rblgp = lv_rblgp + 1.
ls_itemdata-invoice_doc_item = lv_rblgp. "行项目
ls_itemdata-quantity = abs( ls_itemdata-quantity ).
ls_itemdata-po_pr_qnt = abs( ls_itemdata-po_pr_qnt ).
ls_itemdata-item_amount = abs( ls_itemdata-item_amount ). "20220807-yuchengyi-add
"20221111-yuchengyi-工序委外PO设置最后发票标识 begin

  • IF ls_itemdata-item_amount <> 0 AND ls_itemdata-po_number+0(2) = ‘84’.

  •  CLEAR lw_ekpo.
    
  •  SELECT SINGLE * INTO CORRESPONDING FIELDS OF lw_ekpo FROM ekpo WHERE ebeln = ls_itemdata-po_number AND ebelp = ls_itemdata-po_item.
    
  •  IF sy-subrc = 0.
    
  •    IF lw_ekpo-matnr = '' AND lw_ekpo-knttp = 'F'.
    
  •      ls_itemdata-final_inv = 'X'.
    
  •    ENDIF.
    
  •  ENDIF.
    
  • ENDIF.
    "20221111-yuchengyi-工序委外PO设置最后发票标识 end
    MODIFY lt_itemdata FROM ls_itemdata TRANSPORTING invoice_doc_item quantity po_pr_qnt item_amount ."final_inv.
    ENDLOOP.
    "调整金额
    IF wa_head-netwr_mod <> 0.
    CLEAR ls_materiald
    ata.
    READ TABLE gt_out INTO wa_out WITH KEY matnr = wa_head-matnr
    sel = ‘X’.
    IF sy-subrc = 0.
    READ TABLE lt_itemdata INTO ls_itemdata WITH KEY ref_doc = wa_out-belnr
    ref_doc_it = wa_out-buzei.
    IF sy-subrc = 0.
    ls_materialdata-invoice_doc_item = ls_itemdata-invoice_doc_item. "发票凭证中的凭证项目
    ls_materialdata-material = wa_head-matnr. "物料
    ls_materialdata-val_area = wa_out-werks. "估价范围
    IF wa_head-netwr_mod > 0.
    ls_materialdata-db_cr_ind = ‘S’. "借贷标识
    ELSE.
    ls_materialdata-db_cr_ind = ‘H’. "借贷标识
    ENDIF.
    ls_materialdata-item_amount = abs( wa_head-netwr_mod ). "金额
    ls_materialdata-quantity = abs( wa_out-bpmng ). "数量
    SELECT SINGLE meins INTO ls_materialdata-base_uom FROM mara WHERE matnr = wa_head-matnr.
    IF wa_head-mwskz1 = ‘’.
    ls_materialdata-tax_code = ‘J0’. "税码
    ELSE.
    ls_materialdata-tax_code = wa_head-mwskz1. "税码
    ENDIF.

    IF wa_head-waers = ‘JPY’.
    ls_materialdata-item_amount = ls_materialdata-item_amount * 100.
    ENDIF.
    APPEND ls_materialdata TO lt_materialdata.

    "税

  •    IF wa_head-wmwst <> wa_head-wmwst2.
    
  •      CLEAR ls_taxdata.
    
  •      ls_taxdata-tax_code = wa_head-mwskz1. "wa_head-wmwst - wa_head-wmwst2.
    
  •      ls_taxdata-tax_amount   = wa_head-netwr_mod * wa_head-zsl.
    
  •      COLLECT ls_taxdata INTO lt_taxdata.
    
  •    ENDIF.
    ENDIF.
    

    ENDIF.
    ENDIF.
    CHECK lt_itemdata[] IS NOT INITIAL.

    CALL FUNCTION ‘BAPI_INCOMINGINVOICE_PARK’
    EXPORTING
    headerdata = ls_headerdata
    IMPORTING
    invoicedocnumber = lv_invoicedocnumber
    fiscalyear = lv_fiscalyear
    TABLES
    itemdata = lt_itemdata
    materialdata = lt_materialdata
    glaccountdata = lt_glaccountdata
    taxdata = lt_taxdata
    return = lt_return.

    IF lv_invoicedocnumber IS INITIAL OR lv_fiscalyear IS INITIAL.
    CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.

    LOOP AT lt_return INTO ls_return.
    CONCATENATE lv_message ls_return-message INTO lv_message.
    ENDLOOP.

    lv_message = ‘预制发票失败:’ && lv_message.
    MESSAGE lv_message TYPE ‘I’.
    ELSE.

    CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
    EXPORTING
    wait = ‘X’.
    ENDIF.

你可能感兴趣的:(SAP-MM,SAP-ABAP,ABAP,SAP)