首页 > 代码库 > 批量创建SO
批量创建SO
生成一般销售订单和退货订单所要使用的BAPI不同,
一般销售订单: BAPI_SALESORDER_CREATEFROMDAT2
退货订单: BAPI_CUSTOMERRETURN_CREATE
二者的参数基本一样。
以下为例:
" tables for bapi
DATA: gt_order_header_in LIKE bapisdhd1.
DATA: gt_order_header_inx LIKE bapisdhd1x.
DATA: gt_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
DATA: gt_message LIKE TABLE OF bapiret2 WITH HEADER LINE.
DATA: gt_order_items_in LIKE bapisditm OCCURS 0 WITH HEADER LINE.
DATA: gt_order_items_inx LIKE bapisditmx OCCURS 0 WITH HEADER LINE.
DATA: gv_salesdocument LIKE bapivbeln-vbeln.
DATA: gt_order_partners LIKE bapiparnr OCCURS 0 WITH HEADER LINE.
DATA: gt_schedules_in LIKE bapischdl OCCURS 0 WITH HEADER LINE.
DATA: gt_schedules_inx LIKE bapischdlx OCCURS 0 WITH HEADER LINE.
DATA: gt_order_conditions_in LIKE bapicond OCCURS 0 WITH HEADER LINE.
DATA: gt_order_conditions_inx LIKE bapicondx OCCURS 0 WITH HEADER LINE.
DATA: gt_order_text LIKE bapisdtext OCCURS 0 WITH HEADER LINE.
DATA: gv_number TYPE vbap-posnr.
" internal tables
TYPES: BEGIN OF ty_sales_order,
numbr TYPE i, " 序号
auart TYPE vbak-auart, " 订单类型
vkorg TYPE vbak-vkorg, " 销售组织
vtweg TYPE vbak-vtweg, " 分销渠道
spart TYPE vbak-spart, " 产品组
vkbur TYPE vbak-vkbur, " 销售办公室
vkgrp TYPE vbak-vkgrp, " 销售组
kunag TYPE vbak-kunnr, " 售达方
kunwe TYPE vbak-kunnr, " 送达方
kunre TYPE vbak-kunnr, " 收票方
kunrg TYPE vbak-kunnr, " 付款方
matnr TYPE vbap-matnr, " 物料编码
kwmeng TYPE vbap-kwmeng," 订单数量
zieme TYPE vbap-zieme, " 订单单位
werks TYPE vbap-werks, " 生产工厂
lgort TYPE vbap-lgort, " 仓库
kschl TYPE komv-kschl, " 条件类型
cond_value TYPE bapicond-cond_value, " 金额
bstkd TYPE vbkd-bstkd, "客户采购订单编号
charg TYPE vbap-charg, "批次
route TYPE vbap-route, "路线
END OF ty_sales_order.
TYPES:BEGIN OF ty_result,
numbr TYPE i, " 序号
vbeln TYPE vbak-vbeln,
message TYPE bapiret2-message,
bstkd TYPE vbkd-bstkd, "客户采购订单编号
END OF ty_result.
DATA: gs_result TYPE ty_result.
DATA: gt_result TYPE TABLE OF ty_result.
DATA: gs_sales_order TYPE ty_sales_order.
DATA: gt_sales_order TYPE TABLE OF ty_sales_order.
DATA: gv_error_exit_flag TYPE flag. " whether error exit
DATA: BEGIN OF gt_excel_result OCCURS 0,
text TYPE string,
END OF gt_excel_result.
DATA: gt_fcat TYPE lvc_t_fcat.
FIELD-SYMBOLS:
TYPE lvc_s_fcat.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-000.
PARAMETERS p_file TYPE ibipparms-path OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
PARAMETERS p_th AS CHECKBOX. "是否为退货订单
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file .
CALL FUNCTION ‘WS_FILENAME_GET‘
EXPORTING
def_filename = ‘*.xls‘
def_path = ‘C:\‘
mask = ‘,*.xls.‘
mode = ‘O‘
title = ‘Choose Input file‘(002)
IMPORTING
filename = p_file
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
START-OF-SELECTION.
" upload excel
PERFORM process_logic.
*&---------------------------------------------------------------------*
*& Form UPLOAD_EXCEL
*&---------------------------------------------------------------------*
* upload excel
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM process_logic .
DATA: l_row TYPE i,
l_text TYPE string.
" local parameters
DATA: lt_file TYPE STANDARD TABLE OF alsmex_tabline.
DATA: ls_file TYPE alsmex_tabline.
FIELD-SYMBOLS: TYPE ty_sales_order.
" upload excel with sales order information
CALL FUNCTION ‘ALSM_EXCEL_TO_INTERNAL_TABLE‘
EXPORTING
filename = p_file
i_begin_col = 1
i_begin_row = 2
i_end_col = 37
i_end_row = 60000
TABLES
intern = lt_file
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE text-t01 TYPE ‘E‘.
ELSE.
" translate data
LOOP AT lt_file INTO ls_file.
l_row = ls_file-row + 1.
CASE ls_file-col.
WHEN ‘0001‘. "序号
gs_sales_order-numbr = ls_file-value.
WHEN ‘0002‘. " 订单类型
gs_sales_order-auart = ls_file-value.
WHEN ‘0003‘. " 销售组织
gs_sales_order-vkorg = ls_file-value.
WHEN ‘0004‘. " 分销渠道
gs_sales_order-vtweg = ls_file-value.
WHEN ‘0005‘. " 产品组
gs_sales_order-spart = ls_file-value.
WHEN ‘0006‘. " 销售办公室
gs_sales_order-vkbur = ls_file-value.
WHEN ‘0007‘. " 销售组
gs_sales_order-vkgrp = ls_file-value.
WHEN ‘0008‘. " 售达方
gs_sales_order-kunag = ls_file-value.
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
input = gs_sales_order-kunag
IMPORTING
output = gs_sales_order-kunag.
WHEN ‘0009‘. " 送达方
gs_sales_order-kunwe = ls_file-value.
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
input = gs_sales_order-kunwe
IMPORTING
output = gs_sales_order-kunwe.
WHEN ‘0010‘. " 收票方
gs_sales_order-kunre = ls_file-value.
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
input = gs_sales_order-kunre
IMPORTING
output = gs_sales_order-kunre.
WHEN ‘0011‘. " 付款方
gs_sales_order-kunrg = ls_file-value.
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
input = gs_sales_order-kunrg
IMPORTING
output = gs_sales_order-kunrg.
WHEN ‘0012‘. " 物料编码
gs_sales_order-matnr = ls_file-value.
" conversion
IF gs_sales_order-matnr IS NOT INITIAL.
CALL FUNCTION ‘CONVERSION_EXIT_MATN1_INPUT‘
EXPORTING
input = gs_sales_order-matnr
IMPORTING
output = gs_sales_order-matnr.
ENDIF.
WHEN ‘0013‘. " 订单数量
gs_sales_order-kwmeng = ls_file-value.
WHEN ‘0014‘. " 订单单位
gs_sales_order-zieme = ls_file-value.
CALL FUNCTION ‘CONVERSION_EXIT_CUNIT_INPUT‘
EXPORTING
input = gs_sales_order-zieme
* LANGUAGE = SY-LANGU
IMPORTING
output = gs_sales_order-zieme.
WHEN ‘0015‘. " 工厂
gs_sales_order-werks = ls_file-value.
WHEN ‘0016‘. " 仓库
gs_sales_order-lgort = ls_file-value.
WHEN ‘0017‘." 条件类型
gs_sales_order-kschl = ls_file-value.
WHEN ‘0018‘." 价格
gs_sales_order-cond_value = ls_file-value.
WHEN ‘0019‘."采购订单编号
gs_sales_order-bstkd = ls_file-value.
WHEN ‘0020‘."批次
gs_sales_order-charg = ls_file-value.
WHEN ‘0021‘."路线
gs_sales_order-route = ls_file-value.
WHEN OTHERS.
ENDCASE.
AT END OF row.
IF gs_sales_order-numbr = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“序号”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
IF gs_sales_order-auart = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“订单类型”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
IF gs_sales_order-vkorg = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“销售组织”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
IF gs_sales_order-vtweg = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“分销渠道”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
IF gs_sales_order-spart = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“产品组”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
IF gs_sales_order-vkbur = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“销售办公室”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
IF gs_sales_order-matnr = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“物料编码”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
IF gs_sales_order-kwmeng = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“订单数量”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
IF gs_sales_order-zieme = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“订单单位”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
IF gs_sales_order-werks = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“工厂”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
IF gs_sales_order-lgort = ‘‘.
l_text = l_row.
CONCATENATE ‘第‘ l_text ‘行“库存地”不能为空。‘ INTO l_text.
gt_excel_result-text = l_text.
APPEND gt_excel_result.
ENDIF.
APPEND gs_sales_order TO gt_sales_order.
CLEAR gs_sales_order.
ENDAT.
ENDLOOP.
IF gt_excel_result[] IS NOT INITIAL.
LOOP AT gt_excel_result.
WRITE:/ gt_excel_result-text.
ENDLOOP.
ELSE.
SORT gt_sales_order BY numbr
auart " 订单类型
vkorg " 销售组织
vtweg " 分销渠道
spart " 产品组
vkbur " 销售办公室
vkgrp " 销售组
kunag " 售达方
kunwe " 送达方
kunre " 收票方
kunrg. " 付款方
" build records and calling bapi
LOOP AT gt_sales_order ASSIGNING .
gv_number = gv_number + 10.
" build records
PERFORM build_records_4_bapi USING .
" one order with serval items
AT END OF numbr.
" build header
PERFORM build_header_4_bapi USING .
" call bapi to upload sales order
PERFORM upload_sales_order_with_bapi USING .
CLEAR gv_number.
ENDAT.
ENDLOOP.
" if no error exist , raise report
IF gv_error_exit_flag IS INITIAL AND gt_result IS NOT INITIAL.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT‘
EXPORTING
wait = ‘X‘.
PERFORM frm_show_detail.
ELSEIF gv_error_exit_flag IS NOT INITIAL.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK‘ .
PERFORM frm_pop_showmessage USING ‘E‘.
ENDIF.
ENDIF.
ENDIF.
ENDFORM. " UPLOAD_EXCEL
*&---------------------------------------------------------------------*
*& Form BUILD_RECORDS_4_BAPI
*&---------------------------------------------------------------------*
* build records for calling bapi
*----------------------------------------------------------------------*
* -->P_GS_SALES_ORDER text
*----------------------------------------------------------------------*
FORM build_records_4_bapi USING ps_sales_order TYPE ty_sales_order.
" item data
CLEAR gt_order_items_in.
gt_order_items_in-itm_number = gv_number.
gt_order_items_in-material = ps_sales_order-matnr.
gt_order_items_in-plant = ps_sales_order-werks.
gt_order_items_in-store_loc = ps_sales_order-lgort.
gt_order_items_in-target_qty = ps_sales_order-kwmeng.
gt_order_items_in-target_qu = ps_sales_order-zieme.
gt_order_items_in-sales_unit = ps_sales_order-zieme.
gt_order_items_in-batch = ps_sales_order-charg.
gt_order_items_in-route = ps_sales_order-route.
APPEND gt_order_items_in.
CLEAR gt_order_items_inx.
gt_order_items_inx-updateflag = ‘1‘.
gt_order_items_inx-material = abap_true.
gt_order_items_inx-plant = abap_true.
gt_order_items_inx-store_loc = abap_true.
gt_order_items_inx-target_qty = abap_true.
gt_order_items_inx-target_qu = abap_true.
gt_order_items_inx-sales_unit = abap_true.
gt_order_items_inx-batch = abap_true.
gt_order_items_inx-route = abap_true.
APPEND gt_order_items_inx.
" fill schedules
CLEAR gt_schedules_in.
gt_schedules_in-itm_number = gv_number.
gt_schedules_in-req_qty = ps_sales_order-kwmeng.
APPEND gt_schedules_in.
CLEAR gt_schedules_inx.
gt_schedules_inx-updateflag = ‘X‘.
gt_schedules_inx-itm_number = abap_true.
gt_schedules_inx-req_qty = abap_true.
APPEND gt_schedules_inx.
CLEAR gt_order_conditions_in.
CLEAR gt_order_conditions_inx.
IF ps_sales_order-kschl <> ‘‘.
gt_order_conditions_in-itm_number = gv_number.
gt_order_conditions_in-cond_type = ps_sales_order-kschl. "定价条件
gt_order_conditions_in-cond_value = ps_sales_order-cond_value. "价格
gt_order_conditions_in-currency = ‘CNY‘. "币别
APPEND gt_order_conditions_in.
gt_order_conditions_inx-updateflag = ‘X‘.
gt_order_conditions_inx-cond_type = abap_true. "定价条件
gt_order_conditions_inx-cond_value = abap_true. "价格
gt_order_conditions_inx-currency = abap_true. "币别
APPEND gt_order_conditions_inx.
ENDIF.
ENDFORM. " BUILD_RECORDS_4_BAPI
*&---------------------------------------------------------------------*
*& Form UPLOAD_SALES_ORDER_WITH_BAPI
*&---------------------------------------------------------------------*
* calling bapi
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM upload_sales_order_with_bapi USING ps_sales_order TYPE ty_sales_order .
" clear
CLEAR: gt_return,
gt_return[],
gv_salesdocument.
IF p_th IS INITIAL.
CALL FUNCTION ‘BAPI_SALESORDER_CREATEFROMDAT2‘
EXPORTING
order_header_in = gt_order_header_in
order_header_inx = gt_order_header_inx
IMPORTING
salesdocument = gv_salesdocument
TABLES
return = gt_return
order_items_in = gt_order_items_in
order_items_inx = gt_order_items_inx
order_partners = gt_order_partners
order_schedules_in = gt_schedules_in
order_schedules_inx = gt_schedules_inx
order_conditions_in = gt_order_conditions_in
order_conditions_inx = gt_order_conditions_inx.
ELSE.
CALL FUNCTION ‘BAPI_CUSTOMERRETURN_CREATE‘
EXPORTING
RETURN_HEADER_IN = gt_order_header_in
RETURN_HEADER_INX = gt_order_header_inx
IMPORTING
SALESDOCUMENT = gv_salesdocument
TABLES
return = gt_return
RETURN_ITEMS_IN = gt_order_items_in
RETURN_ITEMS_INX = gt_order_items_inx
RETURN_PARTNERS = gt_order_partners
RETURN_SCHEDULES_IN = gt_schedules_in
RETURN_SCHEDULES_INX = gt_schedules_inx
RETURN_CONDITIONS_IN = gt_order_conditions_in.
ENDIF.
READ TABLE gt_return WITH KEY type = ‘E‘.
IF sy-subrc = 0.
LOOP AT gt_return WHERE type = ‘E‘.
gt_message = gt_return.
gt_message-message_v4 = ps_sales_order-numbr.
APPEND gt_message.
ENDLOOP.
gv_error_exit_flag = abap_true.
ELSE.
IF gv_salesdocument IS NOT INITIAL.
CLEAR gs_result.
gs_result-numbr = ps_sales_order-numbr.
gs_result-vbeln = gv_salesdocument.
gs_result-bstkd = ps_sales_order-bstkd.
CONCATENATE ‘销售订单‘ gs_result-vbeln ‘创建成功‘ INTO gs_result-message.
APPEND gs_result TO gt_result.
ENDIF.
ENDIF.
" refresh
PERFORM refresh.
ENDFORM. " UPLOAD_SALES_ORDER_WITH_BAPI
*&---------------------------------------------------------------------*
*& Form BUILD_HEADER_4_BAPI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GS_SALES_ORDER text
*----------------------------------------------------------------------*
FORM build_header_4_bapi USING ps_sales_order TYPE ty_sales_order.
" build header data
CLEAR gt_order_header_in.
gt_order_header_in-doc_type = ps_sales_order-auart.
gt_order_header_in-sales_org = ps_sales_order-vkorg.
gt_order_header_in-distr_chan = ps_sales_order-vtweg.
gt_order_header_in-division = ps_sales_order-spart.
gt_order_header_in-sales_off = ps_sales_order-vkbur.
gt_order_header_in-sales_grp = ps_sales_order-vkgrp.
IF ps_sales_order-bstkd IS NOT INITIAL.
gt_order_header_in-purch_no_c = ps_sales_order-bstkd.
gt_order_header_inx-purch_no_c = abap_true.
gt_order_header_in-purch_no_s = ps_sales_order-bstkd.
gt_order_header_inx-purch_no_s = abap_true.
ENDIF.
CLEAR gt_order_header_inx.
gt_order_header_inx-doc_type = abap_true.
gt_order_header_inx-sales_org = abap_true.
gt_order_header_inx-distr_chan = abap_true.
gt_order_header_inx-division = abap_true.
gt_order_header_inx-sales_off = abap_true.
gt_order_header_inx-sales_grp = abap_true.
gt_order_header_inx-updateflag = ‘1‘.
IF ps_sales_order-bstkd IS NOT INITIAL.
gt_order_header_inx-purch_no_c = abap_true.
gt_order_header_inx-purch_no_s = abap_true.
ENDIF.
" partner function
CLEAR gt_order_partners.
gt_order_partners-partn_role = ‘AG‘.
gt_order_partners-partn_numb = ps_sales_order-kunag.
APPEND gt_order_partners.
CLEAR gt_order_partners.
gt_order_partners-partn_role = ‘WE‘.
gt_order_partners-partn_numb = ps_sales_order-kunwe.
APPEND gt_order_partners.
CLEAR gt_order_partners.
gt_order_partners-partn_role = ‘RE‘.
gt_order_partners-partn_numb = ps_sales_order-kunre.
APPEND gt_order_partners.
CLEAR gt_order_partners.
gt_order_partners-partn_role = ‘RG‘.
gt_order_partners-partn_numb = ps_sales_order-kunrg.
APPEND gt_order_partners.
ENDFORM. " BUILD_HEADER_4_BAPI
*&---------------------------------------------------------------------*
*& Form REFRESH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM refresh .
CLEAR:
gt_order_header_in,
gt_order_header_inx,
gv_salesdocument,
gt_return,
gt_return[],
gt_order_items_in,
gt_order_items_in[],
gt_order_items_inx,
gt_order_items_inx[],
gt_order_partners,
gt_order_partners[],
gt_schedules_in,
gt_schedules_in[],
gt_schedules_inx,
gt_schedules_inx[],
gt_order_conditions_in,
gt_order_conditions_in[],
gt_order_conditions_inx,
gt_order_conditions_inx[].
ENDFORM. " REFRESH
*&---------------------------------------------------------------------*
*& Form FRM_SHOW_DETAIL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_show_detail .
APPEND INITIAL LINE TO gt_fcat ASSIGNING .
-fieldname = ‘NUMBR‘.
-coltext = ‘序号‘.
APPEND INITIAL LINE TO gt_fcat ASSIGNING .
-fieldname = ‘VBELN‘.
-coltext = ‘订单号‘.
APPEND INITIAL LINE TO gt_fcat ASSIGNING .
-fieldname = ‘MESSAGE‘.
-coltext = ‘消息‘.
APPEND INITIAL LINE TO gt_fcat ASSIGNING .
-fieldname = ‘BSTKD‘.
-coltext = ‘采购订单号‘.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY_LVC‘
EXPORTING
it_fieldcat_lvc = gt_fcat
TABLES
t_outtab = gt_result
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM. " FRM_SHOW_DETAIL
*&---------------------------------------------------------------------*
*& Form FRM_POP_SHOWMESSAGE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0899 text
*----------------------------------------------------------------------*
FORM frm_pop_showmessage USING p_type TYPE bapi_mtype.
CALL FUNCTION ‘MESSAGES_INITIALIZE‘. "20140213 Chung 消息处理
LOOP AT gt_message
WHERE type = p_type.
CONDENSE gt_message-message_v4.
CALL FUNCTION ‘MESSAGE_STORE‘
EXPORTING
arbgb = gt_message-id
exception_if_not_active = ‘ ‘
msgty = gt_message-type
msgv1 = gt_message-message_v1
msgv2 = gt_message-message_v2
msgv3 = gt_message-message_v3
* msgv4 = gt_message-message_v4
txtnr = gt_message-number
zeile = gt_message-message_v4
EXCEPTIONS
message_type_not_valid = 1
not_active = 2
OTHERS = 3.
ENDLOOP.
CALL FUNCTION ‘MESSAGES_SHOW‘
EXCEPTIONS
inconsistent_range = 1
no_messages = 2
OTHERS = 3.
ENDFORM. " FRM_POP_SHOWMESSAGE
批量创建SO