SAP 批量擴充會計和成本檢視
前幾天財務在給成本釋出成本價格時,有很多物料都提示沒維護成本檢視,取不到標準價格(成品和半成品是標準價格,原材料是移動平均價格)
可以在表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