ABAP 创建/修改采购订单()

ZYL1=1 创建   ZYL1=2修改

 DATA: ls_poheader    TYPE                   bapimepoheader,
        ls_poheaderx   TYPE                   bapimepoheaderx,
        lt_return      TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE,
        ls_return      TYPE                   bapiret2,
        lt_poitem      TYPE STANDARD TABLE OF bapimepoitem,
        ls_poitem      TYPE                   bapimepoitem,
        lt_poitemx     TYPE STANDARD TABLE OF bapimepoitemx,
        ls_poitemx     TYPE                   bapimepoitemx,
        lt_account     TYPE STANDARD TABLE OF bapimepoaccount,
        lt_accountx    TYPE STANDARD TABLE OF bapimepoaccountx,
        lt_poschedule  TYPE STANDARD TABLE OF bapimeposchedule,
        lt_poschedulex TYPE STANDARD TABLE OF bapimeposchedulx,
        lv_ebeln       TYPE bapimepoheader-po_number,
        ls_account     TYPE bapimepoaccount,
        ls_accountx    TYPE bapimepoaccountx,
        ls_poschedule  TYPE bapimeposchedule,
        ls_poschedulex TYPE bapimeposchedulx,
        lv_msg         TYPE bapi_msg.
  DATA : lv_tabix TYPE syst_tabix.

  LOOP AT in_tab ASSIGNING FIELD-SYMBOL(<fs_zmms003>).
    <fs_zmms003>-lifnr = |{ <fs_zmms003>-lifnr ALPHA = IN }|.
    <fs_zmms003>-ebeln = |{ <fs_zmms003>-ebeln ALPHA = IN }|.
    <fs_zmms003>-ebelp = |{ <fs_zmms003>-ebelp ALPHA = IN }|.
    <fs_zmms003>-vbeln = |{ <fs_zmms003>-vbeln ALPHA = IN }|.
    <fs_zmms003>-vbelp = |{ <fs_zmms003>-vbelp ALPHA = IN }|.
  ENDLOOP.
  SORT in_tab BY lifnr lifnm ebeln.

  LOOP AT in_tab ASSIGNING <fs_zmms003>.
    lv_ebeln = |{ <fs_zmms003>-ebeln ALPHA = IN }|.
    "抬头信息。
    CLEAR :ls_poheader,ls_poheaderx ,ls_return, ls_poitem  ,ls_poitemx .
    IF <fs_zmms003>-bukrs = ''.                                     "公司代码
      ls_poheader-comp_code   = '1000'.
    ELSE.
      ls_poheader-comp_code   = <fs_zmms003>-bukrs.
    ENDIF.
    ls_poheaderx-comp_code   = 'X'.
    ls_poheader-po_number    = <fs_zmms003>-ebeln.                  "采购凭证号
    ls_poheaderx-po_number   = 'X'.
    ls_poheader-doc_type     = 'NBY'.                               "采购订单类型(固定NBY)
