ABAP 创建、修改、删除内部交货单(VL31N/VL32N)

一、干货
VL31N创建的BAPI:
1.GN_DELIVERY_CREATE 通用交货单使用的bapi,推荐使用
2.BAPI_DELIVERYPROCESSING_EXEC 简单,但是字段比较少
3.BBP_INB_DELIVERY_CREATE 听说有bug,我就没有使用这个了

VL32N修改/删除BAPI:
BAPI_INB_DELIVERY_CHANGE 使用这个bapi

二、代码实例
根据采购订单创建内部交货单,推荐使用这个bapi

*"通用函数,创建交货单,但是它返回的结果没有行
DATA: ls_vbsk     LIKE  vbsk,
      es_vbsk     LIKE  vbsk,
      lt_spe_vbfs TYPE TABLE OF  /spe/vbfs,
      ls_spe_vbfs TYPE /spe/vbfs,
      lt_komdlgn  TYPE TABLE OF komdlgn,
      ls_komdlgn  TYPE komdlgn,
      lt_vbfs     TYPE TABLE OF vbfs,
      lt_vbls     TYPE TABLE OF vbls,
      lt_lips     TYPE TABLE OF lips.

CLEAR:ls_vbsk,lt_spe_vbfs,ls_spe_vbfs,lt_komdlgn,ls_komdlgn,lt_vbfs,lt_vbls.

SELECT SINGLE *
        FROM ekpo
  WHERE ekpo~ebeln = '4500001299' AND ekpo~ebelp = '000020'
  INTO @DATA(gs_inbound).

ls_komdlgn-lfart        = 'EL'.
ls_komdlgn-vgtyp        = 'V'.
ls_komdlgn-kzazu        = 'X'.
ls_komdlgn-lfdat        =  '20230417'."交货时间
ls_komdlgn-wadat        = '20230417' ."发货时间

ls_komdlgn-vgbel        = gs_inbound-ebeln.
ls_komdlgn-vgpos        = gs_inbound-ebelp.
ls_komdlgn-lifnr        = '0000100235'."GS_INBOUND-LIFNR."供应商
ls_komdlgn-lfimg        = '1'."GS_INBOUND-LFIMG."数量
ls_komdlgn-matnr        = gs_inbound-matnr.
ls_komdlgn-werks        = gs_inbound-werks.
ls_komdlgn-lgort        = gs_inbound-lgort.

ls_komdlgn-vrkme        = gs_inbound-meins.
ls_komdlgn-meins        = gs_inbound-meins.
ls_komdlgn-lifex        = gs_inbound-ebeln.
ls_komdlgn-lifexpos     = gs_inbound-ebelp.
ls_komdlgn-lichn        =  '55555'. "供应商批次
ls_komdlgn-hsdat        =  '20230416' ."生产日期

APPEND ls_komdlgn TO lt_komdlgn.

ls_vbsk-mandt = sy-mandt.
ls_vbsk-ernam = sy-uname.
ls_vbsk-erdat = sy-datum.
ls_vbsk-uzeit = sy-uzeit.
ls_vbsk-smart = 'L'.


CALL FUNCTION 'GN_DELIVERY_CREATE'
  EXPORTING
    vbsk_i      = ls_vbsk
  IMPORTING
    vbsk_e      = es_vbsk
  TABLES
    xvbfs       = lt_vbfs
    xvbls       = lt_vbls
    xkomdlgn    = lt_komdlgn
    xxlips      = lt_lips
    et_spe_vbfs = lt_spe_vbfs.

LOOP AT lt_spe_vbfs  INTO ls_spe_vbfs WHERE msgty CA  'EAX'.

ENDLOOP.
IF sy-subrc = 0. "创建失败了
  ROLLBACK WORK.
ELSE.
  COMMIT WORK.

ENDIF.

第二种创建内部交货单,这种简单快捷,但是字段少。

DATA: lt_request TYPE TABLE OF bapideliciousrequest .
DATA:ls_request  TYPE bapideliciousrequest .
DATA: lt_created TYPE TABLE OF bapideliciouscreateditems .
DATA: lt_return  TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE.


