SAP BAPI创建销售订单&BAPI扩展字段增强

1、销售订单BAPI创建

退换货销售订单 :SD_SALESDOCUMENT_CREATE

正常销售订单 :BAPI_CUSTOMERRETURN_CREATE

借贷项 :BAPI_SALESORDER_CREATEFROMDAT2
    

退换货销售订单 :SD_SALESDOCUMENT_CREATE

正常销售订单 :BAPI_CUSTOMERRETURN_CREATE

借贷项/退货不退款 :BAPI_SALESORDER_CREATEFROMDAT2


  DATA : lv_posnr   TYPE char6 .
  DATA : lv_sched   TYPE char4 .
  DATA : lv_message TYPE char200 .

  DATA : lv_vbeln                TYPE vbeln_va ,           "销售订单
         lv_order_header_in      TYPE bapisdhd1,           "抬头
         lv_order_header_inx     TYPE bapisdhd1x,
         lt_order_partners       TYPE TABLE OF bapiparnr,  "合作伙伴
         ls_order_partners       TYPE bapiparnr,
         lt_order_items_in       TYPE TABLE OF bapisditm,  "行项目
         ls_order_items_in       TYPE bapisditm,
         lt_order_items_inx      TYPE TABLE OF bapisditmx, "行项目
         ls_order_items_inx      TYPE bapisditmx,
         lt_order_conditions_in  TYPE TABLE OF bapicond,   "条件价格
         ls_order_conditions_in  TYPE  bapicond,
         lt_order_conditions_inx TYPE TABLE OF bapicondx,  "条件价格
         ls_order_conditions_inx TYPE  bapicondx,
         lt_order_schedules_in   TYPE TABLE OF bapischdl,  "计划行
         ls_order_schedules_in   TYPE bapischdl,
         lt_order_schedules_inx  TYPE TABLE OF bapischdlx, "计划行
         ls_order_schedules_inx  TYPE bapischdlx,
         lt_return               TYPE TABLE OF bapiret2,
         ls_return               TYPE bapiret2.

  "抬头数据赋值
  SELECT SINGLE bwkey FROM t001w
    INTO lv_order_header_in-sales_org             "销售组织
    WHERE werks = p_lv_alv-j_werks .
  lv_order_header_in-doc_type = p_lv_alv-auart .  "销售凭证类型
  lv_order_header_in-distr_chan = p_lv_alv-vtweg ."分销渠道
  lv_order_header_in-division   = p_lv_alv-spart ."产品组
  lv_order_header_in-req_date_h = sy-datum .      "要求的交货日期
  IF p_lv_alv-auart = 'ZRE' OR p_lv_alv-auart = 'ZCR' OR p_lv_alv-auart = 'ZRE1' .
    lv_order_header_in-ord_reason = 'Z01' .      "订单原因
  ENDIF .
  lv_order_header_inx-updateflag = 'I' .
  lv_order_header_inx-doc_type   = 'X' .
  lv_order_header_inx-sales_org  = 'X' .
  lv_order_header_inx-distr_chan = 'X' .
  lv_order_header_inx-division   = 'X' .
  lv_order_header_inx-req_date_h = 'X' .
  IF p_lv_alv-auart = 'ZRE' OR p_lv_alv-auart = 'ZCR' OR p_lv_alv-auart = 'ZRE1' .
    lv_order_header_inx-ord_reason = 'X' .
  ENDIF .

  "业务伙伴数据赋值
  ls_order_partners-partn_role = 'AG' .
  ls_order_partners-partn_numb = p_lv_alv-kunnr .
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = ls_order_partners-partn_numb
    IMPORTING
      output = ls_order_partners-partn_numb.
  APPEND ls_order_partners TO lt_order_partners .

  ls_order_partners-partn_role = 'WE' .
  ls_order_partners-partn_numb = p_lv_alv-kunnr .
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = ls_order_partners-partn_numb
    IMPORTING
      output = ls_order_partners-partn_numb.
  APPEND ls_order_partners TO lt_order_partners .

  LOOP AT gt_alv INTO gs_alv WHERE day_no = p_lv_alv-day_no.

    lv_posnr = lv_posnr + 10 .
*    lv_sched = lv_sched + 1  .
    "行项目数据
    CLEAR : ls_order_items_in  .
    ls_order_items_in-itm_number = lv_posnr .      "行号
    ls_order_items_in-material   = gs_alv-matnr .  "物料
    ls_order_items_in-plant      = gs_alv-j_werks ."工厂
    ls_order_items_in-store_loc  = gs_alv-lgort .  "库存地点
    ls_order_items_in-target_qty = gs_alv-qty .    "数量

    APPEND ls_order_items_in TO lt_order_items_in .
    CLEAR : ls_order_items_inx .
    ls_order_items_inx-itm_number = lv_posnr . "行号
    ls_order_items_inx-material   = 'X' .      "物料
    ls_order_items_inx-plant      = 'X' .      "工厂
    ls_order_items_inx-store_loc  = 'X' .      "库存地点
    ls_order_items_inx-target_qty = 'X' .      "数量
    APPEND ls_order_items_inx TO lt_order_items_inx .

    "计划行数据
    CLEAR : ls_order_schedules_in .
    ls_order_schedules_in-itm_number = lv_posnr .          "行号
    ls_order_schedules_in-sched_line = '1' .               "计划行
    ls_order_schedules_in-req_qty    = gs_alv-qty .        "数量
