ABAP 服务类型采购申请创建/修改 BAPI_PR_CREATE/BAPI_PR_CHANGE

BAPI_PR_CREATE

*&---------------------------------------------------------------------*
*& Form frm_create_pr1
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_create_pr1 USING    ps_head     TYPE  zmms035
                    CHANGING ps_retinfo  TYPE  zmms037.

*--变量声明
  DATA: lv_subrc      TYPE i,
        lv_str        TYPE string,
        lv_banfn      TYPE eban-banfn,
        lv_posid      TYPE prps-posid,
        lt_line       TYPE TABLE OF /scmb/bif_delvry_map_shipto,
        ls_prheader   TYPE bapimereqheader,
        ls_prheaderx  TYPE bapimereqheaderx,
        lt_return     TYPE TABLE OF bapiret2,
        ls_return     TYPE bapiret2,
        ls_pritem     TYPE bapimereqitemimp,
        lt_pritem_new TYPE TABLE OF bapimereqitemimp,
        ls_pritemx    TYPE bapimereqitemx,
        lt_pritemx    TYPE TABLE OF bapimereqitemx,
        ls_praccount  TYPE bapimereqaccount,
        lt_praccount  TYPE TABLE OF bapimereqaccount,
        ls_praccountx TYPE bapimereqaccountx,
        lt_praccountx TYPE TABLE OF bapimereqaccountx,
        ls_prheadtext TYPE bapimereqheadtext,
        lt_prheadtext TYPE TABLE OF bapimereqheadtext,
        ls_pritemtext TYPE bapimereqitemtext,
        lt_pritemtext TYPE TABLE OF bapimereqitemtext,
        lt_srv_lines  TYPE TABLE OF bapi_srv_service_line,
        ls_srv_lines  TYPE bapi_srv_service_line,
        lt_srv_linesx TYPE TABLE OF bapi_srv_service_linex,
        ls_srv_linesx TYPE bapi_srv_service_linex,
        lt_srv_acc    TYPE TABLE OF bapi_srv_acc_data,
        ls_srv_acc    TYPE bapi_srv_acc_data,
        lt_srv_accx   TYPE TABLE OF bapi_srv_acc_datax,
        ls_srv_accx   TYPE bapi_srv_acc_datax.

  " 长文本
  DATA: ls_thead TYPE thead,
        lt_tline TYPE TABLE OF tline.

*----抬头长文本数据   有坑,这个BAPI写入长文本时,无论如何改参数都只能创建一个文本标识的长文本
*  lv_str = ps_head-ztext01.  " 长文本-抬头备注
*  TRY.
*      cl_bcs_convert=>string_to_tab(
*        EXPORTING
*          iv_string = lv_str
*        IMPORTING
*          et_ctab = lt_line ).
*    CATCH cx_bcs.
*  ENDTRY.
*  LOOP AT lt_line INTO DATA(ls_line).
*    ls_prheadtext-text_id   = 'B01'.   " 文本标识-抬头
*    ls_prheadtext-text_form = '*'.     " 文本格式
*    ls_prheadtext-text_line = ls_line-note. " 文本行
*
*    APPEND ls_prheadtext TO lt_prheadtext.
*  ENDLOOP.

