SD 销售订单修改:BAPI_SALESORDER_CHANGE

上代码:

*&---------------------------------------------------------------------*
*& Form frm_so_modify
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> IS_INPUT
*&      <-- ES_OUTPUT
*&      <-- LV_FLG
*&---------------------------------------------------------------------*
FORM frm_so_modify  USING    us_input  TYPE zssd005a
                    CHANGING cs_output TYPE ty_return_so_m
                             cv_flg    TYPE char01.

  DATA: ls_order_header_in      TYPE bapisdh1,
        ls_order_header_inx     TYPE bapisdh1x,
        lt_order_partners       TYPE TABLE OF bapiparnr,
        lt_partnerchanges       TYPE TABLE OF bapiparnrc,
        lt_order_items_in       TYPE TABLE OF bapisditm,
        lt_order_items_inx      TYPE TABLE OF bapisditmx,
        lt_order_schedules_in   TYPE TABLE OF bapischdl,
        lt_order_schedules_inx  TYPE TABLE OF bapischdlx,
        lt_order_conditions_in  TYPE TABLE OF bapicond,
        lt_order_conditions_inx TYPE TABLE OF bapicondx,
        ls_extensionin          TYPE bapiparex,
        lt_extensionin          TYPE TABLE OF bapiparex,
        ls_bape_vbak            TYPE bape_vbak,
        ls_bape_vbap            TYPE bape_vbap,
        ls_bape_vbakx           TYPE bape_vbakx,
        ls_bape_vbapx           TYPE bape_vbapx,
        lt_return               TYPE TABLE OF bapiret2,
        lv_salesdocument        TYPE bapivbeln-vbeln,
        lv_message              TYPE string,
        ls_logic_switch         TYPE bapisdls.

* 数据转换
  us_input-header-vbeln  = |{ us_input-header-vbeln ALPHA = IN }|.

* 订单类型 借贷项/标准
  SELECT SINGLE tvak~vbtyp
    FROM tvak
    INNER JOIN vbak
    ON tvak~auart = vbak~auart
    INTO @DATA(lv_vbtyp1)
    WHERE vbeln = @us_input-header-vbeln.

* 抬头
  CLEAR:ls_order_header_in,
        ls_order_header_inx.
  ls_order_header_in-sales_off   = us_input-header-vkbur. " 产品组
  ls_order_header_in-sales_grp   = us_input-header-vkgrp. " 销售组
  ls_order_header_in-purch_no_c  = us_input-header-bstkd. " 客户采购订单号
  ls_order_header_in-purch_date  = us_input-header-bstdk. " 客户参考日期
  ls_order_header_in-pmnttrms    = us_input-header-zterm. " 收付条件代码
  ls_order_header_in-currency    = us_input-header-waerk. " 销售和分销凭证货币
  ls_order_header_in-ord_reason  = us_input-header-augru. " 订单原因

  ls_order_header_inx-updateflag = 'U'.
  ls_order_header_inx-sales_off  = COND #( WHEN ls_order_header_in-sales_off  IS NOT INITIAL THEN abap_true )." 产品组
  ls_order_header_inx-sales_grp  = COND #( WHEN ls_order_header_in-sales_grp  IS NOT INITIAL THEN abap_true )." 销售组
  ls_order_header_inx-purch_no_c = COND #( WHEN ls_order_header_in-purch_no_c IS NOT INITIAL THEN abap_true )." 客户采购订单号
  ls_order_header_inx-purch_date = COND #( WHEN ls_order_header_in-purch_date IS NOT INITIAL THEN abap_true )." 客户参考日期
  ls_order_header_inx-pmnttrms   = COND #( WHEN ls_order_header_in-pmnttrms   IS NOT INITIAL THEN abap_true )." 收付条件代码
  ls_order_header_inx-currency   = COND #( WHEN ls_order_header_in-currency   IS NOT INITIAL THEN abap_true )." 销售和分销凭证货币
  ls_order_header_inx-ord_reason = COND #( WHEN ls_order_header_in-ord_reason IS NOT INITIAL THEN abap_true )." 订单原因