*    ls_poheader-doc_type    = <fs_zmms003>-bsart.                  "采购订单类型
    ls_poheaderx-doc_type    = 'X'.
    ls_poheader-purch_org    = <fs_zmms003>-ekorg.                  "采购组织
    ls_poheaderx-purch_org   = 'X'.
    ls_poheader-pur_group    = <fs_zmms003>-ekgrp.                  "采购组
    ls_poheaderx-pur_group   = 'X'.
    ls_poheader-vendor       = <fs_zmms003>-lifnr.                  "供应商编号
    ls_poheaderx-vendor      = 'X'.
    ls_poheader-sales_pers   = <fs_zmms003>-lifnm.                  "供应商名称
    ls_poheaderx-sales_pers  = 'X'.
    ls_poheader-doc_date     = <fs_zmms003>-bedat.                  "采购订单日期
    ls_poheaderx-doc_date    = 'X'.
    ls_poheader-item_intvl   = space.
    ls_poheaderx-item_intvl  = 'X'.

    "行项目信息
    ls_poitem-po_item       = <fs_zmms003>-ebelp.             "采购凭证的项目编号
    ls_poitem-delete_ind    = <fs_zmms003>-loekz.             "删除标志
    ls_poitem-item_cat      = <fs_zmms003>-pstyp.             "项目类别
    ls_poitem-acctasscat    = <fs_zmms003>-knttp.             "科目分配类别
    ls_poitem-material      = <fs_zmms003>-matnr.             "物料编号
    ls_poitem-short_text    = <fs_zmms003>-txz01.             "短文本
    IF ls_poitem-short_text IS NOT INITIAL.
      ls_poitemx-short_text    = 'X'.
    ENDIF.
    IF <fs_zmms003>-werks = ''.                               "工厂
      ls_poitem-plant         = '1010'.
    ELSE.
      ls_poitem-plant         = <fs_zmms003>-werks.
    ENDIF.
    IF <fs_zmms003>-lgort = ''.                               "库存地点
      ls_poitem-stge_loc      = 'Y010'.
    ELSE.
      ls_poitem-stge_loc      = <fs_zmms003>-lgort.
    ENDIF.
    ls_poitem-quantity                 = <fs_zmms003>-menge.             "采购订单数量
    ls_poitem-po_unit                  = <fs_zmms003>-meins.             "计量单位
    ls_poitem-gr_to_date               = <fs_zmms003>-eindt.             "交货日期
    ls_poitem-ret_item                 = <fs_zmms003>-retpo.             "退货标志
    ls_poitem-preq_name                = <fs_zmms003>-ernam.             "创建人
    APPEND ls_poitem TO lt_poitem .

    ls_poitemx-po_item                  = <fs_zmms003>-ebelp.
    ls_poitemx-delete_ind               = 'X'.
    ls_poitemx-item_cat                 = 'X'.
    ls_poitemx-acctasscat               = 'X'.
    ls_poitemx-material                 = 'X'.
    ls_poitemx-item_cat                 = 'X'.
    ls_poitemx-plant                    = 'X'.
    ls_poitemx-stge_loc                 = 'X'.
    ls_poitemx-crm_sales_order_no       = 'X'.
    ls_poitemx-crm_sales_order_item_no  = 'X'.
    ls_poitemx-quantity                 = 'X'.
    ls_poitemx-po_unit                  = 'X'.
    ls_poitemx-gr_to_date               = 'X'.
    ls_poitemx-ret_item                 = 'X'.
    ls_poitemx-preq_name                = 'X'.
    APPEND ls_poitemx TO lt_poitemx .

    ls_account-po_item      = <fs_zmms003>-ebelp.   "采购订单行项目号
    ls_account-sd_doc       = <fs_zmms003>-vbeln.   "销售订单号
    ls_account-itm_number   = <fs_zmms003>-vbelp.   "销售订单行项目
    APPEND ls_account TO lt_account.

    ls_accountx-po_item     = <fs_zmms003>-ebelp.
