ABAP:VA02销售订单修改BAPI

VA02销售订单修改BAPI

FUNCTION zsdfu016.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(I_ID) TYPE  ZRFC_LOGID OPTIONAL
*"     VALUE(I_CANUM) TYPE  CANUM OPTIONAL
*"  EXPORTING
*"     VALUE(E_CODE) TYPE  MSGTY
*"     VALUE(E_MSG) TYPE  MSGTXT
*"     VALUE(E_FLOW_END) TYPE  CHAR01
*"  TABLES
*"      ET_RETURN STRUCTURE  ZRFCT005 OPTIONAL
*"----------------------------------------------------------------------
  DATA:ls_zsdt024  TYPE zsdt024,
       lt_zsdt024  TYPE TABLE OF zsdt024,
       ls_zsdt008  TYPE zsdt008,
       lt_zsdt008  TYPE TABLE OF zsdt008,
       ls_zsdt025  TYPE zsdt025,
       ls_vbap     TYPE vbap,
       lt_vbap     TYPE TABLE OF vbap,
       ls_zsdt001  TYPE zsdt001,
       ls_zrfct002 TYPE zrfct002,
       ls_zrfct005 TYPE zrfct005.

  DATA:lv_canum      TYPE canum,
       lv_zrfc_logid TYPE zsdt008-zrfc_logid,
       lv_vbeln      TYPE vbak-vbeln,
       lv_waers      TYPE vbak-waerk,
       lv_waers_1    TYPE vbak-waerk,,
       lv_kbetr      TYPE konp-kbetr,
       lv_knumh      TYPE konp-knumh,
       lv_mtart      TYPE mara-mtart.

  DATA:lv_canum_2 TYPE canum,
       lv_code    TYPE msgty,
       lv_msg     TYPE msgtxt,
       lt_return  TYPE TABLE OF zrfct005.
  DATA:lv_mwskz TYPE konv-mwsk1.

  CLEAR:lt_zsdt008,ls_zsdt008,lv_canum,lv_zrfc_logid,lv_vbeln,lv_waers,ls_zsdt025,ls_zsdt001.

  "获取源表数据
  CLEAR:lt_zsdt024.
  SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_zsdt024 FROM zsdt024 WHERE zrfc_logid = i_id.
  IF lt_zsdt024 IS INITIAL.
    e_code = 'E'.
    e_msg  = '无可执行数据'.
    RETURN.
  ENDIF.

  READ TABLE lt_zsdt024 INTO ls_zsdt024 INDEX 1.

  SELECT SINGLE * INTO ls_zsdt025 FROM zsdt025 WHERE zrfcid = ls_zsdt024-zrfcid AND canum = i_canum." AND bsart = ls_zsdt024-bsart.

  SELECT SINGLE zrfc_logid INTO lv_zrfc_logid FROM zsdt008 WHERE zrfcid = ls_zsdt025-zzrfcid AND canum = '0010' AND objkey = ls_zsdt024-ebeln.

  SELECT * INTO TABLE lt_zsdt008 FROM zsdt008 WHERE zrfc_logid = lv_zrfc_logid.
