ABAP BAPI创建SO时 条件类型重复

ABAP BAPI创建SO时 条件类型重复
如下图,第1行PR01是自带多出的行,第2行是bapi创建
ABAP BAPI创建SO时 条件类型重复_第1张图片
解决办法是logic_switch-pricing 传参’G’
is_logic_switch-pricing = ‘G’.
ABAP BAPI创建SO时 条件类型重复_第2张图片

完整代码:
FUNCTION zsdfu005.
*“----------------------------------------------------------------------
"“本地接口:
*” IMPORTING
*” VALUE(ZOAID) TYPE CHAR15 OPTIONAL
*" VALUE(ZNAME) TYPE SY-UNAME OPTIONAL
*" VALUE(AUART) TYPE AUART OPTIONAL
*" VALUE(VKORG) TYPE VKORG OPTIONAL
*" VALUE(BSTNK) TYPE BSTNK OPTIONAL
*" VALUE(BSTDK) TYPE BSTDK OPTIONAL
*" VALUE(TELF1) TYPE TELF1 OPTIONAL
*" VALUE(KUNNR) TYPE KUNNR OPTIONAL
*" VALUE(KUNRG) TYPE KUNRG OPTIONAL
*" VALUE(WAERK) TYPE WAERK OPTIONAL
*" VALUE(ZOANO) TYPE ZOANO OPTIONAL
*" EXPORTING
*" VALUE(ZTYPE) TYPE BAPI_MTYPE
*" VALUE(MESS) TYPE BAPI_MSG
*" VALUE(E_VBELN) TYPE VBELN
*" TABLES
*" ITEM STRUCTURE ZSD005 OPTIONAL
*"----------------------------------------------------------------------
DATA: it_header_in LIKE bapisdhd1.
DATA: it_header_inx LIKE bapisdhd1x.
DATA: it_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
DATA: it_items_in LIKE bapisditm OCCURS 0 WITH HEADER LINE. "通讯字段: 销售和分销凭证项目
DATA: it_items_inx LIKE bapisditmx OCCURS 0 WITH HEADER LINE. "通讯字段: 销售和分销凭证项目
DATA: it_schedules_in LIKE bapischdl OCCURS 0 WITH HEADER LINE. "维护 SD 凭证计划行的通讯字段
DATA: it_schedules_inx LIKE bapischdlx OCCURS 0 WITH HEADER LINE. "维护 SD 凭证计划行的通讯字段
DATA: it_partners LIKE bapiparnr OCCURS 0 WITH HEADER LINE. "通讯字段: SD单据合作伙伴
DATA: order_conditions_in LIKE bapicond OCCURS 0 WITH HEADER LINE.
DATA: order_conditions_inx LIKE bapicondx OCCURS 0 WITH HEADER LINE.
DATA: order_text LIKE bapisdtext OCCURS 0 WITH HEADER LINE.
DATA: lt_bapiparex LIKE bapiparex OCCURS 0 WITH HEADER LINE.
DATA: ls_zbape_vbap TYPE bape_vbap.
DATA: ls_zbape_vbapx TYPE bape_vbapx.
DATA: salesdocument LIKE bapivbeln-vbeln.
DATA: is_logic_switch TYPE bapisdls.

DATA:lv_error.
DATA:lv_mess(200).
DATA:i_kunnr TYPE kna1-kunnr.
DATA:i_kunrg TYPE vbrk-kunrg.
DATA:lv_werks TYPE marc-werks.
DATA:lv_kalks TYPE knvv-kalks.
DATA:it_ztsd008 TYPE TABLE OF ztsd008.
DATA:is_ztsd008 TYPE ztsd008.
DATA:lv_xh TYPE ztsd008-xh.
DATA:lv_xhitem TYPE ztsd008-xhitem.
DATA:BEGIN OF it_posnr OCCURS 0,
posnr LIKE vbap-posnr,
END OF it_posnr.

"1)检验接口数据
IF zoaid = ‘’.
lv_error = ‘X’.
lv_mess = ‘OA单号必填’.
ENDIF.

IF zname = ‘’.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/创建者必填’ INTO lv_mess SEPARATED BY ‘’.
ENDIF.

IF auart = ‘’ OR auart <> ‘ZSE’.
auart = ‘ZSE’.
ENDIF.

