SAP 费用性采购申请创建 demo

SAP 费用性采购申请创建 demo

METHOD zcl_ii_si_mmi015_s4_ib_syn~si_mmi015_s4_ib_syn.
*** **** INSERT IMPLEMENTATION HERE **** ***

DATA: ls_item_req_header TYPE zcl_dt_mmi015_s4_req_head.
DATA: lt_item_req_item TYPE zcl_dt_mmi015_s4_req_item_tab,
      ls_item_req_item LIKE LINE OF lt_item_req_item.

DATA:  lt_return_res TYPE zcl_dt_mmi015_s4_res_retur_tab.
DATA:  ls_return_res LIKE LINE OF lt_return_res.

DATA:w_head      TYPE bapimereqheader,
     w_headx     TYPE bapimereqheaderx,
     t_new_item  TYPE TABLE OF bapimereqitemimp,
     i_new_item  LIKE LINE OF t_new_item,
     t_new_itemx TYPE TABLE OF bapimereqitemx,
     i_new_itemx LIKE LINE OF t_new_itemx,
     t_new_acco  TYPE TABLE OF bapimereqaccount,
     i_new_acco  LIKE LINE OF t_new_acco,
     t_new_accox TYPE TABLE OF bapimereqaccountx,
     i_new_accox LIKE LINE OF t_new_accox.
DATA: t_pritemtext TYPE TABLE OF bapimereqitemtext,
      i_pritemtext LIKE LINE OF t_pritemtext.

DATA:bt_ret2 TYPE TABLE OF  bapiret2,
     bs_ret2 LIKE LINE OF bt_ret2.

DATA:v_banfn TYPE eban-banfn.
DATA:l_txz01 TYPE string.
DATA:lt_ztmm005 TYPE TABLE OF ztmm005.
DATA:ls_ztmm005 TYPE ztmm005.
DATA:lv_xh2 TYPE ztmm005-xh.
DATA:lv_xhitem TYPE ztmm005-xhitem.
DATA:lv_cgsqd TYPE ztmm005-cgsqd.

DATA:lv_error,lv_xh.

ls_item_req_header = input-mt_mmi015_s4_req-head.
lt_item_req_item =  input-mt_mmi015_s4_req-item.

w_head-pr_type = 'Z005'.
w_headx-pr_type = 'X'.

CLEAR:lv_error,v_banfn,lv_xh2,lv_xhitem,lv_cgsqd.

lv_cgsqd = ls_item_req_header-cgsqd.
SELECT SINGLE * INTO CORRESPONDING FIELDS OF ls_ztmm005 FROM ztmm005 WHERE cgsqd = lv_cgsqd AND ztype = 'S'.
IF sy-subrc = 0.
  lv_error = 'X'.
  ls_return_res-type = 'E'.
  ls_return_res-mess = '此OA申请单已创建SAP采购申请,' && ls_ztmm005-banfn.
  ls_return_res-cgsqd = ls_item_req_item-cgsqd.
  APPEND ls_return_res TO lt_return_res.
ENDIF.