*    ls_accountx-po_itemx    = 'X'.
    ls_accountx-sd_doc      = 'X'.
    ls_accountx-itm_number  = 'X'.
    APPEND ls_accountx TO lt_accountx.

    ls_poschedule-po_item        = <fs_zmms003>-ebelp.   "采购订单行项目号
    ls_poschedule-sched_line     = '1'.                  "计划行
    ls_poschedule-delivery_date  = <fs_zmms003>-eindt.   "交货日期
    APPEND ls_poschedule TO lt_poschedule.

    ls_poschedulex-po_item        = <fs_zmms003>-ebelp.   "采购订单行项目号
    ls_poschedulex-sched_line     = '1'.
    ls_poschedulex-delivery_date  = 'X'.
    APPEND ls_poschedulex TO lt_poschedulex.

    AT END OF ebeln.
      IF <fs_zmms003>-zyl1 = '1'.
        CALL FUNCTION 'BAPI_PO_CREATE1'
          EXPORTING
            poheader    = ls_poheader
            poheaderx   = ls_poheaderx
          TABLES
            return      = lt_return
            poitem      = lt_poitem
            poitemx     = lt_poitemx
            poaccount   = lt_account
            poaccountx  = lt_accountx
            poschedule  = lt_poschedule
            poschedulex = lt_poschedulex.
        LOOP AT lt_return WHERE type CA 'AE'.
          lv_msg = |{ lv_msg } { lt_return-message }|.
        ENDLOOP.
        IF sy-subrc <> 0.
          READ TABLE lt_return INTO ls_return WITH KEY type = 'S'.
          <fs_zmms003>-msgty  = 'S'.
          <fs_zmms003>-msgtxt = ls_return-message.
          e_code              = <fs_zmms003>-msgty.
          e_msg               = <fs_zmms003>-msgtxt.
          COMMIT WORK AND WAIT.
        ELSE.
          <fs_zmms003>-msgty  = 'E'.
          <fs_zmms003>-msgtxt = lv_msg.
          e_code              = <fs_zmms003>-msgty.
          e_msg               = <fs_zmms003>-msgtxt.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ENDIF.
      ELSEIF <fs_zmms003>-zyl1 = '2'.
        CALL FUNCTION 'BAPI_PO_CHANGE'
          EXPORTING
            purchaseorder = lv_ebeln
            poheader      = ls_poheader
            poheaderx     = ls_poheaderx
          TABLES
            return        = lt_return
            poitem        = lt_poitem
            poitemx       = lt_poitemx
            poaccount     = lt_account
            poaccountx    = lt_accountx
            poschedule    = lt_poschedule
            poschedulex   = lt_poschedulex.
        LOOP AT lt_return WHERE type CA 'AE'.
          lv_msg = |{ lv_msg } { lt_return-message }|.
        ENDLOOP.
        IF sy-subrc <> 0.
          READ TABLE lt_return INTO ls_return WITH KEY type = 'S'.
          <fs_zmms003>-msgty  = 'S'.
          <fs_zmms003>-msgtxt = ls_return-message.
          e_code              = <fs_zmms003>-msgty.
          e_msg               = <fs_zmms003>-msgtxt.
          COMMIT WORK AND WAIT.
        ELSE.
          <fs_zmms003>-msgty  = 'E'.
          <fs_zmms003>-msgtxt = lv_msg.
          e_code              = <fs_zmms003>-msgty.
          e_msg               = <fs_zmms003>-msgtxt.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ENDIF.
      ELSE.
        <fs_zmms003>-msgty  = 'E'.
        <fs_zmms003>-msgtxt = '请输入正确的业务类型'.
        e_code              = <fs_zmms003>-msgty.
        e_msg               = <fs_zmms003>-msgtxt.
      ENDIF.
      CLEAR : lv_msg,lt_return,lt_poitem,lt_poitemx.
    ENDAT.
  ENDLOOP.

  LOOP AT in_tab ASSIGNING <fs_zmms003>.
    READ TABLE in_tab INTO DATA(ls_tab) WITH KEY ebeln = <fs_zmms003>-ebeln.
    IF sy-subrc = 0.
      lv_tabix = sy-tabix.
      LOOP AT in_tab INTO ls_tab FROM lv_tabix WHERE ebeln = <fs_zmms003>-ebeln.
        IF ls_tab-msgty IS NOT INITIAL.
          <fs_zmms003>-msgty = ls_tab-msgty.
          <fs_zmms003>-msgtxt = ls_tab-msgtxt.
          EXIT.
        ENDIF.
      ENDLOOP.
    ENDIF.
  ENDLOOP.
————————

ZYL1=1 创建   ZYL1=2修改

 DATA: ls_poheader    TYPE                   bapimepoheader,
        ls_poheaderx   TYPE                   bapimepoheaderx,
        lt_return      TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE,
        ls_return      TYPE                   bapiret2,
        lt_poitem      TYPE STANDARD TABLE OF bapimepoitem,
        ls_poitem      TYPE                   bapimepoitem,
        lt_poitemx     TYPE STANDARD TABLE OF bapimepoitemx,
        ls_poitemx     TYPE                   bapimepoitemx,
        lt_account     TYPE STANDARD TABLE OF bapimepoaccount,
        lt_accountx    TYPE STANDARD TABLE OF bapimepoaccountx,
        lt_poschedule  TYPE STANDARD TABLE OF bapimeposchedule,
        lt_poschedulex TYPE STANDARD TABLE OF bapimeposchedulx,
        lv_ebeln       TYPE bapimepoheader-po_number,
        ls_account     TYPE bapimepoaccount,
        ls_accountx    TYPE bapimepoaccountx,
        ls_poschedule  TYPE bapimeposchedule,
        ls_poschedulex TYPE bapimeposchedulx,
        lv_msg         TYPE bapi_msg.
  DATA : lv_tabix TYPE syst_tabix.

  LOOP AT in_tab ASSIGNING FIELD-SYMBOL(<fs_zmms003>).
    <fs_zmms003>-lifnr = |{ <fs_zmms003>-lifnr ALPHA = IN }|.
    <fs_zmms003>-ebeln = |{ <fs_zmms003>-ebeln ALPHA = IN }|.
    <fs_zmms003>-ebelp = |{ <fs_zmms003>-ebelp ALPHA = IN }|.
    <fs_zmms003>-vbeln = |{ <fs_zmms003>-vbeln ALPHA = IN }|.
    <fs_zmms003>-vbelp = |{ <fs_zmms003>-vbelp ALPHA = IN }|.
  ENDLOOP.
  SORT in_tab BY lifnr lifnm ebeln.

  LOOP AT in_tab ASSIGNING <fs_zmms003>.
    lv_ebeln = |{ <fs_zmms003>-ebeln ALPHA = IN }|.
    "抬头信息。
    CLEAR :ls_poheader,ls_poheaderx ,ls_return, ls_poitem  ,ls_poitemx .
    IF <fs_zmms003>-bukrs = ''.                                     "公司代码
      ls_poheader-comp_code   = '1000'.
    ELSE.
      ls_poheader-comp_code   = <fs_zmms003>-bukrs.
    ENDIF.
    ls_poheaderx-comp_code   = 'X'.
    ls_poheader-po_number    = <fs_zmms003>-ebeln.                  "采购凭证号
    ls_poheaderx-po_number   = 'X'.
    ls_poheader-doc_type     = 'NBY'.                               "采购订单类型(固定NBY)