*
  SORT lt_zsdt008 BY zrfc_logid canum.

  READ TABLE lt_zsdt008 INTO ls_zsdt008 WITH KEY canum = ls_zsdt025-zcanum.

  SELECT SINGLE * INTO ls_zsdt001 FROM zsdt001 WHERE zrfcid = ls_zsdt008-zrfcid AND canum = ls_zsdt008-canum.
  lv_vbeln = ls_zsdt008-objkey.

  DATA:ls_vbak TYPE vbak,
       ls_konv TYPE konv,
       lt_konv TYPE TABLE OF konv.
  SELECT SINGLE * INTO ls_vbak FROM vbak WHERE vbeln = lv_vbeln.
  SELECT * INTO TABLE lt_vbap FROM vbap WHERE vbeln = lv_vbeln.

  lv_waers_1 = ls_vbak-waerk.

  SELECT * INTO TABLE lt_konv FROM konv WHERE knumv = ls_vbak-knumv.
  READ TABLE lt_konv INTO ls_konv WITH KEY kschl = 'MWST'.
  lv_mwskz = ls_konv-mwsk1.
  "销售订单修改BAPI结构定义
  DATA: salesdocument           LIKE  bapivbeln-vbeln,
        order_header_in         LIKE  bapisdh1,
        order_header_inx        LIKE  bapisdh1x,
        order_item_in           LIKE  bapisditm  OCCURS 0 WITH HEADER LINE,
        order_item_inx          LIKE  bapisditmx OCCURS 0 WITH HEADER LINE,
        return1                 LIKE  bapiret2   OCCURS 0 WITH HEADER LINE,
        order_schedules_in      LIKE  bapischdl  OCCURS 0 WITH HEADER LINE,
        order_schedules_inx     LIKE  bapischdlx OCCURS 0 WITH HEADER LINE,
        order_conditions_in     LIKE  bapicond   OCCURS 0 WITH HEADER LINE,
        order_conditions_inx    LIKE  bapicondx  OCCURS 0 WITH HEADER LINE,
        logic_switch            LIKE  bapisdls,
        ls_bapi_view            TYPE order_view,
        lt_salesdocuments       TYPE TABLE OF sales_key,
        ls_salesdocuments       TYPE sales_key,
        lt_order_conditions_out TYPE TABLE OF bapisdcond,
        ls_order_conditions_out TYPE bapisdcond.

  CLEAR:ls_bapi_view,ls_salesdocuments,lt_salesdocuments,lt_order_conditions_out.
  ls_bapi_view-sdcond = 'X'.
  ls_salesdocuments   = lv_vbeln.
  APPEND ls_salesdocuments TO lt_salesdocuments.
  CALL FUNCTION 'BAPISDORDER_GETDETAILEDLIST'
    EXPORTING
      i_bapi_view          = ls_bapi_view
    TABLES
      sales_documents      = lt_salesdocuments
      order_conditions_out = lt_order_conditions_out.

  "取税率
  IF lv_mwskz IS NOT INITIAL.
    CLEAR:lv_kbetr,lv_knumh.
    SELECT SINGLE knumh INTO lv_knumh FROM a003 WHERE kappl = 'TX' AND aland = 'CN' AND mwskz = lv_mwskz.
    SELECT SINGLE kbetr INTO lv_kbetr FROM konp WHERE knumh = lv_knumh.
    lv_kbetr = 1 + lv_kbetr / 1000.
  ELSE.
    lv_kbetr = 1.
  ENDIF.


  DATA:ls_ekko TYPE ekko,
       ls_ekpo TYPE ekpo.
  READ TABLE lt_zsdt024 INTO ls_zsdt024 INDEX 1.
  IF sy-subrc = 0.
    SELECT SINGLE * FROM ekko INTO ls_ekko WHERE ebeln = ls_zsdt024-ebeln.
  ENDIF.
  DATA:lv_uebpo TYPE ekpo-uebpo,
       lv_uptyp TYPE ekpo-uptyp.
  DATA:lv_from_cur TYPE bapi1093_1-from_curr,
       lv_to_cur   TYPE bapi1093_1-to_currncy,
       ls_rate     TYPE bapi1093_0,
       ls_return   TYPE bapiret1.
  LOOP AT lt_zsdt024 INTO ls_zsdt024.

    SELECT SINGLE *
      INTO @DATA(ls_ekpo2)
      FROM ekpo
     WHERE ebeln = @ls_zsdt024-ebeln
       AND ebelp = @ls_zsdt024-ebelp.

    IF lv_uebpo NE '' AND lv_uptyp = 4.
      CLEAR:ls_ekpo2.
      CONTINUE.
    ENDIF.
    IF ls_zsdt024-zbs = 'I'. "新增
*      IF lv_ukurs IS INITIAL.
*        lv_ukurs = 1.
*      ENDIF.
      order_item_in-itm_number     = ls_zsdt024-ebelp.
      order_item_inx-itm_number     =   ls_zsdt024-ebelp.
      order_item_inx-updateflag     =  'I'.
      order_item_in-material    = ls_zsdt024-matnr."物料
      order_item_inx-material   = 'X'.

      order_item_in-target_qty  = ls_zsdt024-menge."数量
      order_item_inx-target_qty = 'X'.

      order_item_in-sales_unit = ls_zsdt024-vrkme."单位
      order_item_inx-sales_unit = 'X'.

      order_item_in-plant = ls_zsdt001-werks. "工厂
      order_item_inx-plant = 'X'.

      order_item_in-price_date = sy-datum. "定价日期
      order_item_inx-price_date = 'X'.
      order_item_in-po_itm_no = ls_zsdt024-ebelp.
      order_item_inx-po_itm_no = 'X'.