*    IF    p_lv_alv-j_werks <> p_lv_alv-k_werks
    IF ( p_lv_alv-auart = 'ZRE' OR p_lv_alv-auart = 'ZRE1' ) .
      ls_order_schedules_in-sched_type = 'DN' .              "计划行类别
    ELSEIF ( p_lv_alv-auart = 'ZOR2' OR p_lv_alv-auart = 'ZOR7' ) .
      ls_order_schedules_in-sched_type = 'CP' .              "计划行类别
    ENDIF .
    APPEND ls_order_schedules_in TO lt_order_schedules_in .
    CLEAR : ls_order_schedules_inx .
    ls_order_schedules_inx-itm_number = lv_posnr .
    ls_order_schedules_inx-sched_line = '1' .
    ls_order_schedules_inx-updateflag = 'I' .
    ls_order_schedules_inx-req_qty    = 'X' .
    IF    p_lv_alv-j_werks <> p_lv_alv-k_werks
    AND ( p_lv_alv-auart = 'ZRE' OR p_lv_alv-auart = 'ZRE1' ) .
    ELSE .
      ls_order_schedules_inx-sched_type = 'X' .
    ENDIF .
    APPEND ls_order_schedules_inx TO lt_order_schedules_inx .

    "价格条件
    CLEAR : ls_order_conditions_in .
    ls_order_conditions_in-itm_number = lv_posnr .          "行号
    ls_order_conditions_in-cond_st_no = '10' .              "步骤编号
    ls_order_conditions_in-cond_count = '1' .               "条件计数器
    ls_order_conditions_in-cond_type  = 'ZA05' .            "条件类型
    ls_order_conditions_in-cond_value = gs_alv-js_amount .  "金额
    ls_order_conditions_in-cond_p_unt = '1' .               "条件定价单位
    ls_order_conditions_in-currency   = 'CNY' .
    APPEND ls_order_conditions_in TO lt_order_conditions_in .
    CLEAR : ls_order_conditions_inx .
    ls_order_conditions_inx-itm_number = lv_posnr .
    ls_order_conditions_inx-updateflag = 'I' .
    ls_order_conditions_inx-cond_st_no = '10' .
    ls_order_conditions_inx-cond_count = '1' .
    ls_order_conditions_inx-cond_type  = 'ZA05' .
    ls_order_conditions_inx-cond_value = 'X' .
    ls_order_conditions_inx-cond_p_unt = 'X' .
    ls_order_conditions_inx-currency   = 'X' .
    APPEND ls_order_conditions_inx TO lt_order_conditions_inx .
    CLEAR : gs_alv .

  ENDLOOP .

  IF ( p_lv_alv-auart = 'ZRE' OR p_lv_alv-auart = 'ZRE1' ) .

    CALL FUNCTION 'BAPI_CUSTOMERRETURN_CREATE'
      EXPORTING
        return_header_in     = lv_order_header_in
        return_header_inx    = lv_order_header_inx
      IMPORTING
        salesdocument        = gs_alv-vbeln
      TABLES
        return               = lt_return
        return_items_in      = lt_order_items_in
        return_items_inx     = lt_order_items_inx
        return_partners      = lt_order_partners
        return_schedules_in  = lt_order_schedules_in
        return_schedules_inx = lt_order_schedules_inx
        return_conditions_in = lt_order_conditions_in.

  ELSEIF  p_lv_alv-auart = 'ZCR' OR p_lv_alv-auart = 'ZDR'  .

    CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
      EXPORTING
        sales_header_in     = lv_order_header_in
        sales_header_inx    = lv_order_header_inx
      IMPORTING
        salesdocument_ex    = gs_alv-vbeln
      TABLES
        return              = lt_return
        sales_items_in      = lt_order_items_in
        sales_items_inx     = lt_order_items_inx
        sales_partners      = lt_order_partners
        sales_schedules_in  = lt_order_schedules_in
        sales_schedules_inx = lt_order_schedules_inx.

  ELSE .

    CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
      EXPORTING
        order_header_in      = lv_order_header_in
        order_header_inx     = lv_order_header_inx
      IMPORTING
        salesdocument        = gs_alv-vbeln
      TABLES
        return               = lt_return
        order_items_in       = lt_order_items_in
        order_items_inx      = lt_order_items_inx
        order_partners       = lt_order_partners
        order_schedules_in   = lt_order_schedules_in
        order_schedules_inx  = lt_order_schedules_inx
        order_conditions_in  = lt_order_conditions_in
        order_conditions_inx = lt_order_conditions_inx.

  ENDIF .

  LOOP AT lt_return INTO ls_return WHERE type CA 'EA' .
    gs_alv-message = gs_alv-message && ls_return-message && '/'.
    CLEAR : ls_return .
  ENDLOOP .

  IF sy-subrc = 0 .
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE .

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
  ENDIF .

2、销售订单BAPI扩展字段写入

2.1、相关结构扩展