*    ls_poheader-doc_type    = <fs_zmms003>-bsart.                  "采购订单类型
    ls_poheaderx-doc_type    = 'X'.
    ls_poheader-purch_org    = <fs_zmms003>-ekorg.                  "采购组织
    ls_poheaderx-purch_org   = 'X'.
    ls_poheader-pur_group    = <fs_zmms003>-ekgrp.                  "采购组
    ls_poheaderx-pur_group   = 'X'.
    ls_poheader-vendor       = <fs_zmms003>-lifnr.                  "供应商编号
    ls_poheaderx-vendor      = 'X'.
    ls_poheader-sales_pers   = <fs_zmms003>-lifnm.                  "供应商名称
    ls_poheaderx-sales_pers  = 'X'.
    ls_poheader-doc_date     = <fs_zmms003>-bedat.                  "采购订单日期
    ls_poheaderx-doc_date    = 'X'.
    ls_poheader-item_intvl   = space.
    ls_poheaderx-item_intvl  = 'X'.

    "行项目信息
    ls_poitem-po_item       = <fs_zmms003>-ebelp.             "采购凭证的项目编号
    ls_poitem-delete_ind    = <fs_zmms003>-loekz.             "删除标志
    ls_poitem-item_cat      = <fs_zmms003>-pstyp.             "项目类别
    ls_poitem-acctasscat    = <fs_zmms003>-knttp.             "科目分配类别
    ls_poitem-material      = <fs_zmms003>-matnr.             "物料编号
    ls_poitem-short_text    = <fs_zmms003>-txz01.             "短文本
    IF ls_poitem-short_text IS NOT INITIAL.
      ls_poitemx-short_text    = 'X'.
    ENDIF.
    IF <fs_zmms003>-werks = ''.                               "工厂
      ls_poitem-plant         = '1010'.
    ELSE.
      ls_poitem-plant         = <fs_zmms003>-werks.
    ENDIF.
    IF <fs_zmms003>-lgort = ''.                               "库存地点
      ls_poitem-stge_loc      = 'Y010'.
    ELSE.
      ls_poitem-stge_loc      = <fs_zmms003>-lgort.
    ENDIF.
    ls_poitem-quantity                 = <fs_zmms003>-menge.             "采购订单数量
    ls_poitem-po_unit                  = <fs_zmms003>-meins.             "计量单位
    ls_poitem-gr_to_date               = <fs_zmms003>-eindt.             "交货日期
    ls_poitem-ret_item                 = <fs_zmms003>-retpo.             "退货标志
    ls_poitem-preq_name                = <fs_zmms003>-ernam.             "创建人
    APPEND ls_poitem TO lt_poitem .

    ls_poitemx-po_item                  = <fs_zmms003>-ebelp.
    ls_poitemx-delete_ind               = 'X'.
    ls_poitemx-item_cat                 = 'X'.
    ls_poitemx-acctasscat               = 'X'.
    ls_poitemx-material                 = 'X'.
    ls_poitemx-item_cat                 = 'X'.
    ls_poitemx-plant                    = 'X'.
    ls_poitemx-stge_loc                 = 'X'.
    ls_poitemx-crm_sales_order_no       = 'X'.
    ls_poitemx-crm_sales_order_item_no  = 'X'.
    ls_poitemx-quantity                 = 'X'.
    ls_poitemx-po_unit                  = 'X'.
    ls_poitemx-gr_to_date               = 'X'.
    ls_poitemx-ret_item                 = 'X'.
    ls_poitemx-preq_name                = 'X'.
    APPEND ls_poitemx TO lt_poitemx .

    ls_account-po_item      = <fs_zmms003>-ebelp.   "采购订单行项目号
    ls_account-sd_doc       = <fs_zmms003>-vbeln.   "销售订单号
    ls_account-itm_number   = <fs_zmms003>-vbelp.   "销售订单行项目
    APPEND ls_account TO lt_account.

    ls_accountx-po_item     = <fs_zmms003>-ebelp.