****ADD BY ZJ 20221203 S 转换因子
      order_item_in-salqtynum = ls_ekpo2-umrez.
      order_item_in-salqtyden = ls_ekpo2-umren.

      order_item_inx-salqtynum = 'X'.
      order_item_inx-salqtyden = 'X'.
****ADD BY ZJ 20221203 E 转换因子

      APPEND:order_item_inx,order_item_in.
      CLEAR:order_item_in,order_item_inx.

      IF ls_zsdt001-kschl IS NOT INITIAL.
        order_conditions_in-itm_number = ls_zsdt024-ebelp.  "itm_number
        order_conditions_in-cond_type  = ls_zsdt001-kschl.            "定价条件
        SELECT SINGLE knumv INTO @DATA(lv_knumv) FROM ekko WHERE ebeln = @ls_zsdt024-ebeln.
        SELECT SINGLE kbetr waers kmein stunr zaehk
          FROM konv
          INTO ( order_conditions_in-cond_value,
                 order_conditions_in-currency,
                 order_conditions_in-cond_unit,
                 order_conditions_in-cond_st_no,
                 order_conditions_in-cond_count )
         WHERE knumv = lv_knumv
           AND kposn = ls_zsdt024-ebelp
           AND ( kschl = 'PB00' OR kschl = 'PBXX' )
           AND kinak = ''.
        CLEAR:lv_knumv.
        order_conditions_in-cond_p_unt  = ls_zsdt024-kpein..  "条件单位
        APPEND order_conditions_in.
        CLEAR order_conditions_in.

        order_conditions_inx-itm_number = ls_zsdt024-ebelp.
        order_conditions_inx-cond_type  = ls_zsdt001-kschl.
        order_conditions_inx-cond_value = 'X'.
        order_conditions_inx-currency   = 'X'.
        order_conditions_inx-cond_unit  = 'X'.
        order_conditions_inx-cond_p_unt = 'X'.
        order_conditions_inx-cond_st_no = 'X'.
        order_conditions_inx-cond_count = 'X'.
        order_conditions_inx-updateflag = 'U'.

        APPEND order_conditions_inx.
        CLEAR  order_conditions_inx.

      ENDIF.

      "交货计划行
      order_schedules_in-itm_number = ls_zsdt024-ebelp.
      order_schedules_inx-itm_number = ls_zsdt024-ebelp.
      order_schedules_inx-updateflag = 'I'.
      order_schedules_in-req_qty     = ls_zsdt024-menge.
      order_schedules_inx-req_qty    = 'X'.
      order_schedules_in-req_date   = ls_zsdt024-eindt.
      order_schedules_inx-req_date   = 'X'.
      APPEND:order_schedules_in,order_schedules_inx.
      CLEAR:order_schedules_in,order_schedules_inx.

    ELSEIF ls_zsdt024-zbs = 'U'. "修改
      order_item_in-itm_number     = ls_zsdt024-ebelp.
      order_item_inx-itm_number     =   ls_zsdt024-ebelp.
      order_item_inx-updateflag     =  'U'.
      order_item_in-target_qty  = ls_zsdt024-menge."数量
      order_item_inx-target_qty = 'X'.
      order_item_in-sales_unit = ls_zsdt024-vrkme."单位
      order_item_inx-sales_unit = 'X'.
      IF ls_zsdt024-flag = 'X'.
        order_item_in-reason_rej  = ''.
        order_item_inx-reason_rej = 'X'.
      ENDIF.
      SELECT SINGLE * FROM vbap INTO @DATA(ls_vbap_2) WHERE vbeln = @lv_vbeln AND posnr = @ls_zsdt024-ebelp.
