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, "Subcodewerks 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.SELECTekko~bukrs "公司代码ztmm_incom_list~incom "来料单ztmm_incom_list~item "项次ztmm_incom_list~ebeln "采购订单号ztmm_incom_list~ebelp "采购订单项次ztmm_incom_list~matnr "物资编码makt~maktx AS txz01ztmm_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"SUBCODEekpo~factory_code"厂家码
* ekpo~factory_codeINTO CORRESPONDING FIELDS OF TABLE gt_dataFROM ztmm_incom_listINNER JOIN ekko ON ztmm_incom_list~ebeln = ekko~ebelnINNER JOIN ekpo ON ztmm_incom_list~ebeln = ekpo~ebeln AND ztmm_incom_list~ebelp = ekpo~ebelpINNER JOIN lfa1 ON ekko~lifnr = lfa1~lifnrLEFT JOIN makt ON makt~matnr = ztmm_incom_list~matnrWHEREztmm_incom_list~incom IN so_incom ANDztmm_incom_list~item IN so_item ANDztmm_incom_list~ebeln IN so_ebeln ANDztmm_incom_list~ebelp IN so_ebelp ANDztmm_incom_list~matnr IN so_matnr ANDekko~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'EXPORTINGaland = 'CN'datab = sy-datummwskz = gs_data-mwskztxjcd = 'TAXBRA'
* EXPORT = ' 'TABLESt_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'EXPORTINGinput = gs_data-meinslanguage = sy-languIMPORTING
* 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 BOXl_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'."---------------enterDATA: 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'EXPORTINGi_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[]TABLESt_outtab = gt_dataEXCEPTIONSprogram_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_modifyFOR EVENT data_changed_finished OF cl_gui_alv_gridIMPORTING e_modified et_good_cells.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITIONDATA: 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_displayEXPORTINGis_stable = stbl.ENDMETHOD. "HANDLE_MODIFY
ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATIONDATA: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'IMPORTINGe_grid = ref_grid."设置enter事件CALL METHOD ref_grid->register_edit_eventEXPORTINGi_event_id = cl_gui_alv_grid=>mc_evt_enterEXCEPTIONSerror = 1OTHERS = 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.SELECTekko~bukrs "公司代码ztmm_incom_list~incom "来料单ztmm_incom_list~item "项次ztmm_incom_list~ebeln "采购订单号ztmm_incom_list~ebelp "采购订单项次ztmm_incom_list~matnr "物资编码makt~maktx AS txz01ztmm_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"SUBCODEekpo~factory_code"厂家码
* ekpo~factory_codeINTO CORRESPONDING FIELDS OF TABLE gt_dataFROM ztmm_incom_listINNER JOIN ekko ON ztmm_incom_list~ebeln = ekko~ebelnINNER JOIN ekpo ON ztmm_incom_list~ebeln = ekpo~ebeln AND ztmm_incom_list~ebelp = ekpo~ebelpINNER JOIN lfa1 ON ekko~lifnr = lfa1~lifnrLEFT JOIN makt ON makt~matnr = ztmm_incom_list~matnrWHEREztmm_incom_list~incom IN so_incom ANDztmm_incom_list~item IN so_item ANDztmm_incom_list~ebeln IN so_ebeln ANDztmm_incom_list~ebelp IN so_ebelp ANDztmm_incom_list~matnr IN so_matnr ANDekko~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'EXPORTINGaland = 'CN'datab = sy-datummwskz = gs_data-mwskztxjcd = 'TAXBRA'
* EXPORT = ' 'TABLESt_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'EXPORTINGinput = gs_data-meinslanguage = sy-languIMPORTING
* 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 BOXl_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'."---------------enterDATA: 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'EXPORTINGi_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[]TABLESt_outtab = gt_dataEXCEPTIONSprogram_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_modifyFOR EVENT data_changed_finished OF cl_gui_alv_gridIMPORTING e_modified et_good_cells.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITIONDATA: 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_displayEXPORTINGis_stable = stbl.ENDMETHOD. "HANDLE_MODIFY
ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATIONDATA: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'IMPORTINGe_grid = ref_grid."设置enter事件CALL METHOD ref_grid->register_edit_eventEXPORTINGi_event_id = cl_gui_alv_grid=>mc_evt_enterEXCEPTIONSerror = 1OTHERS = 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-ucommi_wa_selfield TYPE slis_selfield."------------------------------刷新ALV---------------DATA: lr_grid TYPE REF TO cl_gui_alv_grid.CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'IMPORTINGe_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'EXPORTINGretfield = 'LGORT'dynpprog = sy-repiddynpnr = sy-dynnrdynprofield = 'LGORT' "返回到屏幕上的ALV 或者屏幕上对应的字段。window_title = '选择库存地点'value_org = 'S' "StructureTABLESvalue_tab = lt_lgort[]return_tab = l_ddshretval[]dynpfld_mapping = l_dselc[]EXCEPTIONSparameter_error = 1no_values_found = 2OTHERS = 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-bukrsAND 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."创建一个PODATA ls_expheader TYPE bapimepoheader.CALL FUNCTION 'BAPI_PO_CREATE1'EXPORTINGpoheader = ls_poheaderpoheaderx = ls_poheaderxIMPORTINGexpheader = ls_expheaderTABLESreturn = lt_bapiret2poitem = lt_poitempoitemx = lt_poitemxextensionin = 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事务提交EXPORTINGwait = 'X'IMPORTINGreturn = 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'EXPORTINGinput = p_gs_data-meinslanguage = sy-languIMPORTINGoutput = p_gs_data-meins.
ENDFORM.