抬头   :VBAKKOZ、VBAKKOZX、BAPE_VBAK、BAPE_VBAKX

行项目:VBAPKOZ、VBAPKOZX、BAPE_VBAP、BAPE_VBAPX

计划行:VBEPKOZ、VBEPKOZX、BAPE_VBEP、BAPE_VBEPX

(以抬头、行项目为例)SAP BAPI创建销售订单&BAPI扩展字段增强_第1张图片

SAP BAPI创建销售订单&BAPI扩展字段增强_第2张图片SAP BAPI创建销售订单&BAPI扩展字段增强_第3张图片

SAP BAPI创建销售订单&BAPI扩展字段增强_第4张图片SAP BAPI创建销售订单&BAPI扩展字段增强_第5张图片

SAP BAPI创建销售订单&BAPI扩展字段增强_第6张图片

SAP BAPI创建销售订单&BAPI扩展字段增强_第7张图片SAP BAPI创建销售订单&BAPI扩展字段增强_第8张图片

2.2、代码展示

2.2.1:订单创建
  lv_order_header_in-sales_org  = gs_head-vkorg .                          "销售组织
  lv_order_header_in-doc_type   = gs_head-auart .                          "销售凭证类型
  lv_order_header_in-distr_chan = gs_head-vtweg .                          "分销渠道
  lv_order_header_in-division   = gs_head-spart .                          "产品组
  lv_order_header_in-sales_off  = gs_head-vkbur .                          "销售办公室
  lv_order_header_in-req_date_h = sy-datum .                               "要求的交货日期
  lv_order_header_in-purch_date = gs_head-bstdk .                          "客户参考时间
  lv_order_header_in-name       = gs_head-bname .                          "订货人名字
  lv_order_header_in-telephone  = gs_head-telf1 .                          "联系人电话
  SELECT SINGLE vbeln FROM vbak INTO lv_order_header_in-purch_no_c         "参考凭证号客户参考
    WHERE bstnk = gs_head-bstnk2  .
  "为“工程销售订单”时,VBAK-BSTNK=BSTNK1,VBAK-VBTYP=G,取VBAK-VBELN,参考销售合同创建工程销售订单
  IF gv_auart = 'Z040' .
    SELECT SINGLE vbeln ,auart FROM vbak INTO @DATA(ls_vbak) WHERE bstnk = @gs_head-bstnk1 AND vbtyp = 'G'.
    IF sy-subrc = 0 .
      lv_order_header_in-refdoctype = ls_vbak-auart .
      lv_order_header_in-ref_doc    = ls_vbak-vbeln .
    ELSE .
      gs_return-ex_msgtype = 'E' .
      gs_return-ex_msginfo = '合同查询失败,无法建单!' .
    ENDIF .
  ENDIF .

  lv_order_header_inx-updateflag = 'I' .
  lv_order_header_inx-doc_type   = 'X' .
  lv_order_header_inx-sales_org  = 'X' .
  lv_order_header_inx-distr_chan = 'X' .
  lv_order_header_inx-division   = 'X' .
  lv_order_header_inx-sales_off  = 'X' .
  lv_order_header_inx-req_date_h = 'X' .
  lv_order_header_inx-purch_date = 'X' .
  lv_order_header_inx-name       = 'X' .
  lv_order_header_inx-telephone  = 'X' .
  lv_order_header_inx-purch_no_c = 'X' .

  "业务伙伴数据赋值
  ls_order_partners-partn_role = 'AG' .
  ls_order_partners-partn_numb = gs_head-kunnr .
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = ls_order_partners-partn_numb
    IMPORTING
      output = ls_order_partners-partn_numb.
  APPEND ls_order_partners TO lt_order_partners .

  ls_order_partners-partn_role = 'WE' .
  ls_order_partners-partn_numb = gs_head-kunnr .
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = ls_order_partners-partn_numb
    IMPORTING
      output = ls_order_partners-partn_numb.
  APPEND ls_order_partners TO lt_order_partners .

  "抬头扩展字段
