赞
踩
原文链接:https://blog.csdn.net/xiaofei2008gxh/article/details/123198226
实际成果
说重点 上代码
*&---------------------------------------------------------------------* *& REPORT ZRPT_PP_ORDER_CREATEM *& *&---------------------------------------------------------------------* *&DATE:20220228 *&AUTHOR:gavin *&TITLE:生产订单批量生成 4 *&TCODE: ZCO01 *&---------------------------------------------------------------------* REPORT ZRPT_PP_ORDER_CREATEM. TABLES:ZPPCOSTMDATA,SSCRFIELDS. * *定义内表类型 * TYPES: BEGIN OF W_ITAB, MATNR LIKE BAPI_PP_ORDER_CREATE-MATERIAL, WERKS LIKE BAPI_PP_ORDER_CREATE-PLANT, QUANTT LIKE BAPI_PP_ORDER_CREATE-QUANTITY, VERID LIKE BAPI_PP_ORDER_CREATE-PROD_VERSION, START LIKE BAPI_PP_ORDER_CREATE-BASIC_START_DATE, ENDDT LIKE BAPI_PP_ORDER_CREATE-BASIC_END_DATE, AUFNR LIKE BAPI_PP_ORDER_CREATE-ORDER_NUMBER, MSGTXT LIKE BAPIRET2-MESSAGE, RELSTT TYPE C,"下达生产订单的状态 MSGREL LIKE BAPIRET2-MESSAGE, END OF W_ITAB. FIELD-SYMBOLS: <FS>. DATA: FILETAB TYPE FILETABLE WITH HEADER LINE, RC TYPE I, G_FILE TYPE RLGRAP-FILENAME, LT_RETURN TYPE TABLE OF BAPIRET2 WITH HEADER LINE, LT_AUFNR TYPE AUFNR, ITAB TYPE TABLE OF ALSMEX_TABLINE WITH HEADER LINE. DATA: WTAB TYPE TABLE OF W_ITAB WITH HEADER LINE. DATA: COUNT TYPE I. DATA:STDATUM LIKE SY-DATUM, STUZEIT LIKE SY-UZEIT. DATA:L_FILENAME TYPE STRING,L_MUBAN TYPE STRING. DATA:GV_FULLPATH TYPE STRING,GV_PATH TYPE STRING,GV_NAME TYPE STRING. data:header_in TYPE BAPI_PP_ORDER_CREATE. *&---------------------------------------------------------------------* *& 选择界面 *&---------------------------------------------------------------------* SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME TITLE TXT1 . PARAMETER:PAUART TYPE PP_AUFART OBLIGATORY DEFAULT 'ZP05'. PARAMETER:P_FILE TYPE STRING OBLIGATORY. *PARAMETER:DELFLG AS CHECKBOX DEFAULT 'X'. SELECTION-SCREEN END OF BLOCK BLK1. SELECTION-SCREEN SKIP. SELECTION-SCREEN BEGIN OF LINE. *SELECTION-SCREEN PUSHBUTTON 5(60) but1 USER-COMMAND bt1. SELECTION-SCREEN PUSHBUTTON 5(20) but2 USER-COMMAND bt2. SELECTION-SCREEN END OF LINE. INITIALIZATION. TXT1 = '请输入条件'. "生产订单类型固定且不可改 LOOP AT SCREEN. IF SCREEN-NAME = 'PAUART'. SCREEN-INPUT = '0'. MODIFY SCREEN. ENDIF. ENDLOOP. * CALL FUNCTION 'ICON_CREATE' * EXPORTING * name = ICON_BW_REF_STRUCTURE_SAP * TEXT = 'SM30维护 视图 ZPPCOSTMDATA' * INFO = 'SM30维护 视图 ZPPCOSTMDATA' * IMPORTING * result = but1 * EXCEPTIONS * icon_not_found = 1 * outputfield_too_short = 2 * OTHERS = 3. CALL FUNCTION 'ICON_CREATE' EXPORTING name = ICON_BW_REF_STRUCTURE_SAP TEXT = '下载维护模板' INFO = '下载维护模板' IMPORTING result = but2 EXCEPTIONS icon_not_found = 1 outputfield_too_short = 2 OTHERS = 3. *&--------------------------------------------------------------------- AT SELECTION-SCREEN. CASE SSCRFIELDS-UCOMM. * WHEN 'BT1'. * PERFORM CALLSM30. WHEN 'BT2'. PERFORM DOWNLOAD_DATA. ENDCASE. AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE. CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG EXPORTING DEFAULT_FILENAME = P_FILE CHANGING RC = RC FILE_TABLE = FILETAB[]. READ TABLE FILETAB INDEX 1. IF SY-SUBRC EQ 0. P_FILE = FILETAB-FILENAME. ENDIF. START-OF-SELECTION. STDATUM = SY-DATUM. STUZEIT = SY-UZEIT. if p_file <> ''. PERFORM GETEXCEL."EXCEL数据获取 * else. * MESSAGE '请选择要导入的excel文件' TYPE I. * perform getdata."获取版本描述表中数据 endif. COUNT = 0. PERFORM WRITERPTRUNRECORD. PERFORM DISPLAY."数据ALV输出 END-OF-SELECTION. *&---------------------------------------------------------------------* *& Form WRITERPTRUNRECORD *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM WRITERPTRUNRECORD. get time. CALL FUNCTION 'ZSYS_RPT_RUNRECORD' EXPORTING TCODE = SY-TCODE PROGRAMM = SY-CPROG DATUMRST = STDATUM UZEITRST = STUZEIT BATCH = SY-BATCH DATUMREND = SY-datum UZEITREND = sy-UZEIT DATUMC = '20220228' RUNNER = SY-UNAME RPTSRC = 'cfp'. ENDFORM. "WRITERPTRUNRECORD *&---------------------------------------------------------------------* *& FORM DISPLAY *&---------------------------------------------------------------------* * TEXT 数据展示 *----------------------------------------------------------------------* * --> P1 TEXT * <-- P2 TEXT *----------------------------------------------------------------------* FORM DISPLAY . DATA: IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV, IS_VARIANT LIKE DISVARIANT, IT_EVENTS TYPE SLIS_T_EVENT, IS_LAYOUT TYPE SLIS_LAYOUT_ALV, TITLE TYPE LVC_TITLE, I_CALLBACK_PROGRAM TYPE SY-REPID, IT_SORT TYPE SLIS_T_SORTINFO_ALV WITH HEADER LINE, I_GRID_SETTINGS TYPE LVC_S_GLAY. I_GRID_SETTINGS-EDT_CLL_CB = 'X'. "更改数据后更新表数据. PERFORM FIELDCAT_INIT USING IT_FIELDCAT. PERFORM LAYOUT_INIT USING IS_LAYOUT. I_CALLBACK_PROGRAM = SY-REPID. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING I_CALLBACK_PROGRAM = I_CALLBACK_PROGRAM I_GRID_TITLE = TITLE I_GRID_SETTINGS = I_GRID_SETTINGS IS_LAYOUT = IS_LAYOUT I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS' I_CALLBACK_USER_COMMAND = 'USER_COMMAND' IT_FIELDCAT = IT_FIELDCAT IT_EVENTS = IT_EVENTS TABLES T_OUTTAB = WTAB[] EXCEPTIONS PROGRAM_ERROR = 1 OTHERS = 2. IF SY-SUBRC <> 0. ENDIF. ENDFORM. " DISPLAY *&--------------------------------------------------------------------- * *& Form g_status_form 调用自定义屏幕 *&--------------------------------------------------------------------- FORM SET_PF_STATUS USING rt_extab TYPE slis_t_extab. SET PF-STATUS 'STANDARD' EXCLUDING rt_extab. ENDFORM. "SET_PF_STATUS *&---------------------------------------------------------------------* *& FORM FIELDCAT_INIT *&---------------------------------------------------------------------* * TEXT 定义输出项 *----------------------------------------------------------------------* * -->P_IT_FIELDCAT TEXT *----------------------------------------------------------------------* FORM FIELDCAT_INIT USING IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV. DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV. DATA:LOC_REPID LIKE SY-REPID. LOC_REPID = SY-REPID. LS_FIELDCAT-FIELDNAME = 'MATNR'. LS_FIELDCAT-REPTEXT_DDIC = '物料'. APPEND LS_FIELDCAT TO IT_FIELDCAT. CLEAR LS_FIELDCAT. LS_FIELDCAT-FIELDNAME = 'WERKS'. LS_FIELDCAT-REPTEXT_DDIC = '工厂'. APPEND LS_FIELDCAT TO IT_FIELDCAT. CLEAR LS_FIELDCAT. LS_FIELDCAT-FIELDNAME = 'QUANTT'. LS_FIELDCAT-REPTEXT_DDIC = '数量'. APPEND LS_FIELDCAT TO IT_FIELDCAT. CLEAR LS_FIELDCAT. LS_FIELDCAT-FIELDNAME = 'VERID'. LS_FIELDCAT-REPTEXT_DDIC = '生产版本'. APPEND LS_FIELDCAT TO IT_FIELDCAT. CLEAR LS_FIELDCAT. LS_FIELDCAT-FIELDNAME = 'START'. LS_FIELDCAT-REPTEXT_DDIC = '开始日期'. LS_FIELDCAT-REF_TABNAME = 'CAUFV'. LS_FIELDCAT-REF_FIELDNAME = 'ERDAT'. APPEND LS_FIELDCAT TO IT_FIELDCAT. CLEAR LS_FIELDCAT. LS_FIELDCAT-FIELDNAME = 'ENDDT'. LS_FIELDCAT-REPTEXT_DDIC = '结束日期'. LS_FIELDCAT-REF_TABNAME = 'CAUFV'. LS_FIELDCAT-REF_FIELDNAME = 'ERDAT'. APPEND LS_FIELDCAT TO IT_FIELDCAT. CLEAR LS_FIELDCAT. LS_FIELDCAT-FIELDNAME = 'AUFNR'. LS_FIELDCAT-REPTEXT_DDIC = '生产订单号'. LS_FIELDCAT-NO_ZERO = 'X'. APPEND LS_FIELDCAT TO IT_FIELDCAT. CLEAR LS_FIELDCAT. LS_FIELDCAT-FIELDNAME = 'MSGTXT'. LS_FIELDCAT-REPTEXT_DDIC = '消息文本'. APPEND LS_FIELDCAT TO IT_FIELDCAT. CLEAR LS_FIELDCAT. LS_FIELDCAT-FIELDNAME = 'RELSTT'. LS_FIELDCAT-REPTEXT_DDIC = '生产订单下达状态'. APPEND LS_FIELDCAT TO IT_FIELDCAT. CLEAR LS_FIELDCAT. LS_FIELDCAT-FIELDNAME = 'MSGREL'. LS_FIELDCAT-REPTEXT_DDIC = '生产订单下达文本'. APPEND LS_FIELDCAT TO IT_FIELDCAT. CLEAR LS_FIELDCAT. ENDFORM. "FIELDCAT_INIT " FIELDCAT_INIT *&---------------------------------------------------------------------* *& FORM LAYOUT_INIT *&---------------------------------------------------------------------* * TEXT LAYOUT属性设置,双击响应字段设置 *----------------------------------------------------------------------* * -->P_IS_LAYOUT TEXT *----------------------------------------------------------------------* FORM LAYOUT_INIT USING IS_LAYOUT TYPE SLIS_LAYOUT_ALV. IS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'. ENDFORM. " LAYOUT_INIT **&--------------------------------------------------------------------* **& FORM USER_COMMAND **&--------------------------------------------------------------------* ** TEXT **---------------------------------------------------------------------* ** -->RF_UCOMM TEXT ** -->RS_SELFIELDTEXT **---------------------------------------------------------------------* FORM USER_COMMAND USING RF_UCOMM LIKE SY-UCOMM RS_SELFIELD TYPE SLIS_SELFIELD. "#EC CALLED DATA:BEGIN OF ORDERLIST OCCURS 0, AUFNR TYPE AUFNR, END OF ORDERLIST. data:pflg TYPE c. DATA:DELETERETURN TYPE TABLE OF BAPI_ORDER_RETURN. DATA:L_DELETERETURN LIKE LINE OF DELETERETURN. CASE RF_UCOMM. WHEN '&PRT'. *****插入数据 创建生产订单 if COUNT = 0. COUNT = COUNT + 1. CLEAR ORDERLIST[]. loop at WTAB. * HEADER_IN-ORDER_NUMBER = LT_MOINFO-AUFNR."订单号 * 注意:物料前导0 要加上 CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING INPUT = WTAB-MATNR IMPORTING OUTPUT = HEADER_IN-MATERIAL. * HEADER_IN-MATERIAL = WTAB-MATNR."物料编号 HEADER_IN-PLANT = WTAB-WERKS."工厂 HEADER_IN-ORDER_TYPE = PAUART."ORDER_TYPE."订单类型 HEADER_IN-QUANTITY = WTAB-QUANTT."订单数量总计 HEADER_IN-BASIC_START_DATE = WTAB-START."基本开始日期 HEADER_IN-BASIC_END_DATE = WTAB-ENDDT."基本完成日期 HEADER_IN-PROD_VERSION = WTAB-VERID."生产版本 CALL FUNCTION 'BAPI_PRODORD_CREATE' EXPORTING ORDERDATA = HEADER_IN IMPORTING ORDER_NUMBER = LT_AUFNR RETURN = LT_RETURN. "ORDER_TYPE = LT_AUART IF LT_AUFNR IS NOT INITIAL. ORDERLIST-AUFNR = LT_AUFNR. APPEND ORDERLIST. ELSE. WTAB-RELSTT = 'E'. WTAB-MSGREL = '生产订单号为空,不能下达'. ENDIF. IF LT_AUFNR <> '' AND LT_RETURN IS INITIAL . "订单创建成功 WTAB-AUFNR = LT_AUFNR. WTAB-MSGTXT = 'STATU = S,订单创建成功 '. ELSE. WTAB-MSGTXT = 'STATU = E,订单创建失败。' && LT_RETURN-MESSAGE. ENDIF. MODIFY WTAB. ENDLOOP. ”等待10s 否则会有生产订单不存在的提示 CALL FUNCTION 'AB_SLEEP' EXPORTING WAIT_TIME = 10. IF ORDERLIST[] IS NOT INITIAL. CALL FUNCTION 'BAPI_PRODORD_RELEASE' TABLES ORDERS = ORDERLIST DETAIL_RETURN = DELETERETURN. LOOP AT WTAB. SHIFT WTAB-AUFNR LEFT DELETING LEADING '0'. LOOP AT DELETERETURN INTO L_DELETERETURN WHERE MESSAGE_V1 = WTAB-AUFNR. WTAB-RELSTT = L_DELETERETURN-TYPE. WTAB-MSGREL = L_DELETERETURN-MESSAGE. MODIFY WTAB. ENDLOOP. ENDLOOP. ENDIF. RS_SELFIELD-REFRESH = 'X'. ELSE. MESSAGE '订单已经创建并下达,请不要重复创建/下达' TYPE 'I'. ENDIF. ENDCASE. ENDFORM. "USER_COMMAND *&---------------------------------------------------------------------* *& FORM GETEXCEL *&---------------------------------------------------------------------* * TEXT 获取上传EXCEL文件内相关数据 *----------------------------------------------------------------------* * --> P1 TEXT * <-- P2 TEXT *----------------------------------------------------------------------* FORM GETEXCEL . G_FILE = P_FILE. "注意两个文件用于不同的函数,所以类型不同 CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE' EXPORTING FILENAME = G_FILE I_BEGIN_COL = 1 I_BEGIN_ROW = 1 I_END_COL = 10 I_END_ROW = 5000 TABLES INTERN = ITAB[]. DELETE ITAB WHERE ROW = 1. "删除字段名行 LOOP AT ITAB. ON CHANGE OF ITAB-ROW. IF SY-TABIX NE 1. APPEND WTAB. CLEAR WTAB. ENDIF. ENDON. ASSIGN COMPONENT ITAB-COL OF STRUCTURE WTAB TO <FS>. "动态方法将值传到相应的内表 <FS> = ITAB-VALUE. ENDLOOP. APPEND WTAB. "这句不要忘记,要把最后一条APPEND到内表中 sort wtab by MATNR. ENDFORM. " GETEXCEL *&---------------------------------------------------------------------* *& Form GETDATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM GETDATA . * SELECT * * INTO CORRESPONDING FIELDS OF TABLE WTAB * FROM ZPPCOSTMDATA * WHERE KOSTL IN S_KOSTL AND * ZDEPT1 IN S_ZDEPT1 AND * ZDEPT2 IN S_ZDEPT2 AND * ZDEPT3 IN S_ZDEPT3 AND * ZDEPT4 IN S_ZDEPT4 AND * ZDEPT5 IN S_ZDEPT5. * SORT WTAB BY KOSTL. ENDFORM. " GETDATA *&---------------------------------------------------------------------* *& Form CALLSM30 *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM CALLSM30 . CALL FUNCTION 'VIEW_MAINTENANCE_CALL' EXPORTING action = 'U' " S = Display U = Change T = Transport view_name = 'ZPPCOSTMDATA' "读取的表 no_warning_for_clientindep = 'X' EXCEPTIONS client_reference = 1 foreign_lock = 2 invalid_action = 3 no_clientindependent_auth = 4 no_database_function = 5 no_editor_function = 6 no_show_auth = 7 no_tvdir_entry = 8 no_upd_auth = 9 only_show_allowed = 10 system_failure = 11 unknown_field_in_dba_sellist = 12 view_not_found = 13 OTHERS = 14. ENDFORM. " CALLSM30 *&---------------------------------------------------------------------* *& Form DOWNLOAD_DATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM DOWNLOAD_DATA . l_filename = 'ZPPORDERC.xlsx'. l_muban = 'ZPPORDERC'. *&---下载模板 PERFORM frm_get_fullpath CHANGING gv_fullpath gv_path gv_name. *&---路径为空则退出 IF gv_fullpath IS INITIAL. MESSAGE '用户取消操作' TYPE 'S'. RETURN. ENDIF. PERFORM frm_down USING gv_fullpath. ENDFORM. " DOWNLOAD_DATA *&---------------------------------------------------------------------* *& Form FRM_GET_FULLPATH *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * <--P_GV_FULLPATH text * <--P_GV_PATH text * <--P_GV_NAME text *----------------------------------------------------------------------* FORM frm_get_fullpath CHANGING pv_fullpath TYPE string pv_path TYPE string pv_name TYPE string. DATA: lv_init_path TYPE string, lv_init_fname TYPE string, lv_path TYPE string, lv_filename TYPE string, lv_fullpath TYPE string. *&---初始名称(输出的文件名称) * concatenate 'Material_Doc_' SY-DATUM '.xslx' into L_INIT_FNAME. lv_init_fname = l_filename. * 获取桌面路径 CALL METHOD cl_gui_frontend_services=>get_desktop_directory CHANGING desktop_directory = lv_init_path EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4. IF sy-subrc <> 0. EXIT. ENDIF. *&---用户选择名称、路径 CALL METHOD cl_gui_frontend_services=>file_save_dialog EXPORTING * window_title = '指定保存文件名' * default_extension = 'DOC' default_file_name = lv_init_fname * FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_EXCEL * FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_WORD initial_directory = lv_init_path prompt_on_overwrite = 'X' CHANGING filename = lv_filename path = lv_path fullpath = lv_fullpath * USER_ACTION = * FILE_ENCODING = EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4. IF sy-subrc = 0. pv_fullpath = lv_fullpath. pv_path = lv_path. ENDIF. ENDFORM. " FRM_GET_FULLPATH *--------------------------------------------------------------------* * 下载文件 *--------------------------------------------------------------------* FORM frm_down USING pr_filename. DATA: lv_objdata LIKE wwwdatatab, lv_mime LIKE w3mime, lv_destination LIKE rlgrap-filename, lv_objnam TYPE string, lv_rc LIKE sy-subrc, lv_errtxt TYPE string. DATA: lv_filename TYPE string, lv_result, lv_subrc TYPE sy-subrc. DATA: lv_objid TYPE wwwdatatab-objid . lv_objid = l_muban. "上传的模版名称 *&---查找文件是否存在。 SELECT SINGLE relid objid FROM wwwdata INTO CORRESPONDING FIELDS OF lv_objdata WHERE srtf2 = 0 AND relid = 'MI' AND objid = lv_objid. *&---判断模版不存在则报错 IF sy-subrc NE 0 OR lv_objdata-objid EQ space. CONCATENATE '模板文件:' lv_objid '不存在,请用TCODE:SMW0进行加载' INTO lv_errtxt. MESSAGE lv_errtxt TYPE 'E'. ENDIF. lv_filename = pr_filename. "判断本地地址是否已经存在此文件。 CALL METHOD cl_gui_frontend_services=>file_exist EXPORTING file = lv_filename RECEIVING result = lv_result EXCEPTIONS cntl_error = 1 error_no_gui = 2 wrong_parameter = 3 not_supported_by_gui = 4 OTHERS = 5. IF sy-subrc <> 0. CASE sy-subrc. WHEN 1. WHEN 2. WHEN OTHERS. ENDCASE. ENDIF. IF lv_result EQ 'X'. "如果存在则删除原始文件,重新覆盖 CALL METHOD cl_gui_frontend_services=>file_delete EXPORTING filename = lv_filename CHANGING rc = lv_subrc EXCEPTIONS file_delete_failed = 1 cntl_error = 2 error_no_gui = 3 file_not_found = 4 access_denied = 5 unknown_error = 6 not_supported_by_gui = 7 wrong_parameter = 8 OTHERS = 9. IF sy-subrc <> 0. CASE sy-subrc. WHEN 1. WHEN 2. WHEN OTHERS. ENDCASE. ENDIF. IF lv_subrc <> 0. "如果删除失败,则报错。 CONCATENATE '同名EXCEL文件已打开' '请关闭该EXCEL后重试。' INTO lv_errtxt. MESSAGE lv_errtxt TYPE 'E'. ENDIF. ENDIF. lv_destination = pr_filename. *&---下载模版。 CALL FUNCTION 'DOWNLOAD_WEB_OBJECT' EXPORTING KEY = lv_objdata destination = lv_destination IMPORTING rc = lv_rc. IF lv_rc NE 0. CONCATENATE '模板文件' '下载失败' INTO lv_errtxt. MESSAGE lv_errtxt TYPE 'E'. ENDIF. ENDFORM. "frm_down
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。