*&---------------------------------------------------------------------*
*& Report
ZLM_TEST_028
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT
zlm_test_028.
DATA: BEGIN OF gt_data1 OCCURS 0,
bsart
TYPE string, "凭证类型
bnfpo
TYPE string, "项目
*
KNTTP
TYPE STRING, "科目分配类别
matnr
TYPE string,
"商品代码
*
TXZ01
TYPE STRING, "短文本
menge
TYPE string, "数量
meins
TYPE string, "单位
eeind
TYPE string, "交货日期
*
MATKL
TYPE STRING, "物料组
werks
TYPE string, "工厂
ekgrp
TYPE string, "采购组
afnam
TYPE string, "申请者
bednr
TYPE string, "需求跟踪号
sakto
TYPE string, "总帐科目
kostl
TYPE string, "成本中心
anln1
TYPE string, "资产
aufnr
TYPE string, "订单
preis
TYPE string,
"评估价格
waers
TYPE string, "币种
peinh
TYPE string, "价格单位
dispo
TYPE string, "MRP控制者
str1
TYPE string,"行项目文本-传送文本
str2
TYPE string,"行项目文本-预算年度
str3
TYPE string,"行项目文本-资产类别
END OF gt_data1.
DATA: BEGIN OF gt_data OCCURS 0,
bednr
TYPE string, "需求跟踪号
bsart
TYPE string, "凭证类型
bnfpo
TYPE string, "项目
*
KNTTP
TYPE STRING, "科目分配类别
matnr
TYPE string,
"商品代码
*
TXZ01
TYPE STRING, "短文本
menge
TYPE string, "数量
meins
TYPE string, "单位
eeind
TYPE string, "交货日期
*
MATKL
TYPE STRING, "物料组
werks
TYPE string, "工厂
ekgrp
TYPE string, "采购组
afnam
TYPE string, "申请者
sakto
TYPE string, "总帐科目
kostl
TYPE string, "成本中心
anln1
TYPE string, "资产
aufnr
TYPE string, "订单
preis
TYPE string,
"评估价格
waers
TYPE string, "币种
peinh
TYPE string, "价格单位
dispo
TYPE string, "MRP控制者
str1
TYPE string,"行项目文本-传送文本
str2
TYPE string,"行项目文本-预算年度
str3
TYPE string,"行项目文本-资产类别
END OF gt_data.
DATA: BEGIN OF gt_out OCCURS 0,
text(255),
END OF gt_out.
DATA: pr_item LIKE TABLE OF
bapiebanc WITH HEADER LINE,
pr_account LIKE TABLE OF bapiebkn WITH HEADER LINE,
pr_item_id LIKE TABLE OF bapiebantx WITH HEADER LINE,
pr_return LIKE TABLE OF bapireturn WITH HEADER LINE.
DATA: l_return LIKE pr_return.
DATA: lv_message(255).
DATA: pr_no TYPE bapiebanc-preq_no.
DATA: bnfpo TYPE bnfpo.
DATA: matnr TYPE matnr.
DATA: pp_file TYPE string.
PARAMETERS:p_file(128) .
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM get_file.
START-OF-SELECTION.
PERFORM get_data.
PERFORM create_pr.
PERFORM write_out.
*&---------------------------------------------------------------------*
*&
Form
GET_FILE
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->
p1
text
*
<--
p2
text
*----------------------------------------------------------------------*
FORM get_file .
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_filename
= p_file
mask
= ',*.txt,*.TXT.'
*'',*.xls,*.XLS.'如果读入txt文件,在后边就需要用函数
*
CALL FUNCTION 'WS_UPLOAD'
mode
= 'O'
title
= 'File Name'
IMPORTING
filename
=
p_file
EXCEPTIONS
inv_winsys
= 1
no_batch
= 2
selection_cancel = 3
selection_error
= 4
OTHERS
= 5.
pp_file = p_file.
ENDFORM.
" GET_FILE
*&---------------------------------------------------------------------*
*&
Form
GET_DATA
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->
p1
text
*
<--
p2
text
*----------------------------------------------------------------------*
FORM get_data .
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename
= pp_file
filetype
= 'ASC'
has_field_separator
= 'X'
*
HEADER_LENGTH
= 0
read_by_line
= 'X'
*
DAT_MODE
= ' '
*
CODEPAGE
= ' '
*
IGNORE_CERR
= ABAP_TRUE
*
REPLACEMENT
= '#'
*
CHECK_BOM
= ' '
*
VIRUS_SCAN_PROFILE
=
*
NO_AUTH_CHECK
= ' '
* IMPORTING
*
FILELENGTH
=
*
HEADER
=
TABLES
data_tab
= gt_data1
EXCEPTIONS
file_open_error
= 1
file_read_error
= 2
no_batch
= 3
gui_refuse_filetransfer
= 4
invalid_type
= 5
no_authority
= 6
unknown_error
= 7
bad_data_format
= 8
header_not_allowed
= 9
separator_not_allowed
= 10
header_too_long
= 11
unknown_dp_error
= 12
access_denied
= 13
dp_out_of_memory
= 14
disk_full
= 15
dp_timeout
= 16
OTHERS
= 17
.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.MESSAGE 'FILE_OPEN_ERROR ' TYPE 'E'.
WHEN 2.MESSAGE 'FILE_READ_ERROR ' TYPE 'E'.
WHEN 3.MESSAGE 'NO_BATCH' TYPE 'E'.
WHEN 4.MESSAGE 'GUI_REFUSE_FILETRANSFER
' TYPE 'E'.
WHEN 5.MESSAGE 'INVALID_TYPE ' TYPE 'E'.
WHEN 6.MESSAGE ' NO_AUTHORITY' TYPE 'E'.
WHEN 7.MESSAGE 'UNKNOWN_ERROR' TYPE 'E'.
WHEN 8.MESSAGE 'BAD_DATA_FORMAT ' TYPE 'E'.
WHEN 9.MESSAGE 'HEADER_NOT_ALLOWED' TYPE 'E'.
WHEN 10.MESSAGE 'SEPARATOR_NOT_ALLOWED' TYPE 'E'.
WHEN 11.MESSAGE 'HEADER_TOO_LONG ' TYPE 'E'.
WHEN 12.MESSAGE 'UNKNOWN_DP_ERROR' TYPE 'E'.
WHEN 13.MESSAGE ' ACCESS_DENIED ' TYPE 'E'.
WHEN 14.MESSAGE 'DP_OUT_OF_MEMORY ' TYPE 'E'.
WHEN 15.MESSAGE 'DISK_FULL
' TYPE 'E'.
WHEN 16.MESSAGE 'DP_TIMEOUT' TYPE 'E'.
WHEN 17.MESSAGE ' OTHERS
' TYPE 'E'.
WHEN OTHERS.
ENDCASE.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
LOOP AT gt_data1.
MOVE-CORRESPONDING gt_data1 TO gt_data.
APPEND gt_data.
CLEAR: gt_data,gt_data1.
ENDLOOP.
ENDFORM.
" GET_DATA
*&---------------------------------------------------------------------*
*&
Form
CREATE_PR
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->
p1
text
*
<--
p2
text
*----------------------------------------------------------------------*
FORM create_pr .
LOOP AT gt_data.
bnfpo = gt_data-bnfpo.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input
= bnfpo
IMPORTING
output = bnfpo.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input
= gt_data-matnr
IMPORTING
output = matnr.
pr_item-doc_type
= gt_data-bsart.
"凭证类型
pr_item-preq_item
= bnfpo.
"项目
*
PR_ITEM-ACCTASSCAT
= GT_DATA-KNTTP.
"科目分配类别
pr_item-material
= matnr.
"商品代码
*
PR_ITEM-SHORT_TEXT
= GT_DATA-TXZ01.
"短文本
pr_item-quantity
= gt_data-menge.
"数量
pr_item-unit
= gt_data-meins.
"单位
pr_item-deliv_date
= gt_data-eeind.
"交货日期
*
PR_ITEM-MAT_GRP
= GT_DATA-MATKL.
"物料组
pr_item-plant
= gt_data-werks.
"工厂
pr_item-pur_group
= gt_data-ekgrp.
"采购组
pr_item-preq_name
= gt_data-afnam.
"申请者
pr_item-trackingno
= gt_data-bednr.
"需求跟踪号
pr_item-c_amt_bapi
= gt_data-preis.
"评估价格
pr_item-currency
= gt_data-waers.
"货币码
pr_item-price_unit
= gt_data-peinh.
"价格单位
pr_item-mrp_contr
= gt_data-dispo.
"MRP控制者
APPEND pr_item.
CLEAR pr_item.
pr_account-preq_item = bnfpo. "项目
pr_account-g_l_acct = gt_data-sakto.
"总帐科目
pr_account-cost_ctr = gt_data-kostl.
"成本中心
pr_account-asset_no = gt_data-anln1. "资产
pr_account-order_no = gt_data-aufnr. "订单
pr_account-co_area = 'BELL'.
APPEND pr_account.
CLEAR pr_account.
pr_item_id-preq_item = bnfpo."项目
pr_item_id-text_id = 'B03'.
pr_item_id-text_line = gt_data-str1."行项目文本-传送文本
APPEND pr_item_id.
CLEAR pr_item_id.
pr_item_id-preq_item = bnfpo."项目
pr_item_id-text_id = 'B07'.
pr_item_id-text_line = gt_data-str2."行项目文本-预算年度
APPEND pr_item_id.
CLEAR pr_item_id.
pr_item_id-preq_item = bnfpo."项目
pr_item_id-text_id = 'B08'.
pr_item_id-text_line = gt_data-str3."行项目文本-资产类别
APPEND
pr_item_id.
CLEAR
pr_item_id.
AT END OF bednr.
CALL FUNCTION 'BAPI_REQUISITION_CREATE'
* EXPORTING
*
SKIP_ITEMS_WITH_ERROR
=
*
AUTOMATIC_SOURCE
= 'X'
IMPORTING
number
= pr_no
TABLES
requisition_items
= pr_item
requisition_account_assignment
= pr_account
requisition_item_text
= pr_item_id
*
REQUISITION_LIMITS
=
*
REQUISITION_CONTRACT_LIMITS
=
*
REQUISITION_SERVICES
=
*
REQUISITION_SRV_ACCASS_VALUES
=
return
= pr_return
*
REQUISITION_SERVICES_TEXT
=
*
REQUISITION_ADDRDELIVERY
=
*
EXTENSIONIN
=
.
LOOP AT pr_return WHERE type = 'E' OR type = 'A'.
ENDLOOP.
IF sy-subrc = 0.
CLEAR: lv_message.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LOOP AT pr_return INTO l_return WHERE type = 'E' .
CONCATENATE lv_message l_return-message ';'
INTO lv_message.
ENDLOOP.
CONCATENATE gt_data-bednr lv_message INTO gt_out-text.
APPEND gt_out.
CLEAR gt_out.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
CONCATENATE pr_no '创建成功' INTO gt_out-text.
APPEND gt_out.
CLEAR gt_out.
ENDIF.
FREE pr_item.
FREE pr_account.
FREE pr_item_id.
FREE pr_return.
ENDAT.
ENDLOOP.
ENDFORM.
" CREATE_PR
*&---------------------------------------------------------------------*
*&
Form
WRITE_OUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->
p1
text
*
<--
p2
text
*----------------------------------------------------------------------*
FORM write_out .
LOOP AT gt_out.
WRITE: / gt_out-text.
ENDLOOP.
ENDFORM.
" WRITE_OUT
*以下为数据格式(行项目以回车键换行,列以TAB分隔):
*ZB 10 100009 10 EA 20100915 D005
*ZB 10 100008 10 EA 20100915 D005s
另外还有一个BAPI:BAPI_PR_CREATE,大致赋值如下
DATA
:
header
TYPE bapimereqheader
,
headerx
TYPE bapimereqheaderx
,
number
LIKE bapimereqheader
-preq_no
,
return
LIKE bapiret2
OCCURS
0
WITH
HEADER
LINE
,
item
LIKE bapimereqitemimp
OCCURS
0
WITH
HEADER
LINE
,
itemx
LIKE bapimereqitemx
OCCURS
0
WITH
HEADER
LINE
.
header-pr_type = eban-bsart.
headerx-pr_type
= 'X'.
item-item_cat
=
ITEM-PREQ_NAME
=
item-suppl_stloc
=
item-preq_item
=
item-pur_group
=
item-preq_name
=
item-short_text
=
item-material
=
item-plant
=
item-store_loc
=
item-suppl_plnt
=
item-quantity
=
item-deliv_date
=
item-batch
=
ITEM-PREQ_PRICE
=
item-purch_org
=
ITEM-PROCURING_PLANT
=
item-suppl_stloc
=
IF eban-bwtar <> ''.
item-val_type = eban-bwtar.
ENDIF.
APPEND item.
itemx-preq_item
=
itemx-item_cat
= 'X'.
ITEMX-PREQ_NAME
= 'X'.
itemx-suppl_stloc
= 'X'.
itemx-preq_item
= 'X'.
itemx-pur_group
= 'X'.
itemx-preq_name
= 'X'.
itemx-short_text
= 'X'.
itemx-material
= 'X'.
itemx-plant
= 'X'.
itemx-store_loc
= 'X'.
itemx-suppl_plnt
= 'X'.
itemx-quantity
= 'X'.
itemx-deliv_date
= 'X'.
itemx-batch
= 'X'.
itemx-purch_org
= 'X'.
ITEMX-PROCURING_PLANT
= 'X'.
itemx-suppl_stloc
= 'X'.
IF eban-bwtar <> ''.
itemx-val_type
= 'X'.
ENDIF.
APPEND
itemx.
CALL FUNCTION 'BAPI_PR_CREATE'
EXPORTING
prheader
= header
prheaderx = headerx
IMPORTING
number
= number
TABLES
return
= return
pritem
= item
pritemx
= itemx.
COMMIT WORK AND WAIT .