*--------------------------------------------------------------------*
  CLEAR : ls_extensionin ,ls_bape_vbak ,ls_bape_vbakx .
  IF gs_head-zfws IS NOT INITIAL .
    ls_bape_vbak-zfws      = gs_head-zfws .
    ls_bape_vbakx-zfws     = 'X' .
  ENDIF .
  IF gs_head-zfws IS NOT INITIAL .
    ls_bape_vbak-zshdz     = gs_head-zshdz .
    ls_bape_vbakx-zshdz    = 'X' .
  ENDIF .
  IF gs_head-zwlgs IS NOT INITIAL .
    ls_bape_vbak-zwlgs     = gs_head-zwlgs .
    ls_bape_vbakx-zwlgs    = 'X' .
  ENDIF .
  IF gs_head-zptdh IS NOT INITIAL .
    ls_bape_vbak-zptdh     = gs_head-zptdh .
    ls_bape_vbakx-zptdh    = 'X' .
  ENDIF .
  IF gs_head-zedbdh IS NOT INITIAL .
    ls_bape_vbak-zedbdh    = gs_head-zedbdh .
    ls_bape_vbakx-zedbdh   = 'X' .
  ENDIF .
  IF gs_head-zremarks IS NOT INITIAL .
    ls_bape_vbak-zremarks  = gs_head-zremarks .
    ls_bape_vbakx-zremarks = 'X' .
  ENDIF .
  IF ls_bape_vbak IS NOT INITIAL .
    ls_extensionin-structure  = 'BAPE_VBAK'.   "扩展结构
    lv_valuepart = ls_bape_vbak .
    ls_extensionin-valuepart1 = lv_valuepart(240).
    ls_extensionin-valuepart2 = lv_valuepart+240(240).
    ls_extensionin-valuepart3 = lv_valuepart+480(240).
    ls_extensionin-valuepart4 = lv_valuepart+720(240).
    APPEND ls_extensionin TO lt_extensionin.
    CLEAR ls_extensionin .
    CLEAR lv_valuepart.
    ls_extensionin-structure = 'BAPE_VBAKX'.  "扩展结构
    lv_valuepart = ls_bape_vbakx.
    ls_extensionin-valuepart1 = lv_valuepart(240).
    ls_extensionin-valuepart2 = lv_valuepart+240(240).
    ls_extensionin-valuepart3 = lv_valuepart+480(240).
    ls_extensionin-valuepart4 = lv_valuepart+720(240).
    APPEND ls_extensionin TO lt_extensionin.
    CLEAR ls_extensionin.
    CLEAR lv_valuepart.
  ENDIF .
*--------------------------------------------------------------------*

  LOOP AT gt_item INTO gs_item .
    lv_posnr = lv_posnr + 10 .
    "行项目数据
    CLEAR : ls_order_items_in  .
    ls_order_items_in-itm_number = lv_posnr .                              "行号
    ls_order_items_in-material   = gs_item-matnr .                         "物料
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        input        = ls_order_items_in-material
      IMPORTING
        output       = ls_order_items_in-material
      EXCEPTIONS
        length_error = 1
        OTHERS       = 2.
    ls_order_items_in-plant      = gs_item-werks .                         "工厂
    ls_order_items_in-store_loc  = gs_item-lgort .                         "库存地点
    ls_order_items_in-target_qty = gs_item-kwmeng .                        "数量
    IF ls_order_items_in-target_qty <= 0 .
      ls_order_items_in-target_qty = 0 - ls_order_items_in-target_qty .
    ENDIF .
    ls_order_items_in-po_itm_no  = gs_item-posex .
    APPEND ls_order_items_in TO lt_order_items_in .

    CLEAR : ls_order_items_inx .
    ls_order_items_inx-itm_number = lv_posnr .                             "行号
    ls_order_items_inx-material   = 'X' .                                  "物料
    ls_order_items_inx-plant      = 'X' .                                  "工厂
    ls_order_items_inx-store_loc  = 'X' .                                  "库存地点
    ls_order_items_inx-target_qty = 'X' .                                  "数量
    ls_order_items_inx-po_itm_no  = 'X' .
    APPEND ls_order_items_inx TO lt_order_items_inx .

    "计划行数据
    CLEAR : ls_order_schedules_in .
    ls_order_schedules_in-itm_number = lv_posnr .                          "行号
    ls_order_schedules_in-sched_line = '1' .                               "计划行
    ls_order_schedules_in-req_qty    = gs_item-kwmeng .                    "数量
    IF ls_order_items_in-target_qty <= 0 .
      ls_order_schedules_in-req_qty = 0 - ls_order_schedules_in-req_qty .
    ENDIF .
    APPEND ls_order_schedules_in TO lt_order_schedules_in .

    CLEAR : ls_order_schedules_inx .
    ls_order_schedules_inx-itm_number = lv_posnr .
    ls_order_schedules_inx-sched_line = '1' .
    ls_order_schedules_inx-updateflag = 'I' .
    ls_order_schedules_inx-req_qty    = 'X' .
    APPEND ls_order_schedules_inx TO lt_order_schedules_inx .

    "价格条件
    CLEAR : ls_order_conditions_in .
    ls_order_conditions_in-itm_number = lv_posnr .                         "行号
    ls_order_conditions_in-cond_st_no = '10' .                             "步骤编号
    ls_order_conditions_in-cond_count = '1' .                              "条件计数器
    ls_order_conditions_in-cond_type  = 'Z001' .                           "条件类型
    ls_order_conditions_in-cond_value = gs_item-price1 .                   "金额
    ls_order_conditions_in-cond_p_unt = '1'."gs_item-kwmeng .              "条件定价单位
    ls_order_conditions_in-currency   = 'CNY' .
    APPEND ls_order_conditions_in TO lt_order_conditions_in .

    CLEAR : ls_order_conditions_inx .
    ls_order_conditions_inx-itm_number = lv_posnr .
    ls_order_conditions_inx-updateflag = 'U' .
    ls_order_conditions_inx-cond_st_no = '10' .
    ls_order_conditions_inx-cond_count = '1' .
    ls_order_conditions_inx-cond_type  = 'Z001' .                        "条件类型
    ls_order_conditions_inx-cond_value = 'X' .
    ls_order_conditions_inx-cond_p_unt = 'X' .
    ls_order_conditions_inx-currency   = 'X' .
    APPEND ls_order_conditions_inx TO lt_order_conditions_inx .

    IF gs_item-price2 IS NOT INITIAL .
      ls_order_conditions_in-cond_type = 'Z020' .
      ls_order_conditions_in-cond_value = gs_item-price2 .
      APPEND ls_order_conditions_in TO lt_order_conditions_in .
      ls_order_conditions_inx-cond_type  = 'Z020' .
      ls_order_conditions_inx-updateflag = 'I' .
      APPEND ls_order_conditions_inx TO lt_order_conditions_inx .
    ENDIF .

    IF gs_item-price3 IS NOT INITIAL .
      ls_order_conditions_in-cond_type = 'Z030' .
      ls_order_conditions_in-cond_value = gs_item-price3 .
      APPEND ls_order_conditions_in TO lt_order_conditions_in .
      ls_order_conditions_inx-cond_type  = 'Z030' .
      ls_order_conditions_inx-updateflag = 'I' .
      APPEND ls_order_conditions_inx TO lt_order_conditions_inx .
    ENDIF .

    "行项目扩展字段