*----行项目数据
  LOOP AT ps_head-item INTO DATA(ls_item).

    ls_pritem-preq_item  = |{ ls_item-bnfpo ALPHA = IN }|. " 采购申请项目编号
    ls_pritem-acctasscat = ls_item-knttp.  " 科目分配类别
    ls_pritem-item_cat   = ls_item-pstyp.  " 项目类别
    ls_pritem-material   = ls_item-matnr.  " 物料编号
    ls_pritem-short_text = ls_item-txz01.  " 短文本
    ls_pritem-quantity   = ls_item-menge.  " 申请数量
    ls_pritem-unit       = ls_item-meins.  " 计量单位
    ls_pritem-deliv_date = ls_item-lfdat.  " 项目交货日期
    ls_pritem-matl_group = ls_item-matkl.  " 物料组
    ls_pritem-plant      = ls_item-werks.  " 工厂
    ls_pritem-store_loc  = ls_item-lgort.  " 库存地点
    ls_pritem-purch_org  = ls_item-ekorg.  " 采购组织
    ls_pritem-pur_group  = ls_item-ekgrp.  " 采购组
    ls_pritem-preq_name  = ls_item-afnam.  " 需求人
    ls_pritem-trackingno = ls_item-bednr.  " 申请部门
    APPEND ls_pritem TO lt_pritem_new.

    CALL FUNCTION 'CONVERSION_EXIT_ABPSP_OUTPUT'
      EXPORTING
        input  = ls_item-ps_psp_pnr
      IMPORTING
        output = lv_posid.

    ls_pritemx-preq_item  = |{ ls_item-bnfpo ALPHA = IN }|. " 采购申请项目编号
    ls_pritemx-acctasscat = gc_x. " 科目分配类别
    ls_pritemx-item_cat   = gc_x. " 项目类别
    ls_pritemx-material   = gc_x. " 物料编号
    IF ls_pritem-short_text IS NOT INITIAL.
      ls_pritemx-short_text = gc_x. " 短文本
    ENDIF.
    ls_pritemx-quantity   = gc_x. " 申请数量
    ls_pritemx-unit       = gc_x. " 计量单位
    ls_pritemx-deliv_date = gc_x. " 项目交货日期
    ls_pritemx-matl_group = gc_x. " 物料组
    ls_pritemx-plant      = gc_x. " 工厂
    ls_pritemx-store_loc  = gc_x. " 库存地点
    ls_pritemx-purch_org  = gc_x. " 采购组织
    ls_pritemx-pur_group  = gc_x. " 采购组
    ls_pritemx-preq_name  = gc_x. " 需求人
    ls_pritemx-trackingno = gc_x. " 申请部门
    ls_pritemx-delete_ind = gc_x. " 删除标识
    APPEND ls_pritemx TO lt_pritemx.

*----行项目账户数据(每个行项目有且仅有一条)

    ls_praccount-preq_item   = |{ ls_item-bnfpo ALPHA = IN }|.  " 采购申请项目编号
    ls_praccount-serial_no   = '01'. " 帐户分配的顺序编号
    ls_praccount-asset_no    = ls_item-anln1.  " 资产
    ls_praccount-costcenter  = ls_item-kostl.  " 成本中心
    ls_praccount-orderid     = ls_item-aufnr.  " 订单号
    ls_praccount-wbs_element = ls_item-ps_psp_pnr.  " WBS 元素
    ls_praccount-gl_account  = ls_item-sakto.  " 总账科目
    APPEND ls_praccount TO lt_praccount.

    ls_praccountx-preq_item   = |{ ls_item-bnfpo ALPHA = IN }|.  " 采购申请项目编号
    ls_praccountx-serial_no   = '01'. " 帐户分配的顺序编号
    ls_praccountx-asset_no    = gc_x. " 资产
    ls_praccountx-costcenter  = gc_x. " 成本中心
    ls_praccountx-orderid     = gc_x. " 订单号
    ls_praccountx-wbs_element = gc_x. " WBS 元素
    ls_praccountx-gl_account  = gc_x. " 总账科目
    APPEND ls_praccountx TO lt_praccountx.

*----行项目长文本
    lv_str = ls_item-ztext01.  " 长文本-项目文本
    TRY.
        cl_bcs_convert=>string_to_tab(
          EXPORTING
            iv_string = lv_str
          IMPORTING
            et_ctab = lt_line ).
      CATCH cx_bcs.
    ENDTRY.
    LOOP AT lt_line INTO DATA(ls_line).
      ls_pritemtext-preq_item = |{ ls_item-bnfpo ALPHA = IN }|.  " 采购申请项目编号
      ls_pritemtext-text_id   = 'B01'.   " 文本标识-项目文本
      ls_pritemtext-text_form = '*'.     " 文本格式
      ls_pritemtext-text_line = ls_line-note. " 文本行

      APPEND ls_pritemtext TO lt_pritemtext.
    ENDLOOP.

