matinal:ABAP SD_SALESDOCUMENT_CREATE创建销售订单

2023-10-14 14:26:51 浏览数 (1)

代码语言:javascript复制
DATA: ls_sales_header_in      LIKE bapisdhd1,
        ls_sales_header_inx     LIKE bapisdhd1x,
        lv_salesdocument_ex     LIKE bapivbeln-vbeln,
        ls_logic_switch         TYPE bapisdls,
        lt_sales_items_in       LIKE TABLE OF bapisditm  WITH HEADER LINE,
        lt_sales_items_inx      LIKE TABLE OF bapisditmx WITH HEADER LINE,
        lt_sales_schedules_in   LIKE TABLE OF bapischdl  WITH HEADER LINE,
        lt_sales_schedules_inx  LIKE TABLE OF bapischdlx WITH HEADER LINE,
        lt_sales_conditions_in  LIKE TABLE OF bapicond   WITH HEADER LINE,
        lt_sales_conditions_inx LIKE TABLE OF bapicondx  WITH HEADER LINE,
        lt_sales_text           LIKE TABLE OF bapisdtext WITH HEADER LINE,
        lt_sales_partners_in    LIKE TABLE OF bapiparnr  WITH HEADER LINE,
        lt_return               LIKE TABLE OF bapiret2   WITH HEADER LINE,
        ls_zvbap                TYPE zvbap,
        lt_zvbap                TYPE TABLE OF zvbap,
*        lt_zvbapx               TYPE TABLE OF zvbap,
        lv_msg                  TYPE bapi_msg.

*抬头信息
  IF is_header-auart IS INITIAL.                      "销售凭证类型(默认ZS24)
    ls_sales_header_in-doc_type  = 'ZS24'.
  ELSE.
    ls_sales_header_in-doc_type  = is_header-auart.
  ENDIF.
  IF is_header-vkorg IS INITIAL.                      "销售组织(默认1000)
    ls_sales_header_in-sales_org  = '1000'.
  ELSE.
    ls_sales_header_in-sales_org   = is_header-vkorg.
  ENDIF.
  IF is_header-vtweg IS INITIAL.                      "分销渠道(默认20)
    ls_sales_header_in-distr_chan  = '20'.
  ELSE.
    ls_sales_header_in-distr_chan  = is_header-vtweg.
  ENDIF.
  IF is_header-spart IS INITIAL.                      "产品组(默认00)
    ls_sales_header_in-division  = '00'.
  ELSE.
    ls_sales_header_in-division    = is_header-spart.
  ENDIF.
  ls_sales_header_in-sales_off   = is_header-vkbur.    "销售办公室
  ls_sales_header_in-sales_grp   = is_header-vkgrp.    "销售组
  ls_sales_header_in-purch_no_c  = is_header-bstkd.    "合同号
  ls_sales_header_in-currency    = is_header-waerk.    "货币
  ls_sales_header_in-created_by  = is_header-zernam.   "创建人
  ls_sales_header_in-pmnttrms    = is_header-zterm.    "付款条件

  ls_sales_header_inx-doc_type   = abap_true.
  ls_sales_header_inx-sales_org  = abap_true.
  ls_sales_header_inx-distr_chan = abap_true.
  ls_sales_header_inx-division   = abap_true.
  ls_sales_header_inx-sales_off  = abap_true.
  ls_sales_header_inx-sales_grp  = abap_true.
  ls_sales_header_inx-purch_no_c = abap_true.
  ls_sales_header_inx-currency   = abap_true.
  ls_sales_header_in-created_by  = abap_true.
  ls_sales_header_inx-pmnttrms   = abap_true.

*抬头文本
  CLEAR: lt_sales_text[].
  APPEND INITIAL LINE TO lt_sales_text ASSIGNING FIELD-SYMBOL(<ls_sales_text>).
  <ls_sales_text>-doc_number = is_header-vbeln.
  <ls_sales_text>-itm_number = space.
  <ls_sales_text>-langu      = sy-langu.
  <ls_sales_text>-format_col = '*'.
  <ls_sales_text>-text_line  = is_header-ztext.

  SELECT SINGLE * INTO @DATA(ls_kna1) FROM kna1
    WHERE kunnr EQ @is_header-kunnr.
