ABAP采购申请批量导入

    技术2022-06-25  81

    *---------------------------------------------------------------------* * Program ID : ZMM_IF0001 * * Program Name : 采购申请批量导入 * *---------------------------------------------------------------------* * Date Created : <2008/05/15> * * Created By : <*** > * * * * Description : <采购申请批量导入> * *---------------------------------------------------------------------* * Modification : * * Date Modified Modified By Description * * <YYYY/MM/DD> <修改人> <更新理由/内容> * * 2009/05/12 *** 增加上载字段,并考虑项目类别为Q的情况 * * 2009/05/15 *** 增加权限检查 * *---------------------------------------------------------------------* REPORT ZMM_IF0001. INCLUDE Z_AUTHORITY_CHECK. *&----------------------------------------------------------------------- *& 数据类型定义 *&----------------------------------------------------------------------- *定义输入的申请单结构 TYPES: BEGIN OF TYP_REQITEMS, AFNAM TYPE EBAN-AFNAM, "申请者 BSART TYPE EBAN-BSART, "凭证类型 PSTYP TYPE EBAN-PSTYP, "采购凭证中的项目类别 KNTTP TYPE EBAN-KNTTP, "科目设置类型 MATNR TYPE EBAN-MATNR, "物料号码 TXZ01 TYPE EBAN-TXZ01, "物料描述(增加) MENGE TYPE EBAN-MENGE, "数量 MEINS TYPE EBAN-MEINS, "物料单位 LFDAT TYPE EBAN-LFDAT, "需求交货日期 ELPEI TYPE EBAN-LPEIN, "交货日的类别 MATKL TYPE EBAN-MATKL, "物料组(增加) WERKS TYPE EBAN-WERKS, "申请工厂 LGORT TYPE EBAN-LGORT, "库存地点 EKGRP TYPE EBAN-EKGRP, "采购组 SAKTO TYPE EBKN-SAKTO, "总帐科目(增加) PS_PSP_PNR TYPE WBS_ELEM, "WBS 元素 PREIS TYPE EBAN-PREIS, "评估价格(增加) * LINE TYPE I, "行号,用来在出错时告诉在第几行有错误 END OF TYP_REQITEMS. **定义输入的申请单结构(用于排序) *TYPES: * BEGIN OF TYP_REQITEMS_SORT, * PS_PSP_PNR TYPE WBS_ELEM, "WBS 元素 * AFNAM TYPE EBAN-AFNAM, "申请者 * BSART TYPE EBAN-BSART, "凭证类型 * PSTYP TYPE EBAN-PSTYP, "采购凭证中的项目类别 * KNTTP TYPE EBAN-KNTTP, "科目设置类型 * MATNR TYPE EBAN-MATNR, "物料号码 * MENGE TYPE EBAN-MENGE, "数量 * MEINS TYPE EBAN-MEINS, "物料单位 * LFDAT TYPE EBAN-LFDAT, "需求交货日期 * ELPEI TYPE EBAN-LPEIN, "交货日的类别 * WERKS TYPE EBAN-WERKS, "申请工厂 * LGORT TYPE EBAN-LGORT, "库存地点 ** LINE TYPE I, "行号,用来在出错时告诉在第几行有错误 * END OF TYP_REQITEMS_SORT. *保存输出的错误信息 TYPES: BEGIN OF TYP_OUTPUT_FAIL, FILENAME TYPE STRING, MESSAGE LIKE BAPIRETURN-MESSAGE, END OF TYP_OUTPUT_FAIL. * 测试运行成功后ALV输出的信息 TYPES: BEGIN OF TYP_SUCC_OUTPUT, BANFN TYPE EBAN-BANFN, "采购申请号 BNFPO TYPE EBAN-BNFPO, "采购申请的项目编号 AFNAM TYPE EBAN-AFNAM, "申请者 BSART TYPE EBAN-BSART, "凭证类型 PSTYP TYPE EBAN-PSTYP, "采购凭证中的项目类别 KNTTP TYPE EBAN-KNTTP, "科目设置类型 MATNR TYPE EBAN-MATNR, "物料号码 TXZ01 TYPE EBAN-TXZ01, "物料描述(增加) MENGE TYPE EBAN-MENGE, "数量 MEINS TYPE EBAN-MEINS, "物料单位 LFDAT TYPE EBAN-LFDAT, "需求交货日期 ELPEI TYPE EBAN-LPEIN, "交货日的类别 MATKL TYPE EBAN-MATKL, "物料组(增加) WERKS TYPE EBAN-WERKS, "申请工厂 LGORT TYPE EBAN-LGORT, "库存地点 EKGRP TYPE EBAN-EKGRP, "采购组 SAKTO TYPE EBKN-SAKTO, "总帐科目(增加) PS_PSP_PNR TYPE WBS_ELEM, "WBS 元素 PREIS TYPE EBAN-PREIS, "评估价格(增加) FLAG TYPE STRING, "判断该记录成功导入,错误,还是未导入 END OF TYP_SUCC_OUTPUT. *定义写入BAPI的数据 TYPES: BEGIN OF TYP_BAPI_PARA. TYPES: ITEM TYPE BAPIEBANC OCCURS 0."传输结构:创建需求条目 TYPES: ACCOUNT TYPE BAPIEBKN OCCURS 0. "传输结构:创建/显示申请帐目分配 TYPES: END OF TYP_BAPI_PARA. *&----------------------------------------------------------------------- *& 透明表定义 *&----------------------------------------------------------------------- TABLES: RLGRAP, EBAN. "采购申请 *&----------------------------------------------------------------------- *& 全局变量定义 *&----------------------------------------------------------------------- DATA: G_SOURCEFILE_NAME TYPE RLGRAP-FILENAME. "源文件的路径 DATA: G_ERRLOG_NAME TYPE RLGRAP-FILENAME. "错误日志的路径 *DATA: G_COMMIT_RETURN TYPE BAPIRET2. "数据写入数据库时的返回信息. DATA: G_IS_FAIL(1) TYPE C. "如果导入失败 DATA: G_HAS_FAIL(1) TYPE C. DATA: L_SUBRC LIKE SY-SUBRC. DATA: L_TEXT(20) TYPE C. DATA: G_FLAG_AUTH TYPE C. *&----------------------------------------------------------------------- *& 内表定义 *&----------------------------------------------------------------------- DATA: TAB_REQITEMS TYPE STANDARD TABLE OF TYP_REQITEMS. DATA: TAB_OUTPUT_FAIL TYPE STANDARD TABLE OF TYP_OUTPUT_FAIL. DATA: TAB_SUCC_OUTPUT TYPE STANDARD TABLE OF TYP_SUCC_OUTPUT. *DATA: TAB_REQUISITION_ITEMS TYPE STANDARD TABLE OF BAPIEBANC. "传输结构:创建需求条目 *DATA: TAB_REQUISITION_ACCOUNT TYPE STANDARD TABLE OF BAPIEBKN. "传输结构:创建/显示申请帐目分配 DATA: TAB_BAPI_PARA TYPE STANDARD TABLE OF TYP_BAPI_PARA. "传入BAPI的数据 DATA: TAB_ERR_RETURN TYPE STANDARD TABLE OF BAPIRETURN. "创建申请单失败返回的错误信息 *&----------------------------------------------------------------------- *& 结构体定义 *&----------------------------------------------------------------------- DATA: REC_REQITEMS TYPE TYP_REQITEMS. DATA: REC_OUTPUT_FAIL TYPE TYP_OUTPUT_FAIL. DATA: REC_SUCC_OUTPUT TYPE TYP_SUCC_OUTPUT. *DATA: REC_REQUISITION_ITEMS TYPE BAPIEBANC. "传输结构:创建需求条目 *DATA: REC_REQUISITION_ACCOUNT TYPE BAPIEBKN. "传输结构:创建/显示申请帐目分配 DATA: REC_BAPI_PARA TYPE TYP_BAPI_PARA. "传入BAPI的数据 DATA: REC_ERR_RETURN TYPE BAPIRETURN. *&----------------------------------------------------------------------- *& SELECT SCREEN *& 允许用户输入多个文件进行导入 *&----------------------------------------------------------------------- SELECT-OPTIONS P_PATH FOR RLGRAP-FILENAME NO INTERVALS OBLIGATORY. PARAMETERS P_ERRLOG LIKE RLGRAP-FILENAME DEFAULT 'c:/resb/errlog.txt'. *SELECTION-SCREEN SKIP. *PARAMETERS P_TEST AS CHECKBOX DEFAULT 'X'. AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_PATH-LOW. * 从本机取文件 CALL FUNCTION 'F4_FILENAME' EXPORTING FIELD_NAME = 'P_PATH' IMPORTING FILE_NAME = P_PATH-LOW. AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_ERRLOG. * 从本机取文件 CALL FUNCTION 'F4_FILENAME' EXPORTING FIELD_NAME = 'P_ERRLOG' IMPORTING FILE_NAME = P_ERRLOG. *&--------------------------------------------------------------------- *& INITIALIZATION *&--------------------------------------------------------------------- CLEAR G_IS_FAIL. CLEAR G_HAS_FAIL. CLEAR TAB_SUCC_OUTPUT. CLEAR TAB_OUTPUT_FAIL. *&--------------------------------------------------------------------- *& AT SELECTION-SCREEN *&--------------------------------------------------------------------- AT SELECTION-SCREEN. DATA G_FILE_EXIT(1) TYPE C. LOOP AT P_PATH. G_SOURCEFILE_NAME = P_PATH-LOW. IF STRLEN( G_SOURCEFILE_NAME ) > 128. MESSAGE '文件路径过长,请缩短文件路径' TYPE 'I'. STOP. ENDIF. *检查文件是否存在 CALL FUNCTION 'WS_QUERY' EXPORTING * ENVIRONMENT = FILENAME = G_SOURCEFILE_NAME QUERY = 'FE' * WINID = IMPORTING RETURN = G_FILE_EXIT * EXCEPTIONS * INV_QUERY = 1 * NO_BATCH = 2 * FRONTEND_ERROR = 3 * OTHERS = 4 . * IF SY-SUBRC <> 0. ** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO ** WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. * ENDIF. ENDLOOP. IF G_FILE_EXIT = '0'. MESSAGE '文件不存在,请检查文件名' TYPE 'E'. EXIT. CLEAR G_FILE_EXIT. ENDIF. *&----------------------------------------------------------------------- *& START-OF-SELECTION *&----------------------------------------------------------------------- START-OF-SELECTION. *生成错误日志文件的表头信息 REC_OUTPUT_FAIL-FILENAME = '产生错误的文件'. REC_OUTPUT_FAIL-MESSAGE = '错误原因'. APPEND REC_OUTPUT_FAIL TO TAB_OUTPUT_FAIL. LOOP AT P_PATH. G_SOURCEFILE_NAME = P_PATH-LOW. *从文件中读取数据 PERFORM READ_FROM_FILE. IF G_FLAG_AUTH = 'X'. MESSAGE ID 'ZMM001' TYPE 'S' NUMBER 008 WITH G_SOURCEFILE_NAME. RETURN. ENDIF. *根据读取的数据创建申请单 PERFORM CREAT_REQORDER. ENDLOOP. *输出成功导入的采购申请订单 PERFORM OUTPUT_SUCC_INFO. END-OF-SELECTION. *&---------------------------------------------------------------------* *& Form READ_FROM_FILE *&---------------------------------------------------------------------* * 通过ALSM_EXCEL_TO_INTERNAL_TABLE将文件读入内表 *----------------------------------------------------------------------* FORM READ_FROM_FILE. DATA : L_INTERN TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE. DATA : L_INDEX TYPE I. FIELD-SYMBOLS : <FIELD>. CLEAR TAB_REQITEMS. CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE' EXPORTING FILENAME = G_SOURCEFILE_NAME I_BEGIN_COL = '1' I_BEGIN_ROW = '1' I_END_COL = '256' I_END_ROW = '65536' TABLES INTERN = L_INTERN EXCEPTIONS INCONSISTENT_PARAMETERS = 1 UPLOAD_OLE = 2 OTHERS = 3. IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE 'I' NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. *生成内表 SORT L_INTERN BY ROW COL. LOOP AT L_INTERN. IF L_INTERN-COL <= 17. MOVE L_INTERN-COL TO L_INDEX. ASSIGN COMPONENT L_INDEX OF STRUCTURE REC_REQITEMS TO <FIELD>. MOVE L_INTERN-VALUE TO <FIELD> . ENDIF. AT END OF ROW. * REC_REQITEMS-LINE = L_INTERN-ROW. * APPEND REC_REQITEMS TO TAB_REQITEMS. * CLEAR REC_REQITEMS. * 权限检查 * 如果没有权限导入,则将该条数据删除 * 如果有权限,则将其添加入内表 PERFORM F_AUTHORIZATION_CHECK USING SPACE REC_REQITEMS-WERKS SPACE SPACE SPACE SPACE CHANGING L_SUBRC L_TEXT. IF L_SUBRC = 0. APPEND REC_REQITEMS TO TAB_REQITEMS. CLEAR REC_REQITEMS. ENDIF. ENDAT. ENDLOOP. *如果没有数据添加到内表中,则输出消息提示没有权限 CLEAR G_FLAG_AUTH. IF TAB_REQITEMS IS INITIAL. G_FLAG_AUTH = 'X'. RETURN. ENDIF. ENDFORM. "READ_FROM_FILE *&---------------------------------------------------------------------* *& Form CREAT_REQORDER *&---------------------------------------------------------------------* * 先将读入内表的数据整理成BAPI对应的格式 * 数据处理结束,再调用BAPI将数据生成申请单 *----------------------------------------------------------------------* FORM CREAT_REQORDER. *处理文件中的数据,将读入内表的数据字段对应BAPI的字段 PERFORM PROCESS_FILEDATA. *调用BAPI将内表中的数据写入数据字典 PERFORM CALL_BAPI. ENDFORM. "CREAT_REQORDER *&---------------------------------------------------------------------* *& Form PROCESS_FILEDATA *&---------------------------------------------------------------------* * (将内表中的数据放入BAPI的表中 * 若字段‘WBS元素’内容不同,则每一WBS元素生成一个采购申请 * 因此在AT END PS_PSP_PNR时生成一个新的采购申请单, * 每个申请单的行项目号在处理数据时自动添加) * 此时只考虑整个文件的行项目都属于一张采购申请单的情况 *----------------------------------------------------------------------* FORM PROCESS_FILEDATA. * DATA: TAB_REQITEMS_SORT TYPE STANDARD TABLE OF TYP_REQITEMS_SORT. * DATA: REC_REQITEMS_SORT TYPE TYP_REQITEMS_SORT. DATA: LV_ITEM_NO LIKE BAPIEBANC-PREQ_ITEM. "采购申请的项目编号 DATA: REC_BAPI_ITEM TYPE BAPIEBANC. DATA: REC_BAPI_ACCOUNT TYPE BAPIEBKN. * DATA: REC_REQITEMS_TEMP LIKE TYP_REQITEMS. CLEAR REC_BAPI_PARA. CLEAR TAB_BAPI_PARA. * CLEAR REC_REQITEMS_SORT. LV_ITEM_NO = 1. * LOOP AT TAB_REQITEMS INTO REC_REQITEMS. * MOVE-CORRESPONDING REC_REQITEMS TO REC_REQITEMS_SORT. * APPEND REC_REQITEMS_SORT TO TAB_REQITEMS_SORT. * ENDLOOP. * SORT TAB_REQITEMS_SORT."上载的文件已经针对wbs排序过,不需要再对内表进行排序 * LOOP AT TAB_REQITEMS_SORT INTO REC_REQITEMS_SORT. LOOP AT TAB_REQITEMS INTO REC_REQITEMS. *在物料号前面补零 CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING INPUT = REC_REQITEMS-MATNR IMPORTING OUTPUT = REC_REQITEMS-MATNR. **在WBS元素前补零 * CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' * EXPORTING * INPUT = REC_REQITEMS-PS_PSP_PNR * IMPORTING * OUTPUT = REC_REQITEMS-PS_PSP_PNR * . *将WBS元素序列化 CALL FUNCTION 'CONVERSION_EXIT_ABPSN_INPUT' EXPORTING INPUT = REC_REQITEMS-PS_PSP_PNR IMPORTING OUTPUT = REC_REQITEMS-PS_PSP_PNR. * REC_REQITEMS_TEMP = REC_REQITEMS. REC_BAPI_ITEM-PREQ_ITEM = LV_ITEM_NO. REC_BAPI_ITEM-PREQ_NAME = REC_REQITEMS-AFNAM. REC_BAPI_ITEM-PUR_GROUP = REC_REQITEMS-EKGRP. REC_BAPI_ITEM-DOC_TYPE = REC_REQITEMS-BSART. REC_BAPI_ITEM-ITEM_CAT = REC_REQITEMS-PSTYP. REC_BAPI_ITEM-ACCTASSCAT = REC_REQITEMS-KNTTP. REC_BAPI_ITEM-MATERIAL = REC_REQITEMS-MATNR. REC_BAPI_ITEM-SHORT_TEXT = REC_REQITEMS-TXZ01. REC_BAPI_ITEM-QUANTITY = REC_REQITEMS-MENGE. REC_BAPI_ITEM-UNIT = REC_REQITEMS-MEINS. REC_BAPI_ITEM-DELIV_DATE = REC_REQITEMS-LFDAT. REC_BAPI_ITEM-DEL_DATCAT_EXT = REC_REQITEMS-ELPEI. REC_BAPI_ITEM-MAT_GRP = REC_REQITEMS-MATKL. REC_BAPI_ITEM-PLANT = REC_REQITEMS-WERKS. REC_BAPI_ITEM-STORE_LOC = REC_REQITEMS-LGORT. REC_BAPI_ITEM-PUR_GROUP = REC_REQITEMS-EKGRP. REC_BAPI_ITEM-C_AMT_BAPI = REC_REQITEMS-PREIS. * REC_REQUISITION_ACCOUNT-PREQ_NO = REC_REQUISITION_ITEMS-PREQ_NO. * REC_REQUISITION_ACCOUNT-PREQ_ITEM = REC_REQUISITION_ITEMS-PREQ_ITEM. REC_BAPI_ACCOUNT-PREQ_ITEM = LV_ITEM_NO. REC_BAPI_ACCOUNT-WBS_ELEM_E = REC_REQITEMS-PS_PSP_PNR. REC_BAPI_ACCOUNT-G_L_ACCT = REC_REQITEMS-SAKTO. LV_ITEM_NO = LV_ITEM_NO + 1. IF REC_REQITEMS-PSTYP = 'Q'. CLEAR REC_BAPI_ITEM-SHORT_TEXT. CLEAR REC_BAPI_ITEM-MAT_GRP. CLEAR REC_BAPI_ACCOUNT-COST_CTR. ENDIF. APPEND REC_BAPI_ITEM TO REC_BAPI_PARA-ITEM. APPEND REC_BAPI_ACCOUNT TO REC_BAPI_PARA-ACCOUNT. **一条新的申请单 * AT END OF PS_PSP_PNR. * LV_ITEM_NO = 1. * APPEND REC_BAPI_PARA TO TAB_BAPI_PARA. * CLEAR REC_BAPI_PARA. * ENDAT. ENDLOOP. APPEND REC_BAPI_PARA TO TAB_BAPI_PARA. ENDFORM. "PROCESS_FILEDATA *&-------------------------------------------------------------------------------------* *& Form CALL_BAPI *&-------------------------------------------------------------------------------------* * 调用BAPI:BAPI_REQUISITION_CREATE创建申请单 * 如果成功,则用ALV输出运行过程中系统产生的信息 * 还需要将错误信息写入日志文件 *--------------------------------------------------------------------------------------* FORM CALL_BAPI. DATA: ORDER_NO TYPE EBAN-BANFN. "采购申请号 DATA: REC_BAPI_ITEM TYPE BAPIEBANC. DATA: REC_BAPI_ACCOUNT TYPE BAPIEBKN. CLEAR G_IS_FAIL. ****************************************************************************************** *循环内表TAB_BAPI_PARA,每条数据对应一张申请单(可能有多个行项目) *调用BAPI,如果返回了申请单号,表示创建成功了一张申请单, *需要将这条信息写入TAB_SUCC_OUTPUT,作为测试成功时的ALV显示 *如果返回单号为空,则表示该张申请单创建失败, *则从RETURN中读取错误信息,并写入错误日志 ******************************************************************************************* LOOP AT TAB_BAPI_PARA INTO REC_BAPI_PARA. *如果没有发生错误,则继续生成申请订单 * IF G_IS_FAIL IS INITIAL. CALL FUNCTION 'BAPI_REQUISITION_CREATE' EXPORTING SKIP_ITEMS_WITH_ERROR = '' "只要有一个行项目发生错误,则停止整个导入程序 * AUTOMATIC_SOURCE = 'X' IMPORTING NUMBER = ORDER_NO TABLES REQUISITION_ITEMS = REC_BAPI_PARA-ITEM REQUISITION_ACCOUNT_ASSIGNMENT = REC_BAPI_PARA-ACCOUNT * REQUISITION_ITEM_TEXT = * REQUISITION_LIMITS = * REQUISITION_CONTRACT_LIMITS = * REQUISITION_SERVICES = * REQUISITION_SRV_ACCASS_VALUES = RETURN = TAB_ERR_RETURN * REQUISITION_SERVICES_TEXT = * REQUISITION_ADDRDELIVERY = * EXTENSIONIN = . *ENDIF. **将每条申请单放入一个内表,用于ALV输出显示 * LOOP AT REC_BAPI_PARA-ITEM INTO REC_BAPI_ITEM. ** REC_SUCC_OUTPUT-BANFN = ORDER_NO. * REC_SUCC_OUTPUT-BNFPO = REC_BAPI_ITEM-PREQ_ITEM. * REC_SUCC_OUTPUT-AFNAM = REC_BAPI_ITEM-PREQ_NAME. * REC_SUCC_OUTPUT-BSART = REC_BAPI_ITEM-DOC_TYPE. * REC_SUCC_OUTPUT-PSTYP = REC_BAPI_ITEM-ITEM_CAT. * REC_SUCC_OUTPUT-KNTTP = REC_BAPI_ITEM-ACCTASSCAT. * REC_SUCC_OUTPUT-MATNR = REC_BAPI_ITEM-MATERIAL. * REC_SUCC_OUTPUT-MENGE = REC_BAPI_ITEM-QUANTITY. * REC_SUCC_OUTPUT-MEINS = REC_BAPI_ITEM-UNIT. * REC_SUCC_OUTPUT-LFDAT = REC_BAPI_ITEM-DELIV_DATE. * REC_SUCC_OUTPUT-ELPEI = REC_BAPI_ITEM-DEL_DATCAT_EXT. * REC_SUCC_OUTPUT-WERKS = REC_BAPI_ITEM-PLANT. * REC_SUCC_OUTPUT-LGORT = REC_BAPI_ITEM-STORE_LOC. * READ TABLE REC_BAPI_PARA-ACCOUNT WITH KEY PREQ_ITEM = REC_BAPI_ITEM-PREQ_ITEM INTO REC_BAPI_ACCOUNT. * REC_SUCC_OUTPUT-PS_PSP_PNR = REC_BAPI_ACCOUNT-WBS_ELEM. * * APPEND REC_SUCC_OUTPUT TO TAB_SUCC_OUTPUT. * ENDLOOP. *如果生成申请订单失败 IF ORDER_NO IS INITIAL. G_IS_FAIL = 'X'. G_HAS_FAIL = 'X'. ENDIF. *如果生成申请订单成功 IF NOT ORDER_NO IS INITIAL. LOOP AT REC_BAPI_PARA-ITEM INTO REC_BAPI_ITEM. REC_SUCC_OUTPUT-BANFN = ORDER_NO. REC_SUCC_OUTPUT-BNFPO = REC_BAPI_ITEM-PREQ_ITEM. REC_SUCC_OUTPUT-AFNAM = REC_BAPI_ITEM-PREQ_NAME. REC_SUCC_OUTPUT-EKGRP = REC_BAPI_ITEM-PUR_GROUP. REC_SUCC_OUTPUT-BSART = REC_BAPI_ITEM-DOC_TYPE. REC_SUCC_OUTPUT-PSTYP = REC_BAPI_ITEM-ITEM_CAT. REC_SUCC_OUTPUT-KNTTP = REC_BAPI_ITEM-ACCTASSCAT. REC_SUCC_OUTPUT-MATNR = REC_BAPI_ITEM-MATERIAL. REC_SUCC_OUTPUT-TXZ01 = REC_BAPI_ITEM-SHORT_TEXT. REC_SUCC_OUTPUT-MENGE = REC_BAPI_ITEM-QUANTITY. REC_SUCC_OUTPUT-MEINS = REC_BAPI_ITEM-UNIT. REC_SUCC_OUTPUT-LFDAT = REC_BAPI_ITEM-DELIV_DATE. REC_SUCC_OUTPUT-ELPEI = REC_BAPI_ITEM-DEL_DATCAT_EXT. REC_SUCC_OUTPUT-MATKL = REC_BAPI_ITEM-MAT_GRP. REC_SUCC_OUTPUT-WERKS = REC_BAPI_ITEM-PLANT. REC_SUCC_OUTPUT-LGORT = REC_BAPI_ITEM-STORE_LOC. REC_SUCC_OUTPUT-EKGRP = REC_BAPI_ITEM-PUR_GROUP. REC_SUCC_OUTPUT-PREIS = REC_BAPI_ITEM-C_AMT_BAPI. READ TABLE REC_BAPI_PARA-ACCOUNT WITH KEY PREQ_ITEM = REC_BAPI_ITEM-PREQ_ITEM INTO REC_BAPI_ACCOUNT. REC_SUCC_OUTPUT-PS_PSP_PNR = REC_BAPI_ACCOUNT-WBS_ELEM_E. REC_SUCC_OUTPUT-SAKTO = REC_BAPI_ACCOUNT-G_L_ACCT. APPEND REC_SUCC_OUTPUT TO TAB_SUCC_OUTPUT. ENDLOOP. **输出成功导入的采购申请订单 * PERFORM OUTPUT_SUCC_INFO. ENDIF. ENDLOOP. "创建申请订单结束 **如果是测试运行,并全部成功 * IF NOT P_TEST IS INITIAL AND G_IS_FAIL IS INITIAL. * * PERFORM OUTPUT_SUCC_INFO. * * ENDIF. **如果不是测试运行,并全部成功,则需要将申请单写入数据库 * IF P_TEST IS INITIAL AND G_IS_FAIL IS INITIAL. * ** CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' *** EXPORTING *** WAIT = ** IMPORTING ** RETURN = G_COMMIT_RETURN ** . * ** MESSAGE ID G_COMMIT_RETURN-ID TYPE G_COMMIT_RETURN-TYPE NUMBER G_COMMIT_RETURN-NUMBER ** WITH G_COMMIT_RETURN-MESSAGE_V1 G_COMMIT_RETURN-MESSAGE_V2 G_COMMIT_RETURN-MESSAGE_V3 G_COMMIT_RETURN-MESSAGE_V4. ** * ENDIF. *如果导入产生错误 IF G_IS_FAIL = 'X'. * WRITE : / '上载产生错误,请查看错误日志'. PERFORM PROCESS_ERROR. ENDIF. ENDFORM. "CALL_BAPI *&---------------------------------------------------------------------* *& Form PROCESS_ERROR *&---------------------------------------------------------------------* * 从TAB_ERR_RETURN表中读取错误信息,并写入日志 *----------------------------------------------------------------------* FORM PROCESS_ERROR. DATA: L_FILENAME TYPE STRING. "错误日志的文件名 DATA: L_ERRNAME TYPE STRING. "导入出错的文件的文件名 DATA: COMPLETE_FILENAME TYPE DBMSGORA-FILENAME. DATA: PURE_FILENAME TYPE SDBAH-ACTID. DATA: PURE_EXTENSION TYPE SDBAD-FUNCT. L_FILENAME = P_ERRLOG. COMPLETE_FILENAME = G_SOURCEFILE_NAME. ** 将文件路径转变成文件名 *CALL FUNCTION 'PC_SPLIT_COMPLETE_FILENAME' * EXPORTING * COMPLETE_FILENAME = COMPLETE_FILENAME ** CHECK_DOS_FORMAT = * IMPORTING ** DRIVE = ** EXTENSION = ** NAME = * NAME_WITH_EXT = L_ERRNAME ** PATH = * EXCEPTIONS * INVALID_DRIVE = 1 * INVALID_EXTENSION = 2 * INVALID_NAME = 3 * INVALID_PATH = 4 * OTHERS = 5 * . *IF SY-SUBRC <> 0. ** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO ** WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. *ENDIF. CALL FUNCTION 'SPLIT_FILENAME' EXPORTING LONG_FILENAME = COMPLETE_FILENAME IMPORTING PURE_FILENAME = PURE_FILENAME PURE_EXTENSION = PURE_EXTENSION. CONCATENATE PURE_FILENAME '.' PURE_EXTENSION INTO L_ERRNAME. REC_OUTPUT_FAIL-FILENAME = L_ERRNAME. LOOP AT TAB_ERR_RETURN INTO REC_ERR_RETURN. REC_OUTPUT_FAIL-MESSAGE = REC_ERR_RETURN-MESSAGE. ENDLOOP. *将错误信息添加到内表中 APPEND REC_OUTPUT_FAIL TO TAB_OUTPUT_FAIL. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING * BIN_FILESIZE = FILENAME = L_FILENAME * FILETYPE = 'ASC' APPEND = 'X' WRITE_FIELD_SEPARATOR = 'X' * HEADER = '00' * TRUNC_TRAILING_BLANKS = ' ' * WRITE_LF = 'X' * COL_SELECT = ' ' * COL_SELECT_MASK = ' ' * DAT_MODE = ' ' * CONFIRM_OVERWRITE = ' ' * NO_AUTH_CHECK = ' ' * CODEPAGE = ' ' * IGNORE_CERR = ABAP_TRUE * REPLACEMENT = '#' * WRITE_BOM = ' ' * TRUNC_TRAILING_BLANKS_EOL = 'X' * WK1_N_FORMAT = ' ' * WK1_N_SIZE = ' ' * WK1_T_FORMAT = ' ' * WK1_T_SIZE = ' ' * IMPORTING * FILELENGTH = TABLES DATA_TAB = TAB_OUTPUT_FAIL * FIELDNAMES = EXCEPTIONS FILE_WRITE_ERROR = 1 NO_BATCH = 2 GUI_REFUSE_FILETRANSFER = 3 INVALID_TYPE = 4 NO_AUTHORITY = 5 UNKNOWN_ERROR = 6 HEADER_NOT_ALLOWED = 7 SEPARATOR_NOT_ALLOWED = 8 FILESIZE_NOT_ALLOWED = 9 HEADER_TOO_LONG = 10 DP_ERROR_CREATE = 11 DP_ERROR_SEND = 12 DP_ERROR_WRITE = 13 UNKNOWN_DP_ERROR = 14 ACCESS_DENIED = 15 DP_OUT_OF_MEMORY = 16 DISK_FULL = 17 DP_TIMEOUT = 18 FILE_NOT_FOUND = 19 DATAPROVIDER_EXCEPTION = 20 CONTROL_FLUSH_ERROR = 21 OTHERS = 22 . IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDFORM. "PROCESS_ERROR *&---------------------------------------------------------------------* *& Form OUTPUT_SUCC_INFO *&---------------------------------------------------------------------* * 用ALV格式显示测试运行中成功生成的申请单 *----------------------------------------------------------------------* FORM OUTPUT_SUCC_INFO. DATA: G_TITLE TYPE LVC_TITLE. IF G_HAS_FAIL IS INITIAL. G_TITLE = '成功导入以下采购申请单'. ELSE. G_TITLE = '成功导入以下采购申请单,但是有错误,请查看错误日志'. ENDIF. ************************************************************************ *生成alv 框架 ************************************************************************ *定义一个显示的内表的一行 TYPE-POOLS:SLIS. DATA: L_ALV_FILED TYPE SLIS_FIELDCAT_ALV. DATA: LAYOUT TYPE SLIS_LAYOUT_ALV. *定义显示的一列的框架 DATA: L_ALV_FILEDCAT TYPE SLIS_T_FIELDCAT_ALV. CLEAR L_ALV_FILED. *定义第一列的属性 *定义显示位置,显示的字段.在下面传递内表中的名字一样的值 *显示标题名字 L_ALV_FILED-COL_POS = 1. L_ALV_FILED-FIELDNAME = 'BANFN'. L_ALV_FILED-SELTEXT_M = '采购申请号'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 2. L_ALV_FILED-FIELDNAME = 'BNFPO'. L_ALV_FILED-SELTEXT_M = '项目编号'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 3. L_ALV_FILED-FIELDNAME = 'AFNAM'. L_ALV_FILED-SELTEXT_M = '申请者'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 4. L_ALV_FILED-FIELDNAME = 'BSART'. L_ALV_FILED-SELTEXT_M = '凭证类型'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 5. L_ALV_FILED-FIELDNAME = 'PSTYP'. L_ALV_FILED-SELTEXT_M = '采购凭证中的项目类别'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 6. L_ALV_FILED-FIELDNAME = 'KNTTP'. L_ALV_FILED-SELTEXT_M = '科目设置类型'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 7. L_ALV_FILED-FIELDNAME = 'MATNR'. L_ALV_FILED-SELTEXT_M = '物料号码'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 8. L_ALV_FILED-FIELDNAME = 'TXZ01'. L_ALV_FILED-SELTEXT_M = '物料描述'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 9. L_ALV_FILED-FIELDNAME = 'MENGE'. L_ALV_FILED-SELTEXT_M = '数量'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 10. L_ALV_FILED-FIELDNAME = 'MEINS'. L_ALV_FILED-SELTEXT_M = '物料单位'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 11. L_ALV_FILED-FIELDNAME = 'LFDAT'. L_ALV_FILED-SELTEXT_M = '需求交货日期'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 12. L_ALV_FILED-FIELDNAME = 'ELPEI'. L_ALV_FILED-SELTEXT_M = '交货日的类别'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 13. L_ALV_FILED-FIELDNAME = 'MATKL'. L_ALV_FILED-SELTEXT_M = '物料组'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 14. L_ALV_FILED-FIELDNAME = 'BSART'. L_ALV_FILED-SELTEXT_M = '申请工厂'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 15. L_ALV_FILED-FIELDNAME = 'LGORT'. L_ALV_FILED-SELTEXT_M = '库存地点'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 16. L_ALV_FILED-FIELDNAME = 'EKGRP'. L_ALV_FILED-SELTEXT_M = '采购组'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 17. L_ALV_FILED-FIELDNAME = 'SAKTO'. L_ALV_FILED-SELTEXT_M = '总帐科目'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 18. L_ALV_FILED-FIELDNAME = 'PS_PSP_PNR'. L_ALV_FILED-SELTEXT_M = 'WBS 元素'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. L_ALV_FILED-COL_POS = 19. L_ALV_FILED-FIELDNAME = 'PREIS'. L_ALV_FILED-SELTEXT_M = '评估价格'. APPEND L_ALV_FILED TO L_ALV_FILEDCAT. *列宽度自动根据内容优化 LAYOUT-COLWIDTH_OPTIMIZE = 'X'. *&---------------------------------------------------------------------* *&输出ALV表 *&---------------------------------------------------------------------* CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING I_INTERFACE_CHECK = '' I_CALLBACK_PROGRAM = SY-REPID IS_LAYOUT = LAYOUT I_GRID_TITLE = G_TITLE * I_CALLBACK_PF_STATUS_SET = 'MY_STATUS' "自定义工具栏 * I_CALLBACK_USER_COMMAND = 'MY_COMMAND' "自定义工具栏的事件 IT_FIELDCAT = L_ALV_FILEDCAT TABLES T_OUTTAB = TAB_SUCC_OUTPUT. ENDFORM. "OUTPUT_SUCC_INFO


    最新回复(0)