创建销售订单
*抬头信息
lw_head-doc_type = p_auart. "类型
lw_head-sales_org = 1000. "销售组织
lw_head-distr_chan = '00'. "分销渠道
lw_head-division = '00'. "产品组
lw_head-date_type = sy-datum. "订单日期
lw_head-req_date_h = sy-datum. "要求的交货日期
lw_head-purch_no_c = lw_alv-zso_no. "客户参考
lw_headx-doc_type = 'X'. "类型
lw_headx-sales_org = 'X'. "销售组织
lw_headx-distr_chan = 'X'. "分销渠道
lw_headx-division = 'X'. "产品组
lw_headx-date_type = 'X'. "订单日期
lw_headx-req_date_h = 'X'. "要求的交货日期
lw_headx-purch_no_c = 'X'. "客户参考
*合作伙伴
SELECT * FROM knvp
INTO CORRESPONDING FIELDS OF TABLE lt_knvp
WHERE kunnr = p_kunnr
AND vkorg = 1000
AND vtweg = '00'
AND spart = '00'.
*备注文本
CLEAR: lw_order_text,lt_order_text.
lw_order_text-doc_number = space.
lw_order_text-text_id = '0001'.
lw_order_text-langu = sy-langu.
lw_order_text-format_col = '*'.
lw_order_text-itm_number = '000000'.
lw_order_text-text_line = p_zmark. "备注
APPEND lw_order_text TO lt_order_text.
lw_item-itm_number = lw_alv-posnr. "销售凭证项目
lw_item-material = lw_alv-zsku. "物料
lw_item-plant = lw_alv-zwerks. "工厂
lw_item-store_loc = lw_alv-zlgort. "库存地点
lw_item-target_qty = lw_alv-menge. "数量
lw_item-sales_unit = lw_alv-meins. " 计量单位
APPEND lw_item TO lt_item.
CLEAR lw_item.
lw_itemx-itm_number = lw_alv-posnr. "销售凭证项目
lw_itemx-material = 'X'. "物料
lw_itemx-plant = 'X'. "工厂
lw_itemx-store_loc = 'X'. "库存地点
lw_itemx-target_qty = 'X'. "数量
lw_itemx-sales_unit = 'X'. " 计量单位
APPEND lw_itemx TO lt_itemx.
CLEAR lw_itemx.
*计划行
l_index = l_index + 1.
lw_schedul-itm_number = lw_alv-posnr.
lw_schedul-sched_line = l_index. "计划行号
lw_schedul-req_date = sy-datum. " 计划行日期
lw_schedul-req_qty = lw_alv-menge. " 以销售单位计的 订单数量
lw_schedul-req_date = sy-datum. "交货日期
APPEND lw_schedul TO lt_schedul.
CLEAR lw_schedul.
lw_schedulx-itm_number = lw_alv-posnr.
lw_schedulx-sched_line = l_index.
lw_schedulx-req_date = 'X'.
lw_schedulx-req_qty = 'X'.
lw_schedulx-req_date = 'X'.
APPEND lw_schedulx TO lt_schedulx.
CLEAR lw_schedulx.
*条件信息
lw_cond-itm_number = lw_alv-posnr. "行项目编码
lw_cond-cond_type = 'ZP01'. "条件类型
lw_cond-cond_value = lw_alv-zsxje. "单价 金额
APPEND lw_cond TO lt_cond.
CLEAR lw_cond.
lw_condx-itm_number = lw_alv-posnr.
lw_condx-cond_type = 'ZP01'.
lw_condx-cond_value = 'X'.
APPEND lw_condx TO lt_condx.
CLEAR lw_condx.
*扩展结构
CLEAR lw_extension.
lw_extension-structure = ''. “这里放结构名
lw_extension+30(960) = ‘’. “参考的内表”
APPEND lw_extension TO lt_extension.
*判断凭证类别
SELECT SINGLE vbtyp INTO @DATA(lv_vbtyp) FROM tvak
WHERE auart = @p_auart.
IF lv_vbtyp = 'H'. "退货凭证
CALL FUNCTION 'BAPI_CUSTOMERRETURN_CREATE'
EXPORTING
return_header_in = lw_head
return_header_inx = lw_headx
IMPORTING
salesdocument = lv_vbeln
TABLES
return = lt_return
return_items_in = lt_item
return_items_inx = lt_itemx
return_schedules_in = lt_schedul
return_schedules_inx = lt_schedulx
return_partners = lt_partner
return_conditions_in = lt_cond
extensionin = lt_extension.
* return_text = lt_order_text. "备注长文本
ELSEIF lv_vbtyp = 'K' OR lv_vbtyp = 'L'. "借贷项凭证
CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
EXPORTING
sales_header_in = lw_head
sales_header_inx = lw_headx
* LOGIC_SWITCH = LS_SWITCH
"testrun = es_return
IMPORTING
salesdocument_ex = lv_vbeln
TABLES
return = lt_return
sales_items_in = lt_item
sales_items_inx = lt_itemx
sales_schedules_in = lt_schedul
sales_schedules_inx = lt_schedulx
sales_partners = lt_partner
sales_conditions_in = lt_cond
sales_conditions_inx = lt_condx
extensionin = lt_extension.
* sales_text = lt_order_text. "备注长文本
ELSE.
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
EXPORTING
order_header_in = lw_head
order_header_inx = lw_headx
* LOGIC_SWITCH = LS_SWITCH
"testrun = es_return
IMPORTING
salesdocument = lv_vbeln
TABLES
return = lt_return
order_items_in = lt_item
order_items_inx = lt_itemx
order_conditions_in = lt_cond
order_conditions_inx = lt_condx
order_schedules_in = lt_schedul
order_schedules_inx = lt_schedulx
order_partners = lt_partner
extensionin = lt_extension
order_text = lt_order_text. "备注长文本
ENDIF.