*客户编码
  lt_sales_partners_in-partn_role = 'WE'.
  lt_sales_partners_in-partn_numb = is_header-kunnr. "送达方
  lt_sales_partners_in-title      = ls_kna1-anred.   "称谓
  lt_sales_partners_in-name       = ls_kna1-name1.   "名称1
  lt_sales_partners_in-country    = ls_kna1-land1.   "国家代码
  lt_sales_partners_in-postl_code = ls_kna1-pstlz.   "邮政编码
  lt_sales_partners_in-city       = ls_kna1-ort01.   "城市
  lt_sales_partners_in-street     = ls_kna1-stras.   "地址

  APPEND lt_sales_partners_in.
  CLEAR lt_sales_partners_in.

  lt_sales_partners_in-partn_role = 'AG'.
  lt_sales_partners_in-partn_numb = is_header-kunnr." 售达方
  APPEND lt_sales_partners_in.
  CLEAR lt_sales_partners_in.

  lt_sales_partners_in-partn_role = 'RE'.
  lt_sales_partners_in-partn_numb = is_header-kunnr." 售达方
  APPEND lt_sales_partners_in.
  CLEAR lt_sales_partners_in.

  lt_sales_partners_in-partn_role = 'RG'.
  lt_sales_partners_in-partn_numb = is_header-kunnr." 售达方
  APPEND lt_sales_partners_in.
  CLEAR lt_sales_partners_in.

