SAP ABAP顾问能力模型梳理_企业数字化建设者的博客-CSDN博客SAP Abap顾问能力模型https://blog.csdn.net/java_zhong1990/article/details/132469977
一、Function ALV
1.1 基于退货采购订单创建,解释 FUNCTION_ALV开发的程序结构与代码模板参考
1.2 程序结构 top|sel|frm|pbo|pai
*&---------------------------------------------------------------------*
*& Report ZMMRP008
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zmmrp008.
*******************************************************
*程序名: zmmrp008
*程序描述:退货采购订单创建
*----------------------------------------------------------------------
*创建日期 ABAP开发顾问 业务顾问
*2019.09.19.
*===============================================
*修改请求号 修改日期 修改人 修改描述
*xxxxxxxx xxxx.xx.xx xxxxxxxx xxxxxxxx
*******************************************************
INCLUDE zrp008_top.
INCLUDE zrp008_sel.
INCLUDE zrp008_frm.
INCLUDE zrp008_pbo.
INCLUDE zrp008_pai.
*------------------------------------------------------------------------*
* INITIALIZATION *
*------------------------------------------------------------------------*
INITIALIZATION."第一次打开屏幕或者点击执行代码已经执行完成重新显示屏幕的时候会运行
*(1).只能用于报表程序
*(2).在选择屏幕出现之前执行.
*(3).通常的用法是在这里给选择屏幕中的字段赋值。
*------------------------------------------------------------------------*
* AT SELECTION-SCREEN *
*------------------------------------------------------------------------*
AT SELECTION-SCREEN."当在选择屏幕上有做操作的时候都会进入到这个事件
* 选择屏幕PAI事件,检查用户数据正确性和全部字段一致性,发生错误所有元素可重新输入
* 在这里我们可以用来检查输入的参数是否有问题
"选择屏幕至少需要填写一个字段
PERFORM check_inputterm.
*------------------------------------------------------------------------*
* START-OF-SELECTION *
*------------------------------------------------------------------------*
START-OF-SELECTION."当触发执行的时候屏幕
* 选择开始事件(默认事件),在选择屏幕的处理结束后触发
PERFORM get_data. "可以创建一个子程序用来做数据处理的
* PERFORM set_catalog. "可以创建一个子程序用来设置显示字段的信息
PERFORM prm_bulid_field.
PERFORM display_alv. "这个子程序可以用来显示ALV
*------------------------------------------------------------------------*
* END-OF-SELECTION *
*------------------------------------------------------------------------*
END-OF-SELECTION."在选择屏处理完逻辑数据之后(会重新调用屏幕所以会触发INITIALIZATION)
* 选择结束事件,在所有逻辑数据库处理结束后触发,一般用于输出列表
*&---------------------------------------------------------------------*
*& Form CHECK_INPUTTERM
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& 包含 ZRP008_TOP
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
* GLOBLE-DEFINATION *
*----------------------------------------------------------------------*
TABLES ztmm_incom_list."来料单号
TABLES ekpo.
TABLES ekko.
TYPES: BEGIN OF gy_data," 定义最终显示的结构
check(2) TYPE c,
bukrs TYPE ekko-bukrs, "公司代码
incom TYPE ztmm_incom_list-incom, "来料单号
item TYPE ztmm_incom_list-item, "来料单项次
bsart TYPE ekko-bsart, "采购订单类型
ebeln TYPE ekko-ebeln, "采购订单号
ebelp TYPE ekpo-ebelp, "采购订单项次
menge TYPE ekpo-menge, "数量
meins TYPE ekpo-meins, "单位
matnr TYPE ekpo-matnr, "物资编码
txz01 TYPE ekpo-txz01, "物料描述
mfrpn TYPE ekpo-mfrpn, "原厂码
subcode TYPE ekpo-subcode, "Subcode
werks TYPE ekpo-werks, "工厂
lgort TYPE ekpo-lgort, "库存地点
ekorg TYPE ekko-ekorg, "采购组织
ekgrp TYPE ekko-ekgrp, "采购组
netpr_co TYPE ztmm_incom_list-netpr_co, "采购价格-含税
netpr TYPE ekpo-netpr, "采购价格-未税
peinh TYPE ekpo-peinh, "价格基数
waers TYPE ekko-waers, "采购货币
mwskz TYPE ekpo-mwskz, "税码
lifnr TYPE ekko-lifnr, "供应商
name1 TYPE lfa1-name1, "采购供应商名称
kbetr TYPE konp-kbetr, "条件金额(借用辅助计算字段)
effwr TYPE ekpo-effwr, "税率 (借用辅助计算字段)
status(2) TYPE c, "退货订单创建状态
message(255) TYPE c, "退货订单创建消息
END OF gy_data.
DATA gs_data TYPE gy_data.
DATA gt_data TYPE TABLE OF gy_data.
DATA gs_data_copy TYPE gy_data.
DATA gt_data_copy TYPE TABLE OF gy_data.
DATA gt_data_success_log TYPE TABLE OF gy_data. "采购退货订单创建成功记录
DATA gs_data_success_log TYPE gy_data.
DATA gt_data_temp TYPE TABLE OF gy_data. "临时存放
DATA gs_data_temp TYPE gy_data.
DATA: gv_ok_code LIKE sy-ucomm.
DATA: gt_fieldcat_lvc TYPE lvc_t_fcat, "ALV:定义一个先显示字段的表
gs_fieldcat_lvc LIKE LINE OF gt_fieldcat_lvc. "ALV:定义一个先显示字段的结构体
&---------------------------------------------------------------------*
*& 包含 ZRP008_FRM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM get_data.
SELECT
ekko~bukrs "公司代码
ztmm_incom_list~incom "来料单
ztmm_incom_list~item "项次
ztmm_incom_list~ebeln "采购订单号
ztmm_incom_list~ebelp "采购订单项次
ztmm_incom_list~matnr "物资编码
makt~maktx AS txz01
ztmm_incom_list~menge "数量
ztmm_incom_list~meins "订单单位
ztmm_incom_list~netpr_co "采购含税价
ztmm_incom_list~peinh "价格基数
ztmm_incom_list~netpr "采购未税价格
ztmm_incom_list~mwskz "税码
ekko~bsart "取采购凭证类型
ekko~lifnr "供应商
lfa1~name1 "供应商名称
ekko~ekorg "采购组织
ekko~ekgrp "采购组
ekpo~werks "工厂
ekpo~lgort"库存地点
ekpo~subcode"SUBCODE
ekpo~factory_code"厂家码
* ekpo~factory_code
INTO CORRESPONDING FIELDS OF TABLE gt_data
FROM ztmm_incom_list
INNER JOIN ekko ON ztmm_incom_list~ebeln = ekko~ebeln
INNER JOIN ekpo ON ztmm_incom_list~ebeln = ekpo~ebeln AND ztmm_incom_list~ebelp = ekpo~ebelp
INNER JOIN lfa1 ON ekko~lifnr = lfa1~lifnr
LEFT JOIN makt ON makt~matnr = ztmm_incom_list~matnr
WHERE
ztmm_incom_list~incom IN so_incom AND
ztmm_incom_list~item IN so_item AND
ztmm_incom_list~ebeln IN so_ebeln AND
ztmm_incom_list~ebelp IN so_ebelp AND
ztmm_incom_list~matnr IN so_matnr AND
ekko~lifnr IN so_lifnr
.
"根据税码取税率
IF gt_data IS NOT INITIAL.
DATA lt_ftaxp TYPE TABLE OF ftaxp.
DATA ls_ftaxp TYPE ftaxp.
LOOP AT gt_data INTO gs_data.
"税码转换成税率
CALL FUNCTION 'GET_TAX_PERCENTAGE'
EXPORTING
aland = 'CN'
datab = sy-datum
mwskz = gs_data-mwskz
txjcd = 'TAXBRA'
* EXPORT = ' '
TABLES
t_ftaxp = lt_ftaxp.
IF sy-subrc = 0.
READ TABLE lt_ftaxp INTO ls_ftaxp INDEX 1.
gs_data-effwr = ls_ftaxp-kbetr / 1000.
gs_data-kbetr = ls_ftaxp-kbetr.
ENDIF.
"单位转换
CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
EXPORTING
input = gs_data-meins
language = sy-langu
IMPORTING
* LONG_TEXT =
output = gs_data-meins
* SHORT_TEXT =
* EXCEPTIONS
* UNIT_NOT_FOUND = 1
* OTHERS = 2
.
IF sy-subrc = 0.
ENDIF.
MODIFY gt_data FROM gs_data.
ENDLOOP.
ENDIF.
ENDFORM.
FORM check_inputterm .
DATA lv_times TYPE num10.
lv_times = 0.
IF so_incom IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF so_item IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF so_ebeln IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF so_ebelp IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF so_matnr IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF so_lifnr IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF lv_times = 0.
MESSAGE '选择屏幕至少需要填写一个字段!' TYPE 'E'.
ENDIF.
ENDFORM.
*DATA: fieldcat TYPE lvc_t_fcat.
FORM prm_bulid_field .
PERFORM create_fieldcat USING:
gt_fieldcat_lvc 'BUKRS' '公司代码' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'INCOM' '来料单号' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'ITEM' '来料单项次' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'BSART' '采购订单类型' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'EBELN' '采购订单号' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'EBELP' '采购订单项次' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'MATNR' '物资编码' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'MENGE' '数量' '' 'X' '' '' '' '' '' '' '' '' '' 'QUAN' 'C',
gt_fieldcat_lvc 'MEINS' '单位' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'TXZ01' '物料描述' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'FACTORY_CODE' '厂家码' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'SUBCODE' 'SUBCODE' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'WERKS' '工厂' '' 'X' '' '' '' '' '' '' '' 'T001W' 'WERKS' '' '',
gt_fieldcat_lvc 'LGORT' '库存地点' '' 'X' '' '' '' '' '' 'X' '' '' '' '' '',
gt_fieldcat_lvc 'EKORG' '采购组织' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'EKGRP' '采购组' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'NETPR_CO' '采购价格-含税' '' '' '' '' '' '' '' '' '' '' '' 'CURR' '',
gt_fieldcat_lvc 'NETPR' '采购价格-未税' '' 'X' '' '' '' '' '' '' '' '' '' 'CURR' '',
gt_fieldcat_lvc 'PEINH' '价格基数' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'WAERS' '采购货币' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'MWSKZ' '税码' '' '' '' '' '' '' '' '' '' 'EKPO' 'MWSKZ' '' '',
gt_fieldcat_lvc 'LIFNR' '供应商' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'NAME1' '采购供应商名称' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'STATUS' '状态' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'MESSAGE' '消息' '30' '' '' '' '' '' '' '' '' '' '' '' ''
.
ENDFORM.
FORM create_fieldcat USING p0 TYPE lvc_t_fcat p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15.
DATA: l_alv_filed TYPE lvc_s_fcat.
l_alv_filed-fieldname = p1. "内表中的字段 大写
l_alv_filed-reptext = p2. "表头的文本
l_alv_filed-outputlen = p3. "字段长度
l_alv_filed-edit = p4. "是否编辑
l_alv_filed-convexit = p5. "转换例程
l_alv_filed-key = p6. "左右滑动 固定列
l_alv_filed-do_sum = p7. "汇总
l_alv_filed-checkbox = p8. "CHECK BOX
l_alv_filed-no_zero = p9. "LV 控制: 为输出隐藏零
l_alv_filed-hotspot = p10. "当击敏感
l_alv_filed-qfieldname = p11. "参考单位
l_alv_filed-f4availabl = 'X'. "是否有搜索帮助
l_alv_filed-ref_table = p12. "内表对应的搜索帮助表
l_alv_filed-ref_field = p13. "内表对应的搜索帮助字段
l_alv_filed-datatype = p14. "输出类型
l_alv_filed-inttype = p15. ""指定该字段类型
APPEND l_alv_filed TO p0.
CLEAR l_alv_filed.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM display_alv .
DATA: ls_layout TYPE lvc_s_layo.
IF gt_data IS NOT INITIAL.
ls_layout-zebra = 'X'.
ls_layout-box_fname = 'CHECK'. "指定行选择字段
ls_layout-cwidth_opt = 'X'.
"---------------enter
DATA: gt_event TYPE slis_t_event WITH HEADER LINE,
ref_grid TYPE REF TO cl_gui_alv_grid.
gt_event-name = 'CALLER_EXIT'.
gt_event-form = 'FM_BUTTON'.
APPEND gt_event .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid "这里是调用这个ALV的程序名
i_callback_pf_status_set = 'PF_STATUS' "设置ALV状态栏的函数
i_callback_user_command = 'USER_COMMAND' "获取用户事件的函数
is_layout_lvc = ls_layout "显示的布局
it_fieldcat_lvc = gt_fieldcat_lvc "设置显示的字段以及字段的功能
it_events = gt_event[]
TABLES
t_outtab = gt_data
EXCEPTIONS
program_error = 1.
IF sy-subrc <> 0.
ENDIF.
ELSE.
* 这个消息用来显示我们是不是获取到了数据,自己用的时候把这个硬代码换成你需要的TEXT-XXX.
MESSAGE '没有获取到数据,请重新检索!' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE TO LIST-PROCESSING.
EXIT.
ENDIF.
ENDFORM.
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS handle_modify
FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING e_modified et_good_cells.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION
DATA: ref_grid TYPE REF TO cl_gui_alv_grid.
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_modify.
DATA stbl TYPE lvc_s_stbl.
PERFORM frm_refresh_alv.
stbl-row = 'x'." 基于行的稳定刷新
stbl-col = 'x'." 基于列稳定刷新
CALL METHOD ref_grid->refresh_table_display
EXPORTING
is_stable = stbl.
ENDMETHOD. "HANDLE_MODIFY
ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATION
DATA:gt_event_receiver TYPE REF TO lcl_event_receiver.
"实现FM_BUTTON:
FORM fm_button USING e_grid TYPE slis_data_caller_exit.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = ref_grid.
"设置enter事件
CALL METHOD ref_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter
EXCEPTIONS
error = 1
OTHERS = 2.
CREATE OBJECT gt_event_receiver.
SET HANDLER gt_event_receiver->handle_modify FOR ref_grid.
ENDFORM. "FM_BUTTON
*&---------------------------------------------------------------------*
*& Form FRM_REFRESH_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_refresh_alv .
"全表更新 含税价。
CLEAR gs_data.
IF gt_data IS NOT INITIAL.
LOOP AT gt_data INTO gs_data.
IF gs_data-netpr IS NOT INITIAL.
gs_data-netpr_co = gs_data-netpr * ( 1 + gs_data-effwr ).
MODIFY gt_data FROM gs_data.
ENDIF.
ENDLOOP.
ENDIF.
ENDFORM.*&---------------------------------------------------------------------*
*& 包含 ZRP008_FRM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM get_data.
SELECT
ekko~bukrs "公司代码
ztmm_incom_list~incom "来料单
ztmm_incom_list~item "项次
ztmm_incom_list~ebeln "采购订单号
ztmm_incom_list~ebelp "采购订单项次
ztmm_incom_list~matnr "物资编码
makt~maktx AS txz01
ztmm_incom_list~menge "数量
ztmm_incom_list~meins "订单单位
ztmm_incom_list~netpr_co "采购含税价
ztmm_incom_list~peinh "价格基数
ztmm_incom_list~netpr "采购未税价格
ztmm_incom_list~mwskz "税码
ekko~bsart "取采购凭证类型
ekko~lifnr "供应商
lfa1~name1 "供应商名称
ekko~ekorg "采购组织
ekko~ekgrp "采购组
ekpo~werks "工厂
ekpo~lgort"库存地点
ekpo~subcode"SUBCODE
ekpo~factory_code"厂家码
* ekpo~factory_code
INTO CORRESPONDING FIELDS OF TABLE gt_data
FROM ztmm_incom_list
INNER JOIN ekko ON ztmm_incom_list~ebeln = ekko~ebeln
INNER JOIN ekpo ON ztmm_incom_list~ebeln = ekpo~ebeln AND ztmm_incom_list~ebelp = ekpo~ebelp
INNER JOIN lfa1 ON ekko~lifnr = lfa1~lifnr
LEFT JOIN makt ON makt~matnr = ztmm_incom_list~matnr
WHERE
ztmm_incom_list~incom IN so_incom AND
ztmm_incom_list~item IN so_item AND
ztmm_incom_list~ebeln IN so_ebeln AND
ztmm_incom_list~ebelp IN so_ebelp AND
ztmm_incom_list~matnr IN so_matnr AND
ekko~lifnr IN so_lifnr
.
"根据税码取税率
IF gt_data IS NOT INITIAL.
DATA lt_ftaxp TYPE TABLE OF ftaxp.
DATA ls_ftaxp TYPE ftaxp.
LOOP AT gt_data INTO gs_data.
"税码转换成税率
CALL FUNCTION 'GET_TAX_PERCENTAGE'
EXPORTING
aland = 'CN'
datab = sy-datum
mwskz = gs_data-mwskz
txjcd = 'TAXBRA'
* EXPORT = ' '
TABLES
t_ftaxp = lt_ftaxp.
IF sy-subrc = 0.
READ TABLE lt_ftaxp INTO ls_ftaxp INDEX 1.
gs_data-effwr = ls_ftaxp-kbetr / 1000.
gs_data-kbetr = ls_ftaxp-kbetr.
ENDIF.
"单位转换
CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
EXPORTING
input = gs_data-meins
language = sy-langu
IMPORTING
* LONG_TEXT =
output = gs_data-meins
* SHORT_TEXT =
* EXCEPTIONS
* UNIT_NOT_FOUND = 1
* OTHERS = 2
.
IF sy-subrc = 0.
ENDIF.
MODIFY gt_data FROM gs_data.
ENDLOOP.
ENDIF.
ENDFORM.
FORM check_inputterm .
DATA lv_times TYPE num10.
lv_times = 0.
IF so_incom IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF so_item IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF so_ebeln IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF so_ebelp IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF so_matnr IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF so_lifnr IS NOT INITIAL.
lv_times = lv_times + 1.
ENDIF.
IF lv_times = 0.
MESSAGE '选择屏幕至少需要填写一个字段!' TYPE 'E'.
ENDIF.
ENDFORM.
*DATA: fieldcat TYPE lvc_t_fcat.
FORM prm_bulid_field .
PERFORM create_fieldcat USING:
gt_fieldcat_lvc 'BUKRS' '公司代码' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'INCOM' '来料单号' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'ITEM' '来料单项次' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'BSART' '采购订单类型' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'EBELN' '采购订单号' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'EBELP' '采购订单项次' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'MATNR' '物资编码' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'MENGE' '数量' '' 'X' '' '' '' '' '' '' '' '' '' 'QUAN' 'C',
gt_fieldcat_lvc 'MEINS' '单位' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'TXZ01' '物料描述' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'FACTORY_CODE' '厂家码' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'SUBCODE' 'SUBCODE' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'WERKS' '工厂' '' 'X' '' '' '' '' '' '' '' 'T001W' 'WERKS' '' '',
gt_fieldcat_lvc 'LGORT' '库存地点' '' 'X' '' '' '' '' '' 'X' '' '' '' '' '',
gt_fieldcat_lvc 'EKORG' '采购组织' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'EKGRP' '采购组' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'NETPR_CO' '采购价格-含税' '' '' '' '' '' '' '' '' '' '' '' 'CURR' '',
gt_fieldcat_lvc 'NETPR' '采购价格-未税' '' 'X' '' '' '' '' '' '' '' '' '' 'CURR' '',
gt_fieldcat_lvc 'PEINH' '价格基数' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'WAERS' '采购货币' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'MWSKZ' '税码' '' '' '' '' '' '' '' '' '' 'EKPO' 'MWSKZ' '' '',
gt_fieldcat_lvc 'LIFNR' '供应商' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'NAME1' '采购供应商名称' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'STATUS' '状态' '' '' '' '' '' '' '' '' '' '' '' '' '',
gt_fieldcat_lvc 'MESSAGE' '消息' '30' '' '' '' '' '' '' '' '' '' '' '' ''
.
ENDFORM.
FORM create_fieldcat USING p0 TYPE lvc_t_fcat p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15.
DATA: l_alv_filed TYPE lvc_s_fcat.
l_alv_filed-fieldname = p1. "内表中的字段 大写
l_alv_filed-reptext = p2. "表头的文本
l_alv_filed-outputlen = p3. "字段长度
l_alv_filed-edit = p4. "是否编辑
l_alv_filed-convexit = p5. "转换例程
l_alv_filed-key = p6. "左右滑动 固定列
l_alv_filed-do_sum = p7. "汇总
l_alv_filed-checkbox = p8. "CHECK BOX
l_alv_filed-no_zero = p9. "LV 控制: 为输出隐藏零
l_alv_filed-hotspot = p10. "当击敏感
l_alv_filed-qfieldname = p11. "参考单位
l_alv_filed-f4availabl = 'X'. "是否有搜索帮助
l_alv_filed-ref_table = p12. "内表对应的搜索帮助表
l_alv_filed-ref_field = p13. "内表对应的搜索帮助字段
l_alv_filed-datatype = p14. "输出类型
l_alv_filed-inttype = p15. ""指定该字段类型
APPEND l_alv_filed TO p0.
CLEAR l_alv_filed.
ENDFORM.
*
*
**&---------------------------------------------------------------------*
**& Form SET_CATALOG
**&---------------------------------------------------------------------*
**& text
**&---------------------------------------------------------------------*
**& --> p1 text
**& <-- p2 text
**&---------------------------------------------------------------------*
*FORM set_catalog .
* DATA:lv_position TYPE i VALUE 1.
* CLEAR: lv_position.
* lv_position = lv_position + 1.
** 预定义一个输出模式
* DEFINE df_fieldcat.
* CLEAR gs_fieldcat_lvc.
* gs_fieldcat_lvc-col_pos = lv_position."ALV 控制: 输出列
* gs_fieldcat_lvc-scrtext_m = &1."中字段标签
* gs_fieldcat_lvc-fieldname = &2."ALV 控制: 内部表字段的字段名称
* gs_fieldcat_lvc-no_zero = &3."ALV 控制: 为输出隐藏零
* gs_fieldcat_lvc-checkbox = &4."ALV 控制: 作为复选框输出
* gs_fieldcat_lvc-edit = &5."设置可编辑模式
* gs_fieldcat_lvc-hotspot = &6."ALV 控制: 单击敏感
** gs_fieldcat_lvc-outputlen = &7."输出长度
* gs_fieldcat_lvc-datatype = &7."输出类型
* gs_fieldcat_lvc-f4availabl = 'X'."是否有搜索帮助
** gs_fieldcat_lvc-qfieldname = &8. "参考单位
** gs_fieldcat_lvc-convexit = &9."转换例程
* gs_fieldcat_lvc-ref_table = &8."内表对应的搜索帮助表
* gs_fieldcat_lvc-ref_field = &9."内表对应的搜索帮助字段
*
* APPEND gs_fieldcat_lvc TO gt_fieldcat_lvc.
* ADD 1 TO lv_position.
* END-OF-DEFINITION.
* df_fieldcat:
* '公司代码' 'BUKRS' '' '' '' '' '' '' '',
* '来料单号' 'INCOM' '' '' '' '' '' '' '',
* '来料单项次' 'ITEM ' '' '' '' '' '' '' '',
* '采购订单类型' 'BSART' '' '' '' '' '' '' '',
* '采购订单号' 'EBELN' '' '' '' '' '' '' '',
* '采购订单项次' 'EBELP' '' '' '' '' '' '' '',
* '物资编码' 'MATNR' '' '' '' '' '' '' '',
* '数量' 'MENGE' '' '' 'X' '' 'QUAN' '' '',
* '单位' 'MEINS' '' '' '' '' '' '' '',
* '物料描述' 'TXZ01' '' '' '' '' '' '' '',
** '原厂码' 'MFRPN' '' '' '' '' '' '' '',
* '厂家码' 'FACTORY_CODE' '' '' '' '' '' '' '',
* 'Subcode' 'SUBCODE' '' '' '' '' '' '' '',
* '工厂' 'WERKS' '' '' 'X' '' '' 'T001W' 'WERKS',
* '库存地点' 'LGORT' '' '' 'X' 'X' '' '' '',
* '采购组织' 'EKORG' '' '' '' '' '' '' '',
* '采购组' 'EKGRP' '' '' '' '' '' '' '',
* '采购价格-含税' 'NETPR_CO' '' '' '' '' 'CURR' '' '',
* '采购价格-未税' 'NETPR' '' '' 'X' '' 'CURR' '' '',
* '价格基数' 'PEINH' '' '' '' '' '' '' '',
* '采购货币' 'WAERS' '' '' '' '' '' '' '',
* '税码' 'MWSKZ' '' '' 'X' '' '' 'EKPO' 'MWSKZ',
* '供应商' 'LIFNR' '' '' '' '' '' '' '',
* '采购供应商名称' 'NAME1' '' '' '' '' '' '' '',
* '状态' 'STATUS' '' '' '' '' '' '' '',
* '消息' 'MESSAGE' '' '' '' '' '' '' ''
*
* .
*ENDFORM.
*&---------------------------------------------------------------------*
*& Form DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM display_alv .
DATA: ls_layout TYPE lvc_s_layo.
IF gt_data IS NOT INITIAL.
ls_layout-zebra = 'X'.
ls_layout-box_fname = 'CHECK'. "指定行选择字段
ls_layout-cwidth_opt = 'X'.
"---------------enter
DATA: gt_event TYPE slis_t_event WITH HEADER LINE,
ref_grid TYPE REF TO cl_gui_alv_grid.
gt_event-name = 'CALLER_EXIT'.
gt_event-form = 'FM_BUTTON'.
APPEND gt_event .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid "这里是调用这个ALV的程序名
i_callback_pf_status_set = 'PF_STATUS' "设置ALV状态栏的函数
i_callback_user_command = 'USER_COMMAND' "获取用户事件的函数
is_layout_lvc = ls_layout "显示的布局
it_fieldcat_lvc = gt_fieldcat_lvc "设置显示的字段以及字段的功能
it_events = gt_event[]
TABLES
t_outtab = gt_data
EXCEPTIONS
program_error = 1.
IF sy-subrc <> 0.
ENDIF.
ELSE.
* 这个消息用来显示我们是不是获取到了数据,自己用的时候把这个硬代码换成你需要的TEXT-XXX.
MESSAGE '没有获取到数据,请重新检索!' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE TO LIST-PROCESSING.
EXIT.
ENDIF.
ENDFORM.
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS handle_modify
FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING e_modified et_good_cells.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION
DATA: ref_grid TYPE REF TO cl_gui_alv_grid.
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_modify.
DATA stbl TYPE lvc_s_stbl.
PERFORM frm_refresh_alv.
stbl-row = 'x'." 基于行的稳定刷新
stbl-col = 'x'." 基于列稳定刷新
CALL METHOD ref_grid->refresh_table_display
EXPORTING
is_stable = stbl.
ENDMETHOD. "HANDLE_MODIFY
ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATION
DATA:gt_event_receiver TYPE REF TO lcl_event_receiver.
"实现FM_BUTTON:
FORM fm_button USING e_grid TYPE slis_data_caller_exit.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = ref_grid.
"设置enter事件
CALL METHOD ref_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter
EXCEPTIONS
error = 1
OTHERS = 2.
CREATE OBJECT gt_event_receiver.
SET HANDLER gt_event_receiver->handle_modify FOR ref_grid.
ENDFORM. "FM_BUTTON
*&---------------------------------------------------------------------*
*& Form FRM_REFRESH_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_refresh_alv .
"全表更新 含税价。
CLEAR gs_data.
IF gt_data IS NOT INITIAL.
LOOP AT gt_data INTO gs_data.
IF gs_data-netpr IS NOT INITIAL.
gs_data-netpr_co = gs_data-netpr * ( 1 + gs_data-effwr ).
MODIFY gt_data FROM gs_data.
ENDIF.
ENDLOOP.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& 包含 ZRP008_PBO
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form PF_STATUS
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM pf_status USING i_it_extab TYPE slis_t_extab.
* 这里我们需要设置状态栏 首先要在程序里面创建一个状态栏
* 定义好名称来这里替换硬代码
SET PF-STATUS 'ZSTATUS_001'.
* 这里我们需要设置状态栏标题 首先要在程序里面创建一个状态栏标题
* 定义好名称来这里替换硬代码
SET TITLEBAR 'ZSTATUS_TITLE'.
ENDFORM.
*&---------------------------------------------------------------------*
*& 包含 ZRP008_PAI
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form USER_COMMAND
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM user_command USING i_ucomm TYPE sy-ucomm
i_wa_selfield TYPE slis_selfield.
"------------------------------刷新ALV---------------
DATA: lr_grid TYPE REF TO cl_gui_alv_grid.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lr_grid.
CALL METHOD lr_grid->check_changed_data.
CALL METHOD lr_grid->refresh_table_display.
i_wa_selfield-refresh = 'X'.
DATA lv_length TYPE num10.
CASE i_ucomm.
WHEN '&IC1'.
READ TABLE gt_data INTO gs_data INDEX i_wa_selfield-tabindex.
CASE i_wa_selfield-fieldname.
WHEN 'LGORT'.
DATA: l_dselc TYPE TABLE OF dselc WITH HEADER LINE.
DATA: l_ddshretval TYPE TABLE OF ddshretval WITH HEADER LINE.
TYPES:BEGIN OF ly_t001l,
werks TYPE t001l-werks,
lgort TYPE t001l-lgort,
lgobe TYPE t001l-lgobe,
END OF ly_t001l.
DATA: lt_lgort TYPE TABLE OF ly_t001l WITH HEADER LINE.
"T001L 库存地点
SELECT lgort lgobe werks INTO CORRESPONDING FIELDS OF TABLE lt_lgort FROM t001l WHERE werks = gs_data-werks.
l_dselc-fldname = 'LGORT'.
l_dselc-dyfldname = 'LGORT'.
APPEND l_dselc.
l_dselc-fldname = 'LGOBE'.
l_dselc-dyfldname = 'LGOBE'.
APPEND l_dselc.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'LGORT'
dynpprog = sy-repid
dynpnr = sy-dynnr
dynprofield = 'LGORT' "返回到屏幕上的ALV 或者屏幕上对应的字段。
window_title = '选择库存地点'
value_org = 'S' "Structure
TABLES
value_tab = lt_lgort[]
return_tab = l_ddshretval[]
dynpfld_mapping = l_dselc[]
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
IF sy-subrc = 0.
IF l_ddshretval[] IS NOT INITIAL.
READ TABLE l_ddshretval[] INTO l_ddshretval INDEX 1.
gs_data-lgort = l_ddshretval-fieldval.
MODIFY gt_data FROM gs_data INDEX i_wa_selfield-tabindex.
ENDIF.
ENDIF.
ENDCASE.
WHEN 'CREATE_PO'. "创建采购退货订单
lv_length = 0.
CLEAR gs_data.
REFRESH gt_data_copy.
LOOP AT gt_data INTO gs_data WHERE check = 'X' .
lv_length = lv_length + 1.
IF gs_data-status = 'S'.
APPEND gs_data TO gt_data_copy.
ENDIF.
ENDLOOP.
IF lv_length = 0.
MESSAGE '请选择行!' TYPE 'E'.
ENDIF.
IF lines( gt_data_copy ) > 0 .
MESSAGE '选中的行有已经创建成功的记录!' TYPE 'E'.
ENDIF.
"供应商、公司、采购组织、采购组 都相同的数据,创建在同一张采购订单不同行上。
CLEAR gs_data.
REFRESH gt_data_copy.
LOOP AT gt_data INTO gs_data WHERE check = 'X'.
APPEND gs_data TO gt_data_copy.
ENDLOOP.
DELETE ADJACENT DUPLICATES FROM gt_data_copy COMPARING bukrs lifnr ekorg ekgrp.
"=========创建PO需要的参数类型定义
DATA ls_poheader TYPE bapimepoheader.
DATA ls_poheaderx TYPE bapimepoheaderx.
DATA ls_poitem TYPE bapimepoitem.
DATA lt_poitem TYPE TABLE OF bapimepoitem.
DATA ls_poitemx TYPE bapimepoitemx.
DATA lt_poitemx TYPE TABLE OF bapimepoitemx.
DATA lv_poitem TYPE num10.
DATA lt_bapiret2 TYPE TABLE OF bapiret2.
DATA ls_bapiret2 TYPE bapiret2.
DATA lt_extensionin TYPE TABLE OF bapiparex .
DATA ls_extensionin TYPE bapiparex .
DATA ls_bapi_te_mepoitem TYPE bapi_te_mepoitem.
DATA ls_bapi_te_mepoitemx TYPE bapi_te_mepoitemx.
CLEAR gs_data_copy.
LOOP AT gt_data_copy INTO gs_data_copy.
ls_poheader-comp_code = gs_data_copy-bukrs. "公司代码
ls_poheader-doc_type = gs_data_copy-bsart. "采购凭证类型
ls_poheader-creat_date = sy-datum. "系统当前日期
ls_poheader-created_by = sy-uname.
ls_poheader-item_intvl = '00010'. "项目编号间隔
ls_poheader-vendor = gs_data_copy-lifnr."指定的供应商
ls_poheader-purch_org = gs_data_copy-ekorg. "采购组织
ls_poheader-pur_group = gs_data_copy-ekgrp. "采购组
ls_poheaderx-comp_code = 'X'. "公司代码
ls_poheaderx-doc_type = 'X'. "采购凭证类型
ls_poheaderx-creat_date = 'X'. "系统当前日期
ls_poheaderx-created_by = 'X'.
ls_poheaderx-item_intvl = 'X'. "项目编号间隔
ls_poheaderx-vendor = 'X'."指定的供应商
ls_poheaderx-purch_org = 'X'. "采购组织
ls_poheaderx-pur_group = 'X'. "采购组
lv_poitem = 10.
LOOP AT gt_data INTO gs_data WHERE bukrs = gs_data_copy-bukrs
AND lifnr = gs_data_copy-lifnr AND ekorg = gs_data_copy-ekorg AND ekgrp = gs_data_copy-ekgrp AND check = 'X'.
ls_poitem-po_item = lv_poitem.
* ls_poitem-material = gs_data-matnr."物料号 "注:不支持40位物料号,故换成 :ls_poitem-material_long = gs_data-matnr."物料号
ls_poitem-material_long = gs_data-matnr."物料号
ls_poitem-plant = gs_data-werks."工厂
ls_poitem-info_rec = ''."采购信息记录的编号
ls_poitem-quantity = gs_data-menge."数量
"外部单位转换成内部单位
IF gs_data-meins IS NOT INITIAL.
PERFORM conversion_exit_cunit_input CHANGING gs_data.
ENDIF.
ls_poitem-po_unit = gs_data-meins."单位
ls_poitem-net_price = gs_data-netpr."采购价格-未税
ls_poitem-price_unit = gs_data-peinh. "价格单位
ls_poitem-tax_code = gs_data-mwskz."税
ls_poitem-ret_item = 'X'."退货项目
APPEND ls_poitem TO lt_poitem.
ls_poitemx-po_item = lv_poitem.
ls_poitemx-po_itemx = 'X'.
* ls_poitemx-material = 'X'."物料号
ls_poitemx-material_long = 'X'."物料号
ls_poitemx-plant = 'X'."工厂
ls_poitemx-info_rec = 'X'."采购信息记录的编号
ls_poitemx-quantity = 'X'."数量
ls_poitemx-po_unit = 'X'."单位
ls_poitemx-net_price = 'X'."采购价格-含税
ls_poitemx-price_unit = 'X'. "价格单位
ls_poitemx-tax_code = 'X'."税
ls_poitemx-ret_item = 'X'."退货项目
APPEND ls_poitemx TO lt_poitemx.
"增强字段
ls_bapi_te_mepoitem-po_item = lv_poitem."项次
ls_bapi_te_mepoitemx-po_item = lv_poitem."项次 "注:增强这个位置比较特殊。
ls_bapi_te_mepoitem-item_note = gs_data-incom."行项目备注-来料单号
ls_bapi_te_mepoitemx-item_note = 'X'."行项目备注
ls_extensionin-structure = 'BAPI_TE_MEPOITEM'.
ls_extensionin-valuepart1 = ls_bapi_te_mepoitem.
APPEND ls_extensionin TO lt_extensionin.
CLEAR: ls_extensionin.
ls_extensionin-structure = 'BAPI_TE_MEPOITEMX'.
ls_extensionin-valuepart1 = ls_bapi_te_mepoitemx.
APPEND ls_extensionin TO lt_extensionin.
CLEAR: ls_extensionin.
lv_poitem = lv_poitem + 10.
APPEND gs_data TO gt_data_temp. "临时记录将要被更新的记录
ENDLOOP.
"创建一个PO
DATA ls_expheader TYPE bapimepoheader.
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = ls_poheader
poheaderx = ls_poheaderx
IMPORTING
expheader = ls_expheader
TABLES
return = lt_bapiret2
poitem = lt_poitem
poitemx = lt_poitemx
extensionin = lt_extensionin.
IF sy-subrc = 0.
"表示PO创建成功,更新内表记录
IF lt_bapiret2 IS NOT INITIAL.
DATA zmessage(255) TYPE c.
zmessage = ''.
DATA zflag(8) TYPE c.
LOOP AT lt_bapiret2 INTO ls_bapiret2 .
IF ls_bapiret2-type = 'E'.
zmessage = zmessage && ls_bapiret2-type && ':' && ls_bapiret2-message && ','.
ELSEIF ls_bapiret2-type = 'S'.
zflag = 'S'.
ENDIF.
ENDLOOP.
IF zmessage IS NOT INITIAL. "表示有失败的信息记录
MESSAGE zmessage TYPE 'S' DISPLAY LIKE 'E'.
LOOP AT gt_data_temp INTO gs_data_temp.
gs_data_temp-status = 'E'.
gs_data_temp-message = zmessage.
MODIFY gt_data_temp FROM gs_data_temp.
ENDLOOP.
ELSE.
IF zflag = 'S'. "确认采购订单被创建成功
DATA ls_pocommit_return TYPE bapiret2.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' "BAPI事务提交
EXPORTING
wait = 'X'
IMPORTING
return = ls_pocommit_return.
zmessage = ls_expheader-po_number && '采购订单创建成功'.
MESSAGE zmessage TYPE 'S'.
LOOP AT gt_data_temp INTO gs_data_temp.
gs_data_temp-status = zflag.
gs_data_temp-message = zmessage.
MODIFY gt_data_temp FROM gs_data_temp.
ENDLOOP.
ENDIF.
APPEND LINES OF gt_data_temp TO gt_data_success_log. "最终记录创建成功的记录。
ENDIF.
ENDIF.
ENDIF.
CLEAR ls_poitemx.
CLEAR ls_poitem.
REFRESH lt_poitemx.
REFRESH lt_poitem.
CLEAR ls_bapi_te_mepoitem.
CLEAR ls_bapi_te_mepoitemx.
CLEAR: ls_extensionin.
REFRESH lt_extensionin.
REFRESH gt_data_temp.
ENDLOOP.
"对创建成功的记录更新到ALV显示内表 GT_DADA.
CLEAR gs_data.
LOOP AT gt_data INTO gs_data WHERE check = 'X'.
READ TABLE gt_data_success_log INTO gs_data_success_log WITH KEY ebeln = gs_data-ebeln ebelp = gs_data-ebelp.
IF sy-subrc = 0."在记录表中存在, 将更新GT_DATA 记录状态。
gs_data-status = gs_data_success_log-status.
gs_data-message = gs_data_success_log-message.
MODIFY gt_data FROM gs_data.
ENDIF.
ENDLOOP.
WHEN OTHERS.
ENDCASE.
ENDFORM.
*& Form CONVERSION_EXIT_CUNIT_INPUT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& <-- GS_DATA
*&---------------------------------------------------------------------*
FORM conversion_exit_cunit_input CHANGING p_gs_data TYPE gy_data.
CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
EXPORTING
input = p_gs_data-meins
language = sy-langu
IMPORTING
output = p_gs_data-meins.
ENDFORM.