IF vkorg = ‘’.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/销售组织必填’ INTO lv_mess SEPARATED BY ‘’.
ELSE.
SELECT COUNT(*) FROM tvko WHERE vkorg = vkorg.
IF sy-subrc NE 0.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/销售组织不存在:’ vkorg INTO lv_mess SEPARATED BY ‘’.
ELSE.
SELECT SINGLE werks INTO lv_werks FROM tvkwz WHERE vkorg = vkorg.
ENDIF.
ENDIF.

IF bstnk = ‘’.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/客户PO号必填’ INTO lv_mess SEPARATED BY ‘’.
ENDIF.

IF kunnr = ‘’.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/售达方必填’ INTO lv_mess SEPARATED BY ‘’.
ELSE.
i_kunnr = |{ kunnr ALPHA = IN }|.

SELECT COUNT(*) FROM but000 WHERE partner = i_kunnr.
IF sy-subrc NE 0.
  lv_error = 'X'.
  CONCATENATE lv_mess '/售达方:' kunnr '不存在' INTO lv_mess SEPARATED BY ''.
ELSE.
  SELECT SINGLE kalks INTO lv_kalks FROM knvv WHERE kunnr = i_kunnr
                                                AND vkorg = vkorg.
  IF sy-subrc NE 0.
    lv_error = 'X'.
    CONCATENATE lv_mess '/售达方:' kunnr '没有扩充到销售组织:' vkorg INTO lv_mess SEPARATED BY ''.
  ENDIF.
ENDIF.

ENDIF.

IF kunrg <> ‘’.
i_kunrg = |{ kunrg ALPHA = IN }|.
SELECT COUNT() FROM but000 WHERE partner = i_kunrg.
IF sy-subrc NE 0.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/付款方:’ kunrg ‘不存在’ INTO lv_mess SEPARATED BY ‘’.
ELSE.
SELECT COUNT(
) FROM knvv WHERE kunnr = i_kunrg
AND vkorg = vkorg.
IF sy-subrc NE 0.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/付款方:’ kunrg ‘没有扩充到销售组织:’ vkorg INTO lv_mess SEPARATED BY ‘’.
ENDIF.
ENDIF.
ENDIF.

IF waerk = ‘’.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/币种必填’ INTO lv_mess SEPARATED BY ‘’.
ELSE.
SELECT COUNT(*) FROM tcurc WHERE waers = waerk.
IF sy-subrc NE 0.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/币种不存在:’ waerk INTO lv_mess SEPARATED BY ‘’.
ENDIF.
ENDIF.

IF item[] IS INITIAL.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/无明细数据’ INTO lv_mess SEPARATED BY ‘’.
ENDIF.

LOOP AT item.
IF item-posnr = ‘’.
lv_error = ‘X’.
CONCATENATE lv_mess ‘/明细行项目号必填’ INTO lv_mess SEPARATED BY ‘’.
EXIT.
ENDIF.

it_posnr-posnr = item-posnr.
APPEND it_posnr.

IF item-matnr = ''.
  lv_error = 'X'.
  CONCATENATE lv_mess '/物料编码必填' INTO lv_mess SEPARATED BY ''.
  EXIT.
ELSE.
  SELECT COUNT(*) FROM mara WHERE matnr = item-matnr.
  IF sy-subrc NE 0.
    lv_error = 'X'.
    CONCATENATE lv_mess '/物料代码不存在' item-matnr INTO lv_mess SEPARATED BY ''.
    EXIT.
  ELSE.
    SELECT COUNT(*) FROM marc WHERE matnr = item-matnr
                                AND werks = lv_werks.
    IF sy-subrc NE 0.
      lv_error = 'X'.
      CONCATENATE lv_mess '/物料:' item-matnr '没有扩充到工厂:' lv_werks INTO lv_mess SEPARATED BY ''.
      EXIT.
    ELSE.
      SELECT COUNT(*) FROM mvke WHERE matnr = item-matnr
                                  AND vkorg = vkorg.
      IF sy-subrc NE 0.
        lv_error = 'X'.
        CONCATENATE lv_mess '/物料:' item-matnr '没有扩充到销售组织:' vkorg INTO lv_mess SEPARATED BY ''.
        EXIT.
      ENDIF.
    ENDIF.
  ENDIF.
ENDIF.

IF item-menge = ''.
  lv_error = 'X'.
  CONCATENATE lv_mess '/行项目:' item-posnr '数量必填' INTO lv_mess SEPARATED BY ''.
  EXIT.
ENDIF.

IF item-netpr = ''.
  lv_error = 'X'.
  CONCATENATE lv_mess '/行项目:' item-posnr '单价必输' INTO lv_mess SEPARATED BY ''.
ENDIF.

ENDLOOP.