*----服务创建
    IF ps_head-bsart = 'Z007'.

      LOOP AT ls_item-service INTO DATA(ls_service).

        " 项目相关
        ls_srv_lines = VALUE #( doc_item    = ls_item-bnfpo      " 采购申请项目号
                                srv_line    = ls_service-extrow  " 外部编号
                                service     = ls_service-srvpos  " 服务编号
                                short_text  = ls_service-ktext1  " 服务文本
                                quantity    = ls_service-menge   " 数量
                                uom         = ls_service-meins   " 单位
                                gross_price = ls_service-brtwr   " 总价格
                                 ).
        IF ls_srv_lines-uom IS INITIAL.
          ls_srv_lines-uom = 'LE'.  " 默认外码AU,内码LE
        ENDIF.
        APPEND ls_srv_lines TO lt_srv_lines.

        ls_srv_linesx = VALUE #( doc_item    = ls_item-bnfpo      " 采购申请项目号
                                 srv_line    = ls_service-extrow  " 外部编号
                                 service     = gc_x               " 服务编号
                                 short_text  = gc_x               " 服务文本
                                 quantity    = gc_x               " 数量
                                 uom         = gc_x               " 单位
                                 gross_price = gc_x               " 总价格
                                  ).
        APPEND ls_srv_linesx TO lt_srv_linesx.

        " 账户相关
        ls_srv_acc  = VALUE #( doc_item    = ls_item-bnfpo      " 采购申请项目号
                               srv_line    = ls_service-extrow  " 外部编号
                               serial_no   = '01'
                               serial_no_item = '01'
                               ).
        APPEND ls_srv_acc TO lt_srv_acc.

        ls_srv_accx  = VALUE #( doc_item    = ls_item-bnfpo      " 采购申请项目号
                                srv_line    = ls_service-extrow  " 外部编号
                                serial_no   = '01'
                                serial_no_item = gc_x
                                ).
        APPEND ls_srv_accx TO lt_srv_accx.

      ENDLOOP.

    ENDIF.

  ENDLOOP.

*----抬头数据
  ls_prheader-pr_type  = ps_head-bsart.  " 采购申请类型
  ls_prheaderx-pr_type = gc_x.

*----创建采购申请
  CALL FUNCTION 'BAPI_PR_CREATE'
    EXPORTING
*     number          = lv_banfn
      prheader        = ls_prheader
      prheaderx       = ls_prheaderx
    IMPORTING
      number          = ps_retinfo-banfn
    TABLES
      pritem          = lt_pritem_new
      pritemx         = lt_pritemx
      praccount       = lt_praccount
      praccountx      = lt_praccountx
      pritemtext      = lt_pritemtext
      prheadertext    = lt_prheadtext
      servicelines    = lt_srv_lines
      servicelinesx   = lt_srv_linesx
      serviceaccount  = lt_srv_acc
      serviceaccountx = lt_srv_accx
      return          = lt_return.

*----返回消息处理
  lv_subrc = sy-subrc.
  LOOP AT lt_return INTO ls_return WHERE type CA 'AEX'.
    ps_retinfo-code     = gc_e.
    ps_retinfo-message  = ps_retinfo-message && ls_return-message.
  ENDLOOP.
  IF sy-subrc = 0 OR lv_subrc NE 0.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = abap_true.
    WAIT UP TO '0.5' SECONDS.

    " 写入长文本-抬头备注
    lv_banfn = |{ ps_retinfo-banfn ALPHA = IN }|.
    lv_str = ps_head-ztext01.
    TRY.
        cl_bcs_convert=>string_to_tab(
          EXPORTING
            iv_string = lv_str
          IMPORTING
            et_ctab = lt_line ).
      CATCH cx_bcs.
    ENDTRY.
    lt_tline = VALUE #( FOR lw_line IN lt_line
                        ( tdformat = '*' tdline = lw_line-note ) ).

    ls_thead = VALUE #( tdobject = 'EBANH'
                        tdname   = lv_banfn
                        tdid     = 'B01'
                        tdspras  = sy-langu ).
    CALL FUNCTION 'SAVE_TEXT'
      EXPORTING
        client          = sy-mandt
        header          = ls_thead
        savemode_direct = 'X'
      TABLES
        lines           = lt_tline
      EXCEPTIONS
        id              = 1
        language        = 2
        name            = 3
        object          = 4
        OTHERS          = 5.
    IF sy-subrc <> 0.
    ENDIF.
  ENDIF.