*--------------------------------------------------------------------*
    CLEAR : ls_extensionin ,ls_bape_vbap ,ls_bape_vbapx .
    IF gs_item-zxslx IS NOT INITIAL .
      ls_bape_vbap-zxslx      = gs_item-zxslx .
      ls_bape_vbapx-zxslx     = 'X' .
    ENDIF .
    IF gs_item-zjqbm IS NOT INITIAL .
      ls_bape_vbap-zjqbm      = gs_item-zjqbm .
      ls_bape_vbapx-zjqbm     = 'X' .
    ENDIF .
    IF gs_item-zybgmrq IS NOT INITIAL .
      ls_bape_vbap-zybgmrq      = gs_item-zybgmrq .
      ls_bape_vbapx-zybgmrq     = 'X' .
    ENDIF .
    IF gs_item-zazzt IS NOT INITIAL .
      ls_bape_vbap-zazzt      = gs_item-zazzt .
      ls_bape_vbapx-zazzt     = 'X' .
    ENDIF .
    IF gs_item-zazrq IS NOT INITIAL .
      ls_bape_vbap-zazrq      = gs_item-zazrq .
      ls_bape_vbapx-zazrq     = 'X' .
    ENDIF .
    IF gs_item-zybkssj IS NOT INITIAL .
      ls_bape_vbap-zybkssj      = gs_item-zybkssj .
      ls_bape_vbapx-zybkssj     = 'X' .
    ENDIF .
    IF gs_item-zybjssj IS NOT INITIAL .
      ls_bape_vbap-zybjssj      = gs_item-zybjssj .
      ls_bape_vbapx-zybjssj     = 'X' .
    ENDIF .
    IF gs_item-zfwsbm IS NOT INITIAL .
      ls_bape_vbap-zfwsbm      = gs_item-zfwsbm .
      ls_bape_vbapx-zfwsbm     = 'X' .
    ENDIF .
    IF gs_item-zmdxgbm IS NOT INITIAL .
      ls_bape_vbap-zmdxgbm      = gs_item-zmdxgbm .
      ls_bape_vbapx-zmdxgbm     = 'X' .
    ENDIF .
    IF gs_item-zbxgmbs IS NOT INITIAL .
      ls_bape_vbap-zbxgmbs      = gs_item-zbxgmbs .
      ls_bape_vbapx-zbxgmbs     = 'X' .
    ENDIF .
    IF gs_item-zybxsskfs IS NOT INITIAL .
      ls_bape_vbap-zybxsskfs      = gs_item-zybxsskfs .
      ls_bape_vbapx-zybxsskfs     = 'X' .
    ENDIF .
    IF gs_item-zybskzh IS NOT INITIAL .
      ls_bape_vbap-zybskzh      = gs_item-zybskzh .
      ls_bape_vbapx-zybskzh     = 'X' .
    ENDIF .
    IF gs_item-zybskje IS NOT INITIAL .
      ls_bape_vbap-zybskje      = gs_item-zybskje .
      ls_bape_vbapx-zybskje     = 'X' .
    ENDIF .
    IF ls_bape_vbap IS NOT INITIAL .
      ls_bape_vbap-posnr      = lv_posnr .
      ls_bape_vbapx-posnr     = lv_posnr .
      ls_extensionin-structure  = 'BAPE_VBAP'.   "扩展结构
      CALL METHOD cl_abap_container_utilities=>fill_container_c
        EXPORTING
          im_value     = ls_bape_vbap
        IMPORTING
          ex_container = lv_valuepart.
      ls_extensionin-valuepart1 = lv_valuepart(240).
      ls_extensionin-valuepart2 = lv_valuepart+240(240).
      ls_extensionin-valuepart3 = lv_valuepart+480(240).
      ls_extensionin-valuepart4 = lv_valuepart+720(240).
      APPEND ls_extensionin TO lt_extensionin.
      CLEAR ls_extensionin .
      CLEAR lv_valuepart.
      ls_extensionin-structure = 'BAPE_VBAPX'.  "扩展结构
      lv_valuepart = ls_bape_vbapx.
      ls_extensionin-valuepart1 = lv_valuepart(240).
      ls_extensionin-valuepart2 = lv_valuepart+240(240).
      ls_extensionin-valuepart3 = lv_valuepart+480(240).
      ls_extensionin-valuepart4 = lv_valuepart+720(240).
      APPEND ls_extensionin TO lt_extensionin.
      CLEAR ls_extensionin.
      CLEAR lv_valuepart.
    ENDIF .