SORT it_posnr BY posnr.
DELETE ADJACENT DUPLICATES FROM it_posnr COMPARING posnr.
IF lines( item[] ) <> lines( it_posnr[] ).
lv_error = ‘X’.
CONCATENATE lv_mess ‘/行项目号不能重复’ INTO lv_mess SEPARATED BY ‘’.
ENDIF.

IF lv_error = ‘’.
CLEAR:it_header_in,it_header_inx,salesdocument.
REFRESH:it_return,it_items_in,it_items_inx,it_partners,order_conditions_in,order_conditions_inx,lt_bapiparex.

"抬头赋值
it_header_in-doc_type = auart. "订单类型
it_header_in-sales_org = vkorg. "销售组织
it_header_in-distr_chan = '10'. "分销渠道
it_header_in-division = '10'. "产品组
it_header_in-purch_no_c = bstnk. "采购订单号
IF bstdk IS NOT INITIAL.
  it_header_in-purch_date = bstdk. "客户PO日期
ENDIF.
it_header_in-currency = waerk."币种
it_header_in-telephone = telf1.

"合作伙伴
it_partners-partn_role = 'AG'.
it_partners-partn_numb = i_kunnr.
it_partners-itm_number = space.
APPEND it_partners .

it_partners-partn_role = 'RG'.
IF i_kunrg <> ''.
  it_partners-partn_numb = i_kunrg.
ELSE.
  it_partners-partn_numb = i_kunnr.
ENDIF.
APPEND it_partners.

it_partners-partn_role = 'RE'.
it_partners-partn_numb = i_kunnr.
APPEND it_partners.

it_partners-partn_role = 'WE'.
it_partners-partn_numb = i_kunnr.
APPEND it_partners.

"行项目、价格
LOOP AT item.
  CLEAR:it_items_in.
  it_items_in-itm_number = item-posnr.
  it_items_in-material_long = item-matnr.
  it_items_in-plant = lv_werks.
  it_items_in-target_qty = item-menge.
  SELECT SINGLE meins INTO @DATA(lv_meins) FROM mara WHERE matnr = @item-matnr.
  it_items_in-target_qu = lv_meins.
  APPEND it_items_in.

  "行项目增强字段
  CLEAR:ls_zbape_vbap,ls_zbape_vbapx.
  ls_zbape_vbap = VALUE #(
                          posnr = item-posnr
                          zprono = item-zprono
                          zsource2 = item-zsource2 ).
  ls_zbape_vbapx = VALUE #(
                          posnr = item-posnr
                          zprono = 'X'
                          zsource2 = 'X' ).
  CLEAR:lt_bapiparex.
  lt_bapiparex-structure = 'BAPE_VBAP'.
  lt_bapiparex+30(960) = ls_zbape_vbap.
  APPEND lt_bapiparex.

  CLEAR:lt_bapiparex.
  lt_bapiparex-structure = 'BAPE_VBAPX'.
  lt_bapiparex+30(960) = ls_zbape_vbap.
  APPEND lt_bapiparex.

  "价格
  CLEAR:order_conditions_in.
  order_conditions_in-itm_number = item-posnr.
  CASE lv_kalks.
    WHEN '01'.
      order_conditions_in-cond_type = 'PR01'.
    WHEN '02'.
      order_conditions_in-cond_type = 'PR00'.
  ENDCASE.
  order_conditions_in-cond_value = item-netpr.
  IF item-kpein = ''.
    item-kpein = 1.
  ENDIF.
  order_conditions_in-cond_p_unt = item-kpein.
  order_conditions_in-currency = waerk.
  order_conditions_in-cond_st_no = '010'.
  order_conditions_in-cond_count = '001'.
  order_conditions_in-cond_count_long = '001'.
  APPEND order_conditions_in.

  order_conditions_inx-updateflag = 'U'.
  order_conditions_inx-itm_number = item-posnr.
  order_conditions_inx-cond_type = order_conditions_in-cond_type.
  order_conditions_inx-cond_value = 'X'.
  order_conditions_inx-cond_p_unt = 'X'.
  order_conditions_inx-currency = 'X'.
  order_conditions_inx-cond_st_no = 'X'.
  order_conditions_inx-cond_count = 'X'.
  order_conditions_inx-cond_count_long = 'X'.
  APPEND order_conditions_inx.

  "计划行
  CLEAR it_schedules_in.
  it_schedules_in-itm_number   = item-posnr.
  it_schedules_in-sched_line   = 1.
  it_schedules_in-req_qty      = item-menge.      "数量
  it_schedules_in-req_date     = sy-datum.
  APPEND  it_schedules_in.