****ADD BY ZJ 20221204 S 判断行项目是否做更改了
      IF ls_vbap_2-kwmeng NE ls_zsdt024-menge OR
         ls_vbap_2-vrkme NE ls_zsdt024-vrkme OR
         ( ls_vbap_2-abgru NE '' AND ls_zsdt024-flag = 'X' ).
        APPEND:order_item_inx,order_item_in.
      ENDIF.
      CLEAR:order_item_in,order_item_inx.
****ADD BY ZJ 20221204 E 判断行项目是否做更改了
***------价格

      IF ls_zsdt024-kbetr IS NOT INITIAL.
        CLEAR:ls_order_conditions_out.
        READ TABLE lt_order_conditions_out INTO ls_order_conditions_out WITH KEY sd_doc = lv_vbeln itm_number = ls_zsdt024-ebelp cond_type = ls_zsdt001-kschl.

        order_conditions_in-cond_st_no  = ls_order_conditions_out-cond_st_no.
        order_conditions_inx-cond_st_no = ls_order_conditions_out-cond_st_no.
        order_conditions_in-cond_count  = ls_order_conditions_out-cond_count."计数器
        order_conditions_inx-cond_count = ls_order_conditions_out-cond_count.
        order_conditions_in-cond_type   = ls_order_conditions_out-cond_type."条件类型
        order_conditions_inx-cond_type  = ls_order_conditions_out-cond_type."条件类型.
        order_conditions_in-cond_value  = ls_zsdt024-kbetr."" * lv_kbetr
        IF ls_zsdt024-bsart = 'ZFTO' OR ls_zsdt024-bsart = 'ZITO'.      ""非进口单据,货币使用源头订单货币
          order_conditions_in-currency    = ls_zsdt024-koein.
        ELSE.
          IF ls_order_conditions_out-currency <> ls_zsdt024-koein.
            CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
              EXPORTING
                date             = sy-datum
                foreign_amount   = order_conditions_in-cond_value
                foreign_currency = ls_zsdt024-koein
                local_currency   = ls_order_conditions_out-currency
              IMPORTING
                local_amount     = order_conditions_in-cond_value.
          ENDIF.
          order_conditions_in-currency    = ls_order_conditions_out-currency."货币
        ENDIF.
        order_conditions_inx-cond_value = 'X'.
*      order_conditions_in-currency    = ls_order_conditions_out-currency."货币
*      order_conditions_in-currency    = ls_zsdt024-koein.
        order_conditions_inx-currency   = 'X'.
        order_conditions_in-cond_unit   = ls_order_conditions_out-cond_d_unt.  "条件单位
        order_conditions_inx-cond_unit  = 'X'.
        order_conditions_in-cond_p_unt  = ls_zsdt024-kpein.  "条件定价单位 UPDATE BY JT 20221018
        order_conditions_inx-cond_p_unt = 'X'.
        order_conditions_inx-updateflag = 'U'.
        APPEND:order_conditions_in,order_conditions_inx.
        CLEAR:order_conditions_in,order_conditions_inx.
      ENDIF.

      "交货计划行
      SELECT SINGLE * FROM vbep INTO @DATA(ls_vbep) WHERE vbeln = @lv_vbeln AND posnr = @ls_zsdt024-ebelp
                                                      AND etenr = '0001'.
      order_schedules_in-itm_number = ls_zsdt024-ebelp.
      order_schedules_inx-itm_number = ls_zsdt024-ebelp.
      order_schedules_in-sched_line = '0001'.
      order_schedules_inx-sched_line = '0001'.
      order_schedules_inx-updateflag = 'U'.
      order_schedules_in-req_qty     = ls_zsdt024-menge.
      order_schedules_inx-req_qty    = 'X'.
      order_schedules_in-req_date   = ls_zsdt024-eindt.
      order_schedules_inx-req_date   = 'X'.
****ADD BY ZJ 20221204 S 判断行项目是否做更改了
      IF ls_vbep-wmeng NE ls_zsdt024-menge OR
          ls_vbep-edatu NE ls_zsdt024-eindt.
        APPEND:order_schedules_in,order_schedules_inx.
      ENDIF.