* 伙伴
  CLEAR:lt_partnerchanges.
  APPEND INITIAL LINE TO lt_partnerchanges ASSIGNING FIELD-SYMBOL().
  -document   = us_input-header-vbeln.
  -updateflag = 'U'.
  -partn_role = 'AG'.  "售达方
  us_input-header-kunnr1 = |{ us_input-header-kunnr1 ALPHA = IN }|.
  -p_numb_new = us_input-header-kunnr1.

  APPEND INITIAL LINE TO lt_partnerchanges ASSIGNING .
  -document   = us_input-header-vbeln.
  -updateflag = 'U'.
  -partn_role = 'WE'.  "送达方
  us_input-header-kunnr2 = |{ us_input-header-kunnr2 ALPHA = IN }|.
  -p_numb_new = us_input-header-kunnr2.

* 增强字段
  CLEAR:lt_extensionin,
        ls_extensionin.
  CLEAR:ls_bape_vbak,
        ls_bape_vbakx.

  ls_bape_vbak-vbeln     = us_input-header-vbeln.
  ls_bape_vbak-kostl     = us_input-header-kostl.
  ls_bape_vbak-zwwxt     = us_input-header-zwwxt.
  ls_bape_vbak-znumb     = us_input-header-znumb.
  ls_bape_vbak-zjjcd     = us_input-header-zjjcd.

  ls_bape_vbakx-vbeln    = us_input-header-vbeln.
  ls_bape_vbakx-znumb    = us_input-header-znumb.
  ls_bape_vbakx-kostl    = COND #( WHEN ls_bape_vbak-kostl IS NOT INITIAL THEN abap_true ).
  ls_bape_vbakx-zwwxt    = COND #( WHEN ls_bape_vbak-zwwxt IS NOT INITIAL THEN abap_true ).
  ls_bape_vbakx-zjjcd    = COND #( WHEN ls_bape_vbak-zjjcd IS NOT INITIAL THEN abap_true ).

  ls_extensionin-structure = 'BAPE_VBAK'.
  ls_extensionin+30(960)   = ls_bape_vbak.
  APPEND ls_extensionin TO lt_extensionin.

  CLEAR ls_extensionin.
  ls_extensionin-structure = 'BAPE_VBAKX'.
  ls_extensionin+30(960)   = ls_bape_vbakx.
  APPEND ls_extensionin TO lt_extensionin.

  CLEAR:lt_order_items_in,
        lt_order_items_inx.
  CLEAR:lt_order_schedules_in,
        lt_order_schedules_inx.
  LOOP AT us_input-item ASSIGNING FIELD-SYMBOL().
*   行项目
    APPEND INITIAL LINE TO lt_order_items_in ASSIGNING FIELD-SYMBOL().
    -itm_number    = -posnr.  " 行项目
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        input        = -matnr
      IMPORTING
        output       = -matnr
      EXCEPTIONS
        length_error = 1
        OTHERS       = 2.
    -material_long = -matnr.  " 物料
    IF lv_vbtyp1 = 'L'
      OR lv_vbtyp1 = 'K'.
      -target_qty  = -kwmeng. " 数量
      CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
        EXPORTING
          input          = -vrkme
          language       = sy-langu
        IMPORTING
          output         = -vrkme
        EXCEPTIONS
          unit_not_found = 1
          OTHERS         = 2.
      -sales_unit  = -vrkme.  " 销售单位
    ENDIF.
    -reason_rej    = -abgru.  " 拒绝原因
    -plant         = -werks.  " 工厂
    -wbs_elem      = -zpspid.

    APPEND INITIAL LINE TO lt_order_items_inx ASSIGNING FIELD-SYMBOL().
    IF -zflag = 'M'.
      -updateflag    = 'U'.
    ELSEIF -zflag = 'A'.
      -updateflag    = 'I'.
    ENDIF.
    -itm_number    = -posnr. " 行项目
    -material_long = COND #( WHEN -material_long IS NOT INITIAL THEN abap_true ).
    IF lv_vbtyp1 = 'L'
      OR lv_vbtyp1 = 'K'.
      -target_qty  = COND #( WHEN -target_qty IS NOT INITIAL THEN abap_true ).
      -sales_unit  = COND #( WHEN -sales_unit IS NOT INITIAL THEN abap_true ).
    ENDIF.
    -reason_rej    = COND #( WHEN -reason_rej IS NOT INITIAL THEN abap_true ).
    -plant         = COND #( WHEN -plant      IS NOT INITIAL THEN abap_true ).
    -wbs_elem      = COND #( WHEN -wbs_elem   IS NOT INITIAL THEN abap_true ).