*--------------------------------------------------------------------*
    CLEAR : gs_item .
  ENDLOOP .

  IF gs_return-ex_msgtype <> 'E' .

    IF ( gv_auart = 'Z310' OR gv_auart = 'Z320' OR gv_auart = 'Z330' OR gv_auart = 'Z340' ) .

      CALL FUNCTION 'BAPI_CUSTOMERRETURN_CREATE'  "退货销售订单创建
        EXPORTING
          return_header_in     = lv_order_header_in
          return_header_inx    = lv_order_header_inx
          logic_switch         = ls_logic
        IMPORTING
          salesdocument        = lv_vbeln
        TABLES
          return               = lt_return
          return_items_in      = lt_order_items_in
          return_items_inx     = lt_order_items_inx
          return_partners      = lt_order_partners
          return_schedules_in  = lt_order_schedules_in
          return_schedules_inx = lt_order_schedules_inx
          return_conditions_in = lt_order_conditions_in
          extensionin          = lt_extensionin.

    ELSEIF ( gv_auart = 'Z050' OR gv_auart = 'Z060' OR gv_auart = 'Z070' OR gv_auart = 'Z080' OR gv_auart = 'Z090' OR gv_auart = 'Z100' )  .

      CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'  "借贷项凭证
        EXPORTING
          sales_header_in      = lv_order_header_in
          sales_header_inx     = lv_order_header_inx
          logic_switch         = ls_logic
        IMPORTING
          salesdocument_ex     = lv_vbeln
        TABLES
          return               = lt_return
          sales_items_in       = lt_order_items_in
          sales_items_inx      = lt_order_items_inx
          sales_partners       = lt_order_partners
          sales_schedules_in   = lt_order_schedules_in
          sales_schedules_inx  = lt_order_schedules_inx
          sales_conditions_in  = lt_order_conditions_in
          sales_conditions_inx = lt_order_conditions_inx
          extensionin          = lt_extensionin.

    ELSEIF ( gv_auart = 'Z010' OR gv_auart = 'Z020' OR gv_auart = 'Z030' OR gv_auart = 'Z040' ) .

      CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'  "一般销售订单
        EXPORTING
          order_header_in      = lv_order_header_in
          order_header_inx     = lv_order_header_inx
          logic_switch         = ls_logic
        IMPORTING
          salesdocument        = lv_vbeln
        TABLES
          return               = lt_return
          order_items_in       = lt_order_items_in
          order_items_inx      = lt_order_items_inx
          order_partners       = lt_order_partners
          order_schedules_in   = lt_order_schedules_in
          order_schedules_inx  = lt_order_schedules_inx
          order_conditions_in  = lt_order_conditions_in
          order_conditions_inx = lt_order_conditions_inx
          extensionin          = lt_extensionin.

    ENDIF .

    LOOP AT lt_return INTO ls_return WHERE type CA 'EA' .
      gs_return-ex_msginfo = gs_return-ex_msginfo && ls_return-message .
    ENDLOOP .
2.2.2:订单修改
  DATA: ls_hdr         TYPE bapisdh1,
        ls_hdrx        TYPE bapisdh1x,
        ls_logic       TYPE bapisdls,

        ls_ret         TYPE bapiret2,
        lt_ret         TYPE TABLE OF bapiret2,
        ls_itm         TYPE bapisditm,
        lt_itm         TYPE TABLE OF bapisditm,
        ls_itmx        TYPE bapisditmx,
        lt_itmx        TYPE TABLE OF bapisditmx,
        ls_sche        TYPE bapischdl,
        lt_sche        TYPE TABLE OF bapischdl,
        ls_schex       TYPE bapischdlx,
        lt_schex       TYPE TABLE OF bapischdlx,
        ls_cond        TYPE bapicond,
        lt_cond        TYPE TABLE OF bapicond,
        ls_condx       TYPE bapicondx,
        lt_condx       TYPE TABLE OF bapicondx,
        ls_extensionin TYPE bapiparex,           "扩展字段
        lt_extensionin TYPE TABLE OF bapiparex,  "扩展字段
        ls_bape_vbak   TYPE bape_vbak,
        ls_bape_vbakx  TYPE bape_vbakx,
        ls_bape_vbap   TYPE bape_vbap,
        ls_bape_vbapx  TYPE bape_vbapx.
  DATA : lv_valuepart(960).                          "扩展用

  ls_logic-pricing = 'G'.
  ls_hdrx-updateflag = 'U' .

  "抬头扩展字段