*    ls_accountx-po_itemx    = 'X'.
    ls_accountx-sd_doc      = 'X'.
    ls_accountx-itm_number  = 'X'.
    APPEND ls_accountx TO lt_accountx.

    ls_poschedule-po_item        = <fs_zmms003>-ebelp.   "采购订单行项目号
    ls_poschedule-sched_line     = '1'.                  "计划行
    ls_poschedule-delivery_date  = <fs_zmms003>-eindt.   "交货日期
    APPEND ls_poschedule TO lt_poschedule.

    ls_poschedulex-po_item        = <fs_zmms003>-ebelp.   "采购订单行项目号
    ls_poschedulex-sched_line     = '1'.
    ls_poschedulex-delivery_date  = 'X'.
    APPEND ls_poschedulex TO lt_poschedulex.

    AT END OF ebeln.
      IF <fs_zmms003>-zyl1 = '1'.
        CALL FUNCTION 'BAPI_PO_CREATE1'
          EXPORTING
            poheader    = ls_poheader
            poheaderx   = ls_poheaderx
          TABLES
            return      = lt_return
            poitem      = lt_poitem
            poitemx     = lt_poitemx
            poaccount   = lt_account
            poaccountx  = lt_accountx
            poschedule  = lt_poschedule
            poschedulex = lt_poschedulex.
        LOOP AT lt_return WHERE type CA 'AE'.
          lv_msg = |{ lv_msg } { lt_return-message }|.
        ENDLOOP.
        IF sy-subrc <> 0.
          READ TABLE lt_return INTO ls_return WITH KEY type = 'S'.
          <fs_zmms003>-msgty  = 'S'.
          <fs_zmms003>-msgtxt = ls_return-message.
          e_code              = <fs_zmms003>-msgty.
          e_msg               = <fs_zmms003>-msgtxt.
          COMMIT WORK AND WAIT.
        ELSE.
          <fs_zmms003>-msgty  = 'E'.
          <fs_zmms003>-msgtxt = lv_msg.
          e_code              = <fs_zmms003>-msgty.
          e_msg               = <fs_zmms003>-msgtxt.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ENDIF.
      ELSEIF <fs_zmms003>-zyl1 = '2'.
        CALL FUNCTION 'BAPI_PO_CHANGE'
          EXPORTING
            purchaseorder = lv_ebeln
            poheader      = ls_poheader
            poheaderx     = ls_poheaderx
          TABLES
            return        = lt_return
            poitem        = lt_poitem
            poitemx       = lt_poitemx
            poaccount     = lt_account
            poaccountx    = lt_accountx
            poschedule    = lt_poschedule
            poschedulex   = lt_poschedulex.
        LOOP AT lt_return WHERE type CA 'AE'.
          lv_msg = |{ lv_msg } { lt_return-message }|.
        ENDLOOP.
        IF sy-subrc <> 0.
          READ TABLE lt_return INTO ls_return WITH KEY type = 'S'.
          <fs_zmms003>-msgty  = 'S'.
          <fs_zmms003>-msgtxt = ls_return-message.
          e_code              = <fs_zmms003>-msgty.
          e_msg               = <fs_zmms003>-msgtxt.
          COMMIT WORK AND WAIT.
        ELSE.
          <fs_zmms003>-msgty  = 'E'.
          <fs_zmms003>-msgtxt = lv_msg.
          e_code              = <fs_zmms003>-msgty.
          e_msg               = <fs_zmms003>-msgtxt.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ENDIF.
      ELSE.
        <fs_zmms003>-msgty  = 'E'.
        <fs_zmms003>-msgtxt = '请输入正确的业务类型'.
        e_code              = <fs_zmms003>-msgty.
        e_msg               = <fs_zmms003>-msgtxt.
      ENDIF.
      CLEAR : lv_msg,lt_return,lt_poitem,lt_poitemx.
    ENDAT.
  ENDLOOP.

  LOOP AT in_tab ASSIGNING <fs_zmms003>.
    READ TABLE in_tab INTO DATA(ls_tab) WITH KEY ebeln = <fs_zmms003>-ebeln.
    IF sy-subrc = 0.
      lv_tabix = sy-tabix.
      LOOP AT in_tab INTO ls_tab FROM lv_tabix WHERE ebeln = <fs_zmms003>-ebeln.
        IF ls_tab-msgty IS NOT INITIAL.
          <fs_zmms003>-msgty = ls_tab-msgty.
          <fs_zmms003>-msgtxt = ls_tab-msgtxt.
          EXIT.
        ENDIF.
      ENDLOOP.
    ENDIF.
  ENDLOOP.