*   计划行
    IF lv_vbtyp1 NE 'L'
      AND lv_vbtyp1 NE 'K'.
      APPEND INITIAL LINE TO lt_order_schedules_in ASSIGNING FIELD-SYMBOL().
      -itm_number = -posnr.
      -req_qty    = -kwmeng.
      -sched_line = '1'.

      -req_date  = -edatu.
      -dlv_date  = -edatu.
      -date_type = '1'.

      APPEND INITIAL LINE TO lt_order_schedules_inx ASSIGNING FIELD-SYMBOL().
      IF -zflag = 'M'.
        -updateflag    = 'U'.
      ELSEIF -zflag = 'A'.
        -updateflag    = 'I'.
      ENDIF.
      -itm_number = -posnr.
      -req_qty    = COND #( WHEN -req_qty   IS NOT INITIAL THEN abap_true ).
      -sched_line = '1'.
      -req_date   = COND #( WHEN -req_date  IS NOT INITIAL THEN abap_true ).
      -dlv_date   = COND #( WHEN -dlv_date  IS NOT INITIAL THEN abap_true ).
      -date_type  = COND #( WHEN -date_type IS NOT INITIAL THEN abap_true ).
    ENDIF.

*   增强字段
    CLEAR:ls_extensionin.
    CLEAR:ls_bape_vbap,
          ls_bape_vbapx.
    ls_bape_vbap-vbeln   = us_input-header-vbeln.
    ls_bape_vbap-posnr   = -posnr.
    ls_bape_vbap-zmatnr  = -zmatnr.
    ls_bape_vbap-zpspid  = -zpspid.

    ls_bape_vbapx-vbeln  = us_input-header-vbeln.
    ls_bape_vbapx-posnr  = -posnr.
    ls_bape_vbapx-zmatnr = COND #( WHEN ls_bape_vbap-zmatnr IS NOT INITIAL THEN abap_true ).
    ls_bape_vbapx-zpspid = COND #( WHEN ls_bape_vbap-zpspid IS NOT INITIAL THEN abap_true ).

    ls_extensionin-structure = 'BAPE_VBAP'.
    ls_extensionin+30(960)   = ls_bape_vbap.
    CALL METHOD cl_abap_container_utilities=>fill_container_c
      EXPORTING
        im_value     = ls_bape_vbap
      IMPORTING
        ex_container = ls_extensionin+30.
    APPEND ls_extensionin TO lt_extensionin.

    CLEAR ls_extensionin.
    ls_extensionin-structure = 'BAPE_VBAPX'.
    ls_extensionin+30(960)   = ls_bape_vbapx.
    CALL METHOD cl_abap_container_utilities=>fill_container_c
      EXPORTING
        im_value     = ls_bape_vbapx
      IMPORTING
        ex_container = ls_extensionin+30.
    APPEND ls_extensionin TO lt_extensionin.

  ENDLOOP.

* 条件
* 价格条件,需要设置该参数,才能够修改价格条件
  ls_logic_switch-cond_handl = 'X'.
  LOOP AT us_input-conditions INTO DATA(ls_conditions).