BAPI_PR_CREATE

*&---------------------------------------------------------------------*
*& Form frm_change_pr
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> LS_HEAD
*&      <-- LS_RETINFO
*&---------------------------------------------------------------------*
FORM frm_change_pr  USING    ps_head      TYPE  zmms035
                    CHANGING ps_retinfo   TYPE  zmms037.

*--变量声明
  DATA: lv_subrc      TYPE i,
        lv_str        TYPE string,
        lv_banfn      TYPE eban-banfn,
        lv_posid      TYPE prps-posid,
        lt_line       TYPE TABLE OF /scmb/bif_delvry_map_shipto,
        ls_prheader   TYPE bapimereqheader,
        ls_prheaderx  TYPE bapimereqheaderx,
        lt_return     TYPE TABLE OF bapiret2,
        ls_return     TYPE bapiret2,
        ls_pritem     TYPE bapimereqitemimp,
        lt_pritem_old TYPE TABLE OF bapimereqitem,
        ls_pritem_new TYPE bapimereqitemimp,
        lt_pritem_new TYPE TABLE OF bapimereqitemimp,
        ls_pritemx    TYPE bapimereqitemx,
        lt_pritemx    TYPE TABLE OF bapimereqitemx,
        ls_praccount  TYPE bapimereqaccount,
        lt_praccount  TYPE TABLE OF bapimereqaccount,
        ls_praccountx TYPE bapimereqaccountx,
        lt_praccountx TYPE TABLE OF bapimereqaccountx,
        ls_prheadtext TYPE bapimereqheadtext,
        lt_prheadtext TYPE TABLE OF bapimereqheadtext,
        ls_pritemtext TYPE bapimereqitemtext,
        lt_pritemtext TYPE TABLE OF bapimereqitemtext,
        lt_srv_lines  TYPE TABLE OF bapi_srv_service_line,
        ls_srv_lines  TYPE bapi_srv_service_line,
        lt_srv_linesx TYPE TABLE OF bapi_srv_service_linex,
        ls_srv_linesx TYPE bapi_srv_service_linex,
        lt_srv_acc    TYPE TABLE OF bapi_srv_acc_data,
        ls_srv_acc    TYPE bapi_srv_acc_data,
        lt_srv_accx   TYPE TABLE OF bapi_srv_acc_datax,
        ls_srv_accx   TYPE bapi_srv_acc_datax.

  " 长文本
  DATA: ls_thead TYPE thead,
        lt_tline TYPE TABLE OF tline.

  FIELD-SYMBOLS:  TYPE bapimereqitemimp,
                   TYPE bapimereqaccount,
                     TYPE bapi_srv_acc_data,
                   TYPE bapi_srv_service_line.

*--撤销审批才可修改
  PERFORM frm_reject_pr USING ps_head ps_retinfo.
  IF ps_retinfo-code = gc_e.
    EXIT.
  ENDIF.

*--获取旧数据
  lv_banfn = |{ ps_retinfo-banfn ALPHA = IN }|.

  CALL FUNCTION 'BAPI_PR_GETDETAIL'
    EXPORTING
      number             = lv_banfn
      account_assignment = gc_x
      item_text          = gc_x
      services           = gc_x
      serial_numbers     = gc_x
      version            = gc_x
      sc_components      = gc_x
    IMPORTING
      prheader           = ls_prheader
    TABLES
      pritem             = lt_pritem_old
      praccount          = lt_praccount
      servicelines       = lt_srv_lines
      serviceaccount     = lt_srv_acc.
*      pritemtext         = lt_pritemtext.

