首页 > 其他分享 >ABAP:VA02销售订单修改BAPI

ABAP:VA02销售订单修改BAPI

时间:2022-12-19 11:00:38浏览次数:64  
标签:BAPI inx VA02 lv ABAP ls zsdt024 conditions order

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.

 

标签:BAPI,inx,VA02,lv,ABAP,ls,zsdt024,conditions,order
From: https://www.cnblogs.com/ap-ad-java-abap/p/16991659.html

相关文章

  • ABAP:销售订单审批及反审函数
    销售订单审批及反审函数:DATA:ls_zsdt028TYPEzsdt028,lt_zsdt028TYPETABLEOFzsdt028,ls_zsdt008TYPEzsdt008,lt_zsdt008TYPETABLE......
  • ABAP:MIGO收货BAPI
    MIGO收货BAPI:DATA:lt_zsdt008TYPETABLEOFzsdt008,ls_zsdt008LIKELINEOFlt_zsdt008,ls_zsdt008_2LIKELINEOFlt_zsdt008,ls_z......
  • SAP ABAP CDS view 里 INNER JOIN 和 Association 的区别
    最近有朋友在我的知识星球里向我提问,SAPABAPCDSview的INNERJOIN和Association的功能可以理解为一样吗?(关于加入我的知识星球的方式,请移步本文文末)本文就来聊一......
  • WebApi 路由机制剖析
    从网上看了WEBAPI理解感觉不错分享一下一、MVC和WebApi路由机制比较1、MVC里面的路由在MVC里面,默认路由机制是通过url路径去匹配对应的action方法,比如/Home/GetUser这个......
  • WebApi 接口参数不再困惑:传参详解
    从网上看了WEBAPI理解感觉是不错的分享一下前言:还记得刚使用WebApi那会儿,被它的传参机制折腾了好久,查阅了半天资料。如今,使用WebApi也有段时间了,今天就记录下API接口传参......
  • SAP ABAP CDS view Association 引入的缘由
    ABAPCDSview支持三种join方式:InnerJoinLeftOuterjoinRightouterjoin我们使用ABAPDevelopmentTool的CDSview向导创建一个CDSview:向导里包含的$......
  • 在Asp.Net WebApi中使用DotNetZip压缩响应流过滤器
    usingIonic.Zlib;usingSystem;usingSystem.IO;usingSystem.Linq;usingSystem.Net.Http;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.......
  • APAP:销售订单创建BAPI
    销售订单创建BAPI: SD_SALESDOCUMENT_CREATEDATA:lt_zsdt008LIKETABLEOFzsdt008,ls_zsdt008LIKELINEOFlt_zsdt008,ls_zsdt001TYPEzsdt001,......
  • ABAP:采购订单创建BAPI
    采购订单创建BAPI:BAPI_PO_CREATE1DATA:lt_zsdt008LIKETABLEOFzsdt008,ls_zsdt008LIKELINEOFlt_zsdt008,ls_zsdt001TYPEzsdt001,lt_z......
  • WebApi Autofac依赖注入配置
    publicclassAutofacConfig{///<summary>///IOC注册///</summary>publicstaticvoidRegister(){......