ENDLOOP.

is_logic_switch-pricing = 'G'.
CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
  EXPORTING
    sales_header_in      = it_header_in
  •   sales_header_inx     = it_header_inx
      logic_switch         = is_logic_switch
    IMPORTING
      salesdocument_ex     = salesdocument
    TABLES
      return               = it_return
      sales_items_in       = it_items_in
    
  •   sales_items_inx      = it_items_inx
      sales_partners       = it_partners
      sales_schedules_in   = it_schedules_in
    
  •   sales_schedules_inx  = it_schedules_inx
      sales_conditions_in  = order_conditions_in
      sales_conditions_inx = order_conditions_inx
    
  •   sales_text           = order_text
      extensionin          = lt_bapiparex.
    

    IF salesdocument IS INITIAL.
    CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.

    lv_error = 'X'.
    lv_mess = '销售订单创建失败:'.
    LOOP AT it_return WHERE type CA 'AE'.
      lv_mess = lv_mess && '/' && it_return-message.
    ENDLOOP.
    

    ELSE.
    CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
    EXPORTING
    wait = ‘X’.

    lv_mess = '销售订单创建成功'.
    LOOP AT item.
      item-vbeln = salesdocument.
      MODIFY item TRANSPORTING vbeln.
    ENDLOOP.
    

    ENDIF.
    ENDIF.

    IF lv_error = ‘’.
    ztype = ‘S’.
    mess = lv_mess.
    e_vbeln = salesdocument.
    ELSE.
    ztype = ‘E’.
    mess = lv_mess.
    ENDIF.

    "记录日志
    CLEAR:lv_xh,lv_xhitem,it_ztsd008.
    SELECT MAX( xh ) INTO lv_xh FROM ztsd008.
    ADD 1 TO lv_xh.

    IF item IS NOT INITIAL.

    LOOP AT item.
    ADD 1 TO lv_xhitem.
    CLEAR is_ztsd008.
    is_ztsd008-xh = lv_xh.
    is_ztsd008-xhitem = lv_xhitem.
    is_ztsd008-zoaid = zoaid.
    is_ztsd008-zname = zname.
    is_ztsd008-auart = auart.
    is_ztsd008-vkorg = vkorg.
    is_ztsd008-bstnk = bstnk.
    is_ztsd008-bstdk = bstdk.
    is_ztsd008-telf1 = telf1.
    is_ztsd008-kunnr = kunnr.
    is_ztsd008-kunrg = kunrg.
    is_ztsd008-waerk = waerk.
    is_ztsd008-zoano = zoano.
    is_ztsd008-ztype = ztype.
    is_ztsd008-mess = mess.
    is_ztsd008-zdate = sy-datum.
    is_ztsd008-ztime = sy-uzeit.
    is_ztsd008-zuser = sy-uname.
    is_ztsd008-vbeln = item-vbeln.
    is_ztsd008-posnr = item-posnr.
    is_ztsd008-matnr = item-matnr.
    is_ztsd008-menge = item-menge.
    is_ztsd008-netpr = item-netpr.
    is_ztsd008-kpein = item-kpein.
    is_ztsd008-zprono = item-zprono.
    is_ztsd008-zsource2 = item-zsource2.
    APPEND is_ztsd008 TO it_ztsd008.
    ENDLOOP.
    MODIFY ztsd008 FROM TABLE it_ztsd008.

    ELSE.
    is_ztsd008-xh = lv_xh.
    is_ztsd008-xhitem = 0.
    is_ztsd008-zoaid = zoaid.
    is_ztsd008-zname = zname.
    is_ztsd008-auart = auart.
    is_ztsd008-vkorg = vkorg.
    is_ztsd008-bstnk = bstnk.
    is_ztsd008-bstdk = bstdk.
    is_ztsd008-telf1 = telf1.
    is_ztsd008-kunnr = kunnr.
    is_ztsd008-kunrg = kunrg.
    is_ztsd008-waerk = waerk.
    is_ztsd008-zoano = zoano.
    is_ztsd008-ztype = ztype.
    is_ztsd008-mess = mess.
    is_ztsd008-zdate = sy-datum.
    is_ztsd008-ztime = sy-uzeit.
    is_ztsd008-zuser = sy-uname.
    MODIFY ztsd008 FROM is_ztsd008.

    ENDIF.

ENDFUNCTION.

你可能感兴趣的:(SAP-SD,SAP-ABAP,1024程序员节,SAP,ABAP)