*--修改数据
  lt_pritem_new = VALUE #( FOR lw_pritem_new IN lt_pritem_old
                         ( CORRESPONDING #( lw_pritem_new ) ) ).

*----行项目数据
  LOOP AT ps_head-item INTO DATA(ls_item).

    READ TABLE lt_pritem_new ASSIGNING  WITH KEY preq_item = ls_item-bnfpo.
    IF sy-subrc = 0.  " 修改

      IF ls_item-loekz = 'X'. " 删除标记
        -delete_ind = 'X'.

        ls_pritemx-preq_item  = |{ ls_item-bnfpo ALPHA = IN }|. " 采购申请项目编号
        ls_pritemx-delete_ind = gc_x. " 删除标识
        APPEND ls_pritemx TO lt_pritemx.
        CONTINUE.
      ENDIF.

      -preq_item  = |{ ls_item-bnfpo ALPHA = IN }|. " 采购申请项目编号
      -acctasscat = ls_item-knttp.  " 科目分配类别
      -item_cat   = ls_item-pstyp.  " 项目类别
      -material   = ls_item-matnr.  " 物料编号
      -short_text = ls_item-txz01.  " 短文本
      -quantity   = ls_item-menge.  " 申请数量
      -unit       = ls_item-meins.  " 计量单位
      -deliv_date = ls_item-lfdat.  " 项目交货日期
      -matl_group = ls_item-matkl.  " 物料组
      -plant      = ls_item-werks.  " 工厂
      -store_loc  = ls_item-lgort.  " 库存地点
      -purch_org  = ls_item-ekorg.  " 采购组织
      -pur_group  = ls_item-ekgrp.  " 采购组
      -preq_name  = ls_item-afnam.  " 需求人
      -trackingno = ls_item-bednr.  " 申请部门
      -delete_ind = ls_item-loekz.  " 删除标识
    ELSE. " 新增
      ls_pritem-preq_item  = |{ ls_item-bnfpo ALPHA = IN }|. " 采购申请项目编号
      ls_pritem-acctasscat = ls_item-knttp.  " 科目分配类别
      ls_pritem-item_cat   = ls_item-pstyp.  " 项目类别
      ls_pritem-material   = ls_item-matnr.  " 物料编号
      ls_pritem-short_text = ls_item-txz01.  " 短文本
      ls_pritem-quantity   = ls_item-menge.  " 申请数量
      ls_pritem-unit       = ls_item-meins.  " 计量单位
      ls_pritem-deliv_date = ls_item-lfdat.  " 项目交货日期
      ls_pritem-matl_group = ls_item-matkl.  " 物料组
      ls_pritem-plant      = ls_item-werks.  " 工厂
      ls_pritem-store_loc  = ls_item-lgort.  " 库存地点
      ls_pritem-purch_org  = ls_item-ekorg.  " 采购组织
      ls_pritem-pur_group  = ls_item-ekgrp.  " 采购组
      ls_pritem-preq_name  = ls_item-afnam.  " 需求人
      ls_pritem-trackingno = ls_item-bednr.  " 申请部门
      APPEND ls_pritem TO lt_pritem_new.
    ENDIF.

    CALL FUNCTION 'CONVERSION_EXIT_ABPSP_OUTPUT'
      EXPORTING
        input  = ls_item-ps_psp_pnr
      IMPORTING
        output = lv_posid.

    ls_pritemx-preq_item  = |{ ls_item-bnfpo ALPHA = IN }|. " 采购申请项目编号
    ls_pritemx-acctasscat = gc_x. " 科目分配类别
    ls_pritemx-item_cat   = gc_x. " 项目类别
    ls_pritemx-material   = gc_x. " 物料编号
    IF ls_pritem-short_text IS NOT INITIAL.
      ls_pritemx-short_text = gc_x. " 短文本
    ENDIF.
    ls_pritemx-quantity   = gc_x. " 申请数量
    ls_pritemx-unit       = gc_x. " 计量单位
    ls_pritemx-deliv_date = gc_x. " 项目交货日期
    ls_pritemx-matl_group = gc_x. " 物料组
    ls_pritemx-plant      = gc_x. " 工厂
    ls_pritemx-store_loc  = gc_x. " 库存地点
    ls_pritemx-purch_org  = gc_x. " 采购组织
    ls_pritemx-pur_group  = gc_x. " 采购组
    ls_pritemx-preq_name  = gc_x. " 需求人
    ls_pritemx-trackingno = gc_x. " 申请部门
    ls_pritemx-delete_ind = gc_x. " 删除标识
    APPEND ls_pritemx TO lt_pritemx.

*----行项目账户数据(每个行项目有且仅有一条)
    READ TABLE lt_praccount ASSIGNING  WITH KEY preq_item = ls_item-bnfpo.
    IF sy-subrc = 0.  " 修改
      -preq_item   = |{ ls_item-bnfpo ALPHA = IN }|.  " 采购申请项目编号
      -serial_no   = '01'. " 帐户分配的顺序编号
      -asset_no    = ls_item-anln1.  " 资产
      -costcenter  = ls_item-kostl.  " 成本中心
      -orderid     = ls_item-aufnr.  " 订单号
      -wbs_element = lv_posid.  " WBS 元素
      -gl_account  = ls_item-sakto.  " 总账科目
    ELSE. " 新建
      ls_praccount-preq_item   = |{ ls_item-bnfpo ALPHA = IN }|.  " 采购申请项目编号
      ls_praccount-serial_no   = '01'. " 帐户分配的顺序编号
      ls_praccount-asset_no    = ls_item-anln1.  " 资产
      ls_praccount-costcenter  = ls_item-kostl.  " 成本中心
      ls_praccount-orderid     = ls_item-aufnr.  " 订单号
      ls_praccount-wbs_element = ls_item-ps_psp_pnr.  " WBS 元素
      ls_praccount-gl_account  = ls_item-sakto.  " 总账科目
      APPEND ls_praccount TO lt_praccount.
    ENDIF.

    ls_praccountx-preq_item   = |{ ls_item-bnfpo ALPHA = IN }|.  " 采购申请项目编号
    ls_praccountx-serial_no   = '01'. " 帐户分配的顺序编号
    ls_praccountx-asset_no    = gc_x. " 资产
    ls_praccountx-costcenter  = gc_x. " 成本中心
    ls_praccountx-orderid     = gc_x. " 订单号
    ls_praccountx-wbs_element = gc_x. " WBS 元素
    ls_praccountx-gl_account  = gc_x. " 总账科目
    APPEND ls_praccountx TO lt_praccountx.

*----行项目长文本
    lv_str = ls_item-ztext01.  " 长文本-项目文本
    TRY.
        cl_bcs_convert=>string_to_tab(
          EXPORTING
            iv_string = lv_str
          IMPORTING
            et_ctab = lt_line ).
      CATCH cx_bcs.
    ENDTRY.
    LOOP AT lt_line INTO ls_line.
      ls_pritemtext-preq_item = |{ ls_item-bnfpo ALPHA = IN }|.  " 采购申请项目编号
      ls_pritemtext-text_id   = 'B01'.   " 文本标识-项目文本
      ls_pritemtext-text_form = '*'.     " 文本格式
      ls_pritemtext-text_line = ls_line-note. " 文本行

      APPEND ls_pritemtext TO lt_pritemtext.
    ENDLOOP.

*----服务修改
    IF ps_head-bsart = 'Z007'.

      LOOP AT ls_item-service INTO DATA(ls_service).

        " 项目相关
        READ TABLE lt_srv_lines ASSIGNING  WITH KEY doc_item = ls_item-bnfpo
                                                                  srv_line = ls_service-extrow.
        IF sy-subrc = 0.
           = VALUE #( doc_item    = ls_item-bnfpo      " 采购申请项目号
                                    srv_line    = ls_service-extrow  " 外部编号
                                    service     = ls_service-srvpos  " 服务编号
                                    short_text  = ls_service-ktext1  " 服务文本
                                    quantity    = ls_service-menge   " 数量
                                    uom         = ls_service-meins   " 单位
                                    gross_price = ls_service-brtwr   " 总价格
                                     ).

        ELSE.
          ls_srv_lines = VALUE #( doc_item    = ls_item-bnfpo      " 采购申请项目号
                                  srv_line    = ls_service-extrow  " 外部编号
                                  service     = ls_service-srvpos  " 服务编号
                                  short_text  = ls_service-ktext1  " 服务文本
                                  quantity    = ls_service-menge   " 数量
                                  uom         = ls_service-meins   " 单位
                                  gross_price = ls_service-brtwr   " 总价格
                                   ).
          IF ls_srv_lines-uom IS INITIAL.
            ls_srv_lines-uom = 'LE'.  " 默认外码AU,内码LE
          ENDIF.
          APPEND ls_srv_lines TO lt_srv_lines.

        ENDIF.

        ls_srv_linesx = VALUE #( doc_item    = ls_item-bnfpo      " 采购申请项目号
                                 srv_line    = ls_service-extrow  " 外部编号
                                 service     = gc_x               " 服务编号
                                 short_text  = gc_x               " 服务文本
                                 quantity    = gc_x               " 数量
                                 uom         = gc_x               " 单位
                                 gross_price = gc_x               " 总价格
                                  ).
        APPEND ls_srv_linesx TO lt_srv_linesx.

        " 账户相关
        READ TABLE lt_srv_acc ASSIGNING  WITH KEY doc_item = ls_item-bnfpo
                                                              srv_line = ls_service-extrow.
        IF sy-subrc = 0.
            = VALUE #( doc_item    = ls_item-bnfpo      " 采购申请项目号
                                   srv_line    = ls_service-extrow  " 外部编号
                                   serial_no   = '01'
                                   serial_no_item = '01'
                                   ).
        ELSE.
          ls_srv_acc  = VALUE #( doc_item    = ls_item-bnfpo      " 采购申请项目号
                                 srv_line    = ls_service-extrow  " 外部编号
                                 serial_no   = '01'
                                 serial_no_item = '01'
                                 ).
          APPEND ls_srv_acc TO lt_srv_acc.
        ENDIF.

        ls_srv_accx  = VALUE #( doc_item    = ls_item-bnfpo      " 采购申请项目号
                                srv_line    = ls_service-extrow  " 外部编号
                                serial_no   = '01'
                                serial_no_item = gc_x
                                ).
        APPEND ls_srv_accx TO lt_srv_accx.

      ENDLOOP.

    ENDIF.

  ENDLOOP.