*  SELECT * FROM zvbap INTO TABLE lt_zvbap.
*  SORT lt_zvbap BY vbeln posnr.
  SELECT matnr,meins FROM mara INTO TABLE @DATA(lt_mara).
  SORT lt_mara BY matnr.

  LOOP AT it_item INTO DATA(is_item).
    lt_sales_items_in-itm_number = is_item-posnr.      "行项目号
    IF is_item-posnr IS INITIAL.
      es_return-type = 'E'.
      es_return-message = '订单行项目号不能为空!'.
      EXIT.
    ENDIF.
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'        "物料编码
      EXPORTING
        input  = is_item-matnr
      IMPORTING
        output = lt_sales_items_in-material.
    lt_sales_items_in-cust_mat35    = is_item-kdmat.       "客户物料编码
    READ TABLE lt_mara INTO DATA(ls_mara) WITH KEY matnr = is_item-matnr.
    IF sy-subrc = 0.
      lt_sales_items_in-sales_unit    = ls_mara-meins.       "单位
    ENDIF.
    IF is_item-werks IS INITIAL.                           "工厂(默认1010)
      lt_sales_items_in-plant       = '1010'.
    ELSE.
      lt_sales_items_in-plant       = is_item-werks.
    ENDIF.
    lt_sales_items_in-reqmts_typ    = 'KE'.                 "需求类型
    lt_sales_items_in-store_loc     = is_item-lgort.        "库位
    lt_sales_items_in-reason_rej    = is_item-abgru.        "订单关闭

    lt_sales_items_inx-itm_number   = is_item-posnr.
    lt_sales_items_inx-reqmts_typ   = abap_true.
    lt_sales_items_inx-material     = abap_true.
    lt_sales_items_inx-cust_mat35   = abap_true.
    lt_sales_items_inx-sales_unit   = abap_true.
    lt_sales_items_inx-plant        = abap_true.
    lt_sales_items_inx-store_loc    = abap_true.
    lt_sales_items_inx-reason_rej   = abap_true.

    APPEND: lt_sales_items_in, lt_sales_items_inx.
    CLEAR:  lt_sales_items_in, lt_sales_items_inx.

    lt_sales_schedules_in-itm_number = is_item-posnr.
    lt_sales_schedules_in-req_date   = is_item-edatu.     "计划交货时间
    lt_sales_schedules_in-req_qty    = is_item-kwmeng.    "数量

    lt_sales_schedules_inx-itm_number = is_item-posnr.
    lt_sales_schedules_inx-req_date   = abap_true.
    lt_sales_schedules_inx-req_qty    = abap_true.

    APPEND: lt_sales_schedules_in, lt_sales_schedules_inx.
    CLEAR:  lt_sales_schedules_in, lt_sales_schedules_inx.

    ls_logic_switch-pricing = 'G'.

    lt_sales_conditions_in-itm_number  = is_item-posnr.
    lt_sales_conditions_in-cond_type   = 'ZP01'.           "条件类型
    lt_sales_conditions_in-cond_value  = is_item-kbetr.    "单价
    lt_sales_conditions_in-currency    = is_header-waerk.  "货币

    lt_sales_conditions_inx-itm_number = is_item-posnr.
    lt_sales_conditions_inx-cond_type  = 'ZP01'.
    lt_sales_conditions_inx-currency   = abap_true.
    lt_sales_conditions_inx-cond_value = abap_true.

    APPEND: lt_sales_conditions_in,lt_sales_conditions_inx.
    CLEAR:  lt_sales_conditions_in,lt_sales_conditions_inx.

    UNASSIGN <ls_sales_text>.
    APPEND INITIAL LINE TO lt_sales_text ASSIGNING <ls_sales_text>.
    IF <ls_sales_text> IS ASSIGNED.
      <ls_sales_text>-doc_number = is_header-vbeln.
      <ls_sales_text>-itm_number = is_item-posnr.
      <ls_sales_text>-langu      = sy-langu.
      <ls_sales_text>-format_col = '*'.
      <ls_sales_text>-text_line  = is_item-ztext.
    ENDIF.
  ENDLOOP.

  CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
    EXPORTING
      salesdocument        = is_header-vbeln
      sales_header_in      = ls_sales_header_in
      sales_header_inx     = ls_sales_header_inx
      convert_parvw_auart  = abap_on
      logic_switch         = ls_logic_switch
    IMPORTING
      salesdocument_ex     = lv_salesdocument_ex
    TABLES
      return               = lt_return
      sales_items_in       = lt_sales_items_in
      sales_items_inx      = lt_sales_items_inx
      sales_partners       = lt_sales_partners_in
      sales_schedules_in   = lt_sales_schedules_in
      sales_schedules_inx  = lt_sales_schedules_inx
      sales_conditions_in  = lt_sales_conditions_in
      sales_conditions_inx = lt_sales_conditions_inx
      sales_text           = lt_sales_text[].
  LOOP AT lt_return WHERE type CA 'AE'.
    lv_msg = |{ lv_msg } { lt_return-message }|.
  ENDLOOP.
  IF sy-subrc <> 0.
    es_return-type    = 'S'.
    es_return-vbeln   = lv_salesdocument_ex.
    es_return-message = '创建成功!' && es_return-vbeln.
    COMMIT WORK AND WAIT.
  ELSE.
    es_return-type = 'E'.
    es_return-message = lv_msg.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ENDIF.
  "创建销售订单成功后更改ZVBAP字段
  LOOP AT it_item INTO is_item.
*    READ TABLE lt_zvbap INTO ls_zvbap WITH KEY vbeln = es_return-vbeln
*                                               posnr = is_item-posnr
*                                               BINARY SEARCH.
*    IF sy-subrc = 0.
      ls_zvbap-vbeln    = es_return-vbeln.
      ls_zvbap-posnr    = is_item-posnr.
      ls_zvbap-zjzddh   = is_item-zjzddh.
      ls_zvbap-zjzddhh  = is_item-zjzddhh.
      ls_zvbap-zzzkhh   = is_item-zzzkhh.
      APPEND ls_zvbap TO lt_zvbap.
*    ENDIF.
  ENDLOOP.
  MODIFY zvbap FROM TABLE lt_zvbap.
  IF sy-subrc = 0.
    COMMIT WORK AND WAIT.
  ENDIF.
  CLEAR:ls_sales_header_in,ls_sales_header_inx,lt_sales_items_in[],lt_sales_items_inx[],
  lt_sales_schedules_in[],lt_sales_schedules_inx[],lt_sales_conditions_in[],
  lt_sales_conditions_inx,lt_sales_partners_in[],lt_sales_text[].

0 人点赞