独立需求
*&---------------------------------------------------------------------* *& Transcation Code : ZPPC014 * *& Program Name : ZPPC014 * *& Module Name : PP * *& Sub-Module : * *& Author : linyu * *& Create Date : 2012/04/18 * *& Responsible : * *& Program Type : Other * *& Description : 计划独立需求导入程序 * *& SAP Release : 7.2 * *&---------------------------------------------------------------------* *& REVISION LOG * *& * *& LOG# DATE AUTHOR DESCRIPTION * *& ---- ---- ------ ----------- * *& 0001 12/04/18 zengql Created * REPORT ZPPC014. *存放导入的数据 DATA: BEGIN OF GT_UPLOAD OCCURS 0, PBDNR LIKE AM60X-PBDNR, "需求计划编号 WERKS LIKE AM60X-WERKS, "工厂 " VERSB LIKE AM60X-VERSB, "版本号 DATVE LIKE RM60X-DATVE, "计划开始日期 DATBE LIKE RM60X-DATBE, "计划截至日期 ENTLU LIKE RM60X-ENTLU, "计划周期 MATNR LIKE PBPT-MATNR, "产品编码 WERKS1 LIKE PBPT-WERKS, "工厂 VERSB1 LIKE PBPT-VERSB, "版本号 PLN01 LIKE RM60X-PLN01, "第一个月 PLN02 LIKE RM60X-PLN02, "第二个月 PLN03 LIKE RM60X-PLN03, "第三个月 PLN04 LIKE RM60X-PLN04, "第四个月 END OF GT_UPLOAD. *bapi data DATA: REQ_ITEM LIKE BAPISITEMR. DATA: REQ_DATA LIKE BAPISSHDIN OCCURS 0 WITH HEADER LINE. DATA: RETURN LIKE BAPIRETURN1 OCCURS 0 WITH HEADER LINE. DATA: MATERTEMP LIKE BAPISITEMR-MATERIAL. DATA: MATEMP LIKE BAPISITEMR-MATERIAL. "物料编码 DATA: BEGIN OF LOG OCCURS 0, INDEX TYPE I, "序号 MESSAGE LIKE RETURN-MESSAGE, "消息文本 END OF LOG. PARAMETERS: F_FILE LIKE RLGRAP-FILENAME DEFAULT 'C:\DATA.XLS' MEMORY ID F_FILE VISIBLE LENGTH 30 OBLIGATORY. PARAMETERS: LOGFILE LIKE RLGRAP-FILENAME DEFAULT 'c:\log.txt' OBLIGATORY. ***------------------------------------------------------------------------------------*** *** SELECTION-SCREEN ON VALUE-REQUEST ***------------------------------------------------------------------------------------*** AT SELECTION-SCREEN ON VALUE-REQUEST FOR F_FILE . PERFORM WS_FILENAME_GET USING F_FILE. " 打开本地文件的帮助 START-OF-SELECTION. PERFORM UPLOAD_FILE . PERFORM UPDATE_DATA . *&---------------------------------------------------------------------* *& Form UPLOAD_FILE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM UPLOAD_FILE. * UPLOAD FILE FROM TXT TO INT_MAT. DATA: BEGIN OF UP_EXCLE OCCURS 0. INCLUDE STRUCTURE KCDE_CELLS. DATA: END OF UP_EXCLE. CALL FUNCTION 'KCD_EXCEL_OLE_TO_INT_CONVERT' EXPORTING FILENAME = F_FILE I_BEGIN_COL = 1 I_BEGIN_ROW = 2 I_END_COL = 50 I_END_ROW = 65000 TABLES INTERN = UP_EXCLE EXCEPTIONS INCONSISTENT_PARAMETERS = 1 UPLOAD_OLE = 2 OTHERS = 3. CASE SY-SUBRC . WHEN '1' OR '2' OR '3'. MESSAGE I398(00) WITH F_FILE. WHEN OTHERS. FORMAT COLOR COL_GROUP. WRITE : '上传成功!'. ENDCASE.. FIELD-SYMBOLS: <F>. DATA:INDEX TYPE I. DATA: TYP. LOOP AT UP_EXCLE. INDEX = UP_EXCLE-COL. ASSIGN COMPONENT INDEX OF STRUCTURE GT_UPLOAD TO <F>. IF SY-SUBRC = 0. DESCRIBE FIELD <F> TYPE TYP. CASE TYP. WHEN 'C'. <F> = UP_EXCLE-VALUE. CONDENSE <F>. WHEN 'I' "数字串 OR 's' OR 'b' OR 'N' OR 'F' OR 'P'. CATCH SYSTEM-EXCEPTIONS ARITHMETIC_ERRORS = 5 CONVERSION_ERRORS = 6 BCD_OVERFLOW = 7 BCD_FIELD_OVERFLOW = 8 . <F> = UP_EXCLE-VALUE. ENDCATCH. IF SY-SUBRC <> 0. MESSAGE E000(SU) WITH '数据转化错误!'. ENDIF. WHEN OTHERS. <F> = UP_EXCLE-VALUE. ENDCASE. ENDIF. AT END OF ROW. IF GT_UPLOAD-PBDNR NE SPACE. APPEND GT_UPLOAD. CLEAR GT_UPLOAD. ENDIF. ENDAT. ENDLOOP. ENDFORM. "UPLOAD_FILE *&---------------------------------------------------------------------* *& Form UPDATE_DATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM UPDATE_DATA . DATA: LV_INDEX TYPE I. DATA: LV_DATE TYPE SY-DATUM. DATA: LV_LAST_DAY TYPE SY-DATUM. "月末最后一天 DATA: LV_FIRST_DAY TYPE SY-DATUM. "下月第一天 DATA: LV_MATNR TYPE MATNR. DATA: LV_DISGR TYPE DISGR, LV_STRGR TYPE STRGR, LV_BEDVP TYPE BEDVP. LOOP AT GT_UPLOAD. CLEAR: LV_DATE,LV_LAST_DAY,LV_FIRST_DAY. LV_INDEX = LV_INDEX + 1. CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING INPUT = GT_UPLOAD-MATNR IMPORTING OUTPUT = REQ_ITEM-MATERIAL. * CONCATENATE '00000000000' GT_UPLOAD-MATNR INTO MATERTEMP. * REQ_ITEM-MATERIAL = MATERTEMP. REQ_ITEM-PLANT = GT_UPLOAD-WERKS. SELECT SINGLE DISGR STRGR FROM MARC INTO (LV_DISGR,LV_STRGR) WHERE MATNR = REQ_ITEM-MATERIAL AND WERKS = REQ_ITEM-PLANT. IF SY-SUBRC = 0. IF LV_STRGR IS INITIAL. SELECT SINGLE STRGR INTO LV_STRGR FROM T438M WHERE WERKS = REQ_ITEM-PLANT AND MTART = LV_DISGR. ENDIF. SELECT SINGLE BEDVP INTO LV_BEDVP FROM T461S WHERE STRA1 = LV_STRGR. ENDIF. IF LV_BEDVP IS INITIAL. LOG-INDEX = LV_INDEX. CONCATENATE '请维护物料' GT_UPLOAD-MATNR '的MRP组!' into LOG-MESSAGE. APPEND LOG. continue. ELSE. ENDIF. * req_item-REQU_TYPE = 'VSE'. REQ_ITEM-VERSION = GT_UPLOAD-VERSB. REQ_ITEM-VERS_ACTIV = 'X'. REQ_ITEM-REQ_NUMBER = GT_UPLOAD-PBDNR. LV_DATE = GT_UPLOAD-DATVE. REQ_DATA-DATE_TYPE = 3. REQ_DATA-REQ_DATE = LV_DATE. REQ_DATA-REQ_QTY = GT_UPLOAD-PLN01. APPEND REQ_DATA. CLEAR REQ_DATA. REQ_DATA-DATE_TYPE = 3. CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS' EXPORTING DAY_IN = LV_DATE IMPORTING LAST_DAY_OF_MONTH = LV_LAST_DAY EXCEPTIONS DAY_IN_NO_DATE = 1 OTHERS = 2. IF SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. LV_FIRST_DAY = LV_LAST_DAY + 1. REQ_DATA-REQ_DATE = LV_FIRST_DAY. REQ_DATA-REQ_QTY = GT_UPLOAD-PLN02. APPEND REQ_DATA. CLEAR REQ_DATA. REQ_DATA-DATE_TYPE = 3. CLEAR LV_LAST_DAY. CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS' EXPORTING DAY_IN = LV_FIRST_DAY IMPORTING LAST_DAY_OF_MONTH = LV_LAST_DAY EXCEPTIONS DAY_IN_NO_DATE = 1 OTHERS = 2. IF SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. LV_FIRST_DAY = LV_LAST_DAY + 1. REQ_DATA-REQ_DATE = LV_FIRST_DAY. REQ_DATA-REQ_QTY = GT_UPLOAD-PLN03. APPEND REQ_DATA. CLEAR REQ_DATA. SELECT SINGLE MATNR FROM PBIM INTO LV_MATNR WHERE MATNR = REQ_ITEM-MATERIAL AND WERKS = GT_UPLOAD-WERKS AND BEDAE = LV_BEDVP AND VERSB = REQ_ITEM-VERSION AND PBDNR = REQ_ITEM-REQ_NUMBER. IF SY-SUBRC <> 0. CALL FUNCTION 'BAPI_REQUIREMENTS_CREATE' EXPORTING REQUIREMENTS_ITEM = REQ_ITEM * REQUIREMENT_PARAM = IMPORTING MATERIAL = MATEMP * PLANT = * REQUIREMENTSTYPE = * VERSION = * REQMTSPLANNUMBER = TABLES REQUIREMENTS_SCHEDULE_IN = REQ_DATA * REQUIREMENTS_CHAR_IN = RETURN = RETURN. IF NOT MATEMP IS INITIAL. WRITE:/(10) MATEMP COLOR 4, (50) '创建成功'. ELSE. LOG-INDEX = LV_INDEX. LOG-MESSAGE = RETURN-MESSAGE. APPEND LOG. ENDIF. ELSE. * CALL FUNCTION 'BAPI_REQUIREMENTS_CHANGE' * EXPORTING * MATERIAL = * PLANT = * REQUIREMENTSTYPE = * VERSION = * REQMTSPLANNUMBER = * VERS_ACTIV = ** REQUIREMENT_PARAM = ** MRP_AREA = ** DO_COMMIT = 'X' ** UPDATE_MODE = 'X' ** DELETE_OLD = 'X' ** NO_WITHDR = ' ' ** MATERIAL_EVG = ** IMPORTING ** REQUIREMENT_ITEM_OUT = * TABLES ** REQUIREMENTS_SCHEDULE_IN = ** REQUIREMENTS_CHAR_IN = * RETURN = * . CALL FUNCTION 'BAPI_REQUIREMENTS_CHANGE' EXPORTING MATERIAL = REQ_ITEM-MATERIAL PLANT = GT_UPLOAD-WERKS REQUIREMENTSTYPE = LV_BEDVP VERSION = REQ_ITEM-VERSION REQMTSPLANNUMBER = REQ_ITEM-REQ_NUMBER VERS_ACTIV = 'X' DELETE_OLD = '' TABLES REQUIREMENTS_SCHEDULE_IN = REQ_DATA * REQUIREMENTS_CHAR_IN = RETURN = RETURN . CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' * EXPORTING * WAIT = * IMPORTING * RETURN = . IF SY-SUBRC = 0. WRITE:/(10) REQ_ITEM-MATERIAL COLOR 4, (50) '更改成功'. ELSE. LOG-INDEX = LV_INDEX. LOG-MESSAGE = RETURN-MESSAGE. APPEND LOG. ENDIF. ENDIF. CLEAR MATEMP. REFRESH REQ_DATA. CLEAR MATERTEMP. CLEAR REQ_ITEM. ENDLOOP. CLEAR: LV_INDEX,LV_DATE,LV_LAST_DAY,LV_FIRST_DAY. CALL FUNCTION 'WS_DOWNLOAD' EXPORTING FILENAME = LOGFILE FILETYPE = 'DAT' TABLES DATA_TAB = LOG EXCEPTIONS INVALID_FILESIZE = 1 INVALID_TABLE_WIDTH = 2 INVALID_TYPE = 3 NO_BATCH = 4 UNKNOWN_ERROR = 5 GUI_REFUSE_FILETRANSFER = 6 CUSTOMER_ERROR = 7 OTHERS = 8. IF SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. IF LOG[] IS NOT INITIAL. WRITE: /2 '文件行号', 6 '消息'. * LOOP AT LOG. WRITE: /2 LOG-INDEX, 6 LOG-MESSAGE. ENDLOOP. ENDIF. WRITE : '批输入结束!导入详情请检查LOG文件!'. ENDFORM. "UPDATE_DATA *&---------------------------------------------------------------------* *& Form WS_FILENAME_GET *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_F_FILE text *----------------------------------------------------------------------* FORM WS_FILENAME_GET USING P_F_FILE LIKE RLGRAP-FILENAME. CALL FUNCTION 'WS_FILENAME_GET' EXPORTING DEF_FILENAME = P_F_FILE DEF_PATH = ' ' MASK = ',Excel File,*.xls.' MODE = 'O' TITLE = '选择文件' IMPORTING FILENAME = P_F_FILE EXCEPTIONS INV_WINSYS = 01 " rc = subrc NO_BATCH = 02 SELECTION_CANCEL = 03 SELECTION_ERROR = 04. IF SY-SUBRC <> 0 AND SY-SUBRC <> 3. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDFORM. " WS_FILENAME_GET