*----抬头数据
  ls_prheader-preq_no  = ps_head-banfn.  " 采购凭证号
  ls_prheader-pr_type  = ps_head-bsart.  " 采购申请类型

  ls_prheaderx-preq_no = gc_x.
  ls_prheaderx-pr_type = gc_x.

*----修改采购申请
  CALL FUNCTION 'BAPI_PR_CHANGE'
    EXPORTING
      number          = lv_banfn
      prheader        = ls_prheader
      prheaderx       = ls_prheaderx
    TABLES
      pritem          = lt_pritem_new
      pritemx         = lt_pritemx
      praccount       = lt_praccount
      praccountx      = lt_praccountx
      prheadertext    = lt_prheadtext
      pritemtext      = lt_pritemtext
      servicelines    = lt_srv_lines
      servicelinesx   = lt_srv_linesx
      serviceaccount  = lt_srv_acc
      serviceaccountx = lt_srv_accx
      return          = lt_return.

*----返回消息处理
  lv_subrc = sy-subrc.
  LOOP AT lt_return INTO ls_return WHERE type CA 'AEX'.
    ps_retinfo-code     = gc_e.
    ps_retinfo-message  = ps_retinfo-message && ls_return-message.
  ENDLOOP.
  IF sy-subrc = 0 OR lv_subrc NE 0.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait   = abap_true
      IMPORTING
        return = ls_return.
    WAIT UP TO '0.5' SECONDS.

    " 写入长文本-抬头备注
    lv_str = ps_head-ztext01.
    TRY.
        cl_bcs_convert=>string_to_tab(
          EXPORTING
            iv_string = lv_str
          IMPORTING
            et_ctab = lt_line ).
      CATCH cx_bcs.
    ENDTRY.
    lt_tline = VALUE #( FOR lw_line IN lt_line
                        ( tdformat = '*' tdline = lw_line-note ) ).

    ls_thead = VALUE #( tdobject = 'EBANH'
                        tdname   = lv_banfn
                        tdid     = 'B01'
                        tdspras  = sy-langu ).
    CALL FUNCTION 'SAVE_TEXT'
      EXPORTING
        client          = sy-mandt
        header          = ls_thead
        savemode_direct = 'X'
      TABLES
        lines           = lt_tline
      EXCEPTIONS
        id              = 1
        language        = 2
        name            = 3
        object          = 4
        OTHERS          = 5.
    IF sy-subrc <> 0.
    ENDIF.
  ENDIF.

ENDFORM.

你可能感兴趣的:(ABAP学习记录,abap基础,SAP,ABAP)