*--------------------------------------------------------------------*
  CLEAR : ls_extensionin ,ls_bape_vbak ,ls_bape_vbakx .
  IF gs_head-zfws IS NOT INITIAL .
    ls_bape_vbak-zfws      = gs_head-zfws .
    ls_bape_vbakx-zfws     = 'X' .
  ENDIF .
  IF gs_head-zfws IS NOT INITIAL .
    ls_bape_vbak-zshdz     = gs_head-zshdz .
    ls_bape_vbakx-zshdz    = 'X' .
  ENDIF .
  IF gs_head-zwlgs IS NOT INITIAL .
    ls_bape_vbak-zwlgs     = gs_head-zwlgs .
    ls_bape_vbakx-zwlgs    = 'X' .
  ENDIF .
  IF gs_head-zptdh IS NOT INITIAL .
    ls_bape_vbak-zptdh     = gs_head-zptdh .
    ls_bape_vbakx-zptdh    = 'X' .
  ENDIF .
  IF gs_head-zedbdh IS NOT INITIAL .
    ls_bape_vbak-zedbdh    = gs_head-zedbdh .
    ls_bape_vbakx-zedbdh   = 'X' .
  ENDIF .
  IF gs_head-zremarks IS NOT INITIAL .
    ls_bape_vbak-zremarks  = gs_head-zremarks .
    ls_bape_vbakx-zremarks = 'X' .
  ENDIF .
  IF ls_bape_vbak IS NOT INITIAL .
    ls_bape_vbak-vbeln = gs_head-vbeln .
    ls_bape_vbakx-vbeln = gs_head-vbeln .
    ls_extensionin-structure  = 'BAPE_VBAK'.   "扩展结构
    lv_valuepart = ls_bape_vbak .
    ls_extensionin-valuepart1 = lv_valuepart(240).
    ls_extensionin-valuepart2 = lv_valuepart+240(240).
    ls_extensionin-valuepart3 = lv_valuepart+480(240).
    ls_extensionin-valuepart4 = lv_valuepart+720(240).
    APPEND ls_extensionin TO lt_extensionin.
    CLEAR ls_extensionin .
    CLEAR lv_valuepart.
    ls_extensionin-structure = 'BAPE_VBAKX'.  "扩展结构
    lv_valuepart = ls_bape_vbakx.
    ls_extensionin-valuepart1 = lv_valuepart(240).
    ls_extensionin-valuepart2 = lv_valuepart+240(240).
    ls_extensionin-valuepart3 = lv_valuepart+480(240).
    ls_extensionin-valuepart4 = lv_valuepart+720(240).
    APPEND ls_extensionin TO lt_extensionin.
    CLEAR ls_extensionin.
    CLEAR lv_valuepart.
  ENDIF .
*--------------------------------------------------------------------*
  LOOP AT gt_item INTO gs_item .

    CLEAR :ls_itm ,ls_itmx.
    ls_itm-itm_number = gs_item-posnr .
    ls_itmx-itm_number = gs_item-posnr .
    IF gs_item-matnr IS NOT INITIAL .
      ls_itm-material = gs_item-posnr .
      ls_itmx-material = 'X' .
    ENDIF .
    IF gs_item-kwmeng IS NOT INITIAL .
      ls_itm-target_qty = gs_item-kwmeng .
      ls_itmx-target_qty = 'X' .
    ENDIF .
    ls_itmx-updateflag = 'U' .
    APPEND ls_itm TO lt_itm .
    APPEND ls_itmx TO lt_itmx .

    "计划行
    IF gs_item-kwmeng IS NOT INITIAL .
      CLEAR : ls_sche .
      ls_sche-itm_number = gs_item-posnr .                                 "行号
      ls_sche-sched_line = '1' .                                           "计划行
      ls_sche-req_qty    = gs_item-kwmeng .                                "数量
      IF ls_sche-req_qty <= 0 .
        ls_sche-req_qty = 0 - ls_sche-req_qty .
      ENDIF .
      APPEND ls_sche TO lt_sche .

      CLEAR : ls_schex .
      ls_schex-itm_number = gs_item-posnr .
      ls_schex-sched_line = '1' .
      ls_schex-updateflag = 'I' .
      ls_schex-req_qty    = 'X' .
      APPEND ls_schex TO lt_schex .
    ENDIF .
    "价格条件
    IF gs_item-price1 IS NOT INITIAL .
      "价格条件
      CLEAR : ls_cond .
      ls_cond-itm_number = gs_item-posnr .                                 "行号
      ls_cond-cond_st_no = '10' .                                          "步骤编号
      ls_cond-cond_count = '1' .                                           "条件计数器
      ls_cond-cond_type  = 'Z001' .                                        "条件类型
      ls_cond-cond_value = gs_item-price1 .                                "金额
      ls_cond-cond_p_unt = '1'."gs_item-kwmeng .                           "条件定价单位
      ls_cond-currency   = 'CNY' .
      APPEND ls_cond TO lt_cond .

      CLEAR : ls_condx .
      ls_condx-itm_number = gs_item-posnr .
      ls_condx-updateflag = 'U' .
      ls_condx-cond_st_no = '10' .
      ls_condx-cond_count = '1' .
      ls_condx-cond_type  = 'Z001' .                                       "条件类型
      ls_condx-cond_value = 'X' .
      ls_condx-cond_p_unt = 'X' .
      ls_condx-currency   = 'X' .
      APPEND ls_condx TO lt_condx .
    ENDIF .

    "行项目扩展字段
