FORM FRM_CREATE_SO CHANGING CS_OUTTAB TYPE TY_S_OUTTAB.
DATA: LS_HEADER_IN TYPE BAPISDHD1 ,
LS_HEADER_INX TYPE BAPISDHD1X ,
LS_LOGIC_SWITCH TYPE BAPISDLS ,
LV_VBELN TYPE VBAK-VBELN ,
LT_RETURN TYPE TABLE OF BAPIRET2,
LT_ITEMS_IN TYPE TABLE OF BAPISDITM,
LT_ITEMS_INX TYPE TABLE OF BAPISDITMX,
LT_PARTNERS TYPE TABLE OF BAPIPARNR,
LT_CONDITIONS_IN TYPE TABLE OF BAPICOND,
LT_CONDITIONS_INX TYPE TABLE OF BAPICONDX,
LS_RETURN TYPE BAPIRET2,
LS_ITEMS_IN TYPE BAPISDITM,
LS_ITEMS_INX TYPE BAPISDITMX,
LS_PARTNERS TYPE BAPIPARNR,
LS_CONDITIONS_IN TYPE BAPICOND,
LS_CONDITIONS_INX TYPE BAPICONDX.
* 销售凭证类型
LS_HEADER_IN-DOC_TYPE = CS_OUTTAB-AUART.
LS_HEADER_INX-DOC_TYPE = ABAP_TRUE.
* 销售组织
LS_HEADER_IN-SALES_ORG = CS_OUTTAB-VKORG.
LS_HEADER_INX-SALES_ORG = ABAP_TRUE.
* 分销渠道
LS_HEADER_IN-DISTR_CHAN = CS_OUTTAB-VTWEG.
LS_HEADER_INX-DISTR_CHAN = ABAP_TRUE.
* 产品组
LS_HEADER_IN-DIVISION = CS_OUTTAB-SPART.
LS_HEADER_INX-DIVISION = ABAP_TRUE.
* 销售组
LS_HEADER_IN-SALES_GRP = CS_OUTTAB-VKGRP.
LS_HEADER_INX-SALES_GRP = ABAP_TRUE.
* 销售部门
LS_HEADER_IN-SALES_OFF = CS_OUTTAB-VKBUR.
LS_HEADER_INX-SALES_OFF = ABAP_TRUE.
* 单据日期
LS_HEADER_IN-DOC_DATE = CS_OUTTAB-AUDAT .
LS_HEADER_INX-DOC_DATE = ABAP_TRUE.
* 提供服务日期
LS_HEADER_IN-SERV_DATE = CS_OUTTAB-AUDAT .
LS_HEADER_INX-SERV_DATE = ABAP_TRUE.
* 定价日期
LS_HEADER_IN-PRICE_DATE = SY-DATUM .
LS_HEADER_INX-PRICE_DATE = ABAP_TRUE.
* 付款方式
LS_HEADER_IN-PYMT_METH = CS_OUTTAB-ZLSCH .
LS_HEADER_INX-PYMT_METH = ABAP_TRUE.
* 装运类型
LS_HEADER_IN-SHIP_TYPE = '01' .
LS_HEADER_INX-SHIP_TYPE = ABAP_TRUE.
* 特殊标识
LS_HEADER_IN-S_PROC_IND = CS_OUTTAB-SDABW .
LS_HEADER_INX-S_PROC_IND = ABAP_TRUE.
** 实际发货工厂
* ls_header_in-po_supplem = cs_outtab-werks.
* ls_header_inx-po_supplem = abap_true.
LS_PARTNERS-PARTN_ROLE = 'AG'.
LS_PARTNERS-PARTN_NUMB = CS_OUTTAB-KUNNR.
APPEND LS_PARTNERS TO LT_PARTNERS.
CLEAR LS_PARTNERS.
*加油站内部客户
IF CS_OUTTAB-STAID IS NOT INITIAL.
LS_PARTNERS-PARTN_ROLE = 'ZS'.
LS_PARTNERS-PARTN_NUMB = CS_OUTTAB-STAID.
APPEND LS_PARTNERS TO LT_PARTNERS.
CLEAR LS_PARTNERS.
ENDIF.
*
LS_ITEMS_IN-ITM_NUMBER = '000010'.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
INPUT = CS_OUTTAB-MATNR+2(16)
IMPORTING
OUTPUT = LS_ITEMS_IN-MATERIAL
EXCEPTIONS
LENGTH_ERROR = 1
OTHERS = 2.
LS_ITEMS_IN-PLANT = CS_OUTTAB-WERKS. "客户
APPEND LS_ITEMS_IN TO LT_ITEMS_IN.
*含税总额
LS_CONDITIONS_IN-ITM_NUMBER = '000010'. "itm_number
LS_CONDITIONS_IN-COND_TYPE = 'ZSZ1'. "含税价
LS_CONDITIONS_IN-CONDVALUE = CS_OUTTAB-KZWI1.
LS_CONDITIONS_IN-CURRENCY = 'RMB'. "
LS_CONDITIONS_IN-CURRENCY_2 = 'RMB'.
LS_CONDITIONS_IN-COND_UPDAT = ABAP_TRUE.
APPEND LS_CONDITIONS_IN TO LT_CONDITIONS_IN.
CLEAR LS_CONDITIONS_IN.
*税额
IF CS_OUTTAB-MWSBP <> 0.
LS_CONDITIONS_IN-ITM_NUMBER = '000010'. "itm_number
LS_CONDITIONS_IN-COND_TYPE = 'ZMWI'. "税额
LS_CONDITIONS_IN-CONDVALUE = CS_OUTTAB-MWSBP.
LS_CONDITIONS_IN-CURRENCY_2 = 'RMB'.
LS_CONDITIONS_IN-COND_UPDAT = ABAP_TRUE.
APPEND LS_CONDITIONS_IN TO LT_CONDITIONS_IN.
CLEAR LS_CONDITIONS_IN.
ENDIF.
CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
EXPORTING
SALES_HEADER_IN = LS_HEADER_IN
* SALES_HEADER_INx = ls_header_inx
* logic_switch = ls_logic_switch
IMPORTING
SALESDOCUMENT_EX = LV_VBELN
TABLES
RETURN = LT_RETURN
SALES_ITEMS_IN = LT_ITEMS_IN
SALES_PARTNERS = LT_PARTNERS
SALES_CONDITIONS_IN = LT_CONDITIONS_IN
SALES_CONDITIONS_INX = LT_CONDITIONS_INX.
IF LV_VBELN IS NOT INITIAL.
CS_OUTTAB-VBELN = LV_VBELN. "销售凭证
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LOOP AT LT_RETURN INTO LS_RETURN WHERE TYPE = 'E' OR TYPE = 'A'.
* cs_outtab-message = ls_return-message.
CONCATENATE CS_OUTTAB-MESSAGE LS_RETURN-MESSAGE INTO CS_OUTTAB-MESSAGE.
CLEAR:LS_RETURN.
ENDLOOP.
ENDIF.
ENDFORM. " FRM_CREATE_SO