ls_request-id = '1'.
ls_request-document_numb = '4500001299'.
ls_request-document_item = '000020'.
ls_request-document_type = 'B'.
ls_request-quantity_base__uom = '2.000'.
ls_request-delivery_date = sy-datum."交货日期
"LS_REQUEST-DELIVERY_TIME = ''.
APPEND ls_request TO lt_request.

CHECK lt_request[] IS NOT INITIAL.

CALL FUNCTION 'BAPI_DELIVERYPROCESSING_EXEC'
  TABLES
    request      = lt_request[]
    createditems = lt_created
 "  EXTENSIONIN  = LT_EXTENSION
    return       = lt_return.

IF lt_return IS INITIAL."就是成功了
  READ TABLE lt_created INTO DATA(ls_created) INDEX 1.
  COMMIT WORK.
ELSE."失败了
  LOOP AT lt_return INTO DATA(ls_return).

  ENDLOOP.
ENDIF.

修改内部交货单数量,批次之类的,但是有些字段修改不到。

DATA:ls_head     TYPE bapiibdlvhdrchg,
     ls_headx    TYPE bapiibdlvhdrctrlchg,
     ls_del      TYPE bapiibdlvhdrchg-deliv_numb,
     ls_dn_item  TYPE bapiibdlvitemchg,
     lt_dn_item  TYPE TABLE OF bapiibdlvitemchg,
     ls_dn_itemx TYPE bapiibdlvitemctrlchg,
     lt_dn_itemx TYPE TABLE OF bapiibdlvitemctrlchg,
     lt_return   TYPE TABLE OF bapiret2.

CLEAR:ls_head ,ls_headx,ls_del,lt_dn_item,lt_dn_itemx,lt_return.
DATA:us_lips TYPE lips.
us_lips-vbeln =  '0180000460'.
SELECT SINGLE * FROM lips WHERE vbeln =  '0180000460' AND posnr = '000010'  INTO @DATA(ls_lips).
"抬头
ls_head-deliv_numb = '0180000460'.
ls_headx-deliv_numb = '0180000460'.

"行项目
ls_dn_item-deliv_numb = '0180000460'.
ls_dn_item-deliv_item = '000010'.
ls_dn_item-dlv_qty = '1'. "原交货单新数量= 原交货单老数量 - 该行拆分数量
ls_dn_item-sales_unit = ls_lips-vrkme."单位

SELECT SINGLE umrez umren
INTO (ls_dn_item-fact_unit_nom,ls_dn_item-fact_unit_denom)
FROM marm
WHERE matnr = ls_lips-matnr "销售单位一定要先在主数据维护
AND meinh = ls_dn_item-sales_unit.

APPEND ls_dn_item TO lt_dn_item.

ls_dn_itemx-deliv_numb = '0180000460'.
ls_dn_itemx-deliv_item =  '000010'.
ls_dn_itemx-chg_delqty = 'X'."修改交货数量
APPEND ls_dn_itemx TO lt_dn_itemx.


CALL FUNCTION 'BAPI_INB_DELIVERY_CHANGE'
  EXPORTING
    header_data    = ls_head
    header_control = ls_headx
    delivery       = ls_head-deliv_numb
  TABLES
    item_data      = lt_dn_item
    item_control   = lt_dn_itemx
    return         = lt_return.

IF sy-subrc = 0.
  COMMIT WORK AND WAIT.
ENDIF.

删除整单内部交货单,行项目也可删除,去item里面打上删除标记

ls_head-deliv_numb =   '0180000460'.
ls_headx-deliv_numb =  '0180000460'.
ls_headx-dlv_del  = 'X'. "整单删除


CALL FUNCTION 'BAPI_INB_DELIVERY_CHANGE'
  EXPORTING
    header_data    = ls_head
    header_control = ls_headx
    delivery       = ls_head-deliv_numb
  TABLES
    return         = lt_return.

做些资源总结,方便以后使用,把上面具体的单据改成你的测试数据,就可以使用运行了。

好了,分享使我快乐,我是寒武青锋。
ABAP 创建、修改、删除内部交货单(VL31N/VL32N)_第1张图片

你可能感兴趣的:(ABAP,abap)