*   需要读取已经存在行的Key
    SELECT SINGLE knumv INTO @DATA(lv_knumv) FROM vbak WHERE  vbeln = @us_input-header-vbeln.
    SELECT SINGLE MAX( stunr ) , MAX(  zaehk )
      INTO (@DATA(lv_stunr),@DATA(lv_zaehk) )
      FROM prcd_elements
      WHERE  knumv = @lv_knumv
        AND kposn = @ls_conditions-kposn
        AND kschl = @ls_conditions-kschl .
    APPEND INITIAL LINE TO lt_order_conditions_in ASSIGNING FIELD-SYMBOL().
    -itm_number = ls_conditions-kposn.
    -cond_type  = ls_conditions-kschl. " 条件类型
    -cond_value = ls_conditions-kbetr. " 金额
    -currency   = ls_conditions-koein. " 定价货币
    -cond_p_unt = ls_conditions-kpein. " 定价单位
****    -cond_st_no = lv_stunr.
****    -cond_count = lv_zaehk.
    SELECT COUNT(*) FROM t006a WHERE spras EQ sy-langu
                          AND msehi EQ ls_conditions-kmein.
    IF sy-subrc <> 0.
      CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
        EXPORTING
          input    = ls_conditions-kmein
          language = sy-langu
        IMPORTING
          output   = ls_conditions-kmein.
    ENDIF.
    -cond_unit = ls_conditions-kmein. "条件单位

    APPEND INITIAL LINE TO lt_order_conditions_inx ASSIGNING FIELD-SYMBOL().
    -itm_number = ls_conditions-kposn.
    -cond_type  = ls_conditions-kschl.
    IF ls_conditions-zflag = 'M'.
      -updateflag    = 'U'.
    ELSEIF ls_conditions-zflag = 'A'.
      -updateflag    = 'I'.
    ENDIF.
    -cond_value = COND #( WHEN -cond_value IS NOT INITIAL THEN abap_true ).
    -currency   = COND #( WHEN -currency   IS NOT INITIAL THEN abap_true ).
    -cond_p_unt = COND #( WHEN -cond_p_unt IS NOT INITIAL THEN abap_true ).
    -cond_unit  = COND #( WHEN ls_bape_vbap-zpspid IS NOT INITIAL THEN abap_true ).
****    -cond_st_no = lv_stunr.
****    -cond_count = lv_zaehk.

  ENDLOOP.

* 调用BAPI
  CLEAR lt_return.
  CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
    EXPORTING
      salesdocument    = us_input-header-vbeln
      order_header_in  = ls_order_header_in
      order_header_inx = ls_order_header_inx
      logic_switch     = ls_logic_switch
    TABLES
      return           = lt_return
      order_item_in    = lt_order_items_in
      order_item_inx   = lt_order_items_inx
      partnerchanges   = lt_partnerchanges
      schedule_lines   = lt_order_schedules_in
      schedule_linesx  = lt_order_schedules_inx
      conditions_in    = lt_order_conditions_in
      conditions_inx   = lt_order_conditions_inx
      extensionin      = lt_extensionin.

  READ TABLE lt_return INTO DATA(ls_return) INDEX lines( lt_return ).
  IF ls_return-type CA 'EA'.
    cs_output-status = 'E'.
    CONCATENATE cs_output-msg  '销售订单修改失败:' INTO cs_output-msg.
    LOOP AT lt_return  INTO ls_return WHERE type EQ 'E'.
      CLEAR lv_message.
      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
        EXPORTING
          msgid               = ls_return-id
          msgnr               = ls_return-number
          msgv1               = ls_return-message_v1
          msgv2               = ls_return-message_v2
          msgv3               = ls_return-message_v3
          msgv4               = ls_return-message_v4
        IMPORTING
          message_text_output = lv_message.
      CONCATENATE cs_output-msg '/' lv_message INTO cs_output-msg.
    ENDLOOP.

    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

  ELSE.
    cs_output-status   = 'S'.
    cs_output-msg = '销售订单:' && us_input-header-vbeln  && '修改成功'.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
  ENDIF.

ENDFORM.

你可能感兴趣的:(BAPI,程序人生)