*--------------------------------------------------------------------*
    CLEAR : ls_extensionin ,ls_bape_vbap ,ls_bape_vbapx .

    IF gs_item-zxslx IS NOT INITIAL .
      ls_bape_vbap-zxslx        = gs_item-zxslx .
      ls_bape_vbapx-zxslx       = 'X' .
    ENDIF .

    IF gs_item-zjqbm IS NOT INITIAL .
      ls_bape_vbap-zjqbm        = gs_item-zjqbm .
      ls_bape_vbapx-zjqbm       = 'X' .
    ENDIF .

    IF gs_item-zybgmrq IS NOT INITIAL .
      ls_bape_vbap-zybgmrq      = gs_item-zybgmrq .
      ls_bape_vbapx-zybgmrq     = 'X' .
    ENDIF .

    IF gs_item-zazzt IS NOT INITIAL .
      ls_bape_vbap-zazzt        = gs_item-zazzt .
      ls_bape_vbapx-zazzt       = 'X' .
    ENDIF .

    IF gs_item-zazrq IS NOT INITIAL .
      ls_bape_vbap-zazrq        = gs_item-zazrq .
      ls_bape_vbapx-zazrq       = 'X' .
    ENDIF .

    IF gs_item-zybkssj IS NOT INITIAL .
      ls_bape_vbap-zybkssj      = gs_item-zybkssj .
      ls_bape_vbapx-zybkssj     = 'X' .
    ENDIF .

    IF gs_item-zybjssj IS NOT INITIAL .
      ls_bape_vbap-zybjssj      = gs_item-zybjssj .
      ls_bape_vbapx-zybjssj     = 'X' .
    ENDIF .

    IF gs_item-zfwsbm IS NOT INITIAL .
      ls_bape_vbap-zfwsbm       = gs_item-zfwsbm .
      ls_bape_vbapx-zfwsbm      = 'X' .
    ENDIF .

    IF gs_item-zmdxgbm IS NOT INITIAL .
      ls_bape_vbap-zmdxgbm      = gs_item-zmdxgbm .
      ls_bape_vbapx-zmdxgbm     = 'X' .
    ENDIF .

    IF gs_item-zbxgmbs IS NOT INITIAL .
      ls_bape_vbap-zbxgmbs      = gs_item-zbxgmbs .
      ls_bape_vbapx-zbxgmbs     = 'X' .
    ENDIF .

    IF gs_item-zybxsskfs IS NOT INITIAL .
      ls_bape_vbap-zybxsskfs    = gs_item-zybxsskfs .
      ls_bape_vbapx-zybxsskfs   = 'X' .
    ENDIF .

    IF gs_item-zybskzh IS NOT INITIAL .
      ls_bape_vbap-zybskzh      = gs_item-zybskzh .
      ls_bape_vbapx-zybskzh     = 'X' .
    ENDIF .

    IF gs_item-zybskje IS NOT INITIAL .
      ls_bape_vbap-zybskje      = gs_item-zybskje .
      ls_bape_vbapx-zybskje     = 'X' .
    ENDIF .

    IF ls_bape_vbap IS NOT INITIAL  .
      ls_bape_vbap-vbeln      = gs_item-posnr .
      ls_bape_vbapx-vbeln     = gs_item-posnr .
      ls_bape_vbap-posnr      = lv_posnr .
      ls_bape_vbapx-posnr     = lv_posnr .
      ls_extensionin-structure  = 'BAPE_VBAP'.   "扩展结构
      CALL METHOD cl_abap_container_utilities=>fill_container_c
        EXPORTING
          im_value     = ls_bape_vbap
        IMPORTING
          ex_container = ls_extensionin-valuepart1.
      APPEND ls_extensionin TO lt_extensionin.
      CLEAR ls_extensionin .
      CLEAR lv_valuepart.
      ls_extensionin-structure = 'BAPE_VBAPX'.  "扩展结构
      lv_valuepart = ls_bape_vbapx.
      ls_extensionin-valuepart1 = lv_valuepart(240).
      ls_extensionin-valuepart2 = lv_valuepart+240(240).
      ls_extensionin-valuepart3 = lv_valuepart+480(240).
      ls_extensionin-valuepart4 = lv_valuepart+720(240).
      APPEND ls_extensionin TO lt_extensionin.
      CLEAR ls_extensionin.
      CLEAR lv_valuepart.
    ENDIF .
*--------------------------------------------------------------------*
    CLEAR : gs_item .
  ENDLOOP .


  CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
    EXPORTING
      salesdocument    = gs_head-vbeln
      order_header_in  = ls_hdr
      order_header_inx = ls_hdrx
      logic_switch     = ls_logic
    TABLES
      return           = lt_ret
      order_item_in    = lt_itm
      order_item_inx   = lt_itmx
      schedule_lines   = lt_sche
      schedule_linesx  = lt_schex
      conditions_in    = lt_cond
      conditions_inx   = lt_condx
      extensionin      = lt_extensionin.

  LOOP AT lt_ret INTO ls_ret WHERE type CA 'EA' .
    gs_return-ex_msginfo = gs_return-ex_msginfo && ls_ret-message .
  ENDLOOP .

你可能感兴趣的:(经验分享,笔记,ABAP,SAP)