IF lv_error = ''.
  LOOP AT lt_item_req_item INTO ls_item_req_item.
    CLEAR:i_new_item,i_new_itemx,i_new_acco,i_new_accox,i_pritemtext.

    IF ls_item_req_item-knttp = 'F' AND ls_item_req_item-aufnr = ''.
      lv_error = 'X'.
      ls_return_res-type = 'E'.
      ls_return_res-mess = '行:' && ls_item_req_item-row && '/没有指定内订单或工单,科目类别不能传值F。'.
      ls_return_res-cgsqd = ls_item_req_item-cgsqd.
      APPEND ls_return_res TO lt_return_res.
      EXIT.
    ELSEIF ls_item_req_item-knttp <> 'F' AND ls_item_req_item-aufnr <> ''.
      lv_error = 'X'.
      ls_return_res-type = 'E'.
      ls_return_res-mess = '行:' && ls_item_req_item-row && '/指定了内订单或工单,科目类别应传值F。'.
      ls_return_res-cgsqd = ls_item_req_item-cgsqd.
      APPEND ls_return_res TO lt_return_res.
      EXIT.
    ENDIF.

    IF ls_item_req_item-knttp = 'K' AND ls_item_req_item-kostl = ''.
      lv_error = 'X'.
      ls_return_res-type = 'E'.
      ls_return_res-mess = '行:' && ls_item_req_item-row && '/没有指定成本中心,科目类别不能传值K。'.
      ls_return_res-cgsqd = ls_item_req_item-cgsqd.
      APPEND ls_return_res TO lt_return_res.
      EXIT.
    ELSEIF ls_item_req_item-knttp <> 'K' AND ls_item_req_item-kostl <> ''.
      lv_error = 'X'.
      ls_return_res-type = 'E'.
      ls_return_res-mess = '行:' && ls_item_req_item-row && '/指定了成本中心,科目类别应传值K。'.
      ls_return_res-cgsqd = ls_item_req_item-cgsqd.
      APPEND ls_return_res TO lt_return_res.
      EXIT.
    ENDIF.

    i_new_item-preq_item = ls_item_req_item-bnfpo.
    i_new_item-pur_group = ls_item_req_item-ekgrp.
    i_new_item-short_text = ls_item_req_item-txz01.
    i_new_item-matl_group = ls_item_req_item-matkl.
    i_new_item-item_cat = '0'.
    i_new_item-acctasscat = ls_item_req_item-knttp.
    i_new_item-quantity = ls_item_req_item-menge.
    i_new_item-unit = ls_item_req_item-meins.
    i_new_item-deliv_date = ls_item_req_item-lfdat.
    i_new_item-plant = ls_item_req_item-werks.
    i_new_item-preq_name = ls_item_req_item-afnam.
    APPEND i_new_item TO t_new_item.

    i_new_itemx-preq_item = ls_item_req_item-bnfpo.
    i_new_itemx-preq_itemx = 'X'.
    i_new_itemx-pur_group = 'X'.
    i_new_itemx-short_text =  'X'.
    i_new_itemx-matl_group =  'X'.
    i_new_itemx-item_cat = 'X'.
    i_new_itemx-acctasscat = 'X'.
    i_new_itemx-quantity = 'X'.
    i_new_itemx-unit =  'X'.
    i_new_itemx-deliv_date = 'X'.
    i_new_itemx-plant =  'X'.
    i_new_itemx-preq_name = 'X'.
    APPEND i_new_itemx TO t_new_itemx.

    i_new_acco-preq_item = ls_item_req_item-bnfpo.
    i_new_acco-orderid  = |{ ls_item_req_item-aufnr ALPHA = IN }|.
  •  IF ls_item_req_item-aufnr = ''.
      i_new_acco-costcenter = |{ ls_item_req_item-kostl ALPHA = IN }|.
    
  •  ENDIF.
      i_new_acco-gl_account  = |{ ls_item_req_item-sakto ALPHA = IN }|.
      i_new_acco-serial_no = '01'.
      APPEND i_new_acco TO t_new_acco.
    
      i_new_accox-preq_item = 'X'.
      i_new_accox-preq_item = ls_item_req_item-bnfpo.
      i_new_accox-orderid  = 'X'.
    
  •  IF ls_item_req_item-aufnr = ''.
      i_new_accox-costcenter = 'X'.
    
  •  ENDIF.
      i_new_accox-gl_account = 'X'.
      i_new_accox-preq_itemx = 'X'.
      i_new_accox-serial_no = '01'.
      APPEND i_new_accox TO t_new_accox.
    
      i_pritemtext-preq_item = ls_item_req_item-bnfpo.
      i_pritemtext-text_id = 'B01'.
      i_pritemtext-text_line = ls_item_req_item-ztext.
      APPEND i_pritemtext TO t_pritemtext.
    
    ENDLOOP.
    

    ENDIF.

    IF lv_error = ‘’.

    CALL FUNCTION 'BAPI_PR_CREATE'
      EXPORTING
        prheader   = w_head
        prheaderx  = w_headx
      IMPORTING
        number     = v_banfn
      TABLES
        return     = bt_ret2
        pritem     = t_new_item
        pritemx    = t_new_itemx
        praccount  = t_new_acco
        praccountx = t_new_accox
        "pritemtext    = t_new_text
      " prcomponents  = t_new_comp
        "prcomponentsx = t_new_compx
        pritemtext = t_pritemtext
      EXCEPTIONS
        OTHERS     = 1.
    
    LOOP AT bt_ret2 INTO bs_ret2 WHERE  type = 'E' .
      CLEAR ls_return_res.
      ls_return_res-mess = bs_ret2-message.
      ls_return_res-banfn = v_banfn.
      ls_return_res-type = 'E'.
      ls_return_res-cgsqd = ls_item_req_item-cgsqd.
      APPEND ls_return_res TO lt_return_res.
    
    ENDLOOP.
    
    IF sy-subrc = 0.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    
    ELSE.
    
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
    
      ls_return_res-banfn = v_banfn.
      ls_return_res-type = 'S'.
      ls_return_res-cgsqd = ls_item_req_item-cgsqd.
      ls_return_res-mess = '单号:' && ls_item_req_item-cgsqd && '创建sap采购申请单号:' && v_banfn &&  '成功'.
      APPEND ls_return_res TO lt_return_res.
    
    ENDIF.
    

    ENDIF.

    REFRESH:lt_ztmm005.
    SELECT MAX( xh ) INTO lv_xh2 FROM ztmm005.
    ADD 1 TO lv_xh2.
    LOOP AT lt_item_req_item INTO ls_item_req_item.
    ADD 1 TO lv_xhitem.
    CLEAR ls_ztmm005.
    ls_ztmm005-xh = lv_xh2.
    ls_ztmm005-xhitem = lv_xhitem.
    ls_ztmm005-cgsqd = ls_item_req_item-cgsqd.
    ls_ztmm005-zrow = ls_item_req_item-row.
    ls_ztmm005-banfn = v_banfn.
    ls_ztmm005-bnfpo = ls_item_req_item-bnfpo.
    ls_ztmm005-ekgrp = ls_item_req_item-ekgrp.
    ls_ztmm005-txz01 = ls_item_req_item-txz01.
    ls_ztmm005-matkl = ls_item_req_item-matkl.
    ls_ztmm005-knttp = ls_item_req_item-knttp.
    ls_ztmm005-menge = ls_item_req_item-menge.
    ls_ztmm005-meins = ls_item_req_item-meins.
    ls_ztmm005-lfdat = ls_item_req_item-lfdat.
    ls_ztmm005-werks = ls_item_req_item-werks.
    ls_ztmm005-preis = ls_item_req_item-preis.
    ls_ztmm005-afnam = ls_item_req_item-afnam.
    ls_ztmm005-kostl = ls_item_req_item-kostl.
    ls_ztmm005-aufnr = ls_item_req_item-aufnr.
    ls_ztmm005-sakto = ls_item_req_item-sakto.
    ls_ztmm005-ztext = ls_item_req_item-ztext.
    ls_ztmm005-ztype = ls_return_res-type.
    ls_ztmm005-mess = ls_return_res-mess.
    ls_ztmm005-field1 = ls_item_req_item-field1.
    ls_ztmm005-field2 = ls_item_req_item-field2.
    ls_ztmm005-field3 = ls_item_req_item-field3.
    ls_ztmm005-field4 = ls_item_req_item-field4.
    ls_ztmm005-field5 = ls_item_req_item-field5.
    APPEND ls_ztmm005 TO lt_ztmm005.
    ENDLOOP.

    IF lt_ztmm005 IS NOT INITIAL.
    MODIFY ztmm005 FROM TABLE lt_ztmm005.
    ENDIF.

    output-mt_mmi015_s4_res-return = lt_return_res.

    ENDMETHOD.

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