前几天财务在给成本发布成本价格时,有很多物料都提示没维护成本视图,取不到标准价格(成品和半成品是标准价格,原材料是移动平均价格)
可以在表MARC-PSTAT 这个字段没包含有“B” 的就表示没扩充成本视图,
因为物料比较多,一个一个的扩充不现实,就做了这个BDC 。
*&---------------------------------------------------------------------*
*& Report ZBDCMM01
*&
*&-------------------------------------------------------------------*--* EXCEL数据格式:物料编码 / 行业领域 / 物料类型 / 工厂 / 基本单位 /
* 评估类 / 价格控制 / 价格单位 / 移动平均价或标准价格 / 用QS的成本估算 /
* 物料来源 / 差异码 / 利润中心
*& 批量扩充物料主数据的会计和成本视图
*&-------------------------------------------------------------------
REPORT ZBDCMM01.
TABLES: t100.
TABLES: mara,marc,mvke,twewt,mbew.
* Batchinputdata of single transaction
DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE.
* messages of call transaction
DATA: messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
DATA: messtabmsg LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
* error session opened (' ' or 'X')
DATA: e_group_opened.
DATA: smalllog TYPE checkbox.
DATA: e_keep TYPE checkbox.
DATA: interrcnt TYPE i.
DATA: intokcnt TYPE i.
DATA: intmdferrcnt TYPE i.
DATA: intmdfokcnt TYPE i.
DATA: intokcreate TYPE i.
DATA: intexistssovw TYPE i.
DATA: intextwg TYPE i.
DATA:BEGIN OF struitem,
matnr LIKE mara-matnr,
pstat LIKE mara-pstat,
END OF struitem.
DATA infoitem LIKE struitem .
DATA: intpdm TYPE i.
DATA:BEGIN OF itemmsg,
matnr LIKE rmmg1-matnr,
remark(100) TYPE c,
END OF itemmsg.
DATA itemerrs LIKE itemmsg OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF record OCCURS 0, "暂存上载的数据的内表
matnr LIKE rmmg1-matnr, " 物料编码
mbrsh LIKE rmmg1-mbrsh, " 行业领域
mtart LIKE rmmg1-mtart, " 物料类型
werks LIKE rmmg1-werks, " 工厂
* lgort LIKE rmmg1-lgort, " 库存地点
* vkorg LIKE rmmg1-vkorg, " 销售组织
* vtweg LIKE rmmg1-vtweg, " 分销渠道
* maktx LIKE makt-maktx, " 物料描述
meins LIKE mara-meins, " 基本单位
* matkl LIKE mara-matkl, " 物料组
* extwg LIKE mara-extwg, " 外部物料组
* spart LIKE mara-spart, " 产品组
* brgew(17) TYPE c, "like MARA-BRGEW 毛重
* gewei LIKE mara-gewei, " 重量单位
* ntgew(17) TYPE c, "LIKE MARA-NTGEW 净重
* ferth LIKE mara-ferth, " 品牌
* normt LIKE mara-normt, " 市场
* taxkm LIKE mg03steuer-taxkm, " 销项税-税分类
* ktgrm LIKE mvke-ktgrm, " 科目设置组
* mtpos_mara LIKE mara-mtpos_mara, " 普通项目类别组
* mtpos LIKE mvke-mtpos, " 项目类别组
* mtvfp_po LIKE marc-mtvfp, " 可用性检查
* tragr LIKE mara-tragr, " 运输组
* ladgr LIKE marc-ladgr, " 装载组
* ekgrp LIKE marc-ekgrp, " 采购组
* webaz(3) TYPE c, "LIKE MARC-WEBAZ 收货处理时间
* kordb LIKE marc-kordb, " 源清单
* disgr LIKE marc-disgr, " MRP组
* maabc LIKE marc-maabc, " ABC 标识
* dismm LIKE marc-dismm, " MRP 类型
* dispo LIKE marc-dispo, " MRP 控制者
* disls LIKE marc-disls, " 批量大小
* bstrf(17) TYPE c, "LIKE MARC-BSTRF 舍入值
* beskz LIKE marc-beskz, " 获取类型
* lgpro LIKE marc-lgpro, " 生产仓储地点
* sobsl LIKE marc-sobsl, " 特殊获取类
* usequ LIKE marc-usequ, " 配额安排
* rgekz LIKE marc-rgekz, " 反冲
* lgfsb LIKE marc-lgfsb, " 外部采购仓储地点
* kzkup LIKE marc-kzkup, " 联产品
* schgt LIKE marc-schgt, " 散装料
* dzeit(3) TYPE c, "LIKE MARC-DZEIT 自制生产
* plifz(3) TYPE c, "LIKE MARC-PLIFZ 计划交货时间
* mrppp LIKE marc-mrppp, " 计划边际码
* fhori LIKE marc-fhori, " 安全库存
* eisbe(17) TYPE c, "LIKE MARC-EISBE 供货天数参数文件
* rwpro LIKE marc-rwpro, " 安全时间标识
* shflg LIKE marc-shflg, " 安全时间/作业覆盖
* shzet(2) TYPE c, "LIKE MARC-SHZET
* mtvfp_wo LIKE marc-mtvfp, " 可用性检查
* altsl LIKE marc-altsl, " 选择方法
* sbdkz LIKE marc-sbdkz, " 独立/集中
* kausf(3) TYPE c , " LIKE MARC-KAUSF , 部件废品
* ahdis LIKE marc-ahdis, " MRP 相关需求
* kzaus LIKE marc-kzaus, " 非连续标识
* ausdt(10) TYPE c, "LIKE MARC-AUSDT 生效期
* nfmat LIKE marc-nfmat, " 后继的物料
* xchpf LIKE mara-xchpf, " 批次管理需求
bklas LIKE mbew-bklas, " 评估类
vprsv LIKE mbew-vprsv, " 价格控制
peinh(6) TYPE c, "LIKE MBEW-PEINH 价格单位
stprs(13) TYPE c, "LIKE MBEW-STPRS 移动平均价/标准价格
ekalr LIKE mbew-ekalr, " 用QS的成本估算
hkmat LIKE mbew-hkmat, " 物料来源
awsls LIKE marc-awsls, " 差异码
prctr LIKE marc-prctr, " 利润中心
END OF record.
*&------------------------------------------------------------------*
*& SELECT-OPTIONS
*&------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME.
****** N > A
PARAMETERS ctumode LIKE ctu_params-dismode DEFAULT 'N'.
* PARAMETERS CTUMODE LIKE CTU_PARAMS-DISMODE DEFAULT 'P'.
PARAMETERS cupdate LIKE ctu_params-updmode DEFAULT 'L' NO-DISPLAY.
PARAMETERS e_group(12) NO-DISPLAY. "group name of error-session
PARAMETERS: e_user(12) DEFAULT sy-uname. "user for error-session
* PARAMETERS: E_KEEP AS CHECKBOX. "' ' = delete session if finished
"'X' = keep session if finished
PARAMETERS: e_hdate LIKE sy-datum NO-DISPLAY.
SELECTION-SCREEN SKIP.
PARAMETERS: nodata DEFAULT ' ' LOWER CASE NO-DISPLAY. "nodata
* PARAMETERS: SMALLLOG AS CHECKBOX DEFAULT ' ' . "' ' = log all transactions
"'X' = no transaction logging
PARAMETERS: filename(132) LOWER CASE DEFAULT
'D:\data.xls'.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN SKIP.
PARAMETERS: filemsgs(132) LOWER CASE DEFAULT
'D:\Msgs.xls'.
PARAMETERS: fileserr(132) LOWER CASE DEFAULT
'D:\MsgsErr.xls'.
SELECTION-SCREEN END OF BLOCK block1.
*&------------------------------------------------------------------*
*& AT SELECTION-SCREEN ON VALUE-REQUEST FOR
*&------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR filename .
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
* DEF_FILENAME = 'c:\test.xls' "default selected file
def_path = 'd:' "Default path
* MASK = ',*.txt,*.txt.'
mask = ',*.xls,*.xls.'
mode = '0'
title = 'Select a file to open'
IMPORTING
filename = filename
* RC =
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR filemsgs .
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
* DEF_FILENAME = 'c:\test.xls' "default selected file
def_path = 'd:' "Default path
* MASK = ',*.txt,*.txt.'
mask = ',*.xls,*.xls.'
mode = '0'
title = 'Select a file to open'
IMPORTING
filename = filemsgs
* RC =
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR fileserr .
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
* DEF_FILENAME = 'c:\test.xls' "default selected file
def_path = 'd:' "Default path
* MASK = ',*.txt,*.txt.'
mask = ',*.xls,*.xls.'
mode = '0'
title = 'Select a file to open'
IMPORTING
filename = fileserr
* RC =
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
*&------------------------------------------------------------------*
START-OF-SELECTION.
*PERFORM SUB_READ_FILE.
interrcnt = 0.
intokcnt = 0.
intmdferrcnt = 0.
intmdfokcnt = 0.
PERFORM sub_read_file.
LOOP AT record.
TRANSLATE record-matnr TO UPPER CASE.
intokcreate = 0.
CLEAR infoitem.
CLEAR:mara,marc,mvke,twewt.
SELECT SINGLE *
FROM mara
WHERE matnr = record-matnr.
IF SY-SUBRC = 0.
record-MTART = mara-MTART.
MODIFY record.
ENDIF.
SELECT SINGLE *
FROM marc
WHERE matnr = record-matnr
AND werks = record-werks.
"intPdm = 0 ----- 新建
"intPdm = 1 ----- 有基本视图、有存储视图 ,状态为 L
"intPdm = 2 ----- 有基本视图、有存储视图、销售视图 , 状态为LV
intpdm = 9."初始为9 ,什么都不做。
intpdm = 1.
IF record-werks <> 'KAS1'.
intpdm = 9.
ENDIF.
IF mara-mtart IS INITIAL.
mara-mtart = record-mtart.
ENDIF.
IF mara-mtart = 'ZROH'.
PERFORM mm01_sta_l_sel.
PERFORM mm01_sta_l_zroh.
ELSEIF mara-mtart = 'ZHLB'.
PERFORM mm01_sta_l_sel.
PERFORM mm01_sta_l_zroh.
* PERFORM mm01_sta_l_zhlb.
ELSEIF mara-mtart = 'ZFRT'.
* intpdm = 3.
PERFORM mm01_sta_l_sel.
PERFORM mm01_sta_l_zroh.
* PERFORM mm01_sta_l_zfrt.
ELSEIF mara-mtart = 'ZNBW'.
ENDIF.
ENDLOOP.
PERFORM close_group.
PERFORM exp2excel.
*&---------------------------------------------------------------------*
*& Form MM01_STA_L_SEL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM mm01_sta_l_sel.
PERFORM bdc_dynpro USING 'SAPLMGMM' '0060'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RMMG1-MATNR'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
PERFORM bdc_field USING 'RMMG1-MATNR'
record-matnr.
PERFORM bdc_field USING 'RMMG1-MBRSH'
record-mbrsh.
PERFORM bdc_field USING 'RMMG1-MTART'
record-mtart.
PERFORM bdc_dynpro USING 'SAPLMGMM' '0070'.
PERFORM bdc_field USING 'BDC_CURSOR'
'MSICHTAUSW-DYTXT(03)'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=P+'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
PERFORM bdc_field USING 'MSICHTAUSW-KZSEL(08)' "-- 选择采购视图
'X'.
PERFORM bdc_dynpro USING 'SAPLMGMM' '0080'.
* PERFORM bdc_field USING 'BDC_CURSOR'
* 'RMMG1-VTWEG'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
PERFORM bdc_field USING 'RMMG1-WERKS'
record-werks.
ENDFORM. "MM01_STA_L_SEL
*&---------------------------------------------------------------------*
*& Form MM01_STA_L_ZROH
*&---------------------------------------------------------------------*
* text 导入原材料
*----------------------------------------------------------------------*
FORM mm01_sta_l_zroh.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SP24'.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SP25'.
PERFORM bdc_field USING 'BDC_CURSOR'
'MBEW-STPRS'.
PERFORM bdc_field USING 'MBEW-BKLAS'
record-bklas.
PERFORM bdc_field USING 'MBEW-VPRSV'
record-vprsv.
PERFORM bdc_field USING 'MBEW-PEINH'
record-peinh.
IF record-vprsv = 'S'.
PERFORM bdc_field USING 'MBEW-STPRS'
record-stprs.
ELSEIF record-vprsv = 'V'.
PERFORM bdc_field USING 'MBEW-VERPR'
record-stprs.
PERFORM bdc_field USING 'MBEW-STPRS'
record-stprs.
ENDIF.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SP26'.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SP27'.
PERFORM bdc_field USING 'BDC_CURSOR'
'MARC-PRCTR'.
PERFORM bdc_field USING 'MBEW-EKALR'
record-ekalr.
PERFORM bdc_field USING 'MBEW-HKMAT'
record-hkmat.
PERFORM bdc_field USING 'MARC-PRCTR'
record-prctr.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=BU'.
PERFORM bdc_transaction USING 'MM01'.
ENDFORM. "MM01_STA_L_ZROH
*&---------------------------------------------------------------------*
*& Form SUB_READ_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM sub_read_file.
DATA: filename1 LIKE rlgrap-filename.
filename1 = filename.
TYPE-POOLS:truxs.
DATA:l_raw_data TYPE truxs_t_text_data.
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
* I_FIELD_SEPERATOR =
* I_LINE_HEADER =
i_tab_raw_data = l_raw_data
i_filename = filename1
TABLES
i_tab_converted_data = record
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE e398(00) WITH '数据在导入内表时出错!'.
ENDIF.
ENDFORM. " SUB_READ_FILE
*&---------------------------------------------------------------------*
*& Form Exp2Excel
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM exp2excel.
DATA: filemsgs1 LIKE rlgrap-filename.
filemsgs1 = filemsgs.
DATA: filemsgs2 LIKE rlgrap-filename.
filemsgs2 = fileserr.
WRITE: / '创建完成笔数:' , intokcnt.
WRITE: / '创建失败笔数:' , interrcnt.
WRITE: / '更改完成笔数:' , intmdfokcnt.
WRITE: / '更改失败笔数:' , intmdferrcnt.
CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
EXPORTING
* I_FIELD_SEPERATOR =
* I_LINE_HEADER =
i_filename = filemsgs1
* I_APPL_KEEP = ' '
TABLES
i_tab_sap_data = itemerrs.
* CHANGING
* I_TAB_CONVERTED_DATA =
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE e398(00) WITH '数据在导出Excel时出错!'.
ENDIF.
CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
EXPORTING
* I_FIELD_SEPERATOR =
* I_LINE_HEADER =
i_filename = filemsgs2
* I_APPL_KEEP = ' '
TABLES
i_tab_sap_data = messtabmsg.
* CHANGING
* I_TAB_CONVERTED_DATA =
* EXCEPTIONS
* CONVERSION_FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE e398(00) WITH '数据在导出Excel时出错!'.
ENDIF.
ENDFORM. " SUB_READ_FILE
*&---------------------------------------------------------------------*
*& Form BDC_TRANSACTION
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Start new transaction according to parameters *
*----------------------------------------------------------------------*
FORM bdc_transaction USING tcode.
DATA: l_mstring(480).
DATA: l_subrc LIKE sy-subrc.
* call transaction using
REFRESH messtab.
CALL TRANSACTION tcode USING bdcdata
MODE ctumode
UPDATE cupdate
MESSAGES INTO messtab.
**CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
** EXPORTING
*** I_FIELD_SEPERATOR =
*** I_LINE_HEADER =
** I_FILENAME = 'c:\ygytest.xls'
*** I_APPL_KEEP = ' '
** TABLES
** I_TAB_SAP_DATA = MESSTAB
*** CHANGING
*** I_TAB_CONVERTED_DATA =
*** EXCEPTIONS
*** CONVERSION_FAILED = 1
*** OTHERS = 2
.
"itemerrs-matnr = record-matnr.
IF sy-subrc <> 0.
* Implement suitable error handling here
"REFRESH ItemErrs.
IF tcode = 'MM01'.
itemerrs-remark = '创建失败'.
interrcnt = interrcnt + 1.
ELSEIF tcode = 'MM02'.
itemerrs-remark = '更改失败'.
intmdferrcnt = intmdferrcnt + 1.
ENDIF.
* WRITE: / 'E:' , RECORD-MATNR , '创建失败'.
APPEND LINES OF messtab TO messtabmsg.
ELSE.
"WRITE: / 'S:' , RECORD-MATNR.
IF tcode = 'MM01'.
itemerrs-remark = '创建完成'.
intokcnt = intokcnt + 1.
intokcreate = 1.
ELSEIF tcode = 'MM02'.
itemerrs-remark = '更改完成'.
intmdfokcnt = intmdfokcnt + 1.
ENDIF.
ENDIF.
itemerrs-matnr = record-matnr.
APPEND itemerrs.
l_subrc = sy-subrc.
** Erzeugen fehlermappe ************************************************
IF l_subrc <> 0 AND e_group <> space.
IF e_group_opened = ' '.
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
client = sy-mandt
group = e_group
user = e_user
keep = e_keep
holddate = e_hdate.
e_group_opened = 'X'.
ENDIF.
CALL FUNCTION 'BDC_INSERT'
EXPORTING
tcode = tcode
TABLES
dynprotab = bdcdata.
ENDIF.
REFRESH bdcdata.
ENDFORM. " BDC_TRANSACTION
*----------------------------------------------------------------------*
* Start new screen *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM. "BDC_DYNPRO
*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
IF fval <> nodata.
CLEAR bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
APPEND bdcdata.
ENDIF.
ENDFORM. "BDC_FIELD
*&---------------------------------------------------------------------*
*& Form CLOSE_GROUP
*&---------------------------------------------------------------------*
FORM close_group.
IF e_group_opened = 'X'.
CALL FUNCTION 'BDC_CLOSE_GROUP'.
WRITE: /.
WRITE: /(30) 'Fehlermappe wurde erzeugt'(i06).
ENDIF.
ENDFORM. " CLOSE_GROUP