****ADD BY ZJ 20221204 E 判断行项目是否做更改了
      CLEAR:order_schedules_in,order_schedules_inx.


    ELSEIF ls_zsdt024-zbs = 'D'. "删除
      order_item_in-itm_number     = ls_zsdt024-ebelp.
      order_item_inx-itm_number     =   ls_zsdt024-ebelp.
      order_item_inx-updateflag     =  'U'.
*      order_item_inx-updateflag     =  'D'.
      order_item_in-reason_rej  = ls_zsdt025-abgru."拒绝原因
      order_item_inx-reason_rej = 'X'.
      APPEND:order_item_inx,order_item_in.
      CLEAR:order_item_in,order_item_inx.
    ENDIF.

    CLEAR:ls_zsdt024,ls_ekpo2.
  ENDLOOP.
  logic_switch-pricing    = 'G'. "注意手工的价格条件一定要给C,否则会出现定价失败
  order_header_inx-updateflag = 'U'.

  READ TABLE lt_zsdt024 INTO ls_zsdt024 INDEX 1.
  order_header_in-pmnttrms = ls_zsdt024-zterm.
  order_header_inx-pmnttrms = 'X'.

  DATA:lv_flag.
  LOOP AT order_item_in WHERE reason_rej NE '' .

  ENDLOOP.
  IF sy-subrc = 0.
    lv_flag = 'X'.
  ELSE.
    LOOP AT lt_zsdt024 TRANSPORTING NO FIELDS WHERE uebpo <> ''
                                                 AND uptyp = 4
                                                 AND zbs = 'D'.
    ENDLOOP.
    IF sy-subrc = 0.
      lv_flag = 'X'.
    ENDIF.
  ENDIF.

  IF lv_flag = 'X'.
    lv_canum_2 = i_canum + 10.
    CALL FUNCTION 'ZSDFU023'
      EXPORTING
        i_id      = i_id
        i_canum   = lv_canum_2
      IMPORTING
        e_code    = lv_code
        e_msg     = lv_msg
      TABLES
        et_return = lt_return.

    IF lv_code EQ 'E'.
      e_code = 'E'.
      e_msg = lv_msg.
      RETURN.
    ENDIF.
  ENDIF.

  CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
    EXPORTING
      salesdocument    = lv_vbeln
      order_header_in  = order_header_in
      order_header_inx = order_header_inx
      logic_switch     = logic_switch
    TABLES
      return           = return1
      order_item_in    = order_item_in
      order_item_inx   = order_item_inx
      schedule_lines   = order_schedules_in
      schedule_linesx  = order_schedules_inx
      conditions_in    = order_conditions_in
      conditions_inx   = order_conditions_inx.

  LOOP AT return1 WHERE type = 'E' OR type = 'A' OR type = 'X'.
    CONCATENATE e_msg return1-message INTO e_msg SEPARATED BY '/'.
  ENDLOOP.
  IF sy-subrc NE 0.
    CALL METHOD zcl_rfc=>get_source
      EXPORTING
        i_id       = i_id
        i_canum    = i_canum
      IMPORTING
        e_zrfct002 = ls_zrfct002.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    CLEAR:ls_zsdt008.
    ls_zsdt008-canum      = ls_zrfct002-canum.
    ls_zsdt008-objtype    = ls_zrfct002-objtype_save.
    ls_zsdt008-objkey     = lv_vbeln.
    ls_zsdt008-zrfcid     = ls_zrfct002-zrfcid.
    ls_zsdt008-zrfc_logid = i_id.
    APPEND ls_zsdt008 TO lt_zsdt008.

    MODIFY zsdt008 FROM TABLE lt_zsdt008.

    e_code = 'S'.
    CONCATENATE 'SO:' lv_vbeln '修改成功' INTO e_msg.
    CLEAR:ls_zrfct005.
    ls_zrfct005-status = '53'.
    ls_zrfct005-msgty = e_code.
    ls_zrfct005-msgid = '00'.
    ls_zrfct005-msgno = '001'.
    ls_zrfct005-msgv1 = e_msg.
    ls_zrfct005-msgv4 = lv_vbeln.
    APPEND ls_zrfct005 TO et_return.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    e_code = 'E'.
  ENDIF.

ENDFUNCTION.

 

posted @ 2022-12-19 10:54  阿胖的阿多  阅读(1291)  评论(0编辑  收藏  举报